From bada76d426123c791c9ea3a462c4cc4aa09e1a26 Mon Sep 17 00:00:00 2001 From: James Schloss Date: Wed, 20 Oct 2021 13:27:56 +0200 Subject: [PATCH] fixing chapter to use split-op code --- contents/quantum_systems/code/c++/energy.cpp | 59 ------------------ contents/quantum_systems/code/c/energy.c | 61 ------------------- .../quantum_systems/code/haskell/Energy.hs | 14 ----- contents/quantum_systems/code/julia/energy.jl | 18 ------ .../quantum_systems/code/python/energy.py | 17 ------ contents/quantum_systems/quantum_systems.md | 10 +-- 6 files changed, 5 insertions(+), 174 deletions(-) delete mode 100644 contents/quantum_systems/code/c++/energy.cpp delete mode 100644 contents/quantum_systems/code/c/energy.c delete mode 100644 contents/quantum_systems/code/haskell/Energy.hs delete mode 100644 contents/quantum_systems/code/julia/energy.jl delete mode 100644 contents/quantum_systems/code/python/energy.py diff --git a/contents/quantum_systems/code/c++/energy.cpp b/contents/quantum_systems/code/c++/energy.cpp deleted file mode 100644 index 15a58bd01..000000000 --- a/contents/quantum_systems/code/c++/energy.cpp +++ /dev/null @@ -1,59 +0,0 @@ -#include -#include - -#include - -void fft(std::vector> &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(x.size(), 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(static_cast(x.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); - - for (size_t i = 0; i < size; ++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[i] = wfc_k[i] * pow(h_k[i], 2); - } - - fft(energy_k, true); - - for (size_t i = 0; i < size; ++i) { - energy_k[i] *= 0.5 * wfc_c[i]; - energy_r[i] = wfc_c[i] * h_r[i] * wfc[i]; - } - - double energy_final = 0; - - for (size_t i = 0; i < size; ++i) { - energy_final += real(energy_k[i] + energy_r[i]); - } - - return energy_final * dx; -} diff --git a/contents/quantum_systems/code/c/energy.c b/contents/quantum_systems/code/c/energy.c deleted file mode 100644 index 9086ffcd5..000000000 --- a/contents/quantum_systems/code/c/energy.c +++ /dev/null @@ -1,61 +0,0 @@ -#include -#include -#include -#include - -#include - -void fft(double complex *x, int n, bool inverse) { - double complex y[n]; - memset(y, 0, sizeof(y)); - fftw_plan p; - - if (inverse) { - p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y, - FFTW_BACKWARD, FFTW_ESTIMATE); - } else { - p = fftw_plan_dft_1d(n, (fftw_complex*)x, (fftw_complex*)y, - FFTW_FORWARD, FFTW_ESTIMATE); - } - - fftw_execute(p); - fftw_destroy_plan(p); - - for (size_t i = 0; i < n; ++i) { - x[i] = y[i] / sqrt((double)n); - } -} - -double calculate_energy(double complex *wfc, double complex *h_r, - double complex *h_k, double dx, size_t size) { - double complex wfc_k[size]; - double complex wfc_c[size]; - memcpy(wfc_k, wfc, sizeof(wfc_k)); - fft(wfc_k, size, false); - - for (size_t i = 0; i < size; ++i) { - wfc_c[i] = conj(wfc[i]); - } - - double complex energy_k[size]; - double complex energy_r[size]; - - for (size_t i = 0; i < size; ++i) { - energy_k[i] = wfc_k[i] * h_k[i]; - } - - fft(energy_k, size, true); - - for (size_t i = 0; i < size; ++i) { - energy_k[i] *= wfc_c[i]; - energy_r[i] = wfc_c[i] * h_r[i] * wfc[i]; - } - - double energy_final = 0; - - for (size_t i = 0; i < size; ++i) { - energy_final += creal(energy_k[i] + energy_r[i]); - } - - return energy_final * dx; -} diff --git a/contents/quantum_systems/code/haskell/Energy.hs b/contents/quantum_systems/code/haskell/Energy.hs deleted file mode 100644 index a024fd139..000000000 --- a/contents/quantum_systems/code/haskell/Energy.hs +++ /dev/null @@ -1,14 +0,0 @@ -import Data.Array.CArray -import Data.Complex -import Math.FFT (dft, idft) -- Binding to fftw - -type Vector = CArray Int (Complex Double) - -calculateEnergy :: Double -> Vector -> Vector -> Vector -> Double -calculateEnergy dx kin pot wfc = (* dx) . sum . map realPart $ elems total - where - total = liftArray2 (+) kineticE potentialE - potentialE = wfcConj .* pot .* wfc - kineticE = wfcConj .* idft (kin .* dft wfc) - wfcConj = liftArray conjugate wfc - a .* b = liftArray2 (*) a b diff --git a/contents/quantum_systems/code/julia/energy.jl b/contents/quantum_systems/code/julia/energy.jl deleted file mode 100644 index 3efce0cb7..000000000 --- a/contents/quantum_systems/code/julia/energy.jl +++ /dev/null @@ -1,18 +0,0 @@ -# We are calculating the energy to check -function calculate_energy(wfc, H_k, H_r, dx) - # Creating momentum and conjugate wavefunctions - wfc_k = fft(wfc) - wfc_c = conj(wfc) - - # Finding the momentum and real-space energy terms - energy_k = wfc_c.*ifft((H_k) .* wfc_k) - energy_r = wfc_c.* H_r .* wfc - - # Integrating over all space - energy_final = 0 - for i = 1:length(energy_k) - energy_final += real(energy_k[i] + energy_r[i]) - end - - return energy_final*dx -end diff --git a/contents/quantum_systems/code/python/energy.py b/contents/quantum_systems/code/python/energy.py deleted file mode 100644 index 328fa9950..000000000 --- a/contents/quantum_systems/code/python/energy.py +++ /dev/null @@ -1,17 +0,0 @@ -import numpy as np - - -def calculate_energy(wfc, H_k, H_r, dx): - """Calculate the energy .""" - # Creating momentum conjugate wavefunctions - wfc_k = np.fft.fft(wfc) - wfc_c = np.conj(wfc) - - # Finding the momentum and real-space energy terms - energy_k = 0.5 * wfc_c * np.fft.ifft((H_k ** 2) * wfc_k) - energy_r = wfc_c * H_r * wfc - - # Integrating over all space - energy_final = sum(energy_k + energy_r).real - - return energy_final * dx diff --git a/contents/quantum_systems/quantum_systems.md b/contents/quantum_systems/quantum_systems.md index a74c22068..a7a762ca5 100644 --- a/contents/quantum_systems/quantum_systems.md +++ b/contents/quantum_systems/quantum_systems.md @@ -226,15 +226,15 @@ This ultimately looks like this: {% method %} {% sample lang="jl" %} -[import, lang:"julia"](code/julia/energy.jl) +[import:114-132, lang:"julia"](../split-operator_method/code/julia/split_op.jl) {% sample lang="hs" %} -[import, lang:"haskell"](code/haskell/Energy.hs) +[import:75-82, lang:"haskell"](../split-operator_method/code/haskell/splitOp.hs) {% sample lang="c" %} -[import:29-, lang:"c"](code/c/energy.c) +[import:150-184, lang:"c"](../split-operator_method/code/c/split_op.c) {% sample lang="cpp" %} -[import:26-, lang:"cpp"](code/c++/energy.cpp) +[import:158-189, lang:"cpp"](../split-operator_method/code/c++/split_op.cpp) {% sample lang="py" %} -[import:4-17, lang:"python"](code/python/energy.py) +[import:98-112, lang:"python"](../split-operator_method/code/python/split_op.py) {% endmethod %} This calculation will be used in many different simulations of quantum systems to check our results.