Debian Codegraph @

avogadro/qtgui/calcworker.h

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #ifndef AVOGADRO_QTGUI_CALCWORKER_H
7 #define AVOGADRO_QTGUI_CALCWORKER_H
8
9 #include "avogadroqtguiexport.h"
10
11 #include <avogadro/calc/energycalculator.h>
12 #include <avogadro/calc/energyoptimizer.h>
13 #include <avogadro/core/constraint.h>
14 #include <avogadro/core/molecule.h>
15 #include <avogadro/core/vector.h>
16
17 #include <QObject>
18 #include <QThread>
19
20 #include <Eigen/Core>
21
22 #include <atomic>
23 #include <memory>
24 #include <vector>
25
26 Q_DECLARE_METATYPE(Eigen::VectorXd)
27 Q_DECLARE_METATYPE(Avogadro::Core::Molecule)
28 Q_DECLARE_METATYPE(Avogadro::Calc::OptimizationOptions)
29 Q_DECLARE_METATYPE(std::vector<Avogadro::Core::Constraint>)
30 Q_DECLARE_METATYPE(Avogadro::Calc::EnergyCalculator*)
31
32 namespace Avogadro {
33
34 namespace QtGui {
35
36 /**
37 * @class CalcWorker calcworker.h <avogadro/qtgui/calcworker.h>
38 * @brief Worker object that runs EnergyCalculator computations on a
39 * dedicated QThread.
40 *
41 * The worker owns a cloned EnergyCalculator and a Core::Molecule snapshot.
42 * All heavy computation (energy, gradient, optimization steps) happens on
43 * the worker thread; results are delivered back to the main thread via
44 * queued signal/slot connections.
45 *
46 * Typical usage:
47 * @code
48 * auto* thread = new QThread;
49 * auto* worker = new CalcWorker;
50 * worker->moveToThread(thread);
51 * connect(thread, &QThread::finished, worker, &QObject::deleteLater);
52 * connect(thread, &QThread::finished, thread, &QObject::deleteLater);
53 * thread->start();
54 *
55 * // Initialize calculator on the worker thread (important for QProcess)
56 * QMetaObject::invokeMethod(worker, "initCalculator",
57 * Qt::QueuedConnection,
58 * Q_ARG(Avogadro::Calc::EnergyCalculator*, calc),
59 * Q_ARG(Avogadro::Core::Molecule, molSnapshot),
60 * Q_ARG(Eigen::VectorXd, mask),
61 * Q_ARG(std::vector<Avogadro::Core::Constraint>, constraints));
62 * @endcode
63 */
64 class AVOGADROQTGUI_EXPORT CalcWorker : public QObject
65 {
66 Q_OBJECT
67
68 public:
69 explicit CalcWorker(QObject* parent = nullptr);
70 ~CalcWorker() override;
71
72 signals:
73 /**
74 * Emitted after each optimization chunk completes.
75 * @param positions The updated coordinate vector.
76 * @param gradient The gradient at the new positions.
77 * @param energy The energy at the new positions.
78 * @param converged True if optimization could not continue.
79 */
80 void optimizeFinished(Eigen::VectorXd positions, Eigen::VectorXd gradient,
81 double energy, bool converged);
82
83 /**
84 * Emitted after a one-shot energy/gradient evaluation.
85 * @param gradient The gradient vector (empty if not requested).
86 * @param energy The computed energy.
87 */
88 void evaluateFinished(Eigen::VectorXd gradient, double energy);
89
90 /**
91 * Emitted when the calculator has been initialized on the worker thread.
92 */
93 void calculatorReady();
94
95 public slots:
96 /**
97 * Initialize the calculator on the worker thread.
98 * This MUST be called via queued connection so that setMolecule()
99 * runs on the worker thread (required for QProcess affinity in
100 * ScriptEnergy/OBMMEnergy).
101 *
102 * Takes ownership of @p calculator.
103 */
104 void initCalculator(Avogadro::Calc::EnergyCalculator* calculator,
105 Avogadro::Core::Molecule molSnapshot,
106 Eigen::VectorXd mask,
107 std::vector<Avogadro::Core::Constraint> constraints);
108
109 /**
110 * Run a chunk of optimization iterations.
111 */
112 void runOptimizeChunk(Eigen::VectorXd positions,
113 Avogadro::Calc::OptimizationOptions options);
114
115 /**
116 * Run a one-shot energy and optionally gradient evaluation.
117 */
118 void runEvaluate(Eigen::VectorXd positions, bool computeGradient);
119
120 /**
121 * Run a one-shot gradient evaluation.
122 */
123 void runGradient(Eigen::VectorXd positions);
124
125 /**
126 * Request cancellation of the current computation.
127 * Thread-safe: can be called from any thread.
128 */
129 void cancel();
130
131 private:
132 std::unique_ptr<Calc::EnergyCalculator> m_calc;
133 Core::Molecule m_molSnapshot;
134 std::atomic<bool> m_cancelled{ false };
135 };
136
137 } // namespace QtGui
138 } // namespace Avogadro
139
140 #endif // AVOGADRO_QTGUI_CALCWORKER_H