Flow123d  master-3768d5dec
reader_cache.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 reader_cache.cc
15  * @brief
16  */
17 
18 #include "io/reader_cache.hh"
19 #include "io/msh_basereader.hh"
20 #include "io/msh_gmshreader.h"
21 #include "io/msh_vtkreader.hh"
22 #include "io/msh_pvdreader.hh"
23 #include "mesh/mesh.h"
24 #include "mesh/bc_mesh.hh"
25 #include "input/accessors.hh"
26 
27 
28 /***********************************************************************************************
29  * Implementation of ReaderCache
30  */
31 
33  static ReaderCache *instance = new ReaderCache;
34  return instance;
35 }
36 
37 std::shared_ptr<BaseMeshReader> ReaderCache::get_reader(const FilePath &file_path) {
38  return ReaderCache::get_reader_data(file_path)->second.reader_;
39 }
40 
41 std::shared_ptr<Mesh> ReaderCache::get_mesh(const FilePath &file_path) {
42  auto it = ReaderCache::get_reader_data(file_path);
43  // Create and fill mesh if doesn't exist
44  if ( (*it).second.mesh_ == nullptr ) {
45  (*it).second.mesh_ = std::make_shared<Mesh>( Input::Record() );
46  (*it).second.reader_->read_physical_names( (*it).second.mesh_.get() );
47  (*it).second.reader_->read_raw_mesh( (*it).second.mesh_.get() );
48  //(*it).second.reader_->check_compatible_mesh( *((*it).second.mesh_) );
49 
50  }
51  return (*it).second.mesh_;
52 }
53 
54 ReaderCache::ReaderTable::iterator ReaderCache::get_reader_data(const FilePath &file_path) {
55  ReaderTable::iterator it = ReaderCache::instance()->reader_table_.find( string(file_path) );
56  if (it == ReaderCache::instance()->reader_table_.end()) {
57  ReaderData reader_data;
58  if ( file_path.extension() == ".msh" ) {
59  reader_data.reader_ = std::make_shared<GmshMeshReader>(file_path);
60  } else if ( file_path.extension() == ".vtu" ) {
61  reader_data.reader_ = std::make_shared<VtkMeshReader>(file_path);
62  } else if ( file_path.extension() == ".pvd" ) {
63  reader_data.reader_ = std::make_shared<PvdMeshReader>(file_path);
64  } else {
65  THROW(BaseMeshReader::ExcWrongExtension()
66  << BaseMeshReader::EI_FileExtension(file_path.extension()) << BaseMeshReader::EI_MeshFile((string)file_path) );
67  }
68  ReaderCache::instance()->reader_table_.insert( std::pair<string, ReaderData>(string(file_path), reader_data) );
69  it = ReaderCache::instance()->reader_table_.find( string(file_path) );
70  }
71  return it;
72 }
73 
74 void ReaderCache::get_element_ids(const FilePath &file_path, const Mesh &mesh) {
75  auto reader_ptr = ReaderCache::get_reader(file_path);
76  reader_ptr->set_element_ids(mesh);
77 }
78 
79 
81 {
82 
83  // assume both bulk_elements_id and boundary_elements_id are sorted
84  uint i = 0;
85  for(int id : ids) {
86  if ((uint)id == Mesh::undef_idx) return; // Boundary IDs may be undef.
87  map[mesh->elem_index(id)] = i;
88  i++;
89  }
90  ASSERT(i == map.size());
91 }
92 
93 
94 std::shared_ptr<EquivalentMeshMap> ReaderCache::identic_mesh_map(const FilePath &file_path,
95  Mesh *computational_mesh) {
96  //ASSERT_PERMANENT(false).error("Not implemented yet." );
97  auto it = ReaderCache::get_reader_data(file_path);
98  auto &reader_data = (*it).second;
99  if ( reader_data.target_mesh_element_map_ == nullptr ) {
100  // Create map for the identic mesh taking the computational mesh permutation into account.
101  // Assume that element IDs in the source and computational mesh match.
102  // map index of the sorted IDs to the index of the element in permuted computational mesh.
103  // make for both bulk and boundary mesh.
104  reader_data.reader_->has_compatible_mesh_ = true;
105  reader_data.reader_->set_element_ids(*computational_mesh);
106 
107  std::shared_ptr<EquivalentMeshMap> map_ptr =
108  std::make_shared<EquivalentMeshMap>(computational_mesh->n_elements(),
109  computational_mesh->bc_mesh()->n_elements(), (LongIdx)undef_idx);
110  _set_identic_mesh_map(map_ptr->bulk, reader_data.reader_->get_element_ids(false), computational_mesh);
111  _set_identic_mesh_map(map_ptr->boundary, reader_data.reader_->get_element_ids(true), computational_mesh->bc_mesh());
112 
113  reader_data.target_mesh_element_map_ = map_ptr;
114  }
115  return reader_data.target_mesh_element_map_;
116 
117 }
118 
119 std::shared_ptr<EquivalentMeshMap> ReaderCache::eqivalent_mesh_map(const FilePath &file_path,
120  Mesh *computational_mesh) {
121  auto it = ReaderCache::get_reader_data(file_path);
122  auto source_mesh = ReaderCache::get_mesh(file_path);
123  auto &reader_data = (*it).second;
124  //ASSERT_PTR( reader_data.mesh_ ).error("Mesh is not created. Did you call 'ReaderCache::get_mesh(file_path)'?\n");
125  if ( reader_data.target_mesh_element_map_ == nullptr ) {
126  reader_data.reader_->set_element_ids(*source_mesh);
127  reader_data.target_mesh_element_map_ = computational_mesh->check_compatible_mesh( *((*it).second.mesh_.get()) );
128  }
129  return reader_data.target_mesh_element_map_;
130 }
131 
ReaderCache::identic_mesh_map
static std::shared_ptr< EquivalentMeshMap > identic_mesh_map(const FilePath &file_path, Mesh *computational_mesh)
Definition: reader_cache.cc:94
ReaderCache::eqivalent_mesh_map
static std::shared_ptr< EquivalentMeshMap > eqivalent_mesh_map(const FilePath &file_path, Mesh *computational_mesh)
Definition: reader_cache.cc:119
msh_basereader.hh
ReaderCache::instance
static ReaderCache * instance()
Returns singleton instance.
Definition: reader_cache.cc:32
ASSERT
#define ASSERT(expr)
Definition: asserts.hh:351
Mesh::check_compatible_mesh
std::shared_ptr< EquivalentMeshMap > check_compatible_mesh(Mesh &input_mesh) override
Definition: mesh.cc:919
bc_mesh.hh
FilePath::extension
string extension() const
Definition: file_path.cc:198
ReaderCache::get_element_ids
static void get_element_ids(const FilePath &file_path, const Mesh &mesh)
Definition: reader_cache.cc:74
ReaderCache::ReaderCache
ReaderCache()
Constructor.
Definition: reader_cache.hh:82
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
MeshBase::undef_idx
static const unsigned int undef_idx
Definition: mesh.h:105
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
std::vector< int >
reader_cache.hh
uint
unsigned int uint
Definition: mh_dofhandler.hh:101
msh_gmshreader.h
Mesh::bc_mesh
BCMesh * bc_mesh() const override
Implement MeshBase::bc_mesh(), getter of boundary mesh.
Definition: mesh.h:566
Input::Record
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
ReaderCache::get_reader_data
static ReaderTable::iterator get_reader_data(const FilePath &file_path)
Returns instance of given FilePath. If reader doesn't exist, creates new ReaderData object.
Definition: reader_cache.cc:54
undef_idx
const unsigned int undef_idx
Definition: index_types.hh:32
accessors.hh
mesh.h
std::map
Definition: doxy_dummy_defs.hh:11
msh_pvdreader.hh
Input::Type
Definition: balance.hh:41
LongIdx
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
Mesh
Definition: mesh.h:361
MeshBase
Base class for Mesh and BCMesh.
Definition: mesh.h:96
msh_vtkreader.hh
ReaderCache::get_mesh
static std::shared_ptr< Mesh > get_mesh(const FilePath &file_path)
Definition: reader_cache.cc:41
ReaderCache::ReaderData
Definition: reader_cache.hh:39
ReaderCache::get_reader
static std::shared_ptr< BaseMeshReader > get_reader(const FilePath &file_path)
Definition: reader_cache.cc:37
MeshBase::n_elements
unsigned int n_elements() const
Definition: mesh.h:111
ReaderCache::reader_table_
ReaderTable reader_table_
Table of readers.
Definition: reader_cache.hh:88
_set_identic_mesh_map
void _set_identic_mesh_map(std::vector< int > &map, std::vector< int > ids, const MeshBase *mesh)
Definition: reader_cache.cc:80
MeshBase::elem_index
int elem_index(int elem_id) const
For element of given elem_id returns index in element_vec_ or (-1) if element doesn't exist.
Definition: mesh.h:222
ReaderCache::ReaderData::reader_
std::shared_ptr< BaseMeshReader > reader_
Definition: reader_cache.hh:41
ReaderCache
Definition: reader_cache.hh:37