BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
SimulationOptionsItem.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/coregui/Models/SimulationOptionsItem.cpp
6 //! @brief Defines class SimulationOptionsItem
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 
17 #include <thread>
18 
19 namespace {
20 
21 QStringList getRunPolicyTooltips()
22 {
23  QStringList result;
24  result.append("Start simulation immediately, switch to Jobs view automatically when completed");
25  result.append("Start simulation immediately, do not switch to Jobs view when completed");
26  return result;
27 }
28 
29 const QString tooltip_runpolicy = "Defines run policy for the simulation";
30 const QString tooltip_nthreads = "Defines number of threads to use for the simulation.";
31 const QString tooltip_computation =
32  "Defines computation method (analytical or Monte-Carlo integration)";
33 const QString tooltip_ambientmaterial =
34  "Define if the material used for Fresnel calculations should be the ambient layer "
35  "material or the average material of the layer and the particles it contains";
36 const QString tooltip_specularpeak =
37  "Defines if the specular peak should be included in the simulation result";
38 
39 } // namespace
40 
41 const QString SimulationOptionsItem::P_RUN_POLICY = "Run Policy";
42 const QString SimulationOptionsItem::P_NTHREADS = "Number of Threads";
43 const QString SimulationOptionsItem::P_COMPUTATION_METHOD = "Computation method";
44 const QString SimulationOptionsItem::P_MC_POINTS = "Number of MC points";
46  "Material for Fresnel calculations";
47 const QString SimulationOptionsItem::P_INCLUDE_SPECULAR_PEAK = "Include specular peak";
48 
50 {
51 
52  ComboProperty policy;
53  policy << getRunPolicyNames();
54  policy.setToolTips(getRunPolicyTooltips());
55  addProperty(P_RUN_POLICY, policy.variant())->setToolTip(tooltip_runpolicy);
56 
57  ComboProperty nthreads;
58  nthreads << getCPUUsageOptions();
59  addProperty(P_NTHREADS, nthreads.variant())->setToolTip(tooltip_nthreads);
60 
61  ComboProperty computationMethod;
62  computationMethod << "Analytical"
63  << "Monte-Carlo Integration";
64  addProperty(P_COMPUTATION_METHOD, computationMethod.variant())->setToolTip(tooltip_computation);
65 
66  addProperty(P_MC_POINTS, 100)->setEnabled(false);
67 
68  ComboProperty averageLayerMaterials;
69  averageLayerMaterials << "Ambient Layer Material"
70  << "Average Layer Material";
71  addProperty(P_FRESNEL_MATERIAL_METHOD, averageLayerMaterials.variant())
72  ->setToolTip(tooltip_ambientmaterial);
73 
74  ComboProperty includeSpecularPeak;
75  includeSpecularPeak << "No"
76  << "Yes";
77  addProperty(P_INCLUDE_SPECULAR_PEAK, includeSpecularPeak.variant())
78  ->setToolTip(tooltip_specularpeak);
79 
80  mapper()->setOnPropertyChange([this](const QString& name) {
83 
84  if (combo.getValue() == "Analytical") {
86 
87  } else {
89  }
90  } else if (name == P_NTHREADS) {
92  } else if (name == P_RUN_POLICY) {
94  }
95  });
96 }
97 
99 {
101  return m_text_to_nthreads[combo.getValue()];
102 }
103 
105 {
106  return runPolicy() == "Immediately";
107 }
108 
110 {
111  return runPolicy() == "In background";
112 }
113 
114 void SimulationOptionsItem::setRunPolicy(const QString& policy)
115 {
117  combo.setValue(policy);
119 }
120 
122 {
124  combo.setValue(name);
126 }
127 
129 {
131  return combo.getValue();
132 }
133 
135 {
136  return getItemValue(P_MC_POINTS).toInt();
137 }
138 
140 {
141  setItemValue(P_MC_POINTS, npoints);
142 }
143 
145 {
147  combo.setValue(name);
149 }
150 
152 {
154  return combo.getValue();
155 }
156 
158 {
160  combo.setValue(name);
162 }
163 
165 {
167  return combo.getValue();
168 }
169 
171 {
173  return combo.getValue();
174 }
175 
176 //! returns list with number of threads to select
178 {
179  m_text_to_nthreads.clear();
180  QStringList result;
181  int nthreads = static_cast<int>(std::thread::hardware_concurrency());
182  for (int i = nthreads; i > 0; i--) {
183  QString str;
184  if (i == nthreads) {
185  str = QString("Max (%1 threads)").arg(QString::number(i));
186  } else if (i == 1) {
187  str = QString("%1 thread").arg(QString::number(i));
188  } else {
189  str = QString("%1 threads").arg(QString::number(i));
190  }
191  result.append(str);
192  m_text_to_nthreads[str] = i;
193  }
194  return result;
195 }
196 
198 {
199  QStringList result;
200  result << "Immediately"
201  << "In background";
202  return result;
203 }
204 
205 void SimulationOptionsItem::updateComboItem(QString name, QStringList option_names)
206 {
207  ComboProperty combo = getItemValue(name).value<ComboProperty>();
208  if (combo.getValues().size() != option_names.size()) {
209  auto p_item = getItem(name);
210  auto selected_value = combo.getValue();
211  ComboProperty new_combo;
212  new_combo << option_names;
213  if (new_combo.getValues().contains(selected_value))
214  new_combo.setValue(selected_value);
215  p_item->setValue(new_combo.variant());
216  }
217 }
Defines class ComboProperty.
Defines class SimulationOptionsItem.
Custom property to define list of string values with multiple selections.
Definition: ComboProperty.h:25
QStringList getValues() const
QVariant variant() const
Constructs variant enclosing given ComboProperty.
void setValue(const QString &name)
QString getValue() const
void setToolTips(const QStringList &tooltips)
void setOnPropertyChange(std::function< void(QString)> f, const void *caller=0)
Definition: ModelMapper.cpp:35
bool isTag(const QString &name) const
Returns true if tag is available.
SessionItem * addProperty(const QString &name, const QVariant &variant)
Add new property item and register new tag.
QVariant getItemValue(const QString &tag) const
Directly access value of item under given tag.
ModelMapper * mapper()
Returns the current model mapper of this item. Creates new one if necessary.
void setItemValue(const QString &tag, const QVariant &variant)
Directly set value of item under given tag.
SessionItem & setToolTip(const QString &tooltip)
void setEnabled(bool enabled)
SessionItem * getItem(const QString &tag="", int row=0) const
Returns item in given row of given tag.
QString getIncludeSpecularPeak() const
QMap< QString, int > m_text_to_nthreads
void setFresnelMaterialMethod(const QString &name)
QStringList getCPUUsageOptions()
returns list with number of threads to select
void setIncludeSpecularPeak(const QString &name)
static const QString P_RUN_POLICY
static const QString P_MC_POINTS
void setNumberOfMonteCarloPoints(int npoints)
void setComputationMethod(const QString &name)
void setRunPolicy(const QString &policy)
static const QString P_INCLUDE_SPECULAR_PEAK
void updateComboItem(QString name, QStringList option_names)
QString getFresnelMaterialMethod() const
static const QString P_NTHREADS
QString getComputationMethod() const
static const QString P_COMPUTATION_METHOD
static const QString P_FRESNEL_MATERIAL_METHOD
QString const & name(EShape k)
Definition: particles.cpp:21