RDKit: FourthDimContribs.h Source File

| RDKit Open-source cheminformatics and machine learning.

Loading...
Searching...
No Matches
FourthDimContribs.h
Go to the documentation of this file.
1//
2// Copyright (C) 2024 Greg Landrum and other RDKit contributors
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_FOURTHDIMCONTRIBS_H
12#define RD_FOURTHDIMCONTRIBS_H
13
14#include <vector>
15#include <RDGeneral/Invariant.h>
16#include <ForceField/Contrib.h>
17#include <ForceField/ForceField.h>
18
19namespace DistGeom {
20
21 struct FourthDimContribsParams {
22 unsigned int idx{0};
23 double weight{0.0};
24 FourthDimContribsParams(unsigned int idx, double w) : idx(idx), weight(w) {};
25};
26
27//! A term used in penalizing the 4th dimension in order to move from 4D->3D
28//!
29 class RDKIT_DISTGEOMETRY_EXPORT FourthDimContribs
30 : public ForceFields::ForceFieldContrib {
31 public:
32 FourthDimContribs() = default;
33
34 //! Constructor
35 /*!
36 \param owner pointer to the owning ForceField
37 \param idx the index of the atom to be considered
38 \param weight (optional) the weight to be used for this contrib
39
40 */
41 FourthDimContribs(ForceFields::ForceField *owner) {
42 PRECONDITION(owner, "bad force field");
43 PRECONDITION(owner->dimension() == 4, "force field has wrong dimension");
44 dp_forceField = owner;
45 }
46
47 void addContrib(unsigned int idx, double weight) {
48 d_contribs.emplace_back(idx, weight);
49 }
50
51 //! return the contribution of this contrib to the energy of a given state
52 double getEnergy(double *pos) const override {
53 PRECONDITION(pos, "bad vector");
54 constexpr unsigned int ffdim = 4;
55 double res = 0.0;
56 for (const auto &contrib : d_contribs) {
57 unsigned int pid = contrib.idx * ffdim + 3;
58 res += contrib.weight * pos[pid] * pos[pid];
59 }
60 return res;
61 }
62
63 //! calculate the contribution of this contrib to the gradient at a given
64 /// state
65 void getGrad(double *pos, double *grad) const override {
66 PRECONDITION(pos, "bad vector");
67 constexpr unsigned int ffdim = 4;
68 for (const auto &contrib : d_contribs) {
69 unsigned int pid = contrib.idx * ffdim + 3;
70 grad[pid] += contrib.weight * pos[pid];
71 }
72 }
73 FourthDimContribs *copy() const override {
74 return new FourthDimContribs(*this);
75 }
76 bool empty() const { return d_contribs.empty(); }
77 unsigned int size() const { return d_contribs.size(); }
78
79 private:
80 std::vector<FourthDimContribsParams> d_contribs;
81};
82} // namespace DistGeom
83
84#endif
Contrib.h
ForceField.h
Invariant.h
PRECONDITION
#define PRECONDITION(expr, mess)
Definition Invariant.h:108
DistGeom::FourthDimContribs::FourthDimContribs
FourthDimContribs()=default
DistGeom::FourthDimContribs::copy
FourthDimContribs * copy() const override
return a copy
Definition FourthDimContribs.h:73
DistGeom::FourthDimContribs::FourthDimContribs
FourthDimContribs(ForceFields::ForceField *owner)
Constructor.
Definition FourthDimContribs.h:41
DistGeom::FourthDimContribs::size
unsigned int size() const
Definition FourthDimContribs.h:77
DistGeom::FourthDimContribs::empty
bool empty() const
Definition FourthDimContribs.h:76
DistGeom::FourthDimContribs::addContrib
void addContrib(unsigned int idx, double weight)
Definition FourthDimContribs.h:47
DistGeom::FourthDimContribs::getEnergy
double getEnergy(double *pos) const override
return the contribution of this contrib to the energy of a given state
Definition FourthDimContribs.h:52
DistGeom::FourthDimContribs::getGrad
void getGrad(double *pos, double *grad) const override
Definition FourthDimContribs.h:65
ForceFields::ForceFieldContrib
abstract base class for contributions to ForceFields
Definition Contrib.h:18
ForceFields::ForceFieldContrib::dp_forceField
ForceField * dp_forceField
our owning ForceField
Definition Contrib.h:36
ForceFields::ForceField
A class to store forcefields and handle minimization.
Definition ForceField.h:79
ForceFields::ForceField::dimension
unsigned int dimension() const
returns the dimension of the forcefield
Definition ForceField.h:235
export.h
RDKIT_DISTGEOMETRY_EXPORT
#define RDKIT_DISTGEOMETRY_EXPORT
Definition export.h:137
DistGeom
Definition BoundsMatrix.h:19
DistGeom::FourthDimContribsParams::idx
unsigned int idx
Definition FourthDimContribs.h:22
DistGeom::FourthDimContribsParams::FourthDimContribsParams
FourthDimContribsParams(unsigned int idx, double w)
Definition FourthDimContribs.h:24
DistGeom::FourthDimContribsParams::weight
double weight
Definition FourthDimContribs.h:23