Flow123d  release_3.0.0-506-g34af125
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 using namespace std;
32 
33 /**
34  *
35  * This class assumes field python field with @p spacedim arguments containing coordinates of the given point.
36  * The field should return a tuple representing a vector value (possibly of size one for scalar fields)
37  *
38  * TODO:
39  * - use rather only one argument - tuple representing the whole point
40  * - time fields
41  * - set some parameter in the python module
42  * - for fields with one component allow python fields returning the value directly
43  *
44  */
45 template <int spacedim, class Value>
46 class FieldPython : public FieldAlgorithmBase<spacedim, Value>
47 {
48 public:
51 
52  FieldPython(unsigned int n_comp=0);
53 
54  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
55 
56  static const Input::Type::Record & get_input_type();
57 
58  /**
59  * Set the file and field to be called.
60  * TODO: use FilePath
61  */
62  void set_python_field_from_file( const FilePath &file_name, const string &func_name);
63 
64  /**
65  * Set the source in a string and name of the field to be called.
66  */
67  void set_python_field_from_string( const string &python_source, const string &func_name);
68 
69  /**
70  * Returns one value in one given point. ResultType can be used to avoid some costly calculation if the result is trivial.
71  */
72  virtual typename Value::return_type const &value(const Point &p, const ElementAccessor<spacedim> &elm);
73 
74  /**
75  * Returns std::vector of scalar values in several points at once.
76  */
77  virtual void value_list (const std::vector< Point > &point_list, const ElementAccessor<spacedim> &elm,
79 
80 
81  virtual ~FieldPython();
82 
83 private:
84  /// Registrar of class to factory
85  static const int registrar;
86 
87  /**
88  * Common part of set_python_field_from_* methods
89  */
90  void set_func(const string &func_name);
91 
92  /**
93  * Implementation.
94  */
95  inline void set_value(const Point &p, const ElementAccessor<spacedim> &elm, Value &value);
96 
97 #ifdef FLOW123D_HAVE_PYTHON
98  PyObject *p_func_;
99  PyObject *p_module_;
100  mutable PyObject *p_args_;
101  mutable PyObject *p_value_;
102 #endif // FLOW123D_HAVE_PYTHON
103 
104 };
105 
106 
107 
108 #endif /* FUNCTION_PYTHON_HH_ */
FieldAlgorithmBase< spacedim, Value >::Point Point
Definition: field_python.hh:49
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
static constexpr bool value
Definition: json.hpp:87
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
Space< spacedim >::Point Point
static const int registrar
Registrar of class to factory.
Definition: field_python.hh:85
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
FieldAlgorithmBase< spacedim, Value > FactoryBaseType
Definition: field_python.hh:50
Record type proxy class.
Definition: type_record.hh:182