Flow123d  master-7bf36fe
field_formula.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_formula.hh
15  * @brief
16  */
17 
18 #ifndef FIELD_FORMULA_HH_
19 #define FIELD_FORMULA_HH_
20 
21 
22 #define BOOST_MATH_DISABLE_FLOAT128
23 
24 
25 #include <stdio.h> // for sprintf
26 #include <string> // for operator==, string
27 #include <vector> // for vector
28 #include <memory>
29 #include <armadillo>
30 #include <map>
31 #include "fields/field_algo_base.hh" // for FieldAlgorithmBase
32 #include "fields/field_values.hh" // for FieldValue<>::Enum, FieldValu...
33 #include "fields/field_set.hh"
34 #include "input/accessors.hh" // for ExcAccessorForNullStorage
35 #include "input/accessors_impl.hh" // for Record::val
36 #include "input/storage.hh" // for ExcStorageTypeMismatch
37 #include "input/type_record.hh" // for Record::ExcRecordKeyNotFound
38 #include "input/input_exception.hh" // for ExcAssertMsg::~ExcAssertMsg
39 #include "system/exceptions.hh" // for ExcAssertMsg::~ExcAssertMsg
40 #include "tools/time_governor.hh" // for TimeStep
41 //#include "include/assert.hh" // bparser
42 #include "parser.hh" // bparser
43 
44 template <int spacedim> class ElementAccessor;
45 class SurfaceDepth;
46 
47 using namespace std;
48 
49 /**
50  * Class representing fields given by runtime parsed formulas.
51  *
52  * Using libraries:
53  * https://github.com/flow123d/bparser/
54  *
55  * Allows parsing:
56  * - base variables: coordinates (x,y,z), time (t), surface depth (d); constants: e, pi
57  * - standard functions: trigonometric functions, min and max function, exponential function, ternary operator etc.
58  * - expressions dependendent on other fields
59  */
60 template <int spacedim, class Value>
61 class FieldFormula : public FieldAlgorithmBase<spacedim, Value>
62 {
63 public:
66 
67  TYPEDEF_ERR_INFO(EI_Field, std::string);
68  DECLARE_INPUT_EXCEPTION(ExcNotDoubleField,
69  << "Can not use integer valued field " << EI_Field::qval << " in the formula: \n");
70 
71  TYPEDEF_ERR_INFO(EI_BParserMsg, std::string);
72  TYPEDEF_ERR_INFO(EI_Formula, std::string);
73  DECLARE_INPUT_EXCEPTION(ExcParserError,
74  << "Parsing in " << EI_BParserMsg::val << " in the formula: " << EI_Formula::qval << "\n");
75 
76  // Temporary exception of FParser. TODO remove at the same time as FParser
77  TYPEDEF_ERR_INFO(EI_FParserMsg, std::string);
78  TYPEDEF_ERR_INFO(EI_Row, unsigned int);
79  TYPEDEF_ERR_INFO(EI_Col, unsigned int);
80  DECLARE_INPUT_EXCEPTION(ExcFParserError,
81  << "ParserError: " << EI_FParserMsg::val << "\n in the FieldFormula[" << EI_Row::val
82  << "][" << EI_Row::val << "] == " << EI_Formula::qval << " \n");
83 
84  FieldFormula(unsigned int n_comp=0);
85 
86 
87  static const Input::Type::Record & get_input_type();
88 
89  virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData& init_data);
90 
91  /**
92  * For time dependent formulas returns always true. For time independent formulas returns true only for the first time.
93  */
94  bool set_time(const TimeStep &time) override;
95 
96  /**
97  * Create SurfaceDepth object if surface region is set.
98  *
99  * See also description of the FieldBase<...>::set_mesh.
100  */
101  void set_mesh(const Mesh *mesh, bool boundary_domain) override;
102 
103  void cache_update(FieldValueCache<typename Value::element_type> &data_cache,
104  ElementCacheMap &cache_map, unsigned int region_patch_idx) override;
105 
106  /**
107  * Set reference of FieldSet.
108  */
109  std::vector<const FieldCommon *> set_dependency(FieldSet &field_set) override;
110 
111  /**
112  * Overload @p FieldAlgorithmBase::cache_reinit
113  *
114  * Reinit arena data member.
115  */
116  void cache_reinit(const ElementCacheMap &cache_map) override;
117 
118  virtual ~FieldFormula();
119 
120 private:
122 
123  /**
124  * Evaluate depth variable if it is contained in formula.
125  *
126  * Return arma vec of point coordinates extended by depth value (or zero if depth is not contained.
127  */
128  inline arma::vec eval_depth_var(const Point &p);
129 
130  // formula expression, string is set to BParser
131  std::string formula_;
132 
133  // Parser evaluating scalar, vector or tensor expression returned by formula_ string.
134  bparser::Parser b_parser_;
135 
136  /// Accessor to Input::Record
138 
139  /// Surface depth object calculate distance from surface.
140  std::shared_ptr<SurfaceDepth> surface_depth_;
141 
142  /// Flag indicates if depth variable 'd' is used in formula - obsolete parameter of FParser
144 
145  /// Flag indicates if time variable 't' is used in formula - parameter of BParser
146  bool has_time_;
147 
148  /// Helper variable for construct of arena, holds sum of sizes (over shape) of all dependent fields.
150 
151  /// Arena object providing data arrays
152  bparser::ArenaAlloc * arena_alloc_;
153 
154  // BParser data arrays and variables
155  double *X_; ///< Coordinates vector
156  double *x_; ///< Coordinates x, part of previous array
157  double *y_; ///< Coordinates y, part of previous array
158  double *z_; ///< Coordinates z, part of previous array
159  double *d_; ///< Surface depth variable, used optionally if 'd' variable is set
160  double *res_; ///< Result vector of BParser
161  uint *subsets_; ///< Subsets indices in range 0 ... n-1
163 
164  /**
165  * Data of fields evaluated in expressions.
166  *
167  * Temporary data member, we need to copy data from FieldValueCaches to arrays allocated in arena.
168  */
169  std::unordered_map<const FieldCommon *, double *> eval_field_data_;
170 
171  /// Registrar of class to factory
172  static const int registrar;
173 
174 
175 };
176 
177 
178 #endif /* FIELD_FORMULA_HH_ */
FieldFormula::x_
double * x_
Coordinates x, part of previous array.
Definition: field_formula.hh:156
FieldFormula::FactoryBaseType
FieldAlgorithmBase< spacedim, Value > FactoryBaseType
Definition: field_formula.hh:65
Armor::vec
ArmaVec< double, N > vec
Definition: armor.hh:885
time_governor.hh
Basic time management class.
field_algo_base.hh
FieldFormula::sum_shape_sizes_
uint sum_shape_sizes_
Helper variable for construct of arena, holds sum of sizes (over shape) of all dependent fields.
Definition: field_formula.hh:149
FieldFormula::eval_field_data_
std::unordered_map< const FieldCommon *, double * > eval_field_data_
Definition: field_formula.hh:169
ElementCacheMap
Directing class of FieldValueCache.
Definition: field_value_cache.hh:152
field_set.hh
FieldFormula::has_depth_var_
bool has_depth_var_
Flag indicates if depth variable 'd' is used in formula - obsolete parameter of FParser.
Definition: field_formula.hh:143
std::vector< const FieldCommon * >
ElementAccessor
Definition: dh_cell_accessor.hh:32
FieldFormula::res_
double * res_
Result vector of BParser.
Definition: field_formula.hh:160
StringTensorInput
Definition: field_values.hh:596
uint
unsigned int uint
Definition: mh_dofhandler.hh:101
FieldAlgorithmBase::Point
Space< spacedim >::Point Point
Definition: field_algo_base.hh:108
storage.hh
exceptions.hh
type_record.hh
FieldFormula::subsets_
uint * subsets_
Subsets indices in range 0 ... n-1.
Definition: field_formula.hh:161
FieldFormula::b_parser_
bparser::Parser b_parser_
Definition: field_formula.hh:134
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
FieldFormula::Point
FieldAlgorithmBase< spacedim, Value >::Point Point
Definition: field_formula.hh:64
FieldFormula::d_
double * d_
Surface depth variable, used optionally if 'd' variable is set.
Definition: field_formula.hh:159
FieldFormula::surface_depth_
std::shared_ptr< SurfaceDepth > surface_depth_
Surface depth object calculate distance from surface.
Definition: field_formula.hh:140
FieldAlgoBaseInitData
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
Definition: field_algo_base.hh:81
accessors.hh
TimeStep
Representation of one time step..
Definition: time_governor.hh:123
FieldFormula::STI
StringTensorInput< Value::NRows_, Value::NCols_ > STI
Definition: field_formula.hh:121
field_values.hh
input_exception.hh
SurfaceDepth
Definition: surface_depth.hh:30
FieldSet
Container for various descendants of FieldCommonBase.
Definition: field_set.hh:159
FieldFormula::in_rec_
Input::Record in_rec_
Accessor to Input::Record.
Definition: field_formula.hh:137
FieldFormula::required_fields_
std::vector< const FieldCommon * > required_fields_
Definition: field_formula.hh:162
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
Mesh
Definition: mesh.h:362
FieldAlgorithmBase
Definition: field_algo_base.hh:105
accessors_impl.hh
std
Definition: doxy_dummy_defs.hh:5
DECLARE_INPUT_EXCEPTION
#define DECLARE_INPUT_EXCEPTION(ExcName, Format)
Macro for simple definition of input exceptions.
Definition: input_exception.hh:69
Armor::Array
Definition: armor.hh:597
FieldFormula::has_time_
bool has_time_
Flag indicates if time variable 't' is used in formula - parameter of BParser.
Definition: field_formula.hh:146
FieldFormula::z_
double * z_
Coordinates z, part of previous array.
Definition: field_formula.hh:158
FieldFormula::y_
double * y_
Coordinates y, part of previous array.
Definition: field_formula.hh:157
FieldFormula::registrar
static const int registrar
Registrar of class to factory.
Definition: field_formula.hh:172
FieldFormula::formula_
std::string formula_
Definition: field_formula.hh:131
FieldFormula::X_
double * X_
Coordinates vector.
Definition: field_formula.hh:155
FieldFormula::arena_alloc_
bparser::ArenaAlloc * arena_alloc_
Arena object providing data arrays.
Definition: field_formula.hh:152
FieldFormula
Definition: field_formula.hh:61