PseudoEcgCalculator.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2011
00004 
00005 University of Oxford means the Chancellor, Masters and Scholars of the
00006 University of Oxford, having an administrative office at Wellington
00007 Square, Oxford OX1 2JD, UK.
00008 
00009 This file is part of Chaste.
00010 
00011 Chaste is free software: you can redistribute it and/or modify it
00012 under the terms of the GNU Lesser General Public License as published
00013 by the Free Software Foundation, either version 2.1 of the License, or
00014 (at your option) any later version.
00015 
00016 Chaste is distributed in the hope that it will be useful, but WITHOUT
00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00018 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
00019 License for more details. The offer of Chaste under the terms of the
00020 License is subject to the License being interpreted in accordance with
00021 English Law and subject to any action against the University of Oxford
00022 being under the jurisdiction of the English Courts.
00023 
00024 You should have received a copy of the GNU Lesser General Public License
00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>.
00026 
00027 */
00028 
00029 #include "PseudoEcgCalculator.hpp"
00030 #include "HeartConfig.hpp"
00031 #include "PetscTools.hpp"
00032 #include "Version.hpp"
00033 #include <iostream>
00034 
00035 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
00036 double PseudoEcgCalculator<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM> ::GetIntegrand(ChastePoint<SPACE_DIM>& rX,
00037                                 c_vector<double,PROBLEM_DIM>& rU,
00038                                 c_matrix<double,PROBLEM_DIM,SPACE_DIM>& rGradU)
00039 {
00040     c_vector<double,SPACE_DIM> r_vector = rX.rGetLocation()- mProbeElectrode.rGetLocation();
00041     double norm_r = norm_2(r_vector);
00042     if (norm_r <= DBL_EPSILON)
00043     {
00044         EXCEPTION("Probe is on a mesh Gauss point.");
00045     }
00046     c_vector<double,SPACE_DIM> grad_one_over_r = - (r_vector)*SmallPow( (1/norm_r) , 3);
00047     matrix_row<c_matrix<double, PROBLEM_DIM, SPACE_DIM> > grad_u_row(rGradU, 0);
00048     double integrand = inner_prod(grad_u_row, grad_one_over_r);
00049 
00050     return -mDiffusionCoefficient*integrand;
00051 }
00052 
00053 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
00054 PseudoEcgCalculator<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM>::PseudoEcgCalculator(AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>& rMesh,
00055                                                                               const ChastePoint<SPACE_DIM>& rProbeElectrode,
00056                                                                               std::string directory,
00057                                                                               std::string hdf5File,
00058                                                                               std::string variableName,
00059                                                                               bool makeAbsolute)
00060                                       : mrMesh(rMesh),
00061                                         mProbeElectrode(rProbeElectrode),
00062                                         mVariableName(variableName)
00063 
00064 {
00065     
00066     mpDataReader = new Hdf5DataReader(directory, hdf5File, makeAbsolute);
00067     mNumberOfNodes = mpDataReader->GetNumberOfRows();
00068     mNumTimeSteps = mpDataReader->GetVariableOverTime(mVariableName, 0u).size();
00069     mDiffusionCoefficient = 1.0;
00070     //check that the hdf file was generated by simulations from the same mesh
00071     assert(mNumberOfNodes == mrMesh.GetNumNodes());
00072 }
00073 
00074 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
00075 PseudoEcgCalculator<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM>::~PseudoEcgCalculator()
00076 {
00077     delete mpDataReader;
00078 }
00079 
00080 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
00081 void PseudoEcgCalculator<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM>::SetDiffusionCoefficient(double diffusionCoefficient)
00082 {
00083     assert(diffusionCoefficient>=0);
00084     mDiffusionCoefficient = diffusionCoefficient;
00085 }
00086 
00087 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
00088 double PseudoEcgCalculator<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM>::ComputePseudoEcgAtOneTimeStep (unsigned timeStep)
00089 {
00090     Vec solution_at_one_time_step = PetscTools::CreateVec(mNumberOfNodes);
00091     mpDataReader->GetVariableOverNodes(solution_at_one_time_step, mVariableName , timeStep);
00092 
00093     double pseudo_ecg_at_one_timestep;
00094     try
00095     {
00096         pseudo_ecg_at_one_timestep = Calculate(mrMesh, solution_at_one_time_step);
00097     }
00098     catch (Exception &e)
00099     {
00100         VecDestroy(solution_at_one_time_step);
00101         throw e;
00102     }
00103     VecDestroy(solution_at_one_time_step);
00104     return pseudo_ecg_at_one_timestep;
00105 }
00106 
00107 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM>
00108 void PseudoEcgCalculator<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM>::WritePseudoEcg ()
00109 {
00110     std::stringstream stream;
00111     stream << "PseudoEcgFromElectrodeAt" << "_" << mProbeElectrode.GetWithDefault(0)
00112                           << "_" << mProbeElectrode.GetWithDefault(1)
00113                           << "_" << mProbeElectrode.GetWithDefault(2) << ".dat";
00114     OutputFileHandler output_file_handler(HeartConfig::Instance()->GetOutputDirectory() + "/output", false);
00115     out_stream p_file=out_stream(NULL);
00116     if (PetscTools::AmMaster())
00117     {
00118         p_file = output_file_handler.OpenOutputFile(stream.str());
00119     }
00120     for (unsigned i = 0; i < mNumTimeSteps; i++)
00121     {
00122         double pseudo_ecg_at_one_timestep = ComputePseudoEcgAtOneTimeStep(i);
00123         if (PetscTools::AmMaster())
00124         {
00125             *p_file << pseudo_ecg_at_one_timestep << "\n";
00126         }
00127     }
00128     if (PetscTools::AmMaster())
00129     {
00130         //write provenance info
00131         std::string comment = "# " + ChasteBuildInfo::GetProvenanceString();
00132         *p_file << comment;
00133         p_file->close();
00134     }
00135 }
00137 // Explicit instantiation
00139 
00140 template class PseudoEcgCalculator<1,1,1>;
00141 template class PseudoEcgCalculator<1,2,1>;
00142 template class PseudoEcgCalculator<1,3,1>;
00143 template class PseudoEcgCalculator<2,2,1>;
00144 //template class PseudoEcgCalculator<2,3,1>;
00145 template class PseudoEcgCalculator<3,3,1>;
00146 

Generated on Tue May 31 14:31:41 2011 for Chaste by  doxygen 1.5.5