/** \file vector.cpp * * Function implementations for the class vector. * */ #include "molecules.hpp" /************************************ Functions for class vector ************************************/ /** Constructor of class vector. */ vector::vector() { x[0] = x[1] = x[2] = 0.; }; /** Desctructor of class vector. */ vector::~vector() {}; /** Calculates distance between this and another vector. * \param *y array to second vector * \return \f$| x - y |^2\f$ */ double vector::Distance(const vector *y) const { double res = 0.; for (int i=0;ix[i])*(x[i]-y->x[i]); return (res); }; /** Calculates distance between this and another vector in a periodic cell. * \param *y array to second vector * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell * \return \f$| x - y |^2\f$ */ double vector::PeriodicDistance(const vector *y, const double *cell_size) const { double res = Distance(y), tmp, matrix[NDIM*NDIM]; vector Shiftedy, TranslationVector; int N[NDIM]; matrix[0] = cell_size[0]; matrix[1] = cell_size[1]; matrix[2] = cell_size[3]; matrix[3] = cell_size[1]; matrix[4] = cell_size[2]; matrix[5] = cell_size[4]; matrix[6] = cell_size[3]; matrix[7] = cell_size[4]; matrix[8] = cell_size[5]; // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells for (N[0]=-1;N[0]<=1;N[0]++) for (N[1]=-1;N[1]<=1;N[1]++) for (N[2]=-1;N[2]<=1;N[2]++) { // create the translation vector TranslationVector.Zero(); for (int i=0;ix[i]; return (res); }; /** Calculates the projection of a vector onto another \a *y. * \param *y array to second vector * \return \f$\langle x, y \rangle\f$ */ double vector::Projection(const vector *y) const { return (ScalarProduct(y)/Norm()/y->Norm()); }; /** Calculates norm of this vector. * \return \f$|x|\f$ */ double vector::Norm() const { double res = 0.; for (int i=0;ix[i]*this->x[i]; return (sqrt(res)); }; /** Normalizes this vector. */ void vector::Normalize() { double res = 0.; for (int i=0;ix[i]*this->x[i]; res = 1./sqrt(res); Scale(&res); }; /** Zeros all components of this vector. */ void vector::Zero() { for (int i=0;ix[i] = 0.; }; /** Calculates the angle between this and another vector. * \param *y array to second vector * \return \f$\frac{\langle x, y \rangle}{|x||y|}\f$ */ double vector::Angle(vector *y) const { return (this->ScalarProduct(y)/(this->Norm()*y->Norm())); }; /** Rotates the vector around the axis given by \a *axis by an angle of \a alpha. * \param *axis rotation axis * \param alpha rotation angle in radian */ void vector::RotateVector(const vector *axis, const double alpha) { vector a,y; // normalise this vector with respect to axis a.CopyVector(this); a.Scale(Projection(axis)); SubtractVector(&a); // construct normal vector y.MakeNormalVector(axis,this); y.Scale(Norm()); // scale normal vector by sine and this vector by cosine y.Scale(sin(alpha)); Scale(cos(alpha)); // add scaled normal vector onto this vector AddVector(&y); // add part in axis direction AddVector(&a); }; /** Prints a 3dim vector. * prints no end of line. * \param *out output stream */ bool vector::Output(ofstream *out) const { if (out != NULL) { *out << "("; for (int i=0;ix[i] *= (*factor)[i]; }; void vector::Scale(double *factor) { for (int i=0;ix[i] *= *factor; }; void vector::Scale(double factor) { for (int i=0;ix[i] *= factor; }; /** Translate atom by given vector. * \param trans[] translation vector. */ void vector::Translate(const vector *trans) { for (int i=0;ix[i]; }; /** Do a matrix multiplication. * \param *matrix NDIM_NDIM array */ void vector::MatrixMultiplication(double *M) { vector C; // do the matrix multiplication C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2]; C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2]; C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2]; // transfer the result into this for (int i=0;i MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular detAReci = 1./detA; B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33 // do the matrix multiplication C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2]; C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2]; C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2]; // transfer the result into this for (int i=0;ix[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i]; }; /** Mirrors atom against a given plane. * \param n[] normal vector of mirror plane. */ void vector::Mirror(const vector *n) { double projection; projection = ScalarProduct(n)/((vector *)n)->ScalarProduct(n); // remove constancy from n (keep as logical one) // withdraw projected vector twice from original one cout << Verbose(1) << "Vector: "; Output((ofstream *)&cout); cout << "\t"; for (int i=0;ix[i]; cout << "Projected vector: "; Output((ofstream *)&cout); cout << endl; }; /** Calculates normal vector for three given vectors (being three points in space). * Makes this vector orthonormal to the three given points, making up a place in 3d space. * \param *y1 first vector * \param *y2 second vector * \param *y3 third vector * \return true - success, vectors are linear independent, false - failure due to linear dependency */ bool vector::MakeNormalVector(const vector *y1, const vector *y2, const vector *y3) { vector x1, x2; x1.CopyVector(y1); x1.SubtractVector(y2); x2.CopyVector(y3); x2.SubtractVector(y2); if ((x1.Norm()==0) || (x2.Norm()==0)) { cout << Verbose(4) << "Given vectors are linear dependent." << endl; return false; } cout << Verbose(4) << "relative, first plane coordinates:"; x1.Output((ofstream *)&cout); cout << endl; cout << Verbose(4) << "second plane coordinates:"; x2.Output((ofstream *)&cout); cout << endl; this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]); this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]); this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]); Normalize(); return true; }; /** Calculates orthonormal vector to two given vectors. * Makes this vector orthonormal to two given vectors. This is very similar to the other * vector::MakeNormalVector(), only there three points whereas here two difference * vectors are given. * \param *x1 first vector * \param *x2 second vector * \return true - success, vectors are linear independent, false - failure due to linear dependency */ bool vector::MakeNormalVector(const vector *y1, const vector *y2) { vector x1,x2; x1.CopyVector(y1); x2.CopyVector(y2); Zero(); if ((x1.Norm()==0) || (x2.Norm()==0)) { cout << Verbose(4) << "Given vectors are linear dependent." << endl; return false; } cout << Verbose(4) << "relative, first plane coordinates:"; x1.Output((ofstream *)&cout); cout << endl; cout << Verbose(4) << "second plane coordinates:"; x2.Output((ofstream *)&cout); cout << endl; this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]); this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]); this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]); Normalize(); return true; }; /** Calculates orthonormal vector to one given vectors. * Just subtracts the projection onto the given vector from this vector. * \param *x1 vector * \return true - success, false - vector is zero */ bool vector::MakeNormalVector(const vector *y1) { bool result = false; vector x1; x1.CopyVector(y1); x1.Scale(x1.Projection(this)); SubtractVector(&x1); for (int i=0;i MYEPSILON); return result; }; /** Creates this vector as one of the possible orthonormal ones to the given one. * Just scan how many components of given *vector are unequal to zero and * try to get the skp of both to be zero accordingly. * \param *vector given vector * \return true - success, false - failure (null vector given) */ bool vector::GetOneNormalVector(const vector *vector) { int Components[NDIM]; // contains indices of non-zero components int Last = 0; // count the number of non-zero entries in vector int j; // loop variables double norm; cout << Verbose(4); vector->Output((ofstream *)&cout); cout << endl; for (j=0;jx[j]) > MYEPSILON) Components[Last++] = j; cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl; switch(Last) { case 3: // threecomponent system case 2: // two component system norm = sqrt(1./(vector->x[Components[1]]*vector->x[Components[1]]) + 1./(vector->x[Components[0]]*vector->x[Components[0]])); x[Components[2]] = 0.; // in skp both remaining parts shall become zero but with opposite sign and third is zero x[Components[1]] = -1./vector->x[Components[1]] / norm; x[Components[0]] = 1./vector->x[Components[0]] / norm; return true; break; case 1: // one component system // set sole non-zero component to 0, and one of the other zero component pendants to 1 x[(Components[0]+2)%NDIM] = 0.; x[(Components[0]+1)%NDIM] = 1.; x[Components[0]] = 0.; return true; break; default: return false; } }; /** Creates a new vector as the one with least square distance to a given set of \a vectors. * \param *vectors set of vectors * \param num number of vectors * \return true if success, false if failed due to linear dependency */ bool vector::LSQdistance(vector **vectors, int num) { int j; for (j=0;jOutput((ofstream *)&cout); cout << endl; } int np = 3; struct LSQ_params par; const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex; gsl_multimin_fminimizer *s = NULL; gsl_vector *ss, *x; gsl_multimin_function minex_func; size_t iter = 0, i; int status; double size; /* Initial vertex size vector */ ss = gsl_vector_alloc (np); x = gsl_vector_alloc (np); /* Set all step sizes to 1 */ gsl_vector_set_all (ss, 1.0); /* Starting point */ par.vectors = vectors; par.num = num; for (i=0;ix[i] - vectors[1]->x[i])/2.); /* Initialize method and iterate */ minex_func.f = &LSQ; minex_func.n = np; minex_func.params = (void *)∥ s = gsl_multimin_fminimizer_alloc (T, np); gsl_multimin_fminimizer_set (s, &minex_func, x, ss); do { iter++; status = gsl_multimin_fminimizer_iterate(s); if (status) break; size = gsl_multimin_fminimizer_size (s); status = gsl_multimin_test_size (size, 1e-2); if (status == GSL_SUCCESS) { printf ("converged to minimum at\n"); } printf ("%5d ", (int)iter); for (i = 0; i < (size_t)np; i++) { printf ("%10.3e ", gsl_vector_get (s->x, i)); } printf ("f() = %7.3f size = %.3f\n", s->fval, size); } while (status == GSL_CONTINUE && iter < 100); for (i=0;i<(size_t)np;i++) this->x[i] = gsl_vector_get(s->x, i); gsl_vector_free(x); gsl_vector_free(ss); gsl_multimin_fminimizer_free (s); return true; }; /** Adds vector \a *y componentwise. * \param *y vector */ void vector::AddVector(const vector *y) { for (int i=0;ix[i] += y->x[i]; } /** Adds vector \a *y componentwise. * \param *y vector */ void vector::SubtractVector(const vector *y) { for (int i=0;ix[i] -= y->x[i]; } /** Copy vector \a *y componentwise. * \param *y vector */ void vector::CopyVector(const vector *y) { for (int i=0;ix[i] = y->x[i]; } /** Asks for position, checks for boundary. * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size * \param check whether bounds shall be checked (true) or not (false) */ void vector::AskPosition(double *cell_size, bool check) { char coords[3] = {'x','y','z'}; int j = -1; for (int i=0;i<3;i++) { j += i+1; do { cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: "; cin >> x[i]; } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check)); } }; /** Solves a vectorial system consisting of two orthogonal statements and a norm statement. * This is linear system of equations to be solved, however of the three given (skp of this vector\ * with either of the three hast to be zero) only two are linear independent. The third equation * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution * where very often it has to be checked whether a certain value is zero or not and thus forked into * another case. * \param *x1 first vector * \param *x2 second vector * \param *y third vector * \param alpha first angle * \param beta second angle * \param c norm of final vector * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c. * \bug this is not yet working properly */ bool vector::SolveSystem(vector *x1, vector *x2, vector *y, double alpha, double beta, double c) { double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C; double ang; // angle on testing double sign[3]; int i,j,k; A = cos(alpha) * x1->Norm() * c; B1 = cos(beta + M_PI/2.) * y->Norm() * c; B2 = cos(beta) * x2->Norm() * c; C = c * c; cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl; int flag = 0; if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping if (fabs(x1->x[1]) > MYEPSILON) { flag = 1; } else if (fabs(x1->x[2]) > MYEPSILON) { flag = 2; } else { return false; } } switch (flag) { default: case 0: break; case 2: flip(&x1->x[0],&x1->x[1]); flip(&x2->x[0],&x2->x[1]); flip(&y->x[0],&y->x[1]); //flip(&x[0],&x[1]); flip(&x1->x[1],&x1->x[2]); flip(&x2->x[1],&x2->x[2]); flip(&y->x[1],&y->x[2]); //flip(&x[1],&x[2]); case 1: flip(&x1->x[0],&x1->x[1]); flip(&x2->x[0],&x2->x[1]); flip(&y->x[0],&y->x[1]); //flip(&x[0],&x[1]); flip(&x1->x[1],&x1->x[2]); flip(&x2->x[1],&x2->x[2]); flip(&y->x[1],&y->x[2]); //flip(&x[1],&x[2]); break; } // now comes the case system D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1]; D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2]; D3 = y->x[0]/x1->x[0]*A-B1; cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n"; if (fabs(D1) < MYEPSILON) { cout << Verbose(2) << "D1 == 0!\n"; if (fabs(D2) > MYEPSILON) { cout << Verbose(3) << "D2 != 0!\n"; x[2] = -D3/D2; E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2; E2 = -x1->x[1]/x1->x[0]; cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n"; F1 = E1*E1 + 1.; F2 = -E1*E2; F3 = E1*E1 + D3*D3/(D2*D2) - C; cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"; if (fabs(F1) < MYEPSILON) { cout << Verbose(4) << "F1 == 0!\n"; cout << Verbose(4) << "Gleichungssystem linear\n"; x[1] = F3/(2.*F2); } else { p = F2/F1; q = p*p - F3/F1; cout << Verbose(4) << "p " << p << "\tq " << q << endl; if (q < 0) { cout << Verbose(4) << "q < 0" << endl; return false; } x[1] = p + sqrt(q); } x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2]; } else { cout << Verbose(2) << "Gleichungssystem unterbestimmt\n"; return false; } } else { E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1; E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2]; cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n"; F1 = E2*E2 + D2*D2/(D1*D1) + 1.; F2 = -(E1*E2 + D2*D3/(D1*D1)); F3 = E1*E1 + D3*D3/(D1*D1) - C; cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"; if (fabs(F1) < MYEPSILON) { cout << Verbose(3) << "F1 == 0!\n"; cout << Verbose(3) << "Gleichungssystem linear\n"; x[2] = F3/(2.*F2); } else { p = F2/F1; q = p*p - F3/F1; cout << Verbose(3) << "p " << p << "\tq " << q << endl; if (q < 0) { cout << Verbose(3) << "q < 0" << endl; return false; } x[2] = p + sqrt(q); } x[1] = (-D2 * x[2] - D3)/D1; x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2]; } switch (flag) { // back-flipping default: case 0: break; case 2: flip(&x1->x[0],&x1->x[1]); flip(&x2->x[0],&x2->x[1]); flip(&y->x[0],&y->x[1]); flip(&x[0],&x[1]); flip(&x1->x[1],&x1->x[2]); flip(&x2->x[1],&x2->x[2]); flip(&y->x[1],&y->x[2]); flip(&x[1],&x[2]); case 1: flip(&x1->x[0],&x1->x[1]); flip(&x2->x[0],&x2->x[1]); flip(&y->x[0],&y->x[1]); //flip(&x[0],&x[1]); flip(&x1->x[1],&x1->x[2]); flip(&x2->x[1],&x2->x[2]); flip(&y->x[1],&y->x[2]); flip(&x[1],&x[2]); break; } // one z component is only determined by its radius (without sign) // thus check eight possible sign flips and determine by checking angle with second vector for (i=0;i<8;i++) { // set sign vector accordingly for (j=2;j>=0;j--) { k = (i & pot(2,j)) << j; cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl; sign[j] = (k == 0) ? 1. : -1.; } cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n"; // apply sign matrix for (j=0;jAngle (this); cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t"; if (fabs(ang - cos(beta)) < MYEPSILON) { break; } // unapply sign matrix (is its own inverse) for (j=0;j