Skip to content
Snippets Groups Projects
Commit 47c10de7 authored by Pospelov, Gennady's avatar Pospelov, Gennady
Browse files

New PythonFormatting::getSampleFunctionName introduces common constant.

parent 546892af
No related branches found
No related tags found
No related merge requests found
......@@ -34,6 +34,21 @@
namespace PythonFormatting
{
std::string scriptPreamble()
{
const std::string result = "import numpy\n"
"import bornagain as ba\n"
"from bornagain import deg, angstrom, nm, kvector_t\n\n\n";
return result;
}
std::string getSampleFunctionName()
{
return "get_sample";
}
//! Returns fixed Python code snippet that defines the function "runSimulation".
std::string representShape2D(const std::string& indent, const IShape2D* ishape, bool mask_value,
......
......@@ -32,6 +32,9 @@ class IAxis;
namespace PythonFormatting
{
BA_CORE_API_ std::string scriptPreamble();
BA_CORE_API_ std::string getSampleFunctionName();
BA_CORE_API_ std::string representShape2D(const std::string& indent, const IShape2D* ishape,
bool mask_value,
std::function<std::string(double)> printValueFunc);
......
......@@ -86,7 +86,7 @@ SampleToPython::~SampleToPython() = default;
std::string SampleToPython::defineGetSample() const
{
return "def get_sample():\n" + defineMaterials() + defineLayers() + defineFormFactors()
return "def "+getSampleFunctionName()+"():\n" + defineMaterials() + defineLayers() + defineFormFactors()
+ defineParticles() + defineCoreShellParticles() + defineParticleCompositions()
+ defineLattices() + defineCrystals() + defineMesoCrystals()
+ defineParticleDistributions() + defineInterferenceFunctions() + defineParticleLayouts()
......
......@@ -32,12 +32,8 @@ using namespace PythonFormatting;
namespace
{
const std::string preamble = "import numpy\n"
"import bornagain as ba\n"
"from bornagain import deg, angstrom, nm, kvector_t\n\n\n";
const std::string defineSimulate = "def run_simulation():\n"
" sample = get_sample()\n"
" sample = "+getSampleFunctionName()+"()\n"
" simulation = get_simulation()\n"
" simulation.setSample(sample)\n"
" simulation.runSimulation()\n"
......@@ -67,7 +63,7 @@ std::string SimulationToPython::generateSimulationCode(const Simulation& simulat
SampleToPython sampleGenerator;
return preamble + sampleGenerator.generateSampleCode(*simulation.sample())
return scriptPreamble() + sampleGenerator.generateSampleCode(*simulation.sample())
+ defineGetSimulation(&simulation) + defineSimulate + defineMain(mainType);
}
......
......@@ -23,6 +23,7 @@
#include "BornAgainNamespace.h"
#include "SampleBuilderFactory.h"
#include "ExportToPython.h"
#include "PythonFormatting.h"
#include <iostream>
#include <sstream>
......@@ -363,12 +364,10 @@ bool ExportToPythonAndBack::runTest()
auto code = ExportToPython::generateSampleCode(*sample);
std::stringstream snippet;
snippet << "import bornagain as ba \n";
snippet << "from bornagain import deg, angstrom, nm \n\n";
snippet << code;
snippet << PythonFormatting::scriptPreamble() << code;
auto multilayer = PyImport::createFromPython(snippet.str(), "getSample",
BABuild::buildLibDir());
auto multilayer = PyImport::createFromPython(snippet.str(),
PythonFormatting::getSampleFunctionName(), BABuild::buildLibDir());
auto new_code = ExportToPython::generateSampleCode(*multilayer);
return code == new_code;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment