Flow123d  release_2.2.0-914-gf1a3a4f
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 "system/sys_profiler.hh"
25 
26 
28 : element_data_values_(std::make_shared<ElementDataFieldMap>()),
29  tok_(file_name)
30 {}
31 
32 BaseMeshReader::BaseMeshReader(const FilePath &file_name, std::shared_ptr<ElementDataFieldMap> element_data_values)
33 : element_data_values_(element_data_values),
34  tok_(file_name)
35 {}
36 
37 std::shared_ptr< BaseMeshReader > BaseMeshReader::reader_factory(const FilePath &file_name) {
38  std::shared_ptr<BaseMeshReader> reader_ptr;
39  if ( file_name.extension() == ".msh" ) {
40  reader_ptr = std::make_shared<GmshMeshReader>(file_name);
41  } else if ( file_name.extension() == ".vtu" ) {
42  reader_ptr = std::make_shared<VtkMeshReader>(file_name);
43  } else if ( file_name.extension() == ".pvd" ) {
44  reader_ptr = std::make_shared<PvdMeshReader>(file_name);
45  } else {
46  THROW(ExcWrongExtension() << EI_FileExtension(file_name.extension()) << EI_MeshFile((string)file_name) );
47  }
48  return reader_ptr;
49 }
50 
52  START_TIMER("BaseMeshReader - mesh factory");
53 
54  Input::Array region_list;
55  Mesh * mesh = new Mesh( input_mesh_rec );
56 
57  try {
58  std::shared_ptr< BaseMeshReader > reader = BaseMeshReader::reader_factory(input_mesh_rec.val<FilePath>("mesh_file"));
59  reader->read_physical_names(mesh);
60  if (input_mesh_rec.opt_val("regions", region_list)) {
61  mesh->read_regions_from_input(region_list);
62  }
63  reader->read_raw_mesh(mesh);
64  } INPUT_CATCH(FilePath::ExcFileOpen, FilePath::EI_Address_String, input_mesh_rec)
65  mesh->setup_topology();
66  mesh->check_and_finish();
67  return mesh;
68 
69 }
70 
72  ASSERT(mesh).error("Argument mesh is NULL.\n");
73  tok_.set_position( Tokenizer::Position() );
74  read_nodes(mesh);
75  read_elements(mesh);
76 }
77 
78 
80  if (boundary_domain) return boundary_elements_id_;
81  else return bulk_elements_id_;
82 }
83 
84 
85 template<typename T>
86 typename ElementDataCache<T>::ComponentDataPtr BaseMeshReader::get_element_data( unsigned int n_entities, unsigned int n_components,
87  bool boundary_domain, unsigned int component_idx) {
89  .error("Vector of mapping VTK to GMSH element is not initialized. Did you call check_compatible_mesh?");
90  ASSERT(actual_header_.field_name != "").error("Unset MeshDataHeader. Did you call find_header?\n");
91 
92  std::string field_name = actual_header_.field_name;
93 
94  ElementDataFieldMap::iterator it=element_data_values_->find(field_name);
95  if (it == element_data_values_->end()) {
96  (*element_data_values_)[field_name] = std::make_shared< ElementDataCache<T> >();
97  it=element_data_values_->find(field_name);
98  }
99 
100  if ( !it->second->is_actual(actual_header_.time, field_name) ) {
101  unsigned int size_of_cache; // count of vectors stored in cache
102 
103  // check that the header is valid, try to correct
104  if (actual_header_.n_entities != n_entities) {
105  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of entities: {}, using {} instead.\n",
106  tok_.f_name(), data_section_name_, field_name, actual_header_.time, actual_header_.n_entities, n_entities);
107  // actual_header.n_entities=n_entities;
108  }
109 
110  if (n_components == 1) {
111  // read for MultiField to 'n_comp' vectors
112  // or for Field if ElementData contains only one value
113  size_of_cache = actual_header_.n_components;
114  }
115  else {
116  // read for Field if more values is stored to one vector
117  size_of_cache = 1;
118  if (actual_header_.n_components != n_components) {
119  WarningOut().fmt("In file '{}', '{}' section for field '{}', time: {}.\nWrong number of components: {}, using {} instead.\n",
120  tok_.f_name(), data_section_name_, field_name, actual_header_.time, actual_header_.n_components, n_components);
121  actual_header_.n_components=n_components;
122  }
123  }
124 
125  (*element_data_values_)[field_name]
126  = std::make_shared< ElementDataCache<T> >(field_name, actual_header_.time, size_of_cache, n_components*n_entities);
127  this->read_element_data(*(it->second), actual_header_, n_components, boundary_domain );
128  }
129 
131 
132  if (component_idx == std::numeric_limits<unsigned int>::max()) component_idx = 0;
133  ElementDataCache<T> &current_cache = dynamic_cast<ElementDataCache<T> &>(*(it->second));
134  return current_cache.get_component_data(component_idx);
135 }
136 
137 
138 // explicit instantiation of template methods
139 #define MESH_READER_GET_ELEMENT_DATA(TYPE) \
140 template typename ElementDataCache<TYPE>::ComponentDataPtr BaseMeshReader::get_element_data<TYPE>(unsigned int n_entities, \
141  unsigned int n_components, bool boundary_domain, unsigned int component_idx);
142 
144 MESH_READER_GET_ELEMENT_DATA(unsigned int);
146 
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:716
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:99
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)
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:706
static Mesh * mesh_factory(const Input::Record &input_mesh_rec)
void setup_topology()
Definition: mesh.cc:254
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.
vector< IdxInt > boundary_elements_id_
virtual void read_element_data(ElementDataCacheBase &data_cache, MeshDataHeader actual_header, unsigned int n_components, bool boundary_domain)=0
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)
vector< IdxInt > bulk_elements_id_
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)