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