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"
86#include "ROL_CompositeStep.hpp"
87
88typedef double RealT;
89
90int main(int argc, char **argv) {
91
92
93 // Set up MPI
94 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
95
96 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
97 int iprint = argc - 1;
98 ROL::Ptr<std::ostream> outStream;
99 ROL::nullstream bhs; // outputs nothing
100 if (iprint > 0)
101 outStream = ROL::makePtrFromRef(std::cout);
102 else
103 outStream = ROL::makePtrFromRef(bhs);
104
105 int errorFlag = 0;
106
107
108 ROL::ParameterList parlist;
109 std::string paramfile = "parameters.xml";
110 auto gplist = ROL::getParametersFromXmlFile( paramfile );
111
112 int nx = gplist->get("Interior Grid Points",100);
113 RealT gnl = gplist->get("Nonlinearity Coefficient g",50.0);
114 bool exactsolve = gplist->get("Solve Exact Augmented System",false);
115
116 // Command line option to override parameters.xml for solving the exact augmented system
117 if(argc > 1) {
118 std::string input = argv[1];
119 std::transform(input.begin(), input.end(), input.begin(), ::tolower);
120 if(input=="exactsolve") {
121 exactsolve = true;
122 }
123 }
124
125
126 // Grid spacing
127 RealT dx = 1.0/(nx+1);
128
129 // Finite difference class
130 ROL::Ptr<FiniteDifference<RealT> > fd = ROL::makePtr<FiniteDifference<RealT>>(nx,dx);
131
132 // Pointer to linspace type vector \f$x_i = \frac{i+1}{n_x+1}\f$ where \f$i=0,\hdots,n_x\f$
133 ROL::Ptr<std::vector<RealT> > xi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
134
135 for(int i=0; i<nx; ++i) {
136 (*xi_ptr)[i] = RealT(i+1)/(nx+1);
137 }
138
139 // Pointer to potential vector (quadratic centered at x=0.5)
140 ROL::Ptr<std::vector<RealT> > V_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
141 for(int i=0; i<nx; ++i) {
142 (*V_ptr)[i] = 100.0*pow((*xi_ptr)[i]-0.5,2);
143 }
144
145 StdVector<RealT> V(V_ptr);
146
147 // Iteration Vector (pointer to optimzation vector)
148 ROL::Ptr<std::vector<RealT> > psi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
149 OptStdVector<RealT> psi(psi_ptr,fd);
150
151 // Set Initial Guess (normalized)
152 RealT sqrt30 = sqrt(30);
153
154 for (int i=0; i<nx; i++) {
155 (*psi_ptr)[i] = sqrt30*(*xi_ptr)[i]*(1.0-(*xi_ptr)[i]);
156 }
157
158
159 // Constraint value (scalar)
160 ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
161 ConStdVector<RealT> c(c_ptr);
162
163 // Lagrange multiplier value (scalar)
164 ROL::Ptr<std::vector<RealT> > lam_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
165 ConDualStdVector<RealT> lam(lam_ptr);
166
167 // Gradient
168 ROL::Ptr<std::vector<RealT> > g_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
169 OptDualStdVector<RealT> g(g_ptr,fd);
170
171 // Instantiate objective function
173
174 // Instantiate normalization constraint
176 ConStdVector<RealT>,ConDualStdVector<RealT> > constr(nx,dx,fd,exactsolve);
177
178
179 // Define algorithm.
180 std::string stepname = "Composite Step";
181 parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1e-4);
182 parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
183 parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
184 parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
185 parlist.sublist("Step").sublist(stepname).set("Output Level",0);
186 parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
187 parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
188 parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
189 parlist.sublist("Status Test").set("Iteration Limit",100);
190 ROL::Ptr<ROL::StatusTest<RealT>>
191 status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(parlist);
192 ROL::Ptr<ROL::Step<RealT>>
193 step = ROL::makePtr<ROL::CompositeStep<RealT>>(parlist);
194 ROL::Algorithm<RealT> algo(step,status,false);
195
196 // Run algorithm.
197 algo.run(psi, g, lam, c, obj, constr, true, *outStream);
198
199 if(algo.getState()->gnorm>1e-6) {
200 errorFlag += 1;
201 }
202
203 if (errorFlag != 0)
204 std::cout << "End Result: TEST FAILED\n";
205 else
206 std::cout << "End Result: TEST PASSED\n";
207
208 return 0;
209}
Vector< Real > V
Provides an interface to run optimization algorithms.
Provides the ROL::Vector interface for scalar values, to be used, for example, with scalar constraint...
Defines the linear algebra or vector space interface.
Definition: ROL_Vector.hpp:84
int main(int argc, char **argv)