Flow123d  last_with_con_2.0.0-4-g42e6930
field_interpolated_p0.impl.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_interpolated_p0.impl.hh
15  * @brief
16  */
17 
18 #ifndef FIELD_INTERPOLATED_P0_IMPL_HH_
19 #define FIELD_INTERPOLATED_P0_IMPL_HH_
20 
21 #include "field_interpolated_p0.hh"
22 #include "system/system.hh"
23 #include "mesh/msh_gmshreader.h"
24 #include "mesh/bih_tree.hh"
25 #include "mesh/reader_instances.hh"
27 #include "mesh/ngh/include/point.h"
28 #include "system/sys_profiler.hh"
29 
30 
31 namespace it = Input::Type;
32 
33 FLOW123D_FORCE_LINK_IN_CHILD(field_interpolated)
34 
35 
36 
37 template <int spacedim, class Value>
38 const Input::Type::Record & FieldInterpolatedP0<spacedim, Value>::get_input_type()
39 {
40  return it::Record("FieldInterpolatedP0", FieldAlgorithmBase<spacedim,Value>::template_name()+" Field constant in space.")
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  .close();
47 }
48 
49 
50 
51 template <int spacedim, class Value>
53  Input::register_class< FieldInterpolatedP0<spacedim, Value>, unsigned int >("FieldInterpolatedP0") +
55 
56 
57 template <int spacedim, class Value>
59 : FieldAlgorithmBase<spacedim, Value>(n_comp)
60 {}
61 
62 
63 
64 template <int spacedim, class Value>
66 
67  // read mesh, create tree
68  {
69  source_mesh_ = new Mesh();
70  reader_file_ = FilePath( rec.val<FilePath>("gmsh_file") );
72  // no call to mesh->setup_topology, we need only elements, no connectivity
73  }
75 
76  // allocate data_
77  unsigned int data_size = source_mesh_->element.size() * (this->value_.n_rows() * this->value_.n_cols());
78  data_ = std::make_shared<std::vector<typename Value::element_type>>();
79  data_->resize(data_size);
80 
81  field_name_ = rec.val<std::string>("field_name");
82 }
83 
84 
85 
86 
87 template <int spacedim, class Value>
89  OLD_ASSERT(source_mesh_, "Null mesh pointer of elementwise field: %s, did you call init_from_input(Input::Record)?\n", field_name_.c_str());
90  if ( reader_file_ == FilePath() ) return false;
91 
92  //walkaround for the steady time governor - there is no data to be read in time==infinity
93  //TODO: is it possible to check this before calling set_time?
94  //if (time == numeric_limits< double >::infinity()) return false;
95 
96  // value of last computed element must be recalculated if time is changed
97  computed_elm_idx_ = numeric_limits<unsigned int>::max();
98 
99  GMSH_DataHeader search_header;
100  search_header.actual = false;
101  search_header.field_name = field_name_;
102  search_header.n_components = this->value_.n_rows() * this->value_.n_cols();
103  search_header.n_entities = source_mesh_->element.size();
104  search_header.time = time.end();
105 
106  bool boundary_domain_ = false;
107  data_ = ReaderInstances::instance()->get_reader(reader_file_)->get_element_data<typename Value::element_type>(search_header,
108  source_mesh_->elements_id_maps(boundary_domain_), this->component_idx_);
109 
110  return search_header.actual;
111 }
112 
113 
114 
115 template <int spacedim, class Value>
116 typename Value::return_type const &FieldInterpolatedP0<spacedim, Value>::value(const Point &p, const ElementAccessor<spacedim> &elm)
117 {
118  OLD_ASSERT( elm.is_elemental(), "FieldInterpolatedP0 works only for 'elemental' ElementAccessors.\n");
119  if (elm.idx() != computed_elm_idx_ || elm.is_boundary() != computed_elm_boundary_) {
120  computed_elm_idx_ = elm.idx();
122 
123  if (elm.dim() == 3) {
124  xprintf(Err, "Dimension of element in target mesh must be 0, 1 or 2! elm.idx() = %d\n", elm.idx());
125  }
126 
127  double epsilon = 4* numeric_limits<double>::epsilon() * elm.element()->measure();
128 
129  // gets suspect elements
130  if (elm.dim() == 0) {
131  searched_elements_.clear();
132  ((BIHTree *)bih_tree_)->find_point(elm.element()->node[0]->point(), searched_elements_);
133  } else {
134  BoundingBox bb;
135  elm.element()->get_bounding_box(bb);
136  searched_elements_.clear();
137  ((BIHTree *)bih_tree_)->find_bounding_box(bb, searched_elements_);
138  }
139 
140  // set zero values of value_ object
141  for (unsigned int i=0; i < this->value_.n_rows(); i++) {
142  for (unsigned int j=0; j < this->value_.n_cols(); j++) {
143  this->value_(i,j) = 0.0;
144  }
145  }
146 
147  double total_measure=0.0, measure;
148  TIntersectionType iType;
149 
150  START_TIMER("compute_pressure");
153  {
155  if (ele->dim() == 3) {
157  // get intersection (set measure = 0 if intersection doesn't exist)
158  switch (elm.dim()) {
159  case 0: {
161  if ( tetrahedron_.IsInner(point_) ) {
162  measure = 1.0;
163  } else {
164  measure = 0.0;
165  }
166  break;
167  }
168  case 1: {
170  GetIntersection(abscissa_, tetrahedron_, iType, measure);
171  if (iType != line) {
172  measure = 0.0;
173  }
174  break;
175  }
176  case 2: {
178  GetIntersection(triangle_, tetrahedron_, iType, measure);
179  if (iType != area) {
180  measure = 0.0;
181  }
182  break;
183  }
184  }
185 
186 
187 
188  //adds values to value_ object if intersection exists
189  if (measure > epsilon) {
190  unsigned int index = this->value_.n_rows() * this->value_.n_cols() * (*it);
192  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])) );
193  Value tmp_value = Value( ret_type_value );
194 
195  for (unsigned int i=0; i < this->value_.n_rows(); i++) {
196  for (unsigned int j=0; j < this->value_.n_cols(); j++) {
197  this->value_(i,j) += tmp_value(i,j) * measure;
198  }
199  }
200  total_measure += measure;
201  }
202  }
203  }
204 
205  // computes weighted average
206  if (total_measure > epsilon) {
207  for (unsigned int i=0; i < this->value_.n_rows(); i++) {
208  for (unsigned int j=0; j < this->value_.n_cols(); j++) {
209  this->value_(i,j) /= total_measure;
210  }
211  }
212  } else {
213  WarningOut().fmt("Processed element with idx {} is out of source mesh!\n", elm.idx());
214  }
215  END_TIMER("compute_pressure");
216 
217  }
218  return this->r_value_;
219 }
220 
221 
222 
223 template <int spacedim, class Value>
226 {
227  OLD_ASSERT( elm.is_elemental(), "FieldInterpolatedP0 works only for 'elemental' ElementAccessors.\n");
228  FieldAlgorithmBase<spacedim, Value>::value_list(point_list, elm, value_list);
229 }
230 
231 
232 
233 
234 
235 #endif /* FIELD_INTERPOLATED_P0_IMPL_HH_ */
unsigned int computed_elm_boundary_
stored flag if last computed element is boundary
std::string field_name
const Element * element() const
Definition: accessors.hh:86
Bounding box in 3d ambient space.
Definition: bounding_box.hh:45
double measure() const
Definition: elements.cc:89
bool set_time(const TimeStep &time) override
void set_point_from_element(TPoint &p, const Element *ele)
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
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:99
FilePath reader_file_
mesh reader file
virtual Value::return_type const & value(const Point &p, const ElementAccessor< spacedim > &elm)
Definition: mesh.h:95
Node ** node
Definition: elements.h:79
unsigned int computed_elm_idx_
stored index to last computed element
#define ADD_CALLS(n_calls)
Increase number of calls in actual timer.
TAbscissa abscissa_
1D (abscissa) element, used for computing intersection
TPoint point_
0D (point) element, used for computing intersection
Value::return_type r_value_
Record & close() const
Close the Record for further declarations of keys.
Definition: type_record.cc:286
void set_tetrahedron_from_element(TTetrahedron &te, Element *ele)
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.
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
static FileName input()
The factory function for declaring type FileName for input files.
Definition: type_base.hh:606
Mesh * source_mesh_
mesh, which is interpolated
vector< int > const & elements_id_maps(bool boundary_domain) const
Definition: mesh.cc:706
Accessor to the data with type Type::Record.
Definition: accessors.hh:277
const Ret val(const string &key) const
#define xprintf(...)
Definition: system.hh:87
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
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:468
void get_bounding_box(BoundingBox &bounding_box) const
Definition: elements.cc:196
enum Intersections TIntersectionType
double end() const
Space< spacedim >::Point Point
unsigned int n_components
Number of values on one row.
BIHTree * bih_tree_
tree of mesh elements
Dedicated class for storing path to input and output files.
Definition: file_path.hh:48
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
virtual void init_from_input(const Input::Record &rec)
void set_triangle_from_element(TTriangle &tr, const Element *ele)
TTriangle triangle_
2D (triangle) element, used for computing intersection
std::shared_ptr< GmshMeshReader > get_reader(const FilePath &file_path)
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:234
#define END_TIMER(tag)
Ends a timer with specified tag.
Definition: system.hh:59
TTetrahedron tetrahedron_
3D (tetrahedron) element, used for computing intersection
Record type proxy class.
Definition: type_record.hh:171
unsigned int dim() const
Definition: accessors.hh:83
std::string field_name_
field name read from input
arma::vec3 & point()
Definition: nodes.hh:68
static ReaderInstances * instance()
Returns singleton instance.
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:568
void GetIntersection(const TBisector &, const TBisector &, TPosition &, double &, double &)
Representation of one time step..
#define FLOW123D_FORCE_LINK_IN_CHILD(x)
Definition: global_defs.h:180
bool IsInner(const TPoint &) const
ElementVector element
Vector of elements of the mesh.
Definition: mesh.h:221