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 
59  template <typename ValueType>
61  {
62 
63  public:
70 
76  MMFF94GradientCalculator(const MMFF94InteractionData& ia_data, std::size_t num_atoms);
77 
83  void setEnabledInteractionTypes(unsigned int types);
84 
89  unsigned int getEnabledInteractionTypes() const;
90 
96  void setup(const MMFF94InteractionData& ia_data, std::size_t num_atoms);
97 
104  template <typename CoordsArray>
105  const ValueType& operator()(const CoordsArray& coords);
106 
118  template <typename CoordsArray, typename GradVector>
119  const ValueType& operator()(const CoordsArray& coords, GradVector& grad);
120 
125  const ValueType& getTotalEnergy() const;
126 
131  const ValueType& getBondStretchingEnergy() const;
132 
137  const ValueType& getAngleBendingEnergy() const;
138 
143  const ValueType& getStretchBendEnergy() const;
144 
149  const ValueType& getOutOfPlaneBendingEnergy() const;
150 
155  const ValueType& getTorsionEnergy() const;
156 
161  const ValueType& getElectrostaticEnergy() const;
162 
167  const ValueType& getVanDerWaalsEnergy() const;
168 
174 
179  void setFixedAtomMask(const Util::BitSet& mask);
180 
185 
186  private:
187  const MMFF94InteractionData* interactionData;
188  std::size_t numAtoms;
189  ValueType totalEnergy;
190  ValueType bondStretchingEnergy;
191  ValueType angleBendingEnergy;
192  ValueType stretchBendEnergy;
193  ValueType outOfPlaneEnergy;
194  ValueType torsionEnergy;
195  ValueType electrostaticEnergy;
196  ValueType vanDerWaalsEnergy;
197  unsigned int interactionTypes;
198  Util::BitSet fixedAtomMask;
199  };
200  } // namespace ForceField
201 } // namespace CDPL
202 
203 
204 // Implementation
205 // \cond DOC_IMPL_DETAILS
206 
207 template <typename ValueType>
209  interactionData(0), numAtoms(0), totalEnergy(), bondStretchingEnergy(), angleBendingEnergy(),
210  stretchBendEnergy(), outOfPlaneEnergy(), torsionEnergy(), electrostaticEnergy(),
211  vanDerWaalsEnergy(), interactionTypes(InteractionType::ALL)
212 {}
213 
214 template <typename ValueType>
215 CDPL::ForceField::MMFF94GradientCalculator<ValueType>::MMFF94GradientCalculator(const MMFF94InteractionData& ia_data, std::size_t num_atoms):
216  interactionData(&ia_data), numAtoms(num_atoms), totalEnergy(), bondStretchingEnergy(), angleBendingEnergy(),
217  stretchBendEnergy(), outOfPlaneEnergy(), torsionEnergy(), electrostaticEnergy(),
218  vanDerWaalsEnergy(), interactionTypes(InteractionType::ALL)
219 {}
220 
221 template <typename ValueType>
223 {
224  interactionTypes = types;
225 }
226 
227 template <typename ValueType>
229 {
230  return interactionTypes;
231 }
232 
233 template <typename ValueType>
234 void CDPL::ForceField::MMFF94GradientCalculator<ValueType>::setup(const MMFF94InteractionData& ia_data, std::size_t num_atoms)
235 {
236  interactionData = &ia_data;
237  numAtoms = num_atoms;
238 }
239 
240 template <typename ValueType>
241 template <typename CoordsArray>
242 const ValueType& CDPL::ForceField::MMFF94GradientCalculator<ValueType>::operator()(const CoordsArray& coords)
243 {
244  if (!interactionData) {
245  totalEnergy = ValueType();
246  bondStretchingEnergy = ValueType();
247  angleBendingEnergy = ValueType();
248  stretchBendEnergy = ValueType();
249  outOfPlaneEnergy = ValueType();
250  torsionEnergy = ValueType();
251  electrostaticEnergy = ValueType();
252  vanDerWaalsEnergy = ValueType();
253 
254  return totalEnergy;
255  }
256 
257  totalEnergy = ValueType();
258 
259  if (interactionTypes & InteractionType::BOND_STRETCHING) {
260  bondStretchingEnergy = calcMMFF94BondStretchingEnergy<ValueType>(interactionData->getBondStretchingInteractions().getElementsBegin(),
261  interactionData->getBondStretchingInteractions().getElementsEnd(),
262  coords);
263  totalEnergy += bondStretchingEnergy;
264 
265  } else
266  bondStretchingEnergy = ValueType();
267 
268 
269  if (interactionTypes & InteractionType::ANGLE_BENDING) {
270  angleBendingEnergy = calcMMFF94AngleBendingEnergy<ValueType>(interactionData->getAngleBendingInteractions().getElementsBegin(),
271  interactionData->getAngleBendingInteractions().getElementsEnd(),
272  coords);
273  totalEnergy += angleBendingEnergy;
274 
275  } else
276  angleBendingEnergy = ValueType();
277 
278  if (interactionTypes & InteractionType::STRETCH_BEND) {
279  stretchBendEnergy = calcMMFF94StretchBendEnergy<ValueType>(interactionData->getStretchBendInteractions().getElementsBegin(),
280  interactionData->getStretchBendInteractions().getElementsEnd(),
281  coords);
282  totalEnergy += stretchBendEnergy;
283 
284  } else
285  stretchBendEnergy = ValueType();
286 
287  if (interactionTypes & InteractionType::OUT_OF_PLANE_BENDING) {
288  outOfPlaneEnergy = calcMMFF94OutOfPlaneBendingEnergy<ValueType>(interactionData->getOutOfPlaneBendingInteractions().getElementsBegin(),
289  interactionData->getOutOfPlaneBendingInteractions().getElementsEnd(),
290  coords);
291  totalEnergy += outOfPlaneEnergy;
292 
293  } else
294  outOfPlaneEnergy = ValueType();
295 
296  if (interactionTypes & InteractionType::TORSION) {
297  torsionEnergy = calcMMFF94TorsionEnergy<ValueType>(interactionData->getTorsionInteractions().getElementsBegin(),
298  interactionData->getTorsionInteractions().getElementsEnd(),
299  coords);
300  totalEnergy += torsionEnergy;
301 
302  } else
303  torsionEnergy = ValueType();
304 
305  if (interactionTypes & InteractionType::ELECTROSTATIC) {
306  electrostaticEnergy = calcMMFF94ElectrostaticEnergy<ValueType>(interactionData->getElectrostaticInteractions().getElementsBegin(),
307  interactionData->getElectrostaticInteractions().getElementsEnd(),
308  coords);
309  totalEnergy += electrostaticEnergy;
310 
311  } else
312  electrostaticEnergy = ValueType();
313 
314  if (interactionTypes & InteractionType::VAN_DER_WAALS) {
315  vanDerWaalsEnergy = calcMMFF94VanDerWaalsEnergy<ValueType>(interactionData->getVanDerWaalsInteractions().getElementsBegin(),
316  interactionData->getVanDerWaalsInteractions().getElementsEnd(),
317  coords);
318  totalEnergy += vanDerWaalsEnergy;
319 
320  } else
321  vanDerWaalsEnergy = ValueType();
322 
323  return totalEnergy;
324 }
325 
326 template <typename ValueType>
327 template <typename CoordsArray, typename GradVector>
328 const ValueType& CDPL::ForceField::MMFF94GradientCalculator<ValueType>::operator()(const CoordsArray& coords, GradVector& grad)
329 {
331 
332  if (!interactionData) {
333  totalEnergy = ValueType();
334  bondStretchingEnergy = ValueType();
335  angleBendingEnergy = ValueType();
336  stretchBendEnergy = ValueType();
337  outOfPlaneEnergy = ValueType();
338  torsionEnergy = ValueType();
339  electrostaticEnergy = ValueType();
340  vanDerWaalsEnergy = ValueType();
341 
342  return totalEnergy;
343  }
344 
345  totalEnergy = ValueType();
346 
347  if (interactionTypes & InteractionType::BOND_STRETCHING) {
348  bondStretchingEnergy = calcMMFF94BondStretchingGradient<ValueType>(interactionData->getBondStretchingInteractions().getElementsBegin(),
349  interactionData->getBondStretchingInteractions().getElementsEnd(),
350  coords, grad);
351  totalEnergy += bondStretchingEnergy;
352 
353  } else
354  bondStretchingEnergy = ValueType();
355 
356  if (interactionTypes & InteractionType::ANGLE_BENDING) {
357  angleBendingEnergy = calcMMFF94AngleBendingGradient<ValueType>(interactionData->getAngleBendingInteractions().getElementsBegin(),
358  interactionData->getAngleBendingInteractions().getElementsEnd(),
359  coords, grad);
360  totalEnergy += angleBendingEnergy;
361 
362  } else
363  angleBendingEnergy = ValueType();
364 
365  if (interactionTypes & InteractionType::STRETCH_BEND) {
366  stretchBendEnergy = calcMMFF94StretchBendGradient<ValueType>(interactionData->getStretchBendInteractions().getElementsBegin(),
367  interactionData->getStretchBendInteractions().getElementsEnd(),
368  coords, grad);
369  totalEnergy += stretchBendEnergy;
370 
371  } else
372  stretchBendEnergy = ValueType();
373 
374  if (interactionTypes & InteractionType::OUT_OF_PLANE_BENDING) {
375  outOfPlaneEnergy = calcMMFF94OutOfPlaneBendingGradient<ValueType>(interactionData->getOutOfPlaneBendingInteractions().getElementsBegin(),
376  interactionData->getOutOfPlaneBendingInteractions().getElementsEnd(),
377  coords, grad);
378  totalEnergy += outOfPlaneEnergy;
379 
380  } else
381  outOfPlaneEnergy = ValueType();
382 
383  if (interactionTypes & InteractionType::TORSION) {
384  torsionEnergy = calcMMFF94TorsionGradient<ValueType>(interactionData->getTorsionInteractions().getElementsBegin(),
385  interactionData->getTorsionInteractions().getElementsEnd(),
386  coords, grad);
387  totalEnergy += torsionEnergy;
388 
389  } else
390  torsionEnergy = ValueType();
391 
392  if (interactionTypes & InteractionType::ELECTROSTATIC) {
393  electrostaticEnergy = calcMMFF94ElectrostaticGradient<ValueType>(interactionData->getElectrostaticInteractions().getElementsBegin(),
394  interactionData->getElectrostaticInteractions().getElementsEnd(),
395  coords, grad);
396  totalEnergy += electrostaticEnergy;
397 
398  } else
399  electrostaticEnergy = ValueType();
400 
401  if (interactionTypes & InteractionType::VAN_DER_WAALS) {
402  vanDerWaalsEnergy = calcMMFF94VanDerWaalsGradient<ValueType>(interactionData->getVanDerWaalsInteractions().getElementsBegin(),
403  interactionData->getVanDerWaalsInteractions().getElementsEnd(),
404  coords, grad);
405  totalEnergy += vanDerWaalsEnergy;
406 
407  } else
408  vanDerWaalsEnergy = ValueType();
409 
410  if (!fixedAtomMask.empty())
411  for (Util::BitSet::size_type i = fixedAtomMask.find_first(); i != Util::BitSet::npos; i = fixedAtomMask.find_next(i))
412  grad[i].clear(ValueType());
413 
414  return totalEnergy;
415 }
416 
417 template <typename ValueType>
419 {
420  return totalEnergy;
421 }
422 
423 template <typename ValueType>
425 {
426  return bondStretchingEnergy;
427 }
428 
429 template <typename ValueType>
431 {
432  return angleBendingEnergy;
433 }
434 
435 template <typename ValueType>
437 {
438  return stretchBendEnergy;
439 }
440 
441 template <typename ValueType>
443 {
444  return outOfPlaneEnergy;
445 }
446 
447 template <typename ValueType>
449 {
450  return torsionEnergy;
451 }
452 
453 template <typename ValueType>
455 {
456  return electrostaticEnergy;
457 }
458 
459 template <typename ValueType>
461 {
462  return vanDerWaalsEnergy;
463 }
464 
465 template <typename ValueType>
467 {
468  return fixedAtomMask;
469 }
470 
471 template <typename ValueType>
473 {
474  fixedAtomMask = mask;
475 }
476 
477 template <typename ValueType>
479 {
480  fixedAtomMask.clear();
481 }
482 
483 // \endcond
484 
485 #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:61
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 parameters for a molecular graph.
Definition: MMFF94InteractionData.hpp:60
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:66