Chaste  Release::2017.1
PottsMeshReader.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 
38 template<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();
49  ReadHeaders();
50 }
51 
52 template<unsigned SPACE_DIM>
54 {
55  return mNumElements;
56 }
57 
58 template<unsigned SPACE_DIM>
60 {
61  return mNumNodes;
62 }
63 
64 template<unsigned SPACE_DIM>
66 {
67  return mNumElementAttributes;
68 }
69 
70 template<unsigned SPACE_DIM>
72 {
74  ElementData ret;
75  ret.NodeIndices = std::vector<unsigned>();
76  ret.AttributeValue = 0;
77  return ret;
78 }
79 
80 template<unsigned SPACE_DIM>
82 {
84  return 0;
85 }
86 
87 template<unsigned SPACE_DIM>
89 {
90  CloseFiles();
91  OpenFiles();
92  ReadHeaders();
93 
94  mNodesRead = 0;
95  mElementsRead = 0;
96 }
97 
98 template<unsigned SPACE_DIM>
100 {
101  std::vector<double> node_data;
102 
103  std::string 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 
128 template<unsigned SPACE_DIM>
130 {
131  // Create data structure for this element
132  ElementData element_data;
133 
134  std::string 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 
176 template<unsigned SPACE_DIM>
178 {
179  OpenNodeFile();
181 }
182 
183 template<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 
195 template<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 
209 template<unsigned SPACE_DIM>
211 {
212  std::string buffer;
213 
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
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();
231 
233  std::stringstream element_buffer_stream(buffer);
234 
235  element_buffer_stream >> mNumElements >> mNumElementAttributes;
236 }
237 
238 template<unsigned SPACE_DIM>
240 {
241  mNodesFile.close();
242  mElementsFile.close();
243 }
244 
245 template<unsigned SPACE_DIM>
246 void 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
268 template class PottsMeshReader<1>;
269 template class PottsMeshReader<2>;
270 template class PottsMeshReader<3>;
std::ifstream mElementsFile
std::vector< double > GetNextNode()
#define EXCEPTION(message)
Definition: Exception.hpp:143
unsigned GetNumNodes() const
std::ifstream mNodesFile
unsigned GetNumElementAttributes() const
void GetNextLineFromStream(std::ifstream &fileStream, std::string &rawLine)
unsigned mNumNodeAttributes
unsigned mNumElementAttributes
std::vector< unsigned > NodeIndices
PottsMeshReader(std::string pathBaseName)
ElementData GetNextElementData()
unsigned GetNumFaces() const
ElementData GetNextFaceData()
std::string mFilesBaseName
unsigned GetNumElements() const