Flow123d
JB_transport-d4c8564
|
Go to the documentation of this file.
18 #ifndef FIELD_PYTHON_IMPL_HH_
19 #define FIELD_PYTHON_IMPL_HH_
22 #include <type_traits>
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) {
109 template <
int spacedim,
class Value>
112 ASSERT(
false).warning(
"Method FieldPython::value is obsolete. DO not use it!\n");
113 return this->r_value_;
120 template <
int spacedim,
class Value>
124 ASSERT(
false).warning(
"Method FieldPython::value_list is obsolete. DO not use it!\n");
129 template <
int spacedim,
class Value>
131 required_fields_.clear();
133 auto used_fields_array = in_rec_.val<
Input::Array>(
"used_fields");
135 used_fields_array.copy_to(used_fields_vec);
136 for(
auto field_name : used_fields_vec) {
137 auto field_ptr = field_set.
field(field_name);
138 if (field_ptr !=
nullptr) required_fields_.push_back( field_ptr );
139 else THROW( FieldSet::ExcUnknownField() << FieldCommon::EI_Field(field_name) << FieldSet::EI_FieldType(
"python declaration") << Input::EI_Address( in_rec_.address_string() ) );
143 self_field_ptr_ = field_set.
field(this->field_name_);
145 return required_fields_;
150 template <
int spacedim,
class Value>
154 for (
auto field_ptr : required_fields_) {
155 std::string field_name = field_ptr->name();
156 double * cache_data = field_ptr->value_cache()->data_;
160 double * cache_data = self_field_ptr_->value_cache()->data_;
164 py::object p_func = user_class_instance_.attr(
"_cache_reinit");
165 p_func(this->time_.end(), field_data, result_data);
166 }
catch (
const py::error_already_set &ex) {
173 template <
int spacedim,
class Value>
180 py::object p_func = user_class_instance_.attr(
"_cache_update");
181 p_func(this->field_name_, reg_chunk_begin, reg_chunk_end);
182 }
catch (
const py::error_already_set &ex) {
189 template <
int spacedim,
class Value>
static unsigned int get()
Return number of stored elements.
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
void set_python_field_from_class(const string &file_name, const string &class_name)
std::vector< const FieldCommon * > set_dependency(FieldSet &field_set) override
Directing class of FieldValueCache.
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
bool is_constant_in_space_
Flag detects that field is only dependent on time.
void cache_reinit(const ElementCacheMap &cache_map) override
Space< spacedim >::Point Point
unsigned int region_chunk_end(unsigned int region_patch_idx) const
Return end position of region chunk in FieldValueCache.
static const Input::Type::Record & get_input_type()
Implementation.
Helper class, holds data of one field.
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
FieldPython(unsigned int n_comp=0)
Container for various descendants of FieldCommonBase.
static void throw_error(const pybind11::error_already_set &ex)
void cache_update(FieldValueCache< typename Value::element_type > &data_cache, ElementCacheMap &cache_map, unsigned int region_patch_idx) override
unsigned int region_chunk_begin(unsigned int region_patch_idx) const
Return begin position of region chunk in FieldValueCache.
static pybind11::module_ load_module_by_name(const std::string &module_name)
virtual const Value::return_type & value(const Point &p, const ElementAccessor< spacedim > &elm)
virtual void value_list(const Armor::array &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)
static pybind11::module_ load_module_from_file(const std::string &fname)
FieldCommon * field(const std::string &field_name) const