~ubuntu-branches/debian/stretch/openbabel/stretch

« back to all changes in this revision

Viewing changes to test/ffmmff94.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Banck
  • Date: 2008-07-22 23:54:58 UTC
  • mfrom: (3.1.10 intrepid)
  • Revision ID: james.westby@ubuntu.com-20080722235458-3o606czluviz4akx
Tags: 2.2.0-2
* Upload to unstable.
* debian/control: Updated descriptions.
* debian/patches/gauss_cube_format.patch: New patch, makes the 
  gaussian cube format available again.
* debian/rules (DEB_DH_MAKESHLIBS_ARGS_libopenbabel3): Removed.
* debian/rules (DEB_CONFIGURE_EXTRA_FLAGS): Likewise.
* debian/libopenbabel3.install: Adjust formats directory to single 
  version hierarchy.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**********************************************************************
 
2
ffmmff94.cpp - Test energy and gradients for MMFF94 force field
 
3
 
 
4
This file is part of the Open Babel project.
 
5
For more information, see <http://openbabel.sourceforge.net/>
 
6
 
 
7
Some portions Copyright (C) 2008 Geoffrey R. Hutchison
 
8
 
 
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.
 
12
 
 
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
***********************************************************************/
 
18
 
 
19
// used to set import/export for Cygwin DLLs
 
20
#ifdef WIN32
 
21
#define USING_OBDLL
 
22
#endif
 
23
 
 
24
#include <openbabel/babelconfig.h>
 
25
 
 
26
#include <fstream>
 
27
 
 
28
#include <openbabel/mol.h>
 
29
#include <openbabel/obconversion.h>
 
30
#include <openbabel/forcefield.h>
 
31
 
 
32
namespace OpenBabel
 
33
{
 
34
  bool SafeOpen(std::ifstream &fs, const char *filename);
 
35
  bool SafeOpen(std::ofstream &fs, const char *filename);
 
36
}
 
37
 
 
38
using namespace std;
 
39
using namespace OpenBabel;
 
40
 
 
41
#ifdef TESTDATADIR
 
42
  string testdatadir = TESTDATADIR;
 
43
  string results_file = testdatadir + "mmff94results.txt";
 
44
  string molecules_file = testdatadir + "forcefield.sdf";
 
45
#else
 
46
  string results_file = "files/mmff94results.txt";
 
47
  string molecules_file = "files/forcefield.sdf";
 
48
#endif
 
49
 
 
50
void GenerateEnergies();
 
51
 
 
52
int main(int argc,char *argv[])
 
