Chaste  Release::3.4
AbstractTetrahedralMeshWriter.hpp
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 
37 #ifndef _ABSTRACTTETRAHEDRALMESHWRITER_HPP_
38 #define _ABSTRACTTETRAHEDRALMESHWRITER_HPP_
39 
40 // Forward declaration prevents circular include chain
41 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
43 
44 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
46 
47 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
48 class MixedDimensionMesh;
49 
50 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
51 struct MeshWriterIterators;
52 
53 #include <fstream>
54 #include <sstream>
55 #include <boost/scoped_array.hpp>
56 
57 #include "AbstractMeshWriter.hpp"
58 #include "AbstractMesh.hpp"
59 #include "NodeMap.hpp"
60 
61 #include "GenericEventHandler.hpp"
63 class MeshEventHandler : public GenericEventHandler<11, MeshEventHandler>
64 {
65 public:
66  static const char* EventName[11];
69  typedef enum
70  {
71  TRIANGLES=0,
72  BINTRI,
73  VTK,
74  PVTK,
75  NODE,
76  ELE,
77  FACE,
78  NCL,
79  COMM1,
80  COMM2
81  } EventType;
82 };
83 
87 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
88 class AbstractTetrahedralMeshWriter : public AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>
89 {
90 private:
99  void PostElement(unsigned globalIndex, unsigned indices[], unsigned numIndices, unsigned tag, double attribute)
100  {
101  MeshEventHandler::BeginEvent(MeshEventHandler::COMM1);
102  MPI_Ssend(indices, numIndices, MPI_UNSIGNED, 0,
103  tag, //Elements sent with tags offset
104  PETSC_COMM_WORLD);
105  MeshEventHandler::EndEvent(MeshEventHandler::COMM1);
106  // Attribute value has the same tag (assume that it doesn't overtake the previous message)
107  MeshEventHandler::BeginEvent(MeshEventHandler::COMM2);
108  MPI_Ssend(&attribute, 1, MPI_DOUBLE, 0,
109  tag, //Elements sent with tags offset
110  PETSC_COMM_WORLD);
111  MeshEventHandler::EndEvent(MeshEventHandler::COMM2);
112  }
120  void UnpackElement(ElementData& rElementData, unsigned globalIndex, unsigned numIndices, unsigned tag)
121  {
122  assert( numIndices == rElementData.NodeIndices.size());
123  boost::scoped_array<unsigned> raw_indices(new unsigned[numIndices]);
124  MPI_Status status;
125  // Get it from elsewhere
126  MeshEventHandler::BeginEvent(MeshEventHandler::COMM1);
127  MPI_Recv(raw_indices.get(), numIndices, MPI_UNSIGNED, MPI_ANY_SOURCE,
128  tag,
129  PETSC_COMM_WORLD, &status);
130  MeshEventHandler::EndEvent(MeshEventHandler::COMM1);
131  // Convert to std::vector
132  for (unsigned j=0; j< rElementData.NodeIndices.size(); j++)
133  {
134  rElementData.NodeIndices[j] = raw_indices[j];
135  }
136 
137  // Attribute value has the same tag (assume that it doesn't overtake the previous message)
138  double attribute;
139  MeshEventHandler::BeginEvent(MeshEventHandler::COMM2);
140  MPI_Recv(&attribute, 1U, MPI_DOUBLE, MPI_ANY_SOURCE,
141  tag,
142  PETSC_COMM_WORLD, &status);
143  MeshEventHandler::EndEvent(MeshEventHandler::COMM2);
144 
145  // Attribute value
146  rElementData.AttributeValue = attribute;
147  }
148 
155  virtual void WriteFilesUsingParallelMesh(bool keepOriginalElementIndexing=true);
156 
165  bool invertMeshPermutation=false);
166 
170  virtual void CreateFilesWithHeaders();
171 
175  virtual void AppendLocalDataToFiles();
176 
180  virtual void WriteFilesFooter();
181 
184 protected:
185 
186  unsigned mNodesPerElement;
202 public:
203 
211  AbstractTetrahedralMeshWriter(const std::string& rDirectory,
212  const std::string& rBaseName,
213  const bool clearOutputDir=true);
214 
219 
230  bool keepOriginalElementIndexing=true);
231 
243 
247  std::vector<double> GetNextNode();
248 
249 
254 
259 
264 };
265 
266 #endif //_ABSTRACTTETRAHEDRALMESHWRITER_HPP_
void UnpackElement(ElementData &rElementData, unsigned globalIndex, unsigned numIndices, unsigned tag)
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpMesh
AbstractTetrahedralMeshWriter(const std::string &rDirectory, const std::string &rBaseName, const bool clearOutputDir=true)
MeshWriterIterators< ELEMENT_DIM, SPACE_DIM > * mpIters
std::vector< unsigned > NodeIndices
void WriteNclFile(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool invertMeshPermutation=false)
static const char * EventName[11]
DistributedTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpDistributedMesh
virtual void WriteFilesUsingParallelMesh(bool keepOriginalElementIndexing=true)
void PostElement(unsigned globalIndex, unsigned indices[], unsigned numIndices, unsigned tag, double attribute)
MixedDimensionMesh< ELEMENT_DIM, SPACE_DIM > * mpMixedMesh
virtual void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)
void WriteFilesUsingMeshReaderAndMesh(AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > &rMeshReader, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)