Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/nlnml_preconditioner.H

raw
1 // @HEADER
2 // *****************************************************************************
3 // Trilinos: An Object-Oriented Solver Framework
4 //
5 // Copyright 2001-2024 NTESS and the Trilinos contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /* ******************************************************************** */
11 /* See the file COPYRIGHT for a complete copyright notice, contact */
12 /* person and disclaimer. */
13 /* ******************************************************************** */
14 /*!
15 * \file nlnml_preconditioner.H
16 *
17 * \class NLNML_Preconditioner
18 *
19 * \brief ML nonlinear preconditioner and solver
20 *
21 * \date Last update do Doxygen: 6-April-06
22 *
23 */
24 #include "ml_common.h"
25 #include "ml_include.h"
26 #include "TrilinosCouplings_config.h"
27
28 #if defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA) && defined(HAVE_ML_AZTECOO) && defined(HAVE_ML_TEUCHOS) && defined(HAVE_ML_IFPACK) && defined(HAVE_ML_AMESOS) && defined(HAVE_ML_EPETRAEXT)
29
30 #ifndef NLNML_PRECONDITIONER_H
31 #define NLNML_PRECONDITIONER_H
32
33 // ML-headers
34 #include "ml_epetra_utils.h"
35 #include "ml_epetra.h"
36 #include "ml_epetra_operator.h"
37 #include "ml_MultiLevelOperator.h"
38 #include "ml_agg_VBMETIS.h"
39
40 // epetra-headers
41 #include "Epetra_Map.h"
42 #include "Epetra_Vector.h"
43 #include "Epetra_MultiVector.h"
44 #include "Epetra_Operator.h"
45 #include "Epetra_CrsMatrix.h"
46 #include "Epetra_RowMatrix.h"
47 #include "Epetra_CrsGraph.h"
48 #include "Epetra_LinearProblem.h"
49 #include "Epetra_Object.h"
50 #include "Epetra_IntVector.h"
51 #include "Epetra_Comm.h"
52 #include "Epetra_SerialComm.h"
53 #ifdef ML_MPI
54 #include "Epetra_MpiComm.h"
55 #endif
56 #include "Epetra_SerialComm.h"
57
58 // Teuchos
59 #include "Teuchos_RefCountPtr.hpp"
60 #include "Teuchos_ParameterList.hpp"
61
62
63 // NOX-headers
64 #include "NOX_Epetra_Interface_Preconditioner.H"
65 #include "NOX_Epetra_Group.H"
66 //JJH
67 //#include "NOX_Solver_Manager.H"
68 #include "NOX_Epetra_FiniteDifferenceColoring.H"
69 #include "NOX_Epetra_Vector.H"
70
71 // own headers
72 #include "nlnml_preconditioner_utils.H"
73 #include "nlnml_finelevelnoxinterface.H"
74 #include "nlnml_nonlinearlevel.H"
75
76 using namespace Teuchos;
77 //! NLNML: default namespace for all ML/NOX nonlinear methods
78 namespace NLNML
79 {
80
81 // ---------- Forward Declarations ----------
82 class NLNML_FineLevelNoxInterface;
83
84 /*!
85
86 \brief NLNML_Preconditioner: a class to define a multilevel nonlinear preconditioner and solver
87
88 Class NLNML::NLNML_Preconditioner defines an algebraic nonlinear preconditioner
89 or solver that is based on a full approximation scheme (FAS) approach and
90 is implemented making use of NOX' nonlinear solver capabilities.
91 It operates on Epetra objects and implements an Epetra_Operator to be easily
92 used as a preconditioner to some outer nonlinear iteration.
93 It also implements the NOX::EpetraNew::Interface::Preconditioner from NOX
94 so it can serve as it's own interface (see NOX documentation for details).
95 In this class several methods and options can be chosen, which are described in the
96 parameter set functions
97
98
99 To make use of the full functionality of this class it
100 requires ML to be configured with the following options:
101 - \c --with-ml_nox
102 - \c --enable-epetra
103 - \c --enable-epetraext
104 - \c --enable-nox
105 - \c --enable-nox-epetra
106 - \c --enable-prerelease
107 - \c --enable-aztecoo
108 - \c --enable-amesos
109
110 \note
111 an example is given in the directory ml/examples/NonlinML
112
113 \author Michael Gee, SNL 9214
114 */
115 class NLNML_Preconditioner : public virtual Epetra_Operator,
116 public virtual NOX::Epetra::Interface::Preconditioner
117 {
118 public:
119
120 //! Constructs an NLNML_Preconditioner class with given parameters
121 /*!
122 Constructs a NLNML_Preconditioner class and sets parameters from a parameter list
123 This constructor does NOT actually create the Multilevel hierarchy and the
124 methods, this is done by a call to computePreconditioner.
125
126 The complete list of parameters is:
127 \code
128 Teuchos::ParameterList mlparams;
129 mlparams.set("nlnML output", 6 ); // ML-output-level (0-10)
130 mlparams.set("nlnML max levels", 10 ); // max. # levels (minimum = 2 !)
131 mlparams.set("nlnML coarse: max size", 5000 ); // the size ML stops generating coarser levels
132 mlparams.set("nlnML is linear preconditioner", false ); // linear or nonlinear preconditioner
133 mlparams.set("nlnML apply constraints", true ); // make application apply constraints
134 mlparams.set("nlnML is matrixfree", true ); // create Jacobian yourself or ask application for it
135 mlparams.set("nlnML finite difference fine level", true ); // create jacobian on fine grid only
136 mlparams.set("nlnML finite difference alpha", 1.0e-08 ); // finite differencing perturbation value
137 mlparams.set("nlnML finite difference beta", 1.0e-07 ); // finite differencing perturbation value
138 mlparams.set("nlnML finite difference centered", false ); // use centered or forward finite differencing
139 mlparams.set("nlnML Jacobian fix diagonal", true ); // check Jacobian for zero diagonal entries and fix them
140
141 mlparams.set("nlnML absolute residual tolerance", 1.0e-06 ); // internal tolerance for nonlinear preconditioner
142 mlparams.set("nlnML max cycles", 500 ); // max # cycles before we give up
143 mlparams.set("nlnML adaptive recompute", 0.0 ); // recompute if residual is larger then this value
144 mlparams.set("nlnML offset recompute", 0 ); // every offset this preconditioner is recomputed
145 mlparams.set("nlnML additional adaptive nullspace", 0 ); // compute adaptive nullspace (additional kernel vectors)
146 mlparams.set("nlnML PDE equations", 1 ); // dof per node
147 mlparams.set("nlnML null space: dimension", 1 ); // dimension of nullspace
148 mlparams.set("nlnML spatial dimension", 1 ); // spatial dimension of problem: 1, 2, 3
149 mlparams.set("nlnML coarse: type", "Uncoupled"); // Uncoupled METIS VBMETIS
150 mlparams.set("nlnML nodes per aggregate", 3 ); // # nodes per agg for coarsening METIS and VBMETIS
151
152 mlparams.set("nlnML use nlncg on fine level", true ); // use nlnCG or mod. Newton's method
153 mlparams.set("nlnML use nlncg on medium level", true );
154 mlparams.set("nlnML use nlncg on coarsest level", false );
155
156 mlparams.set("nlnML max iterations newton-krylov fine level", 0 ); // # iterations of lin. CG in mod. Newton's method
157 mlparams.set("nlnML max iterations newton-krylov medium level" , 0 );
158 mlparams.set("nlnML max iterations newton-krylov coarsest level", 5 );
159
160 mlparams.set("nlnML linear smoother type fine level", "MLS" ); // SGS BSGS Jacobi MLS Bcheby AmesosKLU
161 mlparams.set("nlnML linear smoother type medium level", "SGS" );
162 mlparams.set("nlnML linear smoother type coarsest level", "AmesosKLU");
163 mlparams.set("nlnML linear smoother sweeps fine level", -3 ); // number of sweeps or polynomial order
164 mlparams.set("nlnML linear smoother sweeps medium level", 3 );
165 mlparams.set("nlnML linear smoother sweeps coarsest level", 1 );
166
167 mlparams.set("nlnML nonlinear presmoothing sweeps fine level", 1 ); // # nonlinear presmoothing sweeps in FAS-cycle fine level
168 mlparams.set("nlnML nonlinear presmoothing sweeps medium level", 0 );
169 mlparams.set("nlnML nonlinear smoothing sweeps coarse level", 3 );
170 mlparams.set("nlnML nonlinear postsmoothing sweeps medium level", 2 );
171 mlparams.set("nlnML nonlinear postsmoothing sweeps fine level", 3 );
172 \endcode
173
174 \param interface (In) : rcptr to an NLNML::NLNML_FineLevelNoxInterface derived class
175 \param mlparams (In) : paramter list
176 \param comm (In) : the Epetra_Comm to be used
177 */
178 explicit NLNML_Preconditioner(
179 RefCountPtr<NLNML::NLNML_FineLevelNoxInterface> interface,
180 ParameterList& mlparams,
181 const Epetra_Comm& comm);
182
183 //! Destroys the preconditioner.
184 /*!
185 Destroys the NLNML_Preconditioner.
186 */
187 virtual ~NLNML_Preconditioner();
188
189 //! Register the outer Nox iterative method with the nonlinear preconditioner.
190 /*!
191 Register the outer Nox iterative method with the nonlinear preconditioner.
192 This is necessary, because the outer NOX iterative method will provide the
193 nonlinear preconditioner with current solution and residual. Necessary only
194 in the nonlinear preconditioner case and when used as a preconditioner. If used as
195 stand-alone solver, this is not necessary.
196 \param s (In) : rcptr to the NOX::Solver::Generic of the outer
197 Nox iterative method.
198 */
199 //JJH
200 //void SetNoxSolver(RefCountPtr<NOX::Solver::Manager> s) { noxsolver_ = s; }
201 void SetNoxSolver(RefCountPtr<NOX::Solver::Generic> s) { noxsolver_ = s; }
202
203 //! Returns the label of this class.
204 /*!
205 Returns the label of this class.
206 */
207 const char* Label() const {return(&(label_[0]));}
208
209 //! get Comm of this class
210 /*!
211 Derived from Epetra_Operator, returns ref to the Epetra_Comm of this class
212 */
213 const Epetra_Comm & Comm() const {return(comm_);}
214
215 //! Get fine level OperatorDomainMap.
216 /*!
217 Derived from Epetra_Operator, get fine level OperatorDomainMap.
218 */
219 const Epetra_Map & OperatorDomainMap() const {return interface_->getMap();}
220
221 //! Get fine level OperatorRangeMap.
222 /*!
223 Derived from Epetra_Operator, get fine level OperatorRangeMap.
224 */
225 const Epetra_Map & OperatorRangeMap() const {return interface_->getMap();}
226
227 //! Returns the level of output to be generated to stdout ( 0 - 10 )
228 inline int OutLevel() const { return params_->get("nlnML output",0); }
229
230 //! Check initialization flag
231 /*!
232 check the init-flag, if false, ApplyInverse will call computePreconditioner
233 */
234 inline bool isinit() const {return(isinit_);}
235
236 //! Set the initialization flag
237 bool setinit(bool flag)
238 {isinit_=flag; ncalls_NewPrec_ = 0; return(true);}
239
240 //! Get the offset to recompute this preconditioner.
241 int getoffset() const {return params_->get("nlnML offset recompute",0);}
242
243 // derived from NOX::Epetra::Interface::Preconditioner
244 //! Construct the preconditioner.
245 /*!
246 Construct the preconditioner. This is derived from the
247 NOX::Epetra::Interface::Preconditioner class. Nox will use this to
248 (re)compute the preconditioner.
249 \param x (In) : Vector of the current solution
250 \param M (In) : this
251 \param precParams (In) : ignored
252 */
253 virtual bool computePreconditioner(const Epetra_Vector& x,
254 Epetra_Operator& M,
255 Teuchos::ParameterList* precParams = 0);
256
257 //! ApplyInverse the preconditioner
258 /*!
259 ApplyInverse the preconditioner. Method is derived from Epetra_Operator.
260 \param X (In) : Epetra_MultiVector matching the fine level map of this
261 preconditioner
262 \param Y (Out) : Epetra_MultiVector containing the result on output
263 */
264 int ApplyInverse(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const ;
265
266 //! Apply the nonlinear multigrid FAS-solver.
267 /*!
268 Apply the nonlinear multigrid FAS-solver.
269 This preconditioner can act as a stand-alone FAS-Solver. Will throw an error,
270 if the preconditioner is set up as a linear preconditioner
271 */
272 int solve() const ;
273
274 //! Apply the preconditioner, not implemented.
275 /*!
276 Derived from Epetra_Operator, not implemented.
277 */
278 int Apply(const Epetra_MultiVector& X, Epetra_MultiVector& Y) const
279 { std::cout << "ML_Nox_Preconditioner::Apply does not make sense for an ML-Multilevel Operator\n"
280 << "Use ApplyInverse\n";return(-1);}
281
282 //! SetUseTranspose, not implemented.
283 /*!
284 Derived from Epetra_Operator, not implemented.
285 */
286 int SetUseTranspose(bool UseTranspose) { return -1; }
287
288 //! NormInf, not implemented.
289 /*!
290 Derived from Epetra_Operator, not implemented.
291 */
292 double NormInf() const {return(-1.0);}
293
294 //! UseTranspose, not implemented.
295 /*!
296 Derived from Epetra_Operator, not implemented.
297 */
298 bool UseTranspose() const {return false;}
299
300 //! HasNormInf, not implemented.
301 /*!
302 Derived from Epetra_Operator, always returns false.
303 */
304 bool HasNormInf() const {return(false);}
305
306 private:
307
308 explicit NLNML_Preconditioner(NLNML::NLNML_Preconditioner& old);
309 NLNML_Preconditioner operator = (const NLNML_Preconditioner& old);
310
311 void Initialize();
312
313 bool compPrec(const Epetra_Vector& x);
314 bool Compute_Linearpreconditioner(const Epetra_Vector& x);
315 bool Compute_Nonlinearpreconditioner(const Epetra_Vector& x);
316
317 void Set_Smoothers();
318 void fix_MainDiagonal(RefCountPtr<Epetra_CrsMatrix> A, int level);
319 RefCountPtr<Epetra_CrsMatrix> ComputeFineLevelJacobian(const Epetra_Vector& x);
320
321 int ApplyInverse_Linear(const Epetra_MultiVector& X,
322 Epetra_MultiVector& Y) const;
323 int ApplyInverse_NonLinear(const Epetra_MultiVector& X,
324 Epetra_MultiVector& Y) const;
325
326 //the FAS-preconditioner
327 bool FAS_cycle(Epetra_Vector* f, Epetra_Vector* x, int level, bool* converged, double* finenorm) const;
328 // the FAS-solver
329 bool FAS_cycle_solver() const;
330
331 bool Adaptivesetup(double** oldns,Epetra_CrsMatrix* Jac,int oldnumpde,int olddimns);
332
333 template<typename kind>
334 kind getParameter(string name,kind dvalue) const
335 { return params_->get(name,dvalue); }
336
337 template<typename kind>
338 void setParameter(string name,kind dvalue) const
339 { params_->set(name,dvalue); return; }
340
341 bool CheckInputParameters(ParameterList& params);
342
343 private:
344
345 const char* label_;
346 bool isinit_;
347 int ncalls_NewPrec_;
348
349 const Epetra_Comm& comm_;
350 RefCountPtr<NLNML::NLNML_FineLevelNoxInterface> interface_;
351 RefCountPtr<Teuchos::ParameterList> params_;
352 //JJH
353 //RefCountPtr<NOX::Solver::Manager> noxsolver_;
354 RCP<NOX::Solver::Generic> noxsolver_;
355 RefCountPtr<Epetra_CrsMatrix> fineJac_;
356 RefCountPtr<Epetra_Map> rowmap_;
357 RefCountPtr<ML_Epetra::MultiLevelOperator> linPrec_;
358 RefCountPtr<std::vector<RefCountPtr<NLNML::NLNML_NonlinearLevel> > > nlnlevel_;
359 ML* ml_;
360 ML_Aggregate* ag_;
361
362
363 }; // class NLNML_Preconditioner
364 } // namespace NLNML
365
366 #endif
367 #endif