Chaste  Release::3.4
PottsMeshReader.cpp
1 /*
2 
3 Copyright (c) 2005-2016, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, 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 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
36 #include "PottsMeshReader.hpp"
37 #include "Exception.hpp"
38 
39 #include <sstream>
40 
41 template<unsigned SPACE_DIM>
43  : mFilesBaseName(pathBaseName),
44  mIndexFromZero(false), // initially assume that nodes are not numbered from zero
45  mNumNodes(0),
46  mNumElements(0),
47  mNodesRead(0),
48  mElementsRead(0),
49  mNumElementAttributes(0)
50 {
51  OpenFiles();
52  ReadHeaders();
53 }
54 
55 template<unsigned SPACE_DIM>
57 {
58  return mNumElements;
59 }
60 
61 template<unsigned SPACE_DIM>
63 {
64  return mNumNodes;
65 }
66 
67 template<unsigned SPACE_DIM>
69 {
70  return mNumElementAttributes;
71 }
72 
73 template<unsigned SPACE_DIM>
75 {
77  ElementData ret;
78  ret.NodeIndices = std::vector<unsigned>();
79  ret.AttributeValue = 0;
80  return ret;
81 }
82 
83 template<unsigned SPACE_DIM>
85 {
87  return 0;
88 }
89 
90 template<unsigned SPACE_DIM>
92 {
93  CloseFiles();
94  OpenFiles();
95  ReadHeaders();
96 
97  mNodesRead = 0;
98  mElementsRead = 0;
99 }
100 
101 template<unsigned SPACE_DIM>
103 {
104  std::vector<double> node_data;
105 
106  std::string buffer;
107  GetNextLineFromStream(mNodesFile, buffer);
108 
109  std::stringstream buffer_stream(buffer);
110 
111  unsigned index;
112  buffer_stream >> index;
113 
114  unsigned offset = mIndexFromZero ? 0 : 1;
115  if (index != mNodesRead + offset)
116  {
117  EXCEPTION("Data for node " << mNodesRead << " missing");
118  }
119 
120  double node_value;
121  for (unsigned i=0; i<SPACE_DIM+1; i++)
122  {
123  buffer_stream >> node_value;
124  node_data.push_back(node_value);
125  }
126 
127  mNodesRead++;
128  return node_data;
129 }
130 
131 template<unsigned SPACE_DIM>
133 {
134  // Create data structure for this element
135  ElementData element_data;
136 
137  std::string buffer;
138  GetNextLineFromStream(mElementsFile, buffer);
139 
140  std::stringstream buffer_stream(buffer);
141 
142  unsigned element_index;
143  buffer_stream >> element_index;
144 
145  unsigned offset = mIndexFromZero ? 0 : 1;
146  if (element_index != mElementsRead + offset)
147  {
148  EXCEPTION("Data for element " << mElementsRead << " missing");
149  }
150 
151  unsigned num_nodes_in_element;
152  buffer_stream >> num_nodes_in_element;
153 
154  // Store node indices owned by this element
155  unsigned node_index;
156  for (unsigned i=0; i<num_nodes_in_element; i++)
157  {
158  buffer_stream >> node_index;
159  element_data.NodeIndices.push_back(node_index - offset);
160  }
161 
162  if (mNumElementAttributes > 0)
163  {
164  assert(mNumElementAttributes==1);
165 
166  unsigned attribute_value;
167  buffer_stream >> attribute_value;
168  element_data.AttributeValue = attribute_value;
169  }
170  else
171  {
172  element_data.AttributeValue = 0;
173  }
174 
175  mElementsRead++;
176  return element_data;
177 }
178 
179 template<unsigned SPACE_DIM>
181 {
182  OpenNodeFile();
183  OpenElementsFile();
184 }
185 
186 template<unsigned SPACE_DIM>
188 {
189  // Nodes definition
190  std::string file_name = mFilesBaseName + ".node";
191  mNodesFile.open(file_name.c_str());
192  if (!mNodesFile.is_open())
193  {
194  EXCEPTION("Could not open data file: " + file_name);
195  }
196 }
197 
198 template<unsigned SPACE_DIM>
200 {
201  // Elements definition
202  std::string file_name;
203  file_name = mFilesBaseName + ".cell";
204 
205  mElementsFile.open(file_name.c_str());
206  if (!mElementsFile.is_open())
207  {
208  EXCEPTION("Could not open data file: " + file_name);
209  }
210 }
211 
212 template<unsigned SPACE_DIM>
214 {
215  std::string buffer;
216 
217  GetNextLineFromStream(mNodesFile, buffer);
218  std::stringstream buffer_stream(buffer);
219  buffer_stream >> mNumNodes >> mNumNodeAttributes;
220 
221  // Get the next line to see if nodes are indexed from zero or not
222  GetNextLineFromStream(mNodesFile, buffer);
223  std::stringstream node_buffer_stream(buffer);
224 
225  unsigned first_index;
226  node_buffer_stream >> first_index;
227  assert(first_index == 0 || first_index == 1);
228  mIndexFromZero = (first_index == 0);
229 
230  // Close, reopen, skip header
231  mNodesFile.close();
232  OpenNodeFile();
233  GetNextLineFromStream(mNodesFile, buffer);
234 
235  GetNextLineFromStream(mElementsFile, buffer);
236  std::stringstream element_buffer_stream(buffer);
237 
238  element_buffer_stream >> mNumElements >> mNumElementAttributes;
239 }
240 
241 template<unsigned SPACE_DIM>
243 {
244  mNodesFile.close();
245  mElementsFile.close();
246 }
247 
248 template<unsigned SPACE_DIM>
249 void PottsMeshReader<SPACE_DIM>::GetNextLineFromStream(std::ifstream& fileStream, std::string& rawLine)
250 {
251  bool line_is_blank;
252 
253  do
254  {
255  getline(fileStream, rawLine);
256 
257  if (fileStream.eof())
258  {
259  EXCEPTION("Cannot get the next line from node or element file due to incomplete data");
260  }
261 
262  // Get rid of any comment
263  rawLine = rawLine.substr(0,rawLine.find('#', 0));
264 
265  line_is_blank = (rawLine.find_first_not_of(" \t", 0) == std::string::npos);
266  }
267  while (line_is_blank);
268 }
269 
270 // Explicit instantiation
271 template class PottsMeshReader<1>;
272 template class PottsMeshReader<2>;
273 template class PottsMeshReader<3>;
std::vector< double > GetNextNode()
#define EXCEPTION(message)
Definition: Exception.hpp:143
unsigned GetNumNodes() const
unsigned GetNumElementAttributes() const
void GetNextLineFromStream(std::ifstream &fileStream, std::string &rawLine)
std::vector< unsigned > NodeIndices
PottsMeshReader(std::string pathBaseName)
ElementData GetNextElementData()
unsigned GetNumFaces() const
ElementData GetNextFaceData()
unsigned GetNumElements() const