Flow123d  release_3.0.0-1182-ga9abfa7
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 bool ReaderCache::check_compatible_mesh(const FilePath &file_path, Mesh &mesh) {
74  auto mesh_ptr = ReaderCache::get_mesh(file_path);
75  auto reader_ptr = ReaderCache::get_reader(file_path);
76  reader_ptr->has_compatible_mesh_ = true;
77  return mesh_ptr->check_compatible_mesh(mesh, reader_ptr->bulk_elements_id_, reader_ptr->boundary_elements_id_);
78 }
79 
80 void ReaderCache::get_element_ids(const FilePath &file_path, const Mesh &mesh) {
81  auto reader_ptr = ReaderCache::get_reader(file_path);
82  reader_ptr->has_compatible_mesh_ = true;
83  mesh.elements_id_maps(reader_ptr->bulk_elements_id_, reader_ptr->boundary_elements_id_);
84 }
static void get_element_ids(const FilePath &file_path, const Mesh &mesh)
Definition: reader_cache.cc:80
Definition: mesh.h:76
ReaderCache()
Constructor.
Definition: reader_cache.hh:69
static ReaderCache * instance()
Returns singleton instance.
Definition: reader_cache.cc:31
Accessor to the data with type Type::Record.
Definition: accessors.hh:292
std::shared_ptr< BaseMeshReader > reader_
Definition: reader_cache.hh:38
static bool check_compatible_mesh(const FilePath &file_path, Mesh &mesh)
Definition: reader_cache.cc:73
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:739
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:75
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