Debian Codegraph @

packages/rol/adapters/tpetra/test/sol/test_01.cpp

raw
1 // @HEADER
2 // *****************************************************************************
3 // Rapid Optimization Library (ROL) Package
4 //
5 // Copyright 2014 NTESS and the ROL contributors.
6 // SPDX-License-Identifier: BSD-3-Clause
7 // *****************************************************************************
8 // @HEADER
9
10 #include "test_01.hpp"
11
12 typedef double RealT;
13
14 template<class Real>
15 Real random(const ROL::Ptr<const Teuchos::Comm<int> > &comm) {
16 Real val = 0.0;
17 if ( Teuchos::rank<int>(*comm)==0 ) {
18 val = (Real)rand()/(Real)RAND_MAX;
19 }
20 Teuchos::broadcast<int,Real>(*comm,0,1,&val);
21 return val;
22 }
23
24 int main(int argc, char* argv[]) {
25
26 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
27 ROL::Ptr<const Teuchos::Comm<int> > comm
28 = Teuchos::DefaultComm<int>::getComm();
29
30 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
31 int iprint = argc - 1;
32 ROL::Ptr<std::ostream> outStream;
33 ROL::nullstream bhs; // outputs nothing
34 if (iprint > 0 && Teuchos::rank<int>(*comm)==0)
35 outStream = ROL::makePtrFromRef(std::cout);
36 else
37 outStream = ROL::makePtrFromRef(bhs);
38
39 int errorFlag = 0;
40
41 try {
42 /**********************************************************************************************/
43 /************************* CONSTRUCT ROL ALGORITHM ********************************************/
44 /**********************************************************************************************/
45 // Get ROL parameterlist
46 std::string filename = "input.xml";
47 Teuchos::RCP<Teuchos::ParameterList> parlist = Teuchos::rcp( new Teuchos::ParameterList() );
48 Teuchos::updateParametersFromXmlFile( filename, parlist.ptr() );
49 /**********************************************************************************************/
50 /************************* CONSTRUCT VECTORS **************************************************/
51 /**********************************************************************************************/
52 // Build control vectors
53 int nx = 1024;
54 ROL::Ptr<std::vector<RealT> > x1_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
55 ROL::StdVector<RealT> x1(x1_ptr);
56 ROL::Ptr<std::vector<RealT> > x2_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
57 ROL::StdVector<RealT> x2(x2_ptr);
58 ROL::Ptr<std::vector<RealT> > x3_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
59 ROL::StdVector<RealT> x3(x3_ptr);
60 ROL::Ptr<std::vector<RealT> > z_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
61 ROL::StdVector<RealT> z(z_ptr);
62 ROL::Ptr<std::vector<RealT> > xr_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
63 ROL::StdVector<RealT> xr(xr_ptr);
64 ROL::Ptr<std::vector<RealT> > d_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
65 ROL::StdVector<RealT> d(d_ptr);
66 for ( int i = 0; i < nx+2; i++ ) {
67 (*xr_ptr)[i] = random<RealT>(comm);
68 (*d_ptr)[i] = random<RealT>(comm);
69 }
70 // Build state and adjoint vectors
71 ROL::Ptr<std::vector<RealT> > u_ptr = ROL::makePtr<std::vector<RealT>>(nx,1.0);
72 ROL::StdVector<RealT> u(u_ptr);
73 ROL::Ptr<std::vector<RealT> > p_ptr = ROL::makePtr<std::vector<RealT>>(nx,0.0);
74 ROL::StdVector<RealT> p(p_ptr);
75 ROL::Ptr<ROL::Vector<RealT> > up = ROL::makePtrFromRef(u);
76 ROL::Ptr<ROL::Vector<RealT> > zp = ROL::makePtrFromRef(z);
77 ROL::Ptr<ROL::Vector<RealT> > pp = ROL::makePtrFromRef(p);
78 /**********************************************************************************************/
79 /************************* CONSTRUCT SOL COMPONENTS *******************************************/
80 /**********************************************************************************************/
81 // Build samplers
82 int dim = 4;
83 int nSamp = parlist->sublist("Problem Description").get("Number of Samples", 20);
84 std::vector<RealT> tmp(2,0.0); tmp[0] = -1.0; tmp[1] = 1.0;
85 std::vector<std::vector<RealT> > bounds(dim,tmp);
86 ROL::Ptr<ROL::BatchManager<RealT> > bman
87 = ROL::makePtr<ROL::StdTeuchosBatchManager<RealT,int>>(comm);
88 ROL::Ptr<ROL::SampleGenerator<RealT> > sampler
89 = ROL::makePtr<ROL::MonteCarloGenerator<RealT>>(nSamp,bounds,bman,false,false,100);
90 /**********************************************************************************************/
91 /************************* CONSTRUCT OBJECTIVE FUNCTION ***************************************/
92 /**********************************************************************************************/
93 // Build risk-averse objective function
94 RealT alpha = 1.e-3;
95 ROL::Ptr<ROL::Objective_SimOpt<RealT> > pobjSimOpt
96 = ROL::makePtr<Objective_BurgersControl<RealT>>(alpha,nx);
97 ROL::Ptr<ROL::Constraint_SimOpt<RealT> > pconSimOpt
98 = ROL::makePtr<Constraint_BurgersControl<RealT>>(nx);
99 ROL::Ptr<ROL::Objective<RealT> > pObj
100 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<RealT>>(pobjSimOpt,pconSimOpt,up,zp,pp);
101 ROL::Ptr<ROL::Objective<RealT> > obj = ROL::makePtr<ROL::RiskNeutralObjective<RealT>>(pObj, sampler, true);
102 // Test parametrized objective functions
103 *outStream << "Check Derivatives of Parametrized Objective Function\n";
104 x1.set(xr);
105 pObj->setParameter(sampler->getMyPoint(0));
106 pObj->checkGradient(x1,d,true,*outStream);
107 pObj->checkHessVec(x1,d,true,*outStream);
108 obj->checkGradient(x1,d,true,*outStream);
109 obj->checkHessVec(x1,d,true,*outStream);
110 ROL::Ptr<ROL::Step<RealT>>
111 steprs = ROL::makePtr<ROL::TrustRegionStep<RealT>>(*parlist);
112 ROL::Ptr<ROL::StatusTest<RealT>>
113 statusrs = ROL::makePtr<ROL::StatusTest<RealT>>(*parlist);
114 ROL::Algorithm<RealT> algors(steprs,statusrs,false);
115 algors.run(z, *obj, true, *outStream);
116 /**********************************************************************************************/
117 /****************** CONSTRUCT SIMULATED CONSTRAINT AND VECTORS ********************************/
118 /**********************************************************************************************/
119
120 // Construct SimulatedConstraint.
121 int useW = parlist->sublist("Problem Description").get("Use Constraint Weights", true);
122 ROL::SimulatedConstraint<RealT> simcon(sampler, pconSimOpt, useW);
123 // Construct SimulatedObjective.
124 ROL::SimulatedObjective<RealT> simobj(sampler, pobjSimOpt);
125 // Simulated vectors.
126 std::vector<ROL::Ptr<ROL::Vector<RealT> > > xu_ptr;
127 std::vector<ROL::Ptr<ROL::Vector<RealT> > > vu_ptr;
128 int nvecloc = sampler->numMySamples();
129 RealT right = 1, left = 0;
130 for( int k=0; k<nvecloc; ++k ) {
131 ROL::Ptr<std::vector<RealT> > xuk_ptr = ROL::makePtr<std::vector<RealT>>(nx,1.0);
132 ROL::Ptr<std::vector<RealT> > vuk_ptr = ROL::makePtr<std::vector<RealT>>(nx,1.0);
133 ROL::Ptr<ROL::Vector<RealT> > xuk = ROL::makePtr<ROL::StdVector<RealT>>( xuk_ptr );
134 ROL::Ptr<ROL::Vector<RealT> > vuk = ROL::makePtr<ROL::StdVector<RealT>>( vuk_ptr );
135 for( int i=0; i<nx; ++i ) {
136 (*xuk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
137 (*vuk_ptr)[i] = ( (RealT)rand() / (RealT)RAND_MAX ) * (right - left) + left;
138 }
139 xu_ptr.push_back(xuk);
140 vu_ptr.push_back(vuk);
141 }
142 ROL::Ptr<ROL::SimulatedVector<RealT> > xu = ROL::makePtr<ROL::SimulatedVector<RealT>>(xu_ptr, bman);
143 ROL::Ptr<ROL::SimulatedVector<RealT> > vu = ROL::makePtr<ROL::SimulatedVector<RealT>>(vu_ptr, bman);
144 // SimOpt vectors.
145 ROL::Ptr<std::vector<RealT> > zvec_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
146 ROL::Ptr<ROL::StdVector<RealT> > zvec = ROL::makePtr<ROL::StdVector<RealT>>(zvec_ptr);
147 ROL::Ptr<std::vector<RealT> > dvec_ptr = ROL::makePtr<std::vector<RealT>>(nx+2,0.0);
148 ROL::Ptr<ROL::StdVector<RealT> > dvec = ROL::makePtr<ROL::StdVector<RealT>>(dvec_ptr);
149 for ( int i = 0; i < nx+2; i++ ) {
150 (*zvec_ptr)[i] = random<RealT>(comm);
151 (*dvec_ptr)[i] = random<RealT>(comm);
152 }
153 ROL::Vector_SimOpt<RealT> x(xu, zvec);
154 ROL::Vector_SimOpt<RealT> v(vu, dvec);
155 *outStream << std::endl << "TESTING SimulatedConstraint" << std::endl;
156 simcon.checkApplyJacobian(x, v, *vu, true, *outStream);
157 simcon.checkAdjointConsistencyJacobian(*vu, v, x, *vu, x, true, *outStream);
158 simcon.checkApplyAdjointHessian(x, *vu, v, x, true, *outStream);
159 *outStream << std::endl << "TESTING SimulatedObjective" << std::endl;
160 simobj.checkGradient(x, v, true, *outStream);
161 simobj.checkHessVec(x, v, true, *outStream);
162 ROL::Ptr<ROL::Step<RealT>>
163 step = ROL::makePtr<ROL::CompositeStep<RealT>>(*parlist);
164 ROL::Ptr<ROL::StatusTest<RealT>>
165 status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(*parlist);
166 ROL::Algorithm<RealT> algo(step,status,false);
167 vu->zero();
168 x.scale(1);
169 algo.run(x, *vu, simobj, simcon, true, *outStream);
170
171 // Output control to file.
172 if (Teuchos::rank<int>(*comm)==0) {
173 std::ofstream file;
174 file.open("control-fs-expv.txt");
175 for ( int i = 0; i < nx+2; ++i ) {
176 file << (*zvec_ptr)[i] << "\n";
177 }
178 file.close();
179 }
180
181 }
182 catch (std::logic_error& err) {
183 *outStream << err.what() << "\n";
184 errorFlag = -1000;
185 }; // end try
186
187 if (errorFlag != 0)
188 std::cout << "End Result: TEST FAILED\n";
189 else
190 std::cout << "End Result: TEST PASSED\n";
191
192 return 0;
193 }