-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumericalSolver.cpp
More file actions
251 lines (211 loc) · 6.91 KB
/
NumericalSolver.cpp
File metadata and controls
251 lines (211 loc) · 6.91 KB
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#ifdef _MSC_VER
#include "pch.h"
#endif
#include "NumericalSolver.h"
#include "Eigen/Dense"
#include "Eigen/Sparse"
#include <fstream>
#include <cmath>
#include <vector>
#include <iostream>
void NumericalSolver::solveSchrodingerFDM(
double mass,
double xMin,
double xMax,
int numPoints,
const std::vector<double>& V,
int numEigenstates,
const std::string& outputFilename)
{
using namespace Eigen;
//const double hbar = 1.0545718e-34;
// NEW: dimensionless units
const double hbar = 1.0;
// treat "mass" argument as dimensionless too (pass 1.0 from main)
double m = mass;
double dx = (xMax - xMin) / (numPoints - 1);
MatrixXd H = MatrixXd::Zero(numPoints, numPoints);
// Build Hamiltonian
for (int i = 0; i < numPoints; ++i) {
H(i, i) = V[i] + (hbar * hbar) / (mass * dx * dx);
if (i > 0)
H(i, i - 1) = -0.5 * (hbar * hbar) / (mass * dx * dx);
if (i < numPoints - 1)
H(i, i + 1) = -0.5 * (hbar * hbar) / (mass * dx * dx);
}
// Solve eigenvalue problem
SelfAdjointEigenSolver<MatrixXd> solver(H);
VectorXd eigenvalues = solver.eigenvalues();
MatrixXd eigenvectors = solver.eigenvectors();
// --- Normalize eigenvectors and export energies (quick polish) ---
for (int i = 0; i < numEigenstates; ++i) {
double norm = eigenvectors.col(i).norm();
if (norm > 0.0) eigenvectors.col(i) /= norm;
}
// Save energies to a separate CSV
{
std::ofstream eout("fdm_energies.csv");
eout << "index,energy_J\n";
for (int i = 0; i < numEigenstates; ++i)
eout << i << "," << eigenvalues(i) << "\n";
}
// Save results
std::ofstream out(outputFilename);
out << "x";
for (int i = 0; i < numEigenstates; ++i)
out << ",psi" << i;
out << "\n";
for (int j = 0; j < numPoints; ++j) {
double x = xMin + j * dx;
out << x;
for (int i = 0; i < numEigenstates; ++i)
out << "," << eigenvectors(j, i);
out << "\n";
}
out.close();
// Print energies
for (int i = 0; i < numEigenstates; ++i)
std::cout << "Energy level " << i << ": " << eigenvalues(i) << " J\n";
}
using cplx = std::complex<double>;
static inline double sqr(double x) { return x * x; }
// ------------------------
// Crank–Nicolson time evolution
// ------------------------
void NumericalSolver::timeEvolveCrankNicolson(
double mass,
double xMin,
double xMax,
int N,
const std::vector<double>& V,
const std::vector<std::complex<double>>& psi0,
double dt,
int numSteps,
const std::string& outCsv,
int snapshotEvery
) {
using namespace Eigen;
const double hbar = 1.0545718e-34;
const double dx = (xMax - xMin) / (N - 1);
// Sanity checks
if ((int)V.size() != N || (int)psi0.size() != N) {
std::cerr << "[CN] Error: V and psi0 must have size N.\n";
return;
}
// Build sparse Hamiltonian H for Dirichlet BCs (psi=0 at endpoints)
using SpMat = SparseMatrix<cplx>;
using Trip = Triplet<cplx>;
std::vector<Trip> Ht;
Ht.reserve(3 * N);
const double pref = -(hbar * hbar) / (2.0 * mass * dx * dx); // for second derivative
// Interior points 1..N-2
for (int i = 1; i < N - 1; ++i) {
Ht.emplace_back(i, i, cplx(-2.0 * pref + V[i], 0.0)); // diag: -2*pref + V
Ht.emplace_back(i, i - 1, cplx(pref, 0.0)); // subdiag
Ht.emplace_back(i, i + 1, cplx(pref, 0.0)); // superdiag
}
// Enforce Dirichlet at boundaries (H large on ends or identity with huge potential)
Ht.emplace_back(0, 0, cplx(1e30, 0.0));
Ht.emplace_back(N - 1, N - 1, cplx(1e30, 0.0));
SpMat H(N, N);
H.setFromTriplets(Ht.begin(), Ht.end());
// Build A = I + i dt H / (2hbar), B = I - i dt H / (2hbar)
const cplx iFactor = cplx(0.0, dt / (2.0 * hbar));
std::vector<Trip> At, Bt;
At.reserve(5 * N);
Bt.reserve(5 * N);
// Start from identity
for (int i = 0; i < N; ++i) {
At.emplace_back(i, i, cplx(1.0, 0.0));
Bt.emplace_back(i, i, cplx(1.0, 0.0));
}
// Add ± i dt H / (2hbar)
// Iterate H non-zeros
for (int k = 0; k < H.outerSize(); ++k) {
for (SpMat::InnerIterator it(H, k); it; ++it) {
int r = (int)it.row();
int c = (int)it.col();
cplx val = it.value();
At.emplace_back(r, c, iFactor * val); // +i(Δt/2ħ)H
Bt.emplace_back(r, c, -iFactor * val); // -i(Δt/2ħ)H
}
}
SpMat A(N, N), B(N, N);
A.setFromTriplets(At.begin(), At.end());
B.setFromTriplets(Bt.begin(), Bt.end());
// Factor A once
SparseLU<SpMat> solver;
solver.analyzePattern(A);
solver.factorize(A);
if (solver.info() != Success) {
std::cerr << "[CN] Factorization failed.\n";
return;
}
// Initialize psi vector
VectorXcd psi(N);
for (int i = 0; i < N; ++i) psi(i) = psi0[i];
// Normalize initial state
double norm0 = std::sqrt((psi.adjoint() * psi).real().value());
if (norm0 > 0) psi /= norm0;
// Open CSV
std::ofstream out(outCsv);
out << "x,t,RePsi,ImPsi,AbsPsi,Phase\n";
const auto dumpSnapshot = [&](double t) {
for (int j = 0; j < N; ++j) {
double x = xMin + j * dx;
double absPsi = std::abs(psi(j));
double phase = std::arg(psi(j));
out << x << "," << t << ","
<< psi(j).real() << ","
<< psi(j).imag() << ","
<< absPsi << ","
<< phase << "\n";
}
};
// write t=0 snapshot
dumpSnapshot(0.0);
// Time march
for (int step = 1; step <= numSteps; ++step) {
// B * psi^t
VectorXcd rhs = B * psi;
// Solve A * psi^{t+dt} = rhs
psi = solver.solve(rhs);
// Re-normalize (numerical drift)
double norm = std::sqrt(((psi.adjoint() * psi).real())(0, 0));
if (norm > 0) psi /= norm;
if (step % snapshotEvery == 0) {
double t = step * dt;
dumpSnapshot(t);
}
}
out.close();
std::cout << "[CN] Wrote time evolution snapshots to: " << outCsv << "\n";
}
// ------------------------
// Gaussian initial state
// ------------------------
std::vector<std::complex<double>> NumericalSolver::makeGaussianInitial(
int N,
double xMin,
double xMax,
double x0,
double sigma,
double k0
) {
const double dx = (xMax - xMin) / (N - 1);
std::vector<cplx> psi(N);
double norm = 0.0;
for (int j = 0; j < N; ++j) {
double x = xMin + j * dx;
double gauss = std::exp(-0.5 * sqr((x - x0) / sigma));
cplx phase = std::exp(cplx(0.0, k0 * x)); // e^{i k0 x}
psi[j] = gauss * phase;
norm += std::norm(psi[j]) * dx;
}
// Normalize to 1
if (norm > 0) {
double s = std::sqrt(norm);
for (auto& c : psi) c /= s;
}
return psi;
}