ROL
gross-pitaevskii/example_01.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
36#include "example_01.hpp"
38#include "ROL_CompositeStep.hpp"
39
40typedef double RealT;
41
42int main(int argc, char **argv) {
43
44 // Set up MPI
45 Teuchos::GlobalMPISession mpiSession(&argc, &argv);
46
47 // This little trick lets us print to std::cout only if a (dummy) command-line argument is provided.
48 int iprint = argc - 1;
49 ROL::Ptr<std::ostream> outStream;
50 ROL::nullstream bhs; // outputs nothing
51 if (iprint > 0)
52 outStream = ROL::makePtrFromRef(std::cout);
53 else
54 outStream = ROL::makePtrFromRef(bhs);
55
56 int errorFlag = 0;
57
58 ROL::ParameterList parlist;
59
60 std::string paramfile = "parameters.xml";
61 auto gplist = ROL::getParametersFromXmlFile( paramfile );
62
63 int nx = gplist -> get("Interior Grid Points",100);
64 RealT gnl = gplist -> get("Nonlinearity Coefficient g",50.0);
65
66 // Grid spacing
67 RealT dx = 1.0/(nx+1);
68
69 // Pointer to linspace type vector \f$x_i = \frac{i+1}{n_x+1}\f$ where \f$i=0,\hdots,n_x\f$
70 ROL::Ptr<std::vector<RealT> > xi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
71
72 for(int i=0; i<nx; ++i) {
73 (*xi_ptr)[i] = RealT(i+1)/(nx+1);
74 }
75
76 // Pointer to potential vector (quadratic centered at x=0.5)
77 ROL::Ptr<std::vector<RealT> > V_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
78 for(int i=0; i<nx; ++i) {
79 (*V_ptr)[i] = 100.0*pow((*xi_ptr)[i]-0.5,2);
80 }
81
82 StdVector<RealT> V(V_ptr);
83
84 // Iteration Vector (pointer to optimzation vector)
85 ROL::Ptr<std::vector<RealT> > psi_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
86
87
88 // Set Initial Guess (normalized)
89 RealT sqrt30 = sqrt(30);
90
91 for (int i=0; i<nx; i++) {
92 (*psi_ptr)[i] = sqrt30*(*xi_ptr)[i]*(1.0-(*xi_ptr)[i]);
93 }
94
95 StdVector<RealT> psi(psi_ptr);
96
97 // Constraint value (scalar)
98 ROL::Ptr<std::vector<RealT> > c_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
99 StdVector<RealT> c(c_ptr);
100
101 // Lagrange multiplier value (scalar)
102 ROL::Ptr<std::vector<RealT> > lam_ptr = ROL::makePtr<std::vector<RealT>>(1, 0.0);
103 StdVector<RealT> lam(lam_ptr);
104
105 // Gradient
106 ROL::Ptr<std::vector<RealT> > g_ptr = ROL::makePtr<std::vector<RealT>>(nx, 0.0);
107 StdVector<RealT> g(g_ptr);
108
109 // Instantiate objective function
111
112 // Instantiate normalization constraint
114
115 // Define algorithm.
116 std::string stepname = "Composite Step";
117 parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Nominal Relative Tolerance",1e-4);
118 parlist.sublist("Step").sublist(stepname).sublist("Optimality System Solver").set("Fix Tolerance",true);
119 parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Iteration Limit",20);
120 parlist.sublist("Step").sublist(stepname).sublist("Tangential Subproblem Solver").set("Relative Tolerance",1e-2);
121 parlist.sublist("Step").sublist(stepname).set("Output Level",0);
122 parlist.sublist("Status Test").set("Gradient Tolerance",1.e-12);
123 parlist.sublist("Status Test").set("Constraint Tolerance",1.e-12);
124 parlist.sublist("Status Test").set("Step Tolerance",1.e-14);
125 parlist.sublist("Status Test").set("Iteration Limit",100);
126 ROL::Ptr<ROL::StatusTest<RealT>>
127 status = ROL::makePtr<ROL::ConstraintStatusTest<RealT>>(parlist);
128 ROL::Ptr<ROL::Step<RealT>>
129 step = ROL::makePtr<ROL::CompositeStep<RealT>>(parlist);
130 ROL::Algorithm<RealT> algo(step,status,false);
131
132 // Run algorithm.
133 algo.run(psi, g, lam, c, obj, constr, true, *outStream);
134
135
136 if(algo.getState()->gnorm>1e-6) {
137 errorFlag += 1;
138 }
139
140 if (errorFlag != 0)
141 std::cout << "End Result: TEST FAILED\n";
142 else
143 std::cout << "End Result: TEST PASSED\n";
144
145
146
147 return 0;
148
149}
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.
int main(int argc, char **argv)
Example of how to supply ROL with parameters from a JSON file. Requires that json-cpp be installed.