| 1 | /** \file vector.cpp
 | 
|---|
| 2 |  * 
 | 
|---|
| 3 |  * Function implementations for the class vector.
 | 
|---|
| 4 |  * 
 | 
|---|
| 5 |  */
 | 
|---|
| 6 |  
 | 
|---|
| 7 | #include "molecules.hpp"
 | 
|---|
| 8 |  
 | 
|---|
| 9 | 
 | 
|---|
| 10 | /************************************ Functions for class vector ************************************/
 | 
|---|
| 11 | 
 | 
|---|
| 12 | /** Constructor of class vector.
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | Vector::Vector() { x[0] = x[1] = x[2] = 0.; };
 | 
|---|
| 15 | 
 | 
|---|
| 16 | /** Constructor of class vector.
 | 
|---|
| 17 |  */
 | 
|---|
| 18 | Vector::Vector(double x1, double x2, double x3) { x[0] = x1; x[1] = x2; x[2] = x3; };
 | 
|---|
| 19 | 
 | 
|---|
| 20 | /** Desctructor of class vector.
 | 
|---|
| 21 |  */
 | 
|---|
| 22 | Vector::~Vector() {};
 | 
|---|
| 23 | 
 | 
|---|
| 24 | /** Calculates distance between this and another vector.
 | 
|---|
| 25 |  * \param *y array to second vector
 | 
|---|
| 26 |  * \return \f$| x - y |^2\f$
 | 
|---|
| 27 |  */
 | 
|---|
| 28 | double Vector::Distance(const Vector *y) const
 | 
|---|
| 29 | {
 | 
|---|
| 30 |   double res = 0.;
 | 
|---|
| 31 |   for (int i=NDIM;i--;)
 | 
|---|
| 32 |     res += (x[i]-y->x[i])*(x[i]-y->x[i]);
 | 
|---|
| 33 |   return (res);  
 | 
|---|
| 34 | };
 | 
|---|
| 35 | 
 | 
|---|
| 36 | /** Calculates distance between this and another vector in a periodic cell.
 | 
|---|
| 37 |  * \param *y array to second vector
 | 
|---|
| 38 |  * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
 | 
|---|
| 39 |  * \return \f$| x - y |^2\f$
 | 
|---|
| 40 |  */
 | 
|---|
| 41 | double Vector::PeriodicDistance(const Vector *y, const double *cell_size) const
 | 
|---|
| 42 | {
 | 
|---|
| 43 |   double res = Distance(y), tmp, matrix[NDIM*NDIM];
 | 
|---|
| 44 |   Vector Shiftedy, TranslationVector;
 | 
|---|
| 45 |   int N[NDIM];
 | 
|---|
| 46 |   matrix[0] = cell_size[0];
 | 
|---|
| 47 |   matrix[1] = cell_size[1];
 | 
|---|
| 48 |   matrix[2] = cell_size[3];
 | 
|---|
| 49 |   matrix[3] = cell_size[1];
 | 
|---|
| 50 |   matrix[4] = cell_size[2];
 | 
|---|
| 51 |   matrix[5] = cell_size[4];
 | 
|---|
| 52 |   matrix[6] = cell_size[3];
 | 
|---|
| 53 |   matrix[7] = cell_size[4];
 | 
|---|
| 54 |   matrix[8] = cell_size[5];
 | 
|---|
| 55 |   // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
 | 
|---|
| 56 |   for (N[0]=-1;N[0]<=1;N[0]++)
 | 
|---|
| 57 |     for (N[1]=-1;N[1]<=1;N[1]++)
 | 
|---|
| 58 |       for (N[2]=-1;N[2]<=1;N[2]++) {
 | 
|---|
| 59 |         // create the translation vector
 | 
|---|
| 60 |         TranslationVector.Zero();
 | 
|---|
| 61 |         for (int i=NDIM;i--;)
 | 
|---|
| 62 |           TranslationVector.x[i] = (double)N[i];
 | 
|---|
| 63 |         TranslationVector.MatrixMultiplication(matrix);
 | 
|---|
| 64 |         // add onto the original vector to compare with
 | 
|---|
| 65 |         Shiftedy.CopyVector(y);
 | 
|---|
| 66 |         Shiftedy.AddVector(&TranslationVector);
 | 
|---|
| 67 |         // get distance and compare with minimum so far
 | 
|---|
| 68 |         tmp = Distance(&Shiftedy);
 | 
|---|
| 69 |         if (tmp < res) res = tmp;
 | 
|---|
| 70 |       }
 | 
|---|
| 71 |   return (res);  
 | 
|---|
| 72 | };
 | 
|---|
| 73 | 
 | 
|---|
| 74 | /** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
 | 
|---|
| 75 |  * \param *out ofstream for debugging messages
 | 
|---|
| 76 |  * Tries to translate a vector into each adjacent neighbouring cell.
 | 
|---|
| 77 |  */
 | 
|---|
| 78 | void Vector::KeepPeriodic(ofstream *out, double *matrix)
 | 
|---|
| 79 | {
 | 
|---|
| 80 | //  int N[NDIM];
 | 
|---|
| 81 | //  bool flag = false;
 | 
|---|
| 82 |   //vector Shifted, TranslationVector;
 | 
|---|
| 83 |   Vector TestVector;
 | 
|---|
| 84 | //  *out << Verbose(1) << "Begin of KeepPeriodic." << endl;
 | 
|---|
| 85 | //  *out << Verbose(2) << "Vector is: ";
 | 
|---|
| 86 | //  Output(out);
 | 
|---|
| 87 | //  *out << endl;
 | 
|---|
| 88 |   TestVector.CopyVector(this);
 | 
|---|
| 89 |   TestVector.InverseMatrixMultiplication(matrix);
 | 
|---|
| 90 |   for(int i=NDIM;i--;) { // correct periodically
 | 
|---|
| 91 |     if (TestVector.x[i] < 0) {  // get every coefficient into the interval [0,1)
 | 
|---|
| 92 |       TestVector.x[i] += ceil(TestVector.x[i]);
 | 
|---|
| 93 |     } else {
 | 
|---|
| 94 |       TestVector.x[i] -= floor(TestVector.x[i]);
 | 
|---|
| 95 |     }
 | 
|---|
| 96 |   }
 | 
|---|
| 97 |   TestVector.MatrixMultiplication(matrix);
 | 
|---|
| 98 |   CopyVector(&TestVector);
 | 
|---|
| 99 | //  *out << Verbose(2) << "New corrected vector is: ";
 | 
|---|
| 100 | //  Output(out);
 | 
|---|
| 101 | //  *out << endl;
 | 
|---|
| 102 | //  *out << Verbose(1) << "End of KeepPeriodic." << endl;
 | 
|---|
| 103 | };
 | 
