Skip to content

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 override compute_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 + b sums two calculators, and calc.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 compute produces without help. Anything else that is asked for is filled in by finite differences.

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
class Calculator(ABC):
    """Base class for anything that computes energies of a `System`.

    Attributes:
        provides: the properties `compute` produces without help. Anything else
            that is asked for is filled in by finite differences.
        cache_size: how many geometries to remember results for.
        stats: call counts and wall time.
    """

    #: what `compute` returns natively; everything else is finite-differenced
    provides: frozenset = frozenset({ENERGY})
    #: how many geometries to remember results for
    cache_size: int = 8
    #: displacement for finite differences, Angstroms
    fd_step: float = DEFAULT_FD_STEP
    #: relative precision of the energies this calculator returns. None means
    #: "work it out from the first energy"; see `energy_noise`.
    energy_precision: float | None = None

    def __init_subclass__(cls, **kwargs):
        """Let a subclass write `provides = {"energy", "forces"}` and mean it."""
        super().__init_subclass__(**kwargs)
        if "provides" in cls.__dict__:
            cls.provides = frozenset(cls.provides)

    def __init__(self, cache_size: int | None = None, fd_step: float | None = None):
        # Calling this is optional: everything it sets has a working default, so
        # a subclass that forgets `super().__init__()` still gets caching and
        # statistics rather than an AttributeError three layers down.
        if cache_size is not None:
            self.cache_size = cache_size
        if fd_step is not None:
            self.fd_step = fd_step

    @property
    def stats(self) -> CalculatorStats:
        "Evaluation counts and wall time"
        stats = self.__dict__.get("_stats")
        if stats is None:
            stats = self.__dict__["_stats"] = CalculatorStats()
        return stats

    @property
    def _results(self) -> OrderedDict:
        cache = self.__dict__.get("_result_cache")
        if cache is None:
            cache = self.__dict__["_result_cache"] = OrderedDict()
        return cache

    # -- the one thing a subclass must write ---------------------------------

    @abstractmethod
    def compute(self, system: System, want: frozenset) -> Result:
        """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.

        Args:
            system: the geometry to evaluate
            want: property names, a subset of `self.provides`

        Returns:
            Result holding at least the requested properties
        """

    def compute_batch(self, systems: list[System], want: frozenset) -> list[Result]:
        """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.
        """
        return [self.compute(system, want) for system in systems]

    # -- what callers use ----------------------------------------------------

    def __call__(self, system: System, want=(ENERGY,)) -> Result:
        """Evaluate `system`, returning at least the properties in `want`.

        Args:
            system: the geometry to evaluate
            want: a property name or an iterable of them

        Returns:
            Result, from cache when the geometry has not changed
        """
        want = normalise_properties(want)
        _check_stress_defined(want, system)
        cached = self._lookup(system, want)
        if cached is not None:
            self.stats.cache_hits += 1
            return cached

        native = want & frozenset(self.provides)
        started = time.perf_counter()
        result = self.compute(system, native)
        self.stats.record(native, time.perf_counter() - started)
        result = self._validate(result, system, native)

        missing = want - self._available(result)
        if missing:
            result = self._finite_difference(system, result, missing)

        self._store(system, result)
        return result

    #: `calc.evaluate(system, ...)` reads better in a pipeline than `calc(...)`
    evaluate = __call__

    def batch(self, systems, want=(ENERGY,)) -> list[Result]:
        """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.
        """
        want = normalise_properties(want)
        systems = list(systems)
        for system in systems:
            _check_stress_defined(want, system)
        results: list[Result | None] = [None] * len(systems)

        todo = []
        for index, system in enumerate(systems):
            cached = self._lookup(system, want)
            if cached is not None:
                self.stats.cache_hits += 1
                results[index] = cached
            else:
                todo.append(index)

        if todo:
            native = want & frozenset(self.provides)
            started = time.perf_counter()
            computed = self.compute_batch([systems[i] for i in todo], native)
            elapsed = time.perf_counter() - started
            self.stats.batched_calls += 1
            self.stats.seconds += elapsed
            self.stats.calls += len(todo)
            for name in native:
                self.stats.per_property[name] = self.stats.per_property.get(
                    name, 0
                ) + len(todo)
            if len(computed) != len(todo):
                raise PropertyNotAvailable(
                    f"{type(self).__name__}.compute_batch returned "
                    f"{len(computed)} results for {len(todo)} systems"
                )
            for index, result in zip(todo, computed, strict=True):
                system = systems[index]
                result = self._validate(result, system, native)
                missing = want - self._available(result)
                if missing:
                    result = self._finite_difference(system, result, missing)
                self._store(system, result)
                results[index] = result

        return results

    def energy(self, system: System) -> float:
        "Total energy in eV"
        return self(system, (ENERGY,)).energy

    def energies(self, system: System) -> np.ndarray:
        "(N,) per-atom energies in eV"
        return self(system, (ENERGIES,)).energies

    def forces(self, system: System) -> np.ndarray:
        "(N, 3) forces in eV/A"
        return self(system, (FORCES,)).forces

    def stress(self, system: System) -> np.ndarray:
        "(3, 3) stress in eV/A^3"
        return self(system, (STRESS,)).stress

    def virial(self, system: System) -> np.ndarray:
        "(3, 3) `-dE/deps` in eV"
        return self(system, (STRESS,)).virial

    def energy_noise(self, energy: float) -> float:
        """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.

        Args:
            energy: the energy in question, eV

        Returns:
            the smallest energy difference worth believing, in eV
        """
        precision = self.energy_precision
        if precision is None:
            precision = self.__dict__.get("_detected_precision")
        if precision is None:
            return float(np.spacing(abs(energy)))
        return float(precision * max(abs(energy), 1.0))

    #: distinct energies that must all be float32-representable before the
    #: precision is inferred to be float32
    precision_samples: int = 3

    def _detect_precision(self, energy: float) -> None:
        """Infer the precision of the energies, once there is enough evidence."""
        if self.energy_precision is not None:
            return
        if "_detected_precision" in self.__dict__:
            return
        if energy == 0.0 or not np.isfinite(energy):
            return
        if float(np.float32(energy)) != energy:
            # one energy that float32 cannot hold settles it
            self.__dict__["_detected_precision"] = float(np.finfo(np.float64).eps)
            return
        seen = self.__dict__.setdefault("_precision_samples", set())
        seen.add(energy)
        if len(seen) >= self.precision_samples:
            self.__dict__["_detected_precision"] = float(np.finfo(np.float32).eps)

    # -- composition ---------------------------------------------------------

    def __add__(self, other: Calculator) -> Calculator:
        if not isinstance(other, Calculator):
            return NotImplemented
        return SumCalculator([self, other])

    def shifted(self, reference_energies: dict) -> Calculator:
        """This calculator with a per-element reference energy subtracted.

        Args:
            reference_energies: atomic number (or element symbol) -> eV

        Returns:
            a calculator whose energies are measured from those references
        """
        return ShiftedCalculator(self, reference_energies)

    # -- checking ------------------------------------------------------------

    def check_gradients(
        self, system: System, step: float | None = None, tolerance: float = 1e-5
    ) -> GradientCheck:
        """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.

        Args:
            system: geometry to check at, ideally one away from a stationary
                point and with no exact symmetry hiding a sign error
            step: displacement in Angstroms and strain increment; defaults to
                `self.fd_step`
            tolerance: relative to the largest analytic component

        Returns:
            GradientCheck, whose `ok` is the verdict
        """
        step = self.fd_step if step is None else step
        analytic = self(system, frozenset(self.provides))

        forces_error = forces_scale = forces_worst = None
        if FORCES in self.provides:
            numeric = self._numeric_forces(system, step)
            difference = np.abs(analytic.forces - numeric)
            forces_error = float(difference.max())
            forces_scale = float(np.abs(analytic.forces).max())
            forces_worst = tuple(
                int(i) for i in np.unravel_index(difference.argmax(), difference.shape)
            )

        stress_error = stress_scale = stress_worst = None
        if STRESS in self.provides and system.periodic:
            numeric = self._numeric_stress(system, step)
            difference = np.abs(analytic.stress - numeric)
            stress_error = float(difference.max())
            stress_scale = float(np.abs(analytic.stress).max())
            stress_worst = tuple(
                int(i) for i in np.unravel_index(difference.argmax(), difference.shape)
            )

        return GradientCheck(
            forces_error,
            forces_scale,
            forces_worst,
            stress_error,
            stress_scale,
            stress_worst,
            tolerance,
            step,
        )

    # -- interoperability ----------------------------------------------------

    @staticmethod
    def from_ase(calculator, **kwargs) -> Calculator:
        """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.

        Args:
            calculator: any `ase.calculators.calculator.Calculator`

        Returns:
            a `Calculator` wrapping it
        """
        from .adapters.ase import AseCalculator

        return AseCalculator(calculator, **kwargs)

    def as_ase(self):
        """Expose this calculator as an `ase.calculators.calculator.Calculator`,
        for workflows that expect one.
        """
        from .adapters.ase import as_ase_calculator

        return as_ase_calculator(self)

    # -- relaxation ----------------------------------------------------------

    def relax(self, structure, **kwargs):
        """Relax a `Crystal`, `Molecule` or `System` with this calculator.

        Keyword arguments go to `chmpy.opt.relax`.
        """
        from chmpy.opt import relax

        return relax(structure, self, **kwargs)

    def lattice_energy(self, crystal, **kwargs):
        """The lattice energy of a molecular crystal with this calculator.

        Keyword arguments go to `chmpy.opt.lattice_energy`.
        """
        from chmpy.opt import lattice_energy

        return lattice_energy(crystal, self, **kwargs)

    def elastic_tensor(self, structure, **kwargs):
        """The elastic tensor of an already-relaxed structure.

        Keyword arguments go to `chmpy.opt.elastic_tensor`.
        """
        from chmpy.opt import elastic_tensor

        return elastic_tensor(structure, self, **kwargs)

    # -- internals -----------------------------------------------------------

    def __repr__(self) -> str:
        return f"<{type(self).__name__} provides={sorted(self.provides)}>"

    def clear_cache(self) -> None:
        "Forget every remembered result"
        self._results.clear()

    def _lookup(self, system, want):
        entry = self._results.get(id(system))
        if entry is None or entry.system is not system:
            return None
        if entry.version != system.version or not want <= entry.available:
            return None
        self._results.move_to_end(id(system))
        return entry.result

    def _store(self, system, result):
        cache = self._results
        # the entry holds the system, so its id cannot be reused while cached
        cache[id(system)] = _CacheEntry(system, result, self._available(result))
        cache.move_to_end(id(system))
        while len(cache) > self.cache_size:
            cache.popitem(last=False)

    @staticmethod
    def _available(result) -> frozenset:
        return frozenset(name for name in ALL_PROPERTIES if result.has(name))

    def _validate(self, result, system, want) -> Result:
        """Check shapes and finiteness, and attach the volume the stress needs."""
        who = type(self).__name__
        if not isinstance(result, Result):
            raise TypeError(f"{who}.compute must return a Result, got {type(result)}")
        if not np.isfinite(result.energy):
            raise PropertyNotAvailable(f"{who} returned a non-finite energy")
        self._detect_precision(result.energy)

        n = len(system)
        for name, shape in ((FORCES, (n, 3)), (STRESS, (3, 3)), (ENERGIES, (n,))):
            value = getattr(result, name)
            if value is None:
                if name in want:
                    raise PropertyNotAvailable(
                        f"{who} declares {name!r} in `provides` but returned None"
                    )
                continue
            if value.shape != shape:
                raise ValueError(
                    f"{who} returned {name} with shape {value.shape}, expected {shape}"
                )
            if not np.all(np.isfinite(value)):
                raise PropertyNotAvailable(f"{who} returned non-finite {name}")

        undeclared = self._available(result) - frozenset(self.provides)
        if undeclared and not self.__dict__.get("_warned_undeclared"):
            self.__dict__["_warned_undeclared"] = True
            LOG.warning(
                "%s returned %s but does not list %s in `provides`; they will be "
                "used, but declaring them avoids a needless finite-difference "
                "fallback when they are requested on their own",
                who,
                sorted(undeclared),
                "them" if len(undeclared) > 1 else "it",
            )

        if result.stress is not None and result.volume == 0.0:
            result = _replace_volume(result, system.volume)
        return result

    def _finite_difference(self, system, result, missing) -> Result:
        """Fill in derivatives the calculator does not provide."""
        forces = result.forces
        stress = result.stress
        if FORCES in missing:
            forces = self._numeric_forces(system, self.fd_step)
        if STRESS in missing:
            if not system.periodic:
                raise PropertyNotAvailable(
                    "stress is only defined for a periodic system"
                )
            stress = self._numeric_stress(system, self.fd_step)
        if ENERGIES in missing:
            raise PropertyNotAvailable(
                f"{type(self).__name__} does not provide per-atom energies, and "
                "they cannot be finite-differenced"
            )
        return Result(
            energy=result.energy,
            forces=forces,
            stress=stress,
            energies=result.energies,
            volume=system.volume,
            extra=result.extra,
        )

    def _energy_at(self, scratch, positions, cell=None) -> float:
        """Energy of a scratch system at a displaced geometry."""
        if cell is not None:
            scratch.set_cell(cell)
        scratch.set_positions(positions)
        self.stats.finite_difference_calls += 1
        return float(self.compute(scratch, frozenset({ENERGY})).energy)

    def _numeric_forces(self, system, step) -> np.ndarray:
        """Central-difference forces: 6N energy evaluations."""
        scratch = system.copy()
        positions = np.array(system.positions)
        forces = np.zeros_like(positions)
        for i in range(len(system)):
            for d in range(3):
                shifted = positions.copy()
                shifted[i, d] += step
                plus = self._energy_at(scratch, shifted)
                shifted[i, d] -= 2 * step
                minus = self._energy_at(scratch, shifted)
                forces[i, d] = -(plus - minus) / (2 * step)
        return forces

    def _numeric_stress(self, system, step) -> np.ndarray:
        """Central-difference stress: 12 energy evaluations.

        Deforms by `x = I + eps` applied as `cell @ x` with the atoms carried
        along in fractional coordinates, which is the strain the stress is the
        derivative with respect to. The shear components put `step / 2` in both
        off-diagonal entries so that `eps` stays symmetric and the derivative is
        with respect to the tensor component, not the engineering shear.
        """
        scratch = system.copy()
        cell = np.array(system.cell)
        scaled = system.scaled_positions
        volume = system.volume
        stress = np.zeros((3, 3))

        def energy_for(deformation):
            deformed = cell @ deformation
            return self._energy_at(scratch, scaled @ deformed, deformed)

        for i in range(3):
            x = np.eye(3)
            x[i, i] += step
            plus = energy_for(x)
            x[i, i] -= 2 * step
            minus = energy_for(x)
            stress[i, i] = (plus - minus) / (2 * step * volume)

        for i, j in ((0, 1), (0, 2), (1, 2)):
            x = np.eye(3)
            x[i, j] = x[j, i] = 0.5 * step
            plus = energy_for(x)
            x[i, j] = x[j, i] = -0.5 * step
            minus = energy_for(x)
            stress[i, j] = stress[j, i] = (plus - minus) / (2 * step * volume)

        return stress

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
def __call__(self, system: System, want=(ENERGY,)) -> Result:
    """Evaluate `system`, returning at least the properties in `want`.

    Args:
        system: the geometry to evaluate
        want: a property name or an iterable of them

    Returns:
        Result, from cache when the geometry has not changed
    """
    want = normalise_properties(want)
    _check_stress_defined(want, system)
    cached = self._lookup(system, want)
    if cached is not None:
        self.stats.cache_hits += 1
        return cached

    native = want & frozenset(self.provides)
    started = time.perf_counter()
    result = self.compute(system, native)
    self.stats.record(native, time.perf_counter() - started)
    result = self._validate(result, system, native)

    missing = want - self._available(result)
    if missing:
        result = self._finite_difference(system, result, missing)

    self._store(system, result)
    return result

