Chaste Commit::9dfedaa0870fc7cfa8911a7ba21eba441bdba6b4
AbstractCachedMeshReader.cpp
1/*
2
3Copyright (c) 2005-2026, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, are permitted provided that the following conditions are met:
14 * Redistributions of source code must retain the above copyright notice,
15 this list of conditions and the following disclaimer.
16 * Redistributions in binary form must reproduce the above copyright notice,
17 this list of conditions and the following disclaimer in the documentation
18 and/or other materials provided with the distribution.
19 * Neither the name of the University of Oxford nor the names of its
20 contributors may be used to endorse or promote products derived from this
21 software without specific prior written permission.
22
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33
34*/
35
36#include "AbstractCachedMeshReader.hpp"
37#include "Exception.hpp"
38#include <algorithm>
39#include <fstream>
40
42// Implementation
44
45template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
47 : mNumNodeAttributes(0),
48 mMaxNodeBdyMarker(0),
49 mNumElementNodes(0),
50 mNumElementAttributes(0),
51 mMaxFaceBdyMarker(0),
52 mIndexFromZero(false) // Initially assume that nodes are not numbered from zero
53{
54 // We have initialized all numeric variables to zero
55}
56
57template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
59 const std::string& rFileName)
60{
61 // Open raw data file
62
63 std::vector<std::string> raw_data;
64 std::ifstream data_file(rFileName.c_str());
65
66 // Checks that input file has been opened correctly. If not throws an
67 // exception that should be caught by the user.
68 if (!data_file.is_open())
69 {
70 EXCEPTION("Could not open data file " + rFileName);
71 }
72
73 // Read each line in turn
74 std::string raw_line;
75 getline(data_file, raw_line);
76
77 while (data_file)
78 {
79 // Remove comments (everything from a hash to the end of the line)
80 // If there is no hash, then hashLocation = string::npos = -1 = 4294967295 = UINT_MAX
81 // (so it works with unsigneds but is a little nasty)
82 long hash_location = raw_line.find('#', 0);
83 if (hash_location >= 0)
84 {
85 raw_line = raw_line.substr(0, hash_location);
86 }
87 // Remove blank lines. This is unnecessary, since the tokenizer will
88 // ignore blank lines anyway.
89 long not_blank_location = raw_line.find_first_not_of(" \t", 0);
90 if (not_blank_location >= 0)
91 {
92 raw_data.push_back(raw_line);
93 }
94
95 // Move onto next line
96 getline(data_file, raw_line);
97 }
98
99 data_file.close(); // Closes the data file
100 return raw_data;
101}
102
103template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
105{
106 unsigned max_node_index = 0;
107
108 for (const auto& indices : mElementData)
109 {
110 max_node_index = std::max(max_node_index, *std::max_element(indices.begin(), indices.begin() + (ELEMENT_DIM+1)));
111 }
112
113 return max_node_index;
114}
115
116template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
118{
119 unsigned min_node_index = UINT_MAX;
120
121 for (const auto& indices : mElementData)
122 {
123 min_node_index = std::min(min_node_index, *std::min_element(indices.begin(), indices.begin() + (ELEMENT_DIM+1)));
124 }
125
126 return min_node_index;
127}
128
129template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
131{
132 // Checks that there are still some nodes left to read. If not throws an
133 // exception that must be caught by the user.
134 if (mpNodeIterator == mNodeData.end())
135 {
136 EXCEPTION("All nodes already got");
137 }
138
139 std::vector<double> next_node = *mpNodeIterator;
140
141 mpNodeIterator++;
142
143 return next_node;
144}
145
146template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
148{
149 // Checks that there are still some elements left to read. If not throws an
150 // exception that must be caught by the user.
151 if (mpElementIterator == mElementData.end())
152 {
153 EXCEPTION("All elements already got");
154 }
155
156 ElementData ret;
157 ret.NodeIndices = *mpElementIterator;
158 ret.AttributeValue = 0;
159
160 mpElementIterator++;
161
162 return ret;
163}
164
165template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
167{
168 mpElementIterator = mElementData.begin();
169 mpFaceIterator = mFaceData.begin();
170 mpNodeIterator = mNodeData.begin();
171}
172
173template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
175{
176 // Checks that there are still some faces left to read. If not throws an
177 // exception that must be caught by the user.
178 if (mpFaceIterator == mFaceData.end())
179 {
180 EXCEPTION("All faces (or edges) already got");
181 }
182
183 ElementData ret;
184 ret.NodeIndices = *mpFaceIterator;
185 ret.AttributeValue = 0;
186
187 mpFaceIterator++;
188
189 return ret;
190}
191
192template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
194{
195 return mElementData.size();
196}
197
198template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
200{
201 return mNodeData.size();
202}
203
204template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
206{
207 return mFaceData.size();
208}
209
210// Explicit instantiation
211template class AbstractCachedMeshReader<1,1>;
212template class AbstractCachedMeshReader<1,2>;
213template class AbstractCachedMeshReader<1,3>;
214template class AbstractCachedMeshReader<2,2>;
215template class AbstractCachedMeshReader<2,3>;
216template class AbstractCachedMeshReader<3,3>;
#define EXCEPTION(message)
std::vector< double > GetNextNode()
std::vector< std::string > GetRawDataFromFile(const std::string &rFileName)
std::vector< unsigned > NodeIndices