bool tBlockLowerTriInverseOp_tpetra::test_apply(int verbosity,std::ostream & os) { bool status = false; bool allPassed = true; RCP<Thyra::PhysicallyBlockedLinearOpBase<ST> > U = getLowerTriBlocks(A_); RCP<const Thyra::LinearOpBase<ST> > invTri = createBlockLowerTriInverseOp(U,invDiag_); Thyra::LinearOpTester<ST> tester; tester.show_all_tests(true); std::stringstream ss; Teuchos::FancyOStream fos(Teuchos::rcpFromRef(ss)," |||"); const bool result = tester.compare( *invA_, *invTri, Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result, std::endl << " tBlockLowerTriInverseOp_tpetra::test_apply " << ": Comparing implicitly generated operator to exact operator"); if(not result || verbosity>=10) os << ss.str(); return allPassed; }
bool tNeumannSeries::test_scaledOp(int verbosity,std::ostream & os) { bool status = false; bool allPassed = true; // perform actual test Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); std::stringstream ss; Teuchos::FancyOStream fos(rcpFromRef(ss)," |||"); { // build library parameter list Teuchos::RCP<Teuchos::ParameterList> pl = buildLibPL(2,"Diagonal"); if(verbosity>=10) { os << " tNeumannSeries::test_scaledOp :" << " printing library parameter list" << std::endl; pl->print(os); } RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*pl); RCP<Teko::InverseFactory> neumann = invLib->getInverseFactory("Neumann"); RCP<Teko::InverseFactory> direct = invLib->getInverseFactory("Amesos"); Teko::LinearOp op = buildExampleOp(2,*GetComm()); Teko::LinearOp neuInv = Teko::buildInverse(*neumann,op); Teko::LinearOp dirInv = Teko::buildInverse(*direct,op); const bool result = tester.compare( *neuInv, *dirInv, Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result, std::endl << " tNeumannSeries::test_scaledOp " << ": Comparing factory generated operator to correct operator"); if(not result || verbosity>=10) os << ss.str(); } return allPassed; }
TEUCHOS_UNIT_TEST(tProbingFactory, basic_test) { // build global (or serial communicator) #ifdef HAVE_MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif Teko::LinearOp lo = buildSystem(Comm,10); Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromStratimikos(); Teuchos::RCP<Teko::InverseFactory> directSolveFactory = invLib->getInverseFactory("Amesos"); Teuchos::RCP<Teko::ProbingPreconditionerFactory> probeFact = rcp(new Teko::ProbingPreconditionerFactory); probeFact->setGraphOperator(lo); probeFact->setInverseFactory(directSolveFactory); RCP<Teko::InverseFactory> invFact = Teuchos::rcp(new Teko::PreconditionerInverseFactory(probeFact,Teuchos::null)); Teko::LinearOp probedInverse = Teko::buildInverse(*invFact,lo); Teko::LinearOp invLo = Teko::buildInverse(*directSolveFactory,lo); Thyra::LinearOpTester<double> tester; tester.dump_all(true); tester.show_all_tests(true); { const bool result = tester.compare( *probedInverse, *invLo, &out); if (!result) { out << "Apply: FAILURE" << std::endl; success = false; } else out << "Apply: SUCCESS" << std::endl; } }
TEUCHOS_UNIT_TEST(tExplicitOps, transpose) { // build global (or serial communicator) #ifdef HAVE_MPI Epetra_MpiComm comm(MPI_COMM_WORLD); #else Epetra_SerialComm comm; #endif int nx = 39; // essentially random values int ny = 53; // create some big blocks to play with Trilinos_Util::CrsMatrixGallery FGallery("recirc_2d",comm,false); // CJ TODO FIXME: change for Epetra64 FGallery.Set("nx",nx); FGallery.Set("ny",ny); Epetra_CrsMatrix & epetraF = FGallery.GetMatrixRef(); Teko::LinearOp F = Thyra::epetraLinearOp(rcp(new Epetra_CrsMatrix(epetraF))); Teko::LinearOp F_T = Teko::explicitTranspose(F); Teko::LinearOp aF = Thyra::adjoint(F); Teuchos::RCP<const Epetra_Operator> eOp = Thyra::get_Epetra_Operator(*F_T); TEST_ASSERT(eOp!=Teuchos::null); Teuchos::RCP<const Epetra_CrsMatrix> eCrs = Teuchos::rcp_dynamic_cast<const Epetra_CrsMatrix>(eOp); TEST_ASSERT(eCrs!=Teuchos::null); Thyra::LinearOpTester<double> tester; tester.set_all_error_tol(1e-14); tester.show_all_tests(true); { std::stringstream ss; const bool result = tester.compare( *aF, *F_T, Teuchos::ptrFromRef(out) ); TEST_ASSERT(result); } }
bool tPCDStrategy::test_PCDStrategy(int verbosity,std::ostream & os) { bool status = false; bool allPassed = true; Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); tester.set_all_error_tol(tolerance_); Teko::LinearOp M1 = buildExampleOp(1,*GetComm(),1.0); Teko::LinearOp M2 = buildExampleOp(2,*GetComm(),1.0); Teko::LinearOp A = Thyra::nonconstBlock2x2<double>(buildExampleOp(2,*GetComm(),1.0), buildExampleOp(1,*GetComm(),2.0), buildExampleOp(1,*GetComm(),3.0), buildExampleOp(2,*GetComm(),4.0)); Teko::BlockedLinearOp blkA = Teko::toBlockedLinearOp(A); Teko::LinearOp laplace = Teko::explicitMultiply(M2,M1); Teko::LinearOp Fp = M1; Teko::LinearOp Qp = buildExampleOp(3,*GetComm(),1.0); std::string pcdStr = Teko::NS::PCDStrategy::getPCDString(); std::string presLapStr = Teko::NS::PCDStrategy::getPressureLaplaceString(); std::string presMassStr = Teko::NS::PCDStrategy::getPressureMassString(); RCP<Teko::StaticRequestCallback<Teko::LinearOp> > pcdCb = rcp(new Teko::StaticRequestCallback<Teko::LinearOp>(pcdStr,Fp)); RCP<Teko::StaticRequestCallback<Teko::LinearOp> > lapCb = rcp(new Teko::StaticRequestCallback<Teko::LinearOp>(presLapStr,laplace)); RCP<Teko::StaticRequestCallback<Teko::LinearOp> > massCb = rcp(new Teko::StaticRequestCallback<Teko::LinearOp>(presMassStr,Qp)); RCP<Teko::RequestHandler> rh = rcp(new Teko::RequestHandler); rh->addRequestCallback(pcdCb); rh->addRequestCallback(lapCb); rh->addRequestCallback(massCb); RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromStratimikos(); RCP<Teko::InverseFactory> inverseFact = invLib->getInverseFactory("Amesos"); // test the inverse of the 00 block { bool result; std::stringstream ss; Teuchos::FancyOStream fos(Teuchos::rcpFromRef(ss)," |||"); Teko::LinearOp invA00 = inverseFact->buildInverse(M2); Teko::BlockPreconditionerState state; Teko::NS::PCDStrategy strategy(inverseFact,inverseFact); strategy.setRequestHandler(rh); // build the state object // state.addLinearOp(presLapStr, laplace); // state.addLinearOp(pcdStr, Fp); // state.addLinearOp(presMassStr, Qp); // test the 0,0 block inverses: hat Teko::LinearOp hatInvA00 = strategy.getHatInvA00(blkA,state); ss.str(""); result = tester.compare( *hatInvA00, *invA00, Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result, std::endl << " tPCDStrategy::test_PCDStrategy " << toString(status) << " : Comparing invHatA00 operators"); if(not result || verbosity>=10) os << ss.str(); // test the 0,0 block inverses: tilde Teko::LinearOp tildeInvA00 = strategy.getTildeInvA00(blkA,state); ss.str(""); result = tester.compare( *tildeInvA00, *invA00, Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result, std::endl << " tPCDStrategy::test_PCDStrategy " << toString(status) << " : Comparing invTildeA00 operators"); if(not result || verbosity>=10) os << ss.str(); } // test the inverse of the Schur complement { bool result; std::stringstream ss; Teuchos::FancyOStream fos(Teuchos::rcpFromRef(ss)," |||"); Teko::LinearOp invLaplace = inverseFact->buildInverse(laplace); Teko::LinearOp iQp = Teko::getInvDiagonalOp(Qp); Teko::LinearOp invSchur = multiply(iQp,Fp,invLaplace); Teko::BlockPreconditionerState state; Teko::NS::PCDStrategy strategy(inverseFact,inverseFact); strategy.setRequestHandler(rh); // build the state object // state.addLinearOp(presLapStr, laplace); // state.addLinearOp(pcdStr, Fp); // state.addLinearOp(presMassStr, Qp); // test the 0,0 block inverses: hat Teko::LinearOp invS_strategy = strategy.getInvS(blkA,state); ss.str(""); result = tester.compare( *invS_strategy, *invSchur, Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result, std::endl << " tPCDStrategy::test_PCDStrategy " << toString(status) << " : Comparing invS operators"); if(not result || verbosity>=10) os << ss.str(); } return allPassed; }
bool runCgSolveExample( const int dim, const Scalar diagScale, const bool symOp, const bool showAllTests, const typename Teuchos::ScalarTraits<Scalar>::magnitudeType tolerance, const int maxNumIters ) { using Teuchos::as; using Teuchos::null; using Teuchos::RCP; using Teuchos::rcp; using Teuchos::OSTab; typedef Teuchos::ScalarTraits<Scalar> ST; using Thyra::multiply; using Thyra::scale; typedef typename ST::magnitudeType ScalarMag; bool success = true; bool result; Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream(); *out << "\n***\n*** Running silly CG solver using scalar type = \'" << ST::name() << "\' ...\n***\n"; Teuchos::Time timer(""); timer.start(true); // // (A) Setup a simple linear system with tridiagonal operator: // // [ a*2 -1 ] // [ -r(1) a*2 -1 ] // A = [ . . . ] // [ -r(n-2) a*2 -1 ] // [ -r(n-1) a*2 ] // // (A.1) Create the tridiagonal matrix operator *out << "\nConstructing tridiagonal matrix A of dimension = " << dim << " and diagonal multiplier = " << diagScale << " ...\n"; Teuchos::Array<Scalar> lower(dim-1), diag(dim), upper(dim-1); const Scalar up = -ST::one(), diagTerm = as<Scalar>(2.0) * diagScale * ST::one(), low = -(symOp ? ST::one() : ST::random()); int k = 0; // First row diag[k] = diagTerm; upper[k] = up; // Middle rows for( k = 1; k < dim - 1; ++k ) { lower[k-1] = low; diag[k] = diagTerm; upper[k] = up; } // Last row lower[k-1] = low; diag[k] = diagTerm; RCP<const Thyra::LinearOpBase<Scalar> > A = rcp(new ExampleTridiagSerialLinearOp<Scalar>(dim, lower, diag, upper)); // (A.2) Testing the linear operator constructed linear operator *out << "\nTesting the constructed linear operator A ...\n"; Thyra::LinearOpTester<Scalar> linearOpTester; linearOpTester.enable_all_tests(false); linearOpTester.check_linear_properties(true); linearOpTester.set_all_error_tol(tolerance); linearOpTester.set_all_warning_tol(1e-2*tolerance); linearOpTester.show_all_tests(showAllTests); result = linearOpTester.check(*A, out.ptr()); if(!result) success = false; // (A.3) Create RHS vector b and set to a random value RCP<Thyra::VectorBase<Scalar> > b = createMember(A->range()); Thyra::seed_randomize<Scalar>(0); Thyra::randomize( -ST::one(), +ST::one(), b.ptr() ); // (A.4) Create LHS vector x and set to zero RCP<Thyra::VectorBase<Scalar> > x = createMember(A->domain()); Thyra::V_S( x.ptr(), ST::zero() ); // (A.5) Create the final linear system if(!symOp) { *out << "\nSetting up normal equations for unsymmetric system A^H*(A*x-b) => new A*x = b ...\n"; // A^H*A RCP<const Thyra::LinearOpBase<Scalar> > AtA = multiply(adjoint(A), A); // A^H*b RCP<Thyra::VectorBase<Scalar> > nb = createMember(AtA->range()); Thyra::apply<Scalar>(*A, Thyra::CONJTRANS, *b, nb.ptr()); A = AtA; b = nb; } // (A.6) Testing the linear operator used with the solve *out << "\nTesting the linear operator used with the solve ...\n"; linearOpTester.check_for_symmetry(true); result = linearOpTester.check(*A, out.ptr()); if(!result) success = false; // // (B) Solve the linear system with the silly CG solver // *out << "\nSolving the linear system with sillyCgSolve(...) ...\n"; { OSTab tab2(out); result = sillyCgSolve(*A, *b, maxNumIters, tolerance, x.ptr(), *out); } if(!result) success = false; // // (C) Check that the linear system was solved to the specified tolerance // RCP<Thyra::VectorBase<Scalar> > r = createMember(A->range()); // r = b Thyra::V_V(r.ptr(), *b); // r = -A*x + r Thyra::apply<Scalar>(*A, Thyra::NOTRANS, *x, r.ptr(), -ST::one(), ST::one()); const ScalarMag r_nrm = Thyra::norm(*r), b_nrm = Thyra::norm(*b); const ScalarMag rel_err = r_nrm/b_nrm, relaxTol = 10.0*tolerance; result = rel_err <= relaxTol; if(!result) success = false; *out << "\nChecking the residual ourselves ...\n"; { OSTab tab(out); *out << "\n||b-A*x||/||b|| = "<<r_nrm<<"/"<<b_nrm<<" = "<<rel_err<<(result?" <= ":" > ") <<"10.0*tolerance = "<<relaxTol<<": "<<(result?"passed":"failed")<<std::endl; } timer.stop(); *out << "\nTotal time = " << timer.totalElapsedTime() << " sec\n"; return success; } // end runCgSolveExample()
bool tLSCHIntegrationTest::test_hScaling(int verbosity,std::ostream & os) { bool status = false; bool allPassed = true; RCP<const Epetra_Comm> comm = GetComm(); // build some operators Teko::LinearOp F = Teko::Test::build2x2(*comm,1,2,2,1); Teko::LinearOp G = Teko::Test::build2x2(*comm,1,-1,-3,1); Teko::LinearOp D = Teko::Test::build2x2(*comm,1,-3,-1,1); double diag[2]; diag[0] = 1.0/3.0; diag[1] = 1.0/2.0; Teko::LinearOp M = Teko::Test::DiagMatrix(2,diag,"M"); diag[0] = 5.0; diag[1] = 9.0; Teko::LinearOp H = Teko::Test::DiagMatrix(2,diag,"H"); Teko::LinearOp A = Thyra::block2x2<double>(F,G,D,Teuchos::null); Teko::LinearOp exact; { // build some operators Teko::LinearOp D0 = Teko::Test::build2x2(*comm,-1.0/3.0,2.0/3.0,2.0/3.0,-1.0/3.0); Teko::LinearOp D1 = Teko::Test::build2x2(*comm,-1.5,-3.0,-3.0,-5.5); Teko::LinearOp U = Teko::Test::build2x2(*comm,-0.5,-1.5,-0.5,-0.5); exact = Thyra::block2x2<double>(D0,U,Teuchos::null,D1); } RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromStratimikos(); RCP<Teko::InverseFactory> invFact = invLib->getInverseFactory("Amesos"); RCP<Teko::NS::InvLSCStrategy> strategy = rcp(new Teko::NS::InvLSCStrategy(invFact,M)); strategy->setHScaling(Teko::getDiagonal(H)); strategy->setUseFullLDU(false); RCP<Teko::BlockPreconditionerFactory> precFact = rcp(new Teko::NS::LSCPreconditionerFactory(strategy)); RCP<Teko::BlockPreconditionerState> bps = Teuchos::rcp_dynamic_cast<Teko::BlockPreconditionerState>(precFact->buildPreconditionerState()); Teko::LinearOp prec = precFact->buildPreconditionerOperator(A,*bps); Teko::BlockedLinearOp bA = Teko::toBlockedLinearOp(A); std::stringstream ss; ss << "invF = " << Teuchos::describe(*strategy->getInvF(bA,*bps),Teuchos::VERB_EXTREME) << std::endl; ss << "invBQBt = " << Teuchos::describe(*strategy->getInvBQBt(bA,*bps),Teuchos::VERB_EXTREME) << std::endl; ss << "invF = " << Teuchos::describe(*strategy->getInvBHBt(bA,*bps),Teuchos::VERB_EXTREME) << std::endl; ss << "invMass = " << Teuchos::describe(*strategy->getInvMass(bA,*bps),Teuchos::VERB_EXTREME) << std::endl; ss << "HScaling = " << Teuchos::describe(*strategy->getHScaling(bA,*bps),Teuchos::VERB_EXTREME) << std::endl; ss << "prec = " << Teuchos::describe(*prec,Teuchos::VERB_EXTREME) << std::endl; // construct a couple of vectors const RCP<Epetra_Map> map = rcp(new Epetra_Map(2,0,*GetComm())); Epetra_Vector ea(*map),eb(*map); const RCP<const Thyra::MultiVectorBase<double> > x = BlockVector(ea,eb,prec->domain()); const RCP<Thyra::MultiVectorBase<double> > y = Thyra::createMembers(prec->range(),1); ea[0] = 1.0; ea[1] = 0.0; eb[0] = 0.0; eb[1] = 0.0; Thyra::apply(*prec,Thyra::NOTRANS,*x,y.ptr()); ss << "prec = " << Teuchos::describe(*y,Teuchos::VERB_EXTREME) << std::endl; ea[0] = 0.0; ea[1] = 1.0; eb[0] = 0.0; eb[1] = 0.0; Thyra::apply(*prec,Thyra::NOTRANS,*x,y.ptr()); ss << "prec = " << Teuchos::describe(*y,Teuchos::VERB_EXTREME) << std::endl; ea[0] = 0.0; ea[1] = 0.0; eb[0] = 1.0; eb[1] = 0.0; Thyra::apply(*prec,Thyra::NOTRANS,*x,y.ptr()); ss << "prec = " << Teuchos::describe(*y,Teuchos::VERB_EXTREME) << std::endl; ea[0] = 0.0; ea[1] = 0.0; eb[0] = 0.0; eb[1] = 1.0; Thyra::apply(*prec,Thyra::NOTRANS,*x,y.ptr()); ss << "prec = " << Teuchos::describe(*y,Teuchos::VERB_EXTREME) << std::endl; Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); Teuchos::FancyOStream fos(Teuchos::rcpFromRef(ss),"|||"); const bool result = tester.compare( *prec, *exact, Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result, std::endl << " tLSCHIntegration::test_hScaling " << ": Comparing preconditioner to exactly computed version"); const bool result2 = tester.compare( *H, *strategy->getHScaling(bA,*bps), Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result2, std::endl << " tLSCHIntegration::test_hScaling " << ": Comparing scaling of H operator"); const bool result3 = tester.compare( *build2x2(*comm,0.208333333333333,0.375, 0.375, 0.875), *strategy->getInvBQBt(bA,*bps), Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result3, std::endl << " tLSCHIntegration::test_hScaling " << ": Comparing inv(BQBt) operator"); const bool result4 = tester.compare( *build2x2(*comm, 0.077777777777778, 0.177777777777778, 0.177777777777778, 0.477777777777778), *strategy->getInvBHBt(bA,*bps), Teuchos::ptrFromRef(fos) ); TEST_ASSERT(result4, std::endl << " tLSCHIntegration::test_hScaling " << ": Comparing inv(BHBt) operator"); if(not allPassed || verbosity>=10) os << ss.str(); return allPassed; }
TEUCHOS_UNIT_TEST(tIterativePreconditionerFactory, parameter_list_init) { // build global (or serial communicator) #ifdef HAVE_MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif Teko::LinearOp A = build2x2(Comm,1,2,3,4); Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); { RCP<Teuchos::ParameterList> pl = buildLibPL(4,"Amesos"); RCP<Teko::IterativePreconditionerFactory> precFact = rcp(new Teko::IterativePreconditionerFactory()); RCP<Teko::InverseFactory> invFact = rcp(new Teko::PreconditionerInverseFactory(precFact,Teuchos::null)); try { precFact->initializeFromParameterList(*pl); out << "Passed correct parameter list" << std::endl; Teko::LinearOp prec = Teko::buildInverse(*invFact,A); } catch(...) { success = false; out << "Failed correct parameter list" << std::endl; } } { Teuchos::ParameterList pl; pl.set("Preconditioner Type","Amesos"); RCP<Teko::IterativePreconditionerFactory> precFact = rcp(new Teko::IterativePreconditionerFactory()); RCP<Teko::InverseFactory> invFact = rcp(new Teko::PreconditionerInverseFactory(precFact,Teuchos::null)); try { precFact->initializeFromParameterList(pl); out << "Passed iteration count" << std::endl; Teko::LinearOp prec = Teko::buildInverse(*invFact,A); } catch(...) { out << "Failed iteration count" << std::endl; } } { Teuchos::ParameterList pl; pl.set("Iteration Count",4); pl.set("Precondiioner Type","Amesos"); RCP<Teko::IterativePreconditionerFactory> precFact = rcp(new Teko::IterativePreconditionerFactory()); try { precFact->initializeFromParameterList(pl); success = false; out << "Failed preconditioner type" << std::endl; // these should not be executed RCP<Teko::InverseFactory> invFact = rcp(new Teko::PreconditionerInverseFactory(precFact,Teuchos::null)); Teko::LinearOp prec = Teko::buildInverse(*invFact,A); } catch(const std::exception & exp) { out << "Passed preconditioner type" << std::endl; } } }
int exampleImplicitlyComposedLinearOperators( const int n0, const int n1, const int n2, Teuchos::FancyOStream &out, const Teuchos::EVerbosityLevel verbLevel, typename Teuchos::ScalarTraits<Scalar>::magnitudeType errorTol, const bool testAdjoint ) { // Using and other declarations typedef Teuchos::ScalarTraits<Scalar> ST; using Teuchos::as; using Teuchos::RCP; using Teuchos::OSTab; using Thyra::VectorSpaceBase; using Thyra::VectorBase; using Thyra::MultiVectorBase; using Thyra::LinearOpBase; using Thyra::defaultSpmdVectorSpace; using Thyra::randomize; using Thyra::identity; using Thyra::diagonal; using Thyra::multiply; using Thyra::add; using Thyra::subtract; using Thyra::scale; using Thyra::adjoint; using Thyra::block1x2; using Thyra::block2x2; using Thyra::block2x2; out << "\n***" << "\n*** Demonstrating building linear operators for scalar type " << ST::name() << "\n***\n"; OSTab tab(out); // // A) Set up the basic objects and other inputs to build the implicitly // composed linear operators. // // Create serial vector spaces in this case const RCP<const VectorSpaceBase<Scalar> > space0 = defaultSpmdVectorSpace<Scalar>(n0), space1 = defaultSpmdVectorSpace<Scalar>(n1), space2 = defaultSpmdVectorSpace<Scalar>(n2); // Create the component linear operators first as multi-vectors const RCP<MultiVectorBase<Scalar> > mvA = createMembers(space2, n0, "A"), mvB = createMembers(space0, n2, "B"), mvC = createMembers(space0, n0, "C"), mvE = createMembers(space0, n1, "E"), mvF = createMembers(space0, n1, "F"), mvJ = createMembers(space2, n1, "J"), mvK = createMembers(space1, n2, "K"), mvL = createMembers(space2, n1, "L"), mvN = createMembers(space0, n1, "N"), mvP = createMembers(space2, n1, "P"), mvQ = createMembers(space0, n2, "Q"); // Create the vector diagonal for D const RCP<VectorBase<Scalar> > d = createMember(space2); // Get the constants const Scalar one = 1.0, beta = 2.0, gamma = 3.0, eta = 4.0; // Randomize the values in the Multi-Vector randomize( -one, +one, mvA.ptr() ); randomize( -one, +one, mvB.ptr() ); randomize( -one, +one, mvC.ptr() ); randomize( -one, +one, d.ptr() ); randomize( -one, +one, mvE.ptr() ); randomize( -one, +one, mvF.ptr() ); randomize( -one, +one, mvJ.ptr() ); randomize( -one, +one, mvK.ptr() ); randomize( -one, +one, mvL.ptr() ); randomize( -one, +one, mvN.ptr() ); randomize( -one, +one, mvP.ptr() ); randomize( -one, +one, mvQ.ptr() ); // Get the linear operator forms of the basic component linear operators const RCP<const LinearOpBase<Scalar> > A = mvA, B = mvB, C = mvC, E = mvE, F = mvF, J = mvJ, K = mvK, L = mvL, N = mvN, P = mvP, Q = mvQ; out << describe(*A, verbLevel); out << describe(*B, verbLevel); out << describe(*C, verbLevel); out << describe(*E, verbLevel); out << describe(*F, verbLevel); out << describe(*J, verbLevel); out << describe(*K, verbLevel); out << describe(*L, verbLevel); out << describe(*N, verbLevel); out << describe(*P, verbLevel); out << describe(*Q, verbLevel); // // B) Create the composed linear operators // // I const RCP<const LinearOpBase<Scalar> > I = identity(space1, "I"); // D = diag(d) const RCP<const LinearOpBase<Scalar> > D = diagonal(d, "D"); // M00 = [ gama*B*A + C, E + F ] ^H // [ J^H * A, I ] const RCP<const LinearOpBase<Scalar> > M00 = adjoint( block2x2( add( scale(gamma,multiply(B,A)), C ), add( E, F ), multiply(adjoint(J),A), I ), "M00" ); out << "\nM00 = " << describe(*M00, verbLevel); // M01 = beta * [ Q ] // [ K ] const RCP<const LinearOpBase<Scalar> > M01 = scale( beta, block2x1( Q, K ), "M01" ); out << "\nM01 = " << describe(*M01, verbLevel); // M10 = [ L * N^H, eta*P ] const RCP<const LinearOpBase<Scalar> > M10 = block1x2( multiply(L,adjoint(N)), scale(eta,P), "M10" ); out << "\nM10 = " << describe(*M10, verbLevel); // M11 = D - Q^H*Q const RCP<const LinearOpBase<Scalar> > M11 = subtract( D, multiply(adjoint(Q),Q), "M11" ); out << "\nM11 = " << describe(*M11, verbLevel); // M = [ M00, M01 ] // [ M10, M11 ] const RCP<const LinearOpBase<Scalar> > M = block2x2( M00, M01, M10, M11, "M" ); out << "\nM = " << describe(*M, verbLevel); // // C) Test the final composed operator // Thyra::LinearOpTester<Scalar> linearOpTester; linearOpTester.set_all_error_tol(errorTol); linearOpTester.check_adjoint(testAdjoint); if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_HIGH)) linearOpTester.show_all_tests(true); if (as<int>(verbLevel) >= as<int>(Teuchos::VERB_EXTREME)) linearOpTester.dump_all(true); const bool result = linearOpTester.check(*M,&out); return result; }
TEUCHOS_UNIT_TEST(tProbingFactory, parameterlist_constr) { // build global (or serial communicator) #ifdef HAVE_MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif Teko::LinearOp lo = buildSystem(Comm,10); Teuchos::RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromStratimikos(); Teuchos::RCP<Teko::InverseFactory> directSolveFactory = invLib->getInverseFactory("Amesos"); { Teuchos::ParameterList pl; pl.set("Inverse Type","Amesos"); pl.set("Probing Graph Operator",lo); Teuchos::RCP<Teko::ProbingPreconditionerFactory> probeFact = rcp(new Teko::ProbingPreconditionerFactory); probeFact->initializeFromParameterList(pl); RCP<Teko::InverseFactory> invFact = Teuchos::rcp(new Teko::PreconditionerInverseFactory(probeFact,Teuchos::null)); Teko::LinearOp probedInverse = Teko::buildInverse(*invFact,lo); Teko::LinearOp invLo = Teko::buildInverse(*directSolveFactory,lo); Thyra::LinearOpTester<double> tester; tester.dump_all(true); tester.show_all_tests(true); { const bool result = tester.compare( *probedInverse, *invLo, &out); if (!result) { out << "Apply: FAILURE" << std::endl; success = false; } else out << "Apply: SUCCESS" << std::endl; } } { Teuchos::RCP<const Epetra_CrsGraph> theGraph = rcpFromRef(rcp_dynamic_cast<const Epetra_CrsMatrix>(Thyra::get_Epetra_Operator(*lo))->Graph()); Teuchos::ParameterList pl; pl.set("Inverse Type","Amesos"); pl.set("Probing Graph",theGraph); Teuchos::RCP<Teko::ProbingPreconditionerFactory> probeFact = rcp(new Teko::ProbingPreconditionerFactory); probeFact->initializeFromParameterList(pl); RCP<Teko::InverseFactory> invFact = Teuchos::rcp(new Teko::PreconditionerInverseFactory(probeFact,Teuchos::null)); Teko::LinearOp probedInverse = Teko::buildInverse(*invFact,lo); Teko::LinearOp invLo = Teko::buildInverse(*directSolveFactory,lo); Thyra::LinearOpTester<double> tester; tester.dump_all(true); tester.show_all_tests(true); { const bool result = tester.compare( *probedInverse, *invLo, &out); if (!result) { out << "Apply: FAILURE" << std::endl; success = false; } else out << "Apply: SUCCESS" << std::endl; } } }
TEUCHOS_UNIT_TEST(tDiagonalPreconditionerFactory, blkdiag_inv_test) { using Teuchos::RCP; // build global (or serial communicator) #ifdef HAVE_MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif int numProc = Comm.NumProc(); // preconditioners to test std::string precName = "BlkDiag"; int myRows = 20; int blockSize = 5; Teko::LinearOp A = buildSystem(Comm,myRows); // sanity check TEUCHOS_ASSERT(myRows % blockSize==0); { RCP<Teuchos::ParameterList> pl = buildLibPL(1); // test diagonal construction RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*pl); RCP<Teko::InverseFactory> invFact = invLib->getInverseFactory(precName); Teko::LinearOp idA_fact = Teko::buildInverse(*invFact,A); // test type Teko::LinearOp srcOp = Teko::extractOperatorFromPrecOp(idA_fact); TEST_ASSERT(Teuchos::rcp_dynamic_cast<const Thyra::EpetraLinearOp>(srcOp)!=Teuchos::null); Teko::LinearOp idA_drct = Teko::getInvDiagonalOp(A,Teko::Diagonal); Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); const bool result = tester.compare( *idA_fact, *idA_drct, Teuchos::ptrFromRef(out)); if (!result) { out << "Apply 0: FAILURE (\"" << precName << "\")" << std::endl; success = false; } else out << "Apply 0: SUCCESS (\"" << precName << "\")" << std::endl; } { RCP<Teuchos::ParameterList> pl = buildLibPL(blockSize); RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(*pl); RCP<Teko::InverseFactory> invFact = invLib->getInverseFactory(precName); Teko::LinearOp idA_fact = Teko::buildInverse(*invFact,A); // test type Teko::LinearOp srcOp = Teko::extractOperatorFromPrecOp(idA_fact); TEST_ASSERT(Teuchos::rcp_dynamic_cast<const Thyra::EpetraLinearOp>(srcOp)!=Teuchos::null); RCP<const Epetra_CrsMatrix> eop = Teuchos::rcp_dynamic_cast<const Epetra_CrsMatrix>(Thyra::get_Epetra_Operator(*srcOp)); TEST_EQUALITY(eop->NumGlobalNonzeros(),blockSize*blockSize*(numProc*myRows)/blockSize); TEST_EQUALITY(eop->NumMyNonzeros(),blockSize*blockSize*myRows/blockSize); } }
// Testing Parameter Support TEUCHOS_UNIT_TEST(model_evaluator_blocked_hessians, d2f_dp2) { typedef panzer::Traits::RealType RealType; typedef Thyra::VectorBase<RealType> VectorType; typedef Thyra::SpmdVectorBase<RealType> SpmdVectorType; typedef Thyra::LinearOpBase<RealType> OperatorType; using Teuchos::RCP; using Teuchos::rcp_dynamic_cast; typedef Thyra::ModelEvaluatorBase::InArgs<double> InArgs; typedef Thyra::ModelEvaluatorBase::OutArgs<double> OutArgs; typedef panzer::ModelEvaluator<double> PME; bool distr_param_on = true; AssemblyPieces ap; buildAssemblyPieces(distr_param_on,ap); int pIndex = -1; std::vector<Teuchos::RCP<Teuchos::Array<std::string> > > p_names; std::vector<Teuchos::RCP<Teuchos::Array<double> > > p_values; bool build_transient_support = true; RCP<PME> me = Teuchos::rcp(new PME(ap.fmb,ap.rLibrary,ap.lof,p_names,p_values,Teuchos::null,ap.gd,build_transient_support,0.0)); const double DENSITY_VALUE = 3.7; const double TEMPERATURE_VALUE = 2.0; const double PERTURB_VALUE = 0.1; // add distributed parameter { RCP<ThyraObjFactory<double> > th_param_lof = rcp_dynamic_cast<ThyraObjFactory<double> >(ap.param_lof); RCP<VectorType> param_density = Thyra::createMember(th_param_lof->getThyraDomainSpace()); Thyra::assign(param_density.ptr(),DENSITY_VALUE); pIndex = me->addDistributedParameter("DENSITY_P",th_param_lof->getThyraDomainSpace(), ap.param_ged,param_density,ap.param_dofManager); } me->setupModel(ap.wkstContainer,ap.physicsBlocks,ap.bcs, *ap.eqset_factory, *ap.bc_factory, ap.cm_factory, ap.cm_factory, ap.closure_models, ap.user_data,false,""); // panzer::printMeshTopology(out,*ap.dofManager); RCP<OperatorType> D2fDp2 = me->create_DfDp_op(pIndex); // test hessian { RCP<VectorType> x = Thyra::createMember(*me->get_x_space()); Thyra::assign(x.ptr(),TEMPERATURE_VALUE); RCP<VectorType> dx = Thyra::createMember(*me->get_x_space()); Thyra::assign(dx.ptr(),PERTURB_VALUE); InArgs in_args = me->createInArgs(); in_args.set_x(x); in_args.set_alpha(1.0/0.1); in_args.set_beta(1.0); me->evalModel_D2fDp2(pIndex,in_args,dx,D2fDp2); out << "D2fDp2 = \n" << Teuchos::describe(*D2fDp2,Teuchos::VERB_EXTREME) << std::endl; } RCP<OperatorType> W = me->create_DfDp_op(pIndex); { RCP<VectorType> x = Thyra::createMember(*me->get_x_space()); Thyra::assign(x.ptr(),TEMPERATURE_VALUE); InArgs in_args = me->createInArgs(); in_args.set_x(x); in_args.set_alpha(1.0); in_args.set_beta(0.0); OutArgs out_args = me->createOutArgs(); out_args.set_DfDp(pIndex,W); me->evalModel(in_args,out_args); out << "W = \n" << Teuchos::describe(*W,Teuchos::VERB_EXTREME) << std::endl; } Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); tester.set_all_error_tol(1e-15); tester.num_random_vectors(20); double scaling_of_dfdp = PERTURB_VALUE/DENSITY_VALUE; Teuchos::FancyOStream fout(Teuchos::rcpFromRef(std::cout)); const bool op_cmp = tester.compare( *Thyra::scaleAndAdjoint(scaling_of_dfdp,Thyra::NOTRANS,W.getConst()), *D2fDp2, Teuchos::ptrFromRef(fout)); TEST_ASSERT(op_cmp); }
TEUCHOS_UNIT_TEST(tStratimikosFactory, test_BlockGaussSeidel) { using Teuchos::RCP; using Teuchos::ParameterList; // build global (or serial communicator) #ifdef HAVE_MPI Epetra_MpiComm comm(MPI_COMM_WORLD); #else Epetra_SerialComm comm; #endif // build epetra operator RCP<Epetra_Operator> eA = buildStridedSystem(comm,5); RCP<Thyra::LinearOpBase<double> > tA = Thyra::nonconstEpetraLinearOp(eA); // build stratimikos factory, adding Teko's version Stratimikos::DefaultLinearSolverBuilder stratFactory; stratFactory.setPreconditioningStrategyFactory( Teuchos::abstractFactoryStd<Thyra::PreconditionerFactoryBase<double>,Teko::StratimikosFactory>(), "Teko"); RCP<ParameterList> params = Teuchos::rcp(new ParameterList(*stratFactory.getValidParameters())); ParameterList & tekoList = params->sublist("Preconditioner Types").sublist("Teko"); tekoList.set("Write Block Operator", false); tekoList.set("Test Block Operator", false); tekoList.set("Strided Blocking","1 1"); tekoList.set("Inverse Type","BGS"); ParameterList & ifl = tekoList.sublist("Inverse Factory Library"); ifl.sublist("BGS").set("Type","Block Gauss-Seidel"); ifl.sublist("BGS").set("Inverse Type","Amesos"); // RCP<Thyra::PreconditionerFactoryBase<double> > precFactory // = stratFactory.createPreconditioningStrategy("Teko"); // build operator to test against Teko::LinearOp testOp; { Teuchos::ParameterList iflCopy(ifl); RCP<Epetra_Operator> strided_eA = Teuchos::rcp(new Teko::Epetra::StridedEpetraOperator(2,eA)); RCP<Teko::InverseLibrary> invLib = Teko::InverseLibrary::buildFromParameterList(iflCopy); RCP<const Teko::InverseFactory> invFact = invLib->getInverseFactory("BGS"); RCP<Teko::Epetra::InverseFactoryOperator> invFactOp = Teuchos::rcp(new Teko::Epetra::InverseFactoryOperator(invFact)); invFactOp->buildInverseOperator(strided_eA); testOp = Thyra::epetraLinearOp(invFactOp,Thyra::NOTRANS,Thyra::EPETRA_OP_APPLY_APPLY_INVERSE); } stratFactory.setParameterList(params); RCP<Thyra::PreconditionerFactoryBase<double> > precFactory = stratFactory.createPreconditioningStrategy("Teko"); // build teko preconditioner factory RCP<Thyra::PreconditionerBase<double> > prec = Thyra::prec<double>(*precFactory,tA); Teko::LinearOp precOp = prec->getUnspecifiedPrecOp(); TEST_ASSERT(precOp!=Teuchos::null); Thyra::LinearOpTester<double> tester; tester.show_all_tests(true); tester.set_all_error_tol(0); TEST_ASSERT(tester.compare(*precOp,*testOp,Teuchos::ptrFromRef(out))); }