Chaste Commit::ca8ccdedf819b6e02855bc0e8e6f50bdecbc5208
PseudoEcgCalculator.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 "PseudoEcgCalculator.hpp"
37#include "HeartRegionCodes.hpp"
38#include "HeartConfig.hpp"
39#include "PetscTools.hpp"
40#include "Version.hpp"
41#include <iostream>
42
43template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
45 c_vector<double,PROBLEM_DIM>& rU,
46 c_matrix<double,PROBLEM_DIM,SPACE_DIM>& rGradU)
47{
48 c_vector<double,SPACE_DIM> r_vector = rX.rGetLocation()- mProbeElectrode.rGetLocation();
49 double norm_r = norm_2(r_vector);
50 if (norm_r <= DBL_EPSILON)
51 {
52 EXCEPTION("Probe is on a mesh Gauss point.");
53 }
54 c_vector<double,SPACE_DIM> grad_one_over_r = - (r_vector)*SmallPow( (1/norm_r) , 3);
55 matrix_row<c_matrix<double, PROBLEM_DIM, SPACE_DIM> > grad_u_row(rGradU, 0);
56 double integrand = inner_prod(grad_u_row, grad_one_over_r);
57
58 return -mDiffusionCoefficient*integrand;
59}
60
61template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
63 const ChastePoint<SPACE_DIM>& rProbeElectrode,
64 const FileFinder& rDirectory,
65 const std::string& rHdf5FileName,
66 const std::string& rVariableName,
67 unsigned timestepStride)
68 : mrMesh(rMesh),
69 mProbeElectrode(rProbeElectrode),
70 mVariableName(rVariableName),
71 mTimestepStride(timestepStride)
72{
73 mpDataReader = new Hdf5DataReader(rDirectory, rHdf5FileName);
77 //check that the hdf file was generated by simulations from the same mesh
78 assert(mNumberOfNodes == mrMesh.GetNumNodes());
79}
80
81template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
83{
84 bool result = false; // Don't skip usually
85 if (mVariableName=="V")
86 {
87 // If we are integrating voltage and this element is in the bath then skip it.
89 }
90 return result;
91}
92
93template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
98
99template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
101{
102 assert(diffusionCoefficient>=0);
103 mDiffusionCoefficient = diffusionCoefficient;
104}
105
106template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
108{
109 Vec solution_at_one_time_step = PetscTools::CreateVec(mNumberOfNodes);
110 mpDataReader->GetVariableOverNodes(solution_at_one_time_step, mVariableName , timeStep);
111
112 double pseudo_ecg_at_one_timestep;
113 try
114 {
115 pseudo_ecg_at_one_timestep = this->Calculate(mrMesh, solution_at_one_time_step);
116 }
117 catch (Exception &e)
118 {
119 PetscTools::Destroy(solution_at_one_time_step);
120 throw e;
121 }
122 PetscTools::Destroy(solution_at_one_time_step);
123 return pseudo_ecg_at_one_timestep;
124}
125
126template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
128{
129 // Cache the time values so that we can plot a decent x-axis
130 std::vector<double> time_points = mpDataReader->GetUnlimitedDimensionValues();
131
132 std::stringstream stream;
133 stream << "PseudoEcgFromElectrodeAt" << "_" << mProbeElectrode.GetWithDefault(0)
134 << "_" << mProbeElectrode.GetWithDefault(1)
135 << "_" << mProbeElectrode.GetWithDefault(2) << ".dat";
136 OutputFileHandler output_file_handler(HeartConfig::Instance()->GetOutputDirectory() + "/output", false);
137 out_stream p_file=out_stream(NULL);
139 {
140 p_file = output_file_handler.OpenOutputFile(stream.str());
141 *p_file << "#Time(ms)\tPseudo-ECG\n";
142 }
143 for (unsigned time_step = 0; time_step < mNumTimeSteps; time_step+=mTimestepStride)
144 {
145 double pseudo_ecg_at_one_timestep = ComputePseudoEcgAtOneTimeStep(time_step);
147 {
148 *p_file << time_points[time_step] << "\t" <<pseudo_ecg_at_one_timestep << "\n";
149 }
150 }
152 {
153 //write provenance info
154 std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
155 *p_file << comment;
156 p_file->close();
157 }
158}
159
160// Explicit instantiation
161template class PseudoEcgCalculator<1,1,1>;
162template class PseudoEcgCalculator<1,2,1>;
163template class PseudoEcgCalculator<1,3,1>;
164template class PseudoEcgCalculator<2,2,1>;
165//template class PseudoEcgCalculator<2,3,1>;
166template class PseudoEcgCalculator<3,3,1>;
#define EXCEPTION(message)
unsigned GetUnsignedAttribute()
static std::string GetProvenanceString()
c_vector< double, DIM > & rGetLocation()
unsigned GetNumberOfRows()
std::vector< double > GetVariableOverTime(const std::string &rVariableName, unsigned nodeIndex)
static HeartConfig * Instance()
static bool IsRegionBath(HeartRegionType regionId)
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
static void Destroy(Vec &rVec)
static bool AmMaster()
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
void SetDiffusionCoefficient(double diffusionCoefficient)
bool ShouldSkipThisElement(Element< ELEMENT_DIM, SPACE_DIM > &rElement)
PseudoEcgCalculator(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, const ChastePoint< SPACE_DIM > &rProbeElectrode, const FileFinder &rDirectory, const std::string &rHdf5FileName, const std::string &rVariableName="V", unsigned timestepStride=1)
double ComputePseudoEcgAtOneTimeStep(unsigned timeStep)
Hdf5DataReader * mpDataReader
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > & mrMesh