Chaste Commit::29c3c4ab2bee00ddd311a035a1e522c03cbe928a
Hdf5ToVtkConverter.cpp
1/*
2
3Copyright (c) 2005-2026, 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
37#include "Hdf5ToVtkConverter.hpp"
38#include "PetscTools.hpp"
39#include "Exception.hpp"
40#include "ReplicatableVector.hpp"
41#include "DistributedVector.hpp"
42#include "DistributedVectorFactory.hpp"
43#include "VtkMeshWriter.hpp"
44#include "GenericMeshReader.hpp"
45#include "DistributedTetrahedralMesh.hpp"
46#include "Warnings.hpp"
47
48template <unsigned ELEMENT_DIM, unsigned SPACE_DIM>
50 const std::string& rFileBaseName,
52 bool parallelVtk,
53 bool usingOriginalNodeOrdering)
54 : AbstractHdf5Converter<ELEMENT_DIM,SPACE_DIM>(rInputDirectory, rFileBaseName, pMesh, "vtk_output",0u)
55{
56 // Write mesh in a suitable form for VTK
58 std::string output_directory = rInputDirectory.GetRelativePath(test_output) + "/" + this->mRelativeSubdirectory;
59
60 VtkMeshWriter<ELEMENT_DIM,SPACE_DIM> vtk_writer(output_directory, rFileBaseName, false);
61
63
64 // Make sure that we are never trying to write from an incomplete data HDF5 file
65 assert(this->mpReader->GetNumberOfRows() == pMesh->GetNumNodes());
66
68
69 unsigned num_nodes = pMesh->GetNumNodes();
70 if (parallelVtk)
71 {
72 // If it's not a distributed mesh, then we might want to give a warning and back-off
73 if (p_distributed_mesh == nullptr)
74 {
75 WARNING("Can only write parallel VTK from a DistributedTetrahedralMesh - writing sequential VTK instead");
76 parallelVtk = false;
77 }
78
79 // If the node ordering flag is set, then we can't do this
80 if (usingOriginalNodeOrdering)
81 {
82 WARNING("Can't write parallel VTK (pvtu) files with original ordering - writing sequential VTK instead");
83 parallelVtk = false;
84 }
85
86 // Are we now committed to writing .pvtu?
87 if (parallelVtk)
88 {
89 vtk_writer.SetParallelFiles(*pMesh);
90 num_nodes = p_distributed_mesh->GetNumLocalNodes();
91 }
92 }
93
94 Vec data = p_factory->CreateVec();
95
96 do // Loop over datasets via MoveOntoNextDataset method in the abstract class
97 {
98 // Make sure that we are never trying to write from an incomplete HDF5 dataset.
99 assert(this->mpReader->GetNumberOfRows() == pMesh->GetNumNodes());
100
101 unsigned num_timesteps = this->mpReader->GetUnlimitedDimensionValues().size();
102
103 // Loop over time steps
104 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
105 {
106 // Loop over variables
107 for (unsigned variable=0; variable<this->mNumVariables; variable++)
108 {
109 std::string variable_name = this->mpReader->GetVariableNames()[variable];
110
111 // Gets variable at this time step from HDF5 archive
112 this->mpReader->GetVariableOverNodes(data, variable_name, time_step);
113
114 std::vector<double> data_for_vtk;
115 data_for_vtk.resize(num_nodes);
116 std::ostringstream variable_point_data_name;
117 variable_point_data_name << variable_name << "_" << std::setw(6) << std::setfill('0') << time_step;
118
119 if (parallelVtk)
120 {
121 // Parallel VTU files
122 double *p_data;
123 VecGetArray(data, &p_data);
124 for (unsigned index=0; index<num_nodes; index++)
125 {
126 data_for_vtk[index] = p_data[index];
127 }
128 VecRestoreArray(data, &p_data);
129 }
130 else
131 {
132 // One VTU file
133 ReplicatableVector repl_data(data);
134 for (unsigned index=0; index<num_nodes; index++)
135 {
136 data_for_vtk[index] = repl_data[index];
137 }
138 }
139 // Add this variable into the node "point" data
140 vtk_writer.AddPointData(variable_point_data_name.str(), data_for_vtk);
141 }
142 }
143 }
144 while ( this->MoveOntoNextDataset() );
145
146 // Tidy up
148
149 // Normally the in-memory mesh is converted
150 if (!usingOriginalNodeOrdering)
151 {
152 vtk_writer.WriteFilesUsingMesh(*(this->mpMesh));
153 }
154 else
155 {
156 // In this case we expect the mesh to have been read in from file
158 // Note that the next line will throw if the mesh has not been read from file
159 std::string original_file = this->mpMesh->GetMeshFileBaseName();
160 auto p_original_mesh_reader = GenericMeshReader<ELEMENT_DIM, SPACE_DIM>(original_file);
161 vtk_writer.WriteFilesUsingMeshReader(*p_original_mesh_reader);
162 }
163}
164
165// Explicit instantiation
166template class Hdf5ToVtkConverter<1,1>;
167template class Hdf5ToVtkConverter<1,2>;
168template class Hdf5ToVtkConverter<2,2>;
169template class Hdf5ToVtkConverter<1,3>;
170template class Hdf5ToVtkConverter<2,3>;
171template class Hdf5ToVtkConverter<3,3>;
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpMesh
boost::shared_ptr< Hdf5DataReader > mpReader
void WriteFilesUsingMeshReader(AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > &rMeshReader)
virtual DistributedVectorFactory * GetDistributedVectorFactory()
virtual unsigned GetNumNodes() const
std::string GetRelativePath(const FileFinder &rBasePath) const
Hdf5ToVtkConverter(const FileFinder &rInputDirectory, const std::string &rFileBaseName, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh, bool parallelVtk, bool usingOriginalNodeOrdering)
static void Destroy(Vec &rVec)
void AddPointData(std::string name, std::vector< double > data)
void SetParallelFiles(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh)
void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)