base
Energies, forces and stresses of a System.
A calculator subclasses Calculator, declares which properties it computes
natively, and implements compute:
class Harmonic(Calculator):
provides = {"energy", "forces"}
def compute(self, system, want):
displacement = system.positions - self.reference
return Result(
energy=0.5 * self.k * float((displacement**2).sum()),
forces=-self.k * displacement,
)
compute is called only with properties the subclass declared, and may return
more than it was asked for -- the extras are cached rather than discarded.
Around it the base class provides:
- results cached on
(system, version), so repeated queries of an unchanged geometry cost one evaluation; - finite-difference forces and stress for derivatives a subclass does not declare, so an energy-only calculator still works under variable cell;
check_gradients, comparing analytic derivatives against finite differences;batch, a loop by default and one model call for backends that overridecompute_batch;energy_noise, the smallest energy difference the calculator can resolve, which the optimiser needs in order not to read rounding as a result;- conversion to and from
ase.calculators; - composition:
a + bsums two calculators, andcalc.shifted(references)subtracts per-element reference energies; - counters on
calc.stats.
Units are eV and Angstroms throughout; see chmpy.calc.result.
Calculator
Bases: ABC
Base class for anything that computes energies of a System.
Attributes:
| Name | Type | Description |
|---|---|---|
provides |
frozenset
|
the properties |
cache_size |
int
|
how many geometries to remember results for. |
stats |
CalculatorStats
|
call counts and wall time. |
Source code in chmpy/calc/base.py
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 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 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 | |
stats
property
Evaluation counts and wall time
__call__(system, want=(ENERGY,))
Evaluate system, returning at least the properties in want.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
System
|
the geometry to evaluate |
required |
want
|
a property name or an iterable of them |
(ENERGY,)
|
Returns:
| Type | Description |
|---|---|
Result
|
Result, from cache when the geometry has not changed |
Source code in chmpy/calc/base.py
__init_subclass__(**kwargs)
Let a subclass write provides = {"energy", "forces"} and mean it.
as_ase()
Expose this calculator as an ase.calculators.calculator.Calculator,
for workflows that expect one.
batch(systems, want=(ENERGY,))
Evaluate several systems, using the native batch path where there is one.
Cached systems are dropped from the batch and only the rest go to the model, so re-evaluating a partly unchanged set is cheap.
Source code in chmpy/calc/base.py
check_gradients(system, step=None, tolerance=1e-05)
Verify analytic forces and stress against finite differences.
Only derivatives the calculator declares in provides are checked --
anything finite-differenced is trivially self-consistent. Print the
result: it names the worst component.
A failing check is not always a wrong derivative. The numeric side has its own error, largest at small steps where a difference of two nearly equal energies loses precision, and at large steps where the quadratic term stops being negligible. On a stiff crystal the two have their minima at different steps -- forces prefer the larger, stress the smaller -- so a check that fails at one step is worth repeating at another before concluding anything.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
System
|
geometry to check at, ideally one away from a stationary point and with no exact symmetry hiding a sign error |
required |
step
|
float | None
|
displacement in Angstroms and strain increment; defaults to
|
None
|
tolerance
|
float
|
relative to the largest analytic component |
1e-05
|
Returns:
| Type | Description |
|---|---|
GradientCheck
|
GradientCheck, whose |
Source code in chmpy/calc/base.py
clear_cache()
compute(system, want)
abstractmethod
Compute want for system. Implemented by subclasses.
want is always a subset of self.provides and always contains
"energy". Returning more than was asked for is welcome -- the extra
properties are cached rather than discarded.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system
|
System
|
the geometry to evaluate |
required |
want
|
frozenset
|
property names, a subset of |
required |
Returns:
| Type | Description |
|---|---|
Result
|
Result holding at least the requested properties |
Source code in chmpy/calc/base.py
compute_batch(systems, want)
Compute want for several systems.
The default runs compute on each in turn. Override it when the
underlying model can evaluate a list in one pass, which is most of what
a GPU offers when the cells are small.
Source code in chmpy/calc/base.py
elastic_tensor(structure, **kwargs)
The elastic tensor of an already-relaxed structure.
Keyword arguments go to chmpy.opt.elastic_tensor.
Source code in chmpy/calc/base.py
energies(system)
energy(system)
energy_noise(energy)
How much of an energy difference this calculator cannot resolve, in eV.
This matters more than it sounds. A model running in float32 returns
energies with about seven significant figures, so for a cell at -190 eV
one unit in the last place is 1.5e-5 eV -- and near a minimum the real
energy change over an optimiser step is smaller than that. An optimiser
that compares energies then sees a reduction of exactly zero, decides
its model is wrong, and shrinks its way to a halt while the structure is
still moving. chmpy.opt.TrustRegion asks for this number and stops
drawing conclusions from differences below it.
energy_precision sets the relative precision, and a calculator that
knows it should say so -- MetatomicCalculator reads it off the model's
dtype. Left as None it is inferred from the energies that come back: a
float32 model's energies are all exactly representable in float32.
The inference waits for several distinct energies before concluding
anything, because single round numbers are exactly representable in
float32 too: deciding on one sample called -1.0, -0.5 and -100.0
float32, which would hand an analytic potential a noise floor of 1e-5
eV and let the optimiser accept steps that genuinely go uphill. What
the inference still cannot do is tell a float32 model from a calculator
whose energies are all round numbers, since those are float32 values
by definition. Real potentials do not behave that way, but a test
double might -- set energy_precision explicitly if yours does.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
energy
|
float
|
the energy in question, eV |
required |
Returns:
| Type | Description |
|---|---|
float
|
the smallest energy difference worth believing, in eV |
Source code in chmpy/calc/base.py
forces(system)
from_ase(calculator, **kwargs)
staticmethod
Wrap an ASE calculator, which is how most models are published.
The wrapper keeps one ase.Atoms alive and writes the geometry into
it, rather than building a new one per evaluation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
calculator
|
any |
required |
Returns:
| Type | Description |
|---|---|
Calculator
|
a |
Source code in chmpy/calc/base.py
lattice_energy(crystal, **kwargs)
The lattice energy of a molecular crystal with this calculator.
Keyword arguments go to chmpy.opt.lattice_energy.
Source code in chmpy/calc/base.py
relax(structure, **kwargs)
Relax a Crystal, Molecule or System with this calculator.
Keyword arguments go to chmpy.opt.relax.
shifted(reference_energies)
This calculator with a per-element reference energy subtracted.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference_energies
|
dict
|
atomic number (or element symbol) -> eV |
required |
Returns:
| Type | Description |
|---|---|
Calculator
|
a calculator whose energies are measured from those references |
Source code in chmpy/calc/base.py
stress(system)
PropertyNotAvailable
ShiftedCalculator
Bases: Calculator
A calculator with a per-element reference energy removed.
Lattice and formation energies want energies measured from isolated atoms or from some other reference, and the shift is constant for a fixed composition, so it changes no derivative.
Source code in chmpy/calc/base.py
SumCalculator
Bases: Calculator
Several calculators added together, e.g. an MLIP plus a dispersion term.
Built by calc_a + calc_b. Provides the intersection of what its parts
provide, since a property only one of them has cannot be summed.