MonodomainMatrixBasedAssembler.cpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2009
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 "MonodomainMatrixBasedAssembler.hpp"
00030 
00031 
00033 //
00034 //
00035 //
00036 //
00037 //   NOTE: The main class in this file, MonodomainMatrixBasedAssembler, is defined at the 
00038 //   bottom, after MonodomainRhsMatrixAssembler
00039 //
00040 //
00041 //
00042 //
00043 //
00045 
00046 
00047 
00049 // MonodomainRhsMatrixAssembler
00051 template<unsigned ELEM_DIM, unsigned SPACE_DIM>
00052 c_matrix<double,1*(ELEM_DIM+1),1*(ELEM_DIM+1)> MonodomainRhsMatrixAssembler<ELEM_DIM,SPACE_DIM>::ComputeMatrixTerm(
00053     c_vector<double, ELEM_DIM+1> &rPhi,
00054     c_matrix<double, SPACE_DIM, ELEM_DIM+1> &rGradPhi,
00055     ChastePoint<SPACE_DIM> &rX,
00056     c_vector<double,1> &u,
00057     c_matrix<double,1,SPACE_DIM> &rGradU /* not used */,
00058     Element<ELEM_DIM,SPACE_DIM>* pElement)
00059 {
00060     return outer_prod(rPhi, rPhi);
00061 }
00062 
00063 template<unsigned ELEM_DIM, unsigned SPACE_DIM>
00064 c_vector<double,1*(ELEM_DIM+1)> MonodomainRhsMatrixAssembler<ELEM_DIM,SPACE_DIM>::ComputeVectorTerm(
00065     c_vector<double, ELEM_DIM+1> &rPhi,
00066     c_matrix<double, SPACE_DIM, ELEM_DIM+1> &rGradPhi,
00067     ChastePoint<SPACE_DIM> &rX,
00068     c_vector<double,1> &u,
00069     c_matrix<double, 1, SPACE_DIM> &rGradU /* not used */,
00070     Element<ELEM_DIM,SPACE_DIM>* pElement)
00071 {
00072     #define COVERAGE_IGNORE
00073     NEVER_REACHED;
00074     return zero_vector<double>(SPACE_DIM+1);
00075     #undef COVERAGE_IGNORE
00076 }
00077 
00078 template<unsigned ELEM_DIM, unsigned SPACE_DIM>
00079 c_vector<double, ELEM_DIM> MonodomainRhsMatrixAssembler<ELEM_DIM, SPACE_DIM>::ComputeVectorSurfaceTerm(
00080     const BoundaryElement<ELEM_DIM-1,SPACE_DIM> &rSurfaceElement,
00081     c_vector<double, ELEM_DIM> &rPhi,
00082     ChastePoint<SPACE_DIM> &rX )
00083 {
00084     #define COVERAGE_IGNORE
00085     NEVER_REACHED;
00086     return zero_vector<double>(SPACE_DIM);
00087     #undef COVERAGE_IGNORE
00088 }
00089 
00090 
00091 template<unsigned ELEM_DIM, unsigned SPACE_DIM>
00092 MonodomainRhsMatrixAssembler<ELEM_DIM,SPACE_DIM>::MonodomainRhsMatrixAssembler(AbstractTetrahedralMesh<ELEM_DIM,SPACE_DIM>* pMesh)
00093     : AbstractLinearAssembler<ELEM_DIM,SPACE_DIM,1,false,MonodomainRhsMatrixAssembler<ELEM_DIM,SPACE_DIM> >()
00094 {
00095     this->mpMesh = pMesh;
00096 
00097     // this needs to be set up, though no boundary condition values are used in the matrix
00098     this->mpBoundaryConditions = new BoundaryConditionsContainer<ELEM_DIM,SPACE_DIM,1>;
00099     this->mpBoundaryConditions->DefineZeroNeumannOnMeshBoundary(pMesh);
00100 
00101     //This linear system needs a distribution from the DistributedVector class
00102     Vec temp_vec = pMesh->GetDistributedVectorFactory()->CreateVec();
00103     this->mpLinearSystem = new LinearSystem(temp_vec);
00104     VecDestroy(temp_vec);
00105     this->AssembleSystem(false,true);
00106 }
00107 
00108 template<unsigned ELEM_DIM, unsigned SPACE_DIM>
00109 MonodomainRhsMatrixAssembler<ELEM_DIM, SPACE_DIM>::~MonodomainRhsMatrixAssembler()
00110 {
00111     delete this->mpBoundaryConditions;
00112 }
00113 
00114 template<unsigned ELEM_DIM, unsigned SPACE_DIM>
00115 Mat* MonodomainRhsMatrixAssembler<ELEM_DIM,SPACE_DIM>::GetMatrix()
00116 {
00117     return &(this->mpLinearSystem->rGetLhsMatrix());
00118 }
00119 
00120 
00121 
00122 
00123 
00125 // MonodomainMatrixBasedAssembler
00127 
00128 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00129 MonodomainMatrixBasedAssembler<ELEMENT_DIM,SPACE_DIM>::MonodomainMatrixBasedAssembler(
00130             AbstractTetrahedralMesh<ELEMENT_DIM,SPACE_DIM>* pMesh,
00131             MonodomainPde<ELEMENT_DIM,SPACE_DIM>* pPde,
00132             BoundaryConditionsContainer<ELEMENT_DIM, SPACE_DIM, 1>* pBcc,
00133             unsigned numQuadPoints)
00134     : MonodomainDg0Assembler<ELEMENT_DIM,SPACE_DIM>(pMesh, pPde, pBcc, numQuadPoints)
00135 {
00136     // construct matrix using the helper class
00137     mpMonodomainRhsMatrixAssembler = new MonodomainRhsMatrixAssembler<ELEMENT_DIM,SPACE_DIM>(pMesh);
00138     this->mpMatrixForMatrixBasedRhsAssembly = mpMonodomainRhsMatrixAssembler->GetMatrix();
00139 
00140     // set variables on parent class so that we do matrix-based assembly, and allocate
00141     // memory for the vector 'z'
00142     this->mUseMatrixBasedRhsAssembly = true;
00143     this->mVectorForMatrixBasedRhsAssembly = pMesh->GetDistributedVectorFactory()->CreateVec();
00144 
00145     // Tell pde there's no need to replicate ionic caches
00146     pPde->SetCacheReplication(false);
00147 }
00148 
00149 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00150 MonodomainMatrixBasedAssembler<ELEMENT_DIM,SPACE_DIM>::~MonodomainMatrixBasedAssembler()
00151 {
00152     delete mpMonodomainRhsMatrixAssembler;
00153     VecDestroy(this->mVectorForMatrixBasedRhsAssembly);
00154 }
00155 
00156 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM>
00157 void MonodomainMatrixBasedAssembler<ELEMENT_DIM,SPACE_DIM>::ConstructVectorForMatrixBasedRhsAssembly(Vec existingSolution)
00158 {
00159     // copy V to z
00160     VecCopy(existingSolution, this->mVectorForMatrixBasedRhsAssembly);
00161 
00162     // set up a vector which has the nodewise force term (ie A*I_ionic+I_stim)
00163     Vec force_term_at_nodes = this->mpMesh->GetDistributedVectorFactory()->CreateVec();
00164     PetscInt lo, hi;
00165     VecGetOwnershipRange(force_term_at_nodes, &lo, &hi);
00166     double *p_force_term;
00167     VecGetArray(force_term_at_nodes, &p_force_term);
00168     for (int global_index=lo; global_index<hi; global_index++)
00169     {
00170         unsigned local_index = global_index - lo;
00171         const Node<SPACE_DIM>* p_node = this->mpMesh->GetNode(global_index);
00172         p_force_term[local_index] = this->mpMonodomainPde->ComputeNonlinearSourceTermAtNode(*p_node, 0.0);
00173     }
00174     VecRestoreArray(force_term_at_nodes, &p_force_term);
00175     VecAssemblyBegin(force_term_at_nodes);
00176     VecAssemblyEnd(force_term_at_nodes);
00177 
00178     double one=1.0;
00179     double scaling=  this->mpMonodomainPde->ComputeDuDtCoefficientFunction(ChastePoint<SPACE_DIM>())
00180                     *this->mDtInverse;
00181 
00182 #if (PETSC_VERSION_MINOR == 2) //Old API
00183     // VecAXPBY(a,b,x,y) does y = ax + by
00184     VecAXPBY(&one, &scaling, force_term_at_nodes, this->mVectorForMatrixBasedRhsAssembly);
00185 #else
00186     // VecAXPBY(y,a,b,x) does y = ax + by
00187     VecAXPBY(this->mVectorForMatrixBasedRhsAssembly,
00188              one,
00189              scaling,
00190              force_term_at_nodes);
00191 #endif
00192 
00193     VecAssemblyBegin(this->mVectorForMatrixBasedRhsAssembly);
00194     VecAssemblyEnd(this->mVectorForMatrixBasedRhsAssembly);
00195     VecDestroy(force_term_at_nodes);
00196 }
00197 
00199 // Explicit instantiation
00201 
00202 template class MonodomainMatrixBasedAssembler<1,1>;
00203 template class MonodomainMatrixBasedAssembler<1,2>;
00204 template class MonodomainMatrixBasedAssembler<1,3>;
00205 template class MonodomainMatrixBasedAssembler<2,2>;
00206 template class MonodomainMatrixBasedAssembler<3,3>;
00207 
00208 template class MonodomainRhsMatrixAssembler<1,1>;
00209 template class MonodomainRhsMatrixAssembler<1,2>;
00210 template class MonodomainRhsMatrixAssembler<1,3>;
00211 template class MonodomainRhsMatrixAssembler<2,2>;
00212 template class MonodomainRhsMatrixAssembler<3,3>;

Generated on Tue Aug 4 16:10:22 2009 for Chaste by  doxygen 1.5.5