47#define RETURN_VARIABLE "ret"
54PythonExpression :: ~PythonExpression()
63 Function :: initializeFrom(ir);
73 this->
f = Py_CompileString(
fExpression.c_str(),
"<internal_f>", Py_file_input);
74 if ( this->
f ==
nullptr) {
86 this->
dfdt = Py_CompileString(
dfdtExpression.c_str(),
"<internal_dfdt>", Py_file_input);
87 if ( this->
dfdt ==
nullptr) {
100 if ( this->
d2fdt2 ==
nullptr) {
106 PyObject *main_module = PyImport_ImportModule(
"__main__");
107 if (main_module != NULL) {
108 this->
main_dict = PyModule_GetDict(main_module);
109 Py_DECREF(main_module);
122 Function :: giveInputRecord(input);
130PythonExpression :: getDict(
const std :: map< std :: string, FunctionArgument > &valDict)
132 PyObject *local_dict = PyDict_New();
133 for (
const auto &named_arg: valDict ) {
136 if ( arg.
type == FunctionArgument :: FAT_double ) {
137 tmp = PyFloat_FromDouble(arg.
val0);
138 }
else if ( arg.
type == FunctionArgument :: FAT_FloatArray ) {
141 PyList_SET_ITEM( tmp, i, PyFloat_FromDouble( arg.
val1[i] ) );
143 }
else if ( arg.
type == FunctionArgument :: FAT_int ) {
144 tmp = PyLong_FromLong(arg.
val2);
145 }
else if ( arg.
type == FunctionArgument :: FAT_IntArray ) {
148 PyList_SET_ITEM( tmp, i, PyLong_FromLong( arg.
val3[i] ) );
154 PyDict_SetItemString(local_dict, named_arg.first.c_str(), tmp);
161PythonExpression :: getArray(
FloatArray &answer,
PyObject **func,
const std :: map< std :: string, FunctionArgument > &valDict)
170 if ( dummy ==
nullptr ) {
174 if ( PyList_Check(ret) ) {
175 int size = PyList_GET_SIZE(ret);
177 for (
int i = 0; i < size; ++i ) {
178 answer(i) = this->
pyObj2double( PyList_GET_ITEM(ret, i) );
184 Py_DECREF(local_dict);
192PythonExpression :: evaluate(
FloatArray &answer,
const std :: map< std :: string, FunctionArgument > &valDict,
GaussPoint *gp,
double param)
194 this->
getArray(answer, &this->
f, valDict);
199PythonExpression :: evaluateVelocity(
FloatArray &answer,
const std :: map< std :: string, FunctionArgument > &valDict)
206PythonExpression :: evaluateAcceleration(
FloatArray &answer,
const std :: map< std :: string, FunctionArgument > &valDict)
213PythonExpression :: getScalar(
PyObject *func,
double time)
215 PyObject *local_dict = PyDict_New();
216 PyDict_SetItemString( local_dict,
"t", PyFloat_FromDouble(time) );
220 if ( PyNumber_Check(ret) ) {
222 }
else if ( PyList_Check(ret) ) {
223 if ( PyList_GET_SIZE(ret) != 1 ) {
224 OOFEM_ERROR(
"Result from python is not a real float!");
229 OOFEM_ERROR(
"Result from python is not a real float!");
232 Py_DECREF(local_dict);
239double PythonExpression :: evaluateAtTime(
double time)
244double PythonExpression :: evaluateVelocityAtTime(
double time)
250double PythonExpression :: evaluateAccelerationAtTime(
double time)
257 if (PyNumber_Check(obj)) {
258 PyObject *float_obj = PyNumber_Float(obj);
259 if (float_obj != NULL) {
260 double result = PyFloat_AsDouble(float_obj);
261 Py_DECREF(float_obj);
271 std::ifstream file(path);
272 if (file.is_open()) {
273 std::stringstream buffer;
274 buffer << file.rdbuf();
275 content = buffer.str();
278 OOFEM_ERROR(
"Could not open file %s", path.c_str());
#define REGISTER_Function(class)
Index giveSize() const
Returns the size of receiver.
FunctionArgumentType type
Determines which of the types the instance points towards.
Function(int n, Domain *d)
double getScalar(PyObject *func, double time)
Helper function to run given function for given time.
void readFile2String(const std ::string &path, std ::string &content)
std::string d2fdt2Expression
Expression for second time derivative or path to python script.
double pyObj2double(PyObject *obj)
std::string dfdtExpression
Expression for first time derivative or path to python script.
PyObject * getDict(const std ::map< std ::string, FunctionArgument > &valDict)
Helper function to convert the std::map to a Python dictionary.
void getArray(FloatArray &answer, PyObject **func, const std ::map< std ::string, FunctionArgument > &valDict)
Helper function to run given function for given value dictionary.
std::string fExpression
Expression for the function value or path to python script.
#define OOFEM_WARNING(...)
#define _IFT_PythonExpression_d2fdt2file
Acceleration with return variable named "ret".
#define _IFT_PythonExpression_d2fdt2
Acceleration with return variable named "ret".
#define _IFT_PythonExpression_dfdtfile
Velocity with return variable named "ret".
#define _IFT_PythonExpression_f
Expression with return variable named "ret".
#define _IFT_PythonExpression_dfdt
Velocity with return variable named "ret".
#define _IFT_PythonExpression_ffile
Expression with return variable named "ret".