ROL
gross-pitaevskii/example_02.cpp
Go to the documentation of this file.
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 
48 #include<algorithm>
49 #include<string>
50 #include"example_02.hpp"
52 #include "ROL_CompositeStep.hpp"
53 
54 typedef double RealT;
55 
56 int main(int argc, char **argv) {
57 
58 
59  // Set up MPI
60  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
61 
62  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
63  int iprint = argc - 1;
64  ROL::Ptr<std::ostream> outStream;
65  ROL::nullstream bhs; // outputs nothing
66  if (iprint > 0)
67  outStream = ROL::makePtrFromRef(std::cout);
68  else
69  outStream = ROL::makePtrFromRef(bhs);
70 
71  int errorFlag = 0;
72 
73 
74  ROL::ParameterList parlist;
75  std::string paramfile = "parameters.xml";
76  auto gplist = ROL::getParametersFromXmlFile( paramfile );
77 
78  int nx = gplist->get("Interior Grid Points",100);
79  RealT gnl = gplist->get("Nonlinearity Coefficient g",50.0);
80  bool exactsolve = gplist->get("Solve Exact Augmented System",false);
81 
82  // Command line option to override parameters.xml for solving the exact augmented system
83  if(argc > 1) {
84  std::string input = argv[1];
85  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
86  if(input=="exactsolve") {
87  exactsolve = true;
88  }
89  }
90 
91 
92  // Grid spacing
93  RealT dx = 1.0/(nx+1);
94 
95  // Finite difference class
96  ROL::Ptr<FiniteDifference<RealT> > fd = ROL::makePtr<FiniteDifference<RealT>>(nx,dx);
97 
98  // Pointer to linspace type vector \f$x_i = \frac{i+1}{n_x+1}\f$ where \f$i=0,\hdots,n_x\f$
99  ROL::Ptr<std::vector<RealT> > xi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
100 
101  for(int i=0; i<nx; ++i) {
102  (*xi_ptr)[i] = RealT(i+1)/(nx+1);
103  }
104 
105  // Pointer to potential vector (quadratic centered at x=0.5)
106  ROL::Ptr<std::vector<RealT> > V_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
107  for(int i=0; i<nx; ++i) {
108  (*V_ptr)[i] = 100.0*pow((*xi_ptr)[i]-0.5,2);
109  }
110 
111  StdVector<RealT> V(V_ptr);
112 
113  // Iteration Vector (pointer to optimzation vector)
114  ROL::Ptr<std::vector<RealT> > psi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
115  OptStdVector<RealT> psi(psi_ptr,fd);
116 
117  // Set Initial Guess (normalized)
118  RealT sqrt30 = sqrt(30);
119 
120  for (int i=0; i<nx; i++) {
121  (*psi_ptr)[i] = sqrt30*(*xi_ptr)[i]*(1.0-(*xi_ptr)[i]);
122  }
123 
124 
125  // Constraint value (scalar)
126  ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
127  ConStdVector<RealT> c(c_ptr);
128 
129  // Lagrange multiplier value (scalar)
130  ROL::Ptr<std::vector<RealT> > lam_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
131  ConDualStdVector<RealT> lam(lam_ptr);
132 
133  // Gradient
134  ROL::Ptr<std::vector<RealT> > g_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
135  OptDualStdVector<RealT> g(g_ptr,fd);
136 
137  // Instantiate objective function
139 
140  // Instantiate normalization constraint
142  ConStdVector<RealT>,ConDualStdVector<RealT> > constr(nx,dx,fd,exactsolve);
143 
144 
145  // Define algorithm.
146  std::string stepname = "Composite Step";
147  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1e-4);
148  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
149  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
150  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
151  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
152  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
153  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
154  parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
155  parlist.sublist("Status Test").set("Iteration Limit",100);
156  ROL::Ptr<ROL::StatusTest<RealT>>
157  status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(parlist);
158  ROL::Ptr<ROL::Step<RealT>>
159  step = ROL::makePtr<ROL::CompositeStep<RealT>>(parlist);
160  ROL::Algorithm<RealT> algo(step,status,false);
161 
162  // Run algorithm.
163  algo.run(psi, g, lam, c, obj, constr, true, *outStream);
164 
165  if(algo.getState()->gnorm>1e-6) {
166  errorFlag += 1;
167  }
168 
169  if (errorFlag != 0)
170  std::cout << "End Result: TEST FAILED\n";
171  else
172  std::cout << "End Result: TEST PASSED\n";
173 
174  return 0;
175 }
Vector< Real > V
basic_nullstream< char, std::char_traits< char >> nullstream
Definition: ROL_Stream.hpp:36
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run optimization algorithms.
int main(int argc, char *argv[])