source: src/vector.cpp@ b1d8092

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 b1d8092 was b8d1aeb, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'MenuRefactoring' into QT4Refactoring

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/builder.cpp
molecuilder/src/unittests/Makefile.am

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