| 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 |
|
| 10 |
/** \file example_02.cpp |
| 11 |
\brief Minimize the Gross-Pitaevskii functional and demonstrate |
| 12 |
the effect of choice of function space of the Gradient on |
| 13 |
convergence. In this version we implement the correct Sobolev |
| 14 |
inner products and Reisz mapping using a finite difference |
| 15 |
approximation of the derivative terms. |
| 16 |
|
| 17 |
|
| 18 |
\details Minimize the one-dimensional Gross-Pitaevskii (GP) energy |
| 19 |
functional |
| 20 |
\f[ J[\psi] = \int \frac{1}{2} |\nabla\psi|^2 + V(x)|\psi|^2 |
| 21 |
+g|\psi|^4 \,\mathrm{d}x \f] |
| 22 |
Subject to the equality constraint that the particle density be |
| 23 |
normalized. |
| 24 |
\f[ e(\psi) = \int |\psi|^2\,\mathrm{d}x - 1 = 0 \f] |
| 25 |
For simplicity, we will assume the wavefunction \f$\psi\f$ to |
| 26 |
be real-valued, the potential function \f$ V(x)\geq 0\f$, |
| 27 |
the computational domain is the interval \f$[0,1]\f$, and that |
| 28 |
\f$\psi(0)=\psi(1)=0\f$. We also discretize the problem using |
| 29 |
second-order centered finite differences on a uniform grid. |
| 30 |
|
| 31 |
\f[ |
| 32 |
\psi''(x_i) \approx = \frac{\psi(x_{i-1})-2\psi(x_i)+\psi(x_{i+1})}{\Delta x^2} |
| 33 |
\f] |
| 34 |
|
| 35 |
The gradient with respect to the \f$L^2\f$ inner product is actually |
| 36 |
an element of \f$H^{-1}\f$, so if we search in this direction, we |
| 37 |
are actually looking for a solution \f$\psi\f$ in a larger space than |
| 38 |
we should. If we compute the gradient with respect to the \f$H^1\f$ |
| 39 |
inner product, by solving a Poisson equation, we search in the right space |
| 40 |
and the optimizer converges much faster than example_01.cpp which |
| 41 |
does not do this. |
| 42 |
|
| 43 |
|
| 44 |
\author Greg von Winckel |
| 45 |
\date Wed Dec 3 16:40:45 MST 2014 |
| 46 |
*/ |
| 47 |
|
| 48 |
#include<algorithm> |
| 49 |
#include<string> |
| 50 |
#include"example_02.hpp" |
| 51 |
#include "ROL_ConstraintStatusTest.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 |
| 138 |
Objective_GrossPitaevskii<RealT,OptStdVector<RealT>,OptDualStdVector<RealT> > obj(gnl,V,fd); |
| 139 |
|
| 140 |
// Instantiate normalization constraint |
| 141 |
Normalization_Constraint<RealT,OptStdVector<RealT>,OptDualStdVector<RealT>, |
| 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 |
} |