| 1 | /* | 
|---|
| 2 | * SaturationDistanceMaximizer.hpp | 
|---|
| 3 | * | 
|---|
| 4 | *  Created on: Jul 27, 2014 | 
|---|
| 5 | *      Author: heber | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #ifndef SATURATIONDISTANCEMAXIMIZER_HPP_ | 
|---|
| 9 | #define SATURATIONDISTANCEMAXIMIZER_HPP_ | 
|---|
| 10 |  | 
|---|
| 11 | // include config.h | 
|---|
| 12 | #ifdef HAVE_CONFIG_H | 
|---|
| 13 | #include <config.h> | 
|---|
| 14 | #endif | 
|---|
| 15 |  | 
|---|
| 16 | #include <vector> | 
|---|
| 17 |  | 
|---|
| 18 | #include <gsl/gsl_vector.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include "SaturatedBond.hpp" | 
|---|
| 21 |  | 
|---|
| 22 | class SaturationDistanceMaximizerTest; | 
|---|
| 23 |  | 
|---|
| 24 | /** This class encapsulates the minimizing/maximization performed to find the | 
|---|
| 25 | * best angles alpha for a vector of SaturatedBonds. | 
|---|
| 26 | */ | 
|---|
| 27 | class SaturationDistanceMaximizer | 
|---|
| 28 | { | 
|---|
| 29 | public: | 
|---|
| 30 | struct Advocate; | 
|---|
| 31 |  | 
|---|
| 32 | //!> typedef for a vector of SaturatedBonds | 
|---|
| 33 | typedef std::vector<SaturatedBond::ptr> PositionContainers_t; | 
|---|
| 34 | //!> typedef for the positions per saturated bond | 
|---|
| 35 | typedef std::vector< SaturatedBond::positions_t > position_bins_t; | 
|---|
| 36 |  | 
|---|
| 37 | SaturationDistanceMaximizer(PositionContainers_t &_PositionContainers) : | 
|---|
| 38 | PositionContainers(_PositionContainers) | 
|---|
| 39 | {} | 
|---|
| 40 | ~SaturationDistanceMaximizer() {} | 
|---|
| 41 |  | 
|---|
| 42 | /** Maximizes the distances between the saturation hydrogens for a number of | 
|---|
| 43 | * SaturedBonds. | 
|---|
| 44 | * | 
|---|
| 45 | * We maximize the function \f$ \sum_{i<j} \frac{1}{ || x_i - x_j ||^2} \f$. | 
|---|
| 46 | */ | 
|---|
| 47 | void operator()(); | 
|---|
| 48 |  | 
|---|
| 49 | /** Requests positions from each SaturatedBond and places it into vector. | 
|---|
| 50 | * | 
|---|
| 51 | * \return vector of tuples of positions | 
|---|
| 52 | */ | 
|---|
| 53 | position_bins_t getAllPositionBins() const; | 
|---|
| 54 |  | 
|---|
| 55 | private: | 
|---|
| 56 | //!> make advocate friend | 
|---|
| 57 | friend struct Advocate; | 
|---|
| 58 | //!> make unit tests friend | 
|---|
| 59 | friend class SaturationDistanceMaximizerTest; | 
|---|
| 60 |  | 
|---|
| 61 | /** Evaluates the penalty function over the current positions. | 
|---|
| 62 | * | 
|---|
| 63 | * \return \f$ \sum_{i<j} \frac{1}{ || x_i - x_j ||^2} \f$ | 
|---|
| 64 | */ | 
|---|
| 65 | double calculatePenality() const; | 
|---|
| 66 |  | 
|---|
| 67 | /** Evaluates the gradient with respect to the angles (i.e. per bin only!) | 
|---|
| 68 | * | 
|---|
| 69 | * \return tuple with amount of change per bin | 
|---|
| 70 | */ | 
|---|
| 71 | std::vector<double> calculatePenalityGradient() const; | 
|---|
| 72 |  | 
|---|
| 73 | /** Getter for the alphas of each SaturatedBond. | 
|---|
| 74 | * | 
|---|
| 75 | * \return vector with all alphas | 
|---|
| 76 | */ | 
|---|
| 77 | std::vector<double> getAlphas() const; | 
|---|
| 78 |  | 
|---|
| 79 | /** Helper function to set the angles alpha of each SaturatedBond from the | 
|---|
| 80 | * components of a gsl_vector \a *x. | 
|---|
| 81 | * | 
|---|
| 82 | * \param x components containing alpha per bond | 
|---|
| 83 | */ | 
|---|
| 84 | void setAlphas(const gsl_vector *x); | 
|---|
| 85 |  | 
|---|
| 86 | /** Getter for the advocate to be handed over to other functions or classes. | 
|---|
| 87 | * | 
|---|
| 88 | * \return ptr to advocate | 
|---|
| 89 | */ | 
|---|
| 90 | Advocate* getAdvocate(); | 
|---|
| 91 |  | 
|---|
| 92 | public: | 
|---|
| 93 | /** This class is friend and may call penalty functions. | 
|---|
| 94 | * | 
|---|
| 95 | * This class is private and only SaturationDistanceMaximizer is its friend. | 
|---|
| 96 | * Hence, it has total control of who may call its function by instantiating | 
|---|
| 97 | * this advocate object abd handing it to someone else (e.g. a function). | 
|---|
| 98 | * | 
|---|
| 99 | */ | 
|---|
| 100 | class Advocate | 
|---|
| 101 | { | 
|---|
| 102 | private: | 
|---|
| 103 | friend class SaturationDistanceMaximizer; | 
|---|
| 104 |  | 
|---|
| 105 | Advocate( | 
|---|
| 106 | SaturationDistanceMaximizer &_maximizer) : | 
|---|
| 107 | maximizer(_maximizer) | 
|---|
| 108 | {} | 
|---|
| 109 |  | 
|---|
| 110 | public: | 
|---|
| 111 | double calculatePenality() const | 
|---|
| 112 | { | 
|---|
| 113 | return maximizer.calculatePenality(); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | /** Evaluates the gradient with respect to the angles (i.e. per bin only!) | 
|---|
| 117 | * | 
|---|
| 118 | * \return tuple with amount of change per bin | 
|---|
| 119 | */ | 
|---|
| 120 | std::vector<double> calculatePenalityGradient() const | 
|---|
| 121 | { | 
|---|
| 122 | return maximizer.calculatePenalityGradient(); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | /** Helper function to set the angles alpha of each SaturatedBond from the | 
|---|
| 126 | * components of a gsl_vector \a *x. | 
|---|
| 127 | * | 
|---|
| 128 | * \param x components containing alpha per bond | 
|---|
| 129 | */ | 
|---|
| 130 | void setAlphas(const gsl_vector *x) | 
|---|
| 131 | { | 
|---|
| 132 | maximizer.setAlphas(x); | 
|---|
| 133 | } | 
|---|
| 134 |  | 
|---|
| 135 | private: | 
|---|
| 136 | //!> internal instance for functionc alls | 
|---|
| 137 | SaturationDistanceMaximizer &maximizer; | 
|---|
| 138 | }; | 
|---|
| 139 |  | 
|---|
| 140 | private: | 
|---|
| 141 | //!> Vectors with all SaturatedBonds belonging to the central atom to saturate | 
|---|
| 142 | PositionContainers_t &PositionContainers; | 
|---|
| 143 | }; | 
|---|
| 144 |  | 
|---|
| 145 |  | 
|---|
| 146 |  | 
|---|
| 147 | #endif /* SATURATIONDISTANCEMAXIMIZER_HPP_ */ | 
|---|