BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
FitParameterContainerItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Model/Job/FitParameterContainerItem.cpp
6 //! @brief Implements class FitParameterContainerItem
7 //!
8 //! @homepage http://www.bornagainproject.org
9 //! @license GNU General Public License v3 or higher (see COPYING)
10 //! @copyright Forschungszentrum Jülich GmbH 2018
11 //! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
12 //
13 // ************************************************************************************************
14 
16 #include "Base/Util/Assert.h"
17 #include "Fit/Param/Parameters.h"
22 #include "GUI/Util/Error.h"
23 
25  : SessionItem(M_TYPE)
26 {
29 }
30 
32 {
33  ASSERT(parameterItem);
34 
35  removeLink(parameterItem);
36 
37  auto* fitPar = model()->insertItem<FitParameterItem>(this);
38  fitPar->setDisplayName("par");
39  fitPar->setStartValue(parameterItem->valueOfLink());
40  fitPar->initMinMaxValues(parameterItem->limitsOfLink());
41  fitPar->addLink(parameterItem->titleForFitItem(), parameterItem->link());
42 }
43 
45  const QString& fitParName)
46 {
47  removeLink(parameterItem);
48  for (FitParameterItem* fitPar : fitParameterItems()) {
49  if (fitPar->displayName() == fitParName) {
50  fitPar->addLink(parameterItem->titleForFitItem(), parameterItem->link());
51  break;
52  }
53  }
54 }
55 
56 //! Returns FitParameterItem for given link (path in model)
57 
59 {
60  for (auto* item : fitParameterItems())
61  if (item->links().contains(link))
62  return item;
63 
64  return nullptr;
65 }
66 
69 {
70  return fitParameterItem(parameterItem->link());
71 }
72 
73 QVector<FitParameterItem*> FitParameterContainerItem::fitParameterItems() const
74 {
75  return items<FitParameterItem>(T_FIT_PARAMETERS);
76 }
77 
79 {
80  QStringList result;
82  result.append(item->displayName());
83  return result;
84 }
85 
87 {
88  if (FitParameterItem* fitParItem = fitParameterItem(parameterItem))
89  fitParItem->removeLink(parameterItem->link());
90 }
91 
93 {
94  return fitParameterItems().isEmpty();
95 }
96 
97 //! Propagate values to the corresponding parameter tree items of parameterContainer.
98 
100  const QVector<double>& values, ParameterContainerItem* parameterContainer)
101 {
102  ASSERT(parameterContainer);
103 
104  QVector<FitParameterItem*> fitPars = items<FitParameterItem>(T_FIT_PARAMETERS);
105 
106  int index(0);
107  for (int i = 0; i < fitPars.size(); ++i) {
108  auto link_list = fitPars[i]->linkItems();
109  if (link_list.empty())
110  continue;
111  for (FitParameterLinkItem* linkItem : link_list)
112  if (auto* itemInTuningTree = parameterContainer->findParameterItem(linkItem->link()))
113  itemInTuningTree->propagateValueToLink(values[index]);
114 
115  index++;
116  }
117 }
118 
120 {
121  mumufit::Parameters result;
122 
123  int index(0);
124  for (auto* fitPar : fitParameterItems()) {
125  if (!fitPar->isValid()) {
126  throw Error(
127  QString("FitParameterContainerItem::createParameters(): invalid starting value "
128  "or (min, max) range in fitting parameter par %1")
129  .arg(index));
130  }
131  double startValue = fitPar->startValue();
132  AttLimits limits = fitPar->attLimits();
133  QString name = QString("par%1").arg(index);
134  result.add(mumufit::Parameter(name.toStdString(), startValue, limits));
135  ++index;
136  }
137 
138  return result;
139 }
Defines error class.
Defines class FitParameterContainerItem.
Defines class FitParameterItem.
Defines class FitParameterLinkItem.
Defines classes for ParameterTreeItems.
Defines class SessionModel.
static constexpr auto T_FIT_PARAMETERS
QStringList fitParameterNames() const
Returns list of fit parameter display names.
FitParameterItem * fitParameterItem(const QString &link) const
get the fit parameter item whose link matches the given link.
mumufit::Parameters createParameters() const
void removeLink(const ParameterItem *parameterItem)
void createFitParameter(ParameterItem *parameterItem)
Creates fit parameter from given ParameterItem, sets starting value to the value of ParameterItem,...
void setValuesInParameterContainer(const QVector< double > &values, class ParameterContainerItem *parameterContainer)
Propagate values to the corresponding parameter tree items of parameterContainer.
QVector< FitParameterItem * > fitParameterItems() const
void addToFitParameter(ParameterItem *parameterItem, const QString &fitParName)
Adds given parameterItem to the existing fit parameter with display name fitParName....
FitParameterItems is a collection of items to define fit parameters in GUI.
static constexpr auto M_TYPE
The FitParameterLinkItem class holds a link to ParameterItem in tuning tree.
The ParameterContainerItem is a top item to hold all ParameterItem, represents an entry point to para...
ParameterItem * findParameterItem(const QString &link) const
The ParameterItem class represent a tuning value in a parameter tuning tree.
QString titleForFitItem() const
double valueOfLink() const
QString link() const
Unique string to identify this ParameterItem.
RealLimits limitsOfLink() const
Base class for a GUI data item.
Definition: SessionItem.h:204
bool registerTag(const QString &name, int min=0, int max=-1, QStringList modelTypes={})
Add new tag to this item with given name, min, max and types. max = -1 -> unlimited,...
void setDisplayName(const QString &display_name)
Set display name.
void setDefaultTag(const QString &tag)
Set default tag.
SessionModel * model() const
Returns model of this item.
Definition: SessionItem.cpp:60
T * item(const QString &tag) const
Definition: SessionItem.h:353
RealLimits limits() const
QModelIndex index() const
Returns model index of this item.
Definition: SessionItem.cpp:74
T * insertItem(SessionItem *parent=nullptr, int row=-1, QString tag="")
Definition: SessionModel.h:137
QString const & name(EShape k)
Definition: particles.cpp:20