Flow123d  JS_before_hm-2087-gb06a8f5d8
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 "input/accessors.hh"
25 
26 
27 /***********************************************************************************************
28  * Implementation of ReaderCache
29  */
30 
32  static ReaderCache *instance = new ReaderCache;
33  return instance;
34 }
35 
36 std::shared_ptr<BaseMeshReader> ReaderCache::get_reader(const FilePath &file_path) {
37  return ReaderCache::get_reader_data(file_path)->second.reader_;
38 }
39 
40 std::shared_ptr<Mesh> ReaderCache::get_mesh(const FilePath &file_path) {
41  auto it = ReaderCache::get_reader_data(file_path);
42  // Create and fill mesh if doesn't exist
43  if ( (*it).second.mesh_ == nullptr ) {
44  (*it).second.mesh_ = std::make_shared<Mesh>( Input::Record() );
45  (*it).second.reader_->read_physical_names( (*it).second.mesh_.get() );
46  (*it).second.reader_->read_raw_mesh( (*it).second.mesh_.get() );
47  //(*it).second.reader_->check_compatible_mesh( *((*it).second.mesh_) );
48 
49  }
50  return (*it).second.mesh_;
51 }
52 
53 ReaderCache::ReaderTable::iterator ReaderCache::get_reader_data(const FilePath &file_path) {
54  ReaderTable::iterator it = ReaderCache::instance()->reader_table_.find( string(file_path) );
55  if (it == ReaderCache::instance()->reader_table_.end()) {
56  ReaderData reader_data;
57  if ( file_path.extension() == ".msh" ) {
58  reader_data.reader_ = std::make_shared<GmshMeshReader>(file_path);
59  } else if ( file_path.extension() == ".vtu" ) {
60  reader_data.reader_ = std::make_shared<VtkMeshReader>(file_path);
61  } else if ( file_path.extension() == ".pvd" ) {
62  reader_data.reader_ = std::make_shared<PvdMeshReader>(file_path);
63  } else {
64  THROW(BaseMeshReader::ExcWrongExtension()
65  << BaseMeshReader::EI_FileExtension(file_path.extension()) << BaseMeshReader::EI_MeshFile((string)file_path) );
66  }
67  ReaderCache::instance()->reader_table_.insert( std::pair<string, ReaderData>(string(file_path), reader_data) );
68  it = ReaderCache::instance()->reader_table_.find( string(file_path) );
69  }
70  return it;
71 }
72 
73 void ReaderCache::get_element_ids(const FilePath &file_path, const Mesh &mesh) {
74  auto reader_ptr = ReaderCache::get_reader(file_path);
75  reader_ptr->has_compatible_mesh_ = true;
76  mesh.elements_id_maps(reader_ptr->bulk_elements_id_, reader_ptr->boundary_elements_id_);
77 }
78 
79 std::shared_ptr<EquivalentMeshMap> ReaderCache::get_target_mesh_element_map(const FilePath &file_path,
80  Mesh *computational_mesh) {
81  auto it = ReaderCache::get_reader_data(file_path);
82  ASSERT( (*it).second.mesh_ != nullptr ).error("Mesh is not created. Did you call 'ReaderCache::get_mesh(file_path)'?\n");
83  if ( (*it).second.target_mesh_element_map_ == nullptr ) {
84  (*it).second.target_mesh_element_map_ = computational_mesh->check_compatible_mesh( *((*it).second.mesh_.get()) );
85  }
86  return (*it).second.target_mesh_element_map_;
87 }
msh_basereader.hh
ReaderCache::instance
static ReaderCache * instance()
Returns singleton instance.
Definition: reader_cache.cc:31
ASSERT
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library.
Definition: asserts.hh:347
Mesh::check_compatible_mesh
virtual std::shared_ptr< EquivalentMeshMap > check_compatible_mesh(Mesh &input_mesh)
Definition: mesh.cc:913
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:73
ReaderCache::ReaderCache
ReaderCache()
Constructor.
Definition: reader_cache.hh:79
FilePath
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
THROW
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
reader_cache.hh
msh_gmshreader.h
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:53
accessors.hh
Mesh::elements_id_maps
void elements_id_maps(vector< LongIdx > &bulk_elements_id, vector< LongIdx > &boundary_elements_id) const
Definition: mesh.cc:878
ReaderCache::get_target_mesh_element_map
static std::shared_ptr< EquivalentMeshMap > get_target_mesh_element_map(const FilePath &file_path, Mesh *computational_mesh)
Definition: reader_cache.cc:79
mesh.h
msh_pvdreader.hh
Input::Type
Definition: balance.hh:41
Mesh
Definition: mesh.h:355
msh_vtkreader.hh
ReaderCache::get_mesh
static std::shared_ptr< Mesh > get_mesh(const FilePath &file_path)
Definition: reader_cache.cc:40
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:36
ReaderCache::reader_table_
ReaderTable reader_table_
Table of readers.
Definition: reader_cache.hh:85
ReaderCache::ReaderData::reader_
std::shared_ptr< BaseMeshReader > reader_
Definition: reader_cache.hh:41
ReaderCache
Definition: reader_cache.hh:37