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