Flow123d  master-921db77
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 struct PatchInternals;
34 
35 using namespace std;
36 namespace py = pybind11;
37 
38 // Pybind11 needs set visibility to hidden (see https://pybind11.readthedocs.io/en/stable/faq.html).
39 #pragma GCC visibility push(hidden)
40 
41 /**
42  *
43  * This class assumes field python field with @p spacedim arguments containing coordinates of the given point.
44  * The field should return a tuple representing a vector value (possibly of size one for scalar fields)
45  *
46  * TODO:
47  * - use rather only one argument - tuple representing the whole point
48  * - time fields
49  * - set some parameter in the python module
50  * - for fields with one component allow python fields returning the value directly
51  *
52  */
53 template <int spacedim, class Value>
54 class FieldPython : public FieldAlgorithmBase<spacedim, Value>
55 {
56 public:
59 
60  TYPEDEF_ERR_INFO( EI_FuncName, std::string);
61  TYPEDEF_ERR_INFO( EI_PModule, std::string);
62  TYPEDEF_ERR_INFO( EI_Size, unsigned int);
63  TYPEDEF_ERR_INFO( EI_ValueSize, unsigned int);
64  DECLARE_EXCEPTION( ExcInvalidCompNumber, << "Field " << EI_FuncName::qval << " from the python module: " << EI_PModule::val
65  << " returns " << EI_Size::val << " components but should return " << EI_ValueSize::val << " components.\n" );
66 
67 
68  FieldPython(unsigned int n_comp=0);
69 
70  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
71 
72  static const Input::Type::Record & get_input_type();
73 
74  /**
75  * Set the source in a string and name of the field to be called.
76  */
77  void set_python_field_from_class(const string &file_name, const string &class_name);
78 
79  /**
80  * Overload @p FieldAlgorithmBase::cache_reinit
81  *
82  * Reinit dictionary of used fields and update result field to dictionary of resul fields.
83  */
84  void cache_reinit(PatchInternals &patch_internals) override;
85 
86  void cache_update(FieldValueCache<typename Value::element_type> &data_cache,
87  ElementCacheMap &cache_map, unsigned int region_patch_idx) override;
88 
89  /**
90  * Returns list of fields on which this field depends.
91  */
92  std::vector<const FieldCommon *> set_dependency(FieldSet &field_set) override;
93 
94  virtual ~FieldPython();
95 
96 private:
97  /// Registrar of class to factory
98  static const int registrar;
99 
100  /// Accessor to Input::Record
102 
103  /// Field name is necessary for set result
104  std::string field_name_;
105 
106  /// Holds python instance of user class.
108 
109  /// List of fields on which this field depends
111 
112  /// Pointer to FieldCommon that holds this fields (stores in set_dependency and uses in cache_reinit)
114 
115 };
116 
117 
118 #pragma GCC visibility pop
119 
120 #endif /* FUNCTION_PYTHON_HH_ */
Directing class of FieldValueCache.
Space< spacedim >::Point Point
Common abstract parent of all Field<...> classes.
Definition: field_common.hh:78
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:58
FieldAlgorithmBase< spacedim, Value >::Point Point
Definition: field_python.hh:57
Input::Record in_rec_
Accessor to Input::Record.
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:98
std::vector< const FieldCommon * > required_fields_
List of fields on which this field depends.
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:161
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.
Holds common data shared between GenericAssemblz and Assembly<dim> classes.