|---|
| 104 | 
 | 
|---|
| 105 | /** Calculates scalar product between this and another vector.
 | 
|---|
| 106 |  * \param *y array to second vector
 | 
|---|
| 107 |  * \return \f$\langle x, y \rangle\f$
 | 
|---|
| 108 |  */
 | 
|---|
| 109 | double Vector::ScalarProduct(const Vector *y) const
 | 
|---|
| 110 | {
 | 
|---|
| 111 |   double res = 0.;
 | 
|---|
| 112 |   for (int i=NDIM;i--;)
 | 
|---|
| 113 |     res += x[i]*y->x[i];
 | 
|---|
| 114 |   return (res);  
 | 
|---|
| 115 | };
 | 
|---|
| 116 | 
 | 
|---|
| 117 | /** projects this vector onto plane defined by \a *y.
 | 
|---|
| 118 |  * \param *y array to normal vector of plane
 | 
|---|
| 119 |  * \return \f$\langle x, y \rangle\f$
 | 
|---|
| 120 |  */
 | 
|---|
| 121 | void Vector::ProjectOntoPlane(const Vector *y)
 | 
|---|
| 122 | {
 | 
|---|
| 123 |   Vector tmp;
 | 
|---|
| 124 |   tmp.CopyVector(y);
 | 
|---|
| 125 |   tmp.Scale(Projection(y));
 | 
|---|
| 126 |   this->SubtractVector(&tmp);
 | 
|---|
| 127 | };
 | 
|---|
| 128 | 
 | 
|---|
| 129 | /** Calculates the projection of a vector onto another \a *y.
 | 
|---|
| 130 |  * \param *y array to second vector
 | 
|---|
| 131 |  * \return \f$\langle x, y \rangle\f$
 | 
|---|
| 132 |  */
 | 
|---|
| 133 | double Vector::Projection(const Vector *y) const
 | 
|---|
| 134 | {
 | 
|---|
| 135 |   return (ScalarProduct(y));
 | 
|---|
| 136 | };
 | 
|---|
| 137 | 
 | 
|---|
| 138 | /** Calculates norm of this vector.
 | 
|---|
| 139 |  * \return \f$|x|\f$
 | 
|---|
| 140 |  */
 | 
|---|
| 141 | double Vector::Norm() const
 | 
|---|
| 142 | {
 | 
|---|
| 143 |   double res = 0.;
 | 
|---|
| 144 |   for (int i=NDIM;i--;)
 | 
|---|
| 145 |     res += this->x[i]*this->x[i];
 | 
|---|
| 146 |   return (sqrt(res));  
 | 
|---|
| 147 | };
 | 
|---|
| 148 | 
 | 
|---|
| 149 | /** Normalizes this vector.
 | 
|---|
| 150 |  */
 | 
|---|
| 151 | void Vector::Normalize()
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   double res = 0.;
 | 
|---|
| 154 |   for (int i=NDIM;i--;)
 | 
|---|
| 155 |     res += this->x[i]*this->x[i];
 | 
|---|
| 156 |   if (fabs(res) > MYEPSILON)
 | 
|---|
| 157 |     res = 1./sqrt(res);
 | 
|---|
| 158 |   Scale(&res);
 | 
|---|
| 159 | };
 | 
|---|
| 160 | 
 | 
|---|
| 161 | /** Zeros all components of this vector.
 | 
|---|
| 162 |  */
 | 
|---|
| 163 | void Vector::Zero()
 | 
|---|
| 164 | {
 | 
|---|
| 165 |   for (int i=NDIM;i--;)
 | 
|---|
| 166 |     this->x[i] = 0.;
 | 
|---|
| 167 | };
 | 
|---|
| 168 | 
 | 
|---|
| 169 | /** Zeros all components of this vector.
 | 
|---|
| 170 |  */
 | 
|---|
| 171 | void Vector::One(double one)
 | 
|---|
| 172 | {
 | 
|---|
| 173 |   for (int i=NDIM;i--;)
 | 
|---|
| 174 |     this->x[i] = one;
 | 
|---|
| 175 | };
 | 
|---|
| 176 | 
 | 
|---|
| 177 | /** Initialises all components of this vector.
 | 
|---|
| 178 |  */
 | 
|---|
| 179 | void Vector::Init(double x1, double x2, double x3)
 | 
|---|
| 180 | { 
 | 
|---|
| 181 |   x[0] = x1;
 | 
|---|
| 182 |   x[1] = x2;
 | 
|---|
| 183 |   x[2] = x3;
 | 
|---|
| 184 | };
 | 
|---|
| 185 | 
 | 
|---|
| 186 | /** Calculates the angle between this and another vector.
 | 
|---|
| 187 |  * \param *y array to second vector
 | 
|---|
| 188 |  * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
 | 
|---|
| 189 |  */
 | 
|---|
| 190 | double Vector::Angle(Vector *y) const
 | 
|---|
| 191 | {
 | 
|---|
| 192 |   return acos(this->ScalarProduct(y)/Norm()/y->Norm()); 
 | 
|---|
| 193 | };
 | 
|---|
| 194 | 
 | 
|---|
| 195 | /** Rotates the vector around the axis given by \a *axis by an angle of \a alpha.
 | 
|---|
| 196 |  * \param *axis rotation axis
 | 
|---|
| 197 |  * \param alpha rotation angle in radian
 | 
|---|
| 198 |  */
 | 
|---|
| 199 | void Vector::RotateVector(const Vector *axis, const double alpha)
 | 
|---|
| 200 | {
 | 
|---|
| 201 |   Vector a,y;
 | 
|---|
| 202 |   // normalise this vector with respect to axis
 | 
|---|
| 203 |   a.CopyVector(this);
 | 
|---|
| 204 |   a.Scale(Projection(axis));
 | 
|---|
| 205 |   SubtractVector(&a);
 | 
|---|
| 206 |   // construct normal vector
 | 
|---|
| 207 |   y.MakeNormalVector(axis,this);
 | 
|---|
| 208 |   y.Scale(Norm());
 | 
|---|
| 209 |   // scale normal vector by sine and this vector by cosine
 | 
|---|
| 210 |   y.Scale(sin(alpha));
 | 
|---|
| 211 |   Scale(cos(alpha));
 | 
|---|
| 212 |   // add scaled normal vector onto this vector
 | 
|---|
| 213 |   AddVector(&y);
 | 
|---|
| 214 |   // add part in axis direction
 | 
|---|
| 215 |   AddVector(&a);
 | 
|---|
| 216 | };
 | 
