Flow123d  3.9.0-44fe12069
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_PTR(mesh).error("Argument mesh is NULL.\n");
76  tok_.set_position( Tokenizer::Position() );
77  read_nodes(mesh);
78  read_elements(mesh);
79 }
80 
82 {
84 }
85 
86 
87 std::vector<int> const & BaseMeshReader::get_element_ids(bool boundary_domain) {
88  if (boundary_domain) return boundary_elements_id_;
89  else return bulk_elements_id_;
90 }
91 
92 
93 template<typename T>
95  MeshDataHeader header, unsigned int expected_n_entities,
96  unsigned int expected_n_components, bool boundary_domain) {
98  .error("Vector of mapping VTK to GMSH element is not initialized. Did you call check_compatible_mesh?");
99 
100  std::string field_name = header.field_name;
101 
102  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
103  if (it == element_data_values_->end()) {
104  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >();
105  it=element_data_values_->find(field_name);
106  }
107 
108  if ( !it->second->is_actual(header.time, field_name) ) {
109  // check that the header is valid - expected_n_entities
110  if (header.n_entities != expected_n_entities) {
111  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nDifferent number of entities: {}, computation needs {}.\n",
112  tok_.f_name(), data_section_name_, field_name, header.time, header.n_entities, expected_n_entities);
113  }
114  // check that the header is valid, try to correct n_components
115  if (header.n_components != expected_n_components) {
116  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of components: {}, expected: {} .\n",
117  tok_.f_name(), data_section_name_, field_name, header.time, header.n_components, expected_n_components);
118  THROW(ExcWrongComponentsCount() << EI_FieldName(field_name) << EI_Time(header.time) << EI_MeshFile(tok_.f_name()) );
119  }
120 
121  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >(
122  field_name, header.time,
123  expected_n_components*expected_n_entities);
124  this->read_element_data(*(it->second), header, boundary_domain );
125  }
126 
127  ElementDataCache<T> &current_cache = dynamic_cast<ElementDataCache<T> &>(*(it->second));
128  return current_cache.get_data();
129 }
130 
131 CheckResult BaseMeshReader::scale_and_check_limits(string field_name, double coef, double default_val, double lower_bound,
132  double upper_bound) {
133  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
134  ASSERT(it != element_data_values_->end())(field_name);
135 
136  std::shared_ptr< ElementDataCache<double> > current_cache = dynamic_pointer_cast<ElementDataCache<double> >(it->second);
137  ASSERT(current_cache)(field_name).error("scale_and_check_limits can be call only for scalable fields!\n");
138 
139  CheckResult check_val = current_cache->check_values(default_val, lower_bound, upper_bound);
140  current_cache->scale_data(coef);
141  return check_val;
142 }
143 
144 
145 
146 // explicit instantiation of template methods
147 #define MESH_READER_GET_ELEMENT_DATA(TYPE) \
148 template typename ElementDataCache<TYPE>::CacheData BaseMeshReader::get_element_data<TYPE>( \
149  MeshDataHeader header, unsigned int n_entities, \
150  unsigned int n_components, bool boundary_domain);
151 
153 MESH_READER_GET_ELEMENT_DATA(unsigned int)
155 
BaseMeshReader::tok_
Tokenizer tok_
Tokenizer used for reading ASCII file format.
Definition: msh_basereader.hh:264
MESH_READER_GET_ELEMENT_DATA
#define MESH_READER_GET_ELEMENT_DATA(TYPE)
Definition: msh_basereader.cc:147
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:131
BaseMeshReader::boundary_elements_id_
vector< LongIdx > boundary_elements_id_
Definition: msh_basereader.hh:269
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
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:350
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
BaseMeshReader::set_element_ids
void set_element_ids(const Mesh &mesh)
Definition: msh_basereader.cc:81
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:133
Mesh::read_regions_from_input
void read_regions_from_input(Input::Array region_list)
Definition: mesh.cc:1028
msh_gmshreader.h
BaseMeshReader::element_data_values_
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
Definition: msh_basereader.hh:261
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:125
ElementDataCache::get_data
CacheData get_data()
Return underlying vector of element data.
Definition: element_data_cache.cc:71
Mesh::elements_id_maps
void elements_id_maps(vector< LongIdx > &bulk_elements_id, vector< LongIdx > &boundary_elements_id) const
Definition: mesh.cc:878
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::get_element_ids
const std::vector< int > & get_element_ids(bool boundary_domain)
Definition: msh_basereader.cc:87
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
mesh.h
std::map
Definition: doxy_dummy_defs.hh:11
msh_pvdreader.hh
Input::Type
Definition: balance.hh:41
BaseMeshReader::MeshDataHeader
Definition: msh_basereader.hh:104
Mesh::setup_topology
void setup_topology()
Definition: mesh.cc:444
Mesh
Definition: mesh.h:361
BaseMeshReader::read_elements
virtual void read_elements(Mesh *mesh)=0
Input::Array
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
ElementDataCache::CacheData
std::shared_ptr< std::vector< T > > CacheData
Definition: element_data_cache.hh:52
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:1038
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:135
BaseMeshReader::get_element_data
ElementDataCache< T >::CacheData get_element_data(MeshDataHeader header, unsigned int expected_n_entities, unsigned int expected_n_components, bool boundary_domain)
Definition: msh_basereader.cc:94
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:258
BaseMeshReader::MeshDataHeader::time
double time
Time of field data (used only for GMSH reader)
Definition: msh_basereader.hh:129
ASSERT_PTR
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:340
BaseMeshReader::read_element_data
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader header, bool boundary_domain)=0
BaseMeshReader::has_compatible_mesh_
bool has_compatible_mesh_
Definition: msh_basereader.hh:255
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:269