ROL
gross-pitaevskii/example_02.cpp
Go to the documentation of this file.
1 // @HEADER
2 // ************************************************************************
3 //
4 // Rapid Optimization Library (ROL) Package
5 // Copyright (2014) Sandia Corporation
6 //
7 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
8 // license for use of this work by or on behalf of the U.S. Government.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are
12 // met:
13 //
14 // 1. Redistributions of source code must retain the above copyright
15 // notice, this list of conditions and the following disclaimer.
16 //
17 // 2. Redistributions in binary form must reproduce the above copyright
18 // notice, this list of conditions and the following disclaimer in the
19 // documentation and/or other materials provided with the distribution.
20 //
21 // 3. Neither the name of the Corporation nor the names of the
22 // contributors may be used to endorse or promote products derived from
23 // this software without specific prior written permission.
24 //
25 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
26 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
29 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 //
37 // Questions? Contact lead developers:
38 // Drew Kouri (dpkouri@sandia.gov) and
39 // Denis Ridzal (dridzal@sandia.gov)
40 //
41 // ************************************************************************
42 // @HEADER
43 
82 #include<algorithm>
83 #include<string>
84 #include"example_02.hpp"
85 
86 typedef double RealT;
87 
88 int main(int argc, char **argv) {
89 
90 
91  // Set up MPI
92  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
93 
94  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
95  int iprint = argc - 1;
96  ROL::Ptr<std::ostream> outStream;
97  ROL::nullstream bhs; // outputs nothing
98  if (iprint > 0)
99  outStream = ROL::makePtrFromRef(std::cout);
100  else
101  outStream = ROL::makePtrFromRef(bhs);
102 
103  int errorFlag = 0;
104 
105 
106  ROL::ParameterList parlist;
107  std::string paramfile = "parameters.xml";
108  auto gplist = ROL::getParametersFromXmlFile( paramfile );
109 
110  int nx = gplist->get("Interior Grid Points",100);
111  RealT gnl = gplist->get("Nonlinearity Coefficient g",50.0);
112  bool exactsolve = gplist->get("Solve Exact Augmented System",false);
113 
114  // Command line option to override parameters.xml for solving the exact augmented system
115  if(argc > 1) {
116  std::string input = argv[1];
117  std::transform(input.begin(), input.end(), input.begin(), ::tolower);
118  if(input=="exactsolve") {
119  exactsolve = true;
120  }
121  }
122 
123 
124  // Grid spacing
125  RealT dx = 1.0/(nx+1);
126 
127  // Finite difference class
128  ROL::Ptr<FiniteDifference<RealT> > fd = ROL::makePtr<FiniteDifference<RealT>>(nx,dx);
129 
130  // Pointer to linspace type vector \f$x_i = \frac{i+1}{n_x+1}\f$ where \f$i=0,\hdots,n_x\f$
131  ROL::Ptr<std::vector<RealT> > xi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
132 
133  for(int i=0; i<nx; ++i) {
134  (*xi_ptr)[i] = RealT(i+1)/(nx+1);
135  }
136 
137  // Pointer to potential vector (quadratic centered at x=0.5)
138  ROL::Ptr<std::vector<RealT> > V_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
139  for(int i=0; i<nx; ++i) {
140  (*V_ptr)[i] = 100.0*pow((*xi_ptr)[i]-0.5,2);
141  }
142 
143  StdVector<RealT> V(V_ptr);
144 
145  // Iteration Vector (pointer to optimzation vector)
146  ROL::Ptr<std::vector<RealT> > psi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
147  OptStdVector<RealT> psi(psi_ptr,fd);
148 
149  // Set Initial Guess (normalized)
150  RealT sqrt30 = sqrt(30);
151 
152  for (int i=0; i<nx; i++) {
153  (*psi_ptr)[i] = sqrt30*(*xi_ptr)[i]*(1.0-(*xi_ptr)[i]);
154  }
155 
156 
157  // Constraint value (scalar)
158  ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
159  ConStdVector<RealT> c(c_ptr);
160 
161  // Lagrange multiplier value (scalar)
162  ROL::Ptr<std::vector<RealT> > lam_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
163  ConDualStdVector<RealT> lam(lam_ptr);
164 
165  // Gradient
166  ROL::Ptr<std::vector<RealT> > g_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
167  OptDualStdVector<RealT> g(g_ptr,fd);
168 
169  // Instantiate objective function
171 
172  // Instantiate normalization constraint
174  ConStdVector<RealT>,ConDualStdVector<RealT> > constr(nx,dx,fd,exactsolve);
175 
176 
177  // Define algorithm.
178  std::string stepname = "Composite Step";
179  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1e-4);
180  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
181  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
182  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
183  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
184  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
185  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
186  parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
187  parlist.sublist("Status Test").set("Iteration Limit",100);
188  ROL::Algorithm<RealT> algo(stepname, parlist);
189 
190  // Run algorithm.
191  algo.run(psi, g, lam, c, obj, constr, true, *outStream);
192 
193  if(algo.getState()->gnorm>1e-6) {
194  errorFlag += 1;
195  }
196 
197  if (errorFlag != 0)
198  std::cout << "End Result: TEST FAILED\n";
199  else
200  std::cout << "End Result: TEST PASSED\n";
201 
202  return 0;
203 }
virtual std::vector< std::string > run(Vector< Real > &x, Objective< Real > &obj, bool print=false, std::ostream &outStream=std::cout, bool printVectors=false, std::ostream &vectorStream=std::cout)
Run algorithm on unconstrained problems (Type-U). This is the primary Type-U interface.
Vector< Real > V
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Provides an interface to run optimization algorithms.
ROL::Ptr< const AlgorithmState< Real > > getState(void) const
basic_nullstream< char, char_traits< char >> nullstream
Definition: ROL_Stream.hpp:72
int main(int argc, char *argv[])