Chemical Data Processing Library C++ API - Version 1.2.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 
51  template <typename T>
53  {
54 
55  public:
56  typedef T EntityType;
57 
65  typedef std::function<double(const EntityType&, const EntityType&)> EntityPairWeightFunction;
66 
74  typedef std::function<const Math::Vector3D&(const EntityType&)> Entity3DCoordinatesFunction;
75 
80 
87  void setSmoothingFactor(double factor);
88 
94  double getSmoothingFactor() const;
95 
101  void setScalingFactor(double factor);
102 
107  double getScalingFactor() const;
108 
114  void setStartRadius(double start_radius);
115 
120  double getStartRadius() const;
121 
127  void setRadiusIncrement(double radius_inc);
128 
133  double getRadiusIncrement() const;
134 
144  void setNumSteps(std::size_t num_steps);
145 
150  std::size_t getNumSteps() const;
151 
158 
166 
173 
179 
193  template <typename Iter, typename Vec>
194  void calculate(Iter beg, Iter end, Vec& rdf_code);
195 
196  private:
197  template <typename Iter>
198  void init(Iter beg, Iter end);
199 
200  double smoothingFactor;
201  double scalingFactor;
202  double startRadius;
203  double radiusIncrement;
204  std::size_t numSteps;
205  std::size_t numEntities;
206  EntityPairWeightFunction weightFunc;
207  Entity3DCoordinatesFunction coordsFunc;
208  bool distToIntervalCenterRounding;
209  Math::DMatrix weightMatrix;
210  Math::DMatrix distMatrix;
211  };
212  } // namespace Descr
213 } // namespace CDPL
214 
215 
216 // Implementation
217 
218 template <typename T>
220  smoothingFactor(1.0), scalingFactor(1.0), startRadius(0.0), radiusIncrement(0.1), numSteps(99),
221  distToIntervalCenterRounding(false)
222 {}
223 
224 template <typename T>
226 {
227  smoothingFactor = factor;
228 }
229 
230 template <typename T>
232 {
233  scalingFactor = factor;
234 }
235 
236 template <typename T>
238 {
239  startRadius = start_radius;
240 }
241 
242 template <typename T>
244 {
245  radiusIncrement = radius_inc;
246 }
247 
248 template <typename T>
250 {
251  numSteps = num_steps;
252 }
253 
254 template <typename T>
256 {
257  return smoothingFactor;
258 }
259 
260 template <typename T>
262 {
263  return scalingFactor;
264 }
265 
266 template <typename T>
268 {
269  return startRadius;
270 }
271 
272 template <typename T>
274 {
275  return radiusIncrement;
276 }
277 
278 template <typename T>
280 {
281  return numSteps;
282 }
283 
284 template <typename T>
286 {
287  weightFunc = func;
288 }
289 
290 template <typename T>
292 {
293  coordsFunc = func;
294 }
295 
296 template <typename T>
298 {
299  distToIntervalCenterRounding = enable;
300 }
301 
302 template <typename T>
304 {
305  return distToIntervalCenterRounding;
306 }
307 
308 template <typename T>
309 template <typename Iter, typename Vec>
310 void CDPL::Descr::RDFCodeCalculator<T>::calculate(Iter beg, Iter end, Vec& rdf_code)
311 {
312  init(beg, end);
313 
314  double r = startRadius;
315 
316  for (std::size_t i = 0; i <= numSteps; i++, r += radiusIncrement) {
317  double sum = 0.0;
318 
319  for (std::size_t j = 0; j < numEntities; j++) {
320  for (std::size_t k = j + 1; k < numEntities; k++) {
321  double t = r - distMatrix(j, k);
322 
323  sum += weightMatrix(j, k) * std::exp(-smoothingFactor * t * t);
324  }
325  }
326 
327  rdf_code[i] = scalingFactor * sum;
328  }
329 }
330 
331 template <typename T>
332 template <typename Iter>
333 void CDPL::Descr::RDFCodeCalculator<T>::init(Iter beg, Iter end)
334 {
335  numEntities = std::distance(beg, end);
336 
337  weightMatrix.resize(numEntities, numEntities, false);
338  distMatrix.resize(numEntities, numEntities, false);
339 
340  Math::Vector3D entity1_pos;
341 
342  for (std::size_t i = 0; beg != end; i++) {
343  const EntityType& entity1 = *beg;
344 
345  if (coordsFunc)
346  entity1_pos = coordsFunc(entity1);
347 
348  std::size_t j = i + 1;
349 
350  for (Iter it = ++beg; it != end; ++it, j++) {
351  const EntityType& entity2 = *it;
352 
353  if (!weightFunc)
354  weightMatrix(i, j) = 1.0;
355  else
356  weightMatrix(i, j) = weightFunc(entity1, entity2);
357 
358  if (coordsFunc) {
359  double dist = length(entity1_pos - coordsFunc(entity2));
360 
361  if (distToIntervalCenterRounding)
362  dist = std::round((dist - startRadius) / radiusIncrement) * radiusIncrement + startRadius;
363 
364  distMatrix(i, j) = dist;
365 
366  } else
367  distMatrix(i, j) = 0.0;
368  }
369  }
370 }
371 
372 #endif // CDPL_DESCR_RDFCODECALCULATOR_HPP
Definition of matrix data types.
Definition of vector data types.
RDFCodeCalculator.
Definition: RDFCodeCalculator.hpp:53
void enableDistanceToIntervalCenterRounding(bool enable)
Allows to specify whether entity pair distances should be rounded to the nearest radius interval cent...
Definition: RDFCodeCalculator.hpp:297
void setSmoothingFactor(double factor)
Allows to specify the smoothing factor used in the calculation of entity pair RDF contributions.
Definition: RDFCodeCalculator.hpp:225
double getRadiusIncrement() const
Returns the radius step size between successive RDF code elements.
Definition: RDFCodeCalculator.hpp:273
void setNumSteps(std::size_t num_steps)
Sets the number of desired radius incrementation steps.
Definition: RDFCodeCalculator.hpp:249
void setEntity3DCoordinatesFunction(const Entity3DCoordinatesFunction &func)
Allows to specify the entity 3D coordinates function.
Definition: RDFCodeCalculator.hpp:291
void setRadiusIncrement(double radius_inc)
Sets the radius step size between successive RDF code elements.
Definition: RDFCodeCalculator.hpp:243
T EntityType
Definition: RDFCodeCalculator.hpp:56
bool distanceToIntervalsCenterRoundingEnabled() const
Tells whether entity pair distances get rounded to the nearest radius interval centers.
Definition: RDFCodeCalculator.hpp:303
void calculate(Iter beg, Iter end, Vec &rdf_code)
Calculates the RDF code of an entity sequence.
Definition: RDFCodeCalculator.hpp:310
std::size_t getNumSteps() const
Returns the number of performed radius incrementation steps.
Definition: RDFCodeCalculator.hpp:279
double getStartRadius() const
Returns the starting value of the radius.
Definition: RDFCodeCalculator.hpp:267
void setScalingFactor(double factor)
Allows to specify the scaling factor for the RDF code elements.
Definition: RDFCodeCalculator.hpp:231
void setStartRadius(double start_radius)
Sets the starting value of the radius.
Definition: RDFCodeCalculator.hpp:237
double getSmoothingFactor() const
Returns the smoothing factor used in the calculation of entity pair RDF contributions.
Definition: RDFCodeCalculator.hpp:255
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:74
RDFCodeCalculator()
Constructs the RDFCodeCalculator instance.
Definition: RDFCodeCalculator.hpp:219
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:65
void setEntityPairWeightFunction(const EntityPairWeightFunction &func)
Allows to specify a custom entity pair weight function.
Definition: RDFCodeCalculator.hpp:285
double getScalingFactor() const
Returns the scaling factor applied to the RDF code elements.
Definition: RDFCodeCalculator.hpp:261
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.