Debian Codegraph @

packages/trilinoscouplings/src/ml/NonlinML/nlnml_coarselevelnoxinterface.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 "nlnml_coarselevelnoxinterface.H"
24
25 using namespace std;
26
27 /*----------------------------------------------------------------------*
28 | ctor (public) m.gee 3/06|
29 *----------------------------------------------------------------------*/
30 NLNML::NLNML_CoarseLevelNoxInterface::NLNML_CoarseLevelNoxInterface(
31 RefCountPtr<NLNML::NLNML_FineLevelNoxInterface> finterface,
32 int level,int outlevel,
33 RefCountPtr< vector< RefCountPtr<Epetra_CrsMatrix> > > P,
34 const Epetra_BlockMap& this_bmap,
35 int maxlevel) :
36 level_(level),
37 maxlevel_(maxlevel),
38 outputlevel_(outlevel),
39 isFASmodfied_(false),
40 nFcalls_(0),
41 fineinterface_(finterface),
42 P_(P),
43 fbar_(NULL),
44 fxbar_(NULL)
45 {
46 this_bmap_ = rcp(new Epetra_BlockMap(this_bmap));
47
48 // create a series of working vectors to be used on prolongation/restriction
49 wvec_.clear();
50 if (Level())
51 {
52 wvec_.resize(Level()+1);
53 for (int i=0; i<Level(); ++i)
54 wvec_[i] = rcp(new Epetra_Vector((*P)[i+1]->OperatorRangeMap(),false));
55 wvec_[Level()] = rcp(new Epetra_Vector((*P)[Level()]->OperatorDomainMap(),false));
56 }
57
58
59
60 return;
61 }
62
63
64 /*----------------------------------------------------------------------*
65 | ctor (public) m.gee 3/06|
66 *----------------------------------------------------------------------*/
67 void NLNML::NLNML_CoarseLevelNoxInterface::recreate()
68 {
69
70 return;
71 }
72
73
74 /*----------------------------------------------------------------------*
75 | dtor (public) m.gee 3/06|
76 *----------------------------------------------------------------------*/
77 NLNML::NLNML_CoarseLevelNoxInterface::~NLNML_CoarseLevelNoxInterface()
78 {
79 return;
80 }
81
82
83 /*----------------------------------------------------------------------*
84 | evaluate nonlinear function (public, derived) m.gee 3/06|
85 *----------------------------------------------------------------------*/
86 bool NLNML::NLNML_CoarseLevelNoxInterface::computeF(
87 const Epetra_Vector& x, Epetra_Vector& F,
88 const FillType fillFlag)
89 {
90 bool err;
91 if (!Level())
92 {
93 err = fineinterface_->computeF(x,F,fillFlag);
94 if (err==false)
95 {
96 cout << "**ERR**: NLNML::NLNML_CoarseLevelNoxInterface::computeF:\n"
97 << "**ERR**: call to fine-userinterface returned false on level " << level_ << "\n"
98 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
99 }
100 }
101 else
102 {
103 RefCountPtr<Epetra_Vector> Ffine = rcp(new Epetra_Vector(fineinterface_->getGraph()->RowMap(),false));
104 RefCountPtr<Epetra_Vector> xfine = rcp(prolong_this_to_fine(x));
105 err = fineinterface_->computeF(*xfine,*Ffine,fillFlag);
106 if (err==false)
107 {
108 cout << "**ERR**: NLNML::NLNML_CoarseLevelNoxInterface::computeF:\n"
109 << "**ERR**: call to fine-userinterface returned false on level " << level_ << "\n"
110 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
111 }
112 RefCountPtr<Epetra_Vector> Fcoarse = rcp(restrict_fine_to_this(*Ffine));
113 F.Scale(1.0,*Fcoarse);
114 }
115 if (isFAS())
116 F.Update(-1.0,*fxbar_,1.0,*fbar_,1.0);
117 return err;
118 }
119
120 /*----------------------------------------------------------------------*
121 | restrict from fine to this level (public) m.gee 3/06|
122 *----------------------------------------------------------------------*/
123 Epetra_Vector* NLNML::NLNML_CoarseLevelNoxInterface::restrict_fine_to_this(
124 const Epetra_Vector& xfine)
125 {
126 if (!Level())
127 {
128 Epetra_Vector* xcoarse = new Epetra_Vector(xfine);
129 return xcoarse;
130 }
131 else
132 {
133 // Note that the GIDs in xfine match those of the fineinterface and
134 // might be different from those in P_[1]->OperatorRangeMap().
135 // The LIDs and the map match, so we have to copy xfine to xfineP
136 // using LIDs
137 Epetra_Vector* xfineP = wvec_[0].get(); // RangeMap of P_[1]
138 if (xfine.MyLength() != xfineP->MyLength() || xfine.GlobalLength() != xfineP->GlobalLength())
139 {
140 cout << "**ERR**: NLNML::NLNML_CoarseLevelNoxInterface::restrict_fine_to_this:\n"
141 << "**ERR**: mismatch in dimension of xfine and xfineP\n"
142 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
143 }
144 const int mylength = xfine.MyLength();
145 for (int i=0; i<mylength; i++)
146 (*xfineP)[i] = xfine[i];
147
148 // loop from the finest level to this level and
149 // apply series of restrictions (that is transposed prolongations)
150 Epetra_Vector* fvec = xfineP;
151 for (int i=0; i<Level()-1; i++)
152 {
153 Epetra_Vector* cvec = wvec_[i+1].get();
154 (*P_)[i+1]->Multiply(true,*fvec,*cvec);
155 fvec = cvec;
156 }
157 Epetra_Vector* out = new Epetra_Vector((*P_)[Level()]->OperatorDomainMap(),false);
158 (*P_)[Level()]->Multiply(true,*fvec,*out);
159 return out;
160 }
161 }
162
163 /*----------------------------------------------------------------------*
164 | prolongate from this level to fine (public) m.gee 3/06|
165 *----------------------------------------------------------------------*/
166 Epetra_Vector* NLNML::NLNML_CoarseLevelNoxInterface::prolong_this_to_fine(
167 const Epetra_Vector& xcoarse)
168 {
169 if (!Level())
170 return new Epetra_Vector(xcoarse);
171 else
172 {
173 Epetra_Vector* cvec = const_cast<Epetra_Vector*>(&xcoarse);
174 for (int i=Level(); i>0; i--)
175 {
176 // allocate a vector matching level i-1
177 Epetra_Vector* fvec = wvec_[i-1].get();
178 // multiply
179 (*P_)[i]->Multiply(false,*cvec,*fvec);
180 cvec = fvec;
181 }
182 // Note that the GIDs in cvec do NOT match those of the fineinterface as
183 // they match the P_[1]->RangeMap.
184 // The LIDs match, so we have to copy cvec to xfine_fineinterface
185 // using LIDs
186 Epetra_Vector* xfine_fineinterface = new Epetra_Vector(fineinterface_->getGraph()->RowMap(),false);
187 if (cvec->MyLength() != xfine_fineinterface->MyLength() ||
188 cvec->GlobalLength() != xfine_fineinterface->GlobalLength())
189 {
190 cout << "**ERR**: NLNML::NLNML_CoarseLevelNoxInterface::prolong_this_to_fine:\n"
191 << "**ERR**: mismatch in dimension of cvec and xfine_fineinterface\n"
192 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
193 }
194 const int mylength = cvec->MyLength();
195 for (int i=0; i<mylength; i++)
196 (*xfine_fineinterface)[i] = (*cvec)[i];
197 return xfine_fineinterface;
198 }
199 }
200
201 /*----------------------------------------------------------------------*
202 | restrict from this to next coarser level (public) m.gee 3/06|
203 *----------------------------------------------------------------------*/
204 Epetra_Vector* NLNML::NLNML_CoarseLevelNoxInterface::restrict_to_next_coarser_level(
205 Epetra_Vector* thisvec,
206 int current, int next)
207 {
208 Epetra_Vector* xfineP = 0;
209 if (current==0)
210 {
211 xfineP = new Epetra_Vector((*P_)[1]->RowMap(),false);
212 if (thisvec->MyLength() != xfineP->MyLength() || thisvec->GlobalLength() != xfineP->GlobalLength())
213 {
214 cout << "**ERR**: NLNML::NLNML_CoarseLevelNoxInterface::restrict_to_next_coarser_level:\n"
215 << "**ERR**: mismatch in dimension of thisvec and xfineP\n"
216 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
217 }
218 const int mylength = thisvec->MyLength();
219 for (int i=0; i<mylength; i++)
220 (*xfineP)[i] = (*thisvec)[i];
221 }
222 else
223 {
224 xfineP = thisvec;
225 }
226 Epetra_Vector* cvec = new Epetra_Vector((*P_)[next]->OperatorDomainMap(),false);
227 (*P_)[next]->Multiply(true,*xfineP,*cvec);
228 if (current==0) delete xfineP;
229 return cvec;
230 }
231
232
233 /*----------------------------------------------------------------------*
234 | prolongate from next coarser level to this level (public) m.gee 3/06|
235 *----------------------------------------------------------------------*/
236 Epetra_Vector* NLNML::NLNML_CoarseLevelNoxInterface::prolong_to_this_level(
237 Epetra_Vector* coarsevec,
238 int current, int next)
239 {
240 Epetra_Vector* fvec = new Epetra_Vector((*P_)[next]->OperatorRangeMap(),false);
241 (*P_)[next]->Multiply(false,*coarsevec,*fvec);
242 if (current==0)
243 {
244 Epetra_Vector* fine = new Epetra_Vector(fineinterface_->getMap(),false);
245 if (fvec->MyLength() != fine->MyLength() || fvec->GlobalLength() != fine->GlobalLength())
246 {
247 cout << "**ERR**: NLNML::NLNML_CoarseLevelNoxInterface::prolong_to_this_level:\n"
248 << "**ERR**: mismatch in dimension of fvec and fine\n"
249 << "**ERR**: file/line: " << __FILE__ << "/" << __LINE__ << "\n"; throw -1;
250 }
251 const int mylength = fvec->MyLength();
252 for (int i=0; i<mylength; ++i)
253 (*fine)[i] = (*fvec)[i];
254 delete fvec;
255 return fine;
256 }
257 else
258 return fvec;
259 }
260
261 /*----------------------------------------------------------------------*
262 | set ptr to all prolongation operators (public) m.gee 3/06|
263 *----------------------------------------------------------------------*/
264 void NLNML::NLNML_CoarseLevelNoxInterface::setP()
265 {
266 return;
267 }
268
269
270 /*----------------------------------------------------------------------*
271 | make application apply all constraints (public) m.gee 3/06|
272 *----------------------------------------------------------------------*/
273 void NLNML::NLNML_CoarseLevelNoxInterface::ApplyAllConstraints(
274 Epetra_Vector& gradient)
275 {
276 if (!Level())
277 fineinterface_->ApplyAllConstraints(gradient,0);
278 else
279 {
280 RefCountPtr<Epetra_Vector> gradientfine = rcp(prolong_this_to_fine(gradient));
281 fineinterface_->ApplyAllConstraints(*gradientfine,Level());
282 RefCountPtr<Epetra_Vector> gradientcoarse = rcp(restrict_fine_to_this(*gradientfine));
283 gradient.Scale(1.0,*gradientcoarse);
284 }
285 return;
286 }
287
288
289 /*----------------------------------------------------------------------*
290 | return this levels blockmap (public) m.gee 3/06|
291 *----------------------------------------------------------------------*/
292 void NLNML::NLNML_CoarseLevelNoxInterface::BlockMap()
293 {
294 return;
295 }
296
297
298
299
300
301
302
303
304
305 #endif