__init_subclass__(**kwargs)

Let a subclass write provides = {"energy", "forces"} and mean it.

Source code in chmpy/calc/base.py
def __init_subclass__(cls, **kwargs):
    """Let a subclass write `provides = {"energy", "forces"}` and mean it."""
    super().__init_subclass__(**kwargs)
    if "provides" in cls.__dict__:
        cls.provides = frozenset(cls.provides)

as_ase()

Expose this calculator as an ase.calculators.calculator.Calculator, for workflows that expect one.

Source code in chmpy/calc/base.py
def as_ase(self):
    """Expose this calculator as an `ase.calculators.calculator.Calculator`,
    for workflows that expect one.
    """
    from .adapters.ase import as_ase_calculator

    return as_ase_calculator(self)

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
def batch(self, systems, want=(ENERGY,)) -> list[Result]:
    """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.
    """
    want = normalise_properties(want)
    systems = list(systems)
    for system in systems:
        _check_stress_defined(want, system)
    results: list[Result | None] = [None] * len(systems)

    todo = []
    for index, system in enumerate(systems):
        cached = self._lookup(system, want)
        if cached is not None:
            self.stats.cache_hits += 1
            results[index] = cached
        else:
            todo.append(index)

    if todo:
        native = want & frozenset(self.provides)
        started = time.perf_counter()
        computed = self.compute_batch([systems[i] for i in todo], native)
        elapsed = time.perf_counter() - started
        self.stats.batched_calls += 1
        self.stats.seconds += elapsed
        self.stats.calls += len(todo)
        for name in native:
            self.stats.per_property[name] = self.stats.per_property.get(
                name, 0
            ) + len(todo)
        if len(computed) != len(todo):
            raise PropertyNotAvailable(
                f"{type(self).__name__}.compute_batch returned "
                f"{len(computed)} results for {len(todo)} systems"
            )
        for index, result in zip(todo, computed, strict=True):
            system = systems[index]
            result = self._validate(result, system, native)
            missing = want - self._available(result)
            if missing:
                result = self._finite_difference(system, result, missing)
            self._store(system, result)
            results[index] = result

    return results

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 self.fd_step

