Changes in src/vector.cpp [cc2ee5:2985c8]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/vector.cpp
-
Property mode
changed from
100755
to100644
rcc2ee5 r2985c8 1 1 /** \file vector.cpp 2 * 2 * 3 3 * Function implementations for the class vector. 4 * 5 */ 6 4 * 5 */ 6 7 7 #include "molecules.hpp" 8 8 9 9 10 10 /************************************ Functions for class vector ************************************/ … … 22 22 Vector::~Vector() {}; 23 23 24 /** Calculates square ofdistance between this and another vector.24 /** Calculates distance between this and another vector. 25 25 * \param *y array to second vector 26 26 * \return \f$| x - y |^2\f$ 27 27 */ 28 double Vector::Distance Squared(const Vector *y) const28 double Vector::Distance(const Vector *y) const 29 29 { 30 30 double res = 0.; 31 31 for (int i=NDIM;i--;) 32 32 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); 46 34 }; 47 35 … … 81 69 if (tmp < res) res = tmp; 82 70 } 83 return (res); 71 return (res); 84 72 }; 85 73 … … 124 112 for (int i=NDIM;i--;) 125 113 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 }; 146 116 147 117 /** projects this vector onto plane defined by \a *y. 148 * \param *y normal vector of plane118 * \param *y array to normal vector of plane 149 119 * \return \f$\langle x, y \rangle\f$ 150 120 */ … … 153 123 Vector tmp; 154 124 tmp.CopyVector(y); 155 tmp.Normalize(); 156 tmp.Scale(ScalarProduct(&tmp)); 125 tmp.Scale(Projection(y)); 157 126 this->SubtractVector(&tmp); 158 127 }; … … 175 144 for (int i=NDIM;i--;) 176 145 res += this->x[i]*this->x[i]; 177 return (sqrt(res)); 146 return (sqrt(res)); 178 147 }; 179 148 … … 209 178 */ 210 179 void Vector::Init(double x1, double x2, double x3) 211 { 180 { 212 181 x[0] = x1; 213 182 x[1] = x2; … … 219 188 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$ 220 189 */ 221 double Vector::Angle( constVector *y) const222 { 223 return acos(this->ScalarProduct(y)/Norm()/y->Norm()); 190 double Vector::Angle(Vector *y) const 191 { 192 return acos(this->ScalarProduct(y)/Norm()/y->Norm()); 224 193 }; 225 194 … … 269 238 270 239 /** Sums two vectors \a and \b component-wise. 271 * \param a first vector 240 * \param a first vector 272 241 * \param b second vector 273 242 * \return a + b … … 282 251 283 252 /** Factors given vector \a a times \a m. 284 * \param a vector 253 * \param a vector 285 254 * \param m factor 286 255 * \return a + b … … 313 282 }; 314 283 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 << ")"; 284 ofstream& operator<<(ofstream& ost,Vector& m) 285 { 286 m.Output(&ost); 329 287 return ost; 330 288 }; … … 351 309 }; 352 310 353 /** Translate atom by given vector. 311 /** Translate atom by given vector. 354 312 * \param trans[] translation vector. 355 313 */ … … 358 316 for (int i=NDIM;i--;) 359 317 x[i] += trans->x[i]; 360 }; 318 }; 361 319 362 320 /** Do a matrix multiplication. … … 397 355 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32 398 356 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33 399 357 400 358 // do the matrix multiplication 401 359 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2]; … … 421 379 { 422 380 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. 427 385 * \param n[] normal vector of mirror plane. 428 386 */ … … 440 398 Output((ofstream *)&cout); 441 399 cout << endl; 442 }; 400 }; 443 401 444 402 /** Calculates normal vector for three given vectors (being three points in space). … … 472 430 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]); 473 431 Normalize(); 474 432 475 433 return true; 476 434 }; … … 530 488 /** Creates this vector as one of the possible orthonormal ones to the given one. 531 489 * 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. 533 491 * \param *vector given vector 534 492 * \return true - success, false - failure (null vector given) … … 551 509 Components[Last++] = j; 552 510 cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl; 553 511 554 512 switch(Last) { 555 513 case 3: // threecomponent system … … 564 522 case 1: // one component system 565 523 // 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.; 569 527 return true; 570 528 break; … … 583 541 { 584 542 // 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"; 588 546 // cout << endl; 589 547 return A->Projection(this); … … 595 553 * \return true if success, false if failed due to linear dependency 596 554 */ 597 bool Vector::LSQdistance(Vector **vectors, int num) 555 bool Vector::LSQdistance(Vector **vectors, int num) 598 556 { 599 557 int j; 600 558 601 559 for (j=0;j<num;j++) { 602 560 cout << Verbose(1) << j << "th atom's vector: "; … … 607 565 int np = 3; 608 566 struct LSQ_params par; 609 567 610 568 const gsl_multimin_fminimizer_type *T = 611 569 gsl_multimin_fminimizer_nmsimplex; … … 613 571 gsl_vector *ss, *y; 614 572 gsl_multimin_function minex_func; 615 573 616 574 size_t iter = 0, i; 617 575 int status; 618 576 double size; 619 577 620 578 /* Initial vertex size vector */ 621 579 ss = gsl_vector_alloc (np); 622 580 y = gsl_vector_alloc (np); 623 581 624 582 /* Set all step sizes to 1 */ 625 583 gsl_vector_set_all (ss, 1.0); 626 584 627 585 /* Starting point */ 628 586 par.vectors = vectors; 629 587 par.num = num; 630 588 631 589 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 634 592 /* Initialize method and iterate */ 635 593 minex_func.f = &LSQ; 636 594 minex_func.n = np; 637 595 minex_func.params = (void *)∥ 638 596 639 597 s = gsl_multimin_fminimizer_alloc (T, np); 640 598 gsl_multimin_fminimizer_set (s, &minex_func, y, ss); 641 599 642 600 do 643 601 { 644 602 iter++; 645 603 status = gsl_multimin_fminimizer_iterate(s); 646 604 647 605 if (status) 648 606 break; 649 607 650 608 size = gsl_multimin_fminimizer_size (s); 651 609 status = gsl_multimin_test_size (size, 1e-2); 652 610 653 611 if (status == GSL_SUCCESS) 654 612 { 655 613 printf ("converged to minimum at\n"); 656 614 } 657 615 658 616 printf ("%5d ", (int)iter); 659 617 for (i = 0; i < (size_t)np; i++) … … 664 622 } 665 623 while (status == GSL_CONTINUE && iter < 100); 666 624 667 625 for (i=(size_t)np;i--;) 668 626 this->x[i] = gsl_vector_get(s->x, i); … … 730 688 * \param alpha first angle 731 689 * \param beta second angle 732 * \param c norm of final vector 690 * \param c norm of final vector 733 691 * \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 735 693 */ 736 694 bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c) … … 748 706 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping 749 707 if (fabs(x1->x[1]) > MYEPSILON) { 750 flag = 1; 708 flag = 1; 751 709 } else if (fabs(x1->x[2]) > MYEPSILON) { 752 710 flag = 2; … … 781 739 // now comes the case system 782 740 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]; 784 742 D3 = y->x[0]/x1->x[0]*A-B1; 785 743 cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n"; 786 744 if (fabs(D1) < MYEPSILON) { 787 cout << Verbose(2) << "D1 == 0!\n"; 745 cout << Verbose(2) << "D1 == 0!\n"; 788 746 if (fabs(D2) > MYEPSILON) { 789 cout << Verbose(3) << "D2 != 0!\n"; 747 cout << Verbose(3) << "D2 != 0!\n"; 790 748 x[2] = -D3/D2; 791 749 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2; … … 797 755 cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"; 798 756 if (fabs(F1) < MYEPSILON) { 799 cout << Verbose(4) << "F1 == 0!\n"; 757 cout << Verbose(4) << "F1 == 0!\n"; 800 758 cout << Verbose(4) << "Gleichungssystem linear\n"; 801 x[1] = F3/(2.*F2); 759 x[1] = F3/(2.*F2); 802 760 } else { 803 761 p = F2/F1; 804 762 q = p*p - F3/F1; 805 cout << Verbose(4) << "p " << p << "\tq " << q << endl; 763 cout << Verbose(4) << "p " << p << "\tq " << q << endl; 806 764 if (q < 0) { 807 765 cout << Verbose(4) << "q < 0" << endl; … … 824 782 cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n"; 825 783 if (fabs(F1) < MYEPSILON) { 826 cout << Verbose(3) << "F1 == 0!\n"; 784 cout << Verbose(3) << "F1 == 0!\n"; 827 785 cout << Verbose(3) << "Gleichungssystem linear\n"; 828 x[2] = F3/(2.*F2); 786 x[2] = F3/(2.*F2); 829 787 } else { 830 788 p = F2/F1; 831 789 q = p*p - F3/F1; 832 cout << Verbose(3) << "p " << p << "\tq " << q << endl; 790 cout << Verbose(3) << "p " << p << "\tq " << q << endl; 833 791 if (q < 0) { 834 792 cout << Verbose(3) << "q < 0" << endl; … … 874 832 } 875 833 cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n"; 876 // apply sign matrix 834 // apply sign matrix 877 835 for (j=NDIM;j--;) 878 836 x[j] *= sign[j]; … … 880 838 ang = x2->Angle (this); 881 839 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) { 883 841 break; 884 842 } -
Property mode
changed from
Note:
See TracChangeset
for help on using the changeset viewer.