| [e895f7] | 1 | /* | 
|---|
|  | 2 | * SphericalPointDistribution.hpp | 
|---|
|  | 3 | * | 
|---|
|  | 4 | *  Created on: May 29, 2014 | 
|---|
|  | 5 | *      Author: heber | 
|---|
|  | 6 | */ | 
|---|
|  | 7 |  | 
|---|
|  | 8 |  | 
|---|
|  | 9 | #ifndef SPHERICALPOINTDISTRIBUTION_HPP_ | 
|---|
|  | 10 | #define SPHERICALPOINTDISTRIBUTION_HPP_ | 
|---|
|  | 11 |  | 
|---|
|  | 12 | // include config.h | 
|---|
|  | 13 | #ifdef HAVE_CONFIG_H | 
|---|
|  | 14 | #include <config.h> | 
|---|
|  | 15 | #endif | 
|---|
|  | 16 |  | 
|---|
|  | 17 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 18 |  | 
|---|
|  | 19 | #include <cmath> | 
|---|
|  | 20 | #include <list> | 
|---|
| [6393ff] | 21 | #include <map> | 
|---|
|  | 22 | #include <set> | 
|---|
|  | 23 | #include <vector> | 
|---|
| [e895f7] | 24 |  | 
|---|
|  | 25 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 26 |  | 
|---|
| [450adf] | 27 | class SphericalPointDistributionTest; | 
|---|
|  | 28 |  | 
|---|
| [e895f7] | 29 | /** contains getters for the VSEPR model for specific number of electrons. | 
|---|
|  | 30 | * | 
|---|
|  | 31 | * This struct contains specialized functions returning a list of Vectors | 
|---|
|  | 32 | * (points in space) to match the VSEPR model for the given number of electrons. | 
|---|
|  | 33 | * | 
|---|
|  | 34 | * This is implemented via template specialization of the function get(). | 
|---|
|  | 35 | * | 
|---|
|  | 36 | * These specializations are taken from the python script \b CreateVspeShapes.py | 
|---|
|  | 37 | * by Christian Neuen, 07th May 2009. | 
|---|
|  | 38 | */ | 
|---|
|  | 39 | struct SphericalPointDistribution | 
|---|
|  | 40 | { | 
|---|
|  | 41 | /** Cstor for SphericalPointDistribution, allows setting radius of sphere | 
|---|
|  | 42 | * | 
|---|
|  | 43 | * \param _BondLength desired radius of sphere | 
|---|
|  | 44 | */ | 
|---|
|  | 45 | SphericalPointDistribution(const double _Bondlength = 1.) : | 
|---|
| [0c42f2] | 46 | Bondlength(_Bondlength) | 
|---|
| [e895f7] | 47 | {} | 
|---|
|  | 48 |  | 
|---|
|  | 49 | //!> typedef for the list of points | 
|---|
|  | 50 | typedef std::list<Vector> Polygon_t; | 
|---|
| [2199c2] | 51 | //!> typedef for the list of points with integral weights | 
|---|
|  | 52 | typedef std::list<std::pair<Vector, int> > WeightedPolygon_t; | 
|---|
| [6393ff] | 53 | //!> typedef for a sorted list of indices | 
|---|
|  | 54 | typedef std::set<unsigned int> IndexSet_t; | 
|---|
|  | 55 | //!> typedef for the adjacency list of a polygon | 
|---|
|  | 56 | typedef std::map<unsigned int, IndexSet_t > adjacency_t; | 
|---|
| [e895f7] | 57 |  | 
|---|
|  | 58 | /** General getter function for the distribution of points on the surface. | 
|---|
|  | 59 | * | 
|---|
|  | 60 | * \warn this function needs to be specialized! | 
|---|
|  | 61 | * | 
|---|
|  | 62 | * \return Polygon_t with points on the surface centered at (0,0,0) | 
|---|
|  | 63 | */ | 
|---|
| [0c42f2] | 64 | template <int N> Polygon_t get() const | 
|---|
| [e895f7] | 65 | { | 
|---|
|  | 66 | ASSERT(0, "SphericalPointDistribution::get() - not specialized for "+toString(N)+"."); | 
|---|
| [0c42f2] | 67 | return Polygon_t(); | 
|---|
| [e895f7] | 68 | } | 
|---|
|  | 69 |  | 
|---|
| [6393ff] | 70 | template <int N> adjacency_t getConnections() | 
|---|
|  | 71 | { | 
|---|
|  | 72 | ASSERT(0, "SphericalPointDistribution::getConnections() - not specialized for "+toString(N)+"."); | 
|---|
|  | 73 | return Polygon_t(); | 
|---|
|  | 74 | } | 
|---|
|  | 75 |  | 
|---|
| [64cafb2] | 76 | /** Matches a given spherical distribution with another containing more | 
|---|
|  | 77 | * points. | 
|---|
|  | 78 | * | 
|---|
|  | 79 | * This is a helper to determine points where to best insert saturation | 
|---|
|  | 80 | * hydrogens. | 
|---|
| [e895f7] | 81 | * | 
|---|
| [64cafb2] | 82 | * \param _polygon current occupied positions | 
|---|
|  | 83 | * \param _newpolygon ideal distribution to match best with current occupied | 
|---|
|  | 84 | *        positions | 
|---|
|  | 85 | * \return remaining vacant positions relative to \a _polygon | 
|---|
| [e895f7] | 86 | */ | 
|---|
| [64cafb2] | 87 | static Polygon_t matchSphericalPointDistributions( | 
|---|
| [2199c2] | 88 | const WeightedPolygon_t &_polygon, | 
|---|
| [450adf] | 89 | Polygon_t &_newpolygon | 
|---|
| [64cafb2] | 90 | ); | 
|---|
|  | 91 |  | 
|---|
| [e895f7] | 92 | //!> default radius of the spherical distribution | 
|---|
|  | 93 | const double Bondlength; | 
|---|
|  | 94 | //!> precalculated value for root of 3 | 
|---|
| [0c42f2] | 95 | static const double SQRT_3; | 
|---|
| [653cea] | 96 | //!> threshold for L1 error below which matching is immediately acceptable | 
|---|
|  | 97 | static const double L1THRESHOLD; | 
|---|
|  | 98 | //!> threshold for L2 error below which matching is acceptable | 
|---|
|  | 99 | static const double L2THRESHOLD; | 
|---|
| [b67d89] | 100 |  | 
|---|
| [450adf] | 101 | //!> typedef for a full rotation specification consisting of axis and angle. | 
|---|
| [b67d89] | 102 | typedef std::pair<Vector, double> Rotation_t; | 
|---|
|  | 103 |  | 
|---|
| [450adf] | 104 | //!> typedef for a list of indices (of points in a polygon) | 
|---|
| [b67d89] | 105 | typedef std::list<unsigned int> IndexList_t; | 
|---|
| [450adf] | 106 | //!> typedef enumerating possibly multiple points accumulated as one point | 
|---|
|  | 107 | typedef std::list< IndexList_t > IndexTupleList_t; | 
|---|
|  | 108 | //!> typedef for a vector of indices | 
|---|
| [b67d89] | 109 | typedef std::vector<unsigned int> IndexArray_t; | 
|---|
| [450adf] | 110 | //!> typedef for a Vector of positions | 
|---|
| [b67d89] | 111 | typedef std::vector<Vector> VectorArray_t; | 
|---|
| [450adf] | 112 | //!> typedef for a Vector of positions with weights | 
|---|
|  | 113 | typedef std::vector< std::pair<Vector, int> > WeightedVectorArray_t; | 
|---|
| [653cea] | 114 | //!> typedef for a vector of degrees (or integral weights) | 
|---|
|  | 115 | typedef std::vector<unsigned int> WeightsArray_t; | 
|---|
| [b67d89] | 116 |  | 
|---|
|  | 117 | //!> amplitude up to which deviations in checks of rotations are tolerated | 
|---|
|  | 118 | static const double warn_amplitude; | 
|---|
|  | 119 |  | 
|---|
| [0983e6] | 120 | struct PolygonWithIndices | 
|---|
|  | 121 | { | 
|---|
|  | 122 | //!> array with points | 
|---|
|  | 123 | VectorArray_t polygon; | 
|---|
|  | 124 | //!> list with indices for the above points, defining subset | 
|---|
|  | 125 | IndexList_t indices; | 
|---|
|  | 126 | }; | 
|---|
|  | 127 |  | 
|---|
| [3678eb] | 128 | static Vector calculateCenterOfMinimumDistance( | 
|---|
|  | 129 | const SphericalPointDistribution::VectorArray_t &_positions, | 
|---|
|  | 130 | const SphericalPointDistribution::IndexList_t &_indices); | 
|---|
|  | 131 |  | 
|---|
| [b67d89] | 132 | private: | 
|---|
| [450adf] | 133 | //!> grant unit tests access to private parts | 
|---|
|  | 134 | friend class SphericalPointDistributionTest; | 
|---|
|  | 135 |  | 
|---|
| [b67d89] | 136 | static std::pair<double, double> calculateErrorOfMatching( | 
|---|
| [653cea] | 137 | const VectorArray_t &_old, | 
|---|
|  | 138 | const VectorArray_t &_new, | 
|---|
|  | 139 | const IndexTupleList_t &_Matching); | 
|---|
| [b67d89] | 140 |  | 
|---|
|  | 141 | static Polygon_t removeMatchingPoints( | 
|---|
| [0983e6] | 142 | const PolygonWithIndices &_points); | 
|---|
| [b67d89] | 143 |  | 
|---|
|  | 144 | struct MatchingControlStructure { | 
|---|
|  | 145 | bool foundflag; | 
|---|
|  | 146 | double bestL2; | 
|---|
| [653cea] | 147 | IndexTupleList_t bestmatching; | 
|---|
| [b67d89] | 148 | VectorArray_t oldpoints; | 
|---|
|  | 149 | VectorArray_t newpoints; | 
|---|
| [653cea] | 150 | WeightsArray_t weights; | 
|---|
| [b67d89] | 151 | }; | 
|---|
|  | 152 |  | 
|---|
|  | 153 | static void recurseMatchings( | 
|---|
|  | 154 | MatchingControlStructure &_MCS, | 
|---|
| [653cea] | 155 | IndexTupleList_t &_matching, | 
|---|
| [b67d89] | 156 | IndexList_t _indices, | 
|---|
| [653cea] | 157 | WeightsArray_t &_remainingweights, | 
|---|
|  | 158 | WeightsArray_t::iterator _remainiter, | 
|---|
|  | 159 | const unsigned int _matchingsize | 
|---|
|  | 160 | ); | 
|---|
| [b67d89] | 161 |  | 
|---|
|  | 162 | static IndexList_t findBestMatching( | 
|---|
| [2199c2] | 163 | const WeightedPolygon_t &_polygon, | 
|---|
| [450adf] | 164 | Polygon_t &_newpolygon | 
|---|
|  | 165 | ); | 
|---|
|  | 166 |  | 
|---|
|  | 167 | static IndexList_t joinPoints( | 
|---|
|  | 168 | Polygon_t &_newpolygon, | 
|---|
|  | 169 | const VectorArray_t &_newpoints, | 
|---|
|  | 170 | const IndexTupleList_t &_bestmatching | 
|---|
| [b67d89] | 171 | ); | 
|---|
|  | 172 |  | 
|---|
|  | 173 | static Rotation_t findPlaneAligningRotation( | 
|---|
| [0983e6] | 174 | const PolygonWithIndices &_referencepositions, | 
|---|
|  | 175 | const PolygonWithIndices &_currentpositions | 
|---|
| [b67d89] | 176 | ); | 
|---|
|  | 177 |  | 
|---|
|  | 178 | static Rotation_t findPointAligningRotation( | 
|---|
| [0983e6] | 179 | const PolygonWithIndices &remainingold, | 
|---|
|  | 180 | const PolygonWithIndices &remainingnew); | 
|---|
| [e895f7] | 181 | }; | 
|---|
|  | 182 |  | 
|---|
| [f54930] | 183 | // declare specializations | 
|---|
|  | 184 |  | 
|---|
| [0c42f2] | 185 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<0>() const; | 
|---|
|  | 186 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<1>() const; | 
|---|
|  | 187 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<2>() const; | 
|---|
|  | 188 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<3>() const; | 
|---|
|  | 189 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<4>() const; | 
|---|
|  | 190 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<5>() const; | 
|---|
|  | 191 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<6>() const; | 
|---|
|  | 192 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<7>() const; | 
|---|
|  | 193 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<8>() const; | 
|---|
|  | 194 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<9>() const; | 
|---|
|  | 195 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<10>() const; | 
|---|
|  | 196 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<11>() const; | 
|---|
|  | 197 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<12>() const; | 
|---|
|  | 198 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<14>() const; | 
|---|
| [e895f7] | 199 |  | 
|---|
| [6393ff] | 200 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<0>(); | 
|---|
|  | 201 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<1>(); | 
|---|
|  | 202 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<2>(); | 
|---|
|  | 203 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<3>(); | 
|---|
|  | 204 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<4>(); | 
|---|
|  | 205 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<5>(); | 
|---|
|  | 206 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<6>(); | 
|---|
|  | 207 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<7>(); | 
|---|
|  | 208 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<8>(); | 
|---|
|  | 209 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<9>(); | 
|---|
|  | 210 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<10>(); | 
|---|
|  | 211 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<11>(); | 
|---|
|  | 212 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<12>(); | 
|---|
|  | 213 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<14>(); | 
|---|
|  | 214 |  | 
|---|
| [e895f7] | 215 | #endif /* SPHERICALPOINTDISTRIBUTION_HPP_ */ | 
|---|