AbstractLinearAssembler.hpp

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 #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     bool mMatrixIsConstant;
00047 
00048 protected:
00049 
00051     void SetMatrixIsConst(bool matrixIsConstant = true)
00052     {
00053          mMatrixIsConstant = matrixIsConstant;
00054     }
00055 
00059     void ApplyDirichletConditions(Vec /* unused */, bool applyToMatrix)
00060     {
00061         this->mpBoundaryConditions->ApplyDirichletToLinearProblem(*(this->mpLinearSystem), applyToMatrix);
00062     }
00063 
00069     virtual void InitialiseForSolve(Vec initialSolution)
00070     {
00071         if (this->mpLinearSystem == NULL)
00072         {
00073             if (initialSolution == NULL)
00074             {
00075                 // Static problem, create linear system
00076                 // The following ensures all the unknowns for a particular node
00077                 // are on the same processor
00078                 DistributedVector::SetProblemSize(this->mpMesh->GetNumNodes());
00079                 Vec template_vec = DistributedVector::CreateVec(PROBLEM_DIM);
00080 
00081                 this->mpLinearSystem = new LinearSystem(template_vec);
00082 
00083                 VecDestroy(template_vec);
00084             }
00085             else
00086             {
00087                 // Use the currrent solution (ie the initial solution)
00088                 // as the template in the alternative constructor of
00089                 // LinearSystem. This is to avoid problems with VecScatter.
00090                 this->mpLinearSystem = new LinearSystem(initialSolution);
00091             }
00092 
00093             this->mpLinearSystem->SetMatrixIsConstant(mMatrixIsConstant);
00094         }
00095     }
00096 
00097     bool ProblemIsNonlinear()
00098     {
00099         return false;
00100     }
00101 
00107     virtual Vec StaticSolve(Vec currentSolutionOrGuess=NULL,
00108                             double currentTime=0.0,
00109                             bool assembleMatrix=true)
00110     {
00111         this->AssembleSystem(true, assembleMatrix, currentSolutionOrGuess, currentTime);
00112         return this->mpLinearSystem->Solve(currentSolutionOrGuess);
00113     }
00114 
00115 
00116 public:
00117     AbstractLinearAssembler(unsigned numQuadPoints = 2) :
00118             AbstractStaticAssembler<ELEMENT_DIM,SPACE_DIM,PROBLEM_DIM, NON_HEART, CONCRETE>(numQuadPoints),
00119             mMatrixIsConstant(true)
00120     {
00121     }
00122 
00126     ~AbstractLinearAssembler()
00127     {
00128     }
00129 
00136     virtual Vec Solve(Vec currentSolutionOrGuess=NULL, double currentTime=0.0)
00137     {
00139         assert(this->mpMesh!=NULL);
00140 
00141         // have to comment this out because the flagged mesh assembler uses a different
00142         // bcc and so this is null.
00143         //assert(this->mpBoundaryConditions!=NULL);
00144 
00145         this->PrepareForSolve();
00146         this->InitialiseForSolve(currentSolutionOrGuess);
00147         return this->StaticSolve(currentSolutionOrGuess, currentTime);
00148     }
00149 
00150 
00151     /*
00152     void DebugWithSolution(Vec sol)
00153     {
00154         std::cout<<"\n\nWS: This is the matrix:\n";
00155         mpLinearSystem->DisplayMatrix();
00156         std::cout<<"\n\nWS: This is the righthand side:\n";
00157         mpLinearSystem->DisplayRhs();
00158         std::cout<<"\n\nWS: This is the solution:\n";
00159         VecView(sol, PETSC_VIEWER_STDOUT_WORLD);
00160     }
00161 
00162     void Debug()
00163     {
00164         std::cout<<"\n\nThis is the matrix:\n";
00165         mpLinearSystem->DisplayMatrix();
00166         std::cout<<"\n\nThis is the righthand side:\n";
00167         mpLinearSystem->DisplayRhs();
00168     }
00169     */
00170 };
00171 
00172 #endif //_ABSTRACTLINEARASSEMBLER_HPP_

Generated on Wed Mar 18 12:51:56 2009 for Chaste by  doxygen 1.5.5