From 734f47bcef9e99052decf2ea8d605c151350e7a3 Mon Sep 17 00:00:00 2001 From: Mikhail Svechnikov <m.svechnikov@fz-juelich.de> Date: Wed, 21 Aug 2024 17:27:30 +0200 Subject: [PATCH 1/3] auto parse code --- Sim/Export/ExportToPython.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Sim/Export/ExportToPython.cpp b/Sim/Export/ExportToPython.cpp index 42e5f6c012c..9d44447b981 100644 --- a/Sim/Export/ExportToPython.cpp +++ b/Sim/Export/ExportToPython.cpp @@ -13,13 +13,15 @@ // ************************************************************************************************ #include "Sim/Export/ExportToPython.h" +#include "Base/Py/PyFmt.h" #include "Sim/Export/SampleToPython.h" #include "Sim/Export/SimulationToPython.h" #include "Sim/Simulation/ISimulation.h" std::string Py::Export::sampleCode(const MultiLayer& sample) { - return SampleToPython().sampleCode(sample); + std::string code = SampleToPython().sampleCode(sample); + return "import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code) + "\n\n" + code; } std::string Py::Export::simulationPlotCode(const ISimulation& simulation) -- GitLab From 6f782e0c88e6bd84b135e4373ac9acd5b1581fb6 Mon Sep 17 00:00:00 2001 From: Mikhail Svechnikov <m.svechnikov@fz-juelich.de> Date: Wed, 21 Aug 2024 17:33:59 +0200 Subject: [PATCH 2/3] use Py::Export::codeHeader --- Sim/Export/ExportToPython.cpp | 8 ++++-- Sim/Export/ExportToPython.h | 1 + Sim/Export/SampleToPython.cpp | 4 +-- Sim/Export/SimulationToPython.cpp | 3 ++- auto/Wrap/libBornAgainSim.py | 4 +++ auto/Wrap/libBornAgainSim_wrap.cpp | 42 ++++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+), 6 deletions(-) diff --git a/Sim/Export/ExportToPython.cpp b/Sim/Export/ExportToPython.cpp index 9d44447b981..aca14763ec2 100644 --- a/Sim/Export/ExportToPython.cpp +++ b/Sim/Export/ExportToPython.cpp @@ -18,12 +18,16 @@ #include "Sim/Export/SimulationToPython.h" #include "Sim/Simulation/ISimulation.h" -std::string Py::Export::sampleCode(const MultiLayer& sample) +std::string Py::Export::codeHeader(const std::string& code) { - std::string code = SampleToPython().sampleCode(sample); return "import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code) + "\n\n" + code; } +std::string Py::Export::sampleCode(const MultiLayer& sample) +{ + return Py::Export::codeHeader(SampleToPython().sampleCode(sample)); +} + std::string Py::Export::simulationPlotCode(const ISimulation& simulation) { return SimulationToPython().simulationPlotCode(simulation); diff --git a/Sim/Export/ExportToPython.h b/Sim/Export/ExportToPython.h index 9cfe6a291a3..4f1e3ba35d1 100644 --- a/Sim/Export/ExportToPython.h +++ b/Sim/Export/ExportToPython.h @@ -24,6 +24,7 @@ class MultiLayer; namespace Py::Export { +std::string codeHeader(const std::string& code); std::string sampleCode(const MultiLayer& sample); std::string simulationPlotCode(const ISimulation& simulation); std::string simulationSaveCode(const ISimulation& simulation, const std::string& fname); diff --git a/Sim/Export/SampleToPython.cpp b/Sim/Export/SampleToPython.cpp index 0912c3bec56..f1192268671 100644 --- a/Sim/Export/SampleToPython.cpp +++ b/Sim/Export/SampleToPython.cpp @@ -597,9 +597,7 @@ std::string SampleToPython::sampleCode(const MultiLayer& sample) objHandler.insertModel("crystal", x); // clang-format off - return "import bornagain as ba\n" - "from bornagain import deg, nm, R3\n\n" - "def get_sample():\n" + return "def get_sample():\n" + defineMaterials(matHandler) + defineFormfactors(objHandler) + defineParticles(objHandler, matHandler) diff --git a/Sim/Export/SimulationToPython.cpp b/Sim/Export/SimulationToPython.cpp index 9edb89e94fa..2fecdd1a71a 100644 --- a/Sim/Export/SimulationToPython.cpp +++ b/Sim/Export/SimulationToPython.cpp @@ -29,6 +29,7 @@ #include "Resample/Option/SimulationOptions.h" #include "Sim/Background/ConstantBackground.h" #include "Sim/Background/PoissonBackground.h" +#include "Sim/Export/ExportToPython.h" #include "Sim/Export/PyFmt2.h" #include "Sim/Export/SampleToPython.h" #include "Sim/Scan/AlphaScan.h" @@ -367,7 +368,7 @@ std::string simulationCode(const ISimulation& simulation) ASSERT(simulation.sample()); std::string code = SampleToPython().sampleCode(*simulation.sample()) + defineSimulate(simulation); - return "import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code) + "\n\n" + code; + return Py::Export::codeHeader(code); } } // namespace diff --git a/auto/Wrap/libBornAgainSim.py b/auto/Wrap/libBornAgainSim.py index 6aa02d8ad81..4f9e7e72216 100644 --- a/auto/Wrap/libBornAgainSim.py +++ b/auto/Wrap/libBornAgainSim.py @@ -2861,6 +2861,10 @@ class PoissonBackground(IBackground): # Register PoissonBackground in _libBornAgainSim: _libBornAgainSim.PoissonBackground_swigregister(PoissonBackground) +def codeHeader(code): + r"""codeHeader(std::string const & code) -> std::string""" + return _libBornAgainSim.codeHeader(code) + def sampleCode(sample): r"""sampleCode(MultiLayer const & sample) -> std::string""" return _libBornAgainSim.sampleCode(sample) diff --git a/auto/Wrap/libBornAgainSim_wrap.cpp b/auto/Wrap/libBornAgainSim_wrap.cpp index 96ffc33827d..ac499a6361c 100644 --- a/auto/Wrap/libBornAgainSim_wrap.cpp +++ b/auto/Wrap/libBornAgainSim_wrap.cpp @@ -37411,6 +37411,47 @@ SWIGINTERN PyObject *PoissonBackground_swiginit(PyObject *SWIGUNUSEDPARM(self), return SWIG_Python_InitShadowInstance(args); } +SWIGINTERN PyObject *_wrap_codeHeader(PyObject *self, PyObject *args) { + PyObject *resultobj = 0; + std::string *arg1 = 0 ; + int res1 = SWIG_OLDOBJ ; + PyObject *swig_obj[1] ; + std::string result; + + (void)self; + if (!args) SWIG_fail; + swig_obj[0] = args; + { + std::string *ptr = (std::string *)0; + res1 = SWIG_AsPtr_std_string(swig_obj[0], &ptr); + if (!SWIG_IsOK(res1)) { + SWIG_exception_fail(SWIG_ArgError(res1), "in method '" "codeHeader" "', argument " "1"" of type '" "std::string const &""'"); + } + if (!ptr) { + SWIG_exception_fail(SWIG_ValueError, "invalid null reference " "in method '" "codeHeader" "', argument " "1"" of type '" "std::string const &""'"); + } + arg1 = ptr; + } + { + try { + result = Py::Export::codeHeader((std::string const &)*arg1); + } catch (const std::exception& ex) { + // message shown in the Python interpreter + const std::string msg { + "BornAgain C++ Exception: " + std::string(ex.what()) + }; + SWIG_exception(SWIG_RuntimeError, msg.c_str()); + } + } + resultobj = SWIG_From_std_string(static_cast< std::string >(result)); + if (SWIG_IsNewObj(res1)) delete arg1; + return resultobj; +fail: + if (SWIG_IsNewObj(res1)) delete arg1; + return NULL; +} + + SWIGINTERN PyObject *_wrap_sampleCode(PyObject *self, PyObject *args) { PyObject *resultobj = 0; MultiLayer *arg1 = 0 ; @@ -39833,6 +39874,7 @@ static PyMethodDef SwigMethods[] = { { "delete_PoissonBackground", _wrap_delete_PoissonBackground, METH_O, "delete_PoissonBackground(PoissonBackground self)"}, { "PoissonBackground_swigregister", PoissonBackground_swigregister, METH_O, NULL}, { "PoissonBackground_swiginit", PoissonBackground_swiginit, METH_VARARGS, NULL}, + { "codeHeader", _wrap_codeHeader, METH_O, "codeHeader(std::string const & code) -> std::string"}, { "sampleCode", _wrap_sampleCode, METH_O, "sampleCode(MultiLayer const & sample) -> std::string"}, { "simulationPlotCode", _wrap_simulationPlotCode, METH_O, "simulationPlotCode(ISimulation simulation) -> std::string"}, { "simulationSaveCode", _wrap_simulationSaveCode, METH_VARARGS, "simulationSaveCode(ISimulation simulation, std::string const & fname) -> std::string"}, -- GitLab From de42fbfb27bd3c4af48d052d90131ab2362d679c Mon Sep 17 00:00:00 2001 From: Mikhail Svechnikov <svechnikovmv@gmail.com> Date: Thu, 22 Aug 2024 12:10:53 +0200 Subject: [PATCH 3/3] only header in codeHeader --- Sim/Export/ExportToPython.cpp | 5 +++-- Sim/Export/SimulationToPython.cpp | 2 +- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/Sim/Export/ExportToPython.cpp b/Sim/Export/ExportToPython.cpp index aca14763ec2..49e1241e995 100644 --- a/Sim/Export/ExportToPython.cpp +++ b/Sim/Export/ExportToPython.cpp @@ -20,12 +20,13 @@ std::string Py::Export::codeHeader(const std::string& code) { - return "import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code) + "\n\n" + code; + return "import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code) + "\n\n"; } std::string Py::Export::sampleCode(const MultiLayer& sample) { - return Py::Export::codeHeader(SampleToPython().sampleCode(sample)); + std::string sample_code = SampleToPython().sampleCode(sample); + return Py::Export::codeHeader(sample_code) + sample_code; } std::string Py::Export::simulationPlotCode(const ISimulation& simulation) diff --git a/Sim/Export/SimulationToPython.cpp b/Sim/Export/SimulationToPython.cpp index 2fecdd1a71a..277f1d3cee2 100644 --- a/Sim/Export/SimulationToPython.cpp +++ b/Sim/Export/SimulationToPython.cpp @@ -368,7 +368,7 @@ std::string simulationCode(const ISimulation& simulation) ASSERT(simulation.sample()); std::string code = SampleToPython().sampleCode(*simulation.sample()) + defineSimulate(simulation); - return Py::Export::codeHeader(code); + return Py::Export::codeHeader(code) + code; } } // namespace -- GitLab