source: src/vector.cpp@ cd032d

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 cd032d was 2561df, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'MenuRefactoring' into QT4Refactoring

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/unittests/Makefile.am
molecuilder/src/vector.cpp
molecuilder/src/vector.hpp

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