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";
89 Thyra::linearOpWithSolve(lowsFactory, rcpFromRef(A));
94 out <<
"\nSolve status:\n" << status;
103template<
class Scalar>
105createScaledAdjointLinearOpWithSolve(
107 const Scalar &scalar,
113 out <<
"\nCreating a scaled adjoint LinearOpWithSolveBase object ...\n";
116 Thyra::linearOpWithSolve(lowsFactory,
scale(scalar,
adjoint(A)));
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";
292 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A, P, invertibleA.
ptr());
304template<
class Scalar>
306createUnspecifiedPreconditionedLinearOpWithSolve(
314 out <<
"\nCreating an LinearOpWithSolveBase object given a preconditioner operator"
315 <<
" not targeted to the left or right ...\n";
318 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A,
333template<
class Scalar>
335createLeftPreconditionedLinearOpWithSolve(
343 out <<
"\nCreating an LinearOpWithSolveBase object given a left preconditioner"
344 <<
" operator ...\n";
347 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A,
360template<
class Scalar>
362createRightPreconditionedLinearOpWithSolve(
370 out <<
"\nCreating an LinearOpWithSolveBase object given a right"
371 <<
" preconditioner operator ...\n";
374 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A,
387template<
class Scalar>
389createLeftRightPreconditionedLinearOpWithSolve(
398 out <<
"\nCreating an LinearOpWithSolveBase object given a left and"
399 <<
"right preconditioner operator ...\n";
402 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A,
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";
461 Thyra::initializePrec<Scalar>(precFactory, A, P.
ptr());
465 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A, P, invertibleA.
ptr());
470 out <<
"\nSolve status:\n" << status1;
483 Thyra::initializePreconditionedOp<Scalar>(lowsFactory, A, P, invertibleA.
ptr());
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";
587 Thyra::initializePrec<Scalar>(precFactory, rcpFromRef(A), P.
ptr());
596 invertibleA = createGeneralPreconditionedLinearOpWithSolve<Scalar>(
603 if (
nonnull(P_op = P->getUnspecifiedPrecOp())) {
606 else if (nonnull (P_op = P->getLeftPrecOp ())) {
609 else if (nonnull (P_op = P->getRightPrecOp ())) {
613 invertibleA = createUnspecifiedPreconditionedLinearOpWithSolve(
614 rcpFromRef(A), P_op, lowsFactory, out);
616 if(supportsLeftPrec) {
617 invertibleA = createLeftPreconditionedLinearOpWithSolve(
618 rcpFromRef(A), P_op, lowsFactory,out);
621 if(supportsRightPrec) {
622 invertibleA = createRightPreconditionedLinearOpWithSolve(
623 rcpFromRef(A), P_op, lowsFactory, out);
627 if( supportsLeftPrec && supportsRightPrec ) {
628 invertibleA = createLeftRightPreconditionedLinearOpWithSolve(
629 rcpFromRef(A), P_op, P_op, lowsFactory, out);
633 invertibleA = createMatrixPreconditionedLinearOpWithSolve<Scalar>(
634 rcpFromRef(A), rcpFromRef(A), lowsFactory,out);
636 externalPreconditionerReuseWithSolves<Scalar>(
639 lowsFactory, precFactory,
640 *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
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...
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 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.
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.
bool nonnull(const std::shared_ptr< T > &p)
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.