API

This covers all supported public API.

class berny.Berny(geom, debug=False, restart=None, maxsteps=100, logger=None, **params)[source]

Generator that receives energy and gradients and yields the next geometry.

Parameters
  • geom (Geometry) – geometry to start with

  • debug (bool) – if True, the generator yields debug info on receiving the energy and gradients, otherwise it yields None

  • restart (dict) – start from a state saved from previous run using debug=True

  • maxsteps (int) – abort after maximum number of steps

  • logger (logging.Logger) – alternative logger to use

  • params – parameters that override the defaults

The Berny object is to be used as follows:

optimizer = Berny(geom)
for geom in optimizer:
    # calculate energy and gradients (as N-by-3 matrix)
    debug = optimizer.send((energy, gradients))
property converged

Whether the optimized has converged.

property trust

Current trust radius.

berny.berny.defaults = {'dihedral': True, 'gradientmax': 0.00045, 'gradientrms': 0.00015, 'stepmax': 0.0018, 'steprms': 0.0012, 'superweakdih': False, 'trust': 0.3}
gradientmax, gradientrms, stepmax, steprms

Convergence criteria in atomic units (“step” refers to the step in internal coordinates, assuming radian units for angles).

trust

Initial trust radius in atomic units. It is the maximum RMS of the quadratic step (see below).

dihedral

Form dihedral angles.

superweakdih

Form dihedral angles containing two or more noncovalent bonds.

berny.coords.angstrom = 1.8897261245650618

Can be imported directly as berny.angstrom.

berny.optimize(optimizer, solver, trajectory=None)[source]

Optimize a geometry with respect to a solver.

Parameters
  • optimizer (Generator) – Optimizer object with the same generator interface as Berny

  • solver (Generator) –

    unprimed generator that receives geometry as a 2-tuple of a list of 2-tuples of the atom symbol and coordinate (as a 3-tuple), and of a list of lattice vectors (or None if molecule), and yields the energy and gradients (as a \(N\)-by-3 matrix or \((N+3)\)-by-3 matrix in case of a crystal geometry).

    See MopacSolver for an example.

  • trajectory (str) – filename for the XYZ trajectory

Returns

The optimized geometry.

The function is equivalent to:

next(solver)
for geom in optimizer:
    energy, gradients = solver.send((list(geom), geom.lattice))
    optimizer.send((energy, gradients))

Geometry operations

class berny.Geometry(species, coords, lattice=None)[source]

Represents a single molecule or a crystal.

Parameters
  • species (list) – list of element symbols

  • coords (list) – list of atomic coordinates in angstroms (as 3-tuples)

  • lattice (list) – list of lattice vectors (None for a moleucle)

Iterating over a geometry yields 2-tuples of symbols and coordinates. len() returns the number of atoms in a geometry. The class supports format() with the same available formats as dump().

bondmatrix(scale=1.3)[source]

Calculate the covalent connectedness matrix.

Parameters

scale (float) – threshold for accepting a distance as a covalent bond

Returns

\(b_{ij}:=R_{ij}<\text{scale}\times (R_i^\text{cov}+R_j^\text{cov})\).

property cms

Calculate the center of mass, \(\mathbf R_\text{CMS}\).

copy()[source]

Make a copy of the geometry.

dist(other=None)[source]

Alias for the first element of dist_diff().

dist_diff(other=None)[source]

Calculate distances and vectors between atoms.

Parameters

other (Geometry) – calculate distances between two geometries if given or within a geometry if not

Returns

\(R_{ij}:=|\mathbf R_i-\mathbf R_j|\) and \(R_{ij\alpha}:=(\mathbf R_i)_\alpha-(\mathbf R_j)_\alpha\).

dump(f, fmt)[source]

Save the geometry into a file.

Parameters
  • f (file) – file object

  • fmt (str) – geometry format, one of "", "xyz", "aims", "mopac".

dumps(fmt)

Return the geometry represented as a string, delegates to dump().

property formula

Chemical formula of the molecule or a unit cell.

classmethod from_atoms(atoms, lattice=None, unit=1.0)[source]

Alternative contructor.

Parameters
  • atoms (list) – list of 2-tuples with an elemnt symbol and a coordinate

  • unit (float) – value to multiple atomic coordiantes with

  • lattice (list) – list of lattice vectors (None for a moleucle)

property inertia

Calculate the moment of inertia.

\[I_{\alpha\beta}:= \sum_im_i\big(r_i^2\delta_{\alpha\beta}-(\mathbf r_i)_\alpha(\mathbf r_i)_\beta\big),\qquad \mathbf r_i=\mathbf R_i-\mathbf R_\text{CMS} \]
property masses

Numpy array of atomic masses.

rho()[source]

Calculate a measure of covalentness.

Returns

\(\rho_{ij}:=\exp\big(-R_{ij}/(R_i^\text{cov}+R_j^\text{cov})\big)\).

super_circum(radius)[source]

Supercell dimensions such that the supercell circumsribes a sphere.

Parameters

radius (float) – circumscribed radius in angstroms

Returns None when geometry is not a crystal.

supercell(ranges=((-1, 1), (-1, 1), (-1, 1)), cutoff=None)[source]

Create a crystal supercell.

Parameters
  • ranges (list) – list of 2-tuples specifying the range of multiples of the unit-cell vectors

  • cutoff (float) – if given, the ranges are determined such that the supercell contains a sphere with the radius qual to the cutoff

Returns a copy of itself when geometry is not a crystal.

write(filename)[source]

Write the geometry into a file, delegates to dump().

Parameters

filename (str) – path that will be overwritten

berny.geomlib.load(fp, fmt)[source]

Read a geometry from a file object.

Parameters
  • fp (file) – file object

  • fmt (str) – the format of the geometry file, can be one of "xyz", "aims"

Returns Geometry.

berny.geomlib.loads(s, fmt)[source]

Read a geometry from a string, delegates to load().

Parameters

s (str) – string with geometry

berny.geomlib.readfile(path, fmt=None)[source]

Read a geometry from a file path, delegates to load().

Parameters
  • path (str) – path to a geometry file

  • fmt (str) – if not given, the format is given from the file extension

Solvers

All functions in this module are coroutines that satisfy the solver interface expected by optimize().

berny.solvers.MopacSolver(cmd='mopac', method='PM7', workdir=None)[source]

Crate a solver that wraps MOPAC.

Mopac needs to be installed on the system.

Parameters
  • cmd (str) – MOPAC executable

  • method (str) – model to calculate energy