|---|
| 217 | 
 | 
|---|
| 218 | /** Sums vector \a to this lhs component-wise.
 | 
|---|
| 219 |  * \param a base vector
 | 
|---|
| 220 |  * \param b vector components to add
 | 
|---|
| 221 |  * \return lhs + a
 | 
|---|
| 222 |  */
 | 
|---|
| 223 | Vector& operator+=(Vector& a, const Vector& b)
 | 
|---|
| 224 | {
 | 
|---|
| 225 |   a.AddVector(&b);
 | 
|---|
| 226 |   return a;
 | 
|---|
| 227 | };
 | 
|---|
| 228 | /** factor each component of \a a times a double \a m.
 | 
|---|
| 229 |  * \param a base vector
 | 
|---|
| 230 |  * \param m factor
 | 
|---|
| 231 |  * \return lhs.x[i] * m
 | 
|---|
| 232 |  */
 | 
|---|
| 233 | Vector& operator*=(Vector& a, const double m)
 | 
|---|
| 234 | {
 | 
|---|
| 235 |   a.Scale(m);
 | 
|---|
| 236 |   return a;
 | 
|---|
| 237 | };
 | 
|---|
| 238 | 
 | 
|---|
| 239 | /** Sums two vectors \a  and \b component-wise.
 | 
|---|
| 240 |  * \param a first vector 
 | 
|---|
| 241 |  * \param b second vector
 | 
|---|
| 242 |  * \return a + b
 | 
|---|
| 243 |  */
 | 
|---|
| 244 | Vector& operator+(const Vector& a, const Vector& b)
 | 
|---|
| 245 | {
 | 
|---|
| 246 |   Vector *x = new Vector;
 | 
|---|
| 247 |   x->CopyVector(&a);
 | 
|---|
| 248 |   x->AddVector(&b);
 | 
|---|
| 249 |   return *x;
 | 
|---|
| 250 | };
 | 
|---|
| 251 | 
 | 
|---|
| 252 | /** Factors given vector \a a times \a m.
 | 
|---|
| 253 |  * \param a vector 
 | 
|---|
| 254 |  * \param m factor
 | 
|---|
| 255 |  * \return a + b
 | 
|---|
| 256 |  */
 | 
|---|
| 257 | Vector& operator*(const Vector& a, const double m)
 | 
|---|
| 258 | {
 | 
|---|
| 259 |   Vector *x = new Vector;
 | 
|---|
| 260 |   x->CopyVector(&a);
 | 
|---|
| 261 |   x->Scale(m);
 | 
|---|
| 262 |   return *x;
 | 
|---|
| 263 | };
 | 
|---|
| 264 | 
 | 
|---|
| 265 | /** Prints a 3dim vector.
 | 
|---|
| 266 |  * prints no end of line.
 | 
|---|
| 267 |  * \param *out output stream
 | 
|---|
| 268 |  */
 | 
|---|
| 269 | bool Vector::Output(ofstream *out) const
 | 
|---|
| 270 | {
 | 
|---|
| 271 |   if (out != NULL) {
 | 
|---|
| 272 |     *out << "(";
 | 
|---|
| 273 |     for (int i=0;i<NDIM;i++) {
 | 
|---|
| 274 |       *out << x[i];
 | 
|---|
| 275 |       if (i != 2)
 | 
|---|
| 276 |         *out << ",";
 | 
|---|
| 277 |     }
 | 
|---|
| 278 |     *out << ")";
 | 
|---|
| 279 |     return true;
 | 
|---|
| 280 |   } else
 | 
|---|
| 281 |     return false;
 | 
|---|
| 282 | };
 | 
|---|
| 283 | 
 | 
|---|
| 284 | ofstream& operator<<(ofstream& ost,Vector& m)
 | 
|---|
| 285 | {
 | 
|---|
| 286 |         m.Output(&ost);
 | 
|---|
| 287 |         return ost;
 | 
|---|
| 288 | };
 | 
|---|
| 289 | 
 | 
|---|
| 290 | /** Scales each atom coordinate by an individual \a factor.
 | 
|---|
| 291 |  * \param *factor pointer to scaling factor
 | 
|---|
| 292 |  */
 | 
|---|
| 293 | void Vector::Scale(double **factor)
 | 
|---|
| 294 | {
 | 
|---|
| 295 |   for (int i=NDIM;i--;)
 | 
|---|
| 296 |     x[i] *= (*factor)[i];
 | 
|---|
| 297 | };
 | 
|---|
| 298 | 
 | 
|---|
| 299 | void Vector::Scale(double *factor)
 | 
|---|
| 300 | {
 | 
|---|
| 301 |   for (int i=NDIM;i--;)
 | 
|---|
| 302 |     x[i] *= *factor;
 | 
|---|
| 303 | };
 | 
|---|
| 304 | 
 | 
|---|
| 305 | void Vector::Scale(double factor)
 | 
|---|
| 306 | {
 | 
|---|
| 307 |   for (int i=NDIM;i--;)
 | 
|---|
| 308 |     x[i] *= factor;
 | 
|---|
| 309 | };
 | 
|---|
| 310 | 
 | 
|---|
| 311 | /** Translate atom by given vector. 
 | 
|---|
| 312 |  * \param trans[] translation vector.
 | 
|---|
| 313 |  */
 | 
|---|
| 314 | void Vector::Translate(const Vector *trans)
 | 
|---|
| 315 | {
 | 
|---|
| 316 |   for (int i=NDIM;i--;)
 | 
|---|
| 317 |     x[i] += trans->x[i];
 | 
|---|
| 318 | }; 
 | 
|---|
| 319 | 
 | 
|---|
| 320 | /** Do a matrix multiplication.
 | 
|---|
| 321 |  * \param *matrix NDIM_NDIM array
 | 
|---|
| 322 |  */
 | 
|---|
| 323 | void Vector::MatrixMultiplication(double *M)
 | 
|---|
| 324 | {
 | 
|---|
| 325 |   Vector C;
 | 
|---|
| 326 |   // do the matrix multiplication
 | 
|---|
| 327 |   C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
 | 
|---|
| 328 |   C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
 | 
|---|
| 329 |   C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
 | 
|---|
| 330 |   // transfer the result into this
 | 
|---|
| 331 |   for (int i=NDIM;i--;)
 | 
|---|
| 332 |     x[i] = C.x[i];
 | 
|---|
| 333 | };
 | 
