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