# OEFF Examples¶

## Simple Functions and Optimization¶

### Solving a simple equation¶

The following example illustrates how to define a simple objective function. By deriving the objective function from OEFunc2, we can find the roots of the simple quadratic equation using OENewtonOpt optimizer. A class derived from OEFunc2 must contain analytical gradients and Hessians. This example expects a number as input for the initial guess to solve the simple function.

### Using checkpoints for optimization monitoring¶

The following example illustrates how to define checkpoints and use then along with optimizers to monitor progress during optimization. For simplicity, a simple quadratic equation is defined as objective function and derived from OEFunc1. The quadratic equation is solved using the OEBFGSOpt optimizer. A class derived from OEFunc1 must contain analytical gradients. This examples expects a number as input for the initial guess to solve the simple function.

## Molecule Functions¶

### User defined molecule function¶

The following example illustrates how to define an objective function within the context of a molecule. Generally speaking, a molecule function (OEMolFunc) defines some sort of interaction involving a part or all of a molecule. For simplicity, a simple energy function is defined that consists sum of square of distance of all the atoms in the molecule. The molecule function is optimized using the OEBFGSOpt optimizer. A class derived from OEMolFunc1 must contain analytical gradients.

## Small Molecule Force fields¶

### Single ligand in vacuum¶

The following example illustrates how to calculate energy and optimize a single ligand in vacuum. The molecule needs to be prepared (`OEForceField.PrepMol`), followed by a call to `OEMolFunc.Setup` to create the interactions. Optimization is carried out using the OEBFGSOpt optimizer.

### Energy components of a single ligand in vacuum¶

The following example illustrates how to obtain various intermolecular energy components of a single ligand in vacuum.

### MMFF interactions of a single ligand¶

The following example illustrates how to obtain various interactions of a single ligand in the context of a force field.

## Protein-Ligand Complexes¶

### Protein-ligand optimization¶

The following example illustrates how to set up a ligand optimization within the context of a protein contained in a design unit.

### Optimizing a single ligand with fixed atoms¶

The following example illustrates how to fix a subset of atoms using the OESubsetAdaptor during a single ligand optimization in vacuum. The adaptor is initialized with the force field interactions of the ligand, and passed as the objective function to be optimized. Methods of the adaptor are used to convert between the Cartesian coordinates of the ligand and the adaptor variables.

### Optimizing a single ligand torsions¶

The following example illustrates how to optimize a set of torsion angles using the OETorAdaptor for a single ligand in vacuum. The adaptor is initialized with the force field interactions of the ligand, and passed as the objective function to be optimized. Methods of the adaptor are used to convert between the Cartesian coordinates of the ligand and the adaptor variables.

### Optimizing rigid ligand in protein¶

The following example illustrates how to perform rigid optimization of a ligand in the context of a protein using the OEQuatAdaptor. The adaptor is initialized with the protein-ligand interactions, and passed as the objective function to be optimized. Methods of the adaptor are used to convert between the Cartesian coordinates of the ligand and the adaptor variables.

## Custom Force Fields¶

### Custom Smirnoff¶

The following example illustrates how to load custom SMIRNOFF force field parameters from an OFFXML file and use that to calculate energy and optimize a single ligand in vacuum.