Flow123d  JB_transport-112d700
field_python.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file field_python.hh
15  * @brief
16  * @todo make FieldPython dummy class if we do not have python, so that we
17  * need not optional code elsewhere
18  */
19 
20 #ifndef FIELD_PYTHON_HH_
21 #define FIELD_PYTHON_HH_
22 
23 
24 #include "system/system.hh"
25 #include "system/python_loader.hh"
27 #include "mesh/point.hh"
28 #include "input/factory.hh"
29 
30 #include <string>
31 #include <pybind11.h>
32 
33 using namespace std;
34 namespace py = pybind11;
35 
36 // Pybind11 needs set visibility to hidden (see https://pybind11.readthedocs.io/en/stable/faq.html).
37 #pragma GCC visibility push(hidden)
38 
39 /**
40  *
41  * This class assumes field python field with @p spacedim arguments containing coordinates of the given point.
42  * The field should return a tuple representing a vector value (possibly of size one for scalar fields)
43  *
44  * TODO:
45  * - use rather only one argument - tuple representing the whole point
46  * - time fields
47  * - set some parameter in the python module
48  * - for fields with one component allow python fields returning the value directly
49  *
50  */
51 template <int spacedim, class Value>
52 class FieldPython : public FieldAlgorithmBase<spacedim, Value>
53 {
54 public:
57 
58  TYPEDEF_ERR_INFO( EI_FuncName, std::string);
59  TYPEDEF_ERR_INFO( EI_PModule, std::string);
60  TYPEDEF_ERR_INFO( EI_Size, unsigned int);
61  TYPEDEF_ERR_INFO( EI_ValueSize, unsigned int);
62  DECLARE_EXCEPTION( ExcInvalidCompNumber, << "Field " << EI_FuncName::qval << " from the python module: " << EI_PModule::val
63  << " returns " << EI_Size::val << " components but should return " << EI_ValueSize::val << " components.\n" );
64 
65 
66  FieldPython(unsigned int n_comp=0);
67 
68  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
69 
70  static const Input::Type::Record & get_input_type();
71 
72  /**
73  * Set the source in a string and name of the field to be called.
74  */
75  void set_python_field_from_class(const string &file_name, const string &class_name);
76 
77  /**
78  * Returns one value in one given point. ResultType can be used to avoid some costly calculation if the result is trivial.
79  */
80  virtual typename Value::return_type const &value(const Point &p, const ElementAccessor<spacedim> &elm);
81 
82  /**
83  * Returns std::vector of scalar values in several points at once.
84  */
85  virtual void value_list (const Armor::array &point_list, const ElementAccessor<spacedim> &elm,
87 
88 
89  /**
90  * Overload @p FieldAlgorithmBase::cache_reinit
91  *
92  * Reinit dictionary of used fields and update result field to dictionary of resul fields.
93  */
94  void cache_reinit(const ElementCacheMap &cache_map) override;
95 
96  void cache_update(FieldValueCache<typename Value::element_type> &data_cache,
97  ElementCacheMap &cache_map, unsigned int region_patch_idx) override;
98 
99  /**
100  * Returns list of fields on which this field depends.
101  */
102  std::vector<const FieldCommon *> set_dependency(FieldSet &field_set) override;
103 
104  virtual ~FieldPython();
105 
106 private:
107  /// Registrar of class to factory
108  static const int registrar;
109 
110  /// Accessor to Input::Record
112 
113  /// Field name is necessary for set result
114  std::string field_name_;
115 
116  /// Holds python instance of user class.
118 
119  /// List of fields on which this field depends
121 
122  /// Pointer to FieldCommon that holds this fields (stores in set_dependency and uses in cache_reinit)
124 
125 };
126 
127 
128 #pragma GCC visibility pop
129 
130 #endif /* FUNCTION_PYTHON_HH_ */
FieldPython::required_fields_
std::vector< const FieldCommon * > required_fields_
List of fields on which this field depends.
Definition: field_python.hh:120
FieldPython::self_field_ptr_
const FieldCommon * self_field_ptr_
Pointer to FieldCommon that holds this fields (stores in set_dependency and uses in cache_reinit)
Definition: field_python.hh:123
factory.hh
python_loader.hh
field_algo_base.hh
FieldPython::user_class_instance_
py::object user_class_instance_
Holds python instance of user class.
Definition: field_python.hh:117
point.hh
ElementCacheMap
Directing class of FieldValueCache.
Definition: field_value_cache.hh:152
value
static constexpr bool value
Definition: json.hpp:87
std::vector
Definition: doxy_dummy_defs.hh:7
ElementAccessor
Definition: dh_cell_accessor.hh:32
system.hh
FieldAlgorithmBase::Point
Space< spacedim >::Point Point
Definition: field_algo_base.hh:115
FieldPython::field_name_
std::string field_name_
Field name is necessary for set result.
Definition: field_python.hh:114
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
FieldAlgoBaseInitData
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
Definition: field_algo_base.hh:81
FieldPython::in_rec_
Input::Record in_rec_
Accessor to Input::Record.
Definition: field_python.hh:111
FieldCommon
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:77
FieldSet
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
FieldPython::registrar
static const int registrar
Registrar of class to factory.
Definition: field_python.hh:108
Input::Type::Record
Record type proxy class.
Definition: type_record.hh:182
TYPEDEF_ERR_INFO
#define TYPEDEF_ERR_INFO(EI_Type, Type)
Macro to simplify declaration of error_info types.
Definition: exceptions.hh:194
FieldPython
Definition: field_python.hh:52
FieldAlgorithmBase
Definition: field_algo_base.hh:112
std
Definition: doxy_dummy_defs.hh:5
Armor::Array< double >
FieldPython::FactoryBaseType
FieldAlgorithmBase< spacedim, Value > FactoryBaseType
Definition: field_python.hh:56
DECLARE_EXCEPTION
#define DECLARE_EXCEPTION(ExcName, Format)
Macro for simple definition of exceptions.
Definition: exceptions.hh:158
FieldPython::Point
FieldAlgorithmBase< spacedim, Value >::Point Point
Definition: field_python.hh:55