Chemical Data Processing Library C++ API - Version 1.4.0
RDFCodeCalculator.hpp
Go to the documentation of this file.
1 /*
2  * RDFCodeCalculator.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_DESCR_RDFCODECALCULATOR_HPP
30 #define CDPL_DESCR_RDFCODECALCULATOR_HPP
31 
32 #include <cstddef>
33 #include <iterator>
34 #include <cmath>
35 #include <functional>
36 
37 #include "CDPL/Math/Matrix.hpp"
38 #include "CDPL/Math/Vector.hpp"
39 
40 
41 namespace CDPL
42 {
43 
44  namespace Descr
45  {
46 
59  template <typename T>
61  {
62 
63  public:
64  typedef T EntityType;
65 
73  typedef std::function<double(const EntityType&, const EntityType&)> EntityPairWeightFunction;
74 
82  typedef std::function<const Math::Vector3D&(const EntityType&)> Entity3DCoordinatesFunction;
83 
88 
95  void setSmoothingFactor(double factor);
96 
102  double getSmoothingFactor() const;
103 
109  void setScalingFactor(double factor);
110 
115  double getScalingFactor() const;
116 
122  void setStartRadius(double start_radius);
123 
128  double getStartRadius() const;
129 
135  void setRadiusIncrement(double radius_inc);
136 
141  double getRadiusIncrement() const;
142 
152  void setNumSteps(std::size_t num_steps);
153 
158  std::size_t getNumSteps() const;
159 
166 
174 
181 
187 
201  template <typename Iter, typename Vec>
202  void calculate(Iter beg, Iter end, Vec& rdf_code);
203 
204  private:
205  template <typename Iter>
206  void init(Iter beg, Iter end);
207 
208  double smoothingFactor;
209  double scalingFactor;
210  double startRadius;
211  double radiusIncrement;
212  std::size_t numSteps;
213  std::size_t numEntities;
214  EntityPairWeightFunction weightFunc;
215  Entity3DCoordinatesFunction coordsFunc;
216  bool distToIntervalCenterRounding;
217  Math::DMatrix weightMatrix;
218  Math::DMatrix distMatrix;
219  };
220  } // namespace Descr
221 } // namespace CDPL
222 
223 
224 // Implementation
225 
226 template <typename T>
228  smoothingFactor(1.0), scalingFactor(1.0), startRadius(0.0), radiusIncrement(0.1), numSteps(99),
229  distToIntervalCenterRounding(false)
230 {}
231 
232 template <typename T>
234 {
235  smoothingFactor = factor;
236 }
237 
238 template <typename T>
240 {
241  scalingFactor = factor;
242 }
243 
244 template <typename T>
246 {
247  startRadius = start_radius;
248 }
249 
250 template <typename T>
252 {
253  radiusIncrement = radius_inc;
254 }
255 
256 template <typename T>
258 {
259  numSteps = num_steps;
260 }
261 
262 template <typename T>
264 {
265  return smoothingFactor;
266 }
267 
268 template <typename T>
270 {
271  return scalingFactor;
272 }
273 
274 template <typename T>
276 {
277  return startRadius;
278 }
279 
280 template <typename T>
282 {
283  return radiusIncrement;
284 }
285 
286 template <typename T>
288 {
289  return numSteps;
290 }
291 
292 template <typename T>
294 {
295  weightFunc = func;
296 }
297 
298 template <typename T>
300 {
301  coordsFunc = func;
302 }
303 
304 template <typename T>
306 {
307  distToIntervalCenterRounding = enable;
308 }
309 
310 template <typename T>
312 {
313  return distToIntervalCenterRounding;
314 }
315 
316 template <typename T>
317 template <typename Iter, typename Vec>
318 void CDPL::Descr::RDFCodeCalculator<T>::calculate(Iter beg, Iter end, Vec& rdf_code)
319 {
320  init(beg, end);
321 
322  double r = startRadius;
323 
324  for (std::size_t i = 0; i <= numSteps; i++, r += radiusIncrement) {
325  double sum = 0.0;
326 
327  for (std::size_t j = 0; j < numEntities; j++) {
328  for (std::size_t k = j + 1; k < numEntities; k++) {
329  double t = r - distMatrix(j, k);
330 
331  sum += weightMatrix(j, k) * std::exp(-smoothingFactor * t * t);
332  }
333  }
334 
335  rdf_code[i] = scalingFactor * sum;
336  }
337 }
338 
339 template <typename T>
340 template <typename Iter>
341 void CDPL::Descr::RDFCodeCalculator<T>::init(Iter beg, Iter end)
342 {
343  numEntities = std::distance(beg, end);
344 
345  weightMatrix.resize(numEntities, numEntities, false);
346  distMatrix.resize(numEntities, numEntities, false);
347 
348  Math::Vector3D entity1_pos;
349 
350  for (std::size_t i = 0; beg != end; i++) {
351  const EntityType& entity1 = *beg;
352 
353  if (coordsFunc)
354  entity1_pos = coordsFunc(entity1);
355 
356  std::size_t j = i + 1;
357 
358  for (Iter it = ++beg; it != end; ++it, j++) {
359  const EntityType& entity2 = *it;
360 
361  if (!weightFunc)
362  weightMatrix(i, j) = 1.0;
363  else
364  weightMatrix(i, j) = weightFunc(entity1, entity2);
365 
366  if (coordsFunc) {
367  double dist = length(entity1_pos - coordsFunc(entity2));
368 
369  if (distToIntervalCenterRounding)
370  dist = std::round((dist - startRadius) / radiusIncrement) * radiusIncrement + startRadius;
371 
372  distMatrix(i, j) = dist;
373 
374  } else
375  distMatrix(i, j) = 0.0;
376  }
377  }
378 }
379 
380 #endif // CDPL_DESCR_RDFCODECALCULATOR_HPP
Definition of matrix data types.
Definition of vector data types.
Generic implementation of the radial distribution function (RDF) code calculation for sequences of en...
Definition: RDFCodeCalculator.hpp:61
void enableDistanceToIntervalCenterRounding(bool enable)
Allows to specify whether entity pair distances should be rounded to the nearest radius interval cent...
Definition: RDFCodeCalculator.hpp:305
void setSmoothingFactor(double factor)
Allows to specify the smoothing factor used in the calculation of entity pair RDF contributions.
Definition: RDFCodeCalculator.hpp:233
double getRadiusIncrement() const
Returns the radius step size between successive RDF code elements.
Definition: RDFCodeCalculator.hpp:281
void setNumSteps(std::size_t num_steps)
Sets the number of desired radius incrementation steps.
Definition: RDFCodeCalculator.hpp:257
void setEntity3DCoordinatesFunction(const Entity3DCoordinatesFunction &func)
Allows to specify the entity 3D coordinates function.
Definition: RDFCodeCalculator.hpp:299
void setRadiusIncrement(double radius_inc)
Sets the radius step size between successive RDF code elements.
Definition: RDFCodeCalculator.hpp:251
T EntityType
Definition: RDFCodeCalculator.hpp:64
bool distanceToIntervalsCenterRoundingEnabled() const
Tells whether entity pair distances get rounded to the nearest radius interval centers.
Definition: RDFCodeCalculator.hpp:311
void calculate(Iter beg, Iter end, Vec &rdf_code)
Calculates the RDF code of an entity sequence.
Definition: RDFCodeCalculator.hpp:318
std::size_t getNumSteps() const
Returns the number of performed radius incrementation steps.
Definition: RDFCodeCalculator.hpp:287
double getStartRadius() const
Returns the starting value of the radius.
Definition: RDFCodeCalculator.hpp:275
void setScalingFactor(double factor)
Allows to specify the scaling factor for the RDF code elements.
Definition: RDFCodeCalculator.hpp:239
void setStartRadius(double start_radius)
Sets the starting value of the radius.
Definition: RDFCodeCalculator.hpp:245
double getSmoothingFactor() const
Returns the smoothing factor used in the calculation of entity pair RDF contributions.
Definition: RDFCodeCalculator.hpp:263
std::function< const Math::Vector3D &(const EntityType &)> Entity3DCoordinatesFunction
Type of the generic functor class used to store a user-defined entity 3D coordinates function.
Definition: RDFCodeCalculator.hpp:82
RDFCodeCalculator()
Constructs the RDFCodeCalculator instance.
Definition: RDFCodeCalculator.hpp:227
std::function< double(const EntityType &, const EntityType &)> EntityPairWeightFunction
Type of the generic functor class used to store a user-defined entity pair weight function.
Definition: RDFCodeCalculator.hpp:73
void setEntityPairWeightFunction(const EntityPairWeightFunction &func)
Allows to specify a custom entity pair weight function.
Definition: RDFCodeCalculator.hpp:293
double getScalingFactor() const
Returns the scaling factor applied to the RDF code elements.
Definition: RDFCodeCalculator.hpp:269
constexpr unsigned int T
Specifies Hydrogen (Tritium).
Definition: AtomType.hpp:67
constexpr unsigned int r
Specifies that the stereocenter has r configuration.
Definition: CIPDescriptor.hpp:76
VectorNorm2< E >::ResultType length(const VectorExpression< E > &e)
Definition: VectorExpression.hpp:553
GridElementSum< E >::ResultType sum(const GridExpression< E > &e)
Definition: GridExpression.hpp:416
CVector< double, 3 > Vector3D
A bounded 3 element vector holding floating point values of type double.
Definition: Vector.hpp:1637
The namespace of the Chemical Data Processing Library.