File: KrylovSolver.cpp

package info (click to toggle)
freefem3d 1.0pre8-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 13,920 kB
  • ctags: 7,145
  • sloc: cpp: 45,748; sh: 8,698; yacc: 2,847; makefile: 600; ansic: 504; perl: 110
file content (162 lines) | stat: -rw-r--r-- 4,919 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//  This file is part of ff3d - http://www.freefem.org/ff3d
//  Copyright (C) 2001, 2002, 2003 Stphane Del Pino

//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2, or (at your option)
//  any later version.

//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software Foundation,
//  Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  

//  $Id: KrylovSolver.cpp,v 1.5 2005/11/27 16:41:48 delpinux Exp $


#include <BaseVector.hpp>
#include <BaseMatrix.hpp>

#include <Vector.hpp>
#include <SparseMatrix.hpp>

#include <KrylovSolver.hpp>

#include <Preconditioner.hpp>
#include <DiagPrecond.hpp>
#include <IncompleteCholeskiFactorization.hpp>
#include <IdentityPrecond.hpp>

#include <PDESystem.hpp>

#include <ConjugateGradient.hpp>
#include <BiConjugateGradient.hpp>
#include <BiConjugateGradientStabilized.hpp>
#include <PETScKrylovSolver.hpp>

#include <MultiGrid.hpp>

#include <Timer.hpp>

void KrylovSolverDim(BaseVector& u,
		     const BaseMatrix& A, const BaseVector& b,
		     const Problem& problem,
		     ReferenceCounting<Structured3DMeshShape> meshShape,
		     const KrylovSolverOptions::Type& type,
		     const KrylovSolverOptions::PreconditionerType& pType,
		     const DegreeOfFreedomSet& degreeOfFreedomSet)
{
#warning temporay implementation
#ifdef    HAVE_PETSC
  assert(A.type() == BaseMatrix::petscMatrix);
  PETScKrylovSolver pks(static_cast<const Vector<real_t>&>(b),
			static_cast<const PETScMatrix&>(A),
			static_cast<Vector<real_t>&>(u));
  return;
#endif // HAVE_PETSC
  ReferenceCounting<Preconditioner> P = 0;

  switch (pType) {
  case KrylovSolverOptions::none: {
    P = new IdentityPredond(problem);
    break;
  }
  case KrylovSolverOptions::diagonal: {
    P = new DiagPrecond(problem, A);
    break;
  }
  case KrylovSolverOptions::incompleteCholeski: {
    P = new IncompleteCholeskiFactorization(problem, A);
    break;
  }
  case KrylovSolverOptions::multiGrid: {
    if (meshShape == 0) {
      throw ErrorHandler(__FILE__,__LINE__,
			 "Cartesian 3D mesh is mandatory to use multigrid precond",
			 ErrorHandler::normal);
    }
    P = new MultiGrid(problem,
		      static_cast<const SparseMatrix&>(A),
		      degreeOfFreedomSet,
		      *meshShape);
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "unexpected preconditioner type",
		       ErrorHandler::unexpected);
  }
  }
  ffout(2) << "- preconditioner: " << (*P).name() << '\n';
  ffout(2) << "- preconditioner initialization\n" << std::flush;
  (*P).initializes();
  ffout(2) << "  preconditioner initialization: done\n";

  switch (type) {
  case (KrylovSolverOptions::conjugateGradient): {
    ConjugateGradient cg(static_cast<const Vector<real_t>&>(b),
			 A,
			 *P,
			 static_cast<Vector<real_t>&>(u));
    break;
  }
  case (KrylovSolverOptions::biConjugateGradient): {
    BiConjugateGradient bicg(static_cast<const Vector<real_t>&>(b),
			     A,
			     *P,
			     static_cast<Vector<real_t>&>(u));
    break;
  }
  case (KrylovSolverOptions::biConjugateGradientStabilized): {
    BiConjugateGradientStabilized bicgstab(static_cast<const Vector<real_t>&>(b),
					   A,
					   *P,
					   static_cast<Vector<real_t>&>(u));
    break;
  }
//   case (KrylovSolverOptions::multiGrid): {
//     MultiGrid<1, real, real_t> mg(static_cast<const Vector<real_t>&>(b),
// 				    static_cast<SparseMatrix<real_t>&>(A),
// 				    static_cast<Vector<real_t>&>(u),
// 				    os);
//     break;
//   }
  case (KrylovSolverOptions::iterativeLUFactorization): {
//     IterativeLUFactorization<1,real,real_t>(static_cast<const Vector<real_t>&>(b),
// 					      static_cast<SparseMatrix<real_t>&>(A),
// 					      static_cast<Vector<real_t>&>(u));
    throw ErrorHandler(__FILE__,__LINE__,
		       "not implemented",
		       ErrorHandler::unexpected);
    break;
  }
  default: {
    throw ErrorHandler(__FILE__,__LINE__,
		       "unexpected preconditioner type",
		       ErrorHandler::unexpected);
  }
  }
}

void KrylovSolver::solve(BaseVector& u, const Problem& problem)
{
  // initializing the timer.
  Timer t;
  t.start();

  ffout(2) << "Krylov solver\n";
  ffout(2) << "- number of unknowns: "
	   << static_cast<Vector<real_t>&>(u).size() << '\n';
  
  KrylovSolverDim(u, __A, __b, problem, __meshShape, __type, __pType,
		  __degreeOfFreedomSet);
  // measure the time
  t.stop();
  ffout(2) << "Krylov solver: done";
  ffout(3) << " [cost: "  << t << ']';
  ffout(2) << '\n';
}