| [bcf653] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2010 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
| [6ac7ee] | 8 | /*
 | 
|---|
 | 9 |  * ellipsoid.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
| [042f82] | 11 |  *  Created on: Jan 20, 2009
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
| [6ac7ee] | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
| [bf3817] | 15 | // include config.h
 | 
|---|
 | 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 17 | #include <config.h>
 | 
|---|
 | 18 | #endif
 | 
|---|
 | 19 | 
 | 
|---|
| [ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| [112b09] | 21 | 
 | 
|---|
| [357fba] | 22 | #include <gsl/gsl_multimin.h>
 | 
|---|
 | 23 | #include <gsl/gsl_vector.h>
 | 
|---|
 | 24 | 
 | 
|---|
| [f66195] | 25 | #include <iomanip>
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | #include <set>
 | 
|---|
 | 28 | 
 | 
|---|
| [d74077] | 29 | #include "BoundaryPointSet.hpp"
 | 
|---|
| [357fba] | 30 | #include "boundary.hpp"
 | 
|---|
| [6ac7ee] | 31 | #include "ellipsoid.hpp"
 | 
|---|
| [f66195] | 32 | #include "linkedcell.hpp"
 | 
|---|
| [ad011c] | 33 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [f66195] | 34 | #include "tesselation.hpp"
 | 
|---|
| [57f243] | 35 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [cca9ef] | 36 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| [ad011c] | 37 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| [6ac7ee] | 38 | 
 | 
|---|
| [a5028f] | 39 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
 | 
|---|
 | 40 | #include "RandomNumbers/RandomNumberGenerator.hpp"
 | 
|---|
 | 41 | 
 | 
|---|
| [6ac7ee] | 42 | /** Determines squared distance for a given point \a x to surface of ellipsoid.
 | 
|---|
 | 43 |  * \param x given point
 | 
|---|
 | 44 |  * \param EllipsoidCenter center of ellipsoid
 | 
|---|
 | 45 |  * \param EllipsoidLength[3] three lengths of half axis of ellipsoid
 | 
|---|
 | 46 |  * \param EllipsoidAngle[3] three rotation angles of ellipsoid
 | 
|---|
 | 47 |  * \return squared distance from point to surface
 | 
|---|
 | 48 |  */
 | 
