Debian Codegraph @

avogadro/qtplugins/autoopt/autoopt.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_QTPLUGINS_AUTOOPT_H
7 #define AVOGADRO_QTPLUGINS_AUTOOPT_H
8
9 #include <avogadro/qtgui/toolplugin.h>
10
11 #include <avogadro/calc/energyoptimizer.h>
12 #include <avogadro/qtgui/molecule.h>
13 #include <avogadro/rendering/primitive.h>
14
15 #include <QtCore/QPoint>
16 #include <QtCore/QTimer>
17 #include <QtWidgets/QAbstractButton>
18
19 class QThread;
20
21 namespace Avogadro {
22
23 namespace QtGui {
24 class CalcWorker;
25 }
26
27 namespace QtPlugins {
28
29 class AutoOptWidget;
30 class CSVRThermostat;
31
32 /**
33 * @class AutoOpt autoopt.h
34 * <avogadro/qtplugins/autoopt/autoopt.h>
35 * @brief Optimize the geometry of a molecule while manipulating it.
36 * @author Geoff Hutchison
37 * Based on the Manipulator class by @author Allison Vacanti
38 */
39 class AutoOpt : public QtGui::ToolPlugin
40 {
41 Q_OBJECT
42 public:
43 explicit AutoOpt(QObject* parent_ = nullptr);
44 ~AutoOpt() override;
45
46 QString name() const override { return tr("Auto Optimize tool"); }
47 QString description() const override
48 {
49 return tr("Interactive optimization of molecular geometry");
50 }
51 unsigned char priority() const override { return 50; }
52 QAction* activateAction() const override { return m_activateAction; }
53 QWidget* toolWidget() const override;
54
55 void setIcon(bool darkTheme = false) override;
56
57 void setMolecule(QtGui::Molecule* mol) override;
58
59 void setEditMolecule(QtGui::RWMolecule* mol) override { m_molecule = mol; }
60
61 void setGLRenderer(Rendering::GLRenderer* renderer) override
62 {
63 m_renderer = renderer;
64 }
65
66 QUndoCommand* mousePressEvent(QMouseEvent* e) override;
67 QUndoCommand* mouseReleaseEvent(QMouseEvent* e) override;
68 QUndoCommand* mouseMoveEvent(QMouseEvent* e) override;
69 QUndoCommand* keyPressEvent(QKeyEvent* e) override;
70
71 void draw(Rendering::GroupNode& node) override;
72
73 public slots:
74 void startStop();
75 void start();
76 void stop();
77 void methodChanged(const QString& method);
78 void moleculeChanged(unsigned int changes);
79
80 void optimizeStep();
81 void dynamicsStep();
82
83 void onOptimizeStepDone(Eigen::VectorXd positions, Eigen::VectorXd gradient,
84 double energy, bool converged);
85 void onGradientDone(Eigen::VectorXd gradient, double energy);
86 void onWorkerReady();
87
88 void taskChanged(int task);
89 void temperatureChanged(double temperature);
90 void timeStepChanged(double timeStep);
91
92 private:
93 /**
94 * Update the currently pressed buttons, accounting for modifier keys.
95 * \todo Account for modifier keys.
96 */
97 void updatePressedButtons(QMouseEvent*, bool release);
98
99 void resetObject() { m_object = Rendering::Identifier(); }
100 void translate(Vector3 delta, bool moveSelected = true);
101 void rotate(Vector3 delta, Vector3 centroid, bool moveSelected = true);
102 void axisRotate(Vector3 delta, Vector3 centroid, bool moveSelected = true);
103 void tilt(Vector3 delta, Vector3 centroid);
104
105 QAction* m_activateAction;
106 QtGui::RWMolecule* m_molecule;
107 Rendering::GLRenderer* m_renderer;
108 Rendering::Identifier m_object;
109 QPoint m_lastMousePosition;
110 Vector3f m_lastMouse3D;
111 Qt::MouseButtons m_pressedButtons;
112
113 Real m_energy;
114 Real m_deltaE;
115
116 Real m_temperature = 300.0; // Kelvin
117 Real m_timeStep = 1.0; // fs
118 int m_task = 0; // default to optimization
119 Eigen::VectorXd m_velocities;
120 Eigen::VectorXd m_acceleration; // stored for Velocity Verlet
121 bool m_firstStep = true; // flag for first dynamics step
122 Eigen::ArrayXd m_masses;
123 CSVRThermostat* m_thermostat = nullptr;
124
125 mutable QWidget* m_toolWidget;
126
127 bool m_running = false;
128 qint64 m_oneStepTime = 0;
129 QTimer m_timer;
130
131 enum ToolAction
132 {
133 Nothing = 0,
134 Rotation,
135 Translation,
136 ZoomTilt,
137 Zoom
138 };
139 ToolAction m_currentAction;
140
141 std::string m_currentMethod;
142
143 // Worker thread state
144 QThread* m_workerThread = nullptr;
145 QtGui::CalcWorker* m_worker = nullptr;
146 bool m_computePending = false;
147 void startWorker();
148 void cleanupWorker();
149 };
150
151 } // namespace QtPlugins
152 } // namespace Avogadro
153
154 #endif // AVOGADRO_QTPLUGINS_AUTOOPT_H