Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
AbstractCachedMeshReader.cpp
1/*
2
3Copyright (c) 2005-2024, 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
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 // Initialize an interator for the vector of nodes
107 std::vector<std::vector<unsigned> >::iterator the_iterator;
108
109 unsigned max_node_index = 0; // Nice if it were negative
110
111 for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
112 {
113 std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
114
115 for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
116 {
117 if (indices[i] > max_node_index)
118 {
119 max_node_index = indices[i];
120 }
121 }
122 }
123
124 return max_node_index;
125}
126
127template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
129{
130 // Initialize an interator for the vector of nodes
131 std::vector<std::vector<unsigned> >::iterator the_iterator;
132
133 unsigned min_node_index = UINT_MAX; // A large integer
134
135 for (the_iterator = mElementData.begin(); the_iterator < mElementData.end(); the_iterator++)
136 {
137 std::vector<unsigned> indices = *the_iterator; // the_iterator points at each line in turn
138
139 for (unsigned i = 0; i < ELEMENT_DIM+1; i++)
140 {
141 if (indices[i] < min_node_index)
142 {
143 min_node_index = indices[i];
144 }
145 }
146 }
147
148 return min_node_index;
149}
150
151template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
153{
154 // Checks that there are still some nodes left to read. If not throws an
155 // exception that must be caught by the user.
156 if (mpNodeIterator == mNodeData.end())
157 {
158 EXCEPTION("All nodes already got");
159 }
160
161 std::vector<double> next_node = *mpNodeIterator;
162
163 mpNodeIterator++;
164
165 return next_node;
166}
167
168template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
170{
171 // Checks that there are still some elements left to read. If not throws an
172 // exception that must be caught by the user.
173 if (mpElementIterator == mElementData.end())
174 {
175 EXCEPTION("All elements already got");
176 }
177
178 ElementData ret;
179 ret.NodeIndices = *mpElementIterator;
180 ret.AttributeValue = 0;
181
182 mpElementIterator++;
183
184 return ret;
185}
186
187template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
189{
190 mpElementIterator = mElementData.begin();
191 mpFaceIterator = mFaceData.begin();
192 mpNodeIterator = mNodeData.begin();
193}
194
195template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
197{
198 // Checks that there are still some faces left to read. If not throws an
199 // exception that must be caught by the user.
200 if (mpFaceIterator == mFaceData.end())
201 {
202 EXCEPTION("All faces (or edges) already got");
203 }
204
205 ElementData ret;
206 ret.NodeIndices = *mpFaceIterator;
207 ret.AttributeValue = 0;
208
209 mpFaceIterator++;
210
211 return ret;
212}
213
214template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
216{
217 return mElementData.size();
218}
219
220template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
222{
223 return mNodeData.size();
224}
225
226template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
228{
229 return mFaceData.size();
230}
231
232// Explicit instantiation
233template class AbstractCachedMeshReader<1,1>;
234template class AbstractCachedMeshReader<1,2>;
235template class AbstractCachedMeshReader<1,3>;
236template class AbstractCachedMeshReader<2,2>;
237template class AbstractCachedMeshReader<2,3>;
238template class AbstractCachedMeshReader<3,3>;
#define EXCEPTION(message)
std::vector< double > GetNextNode()
std::vector< std::string > GetRawDataFromFile(const std::string &rFileName)
std::vector< unsigned > NodeIndices