| 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_SCRIPTENERGY_H |
| 7 |
#define AVOGADRO_QTPLUGINS_SCRIPTENERGY_H |
| 8 |
|
| 9 |
#include <avogadro/calc/energycalculator.h> |
| 10 |
|
| 11 |
#include <avogadro/core/avogadrocore.h> |
| 12 |
|
| 13 |
#include <QtCore/QCoreApplication> |
| 14 |
#include <QtCore/QJsonObject> |
| 15 |
#include <QtCore/QString> |
| 16 |
#include <QtCore/QVariantMap> |
| 17 |
|
| 18 |
namespace Avogadro { |
| 19 |
|
| 20 |
namespace Io { |
| 21 |
class FileFormat; |
| 22 |
} |
| 23 |
|
| 24 |
namespace QtGui { |
| 25 |
class PythonScript; |
| 26 |
} |
| 27 |
|
| 28 |
namespace QtPlugins { |
| 29 |
|
| 30 |
class ScriptEnergy : public Avogadro::Calc::EnergyCalculator |
| 31 |
{ |
| 32 |
Q_DECLARE_TR_FUNCTIONS(ScriptEnergy) |
| 33 |
|
| 34 |
public: |
| 35 |
/** Formats that may be written to the script's input. */ |
| 36 |
enum Format |
| 37 |
{ |
| 38 |
NotUsed, |
| 39 |
Cjson, |
| 40 |
Cml, |
| 41 |
Mdl, |
| 42 |
Pdb, |
| 43 |
Sdf, |
| 44 |
Xyz |
| 45 |
}; |
| 46 |
|
| 47 |
/** Protocol for coordinate/energy/gradient transport. */ |
| 48 |
enum class Protocol |
| 49 |
{ |
| 50 |
TextV1, |
| 51 |
// "binary-v1" uses little-endian float64 values on the wire. |
| 52 |
BinaryV1 |
| 53 |
}; |
| 54 |
|
| 55 |
ScriptEnergy(const QString& scriptFileName = ""); |
| 56 |
~ScriptEnergy() override; |
| 57 |
|
| 58 |
/** |
| 59 |
* Configure the interpreter for package-based (pixi) execution. |
| 60 |
*/ |
| 61 |
void setPackageInfo(const QString& packageDir, const QString& command, |
| 62 |
const QString& identifier); |
| 63 |
|
| 64 |
/** |
| 65 |
* Populate metadata fields from a QVariantMap (e.g. from pyproject.toml) |
| 66 |
* instead of calling the script with --metadata. |
| 67 |
*/ |
| 68 |
void readMetaData(const QVariantMap& metadata); |
| 69 |
|
| 70 |
QString scriptFilePath() const; |
| 71 |
|
| 72 |
Format inputFormat() const { return m_inputFormat; } |
| 73 |
bool isValid() const { return m_valid; } |
| 74 |
|
| 75 |
Calc::EnergyCalculator* newInstance() const override; |
| 76 |
|
| 77 |
std::string identifier() const override { return m_identifier; } |
| 78 |
std::string name() const override { return m_name; } |
| 79 |
std::string description() const override { return m_description; } |
| 80 |
std::string userOptions() const override; |
| 81 |
bool setUserOptions(const std::string& optionsJson) override; |
| 82 |
|
| 83 |
Core::Molecule::ElementMask elements() const override { return m_elements; } |
| 84 |
bool supportsGradients() const { return m_gradients; } |
| 85 |
bool acceptsIons() const override { return m_ions; } |
| 86 |
bool acceptsRadicals() const override { return m_radicals; } |
| 87 |
bool acceptsUnitCell() const override { return m_unitCells; } |
| 88 |
|
| 89 |
// This will check if the molecule is valid for this script |
| 90 |
// and then start the external process |
| 91 |
void setMolecule(Core::Molecule* mol) override; |
| 92 |
// energy |
| 93 |
Real value(const Eigen::VectorXd& x) override; |
| 94 |
// gradient (which may be unsupported and fall back to numeric) |
| 95 |
void gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad) override; |
| 96 |
// fused energy + gradient |
| 97 |
Real evaluate(const Eigen::VectorXd& x, Eigen::VectorXd* grad) override; |
| 98 |
// hessian (falls back to finite difference if unsupported by script) |
| 99 |
void hessian(const Eigen::VectorXd& x, Eigen::MatrixXd& hess) override; |
| 100 |
|
| 101 |
private: |
| 102 |
static Format stringToFormat(const std::string& str); |
| 103 |
static Protocol stringToProtocol(const std::string& str); |
| 104 |
static Io::FileFormat* createFileFormat(Format fmt); |
| 105 |
void resetMetaData(); |
| 106 |
void processElementString(const QString& str); |
| 107 |
void copyMetaDataFrom(const ScriptEnergy& other); |
| 108 |
|
| 109 |
QByteArray writeCoordinatesText(const Eigen::VectorXd& x); |
| 110 |
QByteArray writeCoordinatesBinary(const Eigen::VectorXd& x, |
| 111 |
quint16 requestFlags) const; |
| 112 |
bool parseResponseBinary(const QByteArray& response, quint16 requestFlags, |
| 113 |
double* energy, Eigen::VectorXd* grad, |
| 114 |
Eigen::MatrixXd* hess) const; |
| 115 |
bool readBinaryFrame(const QByteArray& input, QByteArray& frame); |
| 116 |
bool evaluateBinary(const Eigen::VectorXd& x, quint16 requestFlags, |
| 117 |
double* energy = nullptr, Eigen::VectorXd* grad = nullptr, |
| 118 |
Eigen::MatrixXd* hess = nullptr); |
| 119 |
bool buildBootstrapInput(QByteArray& input) const; |
| 120 |
|
| 121 |
private: |
| 122 |
QtGui::PythonScript* m_interpreter; |
| 123 |
Format m_inputFormat; |
| 124 |
Protocol m_protocol; |
| 125 |
Core::Molecule* m_molecule; |
| 126 |
|
| 127 |
// what's supported by this script |
| 128 |
Core::Molecule::ElementMask m_elements; |
| 129 |
bool m_valid; |
| 130 |
bool m_gradients; |
| 131 |
bool m_hessians; |
| 132 |
bool m_ions; |
| 133 |
bool m_radicals; |
| 134 |
bool m_unitCells; |
| 135 |
|
| 136 |
std::string m_identifier; |
| 137 |
std::string m_name; |
| 138 |
std::string m_description; |
| 139 |
QString m_formatString; |
| 140 |
QJsonObject m_userOptionsSchema; |
| 141 |
QJsonObject m_userOptionsValues; |
| 142 |
}; |
| 143 |
|
| 144 |
} // namespace QtPlugins |
| 145 |
} // namespace Avogadro |
| 146 |
|
| 147 |
#endif // AVOGADRO_QTPLUGINS_SCRIPTENERGY_H |