18 #ifndef FIELD_PYTHON_IMPL_HH_
19 #define FIELD_PYTHON_IMPL_HH_
22 #include <type_traits>
26 #include <pybind11/pybind11.h>
27 #include <pybind11/eval.h>
28 #include <pybind11/stl.h>
30 namespace py = pybind11;
40 template <
int spacedim,
class Value>
47 "Python script given as external file in format 'dir'.'file_name' without .py extension")
49 "Function in the given script that returns tuple containing components of the return type.\n"
50 "For NxM tensor values: tensor(row,col) = tuple( M*row + col ).")
52 "Defines list of fields necessary in evaluation of actual field.")
59 template <
int spacedim,
class Value>
61 Input::register_class< FieldPython<spacedim, Value>,
unsigned int >(
"FieldPython") +
66 template <
int spacedim,
class Value>
76 template <
int spacedim,
class Value>
78 this->init_unit_conversion_coefficient(rec, init_data);
81 std::string source_file = rec.
val<
string>(
"source_file");
82 std::string source_class = rec.
val<
string>(
"class");
84 set_python_field_from_class( source_file, source_class );
85 }
INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, rec)
92 template <
int spacedim,
class Value>
95 py::module_ flowpy_module = PythonLoader::load_module_by_name(
"flowpy");
96 py::module_ class_module = PythonLoader::load_module_from_file(
string(file_name) );
98 user_class_instance_ = flowpy_module.attr(
"PythonFieldBase").attr(
"_create")(class_module, class_name.c_str());
99 }
catch (
const py::error_already_set &ex) {
106 template <
int spacedim,
class Value>
108 required_fields_.clear();
110 auto used_fields_array = in_rec_.val<
Input::Array>(
"used_fields");
112 used_fields_array.copy_to(used_fields_vec);
113 for(
auto field_name : used_fields_vec) {
114 auto field_ptr = field_set.
field(field_name);
115 if (field_ptr !=
nullptr) required_fields_.push_back( field_ptr );
116 else THROW( FieldSet::ExcUnknownField() << FieldCommon::EI_Field(field_name) << FieldSet::EI_FieldType(
"python declaration") << Input::EI_Address( in_rec_.address_string() ) );
120 self_field_ptr_ = field_set.
field(this->field_name_);
122 return required_fields_;
127 template <
int spacedim,
class Value>
131 for (
auto field_ptr : required_fields_) {
132 std::string field_name = field_ptr->name();
133 double * cache_data = field_ptr->value_cache()->data_;
137 double * cache_data = self_field_ptr_->value_cache()->data_;
141 py::object p_func = user_class_instance_.attr(
"_cache_reinit");
142 p_func(this->time_.end(), field_data, result_data);
143 }
catch (
const py::error_already_set &ex) {
150 template <
int spacedim,
class Value>
157 py::object p_func = user_class_instance_.attr(
"_cache_update");
158 p_func(this->field_name_, reg_chunk_begin, reg_chunk_end);
159 }
catch (
const py::error_already_set &ex) {
166 template <
int spacedim,
class Value>
static unsigned int get()
Return number of stored elements.
Directing class of FieldValueCache.
unsigned int region_chunk_end(unsigned int region_patch_idx) const
Return end position of region chunk in FieldValueCache.
unsigned int region_chunk_begin(unsigned int region_patch_idx) const
Return begin position of region chunk in FieldValueCache.
bool is_constant_in_space_
Flag detects that field is only dependent on time.
Helper class, holds data of one field.
static const Input::Type::Record & get_input_type()
Implementation.
FieldPython(unsigned int n_comp=0)
void set_python_field_from_class(const string &file_name, const string &class_name)
void cache_reinit(const ElementCacheMap &cache_map) override
void cache_update(FieldValueCache< typename Value::element_type > &data_cache, ElementCacheMap &cache_map, unsigned int region_patch_idx) override
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
std::vector< const FieldCommon * > set_dependency(FieldSet &field_set) override
Container for various descendants of FieldCommonBase.
FieldCommon * field(const std::string &field_name) const
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
void throw_error(spirit_namespace::position_iterator< Iter_type > i, const std::string &reason)
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.