BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
modelutils.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/model/modelutils.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 
15 #include "gui2/model/modelutils.h"
18 #include "mvvm/model/modelutils.h"
22 
23 namespace {
24 bool areCompatibleAxes(const ModelView::Data1DItem& item1, const ModelView::Data1DItem& item2)
25 {
26  // TODO consider moving the logic on board of Data1DItem, consider implement getAxis() getter.
27  auto axis1 = item1.getItem(ModelView::Data1DItem::T_AXIS);
28  auto axis2 = item2.getItem(ModelView::Data1DItem::T_AXIS);
29  if (!axis1 || !axis2)
30  return false;
31 
32  return axis1->modelType() == axis2->modelType();
33 }
34 
35 } // namespace
36 
37 namespace gui2 {
38 
40 {
41  std::string name = graph->parent()->displayName() + "/" + graph->displayName();
42  auto colorName = QString::fromStdString(graph->colorName());
43  return ModelView::ExternalProperty(name, QColor(colorName), graph->identifier());
44 }
45 
46 std::vector<ModelView::ExternalProperty> Utils::CreateGraphProperties(ExperimentalDataModel* model)
47 {
48  std::vector<ModelView::ExternalProperty> result;
49  for (auto graph : ModelView::Utils::FindItems<ModelView::GraphItem>(model))
50  result.push_back(Utils::CreateProperty(graph));
51  return result;
52 }
53 
54 // FIXME unit tests
56 Utils::FindProperty(const std::vector<ModelView::ExternalProperty>& properties,
57  const std::string& id)
58 {
59  for (const auto& prop : properties)
60  if (prop.identifier() == id)
61  return prop;
62 
64 }
65 
66 std::vector<double> Utils::CreateDiffVector(const std::vector<double>& a,
67  const std::vector<double>& b)
68 {
69  size_t length = std::min(a.size(), b.size());
70  std::vector<double> result(length, 0.0);
71  for (size_t i = 0; i < length; ++i) {
72  double denom = a[i] + b[i];
73  result[i] = denom != 0.0 ? 2 * (a[i] - b[i]) / (a[i] + b[i]) : 0.0;
74  }
75  return result;
76 }
77 
79  ModelView::Data1DItem* target)
80 {
81  if (!areCompatibleAxes(*data1, *data2) || !areCompatibleAxes(*data1, *target))
82  return;
83 
84  // We expect same number of points to caclulate the difference graph.
85  if (data1->binCenters().size() != data2->binCenters().size())
86  return;
87 
88  if (data1->binCenters() != target->binCenters()) {
90  ->setParameters(data1->binCenters());
91  }
92  target->setValues(CreateDiffVector(data1->binValues(), data2->binValues()));
93 }
94 
95 } // namespace gui2
Defines class CLASS?
std::string displayName() const override
Returns display name.
Represents one-dimensional data (axis and values).
Definition: data1ditem.h:30
static const std::string T_AXIS
Definition: data1ditem.h:34
std::vector< double > binValues() const
Returns values stored in bins.
Definition: data1ditem.cpp:76
void setValues(const std::vector< double > &data)
Sets internal data buffer to given data.
Definition: data1ditem.cpp:66
std::vector< double > binCenters() const
Sets axis. Bin content will be set to zero.
Definition: data1ditem.cpp:57
Property to carry text, color and identifier.
static ExternalProperty undefined()
One-dimensional graph representation of Data1DItem.
Definition: graphitem.h:29
std::string colorName() const
Returns color name in #RRGGBB format.
Definition: graphitem.cpp:76
Item to represent pointwise axis.
Definition: axisitems.h:94
std::string identifier() const
Returns unique identifier.
Definition: sessionitem.cpp:87
SessionItem * getItem(const std::string &tag, int row=0) const
Returns item at given row of given tag.
SessionItem * parent() const
Returns parent item. Will return nullptr if item doesn't have a parent.
virtual std::string displayName() const
Returns display name.
Definition: sessionitem.cpp:94
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
The model to store imported reflectometry data.
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
Defines class CLASS?
QString const & name(EShape k)
Definition: particles.cpp:21
DAREFLCORE_EXPORT std::vector< double > CreateDiffVector(const std::vector< double > &a, const std::vector< double > &b)
Returns vector representing elementwise 2*(a-b)/(a+b) difference over two vectors.
Definition: modelutils.cpp:66
DAREFLCORE_EXPORT ModelView::ExternalProperty CreateProperty(const ModelView::GraphItem *graph)
Returns property representing given graph.
Definition: modelutils.cpp:39
DAREFLCORE_EXPORT void SetDifference(const ModelView::Data1DItem *data1, const ModelView::Data1DItem *data2, ModelView::Data1DItem *target)
Make target item represent difference of two Data1DItems. Target will get an axis as in data1.
Definition: modelutils.cpp:78
DAREFLCORE_EXPORT ModelView::ExternalProperty FindProperty(const std::vector< ModelView::ExternalProperty > &properties, const std::string &id)
Finds the property with the same id in given vector and returns it.
Definition: modelutils.cpp:56
DAREFLCORE_EXPORT std::vector< ModelView::ExternalProperty > CreateGraphProperties(ExperimentalDataModel *model)
Returns vector of properties representing GraphItem content of the model.
Definition: modelutils.cpp:46
Based on Qt example "codeeditor" Copyright (C) 2016 The Qt Company Ltd.
Definition: app_constants.h:20