From 8c15cf21b2df866195d7f2666031d5d39579097e Mon Sep 17 00:00:00 2001 From: "matthias@quintern.xyz" Date: Sun, 9 Mar 2025 22:03:26 +0100 Subject: [PATCH] cleanup --- src/wasm/result.hpp | 14 +++++++++ src/wasm/run.cpp | 27 ++++++++++------ src/wasm/run.hpp | 75 ++++++++++++++++++++++++--------------------- 3 files changed, 71 insertions(+), 45 deletions(-) diff --git a/src/wasm/result.hpp b/src/wasm/result.hpp index 1faf6cb..3f03e07 100644 --- a/src/wasm/result.hpp +++ b/src/wasm/result.hpp @@ -19,6 +19,10 @@ class Results { virtual ~Results() {}; virtual postUpdateFunction_t getPostUpdateFunction(std::shared_ptr model) = 0; virtual bool postUpdateFunction(std::shared_ptr model, const MPS& psi) = 0; + virtual void finalize(std::shared_ptr model, const MPS& psi) = 0; + /// Norm of the MPS + dtype finalNorm = 0; + std::vector finalStates; }; @@ -27,6 +31,7 @@ class TfiResults : public Results { using Model_t = TfiModel; TfiResults(ems::val callback, double convergence) : callback(callback), convergence(convergence), previousE(std::numeric_limits::max()) { sigmaZ.setValues({{1, 0}, {0, -1}}); + // sigmaZ.setValues({{0, 1}, {1, 0}}); }; /// @todo write a second postUpdateFunction without the JS callback and return that one if callback is null postUpdateFunction_t getPostUpdateFunction(std::shared_ptr model) override { @@ -44,6 +49,10 @@ class TfiResults : public Results { previousE = Eval; return false; } + void finalize(std::shared_ptr model, const MPS& psi) override { + this->finalNorm = psi.collapse(0); + this->finalStates = psi.getSiteExpectationValues(this->sigmaZ); + } private: e::TensorFixedSize> sigmaZ; ems::val callback; @@ -74,6 +83,11 @@ class BoseHubbardResults : public Results { previousE = Eval; return false; } + void finalize(std::shared_ptr model, const MPS& psi) override { + e::TensorMap> particleNumberOperator(model->getLadderOperators().getN().data(), model->localDim, model->localDim); + this->finalNorm = psi.collapse(0); + this->finalStates = psi.getSiteExpectationValues(particleNumberOperator); + } private: ems::val callback; double convergence; diff --git a/src/wasm/run.cpp b/src/wasm/run.cpp index 06251e0..0e20875 100644 --- a/src/wasm/run.cpp +++ b/src/wasm/run.cpp @@ -48,8 +48,14 @@ int testPerformance() { EMSCRIPTEN_BINDINGS(run) { // see https://emscripten.org/docs/porting/connecting_cpp_and_javascript/embind.html#advanced-class-concepts // RESULTS - ems::class_>("ResultsBaseTfi"); - ems::class_>("ResultsBaseBh"); + ems::class_>("ResultsBaseTfi") + .property("finalNorm", &Results::finalNorm) + .property("finalStates", &Results::finalStates, ems::return_value_policy::reference()) + ; + ems::class_>("ResultsBaseBh") + .property("finalNorm", &Results::finalNorm) + .property("finalStates", &Results::finalStates, ems::return_value_policy::reference()) + ; ems::class_>>("ResultsTfi") .constructor() ; @@ -76,20 +82,13 @@ EMSCRIPTEN_BINDINGS(run) { ems::function("initMpsSpinUp", &initMPS_spinUp); ems::function("initMpsSpinRight", &initMPS_spinRight); ems::function("initMpsNParticles", &initMPS_nParticles); - // ems::function("initMPS_spinUp", &initMPS_spinUp); - ems::register_vector("stdVectorDouble"); - ems::register_vector>("stdVectorDouble2D"); - // ems::class_("TEBDResults") - // .property("M", &Results::M) - // .property("E", &Results::E) - // .property("S", &Results::S) - // ; // TEBD ems::enum_("TEBD_TYPE") .value("BRICKWALL", TEBD_TYPE::BRICKWALL) .value("SECOND_ORDER", TEBD_TYPE::SECOND_ORDER) ; ems::function("runTebdTfi", &runTEBD); + ems::function("runTebdEvolveTfi", &runTEBD_evolve, Range>); ems::function("runTebdPhaseDiagramTfi", &runTEBD_phaseDiagram, Range>); ems::function("runTebdBh", &runTEBD); ems::function("runTebdPhaseDiagramBh", &runTEBD_phaseDiagram, Range, unsigned>); @@ -102,6 +101,14 @@ EMSCRIPTEN_BINDINGS(run) { ems::function("test", &test); ems::function("getBuildOptions", &getBuildOptions); ems::function("testPerformance", &testPerformance); + // BASICS + ems::register_vector("stdVectorDouble"); + ems::register_vector("stdVectorDType"); + ems::register_vector>("stdVectorDouble2D"); + ems::class_("dtype") + .function("real", ems::select_overload(&dtype::real)) + .function("imag", ems::select_overload(&dtype::imag)) + ; }; #endif diff --git a/src/wasm/run.hpp b/src/wasm/run.hpp index f46fed2..f1b1333 100644 --- a/src/wasm/run.hpp +++ b/src/wasm/run.hpp @@ -26,19 +26,6 @@ // std::apply([](auto&&... args){ printArgs(args...); }, results); // } -/** - */ - -// EMSCRIPTEN_BINDINGS(my_class_example) { -// class_("MyClass") -// .constructor() -// .function("incrementX", &MyClass::incrementX) -// .property("x", &MyClass::getX, &MyClass::setX) -// .property("x_readonly", &MyClass::getX) -// .class_function("getStringFromInstance", &MyClass::getStringFromInstance) -// ; -// }; - /** * @brief Return a string that was set at compilation using the BUILD_OPTIONS_STRING variable */ @@ -49,16 +36,12 @@ enum class TEBD_TYPE { BRICKWALL = 0, SECOND_ORDER = 1, }; -// EMSCRIPTEN_BINDINGS(tebd_module) { -// enum_(TEBD_BRICKWALL) -// .value("TEBD_BRICKWALL", TEBD_BRICKWALL) -// ; -// class>("MPS") -// .constructor() -// .function("getL", &MPS::getL) -// .function("getN", &MPS::getN) -// .function("getN_real", -// }; + +inline bool post_update_sleep(const MPS&, unsigned) { + emscripten_sleep(0); + return false; +}; +static postUpdateFunction_t post_update_sleep_F(post_update_sleep); // TEBD template @@ -90,8 +73,34 @@ void runTEBD(MPS& psi, const std::shared_ptr model, unsigned nStep } std::println("Chis: {}", psi.getChi()); std::println("Collapse: {}", psi.collapse(0)); + result.finalize(model, psi); }; +/// TODO everything +template +void runTEBD_evolve(const MPS& psiI, unsigned nSteps, double dtReal, double dtImag, double _eps, unsigned chiMax, TEBD_TYPE type, unsigned nValues, Results& result, ModelArgs&&... args) { + dtype eps(_eps); + dtype dt(dtReal, dtImag); + // sanity checks + throwIfNaN(eps, "eps"); + throwIfNaN(dt, "dt"); + throwIfLessThan(nSteps, 2u, "nSteps"); + throwIfLessThan(chiMax, 1u, "chiMax"); + std::println("Running TEBD type={}, nSteps={}, dt={}, eps={}, chiMax={}", static_cast(type), nSteps, dt, eps, chiMax); + MPS psi(psiI); + + for (unsigned i = 0; i < nValues; i++) { + std::shared_ptr model = makeModel(std::forward(args)...); + std::println("{:02} - Model: {}", i, *model); + // std::println("New psi: value at B[2](0, 1, 0)={}", psi.B(2)(0, 1, 0)); + // printTensor(psi.B(0), "psi.B[0]"); + // printTensor(psi.B(1), "psi.B[1]"); + const Bonds bonds = tebd::getTimeEvolutionMatrices(model->getH_BondsMatrices(), dt, model->localDim); + tebd::runBrickwall(psi, bonds, chiMax, eps, 1, post_update_sleep_F); + result.postUpdateFunction(model, psi); + } +} + template void runTEBD_phaseDiagram(const MPS& psiI, unsigned nSteps, double dtReal, double dtImag, double _eps, unsigned chiMax, TEBD_TYPE type, unsigned nValues, Results& result, ModelArgs&&... args) { @@ -104,17 +113,15 @@ void runTEBD_phaseDiagram(const MPS& psiI, unsigned nSteps, double dtReal throwIfLessThan(chiMax, 1u, "chiMax"); std::println("Running TEBD type={}, nSteps={}, dt={}, eps={}, chiMax={}", static_cast(type), nSteps, dt, eps, chiMax); - postUpdateFunction_t F = [](const MPS&, unsigned) { - emscripten_sleep(0); - return false; - }; for (unsigned i = 0; i < nValues; i++) { std::shared_ptr model = makeModel(std::forward(args)...); std::println("{:02} - Model: {}", i, *model); MPS psi(psiI); - std::println("New psi: value at B[2](0, 1, 0)={}", psi.B(2)(0, 1, 0)); + // std::println("New psi: value at B[2](0, 1, 0)={}", psi.B(2)(0, 1, 0)); + // printTensor(psi.B(0), "psi.B[0]"); + // printTensor(psi.B(1), "psi.B[1]"); const Bonds bonds = tebd::getTimeEvolutionMatrices(model->getH_BondsMatrices(), dt, model->localDim); - tebd::runBrickwall(psi, bonds, chiMax, eps, nSteps, F); + tebd::runBrickwall(psi, bonds, chiMax, eps, nSteps, post_update_sleep_F); result.postUpdateFunction(model, psi); } } @@ -137,9 +144,10 @@ void runDMRG(MPS& psi, const std::shared_ptr model, unsigned nSwee // initial values now F(psi, 0); - dmrg::run(psi, model->getH_MPO(), chiMax, eps, nSweeps, F); + dmrg::run(psi, model->getH_MPO(), chiMax, eps, nSweeps, F, post_update_sleep_F); std::println("Chis: {}", psi.getChi()); std::println("Collapse: {}", psi.collapse(0)); + result.finalize(model, psi); }; @@ -152,15 +160,12 @@ void runDMRG_phaseDiagram(const MPS& psiI, unsigned nSweeps, double _eps, throwIfLessThan(chiMax, 1u, "chiMax"); std::println("Running DMRG nSweeps={}, eps={}, chiMax={}", nSweeps, eps, chiMax); - postUpdateFunction_t F = [](const MPS&, unsigned) { - emscripten_sleep(0); - return false; - }; for (unsigned i = 0; i < nValues; i++) { std::shared_ptr model = makeModel(std::forward(args)...); std::println("{:02} - Model: {}", i, *model); MPS psi(psiI); - dmrg::run(psi, model->getH_MPO(), chiMax, eps, nSweeps, F); + dmrg::run(psi, model->getH_MPO(), chiMax, eps, nSweeps, post_update_sleep_F, post_update_sleep_F); result.postUpdateFunction(model, psi); + std::println("Collapse: {}", psi.collapse(0)); } }