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