[6ac7ee] | 1 | /** \file vector.cpp
|
---|
| 2 | *
|
---|
| 3 | * Function implementations for the class vector.
|
---|
| 4 | *
|
---|
| 5 | */
|
---|
| 6 |
|
---|
[edb93c] | 7 |
|
---|
[54a746] | 8 | #include "vector.hpp"
|
---|
| 9 | #include "verbose.hpp"
|
---|
[b34306] | 10 | #include "World.hpp"
|
---|
[0a4f7f] | 11 | #include "Helpers/Assert.hpp"
|
---|
[753f02] | 12 | #include "Helpers/fast_functions.hpp"
|
---|
[6ac7ee] | 13 |
|
---|
[1bd79e] | 14 | #include <iostream>
|
---|
| 15 |
|
---|
| 16 | using namespace std;
|
---|
[6ac7ee] | 17 |
|
---|
[97498a] | 18 |
|
---|
[6ac7ee] | 19 | /************************************ Functions for class vector ************************************/
|
---|
| 20 |
|
---|
| 21 | /** Constructor of class vector.
|
---|
| 22 | */
|
---|
[753f02] | 23 | Vector::Vector()
|
---|
| 24 | {
|
---|
| 25 | x[0] = x[1] = x[2] = 0.;
|
---|
| 26 | };
|
---|
[6ac7ee] | 27 |
|
---|
[753f02] | 28 | /**
|
---|
| 29 | * Copy constructor
|
---|
[821907] | 30 | */
|
---|
[1bd79e] | 31 |
|
---|
[753f02] | 32 | Vector::Vector(const Vector& src)
|
---|
[821907] | 33 | {
|
---|
[753f02] | 34 | x[0] = src[0];
|
---|
| 35 | x[1] = src[1];
|
---|
| 36 | x[2] = src[2];
|
---|
[1bd79e] | 37 | }
|
---|
[821907] | 38 |
|
---|
| 39 | /** Constructor of class vector.
|
---|
| 40 | */
|
---|
[753f02] | 41 | Vector::Vector(const double x1, const double x2, const double x3)
|
---|
[821907] | 42 | {
|
---|
[753f02] | 43 | x[0] = x1;
|
---|
| 44 | x[1] = x2;
|
---|
| 45 | x[2] = x3;
|
---|
[821907] | 46 | };
|
---|
| 47 |
|
---|
[0a4f7f] | 48 | /**
|
---|
| 49 | * Assignment operator
|
---|
[6ac7ee] | 50 | */
|
---|
[0a4f7f] | 51 | Vector& Vector::operator=(const Vector& src){
|
---|
| 52 | // check for self assignment
|
---|
| 53 | if(&src!=this){
|
---|
[753f02] | 54 | x[0] = src[0];
|
---|
| 55 | x[1] = src[1];
|
---|
| 56 | x[2] = src[2];
|
---|
[0a4f7f] | 57 | }
|
---|
| 58 | return *this;
|
---|
| 59 | }
|
---|
[6ac7ee] | 60 |
|
---|
| 61 | /** Desctructor of class vector.
|
---|
| 62 | */
|
---|
| 63 | Vector::~Vector() {};
|
---|
| 64 |
|
---|
| 65 | /** Calculates square of distance between this and another vector.
|
---|
| 66 | * \param *y array to second vector
|
---|
| 67 | * \return \f$| x - y |^2\f$
|
---|
| 68 | */
|
---|
[273382] | 69 | double Vector::DistanceSquared(const Vector &y) const
|
---|
[6ac7ee] | 70 | {
|
---|
[042f82] | 71 | double res = 0.;
|
---|
| 72 | for (int i=NDIM;i--;)
|
---|
[753f02] | 73 | res += (x[i]-y[i])*(x[i]-y[i]);
|
---|
[042f82] | 74 | return (res);
|
---|
[6ac7ee] | 75 | };
|
---|
| 76 |
|
---|
| 77 | /** Calculates distance between this and another vector.
|
---|
| 78 | * \param *y array to second vector
|
---|
| 79 | * \return \f$| x - y |\f$
|
---|
| 80 | */
|
---|
[1513a74] | 81 | double Vector::distance(const Vector &y) const
|
---|
[6ac7ee] | 82 | {
|
---|
[273382] | 83 | return (sqrt(DistanceSquared(y)));
|
---|
[6ac7ee] | 84 | };
|
---|
| 85 |
|
---|
[1513a74] | 86 | Vector Vector::getClosestPoint(const Vector &point) const{
|
---|
| 87 | // the closest point to a single point space is always the single point itself
|
---|
| 88 | return *this;
|
---|
| 89 | }
|
---|
| 90 |
|
---|
[6ac7ee] | 91 | /** Calculates distance between this and another vector in a periodic cell.
|
---|
| 92 | * \param *y array to second vector
|
---|
| 93 | * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
|
---|
| 94 | * \return \f$| x - y |\f$
|
---|
| 95 | */
|
---|
[273382] | 96 | double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
|
---|
[6ac7ee] | 97 | {
|
---|
[1513a74] | 98 | double res = distance(y), tmp, matrix[NDIM*NDIM];
|
---|
[753f02] | 99 | Vector Shiftedy, TranslationVector;
|
---|
| 100 | int N[NDIM];
|
---|
| 101 | matrix[0] = cell_size[0];
|
---|
| 102 | matrix[1] = cell_size[1];
|
---|
| 103 | matrix[2] = cell_size[3];
|
---|
| 104 | matrix[3] = cell_size[1];
|
---|
| 105 | matrix[4] = cell_size[2];
|
---|
| 106 | matrix[5] = cell_size[4];
|
---|
| 107 | matrix[6] = cell_size[3];
|
---|
| 108 | matrix[7] = cell_size[4];
|
---|
| 109 | matrix[8] = cell_size[5];
|
---|
| 110 | // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
|
---|
| 111 | for (N[0]=-1;N[0]<=1;N[0]++)
|
---|
| 112 | for (N[1]=-1;N[1]<=1;N[1]++)
|
---|
| 113 | for (N[2]=-1;N[2]<=1;N[2]++) {
|
---|
| 114 | // create the translation vector
|
---|
| 115 | TranslationVector.Zero();
|
---|
| 116 | for (int i=NDIM;i--;)
|
---|
| 117 | TranslationVector[i] = (double)N[i];
|
---|
| 118 | TranslationVector.MatrixMultiplication(matrix);
|
---|
| 119 | // add onto the original vector to compare with
|
---|
| 120 | Shiftedy = y + TranslationVector;
|
---|
| 121 | // get distance and compare with minimum so far
|
---|
[1513a74] | 122 | tmp = distance(Shiftedy);
|
---|
[753f02] | 123 | if (tmp < res) res = tmp;
|
---|
| 124 | }
|
---|
| 125 | return (res);
|
---|
[6ac7ee] | 126 | };
|
---|
| 127 |
|
---|
| 128 | /** Calculates distance between this and another vector in a periodic cell.
|
---|
| 129 | * \param *y array to second vector
|
---|
| 130 | * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
|
---|
| 131 | * \return \f$| x - y |^2\f$
|
---|
| 132 | */
|
---|
[273382] | 133 | double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
|
---|
[6ac7ee] | 134 | {
|
---|
[042f82] | 135 | double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
|
---|
[753f02] | 136 | Vector Shiftedy, TranslationVector;
|
---|
| 137 | int N[NDIM];
|
---|
| 138 | matrix[0] = cell_size[0];
|
---|
| 139 | matrix[1] = cell_size[1];
|
---|
| 140 | matrix[2] = cell_size[3];
|
---|
| 141 | matrix[3] = cell_size[1];
|
---|
| 142 | matrix[4] = cell_size[2];
|
---|
| 143 | matrix[5] = cell_size[4];
|
---|
| 144 | matrix[6] = cell_size[3];
|
---|
| 145 | matrix[7] = cell_size[4];
|
---|
| 146 | matrix[8] = cell_size[5];
|
---|
| 147 | // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
|
---|
| 148 | for (N[0]=-1;N[0]<=1;N[0]++)
|
---|
| 149 | for (N[1]=-1;N[1]<=1;N[1]++)
|
---|
| 150 | for (N[2]=-1;N[2]<=1;N[2]++) {
|
---|
| 151 | // create the translation vector
|
---|
| 152 | TranslationVector.Zero();
|
---|
| 153 | for (int i=NDIM;i--;)
|
---|
| 154 | TranslationVector[i] = (double)N[i];
|
---|
| 155 | TranslationVector.MatrixMultiplication(matrix);
|
---|
| 156 | // add onto the original vector to compare with
|
---|
| 157 | Shiftedy = y + TranslationVector;
|
---|
| 158 | // get distance and compare with minimum so far
|
---|
| 159 | tmp = DistanceSquared(Shiftedy);
|
---|
| 160 | if (tmp < res) res = tmp;
|
---|
| 161 | }
|
---|
| 162 | return (res);
|
---|
[6ac7ee] | 163 | };
|
---|
| 164 |
|
---|
| 165 | /** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
|
---|
| 166 | * \param *out ofstream for debugging messages
|
---|
| 167 | * Tries to translate a vector into each adjacent neighbouring cell.
|
---|
| 168 | */
|
---|
[e138de] | 169 | void Vector::KeepPeriodic(const double * const matrix)
|
---|
[6ac7ee] | 170 | {
|
---|
[753f02] | 171 | // int N[NDIM];
|
---|
| 172 | // bool flag = false;
|
---|
| 173 | //vector Shifted, TranslationVector;
|
---|
| 174 | // Log() << Verbose(1) << "Begin of KeepPeriodic." << endl;
|
---|
| 175 | // Log() << Verbose(2) << "Vector is: ";
|
---|
| 176 | // Output(out);
|
---|
| 177 | // Log() << Verbose(0) << endl;
|
---|
| 178 | InverseMatrixMultiplication(matrix);
|
---|
| 179 | for(int i=NDIM;i--;) { // correct periodically
|
---|
| 180 | if (at(i) < 0) { // get every coefficient into the interval [0,1)
|
---|
| 181 | at(i) += ceil(at(i));
|
---|
| 182 | } else {
|
---|
| 183 | at(i) -= floor(at(i));
|
---|
| 184 | }
|
---|
[042f82] | 185 | }
|
---|
[753f02] | 186 | MatrixMultiplication(matrix);
|
---|
| 187 | // Log() << Verbose(2) << "New corrected vector is: ";
|
---|
| 188 | // Output(out);
|
---|
| 189 | // Log() << Verbose(0) << endl;
|
---|
| 190 | // Log() << Verbose(1) << "End of KeepPeriodic." << endl;
|
---|
[6ac7ee] | 191 | };
|
---|
| 192 |
|
---|
| 193 | /** Calculates scalar product between this and another vector.
|
---|
| 194 | * \param *y array to second vector
|
---|
| 195 | * \return \f$\langle x, y \rangle\f$
|
---|
| 196 | */
|
---|
[273382] | 197 | double Vector::ScalarProduct(const Vector &y) const
|
---|
[6ac7ee] | 198 | {
|
---|
[042f82] | 199 | double res = 0.;
|
---|
| 200 | for (int i=NDIM;i--;)
|
---|
[753f02] | 201 | res += x[i]*y[i];
|
---|
[042f82] | 202 | return (res);
|
---|
[6ac7ee] | 203 | };
|
---|
| 204 |
|
---|
| 205 |
|
---|
| 206 | /** Calculates VectorProduct between this and another vector.
|
---|
[042f82] | 207 | * -# returns the Product in place of vector from which it was initiated
|
---|
| 208 | * -# ATTENTION: Only three dim.
|
---|
| 209 | * \param *y array to vector with which to calculate crossproduct
|
---|
| 210 | * \return \f$ x \times y \f&
|
---|
[6ac7ee] | 211 | */
|
---|
[273382] | 212 | void Vector::VectorProduct(const Vector &y)
|
---|
[6ac7ee] | 213 | {
|
---|
[042f82] | 214 | Vector tmp;
|
---|
[753f02] | 215 | tmp[0] = x[1]* (y[2]) - x[2]* (y[1]);
|
---|
| 216 | tmp[1] = x[2]* (y[0]) - x[0]* (y[2]);
|
---|
| 217 | tmp[2] = x[0]* (y[1]) - x[1]* (y[0]);
|
---|
| 218 | (*this) = tmp;
|
---|
[6ac7ee] | 219 | };
|
---|
| 220 |
|
---|
| 221 |
|
---|
| 222 | /** projects this vector onto plane defined by \a *y.
|
---|
| 223 | * \param *y normal vector of plane
|
---|
| 224 | * \return \f$\langle x, y \rangle\f$
|
---|
| 225 | */
|
---|
[273382] | 226 | void Vector::ProjectOntoPlane(const Vector &y)
|
---|
[6ac7ee] | 227 | {
|
---|
[042f82] | 228 | Vector tmp;
|
---|
[753f02] | 229 | tmp = y;
|
---|
[042f82] | 230 | tmp.Normalize();
|
---|
[753f02] | 231 | tmp.Scale(ScalarProduct(tmp));
|
---|
| 232 | *this -= tmp;
|
---|
[2319ed] | 233 | };
|
---|
| 234 |
|
---|
[821907] | 235 | /** Calculates the minimum distance vector of this vector to the plane.
|
---|
[c4d4df] | 236 | * \param *out output stream for debugging
|
---|
| 237 | * \param *PlaneNormal normal of plane
|
---|
| 238 | * \param *PlaneOffset offset of plane
|
---|
| 239 | * \return distance to plane
|
---|
[821907] | 240 | * \return distance vector onto to plane
|
---|
[c4d4df] | 241 | */
|
---|
[8cbb97] | 242 | Vector Vector::GetDistanceVectorToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const
|
---|
[c4d4df] | 243 | {
|
---|
[753f02] | 244 | Vector temp = (*this) - PlaneOffset;
|
---|
| 245 | temp.MakeNormalTo(PlaneNormal);
|
---|
[c4d4df] | 246 | temp.Scale(-1.);
|
---|
| 247 | // then add connecting vector from plane to point
|
---|
[753f02] | 248 | temp += (*this)-PlaneOffset;
|
---|
[99593f] | 249 | double sign = temp.ScalarProduct(PlaneNormal);
|
---|
[7ea9e6] | 250 | if (fabs(sign) > MYEPSILON)
|
---|
| 251 | sign /= fabs(sign);
|
---|
| 252 | else
|
---|
| 253 | sign = 0.;
|
---|
[c4d4df] | 254 |
|
---|
[821907] | 255 | temp.Normalize();
|
---|
| 256 | temp.Scale(sign);
|
---|
| 257 | return temp;
|
---|
| 258 | };
|
---|
| 259 |
|
---|
[8cbb97] | 260 |
|
---|
[821907] | 261 | /** Calculates the minimum distance of this vector to the plane.
|
---|
| 262 | * \sa Vector::GetDistanceVectorToPlane()
|
---|
| 263 | * \param *out output stream for debugging
|
---|
| 264 | * \param *PlaneNormal normal of plane
|
---|
| 265 | * \param *PlaneOffset offset of plane
|
---|
| 266 | * \return distance to plane
|
---|
| 267 | */
|
---|
[8cbb97] | 268 | double Vector::DistanceToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const
|
---|
[821907] | 269 | {
|
---|
| 270 | return GetDistanceVectorToPlane(PlaneNormal,PlaneOffset).Norm();
|
---|
[c4d4df] | 271 | };
|
---|
| 272 |
|
---|
[6ac7ee] | 273 | /** Calculates the projection of a vector onto another \a *y.
|
---|
| 274 | * \param *y array to second vector
|
---|
| 275 | */
|
---|
[273382] | 276 | void Vector::ProjectIt(const Vector &y)
|
---|
[6ac7ee] | 277 | {
|
---|
[753f02] | 278 | (*this) += (-ScalarProduct(y))*y;
|
---|
[ef9df36] | 279 | };
|
---|
| 280 |
|
---|
| 281 | /** Calculates the projection of a vector onto another \a *y.
|
---|
| 282 | * \param *y array to second vector
|
---|
| 283 | * \return Vector
|
---|
| 284 | */
|
---|
[273382] | 285 | Vector Vector::Projection(const Vector &y) const
|
---|
[ef9df36] | 286 | {
|
---|
[753f02] | 287 | Vector helper = y;
|
---|
| 288 | helper.Scale((ScalarProduct(y)/y.NormSquared()));
|
---|
[ef9df36] | 289 |
|
---|
| 290 | return helper;
|
---|
[6ac7ee] | 291 | };
|
---|
| 292 |
|
---|
| 293 | /** Calculates norm of this vector.
|
---|
| 294 | * \return \f$|x|\f$
|
---|
| 295 | */
|
---|
| 296 | double Vector::Norm() const
|
---|
| 297 | {
|
---|
[273382] | 298 | return (sqrt(NormSquared()));
|
---|
[6ac7ee] | 299 | };
|
---|
| 300 |
|
---|
[d4d0dd] | 301 | /** Calculates squared norm of this vector.
|
---|
| 302 | * \return \f$|x|^2\f$
|
---|
| 303 | */
|
---|
| 304 | double Vector::NormSquared() const
|
---|
| 305 | {
|
---|
[273382] | 306 | return (ScalarProduct(*this));
|
---|
[d4d0dd] | 307 | };
|
---|
| 308 |
|
---|
[6ac7ee] | 309 | /** Normalizes this vector.
|
---|
| 310 | */
|
---|
| 311 | void Vector::Normalize()
|
---|
| 312 | {
|
---|
[1bd79e] | 313 | double factor = Norm();
|
---|
| 314 | (*this) *= 1/factor;
|
---|
[6ac7ee] | 315 | };
|
---|
| 316 |
|
---|
| 317 | /** Zeros all components of this vector.
|
---|
| 318 | */
|
---|
| 319 | void Vector::Zero()
|
---|
| 320 | {
|
---|
[753f02] | 321 | at(0)=at(1)=at(2)=0;
|
---|
[6ac7ee] | 322 | };
|
---|
| 323 |
|
---|
| 324 | /** Zeros all components of this vector.
|
---|
| 325 | */
|
---|
[776b64] | 326 | void Vector::One(const double one)
|
---|
[6ac7ee] | 327 | {
|
---|
[753f02] | 328 | at(0)=at(1)=at(2)=one;
|
---|
[6ac7ee] | 329 | };
|
---|
| 330 |
|
---|
[9c20aa] | 331 | /** Checks whether vector has all components zero.
|
---|
| 332 | * @return true - vector is zero, false - vector is not
|
---|
| 333 | */
|
---|
[54a746] | 334 | bool Vector::IsZero() const
|
---|
[9c20aa] | 335 | {
|
---|
[54a746] | 336 | return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
|
---|
| 337 | };
|
---|
| 338 |
|
---|
| 339 | /** Checks whether vector has length of 1.
|
---|
| 340 | * @return true - vector is normalized, false - vector is not
|
---|
| 341 | */
|
---|
| 342 | bool Vector::IsOne() const
|
---|
| 343 | {
|
---|
| 344 | return (fabs(Norm() - 1.) < MYEPSILON);
|
---|
[9c20aa] | 345 | };
|
---|
| 346 |
|
---|
[ef9df36] | 347 | /** Checks whether vector is normal to \a *normal.
|
---|
| 348 | * @return true - vector is normalized, false - vector is not
|
---|
| 349 | */
|
---|
[273382] | 350 | bool Vector::IsNormalTo(const Vector &normal) const
|
---|
[ef9df36] | 351 | {
|
---|
| 352 | if (ScalarProduct(normal) < MYEPSILON)
|
---|
| 353 | return true;
|
---|
| 354 | else
|
---|
| 355 | return false;
|
---|
| 356 | };
|
---|
| 357 |
|
---|
[b998c3] | 358 | /** Checks whether vector is normal to \a *normal.
|
---|
| 359 | * @return true - vector is normalized, false - vector is not
|
---|
| 360 | */
|
---|
[273382] | 361 | bool Vector::IsEqualTo(const Vector &a) const
|
---|
[b998c3] | 362 | {
|
---|
| 363 | bool status = true;
|
---|
| 364 | for (int i=0;i<NDIM;i++) {
|
---|
[753f02] | 365 | if (fabs(x[i] - a[i]) > MYEPSILON)
|
---|
[b998c3] | 366 | status = false;
|
---|
| 367 | }
|
---|
| 368 | return status;
|
---|
| 369 | };
|
---|
| 370 |
|
---|
[6ac7ee] | 371 | /** Calculates the angle between this and another vector.
|
---|
| 372 | * \param *y array to second vector
|
---|
| 373 | * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
|
---|
| 374 | */
|
---|
[273382] | 375 | double Vector::Angle(const Vector &y) const
|
---|
[6ac7ee] | 376 | {
|
---|
[753f02] | 377 | double norm1 = Norm(), norm2 = y.Norm();
|
---|
[ef9df36] | 378 | double angle = -1;
|
---|
[d4d0dd] | 379 | if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
|
---|
| 380 | angle = this->ScalarProduct(y)/norm1/norm2;
|
---|
[02da9e] | 381 | // -1-MYEPSILON occured due to numerical imprecision, catch ...
|
---|
[e138de] | 382 | //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
|
---|
[02da9e] | 383 | if (angle < -1)
|
---|
| 384 | angle = -1;
|
---|
| 385 | if (angle > 1)
|
---|
| 386 | angle = 1;
|
---|
[042f82] | 387 | return acos(angle);
|
---|
[6ac7ee] | 388 | };
|
---|
| 389 |
|
---|
[0a4f7f] | 390 |
|
---|
| 391 | double& Vector::operator[](size_t i){
|
---|
[753f02] | 392 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
|
---|
| 393 | return x[i];
|
---|
[0a4f7f] | 394 | }
|
---|
| 395 |
|
---|
| 396 | const double& Vector::operator[](size_t i) const{
|
---|
[753f02] | 397 | ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
|
---|
| 398 | return x[i];
|
---|
[0a4f7f] | 399 | }
|
---|
| 400 |
|
---|
| 401 | double& Vector::at(size_t i){
|
---|
| 402 | return (*this)[i];
|
---|
| 403 | }
|
---|
| 404 |
|
---|
| 405 | const double& Vector::at(size_t i) const{
|
---|
| 406 | return (*this)[i];
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | double* Vector::get(){
|
---|
[753f02] | 410 | return x;
|
---|
[0a4f7f] | 411 | }
|
---|
[6ac7ee] | 412 |
|
---|
[ef9df36] | 413 | /** Compares vector \a to vector \a b component-wise.
|
---|
| 414 | * \param a base vector
|
---|
| 415 | * \param b vector components to add
|
---|
| 416 | * \return a == b
|
---|
| 417 | */
|
---|
[72e7fa] | 418 | bool Vector::operator==(const Vector& b) const
|
---|
[ef9df36] | 419 | {
|
---|
[1bd79e] | 420 | return IsEqualTo(b);
|
---|
[ef9df36] | 421 | };
|
---|
| 422 |
|
---|
[fa5a6a] | 423 | bool Vector::operator!=(const Vector& b) const
|
---|
| 424 | {
|
---|
| 425 | return !IsEqualTo(b);
|
---|
| 426 | }
|
---|
| 427 |
|
---|
[6ac7ee] | 428 | /** Sums vector \a to this lhs component-wise.
|
---|
| 429 | * \param a base vector
|
---|
| 430 | * \param b vector components to add
|
---|
| 431 | * \return lhs + a
|
---|
| 432 | */
|
---|
[72e7fa] | 433 | const Vector& Vector::operator+=(const Vector& b)
|
---|
[6ac7ee] | 434 | {
|
---|
[273382] | 435 | this->AddVector(b);
|
---|
[72e7fa] | 436 | return *this;
|
---|
[6ac7ee] | 437 | };
|
---|
[54a746] | 438 |
|
---|
| 439 | /** Subtracts vector \a from this lhs component-wise.
|
---|
| 440 | * \param a base vector
|
---|
| 441 | * \param b vector components to add
|
---|
| 442 | * \return lhs - a
|
---|
| 443 | */
|
---|
[72e7fa] | 444 | const Vector& Vector::operator-=(const Vector& b)
|
---|
[54a746] | 445 | {
|
---|
[273382] | 446 | this->SubtractVector(b);
|
---|
[72e7fa] | 447 | return *this;
|
---|
[54a746] | 448 | };
|
---|
| 449 |
|
---|
[6ac7ee] | 450 | /** factor each component of \a a times a double \a m.
|
---|
| 451 | * \param a base vector
|
---|
| 452 | * \param m factor
|
---|
| 453 | * \return lhs.x[i] * m
|
---|
| 454 | */
|
---|
[b84d5d] | 455 | const Vector& operator*=(Vector& a, const double m)
|
---|
[6ac7ee] | 456 | {
|
---|
[042f82] | 457 | a.Scale(m);
|
---|
| 458 | return a;
|
---|
[6ac7ee] | 459 | };
|
---|
| 460 |
|
---|
[042f82] | 461 | /** Sums two vectors \a and \b component-wise.
|
---|
[6ac7ee] | 462 | * \param a first vector
|
---|
| 463 | * \param b second vector
|
---|
| 464 | * \return a + b
|
---|
| 465 | */
|
---|
[72e7fa] | 466 | Vector const Vector::operator+(const Vector& b) const
|
---|
[6ac7ee] | 467 | {
|
---|
[72e7fa] | 468 | Vector x = *this;
|
---|
[273382] | 469 | x.AddVector(b);
|
---|
[b84d5d] | 470 | return x;
|
---|
[6ac7ee] | 471 | };
|
---|
| 472 |
|
---|
[54a746] | 473 | /** Subtracts vector \a from \b component-wise.
|
---|
| 474 | * \param a first vector
|
---|
| 475 | * \param b second vector
|
---|
| 476 | * \return a - b
|
---|
| 477 | */
|
---|
[72e7fa] | 478 | Vector const Vector::operator-(const Vector& b) const
|
---|
[54a746] | 479 | {
|
---|
[72e7fa] | 480 | Vector x = *this;
|
---|
[273382] | 481 | x.SubtractVector(b);
|
---|
[b84d5d] | 482 | return x;
|
---|
[54a746] | 483 | };
|
---|
| 484 |
|
---|
[6ac7ee] | 485 | /** Factors given vector \a a times \a m.
|
---|
| 486 | * \param a vector
|
---|
| 487 | * \param m factor
|
---|
[54a746] | 488 | * \return m * a
|
---|
[6ac7ee] | 489 | */
|
---|
[b84d5d] | 490 | Vector const operator*(const Vector& a, const double m)
|
---|
[6ac7ee] | 491 | {
|
---|
[b84d5d] | 492 | Vector x(a);
|
---|
| 493 | x.Scale(m);
|
---|
| 494 | return x;
|
---|
[6ac7ee] | 495 | };
|
---|
| 496 |
|
---|
[54a746] | 497 | /** Factors given vector \a a times \a m.
|
---|
| 498 | * \param m factor
|
---|
| 499 | * \param a vector
|
---|
| 500 | * \return m * a
|
---|
| 501 | */
|
---|
[b84d5d] | 502 | Vector const operator*(const double m, const Vector& a )
|
---|
[54a746] | 503 | {
|
---|
[b84d5d] | 504 | Vector x(a);
|
---|
| 505 | x.Scale(m);
|
---|
| 506 | return x;
|
---|
[54a746] | 507 | };
|
---|
| 508 |
|
---|
[9c20aa] | 509 | ostream& operator<<(ostream& ost, const Vector& m)
|
---|
[6ac7ee] | 510 | {
|
---|
[042f82] | 511 | ost << "(";
|
---|
| 512 | for (int i=0;i<NDIM;i++) {
|
---|
[0a4f7f] | 513 | ost << m[i];
|
---|
[042f82] | 514 | if (i != 2)
|
---|
| 515 | ost << ",";
|
---|
| 516 | }
|
---|
| 517 | ost << ")";
|
---|
| 518 | return ost;
|
---|
[6ac7ee] | 519 | };
|
---|
| 520 |
|
---|
| 521 |
|
---|
[1bd79e] | 522 | void Vector::ScaleAll(const double *factor)
|
---|
[6ac7ee] | 523 | {
|
---|
[042f82] | 524 | for (int i=NDIM;i--;)
|
---|
[753f02] | 525 | x[i] *= factor[i];
|
---|
[6ac7ee] | 526 | };
|
---|
| 527 |
|
---|
| 528 |
|
---|
[1bd79e] | 529 |
|
---|
[776b64] | 530 | void Vector::Scale(const double factor)
|
---|
[6ac7ee] | 531 | {
|
---|
[042f82] | 532 | for (int i=NDIM;i--;)
|
---|
| 533 | x[i] *= factor;
|
---|
[6ac7ee] | 534 | };
|
---|
| 535 |
|
---|
[d09ff7] | 536 | /** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
|
---|
| 537 | * \param *M matrix of box
|
---|
| 538 | * \param *Minv inverse matrix
|
---|
| 539 | */
|
---|
[776b64] | 540 | void Vector::WrapPeriodically(const double * const M, const double * const Minv)
|
---|
[d09ff7] | 541 | {
|
---|
| 542 | MatrixMultiplication(Minv);
|
---|
| 543 | // truncate to [0,1] for each axis
|
---|
| 544 | for (int i=0;i<NDIM;i++) {
|
---|
| 545 | x[i] += 0.5; // set to center of box
|
---|
| 546 | while (x[i] >= 1.)
|
---|
| 547 | x[i] -= 1.;
|
---|
| 548 | while (x[i] < 0.)
|
---|
| 549 | x[i] += 1.;
|
---|
| 550 | }
|
---|
| 551 | MatrixMultiplication(M);
|
---|
| 552 | };
|
---|
| 553 |
|
---|
[6ac7ee] | 554 | /** Do a matrix multiplication.
|
---|
| 555 | * \param *matrix NDIM_NDIM array
|
---|
| 556 | */
|
---|
[776b64] | 557 | void Vector::MatrixMultiplication(const double * const M)
|
---|
[6ac7ee] | 558 | {
|
---|
[042f82] | 559 | // do the matrix multiplication
|
---|
[753f02] | 560 | at(0) = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
|
---|
| 561 | at(1) = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
|
---|
| 562 | at(2) = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
|
---|
[6ac7ee] | 563 | };
|
---|
| 564 |
|
---|
[2319ed] | 565 | /** Do a matrix multiplication with the \a *A' inverse.
|
---|
[6ac7ee] | 566 | * \param *matrix NDIM_NDIM array
|
---|
| 567 | */
|
---|
[0a4f7f] | 568 | bool Vector::InverseMatrixMultiplication(const double * const A)
|
---|
[6ac7ee] | 569 | {
|
---|
[042f82] | 570 | double B[NDIM*NDIM];
|
---|
| 571 | double detA = RDET3(A);
|
---|
| 572 | double detAReci;
|
---|
| 573 |
|
---|
| 574 | // calculate the inverse B
|
---|
| 575 | if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
|
---|
| 576 | detAReci = 1./detA;
|
---|
| 577 | B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
|
---|
| 578 | B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
|
---|
| 579 | B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
|
---|
| 580 | B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
|
---|
| 581 | B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
|
---|
| 582 | B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
|
---|
| 583 | B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
|
---|
| 584 | B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
|
---|
| 585 | B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
|
---|
| 586 |
|
---|
| 587 | // do the matrix multiplication
|
---|
[753f02] | 588 | at(0) = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
|
---|
| 589 | at(1) = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
|
---|
| 590 | at(2) = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
|
---|
| 591 |
|
---|
| 592 | return true;
|
---|
[042f82] | 593 | } else {
|
---|
[753f02] | 594 | return false;
|
---|
[042f82] | 595 | }
|
---|
[6ac7ee] | 596 | };
|
---|
| 597 |
|
---|
| 598 |
|
---|
| 599 | /** Creates this vector as the b y *factors' components scaled linear combination of the given three.
|
---|
| 600 | * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
|
---|
| 601 | * \param *x1 first vector
|
---|
| 602 | * \param *x2 second vector
|
---|
| 603 | * \param *x3 third vector
|
---|
| 604 | * \param *factors three-component vector with the factor for each given vector
|
---|
| 605 | */
|
---|
[273382] | 606 | void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
|
---|
[6ac7ee] | 607 | {
|
---|
[273382] | 608 | (*this) = (factors[0]*x1) +
|
---|
| 609 | (factors[1]*x2) +
|
---|
| 610 | (factors[2]*x3);
|
---|
[6ac7ee] | 611 | };
|
---|
| 612 |
|
---|
| 613 | /** Mirrors atom against a given plane.
|
---|
| 614 | * \param n[] normal vector of mirror plane.
|
---|
| 615 | */
|
---|
[273382] | 616 | void Vector::Mirror(const Vector &n)
|
---|
[6ac7ee] | 617 | {
|
---|
[042f82] | 618 | double projection;
|
---|
[753f02] | 619 | projection = ScalarProduct(n)/n.NormSquared(); // remove constancy from n (keep as logical one)
|
---|
[042f82] | 620 | // withdraw projected vector twice from original one
|
---|
| 621 | for (int i=NDIM;i--;)
|
---|
[8cbb97] | 622 | at(i) -= 2.*projection*n[i];
|
---|
[6ac7ee] | 623 | };
|
---|
| 624 |
|
---|
| 625 | /** Calculates orthonormal vector to one given vectors.
|
---|
| 626 | * Just subtracts the projection onto the given vector from this vector.
|
---|
[ef9df36] | 627 | * The removed part of the vector is Vector::Projection()
|
---|
[6ac7ee] | 628 | * \param *x1 vector
|
---|
| 629 | * \return true - success, false - vector is zero
|
---|
| 630 | */
|
---|
[0a4f7f] | 631 | bool Vector::MakeNormalTo(const Vector &y1)
|
---|
[6ac7ee] | 632 | {
|
---|
[042f82] | 633 | bool result = false;
|
---|
[753f02] | 634 | double factor = y1.ScalarProduct(*this)/y1.NormSquared();
|
---|
[042f82] | 635 | Vector x1;
|
---|
[753f02] | 636 | x1 = factor * y1;
|
---|
| 637 | SubtractVector(x1);
|
---|
[042f82] | 638 | for (int i=NDIM;i--;)
|
---|
| 639 | result = result || (fabs(x[i]) > MYEPSILON);
|
---|
[6ac7ee] | 640 |
|
---|
[042f82] | 641 | return result;
|
---|
[6ac7ee] | 642 | };
|
---|
| 643 |
|
---|
| 644 | /** Creates this vector as one of the possible orthonormal ones to the given one.
|
---|
| 645 | * Just scan how many components of given *vector are unequal to zero and
|
---|
| 646 | * try to get the skp of both to be zero accordingly.
|
---|
| 647 | * \param *vector given vector
|
---|
| 648 | * \return true - success, false - failure (null vector given)
|
---|
| 649 | */
|
---|
[273382] | 650 | bool Vector::GetOneNormalVector(const Vector &GivenVector)
|
---|
[6ac7ee] | 651 | {
|
---|
[042f82] | 652 | int Components[NDIM]; // contains indices of non-zero components
|
---|
| 653 | int Last = 0; // count the number of non-zero entries in vector
|
---|
| 654 | int j; // loop variables
|
---|
| 655 | double norm;
|
---|
| 656 |
|
---|
| 657 | for (j=NDIM;j--;)
|
---|
| 658 | Components[j] = -1;
|
---|
[1829c4] | 659 |
|
---|
| 660 | // in two component-systems we need to find the one position that is zero
|
---|
| 661 | int zeroPos = -1;
|
---|
[042f82] | 662 | // find two components != 0
|
---|
[1829c4] | 663 | for (j=0;j<NDIM;j++){
|
---|
[753f02] | 664 | if (fabs(GivenVector[j]) > MYEPSILON)
|
---|
[042f82] | 665 | Components[Last++] = j;
|
---|
[1829c4] | 666 | else
|
---|
| 667 | // this our zero Position
|
---|
| 668 | zeroPos = j;
|
---|
| 669 | }
|
---|
[042f82] | 670 |
|
---|
| 671 | switch(Last) {
|
---|
| 672 | case 3: // threecomponent system
|
---|
[1829c4] | 673 | // the position of the zero is arbitrary in three component systems
|
---|
| 674 | zeroPos = Components[2];
|
---|
[042f82] | 675 | case 2: // two component system
|
---|
[753f02] | 676 | norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
|
---|
[1829c4] | 677 | at(zeroPos) = 0.;
|
---|
[042f82] | 678 | // in skp both remaining parts shall become zero but with opposite sign and third is zero
|
---|
[1829c4] | 679 | at(Components[1]) = -1./GivenVector[Components[1]] / norm;
|
---|
| 680 | at(Components[0]) = 1./GivenVector[Components[0]] / norm;
|
---|
[042f82] | 681 | return true;
|
---|
| 682 | break;
|
---|
| 683 | case 1: // one component system
|
---|
| 684 | // set sole non-zero component to 0, and one of the other zero component pendants to 1
|
---|
[1829c4] | 685 | at((Components[0]+2)%NDIM) = 0.;
|
---|
| 686 | at((Components[0]+1)%NDIM) = 1.;
|
---|
| 687 | at(Components[0]) = 0.;
|
---|
[042f82] | 688 | return true;
|
---|
| 689 | break;
|
---|
| 690 | default:
|
---|
| 691 | return false;
|
---|
| 692 | }
|
---|
[6ac7ee] | 693 | };
|
---|
| 694 |
|
---|
| 695 | /** Adds vector \a *y componentwise.
|
---|
| 696 | * \param *y vector
|
---|
| 697 | */
|
---|
[273382] | 698 | void Vector::AddVector(const Vector &y)
|
---|
[6ac7ee] | 699 | {
|
---|
[753f02] | 700 | for(int i=NDIM;i--;)
|
---|
| 701 | x[i] += y[i];
|
---|
[6ac7ee] | 702 | }
|
---|
| 703 |
|
---|
| 704 | /** Adds vector \a *y componentwise.
|
---|
| 705 | * \param *y vector
|
---|
| 706 | */
|
---|
[273382] | 707 | void Vector::SubtractVector(const Vector &y)
|
---|
[6ac7ee] | 708 | {
|
---|
[753f02] | 709 | for(int i=NDIM;i--;)
|
---|
| 710 | x[i] -= y[i];
|
---|
[ef9df36] | 711 | }
|
---|
| 712 |
|
---|
[89c8b2] | 713 | /**
|
---|
| 714 | * Checks whether this vector is within the parallelepiped defined by the given three vectors and
|
---|
| 715 | * their offset.
|
---|
| 716 | *
|
---|
| 717 | * @param offest for the origin of the parallelepiped
|
---|
| 718 | * @param three vectors forming the matrix that defines the shape of the parallelpiped
|
---|
| 719 | */
|
---|
[776b64] | 720 | bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
|
---|
[89c8b2] | 721 | {
|
---|
[753f02] | 722 | Vector a = (*this)-offset;
|
---|
[89c8b2] | 723 | a.InverseMatrixMultiplication(parallelepiped);
|
---|
| 724 | bool isInside = true;
|
---|
| 725 |
|
---|
| 726 | for (int i=NDIM;i--;)
|
---|
[753f02] | 727 | isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
|
---|
[89c8b2] | 728 |
|
---|
| 729 | return isInside;
|
---|
| 730 | }
|
---|
[005e18] | 731 |
|
---|
| 732 |
|
---|
| 733 | // some comonly used vectors
|
---|
| 734 | const Vector zeroVec(0,0,0);
|
---|
| 735 | const Vector e1(1,0,0);
|
---|
| 736 | const Vector e2(0,1,0);
|
---|
| 737 | const Vector e3(0,0,1);
|
---|