Skip to content

Commit

Permalink
Merge pull request #1924 from ghutchis/more-defensive-forcefield
Browse files Browse the repository at this point in the history
  • Loading branch information
ghutchis authored Jan 7, 2025
2 parents 857517c + fcdd618 commit 6b8911a
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions avogadro/qtplugins/forcefield/forcefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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"));
Expand Down Expand Up @@ -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<Vector3> pos = m_molecule->atomPositions3d();
Expand All @@ -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
Expand Down

0 comments on commit 6b8911a

Please sign in to comment.