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) && !(comm->getRank()); Teuchos::RCP<std::ostream> outStream; Teuchos::oblackholestream bhs; // outputs nothing if (print) outStream = Teuchos::rcp(&std::cout, false); else outStream = Teuchos::rcp(&bhs, false); int errorFlag = 0; // *** Example body. try { /*************************************************************************/ /************* INITIALIZE BURGERS FEM CLASS ******************************/ /*************************************************************************/ int nx = 512; // Set spatial discretization. RealT x = 0.0; // 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(*outStream); fem->test_inverse_H1(*outStream); /*************************************************************************/ /************* INITIALIZE SIMOPT OBJECTIVE FUNCTION **********************/ /*************************************************************************/ Teuchos::RCP<ROL::ParametrizedObjective_SimOpt<RealT> > pobj = Teuchos::rcp(new Objective_BurgersControl<RealT>(fem,x)); /*************************************************************************/ /************* INITIALIZE SIMOPT EQUALITY CONSTRAINT *********************/ /*************************************************************************/ bool hess = true; Teuchos::RCP<ROL::ParametrizedEqualityConstraint_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++) { (*z_rcp)[i] = 2.0*random<RealT>(comm)-1.0; (*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,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) ); for (int i=0; i<nx; i++) { (*u_rcp)[i] = 2.0*random<RealT>(comm)-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 = 10000; 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::ParametrizedObjective<RealT> > robj = Teuchos::rcp(new ROL::Reduced_ParametrizedObjective_SimOpt<RealT>( pobj,pcon,up,lp,gup,cp,storage,fdhess)); RealT order = 2.0, threshold = -0.85*(1.0-x); Teuchos::RCP<ROL::Objective<RealT> > obj = Teuchos::rcp(new ROL::BPOEObjective<RealT>( robj,order,threshold,sampler,storage)); /*************************************************************************/ /************* INITIALIZE BOUND CONSTRAINTS ******************************/ /*************************************************************************/ std::vector<RealT> Zlo(nx+2,0.0), Zhi(nx+2,10.0); for (int i = 0; i < nx+2; i++) { if ( i < (int)((nx+2)/3) ) { Zlo[i] = -1.0; Zhi[i] = 1.0; } if ( i >= (int)((nx+2)/3) && i < (int)(2*(nx+2)/3) ) { Zlo[i] = 1.0; Zhi[i] = 5.0; } if ( i >= (int)(2*(nx+2)/3) ) { Zlo[i] = 5.0; Zhi[i] = 10.0; } } Teuchos::RCP<ROL::BoundConstraint<RealT> > Zbnd = Teuchos::rcp(new L2BoundConstraint<RealT>(Zlo,Zhi,fem)); Teuchos::RCP<ROL::BoundConstraint<RealT> > bnd = Teuchos::rcp(new ROL::RiskBoundConstraint<RealT>("BPOE",Zbnd)); /*************************************************************************/ /************* CHECK DERIVATIVES AND CONSISTENCY *************************/ /*************************************************************************/ // CHECK OBJECTIVE DERIVATIVES bool derivcheck = false; if (derivcheck) { 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)); robj->checkGradient(*zp,*gzp,*yzp,print,*outStream); robj->checkHessVec(*zp,*gzp,*yzp,print,*outStream); } } obj->checkGradient(z,g,y,print,*outStream); obj->checkHessVec(z,g,y,print,*outStream); /*************************************************************************/ /************* 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() ); // RUN OPTIMIZATION ROL::Algorithm<RealT> algo("Trust Region",*parlist,false); zp->zero(); algo.run(z, g, *obj, *bnd, print, *outStream); /*************************************************************************/ /************* PRINT CONTROL AND STATE TO SCREEN *************************/ /*************************************************************************/ if ( print ) { std::ofstream ofs; ofs.open("output_example_09.txt",std::ofstream::out); for ( int i = 0; i < nx+2; i++ ) { ofs << std::scientific << std::setprecision(10); ofs << std::setw(20) << std::left << (RealT)i/((RealT)nx+1.0); ofs << std::setw(20) << std::left << (*z_rcp)[i]; ofs << "\n"; } ofs.close(); } *outStream << "Scalar Parameter: " << z.getStatistic() << "\n\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() { // The number that the variable "x" will point to double_complex x; // Creates a variable named "x" and which value will be x CVar xvar ( "x" , &x ); // Asks for a fomula depending on the variable x, e.g. "sin 2x" char s[500]=""; printf("Enter a formula depending on the variable x:\n"); gets(s); // Creates an operation with this formula. The operation depends on one // variable, which is xvar ; the third argument is an array of pointers // to variables; the previous argument is its size CVar* vararray[1]; vararray[0]=&xvar; COperation op ( s, 1, vararray ); // Affects (indirectly) a value to xvar x=3; // Printfs the value of the formula for x=3; printf("%s = %s for x=3\n\n", op.Expr(), PrettyPrint(op.Val()) ); // Creates a function name which can be used in later functions to refer to the operation op(x) CFunction f (op, &xvar); f.SetName("f"); // Creates a second variable named y, and a formula depending on both x and y double_complex y; CVar yvar ( "y" , &y ); CVar* vararray2[2]; // table of variables containing the adresses of xvar and yvar vararray2[0]=&xvar; vararray2[1]=&yvar; // Asks for a formula using x, y and the already-defined function f, e.g. x+f(3y) printf("Enter a formula using x, y and the function f(x): x -> %s that you just entered, e.g. x+f(3y) :\n", op.Expr()); gets(s); CFunction* funcarray[1]; funcarray[0]=&f; COperation op2 ( (char*)s , 2 , vararray2 , 1, funcarray ); // vararray2 is a CVar* array with two elements // funcarray is a CFunction* array with one element y=5; printf("Value for x=3, y=5 : %s\n", PrettyPrint(op2.Val()) ); // Turns the last expression into a function of x and y CFunction g(op2, 2, vararray2); g.SetName("g"); // Here is another way to do it double_complex z,t; CVar zvar("z", &z), tvar("t", &t); COperation op3,zop,top; zop=zvar; top=tvar; // constructs, from a variable, the operation returning its value op3=g( (zop+top, top^2) ); // Ready-to-use ; needs two pairs of ( ) // Now op3 contains the operation op2 with x replaced with z+t, and y replaced with t^2 z=5;t=7; printf("\nLet g be the function g : x,y -> %s\n", op2.Expr()); printf("Value of %s for z=5,t=7:\n %s\n", op3.Expr(), PrettyPrint(op3.Val()) ); COperation dopdt=op3.Diff(tvar); // Computes the derivative of op3 w.r.t t printf("Value of d/dt (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdt.Expr(), PrettyPrint(dopdt.Val()) ); COperation dopdtbar=op3.DiffConj(tvar); // Computes the derivative of op3 w.r.t the conjugate of t printf("Value of d/dtbar (g(z+t,t^2)) = %s for z=5,t=7:\n %s\n", dopdtbar.Expr(), PrettyPrint(dopdtbar.Val()) ); return 0; }