|---|
 | 49 | double SquaredDistanceToEllipsoid(Vector &x, Vector &EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
 | 
|---|
 | 50 | {
 | 
|---|
| [042f82] | 51 |   Vector helper, RefPoint;
 | 
|---|
 | 52 |   double distance = -1.;
 | 
|---|
| [cca9ef] | 53 |   RealSpaceMatrix Matrix;
 | 
|---|
| [042f82] | 54 |   double InverseLength[3];
 | 
|---|
 | 55 |   double psi,theta,phi; // euler angles in ZX'Z'' convention
 | 
|---|
 | 56 | 
 | 
|---|
| [e138de] | 57 |   //Log() << Verbose(3) << "Begin of SquaredDistanceToEllipsoid" << endl;
 | 
|---|
| [042f82] | 58 | 
 | 
|---|
 | 59 |   for(int i=0;i<3;i++)
 | 
|---|
 | 60 |     InverseLength[i] = 1./EllipsoidLength[i];
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |   // 1. translate coordinate system so that ellipsoid center is in origin
 | 
|---|
| [273382] | 63 |   RefPoint = helper = x - EllipsoidCenter;
 | 
|---|
| [e138de] | 64 |   //Log() << Verbose(4) << "Translated given point is at " << RefPoint << "." << endl;
 | 
|---|
| [042f82] | 65 | 
 | 
|---|
 | 66 |   // 2. transform coordinate system by inverse of rotation matrix and of diagonal matrix
 | 
|---|
 | 67 |   psi = EllipsoidAngle[0];
 | 
|---|
 | 68 |   theta = EllipsoidAngle[1];
 | 
|---|
 | 69 |   phi = EllipsoidAngle[2];
 | 
|---|
| [a679d1] | 70 |   Matrix.set(0,0, cos(psi)*cos(phi) - sin(psi)*cos(theta)*sin(phi));
 | 
|---|
 | 71 |   Matrix.set(1,0, -cos(psi)*sin(phi) - sin(psi)*cos(theta)*cos(phi));
 | 
|---|
 | 72 |   Matrix.set(2,0, sin(psi)*sin(theta));
 | 
|---|
 | 73 |   Matrix.set(0,1, sin(psi)*cos(phi) + cos(psi)*cos(theta)*sin(phi));
 | 
|---|
 | 74 |   Matrix.set(1,1, cos(psi)*cos(theta)*cos(phi) - sin(psi)*sin(phi));
 | 
|---|
 | 75 |   Matrix.set(2,1, -cos(psi)*sin(theta));
 | 
|---|
 | 76 |   Matrix.set(0,2, sin(theta)*sin(phi));
 | 
|---|
 | 77 |   Matrix.set(1,2, sin(theta)*cos(phi));
 | 
|---|
 | 78 |   Matrix.set(2,2, cos(theta));
 | 
|---|
| [5108e1] | 79 |   helper *= Matrix;
 | 
|---|
| [1bd79e] | 80 |   helper.ScaleAll(InverseLength);
 | 
|---|
| [e138de] | 81 |   //Log() << Verbose(4) << "Transformed RefPoint is at " << helper << "." << endl;
 | 
|---|
| [042f82] | 82 | 
 | 
|---|
 | 83 |   // 3. construct intersection point with unit sphere and ray between origin and x
 | 
|---|
 | 84 |   helper.Normalize(); // is simply normalizes vector in distance direction
 | 
|---|
| [e138de] | 85 |   //Log() << Verbose(4) << "Transformed intersection is at " << helper << "." << endl;
 | 
|---|
| [042f82] | 86 | 
 | 
|---|
 | 87 |   // 4. transform back the constructed intersection point
 | 
|---|
 | 88 |   psi = -EllipsoidAngle[0];
 | 
|---|
 | 89 |   theta = -EllipsoidAngle[1];
 | 
|---|
 | 90 |   phi = -EllipsoidAngle[2];
 | 
|---|
| [1bd79e] | 91 |   helper.ScaleAll(EllipsoidLength);
 | 
|---|
| [a679d1] | 92 |   Matrix.set(0,0, cos(psi)*cos(phi) - sin(psi)*cos(theta)*sin(phi));
 | 
|---|
 | 93 |   Matrix.set(1,0, -cos(psi)*sin(phi) - sin(psi)*cos(theta)*cos(phi));
 | 
|---|
 | 94 |   Matrix.set(2,0, sin(psi)*sin(theta));
 | 
|---|
 | 95 |   Matrix.set(0,1, sin(psi)*cos(phi) + cos(psi)*cos(theta)*sin(phi));
 | 
|---|
 | 96 |   Matrix.set(1,1, cos(psi)*cos(theta)*cos(phi) - sin(psi)*sin(phi));
 | 
|---|
 | 97 |   Matrix.set(2,1, -cos(psi)*sin(theta));
 | 
|---|
 | 98 |   Matrix.set(0,2, sin(theta)*sin(phi));
 | 
|---|
 | 99 |   Matrix.set(1,2, sin(theta)*cos(phi));
 | 
|---|
 | 100 |   Matrix.set(2,2, cos(theta));
 | 
|---|
| [5108e1] | 101 |   helper *= Matrix;
 | 
|---|
| [e138de] | 102 |   //Log() << Verbose(4) << "Intersection is at " << helper << "." << endl;
 | 
|---|
| [042f82] | 103 | 
 | 
|---|
 | 104 |   // 5. determine distance between backtransformed point and x
 | 
|---|
| [273382] | 105 |   distance = RefPoint.DistanceSquared(helper);
 | 
|---|
| [e138de] | 106 |   //Log() << Verbose(4) << "Squared distance between intersection and RefPoint is " << distance << "." << endl;
 | 
|---|
| [042f82] | 107 | 
 | 
|---|
 | 108 |   return distance;
 | 
|---|
| [e138de] | 109 |   //Log() << Verbose(3) << "End of SquaredDistanceToEllipsoid" << endl;
 | 
|---|
| [6ac7ee] | 110 | };
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | /** structure for ellipsoid minimisation containing points to fit to.
 | 
|---|
 | 113 |  */
 | 
|---|
 | 114 | struct EllipsoidMinimisation {
 | 
|---|
| [042f82] | 115 |   int N;      //!< dimension of vector set
 | 
|---|
 | 116 |   Vector *x;  //!< array of vectors
 | 
|---|
| [6ac7ee] | 117 | };
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 | /** Sum of squared distance to ellipsoid to be minimised.
 | 
|---|
 | 120 |  * \param *x parameters for the ellipsoid
 | 
|---|
 | 121 |  * \param *params EllipsoidMinimisation with set of data points to minimise distance to and dimension
 | 
|---|
 | 122 |  * \return sum of squared distance, \sa SquaredDistanceToEllipsoid()
 | 
|---|
 | 123 |  */
 | 
|---|
 | 124 | double SumSquaredDistance (const gsl_vector * x, void * params)
 | 
|---|
 | 125 | {
 | 
|---|
| [042f82] | 126 |   Vector *set= ((struct EllipsoidMinimisation *)params)->x;
 | 
|---|
 | 127 |   int N = ((struct EllipsoidMinimisation *)params)->N;
 | 
|---|
 | 128 |   double SumDistance = 0.;
 | 
|---|
 | 129 |   double distance;
 | 
|---|
 | 130 |   Vector Center;
 | 
|---|
 | 131 |   double EllipsoidLength[3], EllipsoidAngle[3];
 | 
|---|
 | 132 | 
 | 
|---|
 | 133 |   // put parameters into suitable ellipsoid form
 | 
|---|
 | 134 |   for (int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 135 |     Center[i] = gsl_vector_get(x, i+0);
 | 
|---|
| [042f82] | 136 |     EllipsoidLength[i] = gsl_vector_get(x, i+3);
 | 
|---|
 | 137 |     EllipsoidAngle[i] = gsl_vector_get(x, i+6);
 | 
|---|
 | 138 |   }
 | 
|---|
 | 139 | 
 | 
|---|
 | 140 |   // go through all points and sum distance
 | 
|---|
 | 141 |   for (int i=0;i<N;i++) {
 | 
|---|
 | 142 |     distance = SquaredDistanceToEllipsoid(set[i], Center, EllipsoidLength, EllipsoidAngle);
 | 
|---|
 | 143 |     if (!isnan(distance)) {
 | 
|---|
 | 144 |       SumDistance += distance;
 | 
|---|
 | 145 |     } else {
 | 
|---|
 | 146 |       SumDistance = GSL_NAN;
 | 
|---|
 | 147 |       break;
 | 
|---|
 | 148 |     }
 | 
|---|
 | 149 |   }
 | 
|---|
 | 150 | 
 | 
|---|
| [e138de] | 151 |   //Log() << Verbose(0) << "Current summed distance is " << SumDistance << "." << endl;
 | 
|---|
| [042f82] | 152 |   return SumDistance;
 | 
|---|
| [6ac7ee] | 153 | };
 | 
|---|
 | 154 | 
 | 
|---|
 | 155 | /** Finds best fitting ellipsoid parameter set in Least square sense for a given point set.
 | 
|---|
 | 156 |  * \param *out output stream for debugging
 | 
|---|
 | 157 |  * \param *set given point set
 | 
|---|
 | 158 |  * \param N number of points in set
 | 
|---|
 | 159 |  * \param EllipsoidParamter[3] three parameters in ellipsoid equation
 | 
|---|
 | 160 |  * \return true - fit successful, false - fit impossible
 | 
|---|
 | 161 |  */
 | 
|---|
| [e138de] | 162 | bool FitPointSetToEllipsoid(Vector *set, int N, Vector *EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
 | 
|---|
| [6ac7ee] | 163 | {
 | 
|---|
| [042f82] | 164 |   int status = GSL_SUCCESS;
 | 
|---|
| [a67d19] | 165 |   DoLog(2) && (Log() << Verbose(2) << "Begin of FitPointSetToEllipsoid " << endl);
 | 
|---|
| [042f82] | 166 |   if (N >= 3) { // check that enough points are given (9 d.o.f.)
 | 
|---|
 | 167 |     struct EllipsoidMinimisation par;
 | 
|---|
 | 168 |     const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
 | 
|---|
 | 169 |     gsl_multimin_fminimizer *s = NULL;
 | 
|---|
 | 170 |     gsl_vector *ss, *x;
 | 
|---|
 | 171 |     gsl_multimin_function minex_func;
 | 
|---|
 | 172 | 
 | 
|---|
 | 173 |     size_t iter = 0;
 | 
|---|
 | 174 |     double size;
 | 
|---|
 | 175 | 
 | 
|---|
 | 176 |     /* Starting point */
 | 
|---|
 | 177 |     x = gsl_vector_alloc (9);
 | 
|---|
 | 178 |     for (int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 179 |       gsl_vector_set (x, i+0, EllipsoidCenter->at(i));
 | 
|---|
| [042f82] | 180 |       gsl_vector_set (x, i+3, EllipsoidLength[i]);
 | 
|---|
 | 181 |       gsl_vector_set (x, i+6, EllipsoidAngle[i]);
 | 
|---|
 | 182 |     }
 | 
|---|
 | 183 |     par.x = set;
 | 
|---|
 | 184 |     par.N = N;
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 |     /* Set initial step sizes */
 | 
|---|
 | 187 |     ss = gsl_vector_alloc (9);
 | 
|---|
 | 188 |     for (int i=0;i<3;i++) {
 | 
|---|
 | 189 |       gsl_vector_set (ss, i+0, 0.1);
 | 
|---|
 | 190 |       gsl_vector_set (ss, i+3, 1.0);
 | 
|---|
 | 191 |       gsl_vector_set (ss, i+6, M_PI/20.);
 | 
|---|
 | 192 |     }
 | 
|---|
 | 193 | 
 | 
|---|
 | 194 |     /* Initialize method and iterate */
 | 
|---|
 | 195 |     minex_func.n = 9;
 | 
|---|
 | 196 |     minex_func.f = &SumSquaredDistance;
 | 
|---|
 | 197 |     minex_func.params = (void *)∥
 | 
|---|
 | 198 | 
 | 
|---|
 | 199 |     s = gsl_multimin_fminimizer_alloc (T, 9);
 | 
|---|
 | 200 |     gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |     do {
 | 
|---|
 | 203 |       iter++;
 | 
|---|
 | 204 |       status = gsl_multimin_fminimizer_iterate(s);
 | 
|---|
 | 205 | 
 | 
|---|
 | 206 |       if (status)
 | 
|---|
 | 207 |         break;
 | 
|---|
 | 208 | 
 | 
|---|
 | 209 |       size = gsl_multimin_fminimizer_size (s);
 | 
|---|
 | 210 |       status = gsl_multimin_test_size (size, 1e-2);
 | 
|---|
 | 211 | 
 | 
|---|
 | 212 |       if (status == GSL_SUCCESS) {
 | 
|---|
 | 213 |         for (int i=0;i<3;i++) {
 | 
|---|
| [0a4f7f] | 214 |           EllipsoidCenter->at(i) = gsl_vector_get (s->x,i+0);
 | 
|---|
| [042f82] | 215 |           EllipsoidLength[i] = gsl_vector_get (s->x, i+3);
 | 
|---|
 | 216 |           EllipsoidAngle[i] = gsl_vector_get (s->x, i+6);
 | 
|---|
 | 217 |         }
 | 
|---|
| [a67d19] | 218 |         DoLog(4) && (Log() << Verbose(4) << setprecision(3) << "Converged fit at: " << *EllipsoidCenter << ", lengths " << EllipsoidLength[0] << ", " << EllipsoidLength[1] << ", " << EllipsoidLength[2] << ", angles " << EllipsoidAngle[0] << ", " << EllipsoidAngle[1] << ", " << EllipsoidAngle[2] << " with summed distance " << s->fval << "." << endl);
 | 
|---|
| [042f82] | 219 |       }
 | 
|---|
 | 220 | 
 | 
|---|
 | 221 |     } while (status == GSL_CONTINUE && iter < 1000);
 | 
|---|
 | 222 | 
 | 
|---|
 | 223 |     gsl_vector_free(x);
 | 
|---|
 | 224 |     gsl_vector_free(ss);
 | 
|---|
 | 225 |     gsl_multimin_fminimizer_free (s);
 | 
|---|
 | 226 | 
 | 
|---|
 | 227 |   } else {
 | 
|---|
| [a67d19] | 228 |     DoLog(3) && (Log() << Verbose(3) << "Not enough points provided for fit to ellipsoid." << endl);
 | 
|---|
| [042f82] | 229 |     return false;
 | 
|---|
 | 230 |   }
 | 
|---|
| [a67d19] | 231 |   DoLog(2) && (Log() << Verbose(2) << "End of FitPointSetToEllipsoid" << endl);
 | 
|---|
| [042f82] | 232 |   if (status == GSL_SUCCESS)
 | 
|---|
 | 233 |     return true;
 | 
|---|
 | 234 |   else
 | 
|---|
 | 235 |     return false;
 | 
|---|
| [6ac7ee] | 236 | };
 | 
|---|
 | 237 | 
 | 
|---|
 | 238 | /** Picks a number of random points from a LC neighbourhood as a fitting set.
 | 
|---|
 | 239 |  * \param *out output stream for debugging
 | 
|---|
 | 240 |  * \param *T Tesselation containing boundary points
 | 
|---|
 | 241 |  * \param *LC linked cell list of all atoms
 | 
|---|
 | 242 |  * \param *&x random point set on return (not allocated!)
 | 
|---|
 | 243 |  * \param PointsToPick number of points in set to pick
 | 
|---|
 | 244 |  */
 | 
|---|
| [e138de] | 245 | void PickRandomNeighbouredPointSet(class Tesselation *T, class LinkedCell *LC, Vector *&x, size_t PointsToPick)
 | 
|---|
| [6ac7ee] | 246 | {
 | 
|---|
| [70c333f] | 247 |   size_t PointsLeft = 0;
 | 
|---|
 | 248 |   size_t PointsPicked = 0;
 | 
|---|
| [042f82] | 249 |   int Nlower[NDIM], Nupper[NDIM];
 | 
|---|
 | 250 |   set<int> PickedAtomNrs;   // ordered list of picked atoms
 | 
|---|
 | 251 |   set<int>::iterator current;
 | 
|---|
 | 252 |   int index;
 | 
|---|
| [357fba] | 253 |   TesselPoint *Candidate = NULL;
 | 
|---|
| [a67d19] | 254 |   DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl);
 | 
|---|
| [042f82] | 255 | 
 | 
|---|
 | 256 |   // allocate array
 | 
|---|
 | 257 |   if (x == NULL) {
 | 
|---|
 | 258 |     x = new Vector[PointsToPick];
 | 
|---|
 | 259 |   } else {
 | 
|---|
| [58ed4a] | 260 |     DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl);
 | 
|---|
| [042f82] | 261 |   }
 | 
|---|
 | 262 | 
 | 
|---|
| [a5028f] | 263 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator("mt19937", "uniform_int");
 | 
|---|
 | 264 |   // check that random number generator's bounds are ok
 | 
|---|
 | 265 |   ASSERT(random.min() == 0,
 | 
|---|
 | 266 |       "PickRandomNeighbouredPointSet: Chosen RandomNumberGenerator's min "
 | 
|---|
 | 267 |       +toString(random.min())+" is not 0!");
 | 
|---|
 | 268 |   ASSERT(random.max() >= LC->N[0],
 | 
|---|
 | 269 |       "PickRandomNeighbouredPointSet: Chosen RandomNumberGenerator's max "
 | 
|---|
 | 270 |       +toString(random.max())+" is too small"+toString(LC->N[0])
 | 
|---|
 | 271 |       +" for axis 0!");
 | 
|---|
 | 272 |   ASSERT(random.max() >= LC->N[1],
 | 
|---|
 | 273 |       "PickRandomNeighbouredPointSet: Chosen RandomNumberGenerator's max "
 | 
|---|
 | 274 |       +toString(random.max())+" is too small"+toString(LC->N[1])
 | 
|---|
 | 275 |       +" for axis 1!");
 | 
|---|
 | 276 |   ASSERT(random.max() >= LC->N[2],
 | 
|---|
 | 277 |       "PickRandomNeighbouredPointSet: Chosen RandomNumberGenerator's max "
 | 
|---|
 | 278 |       +toString(random.max())+" is too small"+toString(LC->N[2])
 | 
|---|
 | 279 |       +" for axis 2!");
 | 
|---|
 | 280 | 
 | 
|---|
| [042f82] | 281 |   do {
 | 
|---|
 | 282 |     for(int i=0;i<NDIM;i++) // pick three random indices
 | 
|---|
| [a5028f] | 283 |       LC->n[i] = ((int)random() % LC->N[i]);
 | 
|---|
| [a67d19] | 284 |     DoLog(2) && (Log() << Verbose(2) << "INFO: Center cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " ... ");
 | 
|---|
| [042f82] | 285 |     // get random cell
 | 
|---|
| [734816] | 286 |     const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| [042f82] | 287 |     if (List == NULL) {  // set index to it
 | 
|---|
 | 288 |       continue;
 | 
|---|
 | 289 |     }
 | 
|---|
| [a67d19] | 290 |     DoLog(2) && (Log() << Verbose(2) << "with No. " << LC->index << "." << endl);
 | 
|---|
| [042f82] | 291 | 
 | 
|---|
| [a67d19] | 292 |     DoLog(2) && (Log() << Verbose(2) << "LC Intervals:");
 | 
|---|
| [042f82] | 293 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 294 |       Nlower[i] = ((LC->n[i]-1) >= 0) ? LC->n[i]-1 : 0;
 | 
|---|
 | 295 |       Nupper[i] = ((LC->n[i]+1) < LC->N[i]) ? LC->n[i]+1 : LC->N[i]-1;
 | 
|---|
| [a67d19] | 296 |       DoLog(0) && (Log() << Verbose(0) << " [" << Nlower[i] << "," << Nupper[i] << "] ");
 | 
|---|
| [042f82] | 297 |     }
 | 
|---|
| [a67d19] | 298 |     DoLog(0) && (Log() << Verbose(0) << endl);
 | 
|---|
| [042f82] | 299 | 
 | 
|---|
 | 300 |     // count whether there are sufficient atoms in this cell+neighbors
 | 
|---|
 | 301 |     PointsLeft=0;
 | 
|---|
 | 302 |     for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
 | 303 |       for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
 | 304 |         for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| [734816] | 305 |           const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| [042f82] | 306 |           PointsLeft += List->size();
 | 
|---|
 | 307 |         }
 | 
|---|
| [a67d19] | 308 |     DoLog(2) && (Log() << Verbose(2) << "There are " << PointsLeft << " atoms in this neighbourhood." << endl);
 | 
|---|
| [042f82] | 309 |     if (PointsLeft < PointsToPick) {  // ensure that we can pick enough points in its neighbourhood at all.
 | 
|---|
 | 310 |       continue;
 | 
|---|
 | 311 |     }
 | 
|---|
 | 312 | 
 | 
|---|
 | 313 |     // pre-pick a fixed number of atoms
 | 
|---|
 | 314 |     PickedAtomNrs.clear();
 | 
|---|
 | 315 |     do {
 | 
|---|
| [a5028f] | 316 |       index = (((int)random()) % PointsLeft);
 | 
|---|
| [042f82] | 317 |       current = PickedAtomNrs.find(index);  // not present?
 | 
|---|
 | 318 |       if (current == PickedAtomNrs.end()) {
 | 
|---|
| [e138de] | 319 |         //Log() << Verbose(2) << "Picking atom nr. " << index << "." << endl;
 | 
|---|
| [042f82] | 320 |         PickedAtomNrs.insert(index);
 | 
|---|
 | 321 |       }
 | 
|---|
 | 322 |     } while (PickedAtomNrs.size() < PointsToPick);
 | 
|---|
 | 323 | 
 | 
|---|
 | 324 |     index = 0; // now go through all and pick those whose from PickedAtomsNr
 | 
|---|
 | 325 |     PointsPicked=0;
 | 
|---|
 | 326 |     current = PickedAtomNrs.begin();
 | 
|---|
 | 327 |     for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
 | 
|---|
 | 328 |       for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
 | 
|---|
 | 329 |         for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
 | 
|---|
| [734816] | 330 |           const LinkedCell::LinkedNodes *List = LC->GetCurrentCell();
 | 
|---|
| [e138de] | 331 | //          Log() << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl;
 | 
|---|
| [042f82] | 332 |           if (List != NULL) {
 | 
|---|
 | 333 | //            if (List->begin() != List->end())
 | 
|---|
| [e138de] | 334 | //              Log() << Verbose(2) << "Going through candidates ... " << endl;
 | 
|---|
| [042f82] | 335 | //            else
 | 
|---|
| [e138de] | 336 | //              Log() << Verbose(2) << "Cell is empty ... " << endl;
 | 
|---|
| [734816] | 337 |             for (LinkedCell::LinkedNodes::const_iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
| [042f82] | 338 |               if ((current != PickedAtomNrs.end()) && (*current == index)) {
 | 
|---|
 | 339 |                 Candidate = (*Runner);
 | 
|---|
| [d74077] | 340 |                 DoLog(2) && (Log() << Verbose(2) << "Current picked node is " << (*Runner)->getName() << " with index " << index << "." << endl);
 | 
|---|
 | 341 |                 x[PointsPicked++] = Candidate->getPosition();    // we have one more atom picked
 | 
|---|
| [042f82] | 342 |                 current++;    // next pre-picked atom
 | 
|---|
 | 343 |               }
 | 
|---|
 | 344 |               index++;  // next atom nr.
 | 
|---|
 | 345 |             }
 | 
|---|
 | 346 | //          } else {
 | 
|---|
| [e138de] | 347 | //            Log() << Verbose(2) << "List for this index not allocated!" << endl;
 | 
|---|
| [042f82] | 348 |           }
 | 
|---|
 | 349 |         }
 | 
