Skip to content

Commit

Permalink
Add progress bar for optimizations, including cancel
Browse files Browse the repository at this point in the history
Fix OpenChemistry#1601

Signed-off-by: Geoff Hutchison <[email protected]>
  • Loading branch information
ghutchis committed Dec 27, 2024
1 parent b6365a9 commit af1f89a
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions avogadro/qtplugins/forcefield/forcefield.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -297,17 +297,30 @@ void Forcefield::optimize()
qDebug() << " maxSteps" << m_maxSteps << " steps "
<< m_maxSteps / crit.iterations;

QProgressDialog progress(tr("Optimize Geometry"), "Cancel", 0,
m_maxSteps / crit.iterations);
progress.setWindowModality(Qt::WindowModal);
progress.setMinimumDuration(0);
progress.setAutoClose(true);
progress.show();

Real currentEnergy = 0.0;
for (unsigned int i = 0; i < m_maxSteps / crit.iterations; ++i) {
solver.minimize(*m_method, positions);
// update the progress dialog
progress.setValue(i);

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

currentEnergy = m_method->value(positions);
progress.setLabelText(
tr("Energy: %L1", "force field energy").arg(currentEnergy, 0, 'f', 3));
// get the current gradient for force visualization
m_method->gradient(positions, gradient);
#ifndef NDEBUG
qDebug() << " optimize " << i << currentEnergy
<< " gradNorm: " << gradient.norm();
#endif

// update coordinates
bool isFinite = std::isfinite(currentEnergy);
Expand Down Expand Up @@ -352,6 +365,9 @@ void Forcefield::optimize()

energy = currentEnergy;
}

if (progress.wasCanceled())
break;
}

m_molecule->undoMolecule()->setInteractive(isInteractive);
Expand Down

1 comment on commit af1f89a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ERROR: clang-format-diff detected formatting issues. See the artifact for a patch or run clang-format on your branch.

Please sign in to comment.