source: src/vector.cpp@ 2561df

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

Merge branch 'MenuRefactoring' into QT4Refactoring

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/unittests/Makefile.am
molecuilder/src/vector.cpp
molecuilder/src/vector.hpp

  • Property mode set to 100644
File size: 39.0 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 delete(M);
333 Log() << Verbose(1) << "INFO: Line1a = " << *Line1a << ", Line1b = " << *Line1b << ", Line2a = " << *Line2a << ", Line2b = " << *Line2b << "." << endl;
334
335
336 // constuct a,b,c
337 Vector a;
338 Vector b;
339 Vector c;
340 Vector d;
341 a.CopyVector(Line1b);
342 a.SubtractVector(Line1a);
343 b.CopyVector(Line2b);
344 b.SubtractVector(Line2a);
345 c.CopyVector(Line2a);
346 c.SubtractVector(Line1a);
347 d.CopyVector(Line2b);
348 d.SubtractVector(Line1b);
349 Log() << Verbose(1) << "INFO: a = " << a << ", b = " << b << ", c = " << c << "." << endl;
350 if ((a.NormSquared() < MYEPSILON) || (b.NormSquared() < MYEPSILON)) {
351 Zero();
352 Log() << Verbose(1) << "At least one of the lines is ill-defined, i.e. offset equals second vector." << endl;
353 return false;
354 }
355
356 // check for parallelity
357 Vector parallel;
358 double factor = 0.;
359 if (fabs(a.ScalarProduct(&b)*a.ScalarProduct(&b)/a.NormSquared()/b.NormSquared() - 1.) < MYEPSILON) {
360 parallel.CopyVector(Line1a);
361 parallel.SubtractVector(Line2a);
362 factor = parallel.ScalarProduct(&a)/a.Norm();
363 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
364 CopyVector(Line2a);
365 Log() << Verbose(1) << "Lines conincide." << endl;
366 return true;
367 } else {
368 parallel.CopyVector(Line1a);
369 parallel.SubtractVector(Line2b);
370 factor = parallel.ScalarProduct(&a)/a.Norm();
371 if ((factor >= -MYEPSILON) && (factor - 1. < MYEPSILON)) {
372 CopyVector(Line2b);
373 Log() << Verbose(1) << "Lines conincide." << endl;
374 return true;
375 }
376 }
377 Log() << Verbose(1) << "Lines are parallel." << endl;
378 Zero();
379 return false;
380 }
381
382 // obtain s
383 double s;
384 Vector temp1, temp2;
385 temp1.CopyVector(&c);
386 temp1.VectorProduct(&b);
387 temp2.CopyVector(&a);
388 temp2.VectorProduct(&b);
389 Log() << Verbose(1) << "INFO: temp1 = " << temp1 << ", temp2 = " << temp2 << "." << endl;
390 if (fabs(temp2.NormSquared()) > MYEPSILON)
391 s = temp1.ScalarProduct(&temp2)/temp2.NormSquared();
392 else
393 s = 0.;
394 Log() << Verbose(1) << "Factor s is " << temp1.ScalarProduct(&temp2) << "/" << temp2.NormSquared() << " = " << s << "." << endl;
395
396 // construct intersection
397 CopyVector(&a);
398 Scale(s);
399 AddVector(Line1a);
400 Log() << Verbose(1) << "Intersection is at " << *this << "." << endl;
401
402 return true;
403};
404
405/** Calculates the projection of a vector onto another \a *y.
406 * \param *y array to second vector
407 */
408void Vector::ProjectIt(const Vector * const y)
409{
410 Vector helper(*y);
411 helper.Scale(-(ScalarProduct(y)));
412 AddVector(&helper);
413};
414
415/** Calculates the projection of a vector onto another \a *y.
416 * \param *y array to second vector
417 * \return Vector
418 */
419Vector Vector::Projection(const Vector * const y) const
420{
421 Vector helper(*y);
422 helper.Scale((ScalarProduct(y)/y->NormSquared()));
423
424 return helper;
425};
426
427/** Calculates norm of this vector.
428 * \return \f$|x|\f$
429 */
430double Vector::Norm() const
431{
432 double res = 0.;
433 for (int i=NDIM;i--;)
434 res += this->x[i]*this->x[i];
435 return (sqrt(res));
436};
437
438/** Calculates squared norm of this vector.
439 * \return \f$|x|^2\f$
440 */
441double Vector::NormSquared() const
442{
443 return (ScalarProduct(this));
444};
445
446/** Normalizes this vector.
447 */
448void Vector::Normalize()
449{
450 double res = 0.;
451 for (int i=NDIM;i--;)
452 res += this->x[i]*this->x[i];
453 if (fabs(res) > MYEPSILON)
454 res = 1./sqrt(res);
455 Scale(&res);
456};
457
458/** Zeros all components of this vector.
459 */
460void Vector::Zero()
461{
462 for (int i=NDIM;i--;)
463 this->x[i] = 0.;
464};
465
466/** Zeros all components of this vector.
467 */
468void Vector::One(const double one)
469{
470 for (int i=NDIM;i--;)
471 this->x[i] = one;
472};
473
474/** Initialises all components of this vector.
475 */
476void Vector::Init(const double x1, const double x2, const double x3)
477{
478 x[0] = x1;
479 x[1] = x2;
480 x[2] = x3;
481};
482
483/** Checks whether vector has all components zero.
484 * @return true - vector is zero, false - vector is not
485 */
486bool Vector::IsZero() const
487{
488 return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
489};
490
491/** Checks whether vector has length of 1.
492 * @return true - vector is normalized, false - vector is not
493 */
494bool Vector::IsOne() const
495{
496 return (fabs(Norm() - 1.) < MYEPSILON);
497};
498
499/** Checks whether vector is normal to \a *normal.
500 * @return true - vector is normalized, false - vector is not
501 */
502bool Vector::IsNormalTo(const Vector * const normal) const
503{
504 if (ScalarProduct(normal) < MYEPSILON)
505 return true;
506 else
507 return false;
508};
509
510/** Checks whether vector is normal to \a *normal.
511 * @return true - vector is normalized, false - vector is not
512 */
513bool Vector::IsEqualTo(const Vector * const a) const
514{
515 bool status = true;
516 for (int i=0;i<NDIM;i++) {
517 if (fabs(x[i] - a->x[i]) > MYEPSILON)
518 status = false;
519 }
520 return status;
521};
522
523/** Calculates the angle between this and another vector.
524 * \param *y array to second vector
525 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
526 */
527double Vector::Angle(const Vector * const y) const
528{
529 double norm1 = Norm(), norm2 = y->Norm();
530 double angle = -1;
531 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
532 angle = this->ScalarProduct(y)/norm1/norm2;
533 // -1-MYEPSILON occured due to numerical imprecision, catch ...
534 //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
535 if (angle < -1)
536 angle = -1;
537 if (angle > 1)
538 angle = 1;
539 return acos(angle);
540};
541
542/** Rotates the vector relative to the origin around the axis given by \a *axis by an angle of \a alpha.
543 * \param *axis rotation axis
544 * \param alpha rotation angle in radian
545 */
546void Vector::RotateVector(const Vector * const axis, const double alpha)
547{
548 Vector a,y;
549 // normalise this vector with respect to axis
550 a.CopyVector(this);
551 a.ProjectOntoPlane(axis);
552 // construct normal vector
553 bool rotatable = y.MakeNormalVector(axis,&a);
554 // The normal vector cannot be created if there is linar dependency.
555 // Then the vector to rotate is on the axis and any rotation leads to the vector itself.
556 if (!rotatable) {
557 return;
558 }
559 y.Scale(Norm());
560 // scale normal vector by sine and this vector by cosine
561 y.Scale(sin(alpha));
562 a.Scale(cos(alpha));
563 CopyVector(Projection(axis));
564 // add scaled normal vector onto this vector
565 AddVector(&y);
566 // add part in axis direction
567 AddVector(&a);
568};
569
570/** Compares vector \a to vector \a b component-wise.
571 * \param a base vector
572 * \param b vector components to add
573 * \return a == b
574 */
575bool operator==(const Vector& a, const Vector& b)
576{
577 bool status = true;
578 for (int i=0;i<NDIM;i++)
579 status = status && (fabs(a.x[i] - b.x[i]) < MYEPSILON);
580 return status;
581};
582
583/** Sums vector \a to this lhs component-wise.
584 * \param a base vector
585 * \param b vector components to add
586 * \return lhs + a
587 */
588const Vector& operator+=(Vector& a, const Vector& b)
589{
590 a.AddVector(&b);
591 return a;
592};
593
594/** Subtracts vector \a from this lhs component-wise.
595 * \param a base vector
596 * \param b vector components to add
597 * \return lhs - a
598 */
599const Vector& operator-=(Vector& a, const Vector& b)
600{
601 a.SubtractVector(&b);
602 return a;
603};
604
605/** factor each component of \a a times a double \a m.
606 * \param a base vector
607 * \param m factor
608 * \return lhs.x[i] * m
609 */
610const Vector& operator*=(Vector& a, const double m)
611{
612 a.Scale(m);
613 return a;
614};
615
616/** Sums two vectors \a and \b component-wise.
617 * \param a first vector
618 * \param b second vector
619 * \return a + b
620 */
621Vector const operator+(const Vector& a, const Vector& b)
622{
623 Vector x(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 const operator-(const Vector& a, const Vector& b)
634{
635 Vector x(a);
636 x.SubtractVector(&b);
637 return x;
638};
639
640/** Factors given vector \a a times \a m.
641 * \param a vector
642 * \param m factor
643 * \return m * a
644 */
645Vector const operator*(const Vector& a, const double m)
646{
647 Vector x(a);
648 x.Scale(m);
649 return x;
650};
651
652/** Factors given vector \a a times \a m.
653 * \param m factor
654 * \param a vector
655 * \return m * a
656 */
657Vector const operator*(const double m, const Vector& a )
658{
659 Vector x(a);
660 x.Scale(m);
661 return x;
662};
663
664Vector& Vector::operator=(const Vector& src) {
665 CopyVector(src);
666 return *this;
667}
668
669double& Vector::operator[](int i){
670 assert(i<NDIM && "Invalid Vector dimension requested");
671 return x[i];
672}
673
674/** Prints a 3dim vector.
675 * prints no end of line.
676 */
677void Vector::Output() const
678{
679 Log() << Verbose(0) << "(";
680 for (int i=0;i<NDIM;i++) {
681 Log() << Verbose(0) << x[i];
682 if (i != 2)
683 Log() << Verbose(0) << ",";
684 }
685 Log() << Verbose(0) << ")";
686};
687
688ostream& operator<<(ostream& ost, const Vector& m)
689{
690 ost << "(";
691 for (int i=0;i<NDIM;i++) {
692 ost << m.x[i];
693 if (i != 2)
694 ost << ",";
695 }
696 ost << ")";
697 return ost;
698};
699
700/** Scales each atom coordinate by an individual \a factor.
701 * \param *factor pointer to scaling factor
702 */
703void Vector::Scale(const double ** const factor)
704{
705 for (int i=NDIM;i--;)
706 x[i] *= (*factor)[i];
707};
708
709void Vector::Scale(const double * const factor)
710{
711 for (int i=NDIM;i--;)
712 x[i] *= *factor;
713};
714
715void Vector::Scale(const double factor)
716{
717 for (int i=NDIM;i--;)
718 x[i] *= factor;
719};
720
721/** Translate atom by given vector.
722 * \param trans[] translation vector.
723 */
724void Vector::Translate(const Vector * const trans)
725{
726 for (int i=NDIM;i--;)
727 x[i] += trans->x[i];
728};
729
730/** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
731 * \param *M matrix of box
732 * \param *Minv inverse matrix
733 */
734void Vector::WrapPeriodically(const double * const M, const double * const Minv)
735{
736 MatrixMultiplication(Minv);
737 // truncate to [0,1] for each axis
738 for (int i=0;i<NDIM;i++) {
739 x[i] += 0.5; // set to center of box
740 while (x[i] >= 1.)
741 x[i] -= 1.;
742 while (x[i] < 0.)
743 x[i] += 1.;
744 }
745 MatrixMultiplication(M);
746};
747
748/** Do a matrix multiplication.
749 * \param *matrix NDIM_NDIM array
750 */
751void Vector::MatrixMultiplication(const double * const M)
752{
753 Vector C;
754 // do the matrix multiplication
755 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
756 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
757 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
758 // transfer the result into this
759 for (int i=NDIM;i--;)
760 x[i] = C.x[i];
761};
762
763/** Do a matrix multiplication with the \a *A' inverse.
764 * \param *matrix NDIM_NDIM array
765 */
766void Vector::InverseMatrixMultiplication(const double * const A)
767{
768 Vector C;
769 double B[NDIM*NDIM];
770 double detA = RDET3(A);
771 double detAReci;
772
773 // calculate the inverse B
774 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
775 detAReci = 1./detA;
776 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
777 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
778 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
779 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
780 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
781 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
782 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
783 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
784 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
785
786 // do the matrix multiplication
787 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
788 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
789 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
790 // transfer the result into this
791 for (int i=NDIM;i--;)
792 x[i] = C.x[i];
793 } else {
794 eLog() << Verbose(1) << "inverse of matrix does not exists: det A = " << detA << "." << endl;
795 }
796};
797
798
799/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
800 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
801 * \param *x1 first vector
802 * \param *x2 second vector
803 * \param *x3 third vector
804 * \param *factors three-component vector with the factor for each given vector
805 */
806void Vector::LinearCombinationOfVectors(const Vector * const x1, const Vector * const x2, const Vector * const x3, const double * const factors)
807{
808 for(int i=NDIM;i--;)
809 x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
810};
811
812/** Mirrors atom against a given plane.
813 * \param n[] normal vector of mirror plane.
814 */
815void Vector::Mirror(const Vector * const n)
816{
817 double projection;
818 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
819 // withdraw projected vector twice from original one
820 Log() << Verbose(1) << "Vector: ";
821 Output();
822 Log() << Verbose(0) << "\t";
823 for (int i=NDIM;i--;)
824 x[i] -= 2.*projection*n->x[i];
825 Log() << Verbose(0) << "Projected vector: ";
826 Output();
827 Log() << Verbose(0) << endl;
828};
829
830/** Calculates normal vector for three given vectors (being three points in space).
831 * Makes this vector orthonormal to the three given points, making up a place in 3d space.
832 * \param *y1 first vector
833 * \param *y2 second vector
834 * \param *y3 third vector
835 * \return true - success, vectors are linear independent, false - failure due to linear dependency
836 */
837bool Vector::MakeNormalVector(const Vector * const y1, const Vector * const y2, const Vector * const y3)
838{
839 Vector x1, x2;
840
841 x1.CopyVector(y1);
842 x1.SubtractVector(y2);
843 x2.CopyVector(y3);
844 x2.SubtractVector(y2);
845 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
846 eLog() << Verbose(2) << "Given vectors are linear dependent." << endl;
847 return false;
848 }
849// Log() << Verbose(4) << "relative, first plane coordinates:";
850// x1.Output((ofstream *)&cout);
851// Log() << Verbose(0) << endl;
852// Log() << Verbose(4) << "second plane coordinates:";
853// x2.Output((ofstream *)&cout);
854// Log() << Verbose(0) << endl;
855
856 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
857 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
858 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
859 Normalize();
860
861 return true;
862};
863
864
865/** Calculates orthonormal vector to two given vectors.
866 * Makes this vector orthonormal to two given vectors. This is very similar to the other
867 * vector::MakeNormalVector(), only there three points whereas here two difference
868 * vectors are given.
869 * \param *x1 first vector
870 * \param *x2 second vector
871 * \return true - success, vectors are linear independent, false - failure due to linear dependency
872 */
873bool Vector::MakeNormalVector(const Vector * const y1, const Vector * const y2)
874{
875 Vector x1,x2;
876 x1.CopyVector(y1);
877 x2.CopyVector(y2);
878 Zero();
879 if ((fabs(x1.Norm()) < MYEPSILON) || (fabs(x2.Norm()) < MYEPSILON) || (fabs(x1.Angle(&x2)) < MYEPSILON)) {
880 eLog() << Verbose(2) << "Given vectors are linear dependent." << endl;
881 return false;
882 }
883// Log() << Verbose(4) << "relative, first plane coordinates:";
884// x1.Output((ofstream *)&cout);
885// Log() << Verbose(0) << endl;
886// Log() << Verbose(4) << "second plane coordinates:";
887// x2.Output((ofstream *)&cout);
888// Log() << Verbose(0) << endl;
889
890 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
891 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
892 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
893 Normalize();
894
895 return true;
896};
897
898/** Calculates orthonormal vector to one given vectors.
899 * Just subtracts the projection onto the given vector from this vector.
900 * The removed part of the vector is Vector::Projection()
901 * \param *x1 vector
902 * \return true - success, false - vector is zero
903 */
904bool Vector::MakeNormalVector(const Vector * const y1)
905{
906 bool result = false;
907 double factor = y1->ScalarProduct(this)/y1->NormSquared();
908 Vector x1;
909 x1.CopyVector(y1);
910 x1.Scale(factor);
911 SubtractVector(&x1);
912 for (int i=NDIM;i--;)
913 result = result || (fabs(x[i]) > MYEPSILON);
914
915 return result;
916};
917
918/** Creates this vector as one of the possible orthonormal ones to the given one.
919 * Just scan how many components of given *vector are unequal to zero and
920 * try to get the skp of both to be zero accordingly.
921 * \param *vector given vector
922 * \return true - success, false - failure (null vector given)
923 */
924bool Vector::GetOneNormalVector(const Vector * const GivenVector)
925{
926 int Components[NDIM]; // contains indices of non-zero components
927 int Last = 0; // count the number of non-zero entries in vector
928 int j; // loop variables
929 double norm;
930
931 Log() << Verbose(4);
932 GivenVector->Output();
933 Log() << Verbose(0) << endl;
934 for (j=NDIM;j--;)
935 Components[j] = -1;
936 // find two components != 0
937 for (j=0;j<NDIM;j++)
938 if (fabs(GivenVector->x[j]) > MYEPSILON)
939 Components[Last++] = j;
940 Log() << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
941
942 switch(Last) {
943 case 3: // threecomponent system
944 case 2: // two component system
945 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
946 x[Components[2]] = 0.;
947 // in skp both remaining parts shall become zero but with opposite sign and third is zero
948 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
949 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
950 return true;
951 break;
952 case 1: // one component system
953 // set sole non-zero component to 0, and one of the other zero component pendants to 1
954 x[(Components[0]+2)%NDIM] = 0.;
955 x[(Components[0]+1)%NDIM] = 1.;
956 x[Components[0]] = 0.;
957 return true;
958 break;
959 default:
960 return false;
961 }
962};
963
964/** Determines parameter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
965 * \param *A first plane vector
966 * \param *B second plane vector
967 * \param *C third plane vector
968 * \return scaling parameter for this vector
969 */
970double Vector::CutsPlaneAt(const Vector * const A, const Vector * const B, const Vector * const C) const
971{
972// Log() << Verbose(3) << "For comparison: ";
973// Log() << Verbose(0) << "A " << A->Projection(this) << "\t";
974// Log() << Verbose(0) << "B " << B->Projection(this) << "\t";
975// Log() << Verbose(0) << "C " << C->Projection(this) << "\t";
976// Log() << Verbose(0) << endl;
977 return A->ScalarProduct(this);
978};
979
980/** Creates a new vector as the one with least square distance to a given set of \a vectors.
981 * \param *vectors set of vectors
982 * \param num number of vectors
983 * \return true if success, false if failed due to linear dependency
984 */
985bool Vector::LSQdistance(const Vector **vectors, int num)
986{
987 int j;
988
989 for (j=0;j<num;j++) {
990 Log() << Verbose(1) << j << "th atom's vector: ";
991 (vectors[j])->Output();
992 Log() << Verbose(0) << endl;
993 }
994
995 int np = 3;
996 struct LSQ_params par;
997
998 const gsl_multimin_fminimizer_type *T =
999 gsl_multimin_fminimizer_nmsimplex;
1000 gsl_multimin_fminimizer *s = NULL;
1001 gsl_vector *ss, *y;
1002 gsl_multimin_function minex_func;
1003
1004 size_t iter = 0, i;
1005 int status;
1006 double size;
1007
1008 /* Initial vertex size vector */
1009 ss = gsl_vector_alloc (np);
1010 y = gsl_vector_alloc (np);
1011
1012 /* Set all step sizes to 1 */
1013 gsl_vector_set_all (ss, 1.0);
1014
1015 /* Starting point */
1016 par.vectors = vectors;
1017 par.num = num;
1018
1019 for (i=NDIM;i--;)
1020 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
1021
1022 /* Initialize method and iterate */
1023 minex_func.f = &LSQ;
1024 minex_func.n = np;
1025 minex_func.params = (void *)&par;
1026
1027 s = gsl_multimin_fminimizer_alloc (T, np);
1028 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
1029
1030 do
1031 {
1032 iter++;
1033 status = gsl_multimin_fminimizer_iterate(s);
1034
1035 if (status)
1036 break;
1037
1038 size = gsl_multimin_fminimizer_size (s);
1039 status = gsl_multimin_test_size (size, 1e-2);
1040
1041 if (status == GSL_SUCCESS)
1042 {
1043 printf ("converged to minimum at\n");
1044 }
1045
1046 printf ("%5d ", (int)iter);
1047 for (i = 0; i < (size_t)np; i++)
1048 {
1049 printf ("%10.3e ", gsl_vector_get (s->x, i));
1050 }
1051 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
1052 }
1053 while (status == GSL_CONTINUE && iter < 100);
1054
1055 for (i=(size_t)np;i--;)
1056 this->x[i] = gsl_vector_get(s->x, i);
1057 gsl_vector_free(y);
1058 gsl_vector_free(ss);
1059 gsl_multimin_fminimizer_free (s);
1060
1061 return true;
1062};
1063
1064/** Adds vector \a *y componentwise.
1065 * \param *y vector
1066 */
1067void Vector::AddVector(const Vector * const y)
1068{
1069 for (int i=NDIM;i--;)
1070 this->x[i] += y->x[i];
1071}
1072
1073/** Adds vector \a *y componentwise.
1074 * \param *y vector
1075 */
1076void Vector::SubtractVector(const Vector * const y)
1077{
1078 for (int i=NDIM;i--;)
1079 this->x[i] -= y->x[i];
1080}
1081
1082/** Copy vector \a *y componentwise.
1083 * \param *y vector
1084 */
1085void Vector::CopyVector(const Vector * const y)
1086{
1087 // check for self assignment
1088 if(y!=this){
1089 for (int i=NDIM;i--;)
1090 this->x[i] = y->x[i];
1091 }
1092}
1093
1094/** Copy vector \a y componentwise.
1095 * \param y vector
1096 */
1097void Vector::CopyVector(const Vector &y)
1098{
1099 // check for self assignment
1100 if(&y!=this) {
1101 for (int i=NDIM;i--;)
1102 this->x[i] = y.x[i];
1103 }
1104}
1105
1106
1107/** Asks for position, checks for boundary.
1108 * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
1109 * \param check whether bounds shall be checked (true) or not (false)
1110 */
1111void Vector::AskPosition(const double * const cell_size, const bool check)
1112{
1113 char coords[3] = {'x','y','z'};
1114 int j = -1;
1115 for (int i=0;i<3;i++) {
1116 j += i+1;
1117 do {
1118 Log() << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
1119 cin >> x[i];
1120 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
1121 }
1122};
1123
1124/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
1125 * This is linear system of equations to be solved, however of the three given (skp of this vector\
1126 * with either of the three hast to be zero) only two are linear independent. The third equation
1127 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
1128 * where very often it has to be checked whether a certain value is zero or not and thus forked into
1129 * another case.
1130 * \param *x1 first vector
1131 * \param *x2 second vector
1132 * \param *y third vector
1133 * \param alpha first angle
1134 * \param beta second angle
1135 * \param c norm of final vector
1136 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
1137 * \bug this is not yet working properly
1138 */
1139bool Vector::SolveSystem(Vector * x1, Vector * x2, Vector * y, const double alpha, const double beta, const double c)
1140{
1141 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
1142 double ang; // angle on testing
1143 double sign[3];
1144 int i,j,k;
1145 A = cos(alpha) * x1->Norm() * c;
1146 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
1147 B2 = cos(beta) * x2->Norm() * c;
1148 C = c * c;
1149 Log() << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
1150 int flag = 0;
1151 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
1152 if (fabs(x1->x[1]) > MYEPSILON) {
1153 flag = 1;
1154 } else if (fabs(x1->x[2]) > MYEPSILON) {
1155 flag = 2;
1156 } else {
1157 return false;
1158 }
1159 }
1160 switch (flag) {
1161 default:
1162 case 0:
1163 break;
1164 case 2:
1165 flip(x1->x[0],x1->x[1]);
1166 flip(x2->x[0],x2->x[1]);
1167 flip(y->x[0],y->x[1]);
1168 //flip(x[0],x[1]);
1169 flip(x1->x[1],x1->x[2]);
1170 flip(x2->x[1],x2->x[2]);
1171 flip(y->x[1],y->x[2]);
1172 //flip(x[1],x[2]);
1173 case 1:
1174 flip(x1->x[0],x1->x[1]);
1175 flip(x2->x[0],x2->x[1]);
1176 flip(y->x[0],y->x[1]);
1177 //flip(x[0],x[1]);
1178 flip(x1->x[1],x1->x[2]);
1179 flip(x2->x[1],x2->x[2]);
1180 flip(y->x[1],y->x[2]);
1181 //flip(x[1],x[2]);
1182 break;
1183 }
1184 // now comes the case system
1185 D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
1186 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
1187 D3 = y->x[0]/x1->x[0]*A-B1;
1188 Log() << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
1189 if (fabs(D1) < MYEPSILON) {
1190 Log() << Verbose(2) << "D1 == 0!\n";
1191 if (fabs(D2) > MYEPSILON) {
1192 Log() << Verbose(3) << "D2 != 0!\n";
1193 x[2] = -D3/D2;
1194 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
1195 E2 = -x1->x[1]/x1->x[0];
1196 Log() << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1197 F1 = E1*E1 + 1.;
1198 F2 = -E1*E2;
1199 F3 = E1*E1 + D3*D3/(D2*D2) - C;
1200 Log() << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1201 if (fabs(F1) < MYEPSILON) {
1202 Log() << Verbose(4) << "F1 == 0!\n";
1203 Log() << Verbose(4) << "Gleichungssystem linear\n";
1204 x[1] = F3/(2.*F2);
1205 } else {
1206 p = F2/F1;
1207 q = p*p - F3/F1;
1208 Log() << Verbose(4) << "p " << p << "\tq " << q << endl;
1209 if (q < 0) {
1210 Log() << Verbose(4) << "q < 0" << endl;
1211 return false;
1212 }
1213 x[1] = p + sqrt(q);
1214 }
1215 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1216 } else {
1217 Log() << Verbose(2) << "Gleichungssystem unterbestimmt\n";
1218 return false;
1219 }
1220 } else {
1221 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
1222 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
1223 Log() << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
1224 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
1225 F2 = -(E1*E2 + D2*D3/(D1*D1));
1226 F3 = E1*E1 + D3*D3/(D1*D1) - C;
1227 Log() << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
1228 if (fabs(F1) < MYEPSILON) {
1229 Log() << Verbose(3) << "F1 == 0!\n";
1230 Log() << Verbose(3) << "Gleichungssystem linear\n";
1231 x[2] = F3/(2.*F2);
1232 } else {
1233 p = F2/F1;
1234 q = p*p - F3/F1;
1235 Log() << Verbose(3) << "p " << p << "\tq " << q << endl;
1236 if (q < 0) {
1237 Log() << Verbose(3) << "q < 0" << endl;
1238 return false;
1239 }
1240 x[2] = p + sqrt(q);
1241 }
1242 x[1] = (-D2 * x[2] - D3)/D1;
1243 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
1244 }
1245 switch (flag) { // back-flipping
1246 default:
1247 case 0:
1248 break;
1249 case 2:
1250 flip(x1->x[0],x1->x[1]);
1251 flip(x2->x[0],x2->x[1]);
1252 flip(y->x[0],y->x[1]);
1253 flip(x[0],x[1]);
1254 flip(x1->x[1],x1->x[2]);
1255 flip(x2->x[1],x2->x[2]);
1256 flip(y->x[1],y->x[2]);
1257 flip(x[1],x[2]);
1258 case 1:
1259 flip(x1->x[0],x1->x[1]);
1260 flip(x2->x[0],x2->x[1]);
1261 flip(y->x[0],y->x[1]);
1262 //flip(x[0],x[1]);
1263 flip(x1->x[1],x1->x[2]);
1264 flip(x2->x[1],x2->x[2]);
1265 flip(y->x[1],y->x[2]);
1266 flip(x[1],x[2]);
1267 break;
1268 }
1269 // one z component is only determined by its radius (without sign)
1270 // thus check eight possible sign flips and determine by checking angle with second vector
1271 for (i=0;i<8;i++) {
1272 // set sign vector accordingly
1273 for (j=2;j>=0;j--) {
1274 k = (i & pot(2,j)) << j;
1275 Log() << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
1276 sign[j] = (k == 0) ? 1. : -1.;
1277 }
1278 Log() << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
1279 // apply sign matrix
1280 for (j=NDIM;j--;)
1281 x[j] *= sign[j];
1282 // calculate angle and check
1283 ang = x2->Angle (this);
1284 Log() << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
1285 if (fabs(ang - cos(beta)) < MYEPSILON) {
1286 break;
1287 }
1288 // unapply sign matrix (is its own inverse)
1289 for (j=NDIM;j--;)
1290 x[j] *= sign[j];
1291 }
1292 return true;
1293};
1294
1295/**
1296 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
1297 * their offset.
1298 *
1299 * @param offest for the origin of the parallelepiped
1300 * @param three vectors forming the matrix that defines the shape of the parallelpiped
1301 */
1302bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
1303{
1304 Vector a;
1305 a.CopyVector(this);
1306 a.SubtractVector(&offset);
1307 a.InverseMatrixMultiplication(parallelepiped);
1308 bool isInside = true;
1309
1310 for (int i=NDIM;i--;)
1311 isInside = isInside && ((a.x[i] <= 1) && (a.x[i] >= 0));
1312
1313 return isInside;
1314}
Note: See TracBrowser for help on using the repository browser.