45 #define RETURN_VARIABLE "ret" 67 this->
f = Py_CompileString(
fExpression.c_str(),
"internal", Py_eval_input);
73 PyObject *main_module = PyImport_AddModule(
"__main__");
74 this->
main_dict = PyModule_GetDict(main_module);
95 for (
const auto &named_arg: valDict ) {
99 tmp = PyFloat_FromDouble(arg.
val0);
103 PyList_SET_ITEM( tmp, i, PyFloat_FromDouble( arg.
val1.
at(i) ) );
106 tmp = PyLong_FromLong(arg.
val2);
110 PyList_SET_ITEM( tmp, i, PyLong_FromLong( arg.
val3.
at(i) ) );
116 PyDict_SetItemString(local_dict, named_arg.first.c_str(), tmp);
126 PyObject *dummy = PyEval_EvalCode( ( PyCodeObject * ) func,
main_dict, local_dict );
128 if ( PyList_Check(ret) ) {
129 int size = PyList_GET_SIZE(ret);
131 for (
int i = 0; i < size; ++i ) {
132 answer(i) = PyFloat_AS_DOUBLE( PyList_GET_ITEM(ret, i) );
135 answer = {PyFloat_AS_DOUBLE(ret) };
137 Py_DECREF(local_dict);
146 this->
getArray(answer, this->
f, valDict);
167 PyObject *local_dict = PyDict_New();
168 PyDict_SetItemString( local_dict,
"t", PyFloat_FromDouble(time) );
169 PyObject *dummy = PyEval_EvalCode( ( PyCodeObject * ) func,
main_dict, local_dict );
172 if ( PyFloat_Check(ret) ) {
173 val = PyFloat_AS_DOUBLE(ret);
174 }
else if ( PyList_Check(ret) ) {
175 if ( PyList_GET_SIZE(ret) != 1 ) {
176 OOFEM_ERROR(
"Result from python is not a real float!");
178 val = PyFloat_AS_DOUBLE( PyList_GET_ITEM(ret, 0) );
181 OOFEM_ERROR(
"Result from python is not a real float!");
184 Py_DECREF(local_dict);
virtual double evaluateAccelerationAtTime(double t)
Returns the second time derivative of the function at given time.
PythonExpression(int n, Domain *d)
Constructor.
std::string dfdtExpression
Expression for first time derivative.
double & at(int i)
Coefficient access function.
virtual double evaluateAtTime(double t)
Returns the value of the function at given time.
virtual ~PythonExpression()
Destructor.
#define _IFT_PythonExpression_f
Expression with return variable named "ret".
REGISTER_Function(CalculatorFunction)
virtual void giveInputRecord(DynamicInputRecord &input)
Setups the input record string of receiver.
int & at(int i)
Coefficient access function.
double getScalar(PyObject *func, double time)
Helper function to run given function for given time.
Wrapper for values of varying types.
PyObject * getDict(std::map< std::string, FunctionArgument > &valDict)
Helper function to convert the std::map to a Python dictionary.
std::string fExpression
Expression for the function value.
std::string d2fdt2Expression
Expression for second time derivative.
virtual void evaluateAcceleration(FloatArray &answer, std::map< std::string, FunctionArgument > &valDict)
virtual void evaluateVelocity(FloatArray &answer, std::map< std::string, FunctionArgument > &valDict)
virtual IRResultType initializeFrom(InputRecord *ir)
Reads the fields.
virtual void giveInputRecord(DynamicInputRecord &ir)
Setups the input record string of receiver.
virtual IRResultType initializeFrom(InputRecord *ir)
Initializes receiver according to object description stored in input record.
void getArray(FloatArray &answer, PyObject *func, std::map< std::string, FunctionArgument > &valDict)
Helper function to run given function for given value dictionary.
virtual double evaluateVelocityAtTime(double t)
Returns the first time derivative of the function at given time.
FunctionArgumentType type
Determines which of the types the instance points towards.
Class representing vector of real numbers.
virtual void evaluate(FloatArray &answer, std::map< std::string, FunctionArgument > &valDict)
Abstract base class representing a function with vector input and output.
IRResultType
Type defining the return values of InputRecord reading operations.
#define _IFT_PythonExpression_dfdt
Velocity with return variable named "ret".
int giveSize() const
Returns the size of receiver.
the oofem namespace is to define a context or scope in which all oofem names are defined.
#define _IFT_PythonExpression_d2fdt2
Acceleration with return variable named "ret".
void resize(int s)
Resizes receiver towards requested size.