Chemical Data Processing Library C++ API - Version 1.4.0
MMFF94GradientCalculator.hpp
Go to the documentation of this file.
1 /*
2  * MMFF94GradientCalculator.hpp
3  *
4  * This file is part of the Chemical Data Processing Toolkit
5  *
6  * Copyright (C) 2003 Thomas Seidel <thomas.seidel@univie.ac.at>
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License
19  * along with this library; see the file COPYING. If not, write to
20  * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 
29 #ifndef CDPL_FORCEFIELD_MMFF94GRADIENTCALCULATOR_HPP
30 #define CDPL_FORCEFIELD_MMFF94GRADIENTCALCULATOR_HPP
31 
32 #include <cstddef>
33 
39 #include "CDPL/Util/BitSet.hpp"
40 
41 
42 namespace CDPL
43 {
44 
45  namespace ForceField
46  {
47 
60  template <typename ValueType>
62  {
63 
64  public:
71 
77  MMFF94GradientCalculator(const MMFF94InteractionData& ia_data, std::size_t num_atoms);
78 
84  void setEnabledInteractionTypes(unsigned int types);
85 
90  unsigned int getEnabledInteractionTypes() const;
91 
97  void setup(const MMFF94InteractionData& ia_data, std::size_t num_atoms);
98 
105  template <typename CoordsArray>
106  const ValueType& operator()(const CoordsArray& coords);
107 
119  template <typename CoordsArray, typename GradVector>
120  const ValueType& operator()(const CoordsArray& coords, GradVector& grad);
121 
126  const ValueType& getTotalEnergy() const;
127 
132  const ValueType& getBondStretchingEnergy() const;
133 
138  const ValueType& getAngleBendingEnergy() const;
139 
144  const ValueType& getStretchBendEnergy() const;
145 
150  const ValueType& getOutOfPlaneBendingEnergy() const;
151 
156  const ValueType& getTorsionEnergy() const;
157 
162  const ValueType& getElectrostaticEnergy() const;
163 
168  const ValueType& getVanDerWaalsEnergy() const;
169 
175 
180  void setFixedAtomMask(const Util::BitSet& mask);
181 
186 
187  private:
188  const MMFF94InteractionData* interactionData;
189  std::size_t numAtoms;
190  ValueType totalEnergy;
191  ValueType bondStretchingEnergy;
192  ValueType angleBendingEnergy;
193  ValueType stretchBendEnergy;
194  ValueType outOfPlaneEnergy;
195  ValueType torsionEnergy;
196  ValueType electrostaticEnergy;
197  ValueType vanDerWaalsEnergy;
198  unsigned int interactionTypes;
199  Util::BitSet fixedAtomMask;
200  };
201  } // namespace ForceField
202 } // namespace CDPL
203 
204 
205 // Implementation
206 // \cond DOC_IMPL_DETAILS
207 
208 template <typename ValueType>
210  interactionData(0), numAtoms(0), totalEnergy(), bondStretchingEnergy(), angleBendingEnergy(),
211  stretchBendEnergy(), outOfPlaneEnergy(), torsionEnergy(), electrostaticEnergy(),
212  vanDerWaalsEnergy(), interactionTypes(InteractionType::ALL)
213 {}
214 
215 template <typename ValueType>
216 CDPL::ForceField::MMFF94GradientCalculator<ValueType>::MMFF94GradientCalculator(const MMFF94InteractionData& ia_data, std::size_t num_atoms):
217  interactionData(&ia_data), numAtoms(num_atoms), totalEnergy(), bondStretchingEnergy(), angleBendingEnergy(),
218  stretchBendEnergy(), outOfPlaneEnergy(), torsionEnergy(), electrostaticEnergy(),
219  vanDerWaalsEnergy(), interactionTypes(InteractionType::ALL)
220 {}
221 
222 template <typename ValueType>
224 {
225  interactionTypes = types;
226 }
227 
228 template <typename ValueType>
230 {
231  return interactionTypes;
232 }
233 
234 template <typename ValueType>
235 void CDPL::ForceField::MMFF94GradientCalculator<ValueType>::setup(const MMFF94InteractionData& ia_data, std::size_t num_atoms)
236 {
237  interactionData = &ia_data;
238  numAtoms = num_atoms;
239 }
240 
241 template <typename ValueType>
242 template <typename CoordsArray>
243 const ValueType& CDPL::ForceField::MMFF94GradientCalculator<ValueType>::operator()(const CoordsArray& coords)
244 {
245  if (!interactionData) {
246  totalEnergy = ValueType();
247  bondStretchingEnergy = ValueType();
248  angleBendingEnergy = ValueType();
249  stretchBendEnergy = ValueType();
250  outOfPlaneEnergy = ValueType();
251  torsionEnergy = ValueType();
252  electrostaticEnergy = ValueType();
253  vanDerWaalsEnergy = ValueType();
254 
255  return totalEnergy;
256  }
257 
258  totalEnergy = ValueType();
259 
260  if (interactionTypes & InteractionType::BOND_STRETCHING) {
261  bondStretchingEnergy = calcMMFF94BondStretchingEnergy<ValueType>(interactionData->getBondStretchingInteractions().getElementsBegin(),
262  interactionData->getBondStretchingInteractions().getElementsEnd(),
263  coords);
264  totalEnergy += bondStretchingEnergy;
265 
266  } else
267  bondStretchingEnergy = ValueType();
268 
269 
270  if (interactionTypes & InteractionType::ANGLE_BENDING) {
271  angleBendingEnergy = calcMMFF94AngleBendingEnergy<ValueType>(interactionData->getAngleBendingInteractions().getElementsBegin(),
272  interactionData->getAngleBendingInteractions().getElementsEnd(),
273  coords);
274  totalEnergy += angleBendingEnergy;
275 
276  } else
277  angleBendingEnergy = ValueType();
278 
279  if (interactionTypes & InteractionType::STRETCH_BEND) {
280  stretchBendEnergy = calcMMFF94StretchBendEnergy<ValueType>(interactionData->getStretchBendInteractions().getElementsBegin(),
281  interactionData->getStretchBendInteractions().getElementsEnd(),
282  coords);
283  totalEnergy += stretchBendEnergy;
284 
285  } else
286  stretchBendEnergy = ValueType();
287 
288  if (interactionTypes & InteractionType::OUT_OF_PLANE_BENDING) {
289  outOfPlaneEnergy = calcMMFF94OutOfPlaneBendingEnergy<ValueType>(interactionData->getOutOfPlaneBendingInteractions().getElementsBegin(),
290  interactionData->getOutOfPlaneBendingInteractions().getElementsEnd(),
291  coords);
292  totalEnergy += outOfPlaneEnergy;
293 
294  } else
295  outOfPlaneEnergy = ValueType();
296 
297  if (interactionTypes & InteractionType::TORSION) {
298  torsionEnergy = calcMMFF94TorsionEnergy<ValueType>(interactionData->getTorsionInteractions().getElementsBegin(),
299  interactionData->getTorsionInteractions().getElementsEnd(),
300  coords);
301  totalEnergy += torsionEnergy;
302 
303  } else
304  torsionEnergy = ValueType();
305 
306  if (interactionTypes & InteractionType::ELECTROSTATIC) {
307  electrostaticEnergy = calcMMFF94ElectrostaticEnergy<ValueType>(interactionData->getElectrostaticInteractions().getElementsBegin(),
308  interactionData->getElectrostaticInteractions().getElementsEnd(),
309  coords);
310  totalEnergy += electrostaticEnergy;
311 
312  } else
313  electrostaticEnergy = ValueType();
314 
315  if (interactionTypes & InteractionType::VAN_DER_WAALS) {
316  vanDerWaalsEnergy = calcMMFF94VanDerWaalsEnergy<ValueType>(interactionData->getVanDerWaalsInteractions().getElementsBegin(),
317  interactionData->getVanDerWaalsInteractions().getElementsEnd(),
318  coords);
319  totalEnergy += vanDerWaalsEnergy;
320 
321  } else
322  vanDerWaalsEnergy = ValueType();
323 
324  return totalEnergy;
325 }
326 
327 template <typename ValueType>
328 template <typename CoordsArray, typename GradVector>
329 const ValueType& CDPL::ForceField::MMFF94GradientCalculator<ValueType>::operator()(const CoordsArray& coords, GradVector& grad)
330 {
332 
333  if (!interactionData) {
334  totalEnergy = ValueType();
335  bondStretchingEnergy = ValueType();
336  angleBendingEnergy = ValueType();
337  stretchBendEnergy = ValueType();
338  outOfPlaneEnergy = ValueType();
339  torsionEnergy = ValueType();
340  electrostaticEnergy = ValueType();
341  vanDerWaalsEnergy = ValueType();
342 
343  return totalEnergy;
344  }
345 
346  totalEnergy = ValueType();
347 
348  if (interactionTypes & InteractionType::BOND_STRETCHING) {
349  bondStretchingEnergy = calcMMFF94BondStretchingGradient<ValueType>(interactionData->getBondStretchingInteractions().getElementsBegin(),
350  interactionData->getBondStretchingInteractions().getElementsEnd(),
351  coords, grad);
352  totalEnergy += bondStretchingEnergy;
353 
354  } else
355  bondStretchingEnergy = ValueType();
356 
357  if (interactionTypes & InteractionType::ANGLE_BENDING) {
358  angleBendingEnergy = calcMMFF94AngleBendingGradient<ValueType>(interactionData->getAngleBendingInteractions().getElementsBegin(),
359  interactionData->getAngleBendingInteractions().getElementsEnd(),
360  coords, grad);
361  totalEnergy += angleBendingEnergy;
362 
363  } else
364  angleBendingEnergy = ValueType();
365 
366  if (interactionTypes & InteractionType::STRETCH_BEND) {
367  stretchBendEnergy = calcMMFF94StretchBendGradient<ValueType>(interactionData->getStretchBendInteractions().getElementsBegin(),
368  interactionData->getStretchBendInteractions().getElementsEnd(),
369  coords, grad);
370  totalEnergy += stretchBendEnergy;
371 
372  } else
373  stretchBendEnergy = ValueType();
374 
375  if (interactionTypes & InteractionType::OUT_OF_PLANE_BENDING) {
376  outOfPlaneEnergy = calcMMFF94OutOfPlaneBendingGradient<ValueType>(interactionData->getOutOfPlaneBendingInteractions().getElementsBegin(),
377  interactionData->getOutOfPlaneBendingInteractions().getElementsEnd(),
378  coords, grad);
379  totalEnergy += outOfPlaneEnergy;
380 
381  } else
382  outOfPlaneEnergy = ValueType();
383 
384  if (interactionTypes & InteractionType::TORSION) {
385  torsionEnergy = calcMMFF94TorsionGradient<ValueType>(interactionData->getTorsionInteractions().getElementsBegin(),
386  interactionData->getTorsionInteractions().getElementsEnd(),
387  coords, grad);
388  totalEnergy += torsionEnergy;
389 
390  } else
391  torsionEnergy = ValueType();
392 
393  if (interactionTypes & InteractionType::ELECTROSTATIC) {
394  electrostaticEnergy = calcMMFF94ElectrostaticGradient<ValueType>(interactionData->getElectrostaticInteractions().getElementsBegin(),
395  interactionData->getElectrostaticInteractions().getElementsEnd(),
396  coords, grad);
397  totalEnergy += electrostaticEnergy;
398 
399  } else
400  electrostaticEnergy = ValueType();
401 
402  if (interactionTypes & InteractionType::VAN_DER_WAALS) {
403  vanDerWaalsEnergy = calcMMFF94VanDerWaalsGradient<ValueType>(interactionData->getVanDerWaalsInteractions().getElementsBegin(),
404  interactionData->getVanDerWaalsInteractions().getElementsEnd(),
405  coords, grad);
406  totalEnergy += vanDerWaalsEnergy;
407 
408  } else
409  vanDerWaalsEnergy = ValueType();
410 
411  if (!fixedAtomMask.empty())
412  for (Util::BitSet::size_type i = fixedAtomMask.find_first(); i != Util::BitSet::npos; i = fixedAtomMask.find_next(i))
413  grad[i].clear(ValueType());
414 
415  return totalEnergy;
416 }
417 
418 template <typename ValueType>
420 {
421  return totalEnergy;
422 }
423 
424 template <typename ValueType>
426 {
427  return bondStretchingEnergy;
428 }
429 
430 template <typename ValueType>
432 {
433  return angleBendingEnergy;
434 }
435 
436 template <typename ValueType>
438 {
439  return stretchBendEnergy;
440 }
441 
442 template <typename ValueType>
444 {
445  return outOfPlaneEnergy;
446 }
447 
448 template <typename ValueType>
450 {
451  return torsionEnergy;
452 }
453 
454 template <typename ValueType>
456 {
457  return electrostaticEnergy;
458 }
459 
460 template <typename ValueType>
462 {
463  return vanDerWaalsEnergy;
464 }
465 
466 template <typename ValueType>
468 {
469  return fixedAtomMask;
470 }
471 
472 template <typename ValueType>
474 {
475  fixedAtomMask = mask;
476 }
477 
478 template <typename ValueType>
480 {
481  fixedAtomMask.clear();
482 }
483 
484 // \endcond
485 
486 #endif // CDPL_FORCEFIELD_MMFF94GRADIENTCALCULATOR_HPP
Declaration of type CDPL::Util::BitSet.
Definition of class CDPL::ForceField::GradientVectorTraits.
Definition of constants in namespace CDPL::ForceField::InteractionType.
Functions for the calculation of MMFF94 interaction energies.
Functions for the calculation of MMFF94 interaction energy gradients.
Definition of class CDPL::ForceField::MMFF94InteractionData.
Calculates the total MMFF94 force field energy and its gradient for a set of atom 3D coordinates.
Definition: MMFF94GradientCalculator.hpp:62
const ValueType & operator()(const CoordsArray &coords, GradVector &grad)
Computes the total MMFF94 energy and the per-atom gradient of the conformation specified by coords.
const ValueType & operator()(const CoordsArray &coords)
Computes the total MMFF94 energy of the conformation specified by coords without calculating the grad...
void setup(const MMFF94InteractionData &ia_data, std::size_t num_atoms)
Associates the calculator with the supplied ForceField::MMFF94InteractionData instance and atom count...
void setFixedAtomMask(const Util::BitSet &mask)
Sets the bit mask flagging atoms whose gradient components shall be zeroed after calculation.
unsigned int getEnabledInteractionTypes() const
Returns the currently enabled interaction-type contributions.
void setEnabledInteractionTypes(unsigned int types)
Enables/disables specific MMFF94 interaction-type contributions.
const ValueType & getElectrostaticEnergy() const
Returns the electrostatic energy contribution computed by the most recent operator()() call.
void resetFixedAtomMask()
Clears the fixed-atom mask so that all atoms contribute to the gradient.
MMFF94GradientCalculator(const MMFF94InteractionData &ia_data, std::size_t num_atoms)
Constructs the calculator and associates it with the supplied ForceField::MMFF94InteractionData insta...
const ValueType & getStretchBendEnergy() const
Returns the stretch-bend coupling energy contribution computed by the most recent operator()() call.
const ValueType & getVanDerWaalsEnergy() const
Returns the Van der Waals energy contribution computed by the most recent operator()() call.
const ValueType & getAngleBendingEnergy() const
Returns the angle-bending energy contribution computed by the most recent operator()() call.
MMFF94GradientCalculator()
Constructs the calculator without an associated ForceField::MMFF94InteractionData instance.
const ValueType & getOutOfPlaneBendingEnergy() const
Returns the out-of-plane bending energy contribution computed by the most recent operator()() call.
const ValueType & getTotalEnergy() const
Returns the total MMFF94 energy computed by the most recent operator()() call.
const ValueType & getTorsionEnergy() const
Returns the torsion energy contribution computed by the most recent operator()() call.
const Util::BitSet & getFixedAtomMask() const
Returns the bit mask flagging atoms whose gradient components are zeroed after evaluation.
const ValueType & getBondStretchingEnergy() const
Returns the bond-stretching energy contribution computed by the most recent operator()() call.
Container holding the full set of MMFF94 interaction parameter records for a molecular graph.
Definition: MMFF94InteractionData.hpp:62
constexpr unsigned int ALL
Specifies reactants, agents and products of a reaction.
Definition: ReactionRole.hpp:74
constexpr unsigned int STRETCH_BEND
Specifies a stretch-bend coupling interaction.
Definition: InteractionType.hpp:63
constexpr unsigned int TORSION
Specifies a torsion interaction.
Definition: InteractionType.hpp:73
constexpr unsigned int VAN_DER_WAALS
Specifies a Van der Waals interaction.
Definition: InteractionType.hpp:78
constexpr unsigned int BOND_STRETCHING
Specifies a bond stretching interaction.
Definition: InteractionType.hpp:53
constexpr unsigned int ELECTROSTATIC
Specifies an electrostatic interaction.
Definition: InteractionType.hpp:83
constexpr unsigned int OUT_OF_PLANE_BENDING
Specifies an out-of-plane bending interaction.
Definition: InteractionType.hpp:68
constexpr unsigned int ANGLE_BENDING
Specifies an angle bending interaction.
Definition: InteractionType.hpp:58
boost::dynamic_bitset BitSet
Dynamic bitset class.
Definition: BitSet.hpp:46
The namespace of the Chemical Data Processing Library.
static void clear(VectorType &g, std::size_t num_elem)
Zeroes the first num_elem elements of the container g.
Definition: GradientVectorTraits.hpp:71