Flow123d  DF_patch_fe_data_tables-3227f73
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 
105  reader_data.reader_->set_element_ids(*computational_mesh);
106  std::shared_ptr<EquivalentMeshMap> map_ptr =
107  std::make_shared<EquivalentMeshMap>(computational_mesh->n_elements(),
108  computational_mesh->bc_mesh()->n_elements(), (LongIdx)undef_idx);
109  _set_identic_mesh_map(map_ptr->bulk, reader_data.reader_->get_element_ids(false), computational_mesh);
110  _set_identic_mesh_map(map_ptr->boundary, reader_data.reader_->get_element_ids(true), computational_mesh->bc_mesh());
111 
112  reader_data.target_mesh_element_map_ = map_ptr;
113  }
114  return reader_data.target_mesh_element_map_;
115 
116 }
117 
118 std::shared_ptr<EquivalentMeshMap> ReaderCache::eqivalent_mesh_map(const FilePath &file_path,
119  Mesh *computational_mesh) {
120  auto it = ReaderCache::get_reader_data(file_path);
121  auto source_mesh = ReaderCache::get_mesh(file_path);
122  auto &reader_data = (*it).second;
123  //ASSERT_PTR( reader_data.mesh_ ).error("Mesh is not created. Did you call 'ReaderCache::get_mesh(file_path)'?\n");
124  if ( reader_data.target_mesh_element_map_ == nullptr ) {
125  reader_data.reader_->set_element_ids(*source_mesh);
126  reader_data.target_mesh_element_map_ = computational_mesh->check_compatible_mesh( *((*it).second.mesh_.get()) );
127  }
128  return reader_data.target_mesh_element_map_;
129 }
130 
#define ASSERT(expr)
Definition: asserts.hh:351
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 the data with type Type::Record.
Definition: accessors.hh:291
Base class for Mesh and BCMesh.
Definition: mesh.h:96
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
unsigned int n_elements() const
Definition: mesh.h:111
static const unsigned int undef_idx
Definition: mesh.h:105
Definition: mesh.h:362
BCMesh * bc_mesh() const override
Implement MeshBase::bc_mesh(), getter of boundary mesh.
Definition: mesh.h:567
std::shared_ptr< EquivalentMeshMap > check_compatible_mesh(Mesh &input_mesh) override
Definition: mesh.cc:919
static std::shared_ptr< EquivalentMeshMap > identic_mesh_map(const FilePath &file_path, Mesh *computational_mesh)
Definition: reader_cache.cc:94
ReaderCache()
Constructor.
Definition: reader_cache.hh:82
ReaderTable reader_table_
Table of readers.
Definition: reader_cache.hh:88
static std::shared_ptr< EquivalentMeshMap > eqivalent_mesh_map(const FilePath &file_path, Mesh *computational_mesh)
static void get_element_ids(const FilePath &file_path, const Mesh &mesh)
Definition: reader_cache.cc:74
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
static ReaderCache * instance()
Returns singleton instance.
Definition: reader_cache.cc:32
static std::shared_ptr< Mesh > get_mesh(const FilePath &file_path)
Definition: reader_cache.cc:41
static std::shared_ptr< BaseMeshReader > get_reader(const FilePath &file_path)
Definition: reader_cache.cc:37
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
const unsigned int undef_idx
Definition: index_types.hh:32
int LongIdx
Define type that represents indices of large arrays (elements, nodes, dofs etc.)
Definition: index_types.hh:24
unsigned int uint
void _set_identic_mesh_map(std::vector< int > &map, std::vector< int > ids, const MeshBase *mesh)
Definition: reader_cache.cc:80
std::shared_ptr< BaseMeshReader > reader_
Definition: reader_cache.hh:41