Ignore:
Timestamp:
Dec 3, 2008, 2:12:05 PM (17 years ago)
Author:
Christian Neuen <neuen@…>
Children:
3e20fe
Parents:
f5b58e
Message:

Multiple changes to boundary, currently not fully operational.
Molecules has a new routine to create adjacency lists, reading bonds from a dbond file instead of looking for the distances by itself.
Vector function Project onto plane has been updated.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/vector.cpp

    rf5b58e re0c5b1  
    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 ************************************/
     
    3131  for (int i=NDIM;i--;)
    3232    res += (x[i]-y->x[i])*(x[i]-y->x[i]);
    33   return (res); 
     33  return (res);
    3434};
    3535
     
    6969        if (tmp < res) res = tmp;
    7070      }
    71   return (res); 
     71  return (res);
    7272};
    7373
     
    112112  for (int i=NDIM;i--;)
    113113    res += x[i]*y->x[i];
    114   return (res); 
     114  return (res);
    115115};
    116116
     
    121121 *  \param *y array to vector with which to calculate crossproduct
    122122 *  \return \f$ x \times y \f&
    123  */ 
     123 */
    124124void Vector::VectorProduct(const Vector *y)
    125125{
    126126  Vector tmp;
    127   tmp[0] = x[1]*y->x[2] - x[2]*y->x[1];
    128   tmp[1] = x[2]*y->x[0] - x[0]*y->x[2];
    129   tmp[2] = x[0]*y->x[1] - x[1]*Y->x[0];
     127  tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
     128  tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
     129  tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
    130130  this->CopyVector(&tmp);
    131131
     
    134134
    135135/** projects this vector onto plane defined by \a *y.
    136  * \param *y array to normal vector of plane
     136 * \param *y normal vector of plane
    137137 * \return \f$\langle x, y \rangle\f$
    138138 */
     
    141141  Vector tmp;
    142142  tmp.CopyVector(y);
    143   tmp.Scale(Projection(y));
     143  tmp.Normalize();
     144  tmp.Scale(ScalarProduct(&tmp));
    144145  this->SubtractVector(&tmp);
    145146};
     
    162163  for (int i=NDIM;i--;)
    163164    res += this->x[i]*this->x[i];
    164   return (sqrt(res)); 
     165  return (sqrt(res));
    165166};
    166167
     
    196197 */
    197198void Vector::Init(double x1, double x2, double x3)
    198 { 
     199{
    199200  x[0] = x1;
    200201  x[1] = x2;
     
    202203};
    203204
    204 /** Calculates the angle between this and another vector. 
     205/** Calculates the angle between this and another vector.
    205206 * \param *y array to second vector
    206207 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
     
    208209double Vector::Angle(Vector *y) const
    209210{
    210   return acos(this->ScalarProduct(y)/Norm()/y->Norm()); 
     211  return acos(this->ScalarProduct(y)/Norm()/y->Norm());
    211212};
    212213
     
    256257
    257258/** Sums two vectors \a  and \b component-wise.
    258  * \param a first vector 
     259 * \param a first vector
    259260 * \param b second vector
    260261 * \return a + b
     
    269270
    270271/** Factors given vector \a a times \a m.
    271  * \param a vector 
     272 * \param a vector
    272273 * \param m factor
    273274 * \return a + b
     
    327328};
    328329
    329 /** Translate atom by given vector. 
     330/** Translate atom by given vector.
    330331 * \param trans[] translation vector.
    331332 */
     
    334335  for (int i=NDIM;i--;)
    335336    x[i] += trans->x[i];
    336 }; 
     337};
    337338
    338339/** Do a matrix multiplication.
     
    373374    B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]);    // A_32
    374375    B[8] =  detAReci*RDET2(A[0],A[1],A[3],A[4]);    // A_33
    375  
     376
    376377    // do the matrix multiplication
    377378    C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
     
    397398{
    398399  for(int i=NDIM;i--;)
    399     x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i]; 
    400 };
    401 
    402 /** Mirrors atom against a given plane. 
     400    x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
     401};
     402
     403/** Mirrors atom against a given plane.
    403404 * \param n[] normal vector of mirror plane.
    404405 */
     
    416417  Output((ofstream *)&cout);
    417418  cout << endl;
    418 }; 
     419};
    419420
    420421/** Calculates normal vector for three given vectors (being three points in space).
     
    448449  this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
    449450  Normalize();
    450  
     451
    451452  return true;
    452453};
     
    506507/** Creates this vector as one of the possible orthonormal ones to the given one.
    507508 * Just scan how many components of given *vector are unequal to zero and
    508  * try to get the skp of both to be zero accordingly. 
     509 * try to get the skp of both to be zero accordingly.
    509510 * \param *vector given vector
    510511 * \return true - success, false - failure (null vector given)
     
    527528      Components[Last++] = j;
    528529  cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
    529        
     530
    530531  switch(Last) {
    531532    case 3:  // threecomponent system
     
    540541    case 1: // one component system
    541542      // set sole non-zero component to 0, and one of the other zero component pendants to 1
    542       x[(Components[0]+2)%NDIM] = 0.;   
    543       x[(Components[0]+1)%NDIM] = 1.;   
    544       x[Components[0]] = 0.;   
     543      x[(Components[0]+2)%NDIM] = 0.;
     544      x[(Components[0]+1)%NDIM] = 1.;
     545      x[Components[0]] = 0.;
    545546      return true;
    546547      break;
     
    559560{
    560561//  cout << Verbose(3) << "For comparison: ";
    561 //  cout << "A " << A->Projection(this) << "\t"; 
    562 //  cout << "B " << B->Projection(this) << "\t"; 
    563 //  cout << "C " << C->Projection(this) << "\t"; 
     562//  cout << "A " << A->Projection(this) << "\t";
     563//  cout << "B " << B->Projection(this) << "\t";
     564//  cout << "C " << C->Projection(this) << "\t";
    564565//  cout << endl;
    565566  return A->Projection(this);
     
    571572 * \return true if success, false if failed due to linear dependency
    572573 */
    573 bool Vector::LSQdistance(Vector **vectors, int num) 
     574bool Vector::LSQdistance(Vector **vectors, int num)
    574575{
    575576        int j;
    576                                
     577
    577578        for (j=0;j<num;j++) {
    578579                cout << Verbose(1) << j << "th atom's vector: ";
     
    583584  int np = 3;
    584585        struct LSQ_params par;
    585    
     586
    586587   const gsl_multimin_fminimizer_type *T =
    587588     gsl_multimin_fminimizer_nmsimplex;
     
    589590   gsl_vector *ss, *y;
    590591   gsl_multimin_function minex_func;
    591  
     592
    592593   size_t iter = 0, i;
    593594   int status;
    594595   double size;
    595  
     596
    596597   /* Initial vertex size vector */
    597598   ss = gsl_vector_alloc (np);
    598599   y = gsl_vector_alloc (np);
    599  
     600
    600601   /* Set all step sizes to 1 */
    601602   gsl_vector_set_all (ss, 1.0);
    602  
     603
    603604   /* Starting point */
    604605   par.vectors = vectors;
    605606         par.num = num;
    606        
     607
    607608         for (i=NDIM;i--;)
    608                 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.); 
    609          
     609                gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
     610
    610611   /* Initialize method and iterate */
    611612   minex_func.f = &LSQ;
    612613   minex_func.n = np;
    613614   minex_func.params = (void *)&par;
    614  
     615
    615616   s = gsl_multimin_fminimizer_alloc (T, np);
    616617   gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
    617  
     618
    618619   do
    619620     {
    620621       iter++;
    621622       status = gsl_multimin_fminimizer_iterate(s);
    622  
     623
    623624       if (status)
    624625         break;
    625  
     626
    626627       size = gsl_multimin_fminimizer_size (s);
    627628       status = gsl_multimin_test_size (size, 1e-2);
    628  
     629
    629630       if (status == GSL_SUCCESS)
    630631         {
    631632           printf ("converged to minimum at\n");
    632633         }
    633  
     634
    634635       printf ("%5d ", (int)iter);
    635636       for (i = 0; i < (size_t)np; i++)
     
    640641     }
    641642   while (status == GSL_CONTINUE && iter < 100);
    642  
     643
    643644  for (i=(size_t)np;i--;)
    644645    this->x[i] = gsl_vector_get(s->x, i);
     
    706707 * \param alpha first angle
    707708 * \param beta second angle
    708  * \param c norm of final vector 
     709 * \param c norm of final vector
    709710 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
    710  * \bug this is not yet working properly 
     711 * \bug this is not yet working properly
    711712 */
    712713bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
     
    724725  if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
    725726    if (fabs(x1->x[1]) > MYEPSILON) {
    726       flag = 1;   
     727      flag = 1;
    727728    } else if (fabs(x1->x[2]) > MYEPSILON) {
    728729       flag = 2;
     
    757758  // now comes the case system
    758759  D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
    759   D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2]; 
     760  D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
    760761  D3 = y->x[0]/x1->x[0]*A-B1;
    761762  cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
    762763  if (fabs(D1) < MYEPSILON) {
    763     cout << Verbose(2) << "D1 == 0!\n"; 
     764    cout << Verbose(2) << "D1 == 0!\n";
    764765    if (fabs(D2) > MYEPSILON) {
    765       cout << Verbose(3) << "D2 != 0!\n"; 
     766      cout << Verbose(3) << "D2 != 0!\n";
    766767      x[2] = -D3/D2;
    767768      E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
     
    773774      cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
    774775      if (fabs(F1) < MYEPSILON) {
    775         cout << Verbose(4) << "F1 == 0!\n"; 
     776        cout << Verbose(4) << "F1 == 0!\n";
    776777        cout << Verbose(4) << "Gleichungssystem linear\n";
    777         x[1] = F3/(2.*F2); 
     778        x[1] = F3/(2.*F2);
    778779      } else {
    779780        p = F2/F1;
    780781        q = p*p - F3/F1;
    781         cout << Verbose(4) << "p " << p << "\tq " << q << endl; 
     782        cout << Verbose(4) << "p " << p << "\tq " << q << endl;
    782783        if (q < 0) {
    783784          cout << Verbose(4) << "q < 0" << endl;
     
    800801    cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
    801802    if (fabs(F1) < MYEPSILON) {
    802       cout << Verbose(3) << "F1 == 0!\n"; 
     803      cout << Verbose(3) << "F1 == 0!\n";
    803804      cout << Verbose(3) << "Gleichungssystem linear\n";
    804       x[2] = F3/(2.*F2);     
     805      x[2] = F3/(2.*F2);
    805806    } else {
    806807      p = F2/F1;
    807808      q = p*p - F3/F1;
    808       cout << Verbose(3) << "p " << p << "\tq " << q << endl; 
     809      cout << Verbose(3) << "p " << p << "\tq " << q << endl;
    809810      if (q < 0) {
    810811        cout << Verbose(3) << "q < 0" << endl;
     
    850851    }
    851852    cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
    852     // apply sign matrix 
     853    // apply sign matrix
    853854    for (j=NDIM;j--;)
    854855      x[j] *= sign[j];
     
    856857    ang = x2->Angle (this);
    857858    cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
    858     if (fabs(ang - cos(beta)) < MYEPSILON) { 
     859    if (fabs(ang - cos(beta)) < MYEPSILON) {
    859860      break;
    860861    }
Note: See TracChangeset for help on using the changeset viewer.