From fd0740e6f1e5535b3d0fd6d388b97df70817939f Mon Sep 17 00:00:00 2001 From: rajdakin Date: Mon, 1 Oct 2018 21:25:59 +0200 Subject: [PATCH 01/19] Added C++ lang --- .../code/c++/split_op.cpp | 249 ++++++++++++++++++ .../split-operator_method.md | 14 +- 2 files changed, 258 insertions(+), 5 deletions(-) create mode 100644 contents/split-operator_method/code/c++/split_op.cpp diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp new file mode 100644 index 000000000..2f9f1c940 --- /dev/null +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -0,0 +1,249 @@ +#include +#include +#include + +// Using fftw3 library. +#include + +class Params { +public: + 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 = new double[res]; + dk = M_PI / xmax; + k = new double[res]; + im_time = im; + + for (size_t i = 0; i < res; ++i) { + x[i] = xmax / res - xmax + i * (2.0 * xmax / res); + if (i < res / 2) { + k[i] = i * M_PI / xmax; + } else { + k[i] = ((double)i - res) * M_PI / xmax; + } + } + } + + ~Params() { + delete[] x; + delete[] k; + } + + unsigned int getRes() const { + return res; + } + double getXmax() const { + return xmax; + } + double getDt() const { + return dt; + } + double getDx() const { + return dx; + } + double getDk() const { + return dk; + } + unsigned int getTimesteps() const { + return timesteps; + } + double * getXs() const { + return x; + } + double * getKs() const { + return k; + } + bool isImTime() const { + return im_time; + } + +protected: + double xmax; + unsigned int res; + double dt; + unsigned int timesteps; + double dx; + double *x; + double dk; + double *k; + bool im_time; +}; + +class Operators { +public: + Operators(Params &par, double voffset, + double wfcoffset) { + size = par.getRes(); + v = new std::complex[size]; + pe = new std::complex[size]; + ke = new std::complex[size]; + wfc = new std::complex[size]; + + for (size_t i = 0; i < size; ++i) { + v[i] = 0.5 * pow(par.getXs()[i] - voffset, 2); + wfc[i] = exp(-pow(par.getXs()[i] - wfcoffset, 2) / 2.0); + + if (par.isImTime()) { + ke[i] = exp(-0.5 * par.getDt() * pow(par.getKs()[i], 2)); + pe[i] = exp(-0.5 * par.getDt() * v[i]); + } else { + ke[i] = exp(-0.5 * par.getDt() * pow(par.getKs()[i], 2) * std::complex(0.0, 1.0)); + pe[i] = exp(-0.5 * par.getDt() * v[i] * std::complex(0.0, 1.0)); + } + } + } + + ~Operators() { + delete[] v; + delete[] pe; + delete[] ke; + delete[] wfc; + } + + size_t getSize() const { + return size; + } + std::complex *getV() const { + return v; + } + std::complex *getPe() const { + return pe; + } + std::complex *getKe() const { + return ke; + } + std::complex *getWfc() const { + return wfc; + } + +protected: + size_t size; + std::complex *v; + std::complex *pe; + std::complex *ke; + std::complex *wfc; +}; + +void fft(std::complex *x, int n, bool inverse) { + std::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); + } +} + +void split_op(Params &par, Operators &opr) { + double density[opr.getSize()]; + + for (size_t i = 0; i < par.getTimesteps(); ++i) { + for (size_t j = 0; j < opr.getSize(); ++j) { + opr.getWfc()[j] *= opr.getPe()[j]; + } + + fft(opr.getWfc(), opr.getSize(), false); + + for (size_t j = 0; j < opr.getSize(); ++j) { + opr.getWfc()[j] *= opr.getKe()[j]; + } + + fft(opr.getWfc(), opr.getSize(), true); + + for (size_t j = 0; j < opr.getSize(); ++j) { + opr.getWfc()[j] *= opr.getPe()[j]; + } + + for (size_t j = 0; j < opr.getSize(); ++j) { + density[j] = pow(abs(opr.getWfc()[j]), 2); + } + + if (par.isImTime()) { + double sum = 0; + + for (size_t j = 0; j < opr.getSize(); ++j) { + sum += density[j]; + } + + sum *= par.getDx(); + + for (size_t j = 0; j < opr.getSize(); ++j) { + opr.getWfc()[j] /= sqrt(sum); + } + } + + // Writing data into a file in the format of: + // index, density, real potential. + char filename[256]; + sprintf(filename, "output%lu.dat", i); + FILE *fp = fopen(filename, "w"); + + for (int i = 0; i < opr.getSize(); ++i) { + fprintf(fp, "%d\t%f\t%f\n", i, density[i], real(opr.getV()[i])); + } + + fclose(fp); + } +} + +double calculate_energy(Params par, Operators opr) { + std::complex wfc_r[opr.getSize()]; + std::complex wfc_k[opr.getSize()]; + std::complex wfc_c[opr.getSize()]; + std::memcpy(wfc_r, opr.getWfc(), sizeof(wfc_r)); + + std::memcpy(wfc_k, opr.getWfc(), sizeof(wfc_k)); + fft(wfc_k, opr.getSize(), false); + + for (size_t i = 0; i < opr.getSize(); ++i) { + wfc_c[i] = conj(wfc_r[i]); + } + + std::complex energy_k[opr.getSize()]; + std::complex energy_r[opr.getSize()]; + + for (size_t i = 0; i < opr.getSize(); ++i) { + energy_k[i] = wfc_k[i] * pow(par.getKs()[i] + 0.0*std::complex(0.0, 1.0), 2); + } + + fft(energy_k, opr.getSize(), true); + + for (size_t i = 0; i < opr.getSize(); ++i) { + energy_k[i] *= 0.5 * wfc_c[i]; + energy_r[i] = wfc_c[i] * opr.getV()[i] * wfc_r[i]; + } + + double energy_final = 0; + + for (size_t i = 0; i < opr.getSize(); ++i) { + energy_final += real(energy_k[i] + energy_r[i]); + } + + return energy_final * par.getDx(); +} + +int main() { + Params par = Params(5.0, 256, 0.05, 100, true); + Operators opr = Operators(par, 0.0, -1.0); + + split_op(par, opr); + + printf("The energy is %f\n", calculate_energy(par, opr)); + + return 0; +} diff --git a/contents/split-operator_method/split-operator_method.md b/contents/split-operator_method/split-operator_method.md index 34c3b1229..986d5b407 100644 --- a/contents/split-operator_method/split-operator_method.md +++ b/contents/split-operator_method/split-operator_method.md @@ -100,8 +100,8 @@ Regardless, we first need to set all the initial parameters, including the initi {% sample lang="jl" %} [import:11-34, lang:"julia"](code/julia/split_op.jl) {% sample lang="c" %} -[import:10-20, lang:"c_cpp"](code/c/split_op.c) -[import:51-72, lang:"c_cpp"](code/c/split_op.c) +{% sample lang="cpp" %} +[import:8-74, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:11-30, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -119,8 +119,8 @@ Afterwards, we turn them into operators: {% sample lang="jl" %} [import:36-62, lang:"julia"](code/julia/split_op.jl) {% sample lang="c" %} -[import:22-28, lang:"c_cpp"](code/c/split_op.c) -[import:74-95, lang:"c_cpp"](code/c/split_op.c) +{% sample lang="cpp" %} +[import:76-129, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:33-54, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -139,7 +139,9 @@ The final step is to do the iteration, itself. {% sample lang="jl" %} [import:65-112, lang:"julia"](code/julia/split_op.jl) {% sample lang="c" %} -[import:97-145, lang:"c_cpp"](code/c/split_op.c) +[import:98-148, lang:"c_cpp"](code/c/split_op.c) +{% sample lang="cpp" %} +[import:152-202, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:57-95, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -165,6 +167,8 @@ Checking to make sure your code can output the correct energy for a harmonic tra [import, lang:"julia"](code/julia/split_op.jl) {% sample lang="c" %} [import, lang:"c_cpp"](code/c/split_op.c) +{% sample lang="cpp" %} +[import, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:5-127, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} From ff3b24b5d4c1f0c03e4ddce036dedf7f752175f2 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Thu, 4 Oct 2018 19:36:20 +0200 Subject: [PATCH 02/19] Used vector instead of pointers --- .../code/c++/split_op.cpp | 186 ++++++------------ 1 file changed, 57 insertions(+), 129 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 2f9f1c940..581eaf54a 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -1,189 +1,120 @@ #include +#include #include #include // Using fftw3 library. #include -class Params { -public: +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 = new double[res]; dk = M_PI / xmax; - k = new double[res]; im_time = im; for (size_t i = 0; i < res; ++i) { - x[i] = xmax / res - xmax + i * (2.0 * xmax / res); + x.push_back(xmax / res - xmax + i * (2.0 * xmax / res)); if (i < res / 2) { - k[i] = i * M_PI / xmax; + k.push_back(i * M_PI / xmax); } else { - k[i] = ((double)i - res) * M_PI / xmax; + k.push_back((static_cast(i) - res) * M_PI / xmax); } } } - ~Params() { - delete[] x; - delete[] k; - } - - unsigned int getRes() const { - return res; - } - double getXmax() const { - return xmax; - } - double getDt() const { - return dt; - } - double getDx() const { - return dx; - } - double getDk() const { - return dk; - } - unsigned int getTimesteps() const { - return timesteps; - } - double * getXs() const { - return x; - } - double * getKs() const { - return k; - } - bool isImTime() const { - return im_time; - } - -protected: double xmax; unsigned int res; double dt; unsigned int timesteps; double dx; - double *x; + std::vector x; double dk; - double *k; + std::vector k; bool im_time; }; -class Operators { +struct Operators { public: Operators(Params &par, double voffset, double wfcoffset) { - size = par.getRes(); - v = new std::complex[size]; - pe = new std::complex[size]; - ke = new std::complex[size]; - wfc = new std::complex[size]; + size = par.res; for (size_t i = 0; i < size; ++i) { - v[i] = 0.5 * pow(par.getXs()[i] - voffset, 2); - wfc[i] = exp(-pow(par.getXs()[i] - wfcoffset, 2) / 2.0); + v.emplace_back(0.5 * pow(par.x[i] - voffset, 2)); + wfc.emplace_back(exp(-pow(par.x[i] - wfcoffset, 2) / 2.0)); - if (par.isImTime()) { - ke[i] = exp(-0.5 * par.getDt() * pow(par.getKs()[i], 2)); - pe[i] = exp(-0.5 * par.getDt() * v[i]); + if (par.im_time) { + ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2))); + pe.emplace_back(exp(-0.5 * par.dt * v[i])); } else { - ke[i] = exp(-0.5 * par.getDt() * pow(par.getKs()[i], 2) * std::complex(0.0, 1.0)); - pe[i] = exp(-0.5 * par.getDt() * v[i] * std::complex(0.0, 1.0)); + ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * std::complex(0.0, 1.0))); + pe.emplace_back(exp(-0.5 * par.dt * v[i] * std::complex(0.0, 1.0))); } } } - ~Operators() { - delete[] v; - delete[] pe; - delete[] ke; - delete[] wfc; - } - - size_t getSize() const { - return size; - } - std::complex *getV() const { - return v; - } - std::complex *getPe() const { - return pe; - } - std::complex *getKe() const { - return ke; - } - std::complex *getWfc() const { - return wfc; - } - -protected: size_t size; - std::complex *v; - std::complex *pe; - std::complex *ke; - std::complex *wfc; + std::vector> v; + std::vector> pe; + std::vector> ke; + std::vector> wfc; }; -void fft(std::complex *x, int n, bool inverse) { +void fft(std::vector> x, int n, bool inverse) { std::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); - } + p = fftw_plan_dft_1d(n, reinterpret_cast(x.data()), reinterpret_cast(y), + (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((double)n); + x[i] = y[i] / sqrt(static_cast(n)); } } void split_op(Params &par, Operators &opr) { - double density[opr.getSize()]; + double density[opr.size]; - for (size_t i = 0; i < par.getTimesteps(); ++i) { - for (size_t j = 0; j < opr.getSize(); ++j) { - opr.getWfc()[j] *= opr.getPe()[j]; + 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.getWfc(), opr.getSize(), false); + fft(opr.wfc, opr.size, false); - for (size_t j = 0; j < opr.getSize(); ++j) { - opr.getWfc()[j] *= opr.getKe()[j]; + for (size_t j = 0; j < opr.size; ++j) { + opr.wfc[j] *= opr.ke[j]; } - fft(opr.getWfc(), opr.getSize(), true); + fft(opr.wfc, opr.size, true); - for (size_t j = 0; j < opr.getSize(); ++j) { - opr.getWfc()[j] *= opr.getPe()[j]; + for (size_t j = 0; j < opr.size; ++j) { + opr.wfc[j] *= opr.pe[j]; } - for (size_t j = 0; j < opr.getSize(); ++j) { - density[j] = pow(abs(opr.getWfc()[j]), 2); + for (size_t j = 0; j < opr.size; ++j) { + density[j] = pow(abs(opr.wfc[j]), 2); } - if (par.isImTime()) { + if (par.im_time) { double sum = 0; - for (size_t j = 0; j < opr.getSize(); ++j) { + for (size_t j = 0; j < opr.size; ++j) { sum += density[j]; } - sum *= par.getDx(); + sum *= par.dx; - for (size_t j = 0; j < opr.getSize(); ++j) { - opr.getWfc()[j] /= sqrt(sum); + for (size_t j = 0; j < opr.size; ++j) { + opr.wfc[j] /= sqrt(sum); } } @@ -193,8 +124,8 @@ void split_op(Params &par, Operators &opr) { sprintf(filename, "output%lu.dat", i); FILE *fp = fopen(filename, "w"); - for (int i = 0; i < opr.getSize(); ++i) { - fprintf(fp, "%d\t%f\t%f\n", i, density[i], real(opr.getV()[i])); + for (int i = 0; i < opr.size; ++i) { + fprintf(fp, "%d\t%f\t%f\n", i, density[i], real(opr.v[i])); } fclose(fp); @@ -202,39 +133,36 @@ void split_op(Params &par, Operators &opr) { } double calculate_energy(Params par, Operators opr) { - std::complex wfc_r[opr.getSize()]; - std::complex wfc_k[opr.getSize()]; - std::complex wfc_c[opr.getSize()]; - std::memcpy(wfc_r, opr.getWfc(), sizeof(wfc_r)); - - std::memcpy(wfc_k, opr.getWfc(), sizeof(wfc_k)); - fft(wfc_k, opr.getSize(), false); + std::vector> wfc_r = std::vector(opr.wfc); + std::vector> wfc_k = std::vector(opr.wfc); + std::vector> wfc_c; + fft(wfc_k, opr.size, false); - for (size_t i = 0; i < opr.getSize(); ++i) { + for (size_t i = 0; i < opr.size; ++i) { wfc_c[i] = conj(wfc_r[i]); } - std::complex energy_k[opr.getSize()]; - std::complex energy_r[opr.getSize()]; + std::vector> energy_k; + std::vector> energy_r; - for (size_t i = 0; i < opr.getSize(); ++i) { - energy_k[i] = wfc_k[i] * pow(par.getKs()[i] + 0.0*std::complex(0.0, 1.0), 2); + for (size_t i = 0; i < opr.size; ++i) { + energy_k[i] = wfc_k[i] * pow(std::complex(par.k[i], 0.0), 2); } - fft(energy_k, opr.getSize(), true); + fft(energy_k, opr.size, true); - for (size_t i = 0; i < opr.getSize(); ++i) { + for (size_t i = 0; i < opr.size; ++i) { energy_k[i] *= 0.5 * wfc_c[i]; - energy_r[i] = wfc_c[i] * opr.getV()[i] * wfc_r[i]; + energy_r[i] = wfc_c[i] * opr.v[i] * wfc_r[i]; } double energy_final = 0; - for (size_t i = 0; i < opr.getSize(); ++i) { + for (size_t i = 0; i < opr.size; ++i) { energy_final += real(energy_k[i] + energy_r[i]); } - return energy_final * par.getDx(); + return energy_final * par.dx; } int main() { From fb09a9c55230b79ace4a5399f0be0f16bb1e4b03 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Thu, 4 Oct 2018 21:35:58 +0200 Subject: [PATCH 03/19] Used streams instead of files --- .../split-operator_method/code/c++/split_op.cpp | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 581eaf54a..d98e294ef 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -2,6 +2,7 @@ #include #include #include +#include // Using fftw3 library. #include @@ -122,13 +123,18 @@ void split_op(Params &par, Operators &opr) { // index, density, real potential. char filename[256]; sprintf(filename, "output%lu.dat", i); - FILE *fp = fopen(filename, "w"); + std::ofstream fstream; + fstream.open(filename, std::fstream::out); - for (int i = 0; i < opr.size; ++i) { - fprintf(fp, "%d\t%f\t%f\n", i, density[i], real(opr.v[i])); + char buffer[1023]; + if (!fstream.fail()) { + for (int i = 0; i < opr.size; ++i) { + snprintf(buffer, 1023, "%d\t%f\t%f\n", i, density[i], real(opr.v[i])); + fstream.write(buffer, strlen(buffer)); + } } - fclose(fp); + fstream.close(); } } From 2090f2232104936df01fe5e64bc4a541ffda2d05 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Thu, 4 Oct 2018 21:42:00 +0200 Subject: [PATCH 04/19] Updated MarkDown --- contents/split-operator_method/split-operator_method.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/split-operator_method/split-operator_method.md b/contents/split-operator_method/split-operator_method.md index dc5c85270..4949a7559 100644 --- a/contents/split-operator_method/split-operator_method.md +++ b/contents/split-operator_method/split-operator_method.md @@ -103,7 +103,7 @@ Regardless, we first need to set all the initial parameters, including the initi [import:11-21, lang:"c_cpp"](code/c/split_op.c) [import:52-73, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:8-74, lang:"c_cpp"](code/c++/split_op.cpp) +[import:10-39, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:11-30, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -124,7 +124,7 @@ Afterwards, we turn them into operators: [import:23-29, lang:"c_cpp"](code/c/split_op.c) [import:75-96, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:76-129, lang:"c_cpp"](code/c++/split_op.cpp) +[import:41-66, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:33-54, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -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:152-202, lang:"c_cpp"](code/c++/split_op.cpp) +[import:68-172, 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 7aba6f4c7a67f7d03b3133a78553ed55e101b198 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Fri, 5 Oct 2018 20:01:57 +0200 Subject: [PATCH 05/19] Reserve vector before pushing elements to it --- contents/split-operator_method/code/c++/split_op.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index d98e294ef..d229ffb38 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -14,7 +14,9 @@ struct Params { 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) { @@ -43,6 +45,10 @@ struct Operators { Operators(Params &par, double voffset, double wfcoffset) { size = par.res; + v.reserve(size); + pe.reserve(size); + ke.reserve(size); + wfc.reserve(size); for (size_t i = 0; i < size; ++i) { v.emplace_back(0.5 * pow(par.x[i] - voffset, 2)); From d4fd4b704d5eb901f3f6ed2917a9d9695c129656 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Fri, 5 Oct 2018 20:02:32 +0200 Subject: [PATCH 06/19] Using std::stringstreams instead of char arrays --- .../code/c++/split_op.cpp | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index d229ffb38..93dabfd7b 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -127,16 +127,18 @@ void split_op(Params &par, Operators &opr) { // Writing data into a file in the format of: // index, density, real potential. - char filename[256]; - sprintf(filename, "output%lu.dat", i); - std::ofstream fstream; - fstream.open(filename, std::fstream::out); + std::stringstream filename_stream; + filename_stream << "output" << i << ".dat"; - char buffer[1023]; - if (!fstream.fail()) { + std::ofstream fstream = std::ofstream(filename_stream.str()); + + if (fstream) { for (int i = 0; i < opr.size; ++i) { - snprintf(buffer, 1023, "%d\t%f\t%f\n", i, density[i], real(opr.v[i])); - fstream.write(buffer, strlen(buffer)); + 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()); } } From cc09d8af449c58e7d7bd55fccf493ba7a6a61935 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Fri, 5 Oct 2018 20:03:19 +0200 Subject: [PATCH 07/19] Used std::cout instead of printf --- contents/split-operator_method/code/c++/split_op.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 93dabfd7b..814ef2a0d 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -185,7 +185,7 @@ int main() { split_op(par, opr); - printf("The energy is %f\n", calculate_energy(par, opr)); + std::cout << "The energy is " << calculate_energy(par, opr) << "\n"; return 0; } From e7393d644860e227caac749dd82dbcc9873cb1f3 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Fri, 5 Oct 2018 20:23:59 +0200 Subject: [PATCH 08/19] Updated MarkDown --- contents/split-operator_method/split-operator_method.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/split-operator_method/split-operator_method.md b/contents/split-operator_method/split-operator_method.md index 4949a7559..1e33705e9 100644 --- a/contents/split-operator_method/split-operator_method.md +++ b/contents/split-operator_method/split-operator_method.md @@ -103,7 +103,7 @@ Regardless, we first need to set all the initial parameters, including the initi [import:11-21, lang:"c_cpp"](code/c/split_op.c) [import:52-73, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:10-39, lang:"c_cpp"](code/c++/split_op.cpp) +[import:10-41, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:11-30, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -124,7 +124,7 @@ Afterwards, we turn them into operators: [import:23-29, lang:"c_cpp"](code/c/split_op.c) [import:75-96, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:41-66, lang:"c_cpp"](code/c++/split_op.cpp) +[import:43-72, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:33-54, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -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:68-172, lang:"c_cpp"](code/c++/split_op.cpp) +[import:74-180, 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 a511643500f04a5ef0601f29ea0ed3ee4f249b79 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 6 Oct 2018 09:53:59 +0200 Subject: [PATCH 09/19] Fixed segmentation faults --- .../split-operator_method/code/c++/split_op.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 814ef2a0d..362156ab3 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -58,8 +58,8 @@ struct Operators { ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2))); pe.emplace_back(exp(-0.5 * par.dt * v[i])); } else { - ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * std::complex(0.0, 1.0))); - pe.emplace_back(exp(-0.5 * par.dt * v[i] * std::complex(0.0, 1.0))); + ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * std::complex(0.0, 1.0))); + pe.emplace_back(exp(-0.5 * par.dt * v[i] * std::complex(0.0, 1.0))); } } } @@ -147,27 +147,27 @@ void split_op(Params &par, Operators &opr) { } double calculate_energy(Params par, Operators opr) { - std::vector> wfc_r = std::vector(opr.wfc); - std::vector> wfc_k = std::vector(opr.wfc); + std::vector> wfc_r(opr.wfc); + std::vector> wfc_k(opr.wfc); std::vector> wfc_c; fft(wfc_k, opr.size, false); for (size_t i = 0; i < opr.size; ++i) { - wfc_c[i] = conj(wfc_r[i]); + wfc_c.push_back(conj(wfc_r[i])); } std::vector> energy_k; std::vector> energy_r; for (size_t i = 0; i < opr.size; ++i) { - energy_k[i] = wfc_k[i] * pow(std::complex(par.k[i], 0.0), 2); + energy_k.push_back(wfc_k[i] * pow(std::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]; + energy_r.push_back(wfc_c[i] * opr.v[i] * wfc_r[i]); } double energy_final = 0; From 147fc2852af3199c9cdc110c750121d83456d739 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 6 Oct 2018 10:07:41 +0200 Subject: [PATCH 10/19] Added some space allocation with the vectors --- contents/split-operator_method/code/c++/split_op.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 362156ab3..e9d960b72 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -150,6 +150,7 @@ double calculate_energy(Params par, Operators opr) { std::vector> wfc_r(opr.wfc); std::vector> wfc_k(opr.wfc); std::vector> wfc_c; + wfc_c.reserve(opr.size); fft(wfc_k, opr.size, false); for (size_t i = 0; i < opr.size; ++i) { @@ -158,6 +159,8 @@ double calculate_energy(Params par, Operators opr) { std::vector> energy_k; std::vector> energy_r; + energy_k.reserve(opr.size); + energy_r.reserve(opr.size); for (size_t i = 0; i < opr.size; ++i) { energy_k.push_back(wfc_k[i] * pow(std::complex(par.k[i], 0.0), 2)); From 5eb7c53ee69d5e4c672387ef8f483d1dae179262 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 6 Oct 2018 10:33:34 +0200 Subject: [PATCH 11/19] Updated MarkDown --- 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 1e33705e9..93dfa5ca2 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:74-180, lang:"c_cpp"](code/c++/split_op.cpp) +[import:74-183, 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 7bcd36b004b56144debd8204222b26912e7ba0ef Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sun, 7 Oct 2018 10:11:08 +0200 Subject: [PATCH 12/19] Used vector constructor to reserve space --- contents/split-operator_method/code/c++/split_op.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index e9d960b72..b79ff81cf 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -149,18 +149,15 @@ void split_op(Params &par, Operators &opr) { double calculate_energy(Params par, Operators opr) { std::vector> wfc_r(opr.wfc); std::vector> wfc_k(opr.wfc); - std::vector> wfc_c; - wfc_c.reserve(opr.size); + std::vector> wfc_c(opr.size); fft(wfc_k, opr.size, false); for (size_t i = 0; i < opr.size; ++i) { wfc_c.push_back(conj(wfc_r[i])); } - std::vector> energy_k; - std::vector> energy_r; - energy_k.reserve(opr.size); - energy_r.reserve(opr.size); + std::vector> energy_k(opr.size); + std::vector> energy_r(opr.size); for (size_t i = 0; i < opr.size; ++i) { energy_k.push_back(wfc_k[i] * pow(std::complex(par.k[i], 0.0), 2)); From 0d8a8875ffb882de3b6504d034be72772b0500c5 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 13 Oct 2018 15:33:21 +0200 Subject: [PATCH 13/19] Used references instead of copies --- contents/split-operator_method/code/c++/split_op.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index b79ff81cf..26dcc197e 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -71,7 +71,7 @@ struct Operators { std::vector> wfc; }; -void fft(std::vector> x, int n, bool inverse) { +void fft(std::vector> &x, int n, bool inverse) { std::complex y[n]; memset(y, 0, sizeof(y)); fftw_plan p; @@ -146,7 +146,7 @@ void split_op(Params &par, Operators &opr) { } } -double calculate_energy(Params par, Operators opr) { +double calculate_energy(Params &par, Operators &opr) { std::vector> wfc_r(opr.wfc); std::vector> wfc_k(opr.wfc); std::vector> wfc_c(opr.size); From 83fcf6ec6fd7223d4634e11a37bb48e6d0d2d6a6 Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 13 Oct 2018 15:48:39 +0200 Subject: [PATCH 14/19] Simplified types, parameters and defined pi iff not defined --- .../code/c++/split_op.cpp | 44 ++++++++++++------- 1 file changed, 27 insertions(+), 17 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 26dcc197e..79bf79f90 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -7,6 +7,14 @@ // Using fftw3 library. #include +#ifndef M_PI +#define M_PI 3.14159265358979323846 +#endif + +using complex = std::complex; +using vector_real = std::vector; +using vector_complex = std::vector; + struct Params { Params(double _xmax, unsigned int _res, double _dt, unsigned int _timesteps, bool im) { xmax = _xmax; @@ -34,9 +42,9 @@ struct Params { double dt; unsigned int timesteps; double dx; - std::vector x; + vector_real x; double dk; - std::vector k; + vector_real k; bool im_time; }; @@ -58,25 +66,27 @@ struct Operators { ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2))); pe.emplace_back(exp(-0.5 * par.dt * v[i])); } else { - ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * std::complex(0.0, 1.0))); - pe.emplace_back(exp(-0.5 * par.dt * v[i] * std::complex(0.0, 1.0))); + ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * complex(0.0, 1.0))); + pe.emplace_back(exp(-0.5 * par.dt * v[i] * complex(0.0, 1.0))); } } } size_t size; - std::vector> v; - std::vector> pe; - std::vector> ke; - std::vector> wfc; + vector_complex v; + vector_complex pe; + vector_complex ke; + vector_complex wfc; }; -void fft(std::vector> &x, int n, bool inverse) { - std::complex y[n]; +void fft(vector_complex &x, int n, bool inverse) { + complex y[n]; memset(y, 0, sizeof(y)); fftw_plan p; - p = fftw_plan_dft_1d(n, reinterpret_cast(x.data()), reinterpret_cast(y), + 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); @@ -147,20 +157,20 @@ void split_op(Params &par, Operators &opr) { } double calculate_energy(Params &par, Operators &opr) { - std::vector> wfc_r(opr.wfc); - std::vector> wfc_k(opr.wfc); - std::vector> wfc_c(opr.size); + 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.push_back(conj(wfc_r[i])); } - std::vector> energy_k(opr.size); - std::vector> energy_r(opr.size); + vector_complex energy_k(opr.size); + vector_complex energy_r(opr.size); for (size_t i = 0; i < opr.size; ++i) { - energy_k.push_back(wfc_k[i] * pow(std::complex(par.k[i], 0.0), 2)); + energy_k.push_back(wfc_k[i] * pow(complex(par.k[i], 0.0), 2)); } fft(energy_k, opr.size, true); From eb0b0dde891dc9aa63cbfcb467c85b47947ca87f Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 13 Oct 2018 15:51:18 +0200 Subject: [PATCH 15/19] Removed spacing in empty lines --- .../code/c++/split_op.cpp | 70 +++++++++---------- 1 file changed, 35 insertions(+), 35 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 79bf79f90..e6062ab0a 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -26,7 +26,7 @@ struct Params { dk = M_PI / xmax; k.reserve(res); im_time = im; - + for (size_t i = 0; i < res; ++i) { x.push_back(xmax / res - xmax + i * (2.0 * xmax / res)); if (i < res / 2) { @@ -36,7 +36,7 @@ struct Params { } } } - + double xmax; unsigned int res; double dt; @@ -57,11 +57,11 @@ struct Operators { pe.reserve(size); ke.reserve(size); wfc.reserve(size); - + for (size_t i = 0; i < size; ++i) { v.emplace_back(0.5 * pow(par.x[i] - voffset, 2)); wfc.emplace_back(exp(-pow(par.x[i] - wfcoffset, 2) / 2.0)); - + if (par.im_time) { ke.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2))); pe.emplace_back(exp(-0.5 * par.dt * v[i])); @@ -71,7 +71,7 @@ struct Operators { } } } - + size_t size; vector_complex v; vector_complex pe; @@ -83,15 +83,15 @@ 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(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 < n; ++i) { x[i] = y[i] / sqrt(static_cast(n)); } @@ -99,59 +99,59 @@ void fft(vector_complex &x, int n, bool inverse) { 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(); } } @@ -161,41 +161,41 @@ double calculate_energy(Params &par, Operators &opr) { 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.push_back(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.push_back(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.push_back(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; } From ea8d597b38dd9f5058945e51da9bcaf87760a65c Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sat, 13 Oct 2018 19:21:03 +0200 Subject: [PATCH 16/19] Fixed vector value assignment --- contents/split-operator_method/code/c++/split_op.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index e6062ab0a..5c98b094e 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -163,21 +163,21 @@ double calculate_energy(Params &par, Operators &opr) { fft(wfc_k, opr.size, false); for (size_t i = 0; i < opr.size; ++i) { - wfc_c.push_back(conj(wfc_r[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.push_back(wfc_k[i] * pow(complex(par.k[i], 0.0), 2)); + 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.push_back(wfc_c[i] * opr.v[i] * wfc_r[i]); + energy_r[i] = wfc_c[i] * opr.v[i] * wfc_r[i]; } double energy_final = 0; From e9951bee359afd5c637d9aa60deff8d20149b91e Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sun, 14 Oct 2018 19:46:48 +0200 Subject: [PATCH 17/19] Changed emplace_back to push_back --- .../split-operator_method/code/c++/split_op.cpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/contents/split-operator_method/code/c++/split_op.cpp b/contents/split-operator_method/code/c++/split_op.cpp index 5c98b094e..efdad46c1 100644 --- a/contents/split-operator_method/code/c++/split_op.cpp +++ b/contents/split-operator_method/code/c++/split_op.cpp @@ -28,7 +28,7 @@ struct Params { im_time = im; for (size_t i = 0; i < res; ++i) { - x.push_back(xmax / res - xmax + i * (2.0 * xmax / res)); + x.emplace_back(xmax / res - xmax + i * (2.0 * xmax / res)); if (i < res / 2) { k.push_back(i * M_PI / xmax); } else { @@ -59,15 +59,15 @@ struct Operators { wfc.reserve(size); for (size_t i = 0; i < size; ++i) { - v.emplace_back(0.5 * pow(par.x[i] - voffset, 2)); - wfc.emplace_back(exp(-pow(par.x[i] - wfcoffset, 2) / 2.0)); + 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.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2))); - pe.emplace_back(exp(-0.5 * par.dt * v[i])); + 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.emplace_back(exp(-0.5 * par.dt * pow(par.k[i], 2) * complex(0.0, 1.0))); - pe.emplace_back(exp(-0.5 * par.dt * v[i] * complex(0.0, 1.0))); + 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))); } } } From 7675547978adc1aaae6f2caa44db98545225fafc Mon Sep 17 00:00:00 2001 From: rajdakin Date: Sun, 14 Oct 2018 20:29:10 +0200 Subject: [PATCH 18/19] Updated MarkDown --- contents/split-operator_method/split-operator_method.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/contents/split-operator_method/split-operator_method.md b/contents/split-operator_method/split-operator_method.md index 93dfa5ca2..1d50c47dd 100644 --- a/contents/split-operator_method/split-operator_method.md +++ b/contents/split-operator_method/split-operator_method.md @@ -103,7 +103,7 @@ Regardless, we first need to set all the initial parameters, including the initi [import:11-21, lang:"c_cpp"](code/c/split_op.c) [import:52-73, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:10-41, lang:"c_cpp"](code/c++/split_op.cpp) +[import:14-49, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:11-30, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -124,7 +124,7 @@ Afterwards, we turn them into operators: [import:23-29, lang:"c_cpp"](code/c/split_op.c) [import:75-96, lang:"c_cpp"](code/c/split_op.c) {% sample lang="cpp" %} -[import:43-72, lang:"c_cpp"](code/c++/split_op.cpp) +[import:51-80, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:33-54, lang:"python"](code/python/split_op.py) {% sample lang="hs" %} @@ -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:74-183, lang:"c_cpp"](code/c++/split_op.cpp) +[import:82-190, 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 52e566f5602ddf461de335a0474f4279a154c9ee Mon Sep 17 00:00:00 2001 From: rajdakin Date: Wed, 17 Oct 2018 12:11:40 +0200 Subject: [PATCH 19/19] Updated MarkDown --- 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 1d50c47dd..ff74f99d9 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:82-190, lang:"c_cpp"](code/c++/split_op.cpp) +[import:100-157, lang:"c_cpp"](code/c++/split_op.cpp) {% sample lang="py" %} [import:57-95, lang:"python"](code/python/split_op.py) {% sample lang="hs" %}