Chaste  Release::2017.1
OrthotropicConductivityTensors.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 "OrthotropicConductivityTensors.hpp"
37 #include "Exception.hpp"
38 
39 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
41 {
42  this->mpMesh = pMesh;
43 
44  if (!this->mUseNonConstantConductivities && !this->mUseFibreOrientation)
45  {
46  // Constant tensor for every element
47  c_matrix<double, SPACE_DIM, SPACE_DIM> conductivity_matrix(zero_matrix<double>(SPACE_DIM,SPACE_DIM));
48 
49  for (unsigned dim=0; dim<SPACE_DIM; dim++)
50  {
51  assert(this->mConstantConductivities(dim) != DBL_MAX);
52  conductivity_matrix(dim,dim) = this->mConstantConductivities(dim);
53  }
54 
55  this->mTensors.push_back(conductivity_matrix);
56  }
57  else
58  {
59  c_matrix<double,SPACE_DIM,SPACE_DIM> orientation_matrix((identity_matrix<double>(SPACE_DIM)));
60 
61  if (this->mUseFibreOrientation)
62  {
63  // open file
64  this->mFileReader.reset(new FibreReader<SPACE_DIM>(this->mFibreOrientationFile, ORTHO));
65  if (this->mFileReader->GetNumLinesOfData() != this->mpMesh->GetNumElements())
66  {
67  EXCEPTION("The size of the fibre file does not match the number of elements in the mesh");
68  }
69  }
70 
71  if (this->mUseNonConstantConductivities)
72  {
73  if (this->mpNonConstantConductivities->size() != this->mpMesh->GetNumLocalElements())
74  {
75  EXCEPTION("The size of the conductivities vector does not match the number of elements in the mesh");
76  }
77  }
78 
79  // reserve() allocates all the memory at once, more efficient than relying
80  // on the automatic reallocation scheme.
81  this->mTensors.reserve(this->mpMesh->GetNumLocalElements());
82 
83  c_matrix<double, SPACE_DIM, SPACE_DIM> conductivity_matrix(zero_matrix<double>(SPACE_DIM,SPACE_DIM));
84 
85  unsigned local_element_index = 0;
86 
87  for (typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ElementIterator it = this->mpMesh->GetElementIteratorBegin();
88  it != this->mpMesh->GetElementIteratorEnd();
89  ++it)
90  {
91  /*
92  * For every element of the mesh we compute its tensor like (from
93  * "Laminar Arrangement of Ventricular Myocytes Influences Electrical
94  * Behavior of the Heart, DA Hooks et al. 2007):
95  *
96  *
97  * [g_f 0 0 ] [a_f']
98  * tensor = [a_f a_l a_n] [ 0 g_l 0 ] [a_l']
99  * [ 0 0 g_n] [a_n']
100  *
101  * [x_i]
102  * where a_i = [y_i], i={f,l,n} are read from the fibre orientation file and
103  * [z_i]
104  *
105  * g_f = fibre/longitudinal conductivity (constant or element specific)
106  * g_l = laminar/transverse conductivity (constant or element specific)
107  * g_n = normal conductivity (constant or element specific)
108  *
109  */
110  if (this->mUseNonConstantConductivities)
111  {
112  for (unsigned dim=0; dim<SPACE_DIM; dim++)
113  {
114  conductivity_matrix(dim,dim) = (*this->mpNonConstantConductivities)[local_element_index][dim];
115  }
116  }
117  else
118  {
119  for (unsigned dim=0; dim<SPACE_DIM; dim++)
120  {
121  assert(this->mConstantConductivities(dim) != DBL_MAX);
122  conductivity_matrix(dim,dim) = this->mConstantConductivities(dim);
123  }
124  }
125 
126  if (this->mUseFibreOrientation)
127  {
128  unsigned current_fibre_global_index = it->GetIndex();
129  this->mFileReader->GetFibreSheetAndNormalMatrix(current_fibre_global_index, orientation_matrix);
130  }
131 
132  c_matrix<double,SPACE_DIM,SPACE_DIM> temp;
133  noalias(temp) = prod(orientation_matrix, conductivity_matrix);
134  this->mTensors.push_back( prod(temp, trans(orientation_matrix) ) );
135 
136  local_element_index++;
137  }
138  assert(this->mTensors.size() == this->mpMesh->GetNumLocalElements());
139  assert(this->mTensors.size() == local_element_index);
140 
141  if (this->mUseFibreOrientation)
142  {
143  // Close fibre file
144  this->mFileReader.reset();
145  }
146  }
147 
148  this->mInitialised = true;
149 }
150 
151 // Explicit instantiation
void Init(AbstractTetrahedralMesh< ELEMENT_DIM, SPACE_DIM > *pMesh)
#define EXCEPTION(message)
Definition: Exception.hpp:143