trust_region
A scaled trust-region quasi-Newton optimiser.
At each iteration a quadratic model m(p) = g.p + p.B.p / 2 is minimised
inside a ball of radius delta; the step is taken, and the ratio of the actual
reduction to the predicted one decides whether to keep it and what to do with
the radius. The implementation follows klasp's trust_region.hpp, generalised
from rigid bodies to any Coordinates.
Four details carry most of the robustness, each of them a failure mode that had to be fixed rather than a refinement:
-
Scaled coordinates. Degrees of freedom are measured in Angstroms of atomic displacement (
Coordinates.scale), so one radius and one gradient tolerance mean the same thing for an atom, a cell strain and anything added later. Unscaled, the radius is set by whichever freedom carries the largest units. -
Powell damping. Plain BFGS must skip the curvature update whenever
y.s <= 0, which is where the model is worst -- and in a trust region those are also the steps that get rejected, so a model that causes a rejection never learns from it. Damping blendsytowardsBsby just enough to keep the update positive definite, so every step contributes. -
The radius shrinks only on rejection. Shrinking whenever
rho < 1/4regardless of acceptance is the textbook rule; measured against this objective it over-reacts to a noisy energy and collapses the radius. -
A floor on the radius, and restarts. Repeated rejection drives the radius geometrically to zero; once a step is too small to change the energy, every later trial is rejected and the structure is stuck. On reaching the floor the model is reset from the accepted point, and after a few restarts it pins at the floor and keeps inching.
A fifth is about the calculator rather than the algorithm: below the smallest
energy difference a calculator can resolve, the reduction ratio carries no
information and is not computed. See _judge.
Relaxation
dataclass
The outcome of a relaxation.
Attributes:
| Name | Type | Description |
|---|---|---|
converged |
bool
|
whether every convergence criterion was met |
steps |
int
|
iterations taken |
energy |
float
|
final energy in eV |
measures |
dict
|
final convergence measures, e.g. fmax in eV/A and smax in GPa |
evaluations |
int
|
calculator evaluations used |
history |
list
|
one |
structure |
object
|
the relaxed structure |
result |
object
|
the calculator result at the final geometry |
model |
object
|
the |
stages |
list
|
for a staged relaxation, the |
Source code in chmpy/opt/trust_region.py
accepted_steps
property
How many trial steps were kept
Step
dataclass
One iteration, for the trajectory.
Source code in chmpy/opt/trust_region.py
TrustRegion
Relax a structure by a scaled dogleg trust region with damped BFGS.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
coordinates
|
the degrees of freedom to vary |
required | |
calculator
|
what to evaluate energies with |
required | |
options
|
tuning, or None for the defaults |
None
|
|
pressure
|
float
|
external hydrostatic pressure in GPa, applied when the coordinates include a cell |
0.0
|
hessian
|
(n_dof, n_dof) starting model of the curvature, in scaled coordinates. The default is the identity, which is a reasonable model precisely because the coordinates are scaled to Angstroms. Passing a better one -- an analytic or previously converged Hessian -- is the single biggest saving available on a hard system. |
None
|
|
model
|
the curvature model, or None to build one. Anything with
|
None
|
Source code in chmpy/opt/trust_region.py
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 | |
run(fmax=0.01, smax=0.05, steps=200, progress=None)
Relax until converged or out of steps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fmax
|
float
|
force convergence in eV/A, on the largest component |
0.01
|
smax
|
float
|
stress convergence in GPa, on the largest component. Ignored when the coordinates hold no cell degrees of freedom. |
0.05
|
steps
|
int
|
maximum iterations |
200
|
progress
|
True to print a line per step, or a callable given a
|
None
|
Returns:
| Type | Description |
|---|---|
Relaxation
|
Relaxation |
Source code in chmpy/opt/trust_region.py
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 | |
TrustRegionOptions
dataclass
Tuning for TrustRegion. The defaults are klasp's measured ones.
Attributes:
| Name | Type | Description |
|---|---|---|
eta |
float
|
smallest reduction ratio that still accepts a step |
rho_max |
float
|
above this the model is so wrong the step is treated as failed |
delta0 |
float
|
initial and post-restart radius, in Angstroms of displacement |
delta_max |
float
|
largest radius |
delta_min |
float
|
floor below which the radius has collapsed |
energy_noise |
float | None
|
smallest energy difference worth believing, in eV. None asks the calculator. |
max_restarts |
int
|
how many times to reset the model before pinning at the floor |
grow |
float
|
factor to grow the radius by on a good step at the boundary |
shrink |
float
|
factor to shrink by on a rejected step |
shrink_hard |
float
|
factor for a step that was not merely bad but unusable |