source: src/vector.cpp@ 70ff32

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 70ff32 was b453f9, checked in by Frederik Heber <heber@…>, 15 years ago

Begun with ticket #38 (make const what is const).

  • basically all changes to member function that now state that they do not change member attributes.
  • in molecule_template.hpp all member functions are declared const, as we only need start and end from molecule and these are never changed (lots of overloaded templates removed thereby).
  • Vector::Distance...() and ...DistanceSquared() are const now too
  • Property mode set to 100644
File size: 38.2 KB
Line 
1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
7
8#include "defs.hpp"
9#include "helpers.hpp"
10#include "memoryallocator.hpp"
11#include "leastsquaremin.hpp"
12#include "vector.hpp"
13#include "verbose.hpp"
14
15/************************************ Functions for class vector ************************************/
16
17/** Constructor of class vector.
18 */
19Vector::Vector() { x[0] = x[1] = x[2] = 0.; };
20
21/** Constructor of class vector.
22 */
23Vector::Vector(double x1, double x2, double x3) { x[0] = x1; x[1] = x2; x[2] = x3; };
24
25/** Desctructor of class vector.
26 */
27Vector::~Vector() {};
28
29/** Calculates square of distance between this and another vector.
30 * \param *y array to second vector
31 * \return \f$| x - y |^2\f$
32 */
33double Vector::DistanceSquared(const Vector *y) const
34{
35 double res = 0.;
36 for (int i=NDIM;i--;)
37 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
38 return (res);
39};
40
41/** Calculates distance between this and another vector.
42 * \param *y array to second vector
43 * \return \f$| x - y |\f$
44 */
45double Vector::Distance(const Vector *y) const
46{
47 double res = 0.;
48 for (int i=NDIM;i--;)
49 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
50 return (sqrt(res));
51};
52
53/** Calculates distance between this and another vector in a periodic cell.
54 * \param *y array to second vector
55 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
56 * \return \f$| x - y |\f$
57 */
58double Vector::PeriodicDistance(const Vector *y, const double *cell_size) const
59{
60 double res = Distance(y), tmp, matrix[NDIM*NDIM];
61 Vector Shiftedy, TranslationVector;
62 int N[NDIM];
63 matrix[0] = cell_size[0];
64 matrix[1] = cell_size[1];
65 matrix[2] = cell_size[3];
66 matrix[3] = cell_size[1];
67 matrix[4] = cell_size[2];
68 matrix[5] = cell_size[4];
69 matrix[6] = cell_size[3];
70 matrix[7] = cell_size[4];
71 matrix[8] = cell_size[5];
72 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
73 for (N[0]=-1;N[0]<=1;N[0]++)
74 for (N[1]=-1;N[1]<=1;N[1]++)
75 for (N[2]=-1;N[2]<=1;N[2]++) {
76 // create the translation vector
77 TranslationVector.Zero();
78 for (int i=NDIM;i--;)
79 TranslationVector.x[i] = (double)N[i];
80 TranslationVector.MatrixMultiplication(matrix);
81 // add onto the original vector to compare with
82 Shiftedy.CopyVector(y);
83 Shiftedy.AddVector(&TranslationVector);
84 // get distance and compare with minimum so far
85 tmp = Distance(&Shiftedy);
86 if (tmp < res) res = tmp;
87 }
88 return (res);
89};
90
91/** Calculates distance between this and another vector in a periodic cell.
92 * \param *y array to second vector
93 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
94 * \return \f$| x - y |^2\f$
95 */
96double Vector::PeriodicDistanceSquared(const Vector *y, const double *cell_size) const
97{
98 double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
99 Vector Shiftedy, TranslationVector;
100 int N[NDIM];
101 matrix[0] = cell_size[0];
102 matrix[1] = cell_size[1];
103 matrix[2] = cell_size[3];
104 matrix[3] = cell_size[1];
105 matrix[4] = cell_size[2];
106 matrix[5] = cell_size[4];
107 matrix[6] = cell_size[3];
108 matrix[7] = cell_size[4];
109 matrix[8] = cell_size[5];
110 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
111 for (N[0]=-1;N[0]<=1;N[0]++)
112 for (N[1]=-1;N[1]<=1;N[1]++)
113 for (N[2]=-1;N[2]<=1;N[2]++) {
114 // create the translation vector
115 TranslationVector.Zero();
116 for (int i=NDIM;i--;)
117 TranslationVector.x[i] = (double)N[i];
118 TranslationVector.MatrixMultiplication(matrix);
119 // add onto the original vector to compare with
120 Shiftedy.CopyVector(y);
121 Shiftedy.AddVector(&TranslationVector);
122 // get distance and compare with minimum so far
123 tmp = DistanceSquared(&Shiftedy);
124 if (tmp < res) res = tmp;
125 }
126 return (res);
127};
128
129/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
130 * \param *out ofstream for debugging messages
131 * Tries to translate a vector into each adjacent neighbouring cell.
132 */
133void Vector::KeepPeriodic(ofstream *out, double *matrix)
134{
135// int N[NDIM];
136// bool flag = false;
137 //vector Shifted, TranslationVector;
138 Vector TestVector;
139// *out << Verbose(1) << "Begin of KeepPeriodic." << endl;
140// *out << Verbose(2) << "Vector is: ";
141// Output(out);
142// *out << endl;
143 TestVector.CopyVector(this);
144 TestVector.InverseMatrixMultiplication(matrix);
145 for(int i=NDIM;i--;) { // correct periodically
146 if (TestVector.x[i] < 0) { // get every coefficient into the interval [0,1)
147 TestVector.x[i] += ceil(TestVector.x[i]);
148 } else {
149 TestVector.x[i] -= floor(TestVector.x[i]);
150 }
151 }
152 TestVector.MatrixMultiplication(matrix);
153 CopyVector(&TestVector);
154// *out << Verbose(2) << "New corrected vector is: ";
155// Output(out);
156// *out << endl;
157// *out << Verbose(1) << "End of KeepPeriodic." << endl;
158};
159
160/** Calculates scalar product between this and another vector.
161 * \param *y array to second vector
162 * \return \f$\langle x, y \rangle\f$
163 */
164double Vector::ScalarProduct(const Vector *y) const
165{
166 double res = 0.;
167 for (int i=NDIM;i--;)
168 res += x[i]*y->x[i];
169 return (res);
170};
171
172
173/** Calculates VectorProduct between this and another vector.
174 * -# returns the Product in place of vector from which it was initiated
175 * -# ATTENTION: Only three dim.
176 * \param *y array to vector with which to calculate crossproduct
177 * \return \f$ x \times y \f&
178 */
179void Vector::VectorProduct(const Vector *y)
180{
181 Vector tmp;
182 tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
183 tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
184 tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
185 this->CopyVector(&tmp);
186
187};
188
189
190/** projects this vector onto plane defined by \a *y.
191 * \param *y normal vector of plane
192 * \return \f$\langle x, y \rangle\f$
193 */
194void Vector::ProjectOntoPlane(const Vector *y)
195{
196 Vector tmp;
197 tmp.CopyVector(y);
198 tmp.Normalize();
199 tmp.Scale(ScalarProduct(&tmp));
200 this->SubtractVector(&tmp);
201};
202
203/** Calculates the intersection point between a line defined by \a *LineVector and \a *LineVector2 and a plane defined by \a *Normal and \a *PlaneOffset.
204 * According to [Bronstein] the vectorial plane equation is:
205 * -# \f$\stackrel{r}{\rightarrow} \cdot \stackrel{N}{\rightarrow} + D = 0\f$,
206 * where \f$\stackrel{r}{\rightarrow}\f$ is the vector to be testet, \f$\stackrel{N}{\rightarrow}\f$ is the plane's normal vector and
207 * \f$D = - \stackrel{a}{\rightarrow} \stackrel{N}{\rightarrow}\f$, the offset with respect to origin, if \f$\stackrel{a}{\rightarrow}\f$,
208 * is an offset vector onto the plane. The line is parametrized by \f$\stackrel{x}{\rightarrow} + k \stackrel{t}{\rightarrow}\f$, where
209 * \f$\stackrel{x}{\rightarrow}\f$ is the offset and \f$\stackrel{t}{\rightarrow}\f$ the directional vector (NOTE: No need to normalize
210 * the latter). Inserting the parametrized form into the plane equation and solving for \f$k\f$, which we insert then into the parametrization
211 * of the line yields the intersection point on the plane.
212 * \param *out output stream for debugging
213 * \param *PlaneNormal Plane's normal vector
214 * \param *PlaneOffset Plane's offset vector
215 * \param *Origin first vector of line
216 * \param *LineVector second vector of line
217 * \return true - \a this contains intersection point on return, false - line is parallel to plane
218 */
219bool Vector::GetIntersectionWithPlane(ofstream *out, Vector *PlaneNormal, Vector *PlaneOffset, Vector *Origin, Vector *LineVector)
220{
221 double factor;
222 Vector Direction, helper;
223
224 // find intersection of a line defined by Offset and Direction with a plane defined by triangle
225 Direction.CopyVector(LineVector);
226 Direction.SubtractVector(Origin);
227 Direction.Normalize();
228 //*out << Verbose(4) << "INFO: Direction is " << Direction << "." << endl;
229 factor = Direction.ScalarProduct(PlaneNormal);
230 if (factor < MYEPSILON) { // Uniqueness: line parallel to plane?
231 *out << Verbose(2) << "WARNING: Line is parallel to plane, no intersection." << endl;
232 return false;
233 }
234 helper.CopyVector(PlaneOffset);
235 helper.SubtractVector(Origin);
236 factor = helper.ScalarProduct(PlaneNormal)/factor;
237 if (factor < MYEPSILON) { // Origin is in-plane
238 //*out << Verbose(2) << "Origin of line is in-plane, simple." << endl;
239 CopyVector(Origin);
240 return true;
241 }
242 //factor = Origin->ScalarProduct(PlaneNormal)*(-PlaneOffset->ScalarProduct(PlaneNormal))/(Direction.ScalarProduct(PlaneNormal));
243 Direction.Scale(factor);
244 CopyVector(Origin);
245 //*out << Verbose(4) << "INFO: Scaled direction is " << Direction << "." << endl;
246 AddVector(&Direction);
247
248 // test whether resulting vector really is on plane
249 helper.CopyVector(this);
250 helper.SubtractVector(PlaneOffset);
251 if (helper.ScalarProduct(PlaneNormal) < MYEPSILON) {
252 //*out << Verbose(2) << "INFO: Intersection at " << *this << " is good." << endl;
253 return true;
254 } else {
255 *out << Verbose(2) << "WARNING: Intersection point " << *this << " is not on plane." << endl;
256 return false;
257 }
258};
259
260/** Calculates the minimum distance of this vector to the plane.
261 * \param *out output stream for debugging
262 * \param *PlaneNormal normal of plane
263 * \param *PlaneOffset offset of plane
264 * \return distance to plane
265 */
266double Vector::DistanceToPlane(ofstream *out, Vector *PlaneNormal, Vector *PlaneOffset)
267{
268 Vector temp;
269
270 // first create part that is orthonormal to PlaneNormal with withdraw
271 temp.CopyVector(this);
272 temp.SubtractVector(PlaneOffset);
273 temp.MakeNormalVector(PlaneNormal);
274 temp.Scale(-1.);
275 // then add connecting vector from plane to point
276 temp.AddVector(this);
277 temp.SubtractVector(PlaneOffset);
278
279 return temp.Norm();
280};
281
282/** Calculates the intersection of the two lines that are both on the same plane.
283 * We construct auxiliary plane with its vector normal to one line direction and the PlaneNormal, then a vector
284 * from the first line's offset onto the plane. Finally, scale by factor is 1/cos(angle(line1,line2..)) = 1/SP(...), and
285 * project onto the first line's direction and add its offset.
286 * \param *out output stream for debugging
287 * \param *Line1a first vector of first line
288 * \param *Line1b second vector of first line
289 * \param *Line2a first vector of second line
290 * \param *Line2b second vector of second line
291 * \param *PlaneNormal normal of plane, is supplemental/arbitrary
292 * \return true - \a this will contain the intersection on return, false - lines are parallel
293 */
294bool Vector::GetIntersectionOfTwoLinesOnPlane(ofstream *out, Vector *Line1a, Vector *Line1b, Vector *Line2a, Vector *Line2b, const Vector *PlaneNormal)
295{
296 bool result = true;
297 Vector Direction, OtherDirection;
298 Vector AuxiliaryNormal;
299 Vector Distance;
300 const Vector *Normal = NULL;
301 Vector *ConstructedNormal = NULL;
302 bool FreeNormal = false;
303
304 // construct both direction vectors
305 Zero();
306 Direction.CopyVector(Line1b);
307 Direction.SubtractVector(Line1a);
308 if (Direction.IsZero())
309 return false;
310 OtherDirection.CopyVector(Line2b);
311 OtherDirection.SubtractVector(Line2a);
312 if (OtherDirection.IsZero())
313 return false;
314
315 Direction.Normalize();
316 OtherDirection.Normalize();
317
318 //*out << Verbose(4) << "INFO: Normalized Direction " << Direction << " and OtherDirection " << OtherDirection << "." << endl;
319
320 if (fabs(OtherDirection.ScalarProduct(&Direction) - 1.) < MYEPSILON) { // lines are parallel
321 if ((Line1a == Line2a) || (Line1a == Line2b))
322 CopyVector(Line1a);
323 else if ((Line1b == Line2b) || (Line1b == Line2b))
324 CopyVector(Line1b);
325 else
326 return false;
327 *out << Verbose(4) << "INFO: Intersection is " << *this << "." << endl;
328 return true;
329 } else {
330 // check whether we have a plane normal vector
331 if (PlaneNormal == NULL) {
332 ConstructedNormal = new Vector;
333 ConstructedNormal->MakeNormalVector(&Direction, &OtherDirection);
334 Normal = ConstructedNormal;
335 FreeNormal = true;
336 } else
337 Normal = PlaneNormal;
338
339 AuxiliaryNormal.MakeNormalVector(&OtherDirection, Normal);
340 //*out << Verbose(4) << "INFO: PlaneNormal is " << *Normal << " and AuxiliaryNormal " << AuxiliaryNormal << "." << endl;
341
342 Distance.CopyVector(Line2a);
343 Distance.SubtractVector(Line1a);
344 //*out << Verbose(4) << "INFO: Distance is " << Distance << "." << endl;
345 if (Distance.IsZero()) {
346 // offsets are equal, match found
347 CopyVector(Line1a);
348 result = true;
349 } else {
350 CopyVector(Distance.Projection(&AuxiliaryNormal));
351 //*out << Verbose(4) << "INFO: Projected Distance is " << *this << "." << endl;
352 double factor = Direction.ScalarProduct(&AuxiliaryNormal);
353 //*out << Verbose(4) << "INFO: Scaling factor is " << factor << "." << endl;
354 Scale(1./(factor*factor));
355 //*out << Verbose(4) << "INFO: Scaled Distance is " << *this << "." << endl;
356 CopyVector(Projection(&Direction));
357 //*out << Verbose(4) << "INFO: Distance, projected into Direction, is " << *this << "." << endl;
358 if (this->IsZero())
359 result = false;
360 else
361 result = true;
362 AddVector(Line1a);
363 }
364
365 if (FreeNormal)
366 delete(ConstructedNormal);
367 }
368 if (result)
369 *out << Verbose(4) << "INFO: Intersection is " << *this << "." << endl;
370
371 return result;
372};
373
374/** Calculates the projection of a vector onto another \a *y.
375 * \param *y array to second vector
376 */
377void Vector::ProjectIt(const Vector *y)
378{
379 Vector helper(*y);
380 helper.Scale(-(ScalarProduct(y)));
381 AddVector(&helper);
382};
383
384/** Calculates the projection of a vector onto another \a *y.
385 * \param *y array to second vector
386 * \return Vector
387 */
388Vector Vector::Projection(const Vector *y) const
389{
390 Vector helper(*y);
391 helper.Scale((ScalarProduct(y)/y->NormSquared()));
392
393 return helper;
394};
395
396/** Calculates norm of this vector.
397 * \return \f$|x|\f$
398 */
399double Vector::Norm() const
400{
401 double res = 0.;
402 for (int i=NDIM;i--;)
403 res += this->x[i]*this->x[i];
404 return (sqrt(res));
405};
406
407/** Calculates squared norm of this vector.
408 * \return \f$|x|^2\f$
409 */
410double Vector::NormSquared() const
411{
412 return (ScalarProduct(this));
413};
414
415/** Normalizes this vector.
416 */
417void Vector::Normalize()
418{
419 double res = 0.;
420 for (int i=NDIM;i--;)
421 res += this->x[i]*this->x[i];
422 if (fabs(res) > MYEPSILON)
423 res = 1./sqrt(res);
424 Scale(&res);
425};
426
427/** Zeros all components of this vector.
428 */
429void Vector::Zero()
430{
431 for (int i=NDIM;i--;)
432 this->x[i] = 0.;
433};
434
435/** Zeros all components of this vector.
436 */
437void Vector::One(double one)
438{
439 for (int i=NDIM;i--;)
440 this->x[i] = one;
441};
442
443/** Initialises all components of this vector.
444 */
445void Vector::Init(double x1, double x2, double x3)
446{
447 x[0] = x1;
448 x[1] = x2;
449 x[2] = x3;
450};
451
452/** Checks whether vector has all components zero.
453 * @return true - vector is zero, false - vector is not
454 */
455bool Vector::IsZero() const
456{
457 return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
458};
459
460/** Checks whether vector has length of 1.
461 * @return true - vector is normalized, false - vector is not
462 */
463bool Vector::IsOne() const
464{
465 return (fabs(Norm() - 1.) < MYEPSILON);
466};
467
468/** Checks whether vector is normal to \a *normal.
469 * @return true - vector is normalized, false - vector is not
470 */
471bool Vector::IsNormalTo(const Vector *normal) const
472{
473 if (ScalarProduct(normal) < MYEPSILON)
474 return true;
475 else
476 return false;
477};
478
479/** Calculates the angle between this and another vector.
480 * \param *y array to second vector
481 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
482 */
483double Vector::Angle(const Vector *y) const
484{
485 double norm1 = Norm(), norm2 = y->Norm();
486 double angle = -1;
487 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
488 angle = this->ScalarProduct(y)/norm1/norm2;
489 // -1-MYEPSILON occured due to numerical imprecision, catch ...
490 //cout << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
491 if (angle < -1)
492 angle = -1;
493 if (angle > 1)
494 angle = 1;
495 return acos(angle);
496};
497
498/** Rotates the vector relative to the origin around the axis given by \a *axis by an angle of \a alpha.
499 * \param *axis rotation axis
500 * \param alpha rotation angle in radian
501 */
502void Vector::RotateVector(const Vector *axis, const double alpha)
503{
504 Vector a,y;
505 // normalise this vector with respect to axis
506 a.CopyVector(this);
507 a.ProjectOntoPlane(axis);
508 // construct normal vector
509 bool rotatable = y.MakeNormalVector(axis,&a);
510 // The normal vector cannot be created if there is linar dependency.
511 // Then the vector to rotate is on the axis and any rotation leads to the vector itself.
512 if (!rotatable) {
513 return;
514 }
515 y.Scale(Norm());
516 // scale normal vector by sine and this vector by cosine
517 y.Scale(sin(alpha));
518 a.Scale(cos(alpha));
519 CopyVector(Projection(axis));
520 // add scaled normal vector onto this vector
521 AddVector(&y);
522 // add part in axis direction
523 AddVector(&a);
524};
525
526/** Compares vector \a to vector \a b component-wise.
527 * \param a base vector
528 * \param b vector components to add
529 * \return a == b
530 */
531bool operator==(const Vector& a, const Vector& b)
532{
533 bool status = true;
534 for (int i=0;i<NDIM;i++)
535 status = status && (fabs(a.x[i] - b.x[i]) < MYEPSILON);
536 return status;
537};
538
539/** Sums vector \a to this lhs component-wise.
540 * \param a base vector
541 * \param b vector components to add
542 * \return lhs + a
543 */
544Vector& operator+=(Vector& a, const Vector& b)
545{
546 a.AddVector(&b);
547 return a;
548};
549
550/** Subtracts vector \a from this lhs component-wise.
551 * \param a base vector
552 * \param b vector components to add
553 * \return lhs - a
554 */
555Vector& operator-=(Vector& a, const Vector& b)
556{
557 a.SubtractVector(&b);
558 return a;
559};
560
561/** factor each component of \a a times a double \a m.
562 * \param a base vector
563 * \param m factor
564 * \return lhs.x[i] * m
565 */
566Vector& operator*=(Vector& a, const double m)
567{
568 a.Scale(m);
569 return a;
570};
571
572/** Sums two vectors \a and \b component-wise.
573 * \param a first vector
574 * \param b second vector
575 * \return a + b
576 */
577Vector& operator+(const Vector& a, const Vector& b)
578{
579 Vector *x = new Vector;
580 x->CopyVector(&a);
581 x->AddVector(&b);
582 return *x;
583};
584
585/** Subtracts vector \a from \b component-wise.
586 * \param a first vector
587 * \param b second vector
588 * \return a - b
589 */
590Vector& operator-(const Vector& a, const Vector& b)
591{
592 Vector *x = new Vector;
593 x->CopyVector(&a);
594 x->SubtractVector(&b);
595 return *x;
596};
597
598/** Factors given vector \a a times \a m.
599 * \param a vector
600 * \param m factor
601 * \return m * a
602 */
603Vector& operator*(const Vector& a, const double m)
604{
605 Vector *x = new Vector;
606 x->CopyVector(&a);
607 x->Scale(m);
608 return *x;
609};
610
611/** Factors given vector \a a times \a m.
612 * \param m factor
613 * \param a vector
614 * \return m * a
615 */
616Vector& operator*(const double m, const Vector& a )
617{
618 Vector *x = new Vector;
619 x->CopyVector(&a);
620 x->Scale(m);
621 return *x;
622};
623
624/** Prints a 3dim vector.
625 * prints no end of line.
626 * \param *out output stream
627 */
628bool Vector::Output(ofstream *out) const
629{
630 if (out != NULL) {
631 *out << "(";
632 for (int i=0;i<NDIM;i++) {
633 *out << x[i];
634 if (i != 2)
635 *out << ",";
636 }
637 *out << ")";
638 return true;
639 } else
640 return false;
641};
642
643ostream& operator<<(ostream& ost, const Vector& m)
644{
645 ost << "(";
646 for (int i=0;i<NDIM;i++) {
647 ost << m.x[i];
648 if (i != 2)
649 ost << ",";
650 }
651 ost << ")";
652 return ost;
653};
654
655/** Scales each atom coordinate by an individual \a factor.
656 * \param *factor pointer to scaling factor
657 */
658void Vector::Scale(double **factor)
659{
660 for (int i=NDIM;i--;)
661 x[i] *= (*factor)[i];
662};
663
664void Vector::Scale(double *factor)
665{
666 for (int i=NDIM;i--;)
667 x[i] *= *factor;
668};
669
670void Vector::Scale(double factor)
671{
672 for (int i=NDIM;i--;)
673 x[i] *= factor;
674};
675
676/** Translate atom by given vector.
677 * \param trans[] translation vector.
678 */
679void Vector::Translate(const Vector *trans)
680{
681 for (int i=NDIM;i--;)
682 x[i] += trans->x[i];
683};
684
685/** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
686 * \param *M matrix of box
687 * \param *Minv inverse matrix
688 */
689void Vector::WrapPeriodically(const double *M, const double *Minv)
690{
691 MatrixMultiplication(Minv);
692 // truncate to [0,1] for each axis
693 for (int i=0;i<NDIM;i++) {
694 x[i] += 0.5; // set to center of box
695 while (x[i] >= 1.)
696 x[i] -= 1.;
697 while (x[i] < 0.)
698 x[i] += 1.;
699 }
700 MatrixMultiplication(M);
701};
702
703/** Do a matrix multiplication.
704 * \param *matrix NDIM_NDIM array
705 */
706void Vector::MatrixMultiplication(const double *M)
707{
708 Vector C;
709 // do the matrix multiplication
710 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
711 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
712 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
713 // transfer the result into this
714 for (int i=NDIM;i--;)
715 x[i] = C.x[i];
716};
717
718/** Calculate the inverse of a 3x3 matrix.
719 * \param *matrix NDIM_NDIM array
720 */
721double * Vector::InverseMatrix(double *A)
722{
723 double *B = Malloc<double>(NDIM * NDIM, "Vector::InverseMatrix: *B");
724 double detA = RDET3(A);
725 double detAReci;
726
727 for (int i=0;i<NDIM*NDIM;++i)
728 B[i] = 0.;
729 // calculate the inverse B
730 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
731 detAReci = 1./detA;
732 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
733 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
734 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
735 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
736 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
737 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
738 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
739 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
740 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
741 }
742 return B;
743};
744
745/** Do a matrix multiplication with the \a *A' inverse.
746 * \param *matrix NDIM_NDIM array
747 */
748void Vector::InverseMatrixMultiplication(const double *A)
749{
750 Vector C;
751 double B[NDIM*NDIM];
752 double detA = RDET3(A);
753 double detAReci;
754
755 // calculate the inverse B
756 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
757 detAReci = 1./detA;
758 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
759 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
760 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
761 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
762 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
763 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
764 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
765 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
766 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
767
768 // do the matrix multiplication
769 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
770 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
771 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
772 // transfer the result into this
773 for (int i=NDIM;i--;)
774 x[i] = C.x[i];
775 } else {
776 cerr << "ERROR: inverse of matrix does not exists: det A = " << detA << "." << endl;
777 }
778};
779
780
781/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
782 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
783 * \param *x1 first vector
784 * \param *x2 second vector
785 * \param *x3 third vector
786 * \param *factors three-component vector with the factor for each given vector
787 */
788void Vector::LinearCombinationOfVectors(const Vector *x1, const Vector *x2, const Vector *x3, double *factors)
789{
790 for(int i=NDIM;i--;)
791 x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
792};
793
794/** Mirrors atom against a given plane.
795 * \param n[] normal vector of mirror plane.
796 */
797void Vector::Mirror(const Vector *n)
798{
799 double projection;
800 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
801 // withdraw projected vector twice from original one
802 cout << Verbose(1) << "Vector: ";
803 Output((ofstream *)&cout);
804 cout << "\t";
805 for (int i=NDIM;i--;)
806 x[i] -= 2.*projection*n->x[i];
807 cout << "Projected vector: ";
808 Output((ofstream *)&cout);
809 cout << endl;
810};
811
812/** Calculates normal vector for three given vectors (being three points in space).
813 * Makes this vector orthonormal to the three given points, making up a place in 3d space.
814 * \param *y1 first vector
815 * \param *y2 second vector
816 * \param *y3 third vector
817 * \return true - success, vectors are linear independent, false - failure due to linear dependency
818 */
819bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2, const Vector *y3)
820{
821 Vector x1, x2;
822
823 x1.CopyVector(y1);
824 x1.SubtractVector(y2);
825 x2.CopyVector(y3);
826 x2.SubtractVector(y2);
827 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
828 cout << Verbose(4) << "WARNING: Given vectors are linear dependent." << endl;
829 return false;
830 }
831// cout << Verbose(4) << "relative, first plane coordinates:";
832// x1.Output((ofstream *)&cout);
833// cout << endl;
834// cout << Verbose(4) << "second plane coordinates:";
835// x2.Output((ofstream *)&cout);
836// cout << endl;
837
838 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
839 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
840 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
841 Normalize();
842
843 return true;
844};
845
846
847/** Calculates orthonormal vector to two given vectors.
848 * Makes this vector orthonormal to two given vectors. This is very similar to the other
849 * vector::MakeNormalVector(), only there three points whereas here two difference
850 * vectors are given.
851 * \param *x1 first vector
852 * \param *x2 second vector
853 * \return true - success, vectors are linear independent, false - failure due to linear dependency
854 */
855bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2)
856{
857 Vector x1,x2;
858 x1.CopyVector(y1);
859 x2.CopyVector(y2);
860 Zero();
861 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
862 cout << Verbose(4) << "WARNING: Given vectors are linear dependent." << endl;
863 return false;
864 }
865// cout << Verbose(4) << "relative, first plane coordinates:";
866// x1.Output((ofstream *)&cout);
867// cout << endl;
868// cout << Verbose(4) << "second plane coordinates:";
869// x2.Output((ofstream *)&cout);
870// cout << endl;
871
872 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
873 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
874 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
875 Normalize();
876
877 return true;
878};
879
880/** Calculates orthonormal vector to one given vectors.
881 * Just subtracts the projection onto the given vector from this vector.
882 * The removed part of the vector is Vector::Projection()
883 * \param *x1 vector
884 * \return true - success, false - vector is zero
885 */
886bool Vector::MakeNormalVector(const Vector *y1)
887{
888 bool result = false;
889 double factor = y1->ScalarProduct(this)/y1->NormSquared();
890 Vector x1;
891 x1.CopyVector(y1);
892 x1.Scale(factor);
893 SubtractVector(&x1);
894 for (int i=NDIM;i--;)
895 result = result || (fabs(x[i]) > MYEPSILON);
896
897 return result;
898};
899
900/** Creates this vector as one of the possible orthonormal ones to the given one.
901 * Just scan how many components of given *vector are unequal to zero and
902 * try to get the skp of both to be zero accordingly.
903 * \param *vector given vector
904 * \return true - success, false - failure (null vector given)
905 */
906bool Vector::GetOneNormalVector(const Vector *GivenVector)
907{
908 int Components[NDIM]; // contains indices of non-zero components
909 int Last = 0; // count the number of non-zero entries in vector
910 int j; // loop variables
911 double norm;
912
913 cout << Verbose(4);
914 GivenVector->Output((ofstream *)&cout);
915 cout << endl;
916 for (j=NDIM;j--;)
917 Components[j] = -1;
918 // find two components != 0
919 for (j=0;j<NDIM;j++)
920 if (fabs(GivenVector->x[j]) > MYEPSILON)
921 Components[Last++] = j;
922 cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
923
924 switch(Last) {
925 case 3: // threecomponent system
926 case 2: // two component system
927 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
928 x[Components[2]] = 0.;
929 // in skp both remaining parts shall become zero but with opposite sign and third is zero
930 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
931 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
932 return true;
933 break;
934 case 1: // one component system
935 // set sole non-zero component to 0, and one of the other zero component pendants to 1
936 x[(Components[0]+2)%NDIM] = 0.;
937 x[(Components[0]+1)%NDIM] = 1.;
938 x[Components[0]] = 0.;
939 return true;
940 break;
941 default:
942 return false;
943 }
944};
945
946/** Determines parameter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
947 * \param *A first plane vector
948 * \param *B second plane vector
949 * \param *C third plane vector
950 * \return scaling parameter for this vector
951 */
952double Vector::CutsPlaneAt(Vector *A, Vector *B, Vector *C)
953{
954// cout << Verbose(3) << "For comparison: ";
955// cout << "A " << A->Projection(this) << "\t";
956// cout << "B " << B->Projection(this) << "\t";
957// cout << "C " << C->Projection(this) << "\t";
958// cout << endl;
959 return A->ScalarProduct(this);
960};
961
962/** Creates a new vector as the one with least square distance to a given set of \a vectors.
963 * \param *vectors set of vectors
964 * \param num number of vectors
965 * \return true if success, false if failed due to linear dependency
966 */
967bool Vector::LSQdistance(Vector **vectors, int num)
968{
969 int j;
970
971 for (j=0;j<num;j++) {
972 cout << Verbose(1) << j << "th atom's vector: ";
973 (vectors[j])->Output((ofstream *)&cout);
974 cout << endl;
975 }
976
977 int np = 3;
978 struct LSQ_params par;
979
980 const gsl_multimin_fminimizer_type *T =
981 gsl_multimin_fminimizer_nmsimplex;
982 gsl_multimin_fminimizer *s = NULL;
983 gsl_vector *ss, *y;
984 gsl_multimin_function minex_func;
985
986 size_t iter = 0, i;
987 int status;
988 double size;
989
990 /* Initial vertex size vector */
991 ss = gsl_vector_alloc (np);
992 y = gsl_vector_alloc (np);
993
994 /* Set all step sizes to 1 */
995 gsl_vector_set_all (ss, 1.0);
996
997 /* Starting point */
998 par.vectors = vectors;
999 par.num = num;
1000
1001 for (i=NDIM;i--;)
1002 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
1003
1004 /* Initialize method and iterate */
1005 minex_func.f = &LSQ;
1006 minex_func.n = np;
1007 minex_func.params = (void *)&par;
1008
1009 s = gsl_multimin_fminimizer_alloc (T, np);
1010 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
1011
1012 do
1013 {
1014 iter++;
1015 status = gsl_multimin_fminimizer_iterate(s);
1016
1017 if (status)
1018 break;
1019
1020 size = gsl_multimin_fminimizer_size (s);
1021 status = gsl_multimin_test_size (size, 1e-2);
1022
1023 if (status == GSL_SUCCESS)
1024 {
1025 printf ("converged to minimum at\n");
1026 }
1027
1028 printf ("%5d ", (int)iter);
1029 for (i = 0; i < (size_t)np; i++)
1030 {
1031 printf ("%10.3e ", gsl_vector_get (s->x, i));
1032 }
1033 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
1034 }
1035 while (status == GSL_CONTINUE && iter < 100);
1036
1037 for (i=(size_t)np;i--;)
1038 this->x[i] = gsl_vector_get(s->x, i);
1039 gsl_vector_free(y);
1040 gsl_vector_free(ss);
1041 gsl_multimin_fminimizer_free (s);
1042
1043 return true;
1044};
1045
1046/** Adds vector \a *y componentwise.
1047 * \param *y vector
1048 */
1049void Vector::AddVector(const Vector *y)
1050{
1051 for (int i=NDIM;i--;)
1052 this->x[i] += y->x[i];
1053}
1054
1055/** Adds vector \a *y componentwise.
1056 * \param *y vector
1057 */
1058void Vector::SubtractVector(const Vector *y)
1059{
1060 for (int i=NDIM;i--;)
1061 this->x[i] -= y->x[i];
1062}
1063
1064/** Copy vector \a *y componentwise.
1065 * \param *y vector
1066 */
1067void Vector::CopyVector(const Vector *y)
1068{
1069 for (int i=NDIM;i--;)
1070 this->x[i] = y->x[i];
1071}
1072
1073/** Copy vector \a y componentwise.
1074 * \param y vector
1075 */
1076void Vector::CopyVector(const Vector y)
1077{
1078 for (int i=NDIM;i--;)
1079 this->x[i] = y.x[i];
1080}
1081
1082
1083/** Asks for position, checks for boundary.
1084 * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
1085 * \param check whether bounds shall be checked (true) or not (false)
1086 */
1087void Vector::AskPosition(double *cell_size, bool check)
1088{
1089 char coords[3] = {'x','y','z'};
1090 int j = -1;
1091 for (int i=0;i<3;i++) {
1092 j += i+1;
1093 do {
1094 cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
1095 cin >> x[i];
1096 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
1097 }
1098};
1099
1100/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
1101 * This is linear system of equations to be solved, however of the three given (skp of this vector\
1102 * with either of the three hast to be zero) only two are linear independent. The third equation
1103 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
1104 * where very often it has to be checked whether a certain value is zero or not and thus forked into
1105 * another case.
1106 * \param *x1 first vector
1107 * \param *x2 second vector
1108 * \param *y third vector
1109 * \param alpha first angle
1110 * \param beta second angle
1111 * \param c norm of final vector
1112 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
1113 * \bug this is not yet working properly
1114 */
1115bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
1116{
1117 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
1118 double ang; // angle on testing
1119 double sign[3];
1120 int i,j,k;
1121 A = cos(alpha) * x1->Norm() * c;
1122 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
1123 B2 = cos(beta) * x2->Norm() * c;
1124 C = c * c;
1125 cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
1126 int flag = 0;
1127 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
1128 if (fabs(x1->x[1]) > MYEPSILON) {
1129 flag = 1;
1130 } else if (fabs(x1->x[2]) > MYEPSILON) {
1131 flag = 2;
1132 } else {
1133 return false;
1134 }
1135 }
1136 switch (flag) {
1137 default:
1138 case 0:
1139 break;
1140 case 2:
1141 flip(x1->x[0],x1->x[1]);
1142 flip(x2->x[0],x2->x[1]);
1143 flip(y->x[0],y->x[1]);
1144 //flip(x[0],x[1]);
1145 flip(x1->x[1],x1->x[2]);
1146 flip(x2->x[1],x2->x[2]);
1147 flip(y->x[1],y->x[2]);
1148 //flip(x[1],x[2]);
1149 case 1:
1150 flip(x1->x[0],x1->x[1]);
1151 flip(x2->x[0],x2->x[1]);
1152 flip(y->x[0],y->x[1]);
1153 //flip(x[0],x[1]);
1154 flip(x1->x[1],x1->x[2]);
1155 flip(x2->x[1],x2->x[2]);
1156 flip(y->x[1],y->x[2]);
1157 //flip(x[1],x[2]);
1158 break;
1159 }
1160 // now comes the case system
1161 D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
1162 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
1163 D3 = y->x[0]/x1->x[0]*A-B1;
1164 cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
1165 if (fabs(D1) < MYEPSILON) {
1166 cout << Verbose(2) << "D1 == 0!\n";
1167 if (fabs(D2) > MYEPSILON) {
1168 cout << Verbose(3) << "D2 != 0!\n";
1169 x[2] = -D3/D2;
1170 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
1171 E2 = -x1->x[1]/x1->x[0];
1172 cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1173 F1 = E1*E1 + 1.;
1174 F2 = -E1*E2;
1175 F3 = E1*E1 + D3*D3/(D2*D2) - C;
1176 cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1177 if (fabs(F1) < MYEPSILON) {
1178 cout << Verbose(4) << "F1 == 0!\n";
1179 cout << Verbose(4) << "Gleichungssystem linear\n";
1180 x[1] = F3/(2.*F2);
1181 } else {
1182 p = F2/F1;
1183 q = p*p - F3/F1;
1184 cout << Verbose(4) << "p " << p << "\tq " << q << endl;
1185 if (q < 0) {
1186 cout << Verbose(4) << "q < 0" << endl;
1187 return false;
1188 }
1189 x[1] = p + sqrt(q);
1190 }
1191 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1192 } else {
1193 cout << Verbose(2) << "Gleichungssystem unterbestimmt\n";
1194 return false;
1195 }
1196 } else {
1197 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
1198 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
1199 cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1200 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
1201 F2 = -(E1*E2 + D2*D3/(D1*D1));
1202 F3 = E1*E1 + D3*D3/(D1*D1) - C;
1203 cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1204 if (fabs(F1) < MYEPSILON) {
1205 cout << Verbose(3) << "F1 == 0!\n";
1206 cout << Verbose(3) << "Gleichungssystem linear\n";
1207 x[2] = F3/(2.*F2);
1208 } else {
1209 p = F2/F1;
1210 q = p*p - F3/F1;
1211 cout << Verbose(3) << "p " << p << "\tq " << q << endl;
1212 if (q < 0) {
1213 cout << Verbose(3) << "q < 0" << endl;
1214 return false;
1215 }
1216 x[2] = p + sqrt(q);
1217 }
1218 x[1] = (-D2 * x[2] - D3)/D1;
1219 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1220 }
1221 switch (flag) { // back-flipping
1222 default:
1223 case 0:
1224 break;
1225 case 2:
1226 flip(x1->x[0],x1->x[1]);
1227 flip(x2->x[0],x2->x[1]);
1228 flip(y->x[0],y->x[1]);
1229 flip(x[0],x[1]);
1230 flip(x1->x[1],x1->x[2]);
1231 flip(x2->x[1],x2->x[2]);
1232 flip(y->x[1],y->x[2]);
1233 flip(x[1],x[2]);
1234 case 1:
1235 flip(x1->x[0],x1->x[1]);
1236 flip(x2->x[0],x2->x[1]);
1237 flip(y->x[0],y->x[1]);
1238 //flip(x[0],x[1]);
1239 flip(x1->x[1],x1->x[2]);
1240 flip(x2->x[1],x2->x[2]);
1241 flip(y->x[1],y->x[2]);
1242 flip(x[1],x[2]);
1243 break;
1244 }
1245 // one z component is only determined by its radius (without sign)
1246 // thus check eight possible sign flips and determine by checking angle with second vector
1247 for (i=0;i<8;i++) {
1248 // set sign vector accordingly
1249 for (j=2;j>=0;j--) {
1250 k = (i & pot(2,j)) << j;
1251 cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
1252 sign[j] = (k == 0) ? 1. : -1.;
1253 }
1254 cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
1255 // apply sign matrix
1256 for (j=NDIM;j--;)
1257 x[j] *= sign[j];
1258 // calculate angle and check
1259 ang = x2->Angle (this);
1260 cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
1261 if (fabs(ang - cos(beta)) < MYEPSILON) {
1262 break;
1263 }
1264 // unapply sign matrix (is its own inverse)
1265 for (j=NDIM;j--;)
1266 x[j] *= sign[j];
1267 }
1268 return true;
1269};
1270
1271/**
1272 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
1273 * their offset.
1274 *
1275 * @param offest for the origin of the parallelepiped
1276 * @param three vectors forming the matrix that defines the shape of the parallelpiped
1277 */
1278bool Vector::IsInParallelepiped(const Vector offset, const double *parallelepiped) const
1279{
1280 Vector a;
1281 a.CopyVector(this);
1282 a.SubtractVector(&offset);
1283 a.InverseMatrixMultiplication(parallelepiped);
1284 bool isInside = true;
1285
1286 for (int i=NDIM;i--;)
1287 isInside = isInside && ((a.x[i] <= 1) && (a.x[i] >= 0));
1288
1289 return isInside;
1290}
Note: See TracBrowser for help on using the repository browser.