Skip to content

Geometry Optimization

Geometry optimizations and transition-state searches are configured using the OptimizationInput building block. The initial geometry is specified through the initial_molecule argument, and the underlying energy method through method.

Running the input through sierra.run() produces an OptimizationResult. The OptimizationResult most notably contains the fields converged, final_molecule and energies. These results can easily be utilized for more complex workflows.

Simple geometry optimization

In this example a water molecule geometry is optimized using the GFN1-xTB method:

import sierra
from sierra.inputs import *

water = Molecule(pubchem="water")
method = XTBMethod(model="gfn1")
input = OptimizationInput(initial_molecule=water, method=method)
result = sierra.run(input)

print(f"Converged: {result.converged}")
#> Converged: True

print("Optimized molecule:")
#> Optimized molecule:
print(result.final_molecule.write())
"""
3
0 1
O     0.013698339808   0.010141576417  -0.007165050545
H     0.268966471425   0.893269716496   0.262654095920
H     0.601535188770  -0.248811292903  -0.717989045354

"""

print("Energies at the each optimization step:")
#> Energies at the each optimization step:
for i, e in enumerate(result.energies):
    print(f"  {i+1} {e:15.9f}")
    #>   1    -5.768440813
    #>   2    -5.768678192
    #>   3    -5.768772267
    #>   4    -5.768781558
    #>   5    -5.768783910
    #>   6    -5.768783913

Constrained geometry optimization

Constraints on bond-lengths, angles, and dihedrals can be applied during the geometry optimization. The CoordinateConstraint building block can either:

  • Maintain the original value of the coordinate with freeze=True
  • Drive the coordinate towards a value specified by the value argument
import sierra
from sierra.inputs import *

water = Molecule(pubchem="water")
method = XTBMethod(model="gfn1")
constraints = [
    CoordinateConstraint(indices=[0, 1], value="2.0 bohr"),
    CoordinateConstraint(indices=[0, 1, 2], freeze=True),
]
input = OptimizationInput(
    initial_molecule=water, method=method, constraints=constraints
)
result = sierra.run(input)

final_molecule = result.final_molecule

print("Constrained coordinates:")
#> Constrained coordinates:
for c in constraints:
    indices = c.indices
    value = final_molecule.measure(indices)
    print(f"Coordinate {str(indices):10s}: {value:.4f}")
    #> Coordinate [0, 1]    : 2.0000
    #> Coordinate [0, 1, 2] : 38.0100

value = final_molecule.measure([0, 2])
print(f"Unconstrained OH bond length: {value:.4f} Bohr")
#> Unconstrained OH bond length: 1.8308 Bohr

Geometry optimization with further details

A number of details can be specified, if needed, to alter details of the algorithm:

import sierra
from sierra.inputs import *

water = Molecule(pubchem="water")
method = XTBMethod(model="gfn1")
input = OptimizationInput(
    initial_molecule=water,
    method=method,
    details={"max_iter": 2, "no_fail": True},
)
result = sierra.run(input)

print(f"Converged: {result.converged}")
#> Converged: False

OptimizationInput

Configuration for performing an optimization.

Fields

active

Indices of atoms to optimize. Using None or [] indicates optimizing all atoms.

  • Type: Optional[List[int]]
constraints

The coordinate constraints to place on the optimization.

initial_molecule

The initial molecule at the start of the optimization.

method

The computational method to evaluate the optimization under.

transition_search

If 'True' run a transition state search rather than a energy minimization.

  • Type: bool
  • Default: False
details

Additional details to supply to the optimizer.

Detail Fields

result_contract
  • Type: EngineResultContractBase
  • Default: EngineResultContractBase()
store_extras

This field determines the amount of output that is kept in the 'extras' field in addition to the requirements of the contract. 'True' will lead to storing all available information and 'False' will lead to storing no information beyond the requirements of the contract. Alternatively, a list of field names can be provided that shall be stored.

  • Type: One of: [bool, List[str]]
  • Default: False

OptimizationResult

The results of an optimization calculation.

Fields

All the fields in OptimizationInput and the following:

converged

If the optimization converged or not

  • Type: bool
  • Default: False
energies

A list of energies at each step of the geometry optimization

  • Type: Array
extras

Additional key/value pairs generated during the optimization

  • Type: Mapping[str, Any]
  • Default: {}
final_molecule

The optimized molecule or the result of the transition state search

trajectory

A list of results for each step in the geometry optimization

Functions

OptimizationResult.visualize

visualize((str) units) -> None:

For a finished geometry optimization, the trajectory can be displayed by using this function. CAVEAT: This function can only be used inside a jupyter notebook.

Arguments

units

The units in which energy values are displayed.

  • Type: str
  • Default: "kcal / mol"

Returns

None