v0.16.3
Loading...
Searching...
No Matches
Classes | Public Member Functions | Static Public Member Functions | Private Attributes | List of all members
MoFEM::SDFPython Struct Reference

#include "src/interfaces/SDFPython.hpp"

Collaboration diagram for MoFEM::SDFPython:
[legend]

Classes

struct  Impl
 

Public Member Functions

 SDFPython ()
 
 ~SDFPython ()
 
 SDFPython (const SDFPython &)=delete
 
SDFPython & operator= (const SDFPython &)=delete
 
MoFEMErrorCode sdfInit (const std::string &file)
 
MoFEMErrorCode requireDerivatives () const
 
MoFEMErrorCode evalSdf (double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, VectorDouble &values) const
 
MoFEMErrorCode evalGradSdf (double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, MatrixDouble &values) const
 
MoFEMErrorCode evalHessSdf (double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, MatrixDouble &values) const
 
double value (const std::array< double, 3 > &point, double dt=0, double t=0, int block=0) const
 

Static Public Member Functions

static MoFEMErrorCode initialise ()
 
static MoFEMErrorCode finalise ()
 

Private Attributes

std::unique_ptr< Impl > impl
 

Detailed Description

Python/NumPy must be initialised before loading a script. Destroy evaluators before finalising Python. Each evaluator has an independent script namespace.

Examples
mesh_reconstruction.cpp, and mofem/tutorials/adv-1_contact/src/ContactOps.hpp.

Definition at line 16 of file SDFPython.hpp.

Constructor & Destructor Documentation

◆ SDFPython() [1/2]

MoFEM::SDFPython::SDFPython ( )
default

◆ ~SDFPython()

MoFEM::SDFPython::~SDFPython ( )

Definition at line 77 of file SDFPython.cpp.

77 {
78 if (impl) {
79 PythonLock lock;
80 impl.reset();
81 }
82}
std::unique_ptr< Impl > impl
Definition SDFPython.hpp:44

◆ SDFPython() [2/2]

MoFEM::SDFPython::SDFPython ( const SDFPython &  )
delete

Member Function Documentation

◆ evalGradSdf()

MoFEMErrorCode MoFEM::SDFPython::evalGradSdf ( double  dt,
double  t,
const MatrixDouble &  coordinates,
const MatrixDouble &  normals,
int  block,
MatrixDouble &  values 
) const

Definition at line 162 of file SDFPython.cpp.

165 {
167 if (!impl)
168 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL, "Load an SDF first");
169 CHKERR impl->evaluate("grad_sdf", 3, dt, t, x, n, block, values);
171}
Vec t
Removal: the ring edge.
SmartPetscObj< Vec > values
#define MoFEMFunctionBegin
First executable line of each MoFEM function, used for error handling. Final line of MoFEM functions ...
@ MOFEM_OPERATION_UNSUCCESSFUL
Definition definitions.h:34
#define MoFEMFunctionReturn(a)
Last executable line of each PETSc function used for error handling. Replaces return()
#define CHKERR
Inline error check.
double dt

◆ evalHessSdf()

MoFEMErrorCode MoFEM::SDFPython::evalHessSdf ( double  dt,
double  t,
const MatrixDouble &  coordinates,
const MatrixDouble &  normals,
int  block,
MatrixDouble &  values 
) const

Definition at line 173 of file SDFPython.cpp.

176 {
178 if (!impl)
179 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL, "Load an SDF first");
180 CHKERR impl->evaluate("hess_sdf", 6, dt, t, x, n, block, values);
182}

◆ evalSdf()

MoFEMErrorCode MoFEM::SDFPython::evalSdf ( double  dt,
double  t,
const MatrixDouble &  coordinates,
const MatrixDouble &  normals,
int  block,
VectorDouble &  values 
) const

Definition at line 148 of file SDFPython.cpp.

