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 <RDGeneral/RDThreads.h>
14 #include "Builder.h"
15 
16 namespace RDKit {
17 class ROMol;
18 namespace UFF {
19 //! Convenience function for optimizing a molecule using UFF
20 /*
21  \param mol the molecule to use
22  \param maxIters the maximum number of force-field iterations
23  \param vdwThresh the threshold to be used in adding van der Waals terms
24  to the force field. Any non-bonded contact whose current
25  distance is greater than \c vdwThresh * the minimum value
26  for that contact will not be included.
27  \param confId the optional conformer id, if this isn't provided, the
28  molecule's
29  default confId will be used.
30  \param ignoreInterfragInteractions if true, nonbonded terms will not be added
31  between
32  fragments
33 
34  \return a pair with:
35  first: 0 if the optimization converged, 1 if more iterations are required.
36  second: the energy
37 */
38 std::pair<int, double> UFFOptimizeMolecule(
39  ROMol &mol, int maxIters = 1000, double vdwThresh = 10.0, int confId = -1,
40  bool ignoreInterfragInteractions = true) {
42  mol, vdwThresh, confId, ignoreInterfragInteractions);
43  ff->initialize();
44  int res = ff->minimize(maxIters);
45  double e = ff->calcEnergy();
46  delete ff;
47  return std::make_pair(res, e);
48 }
49 #ifdef RDK_THREADSAFE_SSS
50 namespace detail {
51 void UFFOptimizeMoleculeConfsHelper_(ForceFields::ForceField ff, ROMol *mol,
52  std::vector<std::pair<int, double> > *res,
53  unsigned int threadIdx,
54  unsigned int numThreads, int maxIters) {
55  unsigned int i = 0;
56  ff.positions().resize(mol->getNumAtoms());
57  for (ROMol::ConformerIterator cit = mol->beginConformers();
58  cit != mol->endConformers(); ++cit, ++i) {
59  if (i % numThreads != threadIdx) continue;
60  for (unsigned int aidx = 0; aidx < mol->getNumAtoms(); ++aidx) {
61  ff.positions()[aidx] = &(*cit)->getAtomPos(aidx);
62  }
63  ff.initialize();
64  int needsMore = ff.minimize(maxIters);
65  double e = ff.calcEnergy();
66  (*res)[i] = std::make_pair(needsMore, e);
67  }
68 }
69 } // end of detail namespace
70 #endif
71 //! Convenience function for optimizing all of a molecule's conformations using
72 // UFF
73 /*
74  \param mol the molecule to use
75  \param res vector of (needsMore,energy)
76  \param numThreads the number of simultaneous threads to use (only has an
77  effect if the RDKit is compiled with thread support).
78  If set to zero, the max supported by the system will be
79  used.
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
86  between
87  fragments
88 
89 */
91  std::vector<std::pair<int, double> > &res,
92  int numThreads = 1, int maxIters = 1000,
93  double vdwThresh = 10.0,
94  bool ignoreInterfragInteractions = true) {
95  res.resize(mol.getNumConformers());
96  numThreads = getNumThreadsToUse(numThreads);
97  if (numThreads == 1) {
98  unsigned int i = 0;
99  for (ROMol::ConformerIterator cit = mol.beginConformers();
100  cit != mol.endConformers(); ++cit, ++i) {
101  res[i] = UFFOptimizeMolecule(mol, maxIters, vdwThresh, (*cit)->getId(),
102  ignoreInterfragInteractions);
103  }
104  }
105 #ifdef RDK_THREADSAFE_SSS
106  else {
108  mol, vdwThresh, -1, ignoreInterfragInteractions);
109  boost::thread_group tg;
110  for (int ti = 0; ti < numThreads; ++ti) {
111  tg.add_thread(new boost::thread(detail::UFFOptimizeMoleculeConfsHelper_,
112  *ff, &mol, &res, ti, numThreads,
113  maxIters));
114  }
115  tg.join_all();
116  delete ff;
117  }
118 #endif
119 }
120 } // end of namespace UFF
121 } // end of namespace RDKit
122 #endif
unsigned int getNumConformers() const
Definition: ROMol.h:363
int minimize(unsigned int snapshotFreq, RDKit::SnapshotVect *snapshotVect, unsigned int maxIts=200, double forceTol=1e-4, double energyTol=1e-6)
minimizes the energy of the system by following gradients
RDGeom::PointPtrVect & positions()
returns a reference to our points (a PointPtrVect)
Definition: ForceField.h:160
unsigned int getNumAtoms(bool onlyExplicit=1) const
returns our number of atoms
void UFFOptimizeMoleculeConfs(ROMol &mol, std::vector< std::pair< int, double > > &res, 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.
Definition: UFF.h:90
ROMol is a molecule class that is intended to have a fixed topology.
Definition: ROMol.h:103
void initialize()
does initialization
Includes a bunch of functionality for handling Atom and Bond queries.
Definition: Atom.h:29
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:38
unsigned int getNumThreadsToUse(int target)
Definition: RDThreads.h:38
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:543
ConformerIterator beginConformers()
Definition: ROMol.h:541
A class to store forcefields and handle minimization.
Definition: ForceField.h:57
double calcEnergy(std::vector< double > *contribs=NULL) const
calculates and returns the energy (in kcal/mol) based on existing