Actual source code: ksponly.c
petsc-3.8.4 2018-03-24
2: #include <petsc/private/snesimpl.h>
4: static PetscErrorCode SNESSolve_KSPONLY(SNES snes)
5: {
6: PetscErrorCode ierr;
7: PetscInt lits;
8: Vec Y,X,F;
11: if (snes->xl || snes->xu || snes->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)snes),PETSC_ERR_ARG_WRONGSTATE, "SNES solver %s does not support bounds", ((PetscObject)snes)->type_name);
13: snes->numFailures = 0;
14: snes->numLinearSolveFailures = 0;
15: snes->reason = SNES_CONVERGED_ITERATING;
16: snes->iter = 0;
17: snes->norm = 0.0;
19: X = snes->vec_sol;
20: F = snes->vec_func;
21: Y = snes->vec_sol_update;
23: SNESComputeFunction(snes,X,F);
24: if (snes->numbermonitors) {
25: PetscReal fnorm;
26: VecNorm(F,NORM_2,&fnorm);
27: SNESMonitor(snes,0,fnorm);
28: }
30: /* Call general purpose update function */
31: if (snes->ops->update) {
32: (*snes->ops->update)(snes, 0);
33: }
35: /* Solve J Y = F, where J is Jacobian matrix */
36: SNESComputeJacobian(snes,X,snes->jacobian,snes->jacobian_pre);
37: KSPSetOperators(snes->ksp,snes->jacobian,snes->jacobian_pre);
38: KSPSolve(snes->ksp,F,Y);
39: snes->reason = SNES_CONVERGED_ITS;
40: SNESCheckKSPSolve(snes);
42: KSPGetIterationNumber(snes->ksp,&lits);
43: snes->linear_its += lits;
44: PetscInfo2(snes,"iter=%D, linear solve iterations=%D\n",snes->iter,lits);
45: snes->iter++;
47: /* Take the computed step. */
48: VecAXPY(X,-1.0,Y);
49: if (snes->numbermonitors) {
50: PetscReal fnorm;
51: SNESComputeFunction(snes,X,F);
52: VecNorm(F,NORM_2,&fnorm);
53: SNESMonitor(snes,1,fnorm);
54: }
55: return(0);
56: }
58: static PetscErrorCode SNESSetUp_KSPONLY(SNES snes)
59: {
63: SNESSetUpMatrices(snes);
64: return(0);
65: }
67: static PetscErrorCode SNESDestroy_KSPONLY(SNES snes)
68: {
71: return(0);
72: }
74: /* -------------------------------------------------------------------------- */
75: /*MC
76: SNESKSPONLY - Nonlinear solver that only performs one Newton step and does not compute any norms.
77: The main purpose of this solver is to solve linear problems using the SNES interface, without
78: any additional overhead in the form of vector operations.
80: Level: beginner
82: .seealso: SNESCreate(), SNES, SNESSetType(), SNESNEWTONLS, SNESNEWTONTR
83: M*/
84: PETSC_EXTERN PetscErrorCode SNESCreate_KSPONLY(SNES snes)
85: {
88: snes->ops->setup = SNESSetUp_KSPONLY;
89: snes->ops->solve = SNESSolve_KSPONLY;
90: snes->ops->destroy = SNESDestroy_KSPONLY;
91: snes->ops->setfromoptions = 0;
92: snes->ops->view = 0;
93: snes->ops->reset = 0;
95: snes->usesksp = PETSC_TRUE;
96: snes->usesnpc = PETSC_FALSE;
98: snes->alwayscomputesfinalresidual = PETSC_FALSE;
100: snes->data = 0;
101: return(0);
102: }