Chaste  Release::2017.1
PseudoEcgCalculator.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 "PseudoEcgCalculator.hpp"
37 #include "HeartRegionCodes.hpp"
38 #include "HeartConfig.hpp"
39 #include "PetscTools.hpp"
40 #include "Version.hpp"
41 #include <iostream>
42 
43 template<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 
61 template<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 
81 template<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 
93 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
95 {
96  delete mpDataReader;
97 }
98 
99 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
101 {
102  assert(diffusionCoefficient>=0);
103  mDiffusionCoefficient = diffusionCoefficient;
104 }
105 
106 template<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 
126 template<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);
138  if (PetscTools::AmMaster())
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);
146  if (PetscTools::AmMaster())
147  {
148  *p_file << time_points[time_step] << "\t" <<pseudo_ecg_at_one_timestep << "\n";
149  }
150  }
151  if (PetscTools::AmMaster())
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
161 template class PseudoEcgCalculator<1,1,1>;
162 template class PseudoEcgCalculator<1,2,1>;
163 template class PseudoEcgCalculator<1,3,1>;
164 template class PseudoEcgCalculator<2,2,1>;
165 //template class PseudoEcgCalculator<2,3,1>;
166 template class PseudoEcgCalculator<3,3,1>;
void SetDiffusionCoefficient(double diffusionCoefficient)
static bool IsRegionBath(HeartRegionType regionId)
unsigned GetNumberOfRows()
double GetIntegrand(ChastePoint< SPACE_DIM > &rX, c_vector< double, PROBLEM_DIM > &rU, c_matrix< double, PROBLEM_DIM, SPACE_DIM > &rGradU)
c_vector< double, DIM > & rGetLocation()
Definition: ChastePoint.cpp:76
double SmallPow(double x, unsigned exponent)
double GetWithDefault(unsigned i, double def=0.0) const
Definition: ChastePoint.cpp:95
#define EXCEPTION(message)
Definition: Exception.hpp:143
AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > & mrMesh
static bool AmMaster()
Definition: PetscTools.cpp:120
static Vec CreateVec(int size, int localSize=PETSC_DECIDE, bool ignoreOffProcEntries=true)
Definition: PetscTools.cpp:214
bool ShouldSkipThisElement(Element< ELEMENT_DIM, SPACE_DIM > &rElement)
void GetVariableOverNodes(Vec data, const std::string &rVariableName, unsigned timestep=0)
out_stream OpenOutputFile(const std::string &rFileName, std::ios_base::openmode mode=std::ios::out|std::ios::trunc) const
double Calculate(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > &rMesh, Vec solution)
std::vector< double > GetUnlimitedDimensionValues()
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)
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
std::vector< double > GetVariableOverTime(const std::string &rVariableName, unsigned nodeIndex)
ChastePoint< SPACE_DIM > mProbeElectrode
unsigned GetUnsignedAttribute()
static std::string GetProvenanceString()
Hdf5DataReader * mpDataReader
static HeartConfig * Instance()
double ComputePseudoEcgAtOneTimeStep(unsigned timeStep)