source: src/vector.cpp@ 28c351

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 28c351 was 5f612ee, checked in by Frederik Heber <heber@…>, 15 years ago

Merge branch 'Analysis_PairCorrelation' into StructureRefactoring

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/World.cpp
molecuilder/src/World.hpp
molecuilder/src/boundary.cpp
molecuilder/src/builder.cpp
molecuilder/src/log.cpp
molecuilder/src/moleculelist.cpp
molecuilder/src/periodentafel.cpp
molecuilder/src/tesselation.cpp
molecuilder/src/unittests/AnalysisCorrelationToSurfaceUnitTest.cpp
molecuilder/src/unittests/Makefile.am
molecuilder/src/unittests/bondgraphunittest.cpp
molecuilder/src/unittests/gslvectorunittest.cpp
molecuilder/src/unittests/logunittest.cpp
molecuilder/src/unittests/tesselation_boundarytriangleunittest.hpp
molecuilder/src/vector.cpp
molecuilder/tests/Tesselations/defs.in

Conflicts have been many and too numerous to listen here, just the few general cases

  • new molecule() replaced by World::getInstance().createMolecule()
  • new atom() replaced by World::getInstance().createAtom() where appropriate.
  • Some DoLog()s added interfered with changes to the message produced by Log() << Verbose(.) << ...
  • DoLog() has been erroneously added to TestRunner.cpp as well, there cout is appropriate
  • ...

Additionally, there was a bug in atom::clone(), sort was set to atom::nr of the atom to clone not of the clone itself. This caused a failure of the fragmentation.

This merge has been fully checked from a clean build directory with subsequent configure,make all install and make check.
It configures, compiles and runs all test cases and the test suite without errors.

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

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