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