Debian Codegraph @

avogadro/qtplugins/vibrations/vibrations.cpp

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 #include "vibrations.h"
7 #include "vibrationdialog.h"
8
9 #include <avogadro/core/array.h>
10 #include <avogadro/core/variant.h>
11 #include <avogadro/core/vector.h>
12 #include <avogadro/qtgui/molecule.h>
13
14 #include <QAction>
15 #include <QDebug>
16 #include <QtCore/QTimer>
17 #include <QtWidgets/QFileDialog>
18
19 namespace Avogadro::QtPlugins {
20
21 Vibrations::Vibrations(QObject* p)
22 : ExtensionPlugin(p), m_molecule(nullptr), m_dialog(nullptr),
23 m_timer(nullptr), m_mode(0), m_amplitude(20)
24 {
25 auto* action = new QAction(this);
26 action->setEnabled(false);
27 action->setText(tr("Vibrational Modes…"));
28 connect(action, SIGNAL(triggered()), SLOT(openDialog()));
29 m_actions.push_back(action);
30 }
31
32 Vibrations::~Vibrations() {}
33
34 QList<QAction*> Vibrations::actions() const
35 {
36 return m_actions;
37 }
38
39 QStringList Vibrations::menuPath(QAction*) const
40 {
41 QStringList path;
42 path << tr("&Analyze");
43 return path;
44 }
45
46 void Vibrations::setMolecule(QtGui::Molecule* mol)
47 {
48 if (mol == nullptr)
49 return;
50
51 if (m_molecule != nullptr)
52 m_molecule->disconnect(this);
53
54 bool isVibrational(false);
55 if (mol->vibrationFrequencies().size())
56 isVibrational = true;
57
58 m_actions[0]->setEnabled(isVibrational);
59 m_molecule = mol;
60 if (m_dialog)
61 m_dialog->setMolecule(mol);
62
63 if (isVibrational)
64 openDialog();
65
66 connect(m_molecule, SIGNAL(changed(unsigned int)),
67 SLOT(moleculeChanged(unsigned int)));
68 }
69
70 void Vibrations::moleculeChanged([[maybe_unused]] unsigned int changes)
71 {
72 if (m_molecule == nullptr)
73 return;
74
75 bool currentVibrational = m_actions[0]->isEnabled();
76 bool isVibrational = (m_molecule->vibrationFrequencies().size() > 0);
77
78 if (currentVibrational != isVibrational) {
79 m_actions[0]->setEnabled(isVibrational);
80 if (m_dialog)
81 m_dialog->setMolecule(m_molecule); // update the dialog
82 if (isVibrational)
83 openDialog();
84 }
85 }
86
87 void Vibrations::registerCommands()
88 {
89 emit registerCommand("showVibrations",
90 tr("Show the vibrational modes dialog."));
91 emit registerCommand("setVibrationalMode", tr("Set the vibrational mode."));
92 emit registerCommand("setVibrationalAmplitude",
93 tr("Set the vibrational amplitude."));
94 emit registerCommand("startVibrationAnimation",
95 tr("Start the vibrational animation."));
96 emit registerCommand("stopVibrationAnimation",
97 tr("Stop the vibrational animation."));
98 }
99
100 bool Vibrations::handleCommand(const QString& command,
101 const QVariantMap& options)
102 {
103 if (m_molecule == nullptr)
104 return false; // No molecule to handle the command.
105
106 if (command == "showVibrations") {
107 openDialog();
108 return true;
109 } else if (command == "setVibrationalMode") {
110 if (options.contains("mode")) {
111 setMode(options["mode"].toInt());
112 return true;
113 }
114 } else if (command == "setVibrationalAmplitude") {
115 if (options.contains("amplitude")) {
116 setAmplitude(options["amplitude"].toInt());
117 return true;
118 }
119 } else if (command == "startVibrationAnimation") {
120 startVibrationAnimation();
121 return true;
122 } else if (command == "stopVibrationAnimation") {
123 stopVibrationAnimation();
124 return true;
125 }
126 return false;
127 }
128
129 void Vibrations::setMode(int mode)
130 {
131 if (mode >= 0 &&
132 mode < static_cast<int>(m_molecule->vibrationFrequencies().size())) {
133 m_mode = mode;
134
135 // Now calculate the frames and set them on the molecule.
136 m_molecule->setCoordinate3d(0);
137 Core::Array<Vector3> atomPositions = m_molecule->atomPositions3d();
138 Core::Array<Vector3> atomDisplacements = m_molecule->vibrationLx(mode);
139 // TODO: needs an option (show forces or not)
140 double factor = 0.01 * m_amplitude;
141 Index atom = 0;
142 for (Vector3& v : atomDisplacements) {
143 v *= 10.0 * factor;
144 m_molecule->setForceVector(atom, v);
145 ++atom;
146 }
147
148 int frames = 5; // TODO: needs an option
149 int frameCounter = 0;
150 m_molecule->clearCoordinate3d();
151 m_molecule->setCoordinate3d(atomPositions, frameCounter++);
152
153 // Current coords + displacement.
154 for (int i = 1; i <= frames; ++i) {
155 Core::Array<Vector3> framePositions;
156 for (atom = 0; atom < m_molecule->atomCount(); ++atom) {
157 framePositions.push_back(atomPositions[atom] + atomDisplacements[atom] *
158 factor *
159 (double(i) / frames));
160 }
161 m_molecule->setCoordinate3d(framePositions, frameCounter++);
162 }
163 // + displacement back to original.
164 for (int i = frames - 1; i >= 0; --i) {
165 Core::Array<Vector3> framePositions;
166 for (atom = 0; atom < m_molecule->atomCount(); ++atom) {
167 framePositions.push_back(atomPositions[atom] + atomDisplacements[atom] *
168 factor *
169 (double(i) / frames));
170 }
171 m_molecule->setCoordinate3d(framePositions, frameCounter++);
172 }
173 // Current coords - displacement.
174 for (int i = 1; i <= frames; ++i) {
175 Core::Array<Vector3> framePositions;
176 for (atom = 0; atom < m_molecule->atomCount(); ++atom) {
177 framePositions.push_back(atomPositions[atom] - atomDisplacements[atom] *
178 factor *
179 (double(i) / frames));
180 }
181 m_molecule->setCoordinate3d(framePositions, frameCounter++);
182 }
183 // - displacement back to original.
184 for (int i = frames - 1; i >= 0; --i) {
185 Core::Array<Vector3> framePositions;
186 for (atom = 0; atom < m_molecule->atomCount(); ++atom) {
187 framePositions.push_back(atomPositions[atom] - atomDisplacements[atom] *
188 factor *
189 (double(i) / frames));
190 }
191 m_molecule->setCoordinate3d(framePositions, frameCounter++);
192 }
193 }
194 }
195
196 void Vibrations::setAmplitude(int amplitude)
197 {
198 m_amplitude = amplitude;
199 setMode(m_mode);
200 }
201
202 void Vibrations::startVibrationAnimation()
203 {
204 // First calculate our frames, and then start our timer.
205 m_totalFrames = m_molecule->coordinate3dCount();
206 m_currentFrame = 0;
207
208 if (!m_timer) {
209 m_timer = new QTimer(this);
210 connect(m_timer, SIGNAL(timeout()), SLOT(advanceFrame()));
211 }
212 if (!m_timer->isActive()) {
213 m_timer->start(50);
214 }
215 }
216
217 void Vibrations::stopVibrationAnimation()
218 {
219 if (m_timer && m_timer->isActive()) {
220 m_timer->stop();
221 m_molecule->setCoordinate3d(0);
222 m_currentFrame = 0;
223 // Use Moved flag for coordinate changes, not Added (which implies new
224 // atoms)
225 m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Moved);
226 }
227 }
228
229 void Vibrations::openDialog()
230 {
231 if (!m_dialog) {
232 m_dialog = new VibrationDialog(qobject_cast<QWidget*>(parent()));
233 connect(m_dialog, SIGNAL(modeChanged(int)), SLOT(setMode(int)));
234 connect(m_dialog, SIGNAL(amplitudeChanged(int)), SLOT(setAmplitude(int)));
235 connect(m_dialog, SIGNAL(startAnimation()),
236 SLOT(startVibrationAnimation()));
237 connect(m_dialog, SIGNAL(stopAnimation()), SLOT(stopVibrationAnimation()));
238 }
239 if (m_molecule)
240 m_dialog->setMolecule(m_molecule);
241 m_dialog->show();
242 m_dialog->raise();
243 m_dialog->activateWindow();
244 }
245
246 void Vibrations::advanceFrame()
247 {
248 if (++m_currentFrame >= m_totalFrames)
249 m_currentFrame = 0;
250 m_molecule->setCoordinate3d(m_currentFrame);
251 // Use Moved flag for coordinate changes, not Added (which implies new atoms)
252 m_molecule->emitChanged(QtGui::Molecule::Atoms | QtGui::Molecule::Moved);
253 }
254 } // namespace Avogadro::QtPlugins