Loading...
Searching...
No Matches
obforcefield_energy.cpp

Example showing how to compute the enrgy for a molecule.

#include <openbabel/obconversion.h>
#include <openbabel/mol.h>
#include <openbabel/shared_ptr.h>
#include <openbabel/forcefield.h>
#include <iostream>
using namespace OpenBabel;
// Helper function to read molecule from file
shared_ptr<OBMol> GetMol(const std::string &filename)
{
// Create the OBMol object.
shared_ptr<OBMol> mol(new OBMol);
// Create the OBConversion object.
OBConversion conv;
OBFormat *format = conv.FormatFromExt(filename.c_str());
if (!format || !conv.SetInFormat(format)) {
std::cout << "Could not find input format for file " << filename << std::endl;
return mol;
}
// Open the file.
std::ifstream ifs(filename.c_str());
if (!ifs) {
std::cout << "Could not open " << filename << " for reading." << std::endl;
return mol;
}
// Read the molecule.
if (!conv.Read(mol.get(), &ifs)) {
std::cout << "Could not read molecule from file " << filename << std::endl;
return mol;
}
return mol;
}
int main(int argc, char **argv)
{
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <filename>" << std::endl;
return 1;
}
// Read the file.
shared_ptr<OBMol> mol = GetMol(argv[1]);
// Get the forcefield.
OBForceField *ff = OBForceField::FindType("MMFF94");
if (!ff) {
std::cout << "Could not find forcefield." << std::endl;
return 1;
}
// Setup the forcefield.
if (!ff->Setup(mol)) {
std::cout << "Could not setup forcefield." << std::endl;
return 1;
}
// Print the enegy and unit.
std::cout << ff->Energy() << " " << ff->GetUnit() << std::endl;
return 0;
}
OpenBabel::OBConversion
Class to convert from one format to another.
Definition obconversion.h:60
OpenBabel::OBConversion::Read
bool Read(OBBase *pOb, std::istream *pin=nullptr)
Reads an object of a class derived from OBBase into pOb.
Definition obconversion.cpp:830
OpenBabel::OBConversion::FormatFromExt
static OBFormat * FormatFromExt(const char *filename)
Searches registered formats for an ID the same as the file extension.
Definition obconversion.cpp:808
OpenBabel::OBConversion::SetInFormat
bool SetInFormat(const char *inID, bool isgzip=false)
Sets the input format from an id e.g. CML.
Definition obconversion.cpp:440
OpenBabel::OBForceField
Base class for molecular mechanics force fields.
Definition forcefield.h:416
OpenBabel::OBForceField::GetUnit
virtual std::string GetUnit()
Definition forcefield.h:616
OpenBabel::OBForceField::Setup
bool Setup(OBMol &mol)
Definition forcefield.cpp:812
OpenBabel::OBForceField::Energy
virtual double Energy(bool gradients=true)
Definition forcefield.h:913
OpenBabel::OBFormat
Base class for file formats.
Definition format.h:45
OpenBabel::OBMol
Molecule Class.
Definition mol.h:121
forcefield.h
Handle forcefields.
mol.h
Handle molecules. Declarations of OBMol, OBAtom, OBBond, OBResidue. (the main header for Open Babel).
OpenBabel
Global namespace for all Open Babel code.
Definition alias.h:23
obconversion.h
Handle file conversions. Declaration of OBFormat, OBConversion.
shared_ptr.h
Shared pointer.