Flow123d  JS_before_hm-2210-gb57a0a528
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 
81 
83  if (boundary_domain) return boundary_elements_id_;
84  else return bulk_elements_id_;
85 }
86 
87 
88 template<typename T>
90  MeshDataHeader header, unsigned int expected_n_entities,
91  unsigned int expected_n_components, bool boundary_domain) {
93  .error("Vector of mapping VTK to GMSH element is not initialized. Did you call check_compatible_mesh?");
94 
95  std::string field_name = 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(header.time, field_name) ) {
104  // check that the header is valid - expected_n_entities
105  if (header.n_entities != expected_n_entities) {
106  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nDifferent number of entities: {}, computation needs {}.\n",
107  tok_.f_name(), data_section_name_, field_name, header.time, header.n_entities, expected_n_entities);
108  }
109  // check that the header is valid, try to correct n_components
110  if (header.n_components != expected_n_components) {
111  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of components: {}, expected: {} .\n",
112  tok_.f_name(), data_section_name_, field_name, header.time, header.n_components, expected_n_components);
113  THROW(ExcWrongComponentsCount() << EI_FieldName(field_name) << EI_Time(header.time) << EI_MeshFile(tok_.f_name()) );
114  }
115 
116  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >(
117  field_name, header.time,
118  expected_n_components*expected_n_entities);
119  this->read_element_data(*(it->second), header, boundary_domain );
120  }
121 
122  ElementDataCache<T> &current_cache = dynamic_cast<ElementDataCache<T> &>(*(it->second));
123  return current_cache.get_data();
124 }
125 
126 CheckResult BaseMeshReader::scale_and_check_limits(string field_name, double coef, double default_val, double lower_bound,
127  double upper_bound) {
128  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
129  ASSERT(it != element_data_values_->end())(field_name);
130 
131  std::shared_ptr< ElementDataCache<double> > current_cache = dynamic_pointer_cast<ElementDataCache<double> >(it->second);
132  ASSERT(current_cache)(field_name).error("scale_and_check_limits can be call only for scalable fields!\n");
133 
134  CheckResult check_val = current_cache->check_values(default_val, lower_bound, upper_bound);
135  current_cache->scale_data(coef);
136  return check_val;
137 }
138 
139 
140 
141 // explicit instantiation of template methods
142 #define MESH_READER_GET_ELEMENT_DATA(TYPE) \
143 template typename ElementDataCache<TYPE>::CacheData BaseMeshReader::get_element_data<TYPE>( \
144  MeshDataHeader header, unsigned int n_entities, \
145  unsigned int n_components, bool boundary_domain);
146 
148 MESH_READER_GET_ELEMENT_DATA(unsigned int)
150 
BaseMeshReader::tok_
Tokenizer tok_
Tokenizer used for reading ASCII file format.
Definition: msh_basereader.hh:255
MESH_READER_GET_ELEMENT_DATA
#define MESH_READER_GET_ELEMENT_DATA(TYPE)
Definition: msh_basereader.cc:142
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:126
BaseMeshReader::boundary_elements_id_
vector< LongIdx > boundary_elements_id_
Definition: msh_basereader.hh:259
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
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:1022
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:252
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
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::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:359
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:1032
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:89
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:249
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:246
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:259