-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quantum-Mechanics.cpp
231 lines (190 loc) · 8.55 KB
/
Quantum-Mechanics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
///\file Quamtum-Mechanics.cpp
///\author Ethan Knox
///\date 8/2/2020.
#include <iostream>
#include <fstream>
#include <limits>
#include <iomanip>
#include <functional>
#include <boost/program_options.hpp>
#include <boost/numeric/odeint.hpp>
#include <boost/math/interpolators/cubic_hermite.hpp>
#include <boost/math/quadrature/trapezoidal.hpp>
#include <gsl/gsl_const_mksa.h>
#include "Quantum-Potentials.h"
#include "determine_domain_from_potential.h"
using namespace std::placeholders;
namespace opt = boost::program_options;
typedef std::vector<double> state_t;
typedef boost::numeric::odeint::runge_kutta_fehlberg78<state_t> stepper_rkf78_t;
typedef boost::numeric::odeint::runge_kutta4<state_t> rk4;
void assign_potential(std::function<double(double)> &V, std::function<double(double)> &dV, std::string Vfunc) {
if (Vfunc == "QHO") {
V = std::bind(quantum_harmonic_oscillator, _1, m, omega);
dV = std::bind(d_quantum_harmonic_oscillator, _1, m, omega);
}
else { // Vfunc == "ISW"
V = infinite_square_well;
dV = d_infinite_square_well;
}
}
int main(int argc, const char* argv[])
{
double L, psi0, dpsi0, E, h_0, eps_abs, eps_rel;
std::string Vfunc, filename, units;
opt::options_description params("Simulation Parameters");
params.add_options()
("help,h", "Show usage")
("L,l", opt::value<double>(&L)->default_value(10.0), "Characteristic length")
("psi0", opt::value<double>(&psi0)->default_value(1.0e-8), "Initial value")
("dpsi0", opt::value<double>(&dpsi0)->default_value(1.0e-8), "Initial value")
("E,e", opt::value<double>(&E)->default_value(6.5), "Energy")
("V,v", opt::value<std::string>(&Vfunc)->default_value("QHO"), "Potential function")
("h_0,h", opt::value<double>(&h_0)->default_value(1.0e-2), "Initial step size")
("eps_abs", opt::value<double>(&eps_abs)->default_value(1.0e-12), "Allowed absolute error")
("eps_rel", opt::value<double>(&eps_rel)->default_value(0.0), "Allowed relative error")
("filename,f", opt::value<std::string>(&filename)->default_value("output.dat"), "Output file name")
("units,u", opt::value<std::string>(&units)->default_value("natural"), "Use 'natural' or 'mks' units.")
;
opt::variables_map vm;
opt::store(opt::parse_command_line(argc, argv, params), vm);
if (vm.count("help")) {
std::cout << params << std::endl;
return 1;
}
else {
opt::notify(vm);
// Units
if (units == "natural") {
double m = 1.0;
double k = 1.0;
double hbar = 1.0;
double omega = 1.0;
}
else { // units == 'mks'
double m = GSL_CONST_MKSA_MASS_ELECTRON;
// GSL_CONST_MKSA_MASS_PROTON
// GSL_CONST_MKSA_MASS_NEUTRON
double k = 158.2;
/* k \approx \lambda \cdot 2 r_0
| Material | Configuration | Young's Modulus [GPa] | Atomic Radii [pm] | k [N/m] |
| -------- | ------------- | --------------------- | ----------------- | ------- |
| Mg | | 45 | 150 | 13.5 |
| Al | | 69 | 125 | 17.25 |
| Ti | | 110.3 | 140 | 30.884 |
| Cu | | 117 | 135 | 31.59 |
| Si | (Crystal) | 130-185 | 110 | 34.65 |
| Be | | 287 | 105 | 60.27 |
| Mo | | 329-330 | 145 | 95.555 |
| W | | 400-410 | 135 | 109.35 |
| Os | | 525-562 | 130 | 141.31 |
| C | (Graphene) | 1050 | 70 | 147 |
| C | (Diamond) | 1050-1210 | 70 | 158.2 |
| C | (Carbyne) | 32100 | 70 | 294 |
*/
double hbar = GSL_CONST_MKSA_PLANCKS_CONSTANT_HBAR;
double omega = sqrt(k / m);
}
// Potential
std::function<double(double)> V;
std::function<double(double)> dV;
assign_potential(V, dV, Vfunc);
// Domain
std::pair<double, double> domain = assign_domain(Vfunc, L);
const double xi = domain.first;
const double xf = domain.second;
double x = xi;
// Initial Condition
state_t psi{ psi0, dpsi0 };
state_t dpsi_dx(2);
// Data Series
std::vector<double> psi_series;
std::vector<double> dpsi_series;
std::vector<double> x_series;
// Data Recorder
auto observer = [&](const state_t& psi, const double x) {
psi_series.push_back(psi[0]);
dpsi_series.push_back(psi[1]);
x_series.push_back(x);
};
// ODE System
std::function<void(const state_t&, state_t&, double)> sys = [&](const state_t& psi, state_t& dpsi_dx, const double x) {
dpsi_dx[0] = psi[1];
dpsi_dx[1] = (2.0 * m / pow(hbar, 2.0)) * (V(x) - E) * psi[0];
};
boost::numeric::odeint::integrate_const(rk4(), sys, psi, x, xf, h_0, observer);
//boost::numeric::odeint::integrate_adaptive(make_controlled(eps_abs, eps_rel, stepper_rkf78_t()), sys, psi, x, xf, h_0, observer);
std::vector<double> psi_series_copy(psi_series);
std::vector<double> dpsi_series_copy(dpsi_series);
std::vector<double> x_series_copy(x_series);
auto spline = boost::math::interpolators::cubic_hermite(std::move(x_series_copy), std::move(psi_series_copy), std::move(dpsi_series_copy));
double norm = sqrt(boost::math::quadrature::trapezoidal([&](double x) {return pow(spline(x), 2.0); }, xi, xf));
std::ofstream file(filename);
file << "# E = " << E << "\n";
file << "# x, V(x), psi, |psi|^2\n";
for (size_t i = 0; i < x_series.size(); i++) {
psi_series[i] /= norm;
file << std::scientific << x_series[i] << " " << V(x_series[i]) << " " << psi_series[i] + E << " " << pow(psi_series[i], 2.0) + E << "\n";
}
};
}
///////////////////////////////////////////////////////////////////////////////
///\file Stationary-Quantum-EigenE.cpp
///\author Ethan Knox
///\date 8/2/2020.
#include "pch.h"
#include "framework.h"
// TODO: This is an example of a library function
void fnStationaryQuantumEigenE()
{
}
#include <iostream>
#include <fstream>
#include <limits>
#include <iomanip>
#include <functional>
#include <boost/numeric/odeint.hpp>
#include <boost/math/tools/roots.hpp>
#include "determine_domain_from_potential.h"
using namespace std::placeholders;
const double m = 1.0;
const double k = 1.0;
const double hbar = 1.0;
const double omega = 1.0;
typedef std::vector<double> state_t;
typedef boost::numeric::odeint::runge_kutta_fehlberg78<state_t> stepper_rkf78_t;
typedef boost::numeric::odeint::runge_kutta4<state_t> rk4;
void TISE(state_t& psi, state_t& dpsi_dx, double x, std::function<double(double)> V, double E) {
dpsi_dx[0] = psi[1];
dpsi_dx[1] = (2.0 * m / pow(hbar, 2.0)) * (V(x) - E) * psi[0];
}
int eigenE_shooting(state_t& psi, double boundary_condition, std::string Vfunc, double L, double h_0, double eps_rel, double eps_abs, double E)
{
// Potential
std::function<double(double)> V;
std::function<double(double)> dV;
if (Vfunc == "QHO") {
V = std::bind(quantum_harmonic_oscillator, _1, m, omega);
dV = std::bind(d_quantum_harmonic_oscillator, _1, m, omega);
}
else { // Vfunc == "ISW"
V = infinite_square_well;
dV = d_infinite_square_well;
}
// Domain
std::pair<double, double> domain = determine_domain_from_potential(Vfunc, L);
const double xi = domain.first;
const double xf = domain.second;
double x = xi;
// Initial Condition
state_t psi_copy(psi);
state_t dpsi_dx(2);
double h_0 = 1.0e-4;
std::function<double(double)> defect = [x, xf, h_0, psi_copy, &V](double E) {
boost::numeric::odeint::integrate_const(rk4(), std::bind(TISE, _1, _2, _3, V, E), psi_copy, x, xf, h_0);
return psi_copy[0];
};
boost::math::tools::eps_tolerance<double> tol;
std::pair<double, double> r = boost::math::tools::bisect(defect, 0.0, 10.0, tol);
return r.first + (r.second - r.first) / 2;
}