Flow123d  JS_before_hm-1001-gfa0c761
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<std::vector<LongIdx>> ReaderCache::get_target_mesh_element_map(const FilePath &file_path, Mesh *computational_mesh) {
80  auto it = ReaderCache::get_reader_data(file_path);
81  ASSERT( (*it).second.mesh_ != nullptr ).error("Mesh is not created. Did you call 'ReaderCache::get_mesh(file_path)'?\n");
82  if ( (*it).second.target_mesh_element_map_ == nullptr ) {
83  (*it).second.target_mesh_element_map_ = computational_mesh->check_compatible_mesh( *((*it).second.mesh_.get()) );
84  }
85  return (*it).second.target_mesh_element_map_;
86 }
static void get_element_ids(const FilePath &file_path, const Mesh &mesh)
Definition: reader_cache.cc:73
virtual std::shared_ptr< std::vector< LongIdx > > check_compatible_mesh(Mesh &input_mesh)
Definition: mesh.cc:892
Definition: mesh.h:78
ReaderCache()
Constructor.
Definition: reader_cache.hh:77
static ReaderCache * instance()
Returns singleton instance.
Definition: reader_cache.cc:31
#define ASSERT(expr)
Allow use shorter versions of macro names if these names is not used with external library...
Definition: asserts.hh:347
Accessor to the data with type Type::Record.
Definition: accessors.hh:291
std::shared_ptr< BaseMeshReader > reader_
Definition: reader_cache.hh:40
static ReaderTable::iterator get_reader_data(const FilePath &file_path)
Returns instance of given FilePath. If reader doesn&#39;t exist, creates new ReaderData object...
Definition: reader_cache.cc:53
string extension() const
Definition: file_path.cc:198
void elements_id_maps(vector< LongIdx > &bulk_elements_id, vector< LongIdx > &boundary_elements_id) const
Definition: mesh.cc:851
Dedicated class for storing path to input and output files.
Definition: file_path.hh:54
ReaderTable reader_table_
Table of readers.
Definition: reader_cache.hh:83
static std::shared_ptr< Mesh > get_mesh(const FilePath &file_path)
Definition: reader_cache.cc:40
static std::shared_ptr< BaseMeshReader > get_reader(const FilePath &file_path)
Definition: reader_cache.cc:36
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
static std::shared_ptr< std::vector< LongIdx > > get_target_mesh_element_map(const FilePath &file_path, Mesh *computational_mesh)
Definition: reader_cache.cc:79