source: src/vector.cpp@ 7326b2

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

Periodic variants of AnalysisPair...() implemented.

  • BUGFIX: Vector::DistanceToPlane() - we did not check whether sign is 0.
  • NOTE: Due to changes in Vector::DistanceToPlane() with sign, Unit test AnalysisCorrelationToSurfaceUnitTest had to be changed:
    • we now have bin -0.5 filled with 1 (instead of 0.) and -0.288 instead of 0.288
    • find() replaced by lower_bound
  • new functions: PeriodicPairCorrelation(), PeriodicCorrelationToPoint(), PeriodicCorrelationToSurface()
    • each has a ranges[NDIM] argument with specifies the neighbours to scan: [ -ranges[i], ranges[i] ]
    • the atom::node is periodically translated to each periodic cell and the distance calculated.
  • NOTE: make check was broken before due to implementation of Periodic variants as normal ones (yielding more points that expected in unit test).

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

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