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