From ccdb6898b82d7e4b60eba94c36245635808af26e Mon Sep 17 00:00:00 2001 From: leios Date: Tue, 23 Oct 2018 09:18:04 +0900 Subject: [PATCH 1/9] modifying C++ energy calculation to match split-op code. --- contents/quantum_systems/code/c++/energy.cpp | 39 +++++++++++--------- contents/quantum_systems/quantum_systems.md | 2 +- 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index 380e04a76..4326898fc 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -1,50 +1,55 @@ +#include #include #include +#include #include -void fft(std::vector> x, bool inverse) { - std::vector> y(x.size()); - for (size_t i = 0; i < x.size(); i++) { - y.push_back(std::complex(0.0, 0.0)); - } - +void fft(std::vector> x, int n, bool inverse) { + std::complex y[n]; + memset(y, 0, sizeof(y)); + fftw_plan p; - p = fftw_plan_dft_1d(x.size(), reinterpret_cast(x.data()), - reinterpret_cast(y.data()), - (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); + fftw_complex *in = reinterpret_cast(x.data()); + fftw_complex *out = reinterpret_cast(y); + + p = fftw_plan_dft_1d(n, in, out, + (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); + fftw_execute(p); fftw_destroy_plan(p); for (size_t i = 0; i < x.size(); ++i) { - x[i] = y[i] / sqrt((double)x.size()); + x[i] = y[i] / sqrt(static_cast(n)); } } -double calculate_energy(std::vector> wfc, std::vector> h_r, - std::vector> h_k, double dx, size_t size) { +double calculate_energy(std::vector> wfc, + std::vector> h_r, + std::vector> h_k, + double dx, size_t size) { std::vector> wfc_k(wfc); std::vector> wfc_c(size); - fft(wfc_k, false); + fft(wfc_k, size, false); for (size_t i = 0; i < size; ++i) { - wfc_c.push_back(conj(wfc[i])); + wfc_c[i] = conj(wfc[i]); } std::vector> energy_k(size); std::vector> energy_r(size); for (size_t i = 0; i < size; ++i) { - energy_k.push_back(wfc_k[i] * h_k[i]); + energy_k[i] = wfc_k[i] * h_k[i]; } - fft(energy_k, true); + fft(energy_k, size, true); for (size_t i = 0; i < size; ++i) { energy_k[i] *= wfc_c[i]; - energy_r.push_back(wfc_c[i] * h_r[i] * wfc[i]); + energy_r[i] = wfc_c[i] * h_r[i] * wfc[i]; } double energy_final = 0; diff --git a/contents/quantum_systems/quantum_systems.md b/contents/quantum_systems/quantum_systems.md index 1abf8d6a5..b470ac2bf 100644 --- a/contents/quantum_systems/quantum_systems.md +++ b/contents/quantum_systems/quantum_systems.md @@ -232,7 +232,7 @@ This ultimately looks like this: {% sample lang="c" %} [import:29-, lang:"c_cpp"](code/c/energy.c) {% sample lang="cpp" %} -[import:26-57, lang:"c_cpp"](code/c++/energy.cpp) +[import:29-62, lang:"c_cpp"](code/c++/energy.cpp) {% sample lang="py" %} [import:4-17, lang:"python"](code/python/energy.py) {% endmethod %} From 839552ff2ba43e054bd8505522897dc18e0d97ae Mon Sep 17 00:00:00 2001 From: leios Date: Wed, 24 Oct 2018 06:01:01 +0900 Subject: [PATCH 2/9] fixing y generation. --- contents/quantum_systems/code/c++/energy.cpp | 5 ++--- contents/quantum_systems/quantum_systems.md | 2 +- contents/split-operator_method/code/c++/split_op.cpp | 5 ++--- contents/split-operator_method/split-operator_method.md | 2 +- 4 files changed, 6 insertions(+), 8 deletions(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index 4326898fc..c14cb7e6c 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -6,13 +6,12 @@ #include void fft(std::vector> x, int n, bool inverse) { - std::complex y[n]; - memset(y, 0, sizeof(y)); + std::vector> y(x.size(), std::complex(0.0, 0.0)); fftw_plan p; fftw_complex *in = reinterpret_cast(x.data()); - fftw_complex *out = reinterpret_cast(y); + fftw_complex *out = reinterpret_cast(y.data()); p = fftw_plan_dft_1d(n, in, out, (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); diff --git a/contents/quantum_systems/quantum_systems.md b/contents/quantum_systems/quantum_systems.md index b470ac2bf..8ea3ffae2 100644 --- a/contents/quantum_systems/quantum_systems.md +++ b/contents/quantum_systems/quantum_systems.md @@ -232,7 +232,7 @@ This ultimately looks like this: {% sample lang="c" %} [import:29-, lang:"c_cpp"](code/c/energy.c) {% sample lang="cpp" %} -[import:29-62, lang:"c_cpp"](code/c++/energy.cpp) +[import:29-, lang:"c_cpp"](code/c++/energy.cpp) {% sample lang="py" %} [import:4-17, lang:"python"](code/python/energy.py) {% endmethod %} diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index efdad46c1..6e65f9117 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -80,12 +80,11 @@ struct Operators { }; void fft(vector_complex &x, int n, bool inverse) { - complex y[n]; - memset(y, 0, sizeof(y)); + std::vector> y(x.size(), std::complex(0.0, 0.0)); fftw_plan p; fftw_complex *in = reinterpret_cast(x.data()); - fftw_complex *out = reinterpret_cast(y); + fftw_complex *out = reinterpret_cast(y.data()); p = fftw_plan_dft_1d(n, in, out, (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); diff --git a/contents/split-operator_method/split-operator_method.md b/contents/split-operator_method/split-operator_method.md index ff74f99d9..b9030155d 100644 --- a/contents/split-operator_method/split-operator_method.md +++ b/contents/split-operator_method/split-operator_method.md @@ -145,7 +145,7 @@ The final step is to do the iteration, itself. {% sample lang="c" %} [import:98-148, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:100-157, lang:"c_cpp"](code/c++/split_op.cpp) +[import:100-156, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:57-95, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} From ebfad57bef904bff6e1f9ea32bc00ea310d6b842 Mon Sep 17 00:00:00 2001 From: leios Date: Wed, 24 Oct 2018 06:08:34 +0900 Subject: [PATCH 3/9] removing unnnecessary . --- contents/quantum_systems/code/c++/energy.cpp | 10 +++++----- .../split-operator_method/code/c++/split_op.cpp | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index c14cb7e6c..e62a8b858 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -5,7 +5,7 @@ #include -void fft(std::vector> x, int n, bool inverse) { +void fft(std::vector> x, bool inverse) { std::vector> y(x.size(), std::complex(0.0, 0.0)); fftw_plan p; @@ -13,7 +13,7 @@ void fft(std::vector> x, int n, bool inverse) { fftw_complex *in = reinterpret_cast(x.data()); fftw_complex *out = reinterpret_cast(y.data()); - p = fftw_plan_dft_1d(n, in, out, + p = fftw_plan_dft_1d(x.size(), in, out, (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); @@ -21,7 +21,7 @@ void fft(std::vector> x, int n, bool inverse) { fftw_destroy_plan(p); for (size_t i = 0; i < x.size(); ++i) { - x[i] = y[i] / sqrt(static_cast(n)); + x[i] = y[i] / sqrt(static_cast(x.size())); } } @@ -31,7 +31,7 @@ double calculate_energy(std::vector> wfc, double dx, size_t size) { std::vector> wfc_k(wfc); std::vector> wfc_c(size); - fft(wfc_k, size, false); + fft(wfc_k, false); for (size_t i = 0; i < size; ++i) { wfc_c[i] = conj(wfc[i]); @@ -44,7 +44,7 @@ double calculate_energy(std::vector> wfc, energy_k[i] = wfc_k[i] * h_k[i]; } - fft(energy_k, size, true); + fft(energy_k, true); for (size_t i = 0; i < size; ++i) { energy_k[i] *= wfc_c[i]; diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 6e65f9117..74f8df2b7 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -79,20 +79,20 @@ struct Operators { vector_complex wfc; }; -void fft(vector_complex &x, int n, bool inverse) { +void fft(vector_complex &x, bool inverse) { std::vector> y(x.size(), std::complex(0.0, 0.0)); fftw_plan p; fftw_complex *in = reinterpret_cast(x.data()); fftw_complex *out = reinterpret_cast(y.data()); - p = fftw_plan_dft_1d(n, in, out, + p = fftw_plan_dft_1d(x.size(), in, out, (inverse ? FFTW_BACKWARD : FFTW_FORWARD), FFTW_ESTIMATE); fftw_execute(p); fftw_destroy_plan(p); - for (size_t i = 0; i < n; ++i) { - x[i] = y[i] / sqrt(static_cast(n)); + for (size_t i = 0; i < x.size(); ++i) { + x[i] = y[i] / sqrt(static_cast(x.size())); } } @@ -104,13 +104,13 @@ void split_op(Params &par, Operators &opr) { opr.wfc[j] *= opr.pe[j]; } - fft(opr.wfc, opr.size, false); + fft(opr.wfc, false); for (size_t j = 0; j < opr.size; ++j) { opr.wfc[j] *= opr.ke[j]; } - fft(opr.wfc, opr.size, true); + fft(opr.wfc, true); for (size_t j = 0; j < opr.size; ++j) { opr.wfc[j] *= opr.pe[j]; @@ -159,7 +159,7 @@ 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); + fft(wfc_k, false); for (size_t i = 0; i < opr.size; ++i) { wfc_c[i] = conj(wfc_r[i]); @@ -172,7 +172,7 @@ double calculate_energy(Params &par, Operators &opr) { energy_k[i] = wfc_k[i] * pow(complex(par.k[i], 0.0), 2); } - fft(energy_k, opr.size, true); + fft(energy_k, true); for (size_t i = 0; i < opr.size; ++i) { energy_k[i] *= 0.5 * wfc_c[i]; From 415305ce8e7838f712abdbfe834c88a23b420afb Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 28 Oct 2018 17:34:44 +0900 Subject: [PATCH 4/9] Update contents/quantum_systems/code/c++/energy.cpp Co-Authored-By: leios --- contents/quantum_systems/code/c++/energy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index e62a8b858..d78fd452b 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -47,7 +47,7 @@ double calculate_energy(std::vector> wfc, fft(energy_k, true); for (size_t i = 0; i < size; ++i) { - energy_k[i] *= wfc_c[i]; + energy_k[i] *= 0.5 * wfc_c[i]; energy_r[i] = wfc_c[i] * h_r[i] * wfc[i]; } From 5dc434100d9d6091b1ba24ffb106a93399878cfe Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 28 Oct 2018 17:35:27 +0900 Subject: [PATCH 5/9] Update contents/quantum_systems/quantum_systems.md Co-Authored-By: leios --- contents/quantum_systems/quantum_systems.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/quantum_systems/quantum_systems.md b/contents/quantum_systems/quantum_systems.md index 8ea3ffae2..1204bf97f 100644 --- a/contents/quantum_systems/quantum_systems.md +++ b/contents/quantum_systems/quantum_systems.md @@ -232,7 +232,7 @@ This ultimately looks like this: {% sample lang="c" %} [import:29-, lang:"c_cpp"](code/c/energy.c) {% sample lang="cpp" %} -[import:29-, lang:"c_cpp"](code/c++/energy.cpp) +[import:28-, lang:"c_cpp"](code/c++/energy.cpp) {% sample lang="py" %} [import:4-17, lang:"python"](code/python/energy.py) {% endmethod %} From 785d02af41ebe634957ceab50459bab88322ac50 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 28 Oct 2018 17:35:29 +0900 Subject: [PATCH 6/9] Update contents/split-operator_method/split-operator_method.md Co-Authored-By: leios --- contents/split-operator_method/split-operator_method.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/split-operator_method/split-operator_method.md b/contents/split-operator_method/split-operator_method.md index b9030155d..8cdb6434f 100644 --- a/contents/split-operator_method/split-operator_method.md +++ b/contents/split-operator_method/split-operator_method.md @@ -145,7 +145,7 @@ The final step is to do the iteration, itself. {% sample lang="c" %} [import:98-148, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:100-156, lang:"c_cpp"](code/c++/split_op.cpp) +[import:99-156, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:57-95, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} From a460cdb0e9f98eea7ac19ba4d55be8f080d10d7c Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Sun, 28 Oct 2018 17:35:32 +0900 Subject: [PATCH 7/9] Update contents/quantum_systems/code/c++/energy.cpp Co-Authored-By: leios --- contents/quantum_systems/code/c++/energy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index d78fd452b..5f172c9a6 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -5,7 +5,7 @@ #include -void fft(std::vector> x, bool inverse) { +void fft(std::vector> &x, bool inverse) { std::vector> y(x.size(), std::complex(0.0, 0.0)); fftw_plan p; From be63285eb5c433d8e6a22ba4f954d1d99e5db0f9 Mon Sep 17 00:00:00 2001 From: Eric Berquist Date: Tue, 30 Oct 2018 11:42:48 +0900 Subject: [PATCH 8/9] You are right. Thanks! Co-Authored-By: leios --- contents/quantum_systems/code/c++/energy.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index 5f172c9a6..b2782f544 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -41,7 +41,7 @@ double calculate_energy(std::vector> wfc, std::vector> energy_r(size); for (size_t i = 0; i < size; ++i) { - energy_k[i] = wfc_k[i] * h_k[i]; + energy_k[i] = wfc_k[i] * pow(h_k[i], 2); } fft(energy_k, true); From 2dbda0a3b06ed2dc561dc48e9bb3324232d5ae48 Mon Sep 17 00:00:00 2001 From: James Schloss Date: Tue, 30 Oct 2018 12:02:19 +0900 Subject: [PATCH 9/9] fixing unnecessary includes. --- contents/quantum_systems/code/c++/energy.cpp | 2 -- contents/quantum_systems/quantum_systems.md | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp index b2782f544..15a58bd01 100644 --- a/contents/quantum_systems/code/c++/energy.cpp +++ b/contents/quantum_systems/code/c++/energy.cpp @@ -1,7 +1,5 @@ -#include #include #include -#include #include diff --git a/contents/quantum_systems/quantum_systems.md b/contents/quantum_systems/quantum_systems.md index 1204bf97f..2b0b5b0db 100644 --- a/contents/quantum_systems/quantum_systems.md +++ b/contents/quantum_systems/quantum_systems.md @@ -232,7 +232,7 @@ This ultimately looks like this: {% sample lang="c" %} [import:29-, lang:"c_cpp"](code/c/energy.c) {% sample lang="cpp" %} -[import:28-, lang:"c_cpp"](code/c++/energy.cpp) +[import:26-, lang:"c_cpp"](code/c++/energy.cpp) {% sample lang="py" %} [import:4-17, lang:"python"](code/python/energy.py) {% endmethod %}