source: src/vector.cpp@ 8db598

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 8db598 was 821907, checked in by Frederik Heber <heber@…>, 15 years ago

Added Vector Copy constructors, Vector::GetDistanceVectorToPlane() and Vector::DistanceToVector() makes use of it

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