Skip to content

Commit

Permalink
Fixed problem - gradients went the wrong way
Browse files Browse the repository at this point in the history
Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Oct 23, 2023
1 parent 10d7d40 commit f2e416f
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 17 deletions.
2 changes: 0 additions & 2 deletions avogadro/calc/energycalculator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,10 @@ void EnergyCalculator::cleanGradients(TVector& grad)
}

// freeze any masked atoms or coordinates
/*
if (m_mask.rows() == size)
grad = grad.cwiseProduct(m_mask);
else
std::cerr << "Error: mask size " << m_mask.rows() << " " << grad.rows() << std::endl;
*/
}

} // namespace Avogadro
2 changes: 1 addition & 1 deletion avogadro/qtgui/pythonscript.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ void PythonScript::processFinished(int, QProcess::ExitStatus)
void PythonScript::asyncTerminate()
{
if (m_process != nullptr) {
m_process->terminate();
disconnect(m_process, SIGNAL(finished()), this, SLOT(processFinished()));
m_process->kill();
m_process->deleteLater();
m_process = nullptr;
}
Expand Down
3 changes: 3 additions & 0 deletions avogadro/qtplugins/forcefield/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@ target_link_libraries(Forcefield PRIVATE Avogadro::Calc)

# Bundled forcefield scripts
set(forcefields
scripts/gaff.py
scripts/gfn1.py
scripts/gfn2.py
scripts/gfnff.py
scripts/mmff94.py
scripts/uff.py
)

install(PROGRAMS ${forcefields}
Expand Down
20 changes: 10 additions & 10 deletions avogadro/qtplugins/forcefield/forcefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,8 @@

#include <cppoptlib/meta.h>
#include <cppoptlib/problem.h>

#include <cppoptlib/solver/bfgssolver.h>
#include <cppoptlib/solver/lbfgssolver.h>
// not currently used
#include <cppoptlib/solver/conjugatedgradientdescentsolver.h>
#include <cppoptlib/solver/gradientdescentsolver.h>

Expand Down Expand Up @@ -125,9 +124,8 @@ void Forcefield::optimize()
bool isInteractive = m_molecule->undoMolecule()->isInteractive();
m_molecule->undoMolecule()->setInteractive(true);

//cppoptlib::LbfgsSolver<EnergyCalculator> solver;
//cppoptlib::ConjugatedGradientDescentSolver<EnergyCalculator> solver;
cppoptlib::GradientDescentSolver<EnergyCalculator> solver;
cppoptlib::ConjugatedGradientDescentSolver<EnergyCalculator> solver;
//cppoptlib::GradientDescentSolver<EnergyCalculator> solver;

Check notice

Code scanning / CodeQL

Commented-out code Note

This comment appears to contain commented-out code.

int n = m_molecule->atomCount();
// we have to cast the current 3d positions into a VectorXd
Expand All @@ -146,17 +144,17 @@ void Forcefield::optimize()
cppoptlib::Criteria<Real> crit = cppoptlib::Criteria<Real>::defaults();

// e.g., every N steps, update coordinates
crit.iterations = 10;
crit.iterations = m_nSteps;
// we don't set function or gradient criteria
// .. these seem to be broken in the solver code
// .. so we handle ourselves
solver.setStopCriteria(crit);

// set the method
std::string recommended = "MMFF94";
std::string recommended = recommendedForceField();
qDebug() << "Energy method: " << recommended.c_str();

if (m_method == nullptr) {
if (m_method == nullptr || m_method->identifier() != recommended) {
// we have to create the calculator
m_method = Calc::EnergyManager::instance().model(recommended);
}
Expand All @@ -181,6 +179,8 @@ void Forcefield::optimize()
for (unsigned int i = 0; i < m_maxSteps / crit.iterations; ++i) {
solver.minimize(*m_method, positions);

qApp->processEvents(QEventLoop::AllEvents, 500);

currentEnergy = m_method->value(positions);
// get the current gradient for force visualization
m_method->gradient(positions, gradient);
Expand All @@ -204,7 +204,7 @@ void Forcefield::optimize()
pos[i] = Vector3(*(d), *(d + 1), *(d + 2));
d += 3;

forces[i] = -1.0 * Vector3(gradient[3 * i], gradient[3 * i + 1],
forces[i] = -0.1 * Vector3(gradient[3 * i], gradient[3 * i + 1],
gradient[3 * i + 2]);
}
}
Expand Down Expand Up @@ -246,7 +246,7 @@ void Forcefield::energy()
std::string recommended = recommendedForceField();
qDebug() << "Energy method: " << recommended.c_str();

if (m_method == nullptr) {
if (m_method == nullptr || m_method->identifier() != recommended) {
// we have to create the calculator
m_method = Calc::EnergyManager::instance().model(recommended);
}
Expand Down
7 changes: 3 additions & 4 deletions avogadro/qtplugins/forcefield/scriptenergy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ void ScriptEnergy::gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad)
// parse the result
// first split on newlines
QStringList lines = QString(result).remove('\r').split('\n');
double energy = 0.0;
unsigned int i = 0;
bool readingGrad = false;
for (auto line : lines) {
Expand All @@ -166,9 +165,9 @@ void ScriptEnergy::gradient(const Eigen::VectorXd& x, Eigen::VectorXd& grad)
if (readingGrad) {
QStringList items = line.split(" ");
if (items.size() == 3) {
grad[i] = items[0].toDouble();
grad[i + 1] = items[1].toDouble();
grad[i + 2] = items[2].toDouble();
grad[i] = -1.0*items[0].toDouble();
grad[i + 1] = -1.0*items[1].toDouble();
grad[i + 2] = -1.0*items[2].toDouble();
i += 3;
}

Expand Down

0 comments on commit f2e416f

Please sign in to comment.