source: src/Fragmentation/Exporters/SphericalPointDistribution.hpp@ a2f8a9

Last change on this file since a2f8a9 was a2f8a9, checked in by Frederik Heber <heber@…>, 11 years ago

Added calculation of center of minimum distance by bisection.

  • this will give us a unique a definite point independent of the (rotational) position of the point set on the unit sphere.
  • added unit test.
  • Property mode set to 100644
File size: 6.8 KB
Line 
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
24class SphericalPointDistributionTest;
25
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 */
36struct 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;
49 //!> typedef for the list of points with integral weights
50 typedef std::list<std::pair<Vector, int> > WeightedPolygon_t;
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
63
64 /** Matches a given spherical distribution with another containing more
65 * points.
66 *
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 *
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(
83 const WeightedPolygon_t &_polygon,
84 Polygon_t &_newpolygon
85 );
86
87 //!> default radius of the spherical distribution
88 const double Bondlength;
89 //!> precalculated value for root of 3
90 const double SQRT_3;
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;
95
96 //!> typedef for a full rotation specification consisting of axis and angle.
97 typedef std::pair<Vector, double> Rotation_t;
98
99 //!> typedef for a list of indices (of points in a polygon)
100 typedef std::list<unsigned int> IndexList_t;
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
104 typedef std::vector<unsigned int> IndexArray_t;
105 //!> typedef for a Vector of positions
106 typedef std::vector<Vector> VectorArray_t;
107 //!> typedef for a Vector of positions with weights
108 typedef std::vector< std::pair<Vector, int> > WeightedVectorArray_t;
109 //!> typedef for a vector of degrees (or integral weights)
110 typedef std::vector<unsigned int> WeightsArray_t;
111
112 //!> amplitude up to which deviations in checks of rotations are tolerated
113 static const double warn_amplitude;
114
115 struct PolygonWithIndices
116 {
117 //!> array with points
118 VectorArray_t polygon;
119 //!> list with indices for the above points, defining subset
120 IndexList_t indices;
121 };
122
123 static Vector calculateCenterOfMinimumDistance(
124 const SphericalPointDistribution::VectorArray_t &_positions,
125 const SphericalPointDistribution::IndexList_t &_indices);
126
127private:
128 //!> grant unit tests access to private parts
129 friend class SphericalPointDistributionTest;
130
131 static std::pair<double, double> calculateErrorOfMatching(
132 const VectorArray_t &_old,
133 const VectorArray_t &_new,
134 const IndexTupleList_t &_Matching);
135
136 static Polygon_t removeMatchingPoints(
137 const PolygonWithIndices &_points);
138
139 struct MatchingControlStructure {
140 bool foundflag;
141 double bestL2;
142 IndexTupleList_t bestmatching;
143 VectorArray_t oldpoints;
144 VectorArray_t newpoints;
145 WeightsArray_t weights;
146 };
147
148 static void recurseMatchings(
149 MatchingControlStructure &_MCS,
150 IndexTupleList_t &_matching,
151 IndexList_t _indices,
152 WeightsArray_t &_remainingweights,
153 WeightsArray_t::iterator _remainiter,
154 const unsigned int _matchingsize
155 );
156
157 static IndexList_t findBestMatching(
158 const WeightedPolygon_t &_polygon,
159 Polygon_t &_newpolygon
160 );
161
162 static IndexList_t joinPoints(
163 Polygon_t &_newpolygon,
164 const VectorArray_t &_newpoints,
165 const IndexTupleList_t &_bestmatching
166 );
167
168 static Rotation_t findPlaneAligningRotation(
169 const PolygonWithIndices &_referencepositions,
170 const PolygonWithIndices &_currentpositions
171 );
172
173 static Rotation_t findPointAligningRotation(
174 const PolygonWithIndices &remainingold,
175 const PolygonWithIndices &remainingnew);
176};
177
178// declare specializations
179
180template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<0>();
181template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<1>();
182template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<2>();
183template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<3>();
184template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<4>();
185template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<5>();
186template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<6>();
187template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<7>();
188template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<8>();
189template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<9>();
190template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<10>();
191template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<11>();
192template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<12>();
193template <> SphericalPointDistribution::Polygon_t SphericalPointDistribution::get<14>();
194
195#endif /* SPHERICALPOINTDISTRIBUTION_HPP_ */
Note: See TracBrowser for help on using the repository browser.