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