#include <openbabel/forcefield.h>
Public Member Functions | |
virtual | ~OBForceField () |
virtual std::string | GetUnit () |
virtual bool | Setup (OBMol &mol) |
bool | UpdateCoordinates (OBMol &mol) |
bool | UpdateConformers (OBMol &mol) |
void | OBFFLog (std::string msg) |
void | OBFFLog (const char *msg) |
void | kludge () |
Methods for energy evaluation | |
virtual double | Energy (bool gradients=true) |
virtual double | E_Bond (bool gradients=true) |
virtual double | E_Angle (bool gradients=true) |
virtual double | E_StrBnd (bool gradients=true) |
virtual double | E_Torsion (bool gradients=true) |
virtual double | E_OOP (bool gradients=true) |
virtual double | E_VDW (bool gradients=true) |
virtual double | E_Electrostatic (bool gradients=true) |
Methods for logging | |
bool | SetLogFile (std::ostream *pos) |
bool | SetLogLevel (int level) |
int | GetLogLevel () |
Methods for structure generation | |
void | SystematicRotorSearch () |
Methods for energy minimization | |
vector3 | LineSearch (OBAtom *atom, vector3 &direction) |
void | SteepestDescent (int steps, double econv=1e-6f, int method=OBFF_ANALYTICAL_GRADIENT) |
void | SteepestDescentInitialize (int steps=1000, double econv=1e-6f, int method=OBFF_ANALYTICAL_GRADIENT) |
bool | SteepestDescentTakeNSteps (int n) |
void | ConjugateGradients (int steps, double econv=1e-6f, int method=OBFF_ANALYTICAL_GRADIENT) |
void | ConjugateGradientsInitialize (int steps=1000, double econv=1e-6f, int method=OBFF_ANALYTICAL_GRADIENT) |
bool | ConjugateGradientsTakeNSteps (int n) |
Methods for forcefield validation | |
virtual bool | Validate () |
virtual bool | ValidateGradients () |
vector3 | ValidateGradientError (vector3 &numgrad, vector3 &anagrad) |
Static Public Member Functions | |
static OBForceField * | FindForceField (const std::string &ID) |
static OBForceField * | FindForceField (const char *ID) |
Methods for vector analysis (used by OBFFXXXXCalculationYYYY) | |
static double | VectorLengthDerivative (vector3 &a, vector3 &b) |
static double | VectorAngleDerivative (vector3 &a, vector3 &b, vector3 &c) |
static double | VectorTorsionDerivative (vector3 &a, vector3 &b, vector3 &c, vector3 &d) |
Protected Member Functions | |
OBFFParameter * | GetParameter (int a, int b, int c, int d, std::vector< OBFFParameter > ¶meter) |
OBFFParameter * | GetParameter (const char *a, const char *b, const char *c, const char *d, std::vector< OBFFParameter > ¶meter) |
vector3 | NumericalDerivative (OBAtom *a, int terms=OBFF_ENERGY) |
vector3 | NumericalSecondDerivative (OBAtom *a, int terms=OBFF_ENERGY) |
virtual vector3 | GetGradient (OBAtom *a, int terms=OBFF_ENERGY) |
bool | IsInSameRing (OBAtom *a, OBAtom *b) |
Protected Attributes | |
OBMol | _mol |
std::ostream * | logos |
char | logbuf [200] |
int | loglvl |
int | current_conformer |
double | _econv |
double | _e_n1 |
int | _method |
int | _cstep |
int | _nsteps |
std::vector< vector3 > | _grad1 |
std::vector< vector3 > | _dir1 |
The OBForceField class is the base class for molecular mechanics in Open Babel. Classes derived from the OBForceField implement specific force fields (Ghemical, MMFF94, ...). Other classes such as OBFFParameter, OBFFCalculation and its derived classes are only for internal use. As a user interested in using the available force fields in Open Babel, you don't need these classes. The rest of this short introduction is aimed at these users. For information on how to implement additional force fields, see the wiki pages or post your questions to the openbabel-devel mailing list.
Before we can start using a force field, we must first select it and set it up. This is illustrated in the first example below. The Setup procedure assigns atom types, charges and parameters. There are several reasons why this may fail, a log message will be written to the logfile before Setup() returns false.
The force field classes use their own logging functions. You can set the logfile using SetLogFile() and set the log level using SetLogLevel(). If needed you can also write to the logfile using OBFFLog(). There are four log levels: BFF_LOGLVL_NONE, OBFF_LOGLVL_LOW, OBFF_LOGLVL_MEDIUM, OBFF_LOGLVL_HIGH. See the API documentation to know what kind of output each function writes to the logfile for the different log levels.
Below are two examples which explain the basics.
This piece of code can always be used to find available forcefields:
FOR_EACH(OBForceField, iter) { cout << "forcefield ID: " << iter.ID() << endl; }
Calculate the energy for the structure in mol using the Ghemical forcefield.
#include <openbabel/forcefield.h> #include <openbabel/mol.h> // See OBConversion class to fill the mol object. OBMol mol; // Select the forcefield, this returns a pointer that we // will later use to access the forcefield functions. OBForceField* pFF = OBForceField::FindForceField("Ghemical"); // Make sure we have a valid pointer if (!pFF) // exit... // Set the logfile (can also be &cout or &cerr) pFF->SetLogFile(&cerr); // Set the log level. See indivual functions to know // what kind of output each function produces for the // different log levels. pFF->SetLogLevel(OBFF_LOGLVL_HIGH); // We need to setup the forcefield before we can use it. Setup() // returns false if it failes to find the atom types, parameters, ... if (!pFF->Setup(mol)) { cerr << "ERROR: could not setup force field." << endl; } // Calculate the energy. The output will be written to the // logfile specified by SetLogFile() pFF->Energy();
Minimize the structure in mol using conjugate gradients.
#include <openbabel/forcefield.h> #include <openbabel/mol.h> OBMol mol; OBForceField* pFF = OBForceField::FindForceField("Ghemical"); // Make sure we have a valid pointer if (!pFF) // exit... pFF->SetLogFile(&cerr); pFF->SetLogLevel(OBFF_LOGLVL_LOW); if (!pFF->Setup(mol)) { cerr << "ERROR: could not setup force field." << endl; } // Perform the actual minimization, maximum 1000 steps pFF->ConjugateGradients(1000);
virtual ~OBForceField | ( | ) | [inline, virtual] |
Destructor.
OBFFParameter * GetParameter | ( | int | a, | |
int | b, | |||
int | c, | |||
int | d, | |||
std::vector< OBFFParameter > & | parameter | |||
) | [protected] |
Get the correct OBFFParameter from a OBFFParameter vector.
vector<OBFFParameter> parameters;
this vector is filled with entries (as OBFFParameter) from a parameter file. This happens in the Setup() function.
GetParameter(a, 0, 0, 0, parameters);
returns the first OBFFParameter from vector<OBFFParameter> parameters where: pa = a (pa = parameter.a)
use: vdw parameters, ...
GetParameter(a, b, 0, 0, parameters);
returns the first OBFFParameter from vector<OBFFParameter> parameters where: pa = a & pb = b (ab) or: pa = b & pb = a (ba)
use: bond parameters, vdw parameters (pairs), ...
GetParameter(a, b, c, 0, parameters);
returns the first OBFFParameter from vector<OBFFParameter> parameters where: pa = a & pb = b & pc = c (abc) or: pa = c & pb = b & pc = a (cba)
use: angle parameters, ...
GetParameter(a, b, c, d, parameters);
returns the first OBFFParameter from vector<OBFFParameter> parameters where: pa = a & pb = b & pc = c & pd = d (abcd) or: pa = d & pb = b & pc = c & pd = a (dbca) or: pa = a & pb = c & pc = b & pd = d (acbd) or: pa = d & pb = c & pc = b & pd = a (dcba)
use: torsion parameters, ...
OBFFParameter * GetParameter | ( | const char * | a, | |
const char * | b, | |||
const char * | c, | |||
const char * | d, | |||
std::vector< OBFFParameter > & | parameter | |||
) | [protected] |
Calculate the potential energy function derivative numerically with repect to the coordinates of atom with index a (this vector is the gradient).
a | provides coordinates | |
terms | OBFF_ENERGY, OBFF_EBOND, OBFF_EANGLE, OBFF_ESTRBND, OBFF_ETORSION, OBFF_EOOP, OBFF_EVDW, OBFF_ELECTROSTATIC |
Calculate the potential energy function derivative analyticaly with repect to the coordinates of atom with index a (this vector is the gradient)
If the currently selected forcefield doesn't have analytical gradients, we can still call this function which will return the result of NumericalDerivative()
a | provides coordinates | |
terms | OBFF_ENERGY, OBFF_EBOND, OBFF_EANGLE, OBFF_ESTRBND, OBFF_ETORSION, OBFF_EOOP, OBFF_EVDW, OBFF_ELECTROSTATIC |
Check if two atoms are in the same ring
a | atom a | |
b | atom b |
static OBForceField* FindForceField | ( | const std::string & | ID | ) | [inline, static] |
short description of the force field type.
ID | forcefield id (Ghemical, ...) |
static OBForceField* FindForceField | ( | const char * | ID | ) | [inline, static] |
ID | forcefield id (Ghemical, ...) |
virtual std::string GetUnit | ( | ) | [inline, virtual] |
virtual bool Setup | ( | OBMol & | mol | ) | [inline, virtual] |
Setup the forcefield for mol (assigns atom types, charges, etc.)
mol | the OBMol object that contains the atoms and bonds |
bool UpdateCoordinates | ( | OBMol & | mol | ) |
Update coordinates for current conformer
mol | the OBMol object to copy the coordinates to |
bool UpdateConformers | ( | OBMol & | mol | ) |
Update coordinates for all conformers
mol | the OBMol object to copy the coordinates to |
void OBFFLog | ( | std::string | msg | ) | [inline] |
Print msg to the logfile
msg | the message |
void OBFFLog | ( | const char * | msg | ) | [inline] |
Print msg to the logfile
msg | the message |
virtual double Energy | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_Bond | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_Angle | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_StrBnd | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_Torsion | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_OOP | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_VDW | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
virtual double E_Electrostatic | ( | bool | gradients = true |
) | [inline, virtual] |
gradients | Set to true when the gradients need to be calculated (needs to be done before calling GetGradient()) |
bool SetLogFile | ( | std::ostream * | pos | ) |
Set the stream for logging (can also be &cout for logging to screen)
pos | stream |
bool SetLogLevel | ( | int | level | ) |
Set the log level (OBFF_LOGLVL_NONE, OBFF_LOGLVL_LOW, OBFF_LOGLVL_MEDIUM, OBFF_LOGLVL_HIGH)
Inline if statements for logging are available:
#define IF_OBFF_LOGLVL_LOW if(loglvl >= OBFF_LOGLVL_LOW) #define IF_OBFF_LOGLVL_MEDIUM if(loglvl >= OBFF_LOGLVL_MEDIUM) #define IF_OBFF_LOGLVL_HIGH if(loglvl >= OBFF_LOGLVL_HIGH)
example:
SetLogLevel(OBFF_LOGLVL_MEDIUM); IF_OBFF_LOGLVL_HIGH { *logos << "this text will NOT be logged..." << endl } IF_OBFF_LOGLVL_LOW { *logos << "this text will be logged..." << endl } IF_OBFF_LOGLVL_MEDIUM { *logos << "this text will also be logged..." << endl }
int GetLogLevel | ( | ) | [inline] |
void SystematicRotorSearch | ( | ) |
Generate conformers for the molecule (systematicaly rotating torsions).
The initial starting structure here is important, this structure should be minimized for the best results. SystematicRotorSearch works by rotating around the rotatable bond in a molecule (see OBRotamerList class). This rotating generates multiple conformers. The energy for all these conformers is then evaluated and the lowest energy conformer is selected.
Perform a linesearch starting at atom in direction direction
atom | start coordinates | |
direction | the search direction |
void SteepestDescent | ( | int | steps, | |
double | econv = 1e-6f , |
|||
int | method = OBFF_ANALYTICAL_GRADIENT | |||
) |
Perform steepest descent optimalization for steps steps or until convergence criteria is reached.
steps | the number of steps | |
econv | energy convergence criteria (defualt is 1e-6) | |
method | OBFF_ANALYTICAL_GRADIENTS (default) or OBFF_NUMERICAL_GRADIENTS |
void SteepestDescentInitialize | ( | int | steps = 1000 , |
|
double | econv = 1e-6f , |
|||
int | method = OBFF_ANALYTICAL_GRADIENT | |||
) |
Initialize steepest descent optimalization, to be used in combination with SteepestDescentTakeNSteps().
example:
// pFF is a pointer to a OBForceField class pFF->SteepestDescentInitialize(100, 1e-5f); while (pFF->SteepestDescentTakeNSteps(5)) { // do some updating in your program (redraw structure, ...) }
If you don't need any updating in your program, SteepestDescent() is recommended.
steps | the number of steps | |
econv | energy convergence criteria (defualt is 1e-6) | |
method | OBFF_ANALYTICAL_GRADIENTS (default) or OBFF_NUMERICAL_GRADIENTS |
bool SteepestDescentTakeNSteps | ( | int | n | ) |
Take n steps in a steepestdescent optimalization that was previously initialized with SteepestDescentInitialize().
n | the number of steps to take |
void ConjugateGradients | ( | int | steps, | |
double | econv = 1e-6f , |
|||
int | method = OBFF_ANALYTICAL_GRADIENT | |||
) |
Perform conjugate gradient optimalization for steps steps or until convergence criteria is reached.
steps | the number of steps | |
econv | energy convergence criteria (defualt is 1e-6) | |
method | OBFF_ANALYTICAL_GRADIENTS (default) or OBFF_NUMERICAL_GRADIENTS |
void ConjugateGradientsInitialize | ( | int | steps = 1000 , |
|
double | econv = 1e-6f , |
|||
int | method = OBFF_ANALYTICAL_GRADIENT | |||
) |
Initialize conjugate gradient optimalization and take the first step, to be used in combination with ConjugateGradientsTakeNSteps().
example:
// pFF is a pointer to a OBForceField class pFF->ConjugateGradientsInitialize(100, 1e-5f); while (pFF->ConjugateGradientsTakeNSteps(5)) { // do some updating in your program (redraw structure, ...) }
If you don't need any updating in your program, ConjugateGradients() is recommended.
steps | the number of steps | |
econv | energy convergence criteria (defualt is 1e-6) | |
method | OBFF_ANALYTICAL_GRADIENTS (default) or OBFF_NUMERICAL_GRADIENTS |
bool ConjugateGradientsTakeNSteps | ( | int | n | ) |
Take n steps in a conjugate gradient optimalization that was previously initialized with ConjugateGradientsInitialize().
n | the number of steps to take |
virtual bool Validate | ( | ) | [inline, virtual] |
Validate the force field implementation (debugging).
virtual bool ValidateGradients | ( | ) | [inline, virtual] |
Validate the analytical gradients by comparing them to numerical ones. This function has to be implemented force field specific. (debugging)
Calculate the error of the analytical gradient (debugging)
Calculate the derivative of a vector length. The vector is given by a - b, the length of this vector rab = sqrt(ab.x^2 + ab.y^2 + ab.z^2).
a | atom a (coordinates), will be changed to -drab/da | |
b | atom b (coordinates), will be changed to -drab/db |
Calculate the derivative of a angle a-b-c. The angle is given by dot(ab,cb)/rab*rcb.
a | atom a (coordinates), will be changed to -dtheta/da | |
b | atom b (coordinates), will be changed to -dtheta/db | |
c | atom c (coordinates), will be changed to -dtheta/dc |
Calculate the derivative of a torsion angle a-b-c-d. The torsion angle is given by dot(corss(ab,bc),cross(bc,cd)/rabbc*rbccd.
a | atom a (coordinates), will be changed to -dtheta/da | |
b | atom b (coordinates), will be changed to -dtheta/db | |
c | atom c (coordinates), will be changed to -dtheta/dc | |
d | atom d (coordinates), will be changed to -dtheta/dd |
void kludge | ( | ) | [inline] |
Do not use. This function contains rubbish merely to ensure the compiler instantiates some templated functions which are needed for the Windows Python build. TODO Find the proper way of doing this.
std::ostream* logos [protected] |
Output for logfile.
char logbuf[200] [protected] |
int loglvl [protected] |
Log level for output.
int current_conformer [protected] |
used to hold i for current conformer (needed by UpdateConformers)
double _econv [protected] |
Used for conjugate gradients and steepest descent(Initialize and TakeNSteps).
double _e_n1 [protected] |
int _method [protected] |
int _cstep [protected] |
int _nsteps [protected] |