| [97d6ab] | 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 | 
 | 
|---|
 | 22 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
 | 23 | 
 | 
|---|
| [1ae9aa] | 24 | class SphericalPointDistributionTest;
 | 
|---|
 | 25 | 
 | 
|---|
| [97d6ab] | 26 | /** contains getters for the VSEPR model for specific number of electrons.
 | 
|---|
 | 27 |  *
 | 
|---|
 | 28 |  * This struct contains specialized functions returning a list of Vectors
 | 
|---|
 | 29 |  * (points in space) to match the VSEPR model for the given number of electrons.
 | 
|---|
 | 30 |  *
 | 
|---|
 | 31 |  * This is implemented via template specialization of the function get().
 | 
|---|
 | 32 |  *
 | 
|---|
 | 33 |  * These specializations are taken from the python script \b CreateVspeShapes.py
 | 
|---|
 | 34 |  * by Christian Neuen, 07th May 2009.
 | 
|---|
 | 35 |  */
 | 
|---|
 | 36 | struct SphericalPointDistribution
 | 
|---|
 | 37 | {
 | 
|---|
 | 38 |   /** Cstor for SphericalPointDistribution, allows setting radius of sphere
 | 
|---|
 | 39 |    *
 | 
|---|
 | 40 |    * \param _BondLength desired radius of sphere
 | 
|---|
 | 41 |    */
 | 
|---|
 | 42 |   SphericalPointDistribution(const double _Bondlength = 1.) :
 | 
|---|
 | 43 |     Bondlength(_Bondlength),
 | 
|---|
 | 44 |     SQRT_3(sqrt(3.0))
 | 
|---|
 | 45 |   {}
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 |   //!> typedef for the list of points
 | 
|---|
 | 48 |   typedef std::list<Vector> Polygon_t;
 | 
|---|
| [260540] | 49 |   //!> typedef for the list of points with integral weights
 | 
|---|
 | 50 |   typedef std::list<std::pair<Vector, int> > WeightedPolygon_t;
 | 
|---|
| [97d6ab] | 51 | 
 | 
|---|
 | 52 |   /** General getter function for the distribution of points on the surface.
 | 
|---|
 | 53 |    *
 | 
|---|
 | 54 |    * \warn this function needs to be specialized!
 | 
|---|
 | 55 |    *
 | 
|---|
 | 56 |    * \return Polygon_t with points on the surface centered at (0,0,0)
 | 
|---|
 | 57 |    */
 | 
|---|
 | 58 |   template <int N> Polygon_t get()
 | 
|---|
 | 59 |   {
 | 
|---|
 | 60 |     ASSERT(0, "SphericalPointDistribution::get() - not specialized for "+toString(N)+".");
 | 
|---|
 | 61 |   }
 | 
|---|
 | 62 | 
 | 
|---|
| [0d4daf] | 63 | 
 | 
|---|
| [972412] | 64 |   /** Matches a given spherical distribution with another containing more
 | 
|---|
 | 65 |    * points.
 | 
|---|
 | 66 |    *
 | 
|---|
| [6ef48c] | 67 |    * The idea is to produce a matching from all points in \a _polygon to those
 | 
|---|
 | 68 |    * in \a _newpolygon in such a way that their distance difference is minimal.
 | 
|---|
 | 69 |    * As we just look at numbers of points determined by valency, i.e.
 | 
|---|
 | 70 |    * independent of the number of atoms, we simply go through each of the possible
 | 
|---|
 | 71 |    * mappings. We stop when the L1 error is below a certain \a threshold,
 | 
|---|
 | 72 |    * otherwise we pick the matching with the lowest L2 error.
 | 
|---|
 | 73 |    *
 | 
|---|
| [972412] | 74 |    * This is a helper to determine points where to best insert saturation
 | 
|---|
 | 75 |    * hydrogens.
 | 
|---|
 | 76 |    *
 | 
|---|
 | 77 |    * \param _polygon current occupied positions
 | 
|---|
 | 78 |    * \param _newpolygon ideal distribution to match best with current occupied
 | 
|---|
 | 79 |    *        positions
 | 
|---|
 | 80 |    * \return remaining vacant positions relative to \a _polygon
 | 
|---|
 | 81 |    */
 | 
|---|
 | 82 |   static Polygon_t matchSphericalPointDistributions(
 | 
|---|
| [260540] | 83 |       const WeightedPolygon_t &_polygon,
 | 
|---|
| [1ae9aa] | 84 |       Polygon_t &_newpolygon
 | 
|---|
| [972412] | 85 |       );
 | 
|---|
 | 86 | 
 | 
|---|
| [97d6ab] | 87 |   //!> default radius of the spherical distribution
 | 
|---|
 | 88 |   const double Bondlength;
 | 
|---|
 | 89 |   //!> precalculated value for root of 3
 | 
|---|
 | 90 |   const double SQRT_3;
 | 
|---|
| [23c605] | 91 |   //!> threshold for L1 error below which matching is immediately acceptable
 | 
|---|
 | 92 |   static const double L1THRESHOLD;
 | 
|---|
 | 93 |   //!> threshold for L2 error below which matching is acceptable
 | 
|---|
 | 94 |   static const double L2THRESHOLD;
 | 
|---|
| [3da643] | 95 | 
 | 
|---|
| [1ae9aa] | 96 |   //!> typedef for a full rotation specification consisting of axis and angle.
 | 
|---|
| [3da643] | 97 |   typedef std::pair<Vector, double> Rotation_t;
 | 
|---|
 | 98 | 
 | 
|---|
| [1ae9aa] | 99 |   //!> typedef for a list of indices (of points in a polygon)
 | 
|---|
| [3da643] | 100 |   typedef std::list<unsigned int> IndexList_t;
 | 
|---|
| [1ae9aa] | 101 |   //!> typedef enumerating possibly multiple points accumulated as one point
 | 
|---|
 | 102 |   typedef std::list< IndexList_t > IndexTupleList_t;
 | 
|---|
 | 103 |   //!> typedef for a vector of indices
 | 
|---|
| [3da643] | 104 |   typedef std::vector<unsigned int> IndexArray_t;
 | 
|---|
| [1ae9aa] | 105 |   //!> typedef for a Vector of positions
 | 
|---|
| [3da643] | 106 |   typedef std::vector<Vector> VectorArray_t;
 | 
|---|
| [1ae9aa] | 107 |   //!> typedef for a Vector of positions with weights
 | 
|---|
 | 108 |   typedef std::vector< std::pair<Vector, int> > WeightedVectorArray_t;
 | 
|---|
| [23c605] | 109 |   //!> typedef for a vector of degrees (or integral weights)
 | 
|---|
 | 110 |   typedef std::vector<unsigned int> WeightsArray_t;
 | 
|---|
| [3da643] | 111 | 
 | 
|---|
 | 112 |   //!> amplitude up to which deviations in checks of rotations are tolerated
 | 
|---|
 | 113 |   static const double warn_amplitude;
 | 
|---|
 | 114 | 
 | 
|---|
 | 115 | private:
 | 
|---|
| [1ae9aa] | 116 |   //!> grant unit tests access to private parts
 | 
|---|
 | 117 |   friend class SphericalPointDistributionTest;
 | 
|---|
 | 118 | 
 | 
|---|
| [3da643] | 119 |   static std::pair<double, double> calculateErrorOfMatching(
 | 
|---|
| [23c605] | 120 |       const VectorArray_t &_old,
 | 
|---|
 | 121 |       const VectorArray_t &_new,
 | 
|---|
 | 122 |       const IndexTupleList_t &_Matching);
 | 
|---|
| [3da643] | 123 | 
 | 
|---|
 | 124 |   static Polygon_t removeMatchingPoints(
 | 
|---|
 | 125 |       const VectorArray_t &_points,
 | 
|---|
 | 126 |       const IndexList_t &_matchingindices
 | 
|---|
 | 127 |       );
 | 
|---|
 | 128 | 
 | 
|---|
 | 129 |   struct MatchingControlStructure {
 | 
|---|
 | 130 |     bool foundflag;
 | 
|---|
 | 131 |     double bestL2;
 | 
|---|
| [23c605] | 132 |     IndexTupleList_t bestmatching;
 | 
|---|
| [3da643] | 133 |     VectorArray_t oldpoints;
 | 
|---|
 | 134 |     VectorArray_t newpoints;
 | 
|---|
| [23c605] | 135 |     WeightsArray_t weights;
 | 
|---|
| [3da643] | 136 |   };
 | 
|---|
 | 137 | 
 | 
|---|
 | 138 |   static void recurseMatchings(
 | 
|---|
 | 139 |       MatchingControlStructure &_MCS,
 | 
|---|
| [23c605] | 140 |       IndexTupleList_t &_matching,
 | 
|---|
| [3da643] | 141 |       IndexList_t _indices,
 | 
|---|
| [23c605] | 142 |       WeightsArray_t &_remainingweights,
 | 
|---|
 | 143 |       WeightsArray_t::iterator _remainiter,
 | 
|---|
 | 144 |       const unsigned int _matchingsize
 | 
|---|
 | 145 |       );
 | 
|---|
| [3da643] | 146 | 
 | 
|---|
 | 147 |   static IndexList_t findBestMatching(
 | 
|---|
| [260540] | 148 |       const WeightedPolygon_t &_polygon,
 | 
|---|
| [1ae9aa] | 149 |       Polygon_t &_newpolygon
 | 
|---|
 | 150 |       );
 | 
|---|
 | 151 | 
 | 
|---|
 | 152 |   static IndexList_t joinPoints(
 | 
|---|
 | 153 |       Polygon_t &_newpolygon,
 | 
|---|
 | 154 |       const VectorArray_t &_newpoints,
 | 
|---|
 | 155 |       const IndexTupleList_t &_bestmatching
 | 
|---|
| [3da643] | 156 |       );
 | 
|---|
 | 157 | 
 | 
|---|
 | 158 |   static Rotation_t findPlaneAligningRotation(
 | 
|---|
 | 159 |       const VectorArray_t &_referencepositions,
 | 
|---|
 | 160 |       const VectorArray_t &_currentpositions,
 | 
|---|
 | 161 |       const IndexList_t &_bestmatching
 | 
|---|
 | 162 |       );
 | 
|---|
 | 163 | 
 | 
|---|
 | 164 |   static Rotation_t findPointAligningRotation(
 | 
|---|
 | 165 |       const VectorArray_t &remainingold,
 | 
|---|
 | 166 |       const VectorArray_t &remainingnew,
 | 
|---|
 | 167 |       const IndexList_t &_bestmatching);
 | 
|---|
 | 168 | 
 | 
|---|
| [97d6ab] | 169 | };
 | 
|---|
 | 170 | 
 | 
|---|
| [0d4daf] | 171 | // declare specializations
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<0>();
 | 
|---|
 | 174 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<1>();
 | 
|---|
 | 175 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<2>();
 | 
|---|
 | 176 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<3>();
 | 
|---|
 | 177 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<4>();
 | 
|---|
 | 178 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<5>();
 | 
|---|
 | 179 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<6>();
 | 
|---|
 | 180 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<7>();
 | 
|---|
 | 181 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<8>();
 | 
|---|
 | 182 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<9>();
 | 
|---|
 | 183 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<10>();
 | 
|---|
 | 184 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<11>();
 | 
|---|
 | 185 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<12>();
 | 
|---|
 | 186 | template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<14>();
 | 
|---|
| [97d6ab] | 187 | 
 | 
|---|
 | 188 | #endif /* SPHERICALPOINTDISTRIBUTION_HPP_ */
 | 
|---|