Debian Codegraph @

packages/rol/example/gross-pitaevskii/example_01.cpp

raw
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_01.cpp
11 \brief Minimize the Gross-Pitaevskii functional and demonstrate
12 the effect of choice of function space of the Gradient on
13 convergence.
14
15 \details Minimize the one-dimensional Gross-Pitaevskii (GP) energy
16 functional
17 \f[ J[\psi] = \int \frac{1}{2} |\nabla\psi|^2 + V(x)|\psi|^2
18 +g|\psi|^4 \,\mathrm{d}x \f]
19 Subject to the equality constraint that the particle density be
20 normalized.
21 \f[ e(\psi) = \int |\psi|^2\,\mathrm{d}x - 1 = 0 \f]
22 For simplicity, we will assume the wavefunction \f$\psi\f$ to
23 be real-valued, the potential function \f$ V(x)\geq 0\f$,
24 the computational domain is the interval \f$[0,1]\f$, and that
25 \f$\psi(0)=\psi(1)=0\f$. We also discretize the problem using
26 second-order centered finite differences on a uniform grid.
27
28 \f[
29 \psi''(x_i) \approx = \frac{\psi(x_{i-1})-2\psi(x_i)+\psi(x_{i+1})}{\Delta x^2}
30 \f]
31
32 \author Greg von Winckel
33 \date Mon Dec 1 12:55:12 MST 2014
34 */
35
36 #include "example_01.hpp"
37 #include "ROL_ConstraintStatusTest.hpp"
38 #include "ROL_CompositeStep.hpp"
39
40 typedef double RealT;
41
42 int 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
110 Objective_GrossPitaevskii<RealT> obj(gnl,V);
111
112 // Instantiate normalization constraint
113 Normalization_Constraint<RealT> constr(nx,dx);
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 }