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>
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>