source: src/vector.cpp@ 3c349b

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

Huge refactoring to make const what is const (ticket #38), continued.

  • too many changes because of too many cross-references to be able to list them up here.
  • NOTE that "make check" runs fine and did catch several error.
  • note that we had to use const_iterator several times when the map, ... was declared const.
  • at times we changed an allocated LinkedCell LCList(...) into

const LinkedCell *LCList;
LCList = new LinkedCell(...);

  • also mutable (see ticket #5) was used, e.g. for molecule::InternalPointer (PointCloud changes are allowed, because they are just accounting).

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

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