AbstractTetrahedralMeshWriter.hpp

00001 /*
00002 
00003 Copyright (c) 2005-2014, 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 //#include "Debug.hpp"
00084 
00088 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00089 class AbstractTetrahedralMeshWriter : public AbstractMeshWriter<ELEMENT_DIM, SPACE_DIM>
00090 {
00091 private:
00100     void PostElement(unsigned globalIndex, unsigned indices[], unsigned numIndices, unsigned tag, double attribute)
00101     {
00102         MeshEventHandler::BeginEvent(MeshEventHandler::COMM1);
00103         MPI_Ssend(indices, numIndices, MPI_UNSIGNED, 0,
00104                   tag, //Elements sent with tags offset
00105                   PETSC_COMM_WORLD);
00106         MeshEventHandler::EndEvent(MeshEventHandler::COMM1);
00107         // Attribute value has the same tag (assume that it doesn't overtake the previous message)
00108         MeshEventHandler::BeginEvent(MeshEventHandler::COMM2);
00109         MPI_Ssend(&attribute, 1, MPI_DOUBLE, 0,
00110                   tag, //Elements sent with tags offset
00111                   PETSC_COMM_WORLD);
00112         MeshEventHandler::EndEvent(MeshEventHandler::COMM2);
00113     }
00121     void UnpackElement(ElementData& rElementData, unsigned globalIndex, unsigned numIndices, unsigned tag)
00122     {
00123         assert( numIndices == rElementData.NodeIndices.size());
00124         boost::scoped_array<unsigned> raw_indices(new unsigned[numIndices]);
00125         MPI_Status status;
00126         // Get it from elsewhere
00127         MeshEventHandler::BeginEvent(MeshEventHandler::COMM1);
00128         MPI_Recv(raw_indices.get(), numIndices, MPI_UNSIGNED, MPI_ANY_SOURCE,
00129                  tag,
00130                  PETSC_COMM_WORLD, &status);
00131         MeshEventHandler::EndEvent(MeshEventHandler::COMM1);
00132         // Convert to std::vector
00133         for (unsigned j=0; j< rElementData.NodeIndices.size(); j++)
00134         {
00135             rElementData.NodeIndices[j] = raw_indices[j];
00136         }
00137 
00138         // Attribute value has the same tag (assume that it doesn't overtake the previous message)
00139         double attribute;
00140         MeshEventHandler::BeginEvent(MeshEventHandler::COMM2);
00141         MPI_Recv(&attribute, 1U, MPI_DOUBLE, MPI_ANY_SOURCE,
00142                  tag,
00143                  PETSC_COMM_WORLD, &status);
00144         MeshEventHandler::EndEvent(MeshEventHandler::COMM2);
00145 
00146         // Attribute value
00147         rElementData.AttributeValue = attribute;
00148     }
00149 
00156     virtual void WriteFilesUsingParallelMesh(bool keepOriginalElementIndexing=true);
00157 
00165     void WriteNclFile(AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00166                       bool invertMeshPermutation=false);
00167 
00171     virtual void CreateFilesWithHeaders();
00172 
00176     virtual void AppendLocalDataToFiles();
00177 
00181     virtual void WriteFilesFooter();
00182 
00183     NodeMap* mpNodeMap; 
00185 protected:
00186 
00187     unsigned mNodesPerElement; 
00188     unsigned mNodesPerBoundaryElement; 
00190     AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* mpMesh; 
00191     DistributedTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* mpDistributedMesh; 
00192     MixedDimensionMesh<ELEMENT_DIM,SPACE_DIM>* mpMixedMesh; 
00193     MeshWriterIterators<ELEMENT_DIM,SPACE_DIM>* mpIters; 
00195     bool mIndexFromZero; 
00196     bool mWriteMetaFile; 
00197     unsigned mNodeCounterForParallelMesh; 
00198     unsigned mElementCounterForParallelMesh;
00199     unsigned mBoundaryElementCounterForParallelMesh;
00200     unsigned mCableElementCounterForParallelMesh;
00201     bool mFilesAreBinary;  
00203 public:
00204 
00212     AbstractTetrahedralMeshWriter(const std::string& rDirectory,
00213                        const std::string& rBaseName,
00214                        const bool clearOutputDir=true);
00215 
00219     virtual ~AbstractTetrahedralMeshWriter();
00220 
00230     virtual void WriteFilesUsingMesh(AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>& rMesh,
00231                                      bool keepOriginalElementIndexing=true);
00232 
00242     void WriteFilesUsingMeshReaderAndMesh(AbstractMeshReader<ELEMENT_DIM, SPACE_DIM>& rMeshReader,
00243                                           AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM>& rMesh);
00244 
00248     std::vector<double> GetNextNode();
00249 
00250 
00254     ElementData GetNextElement();
00255 
00259     ElementData GetNextBoundaryElement();
00260 
00264     ElementData GetNextCableElement();
00265 };
00266 
00267 #endif //_ABSTRACTTETRAHEDRALMESHWRITER_HPP_

Generated by  doxygen 1.6.2