Flow123d  DF_profiler_memory_monitor-83fd579
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 {
83  has_compatible_mesh_ = true;
85 }
86 
87 
88 std::vector<int> const & BaseMeshReader::get_element_ids(bool boundary_domain) {
89  if (boundary_domain) return boundary_elements_id_;
90  else return bulk_elements_id_;
91 }
92 
93 
94 template<typename T>
96  MeshDataHeader header, unsigned int expected_n_entities,
97  unsigned int expected_n_components, unsigned int boundary_begin) {
99  .error("Vector of mapping VTK to GMSH element is not initialized. Did you call check_compatible_mesh?");
100 
101  std::string field_name = header.field_name;
102 
103  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
104  if (it == element_data_values_->end()) {
105  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >();
106  it=element_data_values_->find(field_name);
107  }
108 
109  if ( !it->second->is_actual(header.time, field_name) ) {
110  // check that the header is valid - expected_n_entities
111  if (header.n_entities != expected_n_entities) {
112  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nDifferent number of entities: {}, computation needs {}.\n",
113  tok_.f_name(), data_section_name_, field_name, header.time, header.n_entities, expected_n_entities);
114  }
115  // check that the header is valid, try to correct n_components
116  if (header.n_components != expected_n_components) {
117  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of components: {}, expected: {} .\n",
118  tok_.f_name(), data_section_name_, field_name, header.time, header.n_components, expected_n_components);
119  THROW(ExcWrongComponentsCount() << EI_FieldName(field_name) << EI_Time(header.time) << EI_MeshFile(tok_.f_name()) );
120  }
121 
122  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >(
123  field_name, header.time,
124  expected_n_components*expected_n_entities, boundary_begin);
125  this->read_element_data(*(it->second), header );
126  }
127 
128  ElementDataCache<T> &current_cache = dynamic_cast<ElementDataCache<T> &>(*(it->second));
129  return current_cache.get_data();
130 }
131 
132 
133 
134 // explicit instantiation of template methods
135 #define MESH_READER_GET_ELEMENT_DATA(TYPE) \
136 template typename ElementDataCache<TYPE>::CacheData BaseMeshReader::get_element_data<TYPE>( \
137  MeshDataHeader header, unsigned int n_entities, \
138  unsigned int n_components, unsigned int boundary_begin);
139 
141 MESH_READER_GET_ELEMENT_DATA(unsigned int)
143 
#define ASSERT(expr)
Definition: asserts.hh:351
#define ASSERT_PTR(ptr)
Definition of assert macro checking non-null pointer (PTR) only for debug mode.
Definition: asserts.hh:341
vector< LongIdx > boundary_elements_id_
virtual void read_elements(Mesh *mesh)=0
vector< LongIdx > bulk_elements_id_
Tokenizer tok_
Tokenizer used for reading ASCII file format.
void set_element_ids(const Mesh &mesh)
ElementDataCache< T >::CacheData get_element_data(MeshDataHeader header, unsigned int expected_n_entities, unsigned int expected_n_components, unsigned int boundary_begin)
virtual void read_nodes(Mesh *mesh)=0
BaseMeshReader(const FilePath &file_name)
Constructor.
std::string data_section_name_
Store name of field data section specify for type of mesh file.
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader header)=0
std::vector< int > const & get_element_ids(bool boundary_domain)
static Mesh * mesh_factory(const Input::Record &input_mesh_rec)
void read_raw_mesh(Mesh *mesh)
static std::shared_ptr< BaseMeshReader > reader_factory(const FilePath &file_name)
std::shared_ptr< ElementDataFieldMap > element_data_values_
Cache with last read element data.
std::shared_ptr< std::vector< T > > CacheData
CacheData get_data()
Return underlying vector of element data.
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
string extension() const
Definition: file_path.cc:198
Accessor to input data conforming to declared Array.
Definition: accessors.hh:566
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
bool opt_val(const string &key, Ret &value) const
const Ret val(const string &key) const
Definition: mesh.h:362
void check_and_finish()
Definition: mesh.cc:1038
void elements_id_maps(vector< LongIdx > &bulk_elements_id, vector< LongIdx > &boundary_elements_id) const
Definition: mesh.cc:878
void setup_topology()
Definition: mesh.cc:444
void read_regions_from_input(Input::Array region_list)
Definition: mesh.cc:1028
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
#define INPUT_CATCH(ExceptionType, AddressEITag, input_accessor)
Definition: accessors.hh:63
#define WarningOut()
Macro defining 'warning' record of log.
Definition: logger.hh:278
#define MESH_READER_GET_ELEMENT_DATA(TYPE)
unsigned int n_components
Number of values on one row.
unsigned int n_entities
Number of rows.
double time
Time of field data (used only for GMSH reader)
std::string field_name
Name of field.
#define START_TIMER(tag)
Starts a timer with specified tag.