source: src/vector.cpp@ e5ad5c

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

Huge change from ofstream * (const) out --> Log().

  • first shift was done via regular expressions
  • then via error messages from the code
  • note that class atom, class element and class molecule kept in parts their output stream, was they print to file.
  • make check runs fine
  • MISSING: Verbosity is not fixed for everything (i.e. if no endl; is present and next has Verbose(0) ...)

Signed-off-by: Frederik Heber <heber@…>

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