| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "obforcefielddialog.h" |
| 7 |
#include "ui_obforcefielddialog.h" |
| 8 |
|
| 9 |
#include <QtCore/QDebug> |
| 10 |
#include <QtCore/QSettings> |
| 11 |
#include <QtCore/QString> |
| 12 |
#include <QtCore/QStringList> |
| 13 |
|
| 14 |
#include <cmath> // for log10 |
| 15 |
|
| 16 |
namespace Avogadro::QtPlugins { |
| 17 |
|
| 18 |
enum OptimizationAlgorithm |
| 19 |
{ |
| 20 |
SteepestDescent = 0, |
| 21 |
ConjugateGradient |
| 22 |
}; |
| 23 |
|
| 24 |
enum LineSearchMethod |
| 25 |
{ |
| 26 |
Simple = 0, |
| 27 |
Newton |
| 28 |
}; |
| 29 |
|
| 30 |
OBForceFieldDialog::OBForceFieldDialog(const QStringList& forceFields, |
| 31 |
QWidget* parent_) |
| 32 |
: QDialog(parent_), ui(new Ui::OBForceFieldDialog) |
| 33 |
{ |
| 34 |
ui->setupUi(this); |
| 35 |
ui->forceField->addItems(forceFields); |
| 36 |
updateRecommendedForceField(); |
| 37 |
|
| 38 |
connect(ui->useRecommended, SIGNAL(toggled(bool)), |
| 39 |
SLOT(useRecommendedForceFieldToggled(bool))); |
| 40 |
|
| 41 |
QSettings settings; |
| 42 |
bool autoDetect = |
| 43 |
settings.value("openbabel/optimizeGeometry/autoDetect", true).toBool(); |
| 44 |
ui->useRecommended->setChecked(autoDetect); |
| 45 |
} |
| 46 |
|
| 47 |
OBForceFieldDialog::~OBForceFieldDialog() |
| 48 |
{ |
| 49 |
delete ui; |
| 50 |
} |
| 51 |
|
| 52 |
QStringList OBForceFieldDialog::prompt(QWidget* parent_, |
| 53 |
const QStringList& forceFields, |
| 54 |
const QStringList& startingOptions, |
| 55 |
const QString& recommendedForceField_) |
| 56 |
{ |
| 57 |
OBForceFieldDialog dlg(forceFields, parent_); |
| 58 |
dlg.setOptions(startingOptions); |
| 59 |
dlg.setRecommendedForceField(recommendedForceField_); |
| 60 |
|
| 61 |
QStringList options; |
| 62 |
if (static_cast<DialogCode>(dlg.exec()) == Accepted) |
| 63 |
options = dlg.options(); |
| 64 |
|
| 65 |
return options; |
| 66 |
} |
| 67 |
|
| 68 |
QStringList OBForceFieldDialog::options() const |
| 69 |
{ |
| 70 |
QStringList opts; |
| 71 |
|
| 72 |
opts << "--crit" |
| 73 |
<< QString::number(std::pow(10.0f, ui->energyConv->value()), 'e', 0) |
| 74 |
<< "--ff" << ui->forceField->currentText() << "--steps" |
| 75 |
<< QString::number(ui->stepLimit->value()) << "--rvdw" |
| 76 |
<< QString::number(ui->vdwCutoff->value()) << "--rele" |
| 77 |
<< QString::number(ui->eleCutoff->value()) << "--freq" |
| 78 |
<< QString::number(ui->pairFreq->value()); |
| 79 |
|
| 80 |
switch (static_cast<OptimizationAlgorithm>(ui->algorithm->currentIndex())) { |
| 81 |
case SteepestDescent: |
| 82 |
opts << "--sd"; |
| 83 |
break; |
| 84 |
default: |
| 85 |
case ConjugateGradient: |
| 86 |
break; |
| 87 |
} |
| 88 |
|
| 89 |
switch (static_cast<LineSearchMethod>(ui->lineSearch->currentIndex())) { |
| 90 |
case Newton: |
| 91 |
opts << "--newton"; |
| 92 |
break; |
| 93 |
default: |
| 94 |
case Simple: |
| 95 |
break; |
| 96 |
} |
| 97 |
|
| 98 |
if (ui->enableCutoffs->isChecked()) |
| 99 |
opts << "--cut"; |
| 100 |
|
| 101 |
return opts; |
| 102 |
} |
| 103 |
|
| 104 |
void OBForceFieldDialog::setOptions(const QStringList& opts) |
| 105 |
{ |
| 106 |
// Set some defaults. These match the defaults in obabel -L minimize |
| 107 |
ui->energyConv->setValue(-6); |
| 108 |
ui->algorithm->setCurrentIndex(static_cast<int>(ConjugateGradient)); |
| 109 |
ui->lineSearch->setCurrentIndex(static_cast<int>(Simple)); |
| 110 |
ui->stepLimit->setValue(2500); |
| 111 |
ui->enableCutoffs->setChecked(false); |
| 112 |
ui->vdwCutoff->setValue(10.0); |
| 113 |
ui->eleCutoff->setValue(10.0); |
| 114 |
ui->pairFreq->setValue(10); |
| 115 |
|
| 116 |
for (QStringList::const_iterator it = opts.constBegin(), |
| 117 |
itEnd = opts.constEnd(); |
| 118 |
it < itEnd; ++it) { |
| 119 |
|
| 120 |
// We'll always use log: |
| 121 |
if (*it == "--log") { |
| 122 |
continue; |
| 123 |
} |
| 124 |
|
| 125 |
// Energy convergence: |
| 126 |
else if (*it == "--crit") { |
| 127 |
++it; |
| 128 |
if (it == itEnd) { |
| 129 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 130 |
"--crit missing argument."; |
| 131 |
continue; |
| 132 |
} |
| 133 |
|
| 134 |
bool ok; |
| 135 |
float econv = it->toFloat(&ok); |
| 136 |
if (!ok) { |
| 137 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 138 |
"--crit is not numeric: " |
| 139 |
<< *it; |
| 140 |
continue; |
| 141 |
} |
| 142 |
|
| 143 |
// We just show the econv as 10^(x), so calculate the nearest x |
| 144 |
int exponent = static_cast<int>(std::floor(std::log10(econv) + 0.5)); |
| 145 |
ui->energyConv->setValue(exponent); |
| 146 |
continue; |
| 147 |
} |
| 148 |
|
| 149 |
// Use steepest descent? |
| 150 |
else if (*it == "--sd") { |
| 151 |
ui->algorithm->setCurrentIndex(SteepestDescent); |
| 152 |
continue; |
| 153 |
} |
| 154 |
|
| 155 |
// Use newton linesearch? |
| 156 |
else if (*it == "--newton") { |
| 157 |
ui->lineSearch->setCurrentIndex(Newton); |
| 158 |
continue; |
| 159 |
} |
| 160 |
|
| 161 |
// Force field? |
| 162 |
else if (*it == "--ff") { |
| 163 |
++it; |
| 164 |
if (it == itEnd) { |
| 165 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 166 |
"--ff missing argument."; |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
int index = ui->forceField->findText(*it); |
| 171 |
if (index < 0) { |
| 172 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 173 |
"--ff unknown: " |
| 174 |
<< *it; |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
ui->forceField->setCurrentIndex(index); |
| 179 |
continue; |
| 180 |
} |
| 181 |
|
| 182 |
// Step limit? |
| 183 |
else if (*it == "--steps") { |
| 184 |
++it; |
| 185 |
if (it == itEnd) { |
| 186 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 187 |
"--steps missing argument."; |
| 188 |
continue; |
| 189 |
} |
| 190 |
|
| 191 |
bool ok; |
| 192 |
int numSteps = it->toInt(&ok); |
| 193 |
if (!ok) { |
| 194 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 195 |
"--steps is not numeric: " |
| 196 |
<< *it; |
| 197 |
continue; |
| 198 |
} |
| 199 |
|
| 200 |
ui->stepLimit->setValue(numSteps); |
| 201 |
continue; |
| 202 |
} |
| 203 |
|
| 204 |
// Use cutoff? |
| 205 |
else if (*it == "--cut") { |
| 206 |
ui->enableCutoffs->setChecked(true); |
| 207 |
continue; |
| 208 |
} |
| 209 |
|
| 210 |
// Van der Waals cutoff |
| 211 |
else if (*it == "--rvdw") { |
| 212 |
++it; |
| 213 |
if (it == itEnd) { |
| 214 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 215 |
"--rvdw missing argument."; |
| 216 |
continue; |
| 217 |
} |
| 218 |
|
| 219 |
bool ok; |
| 220 |
double cutoff = it->toDouble(&ok); |
| 221 |
if (!ok) { |
| 222 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 223 |
"--rvdw is not numeric: " |
| 224 |
<< *it; |
| 225 |
continue; |
| 226 |
} |
| 227 |
|
| 228 |
ui->vdwCutoff->setValue(cutoff); |
| 229 |
continue; |
| 230 |
} |
| 231 |
|
| 232 |
// electrostatic cutoff |
| 233 |
else if (*it == "--rele") { |
| 234 |
++it; |
| 235 |
if (it == itEnd) { |
| 236 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 237 |
"--rele missing argument."; |
| 238 |
continue; |
| 239 |
} |
| 240 |
|
| 241 |
bool ok; |
| 242 |
double cutoff = it->toDouble(&ok); |
| 243 |
if (!ok) { |
| 244 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 245 |
"--rele is not numeric: " |
| 246 |
<< *it; |
| 247 |
continue; |
| 248 |
} |
| 249 |
|
| 250 |
ui->eleCutoff->setValue(cutoff); |
| 251 |
continue; |
| 252 |
} |
| 253 |
|
| 254 |
// Pair update frequency: |
| 255 |
else if (*it == "--freq") { |
| 256 |
++it; |
| 257 |
if (it == itEnd) { |
| 258 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 259 |
"--freq missing argument."; |
| 260 |
continue; |
| 261 |
} |
| 262 |
|
| 263 |
bool ok; |
| 264 |
int numSteps = it->toInt(&ok); |
| 265 |
if (!ok) { |
| 266 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 267 |
"--freq is not numeric: " |
| 268 |
<< *it; |
| 269 |
continue; |
| 270 |
} |
| 271 |
|
| 272 |
ui->pairFreq->setValue(numSteps); |
| 273 |
continue; |
| 274 |
} |
| 275 |
|
| 276 |
// ????? |
| 277 |
else { |
| 278 |
qWarning() << "OBForceFieldDialog::setOptions: " |
| 279 |
"Unrecognized option: " |
| 280 |
<< *it; |
| 281 |
} |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
void OBForceFieldDialog::setRecommendedForceField(const QString& rff) |
| 286 |
{ |
| 287 |
if (rff == m_recommendedForceField) |
| 288 |
return; |
| 289 |
|
| 290 |
if (ui->forceField->findText(rff) == -1) |
| 291 |
return; |
| 292 |
|
| 293 |
m_recommendedForceField = rff; |
| 294 |
updateRecommendedForceField(); |
| 295 |
} |
| 296 |
|
| 297 |
void OBForceFieldDialog::useRecommendedForceFieldToggled(bool state) |
| 298 |
{ |
| 299 |
if (!m_recommendedForceField.isEmpty()) { |
| 300 |
if (state) { |
| 301 |
int index = ui->forceField->findText(m_recommendedForceField); |
| 302 |
if (index >= 0) { |
| 303 |
ui->forceField->setCurrentIndex(index); |
| 304 |
} |
| 305 |
} |
| 306 |
} |
| 307 |
ui->forceField->setEnabled(!state); |
| 308 |
|
| 309 |
QSettings().setValue("openbabel/optimizeGeometry/autoDetect", state); |
| 310 |
} |
| 311 |
|
| 312 |
void OBForceFieldDialog::updateRecommendedForceField() |
| 313 |
{ |
| 314 |
if (m_recommendedForceField.isEmpty()) { |
| 315 |
ui->useRecommended->hide(); |
| 316 |
ui->forceField->setEnabled(true); |
| 317 |
} else { |
| 318 |
ui->useRecommended->setText( |
| 319 |
tr("Autodetect (%1)").arg(m_recommendedForceField)); |
| 320 |
// Force the combo box to update if needed: |
| 321 |
useRecommendedForceFieldToggled(ui->useRecommended->isChecked()); |
| 322 |
ui->useRecommended->show(); |
| 323 |
} |
| 324 |
} |
| 325 |
|
| 326 |
} // namespace Avogadro::QtPlugins |