ROL
poisson-control/example_03.cpp
Go to the documentation of this file.
1
2// Burgers includes
3#include "example_02.hpp"
4// ROL includes
5#include "ROL_StdVector.hpp"
6#include "ROL_StdTeuchosBatchManager.hpp"
9#include "ROL_ParameterList.hpp"
10#include "ROL_Solver.hpp"
13
14// Teuchos includes
15#include "Teuchos_Time.hpp"
16#include "ROL_Stream.hpp"
17#include "Teuchos_GlobalMPISession.hpp"
18#include "Teuchos_Comm.hpp"
19#include "Teuchos_DefaultComm.hpp"
20#include "Teuchos_CommHelpers.hpp"
21
22int main( int argc, char *argv[] ) {
23
24 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
25
26 auto comm = ROL::toPtr( Teuchos::DefaultComm<int>::getComm() );
27
28 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
29 int iprint = argc - 1;
30 ROL::Ptr<std::ostream> outStream;
31 ROL::nullstream bhs; // outputs nothing
32 if (iprint > 0 && comm->getRank()==0)
33 outStream = ROL::makePtrFromRef(std::cout);
34 else
35 outStream = ROL::makePtrFromRef(bhs);
36
37 int errorFlag = 0;
38
39 // *** Example body.
40
41 try {
42
43 /***************************************************************************/
44 /***************** GRAB INPUTS *********************************************/
45 /***************************************************************************/
46 // Get finite element parameter list
47 std::string filename = "input_ex03.xml";
48 auto parlist = ROL::getParametersFromXmlFile( filename );
49
50 if ( parlist->sublist("Problem Data").get("Display Option",0) && (comm->getRank() > 0) ) {
51 parlist->set("Display Option",0);
52 }
53
54 /***************************************************************************/
55 /***************** INITIALIZE SAMPLERS *************************************/
56 /***************************************************************************/
57 int dim = 2;
58 int nSamp = parlist->sublist("Problem Data").get("Number of Monte Carlo Samples",1000);
59 std::vector<double> tmp(2); tmp[0] = -1.0; tmp[1] = 1.0;
60 std::vector<std::vector<double> > bounds(dim,tmp);
61 ROL::Ptr<ROL::BatchManager<double> > bman
62 = ROL::makePtr<ROL::StdTeuchosBatchManager<double,int>>(comm);
63 ROL::Ptr<ROL::SampleGenerator<double> > sampler
64 = ROL::makePtr<ROL::MonteCarloGenerator<double>>(nSamp,bounds,bman,false);
65
66 /***************************************************************************/
67 /***************** INITIALIZE CONTROL VECTOR *******************************/
68 /***************************************************************************/
69 int nx = parlist->sublist("Problem Data").get("Number of Elements", 128);
70 ROL::Ptr<std::vector<double> > z_ptr = ROL::makePtr<std::vector<double>>(nx+1, 0.0);
71 ROL::Ptr<ROL::Vector<double> > z = ROL::makePtr<ROL::StdVector<double>>(z_ptr);
72 ROL::Ptr<ROL::Vector<double> > u = ROL::makePtr<ROL::StdVector<double>>(nx-1);
73 ROL::Ptr<ROL::Vector<double> > p = ROL::makePtr<ROL::StdVector<double>>(nx-1);
74
75 /***************************************************************************/
76 /***************** INITIALIZE OBJECTIVE FUNCTION ***************************/
77 /***************************************************************************/
78 double alpha = parlist->sublist("Problem Data").get("Penalty Parameter", 1.e-4);
79 ROL::Ptr<FEM<double> > fem = ROL::makePtr<FEM<double>>(nx);
80 ROL::Ptr<ROL::Objective_SimOpt<double> > pObj
81 = ROL::makePtr<DiffusionObjective<double>>(fem, alpha);
82 ROL::Ptr<ROL::Constraint_SimOpt<double> > pCon
83 = ROL::makePtr<DiffusionConstraint<double>>(fem);
84 ROL::Ptr<ROL::Objective<double> > robj
85 = ROL::makePtr<ROL::Reduced_Objective_SimOpt<double>>(pObj,pCon,u,z,p);
86 robj->setParameter({0.0,0.0});
87
88 /***************************************************************************/
89 /***************** INITIALIZE ROL ALGORITHM ********************************/
90 /***************************************************************************/
91 bool runBundle = parlist->sublist("Problem Data").get("Run Bundle",false);
92 // Solve using bundle
93 if (runBundle) {
94 z->zero();
95 ROL::Ptr<ROL::StochasticProblem<double>> problem2
96 = ROL::makePtr<ROL::StochasticProblem<double>>(robj, z);
97 problem2->makeObjectiveStochastic(*parlist, sampler);
98 problem2->finalize(false,true,*outStream);
99 parlist->sublist("Step").set("Type","Bundle");
100 parlist->sublist("Step").sublist("Bundle").set("Distance Measure Coefficient",0.0);
101 ROL::Solver<double> solver2(problem2,*parlist);
102 solver2.solve(*outStream);
103 }
104
105 ROL::Ptr<ROL::Problem<double>> problem
106 = ROL::makePtr<ROL::Problem<double>>(robj, z);
107 ROL::PrimalDualRisk<double> solver(problem, sampler, *parlist);
108 if (parlist->sublist("Problem Data").get("Run Derivative Check",false)) {
109 problem->check(true,*outStream);
110 solver.check(*outStream);
111 }
112 solver.run(*outStream);
113
114 /***************************************************************************/
115 /***************** PRINT RESULTS *******************************************/
116 /***************************************************************************/
117 int my_number_samples = sampler->numMySamples(), number_samples = 0;
118 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_samples,&number_samples);
119 int my_number_solves = ROL::dynamicPtrCast<DiffusionConstraint<double> >(pCon)->getNumSolves(), number_solves = 0;
120 Teuchos::reduceAll<int,int>(*comm,Teuchos::REDUCE_SUM,1,&my_number_solves,&number_solves);
121 if (comm->getRank() == 0) {
122 std::cout << "Number of Samples = " << number_samples << "\n";
123 std::cout << "Number of Solves = " << number_solves << "\n";
124 }
125
126 if ( comm->getRank() == 0 ) {
127 std::ofstream file;
128 file.open("control.txt");
129 std::vector<double> xmesh(fem->nz(),0.0);
130 fem->build_mesh(xmesh);
131 for (int i = 0; i < fem->nz(); i++ ) {
132 file << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << xmesh[i] << " "
133 << std::setprecision(std::numeric_limits<double>::digits10) << std::scientific << (*z_ptr)[i]
134 << "\n";
135 }
136 file.close();
137 }
138 }
139 catch (std::logic_error& err) {
140 *outStream << err.what() << "\n";
141 errorFlag = -1000;
142 }; // end try
143
144 if (errorFlag != 0)
145 std::cout << "End Result: TEST FAILED\n";
146 else
147 std::cout << "End Result: TEST PASSED\n";
148
149 return 0;
150}
151
152
153
154
Defines a no-output stream class ROL::NullStream and a function makeStreamPtr which either wraps a re...
Provides a simplified interface for solving a wide range of optimization problems.
Definition: ROL_Solver.hpp:64
int solve(const Ptr< StatusTest< Real > > &status=nullPtr, bool combineStatus=true)
Solve optimization problem with no iteration output.
int main(int argc, char *argv[])
constexpr auto dim