|---|
| [a67d19] | 350 |     DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl);
 | 
|---|
| [042f82] | 351 |     for (size_t i=0;i<PointsPicked;i++)
 | 
|---|
| [a67d19] | 352 |       DoLog(2) && (Log() << Verbose(2) << x[i] << endl);
 | 
|---|
| [042f82] | 353 |     if (PointsPicked == PointsToPick)  // break out of loop if we have all
 | 
|---|
 | 354 |       break;
 | 
|---|
 | 355 |   } while(1);
 | 
|---|
 | 356 | 
 | 
|---|
| [a67d19] | 357 |   DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl);
 | 
|---|
| [6ac7ee] | 358 | };
 | 
|---|
 | 359 | 
 | 
|---|
 | 360 | /** Picks a number of random points from a set of boundary points as a fitting set.
 | 
|---|
 | 361 |  * \param *out output stream for debugging
 | 
|---|
 | 362 |  * \param *T Tesselation containing boundary points
 | 
|---|
 | 363 |  * \param *&x random point set on return (not allocated!)
 | 
|---|
 | 364 |  * \param PointsToPick number of points in set to pick
 | 
|---|
 | 365 |  */
 | 
|---|
| [e138de] | 366 | void PickRandomPointSet(class Tesselation *T, Vector *&x, size_t PointsToPick)
 | 