|---|
| 334 | 
 | 
|---|
| 335 | /** Do a matrix multiplication with \a *matrix' inverse.
 | 
|---|
| 336 |  * \param *matrix NDIM_NDIM array
 | 
|---|
| 337 |  */
 | 
|---|
| 338 | void Vector::InverseMatrixMultiplication(double *A)
 | 
|---|
| 339 | {
 | 
|---|
| 340 |   Vector C;
 | 
|---|
| 341 |   double B[NDIM*NDIM];
 | 
|---|
| 342 |   double detA = RDET3(A);
 | 
|---|
| 343 |   double detAReci;
 | 
|---|
| 344 | 
 | 
|---|
| 345 |   // calculate the inverse B
 | 
|---|
| 346 |   if (fabs(detA) > MYEPSILON) {;  // RDET3(A) yields precisely zero if A irregular
 | 
|---|
| 347 |     detAReci = 1./detA;
 | 
|---|
| 348 |     B[0] =  detAReci*RDET2(A[4],A[5],A[7],A[8]);    // A_11
 | 
|---|
| 349 |     B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]);    // A_12
 | 
|---|
| 350 |     B[2] =  detAReci*RDET2(A[1],A[2],A[4],A[5]);    // A_13
 | 
|---|
| 351 |     B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]);    // A_21
 | 
|---|
| 352 |     B[4] =  detAReci*RDET2(A[0],A[2],A[6],A[8]);    // A_22
 | 
|---|
| 353 |     B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]);    // A_23
 | 
|---|
| 354 |     B[6] =  detAReci*RDET2(A[3],A[4],A[6],A[7]);    // A_31
 | 
|---|
| 355 |     B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]);    // A_32
 | 
|---|
| 356 |     B[8] =  detAReci*RDET2(A[0],A[1],A[3],A[4]);    // A_33
 | 
|---|
| 357 |   
 | 
|---|
| 358 |     // do the matrix multiplication
 | 
|---|
| 359 |     C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
 | 
|---|
| 360 |     C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
 | 
|---|
| 361 |     C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
 | 
|---|
| 362 |     // transfer the result into this
 | 
|---|
| 363 |     for (int i=NDIM;i--;)
 | 
|---|
| 364 |       x[i] = C.x[i];
 | 
|---|
| 365 |   } else {
 | 
|---|
| 366 |     cerr << "ERROR: inverse of matrix does not exists!" << endl;
 | 
|---|
| 367 |   }
 | 
|---|
| 368 | };
 | 
|---|
| 369 | 
 | 
|---|
| 370 | 
 | 
|---|
| 371 | /** Creates this vector as the b y *factors' components scaled linear combination of the given three.
 | 
|---|
| 372 |  * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
 | 
|---|
| 373 |  * \param *x1 first vector
 | 
|---|
| 374 |  * \param *x2 second vector
 | 
|---|
| 375 |  * \param *x3 third vector
 | 
|---|
| 376 |  * \param *factors three-component vector with the factor for each given vector
 | 
|---|
| 377 |  */
 | 
|---|
| 378 | void Vector::LinearCombinationOfVectors(const Vector *x1, const Vector *x2, const Vector *x3, double *factors)
 | 
|---|
| 379 | {
 | 
|---|
| 380 |   for(int i=NDIM;i--;)
 | 
|---|
| 381 |     x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i]; 
 | 
|---|
| 382 | };
 | 
|---|
| 383 | 
 | 
|---|
| 384 | /** Mirrors atom against a given plane. 
 | 
|---|
| 385 |  * \param n[] normal vector of mirror plane.
 | 
|---|
| 386 |  */
 | 
|---|
| 387 | void Vector::Mirror(const Vector *n)
 | 
|---|
| 388 | {
 | 
|---|
| 389 |   double projection;
 | 
|---|
| 390 |   projection = ScalarProduct(n)/n->ScalarProduct(n);    // remove constancy from n (keep as logical one)
 | 
|---|
| 391 |   // withdraw projected vector twice from original one
 | 
|---|
| 392 |   cout << Verbose(1) << "Vector: ";
 | 
|---|
| 393 |   Output((ofstream *)&cout);
 | 
|---|
| 394 |   cout << "\t";
 | 
|---|
| 395 |   for (int i=NDIM;i--;)
 | 
|---|
| 396 |     x[i] -= 2.*projection*n->x[i];
 | 
|---|
| 397 |   cout << "Projected vector: ";
 | 
|---|
| 398 |   Output((ofstream *)&cout);
 | 
|---|
| 399 |   cout << endl;
 | 
|---|
| 400 | }; 
 | 
|---|
| 401 | 
 | 
|---|
| 402 | /** Calculates normal vector for three given vectors (being three points in space).
 | 
|---|
| 403 |  * Makes this vector orthonormal to the three given points, making up a place in 3d space.
 | 
|---|
| 404 |  * \param *y1 first vector
 | 
|---|
| 405 |  * \param *y2 second vector
 | 
|---|
| 406 |  * \param *y3 third vector
 | 
|---|
| 407 |  * \return true - success, vectors are linear independent, false - failure due to linear dependency
 | 
|---|
| 408 |  */
 | 
|---|
| 409 | bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2, const Vector *y3)
 | 
|---|
| 410 | {
 | 
|---|
| 411 |   Vector x1, x2;
 | 
|---|
| 412 | 
 | 
|---|
| 413 |   x1.CopyVector(y1);
 | 
|---|
| 414 |   x1.SubtractVector(y2);
 | 
|---|
| 415 |   x2.CopyVector(y3);
 | 
|---|
| 416 |   x2.SubtractVector(y2);
 | 
|---|
| 417 |   if ((x1.Norm()==0) || (x2.Norm()==0)) {
 | 
|---|
| 418 |     cout << Verbose(4) << "Given vectors are linear dependent." << endl;
 | 
|---|
| 419 |     return false;
 | 
|---|
| 420 |   }
 | 
|---|
| 421 | //  cout << Verbose(4) << "relative, first plane coordinates:";
 | 
|---|
| 422 | //  x1.Output((ofstream *)&cout);
 | 
|---|
| 423 | //  cout << endl;
 | 
|---|
| 424 | //  cout << Verbose(4) << "second plane coordinates:";
 | 
|---|
| 425 | //  x2.Output((ofstream *)&cout);
 | 
|---|
| 426 | //  cout << endl;
 | 
|---|
| 427 | 
 | 
|---|
| 428 |   this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
 | 
|---|
| 429 |   this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
 | 
|---|
| 430 |   this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
 | 
|---|
| 431 |   Normalize();
 | 
|---|
| 432 |   
 | 
|---|
| 433 |   return true;
 | 
|---|
| 434 | };
 | 
