1
/**********************************************************************
2
ffmmff94.cpp - Test energy and gradients for MMFF94 force field
4
This file is part of the Open Babel project.
5
For more information, see <http://openbabel.sourceforge.net/>
7
Some portions Copyright (C) 2008 Geoffrey R. Hutchison
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation version 2 of the License.
13
This program is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16
GNU General Public License for more details.
17
***********************************************************************/
19
// used to set import/export for Cygwin DLLs
24
#include <openbabel/babelconfig.h>
28
#include <openbabel/mol.h>
29
#include <openbabel/obconversion.h>
30
#include <openbabel/forcefield.h>
34
bool SafeOpen(std::ifstream &fs, const char *filename);
35
bool SafeOpen(std::ofstream &fs, const char *filename);
39
using namespace OpenBabel;
42
string testdatadir = TESTDATADIR;
43
string results_file = testdatadir + "mmff94results.txt";
44
string molecules_file = testdatadir + "forcefield.sdf";
46
string results_file = "files/mmff94results.txt";
47
string molecules_file = "files/forcefield.sdf";
50
void GenerateEnergies();
52
int main(int argc,char *argv[])
54
// turn off slow sync with C-style output (we don't use it anyway).
55
std::ios::sync_with_stdio(false);
59
if (strncmp(argv[1], "-g", 2))
61
cout << "Usage: ffmmff94" << endl;
62
cout << " Tests Open Babel MMFF94 force field implementation." << endl;
72
cout << "# Testing MMFF94 Force Field..." << endl;
75
if (!SafeOpen(mifs, molecules_file.c_str()))
77
cout << "Bail out! Cannot read file " << molecules_file << endl;
78
return -1; // test failed
82
if (!SafeOpen(rifs, results_file.c_str()))
84
cout << "Bail out! Cannot read file " << results_file << endl;
85
return -1; // test failed
88
char buffer[BUFF_SIZE];
91
OBConversion conv(&mifs, &cout);
92
unsigned int currentTest = 0;
94
if(! conv.SetInAndOutFormats("SDF","SDF"))
96
cout << "Bail out! SDF format is not loaded" << endl;
97
return -1; // test failed
100
OBForceField* pFF = OBForceField::FindForceField("MMFF94");
103
cerr << "Bail out! Cannot load force field!" << endl;
104
return -1; // test failed
107
pFF->SetLogFile(&cout);
108
pFF->SetLogLevel(OBFF_LOGLVL_NONE);
117
if (!rifs.getline(buffer,BUFF_SIZE))
119
cout << "Bail out! error reading reference data" << endl;
120
return -1; // test failed
123
if (!pFF->Setup(mol)) {
124
cout << "Bail out! could not setup force field on " << mol.GetTitle() << endl;
125
return -1; // test failed
128
// compare the calculated energy to our reference data
129
energy = pFF->Energy(false);
130
if ( fabs(atof(buffer) - energy ) > 1.0e-3)
132
cout << "not ok " << ++currentTest << " # calculated energy incorrect "
133
<< " for molecule " << mol.GetTitle() << "\n";
134
cout << "# Expected " << buffer << " found " <<
138
cout << "ok " << ++currentTest << " # energy \n";
140
// check that gradients validate too
141
if (!pFF->ValidateGradients())
143
cout << "not ok " << ++currentTest << " # gradients do not validate "
144
<< " for molecule " << mol.GetTitle() << "\n";
147
cout << "ok " << ++currentTest << " # gradients \n";
150
// return number of tests run
151
cout << "1.." << currentTest << endl;
157
void GenerateEnergies()
160
if (!SafeOpen(ifs, molecules_file.c_str()))
164
if (!SafeOpen(ofs, results_file.c_str()))
168
OBConversion conv(&ifs, &cout);
169
char buffer[BUFF_SIZE];
171
if(! conv.SetInAndOutFormats("SDF","SDF"))
173
cerr << "SDF format is not loaded" << endl;
177
OBForceField* pFF = OBForceField::FindForceField("MMFF94");
180
cerr << "Cannot load force field!" << endl;
184
pFF->SetLogFile(&cout);
185
pFF->SetLogLevel(OBFF_LOGLVL_NONE);
194
if (!pFF->Setup(mol)) {
195
cerr << "Could not setup force field on molecule: " << mol.GetTitle() << endl;
199
// Don't compute gradients
200
sprintf(buffer, "%15.5f\n", pFF->Energy(false));
204
cerr << " MMFF94 force field energies written successfully" << endl;