Chaste  Release::2017.1
Hdf5ToMeshalyzerConverter.cpp
1 /*
2 
3 Copyright (c) 2005-2017, 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 
36 #include "Hdf5ToMeshalyzerConverter.hpp"
37 #include "MeshalyzerMeshWriter.hpp"
38 #include "GenericMeshReader.hpp"
39 #include "UblasCustomFunctions.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 
47 template <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  }
92  PetscTools::Destroy(data);
94  {
95  std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
96  *p_file << comment;
97  p_file->close();
98  }
99 }
100 
101 template <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
151 template class Hdf5ToMeshalyzerConverter<1,1>;
152 template class Hdf5ToMeshalyzerConverter<1,2>;
153 template class Hdf5ToMeshalyzerConverter<2,2>;
154 template class Hdf5ToMeshalyzerConverter<1,3>;
155 template class Hdf5ToMeshalyzerConverter<2,3>;
156 template class Hdf5ToMeshalyzerConverter<3,3>;
static void Barrier(const std::string callerId="")
Definition: PetscTools.cpp:134
bool IsFile() const
Definition: FileFinder.cpp:185
boost::shared_ptr< Hdf5DataReader > mpReader
static bool AmMaster()
Definition: PetscTools.cpp:120
void WriteFilesUsingMeshReader(AbstractMeshReader< ELEMENT_DIM, SPACE_DIM > &rMeshReader)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
void ReplicatePetscVector(Vec vec)
Hdf5ToMeshalyzerConverter(const FileFinder &rInputDirectory, const std::string &rFileBaseName, AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh, bool usingOriginalNodeOrdering, unsigned precision=0)
static std::string GetProvenanceString()
virtual void WriteFilesUsingMesh(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, bool keepOriginalElementIndexing=true)
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > * mpMesh
std::string GetRelativePath(const FileFinder &rBasePath) const
Definition: FileFinder.cpp:261