Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add progress bar for optimizations, including cancel #1893

Merged
merged 1 commit into from
Dec 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading