Finite Element Domain Decomposition Library
FEDDLib
Loading...
Searching...
No Matches
LinearSolver_def.hpp
1#ifndef LinearSolver_DEF_hpp
2#define LinearSolver_DEF_hpp
3#include "LinearSolver_decl.hpp"
12namespace FEDD {
13
14
15template<class SC,class LO,class GO,class NO>
16LinearSolver<SC,LO,GO,NO>::LinearSolver(){}
17
18
19template<class SC,class LO,class GO,class NO>
20LinearSolver<SC,LO,GO,NO>::~LinearSolver(){}
21
22template<class SC,class LO,class GO,class NO>
23int LinearSolver<SC,LO,GO,NO>::solve(Problem_Type* problem, BlockMultiVectorPtr_Type rhs, std::string type ){
24
25 int its=0;
26 if (!type.compare("Monolithic") || !type.compare("MonolithicConstPrec"))
27 its = solveMonolithic( problem, rhs, type );
28 else if (!type.compare("Teko")){
29#ifdef FEDD_HAVE_TEKO
30// its = solveTeko( problem, rhs );
31 its = solveBlock( problem, rhs, "Teko" );
32#else
33 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "Teko not found! Build Trilinos with Teko.");
34#endif
35 }
36 else if (type=="Diagonal" || type=="Triangular"){
37 its = solveBlock( problem, rhs, type );
38 }
39 else
40 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "Unkown solver type.");
41
42 return its;
43}
44
45template<class SC,class LO,class GO,class NO>
46int LinearSolver<SC,LO,GO,NO>::solve(TimeProblem_Type* problem, BlockMultiVectorPtr_Type rhs, std::string type ){
47
48 int its=0;
49
50 if (!type.compare("Monolithic"))
51 its = solveMonolithic( problem, rhs );
52 else if (!type.compare("Teko")){
53#ifdef FEDD_HAVE_TEKO
54// its = solveTeko( problem, rhs );
55 its = solveBlock( problem, rhs, "Teko" );
56#else
57 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "Teko not found! Build Trilinos with Teko.");
58#endif
59 }
60 else if(!type.compare("FaCSI") || type == "FaCSI-Teko" )
61 its = solveBlock( problem, rhs, type );
62 else if (type=="Diagonal" || type=="Triangular")
63 its = solveBlock( problem, rhs, type );
64 else
65 TEUCHOS_TEST_FOR_EXCEPTION( true, std::logic_error, "Unkown solver type.");
66
67 return its;
68}
69
70template<class SC,class LO,class GO,class NO>
71int LinearSolver<SC,LO,GO,NO>::solveMonolithic(Problem_Type* problem, BlockMultiVectorPtr_Type rhs, std::string type ){
72
73 bool verbose(problem->getVerbose());
74 int its=0;
75 if (problem->getParameterList()->get("Zero Initial Guess",true)) {
76 problem->getSolution()->putScalar(0.);
77 }
78 Teuchos::RCP<Thyra::MultiVectorBase<SC> >thyraX = problem->getSolution()->getThyraMultiVector();
79
80 Teuchos::RCP<const Thyra::MultiVectorBase<SC> > thyraB;
81 if (rhs.is_null())
82 thyraB = problem->getRhs()->getThyraMultiVectorConst();
83 else
84 thyraB = rhs->getThyraMultiVectorConst();
85
86 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(), "ThyraSolver" );
87
88 pListThyraSolver->setParameters( problem->getParameterList()->sublist("ThyraPreconditioner") );
89
90 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
91 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy("");
92
93 bool iterativeSolve = !pListThyraSolver->get("Linear Solver Type", "Belos").compare("Belos");
94 if (iterativeSolve && (type != "MonolithicConstPrec" || problem->getPreconditioner()->getThyraPrec().is_null()))
95 problem->setupPreconditioner("Monolithic");
96
97 if (!pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").get("Level Combination","Additive").compare("Multiplicative")) {
98 pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",true);
99
100 Teuchos::RCP<const Thyra::LinearOpBase<SC> > thyra_linOp = problem->getPreconditioner()->getThyraPrec()->getUnspecifiedPrecOp();
101 Thyra::apply( *thyra_linOp, Thyra::NOTRANS, *thyraB, thyraX.ptr() );
102 pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",false);
103 }
104
105 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
106
107 lowsFactory->setOStream(out);
108 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
109
110 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
111// Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = linearOpWithSolve(*lowsFactory, problem->getSystem()->getThyraLinOp());
112 ThyraLinOpConstPtr_Type thyraMatrix = problem->getSystem()->getThyraLinOp();
113 if ( iterativeSolve ) {
114 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
115 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
116 }
117 else{
118 Thyra::initializeOp<SC>(*lowsFactory, thyraMatrix, solver.ptr());
119 }
120
121 {
122 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraB, thyraX.ptr());
123 if (verbose)
124 std::cout << status << std::endl;
125 if ( !pListThyraSolver->get("Linear Solver Type","Belos").compare("Belos") )
126 its = status.extraParameters->get("Belos/Iteration Count",0);
127 else
128 its = 0;
129
130 problem->getSolution()->fromThyraMultiVector(thyraX);
131 }
132
133 return its;
134}
135
136template<class SC,class LO,class GO,class NO>
137int LinearSolver<SC,LO,GO,NO>::solveMonolithic(TimeProblem_Type* timeProblem, BlockMultiVectorPtr_Type rhs){
138
139
140 bool verbose(timeProblem->getVerbose());
141 int its=0;
142 // timeProblem->getSystem()->getBlock(0,0)->writeMM("System");
143 ProblemPtr_Type problem = timeProblem->getUnderlyingProblem();
144
145 if (problem->getParameterList()->get("Zero Initial Guess",true)) {
146 problem->getSolution()->putScalar(0.);
147 }
148
149 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(), "ThyraSolver" );
150
151 Teuchos::RCP<Thyra::MultiVectorBase<SC> >thyraX = problem->getSolution()->getThyraMultiVector();
152 Teuchos::RCP<const Thyra::MultiVectorBase<SC> > thyraB;
153
154 if (rhs.is_null())
155 thyraB = problem->getRhs()->getThyraMultiVectorConst();
156 else
157 thyraB = rhs->getThyraMultiVectorConst();
158
159 pListThyraSolver->setParameters( problem->getParameterList()->sublist("ThyraPreconditioner") );
160
161 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
162 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy("");
163
164
165 problem->setupPreconditioner( "Monolithic" );
166
167 if (!pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").get("Level Combination","Additive").compare("Multiplicative")) {
168 pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",true);
169
170 Teuchos::RCP<const Thyra::LinearOpBase<SC> > thyra_linOp = problem->getPreconditioner()->getThyraPrec()->getUnspecifiedPrecOp();
171 Thyra::apply( *thyra_linOp, Thyra::NOTRANS, *thyraB, thyraX.ptr() );
172 pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",false);
173 }
174
175 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
176
177 lowsFactory->setOStream(out);
178 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
179
180 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
181 // solver = linearOpWithSolve(*lowsFactory, problem->getSystem()->getThyraLinOp());
182
183 //timeProblem->combineSystems();
184 // timeProblem->getSystemCombined()->getBlock(0,0)->writeMM("SystemCombined");
185
186 ThyraLinOpConstPtr_Type thyraMatrix = timeProblem->getSystemCombined()->getThyraLinOp();
187
188 // Printing the stiffness matrix for the first newton iteration
189 // timeProblem->getSystemCombined()->writeMM("stiffnessMatrixWihtDirichlet");
190 // timeProblem->getSystem()->writeMM("stiffnessMatrixFull");
191 // rhs->writeMM("rhs");
192
193 if ( !pListThyraSolver->get("Linear Solver Type","Belos").compare("Belos") ) {
194 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
195 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
196 }
197 else{
198 Thyra::initializeOp<SC>(*lowsFactory, thyraMatrix, solver.ptr());
199 }
200 {
201 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraB, thyraX.ptr());
202 if (verbose)
203 std::cout << status << std::endl;
204 problem->getSolution()->fromThyraMultiVector(thyraX);
205 // problem->getSolution()->writeMM("solution");
206 if ( !pListThyraSolver->get("Linear Solver Type","Belos").compare("Belos") ){
207 its = status.extraParameters->get("Belos/Iteration Count",0);
208 double achievedTol = status.extraParameters->get("Belos/Achieved Tolerance",-1.);
209 }
210 else
211 its = 0;
212 }
213 return its;
214}
215
216#ifdef FEDD_HAVE_TEKO
217template<class SC,class LO,class GO,class NO>
218int LinearSolver<SC,LO,GO,NO>::solveTeko(Problem_Type* problem, BlockMultiVectorPtr_Type rhs ){
219
220 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
221
222 bool verbose(problem->getVerbose());
223 int its=0;
224 if (problem->getParameterList()->get("Zero Initial Guess",true)) {
225 problem->getSolution()->putScalar(0.);
226 }
227 // typedef Teuchos::RCP< Thyra::ProductMultiVectorBase< double > > Teko::BlockedMultiVector
228 // convert them to teko compatible sub vectors
229 Teko::MultiVector x0_th = problem->getSolution()->getBlockNonConst(0)->getThyraMultiVector();
230 Teko::MultiVector x1_th = problem->getSolution()->getBlockNonConst(1)->getThyraMultiVector();
231
232 Teko::MultiVector b0_th;
233 Teko::MultiVector b1_th;
234 if (rhs.is_null()){
235 b0_th = problem->getRhs()->getBlockNonConst(0)->getThyraMultiVector();
236 b1_th = problem->getRhs()->getBlockNonConst(1)->getThyraMultiVector();
237 }
238 else{
239 b0_th = rhs->getBlockNonConst(0)->getThyraMultiVector();
240 b1_th = rhs->getBlockNonConst(1)->getThyraMultiVector();
241 }
242
243 std::vector<Teko::MultiVector> x_vec; x_vec.push_back(x0_th); x_vec.push_back(x1_th);
244 std::vector<Teko::MultiVector> b_vec; b_vec.push_back(b0_th); b_vec.push_back(b1_th);
245
246 Teko::MultiVector x = Teko::buildBlockedMultiVector(x_vec); // these will be used in the Teko solve
247 Teko::MultiVector b = Teko::buildBlockedMultiVector(b_vec);
248
249 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(), "ThyraSolver" );
250
251// pListThyraSolver->setParameters( problem->getParameterList()->sublist("ThyraPreconditioner") );
252
253 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
254 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy("");
255
256 problem->setupPreconditioner( "Teko" );
257
258 // if (!pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").get("Level Combination","Additive").compare("Multiplicative")) {
259 // pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",true);
260 //
261 // Teuchos::RCP<const Thyra::LinearOpBase<SC> > thyra_linOp = problem->getPreconditioner()->getThyraPrec()->getUnspecifiedPrecOp();
262 // Thyra::apply( *thyra_linOp, Thyra::NOTRANS, *thyraB, thyraX.ptr() );
263 // pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",false);
264 // }
265
266
267 lowsFactory->setOStream(out);
268// lowsFactory->setVerbLevel(Teuchos::VERB_EXTREME);
269
270 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
271
272 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
273
274 ThyraLinOpConstPtr_Type thyraMatrix = problem->getPreconditioner()->getTekoOp();
275
276 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
277
278 {
279 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *b, x.ptr());
280 if (verbose)
281 std::cout << status << std::endl;
282
283 its = status.extraParameters->get("Belos/Iteration Count",0);
284 Teuchos::RCP< Thyra::ProductMultiVectorBase< double > > xBlocked = Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase< double > >( x );
285 problem->getSolution()->getBlockNonConst(0)->fromThyraMultiVector( xBlocked->getNonconstMultiVectorBlock( 0 ) );
286 problem->getSolution()->getBlockNonConst(1)->fromThyraMultiVector( xBlocked->getNonconstMultiVectorBlock( 1 ) );
287 }
288
289 return its;
290}
291
292template<class SC,class LO,class GO,class NO>
293int LinearSolver<SC,LO,GO,NO>::solveTeko(TimeProblem_Type* timeProblem, BlockMultiVectorPtr_Type rhs ){
294
295 bool verbose(timeProblem->getVerbose());
296 int its=0;
297
298 ProblemPtr_Type problem = timeProblem->getUnderlyingProblem();
299 if (problem->getParameterList()->get("Zero Initial Guess",true)) {
300 problem->getSolution()->putScalar(0.);
301 }
302 // typedef Teuchos::RCP< Thyra::ProductMultiVectorBase< double > > Teko::BlockedMultiVector
303 // convert them to teko compatible sub vectors
304 Teko::MultiVector x0_th = problem->getSolution()->getBlockNonConst(0)->getThyraMultiVector();
305 Teko::MultiVector x1_th = problem->getSolution()->getBlockNonConst(1)->getThyraMultiVector();
306
307 Teko::MultiVector b0_th;
308 Teko::MultiVector b1_th;
309 if (rhs.is_null()){
310 b0_th = problem->getRhs()->getBlockNonConst(0)->getThyraMultiVector();
311 b1_th = problem->getRhs()->getBlockNonConst(1)->getThyraMultiVector();
312 }
313 else{
314 b0_th = rhs->getBlockNonConst(0)->getThyraMultiVector();
315 b1_th = rhs->getBlockNonConst(1)->getThyraMultiVector();
316 }
317
318 std::vector<Teko::MultiVector> x_vec; x_vec.push_back(x0_th); x_vec.push_back(x1_th);
319 std::vector<Teko::MultiVector> b_vec; b_vec.push_back(b0_th); b_vec.push_back(b1_th);
320
321 Teko::MultiVector x = Teko::buildBlockedMultiVector(x_vec); // these will be used in the Teko solve
322 Teko::MultiVector b = Teko::buildBlockedMultiVector(b_vec);
323
324 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(), "ThyraSolver" );
325
326// pListThyraSolver->setParameters( problem->getParameterList()->sublist("ThyraPreconditioner") );
327
328 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
329 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy("");
330
331 problem->setupPreconditioner( "Teko" );
332
333// if (!pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").get("Level Combination","Additive").compare("Multiplicative")) {
334// pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",true);
335//
336// Teuchos::RCP<const Thyra::LinearOpBase<SC> > thyra_linOp = problem->getPreconditioner()->getThyraPrec()->getUnspecifiedPrecOp();
337// Thyra::apply( *thyra_linOp, Thyra::NOTRANS, *thyraB, thyraX.ptr() );
338// pListThyraSolver->sublist("Preconditioner Types").sublist("FROSch").set("Only apply coarse",false);
339// }
340
341 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
342
343 lowsFactory->setOStream(out);
344 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
345
346 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
347
348 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
349
350 ThyraLinOpConstPtr_Type thyraMatrix = problem->getPreconditioner()->getTekoOp();
351
352 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
353
354 {
355 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *b, x.ptr());
356 if (verbose)
357 std::cout << status << std::endl;
358
359 its = status.extraParameters->get("Belos/Iteration Count",0);
360 Teuchos::RCP< Thyra::ProductMultiVectorBase< double > > xBlocked = Teuchos::rcp_dynamic_cast<Thyra::ProductMultiVectorBase< double > >( x );
361 problem->getSolution()->getBlockNonConst(0)->fromThyraMultiVector( xBlocked->getNonconstMultiVectorBlock( 0 ) );
362 problem->getSolution()->getBlockNonConst(1)->fromThyraMultiVector( xBlocked->getNonconstMultiVectorBlock( 1 ) );
363 }
364
365 return its;
366}
367#endif
368
369template<class SC,class LO,class GO,class NO>
370int LinearSolver<SC,LO,GO,NO>::solveBlock(Problem_Type* problem, BlockMultiVectorPtr_Type rhs, std::string type ){
371 typedef Thyra::DefaultZeroLinearOp<SC> ZeroOp_Type;
372 typedef Teuchos::RCP<ZeroOp_Type> ZeroOpPtr_Type;
373
374 int rank = problem->getComm()->getRank();
375 bool verbose(problem->getVerbose());
376 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
377
378 int its=0;
379
380 if (problem->getParameterList()->get("Zero Initial Guess",true)) {
381 problem->getSolution()->putScalar(0.);
382 }
383
384
385 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraX = problem->getSolution()->getProdThyraMultiVector();
386
387 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraRHS;
388 if ( rhs.is_null() )
389 thyraRHS = problem->getRhs()->getProdThyraMultiVector();
390 else
391 thyraRHS = rhs->getProdThyraMultiVector();
392
393 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(), "ThyraSolver" );
394
395 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
396 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy("");
397
398 problem->setupPreconditioner( type );
399
400 lowsFactory->setOStream(out);
401 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
402
403 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
404
405 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
406
407// BlockMatrixPtr_Type system = problem->getSystem();
408// ThyraLinOpBlockConstPtr_Type thyraMatrixBlocks = system->getThyraLinBlockOp();
409// ThyraLinOpBlockPtr_Type thyraMatrixBlocksNonConst = Teuchos::rcp_const_cast<ThyraLinOpBlock_Type>( thyraMatrixBlocks );
410//
411// for (int i=0; i<system->size(); i++) {
412// for (int j=0; j<system->size(); j++) {
413// if ( !thyraMatrixBlocks->blockExists(i,j) ) {
414// ZeroOpPtr_Type zeroOp =
415// Teuchos::rcp( new ZeroOp_Type( thyraRHS->getMultiVectorBlock(i)->range(),
416// thyraX->getMultiVectorBlock(j)->range() ) ); //(range , domain)
417// thyraMatrixBlocksNonConst->getNonconstBlock(i,j) = zeroOp;
418// }
419// }
420// }
421
422
423
424// BlockMatrixPtr_Type system = problem->getSystem();
425// for (int i=0; i<system->size(); i++) {
426// for (int j=0; j<system->size(); j++) {
427// if (!system->blockExists(1,1)){
428// MatrixPtr_Type dummy = Teuchos::rcp( new Matrix_Type( system->getBlock(i,j)->getMap(), 0 ) );
429// dummy->fillComplete( problem->getSolution()->getBlock( j )->getMap(), problem->getRhs()->getBlock( i )->getMap() ); //(domain, range)
430// system->addBlock( dummy, i, j );
431// }
432// }
433// }
434
435 ThyraLinOpConstPtr_Type thyraMatrix = problem->getSystem()->getThyraLinBlockOp();
436 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
437 {
438
439 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraRHS, thyraX.ptr());
440 if (verbose)
441 std::cout << status << std::endl;
442
443 its = status.extraParameters->get("Belos/Iteration Count",0);
444
445 problem->getSolution()->fromThyraProdMultiVector( thyraX );
446 }
447
448 return its;
449}
450
451template<class SC,class LO,class GO,class NO>
452int LinearSolver<SC,LO,GO,NO>::solveBlock(TimeProblem_Type* timeProblem, BlockMultiVectorPtr_Type rhs, std::string type ){
453 typedef Thyra::DefaultZeroLinearOp<SC> ZeroOp_Type;
454 typedef Teuchos::RCP<ZeroOp_Type> ZeroOpPtr_Type;
455
456 int rank = timeProblem->getComm()->getRank();
457 bool verbose(timeProblem->getVerbose());
458 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
459
460 int its=0;
461
462 ProblemPtr_Type problem = timeProblem->getUnderlyingProblem();
463 if (problem->getParameterList()->get("Zero Initial Guess",true)) {
464 problem->getSolution()->putScalar(0.);
465 }
466
467
468 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraX = problem->getSolution()->getProdThyraMultiVector();
469
470 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraRHS;
471 if ( rhs.is_null() )
472 thyraRHS = problem->getRhs()->getProdThyraMultiVector();
473 else
474 thyraRHS = rhs->getProdThyraMultiVector();
475
476 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(), "ThyraSolver" );
477
478
479 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
480 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy("");
481
482 problem->setupPreconditioner( type );
483
484 lowsFactory->setOStream(out);
485 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
486
487 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
488
489 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
490
491// BlockMatrixPtr_Type system = timeProblem->getSystemCombined();
492// ThyraLinOpBlockConstPtr_Type thyraMatrixBlocks = system->getThyraLinBlockOp();
493// ThyraLinOpBlockPtr_Type thyraMatrixBlocksNonConst = Teuchos::rcp_const_cast<ThyraLinOpBlock_Type>( thyraMatrixBlocks );
494//
495// for (int i=0; i<system->size(); i++) {
496// for (int j=0; j<system->size(); j++) {
497// if ( !thyraMatrixBlocks->blockExists(i,j) ) {
498// ZeroOpPtr_Type zeroOp =
499// Teuchos::rcp( new ZeroOp_Type( thyraRHS->getMultiVectorBlock(i)->range(),
500// thyraX->getMultiVectorBlock(j)->range() ) ); //(range , domain)
501// thyraMatrixBlocksNonConst->getNonconstBlock(i,j) = zeroOp;
502// }
503// }
504// }
505
506 BlockMatrixPtr_Type system = timeProblem->getSystemCombined();
507// for (int i=0; i<system->size(); i++) {
508// for (int j=0; j<system->size(); j++) {
509// if (!system->blockExists(1,1)){
510// MatrixPtr_Type dummy;
511// dummy.reset( new Matrix_Type( system->getBlock(i,j)->getMap(), 0 ) );
512// dummy->fillComplete( problem->getSolution()->getBlock( j )->getMap(), problem->getRhs()->getBlock( i )->getMap() ); //(domain, range)
513// system->addBlock( dummy, i, j );
514// }
515// }
516// }
517
518 ThyraLinOpConstPtr_Type thyraMatrix = timeProblem->getSystemCombined()->getThyraLinBlockOp();
519// ThyraLinOpBlockConstPtr_Type thyraMatrixBlock = timeProblem->getSystemCombined()->getThyraLinBlockOp();
520 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
521 {
522
523 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraRHS, thyraX.ptr());
524 if (verbose)
525 std::cout << status << std::endl;
526
527 its = status.extraParameters->get("Belos/Iteration Count",0);
528
529 problem->getSolution()->fromThyraProdMultiVector( thyraX );
530 }
531
532 return its;
533}
534}
535#endif
Definition LinearSolver_decl.hpp:25
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement.cpp:5