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