18 #ifndef FIELD_PYTHON_IMPL_HH_ 19 #define FIELD_PYTHON_IMPL_HH_ 22 #include <boost/type_traits.hpp> 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>
63 #ifdef FLOW123D_HAVE_PYTHON 69 xprintf(
UsrErr,
"Flow123d compiled without support for Python, FieldPython can not be used.\n");
70 #endif // FLOW123D_HAVE_PYTHON 75 template <
int spacedim,
class Value>
78 #ifdef FLOW123D_HAVE_PYTHON 79 p_module_ = PythonLoader::load_module_from_string(
"python_field_"+func_name, python_source);
81 #endif // FLOW123D_HAVE_PYTHON 88 template <
int spacedim,
class Value>
97 if (! it)
xprintf(
UsrErr,
"Either 'script_string' or 'script_file' has to be specified in PythonField initialization.");
100 }
INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, rec)
106 template <
int spacedim,
class Value>
109 #ifdef FLOW123D_HAVE_PYTHON 110 p_module_ = PythonLoader::load_module_from_file(
string(file_name) );
112 #endif // FLOW123D_HAVE_PYTHON 118 template <
int spacedim,
class Value>
121 #ifdef FLOW123D_HAVE_PYTHON 122 p_func_ = PythonLoader::get_callable(p_module_, func_name);
124 p_args_ = PyTuple_New( spacedim );
127 for(
unsigned int i = 0; i < spacedim; i++) {
128 p_value_ = PyFloat_FromDouble(
double(i) );
129 PyTuple_SetItem(p_args_, i, p_value_);
131 p_value_ = PyObject_CallObject(p_func_, p_args_);
132 PythonLoader::check_error();
134 if ( ! PyTuple_Check( p_value_)) {
136 ss <<
"Field '" << func_name <<
"' from the python module: " << PyModule_GetName(p_module_) <<
" doesn't return Tuple." << endl;
137 THROW( ExcMessage() << EI_Message( ss.str() ));
140 unsigned int size = PyTuple_Size( p_value_);
142 unsigned int value_size=this->
value_.n_rows() * this->
value_.n_cols();
143 if ( size != value_size) {
144 xprintf(
UsrErr,
"Field '%s' from the python module: %s returns %d components but should return %d components.\n" 145 ,func_name.c_str(), PyModule_GetName(p_module_), size, value_size);
148 #endif // FLOW123D_HAVE_PYTHON 155 template <
int spacedim,
class Value>
167 template <
int spacedim,
class Value>
172 for(
unsigned int i=0; i< point_list.size(); i++) {
173 Value envelope(value_list[i]);
175 "value_list[%d] has wrong number of rows: %d; should match number of components: %d\n",
176 i, envelope.n_rows(),this->
value_.n_rows());
177 set_value(point_list[i], elm, envelope );
185 template <
int spacedim,
class Value>
188 #ifdef FLOW123D_HAVE_PYTHON 189 for(
unsigned int i = 0; i < spacedim; i++) {
190 p_value_ = PyFloat_FromDouble( p[i] );
191 PyTuple_SetItem(p_args_, i, p_value_);
193 p_value_ = PyObject_CallObject(p_func_, p_args_);
194 PythonLoader::check_error();
197 for(
unsigned int row=0; row < value.n_rows(); row++)
198 for(
unsigned int col=0; col < value.n_cols(); col++, pos++)
200 else value(row,col) = PyFloat_AsDouble( PyTuple_GetItem( p_value_, pos ) );
202 #endif // FLOW123D_HAVE_PYTHON 208 template <
int spacedim,
class Value>
210 #ifdef FLOW123D_HAVE_PYTHON 215 #endif // FLOW123D_HAVE_PYTHON void set_python_field_from_file(const FilePath &file_name, const string &func_name)
void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Init value of unit_conversion_coefficient_ from input.
void set_func(const string &func_name)
void set_python_field_from_string(const string &python_source, const string &func_name)
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
void set_value(const Point &p, const ElementAccessor< spacedim > &elm, Value &value)
Value::return_type r_value_
static constexpr bool value
double unit_conversion_coefficient_
Coeficient of conversion of user-defined unit.
FieldPython(unsigned int n_comp=0)
Space< spacedim >::Point Point
Dedicated class for storing path to input and output files.
Value value_
Last value, prevents passing large values (vectors) by value.
#define OLD_ASSERT_EQUAL(a, b)
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm)
#define FLOW123D_FORCE_LINK_IN_CHILD(x)