AbstractLinearAssembler.hpp

00001 /*
00002 
00003 Copyright (C) University of Oxford, 2005-2010
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 #ifndef _ABSTRACTLINEARASSEMBLER_HPP_
00029 #define _ABSTRACTLINEARASSEMBLER_HPP_
00030 
00031 
00032 #include <vector>
00033 #include <iostream>
00034 #include <petscvec.h>
00035 
00036 #include "AbstractStaticAssembler.hpp"
00037 
00038 
00042 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00043 class AbstractLinearAssembler : public AbstractStaticAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>
00044 {
00045 private:
00046 
00048     bool mMatrixIsConstant;
00049 
00050 protected:
00051 
00057     void SetMatrixIsConst(bool matrixIsConstant=true);
00058 
00065     void ApplyDirichletConditions(Vec unusedVector, bool applyToMatrix);
00066 
00073     virtual void InitialiseForSolve(Vec initialSolution);
00074 
00078     bool ProblemIsNonlinear();
00079 
00091     virtual Vec StaticSolve(Vec currentSolutionOrGuess=NULL,
00092                             double currentTime=0.0,
00093                             bool assembleMatrix=true);
00094 
00095 public:
00096 
00102     AbstractLinearAssembler(unsigned numQuadPoints=2);
00103 
00107     ~AbstractLinearAssembler();
00108 
00118     virtual Vec Solve(Vec currentSolutionOrGuess=NULL, double currentTime=0.0);
00119 
00120     /*
00121     void DebugWithSolution(Vec sol)
00122     {
00123         std::cout<<"\n\nWS: This is the matrix:\n";
00124         mpLinearSystem->DisplayMatrix();
00125         std::cout<<"\n\nWS: This is the righthand side:\n";
00126         mpLinearSystem->DisplayRhs();
00127         std::cout<<"\n\nWS: This is the solution:\n";
00128         VecView(sol, PETSC_VIEWER_STDOUT_WORLD);
00129     }
00130 
00131     void Debug()
00132     {
00133         std::cout<<"\n\nThis is the matrix:\n";
00134         mpLinearSystem->DisplayMatrix();
00135         std::cout<<"\n\nThis is the righthand side:\n";
00136         mpLinearSystem->DisplayRhs();
00137     }
00138     */
00139 };
00140 
00141 
00143 // Implementation
00145 
00146 
00147 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00148 void AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::SetMatrixIsConst(bool matrixIsConstant)
00149 {
00150      mMatrixIsConstant = matrixIsConstant;
00151 }
00152 
00153 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00154 void AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::ApplyDirichletConditions(Vec unusedVector, bool applyToMatrix)
00155 {
00156     this->mpBoundaryConditions->ApplyDirichletToLinearProblem(*(this->mpLinearSystem), applyToMatrix);
00157 }
00158 
00159 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00160 void AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::InitialiseForSolve(Vec initialSolution)
00161 {
00162     if (this->mpLinearSystem == NULL)
00163     {
00164         HeartEventHandler::BeginEvent(HeartEventHandler::COMMUNICATION);
00165         if (initialSolution == NULL)
00166         {
00167             // Static problem, create linear system
00168             // The following ensures all the unknowns for a particular node
00169             // are on the same processor
00170             Vec template_vec = this->mpMesh->GetDistributedVectorFactory()->CreateVec(PROBLEM_DIM);
00171 
00172             this->mpLinearSystem = new LinearSystem(template_vec);
00173 
00174             VecDestroy(template_vec);
00175         }
00176         else
00177         {
00178             // Use the currrent solution (ie the initial solution)
00179             // as the template in the alternative constructor of
00180             // LinearSystem. This is to avoid problems with VecScatter.
00181             this->mpLinearSystem = new LinearSystem(initialSolution);
00182         }
00183 
00184         this->mpLinearSystem->SetMatrixIsConstant(mMatrixIsConstant);
00185         HeartEventHandler::EndEvent(HeartEventHandler::COMMUNICATION);
00186     }
00187 }
00188 
00189 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00190 bool AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::ProblemIsNonlinear()
00191 {
00192     return false;
00193 }
00194 
00195 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00196 Vec AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::StaticSolve(Vec currentSolutionOrGuess,
00197                         double currentTime,
00198                         bool assembleMatrix)
00199 {
00200     this->AssembleSystem(true, assembleMatrix, currentSolutionOrGuess, currentTime);
00201     return this->mpLinearSystem->Solve(currentSolutionOrGuess);
00202 }
00203 
00204 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00205 AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::AbstractLinearAssembler(unsigned numQuadPoints)
00206     : AbstractStaticAssembler<ELEMENT_DIM,SPACE_DIM,PROBLEM_DIM, NON_HEART, CONCRETE>(numQuadPoints),
00207       mMatrixIsConstant(true)
00208 {
00209 }
00210 
00211 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00212 AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::~AbstractLinearAssembler()
00213 {
00214 }
00215 
00216 template<unsigned ELEMENT_DIM, unsigned SPACE_DIM, unsigned PROBLEM_DIM, bool NON_HEART, class CONCRETE>
00217 Vec AbstractLinearAssembler<ELEMENT_DIM, SPACE_DIM, PROBLEM_DIM, NON_HEART, CONCRETE>::Solve(Vec currentSolutionOrGuess, double currentTime)
00218 {
00219     // have to comment this out because the flagged mesh assembler uses a different
00220     // bcc and so this is null.
00221     //assert(this->mpBoundaryConditions!=NULL);
00222 
00223     this->PrepareForSolve();
00224     this->InitialiseForSolve(currentSolutionOrGuess);
00225     return this->StaticSolve(currentSolutionOrGuess, currentTime);
00226 }
00227 
00228 #endif //_ABSTRACTLINEARASSEMBLER_HPP_

Generated by  doxygen 1.6.2