Chaste  Release::2017.1
SimplePetscNonlinearSolver.cpp
1 /*
2 
3 Copyright (c) 2005-2017, University of Oxford.
4 All rights reserved.
5 
6 University of Oxford means the Chancellor, Masters and Scholars of the
7 University of Oxford, having an administrative office at Wellington
8 Square, Oxford OX1 2JD, UK.
9 
10 This file is part of Chaste.
11 
12 Redistribution and use in source and binary forms, with or without
13 modification, are permitted provided that the following conditions are met:
14  * Redistributions of source code must retain the above copyright notice,
15  this list of conditions and the following disclaimer.
16  * Redistributions in binary form must reproduce the above copyright notice,
17  this list of conditions and the following disclaimer in the documentation
18  and/or other materials provided with the distribution.
19  * Neither the name of the University of Oxford nor the names of its
20  contributors may be used to endorse or promote products derived from this
21  software without specific prior written permission.
22 
23 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
24 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
27 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
29 GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 
34 */
35 
40 #include <sstream>
41 #include "petscsnes.h"
42 #include "SimplePetscNonlinearSolver.hpp"
43 #include "Exception.hpp"
44 #include "PetscTools.hpp"
45 
46 Vec SimplePetscNonlinearSolver::Solve(PetscErrorCode (*pComputeResidual)(SNES,Vec,Vec,void*),
47 #if (PETSC_VERSION_MAJOR==3 && PETSC_VERSION_MINOR>=5)
48  PetscErrorCode (*pComputeJacobian)(SNES,Vec,Mat,Mat,void*),
49 #else
50  PetscErrorCode (*pComputeJacobian)(SNES,Vec,Mat*,Mat*,MatStructure*,void*),
51 #endif
52  Vec initialGuess,
53  unsigned fill,
54  void* pContext)
55 {
56  SNES snes;
57 
58  // Create the residual vector by copying the structure of the initial guess
59  Vec residual;
60  VecDuplicate(initialGuess, &residual);
61 
62  Mat jacobian; // Jacobian Matrix
63  PetscInt N; // number of elements
64 
65  // Get the size of the Jacobian from the residual
66  VecGetSize(initialGuess, &N);
67 
68  // Note that the Jacobian matrix might involve new non-zero elements in the course of a SNES solve
69  PetscTools::SetupMat(jacobian, N, N, fill, PETSC_DECIDE, PETSC_DECIDE, true, false /*malloc flag*/);
70 
71  SNESCreate(PETSC_COMM_WORLD, &snes);
72 
73  SNESSetFunction(snes, residual, pComputeResidual, pContext);
74  SNESSetJacobian(snes, jacobian, jacobian, pComputeJacobian, pContext);
75 
76 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 4) //PETSc 3.4 or later
77  SNESSetType(snes, SNESNEWTONLS);
78 #else
79  SNESSetType(snes, SNESLS);
80 #endif
81 
82  SNESSetTolerances(snes,1.0e-5,1.0e-5,1.0e-5,PETSC_DEFAULT,PETSC_DEFAULT);
83 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR == 3) //PETSc 3.3
84  SNESLineSearch linesearch;
85  SNESGetSNESLineSearch(snes, &linesearch);
86  SNESLineSearchSetType(linesearch, "bt"); //Use backtracking search as default
87 #elif (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 4) //PETSc 3.4 or later
88  SNESLineSearch linesearch;
89  SNESGetLineSearch(snes, &linesearch);
90  SNESLineSearchSetType(linesearch, "bt"); //Use backtracking search as default
91 #endif
92 
93  // x is the iteration vector SNES uses when solving, set equal to initialGuess to start with
94  Vec x;
95 
96  VecDuplicate(initialGuess, &x);
97  VecCopy(initialGuess, x);
98 
99 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 5)
100  // Seems to want the preconditioner to be explicitly set to none now
101  // Copied this from the similar PETSc example at:
102  // http://www.mcs.anl.gov/petsc/petsc-current/src/snes/examples/tutorials/ex1.c
103  // Which got it to work...
104  KSP ksp;
105  SNESGetKSP(snes,&ksp);
106  PC pc;
107  KSPGetPC(ksp,&pc);
108  PCSetType(pc,PCNONE);
109 #endif
110 
111 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
112  SNESSolve(snes, x);
113 #else
114  SNESSolve(snes, PETSC_NULL, x);
115 #endif
116  PetscTools::Destroy(residual);
117  PetscTools::Destroy(jacobian); // Free Jacobian
118 
119  SNESConvergedReason reason;
120  SNESGetConvergedReason(snes,&reason);
121 // LCOV_EXCL_START
122  if (reason < 0)
123  {
124  std::stringstream reason_stream;
125  reason_stream << reason;
126  PetscTools::Destroy(x); // Since caller can't free the memory in this case
127  SNESDestroy(PETSC_DESTROY_PARAM(snes));
128  EXCEPTION("Nonlinear Solver did not converge. PETSc reason code:"
129  +reason_stream.str()+" .");
130  }
131 // LCOV_EXCL_STOP
132  SNESDestroy(PETSC_DESTROY_PARAM(snes));
133 
134  return x;
135 }
#define PETSC_DESTROY_PARAM(x)
Definition: PetscTools.hpp:70
#define EXCEPTION(message)
Definition: Exception.hpp:143
static void SetupMat(Mat &rMat, int numRows, int numColumns, unsigned rowPreallocation, int numLocalRows=PETSC_DECIDE, int numLocalColumns=PETSC_DECIDE, bool ignoreOffProcEntries=true, bool newAllocationError=true)
Definition: PetscTools.cpp:268
static void Destroy(Vec &rVec)
Definition: PetscTools.hpp:352
Vec Solve(PetscErrorCode(*pComputeResidual)(SNES, Vec, Vec, void *), PetscErrorCode(*pComputeJacobian)(SNES, Vec, Mat *, Mat *, MatStructure *, void *), Vec initialGuess, unsigned fill, void *pContext)