Flow123d  last_with_con_2.0.0-663-gd0e2296
field_elementwise.cc
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_elementwise.cc
15  * @brief
16  */
17 
18 
20 #include "fields/field_instances.hh" // for instantiation macros
21 #include "system/file_path.hh"
22 #include "input/input_type.hh"
23 #include "mesh/msh_gmshreader.h"
24 #include "mesh/reader_instances.hh"
25 
26 /// Implementation.
27 
28 namespace IT = Input::Type;
29 
30 FLOW123D_FORCE_LINK_IN_CHILD(field_elementwise)
31 
32 
33 template <int spacedim, class Value>
34 const Input::Type::Record & FieldElementwise<spacedim, Value>::get_input_type()
35 {
36  return IT::Record("FieldElementwise", FieldAlgorithmBase<spacedim,Value>::template_name()+" Field constant in space.")
40  "Input file with ASCII GMSH file format.")
42  "The values of the Field are read from the ```$ElementData``` section with field name given by this key.")
43  //.declare_key("unit", FieldAlgorithmBase<spacedim, Value>::get_input_type_unit_si(), IT::Default::optional(),
44  // "Definition of unit.")
45  .close();
46 }
47 
48 
49 template <int spacedim, class Value>
51  Input::register_class< FieldElementwise<spacedim, Value>, unsigned int >("FieldElementwise") +
53 
54 
55 
56 template <int spacedim, class Value>
58 : FieldAlgorithmBase<spacedim, Value>(n_comp),
59  internal_raw_data(true), mesh_(NULL), unit_si_( UnitSI::dimensionless() )
60 
61 {
62  n_components_ = this->value_.n_rows() * this->value_.n_cols();
63 }
64 
65 
66 
67 template <int spacedim, class Value>
69  unsigned int n_components)
70 : FieldAlgorithmBase<spacedim, Value>(n_components),
71 internal_raw_data(false), mesh_(NULL), unit_si_( UnitSI::dimensionless() )
72 {
73  n_components_ = this->value_.n_rows() * this->value_.n_cols();
74  data_ = data;
75  this->scale_and_check_limits();
76 }
77 
78 
79 
80 
81 template <int spacedim, class Value>
83  this->init_unit_conversion_coefficient(rec, init_data);
84  this->in_rec_ = rec;
85  this->unit_si_ = init_data.unit_si_;
86  this->limits_ = init_data.limits_;
87 
88  DebugOut() << "Reader file: " << string(reader_file_);
89  ASSERT(internal_raw_data).error("Trying to initialize internal FieldElementwise from input.");
90  ASSERT(reader_file_ == FilePath()).error("Multiple call of init_from_input.");
91  reader_file_ = FilePath( rec.val<FilePath>("gmsh_file") );
93 
94  field_name_ = rec.val<std::string>("field_name");
95 }
96 
97 
98 
99 /*template <int spacedim, class Value>
100 void FieldElementwise<spacedim, Value>::set_data_row(unsigned int boundary_idx, typename Value::return_type &value) {
101  Value ref(value);
102  OLD_ASSERT( this->value_.n_cols() == ref.n_cols(), "Size of variable vectors do not match.\n" );
103  OLD_ASSERT( mesh_, "Null mesh pointer of elementwise field: %s, did you call set_mesh()?\n", field_name_.c_str());
104  OLD_ASSERT( boundary_domain_ , "Method set_data_row can be used only for boundary fields.");
105  unsigned int vec_pos = boundary_idx * n_components_;
106  std::vector<typename Value::element_type> &vec = *( data_.get() );
107  for(unsigned int row=0; row < ref.n_rows(); row++)
108  for(unsigned int col=0; col < ref.n_cols(); col++, vec_pos++)
109  vec[vec_pos] = ref(row,col);
110 
111 }*/
112 
113 
114 template <int spacedim, class Value>
116  OLD_ASSERT(mesh_, "Null mesh pointer of elementwise field: %s, did you call set_mesh()?\n", field_name_.c_str());
117  if ( reader_file_ == FilePath() ) return false;
118 
119  //walkaround for the steady time governor - there is no data to be read in time==infinity
120  //TODO: is it possible to check this before calling set_time?
121  //if (time.end() == numeric_limits< double >::infinity()) return false;
122 
123  GMSH_DataHeader search_header;
124  search_header.actual=false;
125  search_header.field_name=field_name_;
126  search_header.n_components=n_components_;
127  search_header.n_entities=n_entities_;
128  search_header.time=time.end();
129 
130 
131  data_ = ReaderInstances::instance()->get_reader(reader_file_)-> template get_element_data<typename Value::element_type>(search_header,
133  this->scale_and_check_limits();
134  return search_header.actual;
135 }
136 
137 
138 
139 template <int spacedim, class Value>
140 void FieldElementwise<spacedim, Value>::set_mesh(const Mesh *mesh, bool boundary_domain) {
141  // set mesh only once or to same value
142  OLD_ASSERT(mesh_ == nullptr || mesh_ == mesh, "Trying to change mesh of the FieldElementwise.");
143  boundary_domain_ = boundary_domain;
144 
145  mesh_=mesh;
146  if (boundary_domain_) {
148  } else {
150  }
151 
152  // allocate
153  if (!data_) {
154  data_ = std::make_shared<std::vector<typename Value::element_type>>();
155  data_->resize(n_entities_ * n_components_);
156  }
157 
158 }
159 
160 
161 
162 /**
163  * Returns one value in one given point. ResultType can be used to avoid some costly calculation if the result is trivial.
164  */
165 template <int spacedim, class Value>
166 typename Value::return_type const & FieldElementwise<spacedim, Value>::value(const Point &p, const ElementAccessor<spacedim> &elm)
167 {
168  OLD_ASSERT( elm.is_elemental(), "FieldElementwise works only for 'elemental' ElementAccessors.\n");
169  OLD_ASSERT( elm.is_boundary() == boundary_domain_, "Trying to get value of FieldElementwise '%s' for wrong ElementAccessor type (boundary/bulk).\n", field_name_.c_str() );
170 
171  unsigned int idx = n_components_*elm.idx();
173 
174  return Value::from_raw(this->r_value_, (typename Value::element_type *)(&vec[idx]));
175 }
176 
177 
178 
179 /**
180  * Returns std::vector of scalar values in several points at once.
181  */
182 template <int spacedim, class Value>
185 {
186  OLD_ASSERT( elm.is_elemental(), "FieldElementwise works only for 'elemental' ElementAccessors.\n");
187  OLD_ASSERT( elm.is_boundary() == boundary_domain_, "Trying to get value of FieldElementwise '%s' for wrong ElementAccessor type (boundary/bulk).\n", field_name_.c_str() );
188  OLD_ASSERT_EQUAL( point_list.size(), value_list.size() );
190  unsigned int idx = n_components_*elm.idx();
192 
193  typename Value::return_type const &ref = Value::from_raw(this->r_value_, (typename Value::element_type *)(&vec[idx]));
194  for(unsigned int i=0; i< value_list.size(); i++) {
195  OLD_ASSERT( Value(value_list[i]).n_rows()==this->value_.n_rows(),
196  "value_list[%d] has wrong number of rows: %d; should match number of components: %d\n",
197  i, Value(value_list[i]).n_rows(),this->value_.n_rows());
198 
199  value_list[i] = ref;
200  }
201  } else {
202  xprintf(UsrErr, "FieldElementwise is not implemented for discrete return types.\n");
203  }
204 }
205 
206 
207 
208 template <int spacedim, class Value>
210 {
211  if (Value::is_scalable()) {
213  for(unsigned int i=0; i<vec.size(); ++i) {
214  vec[i] *= this->unit_conversion_coefficient_;
215  if ( (vec[i] < limits_.first) || (vec[i] > limits_.second) ) {
216  int el_id = mesh_->elements_id_maps(boundary_domain_)[(unsigned int)(i / this->n_components_)];
217  WarningOut().fmt("Value '{}' of FieldElementwise '{}', element id '{}' at address '{}' is out of limits: <{}, {}>\n"
218  "Unit of the Field: [{}]\n",
219  vec[i], field_name_, el_id, in_rec_.address_string(), limits_.first, limits_.second, unit_si_.format_text() );
220  }
221  }
222  }
223 }
224 
225 
226 
227 template <int spacedim, class Value>
229 
230 
231 // Instantiations of FieldElementwise
std::string field_name
void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Init value of unit_conversion_coefficient_ from input.
std::shared_ptr< std::vector< typename Value::element_type > > data_
Raw buffer of n_entities rows each containing Value::size() doubles.
std::string format_text() const
Definition: unit_si.cc:127
std::pair< double, double > limits_
Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm) override
unsigned int component_idx_
Specify if the field is part of a MultiField and which component it is.
bool is_boundary() const
We need this method after replacing Region by RegionIdx, and movinf RegionDB instance into particular...
Definition: accessors.hh:109
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:99
#define INSTANCE_ALL(field)
Definition: mesh.h:95
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
Value::return_type r_value_
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data) override
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
static constexpr bool value
Definition: json.hpp:87
double unit_conversion_coefficient_
Coeficient of conversion of user-defined unit.
virtual Record & derive_from(Abstract &parent)
Method to derive new Record from an AbstractRecord parent.
Definition: type_record.cc:195
unsigned int size() const
Returns size of the container. This is independent of the allocated space.
Definition: sys_vector.hh:391
#define OLD_ASSERT(...)
Definition: global_defs.h:131
unsigned int n_entities
Number of rows.
unsigned int n_elements() const
Definition: mesh.h:133
bool is_elemental() const
Definition: accessors.hh:75
static FileName input()
The factory function for declaring type FileName for input files.
Definition: type_base.hh:637
bool set_time(const TimeStep &time) override
vector< int > const & elements_id_maps(bool boundary_domain) const
Definition: mesh.cc:709
void scale_and_check_limits()
Multiply data_ with unit_conversion_coefficient_ and check limits of field.
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:87
FieldElementwise(unsigned int n_comp=0)
Record & declare_key(const string &key, std::shared_ptr< TypeBase > type, const Default &default_value, const string &description, TypeBase::attribute_map key_attributes=TypeBase::attribute_map())
Declares a new key of the Record.
Definition: type_record.cc:484
ElementVector bc_elements
Definition: mesh.h:224
double end() const
Space< spacedim >::Point Point
unsigned int n_components
Number of values on one row.
Record & copy_keys(const Record &other)
Copy keys from other record.
Definition: type_record.cc:215
Dedicated class for storing path to input and output files.
Definition: file_path.hh:48
Definition: system.hh:59
unsigned int n_components_
Size of Value.
const UnitSI & unit_si_
void set_mesh(const Mesh *mesh, bool boundary_domain) override
Value value_
Last value, prevents passing large values (vectors) by value.
unsigned int n_entities_
Number of rows in data_ buffer.
std::shared_ptr< GmshMeshReader > get_reader(const FilePath &file_path)
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:234
void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list) override
#define OLD_ASSERT_EQUAL(a, b)
Definition: global_defs.h:133
std::pair< double, double > limits_
Record type proxy class.
Definition: type_record.hh:171
Input::Record in_rec_
Accessor to Input::Record.
Class for representation SI units of Fields.
Definition: unit_si.hh:40
static ReaderInstances * instance()
Returns singleton instance.
#define DebugOut()
Macro defining &#39;debug&#39; record of log.
Definition: logger.hh:240
unsigned int idx() const
Definition: accessors.hh:113
Class for declaration of the input data that are in string format.
Definition: type_base.hh:599
Representation of one time step..
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
string address_string() const
Definition: accessors.cc:175