RDKit
Open-source cheminformatics and machine learning.
UFF.h
Go to the documentation of this file.
1 //
2 // Copyright (C) 2015-2018 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 #include <RDGeneral/export.h>
11 #ifndef RD_UFFCONVENIENCE_H
12 #define RD_UFFCONVENIENCE_H
13 #include <ForceField/ForceField.h>
14 #include <RDGeneral/RDThreads.h>
15 #include "Builder.h"
16 
17 namespace RDKit {
18 class ROMol;
19 namespace UFF {
20 //! Convenience function for optimizing a molecule using UFF
21 /*
22  \param mol the molecule to use
23  \param maxIters the maximum number of force-field iterations
24  \param vdwThresh the threshold to be used in adding van der Waals terms
25  to the force field. Any non-bonded contact whose current
26  distance is greater than \c vdwThresh * the minimum value
27  for that contact will not be included.
28  \param confId the optional conformer id, if this isn't provided, the
29  molecule's
30  default confId will be used.
31  \param ignoreInterfragInteractions if true, nonbonded terms will not be added
32  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(
40  ROMol &mol, int maxIters = 1000, double vdwThresh = 10.0, int confId = -1,
41  bool ignoreInterfragInteractions = true) {
43  mol, vdwThresh, confId, 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, ROMol *mol,
53  std::vector<std::pair<int, double>> *res,
54  unsigned int threadIdx,
55  unsigned int numThreads, int maxIters) {
56  unsigned int i = 0;
57  ff.positions().resize(mol->getNumAtoms());
58  for (ROMol::ConformerIterator cit = mol->beginConformers();
59  cit != mol->endConformers(); ++cit, ++i) {
60  if (i % numThreads != threadIdx) continue;
61  for (unsigned int aidx = 0; aidx < mol->getNumAtoms(); ++aidx) {
62  ff.positions()[aidx] = &(*cit)->getAtomPos(aidx);
63  }
64  ff.initialize();
65  int needsMore = ff.minimize(maxIters);
66  double e = ff.calcEnergy();
67  (*res)[i] = std::make_pair(needsMore, e);
68  }
69 }
70 } // end of detail namespace
71 #endif
72 //! Convenience function for optimizing all of a molecule's conformations using
73 // UFF
74 /*
75  \param mol the molecule to use
76  \param res vector of (needsMore,energy)
77  \param numThreads the number of simultaneous threads to use (only has an
78  effect if the RDKit is compiled with thread support).
79  If set to zero, the max supported by the system will be
80  used.
81  \param maxIters the maximum number of force-field iterations
82  \param vdwThresh the threshold to be used in adding van der Waals terms
83  to the force field. Any non-bonded contact whose current
84  distance is greater than \c vdwThresh * the minimum value
85  for that contact will not be included.
86  \param ignoreInterfragInteractions if true, nonbonded terms will not be added
87  between
88  fragments
89 
90 */
92  std::vector<std::pair<int, double>> &res,
93  int numThreads = 1, int maxIters = 1000,
94  double vdwThresh = 10.0,
95  bool ignoreInterfragInteractions = true) {
96  res.resize(mol.getNumConformers());
97  numThreads = getNumThreadsToUse(numThreads);
98  if (numThreads == 1) {
99  unsigned int i = 0;
100  for (ROMol::ConformerIterator cit = mol.beginConformers();
101  cit != mol.endConformers(); ++cit, ++i) {
102  res[i] = UFFOptimizeMolecule(mol, maxIters, vdwThresh, (*cit)->getId(),
103  ignoreInterfragInteractions);
104  }
105  }
106 #ifdef RDK_THREADSAFE_SSS
107  else {
109  mol, vdwThresh, -1, ignoreInterfragInteractions);
110  std::vector<std::thread> tg;
111  for (int ti = 0; ti < numThreads; ++ti) {
112  tg.emplace_back(std::thread(detail::UFFOptimizeMoleculeConfsHelper_, *ff,
113  &mol, &res, ti, numThreads, maxIters));
114  }
115  for (auto &thread : tg) {
116  if (thread.joinable()) thread.join();
117  }
118  delete ff;
119  }
120 #endif
121 }
122 } // end of namespace UFF
123 } // end of namespace RDKit
124 #endif
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
unsigned int getNumAtoms(bool onlyExplicit=1) const
returns our number of atoms
RDGeom::PointPtrVect & positions()
returns a reference to our points (a PointPtrVect)
Definition: ForceField.h:161
double calcEnergy(std::vector< double > *contribs=NULL) const
calculates and returns the energy (in kcal/mol) based on existing
unsigned int getNumConformers() const
Definition: ROMol.h:435
void initialize()
does initialization
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:91
Std stuff.
Definition: Atom.h:30
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
unsigned int getNumThreadsToUse(int target)
Definition: RDThreads.h:37
ConformerIterator endConformers()
Definition: ROMol.h:611
ConformerIterator beginConformers()
Definition: ROMol.h:609
A class to store forcefields and handle minimization.
Definition: ForceField.h:58
RDKIT_FORCEFIELDHELPERS_EXPORT 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.