10#ifndef THYRA_LINEAR_OP_WITH_SOLVE_EXAMPLES_HPP
11#define THYRA_LINEAR_OP_WITH_SOLVE_EXAMPLES_HPP
14#include "Thyra_LinearOpWithSolveFactoryBase.hpp"
15#include "Thyra_LinearOpWithSolveFactoryHelpers.hpp"
16#include "Thyra_LinearOpWithSolveBase.hpp"
17#include "Thyra_PreconditionerFactoryHelpers.hpp"
18#include "Thyra_DefaultScaledAdjointLinearOp.hpp"
19#include "Thyra_DefaultPreconditioner.hpp"
20#include "Thyra_MultiVectorStdOps.hpp"
21#include "Thyra_VectorStdOps.hpp"
22#include "Thyra_VectorBase.hpp"
75void singleLinearSolve(
84 using Teuchos::rcpFromRef;
86 out <<
"\nPerforming a single linear solve ...\n";
94 out <<
"\nSolve status:\n" << status;
103template<
class Scalar>
105createScaledAdjointLinearOpWithSolve(
107 const Scalar &scalar,
113 out <<
"\nCreating a scaled adjoint LinearOpWithSolveBase object ...\n";
117 out <<
"\nCreated LOWSB object:\n" << describe(*invertibleAdjointA,
119 return invertibleAdjointA;
128template<
class Scalar>
129void solveNumericalChangeSolve(
140 using Teuchos::as;
using Teuchos::ptr;
using Teuchos::rcpFromPtr;
142 out <<
"\nPerforming a solve, changing the operator, then performing another"
177template<
class Scalar>
178void solveSmallNumericalChangeSolve(
189 using Teuchos::ptr;
using Teuchos::as;
using Teuchos::rcpFromPtr;
191 out <<
"\nPerforming a solve, changing the operator in a very small way,"
192 <<
" then performing another solve ...\n";
225template<
class Scalar>
226void solveMajorChangeSolve(
239 out <<
"\nPerforming a solve, changing the operator in a major way, then performing"
240 <<
" another solve ...\n";
260 invertibleA = lowsFactory.
createOp();
279template<
class Scalar>
281createGeneralPreconditionedLinearOpWithSolve(
289 out <<
"\nCreating an externally preconditioned LinearOpWithSolveBase object ...\n";
304template<
class Scalar>
306createUnspecifiedPreconditionedLinearOpWithSolve(
314 out <<
"\nCreating an LinearOpWithSolveBase object given a preconditioner operator"
315 <<
" not targeted to the left or right ...\n";
333template<
class Scalar>
335createLeftPreconditionedLinearOpWithSolve(
343 out <<
"\nCreating an LinearOpWithSolveBase object given a left preconditioner"
344 <<
" operator ...\n";
360template<
class Scalar>
362createRightPreconditionedLinearOpWithSolve(
370 out <<
"\nCreating an LinearOpWithSolveBase object given a right"
371 <<
" preconditioner operator ...\n";
387template<
class Scalar>
389createLeftRightPreconditionedLinearOpWithSolve(
398 out <<
"\nCreating an LinearOpWithSolveBase object given a left and"
399 <<
"right preconditioner operator ...\n";
415template<
class Scalar>
417createMatrixPreconditionedLinearOpWithSolve(
425 out <<
"\nCreating a LinearOpWithSolveBase object given an approximate forward"
426 <<
" operator to define the preconditioner ...\n";
440template<
class Scalar>
441void externalPreconditionerReuseWithSolves(
453 using Teuchos::tab;
using Teuchos::rcpFromPtr;
456 out <<
"\nShowing resuse of the preconditioner ...\n";
470 out <<
"\nSolve status:\n" << status1;
488 out <<
"\nSolve status:\n" << status2;
502template<
class Scalar>
503void nonExternallyPreconditionedLinearSolveUseCases(
506 bool supportsAdjoints,
512 out <<
"\nRunning example use cases for a LinearOpWithSolveFactoryBase object ...\n";
527 singleLinearSolve(A, lowsFactory, *b1, x1.
ptr(), out);
529 if(supportsAdjoints) {
531 invertibleAdjointA = createScaledAdjointLinearOpWithSolve(
532 Teuchos::rcp(&A,
false),as<Scalar>(2.0),lowsFactory,out);
535 solveNumericalChangeSolve<Scalar>(
538 lowsFactory, *b1, x1.
ptr(), *b2, x1.
ptr(), out );
540 solveSmallNumericalChangeSolve<Scalar>(
543 lowsFactory, *b1, x1.
ptr(), *b2, x1.
ptr(), out );
545 solveMajorChangeSolve<Scalar>(
548 lowsFactory, *b1, x1.
ptr(), *b2, x1.
ptr(), out );
557template<
class Scalar>
558void externallyPreconditionedLinearSolveUseCases(
562 const bool supportsLeftPrec,
563 const bool supportsRightPrec,
569 out <<
"\nRunning example use cases with an externally defined"
570 <<
" preconditioner with a LinearOpWithSolveFactoryBase object ...\n";
596 invertibleA = createGeneralPreconditionedLinearOpWithSolve<Scalar>(
603 if (nonnull (P_op = P->getUnspecifiedPrecOp ()))
605 else if (nonnull (P_op = P->getLeftPrecOp ()))
607 else if (nonnull (P_op = P->getRightPrecOp ()))
610 invertibleA = createUnspecifiedPreconditionedLinearOpWithSolve(
611 rcpFromRef(A), P_op, lowsFactory, out);
613 if(supportsLeftPrec) {
614 invertibleA = createLeftPreconditionedLinearOpWithSolve(
615 rcpFromRef(A), P_op, lowsFactory,out);
618 if(supportsRightPrec) {
619 invertibleA = createRightPreconditionedLinearOpWithSolve(
620 rcpFromRef(A), P_op, lowsFactory, out);
624 if( supportsLeftPrec && supportsRightPrec ) {
625 invertibleA = createLeftRightPreconditionedLinearOpWithSolve(
626 rcpFromRef(A), P_op, P_op, lowsFactory, out);
630 invertibleA = createMatrixPreconditionedLinearOpWithSolve<Scalar>(
631 rcpFromRef(A), rcpFromRef(A), lowsFactory,out);
633 externalPreconditionerReuseWithSolves<Scalar>(
636 lowsFactory, precFactory,
637 *b1, x1.
ptr(), *b2, x2.ptr(), out );
RCP< const T > getConst() const
Teuchos::RCP< const DefaultPreconditioner< Scalar > > rightPrec(const Teuchos::RCP< const LinearOpBase< Scalar > > &rightPrecOp)
Create a precondioner from a single linear operator targeted to be used on the right.
Teuchos::RCP< const DefaultPreconditioner< Scalar > > leftPrec(const Teuchos::RCP< const LinearOpBase< Scalar > > &leftPrecOp)
Create a precondioner from a single linear operator targeted to be used on the left.
Teuchos::RCP< const DefaultPreconditioner< Scalar > > unspecifiedPrec(const Teuchos::RCP< const LinearOpBase< Scalar > > &unspecifiedPrecOp)
Create a precondioner from a single linear operator not targeted to be used on the left or the right.
Teuchos::RCP< const DefaultPreconditioner< Scalar > > splitPrec(const Teuchos::RCP< const LinearOpBase< Scalar > > &leftPrecOp, const Teuchos::RCP< const LinearOpBase< Scalar > > &rightPrecOp)
Create a split precondioner from two linear operators, one to be applied on the left and one to be ap...
RCP< const LinearOpBase< Scalar > > scale(const Scalar &scalar, const RCP< const LinearOpBase< Scalar > > &Op, const std::string &label="")
Build an implicit const scaled linear operator.
RCP< const LinearOpBase< Scalar > > adjoint(const RCP< const LinearOpBase< Scalar > > &Op, const std::string &label="")
Build an implicit const adjoined linear operator.
Base class for all linear operators.
virtual RCP< const VectorSpaceBase< Scalar > > range() const =0
Return a smart pointer for the range space for this operator.
virtual RCP< const VectorSpaceBase< Scalar > > domain() const =0
Return a smart pointer for the domain space for this operator.
Silly abstract strategy interface for changing Thyra::LinearOpBase objects.
virtual ~LinearOpChanger()
virtual void changeOp(const Teuchos::Ptr< LinearOpBase< Scalar > > &op) const =0
SolveStatus< Scalar > solve(const LinearOpWithSolveBase< Scalar > &A, const EOpTransp A_trans, const MultiVectorBase< Scalar > &B, const Ptr< MultiVectorBase< Scalar > > &X, const Ptr< const SolveCriteria< Scalar > > solveCriteria=Teuchos::null)
Call solve() as a non-member function.
Factory interface for creating LinearOpWithSolveBase objects from compatible LinearOpBase objects.
void initializeApproxPreconditionedOp(const LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const RCP< const LinearOpBase< Scalar > > &fwdOp, const RCP< const LinearOpBase< Scalar > > &approxFwdOp, const Ptr< LinearOpWithSolveBase< Scalar > > &Op, const ESupportSolveUse supportSolveUse=SUPPORT_SOLVE_UNSPECIFIED)
Initialize a preconditioned LOWSB object given an external operator to be used to generate the precon...
RCP< LinearOpWithSolveBase< Scalar > > linearOpWithSolve(const LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const RCP< const LinearOpBase< Scalar > > &fwdOp, const ESupportSolveUse supportSolveUse=SUPPORT_SOLVE_UNSPECIFIED)
Create and initialize a LinearOpWithSolveBase object from a LinearOpBase object using a LinearOpWithS...
void initializeOp(const LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const RCP< const LinearOpBase< Scalar > > &fwdOp, const Ptr< LinearOpWithSolveBase< Scalar > > &Op, const ESupportSolveUse supportSolveUse=SUPPORT_SOLVE_UNSPECIFIED)
Initialize a pre-created LOWSB object given a forward operator.
void uninitializeOp(const LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const Ptr< LinearOpWithSolveBase< Scalar > > &Op, const Ptr< RCP< const LinearOpBase< Scalar > > > &fwdOp=Teuchos::null, const Ptr< RCP< const PreconditionerBase< Scalar > > > &prec=Teuchos::null, const Ptr< RCP< const LinearOpBase< Scalar > > > &approxFwdOp=Teuchos::null, const Ptr< ESupportSolveUse > &supportSolveUse=Teuchos::null)
Uninitialized a pre-created LOWSB object, returning input objects used to initialize it.
void initializePreconditionedOp(const LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const RCP< const LinearOpBase< Scalar > > &fwdOp, const RCP< const PreconditionerBase< Scalar > > &prec, const Ptr< LinearOpWithSolveBase< Scalar > > &Op, const ESupportSolveUse supportSolveUse=SUPPORT_SOLVE_UNSPECIFIED)
Initialize a preconditioned LOWSB object given an external preconditioner.
void initializeAndReuseOp(const LinearOpWithSolveFactoryBase< Scalar > &lowsFactory, const RCP< const LinearOpBase< Scalar > > &fwdOp, const Ptr< LinearOpWithSolveBase< Scalar > > &Op)
Reinitialize a pre-created LOWSB object given a forward operator, reusing a much as possible from the...
virtual RCP< LinearOpWithSolveBase< Scalar > > createOp() const =0
Create an (uninitialized) LinearOpWithSolveBase object to be initialized later in this->initializeOp(...
void assign(const Ptr< MultiVectorBase< Scalar > > &V, Scalar alpha)
V = alpha.
void randomize(Scalar l, Scalar u, const Ptr< MultiVectorBase< Scalar > > &V)
Generate a random multi-vector with elements uniformly distributed elements.
void changeOp(const Teuchos::Ptr< LinearOpBase< Scalar > > &op) const
Simple interface class to access a precreated preconditioner as one or more linear operators objects ...
Factory interface for creating preconditioner objects from LinearOpBase objects.
void initializePrec(const PreconditionerFactoryBase< Scalar > &precFactory, const Teuchos::RCP< const LinearOpBase< Scalar > > &fwdOp, const Teuchos::Ptr< PreconditionerBase< Scalar > > &prec, const ESupportSolveUse supportSolveUse=SUPPORT_SOLVE_UNSPECIFIED)
Initialize a preconditioner from a forward linear operator.
virtual RCP< PreconditionerBase< Scalar > > createPrec() const =0
Create an (uninitialized) LinearOpBase object to be initialized as the preconditioner later in this->...
Abstract interface for finite-dimensional dense vectors.
RCP< VectorBase< Scalar > > createMember(const RCP< const VectorSpaceBase< Scalar > > &vs, const std::string &label="")
Create a vector member from the vector space.
NOTRANS
Type for the dimension of a vector space. `**/ typedef Teuchos::Ordinal Ordinal;.
TypeTo as(const TypeFrom &t)
basic_FancyOStream< char > FancyOStream
basic_OSTab< char > OSTab
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
Simple struct for the return status from a solve.