|---|
| 435 | 
 | 
|---|
| 436 | 
 | 
|---|
| 437 | /** Calculates orthonormal vector to two given vectors.
 | 
|---|
| 438 |  * Makes this vector orthonormal to two given vectors. This is very similar to the other
 | 
|---|
| 439 |  * vector::MakeNormalVector(), only there three points whereas here two difference
 | 
|---|
| 440 |  * vectors are given.
 | 
|---|
| 441 |  * \param *x1 first vector
 | 
|---|
| 442 |  * \param *x2 second vector
 | 
|---|
| 443 |  * \return true - success, vectors are linear independent, false - failure due to linear dependency
 | 
|---|
| 444 |  */
 | 
|---|
| 445 | bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2)
 | 
|---|
| 446 | {
 | 
|---|
| 447 |   Vector x1,x2;
 | 
|---|
| 448 |   x1.CopyVector(y1);
 | 
|---|
| 449 |   x2.CopyVector(y2);
 | 
|---|
| 450 |   Zero();
 | 
|---|
| 451 |   if ((x1.Norm()==0) || (x2.Norm()==0)) {
 | 
|---|
| 452 |     cout << Verbose(4) << "Given vectors are linear dependent." << endl;
 | 
|---|
| 453 |     return false;
 | 
|---|
| 454 |   }
 | 
|---|
| 455 | //  cout << Verbose(4) << "relative, first plane coordinates:";
 | 
|---|
| 456 | //  x1.Output((ofstream *)&cout);
 | 
|---|
| 457 | //  cout << endl;
 | 
|---|
| 458 | //  cout << Verbose(4) << "second plane coordinates:";
 | 
|---|
| 459 | //  x2.Output((ofstream *)&cout);
 | 
|---|
| 460 | //  cout << endl;
 | 
|---|
| 461 | 
 | 
|---|
| 462 |   this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
 | 
|---|
| 463 |   this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
 | 
|---|
| 464 |   this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
 | 
|---|
| 465 |   Normalize();
 | 
|---|
| 466 | 
 | 
|---|
| 467 |   return true;
 | 
|---|
| 468 | };
 | 
|---|
| 469 | 
 | 
|---|
| 470 | /** Calculates orthonormal vector to one given vectors.
 | 
|---|
| 471 |  * Just subtracts the projection onto the given vector from this vector.
 | 
|---|
| 472 |  * \param *x1 vector
 | 
|---|
| 473 |  * \return true - success, false - vector is zero
 | 
|---|
| 474 |  */
 | 
|---|
| 475 | bool Vector::MakeNormalVector(const Vector *y1)
 | 
|---|
| 476 | {
 | 
|---|
| 477 |         bool result = false;
 | 
|---|
| 478 |   Vector x1;
 | 
|---|
| 479 |   x1.CopyVector(y1);
 | 
|---|
| 480 |   x1.Scale(x1.Projection(this));
 | 
|---|
| 481 |   SubtractVector(&x1);
 | 
|---|
| 482 |   for (int i=NDIM;i--;)
 | 
|---|
| 483 |           result = result || (fabs(x[i]) > MYEPSILON);
 | 
|---|
| 484 | 
 | 
|---|
| 485 |   return result;
 | 
|---|
| 486 | };
 | 
|---|
| 487 | 
 | 
|---|
| 488 | /** Creates this vector as one of the possible orthonormal ones to the given one.
 | 
|---|
| 489 |  * Just scan how many components of given *vector are unequal to zero and
 | 
|---|
| 490 |  * try to get the skp of both to be zero accordingly.  
 | 
|---|
| 491 |  * \param *vector given vector
 | 
|---|
| 492 |  * \return true - success, false - failure (null vector given)
 | 
|---|
| 493 |  */
 | 
|---|
| 494 | bool Vector::GetOneNormalVector(const Vector *GivenVector)
 | 
|---|
| 495 | {
 | 
|---|
| 496 |   int Components[NDIM]; // contains indices of non-zero components
 | 
|---|
| 497 |   int Last = 0;   // count the number of non-zero entries in vector
 | 
|---|
| 498 |   int j;  // loop variables
 | 
|---|
| 499 |   double norm;
 | 
|---|
| 500 | 
 | 
|---|
| 501 |   cout << Verbose(4);
 | 
|---|
| 502 |   GivenVector->Output((ofstream *)&cout);
 | 
|---|
| 503 |   cout << endl;
 | 
|---|
| 504 |   for (j=NDIM;j--;)
 | 
|---|
| 505 |     Components[j] = -1;
 | 
|---|
| 506 |   // find two components != 0
 | 
|---|
| 507 |   for (j=0;j<NDIM;j++)
 | 
|---|
| 508 |     if (fabs(GivenVector->x[j]) > MYEPSILON)
 | 
|---|
| 509 |       Components[Last++] = j;
 | 
|---|
| 510 |   cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
 | 
|---|
| 511 |         
 | 
|---|
| 512 |   switch(Last) {
 | 
|---|
| 513 |     case 3:  // threecomponent system
 | 
|---|
| 514 |     case 2:  // two component system
 | 
|---|
| 515 |       norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
 | 
|---|
| 516 |       x[Components[2]] = 0.;
 | 
|---|
| 517 |       // in skp both remaining parts shall become zero but with opposite sign and third is zero
 | 
|---|
| 518 |       x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
 | 
|---|
| 519 |       x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
 | 
|---|
| 520 |       return true;
 | 
|---|
| 521 |       break;
 | 
|---|
| 522 |     case 1: // one component system
 | 
|---|
| 523 |       // set sole non-zero component to 0, and one of the other zero component pendants to 1
 | 
|---|
| 524 |       x[(Components[0]+2)%NDIM] = 0.;   
 | 
|---|
| 525 |       x[(Components[0]+1)%NDIM] = 1.;   
 | 
|---|
| 526 |       x[Components[0]] = 0.;   
 | 
|---|
| 527 |       return true;
 | 
|---|
| 528 |       break;
 | 
|---|
| 529 |     default:
 | 
|---|
| 530 |       return false;
 | 
|---|
| 531 |   }
 | 
|---|
| 532 | };
 | 
|---|
| 533 | 
 | 
|---|
| 534 | /** Determines paramter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
 | 
|---|
| 535 |  * \param *A first plane vector
 | 
|---|
| 536 |  * \param *B second plane vector
 | 
|---|
| 537 |  * \param *C third plane vector
 | 
|---|
| 538 |  * \return scaling parameter for this vector
 | 
|---|
| 539 |  */
 | 