None
tolerance float

relative to the largest analytic component

1e-05

Returns:

Type Description
GradientCheck

GradientCheck, whose ok is the verdict

Source code in chmpy/calc/base.py
def check_gradients(
    self, system: System, step: float | None = None, tolerance: float = 1e-5
) -> GradientCheck:
    """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.

    Args:
        system: geometry to check at, ideally one away from a stationary
            point and with no exact symmetry hiding a sign error
        step: displacement in Angstroms and strain increment; defaults to
            `self.fd_step`
        tolerance: relative to the largest analytic component

    Returns:
        GradientCheck, whose `ok` is the verdict
    """
    step = self.fd_step if step is None else step
    analytic = self(system, frozenset(self.provides))

    forces_error = forces_scale = forces_worst = None
    if FORCES in self.provides:
        numeric = self._numeric_forces(system, step)
        difference = np.abs(analytic.forces - numeric)
        forces_error = float(difference.max())
        forces_scale = float(np.abs(analytic.forces).max())
        forces_worst = tuple(
            int(i) for i in np.unravel_index(difference.argmax(), difference.shape)
        )

    stress_error = stress_scale = stress_worst = None
    if STRESS in self.provides and system.periodic:
        numeric = self._numeric_stress(system, step)
        difference = np.abs(analytic.stress - numeric)
        stress_error = float(difference.max())
        stress_scale = float(np.abs(analytic.stress).max())
        stress_worst = tuple(
            int(i) for i in np.unravel_index(difference.argmax(), difference.shape)
        )

    return GradientCheck(
        forces_error,
        forces_scale,
        forces_worst,
        stress_error,
        stress_scale,
        stress_worst,
        tolerance,
        step,
    )

