TEUCHOS_UNIT_TEST(NOX_Thyra_RightScaling, CTOR2) { // Create a communicator for Epetra objects #ifdef HAVE_MPI Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif // Check we have only one processor since this problem doesn't work // for more than one proc TEST_ASSERT(Comm.NumProc() == 1); // Create the model evaluator object double d = 1.0; double p0 = 1.0e6; double p1 = -1.0e-6; double x00 = 0.0; double x01 = 0.0; Teuchos::RCP<ModelEvaluator2DSim<double> > thyraModel = Teuchos::rcp(new ModelEvaluator2DSim<double>(Teuchos::rcp(&Comm,false), d,p0,p1,x00,x01)); ::Stratimikos::DefaultLinearSolverBuilder builder; Teuchos::RCP<Teuchos::ParameterList> p = Teuchos::rcp(new Teuchos::ParameterList); { p->set("Linear Solver Type", "AztecOO"); //p->set("Preconditioner Type", "Ifpack"); p->set("Preconditioner Type", "None"); Teuchos::ParameterList& az = p->sublist("Linear Solver Types").sublist("AztecOO"); az.sublist("Forward Solve").sublist("AztecOO Settings").set("Output Frequency", 1); az.sublist("VerboseObject").set("Verbosity Level", "high"); Teuchos::ParameterList& ip = p->sublist("Preconditioner Types").sublist("Ifpack"); ip.sublist("VerboseObject").set("Verbosity Level", "high"); } builder.setParameterList(p); Teuchos::RCP< ::Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory = builder.createLinearSolveStrategy(""); thyraModel->set_W_factory(lowsFactory); // Create nox parameter list Teuchos::RCP<Teuchos::ParameterList> nl_params = Teuchos::rcp(new Teuchos::ParameterList); nl_params->set("Nonlinear Solver", "Line Search Based"); nl_params->sublist("Line Search").set("Method", "Full Step"); Teuchos::ParameterList& printParams = nl_params->sublist("Printing"); printParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::LinearSolverDetails + NOX::Utils::Parameters + NOX::Utils::Details + NOX::Utils::Warning + NOX::Utils::Debug + NOX::Utils::TestDetails + NOX::Utils::Error); nl_params->sublist("Solver Options").set("Status Test Check Type", "Complete"); // Enable row sum scaling nl_params->sublist("Thyra Group Options").set("Function Scaling", "None"); // Create right scaling vector Teuchos::RCP<Thyra::VectorBase<double> > values = Thyra::createMember(thyraModel->get_x_space()); Thyra::put_scalar(0.0,values.ptr()); // Values chosen such that WRMS convergence is achieved in 1 step // Unscaled will not converge in less than 20 steps Thyra::set_ele(0,1.0e14,values.ptr()); Thyra::set_ele(1,1.0e2,values.ptr()); Teuchos::RCP<NOX::Abstract::Vector > right_scaling = Teuchos::rcp<NOX::Abstract::Vector>(new NOX::Thyra::Vector(values)); // Create Status Tests Teuchos::RCP<NOX::StatusTest::Generic> status_tests; { Teuchos::RCP<Teuchos::ParameterList> st = Teuchos::parameterList("Status Tests"); st->set("Test Type", "Combo"); st->set("Combo Type", "OR"); st->set("Number of Tests", 3); { Teuchos::ParameterList& normWRMS = st->sublist("Test 0"); normWRMS.set("Test Type", "NormWRMS"); normWRMS.set("Absolute Tolerance", 1.0e-8); normWRMS.set("Relative Tolerance", 1.0e-5); normWRMS.set("Tolerance", 1.0); normWRMS.set("BDF Multiplier", 1.0); normWRMS.set("Alpha", 1.0); normWRMS.set("Beta", 0.5); normWRMS.set("Disable Implicit Weighting", true); } { Teuchos::ParameterList& fv = st->sublist("Test 1"); fv.set("Test Type", "FiniteValue"); fv.set("Vector Type", "F Vector"); fv.set("Norm Type", "Two Norm"); } { Teuchos::ParameterList& maxiters = st->sublist("Test 2"); maxiters.set("Test Type", "MaxIters"); maxiters.set("Maximum Iterations", 20); } // Create a print class for controlling output below nl_params->sublist("Printing").set("MyPID", Comm.MyPID()); nl_params->sublist("Printing").set("Output Precision", 3); nl_params->sublist("Printing").set("Output Processor", 0); NOX::Utils printing(nl_params->sublist("Printing")); NOX::StatusTest::Factory st_factory; status_tests = st_factory.buildStatusTests(*st,printing); } Teuchos::RCP< ::Thyra::VectorBase<double> > initial_guess = thyraModel->getNominalValues().get_x()->clone_v(); Teuchos::RCP<NOX::Thyra::Vector> noxThyraRightScaleVec = Teuchos::rcp_dynamic_cast<NOX::Thyra::Vector>(right_scaling); Teuchos::RCP<NOX::Thyra::Group> nox_group = Teuchos::rcp(new NOX::Thyra::Group(*initial_guess, thyraModel, thyraModel->create_W_op(), lowsFactory, thyraModel->create_W_prec(), lowsFactory->getPreconditionerFactory(), Teuchos::null, noxThyraRightScaleVec->getThyraRCPVector() )); Teuchos::RCP<NOX::Solver::Generic> solver = NOX::Solver::buildSolver(nox_group, status_tests, nl_params); NOX::StatusTest::StatusType solveStatus = solver->solve(); TEST_ASSERT(solveStatus == NOX::StatusTest::Converged); // Test total num iterations { // Problem converges in >20 nonlinear iterations with NO scaling // Problem converges in 1 nonlinear iterations with scaling TEST_EQUALITY(solver->getNumIterations(), 1); } }
int main(int argc, char *argv[]) { bool boolret; int MyPID; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif MyPID = Comm.MyPID(); bool testFailed; bool verbose = false; bool debug = false; bool shortrun = false; bool insitu = false; std::string which("LM"); CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); cmdp.setOption("insitu","exsitu",&insitu,"Perform in situ restarting."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SM or LM)."); cmdp.setOption("shortrun","longrun",&shortrun,"Allow only a small number of iterations."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } if (debug) verbose = true; typedef double ScalarType; typedef ScalarTraits<ScalarType> SCT; typedef SCT::magnitudeType MagnitudeType; typedef Anasazi::MultiVec<ScalarType> MV; typedef Anasazi::Operator<ScalarType> OP; typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT; typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT; const ScalarType ONE = SCT::one(); if (verbose && MyPID == 0) { cout << Anasazi::Anasazi_Version() << endl << endl; } // Problem information int space_dim = 1; std::vector<double> brick_dim( space_dim ); brick_dim[0] = 1.0; std::vector<int> elements( space_dim ); elements[0] = 100; // Create problem RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); // // Get the stiffness and mass matrices RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); // // Create solver for mass matrix const int maxIterCG = 100; const double tolCG = 1e-7; RCP<BlockPCGSolver> opStiffness = rcp( new BlockPCGSolver(Comm, M.get(), tolCG, maxIterCG, 0) ); opStiffness->setPreconditioner( 0 ); RCP<const Anasazi::EpetraGenOp> InverseOp = rcp( new Anasazi::EpetraGenOp( opStiffness, K ) ); // Create the initial vectors int blockSize = 3; RCP<Anasazi::EpetraOpMultiVec> ivec = rcp( new Anasazi::EpetraOpMultiVec(K, K->OperatorDomainMap(), blockSize) ); ivec->MvRandom(); // Create eigenproblem const int nev = 5; RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem = rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(InverseOp, ivec) ); // // Inform the eigenproblem that the operator InverseOp is Hermitian under an M inner-product problem->setHermitian(true); // // Set the number of eigenvalues requested problem->setNEV( nev ); // // Inform the eigenproblem that you are done passing it information boolret = problem->setProblem(); if (boolret != true) { if (verbose && MyPID == 0) { cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl << "End Result: TEST FAILED" << endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Set verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (verbose) { verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; } if (debug) { verbosity += Anasazi::Debug; } // Eigensolver parameters int numBlocks; int maxRestarts; if (shortrun) { maxRestarts = 25; numBlocks = 5; } else { maxRestarts = 50; numBlocks = 10; } int stepSize = numBlocks*maxRestarts; MagnitudeType tol = tolCG * 10.0; // Create a sort manager to pass into the block Krylov-Schur solver manager // --> Make sure the reference-counted pointer is of type Anasazi::SortManager<> // --> The block Krylov-Schur solver manager uses Anasazi::BasicSort<> by default, // so you can also pass in the parameter "Which", instead of a sort manager. RCP<Anasazi::SortManager<MagnitudeType> > MySort = rcp( new Anasazi::BasicSort<MagnitudeType>( which ) ); // // Create parameter list to pass into the solver manager ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Sort Manager", MySort ); //MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Num Blocks", numBlocks ); MyPL.set( "Maximum Restarts", maxRestarts ); MyPL.set( "Step Size", stepSize ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "In Situ Restarting", insitu ); // // Create the solver manager Anasazi::BlockKrylovSchurSolMgr<ScalarType,MV,OP> MySolverMgr(problem, MyPL); // // Check that the parameters were all consumed if (MyPL.getEntryPtr("Verbosity")->isUsed() == false || MyPL.getEntryPtr("Block Size")->isUsed() == false || MyPL.getEntryPtr("Num Blocks")->isUsed() == false || MyPL.getEntryPtr("Maximum Restarts")->isUsed() == false || MyPL.getEntryPtr("Step Size")->isUsed() == false || MyPL.getEntryPtr("In Situ Restarting")->isUsed() == false || MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false) { if (verbose && MyPID==0) { cout << "Failure! Unused parameters: " << endl; MyPL.unused(cout); } } // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr.solve(); testFailed = false; if (returnCode != Anasazi::Converged && shortrun==false) { testFailed = true; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution(); std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals; RCP<MV> evecs = sol.Evecs; int numev = sol.numVecs; if (numev > 0) { std::ostringstream os; os.setf(std::ios::scientific, std::ios::floatfield); os.precision(6); // Compute the direct residual std::vector<ScalarType> normV( numev ); SerialDenseMatrix<int,ScalarType> T(numev,numev); for (int i=0; i<numev; i++) { T(i,i) = evals[i].realpart; } RCP<OP> KOp = rcp( new Anasazi::EpetraOp( K )); RCP<OP> MOp = rcp( new Anasazi::EpetraOp( M )); RCP<MV> Mvecs = MVT::Clone( *evecs, numev ), Kvecs = MVT::Clone( *evecs, numev ); OPT::Apply( *KOp, *evecs, *Kvecs ); OPT::Apply( *MOp, *evecs, *Mvecs ); MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs ); // compute 2-norm of residuals std::vector<MagnitudeType> resnorm(numev); MVT::MvNorm( *Kvecs, resnorm ); os << "Number of iterations performed in BlockKrylovSchur_test.exe: " << MySolverMgr.getNumIters() << endl << "Direct residual norms computed in BlockKrylovSchur_test.exe" << endl << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual" << endl << "----------------------------------------" << endl; for (int i=0; i<numev; i++) { if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) { resnorm[i] = SCT::magnitude( SCT::squareroot( resnorm[i] ) / evals[i].realpart ); } else { resnorm[i] = SCT::magnitude( SCT::squareroot( resnorm[i] ) ); } os << std::setw(20) << evals[i].realpart << std::setw(20) << resnorm[i] << endl; if ( resnorm[i] > tol ) { testFailed = true; } } if (verbose && MyPID==0) { cout << endl << os.str() << endl; } } #ifdef HAVE_MPI MPI_Finalize() ; #endif if (testFailed) { if (verbose && MyPID==0) { cout << "End Result: TEST FAILED" << endl; } return -1; } // // Default return value // if (verbose && MyPID==0) { cout << "End Result: TEST PASSED" << endl; } return 0; }
int main(int argc, char *argv[]) { using std::cout; using std::endl; int i; #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif int MyPID = Comm.MyPID(); // Number of dimension of the domain int space_dim = 2; // Size of each of the dimensions of the domain std::vector<double> brick_dim( space_dim ); brick_dim[0] = 1.0; brick_dim[1] = 1.0; // Number of elements in each of the dimensions of the domain std::vector<int> elements( space_dim ); elements[0] = 10; elements[1] = 10; // Create problem Teuchos::RCP<ModalProblem> testCase = Teuchos::rcp( new ModeLaplace2DQ2(Comm, brick_dim[0], elements[0], brick_dim[1], elements[1]) ); // Get the stiffness and mass matrices Teuchos::RCP<Epetra_CrsMatrix> K = Teuchos::rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); Teuchos::RCP<Epetra_CrsMatrix> M = Teuchos::rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); // // ************Construct preconditioner************* // Teuchos::ParameterList ifpackList; // allocates an IFPACK factory. No data is associated // to this object (only method Create()). Ifpack Factory; // create the preconditioner. For valid PrecType values, // please check the documentation std::string PrecType = "ICT"; // incomplete Cholesky int OverlapLevel = 0; // must be >= 0. If Comm.NumProc() == 1, // it is ignored. Teuchos::RCP<Ifpack_Preconditioner> Prec = Teuchos::rcp( Factory.Create(PrecType, &*K, OverlapLevel) ); assert(Prec != Teuchos::null); // specify parameters for ICT ifpackList.set("fact: drop tolerance", 1e-4); ifpackList.set("fact: ict level-of-fill", 0.); // the combine mode is on the following: // "Add", "Zero", "Insert", "InsertAdd", "Average", "AbsMax" // Their meaning is as defined in file Epetra_CombineMode.h ifpackList.set("schwarz: combine mode", "Add"); // sets the parameters IFPACK_CHK_ERR(Prec->SetParameters(ifpackList)); // initialize the preconditioner. At this point the matrix must // have been FillComplete()'d, but actual values are ignored. IFPACK_CHK_ERR(Prec->Initialize()); // Builds the preconditioners, by looking for the values of // the matrix. IFPACK_CHK_ERR(Prec->Compute()); // //*******************************************************/ // Set up Belos Block CG operator for inner iteration //*******************************************************/ // int blockSize = 3; // block size used by linear solver and eigensolver [ not required to be the same ] int maxits = K->NumGlobalRows(); // maximum number of iterations to run // // Create the Belos::LinearProblem // Teuchos::RCP<Belos::LinearProblem<double,Epetra_MultiVector,Epetra_Operator> > My_LP = Teuchos::rcp( new Belos::LinearProblem<double,Epetra_MultiVector,Epetra_Operator>() ); My_LP->setOperator( K ); // Create the Belos preconditioned operator from the Ifpack preconditioner. // NOTE: This is necessary because Belos expects an operator to apply the // preconditioner with Apply() NOT ApplyInverse(). Teuchos::RCP<Epetra_Operator> belosPrec = Teuchos::rcp( new Epetra_InvOperator( Prec.get() ) ); My_LP->setLeftPrec( belosPrec ); // // Create the ParameterList for the Belos Operator // Teuchos::RCP<Teuchos::ParameterList> My_List = Teuchos::rcp( new Teuchos::ParameterList() ); My_List->set( "Solver", "BlockCG" ); My_List->set( "Maximum Iterations", maxits ); My_List->set( "Block Size", 1 ); My_List->set( "Convergence Tolerance", 1e-12 ); // // Create the Belos::EpetraOperator // Teuchos::RCP<Belos::EpetraOperator> BelosOp = Teuchos::rcp( new Belos::EpetraOperator( My_LP, My_List )); // // ************************************ // Start the block Arnoldi iteration // ************************************ // // Variables used for the Block Arnoldi Method // double tol = 1.0e-8; int nev = 10; int numBlocks = 3*nev/blockSize; int maxRestarts = 5; //int step = 5; std::string which = "LM"; int verbosity = Anasazi::Errors + Anasazi::Warnings + Anasazi::FinalSummary; // // Create parameter list to pass into solver // Teuchos::ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Num Blocks", numBlocks ); MyPL.set( "Maximum Restarts", maxRestarts ); MyPL.set( "Convergence Tolerance", tol ); //MyPL.set( "Step Size", step ); typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Anasazi::MultiVecTraits<double, MV> MVT; typedef Anasazi::OperatorTraits<double, MV, OP> OPT; // Create an Epetra_MultiVector for an initial vector to start the solver. // Note: This needs to have the same number of columns as the blocksize. Teuchos::RCP<Epetra_MultiVector> ivec = Teuchos::rcp( new Epetra_MultiVector(K->Map(), blockSize) ); MVT::MvRandom( *ivec ); // Call the ctor that calls the petra ctor for a matrix Teuchos::RCP<Anasazi::EpetraGenOp> Aop = Teuchos::rcp( new Anasazi::EpetraGenOp(BelosOp, M, false) ); Teuchos::RCP<Anasazi::BasicEigenproblem<double,MV,OP> > MyProblem = Teuchos::rcp( new Anasazi::BasicEigenproblem<double,MV,OP>(Aop, M, ivec) ); // Inform the eigenproblem that the matrix pencil (K,M) is symmetric MyProblem->setHermitian(true); // Set the number of eigenvalues requested MyProblem->setNEV( nev ); // Inform the eigenproblem that you are finished passing it information bool boolret = MyProblem->setProblem(); if (boolret != true) { if (MyPID == 0) { cout << "Anasazi::BasicEigenproblem::setProblem() returned with error." << endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Initialize the Block Arnoldi solver Anasazi::BlockKrylovSchurSolMgr<double, MV, OP> MySolverMgr(MyProblem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr.solve(); if (returnCode != Anasazi::Converged && MyPID==0) { cout << "Anasazi::EigensolverMgr::solve() returned unconverged." << endl; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<double,MV> sol = MyProblem->getSolution(); std::vector<Anasazi::Value<double> > evals = sol.Evals; Teuchos::RCP<MV> evecs = sol.Evecs; int numev = sol.numVecs; if (numev > 0) { Teuchos::SerialDenseMatrix<int,double> dmatr(numev,numev); Epetra_MultiVector tempvec(K->Map(), MVT::GetNumberVecs( *evecs )); OPT::Apply( *K, *evecs, tempvec ); MVT::MvTransMv( 1.0, tempvec, *evecs, dmatr ); if (MyPID==0) { double compeval = 0.0; cout.setf(std::ios_base::right, std::ios_base::adjustfield); cout<<"Actual Eigenvalues (obtained by Rayleigh quotient) : "<<endl; cout<<"------------------------------------------------------"<<endl; cout<<std::setw(16)<<"Real Part" <<std::setw(16)<<"Rayleigh Error"<<endl; cout<<"------------------------------------------------------"<<endl; for (i=0; i<numev; i++) { compeval = dmatr(i,i); cout<<std::setw(16)<<compeval <<std::setw(16)<<Teuchos::ScalarTraits<double>::magnitude(compeval-1.0/evals[i].realpart) <<endl; } cout<<"------------------------------------------------------"<<endl; } } #ifdef EPETRA_MPI MPI_Finalize(); #endif return 0; }
int main(int argc, char *argv[]) { using Teuchos::rcp_implicit_cast; using std::cout; using std::endl; bool boolret; int MyPID; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif MyPID = Comm.MyPID(); bool testFailed = false; bool verbose = false; bool debug = false; std::string filename("mhd1280b.cua"); std::string which("LR"); bool skinny = true; bool success = true; try { CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SR or LR)."); cmdp.setOption("skinny","hefty",&skinny,"Use a skinny (low-mem) or hefty (higher-mem) implementation of IRTR."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } #ifndef HAVE_EPETRA_THYRA if (verbose && MyPid == 0) { cout << "Please configure Anasazi with:" << endl; cout << "--enable-epetra-thyra" << endl; cout << "--enable-anasazi-thyra" << endl; } return 0; #endif typedef double ScalarType; typedef ScalarTraits<ScalarType> SCT; typedef SCT::magnitudeType MagnitudeType; typedef Thyra::MultiVectorBase<double> MV; typedef Thyra::LinearOpBase<double> OP; typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT; typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT; const ScalarType ONE = SCT::one(); if (verbose && MyPID == 0) { cout << Anasazi::Anasazi_Version() << endl << endl; } // Problem information int space_dim = 1; std::vector<double> brick_dim( space_dim ); brick_dim[0] = 1.0; std::vector<int> elements( space_dim ); elements[0] = 100; // Create problem RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); // // Get the stiffness and mass matrices RCP<Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); RCP<Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); // // Create the initial vectors int blockSize = 5; // // Get a pointer to the Epetra_Map RCP<const Epetra_Map> Map = rcp( &K->OperatorDomainMap(), false ); // // create an epetra multivector RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) ); ivec->Random(); // create a Thyra::VectorSpaceBase RCP<const Thyra::VectorSpaceBase<double> > epetra_vs = Thyra::create_VectorSpace(Map); // create a MultiVectorBase (from the Epetra_MultiVector) RCP<Thyra::MultiVectorBase<double> > thyra_ivec = Thyra::create_MultiVector(ivec, epetra_vs); // Create Thyra LinearOpBase objects from the Epetra_Operator objects RCP<const Thyra::LinearOpBase<double> > thyra_K = Thyra::epetraLinearOp(K); RCP<const Thyra::LinearOpBase<double> > thyra_M = Thyra::epetraLinearOp(M); // Create eigenproblem const int nev = 5; RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem = rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(thyra_K,thyra_M,thyra_ivec) ); // // Inform the eigenproblem that the operator K is symmetric problem->setHermitian(true); // // Set the number of eigenvalues requested problem->setNEV( nev ); // // Inform the eigenproblem that you are done passing it information boolret = problem->setProblem(); if (boolret != true) { if (verbose && MyPID == 0) { cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl << "End Result: TEST FAILED" << endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Set verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (verbose) { verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; } if (debug) { verbosity += Anasazi::Debug; } // Eigensolver parameters int maxIters = 450; MagnitudeType tol = 1.0e-6; // // Create parameter list to pass into the solver manager ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Iterations", maxIters ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Skinny Solver", skinny); // // Create the solver manager Anasazi::RTRSolMgr<ScalarType,MV,OP> MySolverMan(problem, MyPL); // // Check that the parameters were all consumed if (MyPL.getEntryPtr("Verbosity")->isUsed() == false || MyPL.getEntryPtr("Which")->isUsed() == false || MyPL.getEntryPtr("Block Size")->isUsed() == false || MyPL.getEntryPtr("Maximum Iterations")->isUsed() == false || MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false || MyPL.getEntryPtr("Skinny Solver")->isUsed() == false) { if (verbose && MyPID==0) { cout << "Failure! Unused parameters: " << endl; MyPL.unused(cout); } } // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMan.solve(); if (returnCode != Anasazi::Converged) { testFailed = true; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution(); std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals; RCP<MV> evecs = sol.Evecs; int numev = sol.numVecs; if (numev > 0) { std::ostringstream os; os.setf(std::ios::scientific, std::ios::floatfield); os.precision(6); // Compute the direct residual std::vector<ScalarType> normV( numev ); SerialDenseMatrix<int,ScalarType> T(numev,numev); for (int i=0; i<numev; i++) { T(i,i) = evals[i].realpart; } RCP<MV> Mvecs = MVT::Clone( *evecs, numev ), Kvecs = MVT::Clone( *evecs, numev ); OPT::Apply( *thyra_K, *evecs, *Kvecs ); OPT::Apply( *thyra_M, *evecs, *Mvecs ); MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs ); // compute M-norm of residuals OPT::Apply( *thyra_M, *Kvecs, *Mvecs ); MVT::MvDot( *Mvecs, *Kvecs, normV ); os << "Direct residual norms computed in LOBPCGThyra_test.exe" << endl << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl << "----------------------------------------" << endl; for (int i=0; i<numev; i++) { if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) { normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) / evals[i].realpart ); } else { normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) ); } os << std::setw(20) << evals[i].realpart << std::setw(20) << normV[i] << endl; if ( normV[i] > tol ) { testFailed = true; } } if (verbose && MyPID==0) { cout << endl << os.str() << endl; } } } TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success); #ifdef HAVE_MPI MPI_Finalize() ; #endif if (testFailed || success==false) { if (verbose && MyPID==0) { cout << "End Result: TEST FAILED" << endl; } return -1; } // // Default return value // if (verbose && MyPID==0) { cout << "End Result: TEST PASSED" << endl; } return 0; }
int main(int argc, char *argv[]) { int ierr = 0; #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); int rank; // My process ID MPI_Comm_rank(MPI_COMM_WORLD, &rank); Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else int rank = 0; Epetra_SerialComm Comm; #endif bool verbose = false; // Check if we should print results to standard out if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; int verbose_int = verbose ? 1 : 0; Comm.Broadcast(&verbose_int, 1, 0); verbose = verbose_int==1 ? true : false; // char tmp; // if (rank==0) cout << "Press any key to continue..."<< std::endl; // if (rank==0) cin >> tmp; // Comm.Barrier(); Comm.SetTracebackMode(0); // This should shut down any error traceback reporting int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); if(verbose && MyPID==0) cout << Epetra_Version() << std::endl << std::endl; if (verbose) cout << "Processor "<<MyPID<<" of "<< NumProc << " is alive."<<endl; //bool verbose1 = verbose; // unused // Redefine verbose to only print on PE 0 if(verbose && rank!=0) verbose = false; int NumMyEquations = 1; long long NumGlobalEquations = NumProc; // Get update list and number of local equations from newly created Map long long* MyGlobalElementsLL = new long long[NumMyEquations]; MyGlobalElementsLL[0] = 2000000000+MyPID; // Construct a Map that puts approximately the same Number of equations on each processor Epetra_Map MapLL(NumGlobalEquations, NumMyEquations, MyGlobalElementsLL, 0, Comm); EPETRA_TEST_ERR(MapLL.GlobalIndicesInt(),ierr); EPETRA_TEST_ERR(!(MapLL.GlobalIndicesLongLong()),ierr); // Create an integer vector NumNz that is used to build the Petra Matrix. // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation on this processor int* NumNzLL = new int[NumMyEquations]; NumNzLL[0] = 0; // Create int types meant to add to long long matrix for test of failure int* MyIntGlobalElementsLL = new int[NumMyEquations]; MyIntGlobalElementsLL[0] = 20000+MyPID; // Create a long long Epetra_Matrix Epetra_CrsMatrix A_LL(Copy, MapLL, NumNzLL); EPETRA_TEST_ERR(A_LL.IndicesAreGlobal(),ierr); EPETRA_TEST_ERR(A_LL.IndicesAreLocal(),ierr); // Insert values double one = 1.0; #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES // Try to add ints which should fail and be caught as an int try { A_LL.InsertGlobalValues(MyIntGlobalElementsLL[0], 1, &one, MyIntGlobalElementsLL+0); } catch(int i) { EPETRA_TEST_ERR(!(i==-1),ierr); } #endif // Add long longs which should succeed EPETRA_TEST_ERR(!(A_LL.InsertGlobalValues(MyGlobalElementsLL[0], 1, &one, MyGlobalElementsLL+0)==0),ierr); EPETRA_TEST_ERR(!(A_LL.IndicesAreGlobal()),ierr); EPETRA_TEST_ERR(!(A_LL.FillComplete(false)==0),ierr); EPETRA_TEST_ERR(!(A_LL.IndicesAreLocal()),ierr); // Get update list and number of local equations from newly created Map int* MyGlobalElementsInt = new int[NumMyEquations]; MyGlobalElementsInt[0] = 2000+MyPID; // Create an integer vector NumNz that is used to build the Petra Matrix. // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation on this processor int* NumNzInt = new int[NumMyEquations]; NumNzInt[0] = 0; // Create int types meant to add to long long matrix for test of failure long long* MyLLGlobalElementsInt = new long long[NumMyEquations]; MyLLGlobalElementsInt[0] = 2000000000+MyPID; #ifndef EPETRA_NO_32BIT_GLOBAL_INDICES // Construct a Map that puts approximately the same Number of equations on each processor Epetra_Map MapInt(NumGlobalEquations, NumMyEquations, MyGlobalElementsInt, 0LL, Comm); EPETRA_TEST_ERR(!(MapInt.GlobalIndicesInt()),ierr); EPETRA_TEST_ERR(MapInt.GlobalIndicesLongLong(),ierr); // Create a int Epetra_Matrix Epetra_CrsMatrix A_Int(Copy, MapInt, NumNzInt); EPETRA_TEST_ERR(A_Int.IndicesAreGlobal(),ierr); EPETRA_TEST_ERR(A_Int.IndicesAreLocal(),ierr); // Insert values try { A_Int.InsertGlobalValues(MyLLGlobalElementsInt[0], 1, &one, MyLLGlobalElementsInt+0); } catch(int i) { EPETRA_TEST_ERR(!(i==-1),ierr); } // Add long longs which should succeed EPETRA_TEST_ERR(!(A_Int.InsertGlobalValues(MyGlobalElementsInt[0], 1, &one, MyGlobalElementsInt+0)==0),ierr); EPETRA_TEST_ERR(!(A_Int.IndicesAreGlobal()),ierr); EPETRA_TEST_ERR(!(A_Int.FillComplete(false)==0),ierr); EPETRA_TEST_ERR(!(A_Int.IndicesAreLocal()),ierr); #endif delete [] MyGlobalElementsLL; delete [] NumNzLL; delete [] MyIntGlobalElementsLL; delete [] MyGlobalElementsInt; delete [] NumNzInt; delete [] MyLLGlobalElementsInt; #ifdef EPETRA_MPI MPI_Finalize() ; #endif /* end main */ return ierr ; }
int main(int argc, char *argv[]) { int ierr = 0; double elapsed_time; double total_flops; double MFLOPs; #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm comm( MPI_COMM_WORLD ); #else Epetra_SerialComm comm; #endif bool verbose = false; bool summary = false; // Check if we should print verbose results to standard out if (argc>6) if (argv[6][0]=='-' && argv[6][1]=='v') verbose = true; // Check if we should print verbose results to standard out if (argc>6) if (argv[6][0]=='-' && argv[6][1]=='s') summary = true; if(argc < 6) { cerr << "Usage: " << argv[0] << " NumNodesX NumNodesY NumProcX NumProcY NumPoints [-v|-s]" << endl << "where:" << endl << "NumNodesX - Number of mesh nodes in X direction per processor" << endl << "NumNodesY - Number of mesh nodes in Y direction per processor" << endl << "NumProcX - Number of processors to use in X direction" << endl << "NumProcY - Number of processors to use in Y direction" << endl << "NumPoints - Number of points to use in stencil (5, 9 or 25 only)" << endl << "-v|-s - (Optional) Run in verbose mode if -v present or summary mode if -s present" << endl << " NOTES: NumProcX*NumProcY must equal the number of processors used to run the problem." << endl << endl << " Serial example:" << endl << argv[0] << " 16 12 1 1 25 -v" << endl << " Run this program in verbose mode on 1 processor using a 16 X 12 grid with a 25 point stencil."<< endl <<endl << " MPI example:" << endl << "mpirun -np 32 " << argv[0] << " 10 12 4 8 9 -v" << endl << " Run this program in verbose mode on 32 processors putting a 10 X 12 subgrid on each processor using 4 processors "<< endl << " in the X direction and 8 in the Y direction. Total grid size is 40 points in X and 96 in Y with a 9 point stencil."<< endl << endl; return(1); } //char tmp; //if (comm.MyPID()==0) cout << "Press any key to continue..."<< endl; //if (comm.MyPID()==0) cin >> tmp; //comm.Barrier(); comm.SetTracebackMode(0); // This should shut down any error traceback reporting if (verbose && comm.MyPID()==0) cout << Epetra_Version() << endl << endl; if (summary && comm.MyPID()==0) { if (comm.NumProc()==1) cout << Epetra_Version() << endl << endl; else cout << endl << endl; // Print two blank line to keep output columns lined up } if (verbose) cout << comm <<endl; // Redefine verbose to only print on PE 0 if (verbose && comm.MyPID()!=0) verbose = false; if (summary && comm.MyPID()!=0) summary = false; int numNodesX = atoi(argv[1]); int numNodesY = atoi(argv[2]); int numProcsX = atoi(argv[3]); int numProcsY = atoi(argv[4]); int numPoints = atoi(argv[5]); if (verbose || (summary && comm.NumProc()==1)) { cout << " Number of local nodes in X direction = " << numNodesX << endl << " Number of local nodes in Y direction = " << numNodesY << endl << " Number of global nodes in X direction = " << numNodesX*numProcsX << endl << " Number of global nodes in Y direction = " << numNodesY*numProcsY << endl << " Number of local nonzero entries = " << numNodesX*numNodesY*numPoints << endl << " Number of global nonzero entries = " << numNodesX*numNodesY*numPoints*numProcsX*numProcsY << endl << " Number of Processors in X direction = " << numProcsX << endl << " Number of Processors in Y direction = " << numProcsY << endl << " Number of Points in stencil = " << numPoints << endl << endl; } // Print blank line to keep output columns lined up if (summary && comm.NumProc()>1) cout << endl << endl << endl << endl << endl << endl << endl << endl<< endl << endl; if (numProcsX*numProcsY!=comm.NumProc()) { cerr << "Number of processors = " << comm.NumProc() << endl << " is not the product of " << numProcsX << " and " << numProcsY << endl << endl; return(1); } if (numPoints!=5 && numPoints!=9 && numPoints!=25) { cerr << "Number of points specified = " << numPoints << endl << " is not 5, 9, 25" << endl << endl; return(1); } if (numNodesX*numNodesY<=0) { cerr << "Product of number of nodes is <= zero" << endl << endl; return(1); } Epetra_IntSerialDenseVector Xoff, XLoff, XUoff; Epetra_IntSerialDenseVector Yoff, YLoff, YUoff; if (numPoints==5) { // Generate a 5-point 2D Finite Difference matrix Xoff.Size(5); Yoff.Size(5); Xoff[0] = -1; Xoff[1] = 1; Xoff[2] = 0; Xoff[3] = 0; Xoff[4] = 0; Yoff[0] = 0; Yoff[1] = 0; Yoff[2] = 0; Yoff[3] = -1; Yoff[4] = 1; // Generate a 2-point 2D Lower triangular Finite Difference matrix XLoff.Size(2); YLoff.Size(2); XLoff[0] = -1; XLoff[1] = 0; YLoff[0] = 0; YLoff[1] = -1; // Generate a 3-point 2D upper triangular Finite Difference matrix XUoff.Size(3); YUoff.Size(3); XUoff[0] = 0; XUoff[1] = 1; XUoff[2] = 0; YUoff[0] = 0; YUoff[1] = 0; YUoff[2] = 1; } else if (numPoints==9) { // Generate a 9-point 2D Finite Difference matrix Xoff.Size(9); Yoff.Size(9); Xoff[0] = -1; Xoff[1] = 0; Xoff[2] = 1; Yoff[0] = -1; Yoff[1] = -1; Yoff[2] = -1; Xoff[3] = -1; Xoff[4] = 0; Xoff[5] = 1; Yoff[3] = 0; Yoff[4] = 0; Yoff[5] = 0; Xoff[6] = -1; Xoff[7] = 0; Xoff[8] = 1; Yoff[6] = 1; Yoff[7] = 1; Yoff[8] = 1; // Generate a 5-point lower triangular 2D Finite Difference matrix XLoff.Size(5); YLoff.Size(5); XLoff[0] = -1; XLoff[1] = 0; Xoff[2] = 1; YLoff[0] = -1; YLoff[1] = -1; Yoff[2] = -1; XLoff[3] = -1; XLoff[4] = 0; YLoff[3] = 0; YLoff[4] = 0; // Generate a 4-point upper triangular 2D Finite Difference matrix XUoff.Size(4); YUoff.Size(4); XUoff[0] = 1; YUoff[0] = 0; XUoff[1] = -1; XUoff[2] = 0; XUoff[3] = 1; YUoff[1] = 1; YUoff[2] = 1; YUoff[3] = 1; } else { // Generate a 25-point 2D Finite Difference matrix Xoff.Size(25); Yoff.Size(25); int xi = 0, yi = 0; int xo = -2, yo = -2; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; xo = -2, yo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; xo = -2, yo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; xo = -2, yo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; xo = -2, yo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Xoff[xi++] = xo++; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; Yoff[yi++] = yo ; // Generate a 13-point lower triangular 2D Finite Difference matrix XLoff.Size(13); YLoff.Size(13); xi = 0, yi = 0; xo = -2, yo = -2; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; xo = -2, yo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; xo = -2, yo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; XLoff[xi++] = xo++; YLoff[yi++] = yo ; YLoff[yi++] = yo ; YLoff[yi++] = yo ; // Generate a 13-point upper triangular 2D Finite Difference matrix XUoff.Size(13); YUoff.Size(13); xi = 0, yi = 0; xo = 0, yo = 0; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; xo = -2, yo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; xo = -2, yo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; XUoff[xi++] = xo++; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; YUoff[yi++] = yo ; } Epetra_Map * map; Epetra_Map * mapL; Epetra_Map * mapU; Epetra_CrsMatrix * A; Epetra_CrsMatrix * L; Epetra_CrsMatrix * U; Epetra_MultiVector * b; Epetra_MultiVector * bt; Epetra_MultiVector * xexact; Epetra_MultiVector * bL; Epetra_MultiVector * btL; Epetra_MultiVector * xexactL; Epetra_MultiVector * bU; Epetra_MultiVector * btU; Epetra_MultiVector * xexactU; Epetra_SerialDenseVector resvec(0); //Timings Epetra_Flops flopcounter; Epetra_Time timer(comm); #ifdef EPETRA_VERY_SHORT_PERFTEST int jstop = 1; #elif EPETRA_SHORT_PERFTEST int jstop = 1; #else int jstop = 2; #endif for (int j=0; j<jstop; j++) { for (int k=1; k<17; k++) { #ifdef EPETRA_VERY_SHORT_PERFTEST if (k<3 || (k%4==0 && k<9)) { #elif EPETRA_SHORT_PERFTEST if (k<6 || k%4==0) { #else if (k<7 || k%2==0) { #endif int nrhs=k; if (verbose) cout << "\n*************** Results for " << nrhs << " RHS with "; bool StaticProfile = (j!=0); if (verbose) { if (StaticProfile) cout << " static profile\n"; else cout << " dynamic profile\n"; } GenerateCrsProblem(numNodesX, numNodesY, numProcsX, numProcsY, numPoints, Xoff.Values(), Yoff.Values(), nrhs, comm, verbose, summary, map, A, b, bt, xexact, StaticProfile, false); #ifdef EPETRA_HAVE_JADMATRIX timer.ResetStartTime(); Epetra_JadMatrix JA(*A); elapsed_time = timer.ElapsedTime(); if (verbose) cout << "Time to create Jagged diagonal matrix = " << elapsed_time << endl; //cout << "A = " << *A << endl; //cout << "JA = " << JA << endl; runJadMatrixTests(&JA, b, bt, xexact, StaticProfile, verbose, summary); #endif runMatrixTests(A, b, bt, xexact, StaticProfile, verbose, summary); delete A; delete b; delete bt; delete xexact; GenerateCrsProblem(numNodesX, numNodesY, numProcsX, numProcsY, XLoff.Length(), XLoff.Values(), YLoff.Values(), nrhs, comm, verbose, summary, mapL, L, bL, btL, xexactL, StaticProfile, true); GenerateCrsProblem(numNodesX, numNodesY, numProcsX, numProcsY, XUoff.Length(), XUoff.Values(), YUoff.Values(), nrhs, comm, verbose, summary, mapU, U, bU, btU, xexactU, StaticProfile, true); runLUMatrixTests(L, bL, btL, xexactL, U, bU, btU, xexactU, StaticProfile, verbose, summary); delete L; delete bL; delete btL; delete xexactL; delete mapL; delete U; delete bU; delete btU; delete xexactU; delete mapU; Epetra_MultiVector q(*map, nrhs); Epetra_MultiVector z(q); Epetra_MultiVector r(q); delete map; q.SetFlopCounter(flopcounter); z.SetFlopCounter(q); r.SetFlopCounter(q); resvec.Resize(nrhs); flopcounter.ResetFlops(); timer.ResetStartTime(); //10 norms for( int i = 0; i < 10; ++i ) q.Norm2( resvec.Values() ); elapsed_time = timer.ElapsedTime(); total_flops = q.Flops(); MFLOPs = total_flops/elapsed_time/1000000.0; if (verbose) cout << "\nTotal MFLOPs for 10 Norm2's= " << MFLOPs << endl; if (summary) { if (comm.NumProc()==1) cout << "Norm2" << '\t'; cout << MFLOPs << endl; } flopcounter.ResetFlops(); timer.ResetStartTime(); //10 dot's for( int i = 0; i < 10; ++i ) q.Dot(z, resvec.Values()); elapsed_time = timer.ElapsedTime(); total_flops = q.Flops(); MFLOPs = total_flops/elapsed_time/1000000.0; if (verbose) cout << "Total MFLOPs for 10 Dot's = " << MFLOPs << endl; if (summary) { if (comm.NumProc()==1) cout << "DotProd" << '\t'; cout << MFLOPs << endl; } flopcounter.ResetFlops(); timer.ResetStartTime(); //10 dot's for( int i = 0; i < 10; ++i ) q.Update(1.0, z, 1.0, r, 0.0); elapsed_time = timer.ElapsedTime(); total_flops = q.Flops(); MFLOPs = total_flops/elapsed_time/1000000.0; if (verbose) cout << "Total MFLOPs for 10 Updates= " << MFLOPs << endl; if (summary) { if (comm.NumProc()==1) cout << "Update" << '\t'; cout << MFLOPs << endl; } } } } #ifdef EPETRA_MPI MPI_Finalize() ; #endif return ierr ; } // Constructs a 2D PDE finite difference matrix using the list of x and y offsets. // // nx (In) - number of grid points in x direction // ny (In) - number of grid points in y direction // The total number of equations will be nx*ny ordered such that the x direction changes // most rapidly: // First equation is at point (0,0) // Second at (1,0) // ... // nx equation at (nx-1,0) // nx+1st equation at (0,1) // numPoints (In) - number of points in finite difference stencil // xoff (In) - stencil offsets in x direction (of length numPoints) // yoff (In) - stencil offsets in y direction (of length numPoints) // A standard 5-point finite difference stencil would be described as: // numPoints = 5 // xoff = [-1, 1, 0, 0, 0] // yoff = [ 0, 0, 0, -1, 1] // nrhs - Number of rhs to generate. (First interface produces vectors, so nrhs is not needed // comm (In) - an Epetra_Comm object describing the parallel machine (numProcs and my proc ID) // map (Out) - Epetra_Map describing distribution of matrix and vectors/multivectors // A (Out) - Epetra_CrsMatrix constructed for nx by ny grid using prescribed stencil // Off-diagonal values are random between 0 and 1. If diagonal is part of stencil, // diagonal will be slightly diag dominant. // b (Out) - Generated RHS. Values satisfy b = A*xexact // bt (Out) - Generated RHS. Values satisfy b = A'*xexact // xexact (Out) - Generated exact solution to Ax = b and b' = A'xexact // Note: Caller of this function is responsible for deleting all output objects. void GenerateCrsProblem(int numNodesX, int numNodesY, int numProcsX, int numProcsY, int numPoints, int * xoff, int * yoff, const Epetra_Comm &comm, bool verbose, bool summary, Epetra_Map *& map, Epetra_CrsMatrix *& A, Epetra_Vector *& b, Epetra_Vector *& bt, Epetra_Vector *&xexact, bool StaticProfile, bool MakeLocalOnly) { Epetra_MultiVector * b1, * bt1, * xexact1; GenerateCrsProblem(numNodesX, numNodesY, numProcsX, numProcsY, numPoints, xoff, yoff, 1, comm, verbose, summary, map, A, b1, bt1, xexact1, StaticProfile, MakeLocalOnly); b = dynamic_cast<Epetra_Vector *>(b1); bt = dynamic_cast<Epetra_Vector *>(bt1); xexact = dynamic_cast<Epetra_Vector *>(xexact1); return; } void GenerateCrsProblem(int numNodesX, int numNodesY, int numProcsX, int numProcsY, int numPoints, int * xoff, int * yoff, int nrhs, const Epetra_Comm &comm, bool verbose, bool summary, Epetra_Map *& map, Epetra_CrsMatrix *& A, Epetra_MultiVector *& b, Epetra_MultiVector *& bt, Epetra_MultiVector *&xexact, bool StaticProfile, bool MakeLocalOnly) { Epetra_Time timer(comm); // Determine my global IDs long long * myGlobalElements; GenerateMyGlobalElements(numNodesX, numNodesY, numProcsX, numProcsY, comm.MyPID(), myGlobalElements); int numMyEquations = numNodesX*numNodesY; map = new Epetra_Map((long long)-1, numMyEquations, myGlobalElements, 0, comm); // Create map with 2D block partitioning. delete [] myGlobalElements; long long numGlobalEquations = map->NumGlobalElements64(); int profile = 0; if (StaticProfile) profile = numPoints; #ifdef EPETRA_HAVE_STATICPROFILE if (MakeLocalOnly) A = new Epetra_CrsMatrix(Copy, *map, *map, profile, StaticProfile); // Construct matrix with rowmap=colmap else A = new Epetra_CrsMatrix(Copy, *map, profile, StaticProfile); // Construct matrix #else if (MakeLocalOnly) A = new Epetra_CrsMatrix(Copy, *map, *map, profile); // Construct matrix with rowmap=colmap else A = new Epetra_CrsMatrix(Copy, *map, profile); // Construct matrix #endif long long * indices = new long long[numPoints]; double * values = new double[numPoints]; double dnumPoints = (double) numPoints; int nx = numNodesX*numProcsX; for (int i=0; i<numMyEquations; i++) { long long rowID = map->GID64(i); int numIndices = 0; for (int j=0; j<numPoints; j++) { long long colID = rowID + xoff[j] + nx*yoff[j]; // Compute column ID based on stencil offsets if (colID>-1 && colID<numGlobalEquations) { indices[numIndices] = colID; double value = - ((double) rand())/ ((double) RAND_MAX); if (colID==rowID) values[numIndices++] = dnumPoints - value; // Make diagonal dominant else values[numIndices++] = value; } } //cout << "Building row " << rowID << endl; A->InsertGlobalValues(rowID, numIndices, values, indices); } delete [] indices; delete [] values; double insertTime = timer.ElapsedTime(); timer.ResetStartTime(); A->FillComplete(false); double fillCompleteTime = timer.ElapsedTime(); if (verbose) cout << "Time to insert matrix values = " << insertTime << endl << "Time to complete fill = " << fillCompleteTime << endl; if (summary) { if (comm.NumProc()==1) cout << "InsertTime" << '\t'; cout << insertTime << endl; if (comm.NumProc()==1) cout << "FillCompleteTime" << '\t'; cout << fillCompleteTime << endl; } if (nrhs<=1) { b = new Epetra_Vector(*map); bt = new Epetra_Vector(*map); xexact = new Epetra_Vector(*map); } else { b = new Epetra_MultiVector(*map, nrhs); bt = new Epetra_MultiVector(*map, nrhs); xexact = new Epetra_MultiVector(*map, nrhs); } xexact->Random(); // Fill xexact with random values A->Multiply(false, *xexact, *b); A->Multiply(true, *xexact, *bt); return; }
int main(int argc, char *argv[]) { bool boolret; int MyPID; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif MyPID = Comm.MyPID(); bool testFailed = false; bool verbose = false; bool debug = false; string filename("mhd1280b.cua"); string which("LR"); bool skinny = true; bool success = true; try { CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); cmdp.setOption("skinny","hefty",&skinny,"Use a skinny (low-mem) or hefty (higher-mem) implementation of IRTR."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SR or LR)."); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } if (debug) verbose = true; typedef double ScalarType; typedef ScalarTraits<ScalarType> SCT; typedef SCT::magnitudeType MagnitudeType; typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT; typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT; if (verbose && MyPID == 0) { cout << Anasazi::Anasazi_Version() << endl << endl; } // Problem information int space_dim = 1; std::vector<double> brick_dim( space_dim ); brick_dim[0] = 1.0; std::vector<int> elements( space_dim ); elements[0] = 100; // Create problem RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); // // Get the stiffness and mass matrices RCP<const Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); RCP<const Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); const int FIRST_BS = 5; const int SECOND_BS = 5; const int THIRD_BS = 5; const int NEV = FIRST_BS+SECOND_BS+THIRD_BS; //////////////////////////////////////////////////////////////////////////////// // // Shared parameters RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem; Anasazi::Eigensolution<ScalarType,MV> sol1, sol21, sol22, sol23; RCP<const MV> cpoint; RCP<MV> ev2 = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), FIRST_BS+SECOND_BS+THIRD_BS) ); Anasazi::ReturnType returnCode; // // Verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (debug) { verbosity += Anasazi::Debug; } // // Eigensolver parameters int maxIters = 450; MagnitudeType tol = 1.0e-8; // // Create parameter list to pass into the solver managers ParameterList MyPL; MyPL.set( "Skinny Solver", skinny); MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Maximum Iterations", maxIters ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Use Locking", true ); MyPL.set( "Locking Tolerance", tol/10 ); try { //////////////////////////////////////////////////////////////////////////////// // // Build the first eigenproblem { RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), FIRST_BS+SECOND_BS+THIRD_BS) ); ivec->Random(); problem = rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(K,M,ivec) ); problem->setHermitian(true); problem->setNEV( FIRST_BS+SECOND_BS+THIRD_BS ); boolret = problem->setProblem(); TEST_FOR_EXCEPTION(boolret != true,get_out,"Anasazi::BasicEigenproblem::SetProblem() returned with error."); } //////////////////////////////////////////////////////////////////////////////// // // Build the first solver manager { MyPL.set( "Block Size", FIRST_BS+SECOND_BS+THIRD_BS ); Anasazi::RTRSolMgr<ScalarType,MV,OP> solverman1(problem, MyPL); returnCode = solverman1.solve(); TEST_FOR_EXCEPTION(returnCode != Anasazi::Converged, get_out, "First problem was not fully solved."); sol1 = problem->getSolution(); } //////////////////////////////////////////////////////////////////////////////// // // Build the second/1 eigenproblem { RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), FIRST_BS ) ); ivec->Random(); problem->setInitVec(ivec); problem->setNEV( FIRST_BS ); boolret = problem->setProblem(); TEST_FOR_EXCEPTION(boolret != true, get_out, "Anasazi::BasicEigenproblem::SetProblem() returned with error."); } //////////////////////////////////////////////////////////////////////////////// // // Build the second/1 solver manager { MyPL.set( "Block Size", FIRST_BS ); Anasazi::RTRSolMgr<ScalarType,MV,OP> solverman21(problem, MyPL); returnCode = solverman21.solve(); TEST_FOR_EXCEPTION(returnCode != Anasazi::Converged, get_out, "Second/1 problem was not fully solved."); sol21 = problem->getSolution(); std::vector<int> bsind1(FIRST_BS); for (int i=0; i<FIRST_BS; i++) bsind1[i] = i; MVT::SetBlock(*sol21.Evecs,bsind1,*ev2); cpoint = MVT::CloneView(*ev2,bsind1); } //////////////////////////////////////////////////////////////////////////////// // // Build the second/2 eigenproblem { RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), SECOND_BS ) ); ivec->Random(); problem->setAuxVecs(cpoint); problem->setInitVec(ivec); problem->setNEV( SECOND_BS ); boolret = problem->setProblem(); TEST_FOR_EXCEPTION(boolret != true, get_out, "Anasazi::BasicEigenproblem::SetProblem() returned with error."); } //////////////////////////////////////////////////////////////////////////////// // // Build the second/2 solver manager { MyPL.set( "Block Size", SECOND_BS ); Anasazi::RTRSolMgr<ScalarType,MV,OP> solverman22(problem, MyPL); returnCode = solverman22.solve(); TEST_FOR_EXCEPTION(returnCode != Anasazi::Converged, get_out, "Second/2 problem was not fully solved." ); sol22 = problem->getSolution(); std::vector<int> bsind2(SECOND_BS); for (int i=0; i<SECOND_BS; i++) bsind2[i] = FIRST_BS+i; MVT::SetBlock(*sol22.Evecs,bsind2,*ev2); std::vector<int> bsind12(FIRST_BS+SECOND_BS); for (int i=0; i<FIRST_BS+SECOND_BS; i++) bsind12[i] = i; cpoint = MVT::CloneView(*ev2,bsind12); } //////////////////////////////////////////////////////////////////////////////// // // Build the second/3 eigenproblem { RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), THIRD_BS ) ); ivec->Random(); problem->setAuxVecs(cpoint); problem->setInitVec(ivec); problem->setNEV( THIRD_BS ); boolret = problem->setProblem(); TEST_FOR_EXCEPTION(boolret != true, get_out, "Anasazi::BasicEigenproblem::SetProblem() returned with error."); } //////////////////////////////////////////////////////////////////////////////// // // Build the second/3 solver manager { MyPL.set( "Block Size", THIRD_BS ); Anasazi::RTRSolMgr<ScalarType,MV,OP> solverman23(problem, MyPL); returnCode = solverman23.solve(); TEST_FOR_EXCEPTION(returnCode != Anasazi::Converged, get_out, "Second/3 problem was not fully solved." ); sol23 = problem->getSolution(); std::vector<int> bsind3(THIRD_BS); for (int i=0; i<THIRD_BS; i++) bsind3[i] = FIRST_BS+SECOND_BS+i; MVT::SetBlock(*sol23.Evecs,bsind3,*ev2); cpoint = Teuchos::null; } } catch (const get_out &go) { if (verbose && MyPID==0) { cout << go.what() << endl; } testFailed = true; } if (testFailed == false) { cout.setf(std::ios::scientific, std::ios::floatfield); cout.precision(6); // // check the checkpointed solution against the non-checkpointed solution // // First, check the eigenvalues // // unless we altogether missed an eigenvalue, then // {sol21.Evals sol22.Evals sol23.Evals} == {sol1.Evals} std::vector<Anasazi::Value<double> > Evals1 = sol1.Evals; std::vector<Anasazi::Value<double> > Evals2 = sol21.Evals; Evals2.insert(Evals2.end(),sol22.Evals.begin(),sol22.Evals.end()); Evals2.insert(Evals2.end(),sol23.Evals.begin(),sol23.Evals.end()); // compare the differences double maxd = 0; if (verbose && MyPID==0) { cout << std::setw(40) << "Computed Eigenvalues" << endl; cout << std::setw(20) << "Without c/p" << std::setw(20) << "With c/p" << std::setw(20) << "Rel. error" << endl; cout << "============================================================" << endl; } for (int i=0; i<NEV; i++) { double tmpd = SCT::magnitude((Evals1[i].realpart - Evals2[i].realpart)/Evals1[i].realpart); maxd = (tmpd > maxd ? tmpd : maxd); if (verbose && MyPID==0) { cout << std::setw(20) << Evals1[i].realpart << std::setw(20) << Evals2[i].realpart << std::setw(20) << tmpd << endl; } } if (maxd > tol) { testFailed = true; } if (verbose && MyPID==0) { cout << endl; } // // Second, check the eigenspaces RCP<MV> Mev1; if (M != null) { Mev1 = MVT::Clone(*sol1.Evecs,NEV); OPT::Apply(*M,*sol1.Evecs,*Mev1); } else { Mev1 = sol1.Evecs; } SerialDenseMatrix<int,double> vtv(NEV,NEV); MVT::MvTransMv(1.0,*Mev1,*ev2,vtv); for (int i=0; i<NEV; i++) vtv(i,i) = SCT::magnitude(vtv(i,i)) - 1.0; maxd = vtv.normFrobenius(); if (verbose && MyPID==0) { cout << std::setw(20) << "|| EV1^H M EV2 - I ||_F" << endl; cout << std::setw(20) << maxd << endl; cout << endl; } if (maxd > SCT::squareroot(tol)*10) { testFailed = true; } } } TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success); #ifdef HAVE_MPI MPI_Finalize() ; #endif if (testFailed || success==false) { if (verbose && MyPID==0) { cout << "End Result: TEST FAILED" << endl; } return -1; } // // Default return value // if (verbose && MyPID==0) { cout << "End Result: TEST PASSED" << endl; } return 0; }
int main(int argc, char *argv[]) { int ierr = 0; #ifdef EPETRA_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // Comm.SetTracebackMode(0); // This should shut down any error tracing bool verbose = false; // Check if we should print results to standard out if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; #ifdef EPETRA_MPI int localverbose = verbose ? 1 : 0; int globalverbose=0; MPI_Allreduce(&localverbose, &globalverbose, 1, MPI_INT, MPI_SUM, MPI_COMM_WORLD); verbose = (globalverbose>0); #endif int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); if (verbose && MyPID==0) cout << Epetra_Version() << endl << endl; if (verbose) cout << Comm <<endl; int NumMyElements = 4; long long NumGlobalElements = NumMyElements*NumProc; int IndexBase = 0; Epetra_Map Map(NumGlobalElements, NumMyElements, IndexBase, Comm); EPETRA_TEST_ERR( Drumm1(Map, verbose),ierr); EPETRA_TEST_ERR( Drumm2(Map, verbose),ierr); bool preconstruct_graph = false; EPETRA_TEST_ERR( four_quads(Comm, preconstruct_graph, verbose), ierr); preconstruct_graph = true; EPETRA_TEST_ERR( four_quads(Comm, preconstruct_graph, verbose), ierr); EPETRA_TEST_ERR( rectangular(Comm, verbose), ierr); EPETRA_TEST_ERR( Young1(Comm, verbose), ierr); #ifdef EPETRA_MPI MPI_Finalize(); #endif return ierr; }
int main(int argc, char *argv[]) { int ierr = 0, i, j, k; #ifdef EPETRA_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif bool verbose = false; // Check if we should print results to standard out if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; if(verbose && Comm.MyPID()==0) std::cout << Epetra_Version() << std::endl << std::endl; int rank = Comm.MyPID(); // char tmp; // if (rank==0) std::cout << "Press any key to continue..."<< std::endl; // if (rank==0) cin >> tmp; // Comm.Barrier(); Comm.SetTracebackMode(0); // This should shut down any error traceback reporting if (verbose) std::cout << Comm << std::endl; // bool verbose1 = verbose; // Redefine verbose to only print on PE 0 if (verbose && rank!=0) verbose = false; int N = 20; int NRHS = 4; double * A = new double[N*N]; double * A1 = new double[N*N]; double * X = new double[(N+1)*NRHS]; double * X1 = new double[(N+1)*NRHS]; int LDX = N+1; int LDX1 = N+1; double * B = new double[N*NRHS]; double * B1 = new double[N*NRHS]; int LDB = N; int LDB1 = N; int LDA = N; int LDA1 = LDA; double OneNorm1; bool Upper = false; Epetra_SerialSpdDenseSolver solver; Epetra_SerialSymDenseMatrix * Matrix; for (int kk=0; kk<2; kk++) { for (i=1; i<=N; i++) { GenerateHilbert(A, LDA, i); OneNorm1 = 0.0; for (j=1; j<=i; j++) OneNorm1 += 1.0/((double) j); // 1-Norm = 1 + 1/2 + ...+1/n if (kk==0) { Matrix = new Epetra_SerialSymDenseMatrix(View, A, LDA, i); LDA1 = LDA; } else { Matrix = new Epetra_SerialSymDenseMatrix(Copy, A, LDA, i); LDA1 = i; } GenerateHilbert(A1, LDA1, i); if (kk==1) { solver.FactorWithEquilibration(true); Matrix->SetUpper(); Upper = true; solver.SolveToRefinedSolution(false); } for (k=0; k<NRHS; k++) for (j=0; j<i; j++) { B[j+k*LDB] = 1.0/((double) (k+3)*(j+3)); B1[j+k*LDB1] = B[j+k*LDB1]; } Epetra_SerialDenseMatrix Epetra_B(View, B, LDB, i, NRHS); Epetra_SerialDenseMatrix Epetra_X(View, X, LDX, i, NRHS); solver.SetMatrix(*Matrix); solver.SetVectors(Epetra_X, Epetra_B); ierr = check(solver, A1, LDA1, i, NRHS, OneNorm1, B1, LDB1, X1, LDX1, Upper, verbose); assert (ierr>-1); delete Matrix; if (ierr!=0) { if (verbose) std::cout << "Factorization failed due to bad conditioning. This is normal if SCOND is small." << std::endl; break; } } } delete [] A; delete [] A1; delete [] X; delete [] X1; delete [] B; delete [] B1; ///////////////////////////////////////////////////////////////////// // Now test norms and scaling functions ///////////////////////////////////////////////////////////////////// Epetra_SerialSymDenseMatrix D; double ScalarA = 2.0; int DM = 10; int DN = 10; D.Shape(DM); for (j=0; j<DN; j++) for (i=0; i<DM; i++) D[j][i] = (double) (1+i+j*DM) ; //std::cout << D << std::endl; double NormInfD_ref = (double)(DM*(DN*(DN+1))/2); double NormOneD_ref = NormInfD_ref; double NormInfD = D.NormInf(); double NormOneD = D.NormOne(); if (verbose) { std::cout << " *** Before scaling *** " << std::endl << " Computed one-norm of test matrix = " << NormOneD << std::endl << " Expected one-norm = " << NormOneD_ref << std::endl << " Computed inf-norm of test matrix = " << NormInfD << std::endl << " Expected inf-norm = " << NormInfD_ref << std::endl; } D.Scale(ScalarA); // Scale entire D matrix by this value //std::cout << D << std::endl; NormInfD = D.NormInf(); NormOneD = D.NormOne(); if (verbose) { std::cout << " *** After scaling *** " << std::endl << " Computed one-norm of test matrix = " << NormOneD << std::endl << " Expected one-norm = " << NormOneD_ref*ScalarA << std::endl << " Computed inf-norm of test matrix = " << NormInfD << std::endl << " Expected inf-norm = " << NormInfD_ref*ScalarA << std::endl; } ///////////////////////////////////////////////////////////////////// // Now test for larger system, both correctness and performance. ///////////////////////////////////////////////////////////////////// N = 2000; NRHS = 5; LDA = N; LDB = N; LDX = N; if (verbose) std::cout << "\n\nComputing factor of an " << N << " x " << N << " SPD matrix...Please wait.\n\n" << std::endl; // Define A and X A = new double[LDA*N]; X = new double[LDB*NRHS]; for (j=0; j<N; j++) { for (k=0; k<NRHS; k++) X[j+k*LDX] = 1.0/((double) (j+5+k)); for (i=0; i<N; i++) { if (i==j) A[i+j*LDA] = 100.0 + i; else A[i+j*LDA] = -1.0/((double) (i+10)*(j+10)); } } // Define Epetra_SerialDenseMatrix object Epetra_SerialSymDenseMatrix BigMatrix(Copy, A, LDA, N); Epetra_SerialSymDenseMatrix OrigBigMatrix(View, A, LDA, N); Epetra_SerialSpdDenseSolver BigSolver; BigSolver.FactorWithEquilibration(true); BigSolver.SetMatrix(BigMatrix); // Time factorization Epetra_Flops counter; BigSolver.SetFlopCounter(counter); Epetra_Time Timer(Comm); double tstart = Timer.ElapsedTime(); ierr = BigSolver.Factor(); if (ierr!=0 && verbose) std::cout << "Error in factorization = "<<ierr<< std::endl; assert(ierr==0); double time = Timer.ElapsedTime() - tstart; double FLOPS = counter.Flops(); double MFLOPS = FLOPS/time/1000000.0; if (verbose) std::cout << "MFLOPS for Factorization = " << MFLOPS << std::endl; // Define Left hand side and right hand side Epetra_SerialDenseMatrix LHS(View, X, LDX, N, NRHS); Epetra_SerialDenseMatrix RHS; RHS.Shape(N,NRHS); // Allocate RHS // Compute RHS from A and X Epetra_Flops RHS_counter; RHS.SetFlopCounter(RHS_counter); tstart = Timer.ElapsedTime(); RHS.Multiply('L', 1.0, OrigBigMatrix, LHS, 0.0); // Symmetric Matrix-multiply time = Timer.ElapsedTime() - tstart; Epetra_SerialDenseMatrix OrigRHS = RHS; FLOPS = RHS_counter.Flops(); MFLOPS = FLOPS/time/1000000.0; if (verbose) std::cout << "MFLOPS to build RHS (NRHS = " << NRHS <<") = " << MFLOPS << std::endl; // Set LHS and RHS and solve BigSolver.SetVectors(LHS, RHS); tstart = Timer.ElapsedTime(); ierr = BigSolver.Solve(); if (ierr==1 && verbose) std::cout << "LAPACK guidelines suggest this matrix might benefit from equilibration." << std::endl; else if (ierr!=0 && verbose) std::cout << "Error in solve = "<<ierr<< std::endl; assert(ierr>=0); time = Timer.ElapsedTime() - tstart; FLOPS = BigSolver.Flops(); MFLOPS = FLOPS/time/1000000.0; if (verbose) std::cout << "MFLOPS for Solve (NRHS = " << NRHS <<") = " << MFLOPS << std::endl; double * resid = new double[NRHS]; bool OK = Residual(N, NRHS, A, LDA, BigSolver.X(), BigSolver.LDX(), OrigRHS.A(), OrigRHS.LDA(), resid); if (verbose) { if (!OK) std::cout << "************* Residual do not meet tolerance *************" << std::endl; for (i=0; i<NRHS; i++) std::cout << "Residual[" << i <<"] = "<< resid[i] << std::endl; std::cout << std::endl; } // Solve again using the Epetra_SerialDenseVector class for LHS and RHS Epetra_SerialDenseVector X2; Epetra_SerialDenseVector B2; X2.Size(BigMatrix.N()); B2.Size(BigMatrix.M()); int length = BigMatrix.N(); {for (int kk=0; kk<length; kk++) X2[kk] = ((double ) kk)/ ((double) length);} // Define entries of X2 RHS_counter.ResetFlops(); B2.SetFlopCounter(RHS_counter); tstart = Timer.ElapsedTime(); B2.Multiply('N', 'N', 1.0, OrigBigMatrix, X2, 0.0); // Define B2 = A*X2 time = Timer.ElapsedTime() - tstart; Epetra_SerialDenseVector OrigB2 = B2; FLOPS = RHS_counter.Flops(); MFLOPS = FLOPS/time/1000000.0; if (verbose) std::cout << "MFLOPS to build single RHS = " << MFLOPS << std::endl; // Set LHS and RHS and solve BigSolver.SetVectors(X2, B2); tstart = Timer.ElapsedTime(); ierr = BigSolver.Solve(); time = Timer.ElapsedTime() - tstart; if (ierr==1 && verbose) std::cout << "LAPACK guidelines suggest this matrix might benefit from equilibration." << std::endl; else if (ierr!=0 && verbose) std::cout << "Error in solve = "<<ierr<< std::endl; assert(ierr>=0); FLOPS = counter.Flops(); MFLOPS = FLOPS/time/1000000.0; if (verbose) std::cout << "MFLOPS to solve single RHS = " << MFLOPS << std::endl; OK = Residual(N, 1, A, LDA, BigSolver.X(), BigSolver.LDX(), OrigB2.A(), OrigB2.LDA(), resid); if (verbose) { if (!OK) std::cout << "************* Residual do not meet tolerance *************" << std::endl; std::cout << "Residual = "<< resid[0] << std::endl; } delete [] resid; delete [] A; delete [] X; /////////////////////////////////////////////////// // Now test default constructor and index operators /////////////////////////////////////////////////// N = 5; Epetra_SerialSymDenseMatrix C; // Implicit call to default constructor, should not need to call destructor C.Shape(5); // Make it 5 by 5 double * C1 = new double[N*N]; GenerateHilbert(C1, N, N); // Generate Hilber matrix C1[1+2*N] = 1000.0; // Make matrix nonsymmetric // Fill values of C with Hilbert values for (i=0; i<N; i++) for (j=0; j<N; j++) C(i,j) = C1[i+j*N]; // Test if values are correctly written and read for (i=0; i<N; i++) for (j=0; j<N; j++) { assert(C(i,j) == C1[i+j*N]); assert(C(i,j) == C[j][i]); } if (verbose) std::cout << "Default constructor and index operator check OK. Values of Hilbert matrix = " << std::endl << C << std::endl << "Values should be 1/(i+j+1), except value (1,2) should be 1000" << std::endl; delete [] C1; #ifdef EPETRA_MPI MPI_Finalize() ; #endif /* end main */ return ierr ; }
void solvetriadmatrixwithtrilinos(int& nnz, int& order, int* row, int* col, double* val, double* rhs, double* solution) { #else void solvetriadmatrixwithtrilinos_(int& nnz, int& order, int* row, int* col, double* val, double* rhs, double* solution) { #endif try{ #ifdef _MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif int i, j, ierr; int MyPID = Comm.MyPID(); bool verbose = (MyPID == 0); Epetra_Map RowMap(order, 0, Comm); int NumMyElements = RowMap.NumMyElements(); int *MyGlobalElements = new int[NumMyElements]; RowMap.MyGlobalElements(&MyGlobalElements[0]); #ifdef _MPI int nPEs; MPI_Comm_size(MPI_COMM_WORLD, &nPEs); #endif int anEst = nnz / order + 1; Epetra_CrsMatrix A(Copy, RowMap, anEst); for (j=0; j<nnz; ++j) { if (RowMap.MyGID(row[j]) ) { ierr = A.InsertGlobalValues(row[j], 1, &(val[j]), &(col[j]) ); assert(ierr >= 0); } } ierr = A.FillComplete(); assert(ierr == 0); //------------------------------------------------------------------------- // RN_20091221: Taking care of the rhs //------------------------------------------------------------------------- Epetra_Vector b(RowMap); // Inserting values into the rhs double *MyGlobalValues = new double[NumMyElements]; for (j=0; j<NumMyElements; ++j) { MyGlobalValues[j] = rhs[MyGlobalElements[j] ]; } ierr = b.ReplaceGlobalValues(NumMyElements, &MyGlobalValues[0], &MyGlobalElements[0]); //------------------------------------------------------------------------- // RN_20091221: Taking care of the solution //------------------------------------------------------------------------- Epetra_Vector x(RowMap); Teuchos::ParameterList paramList; Teuchos::RCP<Teuchos::ParameterList> paramList1 = Teuchos::rcp(¶mList, false); Teuchos::updateParametersFromXmlFile("./strat1.xml", paramList1.get() ); Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream(); Stratimikos::DefaultLinearSolverBuilder linearSolverBuilder; Teuchos::RCP<Epetra_CrsMatrix> epetraOper = Teuchos::rcp(&A, false); Teuchos::RCP<Epetra_Vector> epetraRhs = Teuchos::rcp(&b, false); Teuchos::RCP<Epetra_Vector> epetraSol = Teuchos::rcp(&x, false); Teuchos::RCP<const Thyra::LinearOpBase<double> > thyraOper = Thyra::epetraLinearOp(epetraOper); Teuchos::RCP<Thyra::VectorBase<double> > thyraRhs = Thyra::create_Vector(epetraRhs, thyraOper->range() ); Teuchos::RCP<Thyra::VectorBase<double> > thyraSol = Thyra::create_Vector(epetraSol, thyraOper->domain() ); linearSolverBuilder.setParameterList(Teuchos::rcp(¶mList, false) ); Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > lowsFactory = linearSolverBuilder.createLinearSolveStrategy(""); lowsFactory->setOStream(out); lowsFactory->setVerbLevel(Teuchos::VERB_LOW); Teuchos::RCP<Thyra::LinearOpWithSolveBase<double> > lows = Thyra::linearOpWithSolve(*lowsFactory, thyraOper); Thyra::SolveStatus<double> status = Thyra::solve(*lows, Thyra::NOTRANS, *thyraRhs, &*thyraSol); thyraSol = Teuchos::null; // For debugging =) // cout << "A: " << A << endl; // cout << "b: " << b << endl; // cout << "x: " << x << endl; // Epetra_Vector temp(RowMap); // double residualNorm; // A.Multiply(false, x, temp); // temp.Update(-1, b, 1); // temp.Norm2(&residualNorm); Epetra_LocalMap localMap(order, 0, Comm); Epetra_Vector xExtra(localMap); // local vector in each processor // RN_20091218: Create an import map and then import the data to the // local vector. Epetra_Import import(localMap, RowMap); xExtra.Import(x, import, Add); xExtra.ExtractCopy(solution); delete[] MyGlobalElements; delete[] MyGlobalValues; } catch (std::exception& e) { cout << e.what() << endl; } catch (string& s) { cout << s << endl; } catch (char *s) { cout << s << endl; } catch (...) { cout << "Caught unknown exception!" << endl; } } } // extern "C"
int main(int argc, char *argv[]) { // Initialize MPI #ifdef HAVE_MPI MPI_Init(&argc,&argv); #endif // Create a communicator for Epetra objects #ifdef HAVE_MPI Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif // Get the process ID and the total number of processors int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); // Check verbosity level bool verbose = false; if (argc > 1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; // Get the number of elements from the command line int NumGlobalElements = 0; if ((argc > 2) && (verbose)) NumGlobalElements = atoi(argv[2]) + 1; else if ((argc > 1) && (!verbose)) NumGlobalElements = atoi(argv[1]) + 1; else NumGlobalElements = 101; // The number of unknowns must be at least equal to the // number of processors. if (NumGlobalElements < NumProc) { cout << "numGlobalBlocks = " << NumGlobalElements << " cannot be < number of processors = " << NumProc << endl; cout << "Test failed!" << endl; throw "NOX Error"; } // Create the interface between NOX and the application // This object is derived from NOX::Epetra::Interface Teuchos::RCP<Interface> interface = Teuchos::rcp(new Interface(NumGlobalElements, Comm)); // Set the PDE factor (for nonlinear forcing term). This could be specified // via user input. interface->setPDEfactor(1000.0); // Begin Nonlinear Solver ************************************ // Create the top level parameter list Teuchos::RCP<Teuchos::ParameterList> IfpackParamsPtr = Teuchos::rcp(new Teuchos::ParameterList); // Set the printing parameters in the "Printing" sublist Teuchos::ParameterList printParams; printParams.set("MyPID", MyPID); printParams.set("Output Precision", 3); printParams.set("Output Processor", 0); if (verbose) printParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::LinearSolverDetails + NOX::Utils::Parameters + NOX::Utils::Details + NOX::Utils::Warning + NOX::Utils::Debug + NOX::Utils::TestDetails + NOX::Utils::Error); else printParams.set("Output Information", NOX::Utils::Error + NOX::Utils::TestDetails); // Create a print class for controlling output below NOX::Utils p(printParams); // ******************************* // Setup Test Objects // ******************************* // Create Linear Objects // Get the vector from the Problem if (verbose) p.out() << "Creating Vectors and Matrices" << endl; Teuchos::RCP<Epetra_Vector> solution_vec = interface->getSolution(); Teuchos::RCP<Epetra_Vector> rhs_vec = Teuchos::rcp(new Epetra_Vector(*solution_vec)); Teuchos::RCP<Epetra_Vector> lhs_vec = Teuchos::rcp(new Epetra_Vector(*solution_vec)); Teuchos::RCP<Epetra_CrsMatrix> jacobian_matrix = interface->getJacobian(); if (verbose) p.out() << "Evaluating F and J" << endl; solution_vec->PutScalar(1.0); interface->computeF(*solution_vec, *rhs_vec); rhs_vec->Scale(-1.0); interface->computeJacobian(*solution_vec, *jacobian_matrix); double norm =0.0; rhs_vec->Norm2(&norm); if (verbose) p.out() << "Step 0, ||F|| = " << norm << endl; if (verbose) p.out() << "Creating Ifpack preconditioner" << endl; Ifpack Factory; Teuchos::RCP<Ifpack_Preconditioner> PreconditionerPtr; PreconditionerPtr = Teuchos::rcp(Factory.Create("ILU", jacobian_matrix.get(),0)); Teuchos::ParameterList teuchosParams; PreconditionerPtr->SetParameters(teuchosParams); PreconditionerPtr->Initialize(); PreconditionerPtr->Compute(); if (verbose) p.out() << "Creating Aztec Solver" << endl; Teuchos::RCP<AztecOO> aztecSolverPtr = Teuchos::rcp(new AztecOO()); if (verbose) aztecSolverPtr->SetAztecOption(AZ_output, AZ_last); else aztecSolverPtr->SetAztecOption(AZ_output, AZ_none); // ******************************* // Recompute Test // ******************************* if (verbose) { p.out() << "**********************************************" << endl; p.out() << "Testing Newton solve with prec reuse" << endl; p.out() << "**********************************************" << endl; } int step_number = 0; int max_steps = 20; bool converged = false; int total_linear_iterations = 0; while (norm > 1.0e-8 && step_number < max_steps) { step_number++; if (verbose) p.out() << "Step " << step_number << ", ||F|| = " << norm << endl; if (step_number != 1) { if (verbose) p.out() << "Recomputing Preconditioner (reusing graph)" << endl; PreconditionerPtr->Compute(); } aztecSolverPtr->SetUserMatrix(jacobian_matrix.get(), false); aztecSolverPtr->SetPrecOperator(PreconditionerPtr.get()); aztecSolverPtr->SetRHS(rhs_vec.get()); aztecSolverPtr->SetLHS(lhs_vec.get()); aztecSolverPtr->Iterate(400, 1.0e-4); solution_vec->Update(1.0, *lhs_vec, 1.0); interface->computeF(*solution_vec, *rhs_vec); rhs_vec->Scale(-1.0); interface->computeJacobian(*solution_vec, *jacobian_matrix); rhs_vec->Norm2(&norm); total_linear_iterations += aztecSolverPtr->NumIters(); if (norm < 1.0e-8) converged = true; } if (verbose) { p.out() << "Final Step " << step_number << ", ||F|| = " << norm << endl; if (converged) p.out() << "Converged!!" << endl; else p.out() << "Failed!!" << endl; } // Tests int status = 0; // Converged if (verbose) p.out() << "Total Number of Linear Iterations = " << total_linear_iterations << endl; if (Comm.NumProc() == 1 && total_linear_iterations != 10) status = 1; if (!converged) status = 2; // Summarize test results if (converged && status == 0) p.out() << "Test passed!" << endl; else p.out() << "Test failed!" << endl; #ifdef HAVE_MPI MPI_Finalize(); #endif if (verbose) p.out() << "Status = " << status << endl; // Final return value (0 = successfull, non-zero = failure) return status; }
//============================================================================== int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // only one process if (Comm.NumProc() != 1) { #ifdef HAVE_MPI MPI_Finalize(); #endif if (Comm.MyPID() == 0) cout << "Please run this test with one process only" << endl; // return success not to break the tests exit(EXIT_SUCCESS); } // ======================================================== // // now create the famous "upper arrow" matrix, which // // should be reordered as a "lower arrow". Sparsity pattern // // will be printed on screen. // // ======================================================== // int NumPoints = 16; #if !defined(EPETRA_NO_32BIT_GLOBAL_INDICES) || !defined(EPETRA_NO_64BIT_GLOBAL_INDICES) Epetra_Map Map(-1,NumPoints,0,Comm); #else Epetra_Map Map; #endif std::vector<int> Indices(NumPoints); std::vector<double> Values(NumPoints); Teuchos::RefCountPtr<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix(Copy,Map,0) ); for (int i = 0 ; i < NumPoints ; ++i) { int NumEntries; if (i == 0) { NumEntries = NumPoints; for (int j = 0 ; j < NumPoints ; ++j) { Indices[j] = j; Values[j] = 1.0; } } else { NumEntries = 2; Indices[0] = 0; Indices[1] = i; Values[0] = 1.0; Values[1] = 1.0; } #if !defined(EPETRA_NO_32BIT_GLOBAL_INDICES) || !defined(EPETRA_NO_64BIT_GLOBAL_INDICES) A->InsertGlobalValues(i, NumEntries, &Values[0], &Indices[0]); #endif } A->FillComplete(); // print the sparsity to file, postscript format ////Ifpack_PrintSparsity(A,"OrigA.ps"); // create the reordering... Teuchos::RefCountPtr<Ifpack_RCMReordering> Reorder = Teuchos::rcp( new Ifpack_RCMReordering() ); // and compute is on A IFPACK_CHK_ERR(Reorder->Compute(*A)); // cout information cout << *Reorder; // create a reordered matrix Ifpack_ReorderFilter ReordA(A, Reorder); // print the sparsity to file, postscript format ////Ifpack_PrintSparsity(ReordA,"ReordA.ps"); #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); }
int main( int argc, char **argv ) { // check for parallel computation #ifdef HAVE_MPI MPI_Init(&argc, &argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // define main parameters double c = 0.9999; // continuation parameter int N = 50; // number of grid points int maxNewtonIters = 20; // max number of Newton iterations int maxSteps = 50; // max number of continuation steps taken int ilocal, iglobal; // counter variables used for loops: // ilocal = counter for local elements on this processor; // iglobal = counter to signify global position across all procs int Myele; // holds the number of elements on the processor // Set flag for whether the computations will be Matrix-free (true) or will use a computed // Jacobian (false) bool doMatFree = false; // Create output file to save solutions ofstream outFile("Heq5.dat"); outFile.setf(ios::scientific, ios::floatfield); outFile.precision(10); // Define the problem class HeqProblem Problem(N,&Comm,outFile); // Build initial guess. The initial guess should be a solution vector x close to the // bifurcation point. // Create the initial guess vector Epetra_Vector InitialGuess(Problem.GetMap()); // Get the number of elements on this processor Myele = Problem.GetMap().NumMyElements(); // Compute the initial guess. For this example, it is a line from (0,1) to (1,8/3) for (ilocal=0; ilocal<Myele; ilocal++) { iglobal=Problem.GetMap().GID(ilocal); InitialGuess[ilocal]= 1.0 + (5.0*iglobal)/(3.0*(N-1)); } // Create the null vector for the Jacobian (ie, J*v=0, used to solve the system of equations // f(x,p)=0; J*v=0; v0*v=1. The solution of the system is [x*,v*,p*]. ) Teuchos::RCP<NOX::Abstract::Vector> nullVec = Teuchos::rcp(new NOX::Epetra::Vector(InitialGuess)); // Initialize to all ones nullVec->init(1.0); // NOTE: init is a function within the NOX::Abstract:Vector class which initializes every // value of the vector to the value within the parentheses (must be in 'double' format) // Create the top level parameter list Teuchos::RCP<Teuchos::ParameterList> ParamList = Teuchos::rcp(new Teuchos::ParameterList); // Create LOCA sublist Teuchos::ParameterList& locaParamsList = ParamList->sublist("LOCA"); // Create the sublist for continuation and set the stepper parameters Teuchos::ParameterList& stepperList = locaParamsList.sublist("Stepper"); //stepperList.set("Continuation Method", "Arc Length");// Default stepperList.set("Continuation Method", "Natural"); stepperList.set("Continuation Parameter", "dummy"); // Must set stepperList.set("Initial Value", 999.0); // Must set stepperList.set("Max Value", 50.0e4); // Must set stepperList.set("Min Value", 0.0); // Must set stepperList.set("Max Steps", maxSteps); // Should set stepperList.set("Max Nonlinear Iterations", maxNewtonIters); // Should set stepperList.set("Bordered Solver Method", "Bordering"); // Teuchos::ParameterList& nestedList = // stepperList.sublist("Nested Bordered Solver"); // nestedList.set("Bordered Solver Method", "Householder"); // nestedList.set("Include UV In Preconditioner", true); // //nestedList.set("Use P For Preconditioner", true); // nestedList.set("Preconditioner Method", "SMW"); // Set up parameters to compute Eigenvalues #ifdef HAVE_LOCA_ANASAZI // Create Anasazi Eigensolver sublist (needs --with-loca-anasazi) stepperList.set("Compute Eigenvalues",true); Teuchos::ParameterList& aList = stepperList.sublist("Eigensolver"); aList.set("Method", "Anasazi"); aList.set("Block Size", 1); // Size of blocks aList.set("Num Blocks", 20); // Size of Arnoldi factorization aList.set("Num Eigenvalues", 5); // Number of eigenvalues // aList.set("Sorting Order", "SR"); aList.set("Convergence Tolerance", 2.0e-7); // Tolerance aList.set("Step Size", 1); // How often to check convergence aList.set("Maximum Restarts",2); // Maximum number of restarts aList.set("Verbosity", Anasazi::Errors + Anasazi::Warnings + Anasazi::FinalSummary); // Verbosity #else stepperList.set("Compute Eigenvalues",false); #endif // Create bifurcation sublist. Note that for turning point continuation, the "type" // is set to "Turning Point". If not doing TP, type should be "None". Teuchos::ParameterList& bifurcationList = locaParamsList.sublist("Bifurcation"); bifurcationList.set("Type", "Turning Point"); bifurcationList.set("Bifurcation Parameter", "c"); // bifurcationList.set("Formulation", "Minimally Augmented"); bifurcationList.set("Symmetric Jacobian", false); bifurcationList.set("Update Null Vectors Every Continuation Step", true); bifurcationList.set("Update Null Vectors Every Nonlinear Iteration", false); bifurcationList.set("Transpose Solver Method","Explicit Transpose"); // bifurcationList.set("Transpose Solver Method","Transpose Preconditioner"); // bifurcationList.set("Transpose Solver Method","Left Preconditioning"); bifurcationList.set("Initial Null Vector Computation", "Solve df/dp"); // bifurcationList.set("Initial A Vector", nullVec); // minimally augmented // bifurcationList.set("Initial B Vector", nullVec); //minimally augmented // bifurcationList.set("Bordered Solver Method", "Householder"); // bifurcationList.set("Include UV In Preconditioner", true); // //bifurcationList.set("Use P For Preconditioner", true); // bifurcationList.set("Preconditioner Method", "SMW"); bifurcationList.set("Formulation", "Moore-Spence"); bifurcationList.set("Solver Method", "Phipps Bordering"); // better for nearly singular matrices // bifurcationList.set("Solver Method", "Salinger Bordering"); bifurcationList.set("Initial Null Vector", nullVec); bifurcationList.set("Length Normalization Vector", nullVec); // Create the sublist for the predictor Teuchos::ParameterList& predictorList = locaParamsList.sublist("Predictor"); predictorList.set("Method", "Secant"); // Default // predictorList.set("Method", "Constant"); // Other options // predictorList.set("Method", "Tangent"); // Other options // Create step size sublist Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size"); stepSizeList.set("Method", "Adaptive"); // Default stepSizeList.set("Initial Step Size", 0.1); // Should set stepSizeList.set("Min Step Size", 1.0e-6); // Should set stepSizeList.set("Max Step Size", 1.0); // Should set stepSizeList.set("Aggressiveness", 0.1); // Set up NOX info Teuchos::ParameterList& nlParams = ParamList->sublist("NOX"); // Set the nonlinear solver method nlParams.set("Nonlinear Solver", "Line Search Based"); // Set the printing parameters in the "Printing" sublist. This list determines how much // of the NOX information is output Teuchos::ParameterList& printParams = nlParams.sublist("Printing"); printParams.set("MyPID", Comm.MyPID()); printParams.set("Output Precision", 5); printParams.set("Output Processor", 0); printParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::LinearSolverDetails + NOX::Utils::Parameters + NOX::Utils::Details + NOX::Utils::Warning + NOX::Utils::StepperIteration + NOX::Utils::StepperDetails + NOX::Utils::StepperParameters); // NOX parameters - Sublist for line search Teuchos::ParameterList& searchParams = nlParams.sublist("Line Search"); searchParams.set("Method", "Backtrack"); // searchParams.set("Method", "Full Step"); // Sublist for direction Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); dirParams.set("Method", "Newton"); Teuchos::ParameterList& newtonParams = dirParams.sublist("Newton"); newtonParams.set("Forcing Term Method", "Constant"); // Sublist for linear solver for the Newton method Teuchos::ParameterList& lsParams = newtonParams.sublist("Linear Solver"); lsParams.set("Aztec Solver", "GMRES"); lsParams.set("Max Iterations", 800); lsParams.set("Tolerance", 1e-8); lsParams.set("Output Frequency", 1); lsParams.set("Preconditioner", "None"); // lsParams.set("Preconditioner", "AztecOO"); // lsParams.set("Aztec Preconditioner", "ilu"); // lsParams.set("Scaling", "None"); // lsParams.set("Scaling", "Row Sum"); lsParams.set("Compute Scaling Manually", false); // lsParams.set("Preconditioner", "Ifpack"); // lsParams.set("Ifpack Preconditioner", "ILU"); // lsParams.set("Preconditioner", "New Ifpack"); // Teuchos::ParameterList& ifpackParams = lsParams.sublist("Ifpack"); // ifpackParams.set("fact: level-of-fill", 1); // Set up the continuation parameter vector LOCA::ParameterVector p; p.addParameter("c",c); p.addParameter("dummy",999.0); // Create the problem interface Teuchos::RCP<SimpleProblemInterface> interface = Teuchos::rcp(new SimpleProblemInterface(&Problem,c) ); Teuchos::RCP<LOCA::Epetra::Interface::Required> iReq = interface; // Create the operator to hold either the Jacobian matrix or the Matrix-free operator Teuchos::RCP<Epetra_Operator> A; // Teuchos::RCP<Epetra_RowMatrix> A; Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac; // Need a NOX::Epetra::Vector for constructor // This becomes the initial guess vector that is used for the nonlinear solves NOX::Epetra::Vector noxInitGuess(InitialGuess, NOX::DeepCopy); if (doMatFree) { // Matrix Free application (Epetra Operator): Teuchos::RCP<NOX::Epetra::MatrixFree> MF = Teuchos::rcp(new NOX::Epetra::MatrixFree(printParams, interface, noxInitGuess)); A = MF; iJac = MF; } else { // Computed Jacobian application A = Teuchos::rcp( Problem.GetMatrix(), false ); iJac = interface; } // Create scaling object Teuchos::RCP<NOX::Epetra::Scaling> scaling = Teuchos::null; // scaling = Teuchos::rcp(new NOX::Epetra::Scaling); // Teuchos::RCP<Epetra_Vector> scalingVector = // Teuchos::rcp(new Epetra_Vector(soln.Map())); // //scaling->addRowSumScaling(NOX::Epetra::Scaling::Left, scalingVector); // scaling->addColSumScaling(NOX::Epetra::Scaling::Right, scalingVector); // Create transpose scaling object Teuchos::RCP<NOX::Epetra::Scaling> trans_scaling = Teuchos::null; // trans_scaling = Teuchos::rcp(new NOX::Epetra::Scaling); // Teuchos::RCP<Epetra_Vector> transScalingVector = // Teuchos::rcp(new Epetra_Vector(soln.Map())); // trans_scaling->addRowSumScaling(NOX::Epetra::Scaling::Right, // transScalingVector); // trans_scaling->addColSumScaling(NOX::Epetra::Scaling::Left, // transScalingVector); //bifurcationList.set("Transpose Scaling", trans_scaling); // Build the linear system solver Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linSys = Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(printParams, lsParams, iReq, iJac, A, noxInitGuess, scaling)); // use if scaling // noxInitGuess)); // use if no scaling // Create the Loca (continuation) vector NOX::Epetra::Vector locaSoln(noxInitGuess); // Create Epetra Factory Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory = Teuchos::rcp(new LOCA::Epetra::Factory); // Create global data object Teuchos::RCP<LOCA::GlobalData> globalData = LOCA::createGlobalData(ParamList, epetraFactory); // Create the Group - must be LOCA group Teuchos::RCP<LOCA::Epetra::Group> grpPtr = Teuchos::rcp(new LOCA::Epetra::Group(globalData, printParams, iReq, locaSoln, linSys, p)); // Calculate the first F(x0) as a starting point. This is only needed for // certain status tests, to ensure that an initial residual (|r0|) is calculated grpPtr->computeF(); // Set up the status tests to check for convergence // Determines the error tolerance for the Newton solves Teuchos::RCP<NOX::StatusTest::NormF> testNormF = Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-4)); // Sets the max number of nonlinear (Newton) iterations that will be taken. If this is not // already set, it will default to the '20' given Teuchos::RCP<NOX::StatusTest::MaxIters> testMaxIters = Teuchos::rcp(new NOX::StatusTest::MaxIters(stepperList.get("Max Nonlinear Iterations", 20))); // This combination of tests will be used by NOX to determine whether the step converged Teuchos::RCP<NOX::StatusTest::Combo> combo = Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR, testNormF, testMaxIters)); // This is sample code to write and read parameters to/from a file. Currently not activated! // To use, change the 'XXXHAVE_TEUCHOS_EXTENDED' TO 'HAVE_TEUCHOS_EXTENDED' #ifdef XXXHAVE_TEUCHOS_EXTENDED // Write the parameter list to a file cout << "Writing parameter list to \"input.xml\"" << endl; Teuchos::writeParameterListToXmlFile(*ParamList, "input.xml"); // Read in the parameter list from a file cout << "Reading parameter list from \"input.xml\"" << endl; Teuchos::RCP<Teuchos::ParameterList> paramList2 = Teuchos::rcp(new Teuchos::ParameterList); Teuchos::updateParametersFromXmlFile("input.xml", paramList2.get()); ParamList = paramList2; #endif // Create the stepper LOCA::Stepper stepper(globalData, grpPtr, combo, ParamList); LOCA::Abstract::Iterator::IteratorStatus status = stepper.run(); // Check if the stepper completed if (status == LOCA::Abstract::Iterator::Finished) globalData->locaUtils->out() << "\nAll tests passed!" << endl; else if (globalData->locaUtils->isPrintType(NOX::Utils::Error)) globalData->locaUtils->out() << "\nStepper failed to converge!" << endl; // Output the stepper parameter list info if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) { globalData->locaUtils->out() << endl << "Final Parameters" << endl << "*******************" << endl; stepper.getList()->print(globalData->locaUtils->out()); globalData->locaUtils->out() << endl; } // Make sure all processors are done and close the output file Comm.Barrier(); outFile.close(); // Deallocate memory LOCA::destroyGlobalData(globalData); #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); } // DONE!!
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif CommandLineProcessor CLP; int n = 10; int m = (int)pow((double)Comm.NumProc(), 0.3334); double DampingFactor = 1.333; std::string AggregationScheme = "Uncoupled"; int NPA = 16; int MaxLevels = 5; CLP.setOption("l", &MaxLevels, "number of levels"); CLP.setOption("n", &n, "number of nodes along each axis"); CLP.setOption("damp", &DampingFactor, "prolongator damping factor"); CLP.setOption("aggr", &AggregationScheme, "aggregation scheme"); CLP.setOption("npa", &NPA, "nodes per aggregate (if supported by the aggregation scheme)"); CLP.throwExceptions(false); CLP.parse(argc,argv); if (m * m * m != Comm.NumProc()) { if (Comm.MyPID() == 0) { std::cout << "Number of processes must be a perfect cube." << std::endl; std::cout << "Please re-run with --help option for details." << std::endl; } #ifdef HAVE_MPI MPI_Finalize(); #endif exit(EXIT_SUCCESS); } n *= m; Laplace3D A(Comm, n, n, n, m, m, m, true); Epetra_Vector LHS(A.OperatorDomainMap()); Epetra_Vector RHS(A.OperatorDomainMap()); LHS.Random(); RHS.PutScalar(0.0); Epetra_LinearProblem Problem(&A, &LHS, &RHS); // Construct a solver object for this problem AztecOO solver(Problem); // create a parameter list for ML options ParameterList MLList; // set defaults for classic smoothed aggregation ML_Epetra::SetDefaults("SA",MLList); // overwrite some parameters. Please refer to the user's guide // for more information MLList.set("max levels",MaxLevels); MLList.set("increasing or decreasing","increasing"); MLList.set("aggregation: type", AggregationScheme); MLList.set("aggregation: damping factor", DampingFactor); MLList.set("aggregation: nodes per aggregate", NPA); MLList.set("smoother: type","symmetric Gauss-Seidel"); MLList.set("smoother: pre or post", "both"); MLList.set("coarse: max size", 512); MLList.set("coarse: type","Amesos-KLU"); MLList.set("analyze memory", true); MLList.set("repartition: enable", true); MLList.set("repartition: max min ratio", 1.1); MLList.set("repartition: min per proc", 512); MLList.set("low memory usage", true); MLList.set("x-coordinates", (double*) A.XCoord()); MLList.set("y-coordinates", (double*) A.YCoord()); MLList.set("z-coordinates", (double*) A.ZCoord()); // create the preconditioner object and compute hierarchy MultiLevelPreconditioner* MLPrec = new MultiLevelPreconditioner(A, MLList); // tell AztecOO to use this preconditioner, then solve solver.SetPrecOperator(MLPrec); solver.SetAztecOption(AZ_solver, AZ_cg_condnum); solver.SetAztecOption(AZ_output, 1); solver.Iterate(500, 1e-10); delete MLPrec; double norm; LHS.Norm2(&norm); if (Comm.MyPID() == 0) std::cout << "Norm of the error = " << norm << std::endl; #ifdef HAVE_MPI MPI_Finalize() ; #endif exit(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int i; #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif int MyPID = Comm.MyPID(); // Number of dimension of the domain int space_dim = 2; // Size of each of the dimensions of the domain std::vector<double> brick_dim( space_dim ); brick_dim[0] = 1.0; brick_dim[1] = 1.0; // Number of elements in each of the dimensions of the domain std::vector<int> elements( space_dim ); elements[0] = 10; elements[1] = 10; // Create problem Teuchos::RCP<ModalProblem> testCase = Teuchos::rcp( new ModeLaplace2DQ2(Comm, brick_dim[0], elements[0], brick_dim[1], elements[1]) ); // Get the stiffness and mass matrices Teuchos::RCP<Epetra_CrsMatrix> K = Teuchos::rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); Teuchos::RCP<Epetra_CrsMatrix> M = Teuchos::rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); // // ******************************************************* // Set up Amesos direct solver for inner iteration // ******************************************************* // // Create the shifted system K - sigma * M. // For the buckling transformation, this shift must be nonzero. double sigma = 1.0; Epetra_CrsMatrix Kcopy( *K ); int addErr = EpetraExt::MatrixMatrix::Add( *M, false, -sigma, Kcopy, 1.0 ); if (addErr != 0) { if (MyPID == 0) { std::cout << "EpetraExt::MatrixMatrix::Add returned with error: " << addErr << std::endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Create Epetra linear problem class to solve "x = b" Epetra_LinearProblem AmesosProblem; AmesosProblem.SetOperator(&Kcopy); // Create Amesos factory and solver for solving "(K - sigma*M)x = b" using a direct factorization Amesos amesosFactory; Teuchos::RCP<Amesos_BaseSolver> AmesosSolver = Teuchos::rcp( amesosFactory.Create( "Klu", AmesosProblem ) ); // The AmesosBucklingOp class assumes that the symbolic/numeric factorizations have already // been performed on the linear problem. AmesosSolver->SymbolicFactorization(); AmesosSolver->NumericFactorization(); // // ************************************ // Start the block Arnoldi iteration // ************************************ // // Variables used for the Block Arnoldi Method // int nev = 10; int blockSize = 3; int numBlocks = 3*nev/blockSize; int maxRestarts = 5; //int step = 5; double tol = 1.0e-8; std::string which = "LM"; int verbosity = Anasazi::Errors + Anasazi::Warnings + Anasazi::FinalSummary; // // Create parameter list to pass into solver // Teuchos::ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Num Blocks", numBlocks ); MyPL.set( "Maximum Restarts", maxRestarts ); MyPL.set( "Convergence Tolerance", tol ); //MyPL.set( "Step Size", step ); typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Anasazi::MultiVecTraits<double, MV> MVT; typedef Anasazi::OperatorTraits<double, MV, OP> OPT; // Create an Epetra_MultiVector for an initial vector to start the solver. // Note: This needs to have the same number of columns as the blocksize. Teuchos::RCP<Epetra_MultiVector> ivec = Teuchos::rcp( new Epetra_MultiVector(K->Map(), blockSize) ); MVT::MvRandom( *ivec ); // Create the Epetra_Operator for the buckling transformation using the Amesos direct solver. Teuchos::RCP<AmesosBucklingOp> BucklingOp = Teuchos::rcp( new AmesosBucklingOp(AmesosProblem, AmesosSolver, K) ); Teuchos::RCP<Anasazi::BasicEigenproblem<double,MV,OP> > MyProblem = Teuchos::rcp( new Anasazi::BasicEigenproblem<double,MV,OP>(BucklingOp, K, ivec) ); // Inform the eigenproblem that the matrix pencil (K,M) is symmetric MyProblem->setHermitian(true); // Set the number of eigenvalues requested MyProblem->setNEV( nev ); // Inform the eigenproblem that you are finished passing it information bool boolret = MyProblem->setProblem(); if (boolret != true) { if (MyPID == 0) { std::cout << "Anasazi::BasicEigenproblem::setProblem() returned with error." << std::endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Initialize the Block Arnoldi solver Anasazi::BlockKrylovSchurSolMgr<double, MV, OP> MySolverMgr(MyProblem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr.solve(); if (returnCode != Anasazi::Converged && MyPID==0) { std::cout << "Anasazi::EigensolverMgr::solve() returned unconverged." << std::endl; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<double,MV> sol = MyProblem->getSolution(); std::vector<Anasazi::Value<double> > evals = sol.Evals; Teuchos::RCP<MV> evecs = sol.Evecs; int numev = sol.numVecs; if (numev > 0) { // Undo buckling transformation; computed eigenvalues are real std::vector<double> compEvals(numev); for (i=0; i<numev; ++i) { compEvals[i] = sigma*evals[i].realpart/(evals[i].realpart-1.0); } // Remember, eigenvectors are constructed K-orthogonal to preserve symmetry, // so numerator of the Rayleigh quotient is 1.0. Teuchos::SerialDenseMatrix<int,double> dmatr(numev,numev); Epetra_MultiVector tempvec(M->Map(), MVT::GetNumberVecs( *evecs )); OPT::Apply( *M, *evecs, tempvec ); MVT::MvTransMv( 1.0, tempvec, *evecs, dmatr ); if (MyPID==0) { double rq_eval = 0.0; std::cout.setf(std::ios_base::right, std::ios_base::adjustfield); std::cout<<"Actual Eigenvalues (obtained by Rayleigh quotient) : "<<std::endl; std::cout<<"------------------------------------------------------"<<std::endl; std::cout<<std::setw(16)<<"Real Part" <<std::setw(16)<<"Rayleigh Error"<<std::endl; std::cout<<"------------------------------------------------------"<<std::endl; for (i=0; i<numev; i++) { rq_eval = 1.0 / dmatr(i,i); std::cout<<std::setw(16)<<rq_eval <<std::setw(16)<<Teuchos::ScalarTraits<double>::magnitude(rq_eval-compEvals[i]) <<std::endl; } std::cout<<"------------------------------------------------------"<<std::endl; } } #ifdef EPETRA_MPI MPI_Finalize(); #endif return 0; }
int main(int argc, char *argv[]) { int ierr=0, returnierr=0; #ifdef EPETRA_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif bool verbose = false; // Check if we should print results to standard out if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; if (!verbose) { Comm.SetTracebackMode(0); // This should shut down any error traceback reporting } int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); if (verbose && MyPID==0) cout << Epetra_Version() << endl << endl; if (verbose) cout << Comm << endl; bool verbose1 = verbose; if (verbose) verbose = (MyPID==0); int NumMyElements = 10000; int NumMyElements1 = NumMyElements; // Used for local map int NumGlobalElements = NumMyElements*NumProc+EPETRA_MIN(NumProc,3); if (MyPID < 3) NumMyElements++; int IndexBase = 0; bool DistributedGlobal = (NumGlobalElements>NumMyElements); Epetra_Map* Map; // Test exceptions if (verbose) cout << "*******************************************************************************************" << endl << " Testing Exceptions (Expect error messages if EPETRA_NO_ERROR_REPORTS is not defined" << endl << "*******************************************************************************************" << endl << endl << endl; try { if (verbose) cout << "Checking Epetra_Map(-2, IndexBase, Comm)" << endl; Map = new Epetra_Map(-2, IndexBase, Comm); } catch (int Error) { if (Error!=-1) { if (Error!=0) { EPETRA_TEST_ERR(Error,returnierr); if (verbose) cout << "Error code should be -1" << endl; } else { cout << "Error code = " << Error << "Should be -1" << endl; returnierr+=1; } } else if (verbose) cout << "Checked OK\n\n" << endl; } try { if (verbose) cout << "Checking Epetra_Map(2, 3, IndexBase, Comm)" << endl; Map = new Epetra_Map(2, 3, IndexBase, Comm); } catch (int Error) { if (Error!=-4) { if (Error!=0) { EPETRA_TEST_ERR(Error,returnierr); if (verbose) cout << "Error code should be -4" << endl; } else { cout << "Error code = " << Error << "Should be -4" << endl; returnierr+=1; } } else if (verbose) cout << "Checked OK\n\n" << endl; } if (verbose) cerr << flush; if (verbose) cout << flush; Comm.Barrier(); if (verbose) cout << endl << endl << "*******************************************************************************************" << endl << " Testing valid constructor now......................................................" << endl << "*******************************************************************************************" << endl << endl << endl; // Test Epetra-defined uniform linear distribution constructor Map = new Epetra_Map(NumGlobalElements, IndexBase, Comm); if (verbose) cout << "Checking Epetra_Map(NumGlobalElements, IndexBase, Comm)" << endl; ierr = checkmap(*Map, NumGlobalElements, NumMyElements, 0, IndexBase, Comm, DistributedGlobal); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; delete Map; // Test User-defined linear distribution constructor Map = new Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm); if (verbose) cout << "Checking Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm)" << endl; ierr = checkmap(*Map, NumGlobalElements, NumMyElements, 0, IndexBase, Comm, DistributedGlobal); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; delete Map; // Test User-defined arbitrary distribution constructor // Generate Global Element List. Do in reverse for fun! int * MyGlobalElements = new int[NumMyElements]; int MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase; if (Comm.MyPID()>2) MaxMyGID+=3; for (int i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i; Map = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm); if (verbose) cout << "Checking Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm)" << endl; ierr = checkmap(*Map, NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm, DistributedGlobal); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; // Test Copy constructor Epetra_Map* Map1 = new Epetra_Map(*Map); // Test SameAs() method bool same = Map1->SameAs(*Map); EPETRA_TEST_ERR(!(same==true),ierr);// should return true since Map1 is a copy of Map Epetra_BlockMap* Map2 = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm); same = Map2->SameAs(*Map); EPETRA_TEST_ERR(!(same==true),ierr); // Map and Map2 were created with the same sets of parameters delete Map2; // now test SameAs() on a map that is different Map2 = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase-1, Comm); same = Map2->SameAs(*Map); EPETRA_TEST_ERR(!(same==false),ierr); // IndexBases are different delete Map2; // Back to testing copy constructor if (verbose) cout << "Checking Epetra_Map(*Map)" << endl; ierr = checkmap(*Map1, NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm, DistributedGlobal); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; Epetra_Map* SmallMap = 0; if (verbose1) { // Build a small map for test cout. Use 10 elements from current map int* MyEls = Map->MyGlobalElements(); int IndBase = Map->IndexBase(); int MyLen = EPETRA_MIN(10+Comm.MyPID(),Map->NumMyElements()); SmallMap = new Epetra_Map(-1, MyLen, MyEls, IndBase, Comm); } delete [] MyGlobalElements; delete Map; delete Map1; // Test reference-counting in Epetra_Map if (verbose) cout << "Checking Epetra_Map reference counting" << endl; ierr = checkMapDataClass(Comm, verbose); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; // Test LocalMap constructor Epetra_LocalMap* LocalMap = new Epetra_LocalMap(NumMyElements1, IndexBase, Comm); if (verbose) cout << "Checking Epetra_LocalMap(NumMyElements1, IndexBase, Comm)" << endl; ierr = checkmap(*LocalMap, NumMyElements1, NumMyElements1, 0, IndexBase, Comm, false); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; // Test Copy constructor Epetra_LocalMap* LocalMap1 = new Epetra_LocalMap(*LocalMap); if (verbose) cout << "Checking Epetra_LocalMap(*LocalMap)" << endl; ierr = checkmap(*LocalMap1, NumMyElements1, NumMyElements1, 0, IndexBase, Comm, false); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; delete LocalMap1; delete LocalMap; // Test reference-counting in Epetra_LocalMap if (verbose) cout << "Checking Epetra_LocalMap reference counting" << endl; ierr = checkLocalMapDataClass(Comm, verbose); EPETRA_TEST_ERR(ierr,returnierr); if (verbose && ierr==0) cout << "Checked OK\n\n" <<endl; // Test output if (verbose1) { if (verbose) cout << "Test ostream << operator" << endl << flush; cout << *SmallMap; delete SmallMap; } #ifdef EPETRA_MPI MPI_Finalize(); #endif return returnierr; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif Teuchos::CommandLineProcessor clp(false); clp.setDocString("This is the canonical ML scaling example"); //Problem std::string optMatrixType = "Laplace2D"; clp.setOption("matrixType", &optMatrixType, "matrix type ('Laplace2D', 'Laplace3D')"); int optNx = 100; clp.setOption("nx", &optNx, "mesh size in x direction"); int optNy = -1; clp.setOption("ny", &optNy, "mesh size in y direction"); int optNz = -1; clp.setOption("nz", &optNz, "mesh size in z direction"); //Smoothers //std::string optSmooType = "Chebshev"; clp.setOption("smooType", &optSmooType, "smoother type ('l1-sgs', 'sgs 'or 'cheby')"); int optSweeps = 3; clp.setOption("sweeps", &optSweeps, "Chebyshev degreee (or SGS sweeps)"); double optAlpha = 7; clp.setOption("alpha", &optAlpha, "Chebyshev eigenvalue ratio (recommend 7 in 2D, 20 in 3D)"); //Coarsening int optMaxCoarseSize = 500; clp.setOption("maxcoarse", &optMaxCoarseSize, "Size of coarsest grid when coarsening should stop"); int optMaxLevels = 10; clp.setOption("maxlevels", &optMaxLevels, "Maximum number of levels"); //Krylov solver double optTol = 1e-12; clp.setOption("tol", &optTol, "stopping tolerance for Krylov method"); int optMaxIts = 500; clp.setOption("maxits", &optMaxIts, "maximum iterations for Krylov method"); //XML file with additional options std::string xmlFile = ""; clp.setOption("xml", &xmlFile, "XML file containing ML options. [OPTIONAL]"); //Debugging int optWriteMatrices = -2; clp.setOption("write", &optWriteMatrices, "write matrices to file (-1 means all; i>=0 means level i)"); switch (clp.parse(argc, argv)) { case Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED: return EXIT_SUCCESS; break; case Teuchos::CommandLineProcessor::PARSE_ERROR: case Teuchos::CommandLineProcessor::PARSE_UNRECOGNIZED_OPTION: return EXIT_FAILURE; break; case Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL: break; } #ifdef ML_SCALING const int ntimers=4; enum {total, probBuild, precBuild, solve}; ml_DblLoc timeVec[ntimers], maxTime[ntimers], minTime[ntimers]; for (int i=0; i<ntimers; i++) timeVec[i].rank = Comm.MyPID(); timeVec[total].value = MPI_Wtime(); #endif // Creates the linear problem using the Galeri package. // Several matrix examples are supported; please refer to the // Galeri documentation for more details. // Most of the examples using the ML_Epetra::MultiLevelPreconditioner // class are based on Epetra_CrsMatrix. Example // `ml_EpetraVbr.cpp' shows how to define a Epetra_VbrMatrix. // `Laplace2D' is a symmetric matrix; an example of non-symmetric // matrices is `Recirc2D' (advection-diffusion in a box, with // recirculating flow). The grid has optNx x optNy nodes, divided into // mx x my subdomains, each assigned to a different processor. if (optNy == -1) optNy = optNx; if (optNz == -1) optNz = optNx; ParameterList GaleriList; GaleriList.set("nx", optNx); GaleriList.set("ny", optNy); GaleriList.set("nz", optNz); //GaleriList.set("mx", 1); //GaleriList.set("my", Comm.NumProc()); #ifdef ML_SCALING timeVec[probBuild].value = MPI_Wtime(); #endif Epetra_Map* Map; Epetra_CrsMatrix* A; Epetra_MultiVector* Coord; if (optMatrixType == "Laplace2D") { Map = CreateMap("Cartesian2D", Comm, GaleriList); A = CreateCrsMatrix("Laplace2D", Map, GaleriList); Coord = CreateCartesianCoordinates("2D", &(A->Map()), GaleriList); } else if (optMatrixType == "Laplace3D") { Map = CreateMap("Cartesian3D", Comm, GaleriList); A = CreateCrsMatrix("Laplace3D", Map, GaleriList); Coord = CreateCartesianCoordinates("3D", &(A->Map()), GaleriList); } else { throw(std::runtime_error("Bad matrix type")); } //EpetraExt::RowMatrixToMatlabFile("A.m",*A); double *x_coord = (*Coord)[0]; double *y_coord = (*Coord)[1]; double* z_coord=NULL; if (optMatrixType == "Laplace3D") z_coord = (*Coord)[2]; //EpetraExt::MultiVectorToMatrixMarketFile("mlcoords.m",*Coord); if( Comm.MyPID()==0 ) { std::cout << "========================================================" << std::endl; std::cout << " Matrix type: " << optMatrixType << std::endl; if (optMatrixType == "Laplace2D") std::cout << " Problem size: " << optNx*optNy << " (" << optNx << "x" << optNy << ")" << std::endl; else if (optMatrixType == "Laplace3D") std::cout << " Problem size: " << optNx*optNy*optNz << " (" << optNx << "x" << optNy << "x" << optNz << ")" << std::endl; int mx = GaleriList.get("mx", -1); int my = GaleriList.get("my", -1); int mz = GaleriList.get("my", -1); std::cout << " Processor subdomains in x direction: " << mx << std::endl << " Processor subdomains in y direction: " << my << std::endl; if (optMatrixType == "Laplace3D") std::cout << " Processor subdomains in z direction: " << mz << std::endl; std::cout << "========================================================" << std::endl; } // Build a linear system with trivial solution, using a random vector // as starting solution. Epetra_Vector LHS(*Map); LHS.Random(); Epetra_Vector RHS(*Map); RHS.PutScalar(0.0); Epetra_LinearProblem Problem(A, &LHS, &RHS); // As we wish to use AztecOO, we need to construct a solver object // for this problem AztecOO solver(Problem); #ifdef ML_SCALING timeVec[probBuild].value = MPI_Wtime() - timeVec[probBuild].value; #endif // =========================== begin of ML part =========================== #ifdef ML_SCALING timeVec[precBuild].value = MPI_Wtime(); #endif // create a parameter list for ML options ParameterList MLList; // Sets default parameters for classic smoothed aggregation. After this // call, MLList contains the default values for the ML parameters, // as required by typical smoothed aggregation for symmetric systems. // Other sets of parameters are available for non-symmetric systems // ("DD" and "DD-ML"), and for the Maxwell equations ("maxwell"). ML_Epetra::SetDefaults("SA",MLList); // overwrite some parameters. Please refer to the user's guide // for more information // some of the parameters do not differ from their default value, // and they are here reported for the sake of clarity // output level, 0 being silent and 10 verbose MLList.set("ML output", 10); // maximum number of levels MLList.set("max levels",optMaxLevels); // set finest level to 0 MLList.set("increasing or decreasing","increasing"); MLList.set("coarse: max size",optMaxCoarseSize); // use Uncoupled scheme to create the aggregate MLList.set("aggregation: type", "Uncoupled"); // smoother is Chebyshev. Example file // `ml/examples/TwoLevelDD/ml_2level_DD.cpp' shows how to use // AZTEC's preconditioners as smoothers MLList.set("smoother: type","Chebyshev"); MLList.set("smoother: Chebyshev alpha",optAlpha); MLList.set("smoother: sweeps",optSweeps); // use both pre and post smoothing MLList.set("smoother: pre or post", "both"); #ifdef HAVE_ML_AMESOS // solve with serial direct solver KLU MLList.set("coarse: type","Amesos-KLU"); #else // this is for testing purposes only, you should have // a direct solver for the coarse problem (either Amesos, or the SuperLU/ // SuperLU_DIST interface of ML) MLList.set("coarse: type","Jacobi"); #endif MLList.set("repartition: enable",1); MLList.set("repartition: start level",1); MLList.set("repartition: max min ratio",1.1); MLList.set("repartition: min per proc",800); MLList.set("repartition: partitioner","Zoltan"); MLList.set("repartition: put on single proc",1); MLList.set("x-coordinates", x_coord); MLList.set("y-coordinates", y_coord); if (optMatrixType == "Laplace2D") { MLList.set("repartition: Zoltan dimensions",2); } else if (optMatrixType == "Laplace3D") { MLList.set("repartition: Zoltan dimensions",3); MLList.set("z-coordinates", z_coord); } MLList.set("print hierarchy",optWriteMatrices); //MLList.set("aggregation: damping factor",0.); // Read in XML options if (xmlFile != "") ML_Epetra::ReadXML(xmlFile,MLList,Comm); // Creates the preconditioning object. We suggest to use `new' and // `delete' because the destructor contains some calls to MPI (as // required by ML and possibly Amesos). This is an issue only if the // destructor is called **after** MPI_Finalize(). ML_Epetra::MultiLevelPreconditioner* MLPrec = new ML_Epetra::MultiLevelPreconditioner(*A, MLList); // verify unused parameters on process 0 (put -1 to print on all // processes) MLPrec->PrintUnused(0); #ifdef ML_SCALING timeVec[precBuild].value = MPI_Wtime() - timeVec[precBuild].value; #endif // ML allows the user to cheaply recompute the preconditioner. You can // simply uncomment the following line: // // MLPrec->ReComputePreconditioner(); // // It is supposed that the linear system matrix has different values, but // **exactly** the same structure and layout. The code re-built the // hierarchy and re-setup the smoothers and the coarse solver using // already available information on the hierarchy. A particular // care is required to use ReComputePreconditioner() with nonzero // threshold. // =========================== end of ML part ============================= // tell AztecOO to use the ML preconditioner, specify the solver // and the output, then solve with 500 maximum iterations and 1e-12 // of tolerance (see AztecOO's user guide for more details) #ifdef ML_SCALING timeVec[solve].value = MPI_Wtime(); #endif solver.SetPrecOperator(MLPrec); solver.SetAztecOption(AZ_solver, AZ_cg); solver.SetAztecOption(AZ_output, 32); solver.Iterate(optMaxIts, optTol); #ifdef ML_SCALING timeVec[solve].value = MPI_Wtime() - timeVec[solve].value; #endif // destroy the preconditioner delete MLPrec; // compute the real residual double residual; LHS.Norm2(&residual); if( Comm.MyPID()==0 ) { cout << "||b-Ax||_2 = " << residual << endl; } // for testing purposes if (residual > 1e-5) exit(EXIT_FAILURE); delete A; delete Map; #ifdef ML_SCALING timeVec[total].value = MPI_Wtime() - timeVec[total].value; //avg double dupTime[ntimers],avgTime[ntimers]; for (int i=0; i<ntimers; i++) dupTime[i] = timeVec[i].value; MPI_Reduce(dupTime,avgTime,ntimers,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD); for (int i=0; i<ntimers; i++) avgTime[i] = avgTime[i]/Comm.NumProc(); //min MPI_Reduce(timeVec,minTime,ntimers,MPI_DOUBLE_INT,MPI_MINLOC,0,MPI_COMM_WORLD); //max MPI_Reduce(timeVec,maxTime,ntimers,MPI_DOUBLE_INT,MPI_MAXLOC,0,MPI_COMM_WORLD); if (Comm.MyPID() == 0) { printf("timing : max (pid) min (pid) avg\n"); printf("Problem build : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[probBuild].value,maxTime[probBuild].rank, minTime[probBuild].value,minTime[probBuild].rank, avgTime[probBuild]); printf("Preconditioner build : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[precBuild].value,maxTime[precBuild].rank, minTime[precBuild].value,minTime[precBuild].rank, avgTime[precBuild]); printf("Solve : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[solve].value,maxTime[solve].rank, minTime[solve].value,minTime[solve].rank, avgTime[solve]); printf("Total : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[total].value,maxTime[total].rank, minTime[total].value,minTime[total].rank, avgTime[total]); } #endif #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc, &argv); // define an Epetra communicator Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // check number of processes if (Comm.NumProc() != 1) { if (Comm.MyPID() == 0) cerr << "*ERR* can be used only with one process" << endl; #ifdef HAVE_MPI MPI_Finalize(); #endif exit(EXIT_SUCCESS); } // process 0 will read an HB matrix, and store it // in the MSR format given by the arrays bindx and val int N_global; int N_nonzeros; double * val = NULL; int * bindx = NULL; double * x = NULL, * b = NULL, * xexact = NULL; FILE* fp = fopen("../HBMatrices/fidap005.rua", "r"); if (fp == 0) { cerr << "Matrix file not available" << endl; #ifdef HAVE_MPI MPI_Finalize(); #endif exit(EXIT_SUCCESS); } fclose(fp); Trilinos_Util_read_hb("../HBMatrices/fidap005.rua", 0, &N_global, &N_nonzeros, &val, &bindx, &x, &b, &xexact); // assign all the elements to process 0 // (this code can run ONLY with one process, extensions to more // processes will require functions to handle update of ghost nodes) Epetra_Map Map(N_global,0,Comm); MSRMatrix A(Map,bindx,val); // define two vectors Epetra_Vector xxx(Map); Epetra_Vector yyy(Map); xxx.Random(); A.Apply(xxx,yyy); cout << yyy; double norm2; yyy.Norm2(&norm2); cout << norm2 << endl; // free memory allocated by Trilinos_Util_read_hb if (val != NULL) free((void*)val); if (bindx != NULL) free((void*)bindx); if (x != NULL) free((void*)x); if (b != NULL) free((void*)b); if (xexact != NULL) free((void*)xexact);; #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); } /* main */
int main(int argc, char *argv[]) { // bool haveM = false; #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif int MyPID = Comm.MyPID(); int nev = 5; int blockSize = 5; int maxIterations = 1000; double tol = 1.0e-8; bool verbose=false, locking=false, fullOrtho=true; std::string k_filename = ""; std::string m_filename = ""; std::string which = "SM"; Teuchos::CommandLineProcessor cmdp(false,true); cmdp.setOption("nev",&nev,"Number of eigenvalues to compute."); cmdp.setOption("blocksize",&blockSize,"Block size used in LOBPCG."); cmdp.setOption("maxiters",&maxIterations,"Maximum number of iterations used in LOBPCG."); cmdp.setOption("tol",&tol,"Convergence tolerance requested for computed eigenvalues."); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("locking","nolocking",&locking,"Use locking of converged eigenvalues."); cmdp.setOption("fullortho","nofullortho",&fullOrtho,"Use full orthogonalization."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SM,LM,SR,or LR)."); cmdp.setOption("K-filename",&k_filename,"Filename and path of the stiffness matrix."); cmdp.setOption("M-filename",&m_filename,"Filename and path of the mass matrix."); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } if (k_filename=="") { cout << "The matrix K must be supplied through an input file!!!" << endl; #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } if (m_filename!="") { haveM = true; } // //********************************************************************** //******************Set up the problem to be solved********************* //********************************************************************** // // *****Read in matrix from file****** // Teuchos::RCP<Epetra_Map> Map; Teuchos::RCP<Epetra_CrsMatrix> K, M; EpetraExt::readEpetraLinearSystem( k_filename, Comm, &K, &Map ); if (haveM) { EpetraExt::readEpetraLinearSystem( m_filename, Comm, &M, &Map ); } // //************************************ // Start the block Davidson iteration //*********************************** // // Variables used for the LOBPCG Method // // Set verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (verbose) { verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; } // // Create parameter list to pass into solver // Teuchos::ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Iterations", maxIterations ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Use Locking", locking ); MyPL.set( "Locking Tolerance", tol/10 ); MyPL.set( "Full Ortho", fullOrtho ); typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Anasazi::MultiVecTraits<double, MV> MVT; typedef Anasazi::OperatorTraits<double, MV, OP> OPT; // // Create the eigenproblem to be solved. // Teuchos::RCP<Epetra_MultiVector> ivec = Teuchos::rcp( new Epetra_MultiVector(*Map, blockSize) ); ivec->Random(); Teuchos::RCP<Anasazi::BasicEigenproblem<double, MV, OP> > MyProblem; if (haveM) { MyProblem = Teuchos::rcp( new Anasazi::BasicEigenproblem<double, MV, OP>( K, M, ivec ) ); } else { MyProblem = Teuchos::rcp( new Anasazi::BasicEigenproblem<double, MV, OP>( K, ivec ) ); } // Inform the eigenproblem that (K,M) is Hermitian MyProblem->setHermitian(true); // Set the number of eigenvalues requested MyProblem->setNEV( nev ); // Inform the eigenproblem that you are finished passing it information bool boolret = MyProblem->setProblem(); if (boolret != true) { if (verbose && MyPID == 0) { cout << "Anasazi::BasicEigenproblem::setProblem() returned with error." << endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Initialize the LOBPCG solver Anasazi::LOBPCGSolMgr<double, MV, OP> MySolverMgr(MyProblem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr.solve(); if (returnCode != Anasazi::Converged && MyPID==0 && verbose) { cout << "Anasazi::EigensolverMgr::solve() returned unconverged." << endl; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<double,MV> sol = MyProblem->getSolution(); std::vector<Anasazi::Value<double> > evals = sol.Evals; Teuchos::RCP<MV> evecs = sol.Evecs; std::vector<int> index = sol.index; int numev = sol.numVecs; if (numev > 0) { // Compute residuals. Teuchos::LAPACK<int,double> lapack; std::vector<double> normEV(numev); // Get storage Teuchos::RCP<Epetra_MultiVector> Kevecs, Mevecs; Teuchos::SerialDenseMatrix<int,double> B(numev,numev); B.putScalar(0.0); for (int i=0; i<numev; i++) {B(i,i) = evals[i].realpart;} // Compute K*evecs Kevecs = Teuchos::rcp(new Epetra_MultiVector(*Map,numev) ); OPT::Apply( *K, *evecs, *Kevecs ); // Compute M*evecs if (haveM) { Mevecs = Teuchos::rcp(new Epetra_MultiVector(*Map,numev) ); OPT::Apply( *M, *evecs, *Mevecs ); } else { Mevecs = evecs; } // Compute K*evecs - lambda*M*evecs and its norm MVT::MvTimesMatAddMv( -1.0, *Mevecs, B, 1.0, *Kevecs ); MVT::MvNorm( *Kevecs, normEV ); // Scale the norms by the eigenvalue for (int i=0; i<numev; i++) { normEV[i] /= Teuchos::ScalarTraits<double>::magnitude( evals[i].realpart ); } // Output computed eigenvalues and their direct residuals if (verbose && MyPID==0) { cout.setf(std::ios_base::right, std::ios_base::adjustfield); cout<<endl<< "Actual Residuals"<<endl; cout<< std::setw(16) << "Real Part" << std::setw(20) << "Direct Residual"<< endl; cout<<"-----------------------------------------------------------"<<endl; for (int i=0; i<numev; i++) { cout<< std::setw(16) << evals[i].realpart << std::setw(20) << normEV[i] << endl; } cout<<"-----------------------------------------------------------"<<endl; } } #ifdef EPETRA_MPI MPI_Finalize() ; #endif return 0; } // end LOBPCGEpetraExFile.cpp
int main(int argc, char *argv[]) { #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif bool testFailed; bool boolret; int MyPID = Comm.MyPID(); bool verbose = true; bool debug = false; std::string which("SM"); Teuchos::CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SM,LM,SR,LR,SI,or LI)."); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } typedef double ScalarType; typedef Teuchos::ScalarTraits<ScalarType> ScalarTypeTraits; typedef ScalarTypeTraits::magnitudeType MagnitudeType; typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Anasazi::MultiVecTraits<ScalarType,MV> MVTraits; typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OpTraits; // Dimension of the matrix int nx = 10; // Discretization points in any one direction. int NumGlobalElements = nx*nx; // Size of matrix nx*nx // Construct a Map that puts approximately the same number of // equations on each processor. Epetra_Map Map(NumGlobalElements, 0, Comm); // Get update list and number of local equations from newly created Map. int NumMyElements = Map.NumMyElements(); std::vector<int> MyGlobalElements(NumMyElements); Map.MyGlobalElements(&MyGlobalElements[0]); // Create an integer vector NumNz that is used to build the Petra Matrix. // NumNz[i] is the Number of OFF-DIAGONAL term for the ith global equation // on this processor std::vector<int> NumNz(NumMyElements); /* We are building a matrix of block structure: | T -I | |-I T -I | | -I T | | ... -I| | -I T| where each block is dimension nx by nx and the matrix is on the order of nx*nx. The block T is a tridiagonal matrix. */ for (int i=0; i<NumMyElements; i++) { if (MyGlobalElements[i] == 0 || MyGlobalElements[i] == NumGlobalElements-1 || MyGlobalElements[i] == nx-1 || MyGlobalElements[i] == nx*(nx-1) ) { NumNz[i] = 3; } else if (MyGlobalElements[i] < nx || MyGlobalElements[i] > nx*(nx-1) || MyGlobalElements[i]%nx == 0 || (MyGlobalElements[i]+1)%nx == 0) { NumNz[i] = 4; } else { NumNz[i] = 5; } } // Create an Epetra_Matrix Teuchos::RCP<Epetra_CrsMatrix> A = Teuchos::rcp( new Epetra_CrsMatrix(Copy, Map, &NumNz[0]) ); // Diffusion coefficient, can be set by user. // When rho*h/2 <= 1, the discrete convection-diffusion operator has real eigenvalues. // When rho*h/2 > 1, the operator has complex eigenvalues. double rho = 2*(nx+1); // Compute coefficients for discrete convection-diffution operator const double one = 1.0; std::vector<double> Values(4); std::vector<int> Indices(4); double h = one /(nx+1); double h2 = h*h; double c = 5.0e-01*rho/ h; Values[0] = -one/h2 - c; Values[1] = -one/h2 + c; Values[2] = -one/h2; Values[3]= -one/h2; double diag = 4.0 / h2; int NumEntries, info; for (int i=0; i<NumMyElements; i++) { if (MyGlobalElements[i]==0) { Indices[0] = 1; Indices[1] = nx; NumEntries = 2; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[1], &Indices[0]); assert( info==0 ); } else if (MyGlobalElements[i] == nx*(nx-1)) { Indices[0] = nx*(nx-1)+1; Indices[1] = nx*(nx-2); NumEntries = 2; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[1], &Indices[0]); assert( info==0 ); } else if (MyGlobalElements[i] == nx-1) { Indices[0] = nx-2; NumEntries = 1; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[0], &Indices[0]); assert( info==0 ); Indices[0] = 2*nx-1; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[2], &Indices[0]); assert( info==0 ); } else if (MyGlobalElements[i] == NumGlobalElements-1) { Indices[0] = NumGlobalElements-2; NumEntries = 1; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[0], &Indices[0]); assert( info==0 ); Indices[0] = nx*(nx-1)-1; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[2], &Indices[0]); assert( info==0 ); } else if (MyGlobalElements[i] < nx) { Indices[0] = MyGlobalElements[i]-1; Indices[1] = MyGlobalElements[i]+1; Indices[2] = MyGlobalElements[i]+nx; NumEntries = 3; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[0], &Indices[0]); assert( info==0 ); } else if (MyGlobalElements[i] > nx*(nx-1)) { Indices[0] = MyGlobalElements[i]-1; Indices[1] = MyGlobalElements[i]+1; Indices[2] = MyGlobalElements[i]-nx; NumEntries = 3; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[0], &Indices[0]); assert( info==0 ); } else if (MyGlobalElements[i]%nx == 0) { Indices[0] = MyGlobalElements[i]+1; Indices[1] = MyGlobalElements[i]-nx; Indices[2] = MyGlobalElements[i]+nx; NumEntries = 3; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[1], &Indices[0]); assert( info==0 ); } else if ((MyGlobalElements[i]+1)%nx == 0) { Indices[0] = MyGlobalElements[i]-nx; Indices[1] = MyGlobalElements[i]+nx; NumEntries = 2; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[2], &Indices[0]); assert( info==0 ); Indices[0] = MyGlobalElements[i]-1; NumEntries = 1; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[0], &Indices[0]); assert( info==0 ); } else { Indices[0] = MyGlobalElements[i]-1; Indices[1] = MyGlobalElements[i]+1; Indices[2] = MyGlobalElements[i]-nx; Indices[3] = MyGlobalElements[i]+nx; NumEntries = 4; info = A->InsertGlobalValues(MyGlobalElements[i], NumEntries, &Values[0], &Indices[0]); assert( info==0 ); } // Put in the diagonal entry info = A->InsertGlobalValues(MyGlobalElements[i], 1, &diag, &MyGlobalElements[i]); assert( info==0 ); } // Finish up info = A->FillComplete(); assert( info==0 ); A->SetTracebackMode(1); // Shutdown Epetra Warning tracebacks //************************************ // Start the block Davidson iteration //*********************************** // // Variables used for the Generalized Davidson Method // int nev = 4; int blockSize = 1; int maxDim = 50; int restartDim = 10; int maxRestarts = 500; double tol = 1e-10; // Set verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (verbose) { verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; } if (debug) { verbosity += Anasazi::Debug; } // // Create parameter list to pass into solver manager // Teuchos::ParameterList MyPL; MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Subspace Dimension", maxDim); MyPL.set( "Restart Dimension", restartDim); MyPL.set( "Maximum Restarts", maxRestarts ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Relative Convergence Tolerance", true ); MyPL.set( "Initial Guess", "User" ); // Create an Epetra_MultiVector for an initial vector to start the solver. // Note: This needs to have the same number of columns as the blocksize. Teuchos::RCP<Epetra_MultiVector> ivec = Teuchos::rcp( new Epetra_MultiVector(Map, blockSize) ); ivec->Random(); // Create the eigenproblem. Teuchos::RCP<Anasazi::BasicEigenproblem<double, MV, OP> > MyProblem = Teuchos::rcp( new Anasazi::BasicEigenproblem<double,MV,OP>() ); MyProblem->setA(A); MyProblem->setInitVec(ivec); // Inform the eigenproblem that the operator A is non-Hermitian MyProblem->setHermitian(false); // Set the number of eigenvalues requested MyProblem->setNEV( nev ); // Inform the eigenproblem that you are finishing passing it information boolret = MyProblem->setProblem(); if (boolret != true) { if (verbose && MyPID == 0) { std::cout << "Anasazi::BasicEigenproblem::setProblem() returned with error." << std::endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Initialize the Block Arnoldi solver Anasazi::GeneralizedDavidsonSolMgr<double, MV, OP> MySolverMgr(MyProblem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMgr.solve(); testFailed = false; if (returnCode != Anasazi::Converged && MyPID==0 && verbose) { testFailed = true; } // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<ScalarType,MV> sol = MyProblem->getSolution(); std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals; Teuchos::RCP<MV> evecs = sol.Evecs; std::vector<int> index = sol.index; int numev = sol.numVecs; // Output computed eigenvalues and their direct residuals if (verbose && MyPID==0) { int numritz = (int)evals.size(); std::cout.setf(std::ios_base::right, std::ios_base::adjustfield); std::cout<<std::endl<< "Computed Ritz Values"<< std::endl; std::cout<< std::setw(16) << "Real Part" << std::setw(16) << "Imag Part" << std::endl; std::cout<<"-----------------------------------------------------------"<<std::endl; for (int i=0; i<numritz; i++) { std::cout<< std::setw(16) << evals[i].realpart << std::setw(16) << evals[i].imagpart << std::endl; } std::cout<<"-----------------------------------------------------------"<<std::endl; } if (numev > 0) { // Compute residuals. Teuchos::LAPACK<int,double> lapack; std::vector<double> normA(numev); // The problem is non-Hermitian. int i=0; std::vector<int> curind(1); std::vector<double> resnorm(1), tempnrm(1); Teuchos::RCP<MV> tempAevec; Teuchos::RCP<const MV> evecr, eveci; Epetra_MultiVector Aevec(Map,numev); // Compute A*evecs OpTraits::Apply( *A, *evecs, Aevec ); Teuchos::SerialDenseMatrix<int,double> Breal(1,1), Bimag(1,1); while (i<numev) { if (index[i]==0) { // Get a view of the current eigenvector (evecr) curind[0] = i; evecr = MVTraits::CloneView( *evecs, curind ); // Get a copy of A*evecr tempAevec = MVTraits::CloneCopy( Aevec, curind ); // Compute A*evecr - lambda*evecr Breal(0,0) = evals[i].realpart; MVTraits::MvTimesMatAddMv( -1.0, *evecr, Breal, 1.0, *tempAevec ); // Compute the norm of the residual and increment counter MVTraits::MvNorm( *tempAevec, resnorm ); normA[i] = resnorm[0] / Teuchos::ScalarTraits<MagnitudeType>::magnitude( evals[i].realpart ); i++; } else { // Get a view of the real part of the eigenvector (evecr) curind[0] = i; evecr = MVTraits::CloneView( *evecs, curind ); // Get a copy of A*evecr tempAevec = MVTraits::CloneCopy( Aevec, curind ); // Get a view of the imaginary part of the eigenvector (eveci) curind[0] = i+1; eveci = MVTraits::CloneView( *evecs, curind ); // Set the eigenvalue into Breal and Bimag Breal(0,0) = evals[i].realpart; Bimag(0,0) = evals[i].imagpart; // Compute A*evecr - evecr*lambdar + eveci*lambdai MVTraits::MvTimesMatAddMv( -1.0, *evecr, Breal, 1.0, *tempAevec ); MVTraits::MvTimesMatAddMv( 1.0, *eveci, Bimag, 1.0, *tempAevec ); MVTraits::MvNorm( *tempAevec, tempnrm ); // Get a copy of A*eveci tempAevec = MVTraits::CloneCopy( Aevec, curind ); // Compute A*eveci - eveci*lambdar - evecr*lambdai MVTraits::MvTimesMatAddMv( -1.0, *evecr, Bimag, 1.0, *tempAevec ); MVTraits::MvTimesMatAddMv( -1.0, *eveci, Breal, 1.0, *tempAevec ); MVTraits::MvNorm( *tempAevec, resnorm ); // Compute the norms and scale by magnitude of eigenvalue normA[i] = lapack.LAPY2( tempnrm[0], resnorm[0] ) / lapack.LAPY2( evals[i].realpart, evals[i].imagpart ); normA[i+1] = normA[i]; i=i+2; } } // Output computed eigenvalues and their direct residuals if (verbose && MyPID==0) { std::cout.setf(std::ios_base::right, std::ios_base::adjustfield); std::cout<<std::endl<< "Actual Residuals"<<std::endl; std::cout<< std::setw(16) << "Real Part" << std::setw(16) << "Imag Part" << std::setw(20) << "Direct Residual"<< std::endl; std::cout<<"-----------------------------------------------------------"<<std::endl; for (int j=0; j<numev; j++) { std::cout<< std::setw(16) << evals[j].realpart << std::setw(16) << evals[j].imagpart << std::setw(20) << normA[j] << std::endl; if ( normA[j] > tol ) { testFailed = true; } } std::cout<<"-----------------------------------------------------------"<<std::endl; } } #ifdef EPETRA_MPI MPI_Finalize(); #endif if (testFailed) { if (verbose && MyPID==0) { std::cout << "End Result: TEST FAILED" << std::endl; } return -1; } // // Default return value // if (verbose && MyPID==0) { std::cout << "End Result: TEST PASSED" << std::endl; } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif #define ML_SCALING #ifdef ML_SCALING const int ntimers=4; enum {total, probBuild, precBuild, solve}; ml_DblLoc timeVec[ntimers], maxTime[ntimers], minTime[ntimers]; for (int i=0; i<ntimers; i++) timeVec[i].rank = Comm.MyPID(); timeVec[total].value = MPI_Wtime(); #endif int nx; if (argc > 1) nx = (int) strtol(argv[1],NULL,10); else nx = 256; if (nx < 1) nx = 256; // input a nonpositive integer if you want to specify // the XML input file name. nx = nx*(int)sqrt((double)Comm.NumProc()); int ny = nx; printf("nx = %d\nny = %d\n",nx,ny); fflush(stdout); char xmlFile[80]; bool readXML=false; if (argc > 2) {strcpy(xmlFile,argv[2]); readXML = true;} else sprintf(xmlFile,"%s","params.xml"); ParameterList GaleriList; GaleriList.set("nx", nx); GaleriList.set("ny", ny); #ifdef ML_SCALING timeVec[probBuild].value = MPI_Wtime(); #endif Epetra_Map* Map = CreateMap("Cartesian2D", Comm, GaleriList); Epetra_CrsMatrix* A = CreateCrsMatrix("Laplace2D", Map, GaleriList); if (!Comm.MyPID()) printf("nx = %d, ny = %d, mx = %d, my = %d\n",nx,ny,GaleriList.get("mx",-1),GaleriList.get("my",-1)); fflush(stdout); //avoid potential overflow double numMyRows = A->NumMyRows(); double numGlobalRows; Comm.SumAll(&numMyRows,&numGlobalRows,1); if (!Comm.MyPID()) printf("# global rows = %1.0f\n",numGlobalRows); //printf("pid %d: #rows = %d\n",Comm.MyPID(),A->NumMyRows()); fflush(stdout); Epetra_MultiVector *coords = CreateCartesianCoordinates("2D", Map,GaleriList); double *x_coord=0,*y_coord=0,*z_coord=0; double **ttt; if (!coords->ExtractView(&ttt)) { x_coord = ttt[0]; y_coord = ttt[1]; } else { if (!Comm.MyPID()) printf("Error extracting coordinate vectors\n"); MPI_Finalize(); exit(EXIT_FAILURE); } Epetra_Vector LHS(*Map); LHS.Random(); Epetra_Vector RHS(*Map); RHS.PutScalar(0.0); Epetra_LinearProblem Problem(A, &LHS, &RHS); AztecOO solver(Problem); #ifdef ML_SCALING timeVec[probBuild].value = MPI_Wtime() - timeVec[probBuild].value; #endif // =========================== begin of ML part =========================== #ifdef ML_SCALING timeVec[precBuild].value = MPI_Wtime(); #endif ParameterList MLList; if (readXML) { MLList.set("read XML",true); MLList.set("XML input file",xmlFile); } else { cout << "here" << endl; ML_Epetra::SetDefaults("SA",MLList); MLList.set("smoother: type","Chebyshev"); MLList.set("smoother: sweeps",3); MLList.set("coarse: max size",1); } MLList.set("x-coordinates", x_coord); MLList.set("y-coordinates", y_coord); MLList.set("z-coordinates", z_coord); /* RCP<std::vector<int> > m_smootherAztecOptions = rcp(new std::vector<int>(AZ_OPTIONS_SIZE)); RCP<std::vector<double> > m_smootherAztecParams = rcp(new std::vector<double>(AZ_PARAMS_SIZE)); //int m_smootherAztecOptions[AZ_OPTIONS_SIZE]; //double m_smootherAztecParams[AZ_PARAMS_SIZE]; std::string smootherType("Aztec"); AZ_defaults(&(*m_smootherAztecOptions)[0],&(*m_smootherAztecParams)[0]); (*m_smootherAztecOptions)[AZ_precond] = AZ_dom_decomp; (*m_smootherAztecOptions)[AZ_subdomain_solve] = AZ_icc; bool smootherAztecAsSolver = true; */ MLList.set("ML output",10); MLList.set("repartition: enable",1); MLList.set("repartition: max min ratio",1.3); MLList.set("repartition: min per proc",200); MLList.set("repartition: partitioner","Zoltan"); MLList.set("repartition: Zoltan dimensions",2); MLList.set("repartition: put on single proc",1); MLList.set("repartition: Zoltan type","hypergraph"); MLList.set("repartition: estimated iterations",13); /* MLList.set("smoother: Aztec options",m_smootherAztecOptions); MLList.set("smoother: Aztec params",m_smootherAztecParams); MLList.set("smoother: type",smootherType.c_str()); MLList.set("smoother: Aztec as solver", smootherAztecAsSolver); MLList.set("ML print initial list", 0); MLList.set("ML print final list", 0); */ ML_Epetra::MultiLevelPreconditioner* MLPrec = new ML_Epetra::MultiLevelPreconditioner(*A, MLList); // verify unused parameters on process 0 (put -1 to print on all // processes) MLPrec->PrintUnused(0); #ifdef ML_SCALING timeVec[precBuild].value = MPI_Wtime() - timeVec[precBuild].value; #endif // =========================== end of ML part ============================= #ifdef ML_SCALING timeVec[solve].value = MPI_Wtime(); #endif solver.SetPrecOperator(MLPrec); solver.SetAztecOption(AZ_solver, AZ_cg); solver.SetAztecOption(AZ_output, 1); solver.Iterate(500, 1e-12); #ifdef ML_SCALING timeVec[solve].value = MPI_Wtime() - timeVec[solve].value; #endif // destroy the preconditioner delete MLPrec; // compute the real residual double residual; LHS.Norm2(&residual); if( Comm.MyPID()==0 ) { cout << "||b-Ax||_2 = " << residual << endl; } // for testing purposes if (residual > 1e-5) exit(EXIT_FAILURE); delete A; delete Map; delete coords; #ifdef ML_SCALING timeVec[total].value = MPI_Wtime() - timeVec[total].value; //avg double dupTime[ntimers],avgTime[ntimers]; for (int i=0; i<ntimers; i++) dupTime[i] = timeVec[i].value; MPI_Reduce(dupTime,avgTime,ntimers,MPI_DOUBLE,MPI_SUM,0,MPI_COMM_WORLD); for (int i=0; i<ntimers; i++) avgTime[i] = avgTime[i]/Comm.NumProc(); //min MPI_Reduce(timeVec,minTime,ntimers,MPI_DOUBLE_INT,MPI_MINLOC,0,MPI_COMM_WORLD); //max MPI_Reduce(timeVec,maxTime,ntimers,MPI_DOUBLE_INT,MPI_MAXLOC,0,MPI_COMM_WORLD); if (Comm.MyPID() == 0) { printf("timing : max (pid) min (pid) avg\n"); printf("Problem build : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[probBuild].value,maxTime[probBuild].rank, minTime[probBuild].value,minTime[probBuild].rank, avgTime[probBuild]); printf("Preconditioner build : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[precBuild].value,maxTime[precBuild].rank, minTime[precBuild].value,minTime[precBuild].rank, avgTime[precBuild]); printf("Solve : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[solve].value,maxTime[solve].rank, minTime[solve].value,minTime[solve].rank, avgTime[solve]); printf("Total : %2.3e (%d) %2.3e (%d) %2.3e \n", maxTime[total].value,maxTime[total].rank, minTime[total].value,minTime[total].rank, avgTime[total]); } #endif #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); }
int main(int argc, char *argv[]) { int pid = -1; Teuchos::GlobalMPISession session(&argc, &argv, NULL); #ifdef EPETRA_MPI Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif bool verbose = false; bool success = true; try { pid = Comm.MyPID(); int n(10); int numRHS=1; Epetra_Map Map = Epetra_Map(n, 0, Comm); Epetra_MultiVector X(Map, numRHS, false), Y(Map, numRHS, false); X.PutScalar( 1.0 ); // Inner computes inv(D2)*y Teuchos::RCP<Diagonal_Operator_2> D2 = Teuchos::rcp(new Diagonal_Operator_2(n, 1.0)); Iterative_Inverse_Operator A2(n, 1, D2, "Belos (inv(D2))", true); // should return x=(1, 1/2, 1/3, ..., 1/10) A2(X,Y); if (pid==0) { std::cout << "Vector Y should have all entries [1, 1/2, 1/3, ..., 1/10]" << std::endl; } Y.Print(std::cout); // Inner computes inv(D)*x Teuchos::RCP<Diagonal_Operator> D = Teuchos::rcp(new Diagonal_Operator(n, 4.0)); Teuchos::RCP<Iterative_Inverse_Operator> Inner = Teuchos::rcp(new Iterative_Inverse_Operator(n, 1, D, "Belos (inv(D))", false)); // Composed_Operator computed inv(D)*B*x Teuchos::RCP<Diagonal_Operator> B = Teuchos::rcp(new Diagonal_Operator(n, 4.0)); Teuchos::RCP<Composed_Operator> C = Teuchos::rcp(new Composed_Operator(n, Inner, B)); // Outer computes inv(C) = inv(inv(D)*B)*x = inv(B)*D*x = x Teuchos::RCP<Iterative_Inverse_Operator> Outer = Teuchos::rcp(new Iterative_Inverse_Operator(n, 1, C, "Belos (inv(C)=inv(inv(D)*B))", true)); // should return x=1/4 (*Inner)(X,Y); if (pid==0) { std::cout << std::endl << "Vector Y should have all entries [1/4, 1/4, 1/4, ..., 1/4]" << std::endl; } Y.Print(std::cout); // should return x=1 (*Outer)(X,Y); if (pid==0) { std::cout << "Vector Y should have all entries [1, 1, 1, ..., 1]" << std::endl; } Y.Print(std::cout); // Compute the norm of Y - 1.0 std::vector<double> norm_Y(Y.NumVectors()); Y.Update(-1.0, X, 1.0); Y.Norm2(&norm_Y[0]); if (pid==0) std::cout << "Two-norm of std::vector (Y-1.0) : "<< norm_Y[0] << std::endl; success = (norm_Y[0] < 1e-10 && !Teuchos::ScalarTraits<double>::isnaninf( norm_Y[0] ) ); if (success) { if (pid==0) std::cout << "End Result: TEST PASSED" << std::endl; } else { if (pid==0) std::cout << "End Result: TEST FAILED" << std::endl; } } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose,std::cerr,success); return success ? EXIT_SUCCESS : EXIT_FAILURE; }
int main(int argc, char *argv[]) { int *update; /* vector elements updated on this node. */ int *bindx; double *val; double *xguess, *b, *xexact; int n_nonzeros; int N_update; /* # of block unknowns updated on this node */ int numLocalEquations; /* Number scalar equations on this node */ int numGlobalEquations; /* Total number of equations */ int *numNz, *ColInds; int row, *col_inds, numEntries; double *row_vals; int i; #ifdef EPETRA_MPI MPI_Init(&argc,&argv); Epetra_MpiComm comm(MPI_COMM_WORLD); #else Epetra_SerialComm comm; #endif cout << comm << endl; if(argc != 2) cerr << "error: enter name of data file on command line" << endl; /* Set exact solution to NULL */ xexact = NULL; /* Read matrix file and distribute among processors. Returns with this processor's set of rows */ Trilinos_Util_read_hb(argv[1], comm.MyPID(), &numGlobalEquations, &n_nonzeros, &val, &bindx, &xguess, &b, &xexact); Trilinos_Util_distrib_msr_matrix(comm, &numGlobalEquations, &n_nonzeros, &N_update, &update, &val, &bindx, &xguess, &b, &xexact); numLocalEquations = N_update; /* Make numNzBlks - number of block entries in each block row */ numNz = new int[numLocalEquations]; for (i=0; i<numLocalEquations; i++) numNz[i] = bindx[i+1] - bindx[i] + 1; /* Make ColInds - Exactly bindx, offset by diag (just copy pointer) */ ColInds = bindx+numLocalEquations+1; Epetra_Map map(numGlobalEquations, numLocalEquations, update, 0, comm); Epetra_CrsMatrix A(Copy, map, numNz); /* Add rows one-at-a-time */ for (row=0; row<numLocalEquations; row++) { row_vals = val + bindx[row]; col_inds = bindx + bindx[row]; numEntries = bindx[row+1] - bindx[row]; assert(A.InsertGlobalValues(update[row], numEntries, row_vals, col_inds)==0); assert(A.InsertGlobalValues(update[row], 1, val+row, update+row)==0); } assert(A.FillComplete()==0); Epetra_Vector xx(Copy, map, xexact); Epetra_Vector bb(Copy, map, b); // Construct a Petra Linear Problem Epetra_Vector x(map); Epetra_LinearProblem problem(&A, &x, &bb); // Construct a solver object for this problem AztecOO solver(problem); // Assert symmetric // problem->AssertSymmetric(); // Set Problem Difficulty Level //problem->SetPDL(easy); //solver.SetAztecOption(AZ_precond, AZ_none); solver.SetAztecOption(AZ_precond, AZ_dom_decomp); //solver.SetAztecOption(AZ_precond, AZ_ls); //solver.SetAztecOption(AZ_scaling, 8); solver.SetAztecOption(AZ_subdomain_solve, AZ_ilut); //solver.SetAztecOption(AZ_subdomain_solve, AZ_bilu_ifp); bool bilu = false; //solver.SetAztecOption(AZ_output, 0); //solver.SetAztecOption(AZ_graph_fill, 2); solver.SetAztecOption(AZ_overlap, 0); //solver.SetAztecOption(AZ_reorder, 0); //solver.SetAztecOption(AZ_poly_ord, 9); solver.SetAztecParam(AZ_ilut_fill, 1.0); solver.SetAztecParam(AZ_drop, 0.0); //double rthresh = 1.01; //cout << "Rel threshold = " << rthresh << endl; //solver.SetAztecParam(AZ_rthresh, rthresh); //double athresh = 1.0e-2; //cout << "Abs threshold = " << athresh << endl; //solver.SetAztecParam(AZ_athresh, athresh); //solver.SetAztecOption(AZ_/conv, AZ_noscaled); //solver.SetAztecParam(AZ_ill_cond_thresh, 1.0e12); int Niters = 400; solver.SetAztecOption(AZ_kspace, Niters); double norminf = A.NormInf(); double normone = A.NormOne(); if (comm.MyPID()==0) cout << "\n Inf-norm of A before scaling = " << norminf << "\n One-norm of A before scaling = " << normone<< endl << endl; if (bilu) { int NumTrials = 3; double athresholds[] = {0.0, 1.0E-14, 1.0E-3}; double rthresholds[] = {0.0, 1.0E-14, 1.0E-3}; double condestThreshold = 1.0E16; double maxFill = 4.0; int maxKspace = 4*Niters; solver.SetAdaptiveParams(NumTrials, athresholds, rthresholds, condestThreshold, maxFill, maxKspace); } else { int NumTrials = 7; double athresholds[] = {0.0, 1.0E-12, 1.0E-12, 1.0E-5, 1.0E-5, 1.0E-2, 1.0E-2}; double rthresholds[] = {1.0, 1.0, 1.01, 1.0, 1.01, 1.01, 1.1 }; double condestThreshold = 1.0E16; double maxFill = 4.0; int maxKspace = 4*Niters; solver.SetAdaptiveParams(NumTrials, athresholds, rthresholds, condestThreshold, maxFill, maxKspace); } Epetra_Time timer(comm); solver.AdaptiveIterate(Niters, 1, 5.0e-7); double atime = timer.ElapsedTime(); if (comm.MyPID()==0) cout << "AdaptiveIterate total time = " << atime << endl; norminf = A.NormInf(); normone = A.NormOne(); if (comm.MyPID()==0) cout << "\n Inf-norm of A after scaling = " << norminf << "\n One-norm of A after scaling = " << normone << endl << endl; Epetra_Vector bcomp(map); assert(A.Multiply(false, x, bcomp)==0); Epetra_Vector resid(map); assert(resid.Update(1.0, bb, -1.0, bcomp, 0.0)==0); double residual; assert(resid.Norm2(&residual)==0); if (comm.MyPID()==0) cout << "Residual = " << residual << endl; assert(resid.Update(1.0, xx, -1.0, x, 0.0)==0); assert(resid.Norm2(&residual)==0); if (comm.MyPID()==0) cout << "2-norm of difference between computed and exact solution = " << residual << endl; if (residual>1.0e-5) { cout << "Difference between computed and exact solution is large..." << endl << "Computing norm of A times this difference. If this norm is small, then matrix is singular" << endl; assert(A.Multiply(false, resid, bcomp)==0); assert(bcomp.Norm2(&residual)==0); if (comm.MyPID()==0) cout << "2-norm of A times difference between computed and exact solution = " << residual << endl; } free ((void *) xguess); free ((void *) b); free ((void *) xexact); free ((void *) val); free ((void *) bindx); free ((void *) update); delete [] numNz; #ifdef EPETRA_MPI MPI_Finalize() ; #endif return 0 ; }
int main(int argc, char *argv[]) { // Initialize MPI #ifdef HAVE_MPI MPI_Init(&argc,&argv); #endif // Create a communicator for Epetra objects #ifdef HAVE_MPI Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif int * testInt = new int[100]; delete [] testInt; bool verbose = false; if (argc > 1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; // Get the process ID and the total number of processors int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); // Set up theolver options parameter list Teuchos::RCP<Teuchos::ParameterList> noxParamsPtr = Teuchos::rcp(new Teuchos::ParameterList); Teuchos::ParameterList & noxParams = *(noxParamsPtr.get()); // Set up the printing utilities // Only print output if the "-v" flag is set on the command line Teuchos::ParameterList& printParams = noxParams.sublist("Printing"); printParams.set("MyPID", MyPID); printParams.set("Output Precision", 5); printParams.set("Output Processor", 0); if( verbose ) printParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::Parameters + NOX::Utils::Details + NOX::Utils::Warning + NOX::Utils::TestDetails); else printParams.set("Output Information", NOX::Utils::Error + NOX::Utils::TestDetails); Teuchos::RCP<NOX::Utils> printing = Teuchos::rcp( new NOX::Utils(printParams) ); // Identify the test problem if (printing->isPrintType(NOX::Utils::TestDetails)) printing->out() << "Starting epetra/NOX_Operators/NOX_BroydenOp.exe" << endl; // Identify processor information #ifdef HAVE_MPI if (printing->isPrintType(NOX::Utils::TestDetails)) { printing->out() << "Parallel Run" << endl; printing->out() << "Number of processors = " << NumProc << endl; printing->out() << "Print Process = " << MyPID << endl; } Comm.Barrier(); if (printing->isPrintType(NOX::Utils::TestDetails)) printing->out() << "Process " << MyPID << " is alive!" << endl; Comm.Barrier(); #else if (printing->isPrintType(NOX::Utils::TestDetails)) printing->out() << "Serial Run" << endl; #endif int status = 0; // Create a TestCompare class NOX::Epetra::TestCompare tester( printing->out(), *printing); double abstol = 1.e-4; double reltol = 1.e-4 ; // Test NOX::Epetra::BroydenOperator int numGlobalElems = 3 * NumProc; Epetra_Map broydenRowMap ( numGlobalElems, 0, Comm ); Epetra_Vector broydenWorkVec ( broydenRowMap ); Epetra_CrsGraph broydenWorkGraph( Copy, broydenRowMap, 0 ); vector<int> globalIndices(3); for( int lcol = 0; lcol < 3; ++lcol ) globalIndices[lcol] = 3 * MyPID + lcol; vector<int> myGlobalIndices(2); // Row 1 structure myGlobalIndices[0] = globalIndices[0]; myGlobalIndices[1] = globalIndices[2]; broydenWorkGraph.InsertGlobalIndices( globalIndices[0], 2, &myGlobalIndices[0] ); // Row 2 structure myGlobalIndices[0] = globalIndices[0]; myGlobalIndices[1] = globalIndices[1]; broydenWorkGraph.InsertGlobalIndices( globalIndices[1], 2, &myGlobalIndices[0] ); // Row 3 structure myGlobalIndices[0] = globalIndices[1]; myGlobalIndices[1] = globalIndices[2]; broydenWorkGraph.InsertGlobalIndices( globalIndices[2], 2, &myGlobalIndices[0] ); broydenWorkGraph.FillComplete(); Teuchos::RCP<Epetra_CrsMatrix> broydenWorkMatrix = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph ) ); // Create an identity matrix broydenWorkVec.PutScalar(1.0); broydenWorkMatrix->ReplaceDiagonalValues(broydenWorkVec); NOX::Epetra::BroydenOperator broydenOp( noxParams, printing, broydenWorkVec, broydenWorkMatrix, true ); broydenWorkVec[0] = 1.0; broydenWorkVec[1] = -1.0; broydenWorkVec[2] = 2.0; broydenOp.setStepVector( broydenWorkVec ); broydenWorkVec[0] = 2.0; broydenWorkVec[1] = 1.0; broydenWorkVec[2] = 3.0; broydenOp.setYieldVector( broydenWorkVec ); broydenOp.computeSparseBroydenUpdate(); // Create the gold matrix for comparison Teuchos::RCP<Epetra_CrsMatrix> goldMatrix = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph ) ); int numCols ; double * values ; // Row 1 answers goldMatrix->ExtractMyRowView( 0, numCols, values ); values[0] = 6.0 ; values[1] = 2.0 ; // Row 2 answers goldMatrix->ExtractMyRowView( 1, numCols, values ); values[0] = 5.0 ; values[1] = 0.0 ; // Row 3 structure goldMatrix->ExtractMyRowView( 2, numCols, values ); values[0] = -1.0 ; values[1] = 7.0 ; goldMatrix->Scale(0.2); status += tester.testCrsMatrices( broydenOp.getBroydenMatrix(), *goldMatrix, reltol, abstol, "Broyden Sparse Operator Update Test" ); // Now try a dense Broyden Update Epetra_CrsGraph broydenWorkGraph2( Copy, broydenRowMap, 0 ); myGlobalIndices.resize(3); // All Rowsstructure myGlobalIndices[0] = globalIndices[0]; myGlobalIndices[1] = globalIndices[1]; myGlobalIndices[2] = globalIndices[2]; broydenWorkGraph2.InsertGlobalIndices( globalIndices[0], 3, &myGlobalIndices[0] ); broydenWorkGraph2.InsertGlobalIndices( globalIndices[1], 3, &myGlobalIndices[0] ); broydenWorkGraph2.InsertGlobalIndices( globalIndices[2], 3, &myGlobalIndices[0] ); broydenWorkGraph2.FillComplete(); Teuchos::RCP<Epetra_CrsMatrix> broydenWorkMatrix2 = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph2 ) ); // Create an identity matrix broydenWorkVec.PutScalar(1.0); broydenWorkMatrix2->ReplaceDiagonalValues(broydenWorkVec); NOX::Epetra::BroydenOperator broydenOp2( noxParams, printing, broydenWorkVec, broydenWorkMatrix2, true ); broydenWorkVec[0] = 1.0; broydenWorkVec[1] = -1.0; broydenWorkVec[2] = 2.0; broydenOp2.setStepVector( broydenWorkVec ); broydenWorkVec[0] = 2.0; broydenWorkVec[1] = 1.0; broydenWorkVec[2] = 3.0; broydenOp2.setYieldVector( broydenWorkVec ); broydenOp2.computeSparseBroydenUpdate(); // Create the gold matrix for comparison Teuchos::RCP<Epetra_CrsMatrix> goldMatrix2 = Teuchos::rcp( new Epetra_CrsMatrix( Copy, broydenWorkGraph2 ) ); // Row 1 answers goldMatrix2->ExtractMyRowView( 0, numCols, values ); values[0] = 7.0 ; values[1] = -1.0 ; values[2] = 2.0 ; // Row 2 answers goldMatrix2->ExtractMyRowView( 1, numCols, values ); values[0] = 2.0 ; values[1] = 4.0 ; values[2] = 4.0 ; // Row 3 structure goldMatrix2->ExtractMyRowView( 2, numCols, values ); values[0] = 1.0 ; values[1] = -1.0 ; values[2] = 8.0 ; double scaleF = 1.0 / 6.0; goldMatrix2->Scale( scaleF ); status += tester.testCrsMatrices( broydenOp2.getBroydenMatrix(), *goldMatrix2, reltol, abstol, "Broyden Sparse Operator Update Test (Dense)" ); // Now test the ability to remove active entries in the Broyden update Epetra_CrsGraph inactiveGraph( Copy, broydenRowMap, 0 ); // Row 1 structure inactiveGraph.InsertGlobalIndices( globalIndices[0], 1, &myGlobalIndices[1] ); // Row 2 structure inactiveGraph.InsertGlobalIndices( globalIndices[1], 1, &myGlobalIndices[2] ); // Row 3 structure inactiveGraph.InsertGlobalIndices( globalIndices[2], 1, &myGlobalIndices[0] ); inactiveGraph.FillComplete(); // Inactivate entries in dense matrix to arrive again at the original sparse structure broydenOp2.removeEntriesFromBroydenUpdate( inactiveGraph ); #ifdef HAVE_NOX_DEBUG if( verbose ) broydenOp2.outputActiveEntries(); #endif // Reset to the identity matrix broydenOp2.resetBroydenMatrix( *broydenWorkMatrix2 ); // Step and Yield vectors are already set broydenOp2.computeSparseBroydenUpdate(); status += tester.testCrsMatrices( broydenOp2.getBroydenMatrix(), *goldMatrix, reltol, abstol, "Broyden Sparse Operator Update Test (Entry Removal)", false ); // Summarize test results if( status == 0 ) printing->out() << "Test passed!" << endl; else printing->out() << "Test failed!" << endl; #ifdef HAVE_MPI MPI_Finalize(); #endif // Final return value (0 = successfull, non-zero = failure) return status; }
int main(int argc, char *argv[]) { using std::cout; using std::endl; bool boolret; int MyPID; #ifdef HAVE_MPI // Initialize MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif MyPID = Comm.MyPID(); bool testFailed = false; bool verbose = false; bool debug = false; bool shortrun = false; bool skinny = true; bool useSA = false; std::string which("SR"); int numElements = 100; std::string prec("none"); int user_verbosity = 0; int numicgs = 2; bool success = true; try { CommandLineProcessor cmdp(false,true); cmdp.setOption("verbose","quiet",&verbose,"Print messages and results."); cmdp.setOption("debug","nodebug",&debug,"Print debugging information."); cmdp.setOption("sort",&which,"Targetted eigenvalues (SR or LR)."); cmdp.setOption("shortrun","longrun",&shortrun,"Allow only a small number of iterations."); cmdp.setOption("skinny","hefty",&skinny,"Use a skinny (low-mem) or hefty (higher-mem) implementation of IRTR."); cmdp.setOption("numElements",&numElements,"Number of elements in discretization."); cmdp.setOption("prec",&prec,"Preconditioning type (""none"", ""olsen"", ""simple"""); cmdp.setOption("useSA","noSA",&useSA,"Use subspace acceleration."); cmdp.setOption("verbosity",&user_verbosity,"Additional verbosity falgs."); cmdp.setOption("numICGS",&numicgs,"Num ICGS iterations"); if (cmdp.parse(argc,argv) != CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } if (debug) verbose = true; if (prec != "olsen" && prec != "simple") { prec = "none"; } typedef double ScalarType; typedef ScalarTraits<ScalarType> SCT; typedef SCT::magnitudeType MagnitudeType; typedef Epetra_MultiVector MV; typedef Epetra_Operator OP; typedef Anasazi::MultiVecTraits<ScalarType,MV> MVT; typedef Anasazi::OperatorTraits<ScalarType,MV,OP> OPT; const ScalarType ONE = SCT::one(); if (verbose && MyPID == 0) { cout << Anasazi::Anasazi_Version() << endl << endl; } // Problem information int space_dim = 1; std::vector<double> brick_dim( space_dim ); brick_dim[0] = 1.0; std::vector<int> elements( space_dim ); elements[0] = numElements; // Create problem RCP<ModalProblem> testCase = rcp( new ModeLaplace1DQ1(Comm, brick_dim[0], elements[0]) ); // // Get the stiffness and mass matrices RCP<const Epetra_CrsMatrix> K = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getStiffness()), false ); RCP<const Epetra_CrsMatrix> M = rcp( const_cast<Epetra_CrsMatrix *>(testCase->getMass()), false ); RCP<const Epetra_CrsMatrix> P; if (prec != "none") { // construct a diagonal preconditioner Epetra_Vector D(K->RowMap(),false); K->ExtractDiagonalCopy(D); for (int i=0; i<D.MyLength(); ++i) { D[i] = 1.0 / D[i]; } RCP<Epetra_CrsMatrix> ncP = rcp( new Epetra_CrsMatrix(Epetra_DataAccess::Copy,K->RowMap(),K->RowMap(),1,true) ); for (int i=0; i<D.MyLength(); ++i) { ncP->InsertMyValues(i,1,&D[i],&i); } ncP->FillComplete(true); P = ncP; } // // Create the initial vectors int blockSize = 5; RCP<Epetra_MultiVector> ivec = rcp( new Epetra_MultiVector(K->OperatorDomainMap(), blockSize) ); ivec->Random(); // Create eigenproblem const int nev = 5; RCP<Anasazi::BasicEigenproblem<ScalarType,MV,OP> > problem = rcp( new Anasazi::BasicEigenproblem<ScalarType,MV,OP>(K,M,ivec) ); // // Inform the eigenproblem that the operator K is symmetric problem->setHermitian(true); // // Set the number of eigenvalues requested problem->setNEV( nev ); // // Set the preconditioner, if using one. Also, indicates the type. if (prec != "none") { problem->setPrec(P); } // // Inform the eigenproblem that you are done passing it information boolret = problem->setProblem(); if (boolret != true) { if (verbose && MyPID == 0) { cout << "Anasazi::BasicEigenproblem::SetProblem() returned with error." << endl << "End Result: TEST FAILED" << endl; } #ifdef HAVE_MPI MPI_Finalize() ; #endif return -1; } // Set verbosity level int verbosity = Anasazi::Errors + Anasazi::Warnings; if (verbose) { verbosity += Anasazi::FinalSummary + Anasazi::TimingDetails; } if (debug) { verbosity += Anasazi::Debug; } verbosity |= user_verbosity; // Eigensolver parameters int maxIters; if (shortrun) { maxIters = 50; } else { maxIters = 450; } MagnitudeType tol = 1.0e-6; // // Create parameter list to pass into the solver manager ParameterList MyPL; MyPL.set( "Skinny Solver", skinny); MyPL.set( "Verbosity", verbosity ); MyPL.set( "Which", which ); MyPL.set( "Block Size", blockSize ); MyPL.set( "Maximum Iterations", maxIters ); MyPL.set( "Convergence Tolerance", tol ); MyPL.set( "Use SA", useSA ); MyPL.set( "Num ICGS", numicgs ); if (prec == "olsen") { MyPL.set( "Olsen Prec", true ); } else if (prec == "simple") { MyPL.set( "Olsen Prec", false ); } // // Create the solver manager Anasazi::RTRSolMgr<ScalarType,MV,OP> MySolverMan(problem, MyPL); // Solve the problem to the specified tolerances or length Anasazi::ReturnType returnCode = MySolverMan.solve(); testFailed = false; if (returnCode != Anasazi::Converged) { if (shortrun==false) { testFailed = true; } } /* // // Check that the parameters were all consumed if (MyPL.getEntryPtr("Verbosity")->isUsed() == false || MyPL.getEntryPtr("Which")->isUsed() == false || MyPL.getEntryPtr("Skinny Solver")->isUsed() == false || MyPL.getEntryPtr("Block Size")->isUsed() == false || MyPL.getEntryPtr("Maximum Iterations")->isUsed() == false || MyPL.getEntryPtr("Convergence Tolerance")->isUsed() == false) { if (verbose && MyPID==0) { cout << "Failure! Unused parameters: " << endl; MyPL.unused(cout); } } */ // Get the eigenvalues and eigenvectors from the eigenproblem Anasazi::Eigensolution<ScalarType,MV> sol = problem->getSolution(); std::vector<Anasazi::Value<ScalarType> > evals = sol.Evals; RCP<MV> evecs = sol.Evecs; int numev = sol.numVecs; if (numev > 0) { std::ostringstream os; os.setf(std::ios::scientific, std::ios::floatfield); os.precision(6); // Check the problem against the analytical solutions if (verbose) { double *revals = new double[numev]; for (int i=0; i<numev; i++) { revals[i] = evals[i].realpart; } bool smallest = false; if (which == "SR") { smallest = true; } testCase->eigenCheck( *evecs, revals, 0, smallest ); delete [] revals; } // Compute the direct residual std::vector<ScalarType> normV( numev ); SerialDenseMatrix<int,ScalarType> T(numev,numev); for (int i=0; i<numev; i++) { T(i,i) = evals[i].realpart; } RCP<MV> Mvecs = MVT::Clone( *evecs, numev ), Kvecs = MVT::Clone( *evecs, numev ); OPT::Apply( *K, *evecs, *Kvecs ); OPT::Apply( *M, *evecs, *Mvecs ); MVT::MvTimesMatAddMv( -ONE, *Mvecs, T, ONE, *Kvecs ); // compute M-norm of residuals OPT::Apply( *M, *Kvecs, *Mvecs ); MVT::MvDot( *Mvecs, *Kvecs, normV ); os << "Number of iterations performed in RTR_test.exe: " << MySolverMan.getNumIters() << endl << "Direct residual norms computed in RTR_test.exe" << endl << std::setw(20) << "Eigenvalue" << std::setw(20) << "Residual(M)" << endl << "----------------------------------------" << endl; for (int i=0; i<numev; i++) { if ( SCT::magnitude(evals[i].realpart) != SCT::zero() ) { normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) / evals[i].realpart ); } else { normV[i] = SCT::magnitude( SCT::squareroot( normV[i] ) ); } os << std::setw(20) << evals[i].realpart << std::setw(20) << normV[i] << endl; if ( normV[i] > tol ) { testFailed = true; } } if (verbose && MyPID==0) { cout << endl << os.str() << endl; } } } TEUCHOS_STANDARD_CATCH_STATEMENTS(true,cout,success); #ifdef HAVE_MPI MPI_Finalize() ; #endif if (testFailed || success==false) { if (verbose && MyPID==0) { cout << "End Result: TEST FAILED" << endl; } return -1; } // // Default return value // if (verbose && MyPID==0) { cout << "End Result: TEST PASSED" << endl; } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc, &argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif bool verbose = (Comm.MyPID() == 0); double TotalResidual = 0.0; // Create the Map, defined as a grid, of size nx x ny x nz, // subdivided into mx x my x mz cubes, each assigned to a // different processor. #if 0 ParameterList GaleriList; GaleriList.set("nx", 4); GaleriList.set("ny", 4); GaleriList.set("nz", 4 * Comm.NumProc()); GaleriList.set("mx", 1); GaleriList.set("my", 1); GaleriList.set("mz", Comm.NumProc()); Epetra_Map* Map = CreateMap("Cartesian3D", Comm, GaleriList); // Create a matrix, in this case corresponding to a 3D Laplacian // discretized using a classical 7-point stencil. Please refer to // the Galeri documentation for an overview of available matrices. // // NOTE: matrix must be symmetric if DSCPACK is used. Epetra_CrsMatrix* Matrix = CreateCrsMatrix("Laplace3D", Map, GaleriList); #else bool transpose = false ; bool distribute = false ; bool symmetric ; Epetra_CrsMatrix *Matrix = 0 ; Epetra_Map *Map = 0 ; CreateCrsMatrix( "ibm.triU", Comm, Map, transpose, distribute, &symmetric, Matrix ) ; #endif // build vectors, in this case with 1 vector Epetra_MultiVector LHS(*Map, 1); Epetra_MultiVector RHS(*Map, 1); // create a linear problem object Epetra_LinearProblem Problem(Matrix, &LHS, &RHS); // use this list to set up parameters, now it is required // to use all the available processes (if supported by the // underlying solver). Uncomment the following two lines // to let Amesos print out some timing and status information. ParameterList List; List.set("PrintTiming",true); List.set("PrintStatus",true); List.set("MaxProcs",Comm.NumProc()); std::vector<std::string> SolverType; SolverType.push_back("Amesos_Paraklete"); Epetra_Time Time(Comm); // this is the Amesos factory object that will create // a specific Amesos solver. Amesos Factory; // Cycle over all solvers. // Only installed solvers will be tested. for (unsigned int i = 0 ; i < SolverType.size() ; ++i) { // Check whether the solver is available or not if (Factory.Query(SolverType[i])) { // 1.- set exact solution (constant vector) LHS.PutScalar(1.0); // 2.- create corresponding rhs Matrix->Multiply(false, LHS, RHS); // 3.- randomize solution vector LHS.Random(); // 4.- create the amesos solver object Amesos_BaseSolver* Solver = Factory.Create(SolverType[i], Problem); assert (Solver != 0); Solver->SetParameters(List); // 5.- factorize and solve Time.ResetStartTime(); AMESOS_CHK_ERR(Solver->SymbolicFactorization()); // 7.- delete the object delete Solver; } } delete Matrix; delete Map; if (TotalResidual > 1e-9) exit(EXIT_FAILURE); #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); } // end of main()
int main(int argc, char *argv[]) { #ifdef HAVE_MPI MPI_Init(&argc,&argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // Creates the linear problem using the Galeri package. // Several matrix examples are supported; please refer to the // Galeri documentation for more details. // Most of the examples using the ML_Epetra::MultiLevelPreconditioner // class are based on Epetra_CrsMatrix. Example // `ml_EpetraVbr.cpp' shows how to define a Epetra_VbrMatrix. // `Laplace2D' is a symmetric matrix; an example of non-symmetric // matrices is `Recirc2D' (advection-diffusion in a box, with // recirculating flow). The grid has nx x ny nodes, divided into // mx x my subdomains, each assigned to a different processor. int nx; if (argc > 1) nx = (int) strtol(argv[1],NULL,10); else nx = 8; int ny = nx * Comm.NumProc(); // each subdomain is a square ParameterList GaleriList; GaleriList.set("nx", nx); GaleriList.set("ny", ny); GaleriList.set("mx", 1); GaleriList.set("my", Comm.NumProc()); Epetra_Map* Map = CreateMap("Cartesian2D", Comm, GaleriList); Epetra_CrsMatrix* A = CreateCrsMatrix("Laplace2D", Map, GaleriList); // Build a linear system with trivial solution, using a random vector // as starting solution. Epetra_Vector LHS(*Map); LHS.Random(); Epetra_Vector RHS(*Map); RHS.PutScalar(0.0); Epetra_LinearProblem Problem(A, &LHS, &RHS); // As we wish to use AztecOO, we need to construct a solver object // for this problem AztecOO solver(Problem); // =========================== begin of ML part =========================== // create a parameter list for ML options ParameterList MLList; // Sets default parameters for classic smoothed aggregation. After this // call, MLList contains the default values for the ML parameters, // as required by typical smoothed aggregation for symmetric systems. // Other sets of parameters are available for non-symmetric systems // ("DD" and "DD-ML"), and for the Maxwell equations ("maxwell"). ML_Epetra::SetDefaults("SA",MLList); // overwrite some parameters. Please refer to the user's guide // for more information // some of the parameters do not differ from their default value, // and they are here reported for the sake of clarity // output level, 0 being silent and 10 verbose MLList.set("ML output", 10); // maximum number of levels MLList.set("max levels",5); // set finest level to 0 MLList.set("increasing or decreasing","increasing"); // use Uncoupled scheme to create the aggregate MLList.set("aggregation: type", "Uncoupled"); // smoother is symmetric Gauss-Seidel. Example file // `ml/examples/TwoLevelDD/ml_2level_DD.cpp' shows how to use // AZTEC's preconditioners as smoothers //MLList.set("smoother: type","symmetric Gauss-Seidel"); // use both pre and post smoothing MLList.set("smoother: pre or post", "both"); MLList.set("smoother: type", "user-defined"); //The function pointer to the user-defined smoother. MLList.set("smoother: user-defined function",userSmoother); //(optional) The label for the user-defined smoother. MLList.set("smoother: user-defined name","myJacobi"); //Use this smoother on the finest level, 0. MLList.set("smoother: type (level 0)", "user-defined"); MLList.set("smoother: sweeps (level 0)", 1); //Use symmetric Gauss-Seidel on all subsequent levels. MLList.set("smoother: type (level 1)", "symmetric Gauss-Seidel"); //user-defined smoothing for a 1-level method only //to use this, uncomment the next 3 lines and comment out the direct-solver //settings further below //MLList.set("coarse: type", "user-defined"); //MLList.set("coarse: user-defined function",userSmoother); //MLList.set("coarse: user-defined name","myJacobi"); #ifdef HAVE_ML_AMESOS // solve with serial direct solver KLU MLList.set("coarse: type","Amesos-KLU"); #else // this is for testing purposes only, you should have // a direct solver for the coarse problem (either Amesos, or the SuperLU/ // SuperLU_DIST interface of ML) MLList.set("aggregation: type", "MIS"); MLList.set("smoother: type","Jacobi"); MLList.set("coarse: type","Jacobi"); #endif MLList.set("read XML", false); // skip XML file in this directory // Creates the preconditioning object. We suggest to use `new' and // `delete' because the destructor contains some calls to MPI (as // required by ML and possibly Amesos). This is an issue only if the // destructor is called **after** MPI_Finalize(). ML_Epetra::MultiLevelPreconditioner* MLPrec = new ML_Epetra::MultiLevelPreconditioner(*A, MLList); // verify unused parameters on process 0 (put -1 to print on all // processes) MLPrec->PrintUnused(0); // =========================== end of ML part ============================= // tell AztecOO to use the ML preconditioner, specify the solver // and the output, then solve with 500 maximum iterations and 1e-12 // of tolerance (see AztecOO's user guide for more details) solver.SetPrecOperator(MLPrec); solver.SetAztecOption(AZ_solver, AZ_gmres); solver.SetAztecOption(AZ_output, 32); solver.Iterate(500, 1e-12); // destroy the preconditioner delete MLPrec; // compute the real residual double residual; LHS.Norm2(&residual); if( Comm.MyPID()==0 ) { cout << "||b-Ax||_2 = " << residual << endl; } // for testing purposes if (residual > 1e-5) exit(EXIT_FAILURE); delete A; delete Map; #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); } //main
int main(int argc, char *argv[]) { int ierr = 0, i; #ifdef EPETRA_MPI // Initialize MPI MPI_Init(&argc,&argv); int rank; // My process ID MPI_Comm_rank(MPI_COMM_WORLD, &rank); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else int rank = 0; Epetra_SerialComm Comm; #endif #ifdef HAVE_EPETRA_TEUCHOS Teuchos::RCP<Teuchos::FancyOStream> fancyOut = Teuchos::VerboseObjectBase::getDefaultOStream(); if (Comm.NumProc() > 1 ) { fancyOut->setShowProcRank(true); fancyOut->setOutputToRootOnly(-1); } std::ostream &out = *fancyOut; #else std::ostream &out = std::cout; #endif Comm.SetTracebackMode(0); // This should shut down any error tracing bool verbose = false; // Check if we should print results to standard out if (argc>1) if (argv[1][0]=='-' && argv[1][1]=='v') verbose = true; // char tmp; // if (rank==0) out << "Press any key to continue..."<< endl; // if (rank==0) cin >> tmp; // Comm.Barrier(); int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); if (verbose && MyPID==0) out << Epetra_Version() << endl << endl; if (verbose) out << Comm <<endl; bool verbose1 = verbose; // Redefine verbose to only print on PE 0 if (verbose && rank!=0) verbose = false; int NumMyElements = 10000; int NumMyElements1 = NumMyElements; // Needed for localmap int NumGlobalElements = NumMyElements*NumProc+EPETRA_MIN(NumProc,3); if (MyPID < 3) NumMyElements++; int IndexBase = 0; int ElementSize = 7; // Test LocalMap constructor // and Petra-defined uniform linear distribution constructor if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_LocalMap(NumMyElements1, IndexBase, Comm)" << endl; if (verbose) out << " and Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; Epetra_LocalMap *LocalMap = new Epetra_LocalMap(NumMyElements1, IndexBase, Comm); Epetra_BlockMap * BlockMap = new Epetra_BlockMap(NumGlobalElements, ElementSize, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr); delete BlockMap; // Test User-defined linear distribution constructor if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, ElementSize, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr); delete BlockMap; // Test User-defined arbitrary distribution constructor // Generate Global Element List. Do in reverse for fun! int * MyGlobalElements = new int[NumMyElements]; int MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase; if (Comm.MyPID()>2) MaxMyGID+=3; for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i; if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSize, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSize, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr); delete BlockMap; int * ElementSizeList = new int[NumMyElements]; int NumMyEquations = 0; int NumGlobalEquations = 0; for (i = 0; i<NumMyElements; i++) { ElementSizeList[i] = i%6+2; // blocksizes go from 2 to 7 NumMyEquations += ElementSizeList[i]; } ElementSize = 7; // Set to maximum for use in checkmap NumGlobalEquations = Comm.NumProc()*NumMyEquations; // Adjust NumGlobalEquations based on processor ID if (Comm.NumProc() > 3) { if (Comm.MyPID()>2) NumGlobalEquations += 3*((NumMyElements)%6+2); else NumGlobalEquations -= (Comm.NumProc()-3)*((NumMyElements-1)%6+2); } if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSizeList, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; BlockMap = new Epetra_BlockMap(NumGlobalElements, NumMyElements, MyGlobalElements, ElementSizeList, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr); // Test Copy constructor if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_BlockMap(*BlockMap)" << endl; if (verbose) out << "*********************************************************" << endl; Epetra_BlockMap * BlockMap1 = new Epetra_BlockMap(*BlockMap); EPETRA_TEST_ERR(VectorTests(*BlockMap, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*BlockMap, *LocalMap, verbose),ierr); delete [] ElementSizeList; delete [] MyGlobalElements; delete BlockMap; delete BlockMap1; // Test Petra-defined uniform linear distribution constructor if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_Map(NumGlobalElements, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; Epetra_Map * Map = new Epetra_Map(NumGlobalElements, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr); delete Map; // Test User-defined linear distribution constructor if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; Map = new Epetra_Map(NumGlobalElements, NumMyElements, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr); delete Map; // Test User-defined arbitrary distribution constructor // Generate Global Element List. Do in reverse for fun! MyGlobalElements = new int[NumMyElements]; MaxMyGID = (Comm.MyPID()+1)*NumMyElements-1+IndexBase; if (Comm.MyPID()>2) MaxMyGID+=3; for (i = 0; i<NumMyElements; i++) MyGlobalElements[i] = MaxMyGID-i; if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm)" << endl; if (verbose) out << "*********************************************************" << endl; Map = new Epetra_Map(NumGlobalElements, NumMyElements, MyGlobalElements, IndexBase, Comm); EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr); // Test Copy constructor if (verbose) out << "\n*********************************************************" << endl; if (verbose) out << "Checking Epetra_Map(*Map)" << endl; if (verbose) out << "*********************************************************" << endl; Epetra_Map Map1(*Map); EPETRA_TEST_ERR(VectorTests(*Map, verbose),ierr); EPETRA_TEST_ERR(MatrixTests(*Map, *LocalMap, verbose),ierr); delete [] MyGlobalElements; delete Map; if (verbose1) { // Test Vector MFLOPS for 2D Dot Product int M = 1; int K = 1000000; Epetra_Map Map2(-1, K, IndexBase, Comm); Epetra_LocalMap Map3(M, IndexBase, Comm); Epetra_Vector A(Map2);A.Random(); Epetra_Vector B(Map2);B.Random(); Epetra_Vector C(Map3);C.Random(); // Test Epetra_Vector label const char* VecLabel = A.Label(); const char* VecLabel1 = "Epetra::Vector"; if (verbose) out << endl << endl <<"This should say " << VecLabel1 << ": " << VecLabel << endl << endl << endl; EPETRA_TEST_ERR(strcmp(VecLabel1,VecLabel),ierr); if (verbose) out << "Testing Assignment operator" << endl; double tmp1 = 1.00001* (double) (MyPID+1); double tmp2 = tmp1; A[1] = tmp1; tmp2 = A[1]; out << "On PE "<< MyPID << " A[1] should equal = " << tmp1; if (tmp1==tmp2) out << " and it does!" << endl; else out << " but it equals " << tmp2; Comm.Barrier(); if (verbose) out << endl << endl << "Testing MFLOPs" << endl; Epetra_Flops counter; C.SetFlopCounter(counter); Epetra_Time mytimer(Comm); C.Multiply('T', 'N', 0.5, A, B, 0.0); double Multiply_time = mytimer.ElapsedTime(); double Multiply_flops = C.Flops(); if (verbose) out << "\n\nTotal FLOPs = " << Multiply_flops << endl; if (verbose) out << "Total Time = " << Multiply_time << endl; if (verbose) out << "MFLOPs = " << Multiply_flops/Multiply_time/1000000.0 << endl; Comm.Barrier(); // Test Vector ostream operator with Petra-defined uniform linear distribution constructor // and a small vector Epetra_Map Map4(100, IndexBase, Comm); double * Dp = new double[100]; for (i=0; i<100; i++) Dp[i] = i; Epetra_Vector D(View, Map4,Dp); if (verbose) out << "\n\nTesting ostream operator: Multivector should be 100-by-2 and print i,j indices" << endl << endl; out << D << endl; if (verbose) out << "Traceback Mode value = " << D.GetTracebackMode() << endl; delete [] Dp; } #ifdef EPETRA_MPI MPI_Finalize(); #endif return ierr; }
int main(int argc, char *argv[]) { int ierr = 0; double nonlinear_factor = 1.0; double left_bc = 0.0; double right_bc = 0.40; // Initialize MPI #ifdef HAVE_MPI MPI_Init(&argc,&argv); #endif // Create a communicator for Epetra objects #ifdef HAVE_MPI Epetra_MpiComm Comm( MPI_COMM_WORLD ); #else Epetra_SerialComm Comm; #endif // Get the process ID and the total number of processors int MyPID = Comm.MyPID(); int NumProc = Comm.NumProc(); // Get the number of elements from the command line int NumGlobalElements = 100 + 1; // The number of unknowns must be at least equal to the // number of processors. if (NumGlobalElements < NumProc) { std::cout << "numGlobalBlocks = " << NumGlobalElements << " cannot be < number of processors = " << NumProc << std::endl; exit(1); } // Create the FiniteElementProblem class. This creates all required // Epetra objects for the problem and allows calls to the // function (RHS) and Jacobian evaluation routines. FiniteElementProblem Problem(NumGlobalElements, Comm); // Get the vector from the Problem Epetra_Vector& soln = Problem.getSolution(); // Initialize Solution soln.PutScalar(0.1); // Create initial guess for the null vector of jacobian Teuchos::RCP<NOX::Abstract::Vector> solnTwo = Teuchos::rcp(new NOX::Epetra::Vector(soln)); solnTwo->init(2.5); // initial value 1.0 // Begin LOCA Solver ************************************ // Create parameter list Teuchos::RCP<Teuchos::ParameterList> paramList = Teuchos::rcp(new Teuchos::ParameterList); // Create LOCA sublist Teuchos::ParameterList& locaParamsList = paramList->sublist("LOCA"); // Create the stepper sublist and set the stepper parameters Teuchos::ParameterList& locaStepperList = locaParamsList.sublist("Stepper"); locaStepperList.set("Continuation Method", "Natural"); //locaStepperList.set("Bordered Solver Method", "Nested"); //locaStepperList.set("Bordered Solver Method", "Householder"); locaStepperList.set("Continuation Parameter", "Nonlinear Factor"); locaStepperList.set("Initial Value", nonlinear_factor); locaStepperList.set("Max Value", 1.6); locaStepperList.set("Min Value", 0.00); locaStepperList.set("Max Steps", 20); locaStepperList.set("Max Nonlinear Iterations", 15); // Create bifurcation sublist Teuchos::ParameterList& bifurcationList = locaParamsList.sublist("Bifurcation"); bifurcationList.set("Type", "Phase Transition"); bifurcationList.set("Bifurcation Parameter", "Right BC"); bifurcationList.set("Second Solution Vector", solnTwo); // Create predictor sublist Teuchos::ParameterList& predictorList = locaParamsList.sublist("Predictor"); predictorList.set("Method", "Secant"); // Create step size sublist Teuchos::ParameterList& stepSizeList = locaParamsList.sublist("Step Size"); stepSizeList.set("Method", "Constant"); stepSizeList.set("Initial Step Size", 0.1); stepSizeList.set("Min Step Size", 1.0e-3); stepSizeList.set("Max Step Size", 2000.0); stepSizeList.set("Aggressiveness", 0.1); // Create the "Solver" parameters sublist to be used with NOX Solvers Teuchos::ParameterList& nlParams = paramList->sublist("NOX"); // Create the NOX printing parameter list Teuchos::ParameterList& nlPrintParams = nlParams.sublist("Printing"); nlPrintParams.set("MyPID", MyPID); nlPrintParams.set("Output Precision", 6); nlPrintParams.set("Output Information", NOX::Utils::OuterIteration + NOX::Utils::OuterIterationStatusTest + NOX::Utils::InnerIteration + NOX::Utils::Details + NOX::Utils::LinearSolverDetails + NOX::Utils::Warning + NOX::Utils::StepperIteration + NOX::Utils::StepperDetails + NOX::Utils::StepperParameters); // Create the "Linear Solver" sublist for Newton's method Teuchos::ParameterList& dirParams = nlParams.sublist("Direction"); Teuchos::ParameterList& newParams = dirParams.sublist("Newton"); Teuchos::ParameterList& lsParams = newParams.sublist("Linear Solver"); lsParams.set("Aztec Solver", "GMRES"); lsParams.set("Max Iterations", 200); lsParams.set("Tolerance", 1e-6); lsParams.set("Output Frequency", 50); //lsParams.set("Scaling", "None"); //lsParams.set("Scaling", "Row Sum"); lsParams.set("Compute Scaling Manually", false); lsParams.set("Preconditioner", "Ifpack"); lsParams.set("Ifpack Preconditioner", "ILU"); //lsParams.set("Preconditioner", "New Ifpack"); //Teuchos::ParameterList& ifpackParams = lsParams.sublist("Ifpack"); //ifpackParams.set("fact: level-of-fill", 1); // Create and initialize the parameter vector LOCA::ParameterVector pVector; pVector.addParameter("Nonlinear Factor",nonlinear_factor); pVector.addParameter("Left BC", left_bc); pVector.addParameter("Right BC", right_bc); // Create the interface between the test problem and the nonlinear solver // This is created by the user using inheritance of the abstract base class: Teuchos::RCP<Problem_Interface> interface = Teuchos::rcp(new Problem_Interface(Problem)); Teuchos::RCP<LOCA::Epetra::Interface::TimeDependent> iReq = interface; Teuchos::RCP<NOX::Epetra::Interface::Jacobian> iJac = interface; // Create the Epetra_RowMatrixfor the Jacobian/Preconditioner Teuchos::RCP<Epetra_RowMatrix> Amat = Teuchos::rcp(&Problem.getJacobian(),false); // Create scaling object Teuchos::RCP<NOX::Epetra::Scaling> scaling = Teuchos::null; // scaling = Teuchos::rcp(new NOX::Epetra::Scaling); // Teuchos::RCP<Epetra_Vector> scalingVector = // Teuchos::rcp(new Epetra_Vector(soln.Map())); // //scaling->addRowSumScaling(NOX::Epetra::Scaling::Left, scalingVector); // scaling->addColSumScaling(NOX::Epetra::Scaling::Right, scalingVector); // Create transpose scaling object Teuchos::RCP<NOX::Epetra::Scaling> trans_scaling = Teuchos::null; // trans_scaling = Teuchos::rcp(new NOX::Epetra::Scaling); // Teuchos::RCP<Epetra_Vector> transScalingVector = // Teuchos::rcp(new Epetra_Vector(soln.Map())); // trans_scaling->addRowSumScaling(NOX::Epetra::Scaling::Right, // transScalingVector); // trans_scaling->addColSumScaling(NOX::Epetra::Scaling::Left, // transScalingVector); //bifurcationList.set("Transpose Scaling", trans_scaling); // Create the linear systems Teuchos::RCP<NOX::Epetra::LinearSystemAztecOO> linsys = Teuchos::rcp(new NOX::Epetra::LinearSystemAztecOO(nlPrintParams, lsParams, iReq, iJac, Amat, soln, scaling)); // Create the loca vector NOX::Epetra::Vector locaSoln(soln); // Create Epetra factory Teuchos::RCP<LOCA::Abstract::Factory> epetraFactory = Teuchos::rcp(new LOCA::Epetra::Factory); // Create global data object Teuchos::RCP<LOCA::GlobalData> globalData = LOCA::createGlobalData(paramList, epetraFactory); // Create the Group Teuchos::RCP<LOCA::Epetra::Group> grp = Teuchos::rcp(new LOCA::Epetra::Group(globalData, nlPrintParams, iReq, locaSoln, linsys, linsys, pVector)); // Inject FreeEnergy interface into the group Teuchos::RCP<LOCA::Epetra::Interface::FreeEnergy> iFE = interface; grp->setFreeEnergyInterface(iFE); grp->computeF(); // Create the Solver convergence test //NOX::StatusTest::NormWRMS wrms(1.0e-2, 1.0e-8); Teuchos::RCP<NOX::StatusTest::NormF> wrms = Teuchos::rcp(new NOX::StatusTest::NormF(1.0e-12)); Teuchos::RCP<NOX::StatusTest::MaxIters> maxiters = Teuchos::rcp(new NOX::StatusTest::MaxIters(locaStepperList.get("Max Nonlinear Iterations", 10))); Teuchos::RCP<NOX::StatusTest::Combo> combo = Teuchos::rcp(new NOX::StatusTest::Combo(NOX::StatusTest::Combo::OR)); combo->addStatusTest(wrms); combo->addStatusTest(maxiters); // Create the stepper LOCA::Stepper stepper(globalData, grp, combo, paramList); LOCA::Abstract::Iterator::IteratorStatus status = stepper.run(); if (status == LOCA::Abstract::Iterator::Finished) globalData->locaUtils->out() << "All tests passed" << std::endl; else { if (globalData->locaUtils->isPrintType(NOX::Utils::Error)) globalData->locaUtils->out() << "Stepper failed to converge!" << std::endl; } // Output the parameter list if (globalData->locaUtils->isPrintType(NOX::Utils::StepperParameters)) { globalData->locaUtils->out() << std::endl << "Final Parameters" << std::endl << "****************" << std::endl; stepper.getList()->print(globalData->locaUtils->out()); globalData->locaUtils->out() << std::endl; } LOCA::destroyGlobalData(globalData); #ifdef HAVE_MPI MPI_Finalize() ; #endif /* end main */ return ierr ; }
int main (int argc, char **argv) { #ifdef HAVE_MPI MPI_Init(&argc, &argv); Epetra_MpiComm Comm(MPI_COMM_WORLD); #else Epetra_SerialComm Comm; #endif // define some Epetra objects int n = Comm.NumProc() * 4; Epetra_Map Map(n, 0, Comm); Epetra_MultiVector x(Map, 2); x.Random(); Epetra_MultiVector b(Map, 2); x.Random(); Epetra_CrsMatrix Matrix(Copy, Map, 0); // diagonal matrix for (int i = 0; i < Map.NumMyElements(); ++i) { int ii = Map.GID(i); double one = 1.0; Matrix.InsertGlobalValues(ii, 1, &one, &ii); } Matrix.FillComplete(); Teuchos::ParameterList List; List.set("int parameter", 10); List.set("double parameter", 10.0); List.set("std::string parameter", "std::string"); // ========================= // // Part I: generate XML file // // ========================= // EpetraExt::XMLWriter XMLWriter(Comm, "data.xml"); std::vector<std::string> Content; Content.push_back("This is an example of description"); Content.push_back("The description is as long as desired,"); Content.push_back("just put it in a std::vector of strings."); XMLWriter.Create("MyProblem"); XMLWriter.Write("Author", "myself and others"); XMLWriter.Write("Date", "May 2006"); XMLWriter.Write("MyMap", Map); XMLWriter.Write("MyMatrix", Matrix); XMLWriter.Write("MyLHS", x); XMLWriter.Write("MyRHS", b); XMLWriter.Write("MyContent", Content); XMLWriter.Write("MyParameters", List); XMLWriter.Close(); // ================== // // Part II: read data // // ================== // EpetraExt::XMLReader XMLReader(Comm, "data.xml"); Epetra_Map* MyMap; Epetra_CrsMatrix* MyMatrix; Epetra_MultiVector* MyLHS; Epetra_MultiVector* MyRHS; Teuchos::ParameterList MyParameters; std::vector<std::string> Author; std::vector<std::string> Date; std::vector<std::string> MyContent; XMLReader.Read("Author", Author); XMLReader.Read("Date", Date); XMLReader.Read("MyMap", MyMap); XMLReader.Read("MyMatrix", MyMatrix); XMLReader.Read("MyLHS", MyLHS); XMLReader.Read("MyRHS", MyRHS); XMLReader.Read("MyContent", MyContent); XMLReader.Read("MyParameters", MyParameters); std::cout << *MyMap; std::cout << *MyMatrix; std::cout << *MyLHS; std::cout << *MyRHS; if (Comm.MyPID() == 0) { int Msize = (int) MyContent.size(); for (int i = 0; i < Msize; ++i) std::cout << MyContent[i] << std::endl; std::cout << MyParameters; std::cout << "Author = " << Author[0] << std::endl; std::cout << "Date = " << Date[0] << std::endl; } delete MyMap; delete MyMatrix; delete MyLHS; delete MyRHS; #ifdef HAVE_MPI MPI_Finalize(); #endif return(EXIT_SUCCESS); }