|---|
| 540 | double Vector::CutsPlaneAt(Vector *A, Vector *B, Vector *C)
 | 
|---|
| 541 | {
 | 
|---|
| 542 | //  cout << Verbose(3) << "For comparison: ";
 | 
|---|
| 543 | //  cout << "A " << A->Projection(this) << "\t"; 
 | 
|---|
| 544 | //  cout << "B " << B->Projection(this) << "\t"; 
 | 
|---|
| 545 | //  cout << "C " << C->Projection(this) << "\t"; 
 | 
|---|
| 546 | //  cout << endl;
 | 
|---|
| 547 |   return A->Projection(this);
 | 
|---|
| 548 | };
 | 
|---|
| 549 | 
 | 
|---|
| 550 | /** Creates a new vector as the one with least square distance to a given set of \a vectors.
 | 
|---|
| 551 |  * \param *vectors set of vectors
 | 
|---|
| 552 |  * \param num number of vectors
 | 
|---|
| 553 |  * \return true if success, false if failed due to linear dependency
 | 
|---|
| 554 |  */
 | 
|---|
| 555 | bool Vector::LSQdistance(Vector **vectors, int num) 
 | 
|---|
| 556 | {
 | 
|---|
| 557 |         int j;
 | 
|---|
| 558 |                                 
 | 
|---|
| 559 |         for (j=0;j<num;j++) {
 | 
|---|
| 560 |                 cout << Verbose(1) << j << "th atom's vector: ";
 | 
|---|
| 561 |                 (vectors[j])->Output((ofstream *)&cout);
 | 
|---|
| 562 |                 cout << endl;
 | 
|---|
| 563 |         }
 | 
|---|
| 564 | 
 | 
|---|
| 565 |   int np = 3;
 | 
|---|
| 566 |         struct LSQ_params par;
 | 
|---|
| 567 |     
 | 
|---|
| 568 |    const gsl_multimin_fminimizer_type *T =
 | 
|---|
| 569 |      gsl_multimin_fminimizer_nmsimplex;
 | 
|---|
| 570 |    gsl_multimin_fminimizer *s = NULL;
 | 
|---|
| 571 |    gsl_vector *ss, *y;
 | 
|---|
| 572 |    gsl_multimin_function minex_func;
 | 
|---|
| 573 |  
 | 
|---|
| 574 |    size_t iter = 0, i;
 | 
|---|
| 575 |    int status;
 | 
|---|
| 576 |    double size;
 | 
|---|
| 577 |  
 | 
|---|
| 578 |    /* Initial vertex size vector */
 | 
|---|
| 579 |    ss = gsl_vector_alloc (np);
 | 
|---|
| 580 |    y = gsl_vector_alloc (np);
 | 
|---|
| 581 |  
 | 
|---|
| 582 |    /* Set all step sizes to 1 */
 | 
|---|
| 583 |    gsl_vector_set_all (ss, 1.0);
 | 
|---|
| 584 |  
 | 
|---|
| 585 |    /* Starting point */
 | 
|---|
| 586 |    par.vectors = vectors;
 | 
|---|
| 587 |          par.num = num;
 | 
|---|
| 588 |         
 | 
|---|
| 589 |          for (i=NDIM;i--;)
 | 
|---|
| 590 |                 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.); 
 | 
|---|
| 591 |          
 | 
|---|
| 592 |    /* Initialize method and iterate */
 | 
|---|
| 593 |    minex_func.f = &LSQ;
 | 
|---|
| 594 |    minex_func.n = np;
 | 
|---|
| 595 |    minex_func.params = (void *)∥
 | 
|---|
| 596 |  
 | 
|---|
| 597 |    s = gsl_multimin_fminimizer_alloc (T, np);
 | 
|---|
| 598 |    gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
 | 
|---|
| 599 |  
 | 
|---|
| 600 |    do
 | 
|---|
| 601 |      {
 | 
|---|
| 602 |        iter++;
 | 
|---|
| 603 |        status = gsl_multimin_fminimizer_iterate(s);
 | 
|---|
| 604 |  
 | 
|---|
| 605 |        if (status)
 | 
|---|
| 606 |          break;
 | 
|---|
| 607 |  
 | 
|---|
| 608 |        size = gsl_multimin_fminimizer_size (s);
 | 
|---|
| 609 |        status = gsl_multimin_test_size (size, 1e-2);
 | 
|---|
| 610 |  
 | 
|---|
| 611 |        if (status == GSL_SUCCESS)
 | 
|---|
| 612 |          {
 | 
|---|
| 613 |            printf ("converged to minimum at\n");
 | 
|---|
| 614 |          }
 | 
|---|
| 615 |  
 | 
|---|
| 616 |        printf ("%5d ", (int)iter);
 | 
|---|
| 617 |        for (i = 0; i < (size_t)np; i++)
 | 
|---|
| 618 |          {
 | 
|---|
| 619 |            printf ("%10.3e ", gsl_vector_get (s->x, i));
 | 
|---|
| 620 |          }
 | 
|---|
| 621 |        printf ("f() = %7.3f size = %.3f\n", s->fval, size);
 | 
|---|
| 622 |      }
 | 
|---|
| 623 |    while (status == GSL_CONTINUE && iter < 100);
 | 
|---|
| 624 |  
 | 
|---|
| 625 |   for (i=(size_t)np;i--;)
 | 
|---|
| 626 |     this->x[i] = gsl_vector_get(s->x, i);
 | 
|---|
| 627 |    gsl_vector_free(y);
 | 
|---|
| 628 |    gsl_vector_free(ss);
 | 
|---|
| 629 |    gsl_multimin_fminimizer_free (s);
 | 
|---|
| 630 | 
 | 
|---|
| 631 |         return true;
 | 
|---|
| 632 | };
 | 
|---|
| 633 | 
 | 
|---|
| 634 | /** Adds vector \a *y componentwise.
 | 
|---|
| 635 |  * \param *y vector
 | 
|---|
| 636 |  */
 | 
|---|
| 637 | void Vector::AddVector(const Vector *y)
 | 
|---|
| 638 | {
 | 
|---|
| 639 |   for (int i=NDIM;i--;)
 | 
|---|
| 640 |     this->x[i] += y->x[i];
 | 
|---|
| 641 | }
 | 
|---|
| 642 | 
 | 
|---|
| 643 | /** Adds vector \a *y componentwise.
 | 
|---|
| 644 |  * \param *y vector
 | 
|---|
| 645 |  */
 | 
