source: src/vector.cpp@ fc3b67

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 Candidate_v1.7.0 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 fc3b67 was 97498a, checked in by Frederik Heber <heber@…>, 16 years ago

Attempt to fix the tesselation::IsInnerPoint().

We try the IsInnerPoint() as follows:

  1. Find nearest BoundaryPoints - working
  2. Find Closest BoundaryLine's - working
  3. Find closest Triangle that is well aligned (wrt to NormalVector and Distance) - unsure whether correctly working
  4. Check whether alignment is on boundary or inside/outside - working
  5. If on boundary, we check whether it's inside of triangle by intersecting with boundary lines - not working

Hence, we code a wrapper for GSL routines, to - finally - allow for solution of linear system of equations.

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

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