mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-04 21:54:27 +08:00
* fix unsigned int to int comparison
* revert previous mistake
* declaration & init together, sinthetaSq in [0, 1]
* using std::swap
* use that sinThetaSq in [0,1]
* declare & init at same time
* use knowledge about target range
* use std::clamp
* use std::max
* numerically more stable trigonometrics
* numerically more stable trigonometrics
* numerically more stable trigonometrics
* range based for-loop
* actually do the assignement
* Update Code/ForceField/MMFF/Params.h
Co-authored-by: Greg Landrum <greg.landrum@gmail.com>
* implemented suggested changes
* Revert "implemented suggested changes"
This reverts commit f56e8f0ab2.
* auto typing
* remove old comment
* revert to numerically more stable expression
* now correctly formatted
---------
Co-authored-by: Greg Landrum <greg.landrum@gmail.com>
103 lines
2.9 KiB
C++
103 lines
2.9 KiB
C++
// $Id$
|
|
//
|
|
// Copyright (C) 2013 Paolo Tosco
|
|
//
|
|
// Copyright (C) 2004-2006 Rational Discovery LLC
|
|
//
|
|
// @@ All Rights Reserved @@
|
|
// This file is part of the RDKit.
|
|
// The contents are covered by the terms of the BSD license
|
|
// which is included in the file license.txt, found at the root
|
|
// of the RDKit source tree.
|
|
//
|
|
#include "DistanceConstraint.h"
|
|
#include <cmath>
|
|
#include <ForceField/ForceField.h>
|
|
#include <RDGeneral/Invariant.h>
|
|
|
|
namespace ForceFields {
|
|
namespace UFF {
|
|
DistanceConstraintContrib::DistanceConstraintContrib(
|
|
ForceField *owner, unsigned int idx1, unsigned int idx2, double minLen,
|
|
double maxLen, double forceConst) {
|
|
PRECONDITION(owner, "bad owner");
|
|
URANGE_CHECK(idx1, owner->positions().size());
|
|
URANGE_CHECK(idx2, owner->positions().size());
|
|
PRECONDITION(maxLen >= minLen, "bad bounds");
|
|
|
|
dp_forceField = owner;
|
|
d_end1Idx = idx1;
|
|
d_end2Idx = idx2;
|
|
d_minLen = minLen;
|
|
d_maxLen = maxLen;
|
|
d_forceConstant = forceConst;
|
|
}
|
|
|
|
DistanceConstraintContrib::DistanceConstraintContrib(
|
|
ForceField *owner, unsigned int idx1, unsigned int idx2, bool relative,
|
|
double minLen, double maxLen, double forceConst) {
|
|
PRECONDITION(owner, "bad owner");
|
|
const RDGeom::PointPtrVect &pos = owner->positions();
|
|
URANGE_CHECK(idx1, pos.size());
|
|
URANGE_CHECK(idx2, pos.size());
|
|
PRECONDITION(maxLen >= minLen, "bad bounds");
|
|
|
|
double dist = 0.0;
|
|
if (relative) {
|
|
RDGeom::Point3D p1 = *((RDGeom::Point3D *)pos[idx1]);
|
|
RDGeom::Point3D p2 = *((RDGeom::Point3D *)pos[idx2]);
|
|
dist = (p1 - p2).length();
|
|
}
|
|
dp_forceField = owner;
|
|
d_end1Idx = idx1;
|
|
d_end2Idx = idx2;
|
|
d_minLen = std::max(dist + minLen, 0.0);
|
|
d_maxLen = std::max(dist + maxLen, 0.0);
|
|
d_forceConstant = forceConst;
|
|
}
|
|
|
|
double DistanceConstraintContrib::getEnergy(double *pos) const {
|
|
PRECONDITION(dp_forceField, "no owner");
|
|
PRECONDITION(pos, "bad vector");
|
|
|
|
double dist = dp_forceField->distance(d_end1Idx, d_end2Idx, pos);
|
|
double distTerm = 0.0;
|
|
if (dist < d_minLen) {
|
|
distTerm = d_minLen - dist;
|
|
} else if (dist > d_maxLen) {
|
|
distTerm = dist - d_maxLen;
|
|
}
|
|
double res = 0.5 * d_forceConstant * distTerm * distTerm;
|
|
|
|
return res;
|
|
}
|
|
|
|
void DistanceConstraintContrib::getGrad(double *pos, double *grad) const {
|
|
PRECONDITION(dp_forceField, "no owner");
|
|
PRECONDITION(pos, "bad vector");
|
|
PRECONDITION(grad, "bad vector");
|
|
|
|
double dist = dp_forceField->distance(d_end1Idx, d_end2Idx, pos);
|
|
|
|
double preFactor = 0.0;
|
|
if (dist < d_minLen) {
|
|
preFactor = dist - d_minLen;
|
|
} else if (dist > d_maxLen) {
|
|
preFactor = dist - d_maxLen;
|
|
} else {
|
|
return;
|
|
}
|
|
preFactor *= d_forceConstant;
|
|
|
|
double *end1Coords = &(pos[3 * d_end1Idx]);
|
|
double *end2Coords = &(pos[3 * d_end2Idx]);
|
|
for (unsigned int i = 0; i < 3; ++i) {
|
|
double dGrad =
|
|
preFactor * (end1Coords[i] - end2Coords[i]) / std::max(dist, 1.0e-8);
|
|
grad[3 * d_end1Idx + i] += dGrad;
|
|
grad[3 * d_end2Idx + i] -= dGrad;
|
|
}
|
|
}
|
|
} // namespace UFF
|
|
} // namespace ForceFields
|