|---|
| 646 | void Vector::SubtractVector(const Vector *y)
 | 
|---|
| 647 | {
 | 
|---|
| 648 |   for (int i=NDIM;i--;)
 | 
|---|
| 649 |     this->x[i] -= y->x[i];
 | 
|---|
| 650 | }
 | 
|---|
| 651 | 
 | 
|---|
| 652 | /** Copy vector \a *y componentwise.
 | 
|---|
| 653 |  * \param *y vector
 | 
|---|
| 654 |  */
 | 
|---|
| 655 | void Vector::CopyVector(const Vector *y)
 | 
|---|
| 656 | {
 | 
|---|
| 657 |   for (int i=NDIM;i--;)
 | 
|---|
| 658 |     this->x[i] = y->x[i];
 | 
|---|
| 659 | }
 | 
|---|
| 660 | 
 | 
|---|
| 661 | 
 | 
|---|
| 662 | /** Asks for position, checks for boundary.
 | 
|---|
| 663 |  * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
 | 
|---|
| 664 |  * \param check whether bounds shall be checked (true) or not (false)
 | 
|---|
| 665 |  */
 | 
|---|
| 666 | void Vector::AskPosition(double *cell_size, bool check)
 | 
|---|
| 667 | {
 | 
|---|
| 668 |   char coords[3] = {'x','y','z'};
 | 
|---|
| 669 |   int j = -1;
 | 
|---|
| 670 |   for (int i=0;i<3;i++) {
 | 
|---|
| 671 |     j += i+1;
 | 
|---|
| 672 |     do {
 | 
|---|
| 673 |       cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
 | 
|---|
| 674 |       cin >> x[i];
 | 
|---|
| 675 |     } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
 | 
|---|
| 676 |   }
 | 
|---|
| 677 | };
 | 
|---|
| 678 | 
 | 
|---|
| 679 | /** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
 | 
|---|
| 680 |  * This is linear system of equations to be solved, however of the three given (skp of this vector\
 | 
|---|
| 681 |  * with either of the three hast to be zero) only two are linear independent. The third equation
 | 
|---|
| 682 |  * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
 | 
|---|
| 683 |  * where very often it has to be checked whether a certain value is zero or not and thus forked into
 | 
|---|
| 684 |  * another case.
 | 
|---|
| 685 |  * \param *x1 first vector
 | 
|---|
| 686 |  * \param *x2 second vector
 | 
|---|
| 687 |  * \param *y third vector
 | 
|---|
| 688 |  * \param alpha first angle
 | 
|---|
| 689 |  * \param beta second angle
 | 
|---|
| 690 |  * \param c norm of final vector 
 | 
|---|
| 691 |  * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
 | 
|---|
| 692 |  * \bug this is not yet working properly 
 | 
|---|
| 693 |  */
 | 
|---|
| 694 | bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
 | 
|---|
| 695 | {
 | 
|---|
| 696 |   double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
 | 
|---|
| 697 |   double ang; // angle on testing
 | 
|---|
| 698 |   double sign[3];
 | 
|---|
| 699 |   int i,j,k;
 | 
|---|
| 700 |   A = cos(alpha) * x1->Norm() * c;
 | 
|---|
| 701 |   B1 = cos(beta + M_PI/2.) * y->Norm() * c;
 | 
|---|
| 702 |   B2 = cos(beta) * x2->Norm() * c;
 | 
|---|
| 703 |   C = c * c;
 | 
|---|
| 704 |   cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
 | 
|---|
| 705 |   int flag = 0;
 | 
|---|
| 706 |   if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
 | 
|---|
| 707 |     if (fabs(x1->x[1]) > MYEPSILON) {
 | 
|---|
| 708 |       flag = 1;    
 | 
|---|
| 709 |     } else if (fabs(x1->x[2]) > MYEPSILON) {
 | 
|---|
| 710 |        flag = 2;
 | 
|---|
| 711 |     } else {
 | 
|---|
| 712 |       return false;
 | 
|---|
| 713 |     }
 | 
|---|
| 714 |   }
 | 
|---|
| 715 |   switch (flag) {
 | 
|---|
| 716 |     default:
 | 
|---|
| 717 |     case 0:
 | 
|---|
| 718 |       break;
 | 
|---|
| 719 |     case 2:
 | 
|---|
| 720 |       flip(&x1->x[0],&x1->x[1]);
 | 
|---|
| 721 |       flip(&x2->x[0],&x2->x[1]);
 | 
|---|
| 722 |       flip(&y->x[0],&y->x[1]);
 | 
|---|
| 723 |       //flip(&x[0],&x[1]);
 | 
|---|
| 724 |       flip(&x1->x[1],&x1->x[2]);
 | 
|---|
| 725 |       flip(&x2->x[1],&x2->x[2]);
 | 
|---|
| 726 |       flip(&y->x[1],&y->x[2]);
 | 
|---|
| 727 |       //flip(&x[1],&x[2]);
 | 
|---|
| 728 |     case 1:
 | 
|---|
| 729 |       flip(&x1->x[0],&x1->x[1]);
 | 
|---|
| 730 |       flip(&x2->x[0],&x2->x[1]);
 | 
|---|
| 731 |       flip(&y->x[0],&y->x[1]);
 | 
|---|
| 732 |       //flip(&x[0],&x[1]);
 | 
|---|
| 733 |       flip(&x1->x[1],&x1->x[2]);
 | 
|---|
| 734 |       flip(&x2->x[1],&x2->x[2]);
 | 
|---|
| 735 |       flip(&y->x[1],&y->x[2]);
 | 
|---|
| 736 |       //flip(&x[1],&x[2]);
 | 
|---|
| 737 |       break;
 | 
|---|
| 738 |   }
 | 
|---|
| 739 |   // now comes the case system
 | 
|---|
| 740 |   D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
 | 
|---|
| 741 |   D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2]; 
 | 
|---|
| 742 |   D3 = y->x[0]/x1->x[0]*A-B1;
 | 
|---|
| 743 |   cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
 | 
