source: src/vector.cpp@ fcad4b

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

InsideOutside unit test of tesselation is working correctly.

  • FIX: BoundaryTriangleSet::GetIntersectionInsideTriangle() - don't need helper, just check whether CrossPoint is returned (and true) for all of the three sides.
  • FIX: Tesselation::IsInnerPoint() - projection onto plane and stuff was nonsense, just take the Point ans Intersection which is on the plane anyway.
  • FIX: Vector::GetIntersectionOfTwoLinesOnPlane() - coefficient MUST be zero (then vectors are coplanar), but parallelity check was missing. Also, we have to check whether s is in [0,1] in order to see whether we are inside the triangle side or outside.

Signed-off-by: Frederik Heber <heber@tabletINS.(none)>

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