source: src/vector.cpp@ 12298c

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 12298c was 44fd95, checked in by Frederik Heber <heber@…>, 17 years ago

Cleaned up all debugging output

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