| 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_FORCEFIELD_H |
| 7 |
#define AVOGADRO_QTPLUGINS_FORCEFIELD_H |
| 8 |
|
| 9 |
#include <avogadro/qtgui/extensionplugin.h> |
| 10 |
|
| 11 |
#include <avogadro/calc/energyoptimizer.h> |
| 12 |
#include <avogadro/core/constraint.h> |
| 13 |
#include <avogadro/core/molecule.h> |
| 14 |
|
| 15 |
#include <Eigen/Core> |
| 16 |
|
| 17 |
#include <QtCore/QMultiHash> |
| 18 |
#include <QtCore/QMultiMap> |
| 19 |
#include <QtCore/QStringList> |
| 20 |
#include <QtCore/QVariant> |
| 21 |
|
| 22 |
class QAction; |
| 23 |
class QDialog; |
| 24 |
class QProgressDialog; |
| 25 |
class QThread; |
| 26 |
|
| 27 |
namespace Avogadro { |
| 28 |
|
| 29 |
namespace Calc { |
| 30 |
class EnergyCalculator; |
| 31 |
} |
| 32 |
|
| 33 |
namespace QtGui { |
| 34 |
class CalcWorker; |
| 35 |
} |
| 36 |
|
| 37 |
namespace QtPlugins { |
| 38 |
|
| 39 |
/** |
| 40 |
* @brief The Forcefield class implements the extension interface for |
| 41 |
* forcefield (and other) optimization |
| 42 |
* @author Geoffrey R. Hutchison |
| 43 |
*/ |
| 44 |
class Forcefield : public QtGui::ExtensionPlugin |
| 45 |
{ |
| 46 |
Q_OBJECT |
| 47 |
|
| 48 |
public: |
| 49 |
// Currently unused - defaults to LBFGS |
| 50 |
enum Minimizer |
| 51 |
{ |
| 52 |
SteepestDescent = 0, |
| 53 |
ConjugateGradients, |
| 54 |
LBFGS, |
| 55 |
FIRE, |
| 56 |
}; |
| 57 |
|
| 58 |
explicit Forcefield(QObject* parent = nullptr); |
| 59 |
~Forcefield() override; |
| 60 |
|
| 61 |
QString name() const override { return tr("Forcefield optimization"); } |
| 62 |
|
| 63 |
QString description() const override |
| 64 |
{ |
| 65 |
return tr("Forcefield minimization, including scripts"); |
| 66 |
} |
| 67 |
|
| 68 |
QList<QAction*> actions() const override; |
| 69 |
|
| 70 |
QStringList menuPath(QAction*) const override; |
| 71 |
|
| 72 |
void setMolecule(QtGui::Molecule* mol) override; |
| 73 |
void setupMethod(); |
| 74 |
|
| 75 |
std::string recommendedForceField() const; |
| 76 |
|
| 77 |
public slots: |
| 78 |
/** |
| 79 |
* Scan for new scripts in the Forcefield directories. |
| 80 |
*/ |
| 81 |
void refreshScripts(); |
| 82 |
void registerScripts(); |
| 83 |
void unregisterScripts(); |
| 84 |
|
| 85 |
void showDialog(); |
| 86 |
|
| 87 |
/** |
| 88 |
* Handle a feature registered by PackageManager. |
| 89 |
*/ |
| 90 |
void registerFeature(const QString& type, const QString& packageDir, |
| 91 |
const QString& command, const QString& identifier, |
| 92 |
const QVariantMap& metadata); |
| 93 |
|
| 94 |
/** |
| 95 |
* Handle a feature removed by PackageManager. |
| 96 |
*/ |
| 97 |
void unregisterFeature(const QString& type, const QString& packageDir, |
| 98 |
const QString& command, const QString& identifier); |
| 99 |
|
| 100 |
private slots: |
| 101 |
void energy(); |
| 102 |
void forces(); |
| 103 |
void optimize(); |
| 104 |
void freezeSelected(); |
| 105 |
void unfreezeSelected(); |
| 106 |
void setupConstraints(); |
| 107 |
|
| 108 |
void freezeAxis(int axis); |
| 109 |
void freezeX(); |
| 110 |
void freezeY(); |
| 111 |
void freezeZ(); |
| 112 |
|
| 113 |
// fuse adds all pairwise distance constraints |
| 114 |
void fuseSelected(); |
| 115 |
void unfuseSelected(); |
| 116 |
void updateActions(); |
| 117 |
|
| 118 |
// worker thread callbacks |
| 119 |
void onOptimizeChunkDone(Eigen::VectorXd positions, Eigen::VectorXd gradient, |
| 120 |
double energy, bool converged); |
| 121 |
void onEnergyDone(Eigen::VectorXd gradient, double energy); |
| 122 |
void onForcesDone(Eigen::VectorXd gradient, double energy); |
| 123 |
void onWorkerReady(); |
| 124 |
|
| 125 |
private: |
| 126 |
void cleanupWorker(); |
| 127 |
void startWorker(); |
| 128 |
void sendInitCalculator(); |
| 129 |
QList<QAction*> m_actions; |
| 130 |
QtGui::Molecule* m_molecule = nullptr; |
| 131 |
Calc::EnergyCalculator* m_method = nullptr; |
| 132 |
std::string m_methodName; |
| 133 |
bool m_autodetect; |
| 134 |
|
| 135 |
// defaults |
| 136 |
Minimizer m_minimizer = LBFGS; |
| 137 |
unsigned int m_maxSteps = 100; |
| 138 |
unsigned int m_nSteps = 5; |
| 139 |
double m_tolerance = 1.0e-6; |
| 140 |
double m_gradientTolerance = 1.0e-4; |
| 141 |
QVariantMap m_modelUserOptions; |
| 142 |
|
| 143 |
QList<Calc::EnergyCalculator*> m_scripts; |
| 144 |
QMultiHash<QString, QString> m_packageScripts; |
| 145 |
|
| 146 |
// worker thread state |
| 147 |
QThread* m_workerThread = nullptr; |
| 148 |
QtGui::CalcWorker* m_worker = nullptr; |
| 149 |
QProgressDialog* m_progressDialog = nullptr; |
| 150 |
bool m_optimizing = false; |
| 151 |
int m_currentStep = 0; |
| 152 |
Eigen::VectorXd m_lastPositions; |
| 153 |
double m_lastEnergy = 0.0; |
| 154 |
Calc::OptimizationOptions m_optOptions; |
| 155 |
|
| 156 |
// Pending initCalculator args (set by startWorker, sent by |
| 157 |
// sendInitCalculator) |
| 158 |
Calc::EnergyCalculator* m_pendingCalc = nullptr; |
| 159 |
Core::Molecule m_pendingSnapshot; |
| 160 |
Eigen::VectorXd m_pendingMask; |
| 161 |
std::vector<Core::Constraint> m_pendingConstraints; |
| 162 |
}; |
| 163 |
|
| 164 |
} // namespace QtPlugins |
| 165 |
} // namespace Avogadro |
| 166 |
|
| 167 |
#endif // AVOGADRO_QTPLUGINS_FORCEFIELD_H |