Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/ml_nox_coarseinterface.cpp

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 // ML-headers
15 #include "ml_common.h"
16 #include "TrilinosCouplings_config.h"
17
18 #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)
19
20 // ---------- Includes ----------
21 #include <ctime>
22 #include <iostream>
23 #include "ml_nox_coarseinterface.H"
24
25 /*----------------------------------------------------------------------*
26 | ctor (public) m.gee 12/04|
27 *----------------------------------------------------------------------*/
28 ML_NOX::Nox_CoarseProblem_Interface::Nox_CoarseProblem_Interface(
29 ML_NOX::Ml_Nox_Fineinterface& interface,
30 int level,
31 int plevel,
32 Epetra_CrsMatrix** P,
33 const Epetra_BlockMap* this_RowMap,
34 int nlevel)
35 : fineinterface_(interface)
36 {
37 if (P==0)
38 {
39 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::Nox_CoarseProblem_Interface:\n"
40 << "**ERR**: Epetra_CrsMatrix** P is 0 in constructor on level " << level_ << "\n"
41 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
42 }
43 int i;
44 for (i=1; i<=level; i++)
45 {
46 if (P[i]==0)
47 {
48 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::Nox_CoarseProblem_Interface:\n"
49 << "**ERR**: ptr to Prolongator level " << i << " is 0\n"
50 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
51 }
52 }
53 if (!this_RowMap)
54 {
55 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::Nox_CoarseProblem_Interface:\n"
56 << "**ERR**: Epetra_BlockMap* this_RowMap is 0 in constructor on level " << level_ << "\n"
57 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
58 }
59
60 // store some data
61 level_ = level; // my level (ML_INCREASING only!!!)
62 nlevel_ = nlevel; // total number of levels
63 ml_printlevel_ = plevel; // set printing level
64 nFcalls_ = 0; // number of cals to the computeF function
65 P_ = P; // ptr to the vector of prolongators
66 fbar_ = 0; // the ptr to one FAS-vector
67 fxbar_ = 0; // the ptr to one FAS-vector
68 isFASmodfied_ = false;
69 this_RowMap_ = new Epetra_BlockMap(*this_RowMap);
70
71 // create a series of working vectors to use in prolongations and restrictions
72 if (level)
73 {
74 wvec_.resize(level+1);
75 for (int i=0; i<level; ++i)
76 wvec_[i] = new Epetra_Vector(P_[i+1]->OperatorRangeMap(),false);
77 wvec_[level] = new Epetra_Vector(P_[level]->OperatorDomainMap(),false);
78 }
79 else
80 wvec_.clear();
81
82 return;
83 }
84
85 /*----------------------------------------------------------------------*
86 | recreate the coarse interface (public) m.gee 12/04|
87 *----------------------------------------------------------------------*/
88 bool ML_NOX::Nox_CoarseProblem_Interface::recreate(int plevel,Epetra_CrsMatrix** P,
89 const Epetra_BlockMap* this_RowMap)
90 {
91 if (P==0)
92 {
93 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::Nox_CoarseProblem_Interface:\n";
94 cout << "**ERR**: Epetra_CrsMatrix** P is 0 in constructor on level " << level_ << "\n";
95 cout << "**ERR**: file/line: " << __FILE__ << "(" << __LINE__ << ")\n"; throw -1;
96 }
97 int i;
98 for (i=1; i<=level_; i++)
99 {
100 if (P[i]==0)
101 {
102 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::Nox_CoarseProblem_Interface:\n"
103 << "**ERR**: ptr to Prolongator level " << i << " is 0\n"
104 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
105 }
106 }
107 // store some data
108 ml_printlevel_ = plevel; // set printing level
109 nFcalls_ = 0; // number of cals to the computeF function
110 P_ = P; // the new prolongators
111 if (!this_RowMap)
112 {
113 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::recreate:\n"
114 << "**ERR**: Epetra_BlockMap* this_RowMap is 0 on level " << level_ << "\n"
115 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
116 }
117 if (this_RowMap_) delete this_RowMap_;
118 this_RowMap_ = new Epetra_BlockMap(*this_RowMap);
119
120 // create a series of working vectors to use in prolongations and restrictions
121 if (level_)
122 {
123 // delete the old working vectors
124 for (int i=0; i<(int)wvec_.size(); ++i)
125 if (wvec_[i]) delete wvec_[i];
126 // create new ones
127 wvec_.resize(level_+1);
128 for (int i=0; i<level_; ++i)
129 wvec_[i] = new Epetra_Vector(P_[i+1]->OperatorRangeMap(),false);
130 wvec_[level_] = new Epetra_Vector(P_[level_]->OperatorDomainMap(),false);
131 }
132 else
133 wvec_.clear();
134
135 return true;
136 }
137
138 /*----------------------------------------------------------------------*
139 | dtor (public) m.gee 12/04|
140 *----------------------------------------------------------------------*/
141 ML_NOX::Nox_CoarseProblem_Interface::~Nox_CoarseProblem_Interface()
142 {
143 // destroy the P-hierarchy, if it still exists
144 destroyP();
145 if (this_RowMap_) delete this_RowMap_;
146 this_RowMap_ = 0;
147
148 for (int i=0; i<(int)wvec_.size(); ++i)
149 {
150 delete wvec_[i];
151 wvec_[i] = NULL;
152 }
153 wvec_.clear();
154
155 return;
156 }
157
158 /*----------------------------------------------------------------------*
159 | restrict a vector from level current to m.gee 01/05|
160 | next coarser level next |
161 | returns ptr to coarse Epetra_Vector |
162 | NOTE: the calling routine is responsible for deleteing the |
163 | returned Epetra_Vector |
164 *----------------------------------------------------------------------*/
165 Epetra_Vector* ML_NOX::Nox_CoarseProblem_Interface::restrict_to_next_coarser_level(
166 Epetra_Vector* thisvec,
167 int current, int next)
168 {
169 // create a thisvec that matches the map of the restriction operator
170 Epetra_Vector* xfineP = 0;
171 if (current==0) // restrict from level 0 to level 1 is different from rest
172 {
173 // Note that the GIDs in xfine match those of the fineinterface and
174 // might be different from those in P_[1]->RowMap.
175 // The LIDs and the map match, so we have to copy xfine to xfineP
176 // using LIDs
177 xfineP = new Epetra_Vector(P_[1]->RowMap(),false);
178 if (thisvec->MyLength() != xfineP->MyLength() || thisvec->GlobalLength() != xfineP->GlobalLength())
179 {
180 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::restrict_to_next_coarser_level:\n"
181 << "**ERR**: mismatch in dimension of thisvec and xfineP\n"
182 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
183 }
184 const int mylength = thisvec->MyLength();
185 for (int i=0; i<mylength; i++)
186 (*xfineP)[i] = (*thisvec)[i];
187 }
188 else
189 {
190 xfineP = thisvec;
191 }
192
193 // Allocate matching coarse vector
194 Epetra_Vector* cvec = new Epetra_Vector(P_[next]->OperatorDomainMap(),false);
195
196 // restrict (transposed mutliply with prolongation)
197 P_[next]->Multiply(true,*xfineP,*cvec);
198
199 if (current==0)
200 delete xfineP;
201
202 return cvec;
203 }
204
205 /*----------------------------------------------------------------------*
206 | prolong a vector from level current to m.gee 01/05|
207 | next coarser level next |
208 | returns ptr to coarse Epetra_Vector |
209 | NOTE: the calling routine is responsible for deleteing the |
210 | returned Epetra_Vector |
211 *----------------------------------------------------------------------*/
212 Epetra_Vector* ML_NOX::Nox_CoarseProblem_Interface::prolong_to_this_level(Epetra_Vector* coarsevec,
213 int current, int next)
214 {
215 // Allocate matching fine vector in ML-global indices
216 Epetra_Vector* fvec = new Epetra_Vector(P_[next]->OperatorRangeMap(),false);
217
218 // prolongate
219 P_[next]->Multiply(false,*coarsevec,*fvec);
220
221 // when prolongating to the finest level, global indexing there is different
222 if (current==0)
223 {
224 Epetra_Vector* fine = new Epetra_Vector(fineinterface_.getMap(),false);
225 if (fvec->MyLength() != fine->MyLength() || fvec->GlobalLength() != fine->GlobalLength())
226 {
227 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::restrict_to_next_coarser_level:\n"
228 << "**ERR**: mismatch in dimension of fvec and fine\n"
229 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
230 }
231 const int mylength = fvec->MyLength();
232 for (int i=0; i<mylength; i++)
233 (*fine)[i] = (*fvec)[i];
234 delete fvec; fvec = 0;
235 return fine;
236 }
237 else
238 return fvec;
239
240 return NULL;
241 }
242
243 /*----------------------------------------------------------------------*
244 | restrict a fine level vector to this level m.gee 12/04|
245 | NOTE: the calling routine is responsible for deleteing the |
246 | returned Epetra_Vector |
247 *----------------------------------------------------------------------*/
248 Epetra_Vector* ML_NOX::Nox_CoarseProblem_Interface::restrict_fine_to_this(
249 const Epetra_Vector& xfine)
250 {
251 int i;
252 // on the finest level, just copy the vector
253 if (level_==0)
254 {
255 Epetra_Vector* xcoarse = new Epetra_Vector(xfine);
256 return xcoarse;
257 }
258 else // not on finest level
259 {
260 // Note that the GIDs in xfine match those of the fineinterface and
261 // might be different from those in P_[1]->OperatorRangeMap().
262 // The LIDs and the map match, so we have to copy xfine to xfineP
263 // using LIDs
264 Epetra_Vector* xfineP = wvec_[0]; // RangeMap of P_[1]
265 if (xfine.MyLength() != xfineP->MyLength() || xfine.GlobalLength() != xfineP->GlobalLength())
266 {
267 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::restrict_fine_to_this:\n"
268 << "**ERR**: mismatch in dimension of xfine and xfineP\n"
269 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
270 }
271
272 const int mylength = xfine.MyLength();
273 for (i=0; i<mylength; i++)
274 (*xfineP)[i] = xfine[i];
275
276 // loop from the finest level to this level and
277 // apply series of restrictions (that is transposed prolongations)
278 Epetra_Vector* fvec = xfineP;
279 for (i=0; i<level_-1; i++)
280 {
281 Epetra_Vector* cvec = wvec_[i+1];
282 P_[i+1]->Multiply(true,*fvec,*cvec);
283 fvec = cvec;
284 }
285 Epetra_Vector* out = new Epetra_Vector(P_[level_]->OperatorDomainMap(),false);
286 P_[level_]->Multiply(true,*fvec,*out);
287 return out;
288 }
289 return NULL;
290 }
291
292 /*----------------------------------------------------------------------*
293 | prolong a this coarse level vector to the finest level m.gee 12/04|
294 | NOTE: the calling routine is responsible of deleteing the |
295 | returned Epetra_Vector |
296 *----------------------------------------------------------------------*/
297 Epetra_Vector* ML_NOX::Nox_CoarseProblem_Interface::prolong_this_to_fine(
298 const Epetra_Vector& xcoarse)
299 {
300 int i;
301 // on the finest level, just copy the vector
302 if (level_==0)
303 {
304 Epetra_Vector* xfine = new Epetra_Vector(xcoarse);
305 return xfine;
306 }
307 else // we are not on the finest level
308 {
309 // loop from this coarsest level to the finest one
310 // apply series of prolongations
311 Epetra_Vector* cvec = const_cast<Epetra_Vector*>(&xcoarse);
312 for (i=level_; i>0; i--)
313 {
314 // allocate a vector matching level i-1
315 Epetra_Vector* fvec = wvec_[i-1];
316 // multiply
317 P_[i]->Multiply(false,*cvec,*fvec);
318 cvec = fvec;
319 }
320 // Note that the GIDs in cvec do NOT match those of the fineinterface as
321 // they match the P_[1]->RangeMap.
322 // The LIDs match, so we have to copy cvec to xfine_fineinterface
323 // using LIDs
324 Epetra_Vector* xfine_fineinterface = new Epetra_Vector(fineinterface_.getGraph()->RowMap(),false);
325 if (cvec->MyLength() != xfine_fineinterface->MyLength() ||
326 cvec->GlobalLength() != xfine_fineinterface->GlobalLength())
327 {
328 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::prolong_this_to_fine:\n"
329 << "**ERR**: mismatch in dimension of cvec and xfine_fineinterface\n"
330 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
331 }
332 const int mylength = cvec->MyLength();
333 for (i=0; i<mylength; i++)
334 (*xfine_fineinterface)[i] = (*cvec)[i];
335 return xfine_fineinterface;
336 }
337 return NULL;
338 }
339
340 /*----------------------------------------------------------------------*
341 | evaluate nonlinear function (public, derived) m.gee 12/04|
342 *----------------------------------------------------------------------*/
343 bool ML_NOX::Nox_CoarseProblem_Interface::computeF(const Epetra_Vector& x,
344 Epetra_Vector& F,
345 const FillType fillFlag)
346 {
347 bool err;
348 //int ierr;
349 ++nFcalls_;
350
351 if (level_==0)
352 {
353 // call fine level interface
354 // in case the fillFlag is residual, sierra will not enforce constraints on it
355 // As we want (need) them enforced all the time in the preconditioner
356 // we switch the type to Prec
357 // We leave all other types unchanged as sierra does certain things on other types
358 // which we still want to happen
359
360 FillType type = fillFlag;
361 //if (fillFlag == NOX::EpetraNew::Interface::Required::Residual)
362 //type = NOX::EpetraNew::Interface::Required::Prec;
363 err = fineinterface_.computeF(x,F,type);
364 if (err==false)
365 {
366 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::computeF:\n"
367 << "**ERR**: call to fine-userinterface returned false on level " << level_ << "\n"
368 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
369 }
370 }
371 else // level_ > 0
372 {
373 // create Ffine and xfine matching the fine interface
374 Epetra_Vector* Ffine = new Epetra_Vector(fineinterface_.getGraph()->RowMap(),false);
375 Epetra_Vector* xfine = prolong_this_to_fine(x);
376
377 // call fine level interface
378 // in case the fillFlag is residual, sierra will not enforce constraints on it
379 // As we want (need) them enforced all the time in the preconditioner
380 // we switch the type to Prec
381 // We leave all other types unchanged as sierra does certain things on other types
382 // which we still want to happen
383 FillType type = fillFlag;
384 //if (fillFlag == NOX::EpetraNew::Interface::Required::Residual)
385 //type = NOX::EpetraNew::Interface::Required::Prec;
386 err = fineinterface_.computeF(*xfine,*Ffine,type);
387 if (err==false)
388 {
389 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::computeF:\n"
390 << "**ERR**: call to fine-userinterface returned false on level " << level_ << "\n"
391 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
392 }
393 delete xfine;
394
395 // get the answer Ffine back to this coarse level
396 Epetra_Vector* Fcoarse = restrict_fine_to_this(*Ffine);
397 delete Ffine;
398
399 F.Scale(1.0,*Fcoarse);
400 delete Fcoarse;
401 } // level_ > 0
402
403 // check for FAS option
404 // solving for modified system: F_c ( x_c ) - F_c ( R x_f ) + R F_f ( x_f ) -> 0
405 if (isFAS()==true)
406 F.Update(-1.0,*fxbar_,1.0,*fbar_,1.0);
407
408 return err;
409 }
410
411 /*----------------------------------------------------------------------*
412 | apply constraints (public) m.gee 11/05|
413 *----------------------------------------------------------------------*/
414 void ML_NOX::Nox_CoarseProblem_Interface::ApplyAllConstraints(Epetra_Vector& gradient)
415 {
416 if (level_==0)
417 {
418 // cout << "Nox_CoarseProblem_Interface::ApplyAllConstraints: fine \n"; fflush(stdout);
419 fineinterface_.ApplyAllConstraints(gradient,0);
420 return;
421 }
422 else
423 {
424 // cout << "Nox_CoarseProblem_Interface::ApplyAllConstraints: coarse \n"; fflush(stdout);
425 Epetra_Vector* gradientfine = prolong_this_to_fine(gradient);
426 fineinterface_.ApplyAllConstraints(*gradientfine,Level());
427 Epetra_Vector* gradientcoarse = restrict_fine_to_this(*gradientfine);
428 delete gradientfine;
429 gradient.Scale(1.0,*gradientcoarse);
430 delete gradientcoarse;
431 return;
432 }
433 return;
434 }
435 /*----------------------------------------------------------------------*
436 | evaluate Jacobian (public, derived) m.gee 12/04|
437 *----------------------------------------------------------------------*/
438 bool ML_NOX::Nox_CoarseProblem_Interface::computeJacobian(const Epetra_Vector& x)
439 {
440 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::computeJacobian(...):\n"
441 << "**ERR**: this is NOT supposed to be called????????\n"
442 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
443 return true;
444 }
445
446 /*----------------------------------------------------------------------*
447 | compute user supplied preconditioner (public, derived) m.gee 12/04|
448 *----------------------------------------------------------------------*/
449 bool ML_NOX::Nox_CoarseProblem_Interface::computePreconditioner(
450 const Epetra_Vector& x,
451 NOX::Parameter::List* precParams)
452 {
453 cout << "**ERR**: ML_Epetra::Nox_CoarseProblem_Interface::computePreconditioner(...):\n"
454 << "**ERR**: this is NOT supposed to be called????????\n"
455 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
456 return(true);
457 }
458
459 //-----------------------------------------------------------------------------
460
461 #endif // defined(HAVE_ML_NOX) && defined(HAVE_ML_EPETRA)