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>
99 if (! it)
THROW( ExcNoPythonInit() );
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>
168 template <
int spacedim,
class Value>
174 for(
unsigned int i=0; i< point_list.
size(); i++) {
175 Value envelope(value_list[i]);
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());
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 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.
unsigned int size() const
void set_func(const string &func_name)
void set_python_field_from_string(const string &python_source, const string &func_name)
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
void set_value(const Point &p, const ElementAccessor< spacedim > &elm, Value &value)
ArmaVec< Type, nr > vec(uint mat_index) const
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.
bool is_constant_in_space_
Flag detects that field is only dependent on time.
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 void value_list(const Armor::array &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm)
#define FLOW123D_FORCE_LINK_IN_CHILD(x)