11#ifndef RD_FFCONVENIENCE_H
12#define RD_FFCONVENIENCE_H
18namespace ForceFieldsHelper {
20#ifdef RDK_BUILD_THREADSAFE_SSS
21inline void OptimizeMoleculeConfsHelper_(
23 std::vector<std::pair<int, double>> *res,
unsigned int threadIdx,
24 unsigned int numThreads,
int maxIters) {
28 "res->size() must be >= mol->getNumConformers()");
33 if (i % numThreads != threadIdx) {
36 for (
unsigned int aidx = 0; aidx < mol->
getNumAtoms(); ++aidx) {
37 ff.
positions()[aidx] = &(*cit)->getAtomPos(aidx);
40 int needsMore = ff.
minimize(maxIters);
42 (*res)[i] = std::make_pair(needsMore, e);
46inline void OptimizeMoleculeConfsMT(
ROMol &mol,
48 std::vector<std::pair<int, double>> &res,
49 int numThreads,
int maxIters) {
50 std::vector<std::thread> tg;
51 for (
int ti = 0; ti < numThreads; ++ti) {
52 tg.emplace_back(std::thread(detail::OptimizeMoleculeConfsHelper_, ff, &mol,
53 &res, ti, numThreads, maxIters));
55 for (
auto &thread : tg) {
56 if (thread.joinable()) {
64 std::vector<std::pair<int, double>> &res,
67 "res.size() must be >= mol.getNumConformers()");
71 for (
unsigned int aidx = 0; aidx < mol.
getNumAtoms(); ++aidx) {
72 ff.
positions()[aidx] = &(*cit)->getAtomPos(aidx);
75 int needsMore = ff.
minimize(maxIters);
77 res[i] = std::make_pair(needsMore, e);
95 int maxIters = 1000) {
99 return std::make_pair(res, e);
116 std::vector<std::pair<int, double>> &res,
117 int numThreads = 1,
int maxIters = 1000) {
120 if (numThreads == 1) {
123#ifdef RDK_BUILD_THREADSAFE_SSS
125 detail::OptimizeMoleculeConfsMT(mol, ff, res, numThreads, maxIters);
138 ROMol &mol,
int confId = -1) {
139 auto res = std::make_unique<ForceFields::ForceField>();
141 for (
auto &pt : conf.getPositions()) {
142 res->positions().push_back(&pt);
#define PRECONDITION(expr, mess)
A class to store forcefields and handle minimization.
double calcEnergy(std::vector< double > *contribs=nullptr) const
RDGeom::PointPtrVect & positions()
returns a reference to our points (a PointPtrVect)
void initialize()
does initialization
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 getNumConformers() const
unsigned int getNumAtoms() const
returns our number of atoms
const Conformer & getConformer(int id=-1) const
ConformerIterator beginConformers()
ConformerIterator endConformers()
void OptimizeMoleculeConfsST(ROMol &mol, ForceFields::ForceField &ff, std::vector< std::pair< int, double > > &res, int maxIters)
void OptimizeMoleculeConfs(ROMol &mol, ForceFields::ForceField &ff, std::vector< std::pair< int, double > > &res, int numThreads=1, int maxIters=1000)
std::unique_ptr< ForceFields::ForceField > createEmptyForceFieldForMol(ROMol &mol, int confId=-1)
std::pair< int, double > OptimizeMolecule(ForceFields::ForceField &ff, int maxIters=1000)
unsigned int getNumThreadsToUse(int target)