Chaste Commit::baa90ac2819b962188b7562f2326be23c47859a7
VoltageInterpolaterOntoMechanicsMesh.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 "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
45template<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);
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
147// Explicit instantiation
unsigned GetNumNodes() const
unsigned GetNodeGlobalIndex(unsigned localIndex) const
virtual DistributedVectorFactory * GetDistributedVectorFactory()
virtual unsigned GetNumNodes() const
Element< ELEMENT_DIM, SPACE_DIM > * GetElement(unsigned index) const
void SetUpBoxesOnFineMesh(double boxWidth=-1)
void ComputeFineElementsAndWeightsForCoarseNodes(bool safeMode)
std::vector< ElementAndWeights< DIM > > & rGetElementsAndWeights()
void GetVariableOverNodes(Vec data, const std::string &rVariableName, unsigned timestep=0)
std::vector< double > GetUnlimitedDimensionValues()
void DefineUnlimitedDimension(const std::string &rVariableName, const std::string &rVariableUnits, unsigned estimatedLength=1)
void PutUnlimitedVariable(double value)
void AdvanceAlongUnlimitedDimension()
void DefineFixedDimension(long dimensionSize)
int DefineVariable(const std::string &rVariableName, const std::string &rVariableUnits)
virtual void EndDefineMode()
void PutVector(int variableID, Vec petscVector)
void SetOutputDirectory(const std::string &rOutputDirectory)
std::string GetOutputDirectory() const
static HeartConfig * Instance()
static void Destroy(Vec &rVec)
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
VoltageInterpolaterOntoMechanicsMesh(TetrahedralMesh< DIM, DIM > &rElectricsMesh, QuadraticMesh< DIM > &rMechanicsMesh, std::vector< std::string > &rVariableNames, std::string directory, std::string inputFileNamePrefix)