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