Debian Codegraph @

packages/nox/src-loca/src-epetra/continuation-manager/ContinuationManager.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // LOCA: Library of Continuation Algorithms Package
4 //
5 // Copyright 2001-2005 NTESS and the LOCA contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "ContinuationManager.H"
11
12 // Trilinos headers
13 #include "LOCA_GlobalData.H"
14
15 // DefCont headers
16 #include "LOCAInterface.H"
17 //#include "PhaseConstraint.h"
18 //#include "IOVtkUtils.h"
19 #include "IOContFileUtils.H"
20 #ifdef HAVE_NOX_AMESOS
21 #include "NOX_Epetra_LinearSystem_Amesos.H"
22 #endif
23
24 #include <sstream>
25
26 ContinuationManager::
27 ContinuationManager(
28 //const Teuchos::RCP< Epetra_MpiComm > & aComm ,
29 const Teuchos::RCP< Epetra_Comm > & aComm ,
30 const std::string & taskFileName):
31 comm(aComm),
32 continuationFileName("continuation.dat"),
33 iniStepLabel(0),
34 interfaceConstraint(Teuchos::null),
35 //isConstrainedProblem(false),
36 locaGlobalData(Teuchos::null),
37 locaStepper(Teuchos::null),
38 locaStepperStatus(LOCA::Abstract::Iterator::NotFinished),
39 maxAllowedSteps(100000),
40 outputDir("."),
41 problem(Teuchos::null),
42 solutionFilesExtension("vtk"),
43 solutionFilesPrefix("step_"),
44 taskList(Teuchos::rcp (new Teuchos::ParameterList())),
45 timeCounter(Epetra_Time(*aComm))
46 {
47 // Reading the parameter task from the file
48 Teuchos::updateParametersFromXmlFile(taskFileName, taskList.ptr());
49
50 if (comm->MyPID()==0) {
51 std::cout << std::endl << "#### Task Parameters from task file \"" <<
52 taskFileName << " ####" << std::endl;
53 taskList->print(std::cout,2,false,false);
54 std::cout << std::endl << "#### End Parameters from "<< taskFileName <<
55 " ####" << std::endl << std::endl;
56 }
57
58 // Getting the initial step label flag
59 iniStepLabel = taskList->sublist("Continuation Manager").
60 sublist("Continuation").
61 get<int>("Label From Step");
62
63 // // If the constraint sublist exists, check whether the constraint is enabled
64 // if ( taskList->sublist("Continuation Manager").isSublist("Constraint") )
65 // isConstrainedProblem = taskList->sublist("Continuation Manager").
66 // sublist("Constraint").
67 // get<bool>("Enable Constraint");
68
69 // Getting solution files prefix and extensions
70 if ( taskList->sublist("Continuation Manager").sublist("Continuation").
71 isParameter("Solution Files Prefix") )
72 solutionFilesPrefix = taskList->sublist("Continuation Manager").
73 sublist("Continuation").
74 get<std::string>("Solution Files Prefix");
75
76 if ( taskList->sublist("Continuation Manager").sublist("Continuation").
77 isParameter("Solution Files Extension") )
78 solutionFilesExtension = taskList->sublist("Continuation Manager").
79 sublist("Continuation").
80 get<std::string>("Solution Files Extension");
81
82 }
83
84 ContinuationManager::
85 ~ContinuationManager()
86 {
87 destroyGlobalData(locaGlobalData);
88 }
89
90 bool ContinuationManager::
91 BuildLOCAStepper()
92 {
93
94 if (comm->MyPID()==0) std::cout << std::endl << "Building the LOCA stepper..." << std::endl;
95
96 // Make sure the problem has been set
97 TEUCHOS_TEST_FOR_EXCEPTION( problem == Teuchos::null,
98 std::logic_error,
99 "ContinuationManager has not been given a valid ProblemLOCAPrototype");
100
101 // Create the interface between the test problem and the nonlinear solver
102 // This is created by the user using inheritance of the abstract base
103 // class
104 Teuchos::RCP <LOCAInterface> interface =
105 Teuchos::rcp(new LOCAInterface(problem,Teuchos::rcp(&*this,false)));
106 Teuchos::RCP <LOCA::Epetra::Interface::Required> interfaceRequired = interface;
107 Teuchos::RCP <NOX::Epetra::Interface::Jacobian> interfaceJacobian = interface;
108
109 // Create the Epetra_RowMatrix for the Jacobian/Preconditioner
110 Teuchos::RCP <Epetra_RowMatrix> jacobian = problem->GetJacF();
111
112 // Get the initial guess vector
113 Teuchos::RCP <Epetra_Vector> initialGuess =
114 problem->GetInitialGuess();
115
116 // NOX and LOCA sublist
117 Teuchos::RCP <Teuchos::ParameterList> noxAndLocaList =
118 Teuchos::rcp(new Teuchos::ParameterList(taskList->sublist("NOX and LOCA")));
119
120 // Create the lists needed for linear systems
121 Teuchos::ParameterList & noxPrintingList =
122 noxAndLocaList->
123 sublist("NOX").
124 sublist("Printing");
125 Teuchos::ParameterList & linearSystemList =
126 noxAndLocaList->
127 sublist("NOX").
128 sublist("Direction").
129 sublist("Newton").
130 sublist("Linear Solver");
131
132 // Instntiating the appropriate linear system
133 std::string linearSolverType = linearSystemList.get("Solver","Aztec");
134 Teuchos::RCP<NOX::Epetra::LinearSystem> linearSystem = Teuchos::null;
135
136 if (linearSolverType == "Aztec")
137 // Create the linear system
138 //Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linearSystem =
139 linearSystem =
140 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(noxPrintingList,
141 linearSystemList,
142 interfaceRequired,
143 interfaceJacobian,
144 jacobian,
145 *initialGuess));
146 #ifdef HAVE_NOX_AMESOS
147 else if (linearSolverType == "Amesos")
148 // Create the linear system
149 //Teuchos::RCP<NOX::Epetra::LinearSystemAmesos> linearSystem =
150 linearSystem =
151 Teuchos::rcp(new NOX::Epetra::LinearSystemAmesos(noxPrintingList,
152 linearSystemList,
153 interfaceRequired,
154 interfaceJacobian,
155 jacobian,
156 *initialGuess));
157 #endif
158 else
159 {
160 TEUCHOS_TEST_FOR_EXCEPTION(true,
161 std::logic_error, "Continuation manager: " +
162 linearSolverType + " is not a valid linear solver");
163 }
164
165 // Get the parameter vector
166 LOCA::ParameterVector continuableParams = problem->GetContinuableParams();
167
168 // Create Epetra factory
169 Teuchos::RCP <LOCA::Abstract::Factory> epetraFactory =
170 Teuchos::rcp(new LOCA::Epetra::Factory);
171
172 // Create the loca vector
173 Teuchos::RCP <NOX::Epetra::Vector> locaInitialGuess =
174 Teuchos::rcp (new NOX::Epetra::Vector(*initialGuess));
175
176 // Instantiate the constraint objects
177 //if (isConstrainedProblem) {
178 if (interfaceConstraint != Teuchos::null) {
179
180 // // Instantiate the constraint
181 // Teuchos::RCP <PhaseConstraint> phaseConstraint =
182 // Teuchos::rcp(new PhaseConstraint(problem,*locaInitialGuess));
183 //
184 // // Instantiate the interface
185 // Teuchos::RCP <LOCA::MultiContinuation::ConstraintInterface>
186 // interfaceConstraint = phaseConstraint;
187
188 // Instantiate the constraint parameters names
189 Teuchos::RCP< std::vector<std::string> > constraintParamsNames =
190 Teuchos::rcp(new std::vector<std::string>());
191
192 // The user-defined constrained parameters
193 Teuchos::ParameterList & constraintParams =
194 taskList->sublist("Continuation Manager").
195 sublist("Constraint").
196 sublist("Constraint Parameters");
197
198 // Getting the parameter names from the user-defined list
199 Teuchos::ParameterList::ConstIterator i;
200 for (i = constraintParams.begin(); i !=constraintParams.end(); ++i)
201 constraintParamsNames->push_back(
202 constraintParams.get<std::string>(constraintParams.name(i)));
203
204 // Instantiating the constraint list
205 Teuchos::ParameterList & constraintsList =
206 noxAndLocaList->sublist("LOCA").sublist("Constraints");
207 constraintsList.set("Constraint Object", interfaceConstraint);
208 constraintsList.set("Constraint Parameter Names",
209 constraintParamsNames);
210 constraintsList.set("Bordered Solver Method", "Householder");
211
212 }
213
214 // Create the global data object
215 locaGlobalData = LOCA::createGlobalData(noxAndLocaList, epetraFactory);
216
217 // Create the Group
218 Teuchos::RCP<LOCA::Epetra::Group> group =
219 Teuchos::rcp(new LOCA::Epetra::Group(locaGlobalData, noxPrintingList,
220 interfaceRequired, *locaInitialGuess, linearSystem, continuableParams));
221
222 // Create the Solver convergence test
223 Teuchos::RCP<NOX::StatusTest::NormF> normTolerance =
224 Teuchos::rcp(new NOX::StatusTest::NormF(
225 taskList->
226 sublist("Continuation Manager").
227 sublist("Continuation").
228 get<double>("Nonlinear Step Tolerance"),
229 NOX::StatusTest::NormF::Scaled));
230 Teuchos::RCP<NOX::StatusTest::MaxIters> maxIterations =
231 Teuchos::rcp(new NOX::StatusTest::MaxIters(
232 noxAndLocaList->
233 sublist("LOCA").
234 sublist("Stepper").
235 get<int>("Max Nonlinear Iterations")));
236 Teuchos::RCP<NOX::StatusTest::Combo> combo =
237 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
238 combo->addStatusTest(normTolerance);
239 combo->addStatusTest(maxIterations);
240
241 // Create the stepper
242 locaStepper = Teuchos::rcp (new LOCA::Stepper(locaGlobalData, group, combo, noxAndLocaList));
243
244 // Performing some checks
245 ValidateLOCAStepper();
246
247 return true;
248 }
249
250
251 bool ContinuationManager::
252 BuildLOCAPeriodicStepper(const Teuchos::RCP<EpetraExt::MultiComm> globalComm)
253 {
254
255 if (comm->MyPID()==0) std::cout << std::endl << "Building the LOCA stepper..." << std::endl;
256
257 // Make sure the problem has been set
258 TEUCHOS_TEST_FOR_EXCEPTION( problem == Teuchos::null,
259 std::logic_error,
260 "ContinuationManager has not been given a valid ProblemLOCAPrototype");
261
262 // Create the Epetra_RowMatrix for the Jacobian/Preconditioner
263 Teuchos::RCP <Epetra_RowMatrix> jacobian = problem->GetJacF();
264
265 // Get the initial guess vector
266 Teuchos::RCP <Epetra_Vector> initialGuess =
267 problem->GetInitialGuess();
268
269 // NOX and LOCA sublist
270 Teuchos::RCP <Teuchos::ParameterList> noxAndLocaList =
271 Teuchos::rcp(new Teuchos::ParameterList(taskList->sublist("NOX and LOCA")));
272
273 // Create the lists needed for linear systems
274 Teuchos::ParameterList & noxPrintingList =
275 noxAndLocaList->
276 sublist("NOX").
277 sublist("Printing");
278 Teuchos::ParameterList & linearSystemList =
279 noxAndLocaList->
280 sublist("NOX").
281 sublist("Direction").
282 sublist("Newton").
283 sublist("Linear Solver");
284
285 // Create the interface between the test problem and the nonlinear solver
286 // This is created by the user using inheritance of the abstract base
287 // class
288 Teuchos::RCP <LOCAInterface> interface =
289 Teuchos::rcp(new LOCAInterface(problem,Teuchos::rcp(&*this,false)));
290
291 Epetra_MultiVector guessMV(initialGuess->Map(), globalComm->NumTimeStepsOnDomain());
292 for (int i=0; i<globalComm->NumTimeStepsOnDomain(); i++) *(guessMV(i)) = *initialGuess;
293
294
295 std::cout << "XXX num time steps on domain = " << globalComm->NumTimeStepsOnDomain() << std::endl;
296
297 double dt = 1.0;
298 Teuchos::RCP <LOCA::Epetra::Interface::xyzt> xyzt_interface =
299 Teuchos::rcp(new LOCA::Epetra::Interface::xyzt(interface, guessMV, jacobian, globalComm, *initialGuess, dt ));
300
301
302 Teuchos::RCP <LOCA::Epetra::Interface::Required> interfaceRequired = xyzt_interface;
303 Teuchos::RCP <NOX::Epetra::Interface::Jacobian> interfaceJacobian = xyzt_interface;
304
305 Teuchos::RCP<Epetra_RowMatrix> Axyzt =
306 Teuchos::rcp(&(xyzt_interface->getJacobian()),false);
307 Epetra_Vector& solnxyzt = xyzt_interface->getSolution();
308
309
310 // Create the linear system
311 Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linearSystem =
312 Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(noxPrintingList,
313 linearSystemList,
314 interfaceRequired,
315 interfaceJacobian,
316 Axyzt,
317 solnxyzt));
318
319 // Get the parameter vector
320 LOCA::ParameterVector continuableParams = problem->GetContinuableParams();
321
322 // Create Epetra factory
323 Teuchos::RCP <LOCA::Abstract::Factory> epetraFactory =
324 Teuchos::rcp(new LOCA::Epetra::Factory);
325
326 // Create the loca vector
327 Teuchos::RCP <NOX::Epetra::Vector> locaInitialGuess =
328 Teuchos::rcp (new NOX::Epetra::Vector(solnxyzt));
329
330 // Instantiate the constraint objects
331 //if (isConstrainedProblem)
332 if (interfaceConstraint != Teuchos::null) {
333
334 // // Instantiate the constraint
335 // Teuchos::RCP <PhaseConstraint> phaseConstraint =
336 // Teuchos::rcp(new PhaseConstraint(problem,*locaInitialGuess));
337 //
338 // // Instantiate the interface
339 // Teuchos::RCP <LOCA::MultiContinuation::ConstraintInterface>
340 // interfaceConstraint = phaseConstraint;
341
342 // Instantiate the constraint parameters names
343 Teuchos::RCP< std::vector<std::string> > constraintParamsNames =
344 Teuchos::rcp(new std::vector<std::string>());
345
346 // The user-defined constrained parameters
347 Teuchos::ParameterList & constraintParams =
348 taskList->sublist("Continuation Manager").
349 sublist("Constraint").
350 sublist("Constraint Parameters");
351
352 // Getting the parameter names from the user-defined list
353 Teuchos::ParameterList::ConstIterator i;
354 for (i = constraintParams.begin(); i !=constraintParams.end(); ++i)
355 constraintParamsNames->push_back(
356 constraintParams.get<std::string>(constraintParams.name(i)));
357
358 // Instantiating the constraint list
359 Teuchos::ParameterList & constraintsList =
360 noxAndLocaList->sublist("LOCA").sublist("Constraints");
361 constraintsList.set("Constraint Object", interfaceConstraint);
362 constraintsList.set("Constraint Parameter Names",
363 constraintParamsNames);
364 constraintsList.set("Bordered Solver Method", "Householder");
365
366 }
367
368 // Create the global data object
369 locaGlobalData = LOCA::createGlobalData(noxAndLocaList, epetraFactory);
370
371 // Create the Group
372 Teuchos::RCP<LOCA::Epetra::Group> group =
373 Teuchos::rcp(new LOCA::Epetra::Group(locaGlobalData, noxPrintingList,
374 interfaceRequired, *locaInitialGuess, linearSystem, continuableParams));
375
376 // Create the Solver convergence test
377 Teuchos::RCP<NOX::StatusTest::NormF> normTolerance =
378 Teuchos::rcp(new NOX::StatusTest::NormF(
379 taskList->
380 sublist("Continuation Manager").
381 sublist("Continuation").
382 get<double>("Nonlinear Step Tolerance"),
383 NOX::StatusTest::NormF::Scaled));
384 Teuchos::RCP<NOX::StatusTest::MaxIters> maxIterations =
385 Teuchos::rcp(new NOX::StatusTest::MaxIters(
386 noxAndLocaList->
387 sublist("LOCA").
388 sublist("Stepper").
389 get<int>("Max Nonlinear Iterations")));
390 Teuchos::RCP<NOX::StatusTest::Combo> combo =
391 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
392 combo->addStatusTest(normTolerance);
393 combo->addStatusTest(maxIterations);
394
395 // Create the stepper
396 locaStepper = Teuchos::rcp (new LOCA::Stepper(locaGlobalData, group, combo, noxAndLocaList));
397
398 // Performing some checks
399 ValidateLOCAStepper();
400
401 return true;
402 }
403
404
405 bool ContinuationManager::
406 ValidateLOCAStepper()
407 {
408 // The number of steps must not exceed maxAllowedSteps
409 int numSteps = iniStepLabel + locaStepper->getList()->sublist("LOCA").
410 sublist("Stepper").
411 get<int>("Max Steps");
412 std::ostringstream osstream;
413 osstream << maxAllowedSteps;
414 std::string errString = "iniStepLabel + Max number of continuation steps must not exceed " +
415 osstream.str();
416 TEUCHOS_TEST_FOR_EXCEPTION( numSteps > maxAllowedSteps,
417 std::logic_error,
418 errString);
419
420 return true;
421 }
422
423 bool ContinuationManager::
424 RunLOCAStepper()
425 {
426 // Touch the continuation file parameter list to get rid of the annoying "[empty]" flag
427 TouchContFileParameters( *(problem->GetContinuationFileParameters()) );
428
429 // Initialise continuation file
430 if ( (comm->MyPID() == 0) && (iniStepLabel == 0) )
431 WriteHeaderToContFile( GetContinuationFileName(),
432 *(problem->GetContinuationFileParameters()) );
433
434 // Running
435 locaStepperStatus = locaStepper->run();
436
437 return true;
438 }
439
440 bool ContinuationManager::
441 PrintLOCAStepperStatistics()
442 {
443 // Ckecking the convergence of a nonlinear step
444 if (locaStepperStatus != LOCA::Abstract::Iterator::Finished)
445 if (locaGlobalData->locaUtils->isPrintType(NOX::Utils::Error))
446 locaGlobalData->locaUtils->out() << "Stepper failed to converge!" << std::endl;
447
448 // Output the parameter list
449 if (locaGlobalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) {
450 locaGlobalData->locaUtils->out() << std::endl <<
451 "### Final Parameters ############" << std::endl;
452 locaStepper->getList()->print(locaGlobalData->locaUtils->out());
453 locaGlobalData->locaUtils->out() << std::endl;
454 }
455
456 // The time spent
457 if (comm->MyPID() == 0)
458 std::cout << std::endl << "#### Statistics ########" << std::endl;
459
460 std::cout << " Time on proc " << comm->MyPID() << " = "
461 << timeCounter.ElapsedTime() << std::endl;
462
463 // Check number of steps
464 int numSteps = locaStepper->getStepNumber();
465 if (comm->MyPID() == 0)
466 std::cout << " Number of continuation Steps = " << numSteps << std::endl;
467
468 // Check number of failed steps
469 int numFailedSteps = locaStepper->getNumFailedSteps();
470 if (comm->MyPID() == 0)
471 std::cout << " Number of failed continuation Steps = " << numFailedSteps << std::endl;
472
473 return true;
474 }
475
476 std::string ContinuationManager::
477 GetSolutionFileName() const
478 {
479 // Number of digits
480 int numDigits =
481 static_cast<int>( std::floor( std::log10( static_cast<double>(maxAllowedSteps) ) ) ) + 1;
482
483 // Composing the filename
484 std::ostringstream fileName;
485 fileName << outputDir + "/" +
486 solutionFilesPrefix +
487 StringifyInt(iniStepLabel + locaStepper->getStepNumber(), numDigits) +
488 "." + solutionFilesExtension;
489
490 return fileName.str();
491 }
492
493 std::string ContinuationManager::
494 GetContinuationFileName() const
495 {
496 return outputDir + "/" + continuationFileName;
497 }
498
499 int ContinuationManager::
500 GetStepID() const
501 {
502 return (iniStepLabel+locaStepper->getStepNumber());
503 }
504
505 ContinuationManager::SolutionFileAttribute ContinuationManager::
506 GetSolutionFileAttribute() const
507 {
508 // Default is NoPrint
509 ContinuationManager::SolutionFileAttribute attribute = NoPrint;
510
511 // Number of steps
512 int numSteps = locaStepper->getStepNumber();
513
514 // Steps per print
515 int stepsPerPrint = 1;
516
517 if ( taskList->sublist("Continuation Manager").
518 sublist("Continuation").
519 isParameter("Steps Per Print"))
520 stepsPerPrint = taskList->sublist("Continuation Manager").
521 sublist("Continuation").
522 get<int>("Steps Per Print");
523
524 // Every few steps, do print
525 if ( (stepsPerPrint != 0 ) && ( numSteps % stepsPerPrint == 0 ) )
526 attribute = Print;
527
528 return attribute;
529 }
530
531 Teuchos::RCP <Teuchos::ParameterList> ContinuationManager::
532 GetTaskList() const
533 {
534 return taskList;
535 }
536
537 bool ContinuationManager::
538 SetLOCAProblem( const Teuchos::RCP <ProblemLOCAPrototype> & aProblem )
539 {
540 problem = aProblem;
541 return true;
542 }
543
544 bool ContinuationManager::
545 SetLOCAConstraint(
546 const Teuchos::RCP <LOCA::MultiContinuation::ConstraintInterface> &
547 anInterfaceConstraint)
548 {
549 interfaceConstraint = anInterfaceConstraint;
550 return true;
551 }
552
553 bool ContinuationManager::
554 SetTaskList( Teuchos::RCP <Teuchos::ParameterList> aTaskList )
555 {
556 taskList = aTaskList;
557 return true;
558 }
559
560 std::string ContinuationManager::
561 StringifyInt( const int & intNumber , const int & digits) const
562 {
563 // The number of Steps
564 std::ostringstream osstream;
565 osstream.width(digits);
566 osstream.fill('0');
567 osstream << std::right << intNumber;
568
569 return osstream.str();
570 }