BornAgain  1.19.0
Simulate and fit neutron and x-ray scattering at grazing incidence
jobmanager.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file gui2/quicksimeditor/jobmanager.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 
17 
18 namespace gui2 {
19 
20 JobManager::JobManager(QObject* parent) : QObject(parent), m_isRunning(true)
21 {
22  // starting thread to run consequent simulations
23  m_simThread = std::thread{&JobManager::wait_and_run, this};
24 }
25 
27 {
28  m_isRunning = false;
29  // making stack throw to stops waiting in JobManager::wait_and_run
31  m_simThread.join();
32 }
33 
34 //! Returns vector representing results of a simulation.
35 
37 {
38  auto result = m_simulationResult.try_pop();
39  return result ? *result.get() : SimulationResult();
40 }
41 
42 //! Performs simulation request. Given multislice will be stored in a stack of values to trigger
43 //! a waiting thread.
44 
46  const std::vector<double>& qvalues, double intensity)
47 {
48  // At this point, non-empty stack means that currently simulation thread is busy.
49  // Replacing top value in a stack, meaning that we are droping previous request.
50  SimulationInput input_data;
51  input_data.slice_data = multislice;
52  input_data.qvalues = qvalues;
53  input_data.intensity = intensity;
54  m_requestedInputValues.update_top(input_data);
55 }
56 
57 //! Processes interrupt request by setting corresponding flag.
58 
60 {
61  m_interruptRequest = true;
62 }
63 
64 //! Performs concequent simulations for given simulation parameter. Waits for simulation input
65 //! parameter to appear in a stack, starts new simulation as soon as input data is ready.
66 //! Method is intended for execution in a thread.
67 
69 {
70  while (m_isRunning) {
71  try {
72  // Waiting here for the value which we will use as simulation input parameter.
73  auto value = m_requestedInputValues.wait_and_pop();
74 
75  // preparing simulation
76  SpecularToySimulation simulation(*value.get());
77  auto on_progress = [this](int value) {
78  progressChanged(value);
79  return m_interruptRequest;
80  };
81  simulation.setProgressCallback(on_progress);
82 
83  // running simulation
84  simulation.runSimulation();
85 
86  // Saving simulation result, overwrite previous if exists. If at this point stack
87  // with results is not empty it means that plotting is disabled or running too slow.
88  m_simulationResult.update_top(simulation.simulationResult());
90 
91  } catch (std::exception& ex) {
92  // Exception is thrown
93  // a) If waiting on stack was stopped my calling threadsafe_stack::stop.
94  // b) If simulation was interrupted via interrupt_request
95  m_interruptRequest = false;
96  progressChanged(0);
97  }
98  }
99 }
100 
101 } // namespace gui2
~JobManager() override
Definition: jobmanager.cpp:26
void wait_and_run()
Performs concequent simulations for given simulation parameter.
Definition: jobmanager.cpp:68
std::atomic< bool > m_isRunning
Definition: jobmanager.h:51
ModelView::threadsafe_stack< SimulationInput > m_requestedInputValues
Definition: jobmanager.h:49
bool m_interruptRequest
Definition: jobmanager.h:52
SimulationResult simulationResult()
Returns vector representing results of a simulation.
Definition: jobmanager.cpp:36
void simulationCompleted()
void requestSimulation(const multislice_t &multislice, const std::vector< double > &qvalues, double intensity)
Performs simulation request.
Definition: jobmanager.cpp:45
JobManager(QObject *parent=nullptr)
Definition: jobmanager.cpp:20
ModelView::threadsafe_stack< SimulationResult > m_simulationResult
Definition: jobmanager.h:50
void onInterruptRequest()
Processes interrupt request by setting corresponding flag.
Definition: jobmanager.cpp:59
void progressChanged(int value)
std::thread m_simThread
Definition: jobmanager.h:48
Toy simulation to calculate "specular reflectivity.
SimulationResult simulationResult() const
void setProgressCallback(ModelView::ProgressHandler::callback_t callback)
Defines class CLASS?
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?
Represents data to run specular simulations.
multislice_t slice_data
std::vector< double > qvalues
Represents results of the simulation.