150 {
152 if (!impl)
153 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL, "Load an SDF first");
155 CHKERR impl->evaluate("sdf", 1, dt, t, x, n, block, result);
156 values.resize(result.size1(), false);
157 for (size_t i = 0; i != values.size(); ++i)
158 values[i] = result(i, 0);
160}
TetMeshData result
FTensor::Index< 'i', SPACE_DIM > i
UBlasMatrix< double > MatrixDouble
Definition Types.hpp:77

◆ finalise()

MoFEMErrorCode MoFEM::SDFPython::finalise ( )
static

Definition at line 101 of file SDFPython.cpp.

101 {
103 if (Py_IsInitialized() && Py_FinalizeEx() < 0)
104 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
105 "Python finalisation failed");
107}

◆ initialise()

MoFEMErrorCode MoFEM::SDFPython::initialise ( )
static

Definition at line 84 of file SDFPython.cpp.

84 {
86 Py_Initialize();
87 PythonLock lock;
88 try {
89 bp::import("numpy");
90 np::initialize();
91 if (PyErr_Occurred())
92 bp::throw_error_already_set();
93 } catch (const bp::error_already_set &) {
94 PyErr_Print();
95 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
96 "Could not initialise NumPy");
97 }
99}

◆ operator=()

SDFPython & MoFEM::SDFPython::operator= ( const SDFPython &  )
delete

◆ requireDerivatives()

MoFEMErrorCode MoFEM::SDFPython::requireDerivatives ( ) const

Definition at line 135 of file SDFPython.cpp.

135 {
137 if (!impl)
138 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL, "Load an SDF first");
139 PythonLock lock;
140 for (const auto *name : {"grad_sdf", "hess_sdf"})
141 if (!impl->globals.has_key(name) ||
142 !PyCallable_Check(bp::object(impl->globals[name]).ptr()))
143 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
144 "Contact requires %s in %s", name, impl->file.c_str());
146}
std::string name
@ MOFEM_DATA_INCONSISTENCY
Definition definitions.h:31

◆ sdfInit()

MoFEMErrorCode MoFEM::SDFPython::sdfInit ( const std::string &  file)

Definition at line 109 of file SDFPython.cpp.

109 {
111 if (!Py_IsInitialized())
112 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
113 "Initialise Python/NumPy before loading an SDF");
114 PythonLock lock;
115 try {
116 auto next = std::make_unique<Impl>();
117 next->file = file;
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()))
124 SETERRQ(PETSC_COMM_SELF, MOFEM_DATA_INCONSISTENCY,
125 "Script %s has no callable sdf", file.c_str());
126 impl = std::move(next);
127 } catch (const bp::error_already_set &) {
128 PyErr_Print();
129 SETERRQ(PETSC_COMM_SELF, MOFEM_OPERATION_UNSUCCESSFUL,
130 "Could not load SDF script %s", file.c_str());
131 }
133}

◆ value()

double MoFEM::SDFPython::value ( const std::array< double, 3 > &  point,
double  dt = 0,
double  t = 0,
int  block = 0 
) const

Scalar adapter for implicit geometry; contact-normal arguments are zero.

Definition at line 184 of file SDFPython.cpp.

185 {
186 MatrixDouble x(1, 3), n(1, 3);
187 for (int d = 0; d != 3; ++d) {
188 x(0, d) = point[d];
189 n(0, d) = 0;
190 }
192 CHK_THROW_MESSAGE(evalSdf(dt, t, x, n, block, result),
193 "SDF evaluation failed");
194 return result[0];
195}
#define CHK_THROW_MESSAGE(err, msg)
Check and throw MoFEM exception.
UBlasVector< double > VectorDouble
Definition Types.hpp:68
MoFEMErrorCode evalSdf(double dt, double t, const MatrixDouble &coordinates, const MatrixDouble &normals, int block, VectorDouble &values) const

Member Data Documentation

◆ impl

std::unique_ptr<Impl> MoFEM::SDFPython::impl
private

Definition at line 44 of file SDFPython.hpp.


The documentation for this struct was generated from the following files: