| 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>
 | 
|---|
| 21 | #include <map>
 | 
|---|
| 22 | #include <set>
 | 
|---|
| 23 | #include <vector>
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 26 | 
 | 
|---|
| 27 | class SphericalPointDistributionTest;
 | 
|---|
| 28 | 
 | 
|---|
| 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.) :
 | 
|---|
| 46 |     Bondlength(_Bondlength)
 | 
|---|
| 47 |   {}
 | 
|---|
| 48 | 
 | 
|---|
| 49 |   //!> typedef for the list of points
 | 
|---|
| 50 |   typedef std::list<Vector> Polygon_t;
 | 
|---|
| 51 |   //!> typedef for the list of points with integral weights
 | 
|---|
| 52 |   typedef std::list<std::pair<Vector, int> > WeightedPolygon_t;
 | 
|---|
| 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;
 | 
|---|
| 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 |    */
 | 
|---|
| 64 |   template <int N> Polygon_t get() const
 | 
|---|
| 65 |   {
 | 
|---|
| 66 |     ASSERT(0, "SphericalPointDistribution::get() - not specialized for "+toString(N)+".");
 | 
|---|
| 67 |     return Polygon_t();
 | 
|---|
| 68 |   }
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   template <int N> adjacency_t getConnections()
 | 
|---|
| 71 |   {
 | 
|---|
| 72 |     ASSERT(0, "SphericalPointDistribution::getConnections() - not specialized for "+toString(N)+".");
 | 
|---|
| 73 |     return Polygon_t();
 | 
|---|
| 74 |   }
 | 
|---|
| 75 | 
 | 
|---|
| 76 |   /** Initializes the polygon with the given \a _NumberOfPoints.
 | 
|---|
| 77 |    *
 | 
|---|
| 78 |    * \param _NumberOfPoints number of points
 | 
|---|
| 79 |    */
 | 
|---|
| 80 |   Polygon_t getSimplePolygon(const int _NumberOfPoints) const;
 | 
|---|
| 81 | 
 | 
|---|
| 82 |   /** Returns vacant spots to fill to get a complete spherical point distribution from
 | 
|---|
| 83 |    * given points \a _polygon, containing then \a _N in total.
 | 
|---|
| 84 |    *
 | 
|---|
| 85 |    * This is a helper to determine points where to best insert saturation
 | 
|---|
| 86 |    * hydrogens.
 | 
|---|
| 87 |    *
 | 
|---|
| 88 |    * \param _polygon already filled places to match
 | 
|---|
| 89 |    * \param _num_spots number of nodes in the polyhedra (e.g., number of orbitals in outer shell)
 | 
|---|
| 90 |    * \param _num_spots_available nodes available (e.g., number of orbitals not fully occupied)
 | 
|---|
| 91 |    */
 | 
|---|
| 92 |   Polygon_t getRemainingPoints(
 | 
|---|
| 93 |       const WeightedPolygon_t &_polygon,
 | 
|---|
| 94 |       const int _num_spots,
 | 
|---|
| 95 |       const int _num_spots_available);
 | 
|---|
| 96 | 
 | 
|---|
| 97 |   //!> default radius of the spherical distribution
 | 
|---|
| 98 |   const double Bondlength;
 | 
|---|
| 99 |   //!> precalculated value for root of 3
 | 
|---|
| 100 |   static const double SQRT_3;
 | 
|---|
| 101 |   //!> threshold for L1 error below which matching is immediately acceptable
 | 
|---|
| 102 |   static const double L1THRESHOLD;
 | 
|---|
| 103 |   //!> threshold for L2 error below which matching is acceptable
 | 
|---|
| 104 |   static const double L2THRESHOLD;
 | 
|---|
| 105 | 
 | 
|---|
| 106 |   //!> typedef for a full rotation specification consisting of axis and angle.
 | 
|---|
| 107 |   typedef std::pair<Vector, double> Rotation_t;
 | 
|---|
| 108 | 
 | 
|---|
| 109 |   //!> typedef for a list of indices (of points in a polygon)
 | 
|---|
| 110 |   typedef std::list<unsigned int> IndexList_t;
 | 
|---|
| 111 |   //!> typedef enumerating possibly multiple points accumulated as one point
 | 
|---|
| 112 |   typedef std::list< IndexList_t > IndexTupleList_t;
 | 
|---|
| 113 |   //!> typedef for a vector of indices
 | 
|---|
| 114 |   typedef std::vector<unsigned int> IndexArray_t;
 | 
|---|
| 115 |   //!> typedef for a Vector of positions
 | 
|---|
| 116 |   typedef std::vector<Vector> VectorArray_t;
 | 
|---|
| 117 |   //!> typedef for a Vector of positions with weights
 | 
|---|
| 118 |   typedef std::vector< std::pair<Vector, int> > WeightedVectorArray_t;
 | 
|---|
| 119 |   //!> typedef for a vector of degrees (or integral weights)
 | 
|---|
| 120 |   typedef std::vector<unsigned int> WeightsArray_t;
 | 
|---|
| 121 | 
 | 
|---|
| 122 |   //!> amplitude up to which deviations in checks of rotations are tolerated
 | 
|---|
| 123 |   static const double warn_amplitude;
 | 
|---|
| 124 | 
 | 
|---|
| 125 |   struct PolygonWithIndices
 | 
|---|
| 126 |   {
 | 
|---|
| 127 |     //!> array with points
 | 
|---|
| 128 |     VectorArray_t polygon;
 | 
|---|
| 129 |     //!> list with indices for the above points, defining subset
 | 
|---|
| 130 |     IndexList_t indices;
 | 
|---|
| 131 |   };
 | 
|---|
| 132 | 
 | 
|---|
| 133 |   static Vector calculateCenterOfMinimumDistance(
 | 
|---|
| 134 |       const SphericalPointDistribution::VectorArray_t &_positions,
 | 
|---|
| 135 |       const SphericalPointDistribution::IndexList_t &_indices);
 | 
|---|
| 136 | 
 | 
|---|
| 137 | private:
 | 
|---|
| 138 |   //!> points for the ideal distribution
 | 
|---|
| 139 |   Polygon_t points;
 | 
|---|
| 140 |   //!> connection information between these ideal points
 | 
|---|
| 141 |   adjacency_t adjacency;
 | 
|---|
| 142 | 
 | 
|---|
| 143 |   /** Initialize inner status (points and adjacency) to desired number of
 | 
|---|
| 144 |    * points.
 | 
|---|
| 145 |    *
 | 
|---|
| 146 |    * \param _N number of points
 | 
|---|
| 147 |    */
 | 
|---|
| 148 |   void initSelf(const int _N);
 | 
|---|
| 149 | 
 | 
|---|
| 150 | private:
 | 
|---|
| 151 |   //!> grant unit tests access to private parts
 | 
|---|
| 152 |   friend class SphericalPointDistributionTest;
 | 
|---|
| 153 | 
 | 
|---|
| 154 |   static std::pair<double, double> calculateErrorOfMatching(
 | 
|---|
| 155 |       const VectorArray_t &_old,
 | 
|---|
| 156 |       const VectorArray_t &_new,
 | 
|---|
| 157 |       const IndexTupleList_t &_Matching);
 | 
|---|
| 158 | 
 | 
|---|
| 159 |   static Polygon_t removeMatchingPoints(
 | 
|---|
| 160 |       const PolygonWithIndices &_points);
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   struct MatchingControlStructure {
 | 
|---|
| 163 |     MatchingControlStructure(
 | 
|---|
| 164 |         const adjacency_t &_adjacency,
 | 
|---|
| 165 |         const VectorArray_t &_oldpoints,
 | 
|---|
| 166 |         const VectorArray_t &_newpoints,
 | 
|---|
| 167 |         const WeightsArray_t &_weights
 | 
|---|
| 168 |         );
 | 
|---|
| 169 |     bool foundflag;
 | 
|---|
| 170 |     double bestL2;
 | 
|---|
| 171 |     const adjacency_t &adjacency;
 | 
|---|
| 172 |     const VectorArray_t oldpoints;
 | 
|---|
| 173 |     const VectorArray_t newpoints;
 | 
|---|
| 174 |     const WeightsArray_t weights;
 | 
|---|
| 175 |     IndexTupleList_t bestmatching;
 | 
|---|
| 176 |   };
 | 
|---|
| 177 | 
 | 
|---|
| 178 |   static void recurseMatchings(
 | 
|---|
| 179 |       MatchingControlStructure &_MCS,
 | 
|---|
| 180 |       IndexTupleList_t &_matching,
 | 
|---|
| 181 |       IndexList_t _indices,
 | 
|---|
| 182 |       WeightsArray_t &_remainingweights,
 | 
|---|
| 183 |       WeightsArray_t::iterator _remainiter,
 | 
|---|
| 184 |       const unsigned int _matchingsize
 | 
|---|
| 185 |       );
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   IndexList_t findBestMatching(const WeightedPolygon_t &_polygon);
 | 
|---|
| 188 | 
 | 
|---|
| 189 |   static IndexList_t joinPoints(
 | 
|---|
| 190 |       Polygon_t &_newpolygon,
 | 
|---|
| 191 |       const VectorArray_t &_newpoints,
 | 
|---|
| 192 |       const IndexTupleList_t &_bestmatching
 | 
|---|
| 193 |       );
 | 
|---|
| 194 | 
 | 
|---|
| 195 |   static Rotation_t findPlaneAligningRotation(
 | 
|---|
| 196 |       const PolygonWithIndices &_referencepositions,
 | 
|---|
| 197 |       const PolygonWithIndices &_currentpositions
 | 
|---|
| 198 |       );
 | 
|---|
| 199 | 
 | 
|---|
| 200 |   static Rotation_t findPointAligningRotation(
 | 
|---|
| 201 |       const PolygonWithIndices &remainingold,
 | 
|---|
| 202 |       const PolygonWithIndices &remainingnew);
 | 
|---|
| 203 | };
 | 
|---|
| 204 | 
 | 
|---|
| 205 | // declare specializations
 | 
|---|
| 206 | 
 | 
|---|
| 207 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<0>() const;
 | 
|---|
| 208 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<1>() const;
 | 
|---|
| 209 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<2>() const;
 | 
|---|
| 210 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<3>() const;
 | 
|---|
| 211 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<4>() const;
 | 
|---|
| 212 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<5>() const;
 | 
|---|
| 213 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<6>() const;
 | 
|---|
| 214 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<7>() const;
 | 
|---|
| 215 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<8>() const;
 | 
|---|
| 216 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<9>() const;
 | 
|---|
| 217 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<10>() const;
 | 
|---|
| 218 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<11>() const;
 | 
|---|
| 219 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<12>() const;
 | 
|---|
| 220 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<14>() const;
 | 
|---|
| 221 | 
 | 
|---|
| 222 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<0>();
 | 
|---|
| 223 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<1>();
 | 
|---|
| 224 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<2>();
 | 
|---|
| 225 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<3>();
 | 
|---|
| 226 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<4>();
 | 
|---|
| 227 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<5>();
 | 
|---|
| 228 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<6>();
 | 
|---|
| 229 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<7>();
 | 
|---|
| 230 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<8>();
 | 
|---|
| 231 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<9>();
 | 
|---|
| 232 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<10>();
 | 
|---|
| 233 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<11>();
 | 
|---|
| 234 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<12>();
 | 
|---|
| 235 | template <> SphericalPointDistribution::adjacency_t SphericalPointDistribution::getConnections<14>();
 | 
|---|
| 236 | 
 | 
|---|
| 237 | #endif /* SPHERICALPOINTDISTRIBUTION_HPP_ */
 | 
|---|