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