int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; double errtol = ROL::ROL_THRESHOLD; // *** Test body. try { int dim = 100; // Instantiate from raw pointer, Teuchos::ArrayRCP, and int (length) constructor ElementT* x_rawp = new ElementT[dim]; Teuchos::ArrayRCP<ElementT> y_arcp(dim,0); ROL::CArrayVector<RealT, ElementT> x(x_rawp,dim); ROL::CArrayVector<RealT, ElementT> y(y_arcp); ROL::CArrayVector<RealT, ElementT> z(dim); RealT left = -1e0, right = 1e0; // set x,y,z for (int i=0; i<dim; i++) { x_rawp[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; y_arcp[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; z.getVector()[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; } // Standard tests. std::vector<RealT> consistency = x.checkVector(y, z, true, *outStream); ROL::StdVector<RealT, ElementT> checkvec(Teuchos::rcp(&consistency, false)); if (checkvec.norm() > std::sqrt(ROL::ROL_EPSILON)) { errorFlag++; } // Basis tests. // set x to first basis vector Teuchos::RCP<ROL::Vector<RealT> > zp = x.clone(); zp = x.basis(0); RealT znorm = zp->norm(); *outStream << "Norm of ROL::Vector z (first basis vector): " << znorm << "\n"; if ( std::abs(znorm-1.0) > errtol ) { *outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; }; // set x to middle basis vector zp = x.basis(dim/2); znorm = zp->norm(); *outStream << "\nNorm of ROL::Vector z ('middle' basis vector): " << znorm << "\n"; if ( std::abs(znorm-1.0) > errtol ) { *outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; }; // set x to last basis vector zp = x.basis(dim-1); znorm = zp->norm(); *outStream << "\nNorm of ROL::Vector z (last basis vector): " << znorm << "\n"; if ( std::abs(znorm-1.0) > errtol ) { *outStream << "---> POSSIBLE ERROR ABOVE!\n"; errorFlag++; }; // Repeat the checkVector tests with a zero vector. x.scale(0.0); consistency = x.checkVector(x, x, true, *outStream); if (checkvec.norm() > 0.0) { errorFlag++; } delete[] x_rawp; } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
int main(int argc, char *argv[]) { #include "MueLu_UseShortNames.hpp" Teuchos::oblackholestream blackhole; Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole); bool success = true; bool verbose = true; try { Teuchos::RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); /**********************************************************************************/ /* SET TEST PARAMETERS */ /**********************************************************************************/ // Note: use --help to list available options. Teuchos::CommandLineProcessor clp(false); // Default is Laplace1D with nx = 8748. // It's a nice size for 1D and perfect aggregation. (6561=3^8) //Nice size for 1D and perfect aggregation on small numbers of processors. (8748=4*3^7) Galeri::Xpetra::Parameters<GO> matrixParameters(clp, 8748); // manage parameters of the test case Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra // custom parameters int pauseForDebugger=0; //std::string aggOrdering = "natural"; int minPerAgg=2; //was 3 in simple int maxNbrAlreadySelected=0; int printTimings=0; std::string xmlFile="parameters.xml"; //clp.setOption("aggOrdering",&aggOrdering,"aggregation ordering strategy (natural,graph)"); clp.setOption("debug",&pauseForDebugger,"pause to attach debugger"); clp.setOption("maxNbrSel",&maxNbrAlreadySelected,"maximum # of nbrs allowed to be in other aggregates"); clp.setOption("minPerAgg",&minPerAgg,"minimum #DOFs per aggregate"); clp.setOption("timings",&printTimings,"print timings to screen"); clp.setOption("xmlFile",&xmlFile,"file name containing MueLu multigrid parameters in XML format"); 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; } Teuchos::RCP<Teuchos::TimeMonitor> globalTimeMonitor = Teuchos::rcp (new Teuchos::TimeMonitor(*Teuchos::TimeMonitor::getNewTimer("Timings: Global Time"))); if (pauseForDebugger) { Utilities::PauseForDebugger(); } matrixParameters.check(); xpetraParameters.check(); Xpetra::UnderlyingLib lib = xpetraParameters.GetLib(); if (comm->getRank() == 0) { std::cout << xpetraParameters << matrixParameters; } /**********************************************************************************/ /* CREATE INITIAL MATRIX */ /**********************************************************************************/ Teuchos::RCP<const Map> map; Teuchos::RCP<Matrix> A; { Teuchos::TimeMonitor tm(*Teuchos::TimeMonitor::getNewTimer("Timings: Matrix Build")); map = MapFactory::Build(lib, matrixParameters.GetNumGlobalElements(), 0, comm); Teuchos::RCP<Galeri::Xpetra::Problem<Map,CrsMatrixWrap,MultiVector> > Pr = Galeri::Xpetra::BuildProblem<SC,LO,GO,Map,CrsMatrixWrap,MultiVector>(matrixParameters.GetMatrixType(), map, matrixParameters.GetParameterList()); //TODO: Matrix vs. CrsMatrixWrap A = Pr->BuildMatrix(); } /**********************************************************************************/ /* */ /**********************************************************************************/ Teuchos::ParameterList paramList; Teuchos::updateParametersFromXmlFileAndBroadcast(xmlFile, Teuchos::Ptr<Teuchos::ParameterList>(¶mList), *comm); // create parameter list interpreter Teuchos::RCP<HierarchyManager> mueluFactory = Teuchos::rcp(new ParameterListInterpreter(paramList)); Teuchos::RCP<Hierarchy> H = mueluFactory->CreateHierarchy(); H->GetLevel(0)->Set< Teuchos::RCP<Matrix> >("A", A); Teuchos::RCP<MultiVector> nullspace = MultiVectorFactory::Build(A->getRowMap(), 1); nullspace->putScalar(1.0); H->GetLevel(0)->Set("Nullspace", nullspace); // set minimal information about number of layers for semicoarsening... // This information can also be provided as a user parameter in the xml file using the // parameter: "semicoarsen: num layers" H->GetLevel(0)->Set("NumZLayers",matrixParameters.GetParameterList().get<GO>("nz")); mueluFactory->SetupHierarchy(*H); for (int l=0; l<H->GetNumLevels(); l++) { Teuchos::RCP<MueLu::Level> level = H->GetLevel(l); if(level->IsAvailable("A", MueLu::NoFactory::get()) == false) { success = false; H->GetLevel(l)->print(std::cout, MueLu::Debug);} if(level->IsAvailable("P", MueLu::NoFactory::get()) == false && l>0) { success = false; H->GetLevel(l)->print(std::cout, MueLu::Debug);} if(level->IsAvailable("R", MueLu::NoFactory::get()) == false && l>0) { success = false; H->GetLevel(l)->print(std::cout, MueLu::Debug);} if(level->IsAvailable("PreSmoother", MueLu::NoFactory::get()) == false) { success = false; H->GetLevel(l)->print(std::cout, MueLu::Debug);} if(level->IsAvailable("PostSmoother", MueLu::NoFactory::get()) == false && l<H->GetNumLevels()-1) { success = false; H->GetLevel(l)->print(std::cout, MueLu::Debug);} if(level->IsAvailable("NumZLayers", MueLu::NoFactory::get()) == true && l>0) { success = false; H->GetLevel(l)->print(std::cout, MueLu::Debug);} H->GetLevel(l)->print(std::cout, MueLu::Debug); } /////////////////////////////////////////////////////////// // ========================================================================= // System solution (Ax = b) // ========================================================================= comm->barrier(); typedef Teuchos::ScalarTraits<SC> STS; SC zero = STS::zero(), one = STS::one(); Teuchos::RCP<Vector> X = VectorFactory::Build(A->getRowMap()); Teuchos::RCP<Vector> B = VectorFactory::Build(A->getRowMap()); { // we set seed for reproducibility Utilities::SetRandomSeed(*comm); X->randomize(); A->apply(*X, *B, Teuchos::NO_TRANS, one, zero); Teuchos::Array<STS::magnitudeType> norms(1); B->norm2(norms); B->scale(one/norms[0]); X->putScalar(zero); } comm->barrier(); H->IsPreconditioner(false); H->Iterate(*B, *X, 20); // Timer final summaries globalTimeMonitor = Teuchos::null; // stop this timer before summary if (printTimings) Teuchos::TimeMonitor::summarize(); } TEUCHOS_STANDARD_CATCH_STATEMENTS(verbose, std::cerr, success); return ( success ? EXIT_SUCCESS : EXIT_FAILURE ); }
int main(int argc, char* argv[]) { Teuchos::RCP<Epetra_Comm> comm; #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); comm = Teuchos::rcp(new Epetra_MpiComm(MPI_COMM_WORLD)); #else comm = Teuchos::rcp(new Epetra_SerialComm()); #endif // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0 && comm->MyPID()==0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; try { /**********************************************************************************************/ /************************* CONSTRUCT ROL ALGORITHM ********************************************/ /**********************************************************************************************/ // Get ROL parameterlist std::string filename = "input.xml"; Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, Teuchos::Ptr<Teuchos::ParameterList>(&*parlist) ); // Build ROL algorithm double gtol = parlist->get("Gradient Tolerance",1.e-6); double stol = parlist->get("Step Tolerance",1.e-12); int maxit = parlist->get("Maximum Number of Iterations",100); ROL::StatusTest<double> status(gtol,stol,maxit); //ROL::LineSearchStep<double> step(*parlist); Teuchos::RCP<ROL::Step<double> > step; Teuchos::RCP<ROL::DefaultAlgorithm<double> > algo; /**********************************************************************************************/ /************************* CONSTRUCT SOL COMPONENTS *******************************************/ /**********************************************************************************************/ // Build vectors unsigned dim = 4; Teuchos::RCP<std::vector<double> > x_rcp = Teuchos::rcp( new std::vector<double>(dim,0.0) ); ROL::StdVector<double> x(x_rcp); Teuchos::RCP<std::vector<double> > x0_rcp = Teuchos::rcp( new std::vector<double>(dim,0.0) ); ROL::StdVector<double> x0(x0_rcp); Teuchos::RCP<std::vector<double> > xr_rcp = Teuchos::rcp( new std::vector<double>(dim,0.0) ); ROL::StdVector<double> xr(xr_rcp); Teuchos::RCP<std::vector<double> > d_rcp = Teuchos::rcp( new std::vector<double>(dim,0.0) ); ROL::StdVector<double> d(d_rcp); for ( unsigned i = 0; i < dim; i++ ) { (*x0_rcp)[i] = 1.0/(double)dim; (*xr_rcp)[i] = random<double>(comm); (*d_rcp)[i] = random<double>(comm); } // Build samplers int nSamp = 1000; std::vector<std::vector<double> > bounds(dim); std::vector<double> tmp(2,0.0); double inc = 0.125; double val = -0.5; for (unsigned i = 0; i < dim; i++) { tmp[0] = val-inc; tmp[1] = val+inc; bounds[i] = tmp; inc *= 2.0; } Teuchos::RCP<ROL::BatchManager<double> > bman = Teuchos::rcp(new ROL::StdEpetraBatchManager<double>(comm)); Teuchos::RCP<ROL::SampleGenerator<double> > sampler = Teuchos::rcp(new ROL::MonteCarloGenerator<double>(nSamp,bounds,bman,false,false,100)); // Build risk-averse objective function bool storage = true; Teuchos::RCP<ROL::ParametrizedObjective<double> > pObj = Teuchos::rcp(new ParametrizedObjectiveEx1<double>(1.e1)); Teuchos::RCP<ROL::RiskMeasure<double> > rm; Teuchos::RCP<ROL::Objective<double> > obj; // Build bound constraints std::vector<double> l(dim,0.0); std::vector<double> u(dim,1.0); Teuchos::RCP<ROL::BoundConstraint<double> > con = Teuchos::rcp( new ROL::StdBoundConstraint<double>(l,u) ); // Test parametrized objective functions *outStream << "Check Derivatives of Parametrized Objective Function\n"; x.set(xr); pObj->setParameter(sampler->getMyPoint(0)); pObj->checkGradient(x,d,true,*outStream); pObj->checkHessVec(x,d,true,*outStream); /**********************************************************************************************/ /************************* RISK NEUTRAL *******************************************************/ /**********************************************************************************************/ *outStream << "\nRISK NEUTRAL\n"; rm = Teuchos::rcp( new ROL::RiskMeasure<double>() ); obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); clock_t start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* RISK NEUTRAL *******************************************************/ /**********************************************************************************************/ *outStream << "\nRISK NEUTRAL\n"; obj = Teuchos::rcp( new ROL::RiskNeutralObjective<double>(pObj,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS DEVIATION ************************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS DEVIATION\n"; // Absolute value approximation double gamma = 0.0; ROL::EAbsoluteValue eav = ROL::ABSOLUTEVALUE_C2; Teuchos::RCP<ROL::PositiveFunction<double> > pf = Teuchos::rcp( new ROL::AbsoluteValue<double>(gamma,eav) ); // Moment vector std::vector<double> order(2,0.0); order[0] = 2.0; order[1] = 4.0; // Moment coefficients std::vector<double> coeff(2,0.1); rm = Teuchos::rcp( new ROL::MeanDeviation<double>(order,coeff,pf) ); obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS VARIANCE *************************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS VARIANCE\n"; rm = Teuchos::rcp( new ROL::MeanVariance<double>(order,coeff,pf) ); obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS DEVIATION FROM TARGET ************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS DEVIATION FROM TARGET\n"; // Moment targets std::vector<double> target(2,-0.1); // Risk measure rm = Teuchos::rcp( new ROL::MeanDeviationFromTarget<double>(target,order,coeff,pf) ); // Risk averse objective obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS VARIANCE FROM TARGET *************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS VARIANCE FROM TARGET\n"; // Risk measure coeff[1] = 0.0; rm = Teuchos::rcp( new ROL::MeanVarianceFromTarget<double>(target,order,coeff,pf) ); coeff[1] = 0.1; // Risk averse objective obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS SEMIDEVIATION ********************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS SEMIDEVIATION\n"; // Plus function approximation gamma = 1.e2; std::vector<double> data2(2,0.0); data2[0] = 0.0; data2[1] = 1.0; Teuchos::RCP<ROL::Distribution<double> > dist2 = Teuchos::rcp(new ROL::Distribution<double>(ROL::DISTRIBUTION_PARABOLIC,data2)); Teuchos::RCP<ROL::PlusFunction<double> > plusf = Teuchos::rcp(new ROL::PlusFunction<double>(dist2,1.0/gamma)); pf = Teuchos::rcp(new ROL::PlusFunction<double>(dist2,1.0/gamma)); // Risk measure rm = Teuchos::rcp( new ROL::MeanDeviation<double>(order,coeff,pf) ); // Risk averse objective obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS SEMIVARIANCE *********************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS SEMIVARIANCE\n"; // Risk measure rm = Teuchos::rcp( new ROL::MeanVariance<double>(order,coeff,pf) ); // Risk averse objective obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS SEMIDEVIATION FROM TARGET ********************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS SEMIDEVIATION FROM TARGET\n"; // Risk measure rm = Teuchos::rcp( new ROL::MeanDeviationFromTarget<double>(target,order,coeff,pf) ); // Risk averse objective obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS SEMIVARIANCE FROM TARGET *********************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS SEMIVARIANCE FROM TARGET\n"; // Risk measure rm = Teuchos::rcp( new ROL::MeanVarianceFromTarget<double>(target,order,coeff,pf) ); // Risk averse objective obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* MEAN PLUS CVAR *****************************************************/ /**********************************************************************************************/ *outStream << "\nMEAN PLUS CONDITIONAL VALUE AT RISK\n"; double prob = 0.8; double c = 0.8; rm = Teuchos::rcp( new ROL::CVaR<double>(prob,c,plusf) ); obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); Teuchos::RCP<ROL::BoundConstraint<double> > CVaRcon = Teuchos::rcp( new ROL::CVaRBoundConstraint<double>(con) ); // Test objective functions double xv = 10.0*random<double>(comm)-5.0; double dv = 10.0*random<double>(comm)-5.0; Teuchos::RCP<ROL::Vector<double> > xp = Teuchos::rcp(&x,false); Teuchos::RCP<ROL::Vector<double> > dp = Teuchos::rcp(&d,false); ROL::CVaRVector<double> xc(xv,xp); ROL::CVaRVector<double> dc(dv,dp); *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(xc,dc,true,*outStream); obj->checkHessVec(xc,dc,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(xc,*obj,*CVaRcon,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "t = " << xc.getVaR() << "\n"; *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* SMOOTHED CVAR QUADRANGLE *******************************************/ /**********************************************************************************************/ *outStream << "\nSMOOTHED CONDITIONAL VALUE AT RISK \n"; prob = 0.9; ROL::CVaRQuadrangle<double> scq(prob,1.0/gamma,plusf); //scq.checkRegret(); rm = Teuchos::rcp(&scq,false); obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions xv = 10.0*random<double>(comm)-5.0; dv = 10.0*random<double>(comm)-5.0; xp = Teuchos::rcp(&x,false); dp = Teuchos::rcp(&d,false); ROL::CVaRVector<double> xq(xv,xp); ROL::CVaRVector<double> dq(dv,dp); *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(xr); obj->checkGradient(xq,dq,true,*outStream); obj->checkHessVec(xq,dq,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(xq,*obj,*CVaRcon,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "t = " << xq.getVaR() << "\n"; *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; /**********************************************************************************************/ /************************* EXPONENTIAL UTILITY FUNCTION ***************************************/ /**********************************************************************************************/ *outStream << "\nEXPONENTIAL UTILITY FUNCTION\n"; rm = Teuchos::rcp( new ROL::ExpUtility<double> ); obj = Teuchos::rcp( new ROL::RiskAverseObjective<double>(pObj,rm,sampler,storage) ); // Test objective functions *outStream << "\nCheck Derivatives of Risk-Averse Objective Function\n"; x.set(x0); obj->checkGradient(x,d,true,*outStream); obj->checkHessVec(x,d,true,*outStream); // Run ROL algorithm step = Teuchos::rcp( new ROL::TrustRegionStep<double>(*parlist) ); algo = Teuchos::rcp( new ROL::DefaultAlgorithm<double>(*step,status,false) ); x.set(x0); start = clock(); algo->run(x,*obj,*con,true,*outStream); *outStream << "Optimization time: " << (double)(clock()-start)/(double)CLOCKS_PER_SEC << " seconds.\n"; // Print Solution *outStream << "x = ("; for ( unsigned i = 0; i < dim-1; i++ ) { *outStream << (*x_rcp)[i] << ", "; } *outStream << (*x_rcp)[dim-1] << ")\n"; } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); Teuchos::RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; bool print = (iprint>0); Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (print) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); bool print0 = print && !comm->getRank(); Teuchos::RCP<std::ostream> outStream0; if (print0) outStream0 = Teuchos::rcp(&std::cout, false); else outStream0 = Teuchos::rcp(&bhs, false); int errorFlag = 0; // *** Example body. try { /*************************************************************************/ /************* INITIALIZE BURGERS FEM CLASS ******************************/ /*************************************************************************/ int nx = 256; // Set spatial discretization. RealT alpha = 1.e-3; // Set penalty parameter. RealT nl = 1.0; // Nonlinearity parameter (1 = Burgers, 0 = linear). RealT cH1 = 1.0; // Scale for derivative term in H1 norm. RealT cL2 = 0.0; // Scale for mass term in H1 norm. Teuchos::RCP<BurgersFEM<RealT> > fem = Teuchos::rcp(new BurgersFEM<RealT>(nx,nl,cH1,cL2)); fem->test_inverse_mass(*outStream0); fem->test_inverse_H1(*outStream0); /*************************************************************************/ /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/ /*************************************************************************/ Teuchos::RCP<std::vector<RealT> > ud_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<ROL::Vector<RealT> > ud = Teuchos::rcp(new L2VectorPrimal<RealT>(ud_rcp,fem)); Teuchos::RCP<ROL::Objective_SimOpt<RealT> > pobj = Teuchos::rcp(new Objective_BurgersControl<RealT>(fem,ud,alpha)); /*************************************************************************/ /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/ /*************************************************************************/ bool hess = true; Teuchos::RCP<ROL::EqualityConstraint_SimOpt<RealT> > pcon = Teuchos::rcp(new EqualityConstraint_BurgersControl<RealT>(fem,hess)); /*************************************************************************/ /************* INITIALIZE VECTOR STORAGE *********************************/ /*************************************************************************/ // INITIALIZE CONTROL VECTORS Teuchos::RCP<std::vector<RealT> > z_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); Teuchos::RCP<std::vector<RealT> > gz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); Teuchos::RCP<std::vector<RealT> > yz_rcp = Teuchos::rcp( new std::vector<RealT> (nx+2, 1.0) ); for (int i=0; i<nx+2; i++) { (*yz_rcp)[i] = 2.0*random<RealT>(comm)-1.0; } Teuchos::RCP<ROL::Vector<RealT> > zp = Teuchos::rcp(new PrimalControlVector(z_rcp,fem)); Teuchos::RCP<ROL::Vector<RealT> > gzp = Teuchos::rcp(new DualControlVector(gz_rcp,fem)); Teuchos::RCP<ROL::Vector<RealT> > yzp = Teuchos::rcp(new PrimalControlVector(yz_rcp,fem)); std::vector<RealT> zvar(1,0.0*random<RealT>(comm)); std::vector<RealT> gvar(1,random<RealT>(comm)); std::vector<RealT> yvar(1,random<RealT>(comm)); ROL::RiskVector<RealT> z(zp,zvar,true), g(gzp,gvar,true), y(yzp,yvar,true); // INITIALIZE STATE VECTORS Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > gu_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(new PrimalStateVector(u_rcp,fem)); Teuchos::RCP<ROL::Vector<RealT> > gup = Teuchos::rcp(new DualStateVector(gu_rcp,fem)); // INITIALIZE CONSTRAINT VECTORS Teuchos::RCP<std::vector<RealT> > c_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); Teuchos::RCP<std::vector<RealT> > l_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 1.0) ); for (int i=0; i<nx; i++) { (*l_rcp)[i] = random<RealT>(comm); } Teuchos::RCP<ROL::Vector<RealT> > cp = Teuchos::rcp(new PrimalConstraintVector(c_rcp,fem)); Teuchos::RCP<ROL::Vector<RealT> > lp = Teuchos::rcp(new DualConstraintVector(l_rcp,fem)); /*************************************************************************/ /************* INITIALIZE SAMPLE GENERATOR *******************************/ /*************************************************************************/ int dim = 4, nSamp = 1000; std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0; std::vector<std::vector<RealT> > bounds(dim,tmp); Teuchos::RCP<ROL::BatchManager<RealT> > bman = Teuchos::rcp(new L2VectorBatchManager<RealT,int>(comm)); Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler = Teuchos::rcp(new ROL::MonteCarloGenerator<RealT>( nSamp,bounds,bman,false,false,100)); /*************************************************************************/ /************* INITIALIZE RISK-AVERSE OBJECTIVE FUNCTION *****************/ /*************************************************************************/ bool storage = true, fdhess = false; Teuchos::RCP<ROL::Objective<RealT> > robj = Teuchos::rcp(new ROL::Reduced_Objective_SimOpt<RealT>( pobj,pcon,up,zp,lp,gup,gzp,cp,storage,fdhess)); RealT order = 2.0, prob = 0.95; Teuchos::RCP<ROL::Objective<RealT> > obj = Teuchos::rcp(new ROL::HMCRObjective<RealT>( robj,order,prob,sampler,storage)); /*************************************************************************/ /************* CHECK DERIVATIVES AND CONSISTENCY *************************/ /*************************************************************************/ // CHECK OBJECTIVE DERIVATIVES bool derivcheck = false; if (derivcheck) { int nranks = sampler->numBatches(); for (int pid = 0; pid < nranks; pid++) { if ( pid == sampler->batchID() ) { for (int i = sampler->start(); i < sampler->numMySamples(); i++) { *outStream << "Sample " << i << " Rank " << sampler->batchID() << "\n"; *outStream << "(" << sampler->getMyPoint(i)[0] << ", " << sampler->getMyPoint(i)[1] << ", " << sampler->getMyPoint(i)[2] << ", " << sampler->getMyPoint(i)[3] << ")\n"; pcon->setParameter(sampler->getMyPoint(i)); pcon->checkSolve(*up,*zp,*cp,print,*outStream); robj->setParameter(sampler->getMyPoint(i)); *outStream << "\n"; robj->checkGradient(*zp,*gzp,*yzp,print,*outStream); robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream); *outStream << "\n\n"; } } comm->barrier(); } } obj->checkGradient(z,g,y,print0,*outStream0); obj->checkHessVec(z,g,y,print0,*outStream0); /*************************************************************************/ /************* RUN OPTIMIZATION ******************************************/ /*************************************************************************/ // READ IN XML INPUT std::string filename = "input.xml"; Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); // DEFINE ALGORITHM ROL::Algorithm<RealT> algo("Trust Region",*parlist,false); // RUN OPTIMIZATION z.zero(); algo.run(z, g, *obj, print0, *outStream0); /*************************************************************************/ /************* PRINT CONTROL AND STATE TO SCREEN *************************/ /*************************************************************************/ *outStream0 << "\n"; for ( int i = 0; i < nx+2; i++ ) { *outStream0 << std::scientific << std::setprecision(10); *outStream0 << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0); *outStream0 << std::setw(20) << std::left << (*z_rcp)[i]; *outStream0 << "\n"; } *outStream0 << "\n"; *outStream0 << "Scalar Parameter: " << z.getStatistic(0) << "\n"; } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try comm->barrier(); if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
int main( int argc, char* argv[] ) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); return Teuchos::UnitTestRepository::runUnitTestsFromMain(argc, argv); }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int commRank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // Required arguments int numRefs = args.Input<int>("--numRefs", "number of refinement steps"); int norm = args.Input<int>("--norm", "0 = graph\n 1 = robust\n 2 = coupled robust"); // Optional arguments (have defaults) bool enforceLocalConservation = args.Input<bool>("--conserve", "enforce local conservation", false); double Re = args.Input("--Re", "Reynolds number", 40); double nu = 1./Re; double lambda = Re/2.-sqrt(Re*Re/4+4*pi*pi); int maxNewtonIterations = args.Input("--maxIterations", "maximum number of Newton iterations", 20); int polyOrder = args.Input("--polyOrder", "polynomial order for field variables", 2); int deltaP = args.Input("--deltaP", "how much to enrich test space", 2); // string saveFile = args.Input<string>("--meshSaveFile", "file to which to save refinement history", ""); // string replayFile = args.Input<string>("--meshLoadFile", "file with refinement history to replay", ""); args.Process(); // if (commRank==0) // { // cout << "saveFile is " << saveFile << endl; // cout << "loadFile is " << replayFile << endl; // } //////////////////// PROBLEM DEFINITIONS /////////////////////// int H1Order = polyOrder+1; //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; VarPtr tau11 = varFactory.testVar("tau11", HGRAD); VarPtr tau12 = varFactory.testVar("tau12", HGRAD); VarPtr tau22 = varFactory.testVar("tau22", HGRAD); VarPtr v1 = varFactory.testVar("v1", HGRAD); VarPtr v2 = varFactory.testVar("v2", HGRAD); // define trial variables VarPtr u1 = varFactory.fieldVar("u1"); VarPtr u2 = varFactory.fieldVar("u2"); VarPtr sigma11 = varFactory.fieldVar("sigma11"); VarPtr sigma12 = varFactory.fieldVar("sigma12"); VarPtr sigma22 = varFactory.fieldVar("sigma22"); VarPtr u1hat = varFactory.traceVar("u1hat"); VarPtr u2hat = varFactory.traceVar("u2hat"); VarPtr t1hat = varFactory.fluxVar("t1hat"); VarPtr t2hat = varFactory.fluxVar("t2hat"); //////////////////// BUILD MESH /////////////////////// BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // define nodes for mesh FieldContainer<double> meshBoundary(4,2); double xmin = -0.5; double xmax = 1.0; double ymin = -0.5; double ymax = 1.5; meshBoundary(0,0) = xmin; // x1 meshBoundary(0,1) = ymin; // y1 meshBoundary(1,0) = xmax; meshBoundary(1,1) = ymin; meshBoundary(2,0) = xmax; meshBoundary(2,1) = ymax; meshBoundary(3,0) = xmin; meshBoundary(3,1) = ymax; int horizontalCells = 6, verticalCells = 8; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = MeshFactory::buildQuadMesh(meshBoundary, horizontalCells, verticalCells, bf, H1Order, H1Order+deltaP); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr u1_prev = Function::solution(u1, backgroundFlow); FunctionPtr u2_prev = Function::solution(u2, backgroundFlow); // FunctionPtr sigma11_prev = Function::solution(sigma11, backgroundFlow); // FunctionPtr sigma12_prev = Function::solution(sigma12, backgroundFlow); // FunctionPtr sigma22_prev = Function::solution(sigma22, backgroundFlow); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); FunctionPtr one = Teuchos::rcp( new ConstantScalarFunction(1.0) ); FunctionPtr u1Exact = Teuchos::rcp( new ExactU1(lambda) ); FunctionPtr u2Exact = Teuchos::rcp( new ExactU2(lambda) ); // ==================== SET INITIAL GUESS ========================== map<int, Teuchos::RCP<Function> > functionMap; // functionMap[u1->ID()] = u1Exact; // functionMap[u2->ID()] = u2Exact; functionMap[u1->ID()] = zero; functionMap[u2->ID()] = zero; backgroundFlow->projectOntoMesh(functionMap); //////////////////// DEFINE BILINEAR FORM /////////////////////// // stress equation bf->addTerm( 1./nu*sigma11, tau11 ); bf->addTerm( 1./nu*sigma12, tau12 ); bf->addTerm( 1./nu*sigma12, tau12 ); bf->addTerm( 1./nu*sigma22, tau22 ); bf->addTerm( -0.5/nu*sigma11, tau11 ); bf->addTerm( -0.5/nu*sigma22, tau11 ); bf->addTerm( -0.5/nu*sigma11, tau22 ); bf->addTerm( -0.5/nu*sigma22, tau22 ); bf->addTerm( 2*u1, tau11->dx() ); bf->addTerm( 2*u1, tau12->dy() ); bf->addTerm( 2*u2, tau12->dx() ); bf->addTerm( 2*u2, tau22->dy() ); bf->addTerm( -2*u1hat, tau11->times_normal_x() ); bf->addTerm( -2*u1hat, tau12->times_normal_y() ); bf->addTerm( -2*u2hat, tau12->times_normal_x() ); bf->addTerm( -2*u2hat, tau22->times_normal_y() ); // momentum equation bf->addTerm( -2.*u1_prev*u1, v1->dx() ); bf->addTerm( -u2_prev*u1, v1->dy() ); bf->addTerm( -u1_prev*u2, v1->dy() ); bf->addTerm( -u2_prev*u1, v2->dx() ); bf->addTerm( -u1_prev*u2, v2->dx() ); bf->addTerm( -2.*u2_prev*u2, v2->dy() ); bf->addTerm( sigma11, v1->dx() ); bf->addTerm( sigma12, v1->dy() ); bf->addTerm( sigma12, v2->dx() ); bf->addTerm( sigma22, v2->dy() ); bf->addTerm( t1hat, v1); bf->addTerm( t2hat, v2); //////////////////// SPECIFY RHS /////////////////////// RHSPtr rhs = RHS::rhs(); // stress equation rhs->addTerm( -2*u1_prev * tau11->dx() ); rhs->addTerm( -2*u1_prev * tau12->dy() ); rhs->addTerm( -2*u2_prev * tau12->dx() ); rhs->addTerm( -2*u2_prev * tau22->dy() ); // momentum equation rhs->addTerm( u1_prev*u1_prev * v1->dx() ); rhs->addTerm( u2_prev*u1_prev * v1->dy() ); rhs->addTerm( u2_prev*u1_prev * v2->dx() ); rhs->addTerm( u2_prev*u2_prev * v2->dy() ); //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = Teuchos::rcp(new IP); if (norm == 0) { ip = bf->graphNorm(); } else if (norm == 1) { ip->addTerm( 0.5/nu*tau11-0.5/nu*tau22 + v1->dx() ); ip->addTerm( 1./nu*tau12 + v1->dy() ); ip->addTerm( 1./nu*tau12 + v2->dx() ); ip->addTerm( 0.5/nu*tau22-0.5/nu*tau11 + v2->dy() ); ip->addTerm( 2*tau11->dx() + 2*tau12->dy() - 2*u1_prev*v1->dx() - u2_prev*v1->dy() - u2_prev*v2->dx() ); ip->addTerm( 2*tau12->dx() + 2*tau22->dy() - 2*u2_prev*v2->dy() - u1_prev*v1->dy() - u1_prev*v2->dx() ); ip->addTerm( v1 ); ip->addTerm( v2 ); ip->addTerm( tau11 ); ip->addTerm( tau12 ); ip->addTerm( tau12 ); ip->addTerm( tau22 ); } else if (norm == 2) { // ip->addTerm( 0.5/sqrt(nu)*tau11-0.5/nu*tau22 ); // ip->addTerm( 1./sqrt(nu)*tau12 ); // ip->addTerm( 1./sqrt(nu)*tau12 ); // ip->addTerm( 0.5/sqrt(nu)*tau22-0.5/nu*tau11 ); ip->addTerm( tau11 ); ip->addTerm( tau12 ); ip->addTerm( tau12 ); ip->addTerm( tau22 ); ip->addTerm( 2*tau11->dx() + 2*tau12->dy() - 2*u1_prev*v1->dx() - u2_prev*v1->dy() - u2_prev*v2->dx() ); ip->addTerm( 2*tau12->dx() + 2*tau22->dy() - 2*u2_prev*v2->dy() - u1_prev*v1->dy() - u1_prev*v2->dx() ); ip->addTerm( 2*u1_prev*v1->dx() + u2_prev*v1->dy() + u2_prev*v2->dx() ); ip->addTerm( 2*u2_prev*v2->dy() + u1_prev*v1->dy() + u1_prev*v2->dx() ); ip->addTerm( sqrt(nu)*v1->grad() ); ip->addTerm( sqrt(nu)*v2->grad() ); ip->addTerm( v1 ); ip->addTerm( v2 ); } //////////////////// CREATE BCs /////////////////////// BCPtr bc = BC::bc(); // Teuchos::RCP<PenaltyConstraints> pc = Teuchos::rcp( new PenaltyConstraints ); SpatialFilterPtr left = Teuchos::rcp( new ConstantXBoundary(-0.5) ); SpatialFilterPtr right = Teuchos::rcp( new ConstantXBoundary(1) ); SpatialFilterPtr top = Teuchos::rcp( new ConstantYBoundary(-0.5) ); SpatialFilterPtr bottom = Teuchos::rcp( new ConstantYBoundary(1.5) ); bc->addDirichlet(u1hat, left, u1Exact); bc->addDirichlet(u2hat, left, u2Exact); bc->addDirichlet(u1hat, right, u1Exact); bc->addDirichlet(u2hat, right, u2Exact); bc->addDirichlet(u1hat, top, u1Exact); bc->addDirichlet(u2hat, top, u2Exact); bc->addDirichlet(u1hat, bottom, u1Exact); bc->addDirichlet(u2hat, bottom, u2Exact); // bc->addDirichlet(u1hat, left, zero); // bc->addDirichlet(u2hat, left, zero); // bc->addDirichlet(u1hat, right, zero); // bc->addDirichlet(u2hat, right, zero); // bc->addDirichlet(u1hat, top, zero); // bc->addDirichlet(u2hat, top, zero); // bc->addDirichlet(u1hat, bottom, zero); // bc->addDirichlet(u2hat, bottom, zero); // pc->addConstraint(u1hat*u2hat-t1hat == zero, top); // pc->addConstraint(u2hat*u2hat-t2hat == zero, top); Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); // solution->setFilter(pc); // if (enforceLocalConservation) { // solution->lagrangeConstraints()->addConstraint(u1hat->times_normal_x() + u2hat->times_normal_y() == zero); // } // ==================== Register Solutions ========================== mesh->registerSolution(solution); mesh->registerSolution(backgroundFlow); // Teuchos::RCP< RefinementHistory > refHistory = Teuchos::rcp( new RefinementHistory ); // mesh->registerObserver(refHistory); //////////////////// SOLVE & REFINE /////////////////////// double energyThreshold = 0.2; // for mesh refinements RefinementStrategy refinementStrategy( solution, energyThreshold ); HDF5Exporter exporter(mesh, "Kovasznay_np"); ofstream convOut; stringstream convOutFile; convOutFile << "Kovasznay_conv_" << Re <<".txt"; if (commRank == 0) convOut.open(convOutFile.str().c_str()); set<int> nonlinearVars; nonlinearVars.insert(u1->ID()); nonlinearVars.insert(u2->ID()); double nonlinearRelativeEnergyTolerance = 1e-5; // used to determine convergence of the nonlinear solution for (int refIndex=0; refIndex<=numRefs; refIndex++) { double L2Update = 1e10; int iterCount = 0; while (L2Update > nonlinearRelativeEnergyTolerance && iterCount < maxNewtonIterations) { solution->solve(false); double u1L2Update = solution->L2NormOfSolutionGlobal(u1->ID()); double u2L2Update = solution->L2NormOfSolutionGlobal(u2->ID()); L2Update = sqrt(u1L2Update*u1L2Update + u2L2Update*u2L2Update); // Check local conservation if (commRank == 0) { cout << "L2 Norm of Update = " << L2Update << endl; // if (saveFile.length() > 0) { // std::ostringstream oss; // oss << string(saveFile) << refIndex ; // cout << "on refinement " << refIndex << " saving mesh file to " << oss.str() << endl; // refHistory->saveToFile(oss.str()); // } } // line search algorithm double alpha = 1.0; backgroundFlow->addSolution(solution, alpha, nonlinearVars); iterCount++; } exporter.exportSolution(backgroundFlow, varFactory, refIndex, 2, cellIDToSubdivision(mesh, 4)); FunctionPtr u1Soln = Function::solution(u1, backgroundFlow); FunctionPtr u2Soln = Function::solution(u2, backgroundFlow); FunctionPtr u1Sqr = (u1Soln-u1Exact)*(u1Soln-u1Exact); FunctionPtr u2Sqr = (u2Soln-u2Exact)*(u2Soln-u2Exact); double u1L2Error = u1Sqr->integrate(mesh, 1e-5); double u2L2Error = u2Sqr->integrate(mesh, 1e-5); double l2Error = sqrt(u1L2Error+u2L2Error); double energyError = solution->energyErrorTotal(); cout << "L2 Error: " << l2Error << " Energy Error: " << energyError << endl; if (refIndex < numRefs) refinementStrategy.refine(commRank==0); // print to console on commRank 0 } return 0; }
int main(int argc, char *argv[]) { bool verbose = false; // verbosity level. bool success = true; // determine if the run was successfull Teuchos::GlobalMPISession mpiSession(&argc,&argv); Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream(); #ifdef HAVE_MPI MPI_Comm mpiComm = MPI_COMM_WORLD; #endif // HAVE_MPI try { // catch exceptions double omega = 1.0e-2; // Van Der Pol coefficient double x0_1 = 2.0; // ODE initial condition double x0_2 = -0.66; // ODE initial condition double initialTime = 0.0; // ODE initial time double finalTime = 2.0; // ODE final time double minStep = 1.0e-10; // minimum step size double maxStep = 1.0; // maximum step size int degree = 40; // degree of taylor polynomial expansion double tol = 1.0e-10; // local error tolerance int N = 1000; // number of steps to take string outfile_name = "vdp.out"; const int num_methods = 2; const EMethod method_values[] = { METHOD_ERK, METHOD_ETI }; const char * method_names[] = { "ERK", "ETI" }; EMethod method_val = METHOD_ETI; Teuchos::EVerbosityLevel verbLevel = Teuchos::VERB_NONE; // Parse the command-line options: Teuchos::CommandLineProcessor clp(false); // Don't throw exceptions clp.addOutputSetupOptions(true); clp.setOption( "x0_1", &x0_1, "First coordinate of initial condition." ); clp.setOption( "x0_2", &x0_2, "Second coordinate of initial condition." ); clp.setOption( "omega", &omega, "VDP Coefficient" ); clp.setOption( "T_initial", &initialTime, "Initial time for simulation." ); clp.setOption( "T_final", &finalTime, "Final time for simulation." ); clp.setOption( "minStep", &minStep, "Minimum step size." ); clp.setOption( "maxStep", &maxStep, "Maximum step size." ); clp.setOption( "degree", °ree, "Degree of taylor polynomial expansion." ); clp.setOption( "tol", &tol, "Local error tolerance." ); clp.setOption( "numsteps", &N, "Number of integration steps to take" ); clp.setOption( "method", &method_val, num_methods, method_values, method_names, "Integration method" ); clp.setOption( "verbose", "quiet", &verbose, "Set if output is printed or not" ); clp.setOption( "output", &outfile_name, "Output file name." ); setVerbosityLevelOption( "verb-level", &verbLevel, "Overall verbosity level.", &clp ); Teuchos::CommandLineProcessor::EParseCommandLineReturn parse_return = clp.parse(argc,argv); if( parse_return != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) return parse_return; if (minStep > maxStep) { std::cerr << "min step must be smaller than max step" << std::endl; return(1); } if (finalTime <= initialTime) { std::cerr << "Final simulation time must be > initial time." << std::endl; return(1); } // Set up the parameter list for the application: Teuchos::ParameterList params; Teuchos::ParameterList& vdpoParams = params.sublist("Van Der Pol Oscillator Settings"); vdpoParams.set( "implicit", false ); vdpoParams.set( "x0_1", x0_1 ); vdpoParams.set( "x0_2", x0_2 ); vdpoParams.set( "omega", omega ); vdpoParams.set( "Output File Name", outfile_name ); Teuchos::ParameterList& etpParams = params.sublist("Explicit Taylor Polynomial Settings"); etpParams.set( "Initial Time", initialTime ); etpParams.set( "Final Time", finalTime ); etpParams.set( "Local Error Tolerance", tol ); etpParams.set( "Minimum Step Size", minStep ); etpParams.set( "Maximum Step Size", maxStep ); etpParams.set( "Taylor Polynomial Degree", Teuchos::as<unsigned int>(degree) ); #ifdef HAVE_MPI Teuchos::RCP<Epetra_Comm> epetra_comm_ptr_ = Teuchos::rcp( new Epetra_MpiComm(mpiComm) ); #else Teuchos::RCP<Epetra_Comm> epetra_comm_ptr_ = Teuchos::rcp( new Epetra_SerialComm ); #endif // HAVE_MPI // Create the factory for the LinearOpWithSolveBase object Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<double> > W_factory; // create interface to problem Teuchos::RCP<VanDerPolOscillator> epetraModel = Teuchos::rcp(new VanDerPolOscillator(epetra_comm_ptr_, vdpoParams)); Teuchos::RCP<Thyra::ModelEvaluator<double> > model = Teuchos::rcp(new Thyra::EpetraModelEvaluator(epetraModel, W_factory)); Thyra::ModelEvaluatorBase::InArgs<double> model_ic = model->getNominalValues(); // Create Stepper object depending on command-line input std::string method; Teuchos::RCP<Rythmos::StepperBase<double> > stepper_ptr; if ( method_val == METHOD_ERK ) { stepper_ptr = Rythmos::explicitRKStepper<double>(model); Teuchos::RCP<Teuchos::ParameterList> ERKparams = Teuchos::rcp(new Teuchos::ParameterList); ERKparams->sublist("VerboseObject").set( "Verbosity Level", Teuchos::getVerbosityLevelParameterValueName(verbLevel) ); stepper_ptr->setParameterList(ERKparams); method = "Explicit Runge-Kutta of order 4"; } else if (method_val == METHOD_ETI) { method = "Explicit Taylor"; stepper_ptr = Teuchos::rcp(new Rythmos::ExplicitTaylorPolynomialStepper<double>); stepper_ptr->setModel(model); stepper_ptr->setParameterList(Teuchos::rcp(&etpParams,false)); } else { TEUCHOS_TEST_FOR_EXCEPT(true); } Rythmos::StepperBase<double> &stepper = *stepper_ptr; stepper.setInitialCondition(model_ic); double t = initialTime; double dt; int step = 0; if (method_val == METHOD_ETI) { while ( fabs(finalTime-t) > 1.0e-14 ) { if (verbose) cout << "t = " << t << endl; dt = stepper.takeStep(0.0, Rythmos::STEP_TYPE_VARIABLE); t += dt; step++; epetraModel->saveSolution(*get_Epetra_Vector(*(epetraModel->get_x_map()), stepper.getStepStatus().solution),t); } } else { dt = (finalTime-initialTime)/N; // Integrate forward with fixed step sizes: for (int i=1 ; i<=N ; ++i, step++) { if (verbose) cout << "t = " << t << endl; double dt_taken = stepper.takeStep(dt, Rythmos::STEP_TYPE_FIXED); if (dt_taken != dt) { cerr << "Error, stepper took step of dt = " << dt_taken << " when asked to take step of dt = " << dt << std::endl; break; } t += dt_taken; epetraModel->saveSolution(*get_Epetra_Vector(*(epetraModel->get_x_map()), stepper.getStepStatus().solution),t); } } if (verbose) cout << "num steps = " << step << endl; // Get final solution Teuchos::RCP<const Epetra_Vector> final_x = get_Epetra_Vector(*(epetraModel->get_x_map()), stepper.getStepStatus().solution); double final_tol = 1.0e-2; if (std::abs((*final_x)[0]-1.93704) < final_tol && std::abs((*final_x)[1]+0.70225) < final_tol) success = true; else success = false; } // end try catch( const std::exception &excpt ) { std::cerr << "*** Caught a standard exception : " << excpt.what() << std::endl; success = false; } catch( ... ) { std::cerr << "*** Caught an unknown exception!\n"; success = false; } if(success) *out << "\nEnd Result: TEST PASSED" << endl; else *out << "\nEnd Result: TEST FAILED" << endl; return success ? 0 : 1; } // end main() [Doxygen looks for this!]
int main(int argc, char* argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); Teuchos::RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0 && Teuchos::rank<int>(*comm)==0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; try { /**********************************************************************************************/ /************************* CONSTRUCT ROL ALGORITHM ********************************************/ /**********************************************************************************************/ // Get ROL parameterlist std::string filename = "input.xml"; Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() ); Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() ); // Build ROL algorithm parlist->sublist("Status Test").set("Gradient Tolerance",1.e-8); parlist->sublist("Status Test").set("Step Tolerance",1.e-14); parlist->sublist("Status Test").set("Iteration Limit",100); /**********************************************************************************************/ /************************* CONSTRUCT VECTORS **************************************************/ /**********************************************************************************************/ // Build control vectors int nx = 256; Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT>(nx+2,0.0) ); ROL::StdVector<RealT> x(x_rcp); Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT>(nx+2,0.0) ); ROL::StdVector<RealT> d(d_rcp); for ( int i = 0; i < nx+2; i++ ) { (*x_rcp)[i] = random<RealT>(comm); (*d_rcp)[i] = random<RealT>(comm); } // Build state and adjoint vectors Teuchos::RCP<std::vector<RealT> > u_rcp = Teuchos::rcp( new std::vector<RealT>(nx,1.0) ); ROL::StdVector<RealT> u(u_rcp); Teuchos::RCP<std::vector<RealT> > p_rcp = Teuchos::rcp( new std::vector<RealT>(nx,0.0) ); ROL::StdVector<RealT> p(p_rcp); Teuchos::RCP<ROL::Vector<RealT> > up = Teuchos::rcp(&u,false); Teuchos::RCP<ROL::Vector<RealT> > pp = Teuchos::rcp(&p,false); /**********************************************************************************************/ /************************* CONSTRUCT SOL COMPONENTS *******************************************/ /**********************************************************************************************/ // Build samplers // int dim = 4; // int nSamp = 100; // std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0; // std::vector<std::vector<RealT> > bounds(dim,tmp); // Teuchos::RCP<ROL::BatchManager<RealT> > bman // = Teuchos::rcp(new ROL::StdTeuchosBatchManager<RealT,int>(comm)); // Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler // = Teuchos::rcp(new ROL::MonteCarloGenerator<RealT>(nSamp,bounds,bman,false,false,100)); ROL::QuadratureInfo info; info.dim = 4; info.maxLevel = 7; info.rule1D.clear(); info.rule1D.resize(info.dim,ROL::QUAD_CLENSHAWCURTIS); info.growth1D.clear(); info.growth1D.resize(info.dim,ROL::GROWTH_DEFAULT); info.normalized = true; info.adaptive = false; Teuchos::RCP<ROL::BatchManager<RealT> > bman = Teuchos::rcp(new ROL::StdTeuchosBatchManager<RealT,int>(comm)); Teuchos::RCP<ROL::SampleGenerator<RealT> > sampler = Teuchos::rcp(new ROL::SparseGridGenerator<RealT>(bman,info)); // Print samples int width = 21; std::stringstream name; name << "samples_" << sampler->batchID() << ".txt"; std::ofstream file(name.str().c_str()); if (file.is_open()) { file << std::scientific << std::setprecision(12); for (int i = 0; i < sampler->numMySamples(); ++i) { std::vector<RealT> pt = sampler->getMyPoint(i); RealT wt = sampler->getMyWeight(i); for (int j = 0; j < static_cast<int>(pt.size()); ++j) { file << std::setw(width) << std::left << pt[j]; } file << std::setw(width) << std::left << wt << std::endl; } file.close(); } else { TEUCHOS_TEST_FOR_EXCEPTION(true, std::invalid_argument, ">>> (adapters/trikota/sol/test/test_01): Unable to open file!"); } /**********************************************************************************************/ /************************* CONSTRUCT OBJECTIVE FUNCTION ***************************************/ /**********************************************************************************************/ // Build risk-averse objective function RealT alpha = 1.e-3; Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<RealT> > pobjSimOpt = Teuchos::rcp(new Objective_BurgersControl<RealT>(alpha,nx)); Teuchos::RCP<ROL::ParametrizedEqualityConstraint_SimOpt<RealT> > pconSimOpt = Teuchos::rcp(new EqualityConstraint_BurgersControl<RealT>(nx)); Teuchos::RCP<ROL::ParametrizedObjective<RealT> > pObj = Teuchos::rcp(new ROL::Reduced_ParametrizedObjective_SimOpt<RealT>(pobjSimOpt,pconSimOpt,up,pp)); Teuchos::RCP<ROL::Objective<RealT> > obj; // Test parametrized objective functions *outStream << "Check Derivatives of Parametrized Objective Function\n"; pObj->setParameter(sampler->getMyPoint(0)); pObj->checkGradient(x,d,true,*outStream); pObj->checkHessVec(x,d,true,*outStream); /**********************************************************************************************/ /************************* RISK NEUTRAL *******************************************************/ /**********************************************************************************************/ *outStream << "\nSOLVE RISK NEUTRAL OPTIMAL CONTROL PROBLEM WITH TRUST REGION\n"; // Build CVaR objective function Teuchos::ParameterList list; list.sublist("SOL").set("Stochastic Optimization Type","Risk Neutral"); list.sublist("SOL").set("Store Sampled Value and Gradient",true); // Build stochastic problem Teuchos::RCP<ROL::Vector<RealT> > xp = Teuchos::rcp(&x,false); ROL::StochasticProblem<RealT> optProb(list,pObj,sampler,xp); optProb.checkObjectiveGradient(d,true,*outStream); optProb.checkObjectiveHessVec(d,true,*outStream); // Run ROL algorithm ROL::Algorithm<RealT> algo("Trust Region",*parlist,false); clock_t start = clock(); xp->zero(); algo.run(optProb,true,*outStream); *outStream << "Optimization time: " << (RealT)(clock()-start)/(RealT)CLOCKS_PER_SEC << " seconds.\n"; } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
int main(int argc, char *argv[]) { Teuchos::GlobalMPISession mpiSession(&argc, &argv); // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided. int iprint = argc - 1; Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (iprint > 0) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; // *** Example body. try { Teuchos::RCP<ROL::Objective<RealT> > obj; Teuchos::RCP<ROL::EqualityConstraint<RealT> > constr; Teuchos::RCP<std::vector<RealT> > x_rcp = Teuchos::rcp( new std::vector<RealT> (0, 0.0) ); Teuchos::RCP<std::vector<RealT> > sol_rcp = Teuchos::rcp( new std::vector<RealT> (0, 0.0) ); ROL::StdVector<RealT> x(x_rcp); // Iteration vector. ROL::StdVector<RealT> sol(sol_rcp); // Reference solution vector. // Retrieve objective, constraint, iteration vector, solution vector. ROL::ZOO::getSimpleEqConstrained <RealT, ROL::StdVector<RealT>, ROL::StdVector<RealT>, ROL::StdVector<RealT>, ROL::StdVector<RealT> > (obj, constr, x, sol); Teuchos::ParameterList parlist; // Define Step parlist.set("Nominal SQP Optimality Solver Tolerance", 1.e-2); ROL::CompositeStepSQP<RealT> step(parlist); // Run derivative checks, etc. int dim = 5; int nc = 3; RealT left = -1e0, right = 1e0; Teuchos::RCP<std::vector<RealT> > xtest_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) ); Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) ); Teuchos::RCP<std::vector<RealT> > d_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) ); Teuchos::RCP<std::vector<RealT> > v_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) ); Teuchos::RCP<std::vector<RealT> > vc_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) ); Teuchos::RCP<std::vector<RealT> > vl_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) ); ROL::StdVector<RealT> xtest(xtest_rcp); ROL::StdVector<RealT> g(g_rcp); ROL::StdVector<RealT> d(d_rcp); ROL::StdVector<RealT> v(v_rcp); ROL::StdVector<RealT> vc(vc_rcp); ROL::StdVector<RealT> vl(vl_rcp); // set xtest, d, v for (int i=0; i<dim; i++) { (*xtest_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; (*d_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; (*v_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; } // set vc, vl for (int i=0; i<nc; i++) { (*vc_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; (*vl_rcp)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left; } obj->checkGradient(xtest, d, true, *outStream); *outStream << "\n"; obj->checkHessVec(xtest, v, true, *outStream); *outStream << "\n"; obj->checkHessSym(xtest, d, v, true, *outStream); *outStream << "\n"; constr->checkApplyJacobian(xtest, v, vc, true, *outStream); *outStream << "\n"; constr->checkApplyAdjointJacobian(xtest, vl, vc, xtest, true, *outStream); *outStream << "\n"; constr->checkApplyAdjointHessian(xtest, vl, d, xtest, true, *outStream); *outStream << "\n"; Teuchos::RCP<std::vector<RealT> > v1_rcp = Teuchos::rcp( new std::vector<RealT> (dim, 0.0) ); Teuchos::RCP<std::vector<RealT> > v2_rcp = Teuchos::rcp( new std::vector<RealT> (nc, 0.0) ); ROL::StdVector<RealT> v1(v1_rcp); ROL::StdVector<RealT> v2(v2_rcp); RealT augtol = 1e-8; constr->solveAugmentedSystem(v1, v2, d, vc, xtest, augtol); // Define Status Test RealT gtol = 1e-12; // norm of gradient tolerance RealT ctol = 1e-12; // norm of constraint tolerance RealT stol = 1e-18; // norm of step tolerance int maxit = 1000; // maximum number of iterations ROL::StatusTestSQP<RealT> status(gtol, ctol, stol, maxit); // Define Algorithm ROL::DefaultAlgorithm<RealT> algo(step, status, false); // Run Algorithm vl.zero(); //(*x_rcp)[0] = 3.0; (*x_rcp)[1] = 2.0; (*x_rcp)[2] = 2.0; (*x_rcp)[3] = 1.0; (*x_rcp)[4] = 1.0; //(*x_rcp)[0] = -5.0; (*x_rcp)[1] = -5.0; (*x_rcp)[2] = -5.0; (*x_rcp)[3] = -6.0; (*x_rcp)[4] = -6.0; std::vector<std::string> output = algo.run(x, g, vl, vc, *obj, *constr, false); for ( unsigned i = 0; i < output.size(); i++ ) { *outStream << output[i]; } // Compute Error *outStream << "\nReference solution x_r =\n"; *outStream << std::scientific << " " << (*sol_rcp)[0] << "\n"; *outStream << std::scientific << " " << (*sol_rcp)[1] << "\n"; *outStream << std::scientific << " " << (*sol_rcp)[2] << "\n"; *outStream << std::scientific << " " << (*sol_rcp)[3] << "\n"; *outStream << std::scientific << " " << (*sol_rcp)[4] << "\n"; *outStream << "\nOptimal solution x =\n"; *outStream << std::scientific << " " << (*x_rcp)[0] << "\n"; *outStream << std::scientific << " " << (*x_rcp)[1] << "\n"; *outStream << std::scientific << " " << (*x_rcp)[2] << "\n"; *outStream << std::scientific << " " << (*x_rcp)[3] << "\n"; *outStream << std::scientific << " " << (*x_rcp)[4] << "\n"; x.axpy(-1.0, sol); RealT abserr = x.norm(); RealT relerr = abserr/sol.norm(); *outStream << std::scientific << "\n Absolute Error: " << abserr; *outStream << std::scientific << "\n Relative Error: " << relerr << "\n"; if ( relerr > sqrt(ROL::ROL_EPSILON) ) { errorFlag += 1; } } catch (std::logic_error err) { *outStream << err.what() << "\n"; errorFlag = -1000; }; // end try if (errorFlag != 0) std::cout << "End Result: TEST FAILED\n"; else std::cout << "End Result: TEST PASSED\n"; return 0; }
int main(int argc, char *argv[]) { Teuchos::oblackholestream blackhole; Teuchos::GlobalMPISession mpiSession(&argc,&argv,&blackhole); RCP<const Teuchos::Comm<int> > comm = Teuchos::DefaultComm<int>::getComm(); /**********************************************************************************/ /* SET TEST PARAMETERS */ /**********************************************************************************/ // Note: use --help to list available options. Teuchos::CommandLineProcessor clp(false); // Default is Laplace1D with nx = 8748. // It's a nice size for 1D and perfect aggregation. (6561=3^8) //Nice size for 1D and perfect aggregation on small numbers of processors. (8748=4*3^7) Galeri::Xpetra::Parameters<GO> matrixParameters(clp, 8748); // manage parameters of the test case Xpetra::Parameters xpetraParameters(clp); // manage parameters of xpetra // custom parameters LO maxLevels = 1; LO its=2; std::string coarseSolver="amesos2"; clp.setOption("maxLevels",&maxLevels,"maximum number of levels allowed"); clp.setOption("its",&its,"number of multigrid cycles"); clp.setOption("coarseSolver",&coarseSolver,"amesos2 or ifpack2 (Tpetra specific. Ignored for Epetra)"); 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; } matrixParameters.check(); xpetraParameters.check(); // TODO: check custom parameters if (comm->getRank() == 0) { matrixParameters.print(); xpetraParameters.print(); // TODO: print custom parameters } #ifdef FOR_PARALLEL_DEBUGGING //Utils::BreakForDebugger(*comm); LO mypid = comm->getRank(); if (mypid == 0) std::cout << "Host and Process Ids for tasks" << std::endl; for (LO i = 0; i <comm->getSize(); i++) { if (i == mypid ) { char buf[80]; char hostname[80]; gethostname(hostname, sizeof(hostname)); LO pid = getpid(); sprintf(buf, "Host: %s\tMPI rank: %d,\tPID: %d\n\tattach %d\n\tcontinue\n", hostname, mypid, pid, pid); printf("%s\n",buf); fflush(stdout); sleep(1); } } if (mypid == 0) { printf( "** Enter a character to continue > "); fflush(stdout); char go = ' '; scanf("%c",&go); } comm->barrier(); #endif /**********************************************************************************/ /* CREATE INITIAL MATRIX */ /**********************************************************************************/ const RCP<const Map> map = MapFactory::Build(xpetraParameters.GetLib(), matrixParameters.GetNumGlobalElements(), 0, comm); RCP<Galeri::Xpetra::Problem<Map,CrsMatrixWrap,MultiVector> > Pr = Galeri::Xpetra::BuildProblem<SC, LO, GO, Map, CrsMatrixWrap, MultiVector>(matrixParameters.GetMatrixType(), map, matrixParameters.GetParameterList()); //TODO: Matrix vs. CrsMatrixWrap RCP<Matrix> Op = Pr->BuildMatrix(); /**********************************************************************************/ /* */ /**********************************************************************************/ // dump matrix to file //std::string fileName = "Amat.mm"; //Utils::Write(fileName,Op); RCP<MultiVector> nullSpace = MultiVectorFactory::Build(map,1); nullSpace->putScalar( (SC) 1.0); Teuchos::Array<ST::magnitudeType> norms(1); nullSpace->norm1(norms); if (comm->getRank() == 0) std::cout << "||NS|| = " << norms[0] << std::endl; RCP<MueLu::Hierarchy<SC,LO,GO,NO,LMO> > H = rcp( new Hierarchy() ); H->setDefaultVerbLevel(Teuchos::VERB_HIGH); RCP<MueLu::Level> Finest = rcp( new MueLu::Level() ); Finest->setDefaultVerbLevel(Teuchos::VERB_HIGH); Finest->Set("A",Op); Finest->Set("Nullspace",nullSpace); Finest->Request("Nullspace"); //FIXME putting this in to avoid error until Merge needs business //FIXME is implemented Finest->Set("NullSpace",nullSpace); H->SetLevel(Finest); RCP<CoupledAggregationFactory> CoupledAggFact = rcp(new CoupledAggregationFactory()); CoupledAggFact->SetMinNodesPerAggregate(3); CoupledAggFact->SetMaxNeighAlreadySelected(0); CoupledAggFact->SetOrdering(MueLu::AggOptions::NATURAL); CoupledAggFact->SetPhase3AggCreation(0.5); RCP<TentativePFactory> TentPFact = rcp(new TentativePFactory(CoupledAggFact)); RCP<SaPFactory> Pfact = rcp( new SaPFactory(TentPFact) ); //Pfact->SetDampingFactor(0.); RCP<Factory> Rfact = rcp( new TransPFactory() ); RCP<GenericPRFactory> PRfact = rcp( new GenericPRFactory(Pfact,Rfact)); RCP<RAPFactory> Acfact = rcp( new RAPFactory() ); RCP<SmootherPrototype> smooProto; Teuchos::ParameterList ifpackList; ifpackList.set("relaxation: sweeps", (LO) 1); ifpackList.set("relaxation: damping factor", (SC) 1.0); /* ifpackList.set("type", "Chebyshev"); ifpackList.set("chebyshev: degree", (int) 1); ifpackList.set("chebyshev: max eigenvalue", (double) 2.0); ifpackList.set("chebyshev: min eigenvalue", (double) 1.0); ifpackList.set("chebyshev: zero starting solution", false); */ if (xpetraParameters.GetLib() == Xpetra::UseEpetra) { #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_IFPACK) ifpackList.set("relaxation: type", "symmetric Gauss-Seidel"); smooProto = rcp( new IfpackSmoother("point relaxation stand-alone",ifpackList) ); #endif } else if (xpetraParameters.GetLib() == Xpetra::UseTpetra) { #if defined(HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_IFPACK2) ifpackList.set("relaxation: type", "Symmetric Gauss-Seidel"); smooProto = rcp( new Ifpack2Smoother("RELAXATION",ifpackList) ); #endif } if (smooProto == Teuchos::null) { throw(MueLu::Exceptions::RuntimeError("main: smoother error")); } RCP<SmootherFactory> SmooFact = rcp( new SmootherFactory(smooProto) ); Acfact->setVerbLevel(Teuchos::VERB_HIGH); Teuchos::ParameterList status; status = H->FullPopulate(PRfact,Acfact,SmooFact,0,maxLevels); //RCP<MueLu::Level> coarseLevel = H.GetLevel(1); //RCP<Matrix> P = coarseLevel->template Get< RCP<Matrix> >("P"); //fileName = "Pfinal.mm"; //Utils::Write(fileName,P); if (comm->getRank() == 0) { std::cout << "======================\n Multigrid statistics \n======================" << std::endl; status.print(std::cout,Teuchos::ParameterList::PrintOptions().indent(2)); } //FIXME we should be able to just call smoother->SetNIts(50) ... but right now an exception gets thrown RCP<SmootherPrototype> coarseProto; if (xpetraParameters.GetLib() == Xpetra::UseEpetra) { #if defined(HAVE_MUELU_EPETRA) && defined(HAVE_MUELU_AMESOS) if (comm->getRank() == 0) std::cout << "CoarseGrid: AMESOS" << std::endl; Teuchos::ParameterList amesosList; amesosList.set("PrintTiming",true); coarseProto = rcp( new AmesosSmoother("Amesos_Klu",amesosList) ); //#elif #endif } else if (xpetraParameters.GetLib() == Xpetra::UseTpetra) { if (coarseSolver=="amesos2") { #if defined(HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_AMESOS2) if (comm->getRank() == 0) std::cout << "CoarseGrid: AMESOS2" << std::endl; Teuchos::ParameterList paramList; //unused coarseProto = rcp( new Amesos2Smoother("Superlu", paramList) ); #else std::cout << "AMESOS2 not available (try --coarseSolver=ifpack2)" << std::endl; return EXIT_FAILURE; #endif // HAVE_MUELU_TPETRA && HAVE_MUELU_AMESOS2 } else if(coarseSolver=="ifpack2") { #if defined(HAVE_MUELU_TPETRA) && defined(HAVE_MUELU_IFPACK2) if (comm->getRank() == 0) std::cout << "CoarseGrid: IFPACK2" << std::endl; Teuchos::ParameterList ifpack2List; ifpack2List.set("fact: ilut level-of-fill",99); // TODO ?? ifpack2List.set("fact: drop tolerance", 0); ifpack2List.set("fact: absolute threshold", 0); ifpack2List.set("fact: relative threshold", 0); coarseProto = rcp( new Ifpack2Smoother("ILUT",ifpack2List) ); #else std::cout << "IFPACK2 not available (try --coarseSolver=amesos2)" << std::endl; return EXIT_FAILURE; #endif } else { std::cout << "Unknow coarse grid solver (try --coarseSolver=ifpack2 or --coarseSolver=amesos2)" << std::endl; return EXIT_FAILURE; } } if (coarseProto == Teuchos::null) { throw(MueLu::Exceptions::RuntimeError("main: coarse smoother error")); } SmootherFactory coarseSolveFact(coarseProto); H->SetCoarsestSolver(coarseSolveFact,MueLu::PRE); // Define RHS RCP<MultiVector> X = MultiVectorFactory::Build(map,1); RCP<MultiVector> RHS = MultiVectorFactory::Build(map,1); X->setSeed(846930886); X->randomize(); X->norm2(norms); if (comm->getRank() == 0) std::cout << "||X_true|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; Op->apply(*X,*RHS,Teuchos::NO_TRANS,(SC)1.0,(SC)0.0); // Use AMG directly as an iterative method { X->putScalar( (SC) 0.0); H->PrintResidualHistory(true); H->Iterate(*RHS,its,*X); X->norm2(norms); if (comm->getRank() == 0) std::cout << "||X_" << std::setprecision(2) << its << "|| = " << std::setiosflags(std::ios::fixed) << std::setprecision(10) << norms[0] << std::endl; } return EXIT_SUCCESS; }
int main(int argc, char *argv[]) { #ifdef ENABLE_INTEL_FLOATING_POINT_EXCEPTIONS cout << "NOTE: enabling floating point exceptions for divide by zero.\n"; _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID); #endif Teuchos::GlobalMPISession mpiSession(&argc, &argv); int rank = Teuchos::GlobalMPISession::getRank(); Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options bool useCondensedSolve = false; // condensed solve not yet compatible with minimum rule meshes int numGridPoints = 32; // in x,y -- idea is to keep the overall order of approximation constant int k = 4; // poly order for u double theta = 0.5; int numTimeSteps = 2000; int numCells = -1; // in x, y (-1 so we can set a default if unset from the command line.) int numFrames = 50; int delta_k = 2; // test space enrichment: should be 2 for 2D bool useMumpsIfAvailable = true; bool convertSolutionsToVTK = false; // when true assumes we've already run with precisely the same options, except without VTK support (so we have a bunch of .soln files) bool usePeriodicBCs = false; bool useConstantConvection = false; cmdp.setOption("polyOrder",&k,"polynomial order for field variable u"); cmdp.setOption("delta_k", &delta_k, "test space polynomial order enrichment"); cmdp.setOption("numCells",&numCells,"number of cells in x and y directions"); cmdp.setOption("theta",&theta,"theta weight for time-stepping"); cmdp.setOption("numTimeSteps",&numTimeSteps,"number of time steps"); cmdp.setOption("numFrames",&numFrames,"number of frames for export"); cmdp.setOption("usePeriodicBCs", "useDirichletBCs", &usePeriodicBCs); cmdp.setOption("useConstantConvection", "useVariableConvection", &useConstantConvection); cmdp.setOption("useCondensedSolve", "useUncondensedSolve", &useCondensedSolve, "use static condensation to reduce the size of the global solve"); cmdp.setOption("useMumps", "useKLU", &useMumpsIfAvailable, "use MUMPS (if available)"); cmdp.setOption("convertPreComputedSolutionsToVTK", "computeSolutions", &convertSolutionsToVTK); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } bool saveSolutionFiles = true; if (numCells==-1) numCells = numGridPoints / k; if (rank==0) { cout << "solving on " << numCells << " x " << numCells << " mesh " << "of order " << k << ".\n"; } set<int> timeStepsToExport; timeStepsToExport.insert(numTimeSteps); int timeStepsPerFrame = numTimeSteps / (numFrames - 1); if (timeStepsPerFrame==0) timeStepsPerFrame = 1; for (int n=0; n<numTimeSteps; n += timeStepsPerFrame) { timeStepsToExport.insert(n); } int H1Order = k + 1; const static double PI = 3.141592653589793238462; double dt = 2 * PI / numTimeSteps; VarFactory varFactory; // traces: VarPtr qHat = varFactory.fluxVar("\\widehat{q}"); // fields: VarPtr u = varFactory.fieldVar("u", L2); // test functions: VarPtr v = varFactory.testVar("v", HGRAD); FunctionPtr x = Function::xn(1); FunctionPtr y = Function::yn(1); FunctionPtr c; if (useConstantConvection) { c = Function::vectorize(Function::constant(0.5), Function::constant(0.5)); } else { c = Function::vectorize(y-0.5, 0.5-x); } // FunctionPtr c = Function::vectorize(y, x); FunctionPtr n = Function::normal(); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); bf->addTerm(u / dt, v); bf->addTerm(- theta * u, c * v->grad()); // bf->addTerm(theta * u_hat, (c * n) * v); bf->addTerm(qHat, v); double width = 2.0, height = 2.0; int horizontalCells = numCells, verticalCells = numCells; double x0 = -0.5; double y0 = -0.5; if (usePeriodicBCs) { x0 = 0.0; y0 = 0.0; width = 1.0; height = 1.0; } BCPtr bc = BC::bc(); SpatialFilterPtr inflowFilter = Teuchos::rcp( new InflowFilterForClockwisePlanarRotation (x0,x0+width,y0,y0+height,0.5,0.5)); vector< PeriodicBCPtr > periodicBCs; if (! usePeriodicBCs) { // bc->addDirichlet(u_hat, SpatialFilter::allSpace(), Function::zero()); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. } else { periodicBCs.push_back(PeriodicBC::xIdentification(x0, x0+width)); periodicBCs.push_back(PeriodicBC::yIdentification(y0, y0+height)); } MeshPtr mesh = MeshFactory::quadMeshMinRule(bf, H1Order, delta_k, width, height, horizontalCells, verticalCells, false, x0, y0, periodicBCs); FunctionPtr u0 = Teuchos::rcp( new Cone_U0(0.0, 0.25, 0.1, 1.0, usePeriodicBCs) ); RHSPtr initialRHS = RHS::rhs(); initialRHS->addTerm(u0 / dt * v); initialRHS->addTerm((1-theta) * u0 * c * v->grad()); IPPtr ip; // ip = Teuchos::rcp( new IP ); // ip->addTerm(v); // ip->addTerm(c * v->grad()); ip = bf->graphNorm(); // create two Solution objects; we'll switch between these for time steps SolutionPtr soln0 = Solution::solution(mesh, bc, initialRHS, ip); soln0->setCubatureEnrichmentDegree(5); FunctionPtr u_soln0 = Function::solution(u, soln0); FunctionPtr qHat_soln0 = Function::solution(qHat, soln0); RHSPtr rhs1 = RHS::rhs(); rhs1->addTerm(u_soln0 / dt * v); rhs1->addTerm((1-theta) * u_soln0 * c * v->grad()); SolutionPtr soln1 = Solution::solution(mesh, bc, rhs1, ip); soln1->setCubatureEnrichmentDegree(5); FunctionPtr u_soln1 = Function::solution(u, soln1); FunctionPtr qHat_soln1 = Function::solution(qHat, soln1); RHSPtr rhs2 = RHS::rhs(); // after the first solve on soln0, we'll swap out initialRHS for rhs2 rhs2->addTerm(u_soln1 / dt * v); rhs2->addTerm((1-theta) * u_soln1 * c * v->grad()); Teuchos::RCP<Solver> solver = Teuchos::rcp( new KluSolver ); #ifdef HAVE_AMESOS_MUMPS if (useMumpsIfAvailable) solver = Teuchos::rcp( new MumpsSolver ); #endif // double energyErrorSum = 0; ostringstream filePrefix; filePrefix << "convectingCone_k" << k << "_t"; int frameNumber = 0; #ifdef USE_HDF5 ostringstream dir_name; dir_name << "convectingCone_k" << k; HDF5Exporter exporter(mesh,dir_name.str()); #endif #ifdef USE_VTK VTKExporter soln0Exporter(soln0,mesh,varFactory); VTKExporter soln1Exporter(soln1,mesh,varFactory); #endif if (convertSolutionsToVTK) { #ifdef USE_VTK if (rank==0) { cout << "Converting .soln files to VTK.\n"; for (int frameNumber=0; frameNumber<=numFrames; frameNumber++) { ostringstream filename; filename << filePrefix.str() << frameNumber << ".soln"; soln0->readFromFile(filename.str()); filename.str(""); filename << filePrefix.str() << frameNumber; soln0Exporter.exportFields(filename.str()); } } #else if (rank==0) cout << "Driver was built without USE_VTK defined. This must be defined to convert solution files to VTK files.\n"; #endif exit(0); } if (timeStepsToExport.find(0) != timeStepsToExport.end()) { map<int,FunctionPtr> solnMap; solnMap[u->ID()] = u0; // project field variables if (rank==0) cout << "About to project initial solution onto mesh.\n"; soln0->projectOntoMesh(solnMap); if (rank==0) cout << "...projected initial solution onto mesh.\n"; ostringstream filename; filename << filePrefix.str() << frameNumber++; if (rank==0) cout << "About to export initial solution.\n"; #ifdef USE_VTK if (rank==0) soln0Exporter.exportFields(filename.str()); #endif #ifdef USE_HDF5 exporter.exportSolution(soln0, varFactory,0); #endif if (saveSolutionFiles) { if (rank==0) { filename << ".soln"; soln0->writeToFile(filename.str()); cout << endl << "wrote " << filename.str() << endl; } } if (rank==0) cout << "...exported initial solution.\n"; } if (rank==0) cout << "About to solve initial time step.\n"; // first time step: soln0->setReportTimingResults(true); // added to gain insight into why MPI blocks in some cases on the server... if (useCondensedSolve) soln0->condensedSolve(solver); else soln0->solve(solver); soln0->setReportTimingResults(false); // energyErrorSum += soln0->energyErrorTotal(); soln0->setRHS(rhs2); if (rank==0) cout << "Solved initial time step.\n"; if (timeStepsToExport.find(1) != timeStepsToExport.end()) { ostringstream filename; filename << filePrefix.str() << frameNumber++; #ifdef USE_VTK if (rank==0) soln0Exporter.exportFields(filename.str()); #endif #ifdef USE_HDF5 exporter.exportSolution(soln0, varFactory); #endif if (saveSolutionFiles) { if (rank==0) { filename << ".soln"; soln0->writeToFile(filename.str()); cout << endl << "wrote " << filename.str() << endl; } } } bool reportTimings = false; for (int n=1; n<numTimeSteps; n++) { bool odd = (n%2)==1; SolutionPtr soln_n = odd ? soln1 : soln0; if (useCondensedSolve) soln_n->solve(solver); else soln_n->solve(solver); if (reportTimings) { if (rank==0) cout << "time step " << n << ", timing report:\n"; soln_n->reportTimings(); } if (rank==0) { cout << "\x1B[2K"; // Erase the entire current line. cout << "\x1B[0E"; // Move to the beginning of the current line. cout << "Solved time step: " << n; flush(cout); } if (timeStepsToExport.find(n+1)!=timeStepsToExport.end()) { ostringstream filename; filename << filePrefix.str() << frameNumber++; #ifdef USE_VTK if (rank==0) { if (odd) { soln1Exporter.exportFields(filename.str()); } else { soln0Exporter.exportFields(filename.str()); } } #endif #ifdef USE_HDF5 double t = n * dt; if (odd) { exporter.exportSolution(soln1, varFactory, t); } else { exporter.exportSolution(soln0, varFactory, t); } #endif if (saveSolutionFiles) { if (rank==0) { filename << ".soln"; if (odd) { soln1->writeToFile(filename.str()); } else { soln0->writeToFile(filename.str()); } cout << endl << "wrote " << filename.str() << endl; } } } // energyErrorSum += soln_n->energyErrorTotal(); } // if (rank==0) cout << "energy error, sum over all time steps: " << energyErrorSum << endl; return 0; }
int main(int argc, char* argv[]) { // // Get a default output stream from the Teuchos::VerboseObjectBase // Teuchos::RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream(); Teuchos::GlobalMPISession mpiSession(&argc,&argv); #ifdef HAVE_COMPLEX typedef std::complex<double> ST; // Scalar-type typedef #elif HAVE_COMPLEX_H typedef std::complex<double> ST; // Scalar-type typedef #else typedef double ST; // Scalar-type typedef #endif typedef Teuchos::ScalarTraits<ST>::magnitudeType MT; // Magnitude-type typedef typedef int OT; // Ordinal-type typedef ST one = Teuchos::ScalarTraits<ST>::one(); ST zero = Teuchos::ScalarTraits<ST>::zero(); #ifdef HAVE_MPI MPI_Comm mpiComm = MPI_COMM_WORLD; const Tpetra::MpiPlatform<OT,OT> ordinalPlatform(mpiComm); const Tpetra::MpiPlatform<OT,ST> scalarPlatform(mpiComm); #else const Tpetra::SerialPlatform<OT,OT> ordinalPlatform; const Tpetra::SerialPlatform<OT,ST> scalarPlatform; #endif // // Get the data from the HB file // // Name of input matrix file std::string matrixFile = "mhd1280b.cua"; int info=0; int dim,dim2,nnz; MT *dvals; int *colptr,*rowind; ST *cvals; nnz = -1; info = readHB_newmat_double(matrixFile.c_str(),&dim,&dim2,&nnz, &colptr,&rowind,&dvals); if (info == 0 || nnz < 0) { *out << "Error reading '" << matrixFile << "'" << std::endl; } #ifdef HAVE_MPI MPI_Finalize(); #endif // Convert interleaved doubles to std::complex values cvals = new ST[nnz]; for (int ii=0; ii<nnz; ii++) { cvals[ii] = ST(dvals[ii*2],dvals[ii*2+1]); } // Declare global dimension of the linear operator OT globalDim = dim; // Create the element space and std::vector space const Tpetra::ElementSpace<OT> elementSpace(globalDim,0,ordinalPlatform); const Tpetra::VectorSpace<OT,ST> vectorSpace(elementSpace,scalarPlatform); // Create my implementation of a Tpetra::Operator RCP<Tpetra::Operator<OT,ST> > tpetra_A = rcp( new MyOperator<OT,ST>(vectorSpace,dim,colptr,nnz,rowind,cvals) ); // Create a Thyra linear operator (A) using the Tpetra::CisMatrix (tpetra_A). RCP<Thyra::LinearOpBase<ST> > A = Teuchos::rcp( new Thyra::TpetraLinearOp<OT,ST>(tpetra_A) ); // // Set the parameters for the Belos LOWS Factory and create a parameter list. // int blockSize = 1; int maxIterations = globalDim; int maxRestarts = 15; int gmresKrylovLength = 50; int outputFrequency = 100; bool outputMaxResOnly = true; MT maxResid = 1e-5; Teuchos::RCP<Teuchos::ParameterList> belosLOWSFPL = Teuchos::rcp( new Teuchos::ParameterList() ); belosLOWSFPL->set("Solver Type","Block GMRES"); Teuchos::ParameterList& belosLOWSFPL_solver = belosLOWSFPL->sublist("Solver Types"); Teuchos::ParameterList& belosLOWSFPL_gmres = belosLOWSFPL_solver.sublist("Block GMRES"); belosLOWSFPL_gmres.set("Maximum Iterations",int(maxIterations)); belosLOWSFPL_gmres.set("Convergence Tolerance",MT(maxResid)); belosLOWSFPL_gmres.set("Maximum Restarts",int(maxRestarts)); belosLOWSFPL_gmres.set("Block Size",int(blockSize)); belosLOWSFPL_gmres.set("Num Blocks",int(gmresKrylovLength)); belosLOWSFPL_gmres.set("Output Frequency",int(outputFrequency)); belosLOWSFPL_gmres.set("Show Maximum Residual Norm Only",bool(outputMaxResOnly)); // Whether the linear solver succeeded. // (this will be set during the residual check at the end) bool success = true; // Number of random right-hand sides we will be solving for. int numRhs = 1; // Get the domain space for the Thyra linear operator Teuchos::RCP<const Thyra::VectorSpaceBase<ST> > domain = A->domain(); // Create the Belos LOWS factory. Teuchos::RCP<Thyra::LinearOpWithSolveFactoryBase<ST> > belosLOWSFactory = Teuchos::rcp(new Thyra::BelosLinearOpWithSolveFactory<ST>()); // Set the parameter list to specify the behavior of the factory. belosLOWSFactory->setParameterList( belosLOWSFPL ); // Set the output stream and the verbosity level (prints to std::cout by defualt) // NOTE: Set to VERB_NONE for no output from the solver. belosLOWSFactory->setVerbLevel(Teuchos::VERB_LOW); // Create a BelosLinearOpWithSolve object from the Belos LOWS factory. Teuchos::RCP<Thyra::LinearOpWithSolveBase<ST> > nsA = belosLOWSFactory->createOp(); // Initialize the BelosLinearOpWithSolve object with the Thyra linear operator. Thyra::initializeOp<ST>( *belosLOWSFactory, A, &*nsA ); // Create a right-hand side with numRhs vectors in it. Teuchos::RCP< Thyra::MultiVectorBase<ST> > b = Thyra::createMembers(domain, numRhs); // Create an initial std::vector with numRhs vectors in it and initialize it to one. Teuchos::RCP< Thyra::MultiVectorBase<ST> > x = Thyra::createMembers(domain, numRhs); Thyra::assign(&*x, one); // Initialize the right-hand side so that the solution is a std::vector of ones. A->apply( Thyra::NONCONJ_ELE, *x, &*b ); Thyra::assign(&*x, zero); // Perform solve using the linear operator to get the approximate solution of Ax=b, // where b is the right-hand side and x is the left-hand side. Thyra::SolveStatus<ST> solveStatus; solveStatus = Thyra::solve( *nsA, Thyra::NONCONJ_ELE, *b, &*x ); // Print out status of solve. *out << "\nBelos LOWS Status: "<< solveStatus << std::endl; // // Compute residual and ST check convergence. // std::vector<MT> norm_b(numRhs), norm_res(numRhs); Teuchos::RCP< Thyra::MultiVectorBase<ST> > y = Thyra::createMembers(domain, numRhs); // Compute the column norms of the right-hand side b. Thyra::norms_2( *b, &norm_b[0] ); // Compute y=A*x, where x is the solution from the linear solver. A->apply( Thyra::NONCONJ_ELE, *x, &*y ); // Compute A*x-b = y-b Thyra::update( -one, *b, &*y ); // Compute the column norms of A*x-b. Thyra::norms_2( *y, &norm_res[0] ); // Print out the final relative residual norms. MT rel_res = 0.0; *out << "Final relative residual norms" << std::endl; for (int i=0; i<numRhs; ++i) { rel_res = norm_res[i]/norm_b[i]; if (rel_res > maxResid) success = false; *out << "RHS " << i+1 << " : " << std::setw(16) << std::right << rel_res << std::endl; } return ( success ? 0 : 1 ); }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); int rank=mpiSession.getRank(); int numProcs=mpiSession.getNProc(); #else int rank = 0; int numProcs = 1; #endif int polyOrder = 3; int pToAdd = 2; // for tests // define our manufactured solution or problem bilinear form: bool useTriangles = false; FieldContainer<double> meshPoints(4,2); meshPoints(0,0) = 0.0; // x1 meshPoints(0,1) = 0.0; // y1 meshPoints(1,0) = 1.0; meshPoints(1,1) = 0.0; meshPoints(2,0) = 1.0; meshPoints(2,1) = 1.0; meshPoints(3,0) = 0.0; meshPoints(3,1) = 1.0; int H1Order = polyOrder + 1; int horizontalCells = 4, verticalCells = 4; double energyThreshold = 0.2; // for mesh refinements double nonlinearStepSize = 0.5; double nonlinearRelativeEnergyTolerance = 1e-8; // used to determine convergence of the nonlinear solution //////////////////////////////////////////////////////////////////// // DEFINE VARIABLES //////////////////////////////////////////////////////////////////// // new-style bilinear form definition VarFactory varFactory; VarPtr fhat = varFactory.fluxVar("\\widehat{f}"); VarPtr u = varFactory.fieldVar("u"); VarPtr v = varFactory.testVar("v",HGRAD); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); // initialize bilinear form //////////////////////////////////////////////////////////////////// // CREATE MESH //////////////////////////////////////////////////////////////////// // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshPoints, horizontalCells, verticalCells, bf, H1Order, H1Order+pToAdd, useTriangles); mesh->setPartitionPolicy(Teuchos::rcp(new ZoltanMeshPartitionPolicy("HSFC"))); //////////////////////////////////////////////////////////////////// // INITIALIZE BACKGROUND FLOW FUNCTIONS //////////////////////////////////////////////////////////////////// BCPtr nullBC = Teuchos::rcp((BC*)NULL); RHSPtr nullRHS = Teuchos::rcp((RHS*)NULL); IPPtr nullIP = Teuchos::rcp((IP*)NULL); SolutionPtr backgroundFlow = Teuchos::rcp(new Solution(mesh, nullBC, nullRHS, nullIP) ); vector<double> e1(2); // (1,0) e1[0] = 1; vector<double> e2(2); // (0,1) e2[1] = 1; FunctionPtr u_prev = Teuchos::rcp( new PreviousSolutionFunction(backgroundFlow, u) ); FunctionPtr beta = e1 * u_prev + Teuchos::rcp( new ConstantVectorFunction( e2 ) ); //////////////////////////////////////////////////////////////////// // DEFINE BILINEAR FORM //////////////////////////////////////////////////////////////////// // v: // (sigma, grad v)_K - (sigma_hat_n, v)_dK - (u, beta dot grad v) + (u_hat * n dot beta, v)_dK bf->addTerm( -u, beta * v->grad()); bf->addTerm( fhat, v); // ==================== SET INITIAL GUESS ========================== mesh->registerSolution(backgroundFlow); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); FunctionPtr u0 = Teuchos::rcp( new U0 ); map<int, Teuchos::RCP<Function> > functionMap; functionMap[u->ID()] = u0; backgroundFlow->projectOntoMesh(functionMap); // ==================== END SET INITIAL GUESS ========================== //////////////////////////////////////////////////////////////////// // DEFINE INNER PRODUCT //////////////////////////////////////////////////////////////////// IPPtr ip = Teuchos::rcp( new IP ); ip->addTerm( v ); ip->addTerm( beta * v->grad() ); //////////////////////////////////////////////////////////////////// // DEFINE RHS //////////////////////////////////////////////////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr u_prev_squared_div2 = 0.5 * u_prev * u_prev; rhs->addTerm( (e1 * u_prev_squared_div2 + e2 * u_prev) * v->grad()); //////////////////////////////////////////////////////////////////// // DEFINE DIRICHLET BC //////////////////////////////////////////////////////////////////// Teuchos::RCP<BCEasy> inflowBC = Teuchos::rcp( new BCEasy ); // Create spatial filters SpatialFilterPtr bottomBoundary = Teuchos::rcp( new BottomBoundary ); SpatialFilterPtr leftBoundary = Teuchos::rcp( new LeftBoundary ); SpatialFilterPtr rightBoundary = Teuchos::rcp( new LeftBoundary ); // Create BCs FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); FunctionPtr u0_squared_div_2 = 0.5 * u0 * u0; SimpleFunction* u0Ptr = static_cast<SimpleFunction *>(u0.get()); double u0Left = u0Ptr->value(0,0); double u0Right = u0Ptr->value(1.0,0); FunctionPtr leftVal = Teuchos::rcp( new ConstantScalarFunction( -0.5*u0Left*u0Left ) ); FunctionPtr rightVal = Teuchos::rcp( new ConstantScalarFunction( 0.5*u0Right*u0Right ) ); inflowBC->addDirichlet(fhat, bottomBoundary, -u0 ); inflowBC->addDirichlet(fhat, leftBoundary, leftVal ); inflowBC->addDirichlet(fhat, rightBoundary, rightVal ); //////////////////////////////////////////////////////////////////// // CREATE SOLUTION OBJECT //////////////////////////////////////////////////////////////////// Teuchos::RCP<Solution> solution = Teuchos::rcp(new Solution(mesh, inflowBC, rhs, ip)); mesh->registerSolution(solution); if (enforceLocalConservation) { FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); solution->lagrangeConstraints()->addConstraint(fhat == zero); } //////////////////////////////////////////////////////////////////// // DEFINE REFINEMENT STRATEGY //////////////////////////////////////////////////////////////////// Teuchos::RCP<RefinementStrategy> refinementStrategy; refinementStrategy = Teuchos::rcp(new RefinementStrategy(solution,energyThreshold)); //////////////////////////////////////////////////////////////////// // SOLVE //////////////////////////////////////////////////////////////////// for (int refIndex=0; refIndex<=numRefs; refIndex++) { double L2Update = 1e7; int iterCount = 0; while (L2Update > nonlinearRelativeEnergyTolerance && iterCount < maxNewtonIterations) { solution->solve(); L2Update = solution->L2NormOfSolutionGlobal(u->ID()); cout << "L2 Norm of Update = " << L2Update << endl; // backgroundFlow->clear(); backgroundFlow->addSolution(solution, newtonStepSize); iterCount++; } cout << endl; // check conservation VarPtr testOne = varFactory.testVar("1", CONSTANT_SCALAR); // Create a fake bilinear form for the testing BFPtr fakeBF = Teuchos::rcp( new BF(varFactory) ); // Define our mass flux FunctionPtr massFlux = Teuchos::rcp( new PreviousSolutionFunction(solution, fhat) ); LinearTermPtr massFluxTerm = massFlux * testOne; Teuchos::RCP<shards::CellTopology> quadTopoPtr = Teuchos::rcp(new shards::CellTopology(shards::getCellTopologyData<shards::Quadrilateral<4> >() )); DofOrderingFactory dofOrderingFactory(fakeBF); int fakeTestOrder = H1Order; DofOrderingPtr testOrdering = dofOrderingFactory.testOrdering(fakeTestOrder, *quadTopoPtr); int testOneIndex = testOrdering->getDofIndex(testOne->ID(),0); vector< ElementTypePtr > elemTypes = mesh->elementTypes(); // global element types map<int, double> massFluxIntegral; // cellID -> integral double maxMassFluxIntegral = 0.0; double totalMassFlux = 0.0; double totalAbsMassFlux = 0.0; for (vector< ElementTypePtr >::iterator elemTypeIt = elemTypes.begin(); elemTypeIt != elemTypes.end(); elemTypeIt++) { ElementTypePtr elemType = *elemTypeIt; vector< ElementPtr > elems = mesh->elementsOfTypeGlobal(elemType); vector<int> cellIDs; for (int i=0; i<elems.size(); i++) { cellIDs.push_back(elems[i]->cellID()); } FieldContainer<double> physicalCellNodes = mesh->physicalCellNodesGlobal(elemType); BasisCachePtr basisCache = Teuchos::rcp( new BasisCache(elemType,mesh) ); basisCache->setPhysicalCellNodes(physicalCellNodes,cellIDs,true); // true: create side caches FieldContainer<double> cellMeasures = basisCache->getCellMeasures(); FieldContainer<double> fakeRHSIntegrals(elems.size(),testOrdering->totalDofs()); massFluxTerm->integrate(fakeRHSIntegrals,testOrdering,basisCache,true); // true: force side evaluation for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; // pick out the ones for testOne: massFluxIntegral[cellID] = fakeRHSIntegrals(i,testOneIndex); } // find the largest: for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); } for (int i=0; i<elems.size(); i++) { int cellID = cellIDs[i]; maxMassFluxIntegral = max(abs(massFluxIntegral[cellID]), maxMassFluxIntegral); totalMassFlux += massFluxIntegral[cellID]; totalAbsMassFlux += abs( massFluxIntegral[cellID] ); } } if (rank==0) { cout << endl; cout << "largest mass flux: " << maxMassFluxIntegral << endl; cout << "total mass flux: " << totalMassFlux << endl; cout << "sum of mass flux absolute value: " << totalAbsMassFlux << endl; cout << endl; stringstream outfile; outfile << "burgers_" << refIndex; backgroundFlow->writeToVTK(outfile.str(), 5); } if (refIndex < numRefs) refinementStrategy->refine(rank==0); // print to console on rank 0 } return 0; }
int main(int argc, char* argv[]) { Teuchos::GlobalMPISession mpiSession(&argc,&argv); const int procRank = Teuchos::GlobalMPISession::getRank(); Teuchos::oblackholestream blackhole; std::ostream &out = ( procRank == 0 ? std::cout : blackhole ); bool success = true; try { out << Teuchos::Teuchos_Version() << std::endl << std::endl; // Creating an empty command line processor looks like: Teuchos::CommandLineProcessor My_CLP; My_CLP.setDocString( "This example program demonstrates how to use this Teuchos::CommandLineProcessor class\n" "to get options from the command-line and print this help messange automatically.\n" ); /* To set and option, it must be given a name and default value. Additionally, each option can be given a help std::string. Although it is not necessary, a help std::string aids a users comprehension of the acceptable command line arguments. Some examples of setting command line options are: */ // Set an integer command line option. int NumIters = 1550; My_CLP.setOption("iterations", &NumIters, "Number of iterations"); // Set a double-precision command line option. double Tolerance = 1e-10; My_CLP.setOption("tolerance", &Tolerance, "Tolerance"); // Set a std::string command line option. std::string Solver = "GMRES"; My_CLP.setOption("solver", &Solver, "Linear solver"); // Set a boolean command line option. bool Precondition = true; My_CLP.setOption("precondition","no-precondition", &Precondition,"Preconditioning flag"); // Set an enumeration command line option const int num_speed_values = 3; const ESpeed speed_opt_values[] = { SPEED_SLOW, SPEED_MEDIUM, SPEED_FAST }; const char* speed_opt_names[] = { "slow", "medium", "fast" }; ESpeed Speed = SPEED_MEDIUM; My_CLP.setOption( "speed", &Speed, num_speed_values, speed_opt_values, speed_opt_names, "Speed of our solver" ); /* There are also two methods that control the behavior of the command line processor. First, for the command line processor to allow an unrecognized a command line option to be ignored (and only have a warning printed), use: */ My_CLP.recogniseAllOptions(true); /* Second, by default, if the parser finds a command line option it doesn't recognize or finds the --help option, it will throw an std::exception. If you want prevent a command line processor from throwing an std::exception (which is important in this program since we don't have an try/catch around this) when it encounters a unrecognized option or help is printed, use: */ My_CLP.throwExceptions(false); /* We now parse the command line where argc and argv are passed to the parse method. Note that since we have turned off std::exception throwing above we had better grab the return argument so that we can see what happened and act accordingly. */ Teuchos::CommandLineProcessor::EParseCommandLineReturn parseReturn= My_CLP.parse( argc, argv ); if( parseReturn == Teuchos::CommandLineProcessor::PARSE_HELP_PRINTED ) { return 0; } if( parseReturn != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL ) { return 1; // Error! } // Here is where you would use these command line arguments but for this example program // we will just print the help message with the new values of the command-line arguments. if (procRank == 0) out << "\nPrinting help message with new values of command-line arguments ...\n\n"; My_CLP.printHelpMessage(argv[0],out); // Now we will print the option values if (procRank == 0) { out << "\nPrinting user options after parsing ...\n\n"; out << "NumIters = " << NumIters << std::endl; out << "Tolerance = " << Tolerance << std::endl; out << "Solver = \"" << Solver << "\"\n"; out << "Precondition = " << Precondition << std::endl; out << "Speed = " << Speed << std::endl; } } // try TEUCHOS_STANDARD_CATCH_STATEMENTS(true,std::cerr,success); if(success) out << "\nEnd Result: TEST PASSED" << std::endl; return ( success ? 0 : 1 ); }
int main(int argc, char *argv[]) { #ifdef ENABLE_INTEL_FLOATING_POINT_EXCEPTIONS cout << "NOTE: enabling floating point exceptions for divide by zero.\n"; _MM_SET_EXCEPTION_MASK(_MM_GET_EXCEPTION_MASK() & ~_MM_MASK_INVALID); #endif Teuchos::GlobalMPISession mpiSession(&argc, &argv); int rank = Teuchos::GlobalMPISession::getRank(); Teuchos::CommandLineProcessor cmdp(false,true); // false: don't throw exceptions; true: do return errors for unrecognized options const static double PI = 3.141592653589793238462; bool useCondensedSolve = true; // condensed solve not yet compatible with minimum rule meshes int k = 2; // poly order for u in every direction, including temporal int numCells = 32; // in x, y int numTimeCells = 1; int numTimeSlabs = -1; int numFrames = 201; int delta_k = 3; // test space enrichment: should be 3 for 3D int maxRefinements = 0; // maximum # of refinements on each time slab bool useMumpsIfAvailable = true; bool useConstantConvection = false; double refinementTolerance = 0.1; int checkPointFrequency = 50; // output solution and mesh every 50 time slabs int previousSolutionTimeSlabNumber = -1; string previousSolutionFile = ""; string previousMeshFile = ""; cmdp.setOption("polyOrder",&k,"polynomial order for field variable u"); cmdp.setOption("delta_k", &delta_k, "test space polynomial order enrichment"); cmdp.setOption("numCells",&numCells,"number of cells in x and y directions"); cmdp.setOption("numTimeCells",&numTimeCells,"number of time axis cells"); cmdp.setOption("numTimeSlabs",&numTimeSlabs,"number of time slabs"); cmdp.setOption("numFrames",&numFrames,"number of frames for export"); cmdp.setOption("useConstantConvection", "useVariableConvection", &useConstantConvection); cmdp.setOption("useCondensedSolve", "useUncondensedSolve", &useCondensedSolve, "use static condensation to reduce the size of the global solve"); cmdp.setOption("useMumps", "useKLU", &useMumpsIfAvailable, "use MUMPS (if available)"); cmdp.setOption("refinementTolerance", &refinementTolerance, "relative error beyond which to stop refining"); cmdp.setOption("maxRefinements", &maxRefinements, "maximum # of refinements on each time slab"); cmdp.setOption("previousSlabNumber", &previousSolutionTimeSlabNumber, "time slab number of previous solution"); cmdp.setOption("previousSolution", &previousSolutionFile, "file with previous solution"); cmdp.setOption("previousMesh", &previousMeshFile, "file with previous mesh"); if (cmdp.parse(argc,argv) != Teuchos::CommandLineProcessor::PARSE_SUCCESSFUL) { #ifdef HAVE_MPI MPI_Finalize(); #endif return -1; } int H1Order = k + 1; VarFactory varFactory; // traces: VarPtr qHat = varFactory.fluxVar("\\widehat{q}"); // fields: VarPtr u = varFactory.fieldVar("u", L2); // test functions: VarPtr v = varFactory.testVar("v", HGRAD); FunctionPtr x = Function::xn(1); FunctionPtr y = Function::yn(1); FunctionPtr c; if (useConstantConvection) { c = Function::vectorize(Function::constant(0.5), Function::constant(0.5), Function::constant(1.0)); } else { c = Function::vectorize(y-0.5, 0.5-x, Function::constant(1.0)); } FunctionPtr n = Function::normal(); BFPtr bf = Teuchos::rcp( new BF(varFactory) ); bf->addTerm( u, c * v->grad()); bf->addTerm(qHat, v); double width = 2.0, height = 2.0; int horizontalCells = numCells, verticalCells = numCells; int depthCells = numTimeCells; double x0 = -0.5; double y0 = -0.5; double t0 = 0; double totalTime = 2.0 * PI; vector<double> frameTimes; for (int i=0; i<numFrames; i++) { frameTimes.push_back((totalTime*i) / (numFrames-1)); } if (numTimeSlabs==-1) { // want the number of grid points in temporal direction to be about 2000. The temporal length is 2 * PI numTimeSlabs = (int) 2000 / k; } double timeLengthPerSlab = totalTime / numTimeSlabs; if (rank==0) { cout << "solving on " << numCells << " x " << numCells << " x " << numTimeCells << " mesh " << "of order " << k << ".\n"; cout << "numTimeSlabs: " << numTimeSlabs << endl; } SpatialFilterPtr inflowFilter = Teuchos::rcp( new InflowFilterForClockwisePlanarRotation (x0,x0+width,y0,y0+height,0.5,0.5)); vector<double> dimensions; dimensions.push_back(width); dimensions.push_back(height); dimensions.push_back(timeLengthPerSlab); vector<int> elementCounts(3); elementCounts[0] = horizontalCells; elementCounts[1] = verticalCells; elementCounts[2] = depthCells; vector<double> origin(3); origin[0] = x0; origin[1] = y0; origin[2] = t0; Teuchos::RCP<Solver> solver = Teuchos::rcp( new KluSolver ); #ifdef HAVE_AMESOS_MUMPS if (useMumpsIfAvailable) solver = Teuchos::rcp( new MumpsSolver ); #endif // double errorPercentage = 0.5; // for mesh refinements: ask to refine elements that account for 80% of the error in each step // Teuchos::RCP<RefinementStrategy> refinementStrategy; // refinementStrategy = Teuchos::rcp( new ErrorPercentageRefinementStrategy( soln, errorPercentage )); if (maxRefinements != 0) { cout << "Warning: maxRefinements is not 0, but the slice exporter implicitly assumes there won't be any refinements.\n"; } MeshPtr mesh; MeshPtr prevMesh; SolutionPtr prevSoln; mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k, origin); if (rank==0) cout << "Initial mesh has " << mesh->getTopology()->activeCellCount() << " active (leaf) cells " << "and " << mesh->globalDofCount() << " degrees of freedom.\n"; FunctionPtr sideParity = Function::sideParity(); int lastFrameOutputted = -1; SolutionPtr soln; IPPtr ip; ip = bf->graphNorm(); FunctionPtr u0 = Teuchos::rcp( new Cone_U0(0.0, 0.25, 0.1, 1.0, false) ); BCPtr bc = BC::bc(); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. bc->addDirichlet(qHat, SpatialFilter::matchingZ(t0), u0); MeshPtr initialMesh = mesh; int startingSlabNumber; if (previousSolutionTimeSlabNumber != -1) { startingSlabNumber = previousSolutionTimeSlabNumber + 1; if (rank==0) cout << "Loading mesh from " << previousMeshFile << endl; prevMesh = MeshFactory::loadFromHDF5(bf, previousMeshFile); prevSoln = Solution::solution(mesh, bc, RHS::rhs(), ip); // include BC and IP objects for sake of condensed dof interpreter setup... prevSoln->setUseCondensedSolve(useCondensedSolve); if (rank==0) cout << "Loading solution from " << previousSolutionFile << endl; prevSoln->loadFromHDF5(previousSolutionFile); double tn = (previousSolutionTimeSlabNumber+1) * timeLengthPerSlab; origin[2] = tn; mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k, origin); FunctionPtr q_prev = Function::solution(qHat, prevSoln); FunctionPtr q_transfer = Teuchos::rcp( new MeshTransferFunction(-q_prev, prevMesh, mesh, tn) ); // negate because the normals go in opposite directions bc = BC::bc(); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. bc->addDirichlet(qHat, SpatialFilter::matchingZ(tn), q_transfer); double t_slab_final = (previousSolutionTimeSlabNumber+1) * timeLengthPerSlab; int frameOrdinal = 0; while (frameTimes[frameOrdinal] < t_slab_final) { lastFrameOutputted = frameOrdinal++; } } else { startingSlabNumber = 0; } #ifdef HAVE_EPETRAEXT_HDF5 ostringstream dir_name; dir_name << "spacetime_slice_convectingCone_k" << k << "_startSlab" << startingSlabNumber; map<GlobalIndexType,GlobalIndexType> cellMap; MeshPtr meshSlice = MeshTools::timeSliceMesh(initialMesh, 0, cellMap, H1Order); HDF5Exporter sliceExporter(meshSlice,dir_name.str()); #endif soln = Solution::solution(mesh, bc, RHS::rhs(), ip); soln->setUseCondensedSolve(useCondensedSolve); for(int timeSlab = startingSlabNumber; timeSlab<numTimeSlabs; timeSlab++) { double energyThreshold = 0.2; // for mesh refinements: ask to refine elements that account for 80% of the error in each step Teuchos::RCP<RefinementStrategy> refinementStrategy; refinementStrategy = Teuchos::rcp( new RefinementStrategy( soln, energyThreshold )); FunctionPtr u_spacetime = Function::solution(u, soln); double relativeEnergyError; int refNumber = 0; // { // // DEBUGGING: just to try running the time slicing: // double t_slab_final = (timeStep+1) * timeLengthPerSlab; // int frameOrdinal = lastFrameOutputted + 1; // while (frameTimes[frameOrdinal] < t_slab_final) { // FunctionPtr u_spacetime = Function::solution(u, soln); // ostringstream dir_name; // dir_name << "spacetime_slice_convectingCone_k" << k; // MeshTools::timeSliceExport(dir_name.str(), mesh, u_spacetime, frameTimes[frameOrdinal], "u_slice"); // // cout << "Exported frame " << frameOrdinal << ", t=" << frameTimes[frameOrdinal] << endl; // frameOrdinal++; // } // } do { soln->solve(solver); soln->reportTimings(); #ifdef HAVE_EPETRAEXT_HDF5 ostringstream dir_name; dir_name << "spacetime_convectingCone_k" << k << "_t" << timeSlab; HDF5Exporter exporter(soln->mesh(),dir_name.str()); exporter.exportSolution(soln, varFactory); if (rank==0) cout << "Exported HDF solution for time slab to directory " << dir_name.str() << endl; // string u_name = "u_spacetime"; // exporter.exportFunction(u_spacetime, u_name); ostringstream file_name; file_name << dir_name.str(); bool saveSolutionAndMeshForThisSlab = ((timeSlab + 1) % checkPointFrequency == 0); // +1 so that first output is nth, not first if (saveSolutionAndMeshForThisSlab) { dir_name << ".soln"; soln->saveToHDF5(dir_name.str()); if (rank==0) cout << endl << "wrote " << dir_name.str() << endl; file_name << ".mesh"; soln->mesh()->saveToHDF5(file_name.str()); } #endif FunctionPtr u_soln = Function::solution(u, soln); double solnNorm = u_soln->l2norm(mesh); double energyError = soln->energyErrorTotal(); relativeEnergyError = energyError / solnNorm; if (rank==0) { cout << "Relative energy error for refinement " << refNumber++ << ": " << relativeEnergyError << endl; } if ((relativeEnergyError > refinementTolerance) && (refNumber < maxRefinements)) { refinementStrategy->refine(); if (rank==0) { cout << "After refinement, mesh has " << mesh->getTopology()->activeCellCount() << " active (leaf) cells " << "and " << mesh->globalDofCount() << " degrees of freedom.\n"; } } } while ((relativeEnergyError > refinementTolerance) && (refNumber < maxRefinements)); double t_slab_final = (timeSlab+1) * timeLengthPerSlab; int frameOrdinal = lastFrameOutputted + 1; vector<double> timesForSlab; while (frameTimes[frameOrdinal] < t_slab_final) { double t = frameTimes[frameOrdinal]; if (rank==0) cout << "exporting t=" << t << " on slab " << timeSlab << endl; FunctionPtr sliceFunction = MeshTools::timeSliceFunction(mesh, cellMap, u_spacetime, t); sliceExporter.exportFunction(sliceFunction, "u_slice", t); lastFrameOutputted = frameOrdinal++; } // set up next mesh/solution: FunctionPtr q_prev = Function::solution(qHat, soln); // cout << "Error in setup of q_prev: simple solution doesn't know about the map from the previous time slab to the current one. (TODO: fix this.)\n"; double tn = (timeSlab+1) * timeLengthPerSlab; origin[2] = tn; mesh = MeshFactory::rectilinearMesh(bf, dimensions, elementCounts, H1Order, delta_k, origin); FunctionPtr q_transfer = Teuchos::rcp( new MeshTransferFunction(-q_prev, soln->mesh(), mesh, tn) ); // negate because the normals go in opposite directions bc = BC::bc(); bc->addDirichlet(qHat, inflowFilter, Function::zero()); // zero BCs enforced at the inflow boundary. bc->addDirichlet(qHat, SpatialFilter::matchingZ(tn), q_transfer); // IMPORTANT: now that we are ready to step to next soln, nullify BC. If we do not do this, then we have an RCP chain // that extends back to the first time slab, effectively a memory leak. soln->setBC(BC::bc()); soln = Solution::solution(mesh, bc, RHS::rhs(), ip); soln->setUseCondensedSolve(useCondensedSolve); } return 0; }
int main(int argc, char *argv[]) { #ifdef HAVE_MPI Teuchos::GlobalMPISession mpiSession(&argc, &argv,0); choice::MpiArgs args( argc, argv ); #else choice::Args args( argc, argv ); #endif int commRank = Teuchos::GlobalMPISession::getRank(); int numProcs = Teuchos::GlobalMPISession::getNProc(); // Required arguments double epsilon = args.Input<double>("--epsilon", "diffusion parameter"); int numRefs = args.Input<int>("--numRefs", "number of refinement steps"); bool enforceLocalConservation = args.Input<bool>("--conserve", "enforce local conservation"); int norm = args.Input<int>("--norm", "0 = graph\n 1 = robust\n 2 = modified robust"); // Optional arguments (have defaults) bool zeroL2 = args.Input("--zeroL2", "take L2 term on v in robust norm to zero", true); args.Process(); //////////////////// DECLARE VARIABLES /////////////////////// // define test variables VarFactory varFactory; VarPtr tau = varFactory.testVar("tau", HDIV); VarPtr v = varFactory.testVar("v", HGRAD); // define trial variables VarPtr uhat = varFactory.traceVar("uhat"); VarPtr fhat = varFactory.fluxVar("fhat"); VarPtr u = varFactory.fieldVar("u"); VarPtr sigma = varFactory.fieldVar("sigma", VECTOR_L2); //////////////////// BUILD MESH /////////////////////// BFPtr bf = Teuchos::rcp( new BF(varFactory) ); int H1Order = 3, pToAdd = 2; // define nodes for mesh FieldContainer<double> meshBoundary(4,2); meshBoundary(0,0) = 0.0; // x1 meshBoundary(0,1) = 0.0; // y1 meshBoundary(1,0) = 1.0; meshBoundary(1,1) = 0.0; meshBoundary(2,0) = 1.0; meshBoundary(2,1) = 1.0; meshBoundary(3,0) = 0.0; meshBoundary(3,1) = 1.0; int horizontalCells = 4, verticalCells = 4; // create a pointer to a new mesh: Teuchos::RCP<Mesh> mesh = Mesh::buildQuadMesh(meshBoundary, horizontalCells, verticalCells, bf, H1Order, H1Order+pToAdd, false); vector<double> beta; beta.push_back(2.0); beta.push_back(1.0); //////////////////// DEFINE BILINEAR FORM /////////////////////// // tau terms: bf->addTerm(sigma / epsilon, tau); bf->addTerm(u, tau->div()); bf->addTerm(-uhat, tau->dot_normal()); // v terms: bf->addTerm( sigma, v->grad() ); bf->addTerm( beta * u, - v->grad() ); bf->addTerm( fhat, v); //////////////////// DEFINE INNER PRODUCT(S) /////////////////////// IPPtr ip = Teuchos::rcp(new IP); if (norm == 0) { ip = bf->graphNorm(); FunctionPtr h2_scaling = Teuchos::rcp( new ZeroMeanScaling ); ip->addZeroMeanTerm( h2_scaling*v ); } // Robust norm else if (norm == 1) { // robust test norm FunctionPtr ip_scaling = Teuchos::rcp( new EpsilonScaling(epsilon) ); FunctionPtr h2_scaling = Teuchos::rcp( new ZeroMeanScaling ); if (!zeroL2) ip->addTerm( v ); ip->addTerm( sqrt(epsilon) * v->grad() ); // Weight these two terms for inflow ip->addTerm( beta * v->grad() ); ip->addTerm( tau->div() ); ip->addTerm( ip_scaling/sqrt(epsilon) * tau ); if (zeroL2) ip->addZeroMeanTerm( h2_scaling*v ); } // Modified robust norm else if (norm == 2) { // robust test norm FunctionPtr ip_scaling = Teuchos::rcp( new EpsilonScaling(epsilon) ); FunctionPtr h2_scaling = Teuchos::rcp( new ZeroMeanScaling ); // FunctionPtr ip_weight = Teuchos::rcp( new IPWeight() ); if (!zeroL2) ip->addTerm( v ); ip->addTerm( sqrt(epsilon) * v->grad() ); ip->addTerm( beta * v->grad() ); ip->addTerm( tau->div() - beta*v->grad() ); ip->addTerm( ip_scaling/sqrt(epsilon) * tau ); if (zeroL2) ip->addZeroMeanTerm( h2_scaling*v ); } //////////////////// SPECIFY RHS /////////////////////// Teuchos::RCP<RHSEasy> rhs = Teuchos::rcp( new RHSEasy ); FunctionPtr f = Teuchos::rcp( new ConstantScalarFunction(0.0) ); rhs->addTerm( f * v ); // obviously, with f = 0 adding this term is not necessary! //////////////////// CREATE BCs /////////////////////// Teuchos::RCP<BCEasy> bc = Teuchos::rcp( new BCEasy ); SpatialFilterPtr inflowBoundary = Teuchos::rcp( new InflowBoundary ); SpatialFilterPtr outflowBoundary = Teuchos::rcp( new OutflowBoundary ); FunctionPtr u0 = Teuchos::rcp( new U0 ); bc->addDirichlet(uhat, outflowBoundary, u0); FunctionPtr n = Teuchos::rcp( new UnitNormalFunction ); bc->addDirichlet(fhat, inflowBoundary, beta*n*u0); // Teuchos::RCP<PenaltyConstraints> pc = Teuchos::rcp(new PenaltyConstraints); // pc->addConstraint(uhat==u0,inflowBoundary); //////////////////// SOLVE & REFINE /////////////////////// Teuchos::RCP<Solution> solution = Teuchos::rcp( new Solution(mesh, bc, rhs, ip) ); // solution->setFilter(pc); if (enforceLocalConservation) { FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); solution->lagrangeConstraints()->addConstraint(fhat == zero); } double energyThreshold = 0.2; // for mesh refinements RefinementStrategy refinementStrategy( solution, energyThreshold ); VTKExporter exporter(solution, mesh, varFactory); ofstream errOut; if (commRank == 0) errOut.open("confusion_err.txt"); for (int refIndex=0; refIndex<=numRefs; refIndex++){ solution->solve(false); double energy_error = solution->energyErrorTotal(); if (commRank==0){ stringstream outfile; outfile << "confusion_" << refIndex; exporter.exportSolution(outfile.str()); // solution->writeToVTK(outfile.str()); // Check local conservation FunctionPtr flux = Function::solution(fhat, solution); FunctionPtr zero = Teuchos::rcp( new ConstantScalarFunction(0.0) ); Teuchos::Tuple<double, 3> fluxImbalances = checkConservation(flux, zero, varFactory, mesh); cout << "Mass flux: Largest Local = " << fluxImbalances[0] << ", Global = " << fluxImbalances[1] << ", Sum Abs = " << fluxImbalances[2] << endl; errOut << mesh->numGlobalDofs() << " " << energy_error << " " << fluxImbalances[0] << " " << fluxImbalances[1] << " " << fluxImbalances[2] << endl; } if (refIndex < numRefs) refinementStrategy.refine(commRank==0); // print to console on commRank 0 } if (commRank == 0) errOut.close(); return 0; }