From fcdd6183312e64ff52dd8298f9ee512a378acd25 Mon Sep 17 00:00:00 2001 From: Geoff Hutchison Date: Tue, 7 Jan 2025 14:58:32 -0500 Subject: [PATCH] Be more careful in forcefield setup Signed-off-by: Geoff Hutchison --- avogadro/qtplugins/forcefield/forcefield.cpp | 28 ++++++++++++++++---- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/avogadro/qtplugins/forcefield/forcefield.cpp b/avogadro/qtplugins/forcefield/forcefield.cpp index ce779507ae..85a0b426b5 100644 --- a/avogadro/qtplugins/forcefield/forcefield.cpp +++ b/avogadro/qtplugins/forcefield/forcefield.cpp @@ -207,6 +207,9 @@ void Forcefield::setMolecule(QtGui::Molecule* mol) void Forcefield::setupMethod() { + if (m_molecule == nullptr) + return; // nothing to do until its set + if (m_autodetect) m_methodName = recommendedForceField(); @@ -234,15 +237,20 @@ void Forcefield::setupMethod() delete m_method; // delete the previous one m_method = Calc::EnergyManager::instance().model(m_methodName); } - - m_method->setMolecule(m_molecule); + if (m_method != nullptr) + m_method->setMolecule(m_molecule); } void Forcefield::optimize() { - if (m_molecule == nullptr || m_method == nullptr) + if (m_molecule == nullptr) return; + if (m_method == nullptr) + setupMethod(); + if (m_method == nullptr) + return; // bad news + if (!m_molecule->atomCount()) { QMessageBox::information(nullptr, tr("Avogadro"), tr("No atoms provided for optimization")); @@ -375,9 +383,14 @@ void Forcefield::optimize() void Forcefield::energy() { - if (m_molecule == nullptr || m_method == nullptr) + if (m_molecule == nullptr) return; + if (m_method == nullptr) + setupMethod(); + if (m_method == nullptr) + return; // bad news + int n = m_molecule->atomCount(); // we have to cast the current 3d positions into a VectorXd Core::Array pos = m_molecule->atomPositions3d(); @@ -395,9 +408,14 @@ void Forcefield::energy() void Forcefield::forces() { - if (m_molecule == nullptr || m_method == nullptr) + if (m_molecule == nullptr) return; + if (m_method == nullptr) + setupMethod(); + if (m_method == nullptr) + return; // bad news + int n = m_molecule->atomCount(); // double-check the mask