|---|
| [6ac7ee] | 367 | {
 | 
|---|
| [70c333f] | 368 |   size_t PointsLeft = (size_t) T->PointsOnBoundaryCount;
 | 
|---|
 | 369 |   size_t PointsPicked = 0;
 | 
|---|
| [042f82] | 370 |   double value, threshold;
 | 
|---|
 | 371 |   PointMap *List = &T->PointsOnBoundary;
 | 
|---|
| [a67d19] | 372 |   DoLog(2) && (Log() << Verbose(2) << "Begin of PickRandomPointSet" << endl);
 | 
|---|
| [042f82] | 373 | 
 | 
|---|
 | 374 |   // allocate array
 | 
|---|
 | 375 |   if (x == NULL) {
 | 
|---|
 | 376 |     x = new Vector[PointsToPick];
 | 
|---|
 | 377 |   } else {
 | 
|---|
| [58ed4a] | 378 |     DoeLog(2) && (eLog()<< Verbose(2) << "Given pointer to vector array seems already allocated." << endl);
 | 
|---|
| [042f82] | 379 |   }
 | 
|---|
 | 380 | 
 | 
|---|
| [a5028f] | 381 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator("mt19937", "uniform_int");
 | 
|---|
 | 382 |   const double rng_min = random.min();
 | 
|---|
 | 383 |   const double rng_max = random.max();
 | 
|---|
| [042f82] | 384 |   if (List != NULL)
 | 
|---|
 | 385 |     for (PointMap::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
 | 
|---|
 | 386 |       threshold = 1. - (double)(PointsToPick - PointsPicked)/(double)PointsLeft;
 | 
|---|
| [a5028f] | 387 |       value = (double)random()/(double)(rng_max-rng_min);
 | 
|---|
| [e138de] | 388 |       //Log() << Verbose(3) << "Current node is " << *Runner->second->node << " with " << value << " ... " << threshold << ": ";
 | 
|---|
| [042f82] | 389 |       if (value > threshold) {
 | 
|---|
| [d74077] | 390 |         x[PointsPicked] = (Runner->second->node->getPosition());
 | 
|---|
| [042f82] | 391 |         PointsPicked++;
 | 
|---|
| [e138de] | 392 |         //Log() << Verbose(0) << "IN." << endl;
 | 
|---|
| [042f82] | 393 |       } else {
 | 
|---|
| [e138de] | 394 |         //Log() << Verbose(0) << "OUT." << endl;
 | 
|---|
| [042f82] | 395 |       }
 | 
|---|
 | 396 |       PointsLeft--;
 | 
|---|
 | 397 |     }
 | 
|---|
| [a67d19] | 398 |   DoLog(2) && (Log() << Verbose(2) << "The following points were picked: " << endl);
 | 
|---|
| [042f82] | 399 |   for (size_t i=0;i<PointsPicked;i++)
 | 
|---|
| [a67d19] | 400 |     DoLog(3) && (Log() << Verbose(3) << x[i] << endl);
 | 
|---|
| [042f82] | 401 | 
 | 
|---|
| [a67d19] | 402 |   DoLog(2) && (Log() << Verbose(2) << "End of PickRandomPointSet" << endl);
 | 
|---|
| [6ac7ee] | 403 | };
 | 
