Debian Codegraph @

packages/rol/example/PDE-OPT/dynamic/navier-stokes/AugmentedSystem.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 /*! \file example_01.cpp
11 \brief Shows how to solve the Navier-Stokes control problem.
12 */
13
14 #include "Teuchos_Comm.hpp"
15 #include "Teuchos_GlobalMPISession.hpp"
16 #include "Tpetra_Core.hpp"
17 #include "Tpetra_Version.hpp"
18
19 #include <iostream>
20 #include <algorithm>
21 //#include <fenv.h>
22
23 #include "ROL_Bounds.hpp"
24 #include "ROL_Stream.hpp"
25 #include "ROL_ParameterList.hpp"
26 #include "ROL_OptimizationSolver.hpp"
27 #include "ROL_ReducedDynamicObjective.hpp"
28 #include "ROL_DynamicConstraintCheck.hpp"
29 #include "ROL_DynamicObjectiveCheck.hpp"
30 #include "ROL_RandomVector.hpp"
31 #include "ROL_PinTConstraint.hpp"
32 #include "ROL_PinTVectorCommunication.hpp"
33 //#include "ROL_PinTVectorCommunication_Tpetra.hpp"
34 #include "ROL_PinTVectorCommunication_StdTpetraComposite.hpp"
35
36 #include "../../TOOLS/dynconstraint.hpp"
37 #include "../../TOOLS/pdeconstraint.hpp"
38 #include "../../TOOLS/pdeobjective.hpp"
39 #include "../../TOOLS/ltiobjective.hpp"
40 #include "../../TOOLS/meshreader.hpp"
41 #include "../../TOOLS/pdevector.hpp"
42 #include "dynpde_navier-stokes.hpp"
43 #include "obj_navier-stokes.hpp"
44 #include "initial_condition.hpp"
45
46 using RealT = double;
47
48 class KKTOperator : public ROL::LinearOperator<RealT> {
49 public:
50 ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con;
51 ROL::Ptr<ROL::Vector<RealT>> state;
52 ROL::Ptr<ROL::Vector<RealT>> control;
53 int myRank;
54 ROL::Ptr<std::ostream> outStream;
55
56 void apply( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override
57 {
58 pint_con->applyAugmentedKKT(Hv,v,*state,*control,tol);
59 }
60 };
61
62 class MGRITKKTOperator : public ROL::LinearOperator<RealT> {
63 public:
64 ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con;
65 ROL::Ptr<ROL::Vector<RealT>> state;
66 ROL::Ptr<ROL::Vector<RealT>> control;
67 int myRank;
68 ROL::Ptr<std::ostream> outStream;
69
70 void apply( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override
71 {
72 assert(false);
73 pint_con->applyMultigridAugmentedKKT(Hv,v,*state,*control,tol);
74 }
75 void applyInverse( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override
76 {
77 pint_con->applyMultigridAugmentedKKT(Hv,v,*state,*control,tol);
78 }
79 };
80
81 class WathenKKTOperator : public ROL::LinearOperator<RealT> {
82 public:
83 ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con;
84 ROL::Ptr<ROL::Vector<RealT>> state;
85 ROL::Ptr<ROL::Vector<RealT>> control;
86 int myRank;
87 ROL::Ptr<std::ostream> outStream;
88
89 void apply( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override
90 {
91 assert(false);
92 }
93 void applyInverse( ROL::Vector<RealT> &Hv, const ROL::Vector<RealT> &v, RealT &tol ) const override
94 {
95 pint_con->applyWathenInverse(Hv,v,*state,*control,tol,false,0);
96 // pint_con->applyLocalInverse(Hv,v,*state,*control,tol,0,false); // do this to exactly solve
97 }
98 };
99
100 ROL::Ptr<ROL::Vector<RealT>>
101 solveKKTSystem(const std::string & prefix,
102 bool useWathenPrec,int myRank,
103 double absTol,double relTol,
104 const Teuchos::RCP<Teuchos::StackedTimer> & timer,
105 const ROL::Ptr<std::ostream> & outStream,
106 const ROL::Ptr<ROL::PinTConstraint<RealT>> & pint_con,
107 const ROL::Ptr<ROL::Vector<RealT>> & kkt_b,
108 const ROL::Ptr<ROL::PinTVector<RealT>> & state,
109 const ROL::Ptr<ROL::PinTVector<RealT>> & control);
110
111
112 template<class Real>
113 void computeInitialCondition(const ROL::Ptr<ROL::Vector<Real>> &u0,
114 const ROL::Ptr<ROL::Vector<Real>> &ck,
115 const ROL::Ptr<ROL::Vector<Real>> &uo,
116 const ROL::Ptr<ROL::Vector<Real>> &un,
117 const ROL::Ptr<ROL::Vector<Real>> &zk,
118 const ROL::Ptr<DynConstraint<Real>> &con,
119 const Real dt,
120 std::ostream &outStream);
121
122 int main(int argc, char *argv[]) {
123
124 using Teuchos::RCP;
125
126 /*** Initialize communicator. ***/
127 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
128 ROL::Ptr<const Teuchos::Comm<int>> comm
129 = Tpetra::getDefaultComm();
130
131 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
132 const int myRank = comm->getRank();
133 ROL::Ptr<std::ostream> outStream = ROL::makeStreamPtr( std::cout, (argc > 1) && (myRank==0) );
134
135 int errorFlag = 0;
136
137 // *** Example body.
138 try {
139
140 /*** Read in XML input ***/
141 ROL::Ptr<ROL::ParameterList> parlist = ROL::getParametersFromXmlFile("input.xml");
142 int nt = parlist->sublist("Time Discretization").get("Number of Time Steps", 100);
143 RealT T = parlist->sublist("Time Discretization").get("End Time", 1.0);
144 RealT dt = T/static_cast<RealT>(nt);
145 bool useParametricControl = parlist->sublist("Problem").get("Use Parametric Control", false);
146 int verbosity = parlist->sublist("General").get("Print Verbosity", 0);
147 verbosity = (myRank==0 ? verbosity : 0);
148 parlist->sublist("General").set("Print Verbosity", verbosity);
149 bool solveOutput = parlist->sublist("Dynamic Constraint").sublist("Solve").get("Output Iteration History", false);
150 solveOutput = (myRank==0 ? solveOutput : false);
151 parlist->sublist("Dynamic Constraint").sublist("Solve").set("Output Iteration History", solveOutput);
152 solveOutput = parlist->sublist("SimOpt").sublist("Solve").get("Output Iteration History", false);
153 solveOutput = (myRank==0 ? solveOutput : false);
154 parlist->sublist("SimOpt").sublist("Solve").set("Output Iteration History", solveOutput);
155
156 /*** Add MGRIT parameter list options. ***/
157 int sweeps = parlist->get("MGRIT Sweeps", 1);
158 RealT omega = parlist->get("MGRIT Relaxation",2.0/3.0);
159 int coarseSweeps = parlist->get("MGRIT Coarse Sweeps", 1);
160 RealT coarseOmega = parlist->get("MGRIT Coarse Relaxation",1.0);
161 int numLevels = parlist->get("MGRIT Levels",3);
162 double relTol = parlist->get("MGRIT Krylov Relative Tolerance",1e-4);
163 double absTol = parlist->get("MGRIT Krylov Absolute Tolerance",1e-4);
164 int spaceProc = parlist->get("MGRIT Spatial Procs",1);
165 double globalScale = parlist->get("MGRIT Global Scale",1.0);
166 bool rebalance = parlist->get("MGRIT Rebalance",false);
167 int cgIterations = parlist->get("MGRIT CG Iterations",1);
168 double cntrlRegPar = parlist->get("MGRIT Control Regularization Parameter",1.0);
169
170 ROL::Ptr<const ROL::PinTCommunicators> communicators = ROL::makePtr<ROL::PinTCommunicators>(MPI_COMM_WORLD,spaceProc);
171 ROL::Ptr<const Teuchos::Comm<int>> mpiSpaceComm = ROL::makePtr<Teuchos::MpiComm<int>>(communicators->getSpaceCommunicator());
172
173 // for "serial" in time cases, use the wathen preconditioner
174 bool useWathenPrec = communicators->getTimeSize()==spaceProc;
175
176 /*************************************************************************/
177 /***************** BUILD GOVERNING PDE ***********************************/
178 /*************************************************************************/
179 /*** Initialize mesh data structure. ***/
180 ROL::Ptr<MeshManager<RealT>> meshMgr
181 //= ROL::makePtr<MeshReader<RealT>>(*parlist, numProcs);
182 = ROL::makePtr<MeshReader<RealT>>(*parlist, 0);
183 // Initialize PDE describing Navier-Stokes equations.
184 ROL::Ptr<DynamicPDE_NavierStokes<RealT>> pde
185 = ROL::makePtr<DynamicPDE_NavierStokes<RealT>>(*parlist);
186
187 /*************************************************************************/
188 /***************** BUILD CONSTRAINT **************************************/
189 /*************************************************************************/
190 ROL::Ptr<DynConstraint<RealT>> dyn_con
191 = ROL::makePtr<DynConstraint<RealT>>(pde,meshMgr,mpiSpaceComm,*parlist,*outStream);
192 const ROL::Ptr<Assembler<RealT>> assembler = dyn_con->getAssembler();
193 dyn_con->setSolveParameters(*parlist);
194 dyn_con->getAssembler()->printMeshData(*outStream);
195
196 /*************************************************************************/
197 /***************** BUILD VECTORS *****************************************/
198 /*************************************************************************/
199 ROL::Ptr<Tpetra::MultiVector<>> u0_ptr = assembler->createStateVector();
200 ROL::Ptr<Tpetra::MultiVector<>> uo_ptr = assembler->createStateVector();
201 ROL::Ptr<Tpetra::MultiVector<>> un_ptr = assembler->createStateVector();
202 ROL::Ptr<Tpetra::MultiVector<>> ck_ptr = assembler->createResidualVector();
203 ROL::Ptr<ROL::Vector<RealT>> u0, uo, un, ck, zk;
204 u0 = ROL::makePtr<PDE_PrimalSimVector<RealT>>(u0_ptr,pde,*assembler,*parlist);
205 uo = ROL::makePtr<PDE_PrimalSimVector<RealT>>(uo_ptr,pde,*assembler,*parlist);
206 un = ROL::makePtr<PDE_PrimalSimVector<RealT>>(un_ptr,pde,*assembler,*parlist);
207 ck = ROL::makePtr<PDE_DualSimVector<RealT>>(ck_ptr,pde,*assembler,*parlist);
208 if (!useParametricControl) {
209 ROL::Ptr<Tpetra::MultiVector<>> zk_ptr = assembler->createControlVector();
210 zk = ROL::makePtr<PDE_PrimalOptVector<RealT>>(zk_ptr,pde,*assembler,*parlist);
211 }
212 else {
213 zk = ROL::makePtr<PDE_OptVector<RealT>>(ROL::makePtr<ROL::StdVector<RealT>>(1));
214 }
215 ROL::Ptr<ROL::PartitionedVector<RealT>> z
216 = ROL::PartitionedVector<RealT>::create(*zk, nt);
217
218 ROL::Ptr<const ROL::PinTVectorCommunication<RealT>> vectorComm;
219 if (!useParametricControl) {
220 vectorComm = ROL::makePtr<ROL::PinTVectorCommunication_Tpetra<RealT>>();
221 }
222 else {
223 vectorComm = ROL::makePtr<ROL::PinTVectorCommunication_StdTpetraComposite<RealT>>();
224 }
225 ROL::Ptr<ROL::PinTVector<RealT>> state = ROL::buildStatePinTVector<RealT>( communicators, vectorComm, nt, u0); // for Euler, Crank-Nicolson, stencil = [-1,0]
226 ROL::Ptr<ROL::PinTVector<RealT>> control = ROL::buildControlPinTVector<RealT>( communicators, vectorComm, nt, zk); // time discontinous, stencil = [0]
227
228 // make sure we are globally consistent
229 state->boundaryExchange();
230 control->boundaryExchange();
231
232 ROL::Ptr<ROL::Vector<RealT>> kkt_vector = ROL::makePtr<ROL::PartitionedVector<RealT>>({state->clone(),control->clone(),state->clone()});
233
234 /*************************************************************************/
235 /************************* Set initial condition *************************/
236 /*************************************************************************/
237 // ??? yanked code, reinsert
238
239 /*************************************************************************/
240 /***************** Set initial guess for optimization ********************/
241 /*************************************************************************/
242 // ??? yanked code, reinsert
243 state->zero();
244 control->zero();
245
246 /*************************************************************************/
247 /***************** Build PINT Constraint *********************************/
248 /*************************************************************************/
249 auto timeStamp = ROL::makePtr<std::vector<ROL::TimeStamp<RealT>>>(control->numOwnedSteps());
250 for( uint k=0; k<timeStamp->size(); ++k ) {
251 timeStamp->at(k).t.resize(2);
252 timeStamp->at(k).t.at(0) = k*dt;
253 timeStamp->at(k).t.at(1) = (k+1)*dt;
254 }
255
256 if(myRank==0) {
257 (*outStream) << "Sweeps = " << sweeps << std::endl;
258 (*outStream) << "Omega = " << omega << std::endl;
259 (*outStream) << "Levels = " << numLevels << std::endl;
260 (*outStream) << "Coarse Sweeps = " << coarseSweeps << std::endl;
261 (*outStream) << "Coarse Omega = " << coarseOmega << std::endl;
262 (*outStream) << "Global Scale = " << globalScale << std::endl;
263 (*outStream) << "Rebalance = " << rebalance << std::endl;
264 (*outStream) << "CG Iterations = " << cgIterations;
265 if(cgIterations==0)
266 *(outStream) << " (apply Wathen smoothing)" << std::endl;
267 else
268 *(outStream) << " (apply CG fix to Wathen smoothing)" << std::endl;
269 (*outStream) << "Cntrl Reg Par = " << cntrlRegPar << std::endl;
270 }
271
272 // build the parallel in time constraint from the user constraint
273 ROL::Ptr<ROL::PinTConstraint<RealT>> pint_con = ROL::makePtr<ROL::PinTConstraint<RealT>>(dyn_con,u0,timeStamp);
274 pint_con->applyMultigrid(numLevels,communicators,vectorComm);
275 pint_con->setSweeps(sweeps);
276 pint_con->setRelaxation(omega);
277 pint_con->setCoarseSweeps(coarseSweeps);
278 pint_con->setCoarseRelaxation(coarseOmega);
279 pint_con->setGlobalScale(globalScale);
280 pint_con->setCGIterations(cgIterations);
281 pint_con->setControlRegParam(cntrlRegPar);
282 pint_con->setRecordResidualReductions(true);
283
284 /*************************************************************************/
285 /***************** Run KKT Solver ***************************************/
286 /*************************************************************************/
287
288 double tol = 1e-12;
289 auto kkt_x_in = kkt_vector->clone();
290 auto kkt_b = kkt_vector->clone();
291
292 ROL::RandomizeVector(*kkt_x_in);
293 kkt_b->zero();
294
295 if(myRank==0) {
296 (*outStream) << "Applying augmented KKT system" << std::endl;
297 }
298
299 pint_con->applyAugmentedKKT(*kkt_b,*kkt_x_in,*state,*control,tol); // b = A * x
300
301 if(myRank==0) {
302 (*outStream) << "Applying augmented KKT system - complete" << std::endl;
303 (*outStream) << std::endl;
304 }
305
306 auto timer = Teuchos::TimeMonitor::getStackedTimer();
307
308
309 {
310 RealT res0 = kkt_b->norm();
311 kkt_b->scale(1.0/res0);
312 res0 = kkt_b->norm();
313
314 if(myRank==0) (*outStream) << std::endl;
315
316 solveKKTSystem("SOLVE: ",
317 useWathenPrec,myRank,absTol,relTol,
318 timer,outStream,
319 pint_con,kkt_b,state,control);
320
321 if(myRank==0) (*outStream) << std::endl;
322 }
323
324 #if 0
325 {
326 RealT res0 = kkt_b->norm();
327 kkt_b->scale(1.0/res0);
328 res0 = kkt_b->norm();
329
330 assert(std::fabs(res0-1.0) <= 1.0e-14);
331
332 auto kkt_x_out = kkt_vector->clone();
333 kkt_x_out->zero();
334
335 KKTOperator kktOperator;
336 kktOperator.pint_con = pint_con;
337 kktOperator.state = state;
338 kktOperator.control = control;
339 kktOperator.outStream = outStream;
340 kktOperator.myRank = myRank;
341
342 RCP<ROL::LinearOperator<RealT>> precOperator;
343
344 // build the preconditioner
345 if(useWathenPrec) {
346 RCP<WathenKKTOperator> wathenOperator(new WathenKKTOperator);
347 wathenOperator->pint_con = pint_con;
348 wathenOperator->state = state;
349 wathenOperator->control = control;
350 wathenOperator->outStream = outStream;
351 wathenOperator->myRank = myRank;
352
353 precOperator = wathenOperator;
354 }
355 else {
356 RCP<MGRITKKTOperator> mgOperator(new MGRITKKTOperator);
357 mgOperator->pint_con = pint_con;
358 mgOperator->state = state;
359 mgOperator->control = control;
360 mgOperator->outStream = outStream;
361 mgOperator->myRank = myRank;
362
363 precOperator = mgOperator;
364 }
365
366 Teuchos::ParameterList parlist;
367 ROL::ParameterList &krylovList = parlist.sublist("General").sublist("Krylov");
368 krylovList.set("Absolute Tolerance",absTol);
369 krylovList.set("Relative Tolerance",relTol);
370
371 if(myRank==0)
372 (*outStream) << "RELATIVE TOLERANCE = " << relTol << std::endl;
373
374 ROL::GMRES<RealT> krylov(parlist); // TODO: Do Belos
375 krylov.enableOutput(*outStream);
376
377 int flag = 0, iter = 0;
378
379 MPI_Barrier(MPI_COMM_WORLD);
380 double t0 = MPI_Wtime();
381
382 if(myRank==0) {
383 (*outStream) << "Solving KKT system: ";
384 if(useWathenPrec)
385 (*outStream) << "using Wathen preconditioner" << std::endl;
386 else
387 (*outStream) << "using MGRIT preconditioner" << std::endl;
388 }
389
390 timer->start("krylov");
391 RealT finalTol = krylov.run(*kkt_x_out,kktOperator,*kkt_b,*precOperator,iter,flag);
392 timer->stop("krylov");
393
394 if(myRank==0)
395 (*outStream) << "Solving KKT system - complete" << std::endl;
396
397 MPI_Barrier(MPI_COMM_WORLD);
398 double tf = MPI_Wtime();
399
400 std::stringstream ss;
401 timer->report(ss);
402
403 if(myRank==0) {
404 (*outStream) << "Krylov Iteration = " << iter << " " << (finalTol / res0) << " " << tf-t0 << std::endl;
405 (*outStream) << std::endl;
406 (*outStream) << ss.str();
407 }
408 }
409 #endif
410
411 }
412 catch (std::logic_error& err) {
413 *outStream << err.what() << "\n";
414 errorFlag = -1000;
415 }; // end try
416
417 if (errorFlag != 0)
418 std::cout << "End Result: TEST FAILED\n";
419 else
420 std::cout << "End Result: TEST PASSED\n";
421
422 return 0;
423 }
424
425 template<class Real>
426 void computeInitialCondition(const ROL::Ptr<ROL::Vector<Real>> &u0,
427 const ROL::Ptr<ROL::Vector<Real>> &ck,
428 const ROL::Ptr<ROL::Vector<Real>> &uo,
429 const ROL::Ptr<ROL::Vector<Real>> &un,
430 const ROL::Ptr<ROL::Vector<Real>> &zk,
431 const ROL::Ptr<DynConstraint<Real>> &con,
432 const Real dt,
433 std::ostream &outStream) {
434 Real T = 80.0;
435 int nt = static_cast<int>(T/dt);
436 std::vector<ROL::TimeStamp<Real>> ts(nt);
437 for( int k=0; k<nt; ++k ) {
438 ts.at(k).t.resize(2);
439 ts.at(k).t.at(0) = k*dt;
440 ts.at(k).t.at(1) = (k+1)*dt;
441 }
442 // Solve Navier-Stokes equation to determine initial condition
443 zk->zero(); uo->set(*u0); un->zero();
444 Real unorm = uo->norm();
445 outStream << std::scientific << std::setprecision(6);
446 outStream << std::right << std::setw(8) << "ts"
447 << std::right << std::setw(16) << "||u(ts)||"
448 << std::right << std::setw(16) << "avg time (sec)"
449 << std::endl;
450 outStream << std::right << std::setw(8) << 0
451 << std::right << std::setw(16) << unorm
452 << std::right << std::setw(16) << "---"
453 << std::endl;
454 std::vector<Real> time(10);
455 std::clock_t timer_step;
456 Real time_avg(0);
457 for (int k = 1; k < nt; ++k) {
458 // Advance time stepper
459 timer_step = std::clock();
460 con->solve(*ck, *uo, *un, *zk, ts[k]);
461 time[k%10] = static_cast<Real>(std::clock()-timer_step)/static_cast<Real>(CLOCKS_PER_SEC);
462 uo->set(*un);
463 if ( k%10==0 ) {
464 unorm = uo->norm();
465 time_avg = 0.0;
466 for (int i = 0; i < 10; ++i) {
467 time_avg += time[i];
468 }
469 time_avg *= 0.1;
470 outStream << std::right << std::setw(8) << k
471 << std::right << std::setw(16) << unorm
472 << std::right << std::setw(16) << time_avg
473 << std::endl;
474 }
475 }
476 u0->set(*uo);
477 }
478
479 ROL::Ptr<ROL::Vector<RealT>>
480 solveKKTSystem(const std::string & prefix,
481 bool useWathenPrec,int myRank,
482 double absTol,double relTol,
483 const Teuchos::RCP<Teuchos::StackedTimer> & timer,
484 const ROL::Ptr<std::ostream> & outStream,
485 const ROL::Ptr<ROL::PinTConstraint<RealT>> & pint_con,
486 const ROL::Ptr<ROL::Vector<RealT>> & kkt_b,
487 const ROL::Ptr<ROL::PinTVector<RealT>> & state,
488 const ROL::Ptr<ROL::PinTVector<RealT>> & control)
489 {
490 using Teuchos::RCP;
491
492 RealT res0 = kkt_b->norm();
493 res0 = kkt_b->norm();
494
495 auto kkt_x_out = kkt_b->clone();
496 kkt_x_out->zero();
497
498 KKTOperator kktOperator;
499 kktOperator.pint_con = pint_con;
500 kktOperator.state = state;
501 kktOperator.control = control;
502 kktOperator.outStream = outStream;
503 kktOperator.myRank = myRank;
504
505 RCP<ROL::LinearOperator<RealT>> precOperator;
506
507 // build the preconditioner
508 if(useWathenPrec) {
509 RCP<WathenKKTOperator> wathenOperator(new WathenKKTOperator);
510 wathenOperator->pint_con = pint_con;
511 wathenOperator->state = state;
512 wathenOperator->control = control;
513 wathenOperator->outStream = outStream;
514 wathenOperator->myRank = myRank;
515
516 precOperator = wathenOperator;
517 }
518 else {
519 RCP<MGRITKKTOperator> mgOperator(new MGRITKKTOperator);
520 mgOperator->pint_con = pint_con;
521 mgOperator->state = state;
522 mgOperator->control = control;
523 mgOperator->outStream = outStream;
524 mgOperator->myRank = myRank;
525
526 precOperator = mgOperator;
527 }
528
529 Teuchos::ParameterList parlist;
530 ROL::ParameterList &krylovList = parlist.sublist("General").sublist("Krylov");
531 krylovList.set("Absolute Tolerance",absTol);
532 krylovList.set("Relative Tolerance",relTol);
533
534 if(myRank==0)
535 (*outStream) << "Relative tolerance = " << relTol << std::endl;
536
537 ROL::GMRES<RealT> krylov(parlist); // TODO: Do Belos
538 // ROL::MINRES<RealT> krylov(1e0, 1e-6, 200); // TODO: Do Belos
539
540 krylov.enableOutput(*outStream);
541
542 int flag = 0;
543 int iter = 0;
544
545
546 if(myRank==0) {
547 (*outStream) << "Solving KKT system: ";
548 if(useWathenPrec)
549 (*outStream) << "using Wathen preconditioner" << std::endl;
550 else
551 (*outStream) << "using MGRIT preconditioner" << std::endl;
552 }
553
554 MPI_Barrier(MPI_COMM_WORLD);
555 double t0 = MPI_Wtime();
556
557 timer->start("krylov");
558 RealT finalTol = krylov.run(*kkt_x_out,kktOperator,*kkt_b,*precOperator,iter,flag);
559 timer->stop("krylov");
560
561 MPI_Barrier(MPI_COMM_WORLD);
562 double tf = MPI_Wtime();
563
564 // Don't trust residual computation, so repeat.
565 auto myb = kkt_b->clone();
566 myb->set(*kkt_b);
567 auto norm_myb = myb->norm();
568 auto tmp = kkt_b->clone();
569 RealT dummytol = 1e-8;
570 kktOperator.apply(*tmp, *kkt_x_out, dummytol);
571 tmp->axpy(-1, *myb);
572 auto norm_myAxmb = tmp->norm();
573 auto norm_mysoln = kkt_x_out->norm();
574
575 if(myRank==0) {
576 (*outStream) << prefix << "Krylov Iteration = " << iter << " " << (finalTol / res0) << " " << tf-t0 << std::endl;
577 (*outStream) << "||x||=" << norm_mysoln << " ||b||=" << norm_myb << " ||Ax-b||=" << norm_myAxmb << std::endl;
578
579 const std::map<int,std::vector<double>> & preSmooth = pint_con->getPreSmoothResidualReductions();
580 const std::map<int,std::vector<double>> & postSmooth = pint_con->getPostSmoothResidualReduction();
581 const std::vector<double> & coarseSmooth = pint_con->getCoarseResidualReduction();
582
583 if(not useWathenPrec) {
584 // loop over presmoother residual reductions
585 *outStream << "Presmooth Reductions: " << std::endl;
586 for(auto iter_l : preSmooth) {
587 double average = std::accumulate( iter_l.second.begin(), iter_l.second.end(), 0.0)/coarseSmooth.size();
588 double min = *std::min_element(iter_l.second.begin(),iter_l.second.end());
589 double max = *std::max_element(iter_l.second.begin(),iter_l.second.end());
590 *outStream << " level ";
591 *outStream << "min,avg,max = " << min << ", " << average << ", " << max << std::endl;
592 }
593
594 // loop over postsmoother residual reductions
595 *outStream << "Postsmooth Reductions: " << std::endl;
596 for(auto iter_l : postSmooth) {
597 double average = std::accumulate( iter_l.second.begin(), iter_l.second.end(), 0.0)/coarseSmooth.size();
598 double min = *std::min_element(iter_l.second.begin(),iter_l.second.end());
599 double max = *std::max_element(iter_l.second.begin(),iter_l.second.end());
600 *outStream << " level ";
601 *outStream << "min,avg,max = " << min << ", " << average << ", " << max << std::endl;
602 }
603
604 // loop over coarse residual reductions
605 {
606 *outStream << "Coarse Reductions: ";
607 double average = std::accumulate( coarseSmooth.begin(), coarseSmooth.end(), 0.0)/coarseSmooth.size();
608 double min = *std::min_element(coarseSmooth.begin(),coarseSmooth.end());
609 double max = *std::max_element(coarseSmooth.begin(),coarseSmooth.end());
610 *outStream << "min,avg,max = " << min << ", " << average << ", " << max << std::endl;
611 *outStream << std::endl;
612 }
613 }
614 }
615
616 pint_con->clearResidualReduction();
617
618 return kkt_x_out;
619 }
620
621