SimplePetscNonlinearSolver.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 
00036 #include "SimplePetscNonlinearSolver.hpp"
00037 #include "Exception.hpp"
00038 #include "petscsnes.h"
00039 #include "PetscTools.hpp"
00040 #include <sstream>
00041 
00042 
00072 Vec SimplePetscNonlinearSolver::Solve(PetscErrorCode (*pComputeResidual)(SNES,Vec,Vec,void*),
00073                                       PetscErrorCode (*pComputeJacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),
00074                                       Vec initialGuess,
00075                                       void *pContext)
00076 {
00077     SNES snes;
00078 
00079     // create the residual vector by copying the structure of the initial guess
00080     Vec residual;
00081     VecDuplicate(initialGuess, &residual);
00082 
00083     Mat jacobian; //Jacobian Matrix
00084 
00085     PetscInt N; //number of elements
00086     //get the size of the jacobian from the residual
00087     VecGetSize(initialGuess,&N);
00088 
00089     PetscTools::SetupMat(jacobian, N, N);
00090 
00091     SNESCreate(PETSC_COMM_WORLD, &snes);
00092     SNESSetFunction(snes, residual, pComputeResidual, pContext);
00093     SNESSetJacobian(snes, jacobian, jacobian, pComputeJacobian, pContext);
00094     SNESSetType(snes,SNESLS);
00095     SNESSetTolerances(snes,1.0e-5,1.0e-5,1.0e-5,PETSC_DEFAULT,PETSC_DEFAULT);
00096 
00097     // x is the iteration vector SNES uses when solving, set equal to initialGuess to start with
00098     Vec x;
00099     VecDuplicate(initialGuess, &x);
00100     VecCopy(initialGuess, x);
00101 
00102 
00103 #if (PETSC_VERSION_MINOR == 2) //Old API
00104     SNESSolve(snes, x);
00105 #else
00106     SNESSolve(snes, PETSC_NULL, x);
00107 #endif
00108 
00109     VecDestroy(residual);
00110     MatDestroy(jacobian); // Free Jacobian
00111 
00112     SNESConvergedReason reason;
00113     SNESGetConvergedReason(snes,&reason);
00114 #define COVERAGE_IGNORE
00115     if (reason<0)
00116     {
00117         std::stringstream reason_stream;
00118         reason_stream << reason;
00119         VecDestroy(x); // Since caller can't free the memory in this case
00120         SNESDestroy(snes);
00121         EXCEPTION("Nonlinear Solver did not converge. Petsc reason code:"
00122                   +reason_stream.str()+" .");
00123     }
00124 #undef COVERAGE_IGNORE
00125     SNESDestroy(snes);
00126 
00127     return x;
00128 }

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