BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
quicksimutils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/quicksimeditor/quicksimutils.cpp
6 //! @brief Implements class CLASS?
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2020
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
18 #include "gui2/model/sampleitems.h"
23 #include <Sample/Slice/Slice.h>
24 #include <stdexcept>
25 
26 namespace gui2 {
27 
28 namespace {
29 
30 //! Creates slice from layer content.
31 SliceData create_slice(const ModelView::SessionItem& layer)
32 {
33  if (layer.modelType() != Constants::LayerItemType)
34  throw std::runtime_error("Error in create_slice(): not a layer.");
35 
36  double thickness = layer.property<double>(LayerItem::P_THICKNESS);
37  auto roughness = layer.item<RoughnessItem>(LayerItem::P_ROUGHNESS);
38  double sigma = roughness->property<double>(RoughnessItem::P_SIGMA);
39 
40  auto material_property = layer.property<ModelView::ExternalProperty>(LayerItem::P_MATERIAL);
41  auto material = layer.model()->findItem(material_property.identifier());
42  // layer which is not linked with material will get (0,0) as SLD material.
43  double sld_real = material ? material->property<double>(SLDMaterialItem::P_SLD_REAL) : 0.0;
44  double sld_imag = material ? material->property<double>(SLDMaterialItem::P_SLD_IMAG) : 0.0;
45  return {complex_t{sld_real, sld_imag}, thickness, sigma};
46 }
47 
48 //! Adds slices to existing vector of slices using content of a multilayer.
49 //! Will be called recursively for multilayers inside multilayers.
50 void AddToMultiSlice(multislice_t& result, const ModelView::SessionItem& multilayer)
51 {
52  for (const auto item : multilayer.getItems(MultiLayerItem::T_LAYERS)) {
53  if (item->modelType() == Constants::LayerItemType) {
54  result.push_back(create_slice(*item));
55  } else if (item->modelType() == Constants::MultiLayerItemType) {
56  const int rep_count = item->property<int>(MultiLayerItem::P_NREPETITIONS);
57  for (int i_rep = 0; i_rep < rep_count; ++i_rep)
58  AddToMultiSlice(result, *item);
59  } else {
60  throw std::runtime_error("Error in AddToMultiSlice: unsupported item type.");
61  }
62  }
63 }
64 
65 } // namespace
66 
68 {
69  multislice_t result;
70  AddToMultiSlice(result, multilayer);
71  return result;
72 }
73 
74 std::vector<Slice> Utils::createBornAgainSlices(const multislice_t& multislice)
75 {
76  std::vector<Slice> result;
77  result.reserve(multislice.size());
78 
79  for (auto& slice : multislice) {
80  auto material = MaterialBySLD("", slice.material.real(), slice.material.imag());
81  auto roughness = LayerRoughness(slice.sigma, 0., 0.);
82 
83  result.emplace_back(slice.thickness, material, roughness);
84  }
85 
86  return result;
87 }
88 
89 } // namespace gui2
std::complex< double > complex_t
Definition: Complex.h:20
Defines class LayerRoughness.
Factory functions used to create material instances.
Defines class Slice.
A roughness of interface between two layers.
Property to carry text, color and identifier.
The main object representing an editable/displayable/serializable entity.
Definition: sessionitem.h:38
std::vector< SessionItem * > getItems(const std::string &tag) const
Returns all children stored at given tag.
SessionModel * model() const
Returns the model to which given item belongs to.
T property(const std::string &tag) const
Returns data stored in property item.
Definition: sessionitem.h:181
T * item(const std::string &tag) const
Returns first item under given tag casted to a specified type.
Definition: sessionitem.h:156
model_type modelType() const
Returns item's model type.
Definition: sessionitem.cpp:80
SessionItem * findItem(const identifier_type &id)
Returns SessionItem for given identifier.
static const std::string P_MATERIAL
Definition: sampleitems.h:42
static const std::string P_THICKNESS
Definition: sampleitems.h:43
static const std::string P_ROUGHNESS
Definition: sampleitems.h:44
Multi layer capable of holding layers and other multi-layers.
Definition: sampleitems.h:51
static const std::string T_LAYERS
Definition: sampleitems.h:54
static const std::string P_NREPETITIONS
Definition: sampleitems.h:55
static const std::string P_SIGMA
Definition: sampleitems.h:30
static const std::string P_SLD_REAL
Definition: materialitems.h:59
static const std::string P_SLD_IMAG
Definition: materialitems.h:60
Defines class CLASS?
Material MaterialBySLD()
Defines class CLASS?
Defines class CLASS?
const std::string MultiLayerItemType
const std::string LayerItemType
DAREFLCORE_EXPORT std::vector< Slice > createBornAgainSlices(const multislice_t &multislice)
DAREFLCORE_EXPORT multislice_t CreateMultiSlice(const MultiLayerItem &multilayer)
Creates multi-slice presentation of internal multilayer structure.
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20
std::vector< SliceData > multislice_t
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?