Flow123d  JS_before_hm-1828-g90ad75301
msh_basereader.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 msh_basereader.cc
15  * @brief
16  * @author dalibor
17  */
18 
19 
20 #include "io/msh_basereader.hh"
21 #include "io/msh_gmshreader.h"
22 #include "io/msh_vtkreader.hh"
23 #include "io/msh_pvdreader.hh"
24 #include "mesh/mesh.h"
25 #include "system/sys_profiler.hh"
26 
27 
29 : element_data_values_(std::make_shared<ElementDataFieldMap>()),
30  tok_(file_name)
31 {}
32 
33 BaseMeshReader::BaseMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values)
34 : element_data_values_(element_data_values),
35  tok_(file_name)
36 {}
37 
38 std::shared_ptr< BaseMeshReader > BaseMeshReader::reader_factory(const FilePath &file_name) {
39  std::shared_ptr<BaseMeshReader> reader_ptr;
40  if ( file_name.extension() == ".msh" ) {
41  reader_ptr = std::make_shared<GmshMeshReader>(file_name);
42  } else if ( file_name.extension() == ".vtu" ) {
43  reader_ptr = std::make_shared<VtkMeshReader>(file_name);
44  } else if ( file_name.extension() == ".pvd" ) {
45  reader_ptr = std::make_shared<PvdMeshReader>(file_name);
46  } else {
47  THROW(ExcWrongExtension() << EI_FileExtension(file_name.extension()) << EI_MeshFile((string)file_name) );
48  }
49  return reader_ptr;
50 }
51 
53  START_TIMER("BaseMeshReader - mesh factory");
54 
55  Input::Array region_list;
56  Mesh * mesh = new Mesh( input_mesh_rec );
57 
58  try {
59  auto file = input_mesh_rec.val<FilePath>("mesh_file");
60  std::shared_ptr< BaseMeshReader > reader = BaseMeshReader::reader_factory(file);
61  reader->read_physical_names(mesh);
62  if (input_mesh_rec.opt_val("regions", region_list)) {
63  mesh->read_regions_from_input(region_list);
64  }
65  reader->read_raw_mesh(mesh);
66  } INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, input_mesh_rec)
67 
68  mesh->setup_topology();
69  mesh->check_and_finish();
70  return mesh;
71 
72 }
73 
75  ASSERT(mesh).error("Argument mesh is NULL.\n");
76  tok_.set_position( Tokenizer::Position() );
77  read_nodes(mesh);
78  read_elements(mesh);
79 }
80 
81 
83  if (boundary_domain) return boundary_elements_id_;
84  else return bulk_elements_id_;
85 }
86 
87 
88 template<typename T>
89 typename ElementDataCache<T>::ComponentDataPtr BaseMeshReader::get_element_data( unsigned int n_entities, unsigned int n_components,
90  bool boundary_domain, unsigned int component_idx) {
92  .error("Vector of mapping VTK to GMSH element is not initialized. Did you call check_compatible_mesh?");
93  ASSERT(actual_header_.field_name != "").error("Unset MeshDataHeader. Did you call find_header?\n");
94 
95  std::string field_name = actual_header_.field_name;
96 
97  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
98  if (it == element_data_values_->end()) {
99  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >();
100  it=element_data_values_->find(field_name);
101  }
102 
103  if ( !it->second->is_actual(actual_header_.time, field_name) ) {
104  unsigned int size_of_cache; // count of vectors stored in cache
105 
106  // check that the header is valid, try to correct
107  if (actual_header_.n_entities != n_entities) {
108  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of entities: {}, using {} instead.\n",
109  tok_.f_name(), data_section_name_, field_name, actual_header_.time, actual_header_.n_entities, n_entities);
110  // actual_header.n_entities=n_entities;
111  }
112 
113  if (n_components == 1) {
114  // read for MultiField to 'n_comp' vectors
115  // or for Field if ElementData contains only one value
116  size_of_cache = actual_header_.n_components;
117  }
118  else {
119  // read for Field if more values is stored to one vector
120  size_of_cache = 1;
121  if (actual_header_.n_components != n_components) {
122  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of components: {}, using {} instead.\n",
123  tok_.f_name(), data_section_name_, field_name, actual_header_.time, actual_header_.n_components, n_components);
124  actual_header_.n_components=n_components;
125  }
126  }
127 
128  (*element_data_values_)[field_name]
129  = std::make_shared< ElementDataCache<T> >(field_name, actual_header_.time, size_of_cache, n_components*n_entities);
130  this->read_element_data(*(it->second), actual_header_, n_components, boundary_domain );
131  }
132 
134 
135  if (component_idx == std::numeric_limits<unsigned int>::max() ||
137  component_idx = 0;
138  ElementDataCache<T> &current_cache = dynamic_cast<ElementDataCache<T> &>(*(it->second));
139  return current_cache.get_component_data(component_idx);
140 }
141 
142 CheckResult BaseMeshReader::scale_and_check_limits(string field_name, double coef, double default_val, double lower_bound,
143  double upper_bound) {
144  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
145  ASSERT(it != element_data_values_->end())(field_name);
146 
147  std::shared_ptr< ElementDataCache<double> > current_cache = dynamic_pointer_cast<ElementDataCache<double> >(it->second);
148  ASSERT(current_cache)(field_name).error("scale_and_check_limits can be call only for scalable fields!\n");
149 
150  CheckResult check_val = current_cache->check_values(default_val, lower_bound, upper_bound);
151  current_cache->scale_data(coef);
152  return check_val;
153 }
154 
155 
156 
157 // explicit instantiation of template methods
158 #define MESH_READER_GET_ELEMENT_DATA(TYPE) \
159 template typename ElementDataCache<TYPE>::ComponentDataPtr BaseMeshReader::get_element_data<TYPE>(unsigned int n_entities, \
160  unsigned int n_components, bool boundary_domain, unsigned int component_idx);
161 
163 MESH_READER_GET_ELEMENT_DATA(unsigned int)
165 
BaseMeshReader::actual_header_
MeshDataHeader actual_header_
Header of actual loaded data.
Definition: msh_basereader.hh:257
BaseMeshReader::tok_
Tokenizer tok_
Tokenizer used for reading ASCII file format.
Definition: msh_basereader.hh:250
MESH_READER_GET_ELEMENT_DATA
#define MESH_READER_GET_ELEMENT_DATA(TYPE)
Definition: msh_basereader.cc:158
BaseMeshReader::scale_and_check_limits
CheckResult scale_and_check_limits(string field_name, double coef, double default_val, double lower_bound=-std::numeric_limits< double >::max(), double upper_bound=std::numeric_limits< double >::max())
Definition: msh_basereader.cc:142
BaseMeshReader::read_element_data
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain)=0
BaseMeshReader::boundary_elements_id_
vector< LongIdx > boundary_elements_id_
Definition: msh_basereader.hh:254
BaseMeshReader::MeshDataHeader::reset
void reset()
Set field_name value to empty string, that signs invalid header (using after reading data)
Definition: msh_basereader.hh:119
msh_basereader.hh
BaseMeshReader::reader_factory
static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name)
Definition: msh_basereader.cc:38
ElementDataCache
Definition: element_data_cache.hh:44
ElementDataCache::get_component_data
ComponentDataPtr get_component_data(unsigned int component_idx)
Return vector of element data for get component.
Definition: element_data_cache.cc:71
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
FilePath::extension
string extension() const
Definition: file_path.cc:198
Input::Record::val
const Ret val(const string &key) const
Definition: accessors_impl.hh:31
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
BaseMeshReader::read_nodes
virtual void read_nodes(Mesh *mesh)=0
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
std::vector< int >
BaseMeshReader::MeshDataHeader::n_components
unsigned int n_components
Number of values on one row.
Definition: msh_basereader.hh:129
Mesh::read_regions_from_input
void read_regions_from_input(Input::Array region_list)
Definition: mesh.cc:979
msh_gmshreader.h
BaseMeshReader::get_element_vector
const std::vector< int > & get_element_vector(bool boundary_domain)
Definition: msh_basereader.cc:82
BaseMeshReader::element_data_values_
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
Definition: msh_basereader.hh:247
BaseMeshReader::mesh_factory
static Mesh * mesh_factory(const Input::Record &input_mesh_rec)
Definition: msh_basereader.cc:52
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
sys_profiler.hh
BaseMeshReader::MeshDataHeader::field_name
std::string field_name
Name of field.
Definition: msh_basereader.hh:121
INPUT_CATCH
#define INPUT_CATCH(ExceptionType, AddressEITag, input_accessor)
Definition: accessors.hh:63
CheckResult
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits)
Definition: element_data_cache.hh:36
BaseMeshReader::can_have_components_
bool can_have_components_
Definition: msh_basereader.hh:264
BaseMeshReader::BaseMeshReader
BaseMeshReader(const FilePath &file_name)
Constructor.
Definition: msh_basereader.cc:28
Input::Record::opt_val
bool opt_val(const string &key, Ret &value) const
Definition: accessors_impl.hh:107
BaseMeshReader::get_element_data
ElementDataCache< T >::ComponentDataPtr get_element_data(unsigned int n_entities, unsigned int n_components, bool boundary_domain, unsigned int component_idx)
Definition: msh_basereader.cc:89
mesh.h
std::map
Definition: doxy_dummy_defs.hh:11
msh_pvdreader.hh
Input::Type
Definition: balance.hh:41
Mesh::setup_topology
void setup_topology()
Definition: mesh.cc:399
Mesh
Definition: mesh.h:98
ElementDataCache::ComponentDataPtr
std::shared_ptr< std::vector< T > > ComponentDataPtr
Definition: element_data_cache.hh:52
BaseMeshReader::read_elements
virtual void read_elements(Mesh *mesh)=0
Input::Array
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
WarningOut
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
std
Definition: doxy_dummy_defs.hh:5
Mesh::check_and_finish
void check_and_finish()
Definition: mesh.cc:989
msh_vtkreader.hh
BaseMeshReader::read_raw_mesh
void read_raw_mesh(Mesh *mesh)
Definition: msh_basereader.cc:74
BaseMeshReader::MeshDataHeader::n_entities
unsigned int n_entities
Number of rows.
Definition: msh_basereader.hh:131
BaseMeshReader::data_section_name_
std::string data_section_name_
Store name of field data section specify for type of mesh file.
Definition: msh_basereader.hh:244
BaseMeshReader::MeshDataHeader::time
double time
Time of field data (used only for GMSH reader)
Definition: msh_basereader.hh:125
BaseMeshReader::has_compatible_mesh_
bool has_compatible_mesh_
Definition: msh_basereader.hh:241
START_TIMER
#define START_TIMER(tag)
Starts a timer with specified tag.
Definition: sys_profiler.hh:115
BaseMeshReader::bulk_elements_id_
vector< LongIdx > bulk_elements_id_
Definition: msh_basereader.hh:254