1#ifndef LinearSolver_DEF_hpp
2#define LinearSolver_DEF_hpp
3#include "LinearSolver_decl.hpp"
15template<
class SC,
class LO,
class GO,
class NO>
16LinearSolver<SC,LO,GO,NO>::LinearSolver(){}
19template<
class SC,
class LO,
class GO,
class NO>
20LinearSolver<SC,LO,GO,NO>::~LinearSolver(){}
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 ){
26 if (!type.compare(
"Monolithic") || !type.compare(
"MonolithicConstPrec"))
27 its = solveMonolithic( problem, rhs, type );
28 else if (!type.compare(
"Teko")){
31 its = solveBlock( problem, rhs,
"Teko" );
33 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Teko not found! Build Trilinos with Teko.");
36 else if (type==
"Diagonal" || type==
"Triangular"){
37 its = solveBlock( problem, rhs, type );
40 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unkown solver type.");
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 ){
50 if (!type.compare(
"Monolithic"))
51 its = solveMonolithic( problem, rhs );
52 else if (!type.compare(
"Teko")){
55 its = solveBlock( problem, rhs,
"Teko" );
57 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Teko not found! Build Trilinos with Teko.");
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 );
65 TEUCHOS_TEST_FOR_EXCEPTION(
true, std::logic_error,
"Unkown solver type.");
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 ){
73 bool verbose(problem->getVerbose());
75 if (problem->getParameterList()->get(
"Zero Initial Guess",
true)) {
76 problem->getSolution()->putScalar(0.);
78 Teuchos::RCP<Thyra::MultiVectorBase<SC> >thyraX = problem->getSolution()->getThyraMultiVector();
80 Teuchos::RCP<const Thyra::MultiVectorBase<SC> > thyraB;
82 thyraB = problem->getRhs()->getThyraMultiVectorConst();
84 thyraB = rhs->getThyraMultiVectorConst();
86 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(),
"ThyraSolver" );
88 pListThyraSolver->setParameters( problem->getParameterList()->sublist(
"ThyraPreconditioner") );
90 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
91 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy(
"");
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");
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);
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);
105 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
107 lowsFactory->setOStream(out);
108 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
110 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
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());
118 Thyra::initializeOp<SC>(*lowsFactory, thyraMatrix, solver.ptr());
122 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraB, thyraX.ptr());
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);
130 problem->getSolution()->fromThyraMultiVector(thyraX);
136template<
class SC,
class LO,
class GO,
class NO>
137int LinearSolver<SC,LO,GO,NO>::solveMonolithic(TimeProblem_Type* timeProblem, BlockMultiVectorPtr_Type rhs){
140 bool verbose(timeProblem->getVerbose());
143 ProblemPtr_Type problem = timeProblem->getUnderlyingProblem();
145 if (problem->getParameterList()->get(
"Zero Initial Guess",
true)) {
146 problem->getSolution()->putScalar(0.);
149 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(),
"ThyraSolver" );
151 Teuchos::RCP<Thyra::MultiVectorBase<SC> >thyraX = problem->getSolution()->getThyraMultiVector();
152 Teuchos::RCP<const Thyra::MultiVectorBase<SC> > thyraB;
155 thyraB = problem->getRhs()->getThyraMultiVectorConst();
157 thyraB = rhs->getThyraMultiVectorConst();
159 pListThyraSolver->setParameters( problem->getParameterList()->sublist(
"ThyraPreconditioner") );
161 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
162 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy(
"");
165 problem->setupPreconditioner(
"Monolithic" );
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);
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);
175 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
177 lowsFactory->setOStream(out);
178 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
180 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
186 ThyraLinOpConstPtr_Type thyraMatrix = timeProblem->getSystemCombined()->getThyraLinOp();
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());
198 Thyra::initializeOp<SC>(*lowsFactory, thyraMatrix, solver.ptr());
201 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraB, thyraX.ptr());
203 std::cout << status << std::endl;
204 problem->getSolution()->fromThyraMultiVector(thyraX);
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.);
217template<
class SC,
class LO,
class GO,
class NO>
220 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
222 bool verbose(problem->getVerbose());
224 if (problem->getParameterList()->get(
"Zero Initial Guess",
true)) {
225 problem->getSolution()->putScalar(0.);
229 Teko::MultiVector x0_th = problem->getSolution()->getBlockNonConst(0)->getThyraMultiVector();
230 Teko::MultiVector x1_th = problem->getSolution()->getBlockNonConst(1)->getThyraMultiVector();
232 Teko::MultiVector b0_th;
233 Teko::MultiVector b1_th;
235 b0_th = problem->getRhs()->getBlockNonConst(0)->getThyraMultiVector();
236 b1_th = problem->getRhs()->getBlockNonConst(1)->getThyraMultiVector();
239 b0_th = rhs->getBlockNonConst(0)->getThyraMultiVector();
240 b1_th = rhs->getBlockNonConst(1)->getThyraMultiVector();
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);
246 Teko::MultiVector x = Teko::buildBlockedMultiVector(x_vec);
247 Teko::MultiVector b = Teko::buildBlockedMultiVector(b_vec);
249 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(),
"ThyraSolver" );
253 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
254 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy(
"");
256 problem->setupPreconditioner(
"Teko" );
267 lowsFactory->setOStream(out);
270 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
272 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
274 ThyraLinOpConstPtr_Type thyraMatrix = problem->getPreconditioner()->getTekoOp();
276 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
279 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *b, x.ptr());
281 std::cout << status << std::endl;
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 ) );
292template<
class SC,
class LO,
class GO,
class NO>
295 bool verbose(timeProblem->getVerbose());
298 ProblemPtr_Type problem = timeProblem->getUnderlyingProblem();
299 if (problem->getParameterList()->get(
"Zero Initial Guess",
true)) {
300 problem->getSolution()->putScalar(0.);
304 Teko::MultiVector x0_th = problem->getSolution()->getBlockNonConst(0)->getThyraMultiVector();
305 Teko::MultiVector x1_th = problem->getSolution()->getBlockNonConst(1)->getThyraMultiVector();
307 Teko::MultiVector b0_th;
308 Teko::MultiVector b1_th;
310 b0_th = problem->getRhs()->getBlockNonConst(0)->getThyraMultiVector();
311 b1_th = problem->getRhs()->getBlockNonConst(1)->getThyraMultiVector();
314 b0_th = rhs->getBlockNonConst(0)->getThyraMultiVector();
315 b1_th = rhs->getBlockNonConst(1)->getThyraMultiVector();
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);
321 Teko::MultiVector x = Teko::buildBlockedMultiVector(x_vec);
322 Teko::MultiVector b = Teko::buildBlockedMultiVector(b_vec);
324 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(),
"ThyraSolver" );
328 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
329 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy(
"");
331 problem->setupPreconditioner(
"Teko" );
341 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
343 lowsFactory->setOStream(out);
344 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
346 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
348 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
350 ThyraLinOpConstPtr_Type thyraMatrix = problem->getPreconditioner()->getTekoOp();
352 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
355 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *b, x.ptr());
357 std::cout << status << std::endl;
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 ) );
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;
374 int rank = problem->getComm()->getRank();
375 bool verbose(problem->getVerbose());
376 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
380 if (problem->getParameterList()->get(
"Zero Initial Guess",
true)) {
381 problem->getSolution()->putScalar(0.);
385 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraX = problem->getSolution()->getProdThyraMultiVector();
387 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraRHS;
389 thyraRHS = problem->getRhs()->getProdThyraMultiVector();
391 thyraRHS = rhs->getProdThyraMultiVector();
393 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(),
"ThyraSolver" );
395 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
396 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy(
"");
398 problem->setupPreconditioner( type );
400 lowsFactory->setOStream(out);
401 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
403 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
405 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
435 ThyraLinOpConstPtr_Type thyraMatrix = problem->getSystem()->getThyraLinBlockOp();
436 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
439 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraRHS, thyraX.ptr());
441 std::cout << status << std::endl;
443 its = status.extraParameters->get(
"Belos/Iteration Count",0);
445 problem->getSolution()->fromThyraProdMultiVector( thyraX );
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;
456 int rank = timeProblem->getComm()->getRank();
457 bool verbose(timeProblem->getVerbose());
458 Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream();
462 ProblemPtr_Type problem = timeProblem->getUnderlyingProblem();
463 if (problem->getParameterList()->get(
"Zero Initial Guess",
true)) {
464 problem->getSolution()->putScalar(0.);
468 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraX = problem->getSolution()->getProdThyraMultiVector();
470 Teuchos::RCP< Thyra::ProductMultiVectorBase<SC> > thyraRHS;
472 thyraRHS = problem->getRhs()->getProdThyraMultiVector();
474 thyraRHS = rhs->getProdThyraMultiVector();
476 ParameterListPtr_Type pListThyraSolver = sublist( problem->getParameterList(),
"ThyraSolver" );
479 problem->getLinearSolverBuilder()->setParameterList(pListThyraSolver);
480 Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<SC> > lowsFactory = problem->getLinearSolverBuilder()->createLinearSolveStrategy(
"");
482 problem->setupPreconditioner( type );
484 lowsFactory->setOStream(out);
485 lowsFactory->setVerbLevel(Teuchos::VERB_HIGH);
487 Teuchos::RCP<Thyra::LinearOpWithSolveBase<SC> > solver = lowsFactory->createOp();
489 ThyraPrecPtr_Type thyraPrec = problem->getPreconditioner()->getThyraPrec();
506 BlockMatrixPtr_Type system = timeProblem->getSystemCombined();
518 ThyraLinOpConstPtr_Type thyraMatrix = timeProblem->getSystemCombined()->getThyraLinBlockOp();
520 Thyra::initializePreconditionedOp<SC>(*lowsFactory, thyraMatrix, thyraPrec.getConst(), solver.ptr());
523 Thyra::SolveStatus<SC> status = Thyra::solve<SC>(*solver, Thyra::NOTRANS, *thyraRHS, thyraX.ptr());
525 std::cout << status << std::endl;
527 its = status.extraParameters->get(
"Belos/Iteration Count",0);
529 problem->getSolution()->fromThyraProdMultiVector( thyraX );
Definition LinearSolver_decl.hpp:25
Adaptive Mesh Refinement.
Definition AdaptiveMeshRefinement.cpp:5