BornAgain  1.19.79
Simulate and fit neutron and x-ray scattering at grazing incidence
JobWorker.cpp
Go to the documentation of this file.
1 // ************************************************************************************************
2 //
3 // BornAgain: simulate and fit reflection and scattering
4 //
5 //! @file GUI/Support/Data/JobWorker.cpp
6 //! @brief Implements class JobWorker
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 "Device/Histo/SimulationResult.h"
17 #include "Sim/Simulation/ScatteringSimulation.h"
18 #include <QDateTime>
19 #include <memory>
20 #include <utility>
21 
22 JobWorker::JobWorker(QString identifier, ISimulation* simulation)
23  : m_identifier(std::move(identifier))
24  , m_simulation(simulation)
25  , m_percentage_done(0)
26  , m_job_status(JobStatus::Idle)
27  , m_terminate_request_flag(false)
28 {
29 }
30 
31 JobWorker::~JobWorker() = default;
32 
33 QString JobWorker::identifier() const
34 {
35  return m_identifier;
36 }
37 
39 {
40  return m_percentage_done;
41 }
42 
44 {
46  m_simulation_start = QDateTime::currentDateTime();
47  m_simulation_end = QDateTime();
48  m_result.release();
49  emit started();
50 
51  if (m_simulation) {
52  m_simulation->subscribe([this](size_t percentage_done) {
53  return updateProgress(static_cast<int>(percentage_done));
54  });
55 
57 
58  try {
59  SimulationResult result = m_simulation->simulate();
62  m_result = std::make_unique<SimulationResult>(std::move(result));
63  } catch (const std::exception& ex) {
65  m_percentage_done = 100;
67  "JobRunner::start() -> ISimulation failed with exception throw:\n\n";
68 
69  m_failure_message.append(QString(ex.what()));
70  }
71  } else {
73  m_percentage_done = 100;
74  m_failure_message = "JobRunner::start() -> Error. ISimulation doesn't exist.";
75  }
76 
77  m_simulation_end = QDateTime::currentDateTime();
78  emit progressUpdate();
79  emit finished();
80 }
81 
83 {
84  return m_job_status;
85 }
86 
88 {
89  return m_failure_message;
90 }
91 
92 const QDateTime& JobWorker::simulationStart() const
93 {
94  return m_simulation_start;
95 }
96 
97 const QDateTime& JobWorker::simulationEnd() const
98 {
99  return m_simulation_end;
100 }
101 
102 const SimulationResult* JobWorker::result() const
103 {
104  return m_result.get();
105 }
106 
107 //! Sets request for JobRunner to terminate underlying domain simulation.
108 
110 {
113 }
114 
115 //! Sets current progress. Returns true if we want to continue the simulation.
116 
117 bool JobWorker::updateProgress(int percentage_done)
118 {
119  if (percentage_done > m_percentage_done) {
120  m_percentage_done = percentage_done;
121  emit progressUpdate();
122  }
123  return !m_terminate_request_flag;
124 }
JobStatus
The JobStatus enum lists the possible states of a job.
Definition: JobStatus.h:22
@ Completed
the job was successfully completed
@ Canceled
the job was stopped by the user
@ Running
the job is busy calculating
@ Failed
the job aborted because it hit an error
@ Idle
the job has not been started yet
Defines class JobWorker.
bool updateProgress(int percentage_done)
Sets current progress. Returns true if we want to continue the simulation.
Definition: JobWorker.cpp:117
QDateTime m_simulation_end
Definition: JobWorker.h:66
int m_percentage_done
Definition: JobWorker.h:61
void progressUpdate()
const QDateTime & simulationStart() const
Definition: JobWorker.cpp:92
QString failureMessage() const
Definition: JobWorker.cpp:87
QString identifier() const
Definition: JobWorker.cpp:33
int progress() const
Definition: JobWorker.cpp:38
void start()
Definition: JobWorker.cpp:43
bool m_terminate_request_flag
Definition: JobWorker.h:63
const SimulationResult * result() const
Definition: JobWorker.cpp:102
QDateTime m_simulation_start
Definition: JobWorker.h:65
QString m_identifier
Definition: JobWorker.h:59
void finished()
JobWorker(QString identifier, ISimulation *simulation)
Definition: JobWorker.cpp:22
std::unique_ptr< const SimulationResult > m_result
Definition: JobWorker.h:67
JobStatus m_job_status
Definition: JobWorker.h:62
const QDateTime & simulationEnd() const
Definition: JobWorker.cpp:97
QString m_failure_message
Definition: JobWorker.h:64
void terminate()
Sets request for JobRunner to terminate underlying domain simulation.
Definition: JobWorker.cpp:109
ISimulation * m_simulation
Definition: JobWorker.h:60
void started()
JobStatus status() const
Definition: JobWorker.cpp:82