Chaste Commit::1fd4e48e3990e67db148bc1bc4cf6991a0049d0c
AbstractPerElementWriter.hpp
1/*
2
3Copyright (c) 2005-2024, University of Oxford.
4All rights reserved.
5
6University of Oxford means the Chancellor, Masters and Scholars of the
7University of Oxford, having an administrative office at Wellington
8Square, Oxford OX1 2JD, UK.
9
10This file is part of Chaste.
11
12Redistribution and use in source and binary forms, with or without
13modification, 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
23THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32OF 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"
40#include "OutputFileHandler.hpp"
41#include "Version.hpp"
42
48template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned DATA_SIZE>
50{
51private:
54protected:
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
123public:
124
137
149 void WriteData(OutputFileHandler& rHandler, const std::string& rFileName)
150 {
151 PreWriteCalculations(rHandler);
152
153 c_vector<double, DATA_SIZE> data;
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 }
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 WriteElementOnMaster(const c_vector< double, DATA_SIZE > &rData)
AbstractPerElementWriter(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
void SetWriteFileAsBinary(bool binary=true)
void WriteData(OutputFileHandler &rHandler, const std::string &rFileName)
virtual void Visit(Element< ELEMENT_DIM, SPACE_DIM > *pElement, unsigned localElementIndex, c_vector< double, DATA_SIZE > &rData)=0
virtual void PreWriteCalculations(OutputFileHandler &rOutputDirectory)
static std::string GetProvenanceString()
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
static bool AmMaster()