Chaste  Release::3.4
VoltageInterpolaterOntoMechanicsMesh.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 "VoltageInterpolaterOntoMechanicsMesh.hpp"
37 #include "Hdf5ToCmguiConverter.hpp"
38 #include "FineCoarseMeshPair.hpp"
39 #include "ReplicatableVector.hpp"
40 #include "HeartConfig.hpp"
41 #include "Hdf5DataReader.hpp"
42 #include "PetscTools.hpp"
43 #include "Hdf5DataWriter.hpp"
44 
45 template<unsigned DIM>
47  TetrahedralMesh<DIM,DIM>& rElectricsMesh,
48  QuadraticMesh<DIM>& rMechanicsMesh,
49  std::vector<std::string>& rVariableNames,
50  std::string directory,
51  std::string inputFileNamePrefix)
52 {
53  // Read the data from the HDF5 file
54  Hdf5DataReader reader(directory,inputFileNamePrefix);
55 
56  unsigned num_timesteps = reader.GetUnlimitedDimensionValues().size();
57 
58  // set up the elements and weights for the coarse nodes in the fine mesh
59  FineCoarseMeshPair<DIM> mesh_pair(rElectricsMesh, rMechanicsMesh);
60  mesh_pair.SetUpBoxesOnFineMesh();
62  assert(mesh_pair.rGetElementsAndWeights().size()==rMechanicsMesh.GetNumNodes());
63 
64  // create and setup a writer
65  Hdf5DataWriter* p_writer = new Hdf5DataWriter(*rMechanicsMesh.GetDistributedVectorFactory(),
66  directory,
67  "voltage_mechanics_mesh",
68  false, //don't clean
69  false);
70 
71  std::vector<int> columns_id;
72  for (unsigned var_index = 0; var_index < rVariableNames.size(); var_index++)
73  {
74  std::string var_name = rVariableNames[var_index];
75  columns_id.push_back( p_writer->DefineVariable(var_name,"mV") );
76  }
77 
78  p_writer->DefineUnlimitedDimension("Time","msecs", num_timesteps);
79  p_writer->DefineFixedDimension( rMechanicsMesh.GetNumNodes() );
80  p_writer->EndDefineMode();
81 
82  assert(columns_id.size() == rVariableNames.size());
83 
84  // set up a vector to read into
85  DistributedVectorFactory factory(rElectricsMesh.GetNumNodes());
86  Vec voltage = factory.CreateVec();
87  std::vector<double> interpolated_voltages(rMechanicsMesh.GetNumNodes());
88  Vec voltage_coarse = NULL;
89 
90  for(unsigned time_step=0; time_step<num_timesteps; time_step++)
91  {
92  for (unsigned var_index = 0; var_index < rVariableNames.size(); var_index++)
93  {
94  std::string var_name = rVariableNames[var_index];
95  // read
96  reader.GetVariableOverNodes(voltage, var_name, time_step);
97  ReplicatableVector voltage_repl(voltage);
98 
99  // interpolate
100  for(unsigned i=0; i<mesh_pair.rGetElementsAndWeights().size(); i++)
101  {
102  double interpolated_voltage = 0;
103 
104  Element<DIM,DIM>& element = *(rElectricsMesh.GetElement(mesh_pair.rGetElementsAndWeights()[i].ElementNum));
105  for(unsigned node_index = 0; node_index<element.GetNumNodes(); node_index++)
106  {
107  unsigned global_node_index = element.GetNodeGlobalIndex(node_index);
108  interpolated_voltage += voltage_repl[global_node_index]*mesh_pair.rGetElementsAndWeights()[i].Weights(node_index);
109  }
110 
111  interpolated_voltages[i] = interpolated_voltage;
112  }
113 
114  if(voltage_coarse!=NULL)
115  {
116  PetscTools::Destroy(voltage_coarse);
117  }
118  voltage_coarse = PetscTools::CreateVec(interpolated_voltages);
119  // write
120  p_writer->PutVector(columns_id[var_index], voltage_coarse);
121  }
122  p_writer->PutUnlimitedVariable(time_step);
123  p_writer->AdvanceAlongUnlimitedDimension();
124  }
125 
126  if(voltage_coarse!=NULL)
127  {
128  PetscTools::Destroy(voltage);
129  PetscTools::Destroy(voltage_coarse);
130  }
131 
132  // delete to flush
133  delete p_writer;
134 
135  // Convert the new data to CMGUI format.
136  // alter the directory in HeartConfig as that is where Hdf5ToCmguiConverter decides
137  // where to output
138  std::string config_directory = HeartConfig::Instance()->GetOutputDirectory();
141  "voltage_mechanics_mesh",
142  &rMechanicsMesh,
143  false);
144  HeartConfig::Instance()->SetOutputDirectory(config_directory);
145 }
146 
148 // explicit instantiation
150 
virtual DistributedVectorFactory * GetDistributedVectorFactory()
void SetOutputDirectory(const std::string &rOutputDirectory)
unsigned GetNodeGlobalIndex(unsigned localIndex) const
void ComputeFineElementsAndWeightsForCoarseNodes(bool safeMode)
std::vector< ElementAndWeights< DIM > > & rGetElementsAndWeights()
Element< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
Definition: PetscTools.cpp:214
virtual unsigned GetNumNodes() const
VoltageInterpolaterOntoMechanicsMesh(TetrahedralMesh< DIM, DIM > &rElectricsMesh, QuadraticMesh< DIM > &rMechanicsMesh, std::vector< std::string > &rVariableNames, std::string directory, std::string inputFileNamePrefix)
void GetVariableOverNodes(Vec data, const std::string &rVariableName, unsigned timestep=0)
unsigned GetNumNodes() const
std::vector< double > GetUnlimitedDimensionValues()
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:351
void SetUpBoxesOnFineMesh(double boxWidth=-1)
std::string GetOutputDirectory() const
static HeartConfig * Instance()
void DefineUnlimitedDimension(const std::string &rVariableName, const std::string &rVariableUnits, unsigned estimatedLength=1)