Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
PottsMeshReader.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 "PottsMeshReader.hpp"
37
38template<unsigned SPACE_DIM>
40 : mFilesBaseName(pathBaseName),
41 mIndexFromZero(false), // initially assume that nodes are not numbered from zero
42 mNumNodes(0),
43 mNumElements(0),
44 mNodesRead(0),
45 mElementsRead(0),
46 mNumElementAttributes(0)
47{
48 OpenFiles();
50}
51
52template<unsigned SPACE_DIM>
54{
55 return mNumElements;
56}
57
58template<unsigned SPACE_DIM>
60{
61 return mNumNodes;
62}
63
64template<unsigned SPACE_DIM>
66{
67 return mNumElementAttributes;
68}
69
70template<unsigned SPACE_DIM>
72{
74 ElementData ret;
75 ret.NodeIndices = std::vector<unsigned>();
76 ret.AttributeValue = 0;
77 return ret;
78}
79
80template<unsigned SPACE_DIM>
82{
84 return 0;
85}
86
87template<unsigned SPACE_DIM>
89{
90 CloseFiles();
91 OpenFiles();
92 ReadHeaders();
93
94 mNodesRead = 0;
95 mElementsRead = 0;
96}
97
98template<unsigned SPACE_DIM>
100{
101 std::vector<double> node_data;
102
103 std::string buffer;
104 GetNextLineFromStream(mNodesFile, buffer);
105
106 std::stringstream buffer_stream(buffer);
107
108 unsigned index;
109 buffer_stream >> index;
110
111 unsigned offset = mIndexFromZero ? 0 : 1;
112 if (index != mNodesRead + offset)
113 {
114 EXCEPTION("Data for node " << mNodesRead << " missing");
115 }
116
117 double node_value;
118 for (unsigned i=0; i<SPACE_DIM+1; i++)
119 {
120 buffer_stream >> node_value;
121 node_data.push_back(node_value);
122 }
123
124 mNodesRead++;
125 return node_data;
126}
127
128template<unsigned SPACE_DIM>
130{
131 // Create data structure for this element
132 ElementData element_data;
133
134 std::string buffer;
135 GetNextLineFromStream(mElementsFile, buffer);
136
137 std::stringstream buffer_stream(buffer);
138
139 unsigned element_index;
140 buffer_stream >> element_index;
141
142 unsigned offset = mIndexFromZero ? 0 : 1;
143 if (element_index != mElementsRead + offset)
144 {
145 EXCEPTION("Data for element " << mElementsRead << " missing");
146 }
147
148 unsigned num_nodes_in_element;
149 buffer_stream >> num_nodes_in_element;
150
151 // Store node indices owned by this element
152 unsigned node_index;
153 for (unsigned i=0; i<num_nodes_in_element; i++)
154 {
155 buffer_stream >> node_index;
156 element_data.NodeIndices.push_back(node_index - offset);
157 }
158
159 if (mNumElementAttributes > 0)
160 {
161 assert(mNumElementAttributes==1);
162
163 unsigned attribute_value;
164 buffer_stream >> attribute_value;
165 element_data.AttributeValue = attribute_value;
166 }
167 else
168 {
169 element_data.AttributeValue = 0;
170 }
171
172 mElementsRead++;
173 return element_data;
174}
175
176template<unsigned SPACE_DIM>
178{
179 OpenNodeFile();
180 OpenElementsFile();
181}
182
183template<unsigned SPACE_DIM>
185{
186 // Nodes definition
187 std::string file_name = mFilesBaseName + ".node";
188 mNodesFile.open(file_name.c_str());
189 if (!mNodesFile.is_open())
190 {
191 EXCEPTION("Could not open data file: " + file_name);
192 }
193}
194
195template<unsigned SPACE_DIM>
197{
198 // Elements definition
199 std::string file_name;
200 file_name = mFilesBaseName + ".cell";
201
202 mElementsFile.open(file_name.c_str());
203 if (!mElementsFile.is_open())
204 {
205 EXCEPTION("Could not open data file: " + file_name);
206 }
207}
208
209template<unsigned SPACE_DIM>
211{
212 std::string buffer;
213
214 GetNextLineFromStream(mNodesFile, buffer);
215 std::stringstream buffer_stream(buffer);
216 buffer_stream >> mNumNodes >> mNumNodeAttributes;
217
218 // Get the next line to see if nodes are indexed from zero or not
219 GetNextLineFromStream(mNodesFile, buffer);
220 std::stringstream node_buffer_stream(buffer);
221
222 unsigned first_index;
223 node_buffer_stream >> first_index;
224 assert(first_index == 0 || first_index == 1);
225 mIndexFromZero = (first_index == 0);
226
227 // Close, reopen, skip header
228 mNodesFile.close();
229 OpenNodeFile();
230 GetNextLineFromStream(mNodesFile, buffer);
231
232 GetNextLineFromStream(mElementsFile, buffer);
233 std::stringstream element_buffer_stream(buffer);
234
235 element_buffer_stream >> mNumElements >> mNumElementAttributes;
236}
237
238template<unsigned SPACE_DIM>
240{
241 mNodesFile.close();
242 mElementsFile.close();
243}
244
245template<unsigned SPACE_DIM>
246void PottsMeshReader<SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
247{
248 bool line_is_blank;
249
250 do
251 {
252 getline(fileStream, rawLine);
253
254 if (fileStream.eof())
255 {
256 EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
257 }
258
259 // Get rid of any comment
260 rawLine = rawLine.substr(0,rawLine.find('#', 0));
261
262 line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
263 }
264 while (line_is_blank);
265}
266
267// Explicit instantiation
268template class PottsMeshReader<1>;
269template class PottsMeshReader<2>;
270template class PottsMeshReader<3>;
#define EXCEPTION(message)
void GetNextLineFromStream(std::ifstream &fileStream, std::string &rawLine)
ElementData GetNextElementData()
unsigned GetNumNodes() const
unsigned GetNumFaces() const
std::vector< double > GetNextNode()
unsigned GetNumElements() const
PottsMeshReader(std::string pathBaseName)
unsigned GetNumElementAttributes() const
ElementData GetNextFaceData()
std::vector< unsigned > NodeIndices