clear_cache()

Forget every remembered result

Source code in chmpy/calc/base.py
def clear_cache(self) -> None:
    "Forget every remembered result"
    self._results.clear()

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 self.provides

required

Returns:

Type Description
Result

Result holding at least the requested properties

Source code in chmpy/calc/base.py
@abstractmethod
def compute(self, system: System, want: frozenset) -> Result:
    """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.

    Args:
        system: the geometry to evaluate
        want: property names, a subset of `self.provides`

    Returns:
        Result holding at least the requested properties
    """

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
def compute_batch(self, systems: list[System], want: frozenset) -> list[Result]:
    """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.
    """
    return [self.compute(system, want) for system in systems]

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
def elastic_tensor(self, structure, **kwargs):
    """The elastic tensor of an already-relaxed structure.

    Keyword arguments go to `chmpy.opt.elastic_tensor`.
    """
    from chmpy.opt import elastic_tensor

    return elastic_tensor(structure, self, **kwargs)

energies(system)

(N,) per-atom energies in eV

Source code in chmpy/calc/base.py
def energies(self, system: System) -> np.ndarray:
    "(N,) per-atom energies in eV"
    return self(system, (ENERGIES,)).energies

energy(system)

Total energy in eV

Source code in chmpy/calc/base.py
def energy(self, system: System) -> float:
    "Total energy in eV"
    return self(system, (ENERGY,)).energy

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
def energy_noise(self, energy: float) -> float:
    """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.

    Args:
        energy: the energy in question, eV

    Returns:
        the smallest energy difference worth believing, in eV
    """
    precision = self.energy_precision
    if precision is None:
        precision = self.__dict__.get("_detected_precision")
    if precision is None:
        return float(np.spacing(abs(energy)))
    return float(precision * max(abs(energy), 1.0))