53
{
 
54
  // turn off slow sync with C-style output (we don't use it anyway).
 
55
  std::ios::sync_with_stdio(false);
 
56
 
 
57
  if (argc != 1)
 
58
    {
 
59
      if (strncmp(argv[1], "-g", 2))
 
60
        {
 
61
          cout << "Usage: ffmmff94" << endl;
 
62
          cout << "   Tests Open Babel MMFF94 force field implementation." << endl;
 
63
          return 0;
 
64
        }
 
65
      else
 
66
        {
 
67
          GenerateEnergies();
 
68
          return 0;
 
69
        }
 
70
    }
 
71
 
 
72
  cout << "# Testing MMFF94 Force Field..." << endl;
 
73
 
 
74
  std::ifstream mifs;
 
75
  if (!SafeOpen(mifs, molecules_file.c_str()))
 
76
    {
 
77
      cout << "Bail out! Cannot read file " << molecules_file << endl;
 
78
      return -1; // test failed
 
79
    }
 
80
 
 
81
  std::ifstream rifs;
 
82
  if (!SafeOpen(rifs, results_file.c_str()))
 
83
    {
 
84
      cout << "Bail out! Cannot read file " << results_file << endl;
 
85
      return -1; // test failed
 
86
    }
 
87
 
 
88
  char buffer[BUFF_SIZE];
 
89
  vector<string> vs;
 
90
  OBMol mol;
 
91
  OBConversion conv(&mifs, &cout);
 
92
  unsigned int currentTest = 0;
 
93
 
 
94
  if(! conv.SetInAndOutFormats("SDF","SDF"))
 
95
    {
 
96
      cout << "Bail out! SDF format is not loaded" << endl;
 
97
      return -1; // test failed
 
98
    }
 
99
    
 
100
  OBForceField* pFF = OBForceField::FindForceField("MMFF94");
 
101
 
 
102
  if (pFF == NULL) {
 
103
    cerr << "Bail out! Cannot load force field!" << endl;
 
104
    return -1; // test failed
 
105
  }
 
106
 
 
107
  pFF->SetLogFile(&cout);
 
108
  pFF->SetLogLevel(OBFF_LOGLVL_NONE);
 
109
 
 
110
  double energy;
 
111
  while(mifs)
 
112
    {
 
113
      mol.Clear();
 
114
      conv.Read(&mol);
 
115
      if (mol.Empty())
 
116
        continue;
 
117
      if (!rifs.getline(buffer,BUFF_SIZE))
 
118
        {
 
119
          cout << "Bail out! error reading reference data" << endl;
 
120
          return -1; // test failed
 
121
        }
 
122
        
 
123
      if (!pFF->Setup(mol)) {
 
124
        cout << "Bail out! could not setup force field on " << mol.GetTitle() << endl;
 
125
        return -1; // test failed
 
126
      }
 
127
 
 
128
      // compare the calculated energy to our reference data
 
129
      energy = pFF->Energy(false);
 
130
      if ( fabs(atof(buffer) - energy ) > 1.0e-3)
 
131
        {
 
132
          cout << "not ok " << ++currentTest << " # calculated energy incorrect "
 
133
               << " for molecule " << mol.GetTitle() << "\n";
 
134
          cout << "# Expected " << buffer << " found " <<
 
135
            energy << "\n";
 
136
        }
 
137
      else
 
138
        cout << "ok " << ++currentTest << " # energy \n";
 
139
 
 
140
      // check that gradients validate too
 
141
      if (!pFF->ValidateGradients())
 
142
        {
 
143
          cout << "not ok " << ++currentTest << " # gradients do not validate "
 
144
               << " for molecule " << mol.GetTitle() << "\n";
 
145
        }
 
146
      else
 
147
        cout << "ok " << ++currentTest << " # gradients \n";
 
148
    }
 
149
 
 
150
  // return number of tests run
 
151
  cout << "1.." << currentTest << endl;
 
152
 
 
153
  // Passed tests
 
154
  return 0;
 
155
}
 
156
 
 
157
void GenerateEnergies()
 
158
{
 
159
  std::ifstream ifs;
 
160
  if (!SafeOpen(ifs, molecules_file.c_str()))
 
161
    return;
 
162
 
 
163
  std::ofstream ofs;
 
164
  if (!SafeOpen(ofs, results_file.c_str()))
 
165
    return;
 
166
 
 
167
  OBMol mol;
 
168
  OBConversion conv(&ifs, &cout);
 
169
  char buffer[BUFF_SIZE];
 
170
  
 
171
  if(! conv.SetInAndOutFormats("SDF","SDF"))
 
172
    {
 
173
      cerr << "SDF format is not loaded" << endl;
 
174
      return;
 
175
    }
 
176
 
 
177
  OBForceField* pFF = OBForceField::FindForceField("MMFF94");
 
178
 
 
179
  if (pFF == NULL) {
 
180
    cerr << "Cannot load force field!" << endl;
 
181
    return;
 
182
  }
 
183
 
 
184
  pFF->SetLogFile(&cout);
 
185
  pFF->SetLogLevel(OBFF_LOGLVL_NONE);
 
186
 
 
187
  for (;ifs;)
 
188
    {
 
189
      mol.Clear();
 
190
      conv.Read(&mol);
 
191
      if (mol.Empty())
 
192
        continue;
 
193
 
 
194
      if (!pFF->Setup(mol)) {
 
195
        cerr << "Could not setup force field on molecule: " << mol.GetTitle() << endl;
 
196
        return;
 
197
      }
 
198
      
 
199
      // Don't compute gradients
 
200
      sprintf(buffer, "%15.5f\n", pFF->Energy(false));
 
201
      ofs << buffer;
 
202
    }
 
203
 
 
204
        cerr << " MMFF94 force field energies written successfully" << endl;
 
205
  return;
 
206
}