Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/vector.cpp

    • Property mode changed from 100755 to 100644
    rcc2ee5 r2985c8  
    11/** \file vector.cpp
    2  *
     2 * 
    33 * Function implementations for the class vector.
    4  *
    5  */
    6 
     4 * 
     5 */
     6 
    77#include "molecules.hpp"
    8 
     8 
    99
    1010/************************************ Functions for class vector ************************************/
     
    2222Vector::~Vector() {};
    2323
    24 /** Calculates square of distance between this and another vector.
     24/** Calculates distance between this and another vector.
    2525 * \param *y array to second vector
    2626 * \return \f$| x - y |^2\f$
    2727 */
    28 double Vector::DistanceSquared(const Vector *y) const
     28double Vector::Distance(const Vector *y) const
    2929{
    3030  double res = 0.;
    3131  for (int i=NDIM;i--;)
    3232    res += (x[i]-y->x[i])*(x[i]-y->x[i]);
    33   return (res);
    34 };
    35 
    36 /** Calculates distance between this and another vector.
    37  * \param *y array to second vector
    38  * \return \f$| x - y |\f$
    39  */
    40 double Vector::Distance(const Vector *y) const
    41 {
    42   double res = 0.;
    43   for (int i=NDIM;i--;)
    44     res += (x[i]-y->x[i])*(x[i]-y->x[i]);
    45   return (sqrt(res));
     33  return (res); 
    4634};
    4735
     
    8169        if (tmp < res) res = tmp;
    8270      }
    83   return (res);
     71  return (res); 
    8472};
    8573
     
    124112  for (int i=NDIM;i--;)
    125113    res += x[i]*y->x[i];
    126   return (res);
    127 };
    128 
    129 
    130 /** Calculates VectorProduct between this and another vector.
    131  *  -# returns the Product in place of vector from which it was initiated
    132  *  -# ATTENTION: Only three dim.
    133  *  \param *y array to vector with which to calculate crossproduct
    134  *  \return \f$ x \times y \f&
    135  */
    136 void Vector::VectorProduct(const Vector *y)
    137 {
    138   Vector tmp;
    139   tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
    140   tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
    141   tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
    142   this->CopyVector(&tmp);
    143 
    144 };
    145 
     114  return (res); 
     115};
    146116
    147117/** projects this vector onto plane defined by \a *y.
    148  * \param *y normal vector of plane
     118 * \param *y array to normal vector of plane
    149119 * \return \f$\langle x, y \rangle\f$
    150120 */
     
    153123  Vector tmp;
    154124  tmp.CopyVector(y);
    155   tmp.Normalize();
    156   tmp.Scale(ScalarProduct(&tmp));
     125  tmp.Scale(Projection(y));
    157126  this->SubtractVector(&tmp);
    158127};
     
    175144  for (int i=NDIM;i--;)
    176145    res += this->x[i]*this->x[i];
    177   return (sqrt(res));
     146  return (sqrt(res)); 
    178147};
    179148
     
    209178 */
    210179void Vector::Init(double x1, double x2, double x3)
    211 {
     180{ 
    212181  x[0] = x1;
    213182  x[1] = x2;
     
    219188 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
    220189 */
    221 double Vector::Angle(const Vector *y) const
    222 {
    223   return acos(this->ScalarProduct(y)/Norm()/y->Norm());
     190double Vector::Angle(Vector *y) const
     191{
     192  return acos(this->ScalarProduct(y)/Norm()/y->Norm()); 
    224193};
    225194
     
    269238
    270239/** Sums two vectors \a  and \b component-wise.
    271  * \param a first vector
     240 * \param a first vector 
    272241 * \param b second vector
    273242 * \return a + b
     
    282251
    283252/** Factors given vector \a a times \a m.
    284  * \param a vector
     253 * \param a vector 
    285254 * \param m factor
    286255 * \return a + b
     
    313282};
    314283
    315 /** Prints a 3dim vector to a stream.
    316  * \param ost output stream
    317  * \param v Vector to be printed
    318  * \return output stream
    319  */
    320 ostream& operator<<(ostream& ost,Vector& m)
    321 {
    322   ost << "(";
    323   for (int i=0;i<NDIM;i++) {
    324     ost << m.x[i];
    325     if (i != 2)
    326       ost << ",";
    327   }
    328   ost << ")";
     284ofstream& operator<<(ofstream& ost,Vector& m)
     285{
     286        m.Output(&ost);
    329287        return ost;
    330288};
     
    351309};
    352310
    353 /** Translate atom by given vector.
     311/** Translate atom by given vector. 
    354312 * \param trans[] translation vector.
    355313 */
     
    358316  for (int i=NDIM;i--;)
    359317    x[i] += trans->x[i];
    360 };
     318}; 
    361319
    362320/** Do a matrix multiplication.
     
    397355    B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]);    // A_32
    398356    B[8] =  detAReci*RDET2(A[0],A[1],A[3],A[4]);    // A_33
    399 
     357 
    400358    // do the matrix multiplication
    401359    C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
     
    421379{
    422380  for(int i=NDIM;i--;)
    423     x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
    424 };
    425 
    426 /** Mirrors atom against a given plane.
     381    x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i]; 
     382};
     383
     384/** Mirrors atom against a given plane. 
    427385 * \param n[] normal vector of mirror plane.
    428386 */
     
    440398  Output((ofstream *)&cout);
    441399  cout << endl;
    442 };
     400}; 
    443401
    444402/** Calculates normal vector for three given vectors (being three points in space).
     
    472430  this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
    473431  Normalize();
    474 
     432 
    475433  return true;
    476434};
     
    530488/** Creates this vector as one of the possible orthonormal ones to the given one.
    531489 * Just scan how many components of given *vector are unequal to zero and
    532  * try to get the skp of both to be zero accordingly.
     490 * try to get the skp of both to be zero accordingly. 
    533491 * \param *vector given vector
    534492 * \return true - success, false - failure (null vector given)
     
    551509      Components[Last++] = j;
    552510  cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
    553 
     511       
    554512  switch(Last) {
    555513    case 3:  // threecomponent system
     
    564522    case 1: // one component system
    565523      // set sole non-zero component to 0, and one of the other zero component pendants to 1
    566       x[(Components[0]+2)%NDIM] = 0.;
    567       x[(Components[0]+1)%NDIM] = 1.;
    568       x[Components[0]] = 0.;
     524      x[(Components[0]+2)%NDIM] = 0.;   
     525      x[(Components[0]+1)%NDIM] = 1.;   
     526      x[Components[0]] = 0.;   
    569527      return true;
    570528      break;
     
    583541{
    584542//  cout << Verbose(3) << "For comparison: ";
    585 //  cout << "A " << A->Projection(this) << "\t";
    586 //  cout << "B " << B->Projection(this) << "\t";
    587 //  cout << "C " << C->Projection(this) << "\t";
     543//  cout << "A " << A->Projection(this) << "\t"; 
     544//  cout << "B " << B->Projection(this) << "\t"; 
     545//  cout << "C " << C->Projection(this) << "\t"; 
    588546//  cout << endl;
    589547  return A->Projection(this);
     
    595553 * \return true if success, false if failed due to linear dependency
    596554 */
    597 bool Vector::LSQdistance(Vector **vectors, int num)
     555bool Vector::LSQdistance(Vector **vectors, int num) 
    598556{
    599557        int j;
    600 
     558                               
    601559        for (j=0;j<num;j++) {
    602560                cout << Verbose(1) << j << "th atom's vector: ";
     
    607565  int np = 3;
    608566        struct LSQ_params par;
    609 
     567   
    610568   const gsl_multimin_fminimizer_type *T =
    611569     gsl_multimin_fminimizer_nmsimplex;
     
    613571   gsl_vector *ss, *y;
    614572   gsl_multimin_function minex_func;
    615 
     573 
    616574   size_t iter = 0, i;
    617575   int status;
    618576   double size;
    619 
     577 
    620578   /* Initial vertex size vector */
    621579   ss = gsl_vector_alloc (np);
    622580   y = gsl_vector_alloc (np);
    623 
     581 
    624582   /* Set all step sizes to 1 */
    625583   gsl_vector_set_all (ss, 1.0);
    626 
     584 
    627585   /* Starting point */
    628586   par.vectors = vectors;
    629587         par.num = num;
    630 
     588       
    631589         for (i=NDIM;i--;)
    632                 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
    633 
     590                gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.); 
     591         
    634592   /* Initialize method and iterate */
    635593   minex_func.f = &LSQ;
    636594   minex_func.n = np;
    637595   minex_func.params = (void *)&par;
    638 
     596 
    639597   s = gsl_multimin_fminimizer_alloc (T, np);
    640598   gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
    641 
     599 
    642600   do
    643601     {
    644602       iter++;
    645603       status = gsl_multimin_fminimizer_iterate(s);
    646 
     604 
    647605       if (status)
    648606         break;
    649 
     607 
    650608       size = gsl_multimin_fminimizer_size (s);
    651609       status = gsl_multimin_test_size (size, 1e-2);
    652 
     610 
    653611       if (status == GSL_SUCCESS)
    654612         {
    655613           printf ("converged to minimum at\n");
    656614         }
    657 
     615 
    658616       printf ("%5d ", (int)iter);
    659617       for (i = 0; i < (size_t)np; i++)
     
    664622     }
    665623   while (status == GSL_CONTINUE && iter < 100);
    666 
     624 
    667625  for (i=(size_t)np;i--;)
    668626    this->x[i] = gsl_vector_get(s->x, i);
     
    730688 * \param alpha first angle
    731689 * \param beta second angle
    732  * \param c norm of final vector
     690 * \param c norm of final vector 
    733691 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
    734  * \bug this is not yet working properly
     692 * \bug this is not yet working properly 
    735693 */
    736694bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
     
    748706  if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
    749707    if (fabs(x1->x[1]) > MYEPSILON) {
    750       flag = 1;
     708      flag = 1;   
    751709    } else if (fabs(x1->x[2]) > MYEPSILON) {
    752710       flag = 2;
     
    781739  // now comes the case system
    782740  D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
    783   D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
     741  D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2]; 
    784742  D3 = y->x[0]/x1->x[0]*A-B1;
    785743  cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
    786744  if (fabs(D1) < MYEPSILON) {
    787     cout << Verbose(2) << "D1 == 0!\n";
     745    cout << Verbose(2) << "D1 == 0!\n"; 
    788746    if (fabs(D2) > MYEPSILON) {
    789       cout << Verbose(3) << "D2 != 0!\n";
     747      cout << Verbose(3) << "D2 != 0!\n"; 
    790748      x[2] = -D3/D2;
    791749      E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
     
    797755      cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
    798756      if (fabs(F1) < MYEPSILON) {
    799         cout << Verbose(4) << "F1 == 0!\n";
     757        cout << Verbose(4) << "F1 == 0!\n"; 
    800758        cout << Verbose(4) << "Gleichungssystem linear\n";
    801         x[1] = F3/(2.*F2);
     759        x[1] = F3/(2.*F2); 
    802760      } else {
    803761        p = F2/F1;
    804762        q = p*p - F3/F1;
    805         cout << Verbose(4) << "p " << p << "\tq " << q << endl;
     763        cout << Verbose(4) << "p " << p << "\tq " << q << endl; 
    806764        if (q < 0) {
    807765          cout << Verbose(4) << "q < 0" << endl;
     
    824782    cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
    825783    if (fabs(F1) < MYEPSILON) {
    826       cout << Verbose(3) << "F1 == 0!\n";
     784      cout << Verbose(3) << "F1 == 0!\n"; 
    827785      cout << Verbose(3) << "Gleichungssystem linear\n";
    828       x[2] = F3/(2.*F2);
     786      x[2] = F3/(2.*F2);     
    829787    } else {
    830788      p = F2/F1;
    831789      q = p*p - F3/F1;
    832       cout << Verbose(3) << "p " << p << "\tq " << q << endl;
     790      cout << Verbose(3) << "p " << p << "\tq " << q << endl; 
    833791      if (q < 0) {
    834792        cout << Verbose(3) << "q < 0" << endl;
     
    874832    }
    875833    cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
    876     // apply sign matrix
     834    // apply sign matrix 
    877835    for (j=NDIM;j--;)
    878836      x[j] *= sign[j];
     
    880838    ang = x2->Angle (this);
    881839    cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
    882     if (fabs(ang - cos(beta)) < MYEPSILON) {
     840    if (fabs(ang - cos(beta)) < MYEPSILON) { 
    883841      break;
    884842    }
Note: See TracChangeset for help on using the changeset viewer.