Chaste  Release::2018.1
AbstractPerElementWriter.hpp
1 /*
2 
3 Copyright (c) 2005-2018, 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 #ifndef ABSTRACTPERELEMENTWRITER_HPP_
36 #define ABSTRACTPERELEMENTWRITER_HPP_
37 
38 #include "AbstractTetrahedralMesh.hpp"
39 #include "UblasCustomFunctions.hpp"
40 #include "OutputFileHandler.hpp"
41 #include "Version.hpp"
42 
48 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned DATA_SIZE>
50 {
51 private:
54 protected:
59 
64  out_stream mpMasterFile;
65 
74  virtual void Visit(Element<ELEMENT_DIM, SPACE_DIM>* pElement,
75  unsigned localElementIndex,
76  c_vector<double, DATA_SIZE>& rData)=0;
77 
85  virtual void WriteElementOnMaster(const c_vector<double, DATA_SIZE>& rData)
86  {
87  if (mFileIsBinary)
88  {
89  //The binary file is row-major
90  mpMasterFile->write((char*)&rData[0], DATA_SIZE*sizeof(double));
91  }
92  else
93  {
94  for (unsigned i=0; i<DATA_SIZE; i++)
95  {
96  (*mpMasterFile) << rData[i] << "\t";
97  }
98  (*mpMasterFile)<<"\n";
99  }
100  }
101 
109  virtual void WriteHeaderOnMaster()
110  {
111  }
112 
119  virtual void PreWriteCalculations(OutputFileHandler& rOutputDirectory)
120  {
121  }
122 
123 public:
124 
131  : mFileIsBinary(false),
132  mpMesh(pMesh),
133  mpMasterFile(nullptr)
134  {
135 
136  }
137 
149  void WriteData(OutputFileHandler& rHandler, const std::string& rFileName)
150  {
151  PreWriteCalculations(rHandler);
152 
153  c_vector<double, DATA_SIZE> data;
154  if (PetscTools::AmMaster())
155  {
156  mpMasterFile = rHandler.OpenOutputFile(rFileName);
157  MPI_Status status;
158  status.MPI_ERROR = MPI_SUCCESS; //For MPICH2
160  // say whether the fibres are binary in the header line (not ended yet!)
161  if (mFileIsBinary)
162  {
163  *mpMasterFile << "\tBIN\n";
164  }
165  else
166  {
167  *mpMasterFile << "\n";
168  }
169  // The master process needs to keep track of both the global element list
170  // (so that all elements are concentrated) and the local element list (so that
171  // a local element index can be applied
172  unsigned local_element_index=0u; //Data invariant associates iter with local_element_index
173  typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ElementIterator iter = mpMesh->GetElementIteratorBegin();
174 
175  for (unsigned global_element_index=0; global_element_index<mpMesh->GetNumElements(); global_element_index++)
176  {
177  if (mpMesh->CalculateDesignatedOwnershipOfElement(global_element_index))
178  {
179  Element<ELEMENT_DIM,SPACE_DIM>* p_elem = mpMesh->GetElement(global_element_index);
180  //Spool forward in the local vector of elements
181  while (&(*iter) != p_elem)
182  {
183  ++iter;
184  local_element_index++;
185  assert(iter != mpMesh->GetElementIteratorEnd());
186  }
187  //Master owns this process and can write it directly
188  Visit(p_elem, local_element_index, data);
189  }
190  else
191  {
192  //Data must come from a remote process
193  MPI_Recv(&data[0], DATA_SIZE, MPI_DOUBLE, MPI_ANY_SOURCE, global_element_index, PETSC_COMM_WORLD, &status);
194  }
195  WriteElementOnMaster(data);
196  }
198  mpMasterFile->close();
199  }
200  else
201  {
202  //Not master process
203  unsigned previous_index = 0u; //Used to check that the indices are monotone
204  unsigned local_index = 0u;
205  for (typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ElementIterator iter = mpMesh->GetElementIteratorBegin();
206  iter != mpMesh->GetElementIteratorEnd();
207  ++iter, local_index++)
208  {
209  unsigned element_index = iter->GetIndex();
210  //Check monotonicity
211  if (previous_index>0u)
212  {
213  assert(element_index > previous_index);
214  }
215  previous_index = element_index;
216  if (mpMesh->CalculateDesignatedOwnershipOfElement(element_index))
217  {
218  // The master needs to know about this one.
219  Visit(&(*iter), local_index, data);
221  MPI_Ssend(&data[0], DATA_SIZE, MPI_DOUBLE, 0, element_index, PETSC_COMM_WORLD);//Tag with element_index
222  }
223  }
224 
225  }
226  }
227 
235  void SetWriteFileAsBinary(bool binary=true)
236  {
237  mFileIsBinary = binary;
238  }
239 
244  {
245  }
246 };
247 
248 
249 #endif /*ABSTRACTPERELEMENTWRITER_HPP_*/
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpMesh
virtual void PreWriteCalculations(OutputFileHandler &rOutputDirectory)
static bool AmMaster()
Definition: PetscTools.cpp:120
virtual void WriteElementOnMaster(const c_vector< double, DATA_SIZE > &rData)
void SetWriteFileAsBinary(bool binary=true)
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
virtual void Visit(Element< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localElementIndex, c_vector< double, DATA_SIZE > &rData)=0
void WriteData(OutputFileHandler &rHandler, const std::string &rFileName)
static std::string GetProvenanceString()
AbstractPerElementWriter(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)