Chemical Data Processing Library C++ API - Version 1.4.0
ElasticPotentialFunctions.hpp
Go to the documentation of this file.
1 /*
2  * ElasticPotentialFunctions.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_ELASTICPOTENTIALFUNCTIONS_HPP
30 #define CDPL_FORCEFIELD_ELASTICPOTENTIALFUNCTIONS_HPP
31 
35 #include "CDPL/Util/BitSet.hpp"
36 
37 
38 namespace CDPL
39 {
40 
41  namespace ForceField
42  {
43 
56  template <typename ValueType, typename Iter, typename CoordsArray>
57  ValueType calcElasticPotentialEnergy(Iter beg, const Iter& end, const CoordsArray& coords);
58 
68  template <typename ValueType, typename CoordsArray>
69  ValueType calcElasticPotentialEnergy(const ElasticPotential& pot, const CoordsArray& coords);
70 
88  template <typename ValueType, typename CoordsVec>
89  ValueType calcElasticPotentialEnergy(const CoordsVec& atom1_pos, const CoordsVec& atom2_pos,
90  const ValueType& force_const, const ValueType& ref_length);
91 
107  template <typename ValueType, typename Iter, typename CoordsArray, typename GradVector>
108  ValueType calcElasticPotentialGradient(Iter beg, const Iter& end, const CoordsArray& coords, GradVector& grad);
109 
122  template <typename ValueType, typename CoordsArray, typename GradVector>
123  ValueType calcElasticPotentialGradient(const ElasticPotential& pot, const CoordsArray& coords, GradVector& grad);
124 
157  template <typename ValueType, typename CoordsVec, typename GradVec>
158  ValueType calcElasticPotentialGradient(const CoordsVec& atom1_pos, const CoordsVec& atom2_pos, GradVec& atom1_grad, GradVec& atom2_grad,
159  const ValueType& force_const, const ValueType& ref_length);
160  } // namespace ForceField
161 } // namespace CDPL
162 
163 
164 // Implementation
165 // \cond DOC_IMPL_DETAILS
166 
167 template <typename ValueType, typename Iter, typename CoordsArray>
168 ValueType CDPL::ForceField::calcElasticPotentialEnergy(Iter beg, const Iter& end, const CoordsArray& coords)
169 {
170  return Detail::accumInteractionEnergies<ValueType>(beg, end, coords,
171  static_cast<ValueType (*)(const ElasticPotential&, const CoordsArray&)>(
172  &calcElasticPotentialEnergy<ValueType, CoordsArray>));
173 }
174 
175 template <typename ValueType, typename CoordsArray>
176 ValueType CDPL::ForceField::calcElasticPotentialEnergy(const ElasticPotential& pot, const CoordsArray& coords)
177 {
178  return calcElasticPotentialEnergy<ValueType>(coords[pot.getAtom1Index()], coords[pot.getAtom2Index()],
179  pot.getForceConstant(), pot.getReferenceLength());
180 }
181 
182 template <typename ValueType, typename CoordsVec>
183 ValueType CDPL::ForceField::calcElasticPotentialEnergy(const CoordsVec& atom1_pos, const CoordsVec& atom2_pos,
184  const ValueType& force_const, const ValueType& ref_length)
185 {
186  ValueType dr_ij = calcDistance<ValueType>(atom1_pos, atom2_pos) - ref_length;
187  ValueType e = force_const * dr_ij * dr_ij;
188 
189  return e;
190 }
191 
192 
193 template <typename ValueType, typename Iter, typename CoordsArray, typename GradVector>
194 ValueType CDPL::ForceField::calcElasticPotentialGradient(Iter beg, const Iter& end, const CoordsArray& coords, GradVector& grad)
195 {
196  return Detail::calcInteractionGradient<ValueType>(beg, end, coords, grad,
197  static_cast<ValueType (*)(const ElasticPotential&, const CoordsArray&, GradVector&)>(
198  &calcElasticPotentialGradient<ValueType, CoordsArray, GradVector>));
199 }
200 
201 template <typename ValueType, typename CoordsArray, typename GradVector>
202 ValueType CDPL::ForceField::calcElasticPotentialGradient(const ElasticPotential& pot, const CoordsArray& coords, GradVector& grad)
203 {
204  return calcElasticPotentialGradient<ValueType>(coords[pot.getAtom1Index()], coords[pot.getAtom2Index()], grad[pot.getAtom1Index()],
205  grad[pot.getAtom2Index()], pot.getForceConstant(), pot.getReferenceLength());
206 }
207 
208 template <typename ValueType, typename CoordsVec, typename GradVec>
209 ValueType CDPL::ForceField::calcElasticPotentialGradient(const CoordsVec& atom1_pos, const CoordsVec& atom2_pos, GradVec& atom1_grad, GradVec& atom2_grad,
210  const ValueType& force_const, const ValueType& ref_length)
211 {
212  ValueType dist_atom1_grad[3];
213  ValueType dist_atom2_grad[3];
214 
215  ValueType dr_ij = calcDistanceDerivatives<ValueType>(atom1_pos, atom2_pos, dist_atom1_grad, dist_atom2_grad) - ref_length;
216  ValueType grad_fact = ValueType(2) * force_const * dr_ij;
217 
218  Detail::scaleAddVector(dist_atom1_grad, grad_fact, atom1_grad);
219  Detail::scaleAddVector(dist_atom2_grad, grad_fact, atom2_grad);
220 
221  ValueType e = force_const * dr_ij * dr_ij;
222 
223  return e;
224 }
225 
226 // \endcond
227 
228 #endif // CDPL_FORCEFIELD_ELASTICPOTENTIALENERGYFUNCTIONS_HPP
Declaration of type CDPL::Util::BitSet.
Definition of class CDPL::ForceField::ElasticPotential.
Definition of the preprocessor macro CDPL_FORCEFIELD_API.
Utility functions used in the calculation of force field energies and gradients.
Stores parameters for an elastic potential (harmonic distance restraint) between a pair of atoms.
Definition: ElasticPotential.hpp:47
ValueType calcElasticPotentialEnergy(Iter beg, const Iter &end, const CoordsArray &coords)
Accumulates the energies calculated for the elastic potentials specified by the iterators in the give...
ValueType calcElasticPotentialGradient(Iter beg, const Iter &end, const CoordsArray &coords, GradVector &grad)
Accumulates the energies and gradient contributions calculated for the elastic potentials specified b...
The namespace of the Chemical Data Processing Library.