Chaste Commit::f2ff7ee04e70ac9d06c57344df8d017dbb12b97b
Hdf5ToMeshalyzerConverter.cpp
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
36#include "Hdf5ToMeshalyzerConverter.hpp"
37#include "MeshalyzerMeshWriter.hpp"
38#include "GenericMeshReader.hpp"
40#include "PetscTools.hpp"
41#include "Exception.hpp"
42#include "ReplicatableVector.hpp"
43#include "DistributedVector.hpp"
44#include "DistributedVectorFactory.hpp"
45#include "Version.hpp"
46
47template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
49{
50
51 std::string filename = "";
52 if (this->mDatasetNames[this->mOpenDatasetIndex] == "Data")
53 {
54 filename += this->mFileBaseName + "_";
55 }
56 filename += type + ".dat";
57
58 out_stream p_file = out_stream(nullptr);
60 {
61 p_file = this->mpOutputFileHandler->OpenOutputFile(filename);
62
63 // Check how many digits are to be output in the solution (0 goes to default value of digits)
64 if (this->mPrecision != 0)
65 {
66 p_file->precision(this->mPrecision);
67 }
68 }
69
70 unsigned num_nodes = this->mpReader->GetNumberOfRows();
71 unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
72
73 DistributedVectorFactory factory(num_nodes);
74
75 Vec data = factory.CreateVec();
76 ReplicatableVector repl_data(num_nodes);
77 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
78 {
79 this->mpReader->GetVariableOverNodes(data, type, time_step);
80 repl_data.ReplicatePetscVector(data);
81
82 assert(repl_data.GetSize() == num_nodes);
83
85 {
86 for (unsigned i=0; i<num_nodes; i++)
87 {
88 *p_file << repl_data[i] << "\n";
89 }
90 }
91 }
94 {
95 std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
96 *p_file << comment;
97 p_file->close();
98 }
99}
100
101template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
103 const std::string& rFileBaseName,
105 bool usingOriginalNodeOrdering,
106 unsigned precision)
107 : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "output", precision)
108{
109 do
110 {
111 std::vector<std::string> variable_names = this->mpReader->GetVariableNames();
112 for (unsigned i=0; i<variable_names.size(); i++)
113 {
114 Write(variable_names[i]);
115 }
116 }
117 while ( this->MoveOntoNextDataset() );
118
119 // Now we might call this class more than once, so we don't always need to write the mesh out.
120 // so check to see if it is there already.
122 std::string output_directory = rInputDirectory.GetRelativePath(test_output) + "/" + this->mRelativeSubdirectory;
123 FileFinder mesh_file(output_directory + "/" + rFileBaseName + "_mesh.pts", RelativeTo::ChasteTestOutput);
124
125 if (!mesh_file.IsFile())
126 {
127 // Write mesh in a suitable form for meshalyzer
128 MeshalyzerMeshWriter<ELEMENT_DIM,SPACE_DIM> mesh_writer(output_directory, rFileBaseName + "_mesh", false);
129
130 // Normal case is that the in-memory mesh is converted
131 if (!usingOriginalNodeOrdering || !this->mpMesh->IsMeshOnDisk())
132 {
133 // The second argument tells the writer to not follow original element ordering for performance reasons.
134 mesh_writer.WriteFilesUsingMesh(*(this->mpMesh), false);
135 }
136 else
137 {
138 // In this case we expect the mesh to have been read in from file
140 // Note that the next line will throw if the mesh has not been read from file
141 std::string original_file = this->mpMesh->GetMeshFileBaseName();
142 std::shared_ptr<AbstractMeshReader<ELEMENT_DIM, SPACE_DIM> > p_original_mesh_reader
143 = GenericMeshReader<ELEMENT_DIM, SPACE_DIM>(original_file);
144 mesh_writer.WriteFilesUsingMeshReader(*p_original_mesh_reader);
145 }
146 }
147 PetscTools::Barrier("Hdf5ToMeshalyzerConverter");
148}
149
150// Explicit instantiation
151template class Hdf5ToMeshalyzerConverter<1,1>;
152template class Hdf5ToMeshalyzerConverter<1,2>;
153template class Hdf5ToMeshalyzerConverter<2,2>;
154template class Hdf5ToMeshalyzerConverter<1,3>;
155template class Hdf5ToMeshalyzerConverter<2,3>;
156template class Hdf5ToMeshalyzerConverter<3,3>;
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpMesh
boost::shared_ptr< Hdf5DataReader > mpReader
void WriteFilesUsingMeshReader(AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > &rMeshReader)
virtual void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)
static std::string GetProvenanceString()
std::string GetRelativePath(const FileFinder &rBasePath) const
bool IsFile() const
Hdf5ToMeshalyzerConverter(const FileFinder &rInputDirectory, const std::string &rFileBaseName, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh, bool usingOriginalNodeOrdering, unsigned precision=0)
static void Destroy(Vec &rVec)
static bool AmMaster()
static void Barrier(const std::string callerId="")
void ReplicatePetscVector(Vec vec)