Flow123d  release_2.2.0-914-gf1a3a4f
field_interpolated_p0.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_interpolated_p0.cc
15  * @brief
16  */
17 
18 
19 #include "field_interpolated_p0.hh"
20 #include "fields/field_instances.hh" // for instantiation macros
21 #include "system/system.hh"
22 #include "io/msh_gmshreader.h"
23 #include "mesh/bih_tree.hh"
24 #include "io/reader_cache.hh"
26 #include "mesh/ngh/include/point.h"
27 #include "system/sys_profiler.hh"
28 
29 
30 namespace it = Input::Type;
31 
32 FLOW123D_FORCE_LINK_IN_CHILD(field_interpolated)
33 
34 
35 
36 template <int spacedim, class Value>
37 const Input::Type::Record & FieldInterpolatedP0<spacedim, Value>::get_input_type()
38 {
39  return it::Record("FieldInterpolatedP0", FieldAlgorithmBase<spacedim,Value>::template_name()+" Field interpolated from external mesh data and piecewise constant on mesh elements.")
43  "Input file with ASCII GMSH file format.")
45  "The values of the Field are read from the ```$ElementData``` section with field name given by this key.")
46  //.declare_key("unit", FieldAlgorithmBase<spacedim, Value>::get_input_type_unit_si(), it::Default::optional(),
47  // "Definition of unit.")
48  .close();
49 }
50 
51 
52 
53 template <int spacedim, class Value>
55  Input::register_class< FieldInterpolatedP0<spacedim, Value>, unsigned int >("FieldInterpolatedP0") +
57 
58 
59 template <int spacedim, class Value>
61 : FieldAlgorithmBase<spacedim, Value>(n_comp)
62 {}
63 
64 
65 
66 template <int spacedim, class Value>
68  this->init_unit_conversion_coefficient(rec, init_data);
69 
70 
71  // read mesh, create tree
72  {
73  reader_file_ = FilePath( rec.val<FilePath>("mesh_data_file") );
75  // no call to mesh->setup_topology, we need only elements, no connectivity
76  }
77  bih_tree_ = new BIHTree( source_mesh_.get() );
78 
79  // allocate data_
80  unsigned int data_size = source_mesh_->element.size() * (this->value_.n_rows() * this->value_.n_cols());
81  data_ = std::make_shared<std::vector<typename Value::element_type>>();
82  data_->resize(data_size);
83 
84  field_name_ = rec.val<std::string>("field_name");
85 }
86 
87 
88 
89 
90 template <int spacedim, class Value>
92  OLD_ASSERT(source_mesh_, "Null mesh pointer of elementwise field: %s, did you call init_from_input(Input::Record)?\n", field_name_.c_str());
93  if ( reader_file_ == FilePath() ) return false;
94 
95  //walkaround for the steady time governor - there is no data to be read in time==infinity
96  //TODO: is it possible to check this before calling set_time?
97  //if (time == numeric_limits< double >::infinity()) return false;
98 
99  // value of last computed element must be recalculated if time is changed
100  computed_elm_idx_ = numeric_limits<unsigned int>::max();
101 
102  bool boundary_domain_ = false;
103  BaseMeshReader::HeaderQuery header_query(field_name_, time.end(), OutputTime::DiscreteSpace::ELEM_DATA);
104  ReaderCache::get_reader(reader_file_ )->find_header(header_query);
105  data_ = ReaderCache::get_reader(reader_file_ )->template get_element_data<typename Value::element_type>(
106  source_mesh_->element.size(), this->value_.n_rows() * this->value_.n_cols(), boundary_domain_, this->component_idx_);
107  this->scale_data();
108 
109  return true;
110 }
111 
112 
113 
114 template <int spacedim, class Value>
115 typename Value::return_type const &FieldInterpolatedP0<spacedim, Value>::value(const Point &p, const ElementAccessor<spacedim> &elm)
116 {
117  OLD_ASSERT( elm.is_elemental(), "FieldInterpolatedP0 works only for 'elemental' ElementAccessors.\n");
118  if (elm.idx() != computed_elm_idx_ || elm.is_boundary() != computed_elm_boundary_) {
119  computed_elm_idx_ = elm.idx();
121 
122  if (elm.dim() == 3) {
123  xprintf(Err, "Dimension of element in target mesh must be 0, 1 or 2! elm.idx() = %d\n", elm.idx());
124  }
125 
126  double epsilon = 4* numeric_limits<double>::epsilon() * elm.element()->measure();
127 
128  // gets suspect elements
129  if (elm.dim() == 0) {
130  searched_elements_.clear();
131  ((BIHTree *)bih_tree_)->find_point(elm.element()->node[0]->point(), searched_elements_);
132  } else {
133  BoundingBox bb;
134  elm.element()->get_bounding_box(bb);
135  searched_elements_.clear();
136  ((BIHTree *)bih_tree_)->find_bounding_box(bb, searched_elements_);
137  }
138 
139  // set zero values of value_ object
140  for (unsigned int i=0; i < this->value_.n_rows(); i++) {
141  for (unsigned int j=0; j < this->value_.n_cols(); j++) {
142  this->value_(i,j) = 0.0;
143  }
144  }
145 
146  double total_measure=0.0, measure;
148 
149  START_TIMER("compute_pressure");
152  {
153  ElementFullIter ele = source_mesh_->element( *it );
154  if (ele->dim() == 3) {
156  // get intersection (set measure = 0 if intersection doesn't exist)
157  switch (elm.dim()) {
158  case 0: {
160  if ( tetrahedron_.IsInner(point_) ) {
161  measure = 1.0;
162  } else {
163  measure = 0.0;
164  }
165  break;
166  }
167  case 1: {
169  ngh::GetIntersection(abscissa_, tetrahedron_, iType, measure);
170  if (iType != ngh::line) {
171  measure = 0.0;
172  }
173  break;
174  }
175  case 2: {
177  ngh::GetIntersection(triangle_, tetrahedron_, iType, measure);
178  if (iType != ngh::area) {
179  measure = 0.0;
180  }
181  break;
182  }
183  }
184 
185 
186 
187  //adds values to value_ object if intersection exists
188  if (measure > epsilon) {
189  unsigned int index = this->value_.n_rows() * this->value_.n_cols() * (*it);
191  typename Value::return_type & ret_type_value = const_cast<typename Value::return_type &>( Value::from_raw(this->r_value_, (typename Value::element_type *)(&vec[index])) );
192  Value tmp_value = Value( ret_type_value );
193 
194  for (unsigned int i=0; i < this->value_.n_rows(); i++) {
195  for (unsigned int j=0; j < this->value_.n_cols(); j++) {
196  this->value_(i,j) += tmp_value(i,j) * measure;
197  }
198  }
199  total_measure += measure;
200  }
201  }
202  }
203 
204  // computes weighted average
205  if (total_measure > epsilon) {
206  for (unsigned int i=0; i < this->value_.n_rows(); i++) {
207  for (unsigned int j=0; j < this->value_.n_cols(); j++) {
208  this->value_(i,j) /= total_measure;
209  }
210  }
211  } else {
212  WarningOut().fmt("Processed element with idx {} is out of source mesh!\n", elm.idx());
213  }
214  END_TIMER("compute_pressure");
215 
216  }
217  return this->r_value_;
218 }
219 
220 
221 
222 template <int spacedim, class Value>
225 {
226  OLD_ASSERT( elm.is_elemental(), "FieldInterpolatedP0 works only for 'elemental' ElementAccessors.\n");
227  FieldAlgorithmBase<spacedim, Value>::value_list(point_list, elm, value_list);
228 }
229 
230 
231 
232 template <int spacedim, class Value>
234 {
235  if (Value::is_scalable()) {
237  for(unsigned int i=0; i<vec.size(); ++i)
238  vec[i] *= this->unit_conversion_coefficient_;
239  }
240 }
241 
242 
243 
244 // Instantiations of FieldInterpolatedP0
unsigned int computed_elm_boundary_
stored flag if last computed element is boundary
void GetIntersection(const TBisector &, const TBisector &, TPosition &, double &, double &)
void init_unit_conversion_coefficient(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
Init value of unit_conversion_coefficient_ from input.
const Element * element() const
Definition: accessors.hh:86
Bounding box in 3d ambient space.
Definition: bounding_box.hh:45
double measure() const
Computes the measure of the element.
Definition: elements.cc:90
bool set_time(const TimeStep &time) override
void set_point_from_element(TPoint &p, const Element *ele)
bool IsInner(const TPoint &) const
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
Abstract linear system class.
Definition: equation.hh:37
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)=0
static Default obligatory()
The factory function to make an empty default value which is obligatory.
Definition: type_record.hh:110
FilePath reader_file_
mesh reader file
virtual void init_from_input(const Input::Record &rec, const struct FieldAlgoBaseInitData &init_data)
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm)
#define INSTANCE_ALL(field)
Helper struct stores data for initizalize descentants of FieldAlgorithmBase.
Node ** node
Definition: elements.h:90
unsigned int computed_elm_idx_
stored index to last computed element
#define ADD_CALLS(n_calls)
Increase number of calls in actual timer.
Value::return_type r_value_
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:303
ngh::TAbscissa abscissa_
1D (abscissa) element, used for computing intersection
void set_tetrahedron_from_element(TTetrahedron &te, Element *ele)
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
#define OLD_ASSERT(...)
Definition: global_defs.h:131
virtual void value_list(const std::vector< Point > &point_list, const ElementAccessor< spacedim > &elm, std::vector< typename Value::return_type > &value_list)
bool is_elemental() const
Definition: accessors.hh:75
ngh::TPoint point_
0D (point) element, used for computing intersection
static FileName input()
The factory function for declaring type FileName for input files.
Definition: type_base.cc:526
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:92
FieldInterpolatedP0(unsigned int n_comp=0)
#define START_TIMER(tag)
Starts a timer with specified tag.
void set_abscissa_from_element(TAbscissa &ab, const Element *ele)
Class for O(log N) lookup for intersections with a set of bounding boxes.
Definition: bih_tree.hh:36
std::shared_ptr< Mesh > source_mesh_
mesh, which is interpolated
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:490
void get_bounding_box(BoundingBox &bounding_box) const
Definition: elements.cc:206
double end() const
Space< spacedim >::Point Point
BIHTree * bih_tree_
tree of mesh elements
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:54
ngh::TTetrahedron tetrahedron_
3D (tetrahedron) element, used for computing intersection
const double epsilon
Definition: mathfce.h:23
Value value_
Last value, prevents passing large values (vectors) by value.
std::vector< unsigned int > searched_elements_
vector stored suspect elements in calculating the intersection
enum ngh::Intersections TIntersectionType
void scale_data()
Multiply data_ with unit_conversion_coefficient_.
void set_triangle_from_element(TTriangle &tr, const Element *ele)
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:246
#define END_TIMER(tag)
Ends a timer with specified tag.
Definition: system.hh:64
static std::shared_ptr< Mesh > get_mesh(const FilePath &file_path)
Definition: reader_cache.cc:38
Record type proxy class.
Definition: type_record.hh:182
ngh::TTriangle triangle_
2D (triangle) element, used for computing intersection
unsigned int dim() const
Definition: accessors.hh:83
static std::shared_ptr< BaseMeshReader > get_reader(const FilePath &file_path)
Definition: reader_cache.cc:34
std::string field_name_
field name read from input
arma::vec3 & point()
Definition: nodes.hh:68
unsigned int idx() const
Definition: accessors.hh:113
std::shared_ptr< std::vector< typename Value::element_type > > data_
Raw buffer of n_entities rows each containing Value::size() doubles.
Class for declaration of the input data that are in string format.
Definition: type_base.hh:588
Representation of one time step..
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180