Chaste  Release::3.4
BidomainTissue.cpp
1 /*
2 
3 Copyright (c) 2005-2016, 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 "BidomainTissue.hpp"
37 
38 #include "DistributedVector.hpp"
39 #include "AxisymmetricConductivityTensors.hpp"
40 #include "OrthotropicConductivityTensors.hpp"
41 #include "ChastePoint.hpp"
42 #include "AbstractChasteRegion.hpp"
43 
44 template <unsigned SPACE_DIM>
47  bool exchangeHalos)
48  : AbstractCardiacTissue<SPACE_DIM>(pCellFactory, exchangeHalos)
49 {
51 }
52 
53 template <unsigned SPACE_DIM>
55  : AbstractCardiacTissue<SPACE_DIM>(pMesh)
56 {
58 }
59 
60 template <unsigned SPACE_DIM>
62 {
63  if (this->mpConfig->IsMeshProvided() && this->mpConfig->GetLoadMesh())
64  {
65  assert(this->mFibreFilePathNoExtension != "");
66 
67  switch (this->mpConfig->GetConductivityMedia())
68  {
69  case cp::media_type::Orthotropic:
70  {
71  mpExtracellularConductivityTensors = new OrthotropicConductivityTensors<SPACE_DIM,SPACE_DIM>;
72  FileFinder ortho_file(this->mFibreFilePathNoExtension + ".ortho", RelativeTo::AbsoluteOrCwd);
73  assert(ortho_file.Exists());
74  mpExtracellularConductivityTensors->SetFibreOrientationFile(ortho_file);
75  break;
76  }
77 
78  case cp::media_type::Axisymmetric:
79  {
80  mpExtracellularConductivityTensors = new AxisymmetricConductivityTensors<SPACE_DIM,SPACE_DIM>;
81  FileFinder axi_file(this->mFibreFilePathNoExtension + ".axi", RelativeTo::AbsoluteOrCwd);
82  assert(axi_file.Exists());
83  mpExtracellularConductivityTensors->SetFibreOrientationFile(axi_file);
84  break;
85  }
86 
87  case cp::media_type::NoFibreOrientation:
88  mpExtracellularConductivityTensors = new OrthotropicConductivityTensors<SPACE_DIM,SPACE_DIM>;
89  break;
90 
91  default :
93  }
94  }
95  else // Slab defined in config file or SetMesh() called; no fibre orientation assumed
96  {
97  mpExtracellularConductivityTensors = new OrthotropicConductivityTensors<SPACE_DIM,SPACE_DIM>;
98  }
99 
100  c_vector<double, SPACE_DIM> extra_conductivities;
101  this->mpConfig->GetExtracellularConductivities(extra_conductivities);
102 
103  // this definition must be here (and not inside the if statement) because SetNonConstantConductivities() will keep
104  // a pointer to it and we don't want it to go out of scope before Init() is called
105  unsigned num_local_elements = this->mpMesh->GetNumLocalElements();
106  std::vector<c_vector<double, SPACE_DIM> > hetero_extra_conductivities;
107 
108  if (this->mpConfig->GetConductivityHeterogeneitiesProvided())
109  {
110  try
111  {
112  assert(hetero_extra_conductivities.size()==0);
113  //initialise with the values of the default conductivity tensor
114  hetero_extra_conductivities.resize(num_local_elements, extra_conductivities);
115  }
116  catch(std::bad_alloc &r_bad_alloc)
117  {
118 #define COVERAGE_IGNORE
119  std::cout << "Failed to allocate std::vector of size " << num_local_elements << std::endl;
121  throw r_bad_alloc;
122 #undef COVERAGE_IGNORE
123  }
125 
126  std::vector<boost::shared_ptr<AbstractChasteRegion<SPACE_DIM> > > conductivities_heterogeneity_areas;
127  std::vector< c_vector<double,3> > intra_h_conductivities;
128  std::vector< c_vector<double,3> > extra_h_conductivities;
129  HeartConfig::Instance()->GetConductivityHeterogeneities(conductivities_heterogeneity_areas,
130  intra_h_conductivities,
131  extra_h_conductivities);
132 
133  unsigned local_element_index = 0;
134 
135  for (typename AbstractTetrahedralMesh<SPACE_DIM,SPACE_DIM>::ElementIterator iter = (this->mpMesh)->GetElementIteratorBegin();
136  iter != (this->mpMesh)->GetElementIteratorEnd();
137  ++iter)
138  {
139  // if element centroid is contained in the region
140  ChastePoint<SPACE_DIM> element_centroid(iter->CalculateCentroid());
141  for (unsigned region_index=0; region_index< conductivities_heterogeneity_areas.size(); region_index++)
142  {
143  // if element centroid is contained in the region
144  if ( conductivities_heterogeneity_areas[region_index]->DoesContain( element_centroid ) )
145  {
146  //We don't use ublas vector assignment here, because we might be getting a subvector of a 3-vector
147  for (unsigned i=0; i<SPACE_DIM; i++)
148  {
149  hetero_extra_conductivities[local_element_index][i] = extra_h_conductivities[region_index][i];
150  }
151  }
152  }
153  local_element_index++;
154  }
155  mpExtracellularConductivityTensors->SetNonConstantConductivities(&hetero_extra_conductivities);
156  }
157  else
158  {
159  mpExtracellularConductivityTensors->SetConstantConductivities(extra_conductivities);
160  }
161 
162  mpExtracellularConductivityTensors->Init(this->mpMesh);
163 }
164 
165 template <unsigned SPACE_DIM>
167 {
168  if (mpExtracellularConductivityTensors)
169  {
170  delete mpExtracellularConductivityTensors;
171  }
172 }
173 
174 
175 template <unsigned SPACE_DIM>
176 const c_matrix<double, SPACE_DIM, SPACE_DIM>& BidomainTissue<SPACE_DIM>::rGetExtracellularConductivityTensor(unsigned elementIndex)
177 {
178  assert(mpExtracellularConductivityTensors);
179  if(this->mpConductivityModifier==NULL)
180  {
181  return (*mpExtracellularConductivityTensors)[elementIndex];
182  }
183  else
184  {
185  return this->mpConductivityModifier->rGetModifiedConductivityTensor(elementIndex, (*mpExtracellularConductivityTensors)[elementIndex], 1u);
186  }
187 }
188 
189 
191 // Explicit instantiation
193 
194 template class BidomainTissue<1>;
195 template class BidomainTissue<2>;
196 template class BidomainTissue<3>;
197 
198 
199 // Serialization for Boost >= 1.36
BidomainTissue(AbstractCardiacCellFactory< SPACE_DIM > *pCellFactory, bool exchangeHalos=false)
void CreateExtracellularConductivityTensors()
#define NEVER_REACHED
Definition: Exception.hpp:206
void GetConductivityHeterogeneities(std::vector< boost::shared_ptr< AbstractChasteRegion< DIM > > > &conductivitiesHeterogeneityAreas, std::vector< c_vector< double, 3 > > &intraConductivities, std::vector< c_vector< double, 3 > > &extraConductivities) const
#define EXPORT_TEMPLATE_CLASS_SAME_DIMS(CLASS)
static void ReplicateException(bool flag)
Definition: PetscTools.cpp:198
bool Exists() const
Definition: FileFinder.cpp:180
static HeartConfig * Instance()
const c_matrix< double, SPACE_DIM, SPACE_DIM > & rGetExtracellularConductivityTensor(unsigned elementIndex)