AbstractTetrahedralMeshWriter.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2015, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 
00037 #ifndef _ABSTRACTTETRAHEDRALMESHWRITER_HPP_
00038 #define _ABSTRACTTETRAHEDRALMESHWRITER_HPP_
00039 
00040 // Forward declaration prevents circular include chain
00041 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00042 class AbstractTetrahedralMesh;
00043 
00044 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00045 class DistributedTetrahedralMesh;
00046 
00047 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00048 class MixedDimensionMesh;
00049 
00050 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00051 struct MeshWriterIterators;
00052 
00053 #include <fstream>
00054 #include <sstream>
00055 #include <boost/scoped_array.hpp>
00056 
00057 #include "AbstractMeshWriter.hpp"
00058 #include "AbstractMesh.hpp"
00059 #include "NodeMap.hpp"
00060 
00061 #include "GenericEventHandler.hpp"
00063 class MeshEventHandler : public GenericEventHandler<11, MeshEventHandler>
00064 {
00065 public:
00066     static const char* EventName[11]; 
00069     typedef enum
00070     {
00071         TRIANGLES=0,
00072         BINTRI,
00073         VTK,
00074         PVTK,
00075         NODE,
00076         ELE,
00077         FACE,
00078         NCL,
00079         COMM1,
00080         COMM2
00081     } EventType;
00082 };
00083 
00087 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00088 class AbstractTetrahedralMeshWriter : public AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>
00089 {
00090 private:
00099     void PostElement(unsigned globalIndex, unsigned indices[], unsigned numIndices, unsigned tag, double attribute)
00100     {
00101         MeshEventHandler::BeginEvent(MeshEventHandler::COMM1);
00102         MPI_Ssend(indices, numIndices, MPI_UNSIGNED, 0,
00103                   tag, //Elements sent with tags offset
00104                   PETSC_COMM_WORLD);
00105         MeshEventHandler::EndEvent(MeshEventHandler::COMM1);
00106         // Attribute value has the same tag (assume that it doesn't overtake the previous message)
00107         MeshEventHandler::BeginEvent(MeshEventHandler::COMM2);
00108         MPI_Ssend(&attribute, 1, MPI_DOUBLE, 0,
00109                   tag, //Elements sent with tags offset
00110                   PETSC_COMM_WORLD);
00111         MeshEventHandler::EndEvent(MeshEventHandler::COMM2);
00112     }
00120     void UnpackElement(ElementData& rElementData, unsigned globalIndex, unsigned numIndices, unsigned tag)
00121     {
00122         assert( numIndices == rElementData.NodeIndices.size());
00123         boost::scoped_array<unsigned> raw_indices(new unsigned[numIndices]);
00124         MPI_Status status;
00125         // Get it from elsewhere
00126         MeshEventHandler::BeginEvent(MeshEventHandler::COMM1);
00127         MPI_Recv(raw_indices.get(), numIndices, MPI_UNSIGNED, MPI_ANY_SOURCE,
00128                  tag,
00129                  PETSC_COMM_WORLD, &status);
00130         MeshEventHandler::EndEvent(MeshEventHandler::COMM1);
00131         // Convert to std::vector
00132         for (unsigned j=0; j< rElementData.NodeIndices.size(); j++)
00133         {
00134             rElementData.NodeIndices[j] = raw_indices[j];
00135         }
00136 
00137         // Attribute value has the same tag (assume that it doesn't overtake the previous message)
00138         double attribute;
00139         MeshEventHandler::BeginEvent(MeshEventHandler::COMM2);
00140         MPI_Recv(&attribute, 1U, MPI_DOUBLE, MPI_ANY_SOURCE,
00141                  tag,
00142                  PETSC_COMM_WORLD, &status);
00143         MeshEventHandler::EndEvent(MeshEventHandler::COMM2);
00144 
00145         // Attribute value
00146         rElementData.AttributeValue = attribute;
00147     }
00148 
00155     virtual void WriteFilesUsingParallelMesh(bool keepOriginalElementIndexing=true);
00156 
00164     void WriteNclFile(AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00165                       bool invertMeshPermutation=false);
00166 
00170     virtual void CreateFilesWithHeaders();
00171 
00175     virtual void AppendLocalDataToFiles();
00176 
00180     virtual void WriteFilesFooter();
00181 
00182     NodeMap* mpNodeMap; 
00184 protected:
00185 
00186     unsigned mNodesPerElement; 
00187     unsigned mNodesPerBoundaryElement; 
00189     AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* mpMesh; 
00190     DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* mpDistributedMesh; 
00191     MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>* mpMixedMesh; 
00192     MeshWriterIterators<ELEMENT_DIM,SPACE_DIM>* mpIters; 
00194     bool mIndexFromZero; 
00195     bool mWriteMetaFile; 
00196     unsigned mNodeCounterForParallelMesh; 
00197     unsigned mElementCounterForParallelMesh;
00198     unsigned mBoundaryElementCounterForParallelMesh;
00199     unsigned mCableElementCounterForParallelMesh;
00200     bool mFilesAreBinary;  
00202 public:
00203 
00211     AbstractTetrahedralMeshWriter(const std::string& rDirectory,
00212                        const std::string& rBaseName,
00213                        const bool clearOutputDir=true);
00214 
00218     virtual ~AbstractTetrahedralMeshWriter();
00219 
00229     virtual void WriteFilesUsingMesh(AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00230                                      bool keepOriginalElementIndexing=true);
00231 
00241     void WriteFilesUsingMeshReaderAndMesh(AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader,
00242                                           AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00243 
00247     std::vector<double> GetNextNode();
00248 
00249 
00253     ElementData GetNextElement();
00254 
00258     ElementData GetNextBoundaryElement();
00259 
00263     ElementData GetNextCableElement();
00264 };
00265 
00266 #endif //_ABSTRACTTETRAHEDRALMESHWRITER_HPP_

Generated by  doxygen 1.6.2