Chaste Release::3.1
BidomainTissue.cpp
00001 /*
00002 
00003 Copyright (c) 2005-2012, University of Oxford.
00004 All rights reserved.
00005 
00006 University of Oxford means the Chancellor, Masters and Scholars of the
00007 University of Oxford, having an administrative office at Wellington
00008 Square, Oxford OX1 2JD, UK.
00009 
00010 This file is part of Chaste.
00011 
00012 Redistribution and use in source and binary forms, with or without
00013 modification, are permitted provided that the following conditions are met:
00014  * Redistributions of source code must retain the above copyright notice,
00015    this list of conditions and the following disclaimer.
00016  * Redistributions in binary form must reproduce the above copyright notice,
00017    this list of conditions and the following disclaimer in the documentation
00018    and/or other materials provided with the distribution.
00019  * Neither the name of the University of Oxford nor the names of its
00020    contributors may be used to endorse or promote products derived from this
00021    software without specific prior written permission.
00022 
00023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00024 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00025 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00026 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00027 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00028 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00029 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00030 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00031 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
00032 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 
00034 */
00035 
00036 #include "BidomainTissue.hpp"
00037 
00038 #include "DistributedVector.hpp"
00039 #include "AxisymmetricConductivityTensors.hpp"
00040 #include "OrthotropicConductivityTensors.hpp"
00041 #include "ChastePoint.hpp"
00042 #include "AbstractChasteRegion.hpp"
00043 
00044 template <unsigned SPACE_DIM>
00045 BidomainTissue<SPACE_DIM>::BidomainTissue(
00046             AbstractCardiacCellFactory<SPACE_DIM>* pCellFactory,
00047             bool exchangeHalos)
00048     : AbstractCardiacTissue<SPACE_DIM>(pCellFactory, exchangeHalos)
00049 {
00050     CreateExtracellularConductivityTensors();
00051 }
00052 
00053 template <unsigned SPACE_DIM>
00054 BidomainTissue<SPACE_DIM>::BidomainTissue(AbstractTetrahedralMesh<SPACE_DIM,SPACE_DIM>* pMesh)
00055         :  AbstractCardiacTissue<SPACE_DIM>(pMesh)
00056 {
00057     CreateExtracellularConductivityTensors();
00058 }
00059 
00060 template <unsigned SPACE_DIM>
00061 void BidomainTissue<SPACE_DIM>::CreateExtracellularConductivityTensors()
00062 {
00063     if (this->mpConfig->IsMeshProvided() && this->mpConfig->GetLoadMesh())
00064     {
00065         assert(this->mFibreFilePathNoExtension != "");
00066 
00067         switch (this->mpConfig->GetConductivityMedia())
00068         {
00069             case cp::media_type::Orthotropic:
00070             {
00071                 mpExtracellularConductivityTensors =  new OrthotropicConductivityTensors<SPACE_DIM,SPACE_DIM>;
00072                 FileFinder ortho_file(this->mFibreFilePathNoExtension + ".ortho", RelativeTo::AbsoluteOrCwd);
00073                 assert(ortho_file.Exists());
00074                 mpExtracellularConductivityTensors->SetFibreOrientationFile(ortho_file);
00075                 break;
00076             }
00077 
00078             case cp::media_type::Axisymmetric:
00079             {
00080                 mpExtracellularConductivityTensors =  new AxisymmetricConductivityTensors<SPACE_DIM,SPACE_DIM>;
00081                 FileFinder axi_file(this->mFibreFilePathNoExtension + ".axi", RelativeTo::AbsoluteOrCwd);
00082                 assert(axi_file.Exists());
00083                 mpExtracellularConductivityTensors->SetFibreOrientationFile(axi_file);
00084                 break;
00085             }
00086 
00087             case cp::media_type::NoFibreOrientation:
00088                 mpExtracellularConductivityTensors =  new OrthotropicConductivityTensors<SPACE_DIM,SPACE_DIM>;
00089                 break;
00090 
00091             default :
00092                 NEVER_REACHED;
00093         }
00094     }
00095     else // Slab defined in config file or SetMesh() called; no fibre orientation assumed
00096     {
00097         mpExtracellularConductivityTensors =  new OrthotropicConductivityTensors<SPACE_DIM,SPACE_DIM>;
00098     }
00099 
00100     c_vector<double, SPACE_DIM> extra_conductivities;
00101     this->mpConfig->GetExtracellularConductivities(extra_conductivities);
00102 
00103     // this definition must be here (and not inside the if statement) because SetNonConstantConductivities() will keep
00104     // a pointer to it and we don't want it to go out of scope before Init() is called
00105     unsigned num_local_elements = this->mpMesh->GetNumLocalElements();
00106     std::vector<c_vector<double, SPACE_DIM> > hetero_extra_conductivities;
00107 
00108     if (this->mpConfig->GetConductivityHeterogeneitiesProvided())
00109     {
00110         try
00111         {
00112             assert(hetero_extra_conductivities.size()==0);
00113             //initialise with the values of the default conductivity tensor
00114             hetero_extra_conductivities.resize(num_local_elements, extra_conductivities);
00115         }
00116         catch(std::bad_alloc &r_bad_alloc)
00117         {
00118 #define COVERAGE_IGNORE
00119             std::cout << "Failed to allocate std::vector of size " << num_local_elements << std::endl;
00120             PetscTools::ReplicateException(true);
00121             throw r_bad_alloc;
00122 #undef COVERAGE_IGNORE
00123         }
00124         PetscTools::ReplicateException(false);
00125 
00126         std::vector<boost::shared_ptr<AbstractChasteRegion<SPACE_DIM> > > conductivities_heterogeneity_areas;
00127         std::vector< c_vector<double,3> > intra_h_conductivities;
00128         std::vector< c_vector<double,3> > extra_h_conductivities;
00129         HeartConfig::Instance()->GetConductivityHeterogeneities(conductivities_heterogeneity_areas,
00130                                                                 intra_h_conductivities,
00131                                                                 extra_h_conductivities);
00132 
00133         unsigned local_element_index = 0;
00134 
00135         for (typename AbstractTetrahedralMesh<SPACE_DIM,SPACE_DIM>::ElementIterator iter = (this->mpMesh)->GetElementIteratorBegin();
00136              iter != (this->mpMesh)->GetElementIteratorEnd();
00137              ++iter)
00138         {
00139             // if element centroid is contained in the region
00140             ChastePoint<SPACE_DIM> element_centroid(iter->CalculateCentroid());
00141             for (unsigned region_index=0; region_index< conductivities_heterogeneity_areas.size(); region_index++)
00142             {
00143                 // if element centroid is contained in the region
00144               if ( conductivities_heterogeneity_areas[region_index]->DoesContain( element_centroid ) )
00145                 {
00146                     //We don't use ublas vector assignment here, because we might be getting a subvector of a 3-vector
00147                     for (unsigned i=0; i<SPACE_DIM; i++)
00148                     {
00149                         hetero_extra_conductivities[local_element_index][i] = extra_h_conductivities[region_index][i];
00150                     }
00151                 }
00152             }
00153             local_element_index++;
00154         }
00155         mpExtracellularConductivityTensors->SetNonConstantConductivities(&hetero_extra_conductivities);
00156     }
00157     else
00158     {
00159         mpExtracellularConductivityTensors->SetConstantConductivities(extra_conductivities);
00160     }
00161 
00162     mpExtracellularConductivityTensors->Init(this->mpMesh);
00163 }
00164 
00165 template <unsigned SPACE_DIM>
00166 BidomainTissue<SPACE_DIM>::~BidomainTissue()
00167 {
00168     if (mpExtracellularConductivityTensors)
00169     {
00170         delete mpExtracellularConductivityTensors;
00171     }
00172 }
00173 
00174 
00175 template <unsigned SPACE_DIM>
00176 const c_matrix<double, SPACE_DIM, SPACE_DIM>& BidomainTissue<SPACE_DIM>::rGetExtracellularConductivityTensor(unsigned elementIndex)
00177 {
00178     assert(mpExtracellularConductivityTensors);
00179     if(this->mpConductivityModifier==NULL)
00180     {
00181         return (*mpExtracellularConductivityTensors)[elementIndex];
00182     }
00183     else
00184     {
00185         return this->mpConductivityModifier->rGetModifiedConductivityTensor(elementIndex, (*mpExtracellularConductivityTensors)[elementIndex]);
00186     }
00187 }
00188 
00189 
00191 // Explicit instantiation
00193 
00194 template class BidomainTissue<1>;
00195 template class BidomainTissue<2>;
00196 template class BidomainTissue<3>;
00197 
00198 
00199 // Serialization for Boost >= 1.36
00200 #include "SerializationExportWrapperForCpp.hpp"
00201 EXPORT_TEMPLATE_CLASS_SAME_DIMS(BidomainTissue)