|---|
 | 404 | 
 | 
|---|
 | 405 | /** Finds best fitting ellipsoid parameter set in least square sense for a given point set.
 | 
|---|
 | 406 |  * \param *out output stream for debugging
 | 
|---|
 | 407 |  * \param *T Tesselation containing boundary points
 | 
|---|
 | 408 |  * \param *LCList linked cell list of all atoms
 | 
|---|
 | 409 |  * \param N number of unique points in ellipsoid fit, must be greater equal 6
 | 
|---|
 | 410 |  * \param number of fits (i.e. parameter sets in output file)
 | 
|---|
 | 411 |  * \param *filename name for output file
 | 
|---|
 | 412 |  */
 | 
|---|
| [e138de] | 413 | void FindDistributionOfEllipsoids(class Tesselation *T, class LinkedCell *LCList, int N, int number, const char *filename)
 | 
|---|
| [6ac7ee] | 414 | {
 | 
|---|
| [042f82] | 415 |   ofstream output;
 | 
|---|
 | 416 |   Vector *x = NULL;
 | 
|---|
 | 417 |   Vector Center;
 | 
|---|
 | 418 |   Vector EllipsoidCenter;
 | 
|---|
 | 419 |   double EllipsoidLength[3];
 | 
|---|
 | 420 |   double EllipsoidAngle[3];
 | 
|---|
 | 421 |   double distance, MaxDistance, MinDistance;
 | 
|---|
| [a67d19] | 422 |   DoLog(0) && (Log() << Verbose(0) << "Begin of FindDistributionOfEllipsoids" << endl);
 | 
|---|
| [042f82] | 423 | 
 | 
|---|
 | 424 |   // construct center of gravity of boundary point set for initial ellipsoid center
 | 
|---|
 | 425 |   Center.Zero();
 | 
|---|
 | 426 |   for (PointMap::iterator Runner = T->PointsOnBoundary.begin(); Runner != T->PointsOnBoundary.end(); Runner++)
 | 
|---|
| [d74077] | 427 |     Center += (Runner->second->node->getPosition());
 | 
|---|
| [042f82] | 428 |   Center.Scale(1./T->PointsOnBoundaryCount);
 | 
|---|
| [a67d19] | 429 |   DoLog(1) && (Log() << Verbose(1) << "Center is at " << Center << "." << endl);
 | 
|---|
| [042f82] | 430 | 
 | 
|---|
 | 431 |   // Output header
 | 
|---|
 | 432 |   output.open(filename, ios::trunc);
 | 
|---|
 | 433 |   output << "# Nr.\tCenterX\tCenterY\tCenterZ\ta\tb\tc\tpsi\ttheta\tphi" << endl;
 | 
|---|
 | 434 | 
 | 
|---|
 | 435 |   // loop over desired number of parameter sets
 | 
|---|
 | 436 |   for (;number >0;number--) {
 | 
|---|
| [a67d19] | 437 |     DoLog(1) && (Log() << Verbose(1) << "Determining data set " << number << " ... " << endl);
 | 
|---|
| [042f82] | 438 |     // pick the point set
 | 
|---|
 | 439 |     x = NULL;
 | 
|---|
| [e138de] | 440 |     //PickRandomPointSet(T, LCList, x, N);
 | 
|---|
 | 441 |     PickRandomNeighbouredPointSet(T, LCList, x, N);
 | 
|---|
| [042f82] | 442 | 
 | 
|---|
 | 443 |     // calculate some sensible starting values for parameter fit
 | 
|---|
 | 444 |     MaxDistance = 0.;
 | 
|---|
| [273382] | 445 |     MinDistance = x[0].ScalarProduct(x[0]);
 | 
|---|
| [042f82] | 446 |     for (int i=0;i<N;i++) {
 | 
|---|
| [273382] | 447 |       distance = x[i].ScalarProduct(x[i]);
 | 
|---|
| [042f82] | 448 |       if (distance > MaxDistance)
 | 
|---|
 | 449 |         MaxDistance = distance;
 | 
|---|
 | 450 |       if (distance < MinDistance)
 | 
|---|
 | 451 |         MinDistance = distance;
 | 
|---|
 | 452 |     }
 | 
|---|
| [e138de] | 453 |     //Log() << Verbose(2) << "MinDistance " << MinDistance << ", MaxDistance " << MaxDistance << "." << endl;
 | 
|---|
| [273382] | 454 |     EllipsoidCenter = Center;  // use Center of Gravity as initial center of ellipsoid
 | 
|---|
| [042f82] | 455 |     for (int i=0;i<3;i++)
 | 
|---|
 | 456 |       EllipsoidAngle[i] = 0.;
 | 
|---|
 | 457 |     EllipsoidLength[0] = sqrt(MaxDistance);
 | 
|---|
 | 458 |     EllipsoidLength[1] = sqrt((MaxDistance+MinDistance)/2.);
 | 
|---|
 | 459 |     EllipsoidLength[2] = sqrt(MinDistance);
 | 
|---|
 | 460 | 
 | 
|---|
 | 461 |     // fit the parameters
 | 
|---|
| [e138de] | 462 |     if (FitPointSetToEllipsoid(x, N, &EllipsoidCenter, &EllipsoidLength[0], &EllipsoidAngle[0])) {
 | 
|---|
| [a67d19] | 463 |       DoLog(1) && (Log() << Verbose(1) << "Picking succeeded!" << endl);
 | 
|---|
| [042f82] | 464 |       // output obtained parameter set
 | 
|---|
 | 465 |       output << number << "\t";
 | 
|---|
 | 466 |       for (int i=0;i<3;i++)
 | 
|---|
| [0a4f7f] | 467 |         output << setprecision(9) << EllipsoidCenter[i] << "\t";
 | 
|---|
| [042f82] | 468 |       for (int i=0;i<3;i++)
 | 
|---|
 | 469 |         output << setprecision(9) << EllipsoidLength[i] << "\t";
 | 
|---|
 | 470 |       for (int i=0;i<3;i++)
 | 
|---|
 | 471 |         output << setprecision(9) << EllipsoidAngle[i] << "\t";
 | 
|---|
 | 472 |       output << endl;
 | 
|---|
 | 473 |     } else { // increase N to pick one more
 | 
|---|
| [a67d19] | 474 |       DoLog(1) && (Log() << Verbose(1) << "Picking failed!" << endl);
 | 
|---|
| [042f82] | 475 |       number++;
 | 
|---|
 | 476 |     }
 | 
|---|
 | 477 |     delete[](x);  // free allocated memory for point set
 | 
|---|
 | 478 |   }
 | 
|---|
 | 479 |   // close output and finish
 | 
|---|
 | 480 |   output.close();
 | 
|---|
 | 481 | 
 | 
|---|
| [a67d19] | 482 |   DoLog(0) && (Log() << Verbose(0) << "End of FindDistributionOfEllipsoids" << endl);
 | 
|---|
| [6ac7ee] | 483 | };
 | 
|---|