BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
FitObjectiveBuilder.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/View/Fit/FitObjectiveBuilder.cpp
6 //! @brief Implements class SimulationBuilder
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 "Fit/Kernel/Minimizer.h"
17 #include "Fit/Minimizer/IMinimizer.h"
22 #include "GUI/Model/Job/JobItem.h"
25 #include "GUI/Util/Error.h"
27 #include "Sim/Fitting/FitObjective.h"
28 #include "Sim/Fitting/ObjectiveMetric.h"
29 #include "Sim/Simulation/ISimulation.h"
30 
32  : m_jobItem(jobItem)
33 {
34  ASSERT(m_jobItem->fitSuiteItem());
35 }
36 
38 
40 {
42 
44  m_fit_objective->setObjectiveMetric(std::move(module));
45 
46  if (m_observer) {
47  fit_observer_t plot_observer = [&](const FitObjective& obj) { m_observer->update(&obj); };
48  m_fit_objective->initPlot(1, std::move(plot_observer));
49  }
50 
51  auto minimizer_impl = createMinimizer();
52  const bool requires_residuals = minimizer_impl->requiresResiduals();
53 
54  mumufit::Minimizer minimizer;
55  minimizer.setMinimizer(minimizer_impl.release());
56 
57  auto result = requires_residuals ? minimizer.minimize(
58  [&](const mumufit::Parameters& params) {
59  return m_fit_objective->evaluate_residuals(params);
60  },
62  : minimizer.minimize(
63  [&](const mumufit::Parameters& params) {
64  return m_fit_objective->evaluate(params);
65  },
67  m_fit_objective->finalize(result);
68 }
69 
70 std::unique_ptr<FitObjective> FitObjectiveBuilder::createFitObjective() const
71 {
72  auto result = std::make_unique<FitObjective>();
73 
74  simulation_builder_t builder = [&](const mumufit::Parameters& params) {
75  return buildSimulation(params);
76  };
77 
78  auto* realDataItem = m_jobItem->realDataItem();
79  ASSERT(realDataItem);
80 
81  const DataItem* intensity_item = realDataItem->dataItem();
82  ASSERT(intensity_item);
83  ASSERT(intensity_item->getDatafield());
84 
85  std::unique_ptr<Datafield> data(intensity_item->getDatafield()->clone());
86 
87  result->execAddSimulationAndData(builder, *data, nullptr, 1.0);
88 
89  return result;
90 }
91 
92 std::unique_ptr<IMinimizer> FitObjectiveBuilder::createMinimizer() const
93 {
95 }
96 
97 mumufit::Parameters FitObjectiveBuilder::createParameters() const
98 {
100 }
101 
102 void FitObjectiveBuilder::attachObserver(std::shared_ptr<GUIFitObserver> observer)
103 {
104  m_observer = observer;
105 }
106 
108 {
109  m_fit_objective->interruptFitting();
110 }
111 
112 std::unique_ptr<ISimulation>
113 FitObjectiveBuilder::buildSimulation(const mumufit::Parameters& params) const
114 {
115  static std::mutex build_simulation_mutex;
116  std::unique_lock<std::mutex> lock(build_simulation_mutex);
117 
118  update_fit_parameters(params);
121 }
122 
123 void FitObjectiveBuilder::update_fit_parameters(const mumufit::Parameters& params) const
124 {
125  QVector<double> values = QVector<double>::fromStdVector(params.values());
126 
127  auto* fitParContainer = m_jobItem->fitParameterContainerItem();
129 }
Declares class DataItem.
Defines error class.
Defines class FitObjectiveBuilder.
Defines class FitParameterContainerItem.
Defines class FitSuiteItem.
Implements class GUIFitObserver.
Defines class JobItem.
Defines MinimizerItem class.
Defines class RealDataItem.
Defines namespace GUI::Transform::ToCore.
Abstract base class for IntensityDataItem and SpecularDataItem. Owns one simulated data set of type D...
Definition: DataItem.h:34
Datafield * getDatafield()
Definition: DataItem.h:41
void attachObserver(std::shared_ptr< GUIFitObserver > observer)
std::unique_ptr< ISimulation > buildSimulation(const mumufit::Parameters &params) const
std::unique_ptr< IMinimizer > createMinimizer() const
FitObjectiveBuilder(JobItem *jobItem)
std::shared_ptr< GUIFitObserver > m_observer
std::unique_ptr< FitObjective > createFitObjective() const
mumufit::Parameters createParameters() const
std::unique_ptr< FitObjective > m_fit_objective
void update_fit_parameters(const mumufit::Parameters &params) const
mumufit::Parameters createParameters() const
void setValuesInParameterContainer(const QVector< double > &values, class ParameterContainerItem *parameterContainer)
Propagate values to the corresponding parameter tree items of parameterContainer.
MinimizerContainerItem * minimizerContainerItem()
FitParameterContainerItem * fitParameterContainerItem()
const SimulationOptionsItem & simulationOptionsItem() const
Definition: JobItem.cpp:415
MultiLayerItem * sampleItem()
Definition: JobItem.cpp:222
FitParameterContainerItem * fitParameterContainerItem()
Definition: JobItem.cpp:282
FitSuiteItem * fitSuiteItem()
Definition: JobItem.cpp:257
InstrumentItem * instrumentItem() const
Definition: JobItem.cpp:233
RealDataItem * realDataItem()
Definition: JobItem.cpp:290
ParameterContainerItem * parameterContainerItem()
Definition: JobItem.cpp:271
std::unique_ptr< IMinimizer > createMinimizer() const override
std::unique_ptr< ObjectiveMetric > createMetric() const
std::unique_ptr< ISimulation > itemsToSimulation(const MultiLayerItem *sampleItem, const InstrumentItem *instrumentItem, const SimulationOptionsItem &optionsItem)
Creates domain simulation from sample and instrument items.