Debian Codegraph @

packages/nox/test/epetra/MultiPhysics/Example_HMX.C

raw
1 // @HEADER
2 // *****************************************************************************
3 // NOX: An Object-Oriented Nonlinear Solver Package
4 //
5 // Copyright 2002 NTESS and the NOX contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 // HMX Cook-off Test Problem
11
12 /* Solves the nonlinear equation:
13 *
14 */
15
16 // NOX Objects
17 #include "NOX.H"
18 #include "NOX_Epetra.H"
19 #include "NOX_TestCompare.H"
20 #include "NOX_Epetra_DebugTools.H"
21
22 // For parsing command line
23 #include "Teuchos_CommandLineProcessor.hpp"
24
25 // Trilinos Objects
26 #ifdef HAVE_MPI
27 #include "Epetra_MpiComm.h"
28 #else
29 #include "Epetra_SerialComm.h"
30 #endif
31 #include "Epetra_Map.h"
32 #include "Epetra_Vector.h"
33 #include "Epetra_RowMatrix.h"
34 #include "Epetra_CrsMatrix.h"
35 #include "Epetra_Map.h"
36 #include "Epetra_LinearProblem.h"
37 #include "AztecOO.h"
38
39 #ifdef HAVE_NOX_ML_EPETRA
40 #include "Teuchos_ParameterList.hpp"
41 #endif
42
43 // Headers needed for FD coloring
44 #include <vector>
45 #ifdef HAVE_NOX_EPETRAEXT // Use epetraext package in Trilinos
46 #include "EpetraExt_MapColoring.h"
47 #include "EpetraExt_MapColoringIndex.h"
48 #endif
49
50 // New coupling library headers
51 #include "NOX_Multiphysics_Solver_Manager.H"
52
53 // User's application specific files
54 #include "Problem_Manager.H"
55 #include "Problem_Interface.H"
56 #include "HMX_PDE.H"
57
58 // Added to allow timings
59 #include "Epetra_Time.h"
60
61 using namespace std;
62
63 int main(int argc, char *argv[])
64 {
65
66 // Initialize MPI
67 #ifdef HAVE_MPI
68 MPI_Init(&argc,&argv);
69 #endif
70
71 // Create a communicator for Epetra objects
72 #ifdef HAVE_MPI
73 Epetra_MpiComm Comm( MPI_COMM_WORLD );
74 #else
75 Epetra_SerialComm Comm;
76 #endif
77
78 Teuchos::CommandLineProcessor clp( false );
79
80 // Default run-time options that can be changed from the command line
81 bool verbose = true ;
82 int NumGlobalNodes = 10 ;
83 bool runMF = true ;
84 bool useMatlab = false ;
85 bool doOffBlocks = true ;
86 bool libloose = true ;
87 std::string outputDir = "." ;
88 std::string goldDir = "." ;
89
90 clp.setOption( "verbose", "no-verbose", &verbose, "Verbosity on or off." );
91 clp.setOption( "n", &NumGlobalNodes, "Number of elements" );
92 clp.setOption( "runMF", "loose", &runMF, "Use Matrix-Free strong coupling" );
93 clp.setOption( "offblocks", "no-offblocks", &doOffBlocks, "Include off-diagonal blocks in preconditioning matrix" );
94 clp.setOption( "matlab", "no-matlab", &useMatlab, "Use Matlab debugging engine" );
95 clp.setOption( "noxlib", "no-noxlib", &libloose, "Perform loose coupling using NOX's library (as opposed to hard-coded test driver)." );
96 clp.setOption( "outputdir", &outputDir, "Directory to output mesh and results into. Default is \"./\"" );
97 clp.setOption( "golddir", &goldDir, "Directory to read gold test from. Default is \"./\"" );
98
99 Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv);
100
101 if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL )
102 return parse_return;
103
104 outputDir += "/";
105 goldDir += "/";
106
107 // Create and reset the Timer
108 Epetra_Time myTimer(Comm);
109 double startWallTime = myTimer.WallTime();
110
111 // Get the process ID and the total number of processors
112 int MyPID = Comm.MyPID();
113 int NumProc = Comm.NumProc();
114
115 NumGlobalNodes++; // convert #elements to #nodes
116
117 // The number of unknowns must be at least equal to the number of processors.
118 if (NumGlobalNodes < NumProc)
119 {
120 std::cout << "numGlobalNodes = " << NumGlobalNodes
121 << " cannot be < number of processors = " << NumProc << std::endl;
122 exit(1);
123 }
124
125 // Begin Nonlinear Solver ************************************
126
127 // NOTE: For now these parameters apply to all problems handled by
128 // Problem_Manager. Each problem could be made to have its own
129 // parameter list as wwell as its own convergence test(s).
130
131 // Create the top level parameter list
132 Teuchos::RCP<Teuchos::ParameterList> nlParamsPtr =
133 Teuchos::rcp(new Teuchos::ParameterList);
134 Teuchos::ParameterList& nlParams = *(nlParamsPtr.get());
135
136 // Set the nonlinear solver method
137 nlParams.set("Nonlinear Solver", "Line Search Based");
138 //nlParams.set("Nonlinear Solver", "Trust Region Based");
139
140 // Set the printing parameters in the "Printing" sublist
141 Teuchos::ParameterList& printParams = nlParams.sublist("Printing");
142 printParams.set("MyPID", MyPID);
143 printParams.set("Output Precision", 3);
144 printParams.set("Output Processor", 0);
145 printParams.set("Output Information",
146 NOX::Utils::Warning +
147 NOX::Utils::OuterIteration +
148 NOX::Utils::InnerIteration +
149 NOX::Utils::Parameters +
150 NOX::Utils::Details +
151 NOX::Utils::OuterIterationStatusTest +
152 NOX::Utils::LinearSolverDetails +
153 NOX::Utils::TestDetails );
154
155 NOX::Utils outputUtils(printParams);
156
157 // Sublist for line search
158 Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search");
159 searchParams.set("Method", "Full Step");
160
161 // Sublist for direction
162 Teuchos::ParameterList& dirParams = nlParams.sublist("Direction");
163 dirParams.set("Method", "Newton");
164 Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton");
165 newtonParams.set("Forcing Term Method", "Constant");
166
167 // Sublist for linear solver for the Newton method
168 Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver");
169 lsParams.set("Aztec Solver", "GMRES");
170 lsParams.set("Max Iterations", 800);
171 lsParams.set("Tolerance", 1e-4);
172 lsParams.set("Output Frequency", 50);
173 lsParams.set("Preconditioner", "AztecOO");
174
175 // Create the convergence tests
176 // Note: as for the parameter list, both (all) problems use the same
177 // convergence test(s) for now, but each could have its own.
178 Teuchos::RCP<NOX::StatusTest::NormF> absresid =
179 Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-8));
180 Teuchos::RCP<NOX::StatusTest::NormUpdate> update =
181 Teuchos::rcp(new NOX::StatusTest::NormUpdate(1.0e-5));
182 Teuchos::RCP<NOX::StatusTest::Combo> converged =
183 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::AND));
184 converged->addStatusTest(absresid);
185 Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters =
186 Teuchos::rcp(new NOX::StatusTest::MaxIters(20));
187 Teuchos::RCP<NOX::StatusTest::FiniteValue> finiteValue =
188 Teuchos::rcp(new NOX::StatusTest::FiniteValue);
189 Teuchos::RCP<NOX::StatusTest::Combo> combo =
190 Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR));
191 combo->addStatusTest(converged);
192 combo->addStatusTest(maxiters);
193 combo->addStatusTest(finiteValue);
194
195 // Create the Problem Manager
196 Problem_Manager problemManager(Comm, doOffBlocks, 0, useMatlab);
197
198 // Note that each problem could contain its own nlParams list as well as
199 // its own convergence test(s).
200 problemManager.registerParameters(nlParamsPtr);
201 problemManager.registerStatusTest(combo);
202
203 std::string nameT = "Temperature";
204 double Const_R = 1.9872 ;
205 double Specific_H = 0.42 ;
206 double Density = 1.90 ;
207 double Thermal_K = 0.8658e-3 ;
208 double diffCoef_T = Thermal_K / (Density * Specific_H);
209
210 double StericCoef_T = 0.0;
211 double PreExp_T = 0.0;
212 double ActEnergy_T = 0.0 ;
213 map<string, double> SrcTermExponent_T; // Leave empty if no volume source
214 map<string, double> SrcTermWeight_T;
215 SrcTermWeight_T.insert( pair<string, double> ("SpeciesA", -190.0) );
216 SrcTermWeight_T.insert( pair<string, double> ("SpeciesB", 570.0) );
217 SrcTermWeight_T.insert( pair<string, double> ("SpeciesC", 2280.0) );
218
219 // Create each part of the HMX cook-off problem
220 HMX_PDE HMX_TempEq (Comm,
221 diffCoef_T,
222 Const_R,
223 StericCoef_T, // Dummy for Temp Eq.
224 PreExp_T, // Dummy for Temp Eq.
225 ActEnergy_T, // Dummy for Temp Eq.
226 SrcTermExponent_T,
227 SrcTermWeight_T,
228 1*NumGlobalNodes, nameT);
229
230 HMX_TempEq.setTempFieldName(HMX_TempEq.getName());
231
232 // Override default initialization with values we want
233 HMX_TempEq.initializeSolution(500.0);
234
235 problemManager.addProblem(HMX_TempEq);
236
237
238 std::string nameA = "SpeciesA";
239 double diffCoef_A = 0.0 ;
240 //double stericCoef_A = 0.0 ;
241 double stericCoef_A = 2.0 ; // ROGER: high coupling
242 //double stericCoef_A = 2.0 ; // ROGER: high coupling
243 double preExp_A = exp(48.7) ;
244 double actEnergy_A = 52700.0 ;
245 map<string, double> SrcTermExponent_A;
246 SrcTermExponent_A.insert( pair<string, double> (nameA, 1.0) );
247 map<string, double> SrcTermWeight_A;
248 SrcTermWeight_A.insert( pair<string, double> (nameA, -1.0) );
249
250 HMX_PDE HMX_RxnA(Comm,
251 diffCoef_A,
252 Const_R,
253 stericCoef_A,
254 preExp_A,
255 actEnergy_A,
256 SrcTermExponent_A,
257 SrcTermWeight_A,
258 1*NumGlobalNodes, nameA);
259
260 HMX_RxnA.setTempFieldName(HMX_TempEq.getName());
261
262 // Override default initialization with values we want
263 HMX_RxnA.initializeSolution(2.0);
264
265 problemManager.addProblem(HMX_RxnA);
266
267
268 std::string nameB = "SpeciesB";
269 double diffCoef_B = 0.0 ;
270 double stericCoef_B = 1.0 ;
271 //double stericCoef_B = -1.0 ;
272 double preExp_B = exp(37.5) ;
273 double actEnergy_B = 44100.0 ;
274 map<string, double> SrcTermExponent_B;
275 SrcTermExponent_B.insert( pair<string, double> (nameB, 1.0) );
276 map<string, double> SrcTermWeight_B;
277 SrcTermWeight_B.insert( pair<string, double> (nameA, 1.0) );
278 SrcTermWeight_B.insert( pair<string, double> (nameB, -1.0) );
279
280 HMX_PDE HMX_RxnB(Comm,
281 diffCoef_B,
282 Const_R,
283 stericCoef_B,
284 preExp_B,
285 actEnergy_B,
286 SrcTermExponent_B,
287 SrcTermWeight_B,
288 NumGlobalNodes, nameB);
289
290 HMX_RxnB.setTempFieldName(HMX_TempEq.getName());
291
292 // Override default initialization with values we want
293 HMX_RxnB.initializeSolution(1.0);
294
295 problemManager.addProblem(HMX_RxnB);
296
297
298 std::string nameC = "SpeciesC";
299 double diffCoef_C = 0.0 ;
300 double stericCoef_C = 0.0 ;
301 double preExp_C = exp(28.1) ;
302 double actEnergy_C = 34100.0 ;
303 map<string, double> SrcTermExponent_C;
304 SrcTermExponent_C.insert( pair<string, double> (nameC, 2.0) );
305 map<string, double> SrcTermWeight_C;
306 SrcTermWeight_C.insert( pair<string, double> (nameB, 2.0) );
307 SrcTermWeight_C.insert( pair<string, double> (nameC, -2.0) );
308
309 HMX_PDE HMX_RxnC(Comm,
310 diffCoef_C,
311 Const_R,
312 stericCoef_C,
313 preExp_C,
314 actEnergy_C,
315 SrcTermExponent_C,
316 SrcTermWeight_C,
317 1*NumGlobalNodes, nameC);
318
319 HMX_RxnC.setTempFieldName(HMX_TempEq.getName());
320
321 // Override default initialization with values we want
322 // HMX_RxnC.initializeSolution(0.0);
323
324 problemManager.addProblem(HMX_RxnC);
325
326 problemManager.createDependency(HMX_TempEq, HMX_RxnA);
327 problemManager.createDependency(HMX_TempEq, HMX_RxnB);
328 problemManager.createDependency(HMX_TempEq, HMX_RxnC);
329
330 problemManager.createDependency(HMX_RxnA, HMX_TempEq);
331
332 problemManager.createDependency(HMX_RxnB, HMX_TempEq);
333 problemManager.createDependency(HMX_RxnB, HMX_RxnA);
334
335 problemManager.createDependency(HMX_RxnC, HMX_TempEq);
336 problemManager.createDependency(HMX_RxnC, HMX_RxnB);
337
338 // Initialize time integration parameters
339 int maxTimeSteps = 3;
340 int timeStep = 0;
341 double time = 0.;
342 //double dt = HMX_TempEq.getdt();
343 double dt = 10.0 * HMX_TempEq.getdt();
344
345 problemManager.registerComplete();
346
347 problemManager.outputStatus(std::cout);
348
349 // Print initial solution
350 if( verbose )
351 problemManager.outputSolutions( outputDir);
352
353 // Identify the test problem
354 if( outputUtils.isPrintType(NOX::Utils::TestDetails) )
355 outputUtils.out() << "Starting epetra/MultiPhysics/example_hmx.exe" << std::endl;
356
357 // Identify processor information
358 #ifdef HAVE_MPI
359 outputUtils.out() << "This test is broken in parallel." << std::endl;
360 outputUtils.out() << "Test failed!" << std::endl;
361 MPI_Finalize();
362 return -1;
363 #else
364 if (outputUtils.isPrintType(NOX::Utils::TestDetails))
365 outputUtils.out() << "Serial Run" << std::endl;
366 #endif
367
368 // Create a TestCompare class
369 int status = 0;
370 NOX::TestCompare tester( outputUtils.out(), outputUtils );
371 double abstol = 1.e-4;
372 double reltol = 1.e-4 ;
373
374 // Time integration loop
375 while(timeStep < maxTimeSteps) {
376
377 timeStep++;
378 time += dt;
379
380 std::cout << "Time Step: " << timeStep << ",\tTime: " << time << std::endl;
381
382 // Solve the coupled problem
383 if( runMF )
384 problemManager.solveMF(); // Need a status test check here ....
385 else if( !libloose )
386 problemManager.solve(); // Hard-coded loose coupling
387 else // Loose coupling via NOX library
388 {
389 // Create the loose coupling solver manager
390 Teuchos::RCP<vector<Teuchos::RCP<NOX::Solver::Generic> > > solversVec =
391 Teuchos::rcp( new std::vector<Teuchos::RCP<NOX::Solver::Generic> > );
392
393 map<int, Teuchos::RCP<NOX::Solver::Generic> >::iterator iter = problemManager.getSolvers().begin(),
394 iter_end = problemManager.getSolvers().end() ;
395 for( ; iter_end != iter; ++iter )
396 {
397 std::cout << " ........ registered Solver::Manager # " << (*iter).first << std::endl;
398 solversVec->push_back( (*iter).second );
399 }
400
401 // Package the Problem_Manager as the DataExchange::Intreface
402 Teuchos::RCP<NOX::Multiphysics::DataExchange::Interface> dataExInterface =
403 Teuchos::rcp( &problemManager, false );
404
405 NOX::Multiphysics::Solver::Manager cplSolv( solversVec, dataExInterface, combo, nlParamsPtr );
406
407 cplSolv.solve();
408
409 // Refresh all problems with solutions from solver
410 problemManager.copyAllGroupXtoProblems();
411
412 // Reset all solver groups to force recomputation of residuals
413 problemManager.resetAllCurrentGroupX();
414 }
415
416 if( verbose )
417 problemManager.outputSolutions( outputDir, timeStep );
418
419 map<int, Teuchos::RCP<GenericEpetraProblem> >::iterator iter = problemManager.getProblems().begin(),
420 iter_end = problemManager.getProblems().end() ;
421 for( ; iter_end != iter; ++iter )
422 {
423 GenericEpetraProblem & problem = *(*iter).second;
424 std::string msg = "Numerical-to-Gold Solution comparison for problem \"" + problem.getName() + "\"";
425
426 // Get the gold copy to comapre against current solution
427 std::string baseFileame = problemManager.createIOname( problem, timeStep );
428 std::string goldFileame = goldDir + "gold_hmx/" + baseFileame;
429
430 Epetra_Vector * tmpVec = NULL;
431 int ierr = NOX::Epetra::DebugTools::readVector( goldFileame, Comm, tmpVec );
432 if( ierr != 0 )
433 {
434 outputUtils.out() << "ERROR opening gold copy file \"" << goldFileame << "\"." << std::endl;
435 status = ierr;
436 break;
437 }
438
439 Teuchos::RCP<Epetra_Vector> goldSoln = Teuchos::rcp( tmpVec );
440
441 // Need NOX::Epetra::Vectors for tests
442 NOX::Epetra::Vector numerical ( problem.getSolution() , NOX::Epetra::Vector::CreateView );
443 NOX::Epetra::Vector goldVec ( goldSoln , NOX::Epetra::Vector::CreateView );
444
445 status += tester.testVector( numerical, goldVec, reltol, abstol, msg );
446
447 // Quit if test fails
448 if( status != 0 )
449 break;
450 }
451
452 // Quit if test fails
453 if( status != 0 )
454 break;
455
456 // Reset problems by copying solution into old solution
457 problemManager.resetProblems();
458
459 } // end time step while loop
460
461 // Output timing info
462 if(MyPID==0)
463 std::cout << "\nTimings :\n\tWallTime --> " << myTimer.WallTime() - startWallTime << " sec."
464 << "\n\tElapsedTime --> " << myTimer.ElapsedTime() << " sec." << std::endl << std::endl;
465
466 if( 1 ) // this will be turned on later
467 {
468 // Summarize test results
469 if( status == 0 )
470 outputUtils.out() << "Test passed!" << std::endl;
471 else
472 outputUtils.out() << "Test failed!" << std::endl;
473 }
474 else // force this test to pass for now, but at least warn of failure
475 {
476 // Summarize test results
477 if( status == 0 )
478 outputUtils.out() << "Test passed!" << std::endl;
479 else
480 {
481 outputUtils.out() << "This test actually F-A-I-L-E-D." << std::endl;
482 outputUtils.out() << "Test passed!" << std::endl;
483 }
484 }
485
486 #ifdef HAVE_MPI
487 MPI_Finalize() ;
488 #endif
489
490 return 0 ;
491 }
492