Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • c.trageser/bornagain
  • mlz/bornagain
2 results
Show changes
Commits on Source (7)
......@@ -27,6 +27,7 @@ target_include_directories(${lib}
PUBLIC
${CMAKE_SOURCE_DIR}
SYSTEM PUBLIC # needed for Mac and Win, even if they are implied under Linux
${LibHeinz_INCLUDE_DIR}
${GSL_INCLUDE_DIR}
${FFTW3_INCLUDE_DIR}
${Boost_INCLUDE_DIRS}
......
......@@ -311,14 +311,14 @@ TEST_F(PyEmbedded, EmbeddedMultiLayer)
// compile our function
std::stringstream buf;
buf << "import bornagain as ba \n";
buf << " \n";
buf << "def get_simulation(): \n";
buf << " m_vacuum = ba.HomogeneousMaterial(\"Vacuum\", 0.0, 0.0) \n";
buf << " vacuum_layer = ba.Layer(m_vacuum) \n";
buf << " multilayer = ba.MultiLayer() \n";
buf << " multilayer.addLayer(vacuum_layer) \n";
buf << " return multilayer \n";
buf << "import bornagain as ba\n";
buf << "\n";
buf << "def get_simulation():\n";
buf << " m_vacuum = ba.HomogeneousMaterial(\"Vacuum\", 0.0, 0.0)\n";
buf << " vacuum_layer = ba.Layer(m_vacuum)\n";
buf << " multilayer = ba.MultiLayer()\n";
buf << " multilayer.addLayer(vacuum_layer)\n";
buf << " return multilayer\n";
PyObject* pCompiledFn = Py_CompileString(buf.str().c_str(), "", Py_file_input);
if (!pCompiledFn)
......@@ -360,6 +360,26 @@ TEST_F(PyEmbedded, EmbeddedMultiLayer)
EXPECT_TRUE(n_layers == 1);
}
//! We use one of our standard sample builders to build a sample, then generate Python snippet
//! using our standard ExportToPython machinery. Ditto for a cloned sample.
//! Two exported code snippets must be identical.
TEST_F(PyEmbedded, CloneAndExportToPython)
{
std::unique_ptr<MultiLayer> sample1(ExemplarySamples::createMultiLayerWithNCRoughness());
const std::string code1 = Py::Export::sampleCode(*sample1);
std::cout << "Test ExportToPythonAndBack: code1:\n" << code1 << std::endl;
std::unique_ptr<MultiLayer> sample2(sample1->clone());
const std::string code2 = Py::Export::sampleCode(*sample2);
if (code2 != code1)
std::cout << "Test ExportToPythonAndBack: code2:\n" << code2 << std::endl;
else
std::cout << "Test ExportToPythonAndBack: code2 = code1" << std::endl;
EXPECT_TRUE(code2 == code1);
}
//! We use one of our standard sample builders to build a sample, then generate Python snippet
//! using our standard ExportToPython machinery.
//! Given snippet is compiled and executed in embedded interpretor. Resulting multi layer
......@@ -368,26 +388,21 @@ TEST_F(PyEmbedded, EmbeddedMultiLayer)
TEST_F(PyEmbedded, ExportToPythonAndBack)
{
std::cout << "Test(ExportToPythonAndBack): begin" << std::endl;
std::unique_ptr<MultiLayer> sample1(ExemplarySamples::createCylindersAndPrisms());
std::unique_ptr<MultiLayer> sample(ExemplarySamples::createCylindersAndPrisms());
std::cout << "Test(ExportToPythonAndBack): have sample" << std::endl;
const std::string code = Py::Export::sampleCode(*sample);
std::cout << "Test(ExportToPythonAndBack): have code:\n" << code << std::endl;
const std::string code1 = Py::Export::sampleCode(*sample1);
std::cout << "Test ExportToPythonAndBack: code1:\n" << code1 << std::endl;
const std::string snippet =
"import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code) + "\n\n" + code;
std::cout << "Test(ExportToPythonAndBack): have snippet" << std::endl;
const auto multilayer =
"import bornagain as ba\n" + Py::Fmt::printImportedSymbols(code1) + "\n\n" + code1;
const auto sample2 =
Py::Import::createFromPython(snippet, "get_sample", BABuild::buildLibDir());
std::cout << "Test(ExportToPythonAndBack): sample from code" << std::endl;
const std::string new_code = Py::Export::sampleCode(*multilayer);
std::cout << "Test(ExportToPythonAndBack): code from cloned sample:\n" << code << std::endl;
EXPECT_TRUE(code == new_code);
const std::string code2 = Py::Export::sampleCode(*sample2);
if (code2 != code1)
std::cout << "Test ExportToPythonAndBack: code2:\n" << code2 << std::endl;
else
std::cout << "Test ExportToPythonAndBack: code2 = code1" << std::endl;
EXPECT_TRUE(code2 == code1);
}
//! Retrieves list of functions from the imported script and checks, that there is
......@@ -397,14 +412,14 @@ TEST_F(PyEmbedded, ModuleFunctionsList)
{
// compile our function
std::stringstream buf;
buf << "import bornagain as ba \n";
buf << " \n";
buf << "def get_simulation(): \n";
buf << " m_vacuum = ba.HomogeneousMaterial(\"Vacuum\", 0.0, 0.0) \n";
buf << " vacuum_layer = ba.Layer(m_vacuum) \n";
buf << " multilayer = ba.MultiLayer() \n";
buf << " multilayer.addLayer(vacuum_layer) \n";
buf << " return multilayer \n";
buf << "import bornagain as ba\n";
buf << "\n";
buf << "def get_simulation():\n";
buf << " m_vacuum = ba.HomogeneousMaterial(\"Vacuum\", 0.0, 0.0)\n";
buf << " vacuum_layer = ba.Layer(m_vacuum)\n";
buf << " multilayer = ba.MultiLayer()\n";
buf << " multilayer.addLayer(vacuum_layer)\n";
buf << " return multilayer\n";
auto listOfFunc = Py::Import::listOfFunctions(buf.str(), BABuild::buildLibDir());
for (auto s : listOfFunc)
......