18 #ifndef FIELD_PYTHON_IMPL_HH_
19 #define FIELD_PYTHON_IMPL_HH_
22 #include <type_traits>
33 template <
int spacedim,
class Value>
40 "Python script given as in place string")
42 "Python script given as external file")
44 "Function in the given script that returns tuple containing components of the return type.\n"
45 "For NxM tensor values: tensor(row,col) = tuple( M*row + col ).")
52 template <
int spacedim,
class Value>
54 Input::register_class< FieldPython<spacedim, Value>,
unsigned int >(
"FieldPython") +
59 template <
int spacedim,
class Value>
65 #ifdef FLOW123D_HAVE_PYTHON
71 THROW( ExcNoPythonSupport() );
72 #endif // FLOW123D_HAVE_PYTHON
77 template <
int spacedim,
class Value>
80 #ifdef FLOW123D_HAVE_PYTHON
81 p_module_ = PythonLoader::load_module_from_string(
"python_field_"+func_name, python_source);
83 #endif // FLOW123D_HAVE_PYTHON
90 template <
int spacedim,
class Value>
92 this->init_unit_conversion_coefficient(rec, init_data);
96 set_python_field_from_string( *
it, rec.
val<
string>(
"function") );
99 if (!
it)
THROW( ExcNoPythonInit() );
101 set_python_field_from_file( *
it, rec.
val<
string>(
"function") );
102 }
INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, rec)
108 template <
int spacedim,
class Value>
111 #ifdef FLOW123D_HAVE_PYTHON
112 p_module_ = PythonLoader::load_module_from_file(
string(file_name) );
114 #endif // FLOW123D_HAVE_PYTHON
120 template <
int spacedim,
class Value>
123 #ifdef FLOW123D_HAVE_PYTHON
124 p_func_ = PythonLoader::get_callable(p_module_, func_name);
126 p_args_ = PyTuple_New( spacedim );
129 for(
unsigned int i = 0; i < spacedim; i++) {
130 p_value_ = PyFloat_FromDouble(
double(i) );
131 PyTuple_SetItem(p_args_, i, p_value_);
133 p_value_ = PyObject_CallObject(p_func_, p_args_);
134 PythonLoader::check_error();
136 if ( ! PyTuple_Check( p_value_)) {
138 ss <<
"Field '" << func_name <<
"' from the python module: " << PyModule_GetName(p_module_) <<
" doesn't return Tuple." << endl;
139 THROW( ExcMessage() << EI_Message( ss.str() ));
142 unsigned int size = PyTuple_Size( p_value_);
144 unsigned int value_size=this->value_.n_rows() * this->value_.n_cols();
145 if ( size != value_size) {
146 THROW( ExcInvalidCompNumber() << EI_FuncName(func_name) << EI_PModule(PyModule_GetName(p_module_)) << EI_Size(size) << EI_ValueSize(value_size) );
149 #endif // FLOW123D_HAVE_PYTHON
156 template <
int spacedim,
class Value>
159 set_value(p,elm, this->value_);
160 this->value_.scale(this->unit_conversion_coefficient_);
161 return this->r_value_;
168 template <
int spacedim,
class Value>
174 for(
unsigned int i=0; i< point_list.
size(); i++) {
175 Value envelope(value_list[i]);
176 OLD_ASSERT( envelope.n_rows()==this->value_.n_rows(),
177 "value_list[%d] has wrong number of rows: %d; should match number of components: %d\n",
178 i, envelope.n_rows(),this->value_.n_rows());
179 set_value(point_list.
vec<spacedim>(i), elm, envelope );
180 envelope.scale(this->unit_conversion_coefficient_);
187 template <
int spacedim,
class Value>
190 #ifdef FLOW123D_HAVE_PYTHON
191 for(
unsigned int i = 0; i < spacedim; i++) {
192 p_value_ = PyFloat_FromDouble( p[i] );
193 PyTuple_SetItem(p_args_, i, p_value_);
195 p_value_ = PyObject_CallObject(p_func_, p_args_);
196 PythonLoader::check_error();
199 for(
unsigned int row=0; row <
value.n_rows(); row++)
200 for(
unsigned int col=0; col <
value.n_cols(); col++, pos++)
202 else value(row,col) = PyFloat_AsDouble( PyTuple_GetItem( p_value_, pos ) );
204 #endif // FLOW123D_HAVE_PYTHON
210 template <
int spacedim,
class Value>
212 #ifdef FLOW123D_HAVE_PYTHON
217 #endif // FLOW123D_HAVE_PYTHON