=============================== Use a trained GP as a potential =============================== This tutorial trains a small Gaussian process, inspects it, and uses the same surface through its position API, ASE, and rgpot. It ends by comparing the GP with the Lennard-Jones oracle along a path. Build training data ------------------- The four configurations use ASE's Lennard-Jones calculator as the true potential. ``observe`` reads both energy and forces. Setting ``max_iter`` to zero keeps this documentation example short; it still conditions the posterior. .. code:: python import numpy as np from ase import Atoms from ase.calculators.lj import LennardJones import gpridge from gpridge.gp import GaussianProcess template = Atoms( "Ar2", positions=[[0.0, 0.0, 0.0], [1.2, 0.0, 0.0]], cell=np.eye(3) * 8.0, pbc=True, ) parameters = gpridge.default_gpr_parameters() parameters["max_iter"] = 0 gp = GaussianProcess(template_atoms=template, gpr_params=parameters) for distance in (1.05, 1.15, 1.3, 1.5): sample = template.copy() sample.positions[1, 0] = distance sample.calc = LennardJones() gp.observe(sample) gp.fit() assert gp.is_fit and gp.n_observations == 4 If a search result supplies a trained ``GaussianProcess``, start with that object instead. The surface API does not import or depend on a result class. Inspect the fit --------------- ``training_set`` uses ASE force signs. Its arrays are copies, so inspection cannot change the model. .. code:: python training = gp.training_set theta = gp.hyperparameters noise = gp.noise_report fit = gp.fit_diagnostics assert training.positions.shape == training.forces.shape == (4, 2, 3) assert training.energies.shape == (4,) assert theta["length_scales"].shape == (1,) assert noise.effective_variance >= noise.fitted_likelihood_variance assert fit.iterations >= 0 GPRidge does not currently serialize a trained posterior. Keep the ``GaussianProcess`` object, or reconstruct it from its training data and fit settings. Evaluate the surface -------------------- Every scalar method accepts an ASE ``Atoms`` object or an ``(N, 3)`` position array. Forces use the ASE convention and point downhill. .. code:: python query = template.copy() query.positions[1, 0] = 1.24 energy = gp.energy(query.positions) forces = gp.forces(query.positions) same_energy, same_forces = gp.energy_and_forces(query) variance = gp.variance(query.positions) assert energy == same_energy np.testing.assert_array_equal(forces, same_forces) assert np.isfinite(variance) ``hessian_action(x, direction)`` applies the posterior-mean Cartesian Hessian. ``predict_hessian(atoms)`` returns the full Hessian when it is needed. Minimise with ASE ----------------- ``as_calculator()`` returns the surface through ASE's calculator interface. The same handoff works with ASE optimizers, molecular dynamics, and NEB. .. code:: python from ase.optimize import FIRE relaxed = query.copy() relaxed.calc = gp.as_calculator() initial_gp_energy = relaxed.get_potential_energy() FIRE(relaxed, logfile=None).run(fmax=0.2, steps=5) assert np.isfinite(relaxed.get_potential_energy()) Use the rgpot interface ----------------------- ``as_rgpot()`` returns a native ``rgpot::PotentialBase`` adapter. Its Python call surface is the rgpot convention: positions, atomic numbers, and the cell produce energy, forces, and energy variance. Native rgpot consumers receive the same ``PotentialBase``. .. code:: python rgpot_surface = gp.as_rgpot() rg_energy, rg_forces, rg_variance = rgpot_surface( np.ascontiguousarray(query.positions, dtype=np.float64), np.ascontiguousarray(query.numbers, dtype=np.int32), np.ascontiguousarray(query.cell.array, dtype=np.float64), ) assert rg_energy == energy np.testing.assert_array_equal(rg_forces, forces) variance_tolerance = np.finfo(np.float64).eps * max( abs(rg_variance), abs(variance), 1.0 ) assert abs(rg_variance - variance) <= variance_tolerance The scalar variance is the energy variance. rgpot has no standard per-component force-variance output. Use ``predict_with_uncertainty`` when that array is needed. Compare the GP and true path ---------------------------- The path below stays within the training interval. Each row contains the distance, GP energy, true energy, and GP energy variance. .. code:: python rows = [] for distance in np.linspace(1.08, 1.45, 6): point = template.copy() point.positions[1, 0] = distance gp_energy = gp.energy(point) gp_variance = gp.variance(point) point.calc = LennardJones() true_energy = point.get_potential_energy() rows.append((distance, gp_energy, true_energy, gp_variance)) comparison = np.asarray(rows) assert comparison.shape == (6, 4) assert np.isfinite(comparison).all()