source: src/vector.cpp@ 1ffa21

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 1ffa21 was 03648b, checked in by Christian Neuen <neuen@…>, 17 years ago

In vector a function for calculation of the vector-(cross-)product has been added.
In Boundary a new way for finding the non-convex boundary is implemented.
Currently problem with comparison of the return value of the map::find routine.

  • Property mode set to 100644
File size: 25.0 KB
RevLine 
[14de469]1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
7#include "molecules.hpp"
8
9
10/************************************ Functions for class vector ************************************/
11
12/** Constructor of class vector.
13 */
[e9b8bb]14Vector::Vector() { x[0] = x[1] = x[2] = 0.; };
[14de469]15
[498a9f]16/** Constructor of class vector.
17 */
[e9b8bb]18Vector::Vector(double x1, double x2, double x3) { x[0] = x1; x[1] = x2; x[2] = x3; };
[498a9f]19
[14de469]20/** Desctructor of class vector.
21 */
[e9b8bb]22Vector::~Vector() {};
[14de469]23
24/** Calculates distance between this and another vector.
25 * \param *y array to second vector
26 * \return \f$| x - y |^2\f$
27 */
[e9b8bb]28double Vector::Distance(const Vector *y) const
[14de469]29{
30 double res = 0.;
[7f3b9d]31 for (int i=NDIM;i--;)
[14de469]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 in a periodic cell.
37 * \param *y array to second vector
38 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
39 * \return \f$| x - y |^2\f$
40 */
[e9b8bb]41double Vector::PeriodicDistance(const Vector *y, const double *cell_size) const
[14de469]42{
43 double res = Distance(y), tmp, matrix[NDIM*NDIM];
[e9b8bb]44 Vector Shiftedy, TranslationVector;
[14de469]45 int N[NDIM];
46 matrix[0] = cell_size[0];
47 matrix[1] = cell_size[1];
48 matrix[2] = cell_size[3];
49 matrix[3] = cell_size[1];
50 matrix[4] = cell_size[2];
51 matrix[5] = cell_size[4];
52 matrix[6] = cell_size[3];
53 matrix[7] = cell_size[4];
54 matrix[8] = cell_size[5];
55 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
56 for (N[0]=-1;N[0]<=1;N[0]++)
57 for (N[1]=-1;N[1]<=1;N[1]++)
58 for (N[2]=-1;N[2]<=1;N[2]++) {
59 // create the translation vector
60 TranslationVector.Zero();
[7f3b9d]61 for (int i=NDIM;i--;)
[14de469]62 TranslationVector.x[i] = (double)N[i];
63 TranslationVector.MatrixMultiplication(matrix);
64 // add onto the original vector to compare with
65 Shiftedy.CopyVector(y);
66 Shiftedy.AddVector(&TranslationVector);
67 // get distance and compare with minimum so far
68 tmp = Distance(&Shiftedy);
69 if (tmp < res) res = tmp;
70 }
71 return (res);
72};
73
74/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
75 * \param *out ofstream for debugging messages
76 * Tries to translate a vector into each adjacent neighbouring cell.
77 */
[e9b8bb]78void Vector::KeepPeriodic(ofstream *out, double *matrix)
[14de469]79{
80// int N[NDIM];
81// bool flag = false;
82 //vector Shifted, TranslationVector;
[e9b8bb]83 Vector TestVector;
[db942e]84// *out << Verbose(1) << "Begin of KeepPeriodic." << endl;
85// *out << Verbose(2) << "Vector is: ";
86// Output(out);
87// *out << endl;
[14de469]88 TestVector.CopyVector(this);
89 TestVector.InverseMatrixMultiplication(matrix);
[7f3b9d]90 for(int i=NDIM;i--;) { // correct periodically
[14de469]91 if (TestVector.x[i] < 0) { // get every coefficient into the interval [0,1)
92 TestVector.x[i] += ceil(TestVector.x[i]);
93 } else {
94 TestVector.x[i] -= floor(TestVector.x[i]);
95 }
96 }
97 TestVector.MatrixMultiplication(matrix);
98 CopyVector(&TestVector);
[db942e]99// *out << Verbose(2) << "New corrected vector is: ";
100// Output(out);
101// *out << endl;
102// *out << Verbose(1) << "End of KeepPeriodic." << endl;
[14de469]103};
104
105/** Calculates scalar product between this and another vector.
106 * \param *y array to second vector
107 * \return \f$\langle x, y \rangle\f$
108 */
[e9b8bb]109double Vector::ScalarProduct(const Vector *y) const
[14de469]110{
111 double res = 0.;
[7f3b9d]112 for (int i=NDIM;i--;)
[14de469]113 res += x[i]*y->x[i];
114 return (res);
115};
116
[03648b]117
118/** Calculates VectorProduct between this and another vector.
119 * -# returns the Product in place of vector from which it was initiated
120 * -# ATTENTION: Only three dim.
121 * \param *y array to vector with which to calculate crossproduct
122 * \return \f$ x \times y \f&
123 */
124void Vector::VectorProduct(const Vector *y)
125{
126 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];
130 this->CopyVector(&tmp);
131
132};
133
134
[498a9f]135/** projects this vector onto plane defined by \a *y.
136 * \param *y array to normal vector of plane
137 * \return \f$\langle x, y \rangle\f$
138 */
[e9b8bb]139void Vector::ProjectOntoPlane(const Vector *y)
[498a9f]140{
[e9b8bb]141 Vector tmp;
[498a9f]142 tmp.CopyVector(y);
143 tmp.Scale(Projection(y));
144 this->SubtractVector(&tmp);
145};
146
[14de469]147/** Calculates the projection of a vector onto another \a *y.
148 * \param *y array to second vector
149 * \return \f$\langle x, y \rangle\f$
150 */
[e9b8bb]151double Vector::Projection(const Vector *y) const
[14de469]152{
[498a9f]153 return (ScalarProduct(y));
[14de469]154};
155
156/** Calculates norm of this vector.
157 * \return \f$|x|\f$
158 */
[e9b8bb]159double Vector::Norm() const
[14de469]160{
161 double res = 0.;
[7f3b9d]162 for (int i=NDIM;i--;)
[14de469]163 res += this->x[i]*this->x[i];
164 return (sqrt(res));
165};
166
167/** Normalizes this vector.
168 */
[e9b8bb]169void Vector::Normalize()
[14de469]170{
171 double res = 0.;
[7f3b9d]172 for (int i=NDIM;i--;)
[14de469]173 res += this->x[i]*this->x[i];
[2985c8]174 if (fabs(res) > MYEPSILON)
175 res = 1./sqrt(res);
[14de469]176 Scale(&res);
177};
178
179/** Zeros all components of this vector.
180 */
[e9b8bb]181void Vector::Zero()
[14de469]182{
[7f3b9d]183 for (int i=NDIM;i--;)
[14de469]184 this->x[i] = 0.;
185};
186
[498a9f]187/** Zeros all components of this vector.
188 */
[e9b8bb]189void Vector::One(double one)
[498a9f]190{
191 for (int i=NDIM;i--;)
192 this->x[i] = one;
193};
194
195/** Initialises all components of this vector.
196 */
[e9b8bb]197void Vector::Init(double x1, double x2, double x3)
[498a9f]198{
199 x[0] = x1;
200 x[1] = x2;
201 x[2] = x3;
202};
203
[03648b]204/** Calculates the angle between this and another vector.
[14de469]205 * \param *y array to second vector
[498a9f]206 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
[14de469]207 */
[e9b8bb]208double Vector::Angle(Vector *y) const
[14de469]209{
[498a9f]210 return acos(this->ScalarProduct(y)/Norm()/y->Norm());
[14de469]211};
212
213/** Rotates the vector around the axis given by \a *axis by an angle of \a alpha.
214 * \param *axis rotation axis
215 * \param alpha rotation angle in radian
216 */
[e9b8bb]217void Vector::RotateVector(const Vector *axis, const double alpha)
[14de469]218{
[e9b8bb]219 Vector a,y;
[14de469]220 // normalise this vector with respect to axis
221 a.CopyVector(this);
222 a.Scale(Projection(axis));
223 SubtractVector(&a);
224 // construct normal vector
225 y.MakeNormalVector(axis,this);
226 y.Scale(Norm());
227 // scale normal vector by sine and this vector by cosine
228 y.Scale(sin(alpha));
229 Scale(cos(alpha));
230 // add scaled normal vector onto this vector
231 AddVector(&y);
232 // add part in axis direction
233 AddVector(&a);
234};
235
[342f33f]236/** Sums vector \a to this lhs component-wise.
237 * \param a base vector
238 * \param b vector components to add
239 * \return lhs + a
240 */
[e9b8bb]241Vector& operator+=(Vector& a, const Vector& b)
[342f33f]242{
243 a.AddVector(&b);
244 return a;
245};
246/** factor each component of \a a times a double \a m.
247 * \param a base vector
248 * \param m factor
249 * \return lhs.x[i] * m
250 */
[e9b8bb]251Vector& operator*=(Vector& a, const double m)
[342f33f]252{
253 a.Scale(m);
254 return a;
255};
256
257/** Sums two vectors \a and \b component-wise.
258 * \param a first vector
259 * \param b second vector
260 * \return a + b
261 */
[e9b8bb]262Vector& operator+(const Vector& a, const Vector& b)
[342f33f]263{
[e9b8bb]264 Vector *x = new Vector;
[342f33f]265 x->CopyVector(&a);
266 x->AddVector(&b);
267 return *x;
268};
269
270/** Factors given vector \a a times \a m.
271 * \param a vector
272 * \param m factor
273 * \return a + b
274 */
[e9b8bb]275Vector& operator*(const Vector& a, const double m)
[342f33f]276{
[e9b8bb]277 Vector *x = new Vector;
[342f33f]278 x->CopyVector(&a);
279 x->Scale(m);
280 return *x;
281};
282
[14de469]283/** Prints a 3dim vector.
284 * prints no end of line.
285 * \param *out output stream
286 */
[e9b8bb]287bool Vector::Output(ofstream *out) const
[14de469]288{
289 if (out != NULL) {
290 *out << "(";
291 for (int i=0;i<NDIM;i++) {
292 *out << x[i];
293 if (i != 2)
294 *out << ",";
295 }
296 *out << ")";
297 return true;
298 } else
299 return false;
300};
301
[e9b8bb]302ofstream& operator<<(ofstream& ost,Vector& m)
[14de469]303{
304 m.Output(&ost);
305 return ost;
306};
307
308/** Scales each atom coordinate by an individual \a factor.
309 * \param *factor pointer to scaling factor
310 */
[e9b8bb]311void Vector::Scale(double **factor)
[14de469]312{
[7f3b9d]313 for (int i=NDIM;i--;)
[342f33f]314 x[i] *= (*factor)[i];
[14de469]315};
316
[e9b8bb]317void Vector::Scale(double *factor)
[14de469]318{
[7f3b9d]319 for (int i=NDIM;i--;)
[342f33f]320 x[i] *= *factor;
[14de469]321};
322
[e9b8bb]323void Vector::Scale(double factor)
[14de469]324{
[7f3b9d]325 for (int i=NDIM;i--;)
[342f33f]326 x[i] *= factor;
[14de469]327};
328
329/** Translate atom by given vector.
330 * \param trans[] translation vector.
331 */
[e9b8bb]332void Vector::Translate(const Vector *trans)
[14de469]333{
[7f3b9d]334 for (int i=NDIM;i--;)
[14de469]335 x[i] += trans->x[i];
336};
337
338/** Do a matrix multiplication.
339 * \param *matrix NDIM_NDIM array
340 */
[e9b8bb]341void Vector::MatrixMultiplication(double *M)
[14de469]342{
[e9b8bb]343 Vector C;
[14de469]344 // do the matrix multiplication
345 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
346 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
347 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
348 // transfer the result into this
[7f3b9d]349 for (int i=NDIM;i--;)
[14de469]350 x[i] = C.x[i];
351};
352
353/** Do a matrix multiplication with \a *matrix' inverse.
354 * \param *matrix NDIM_NDIM array
355 */
[e9b8bb]356void Vector::InverseMatrixMultiplication(double *A)
[14de469]357{
[e9b8bb]358 Vector C;
[14de469]359 double B[NDIM*NDIM];
360 double detA = RDET3(A);
361 double detAReci;
362
363 // calculate the inverse B
364 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
365 detAReci = 1./detA;
366 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
367 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
368 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
369 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
370 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
371 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
372 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
373 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
374 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
375
376 // do the matrix multiplication
377 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
378 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
379 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
380 // transfer the result into this
[7f3b9d]381 for (int i=NDIM;i--;)
[14de469]382 x[i] = C.x[i];
383 } else {
384 cerr << "ERROR: inverse of matrix does not exists!" << endl;
385 }
386};
387
388
389/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
390 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
391 * \param *x1 first vector
392 * \param *x2 second vector
393 * \param *x3 third vector
394 * \param *factors three-component vector with the factor for each given vector
395 */
[e9b8bb]396void Vector::LinearCombinationOfVectors(const Vector *x1, const Vector *x2, const Vector *x3, double *factors)
[14de469]397{
[7f3b9d]398 for(int i=NDIM;i--;)
[14de469]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.
403 * \param n[] normal vector of mirror plane.
404 */
[e9b8bb]405void Vector::Mirror(const Vector *n)
[14de469]406{
407 double projection;
[65684f]408 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
[14de469]409 // withdraw projected vector twice from original one
410 cout << Verbose(1) << "Vector: ";
411 Output((ofstream *)&cout);
412 cout << "\t";
[7f3b9d]413 for (int i=NDIM;i--;)
[14de469]414 x[i] -= 2.*projection*n->x[i];
415 cout << "Projected vector: ";
416 Output((ofstream *)&cout);
417 cout << endl;
418};
419
420/** Calculates normal vector for three given vectors (being three points in space).
421 * Makes this vector orthonormal to the three given points, making up a place in 3d space.
422 * \param *y1 first vector
423 * \param *y2 second vector
424 * \param *y3 third vector
425 * \return true - success, vectors are linear independent, false - failure due to linear dependency
426 */
[e9b8bb]427bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2, const Vector *y3)
[14de469]428{
[e9b8bb]429 Vector x1, x2;
[14de469]430
431 x1.CopyVector(y1);
432 x1.SubtractVector(y2);
433 x2.CopyVector(y3);
434 x2.SubtractVector(y2);
435 if ((x1.Norm()==0) || (x2.Norm()==0)) {
436 cout << Verbose(4) << "Given vectors are linear dependent." << endl;
437 return false;
438 }
[110ceb]439// cout << Verbose(4) << "relative, first plane coordinates:";
440// x1.Output((ofstream *)&cout);
441// cout << endl;
442// cout << Verbose(4) << "second plane coordinates:";
443// x2.Output((ofstream *)&cout);
444// cout << endl;
[14de469]445
446 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
447 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
448 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
449 Normalize();
450
451 return true;
452};
453
454
455/** Calculates orthonormal vector to two given vectors.
456 * Makes this vector orthonormal to two given vectors. This is very similar to the other
457 * vector::MakeNormalVector(), only there three points whereas here two difference
458 * vectors are given.
459 * \param *x1 first vector
460 * \param *x2 second vector
461 * \return true - success, vectors are linear independent, false - failure due to linear dependency
462 */
[e9b8bb]463bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2)
[14de469]464{
[e9b8bb]465 Vector x1,x2;
[14de469]466 x1.CopyVector(y1);
467 x2.CopyVector(y2);
468 Zero();
469 if ((x1.Norm()==0) || (x2.Norm()==0)) {
470 cout << Verbose(4) << "Given vectors are linear dependent." << endl;
471 return false;
472 }
[110ceb]473// cout << Verbose(4) << "relative, first plane coordinates:";
474// x1.Output((ofstream *)&cout);
475// cout << endl;
476// cout << Verbose(4) << "second plane coordinates:";
477// x2.Output((ofstream *)&cout);
478// cout << endl;
[14de469]479
480 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
481 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
482 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
483 Normalize();
484
485 return true;
486};
487
488/** Calculates orthonormal vector to one given vectors.
489 * Just subtracts the projection onto the given vector from this vector.
490 * \param *x1 vector
491 * \return true - success, false - vector is zero
492 */
[e9b8bb]493bool Vector::MakeNormalVector(const Vector *y1)
[14de469]494{
495 bool result = false;
[e9b8bb]496 Vector x1;
[14de469]497 x1.CopyVector(y1);
498 x1.Scale(x1.Projection(this));
499 SubtractVector(&x1);
[7f3b9d]500 for (int i=NDIM;i--;)
[14de469]501 result = result || (fabs(x[i]) > MYEPSILON);
502
503 return result;
504};
505
506/** Creates this vector as one of the possible orthonormal ones to the given one.
507 * 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 * \param *vector given vector
510 * \return true - success, false - failure (null vector given)
511 */
[e9b8bb]512bool Vector::GetOneNormalVector(const Vector *GivenVector)
[14de469]513{
514 int Components[NDIM]; // contains indices of non-zero components
515 int Last = 0; // count the number of non-zero entries in vector
516 int j; // loop variables
517 double norm;
518
519 cout << Verbose(4);
[65684f]520 GivenVector->Output((ofstream *)&cout);
[14de469]521 cout << endl;
[7f3b9d]522 for (j=NDIM;j--;)
[14de469]523 Components[j] = -1;
524 // find two components != 0
525 for (j=0;j<NDIM;j++)
[65684f]526 if (fabs(GivenVector->x[j]) > MYEPSILON)
[14de469]527 Components[Last++] = j;
528 cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
529
530 switch(Last) {
531 case 3: // threecomponent system
532 case 2: // two component system
[65684f]533 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
[14de469]534 x[Components[2]] = 0.;
535 // in skp both remaining parts shall become zero but with opposite sign and third is zero
[65684f]536 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
537 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
[14de469]538 return true;
539 break;
540 case 1: // one component system
541 // 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.;
545 return true;
546 break;
547 default:
548 return false;
549 }
550};
551
[110ceb]552/** Determines paramter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
553 * \param *A first plane vector
554 * \param *B second plane vector
555 * \param *C third plane vector
556 * \return scaling parameter for this vector
557 */
[e9b8bb]558double Vector::CutsPlaneAt(Vector *A, Vector *B, Vector *C)
[110ceb]559{
560// 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";
564// cout << endl;
565 return A->Projection(this);
566};
567
[14de469]568/** Creates a new vector as the one with least square distance to a given set of \a vectors.
569 * \param *vectors set of vectors
570 * \param num number of vectors
571 * \return true if success, false if failed due to linear dependency
572 */
[e9b8bb]573bool Vector::LSQdistance(Vector **vectors, int num)
[14de469]574{
575 int j;
576
577 for (j=0;j<num;j++) {
578 cout << Verbose(1) << j << "th atom's vector: ";
579 (vectors[j])->Output((ofstream *)&cout);
580 cout << endl;
581 }
582
583 int np = 3;
584 struct LSQ_params par;
585
586 const gsl_multimin_fminimizer_type *T =
587 gsl_multimin_fminimizer_nmsimplex;
588 gsl_multimin_fminimizer *s = NULL;
[65684f]589 gsl_vector *ss, *y;
[14de469]590 gsl_multimin_function minex_func;
591
592 size_t iter = 0, i;
593 int status;
594 double size;
595
596 /* Initial vertex size vector */
597 ss = gsl_vector_alloc (np);
[65684f]598 y = gsl_vector_alloc (np);
[14de469]599
600 /* Set all step sizes to 1 */
601 gsl_vector_set_all (ss, 1.0);
602
603 /* Starting point */
604 par.vectors = vectors;
605 par.num = num;
606
[7f3b9d]607 for (i=NDIM;i--;)
[65684f]608 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
[14de469]609
610 /* Initialize method and iterate */
611 minex_func.f = &LSQ;
612 minex_func.n = np;
613 minex_func.params = (void *)&par;
614
615 s = gsl_multimin_fminimizer_alloc (T, np);
[65684f]616 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
[14de469]617
618 do
619 {
620 iter++;
621 status = gsl_multimin_fminimizer_iterate(s);
622
623 if (status)
624 break;
625
626 size = gsl_multimin_fminimizer_size (s);
627 status = gsl_multimin_test_size (size, 1e-2);
628
629 if (status == GSL_SUCCESS)
630 {
631 printf ("converged to minimum at\n");
632 }
633
634 printf ("%5d ", (int)iter);
635 for (i = 0; i < (size_t)np; i++)
636 {
637 printf ("%10.3e ", gsl_vector_get (s->x, i));
638 }
639 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
640 }
641 while (status == GSL_CONTINUE && iter < 100);
642
[7f3b9d]643 for (i=(size_t)np;i--;)
[14de469]644 this->x[i] = gsl_vector_get(s->x, i);
[65684f]645 gsl_vector_free(y);
[14de469]646 gsl_vector_free(ss);
647 gsl_multimin_fminimizer_free (s);
648
649 return true;
650};
651
652/** Adds vector \a *y componentwise.
653 * \param *y vector
654 */
[e9b8bb]655void Vector::AddVector(const Vector *y)
[14de469]656{
[7f3b9d]657 for (int i=NDIM;i--;)
[14de469]658 this->x[i] += y->x[i];
659}
660
661/** Adds vector \a *y componentwise.
662 * \param *y vector
663 */
[e9b8bb]664void Vector::SubtractVector(const Vector *y)
[14de469]665{
[7f3b9d]666 for (int i=NDIM;i--;)
[14de469]667 this->x[i] -= y->x[i];
668}
669
670/** Copy vector \a *y componentwise.
671 * \param *y vector
672 */
[e9b8bb]673void Vector::CopyVector(const Vector *y)
[14de469]674{
[7f3b9d]675 for (int i=NDIM;i--;)
[14de469]676 this->x[i] = y->x[i];
677}
678
679
680/** Asks for position, checks for boundary.
681 * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
682 * \param check whether bounds shall be checked (true) or not (false)
683 */
[e9b8bb]684void Vector::AskPosition(double *cell_size, bool check)
[14de469]685{
686 char coords[3] = {'x','y','z'};
687 int j = -1;
688 for (int i=0;i<3;i++) {
689 j += i+1;
690 do {
691 cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
692 cin >> x[i];
693 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
694 }
695};
696
697/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
698 * This is linear system of equations to be solved, however of the three given (skp of this vector\
699 * with either of the three hast to be zero) only two are linear independent. The third equation
700 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
701 * where very often it has to be checked whether a certain value is zero or not and thus forked into
702 * another case.
703 * \param *x1 first vector
704 * \param *x2 second vector
705 * \param *y third vector
706 * \param alpha first angle
707 * \param beta second angle
708 * \param c norm of final vector
709 * \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 */
[e9b8bb]712bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
[14de469]713{
714 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
715 double ang; // angle on testing
716 double sign[3];
717 int i,j,k;
718 A = cos(alpha) * x1->Norm() * c;
719 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
720 B2 = cos(beta) * x2->Norm() * c;
721 C = c * c;
722 cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
723 int flag = 0;
724 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
725 if (fabs(x1->x[1]) > MYEPSILON) {
726 flag = 1;
727 } else if (fabs(x1->x[2]) > MYEPSILON) {
728 flag = 2;
729 } else {
730 return false;
731 }
732 }
733 switch (flag) {
734 default:
735 case 0:
736 break;
737 case 2:
738 flip(&x1->x[0],&x1->x[1]);
739 flip(&x2->x[0],&x2->x[1]);
740 flip(&y->x[0],&y->x[1]);
741 //flip(&x[0],&x[1]);
742 flip(&x1->x[1],&x1->x[2]);
743 flip(&x2->x[1],&x2->x[2]);
744 flip(&y->x[1],&y->x[2]);
745 //flip(&x[1],&x[2]);
746 case 1:
747 flip(&x1->x[0],&x1->x[1]);
748 flip(&x2->x[0],&x2->x[1]);
749 flip(&y->x[0],&y->x[1]);
750 //flip(&x[0],&x[1]);
751 flip(&x1->x[1],&x1->x[2]);
752 flip(&x2->x[1],&x2->x[2]);
753 flip(&y->x[1],&y->x[2]);
754 //flip(&x[1],&x[2]);
755 break;
756 }
757 // now comes the case system
758 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 D3 = y->x[0]/x1->x[0]*A-B1;
761 cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
762 if (fabs(D1) < MYEPSILON) {
763 cout << Verbose(2) << "D1 == 0!\n";
764 if (fabs(D2) > MYEPSILON) {
765 cout << Verbose(3) << "D2 != 0!\n";
766 x[2] = -D3/D2;
767 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
768 E2 = -x1->x[1]/x1->x[0];
769 cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
770 F1 = E1*E1 + 1.;
771 F2 = -E1*E2;
772 F3 = E1*E1 + D3*D3/(D2*D2) - C;
773 cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
774 if (fabs(F1) < MYEPSILON) {
775 cout << Verbose(4) << "F1 == 0!\n";
776 cout << Verbose(4) << "Gleichungssystem linear\n";
777 x[1] = F3/(2.*F2);
778 } else {
779 p = F2/F1;
780 q = p*p - F3/F1;
781 cout << Verbose(4) << "p " << p << "\tq " << q << endl;
782 if (q < 0) {
783 cout << Verbose(4) << "q < 0" << endl;
784 return false;
785 }
786 x[1] = p + sqrt(q);
787 }
788 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
789 } else {
790 cout << Verbose(2) << "Gleichungssystem unterbestimmt\n";
791 return false;
792 }
793 } else {
794 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
795 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
796 cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
797 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
798 F2 = -(E1*E2 + D2*D3/(D1*D1));
799 F3 = E1*E1 + D3*D3/(D1*D1) - C;
800 cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
801 if (fabs(F1) < MYEPSILON) {
802 cout << Verbose(3) << "F1 == 0!\n";
803 cout << Verbose(3) << "Gleichungssystem linear\n";
804 x[2] = F3/(2.*F2);
805 } else {
806 p = F2/F1;
807 q = p*p - F3/F1;
808 cout << Verbose(3) << "p " << p << "\tq " << q << endl;
809 if (q < 0) {
810 cout << Verbose(3) << "q < 0" << endl;
811 return false;
812 }
813 x[2] = p + sqrt(q);
814 }
815 x[1] = (-D2 * x[2] - D3)/D1;
816 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
817 }
818 switch (flag) { // back-flipping
819 default:
820 case 0:
821 break;
822 case 2:
823 flip(&x1->x[0],&x1->x[1]);
824 flip(&x2->x[0],&x2->x[1]);
825 flip(&y->x[0],&y->x[1]);
826 flip(&x[0],&x[1]);
827 flip(&x1->x[1],&x1->x[2]);
828 flip(&x2->x[1],&x2->x[2]);
829 flip(&y->x[1],&y->x[2]);
830 flip(&x[1],&x[2]);
831 case 1:
832 flip(&x1->x[0],&x1->x[1]);
833 flip(&x2->x[0],&x2->x[1]);
834 flip(&y->x[0],&y->x[1]);
835 //flip(&x[0],&x[1]);
836 flip(&x1->x[1],&x1->x[2]);
837 flip(&x2->x[1],&x2->x[2]);
838 flip(&y->x[1],&y->x[2]);
839 flip(&x[1],&x[2]);
840 break;
841 }
842 // one z component is only determined by its radius (without sign)
843 // thus check eight possible sign flips and determine by checking angle with second vector
844 for (i=0;i<8;i++) {
845 // set sign vector accordingly
846 for (j=2;j>=0;j--) {
847 k = (i & pot(2,j)) << j;
848 cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
849 sign[j] = (k == 0) ? 1. : -1.;
850 }
851 cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
852 // apply sign matrix
[7f3b9d]853 for (j=NDIM;j--;)
[14de469]854 x[j] *= sign[j];
855 // calculate angle and check
856 ang = x2->Angle (this);
857 cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
858 if (fabs(ang - cos(beta)) < MYEPSILON) {
859 break;
860 }
861 // unapply sign matrix (is its own inverse)
[7f3b9d]862 for (j=NDIM;j--;)
[14de469]863 x[j] *= sign[j];
864 }
865 return true;
866};
Note: See TracBrowser for help on using the repository browser.