From 509b1b44815c8f582467120fa6a1165c058334b4 Mon Sep 17 00:00:00 2001 From: Dmitry Yurov <d.yurov@fz-juelich.de> Date: Wed, 8 Nov 2017 10:10:50 +0100 Subject: [PATCH] Exporting all types of materials Redmine: #1875 --- Core/Export/ExportToPython.cpp | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/Core/Export/ExportToPython.cpp b/Core/Export/ExportToPython.cpp index 401c673df1d..e9bc17c5744 100644 --- a/Core/Export/ExportToPython.cpp +++ b/Core/Export/ExportToPython.cpp @@ -45,6 +45,7 @@ #include "ParameterUtils.h" #include <iomanip> #include <set> +#include <map> #include <functional> class IFormFactor; @@ -183,6 +184,10 @@ std::string ExportToPython::defineGetSample() const + "\n"; } +const std::map<MATERIAL_TYPES, std::string> factory_names{ + {MATERIAL_TYPES::RefractiveMaterial, "HomogeneousMaterial"}, + {MATERIAL_TYPES::MaterialBySLD, "MaterialBySLD"}}; + std::string ExportToPython::defineMaterials() const { const auto themap = m_label->materialMap(); @@ -197,22 +202,24 @@ std::string ExportToPython::defineMaterials() const continue; visitedMaterials.insert(it->second); const Material* p_material = it->first; - complex_t material_data = p_material->materialData(); - double real = std::real(material_data); - double imag = std::imag(material_data); + const auto factory_name = factory_names.find(p_material->typeID()); + if (factory_name == factory_names.cend()) + throw std::runtime_error( + "Error in ExportToPython::defineMaterials(): unknown material type"); + const complex_t& material_data = p_material->materialData(); if (p_material->isScalarMaterial()) { - result << indent() << m_label->labelMaterial(p_material) - << " = ba.HomogeneousMaterial(\"" << p_material->getName() - << "\", " << printDouble(real) << ", " - << printDouble(imag) << ")\n"; + result << indent() << m_label->labelMaterial(p_material) << " = ba." + << factory_name->second << "(\"" << p_material->getName() << "\", " + << printDouble(material_data.real()) << ", " << printDouble(material_data.imag()) + << ")\n"; } else { kvector_t magnetic_field = p_material->magnetization(); result << indent() << "magnetic_field = kvector_t(" << magnetic_field.x() << ", " << magnetic_field.y() << ", " << magnetic_field.z() << ")\n"; - result << indent() << m_label->labelMaterial(p_material) - << " = ba.HomogeneousMaterial(\"" << p_material->getName(); - result << "\", " << printDouble(real) << ", " - << printDouble(imag) << ", " + result << indent() << m_label->labelMaterial(p_material) << " = ba." + << factory_name->second << "(\"" << p_material->getName(); + result << "\", " << printDouble(material_data.real()) << ", " + << printDouble(material_data.imag()) << ", " << "magnetic_field)\n"; } } -- GitLab