Flow123d  master-f44eb46
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/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  * Overload @p FieldAlgorithmBase::cache_reinit
79  *
80  * Reinit dictionary of used fields and update result field to dictionary of resul fields.
81  */
82  void cache_reinit(const ElementCacheMap &cache_map) override;
83 
84  void cache_update(FieldValueCache<typename Value::element_type> &data_cache,
85  ElementCacheMap &cache_map, unsigned int region_patch_idx) override;
86 
87  /**
88  * Returns list of fields on which this field depends.
89  */
90  std::vector<const FieldCommon *> set_dependency(FieldSet &field_set) override;
91 
92  virtual ~FieldPython();
93 
94 private:
95  /// Registrar of class to factory
96  static const int registrar;
97 
98  /// Accessor to Input::Record
100 
101  /// Field name is necessary for set result
102  std::string field_name_;
103 
104  /// Holds python instance of user class.
106 
107  /// List of fields on which this field depends
109 
110  /// Pointer to FieldCommon that holds this fields (stores in set_dependency and uses in cache_reinit)
112 
113 };
114 
115 
116 #pragma GCC visibility pop
117 
118 #endif /* FUNCTION_PYTHON_HH_ */
Directing class of FieldValueCache.
Space< spacedim >::Point Point
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:77
TYPEDEF_ERR_INFO(EI_PModule, std::string)
std::string field_name_
Field name is necessary for set result.
DECLARE_EXCEPTION(ExcInvalidCompNumber,<< "Field "<< EI_FuncName::qval<< " from the python module: "<< EI_PModule::val<< " returns "<< EI_Size::val<< " components but should return "<< EI_ValueSize::val<< " components.\n")
TYPEDEF_ERR_INFO(EI_ValueSize, unsigned int)
TYPEDEF_ERR_INFO(EI_FuncName, std::string)
FieldAlgorithmBase< spacedim, Value > FactoryBaseType
Definition: field_python.hh:56
FieldAlgorithmBase< spacedim, Value >::Point Point
Definition: field_python.hh:55
Input::Record in_rec_
Accessor to Input::Record.
Definition: field_python.hh:99
py::object user_class_instance_
Holds python instance of user class.
TYPEDEF_ERR_INFO(EI_Size, unsigned int)
const FieldCommon * self_field_ptr_
Pointer to FieldCommon that holds this fields (stores in set_dependency and uses in cache_reinit)
static const int registrar
Registrar of class to factory.
Definition: field_python.hh:96
std::vector< const FieldCommon * > required_fields_
List of fields on which this field depends.
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
Record type proxy class.
Definition: type_record.hh:182
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.