|---|
| 744 |   if (fabs(D1) < MYEPSILON) {
 | 
|---|
| 745 |     cout << Verbose(2) << "D1 == 0!\n"; 
 | 
|---|
| 746 |     if (fabs(D2) > MYEPSILON) {
 | 
|---|
| 747 |       cout << Verbose(3) << "D2 != 0!\n"; 
 | 
|---|
| 748 |       x[2] = -D3/D2;
 | 
|---|
| 749 |       E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
 | 
|---|
| 750 |       E2 = -x1->x[1]/x1->x[0];
 | 
|---|
| 751 |       cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
 | 
|---|
| 752 |       F1 = E1*E1 + 1.;
 | 
|---|
| 753 |       F2 = -E1*E2;
 | 
|---|
| 754 |       F3 = E1*E1 + D3*D3/(D2*D2) - C;
 | 
|---|
| 755 |       cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
 | 
|---|
| 756 |       if (fabs(F1) < MYEPSILON) {
 | 
|---|
| 757 |         cout << Verbose(4) << "F1 == 0!\n"; 
 | 
|---|
| 758 |         cout << Verbose(4) << "Gleichungssystem linear\n";
 | 
|---|
| 759 |         x[1] = F3/(2.*F2); 
 | 
|---|
| 760 |       } else {
 | 
|---|
| 761 |         p = F2/F1;
 | 
|---|
| 762 |         q = p*p - F3/F1;
 | 
|---|
| 763 |         cout << Verbose(4) << "p " << p << "\tq " << q << endl;  
 | 
|---|
| 764 |         if (q < 0) {
 | 
|---|
| 765 |           cout << Verbose(4) << "q < 0" << endl;
 | 
|---|
| 766 |           return false;
 | 
|---|
| 767 |         }
 | 
|---|
| 768 |         x[1] = p + sqrt(q);
 | 
|---|
| 769 |       }
 | 
|---|
| 770 |       x[0] =  A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
 | 
|---|
| 771 |     } else {
 | 
|---|
| 772 |       cout << Verbose(2) << "Gleichungssystem unterbestimmt\n";
 | 
|---|
| 773 |       return false;
 | 
|---|
| 774 |     }
 | 
|---|
| 775 |   } else {
 | 
|---|
| 776 |     E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
 | 
|---|
| 777 |     E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
 | 
|---|
| 778 |     cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
 | 
|---|
| 779 |     F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
 | 
|---|
| 780 |     F2 = -(E1*E2 + D2*D3/(D1*D1));
 | 
|---|
| 781 |     F3 = E1*E1 + D3*D3/(D1*D1) - C;
 | 
|---|
| 782 |     cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
 | 
|---|
| 783 |     if (fabs(F1) < MYEPSILON) {
 | 
|---|
| 784 |       cout << Verbose(3) << "F1 == 0!\n"; 
 | 
|---|
| 785 |       cout << Verbose(3) << "Gleichungssystem linear\n";
 | 
|---|
| 786 |       x[2] = F3/(2.*F2);     
 | 
|---|
| 787 |     } else {
 | 
|---|
| 788 |       p = F2/F1;
 | 
|---|
| 789 |       q = p*p - F3/F1;
 | 
|---|
| 790 |       cout << Verbose(3) << "p " << p << "\tq " << q << endl;  
 | 
|---|
| 791 |       if (q < 0) {
 | 
|---|
| 792 |         cout << Verbose(3) << "q < 0" << endl;
 | 
|---|
| 793 |         return false;
 | 
|---|
| 794 |       }
 | 
|---|
| 795 |       x[2] = p + sqrt(q);
 | 
|---|
| 796 |     }
 | 
|---|
| 797 |     x[1] = (-D2 * x[2] - D3)/D1;
 | 
|---|
| 798 |     x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
 | 
|---|
| 799 |   }
 | 
|---|
| 800 |   switch (flag) { // back-flipping
 | 
|---|
| 801 |     default:
 | 
|---|
| 802 |     case 0:
 | 
|---|
| 803 |       break;
 | 
|---|
| 804 |     case 2:
 | 
|---|
| 805 |       flip(&x1->x[0],&x1->x[1]);
 | 
|---|
| 806 |       flip(&x2->x[0],&x2->x[1]);
 | 
|---|
| 807 |       flip(&y->x[0],&y->x[1]);
 | 
|---|
| 808 |       flip(&x[0],&x[1]);
 | 
|---|
| 809 |       flip(&x1->x[1],&x1->x[2]);
 | 
|---|
| 810 |       flip(&x2->x[1],&x2->x[2]);
 | 
|---|
| 811 |       flip(&y->x[1],&y->x[2]);
 | 
|---|
| 812 |       flip(&x[1],&x[2]);
 | 
|---|
| 813 |     case 1:
 | 
|---|
| 814 |       flip(&x1->x[0],&x1->x[1]);
 | 
|---|
| 815 |       flip(&x2->x[0],&x2->x[1]);
 | 
|---|
| 816 |       flip(&y->x[0],&y->x[1]);
 | 
|---|
| 817 |       //flip(&x[0],&x[1]);
 | 
|---|
| 818 |       flip(&x1->x[1],&x1->x[2]);
 | 
|---|
| 819 |       flip(&x2->x[1],&x2->x[2]);
 | 
|---|
| 820 |       flip(&y->x[1],&y->x[2]);
 | 
|---|
| 821 |       flip(&x[1],&x[2]);
 | 
|---|
| 822 |       break;
 | 
|---|
| 823 |   }
 | 
|---|
| 824 |   // one z component is only determined by its radius (without sign)
 | 
|---|
| 825 |   // thus check eight possible sign flips and determine by checking angle with second vector
 | 
|---|
| 826 |   for (i=0;i<8;i++) {
 | 
|---|
| 827 |     // set sign vector accordingly
 | 
|---|
| 828 |     for (j=2;j>=0;j--) {
 | 
|---|
| 829 |       k = (i & pot(2,j)) << j;
 | 
|---|
| 830 |       cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
 | 
|---|
| 831 |       sign[j] = (k == 0) ? 1. : -1.;
 | 
|---|
| 832 |     }
 | 
|---|
| 833 |     cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
 | 
|---|
| 834 |     // apply sign matrix 
 | 
|---|
| 835 |     for (j=NDIM;j--;)
 | 
|---|
| 836 |       x[j] *= sign[j];
 | 
|---|
| 837 |     // calculate angle and check
 | 
|---|
| 838 |     ang = x2->Angle (this);
 | 
|---|
| 839 |     cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
 | 
|---|
| 840 |     if (fabs(ang - cos(beta)) < MYEPSILON) {  
 | 
|---|
| 841 |       break;
 | 
|---|
| 842 |     }
 | 
|---|
| 843 |     // unapply sign matrix (is its own inverse)
 | 
|---|
| 844 |     for (j=NDIM;j--;)
 | 
|---|
| 845 |       x[j] *= sign[j];
 | 
|---|
| 846 |   }
 | 
|---|
| 847 |   return true;
 | 
|---|
| 848 | };
 | 
|---|