AxisymmetricConductivityTensors.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 <vector>
00030 #include "UblasIncludes.hpp"
00031 #include "AxisymmetricConductivityTensors.hpp"
00032 #include "Exception.hpp"
00033 
00034 
00035 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00036 AxisymmetricConductivityTensors<ELEMENT_DIM, SPACE_DIM>::AxisymmetricConductivityTensors()
00037 {
00038     if (SPACE_DIM != 3)
00039     {
00040         EXCEPTION("Axisymmetric anisotropic conductivity only makes sense in 3D");
00041     }
00042 }
00043 
00044 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00045 void AxisymmetricConductivityTensors<ELEMENT_DIM, SPACE_DIM>::SetConstantConductivities(c_vector<double, 3> constantConductivities)
00046 {
00047     //assert(SPACE_DIM == 3);//Otherwise constructor would have thrown
00048     if (constantConductivities[1] != constantConductivities[2])
00049     {
00050         EXCEPTION("Axisymmetric media defined: transversal and normal conductivities should have the same value");
00051     }
00052 
00053     this->mUseNonConstantConductivities = false;
00054     this->mConstantConductivities = constantConductivities;
00055 }
00056 
00057 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00058 void AxisymmetricConductivityTensors<ELEMENT_DIM, SPACE_DIM>::Init(AbstractTetrahedralMesh<ELEMENT_DIM, SPACE_DIM> *pMesh) throw (Exception)
00059 {
00060     this->mpMesh = pMesh;
00061 
00062     if (!this->mUseNonConstantConductivities && !this->mUseFibreOrientation)
00063     {
00064         // Constant tensor for every element
00065         c_matrix<double, SPACE_DIM, SPACE_DIM> conductivity_matrix(zero_matrix<double>(SPACE_DIM,SPACE_DIM));
00066 
00067         for (unsigned dim=0; dim<SPACE_DIM; dim++)
00068         {
00069             assert(this->mConstantConductivities(dim) != DBL_MAX);
00070             conductivity_matrix(dim,dim) = this->mConstantConductivities(dim);
00071         }
00072         this->mTensors.push_back(conductivity_matrix);
00073     }
00074     else
00075     {
00076         c_vector<double,SPACE_DIM> fibre_vector((zero_vector<double>(SPACE_DIM)));
00077         fibre_vector[0]=1.0;
00078 
00079         if (this->mUseFibreOrientation)
00080         {
00081             // open file
00082             this->mFileReader.reset(new FibreReader<SPACE_DIM>(this->mFibreOrientationFile, AXISYM));
00083             if(this->mFileReader->GetNumLinesOfData() != this->mpMesh->GetNumElements())
00084             {
00085                 EXCEPTION("The size of the fibre file does not match the number of elements in the mesh");
00086             }
00087         }
00088 
00089         if (this->mUseNonConstantConductivities)
00090         {
00091             if(this->mpNonConstantConductivities->size() != this->mpMesh->GetNumLocalElements())
00092             {
00093                 EXCEPTION("The size of the conductivities vector does not match the number of elements in the mesh");
00094             }
00095         }
00096 
00097         // reserve() allocates all the memory at once, more efficient than relying
00098         // on the automatic reallocation scheme.
00099         this->mTensors.reserve(this->mpMesh->GetNumLocalElements());
00100 
00101         c_matrix<double, SPACE_DIM, SPACE_DIM> conductivity_matrix(zero_matrix<double>(SPACE_DIM,SPACE_DIM));
00102         
00103         unsigned local_element_index = 0;
00104 
00105         int previous_global_index=-1;
00106         
00107         for (typename AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>::ElementIterator it = this->mpMesh->GetElementIteratorBegin();
00108              it != this->mpMesh->GetElementIteratorEnd();
00109              ++it)
00110         {            
00111             if (this->mUseFibreOrientation)
00112             {
00113                 int current_fibre_global_index = (int) it->GetIndex();
00114 
00115                 // Assumption about ElementIterator returning elements in ascending order is wrong
00116                 // if this fails
00117                 assert(current_fibre_global_index > previous_global_index);
00118                 
00119                 for (int fibre_index=previous_global_index; fibre_index<current_fibre_global_index-1; fibre_index++)
00120                 {
00121                     this->mFileReader->GetNextFibreVector(fibre_vector);
00122                 }
00123                 
00124                 previous_global_index = current_fibre_global_index;
00125             }
00126             
00127             
00128             /*
00129              *  For every element of the mesh we compute its tensor like (from
00130              * "Laminar Arrangement of VentricularMyocites Influences Electrical
00131              * Behavior of the Heart", Darren et al. 2007):
00132              *
00133              *                         [g_f  0   0 ] [a_f']
00134              *  tensor = [a_f a_l a_n] [ 0  g_l  0 ] [a_l']
00135              *                         [ 0   0  g_n] [a_n']
00136              *
00137              *              [x_i]
00138              *  where a_i = [y_i], i={f,l,n} are read from the fibre orientation file and
00139              *              [z_i]
00140              *
00141              *  g_f = fibre/longitudinal conductivity (constant or element specific)
00142              *  g_l = laminar/transverse conductivity (constant or element specific)
00143              *  g_n = normal conductivity (constant or element specific)
00144              *
00145              *
00146              *  For axisymmetric anisotropic media (g_l = g_n) we can simplify previous expression to
00147              *
00148              *
00149              *  tensor = g_l * I + (g_f - g_l) * a_f * a_f'
00150              *
00151              */
00152 
00153             if (this->mUseNonConstantConductivities)
00154             {
00155                 for (unsigned dim=0; dim<SPACE_DIM; dim++)
00156                 {
00157                     conductivity_matrix(dim,dim) = (*this->mpNonConstantConductivities)[local_element_index][dim];
00158                 }
00159             }
00160             else
00161             {
00162                 for (unsigned dim=0; dim<SPACE_DIM; dim++)
00163                 {
00164                     assert(this->mConstantConductivities(dim) != DBL_MAX);
00165                     conductivity_matrix(dim,dim) = this->mConstantConductivities(dim);
00166                 }
00167             }
00168 
00169 
00170             if (this->mUseFibreOrientation)
00171             {
00172                 this->mFileReader->GetNextFibreVector(fibre_vector);
00173             }
00174 
00175             this->mTensors.push_back( conductivity_matrix(1,1) * identity_matrix<double>(SPACE_DIM) +
00176                                       (conductivity_matrix(0,0) - conductivity_matrix(1,1)) * outer_prod(fibre_vector,fibre_vector));
00177                                       
00178             local_element_index++;
00179         }
00180 
00181         assert(this->mTensors.size() == this->mpMesh->GetNumLocalElements());
00182         assert(this->mTensors.size() == local_element_index);
00183 
00184         if (this->mUseFibreOrientation)
00185         {
00186             // close fibre file
00187             this->mFileReader.reset();
00188         }
00189     }
00190 
00191     this->mInitialised = true;
00192 }
00193 
00194 
00195 
00197 // Explicit instantiation
00199 
00200 // only makes sense for 3d elements in 3d, but we need the other to compile
00201 // AbstractCardiacTissue and BidomainTissue.
00202 template class AxisymmetricConductivityTensors<1,1>;
00203 template class AxisymmetricConductivityTensors<1,2>;
00204 template class AxisymmetricConductivityTensors<1,3>;
00205 template class AxisymmetricConductivityTensors<2,2>;
00206 template class AxisymmetricConductivityTensors<2,3>;
00207 template class AxisymmetricConductivityTensors<3,3>;

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