3#include <boost/python.hpp>
4#include <boost/python/numpy.hpp>
9namespace bp = boost::python;
10namespace np = boost::python::numpy;
13 PyGILState_STATE
state = PyGILState_Ensure();
14 ~PythonLock() { PyGILState_Release(
state); }
27 if (
coordinates.size2() != 3 || normals.size2() != 3 ||
30 "SDF coordinates and normals must both have shape (N,3)");
35 "Script %s has no callable %s",
file.c_str(),
name);
38 std::vector<np::ndarray> args;
39 for (
int d = 0;
d != 6; ++
d) {
40 auto a = np::empty(bp::make_tuple(
n), np::dtype::get_builtin<double>());
41 auto *p =
reinterpret_cast<double *
>(
a.get_data());
42 for (
size_t i = 0;
i !=
n; ++
i)
47 args[3], args[4], args[5], block);
48 auto a = np::from_object(
result, np::dtype::get_builtin<double>());
49 if (
a.get_nd() != (columns == 1 ? 1 : 2) ||
50 a.shape(0) !=
static_cast<Py_intptr_t
>(
n) ||
51 (columns != 1 &&
a.shape(1) != columns))
53 "Wrong result shape from %s in %s",
name,
file.c_str());
54 values.resize(
n, columns,
false);
55 for (
size_t i = 0;
i !=
n; ++
i)
56 for (
int d = 0;
d != columns; ++
d) {
59 a.get_data() +
i *
a.strides(0) +
60 (columns == 1 ? 0 :
d *
a.strides(1)),
62 if (!std::isfinite(
v))
64 "Non-finite result from %s in %s",
name,
file.c_str());
67 }
catch (
const bp::error_already_set &) {
70 "Python %s failed in %s",
name,
file.c_str());
92 bp::throw_error_already_set();
93 }
catch (
const bp::error_already_set &) {
96 "Could not initialise NumPy");
103 if (Py_IsInitialized() && Py_FinalizeEx() < 0)
105 "Python finalisation failed");
111 if (!Py_IsInitialized())
113 "Initialise Python/NumPy before loading an SDF");
116 auto next = std::make_unique<Impl>();
118 next->globals[
"__builtins__"] = bp::import(
"builtins");
119 next->globals[
"__file__"] = file;
120 next->globals[
"__name__"] =
"mofem_sdf";
121 bp::exec_file(file.c_str(), next->globals, next->globals);
122 if (!next->globals.has_key(
"sdf") ||
123 !PyCallable_Check(bp::object(next->globals[
"sdf"]).ptr()))
125 "Script %s has no callable sdf", file.c_str());
126 impl = std::move(next);
127 }
catch (
const bp::error_already_set &) {
130 "Could not load SDF script %s", file.c_str());
140 for (
const auto *
name : {
"grad_sdf",
"hess_sdf"})
142 !PyCallable_Check(bp::object(
impl->globals[
name]).ptr()))
144 "Contact requires %s in %s",
name,
impl->file.c_str());
157 for (
size_t i = 0;
i !=
values.size(); ++
i)
187 for (
int d = 0;
d != 3; ++
d) {
193 "SDF evaluation failed");
Vec t
Removal: the ring edge.
std::vector< std::array< double, 3 > > coordinates
Reusable vectorised Python signed-distance evaluator.
SmartPetscObj< Vec > values
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_OPERATION_UNSUCCESSFUL
@ MOFEM_DATA_INCONSISTENCY
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
FTensor::Index< 'i', SPACE_DIM > i
const double v
phase velocity of light in medium (cm/ns)
PetscErrorCode MoFEMErrorCode
MoFEM/PETSc error code.
implementation of Data Operators for Forces and Sources
MoFEMErrorCode evaluate(const char *name, int columns, double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, MatrixDouble &values) const
MoFEMErrorCode requireDerivatives() const
static MoFEMErrorCode initialise()
MoFEMErrorCode evalHessSdf(double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, MatrixDouble &values) const
MoFEMErrorCode sdfInit(const std::string &file)
double value(const std::array< double, 3 > &point, double dt=0, double t=0, int block=0) const
MoFEMErrorCode evalSdf(double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, VectorDouble &values) const
static MoFEMErrorCode finalise()
std::unique_ptr< Impl > impl
MoFEMErrorCode evalGradSdf(double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, MatrixDouble &values) const