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>
173 ASSERT( point_list.
n_rows() == spacedim && point_list.
n_cols() == 1 ).error(
"Invalid point size.\n");
174 for(
unsigned int i=0; i< point_list.
size(); i++) {
175 Value envelope(value_list[i]);
176 ASSERT_EQ( envelope.n_rows(), this->value_.n_rows() )(i)
177 .error(
"value_list[i] has wrong number of rows\n");
178 set_value(point_list.
vec<spacedim>(i), elm, envelope );
179 envelope.scale(this->unit_conversion_coefficient_);
186 template <
int spacedim,
class Value>
189 #ifdef FLOW123D_HAVE_PYTHON
190 for(
unsigned int i = 0; i < spacedim; i++) {
191 p_value_ = PyFloat_FromDouble( p[i] );
192 PyTuple_SetItem(p_args_, i, p_value_);
194 p_value_ = PyObject_CallObject(p_func_, p_args_);
195 PythonLoader::check_error();
198 for(
unsigned int row=0; row <
value.n_rows(); row++)
199 for(
unsigned int col=0; col <
value.n_cols(); col++, pos++)
201 else value(row,col) = PyFloat_AsDouble( PyTuple_GetItem( p_value_, pos ) );
203 #endif // FLOW123D_HAVE_PYTHON
209 template <
int spacedim,
class Value>
211 #ifdef FLOW123D_HAVE_PYTHON
216 #endif // FLOW123D_HAVE_PYTHON