forces(system)

(N, 3) forces in eV/A

Source code in chmpy/calc/base.py
def forces(self, system: System) -> np.ndarray:
    "(N, 3) forces in eV/A"
    return self(system, (FORCES,)).forces

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 ase.calculators.calculator.Calculator

required

Returns:

Type Description
Calculator

a Calculator wrapping it

Source code in chmpy/calc/base.py
@staticmethod
def from_ase(calculator, **kwargs) -> Calculator:
    """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.

    Args:
        calculator: any `ase.calculators.calculator.Calculator`

    Returns:
        a `Calculator` wrapping it
    """
    from .adapters.ase import AseCalculator

    return AseCalculator(calculator, **kwargs)

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
def lattice_energy(self, crystal, **kwargs):
    """The lattice energy of a molecular crystal with this calculator.

    Keyword arguments go to `chmpy.opt.lattice_energy`.
    """
    from chmpy.opt import lattice_energy

    return lattice_energy(crystal, self, **kwargs)

relax(structure, **kwargs)

Relax a Crystal, Molecule or System with this calculator.

Keyword arguments go to chmpy.opt.relax.

Source code in chmpy/calc/base.py
def relax(self, structure, **kwargs):
    """Relax a `Crystal`, `Molecule` or `System` with this calculator.

    Keyword arguments go to `chmpy.opt.relax`.
    """
    from chmpy.opt import relax

    return relax(structure, self, **kwargs)

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
def shifted(self, reference_energies: dict) -> Calculator:
    """This calculator with a per-element reference energy subtracted.

    Args:
        reference_energies: atomic number (or element symbol) -> eV

    Returns:
        a calculator whose energies are measured from those references
    """
    return ShiftedCalculator(self, reference_energies)

