Chaste  Release::3.4
MeshalyzerMeshWriter.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 "MeshalyzerMeshWriter.hpp"
37 #include "Version.hpp"
38 
39 // We need these two includes for the node/element/face iterators to compile
40 #include "AbstractTetrahedralMesh.hpp"
41 #include "DistributedTetrahedralMesh.hpp"
42 
43 
45 // Implementation
47 
48 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
50  const std::string& rBaseName,
51  const bool &rCleanDirectory,
52  const bool &rSetCoolGraphics)
53  : AbstractTetrahedralMeshWriter<ELEMENT_DIM, SPACE_DIM>(rDirectory, rBaseName, rCleanDirectory)
54 {
55  /* if (ELEMENT_DIM != SPACE_DIM)
56  {
57  EXCEPTION("ELEMENT_DIM must be equal to SPACE_DIM");
58  }*/
59 
60  if (rSetCoolGraphics)
61  {
62  this->mIndexFromZero = false;
63  this->mWriteMetaFile = true;
64  }
65  else
66  {
67  this->mIndexFromZero = true;
68  this->mWriteMetaFile = false;
69  }
70 }
71 
72 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
74 {
75  std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
76 
77  //Write node file
78  out_stream p_node_file = OpenNodeFile();
79 
80  //Write the node header
81  unsigned num_nodes = this->GetNumNodes();
82  *p_node_file << num_nodes << "\n";
83 
84  // Write each node's data
85  for (unsigned item_num=0; item_num<num_nodes; item_num++)
86  {
87  std::vector<double> current_item = this->GetNextNode(); //this->mNodeData[item_num];
88  for (unsigned i=0; i<SPACE_DIM; i++)
89  {
90  *p_node_file << current_item[i] << "\t";
91  }
92  if (SPACE_DIM==2)
93  {
94  *p_node_file << 0 << "\t";
95  }
96  if (SPACE_DIM==1)
97  {
98  *p_node_file << 0 << "\t" << 0 << "\t";
99  }
100  *p_node_file << "\n";
101 
102  }
103  *p_node_file << comment;
104  p_node_file->close();
105 
106  // Write element file
107  std::string element_file_name;
108 
109  if (ELEMENT_DIM == 3)
110  {
111  element_file_name = this->mBaseName + ".tetras";
112  }
113  else if (ELEMENT_DIM == 2)
114  {
115  element_file_name = this->mBaseName + ".tri";
116  }
117  else //ELEMENT_DIM == 1
118  {
119  element_file_name = this->mBaseName + ".cnnx";
120  }
121 
122  out_stream p_element_file = OpenElementFile();
123 
124  // Write the element header
125  unsigned num_elements = this->GetNumElements();
126 
127  *p_element_file << num_elements << "\n";
128 
129  // Write each element's data
130  unsigned nodes_per_element = ELEMENT_DIM+1;
131  for (unsigned item_num=0; item_num<num_elements; item_num++)
132  {
133  ElementData element_data = this->GetNextElement();
134 
135  std::vector<unsigned> current_item = element_data.NodeIndices;
136  for (unsigned i=0; i<nodes_per_element; i++)
137  {
138  if (this->mIndexFromZero)
139  {
140  *p_element_file << current_item[i] << "\t";
141  }
142  else
143  {
144  *p_element_file << current_item[i]+1 << "\t";
145  }
146  }
147 
148  *p_element_file << element_data.AttributeValue << "\n";
149  }
150  *p_element_file << comment;
151  p_element_file->close();
152 
153  if (ELEMENT_DIM==3)
154  {
155  // Write boundary face file
156  out_stream p_face_file = OpenFaceFile();
157 
158  // Write the boundary face header
159  unsigned num_faces = this->GetNumBoundaryFaces();
160 
161  *p_face_file << num_faces << "\n";
162 
163  // Write each face's data
164  double material_property = 0.0;
165  for (unsigned item_num=0; item_num<num_faces; item_num++)
166  {
167  ElementData current_item = this->GetNextBoundaryElement();
168  for (unsigned i=0; i<ELEMENT_DIM; i++)
169  {
170  if (this->mIndexFromZero)
171  {
172  *p_face_file << current_item.NodeIndices[i] << "\t";
173  }
174  else
175  {
176  *p_face_file << current_item.NodeIndices[i]+1 <<"\t";
177  }
178  }
179  *p_face_file << material_property << "\n";
180  }
181  *p_face_file << comment;
182  p_face_file->close();
183 
184  WriteMetaFile();
185  }
186 }
187 
188 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
190 {
191 }
192 
193 
194 
195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
197 {
198  if (this->mWriteMetaFile)
199  {
200  std::string meta_file_name = this->mBaseName + ".cg_in";
201  out_stream p_meta_file = this->mpOutputFileHandler->OpenOutputFile(meta_file_name);
202 
203  *p_meta_file << "1\n" << "0\n";
204  std::string face_file_name = this->mBaseName + ".tri";
205  *p_meta_file << face_file_name <<"\n";
206  std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
207  *p_meta_file << comment;
208  p_meta_file->close();
209  }
210 }
211 
212 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
214 {
215  std::ios_base::openmode mode = std::ios::out;
216  if (append)
217  {
218  mode |= std::ios::app; // Note: bitwise OR operation
219  }
220  else
221  {
222  mode |= std::ios::trunc;
223  }
224  return mode;
225 }
226 
227 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
229 {
230  std::string node_file_name = this->mBaseName + ".pts";
231  return this->mpOutputFileHandler->OpenOutputFile(node_file_name, GetOpenMode(append));
232 }
233 
234 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
236 {
237  std::string element_file_name;
238 
239  if (ELEMENT_DIM == 3)
240  {
241  element_file_name = this->mBaseName + ".tetras";
242  }
243  else if (ELEMENT_DIM == 2)
244  {
245  element_file_name = this->mBaseName + ".tri";
246  }
247  else //ELEMENT_DIM == 1
248  {
249  element_file_name = this->mBaseName + ".cnnx";
250  }
251 
252  return this->mpOutputFileHandler->OpenOutputFile(element_file_name, GetOpenMode(append));
253 }
254 
255 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
257 {
258  std::string face_file_name = this->mBaseName + ".tri";
259  return this->mpOutputFileHandler->OpenOutputFile(face_file_name, GetOpenMode(append));
260 }
261 
262 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
264 {
265  /*
266  * Node file
267  */
268  out_stream p_node_file = OpenNodeFile();
269 
270  //Write the node header
271  unsigned num_nodes = this->GetNumNodes();
272  *p_node_file << num_nodes << "\n";
273 
274  p_node_file->close();
275 
276  /*
277  * Element file
278  */
279  // Write element file
280  out_stream p_element_file = OpenElementFile();
281 
282  // Write the element header
283  unsigned num_elements = this->GetNumElements();
284  *p_element_file << num_elements << "\n";
285 
286  p_element_file->close();
287 
288  /*
289  * Face file
290  */
291  if (ELEMENT_DIM==3)
292  {
293  // Write boundary face file
294  out_stream p_face_file = OpenFaceFile();
295 
296  // Write the boundary face header
297  unsigned num_faces = this->GetNumBoundaryFaces();
298  *p_face_file << num_faces << "\n";
299 
300  p_face_file->close();
301 
302  WriteMetaFile();
303  }
304 
305 }
306 
307 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
309 {
310  out_stream p_node_file = OpenNodeFile(true);
311 
312  typedef typename AbstractMesh<ELEMENT_DIM,SPACE_DIM>::NodeIterator NodeIterType;
313 
314  for (NodeIterType iter = this->mpDistributedMesh->GetNodeIteratorBegin();
315  iter != this->mpDistributedMesh->GetNodeIteratorEnd();
316  ++iter)
317  {
318  const c_vector<double, SPACE_DIM>& r_current_item = iter->rGetLocation();
319  for (unsigned i=0; i<SPACE_DIM; i++)
320  {
321  *p_node_file << r_current_item[i] << "\t";
322  }
323  if (SPACE_DIM==2)
324  {
325  *p_node_file << 0 << "\t";
326  }
327  if (SPACE_DIM==1)
328  {
329  *p_node_file << 0 << "\t" << 0 << "\t";
330  }
331  *p_node_file << "\n";
332  }
333  p_node_file->close();
334 
335  out_stream p_element_file = OpenElementFile(true);
336 
338 
339  for (ElemIterType iter = this->mpDistributedMesh->GetElementIteratorBegin();
340  iter != this->mpDistributedMesh->GetElementIteratorEnd();
341  ++iter)
342  {
343  if ( this->mpDistributedMesh->CalculateDesignatedOwnershipOfElement(iter->GetIndex()))
344  {
345  for (unsigned i=0; i<this->mNodesPerElement; i++)
346  {
347  if (this->mIndexFromZero)
348  {
349  *p_element_file << iter->GetNodeGlobalIndex(i) << "\t";
350  }
351  else
352  {
353  *p_element_file << iter->GetNodeGlobalIndex(i)+1 << "\t";
354  }
355  }
356 
357  *p_element_file << iter->GetAttribute() << "\n";
358  }
359  }
360  p_element_file->close();
361 
362 
363  if (ELEMENT_DIM == 3)
364  {
365  out_stream p_face_file = OpenFaceFile(true);
366 
368 
369  for (BoundaryElemIterType iter = this->mpDistributedMesh->GetBoundaryElementIteratorBegin();
370  iter != this->mpDistributedMesh->GetBoundaryElementIteratorEnd();
371  ++iter)
372  {
373  if ( this->mpDistributedMesh->CalculateDesignatedOwnershipOfBoundaryElement((*iter)->GetIndex()))
374  {
375  for (unsigned i=0; i<ELEMENT_DIM; i++)
376  {
377  if (this->mIndexFromZero)
378  {
379  *p_face_file << (*iter)->GetNodeGlobalIndex(i) << "\t";
380  }
381  else
382  {
383  *p_face_file << (*iter)->GetNodeGlobalIndex(i)+1 << "\t";
384  }
385  }
386 
387  *p_face_file << (*iter)->GetAttribute() << "\n";
388  }
389  }
390  p_face_file->close();
391  }
392 }
393 
394 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
396 {
397  std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
398 
399  out_stream p_node_file = OpenNodeFile(true);
400  *p_node_file << comment;
401  p_node_file->close();
402 
403  out_stream p_element_file = OpenElementFile(true);
404  *p_element_file << comment;
405  p_element_file->close();
406 
407  if (ELEMENT_DIM == 3)
408  {
409  out_stream p_face_file = OpenFaceFile(true);
410  *p_face_file << comment;
411  p_face_file->close();
412  }
413 }
414 
415 
417 // Explicit instantiation
419 
420 template class MeshalyzerMeshWriter<1,1>;
421 template class MeshalyzerMeshWriter<1,2>;
422 template class MeshalyzerMeshWriter<1,3>;
423 template class MeshalyzerMeshWriter<2,2>;
424 template class MeshalyzerMeshWriter<2,3>;
425 template class MeshalyzerMeshWriter<3,3>;
MeshalyzerMeshWriter(const std::string &rDirectory, const std::string &rBaseName, const bool &rCleanDirectory=true, const bool &rSetCoolGraphics=false)
out_stream OpenNodeFile(bool append=false)
out_stream OpenFaceFile(bool append=false)
std::ios_base::openmode GetOpenMode(bool append)
out_stream OpenElementFile(bool append=false)
std::vector< unsigned > NodeIndices
static std::string GetProvenanceString()
std::vector< BoundaryElement< ELEMENT_DIM-1, SPACE_DIM > * >::const_iterator BoundaryElementIterator