Chaste Commit::675f9facbe008c5eacb9006feaeb6423206579ea
VoltageInterpolaterOntoMechanicsMesh.cpp
1/*
2
3Copyright (c) 2005-2025, 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 "HeartConfig.hpp"
39#include "Hdf5DataReader.hpp"
40#include "PetscTools.hpp"
41#include "Hdf5DataWriter.hpp"
42
43
44template<unsigned DIM>
46 TetrahedralMesh<DIM,DIM>& rElectricsMesh,
47 QuadraticMesh<DIM>& rMechanicsMesh)
48 : mrElectricsMesh(rElectricsMesh),
49 mrMechanicsMesh(rMechanicsMesh),
50 mpMeshPair(new FineCoarseMeshPair<DIM>(rElectricsMesh, rMechanicsMesh))
51
52{
53 assert(mpMeshPair);
54 mpMeshPair->SetUpBoxesOnFineMesh();
55 mpMeshPair->ComputeFineElementsAndWeightsForCoarseNodes(true);
56 assert(mpMeshPair->rGetElementsAndWeights().size()==rMechanicsMesh.GetNumNodes());
57}
58
59template<unsigned DIM>
64
65template <unsigned DIM>
66void VoltageInterpolaterOntoMechanicsMesh<DIM>::InterpolateOnCoarseMesh(std::vector<double>& rValuesOnCoarseMesh, ReplicatableVector& rValuesOnFineMesh)
67{
68 assert(rValuesOnCoarseMesh.size() == mrMechanicsMesh.GetNumNodes());
69 for (unsigned i=0; i<mpMeshPair->rGetElementsAndWeights().size(); i++)
70 {
71 Element<DIM,DIM>& element = *(mrElectricsMesh.GetElement(mpMeshPair->rGetElementsAndWeights()[i].ElementNum));
72
73 double interpolated_value = 0;
74 for (unsigned node_index = 0; node_index<element.GetNumNodes(); node_index++)
75 {
76 unsigned global_node_index = element.GetNodeGlobalIndex(node_index);
77 interpolated_value += rValuesOnFineMesh[global_node_index]*mpMeshPair->rGetElementsAndWeights()[i].Weights(node_index);
78 }
79 rValuesOnCoarseMesh[i] = interpolated_value;
80 }
81
82}
83template<unsigned DIM>
84void VoltageInterpolaterOntoMechanicsMesh<DIM>::OutputToCmgui(std::vector<std::string>& rVariableNames,
85 std::string directory,
86 std::string inputFileNamePrefix)
87{
88 assert(mpMeshPair);
89 mpMeshPair->SetUpBoxesOnFineMesh();
90 mpMeshPair->ComputeFineElementsAndWeightsForCoarseNodes(true);
91 assert(mpMeshPair->rGetElementsAndWeights().size()==mrMechanicsMesh.GetNumNodes());
92
93 // Read the data from the HDF5 file
94 Hdf5DataReader reader(directory,inputFileNamePrefix);
95
96 unsigned num_timesteps = reader.GetUnlimitedDimensionValues().size();
97
98 // create and setup a writer
99 Hdf5DataWriter* p_writer = new Hdf5DataWriter(*mrMechanicsMesh.GetDistributedVectorFactory(),
100 directory,
101 "voltage_mechanics_mesh",
102 false, //don't clean
103 false);
104
105 std::vector<int> columns_id;
106 for (unsigned var_index = 0; var_index < rVariableNames.size(); var_index++)
107 {
108 std::string var_name = rVariableNames[var_index];
109 columns_id.push_back( p_writer->DefineVariable(var_name,"mV") );
110 }
111
112 p_writer->DefineUnlimitedDimension("Time","msecs", num_timesteps);
113 p_writer->DefineFixedDimension(mrMechanicsMesh.GetNumNodes() );
114 p_writer->EndDefineMode();
115
116 assert(columns_id.size() == rVariableNames.size());
117
118 // set up a vector to read into
119 DistributedVectorFactory factory(mrElectricsMesh.GetNumNodes());
120 Vec voltage = factory.CreateVec();
121 std::vector<double> interpolated_voltages(mrMechanicsMesh.GetNumNodes());
122 Vec voltage_coarse = NULL;
123
124 for (unsigned time_step=0; time_step<num_timesteps; time_step++)
125 {
126 for (unsigned var_index = 0; var_index < rVariableNames.size(); var_index++)
127 {
128 std::string var_name = rVariableNames[var_index];
129 // read
130 reader.GetVariableOverNodes(voltage, var_name, time_step);
131 ReplicatableVector voltage_repl(voltage);
132
133 InterpolateOnCoarseMesh(interpolated_voltages, voltage_repl);
134
135 if (voltage_coarse != NULL)
136 {
137 PetscTools::Destroy(voltage_coarse);
138 }
139 voltage_coarse = PetscTools::CreateVec(interpolated_voltages);
140 // write
141 p_writer->PutVector(columns_id[var_index], voltage_coarse);
142 }
143 p_writer->PutUnlimitedVariable(time_step);
145 }
146
147 if (voltage_coarse != NULL)
148 {
149 PetscTools::Destroy(voltage);
150 PetscTools::Destroy(voltage_coarse);
151 }
152
153 // delete to flush
154 delete p_writer;
155
156 // Convert the new data to CMGUI format.
157 // alter the directory in HeartConfig as that is where Hdf5ToCmguiConverter decides
158 // where to output
159 std::string config_directory = HeartConfig::Instance()->GetOutputDirectory();
162 "voltage_mechanics_mesh",
163 &mrMechanicsMesh,
164 false);
165 HeartConfig::Instance()->SetOutputDirectory(config_directory);
166}
167
168// Explicit instantiation
unsigned GetNumNodes() const
unsigned GetNodeGlobalIndex(unsigned localIndex) const
virtual unsigned GetNumNodes() const
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)
void OutputToCmgui(std::vector< std::string > &rVariableNames, std::string directory, std::string inputFileNamePrefix)
VoltageInterpolaterOntoMechanicsMesh(TetrahedralMesh< DIM, DIM > &rElectricsMesh, QuadraticMesh< DIM > &rMechanicsMesh)
void InterpolateOnCoarseMesh(std::vector< double > &rValuesOnCoarseMesh, ReplicatableVector &rValuesOnFineMesh)