stress(system)

(3, 3) stress in eV/A^3

Source code in chmpy/calc/base.py
def stress(self, system: System) -> np.ndarray:
    "(3, 3) stress in eV/A^3"
    return self(system, (STRESS,)).stress

virial(system)

(3, 3) -dE/deps in eV

Source code in chmpy/calc/base.py
def virial(self, system: System) -> np.ndarray:
    "(3, 3) `-dE/deps` in eV"
    return self(system, (STRESS,)).virial

PropertyNotAvailable

Bases: RuntimeError

A calculator was asked for something it cannot produce.

Source code in chmpy/calc/base.py
class PropertyNotAvailable(RuntimeError):
    """A calculator was asked for something it cannot produce."""

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
class ShiftedCalculator(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.
    """

    def __init__(self, calculator, reference_energies, **kwargs):
        from chmpy.core import Element

        self.calculator = calculator
        self.references = {}
        for key, value in reference_energies.items():
            number = key if isinstance(key, int) else Element[key].atomic_number
            self.references[int(number)] = float(value)
        self.provides = frozenset(calculator.provides)
        super().__init__(**kwargs)

    def _shift(self, system) -> float:
        return sum(self.references.get(int(z), 0.0) for z in system.numbers)

    def compute(self, system, want):
        result = self.calculator(system, want)
        shift = self._shift(system)
        energies = result.energies
        if energies is not None:
            energies = energies - np.array(
                [self.references.get(int(z), 0.0) for z in system.numbers]
            )
        return Result(
            energy=result.energy - shift,
            forces=result.forces,
            stress=result.stress,
            energies=energies,
            volume=result.volume,
            extra=result.extra,
        )

    def __repr__(self) -> str:
        return f"<Shifted {self.calculator!r}>"

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.

Source code in chmpy/calc/base.py
class SumCalculator(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.
    """

    def __init__(self, calculators, **kwargs):
        flattened = []
        for calculator in calculators:
            if isinstance(calculator, SumCalculator):
                flattened.extend(calculator.calculators)
            else:
                flattened.append(calculator)
        self.calculators = flattened
        self.provides = frozenset.intersection(
            *(frozenset(c.provides) for c in flattened)
        )
        super().__init__(**kwargs)

    def compute(self, system, want):
        results = [calculator(system, want) for calculator in self.calculators]
        energy = sum(r.energy for r in results)
        forces = stress = energies = None
        if FORCES in want:
            forces = sum(r.forces for r in results)
        if STRESS in want:
            stress = sum(r.stress for r in results)
        if ENERGIES in want:
            energies = sum(r.energies for r in results)
        return Result(energy, forces, stress, energies, volume=system.volume)

    def __repr__(self) -> str:
        return " + ".join(type(c).__name__ for c in self.calculators)