ROL
gross-pitaevskii/example_01.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 
70 #include "example_01.hpp"
71 
72 typedef double RealT;
73 
74 int main(int argc, char **argv) {
75 
76  // Set up MPI
77  Teuchos::GlobalMPISession mpiSession(&argc, &argv);
78 
79  // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
80  int iprint = argc - 1;
81  Teuchos::RCP<std::ostream> outStream;
82  Teuchos::oblackholestream bhs; // outputs nothing
83  if (iprint > 0)
84  outStream = Teuchos::rcp(&std::cout, false);
85  else
86  outStream = Teuchos::rcp(&bhs, false);
87 
88  int errorFlag = 0;
89 
90  Teuchos::ParameterList parlist;
91  Teuchos::ParameterList gplist;
92 
93  std::string paramfile = "parameters.xml";
94  Teuchos::updateParametersFromXmlFile(paramfile,Teuchos::Ptr<Teuchos::ParameterList>(&gplist));
95 
96  int nx = gplist.get("Interior Grid Points",100);
97  RealT gnl = gplist.get("Nonlinearity Coefficient g",50.0);
98 
99  // Grid spacing
100  RealT dx = 1.0/(nx+1);
101 
102  // Pointer to linspace type vector \f$x_i = \frac{i+1}{n_x+1}\f$ where \f$i=0,\hdots,n_x\f$
103  Teuchos::RCP<std::vector<RealT> > xi_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 0.0) );
104 
105  for(int i=0; i<nx; ++i) {
106  (*xi_rcp)[i] = RealT(i+1)/(nx+1);
107  }
108 
109  // Pointer to potential vector (quadratic centered at x=0.5)
110  Teuchos::RCP<std::vector<RealT> > V_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 0.0) );
111  for(int i=0; i<nx; ++i) {
112  (*V_rcp)[i] = 100.0*pow((*xi_rcp)[i]-0.5,2);
113  }
114 
115  StdVector<RealT> V(V_rcp);
116 
117  // Iteration Vector (pointer to optimzation vector)
118  Teuchos::RCP<std::vector<RealT> > psi_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 0.0) );
119 
120 
121  // Set Initial Guess (normalized)
122  RealT sqrt30 = sqrt(30);
123 
124  for (int i=0; i<nx; i++) {
125  (*psi_rcp)[i] = sqrt30*(*xi_rcp)[i]*(1.0-(*xi_rcp)[i]);
126  }
127 
128  StdVector<RealT> psi(psi_rcp);
129 
130  // Equality constraint value (scalar)
131  Teuchos::RCP<std::vector<RealT> > c_rcp = Teuchos::rcp( new std::vector<RealT> (1, 0.0) );
132  StdVector<RealT> c(c_rcp);
133 
134  // Lagrange multiplier value (scalar)
135  Teuchos::RCP<std::vector<RealT> > lam_rcp = Teuchos::rcp( new std::vector<RealT> (1, 0.0) );
136  StdVector<RealT> lam(lam_rcp);
137 
138  // Gradient
139  Teuchos::RCP<std::vector<RealT> > g_rcp = Teuchos::rcp( new std::vector<RealT> (nx, 0.0) );
140  StdVector<RealT> g(g_rcp);
141 
142  // Instantiate objective function
144 
145  // Instantiate normalization constraint
146  Normalization_Constraint<RealT> constr(nx,dx);
147 
148  // Define algorithm.
149  std::string stepname = "Composite Step";
150  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1e-4);
151  parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
152  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
153  parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
154  parlist.sublist("Step").sublist(stepname).set("Output Level",0);
155  parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
156  parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
157  parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
158  parlist.sublist("Status Test").set("Iteration Limit",100);
159  ROL::Algorithm<RealT> algo(stepname, parlist);
160 
161  // Run algorithm.
162  algo.run(psi, g, lam, c, obj, constr, true, *outStream);
163 
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 
175 
176  return 0;
177 
178 }
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
Teuchos::RCP< const AlgorithmState< Real > > getState(void) const
Provides the std::vector implementation of the ROL::Vector interface.
Provides an interface to run optimization algorithms.
int main(int argc, char **argv)