RDKit
Open-source cheminformatics and machine learning.
UFF.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2015 Greg Landrum
3 //
4 // @@ All Rights Reserved @@
5 // This file is part of the RDKit.
6 // The contents are covered by the terms of the BSD license
7 // which is included in the file license.txt, found at the root
8 // of the RDKit source tree.
9 //
10 #ifndef RD_UFFCONVENIENCE_H
11 #define RD_UFFCONVENIENCE_H
12 #include <ForceField/ForceField.h>
13 #include "Builder.h"
14 
15 #ifdef RDK_THREADSAFE_SSS
16 #include <boost/thread.hpp>
17 #endif
18 
19 namespace RDKit {
20  class ROMol;
21  namespace UFF {
22  //! Convenience function for optimizing a molecule using UFF
23  /*
24  \param mol the molecule to use
25  \param maxIters the maximum number of force-field iterations
26  \param vdwThresh the threshold to be used in adding van der Waals terms
27  to the force field. Any non-bonded contact whose current
28  distance is greater than \c vdwThresh * the minimum value
29  for that contact will not be included.
30  \param confId the optional conformer id, if this isn't provided, the molecule's
31  default confId will be used.
32  \param ignoreInterfragInteractions if true, nonbonded terms will not be added between
33  fragments
34 
35  \return a pair with:
36  first: 0 if the optimization converged, 1 if more iterations are required.
37  second: the energy
38  */
39  std::pair<int,double> UFFOptimizeMolecule(ROMol &mol, int maxIters=1000,
40  double vdwThresh=10.0, int confId=-1,
41  bool ignoreInterfragInteractions=true ){
42  ForceFields::ForceField *ff=UFF::constructForceField(mol,vdwThresh, confId,
43  ignoreInterfragInteractions);
44  ff->initialize();
45  int res=ff->minimize(maxIters);
46  double e=ff->calcEnergy();
47  delete ff;
48  return std::make_pair(res,e);
49  }
50 #ifdef RDK_THREADSAFE_SSS
51  namespace detail {
52  void UFFOptimizeMoleculeConfsHelper_(ForceFields::ForceField ff,
53  ROMol *mol,
54  std::vector< std::pair<int, double> > *res,
55  unsigned int threadIdx,
56  unsigned int numThreads,
57  int maxIters){
58  unsigned int i=0;
59  ff.positions().resize(mol->getNumAtoms());
60  for(ROMol::ConformerIterator cit=mol->beginConformers();
61  cit!=mol->endConformers();++cit,++i){
62  if(i%numThreads != threadIdx) continue;
63  for(unsigned int aidx=0;aidx<mol->getNumAtoms();++aidx){
64  ff.positions()[aidx]=&(*cit)->getAtomPos(aidx);
65  }
66  ff.initialize();
67  int needsMore=ff.minimize(maxIters);
68  double e=ff.calcEnergy();
69  (*res)[i] = std::make_pair(needsMore,e);
70  }
71  }
72  } //end of detail namespace
73 #endif
74  //! Convenience function for optimizing all of a molecule's conformations using UFF
75  /*
76  \param mol the molecule to use
77  \param res vector of (needsMore,energy) pairs
78  \param numThreads the number of simultaneous threads to use (only has an
79  effect if the RDKit is compiled with thread support).
80  \param maxIters the maximum number of force-field iterations
81  \param vdwThresh the threshold to be used in adding van der Waals terms
82  to the force field. Any non-bonded contact whose current
83  distance is greater than \c vdwThresh * the minimum value
84  for that contact will not be included.
85  \param ignoreInterfragInteractions if true, nonbonded terms will not be added between
86  fragments
87 
88  */
90  std::vector< std::pair<int, double> > &res,
91  unsigned int numThreads=1,
92  int maxIters=1000,
93  double vdwThresh=10.0,
94  bool ignoreInterfragInteractions=true ){
95  res.resize(mol.getNumConformers());
96 #ifndef RDK_THREADSAFE_SSS
97  numThreads=1;
98 #endif
99  if(numThreads<=1){
100  unsigned int i=0;
101  for(ROMol::ConformerIterator cit=mol.beginConformers();
102  cit!=mol.endConformers();++cit,++i){
103  res[i] = UFFOptimizeMolecule(mol,maxIters,vdwThresh,(*cit)->getId(),
104  ignoreInterfragInteractions);
105  }
106  }
107 #ifdef RDK_THREADSAFE_SSS
108  else {
110  ignoreInterfragInteractions);
111  boost::thread_group tg;
112  for(unsigned int ti=0;ti<numThreads;++ti){
113  tg.add_thread(new boost::thread(detail::UFFOptimizeMoleculeConfsHelper_,
114  *ff,
115  &mol,&res,ti,numThreads,maxIters));
116 
117  }
118  tg.join_all();
119  delete ff;
120  }
121 #endif
122  }
123  } // end of namespace UFF
124 } // end of namespace RDKit
125 #endif
std::pair< int, double > UFFOptimizeMolecule(ROMol &mol, int maxIters=1000, double vdwThresh=10.0, int confId=-1, bool ignoreInterfragInteractions=true)
Convenience function for optimizing a molecule using UFF.
Definition: UFF.h:39
double calcEnergy() const
calculates and returns the energy (in kcal/mol) based on existing positions in the forcefield ...
unsigned int getNumConformers() const
Definition: ROMol.h:315
RDGeom::PointPtrVect & positions()
returns a reference to our points (a PointPtrVect)
Definition: ForceField.h:137
void UFFOptimizeMoleculeConfs(ROMol &mol, std::vector< std::pair< int, double > > &res, unsigned int numThreads=1, int maxIters=1000, double vdwThresh=10.0, bool ignoreInterfragInteractions=true)
Convenience function for optimizing all of a molecule&#39;s conformations using UFF.
Definition: UFF.h:89
unsigned int getNumAtoms(bool onlyExplicit=1) const
returns our number of atoms
int minimize(unsigned int maxIts=200, double forceTol=1e-4, double energyTol=1e-6)
minimizes the energy of the system by following gradients
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:105
void initialize()
does initialization
Definition: types.h:23
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:28
ForceFields::ForceField * constructForceField(ROMol &mol, double vdwThresh=100.0, int confId=-1, bool ignoreInterfragInteractions=true)
Builds and returns a UFF force field for a molecule.
ConformerIterator endConformers()
Definition: ROMol.h:498
ConformerIterator beginConformers()
Definition: ROMol.h:494
A class to store forcefields and handle minimization.
Definition: ForceField.h:56