-
-
Notifications
You must be signed in to change notification settings - Fork 360
Added C++ lang implementation of the split operator #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
berquist
merged 21 commits into
algorithm-archivists:master
from
rajdakin:CPP_integration
Oct 22, 2018
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
fd0740e
Added C++ lang
rajdakin 13420c9
Merge branch 'master' of https://github.com/algorithm-archivists/algo…
rajdakin ff3b24b
Used vector instead of pointers
rajdakin fb09a9c
Used streams instead of files
rajdakin 2090f22
Updated MarkDown
rajdakin 7aba6f4
Reserve vector before pushing elements to it
rajdakin d4fd4b7
Using std::stringstreams instead of char arrays
rajdakin cc09d8a
Used std::cout instead of printf
rajdakin e7393d6
Updated MarkDown
rajdakin a511643
Fixed segmentation faults
rajdakin 147fc28
Added some space allocation with the vectors
rajdakin 845074d
Merge branch 'master' of https://github.com/algorithm-archivists/algo…
rajdakin 5eb7c53
Updated MarkDown
rajdakin 7bcd36b
Used vector constructor to reserve space
rajdakin 0d8a887
Used references instead of copies
rajdakin 83fcf6e
Simplified types, parameters and defined pi iff not defined
rajdakin eb0b0dd
Removed spacing in empty lines
rajdakin ea8d597
Fixed vector value assignment
rajdakin e9951be
Changed emplace_back to push_back
rajdakin 7675547
Updated MarkDown
rajdakin 52e566f
Updated MarkDown
rajdakin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,201 @@ | ||
#include <complex> | ||
#include <vector> | ||
#include <iostream> | ||
#include <cstring> | ||
#include <fstream> | ||
|
||
// Using fftw3 library. | ||
#include <fftw3.h> | ||
|
||
#ifndef M_PI | ||
#define M_PI 3.14159265358979323846 | ||
#endif | ||
|
||
using complex = std::complex<double>; | ||
using vector_real = std::vector<double>; | ||
using vector_complex = std::vector<complex>; | ||
|
||
struct Params { | ||
Params(double _xmax, unsigned int _res, double _dt, unsigned int _timesteps, bool im) { | ||
xmax = _xmax; | ||
res = _res; | ||
dt = _dt; | ||
timesteps = _timesteps; | ||
dx = 2.0 * xmax / res; | ||
x.reserve(res); | ||
dk = M_PI / xmax; | ||
k.reserve(res); | ||
im_time = im; | ||
|
||
for (size_t i = 0; i < res; ++i) { | ||
x.emplace_back(xmax / res - xmax + i * (2.0 * xmax / res)); | ||
if (i < res / 2) { | ||
k.push_back(i * M_PI / xmax); | ||
} else { | ||
k.push_back((static_cast<double>(i) - res) * M_PI / xmax); | ||
rajdakin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
} | ||
} | ||
|
||
double xmax; | ||
unsigned int res; | ||
double dt; | ||
unsigned int timesteps; | ||
double dx; | ||
vector_real x; | ||
double dk; | ||
vector_real k; | ||
bool im_time; | ||
}; | ||
|
||
struct Operators { | ||
public: | ||
Operators(Params &par, double voffset, | ||
double wfcoffset) { | ||
size = par.res; | ||
rajdakin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
v.reserve(size); | ||
pe.reserve(size); | ||
ke.reserve(size); | ||
wfc.reserve(size); | ||
|
||
for (size_t i = 0; i < size; ++i) { | ||
v.push_back(0.5 * pow(par.x[i] - voffset, 2)); | ||
wfc.push_back(exp(-pow(par.x[i] - wfcoffset, 2) / 2.0)); | ||
|
||
if (par.im_time) { | ||
ke.push_back(exp(-0.5 * par.dt * pow(par.k[i], 2))); | ||
pe.push_back(exp(-0.5 * par.dt * v[i])); | ||
} else { | ||
ke.push_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * complex(0.0, 1.0))); | ||
pe.push_back(exp(-0.5 * par.dt * v[i] * complex(0.0, 1.0))); | ||
} | ||
} | ||
} | ||
|
||
size_t size; | ||
vector_complex v; | ||
vector_complex pe; | ||
vector_complex ke; | ||
vector_complex wfc; | ||
}; | ||
|
||
void fft(vector_complex &x, int n, bool inverse) { | ||
complex y[n]; | ||
memset(y, 0, sizeof(y)); | ||
fftw_plan p; | ||
|
||
fftw_complex *in = reinterpret_cast<fftw_complex*>(x.data()); | ||
fftw_complex *out = reinterpret_cast<fftw_complex*>(y); | ||
p = fftw_plan_dft_1d(n, in, out, | ||
(inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); | ||
rajdakin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
fftw_execute(p); | ||
fftw_destroy_plan(p); | ||
|
||
for (size_t i = 0; i < n; ++i) { | ||
x[i] = y[i] / sqrt(static_cast<double>(n)); | ||
} | ||
} | ||
|
||
void split_op(Params &par, Operators &opr) { | ||
double density[opr.size]; | ||
|
||
for (size_t i = 0; i < par.timesteps; ++i) { | ||
for (size_t j = 0; j < opr.size; ++j) { | ||
opr.wfc[j] *= opr.pe[j]; | ||
} | ||
|
||
fft(opr.wfc, opr.size, false); | ||
|
||
for (size_t j = 0; j < opr.size; ++j) { | ||
opr.wfc[j] *= opr.ke[j]; | ||
} | ||
|
||
fft(opr.wfc, opr.size, true); | ||
|
||
for (size_t j = 0; j < opr.size; ++j) { | ||
opr.wfc[j] *= opr.pe[j]; | ||
} | ||
|
||
for (size_t j = 0; j < opr.size; ++j) { | ||
density[j] = pow(abs(opr.wfc[j]), 2); | ||
} | ||
|
||
if (par.im_time) { | ||
double sum = 0; | ||
|
||
for (size_t j = 0; j < opr.size; ++j) { | ||
sum += density[j]; | ||
} | ||
|
||
sum *= par.dx; | ||
|
||
for (size_t j = 0; j < opr.size; ++j) { | ||
opr.wfc[j] /= sqrt(sum); | ||
} | ||
} | ||
|
||
// Writing data into a file in the format of: | ||
// index, density, real potential. | ||
std::stringstream filename_stream; | ||
filename_stream << "output" << i << ".dat"; | ||
|
||
std::ofstream fstream = std::ofstream(filename_stream.str()); | ||
|
||
if (fstream) { | ||
for (int i = 0; i < opr.size; ++i) { | ||
std::stringstream data_stream; | ||
|
||
data_stream << i << "\t" << density[i] << "\t" << real(opr.v[i]) << "\n"; | ||
|
||
fstream.write(data_stream.str().c_str(), data_stream.str().length()); | ||
} | ||
} | ||
|
||
fstream.close(); | ||
} | ||
} | ||
|
||
double calculate_energy(Params &par, Operators &opr) { | ||
vector_complex wfc_r(opr.wfc); | ||
vector_complex wfc_k(opr.wfc); | ||
vector_complex wfc_c(opr.size); | ||
fft(wfc_k, opr.size, false); | ||
|
||
for (size_t i = 0; i < opr.size; ++i) { | ||
wfc_c[i] = conj(wfc_r[i]); | ||
} | ||
|
||
vector_complex energy_k(opr.size); | ||
vector_complex energy_r(opr.size); | ||
|
||
for (size_t i = 0; i < opr.size; ++i) { | ||
energy_k[i] = wfc_k[i] * pow(complex(par.k[i], 0.0), 2); | ||
} | ||
|
||
fft(energy_k, opr.size, true); | ||
|
||
for (size_t i = 0; i < opr.size; ++i) { | ||
energy_k[i] *= 0.5 * wfc_c[i]; | ||
energy_r[i] = wfc_c[i] * opr.v[i] * wfc_r[i]; | ||
} | ||
|
||
double energy_final = 0; | ||
|
||
for (size_t i = 0; i < opr.size; ++i) { | ||
energy_final += real(energy_k[i] + energy_r[i]); | ||
} | ||
|
||
return energy_final * par.dx; | ||
} | ||
|
||
int main() { | ||
Params par = Params(5.0, 256, 0.05, 100, true); | ||
Operators opr = Operators(par, 0.0, -1.0); | ||
|
||
split_op(par, opr); | ||
|
||
std::cout << "The energy is " << calculate_energy(par, opr) << "\n"; | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.