Skip to content

Commit

Permalink
Wip
Browse files Browse the repository at this point in the history
Signed-off-by: Geoffroy Jamgotchian <[email protected]>
  • Loading branch information
geofjamg committed Nov 19, 2023
1 parent 1fb5e3c commit 8ef2b21
Show file tree
Hide file tree
Showing 12 changed files with 152 additions and 36 deletions.
16 changes: 14 additions & 2 deletions src/jniwrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,23 @@ namespace powsybl {

namespace jni {

void throwMatrixException(JNIEnv* env, const char* msg) {
jclass clazz = env->FindClass("com/powsybl/math/matrix/MatrixException");
void throwException(JNIEnv* env, const char* msg, const std::string& className) {
jclass clazz = env->FindClass(className.c_str());
env->ThrowNew(clazz, msg);
}

void throwMathException(JNIEnv* env, const char* msg) {
throwException(env, msg, "com/powsybl/math/MathException");
}

void throwMatrixException(JNIEnv* env, const char* msg) {
throwException(env, msg, "com/powsybl/math/matrix/MatrixException");
}

void throwSolverException(JNIEnv* env, const char* msg) {
throwException(env, msg, "com/powsybl/math/solver/SolverException");
}

} // namespace jni

} // namespace powsybl
2 changes: 2 additions & 0 deletions src/jniwrapper.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ class DoubleArray : public JniWrapper<jdoubleArray> {
mutable jdouble* _ptr;
};

void throwMathException(JNIEnv* env, const char* msg);
void throwMatrixException(JNIEnv* env, const char* msg);
void throwSolverException(JNIEnv* env, const char* msg);

} // namespace jni

Expand Down
16 changes: 0 additions & 16 deletions src/lu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,6 @@ JNIEXPORT void JNICALL Java_com_powsybl_math_matrix_SparseLUDecomposition_solve2
}
}

/*
* Class: com_powsybl_math_matrix_SparseMatrix
* Method: nativeInit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_powsybl_math_matrix_SparseMatrix_nativeInit(JNIEnv * env, jclass) {
try {
// lookup caching
powsybl::jni::ComPowsyblMathMatrixSparseMatrix::init(env);
} catch (const std::exception& e) {
powsybl::jni::throwMatrixException(env, e.what());
} catch (...) {
powsybl::jni::throwMatrixException(env, "Unknown exception");
}
}

/*
* Class: com_powsybl_math_matrix_SparseMatrix
* Method: times
Expand Down
33 changes: 33 additions & 0 deletions src/native.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* @file native.cpp
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/

#ifdef __cplusplus
extern "C" {
#endif

/*
* Class: com_powsybl_math_MathNative
* Method: nativeInit
* Signature: ()V
*/
JNIEXPORT void JNICALL Java_com_powsybl_math_MathNative_nativeInit(JNIEnv * env, jclass) {
try {
// lookup caching
powsybl::jni::ComPowsyblMathMatrixSparseMatrix::init(env);
} catch (const std::exception& e) {
powsybl::jni::throwMathException(env, e.what());
} catch (...) {
powsybl::jni::throwMathException(env, "Unknown exception");
}
}

#ifdef __cplusplus
}
#endif
2 changes: 1 addition & 1 deletion src/solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ namespace powsybl {
extern "C" {
#endif

JNIEXPORT void JNICALL Java_com_powsybl_math_solver_KinsolSolver_test(JNIEnv * env, jobject) {
JNIEXPORT void JNICALL Java_com_powsybl_math_solver_NewtonKrylovSolver_solve(JNIEnv * env, jobject, jobject) {
try {
std::cout << "start" << std::endl;

Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/powsybl/math/MathException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.math;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class MathException extends RuntimeException {

public MathException(String msg) {
super(msg);
}

public MathException(Throwable throwable) {
super(throwable);
}
}
34 changes: 34 additions & 0 deletions src/test/java/com/powsybl/math/MathNative.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.math;

import org.scijava.nativelib.NativeLoader;

import java.io.IOException;
import java.io.UncheckedIOException;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class MathNative {

private static boolean INIT = false;

private static native void nativeInit();

public static void init() {
if (!INIT) {
try {
NativeLoader.loadLibrary("math");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
init();
INIT = true;
}
}
}
14 changes: 2 additions & 12 deletions src/test/java/com/powsybl/math/matrix/SparseMatrix.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,15 @@
*/
package com.powsybl.math.matrix;

import org.scijava.nativelib.NativeLoader;

import java.io.IOException;
import java.io.UncheckedIOException;
import com.powsybl.math.MathNative;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class SparseMatrix {

private static native void nativeInit();

static {
try {
NativeLoader.loadLibrary("math");
} catch (IOException e) {
throw new UncheckedIOException(e);
}
nativeInit();
MathNative.init();
}

public SparseMatrix(int rowCount, int columnCount, int[] columnStart, int[] rowIndices, double[] values) {
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/com/powsybl/math/solver/NewtonKrylovSolver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.math.solver;

import com.powsybl.math.MathNative;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class NewtonKrylovSolver {

static {
MathNative.init();
}

public native void solve(NewtonKrylovSolverContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@
/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class KinsolSolver {

public native void test();
public class NewtonKrylovSolverContext {
}
21 changes: 21 additions & 0 deletions src/test/java/com/powsybl/math/solver/SolverException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Copyright (c) 2023, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package com.powsybl.math.solver;

/**
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
public class SolverException extends RuntimeException {

public SolverException(String msg) {
super(msg);
}

public SolverException(Throwable throwable) {
super(throwable);
}
}
4 changes: 2 additions & 2 deletions src/test/java/com/powsybl/mathnative/SolverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
package com.powsybl.mathnative;

import com.powsybl.math.matrix.SparseMatrix;
import com.powsybl.math.solver.KinsolSolver;
import com.powsybl.math.solver.NewtonKrylovSolver;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -18,7 +18,7 @@ class SolverTest {
@Test
void test() {
SparseMatrix m = new SparseMatrix(0, 0, new int[] {}, new int[] {}, new double[] {});
KinsolSolver solver = new KinsolSolver();
NewtonKrylovSolver solver = new NewtonKrylovSolver();
solver.test();
}
}

0 comments on commit 8ef2b21

Please sign in to comment.