Flow123d  release_3.0.0-962-g8853c67
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  std::shared_ptr< BaseMeshReader > reader = BaseMeshReader::reader_factory(input_mesh_rec.val<FilePath>("mesh_file"));
60  reader->read_physical_names(mesh);
61  if (input_mesh_rec.opt_val("regions", region_list)) {
62  mesh->read_regions_from_input(region_list);
63  }
64  reader->read_raw_mesh(mesh);
65  } INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, input_mesh_rec)
66  mesh->setup_topology();
67  mesh->check_and_finish();
68  return mesh;
69 
70 }
71 
73  ASSERT(mesh).error("Argument mesh is NULL.\n");
74  tok_.set_position( Tokenizer::Position() );
75  read_nodes(mesh);
76  read_elements(mesh);
77 }
78 
79 
81  if (boundary_domain) return boundary_elements_id_;
82  else return bulk_elements_id_;
83 }
84 
85 
86 template<typename T>
87 typename ElementDataCache<T>::ComponentDataPtr BaseMeshReader::get_element_data( unsigned int n_entities, unsigned int n_components,
88  bool boundary_domain, unsigned int component_idx) {
90  .error("Vector of mapping VTK to GMSH element is not initialized. Did you call check_compatible_mesh?");
91  ASSERT(actual_header_.field_name != "").error("Unset MeshDataHeader. Did you call find_header?\n");
92 
93  std::string field_name = actual_header_.field_name;
94 
95  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
96  if (it == element_data_values_->end()) {
97  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >();
98  it=element_data_values_->find(field_name);
99  }
100 
101  if ( !it->second->is_actual(actual_header_.time, field_name) ) {
102  unsigned int size_of_cache; // count of vectors stored in cache
103 
104  // check that the header is valid, try to correct
105  if (actual_header_.n_entities != n_entities) {
106  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of entities: {}, using {} instead.\n",
107  tok_.f_name(), data_section_name_, field_name, actual_header_.time, actual_header_.n_entities, n_entities);
108  // actual_header.n_entities=n_entities;
109  }
110 
111  if (n_components == 1) {
112  // read for MultiField to 'n_comp' vectors
113  // or for Field if ElementData contains only one value
114  size_of_cache = actual_header_.n_components;
115  }
116  else {
117  // read for Field if more values is stored to one vector
118  size_of_cache = 1;
119  if (actual_header_.n_components != n_components) {
120  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of components: {}, using {} instead.\n",
121  tok_.f_name(), data_section_name_, field_name, actual_header_.time, actual_header_.n_components, n_components);
122  actual_header_.n_components=n_components;
123  }
124  }
125 
126  (*element_data_values_)[field_name]
127  = std::make_shared< ElementDataCache<T> >(field_name, actual_header_.time, size_of_cache, n_components*n_entities);
128  this->read_element_data(*(it->second), actual_header_, n_components, boundary_domain );
129  }
130 
132 
133  if (component_idx == std::numeric_limits<unsigned int>::max()) component_idx = 0;
134  ElementDataCache<T> &current_cache = dynamic_cast<ElementDataCache<T> &>(*(it->second));
135  return current_cache.get_component_data(component_idx);
136 }
137 
138 CheckResult BaseMeshReader::scale_and_check_limits(string field_name, double coef, double default_val, double lower_bound,
139  double upper_bound) {
140  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
141  ASSERT(it != element_data_values_->end())(field_name);
142 
143  std::shared_ptr< ElementDataCache<double> > current_cache = dynamic_pointer_cast<ElementDataCache<double> >(it->second);
144  ASSERT(current_cache)(field_name).error("scale_and_check_limits can be call only for scalable fields!\n");
145 
146  CheckResult check_val = current_cache->check_values(default_val, lower_bound, upper_bound);
147  current_cache->scale_data(coef);
148  return check_val;
149 }
150 
151 
152 
153 // explicit instantiation of template methods
154 #define MESH_READER_GET_ELEMENT_DATA(TYPE) \
155 template typename ElementDataCache<TYPE>::ComponentDataPtr BaseMeshReader::get_element_data<TYPE>(unsigned int n_entities, \
156  unsigned int n_components, bool boundary_domain, unsigned int component_idx);
157 
159 MESH_READER_GET_ELEMENT_DATA(unsigned int);
161 
std::shared_ptr< std::vector< T > > ComponentDataPtr
Accessor to input data conforming to declared Array.
Definition: accessors.hh:567
void check_and_finish()
Definition: mesh.cc:901
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
BaseMeshReader(const FilePath &file_name)
Constructor.
#define INPUT_CATCH(ExceptionType, AddressEITag, input_accessor)
Definition: accessors.hh:64
Definition: mesh.h:76
std::string data_section_name_
Store name of field data section specify for type of mesh file.
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:346
#define MESH_READER_GET_ELEMENT_DATA(TYPE)
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())
unsigned int n_entities
Number of rows.
bool opt_val(const string &key, Ret &value) const
virtual void read_nodes(Mesh *mesh)=0
void read_regions_from_input(Input::Array region_list)
Definition: mesh.cc:891
static Mesh * mesh_factory(const Input::Record &input_mesh_rec)
void setup_topology()
Definition: mesh.cc:290
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
const Ret val(const string &key) const
void read_raw_mesh(Mesh *mesh)
virtual void read_elements(Mesh *mesh)=0
#define START_TIMER(tag)
Starts a timer with specified tag.
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain)=0
CheckResult
Return type of method that checked data stored in ElementDataCache (NaN values, limits) ...
string extension() const
Definition: file_path.cc:198
void reset()
Set field_name value to empty string, that signs invalid header (using after reading data) ...
std::string field_name
Name of field.
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
double time
Time of field data (used only for GMSH reader)
unsigned int n_components
Number of values on one row.
ComponentDataPtr get_component_data(unsigned int component_idx)
Return vector of element data for get component.
MeshDataHeader actual_header_
Header of actual loaded data.
#define WarningOut()
Macro defining &#39;warning&#39; record of log.
Definition: logger.hh:246
ElementDataCache< T >::ComponentDataPtr get_element_data(unsigned int n_entities, unsigned int n_components, bool boundary_domain, unsigned int component_idx)
static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name)
Tokenizer tok_
Tokenizer used for reading ASCII file format.
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
std::vector< int > const & get_element_vector(bool boundary_domain)
vector< LongIdx > boundary_elements_id_
vector< LongIdx > bulk_elements_id_