Flow123d  3.9.0-9663d1cde
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  TYPEDEF_ERR_INFO( EI_FuncName, std::string);
53  TYPEDEF_ERR_INFO( EI_PModule, std::string);
54  TYPEDEF_ERR_INFO( EI_Size, unsigned int);
55  TYPEDEF_ERR_INFO( EI_ValueSize, unsigned int);
56  DECLARE_EXCEPTION( ExcNoPythonSupport, << "Flow123d compiled without support for Python, FieldPython can not be used.\n" );
57  DECLARE_EXCEPTION( ExcNoPythonInit, << "Either 'script_string' or 'script_file' has to be specified in PythonField initialization.\n" );
58  DECLARE_EXCEPTION( ExcInvalidCompNumber, << "Field " << EI_FuncName::qval << " from the python module: " << EI_PModule::val
59  << " returns " << EI_Size::val << " components but should return " << EI_ValueSize::val << " components.\n" );
60 
61  FieldPython(unsigned int n_comp=0);
62 
63  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
64 
65  static const Input::Type::Record & get_input_type();
66 
67  /**
68  * Set the file and field to be called.
69  * TODO: use FilePath
70  */
71  void set_python_field_from_file( const FilePath &file_name, const string &func_name);
72 
73  /**
74  * Set the source in a string and name of the field to be called.
75  */
76  void set_python_field_from_string( const string &python_source, const string &func_name);
77 
78  /**
79  * Returns one value in one given point. ResultType can be used to avoid some costly calculation if the result is trivial.
80  */
81  virtual typename Value::return_type const &value(const Point &p, const ElementAccessor<spacedim> &elm);
82 
83  /**
84  * Returns std::vector of scalar values in several points at once.
85  */
86  virtual void value_list (const Armor::array &point_list, const ElementAccessor<spacedim> &elm,
88 
89 
90  virtual ~FieldPython();
91 
92 private:
93  /// Registrar of class to factory
94  static const int registrar;
95 
96  /**
97  * Common part of set_python_field_from_* methods
98  */
99  void set_func(const string &func_name);
100 
101  /**
102  * Implementation.
103  */
104  inline void set_value(const Point &p, const ElementAccessor<spacedim> &elm, Value &value);
105 
106 #ifdef FLOW123D_HAVE_PYTHON
107  PyObject *p_func_;
108  PyObject *p_module_;
109  mutable PyObject *p_args_;
110  mutable PyObject *p_value_;
111 #endif // FLOW123D_HAVE_PYTHON
112 
113 };
114 
115 
116 
117 #endif /* FUNCTION_PYTHON_HH_ */
factory.hh
python_loader.hh
field_algo_base.hh
point.hh
value
static constexpr bool value
Definition: json.hpp:87
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
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
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::registrar
static const int registrar
Registrar of class to factory.
Definition: field_python.hh:94
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
Value
@ Value
Definition: finite_element.hh:43
FieldPython
Definition: field_python.hh:46
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:50
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:49