source: src/vector.cpp@ 0a4f7f

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

Made data internal data-structure of vector class private

  • Replaced occurences of access to internals with operator
  • moved Vector-class into LinAlg-Module
  • Reworked Vector to allow clean modularization
  • Added Plane class to describe arbitrary planes in 3d space
  • Property mode set to 100644
File size: 26.4 KB
Line 
1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
7
8#include "defs.hpp"
9#include "gslmatrix.hpp"
10#include "leastsquaremin.hpp"
11#include "memoryallocator.hpp"
12#include "vector.hpp"
13#include "Helpers/fast_functions.hpp"
14#include "Helpers/Assert.hpp"
15#include "Plane.hpp"
16#include "Exceptions/LinearDependenceException.hpp"
17
18#include <gsl/gsl_linalg.h>
19#include <gsl/gsl_matrix.h>
20#include <gsl/gsl_permutation.h>
21#include <gsl/gsl_vector.h>
22
23/************************************ Functions for class vector ************************************/
24
25/** Constructor of class vector.
26 */
27Vector::Vector()
28{
29 x[0] = x[1] = x[2] = 0.;
30};
31
32/** Constructor of class vector.
33 */
34Vector::Vector(const double x1, const double x2, const double x3)
35{
36 x[0] = x1;
37 x[1] = x2;
38 x[2] = x3;
39};
40
41/**
42 * Copy constructor
43 */
44Vector::Vector(const Vector& src)
45{
46 x[0] = src[0];
47 x[1] = src[1];
48 x[2] = src[2];
49}
50
51/**
52 * Assignment operator
53 */
54Vector& Vector::operator=(const Vector& src){
55 // check for self assignment
56 if(&src!=this){
57 x[0] = src[0];
58 x[1] = src[1];
59 x[2] = src[2];
60 }
61 return *this;
62}
63
64/** Desctructor of class vector.
65 */
66Vector::~Vector() {};
67
68/** Calculates square of distance between this and another vector.
69 * \param *y array to second vector
70 * \return \f$| x - y |^2\f$
71 */
72double Vector::DistanceSquared(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 (res);
78};
79
80/** Calculates distance between this and another vector.
81 * \param *y array to second vector
82 * \return \f$| x - y |\f$
83 */
84double Vector::Distance(const Vector * const y) const
85{
86 double res = 0.;
87 for (int i=NDIM;i--;)
88 res += (x[i]-y->x[i])*(x[i]-y->x[i]);
89 return (sqrt(res));
90};
91
92/** Calculates distance between this and another vector in a periodic cell.
93 * \param *y array to second vector
94 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
95 * \return \f$| x - y |\f$
96 */
97double Vector::PeriodicDistance(const Vector * const y, const double * const cell_size) const
98{
99 double res = Distance(y), tmp, matrix[NDIM*NDIM];
100 Vector Shiftedy, TranslationVector;
101 int N[NDIM];
102 matrix[0] = cell_size[0];
103 matrix[1] = cell_size[1];
104 matrix[2] = cell_size[3];
105 matrix[3] = cell_size[1];
106 matrix[4] = cell_size[2];
107 matrix[5] = cell_size[4];
108 matrix[6] = cell_size[3];
109 matrix[7] = cell_size[4];
110 matrix[8] = cell_size[5];
111 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
112 for (N[0]=-1;N[0]<=1;N[0]++)
113 for (N[1]=-1;N[1]<=1;N[1]++)
114 for (N[2]=-1;N[2]<=1;N[2]++) {
115 // create the translation vector
116 TranslationVector.Zero();
117 for (int i=NDIM;i--;)
118 TranslationVector.x[i] = (double)N[i];
119 TranslationVector.MatrixMultiplication(matrix);
120 // add onto the original vector to compare with
121 Shiftedy.CopyVector(y);
122 Shiftedy.AddVector(&TranslationVector);
123 // get distance and compare with minimum so far
124 tmp = Distance(&Shiftedy);
125 if (tmp < res) res = tmp;
126 }
127 return (res);
128};
129
130/** Calculates distance between this and another vector in a periodic cell.
131 * \param *y array to second vector
132 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
133 * \return \f$| x - y |^2\f$
134 */
135double Vector::PeriodicDistanceSquared(const Vector * const y, const double * const cell_size) const
136{
137 double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
138 Vector Shiftedy, TranslationVector;
139 int N[NDIM];
140 matrix[0] = cell_size[0];
141 matrix[1] = cell_size[1];
142 matrix[2] = cell_size[3];
143 matrix[3] = cell_size[1];
144 matrix[4] = cell_size[2];
145 matrix[5] = cell_size[4];
146 matrix[6] = cell_size[3];
147 matrix[7] = cell_size[4];
148 matrix[8] = cell_size[5];
149 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
150 for (N[0]=-1;N[0]<=1;N[0]++)
151 for (N[1]=-1;N[1]<=1;N[1]++)
152 for (N[2]=-1;N[2]<=1;N[2]++) {
153 // create the translation vector
154 TranslationVector.Zero();
155 for (int i=NDIM;i--;)
156 TranslationVector.x[i] = (double)N[i];
157 TranslationVector.MatrixMultiplication(matrix);
158 // add onto the original vector to compare with
159 Shiftedy.CopyVector(y);
160 Shiftedy.AddVector(&TranslationVector);
161 // get distance and compare with minimum so far
162 tmp = DistanceSquared(&Shiftedy);
163 if (tmp < res) res = tmp;
164 }
165 return (res);
166};
167
168/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
169 * \param *out ofstream for debugging messages
170 * Tries to translate a vector into each adjacent neighbouring cell.
171 */
172void Vector::KeepPeriodic(const double * const matrix)
173{
174// int N[NDIM];
175// bool flag = false;
176 //vector Shifted, TranslationVector;
177 Vector TestVector;
178// Log() << Verbose(1) << "Begin of KeepPeriodic." << endl;
179// Log() << Verbose(2) << "Vector is: ";
180// Output(out);
181// Log() << Verbose(0) << endl;
182 TestVector.CopyVector(this);
183 TestVector.InverseMatrixMultiplication(matrix);
184 for(int i=NDIM;i--;) { // correct periodically
185 if (TestVector.x[i] < 0) { // get every coefficient into the interval [0,1)
186 TestVector.x[i] += ceil(TestVector.x[i]);
187 } else {
188 TestVector.x[i] -= floor(TestVector.x[i]);
189 }
190 }
191 TestVector.MatrixMultiplication(matrix);
192 CopyVector(&TestVector);
193// Log() << Verbose(2) << "New corrected vector is: ";
194// Output(out);
195// Log() << Verbose(0) << endl;
196// Log() << Verbose(1) << "End of KeepPeriodic." << endl;
197};
198
199/** Calculates scalar product between this and another vector.
200 * \param *y array to second vector
201 * \return \f$\langle x, y \rangle\f$
202 */
203double Vector::ScalarProduct(const Vector * const y) const
204{
205 double res = 0.;
206 for (int i=NDIM;i--;)
207 res += x[i]*y->x[i];
208 return (res);
209};
210
211
212/** Calculates VectorProduct between this and another vector.
213 * -# returns the Product in place of vector from which it was initiated
214 * -# ATTENTION: Only three dim.
215 * \param *y array to vector with which to calculate crossproduct
216 * \return \f$ x \times y \f&
217 */
218void Vector::VectorProduct(const Vector * const y)
219{
220 Vector tmp;
221 tmp.x[0] = x[1]* (y->x[2]) - x[2]* (y->x[1]);
222 tmp.x[1] = x[2]* (y->x[0]) - x[0]* (y->x[2]);
223 tmp.x[2] = x[0]* (y->x[1]) - x[1]* (y->x[0]);
224 this->CopyVector(&tmp);
225};
226
227
228/** projects this vector onto plane defined by \a *y.
229 * \param *y normal vector of plane
230 * \return \f$\langle x, y \rangle\f$
231 */
232void Vector::ProjectOntoPlane(const Vector * const y)
233{
234 Vector tmp;
235 tmp.CopyVector(y);
236 tmp.Normalize();
237 tmp.Scale(ScalarProduct(&tmp));
238 this->SubtractVector(&tmp);
239};
240
241/** Calculates the minimum distance of this vector to the plane.
242 * \param *out output stream for debugging
243 * \param *PlaneNormal normal of plane
244 * \param *PlaneOffset offset of plane
245 * \return distance to plane
246 */
247double Vector::DistanceToPlane(const Vector * const PlaneNormal, const Vector * const PlaneOffset) const
248{
249 Vector temp;
250
251 // first create part that is orthonormal to PlaneNormal with withdraw
252 temp.CopyVector(this);
253 temp.SubtractVector(PlaneOffset);
254 temp.MakeNormalTo(*PlaneNormal);
255 temp.Scale(-1.);
256 // then add connecting vector from plane to point
257 temp.AddVector(this);
258 temp.SubtractVector(PlaneOffset);
259 double sign = temp.ScalarProduct(PlaneNormal);
260 if (fabs(sign) > MYEPSILON)
261 sign /= fabs(sign);
262 else
263 sign = 0.;
264
265 return (temp.Norm()*sign);
266};
267
268/** Calculates the projection of a vector onto another \a *y.
269 * \param *y array to second vector
270 */
271void Vector::ProjectIt(const Vector * const y)
272{
273 Vector helper(*y);
274 helper.Scale(-(ScalarProduct(y)));
275 AddVector(&helper);
276};
277
278/** Calculates the projection of a vector onto another \a *y.
279 * \param *y array to second vector
280 * \return Vector
281 */
282Vector Vector::Projection(const Vector * const y) const
283{
284 Vector helper(*y);
285 helper.Scale((ScalarProduct(y)/y->NormSquared()));
286
287 return helper;
288};
289
290/** Calculates norm of this vector.
291 * \return \f$|x|\f$
292 */
293double Vector::Norm() const
294{
295 double res = 0.;
296 for (int i=NDIM;i--;)
297 res += this->x[i]*this->x[i];
298 return (sqrt(res));
299};
300
301/** Calculates squared norm of this vector.
302 * \return \f$|x|^2\f$
303 */
304double Vector::NormSquared() const
305{
306 return (ScalarProduct(this));
307};
308
309/** Normalizes this vector.
310 */
311void Vector::Normalize()
312{
313 double res = 0.;
314 for (int i=NDIM;i--;)
315 res += this->x[i]*this->x[i];
316 if (fabs(res) > MYEPSILON)
317 res = 1./sqrt(res);
318 Scale(&res);
319};
320
321/** Zeros all components of this vector.
322 */
323void Vector::Zero()
324{
325 for (int i=NDIM;i--;)
326 this->x[i] = 0.;
327};
328
329/** Zeros all components of this vector.
330 */
331void Vector::One(const double one)
332{
333 for (int i=NDIM;i--;)
334 this->x[i] = one;
335};
336
337/** Initialises all components of this vector.
338 */
339void Vector::Init(const double x1, const double x2, const double x3)
340{
341 x[0] = x1;
342 x[1] = x2;
343 x[2] = x3;
344};
345
346/** Checks whether vector has all components zero.
347 * @return true - vector is zero, false - vector is not
348 */
349bool Vector::IsZero() const
350{
351 return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
352};
353
354/** Checks whether vector has length of 1.
355 * @return true - vector is normalized, false - vector is not
356 */
357bool Vector::IsOne() const
358{
359 return (fabs(Norm() - 1.) < MYEPSILON);
360};
361
362/** Checks whether vector is normal to \a *normal.
363 * @return true - vector is normalized, false - vector is not
364 */
365bool Vector::IsNormalTo(const Vector * const normal) const
366{
367 if (ScalarProduct(normal) < MYEPSILON)
368 return true;
369 else
370 return false;
371};
372
373/** Checks whether vector is normal to \a *normal.
374 * @return true - vector is normalized, false - vector is not
375 */
376bool Vector::IsEqualTo(const Vector * const a) const
377{
378 bool status = true;
379 for (int i=0;i<NDIM;i++) {
380 if (fabs(x[i] - a->x[i]) > MYEPSILON)
381 status = false;
382 }
383 return status;
384};
385
386/** Calculates the angle between this and another vector.
387 * \param *y array to second vector
388 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
389 */
390double Vector::Angle(const Vector * const y) const
391{
392 double norm1 = Norm(), norm2 = y->Norm();
393 double angle = -1;
394 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
395 angle = this->ScalarProduct(y)/norm1/norm2;
396 // -1-MYEPSILON occured due to numerical imprecision, catch ...
397 //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
398 if (angle < -1)
399 angle = -1;
400 if (angle > 1)
401 angle = 1;
402 return acos(angle);
403};
404
405
406double& Vector::operator[](size_t i){
407 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
408 return x[i];
409}
410
411const double& Vector::operator[](size_t i) const{
412 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
413 return x[i];
414}
415
416double& Vector::at(size_t i){
417 return (*this)[i];
418}
419
420const double& Vector::at(size_t i) const{
421 return (*this)[i];
422}
423
424double* Vector::get(){
425 return x;
426}
427
428/** Compares vector \a to vector \a b component-wise.
429 * \param a base vector
430 * \param b vector components to add
431 * \return a == b
432 */
433bool operator==(const Vector& a, const Vector& b)
434{
435 bool status = true;
436 for (int i=0;i<NDIM;i++)
437 status = status && (fabs(a[i] - b[i]) < MYEPSILON);
438 return status;
439};
440
441/** Sums vector \a to this lhs component-wise.
442 * \param a base vector
443 * \param b vector components to add
444 * \return lhs + a
445 */
446const Vector& operator+=(Vector& a, const Vector& b)
447{
448 a.AddVector(&b);
449 return a;
450};
451
452/** Subtracts vector \a from this lhs component-wise.
453 * \param a base vector
454 * \param b vector components to add
455 * \return lhs - a
456 */
457const Vector& operator-=(Vector& a, const Vector& b)
458{
459 a.SubtractVector(&b);
460 return a;
461};
462
463/** factor each component of \a a times a double \a m.
464 * \param a base vector
465 * \param m factor
466 * \return lhs.x[i] * m
467 */
468const Vector& operator*=(Vector& a, const double m)
469{
470 a.Scale(m);
471 return a;
472};
473
474/** Sums two vectors \a and \b component-wise.
475 * \param a first vector
476 * \param b second vector
477 * \return a + b
478 */
479Vector const operator+(const Vector& a, const Vector& b)
480{
481 Vector x(a);
482 x.AddVector(&b);
483 return x;
484};
485
486/** Subtracts vector \a from \b component-wise.
487 * \param a first vector
488 * \param b second vector
489 * \return a - b
490 */
491Vector const operator-(const Vector& a, const Vector& b)
492{
493 Vector x(a);
494 x.SubtractVector(&b);
495 return x;
496};
497
498/** Factors given vector \a a times \a m.
499 * \param a vector
500 * \param m factor
501 * \return m * a
502 */
503Vector const operator*(const Vector& a, const double m)
504{
505 Vector x(a);
506 x.Scale(m);
507 return x;
508};
509
510/** Factors given vector \a a times \a m.
511 * \param m factor
512 * \param a vector
513 * \return m * a
514 */
515Vector const operator*(const double m, const Vector& a )
516{
517 Vector x(a);
518 x.Scale(m);
519 return x;
520};
521
522ostream& operator<<(ostream& ost, const Vector& m)
523{
524 ost << "(";
525 for (int i=0;i<NDIM;i++) {
526 ost << m[i];
527 if (i != 2)
528 ost << ",";
529 }
530 ost << ")";
531 return ost;
532};
533
534/** Scales each atom coordinate by an individual \a factor.
535 * \param *factor pointer to scaling factor
536 */
537void Vector::Scale(const double ** const factor)
538{
539 for (int i=NDIM;i--;)
540 x[i] *= (*factor)[i];
541};
542
543void Vector::Scale(const double * const factor)
544{
545 for (int i=NDIM;i--;)
546 x[i] *= *factor;
547};
548
549void Vector::Scale(const double factor)
550{
551 for (int i=NDIM;i--;)
552 x[i] *= factor;
553};
554
555/** Translate atom by given vector.
556 * \param trans[] translation vector.
557 */
558void Vector::Translate(const Vector * const trans)
559{
560 for (int i=NDIM;i--;)
561 x[i] += trans->x[i];
562};
563
564/** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
565 * \param *M matrix of box
566 * \param *Minv inverse matrix
567 */
568void Vector::WrapPeriodically(const double * const M, const double * const Minv)
569{
570 MatrixMultiplication(Minv);
571 // truncate to [0,1] for each axis
572 for (int i=0;i<NDIM;i++) {
573 x[i] += 0.5; // set to center of box
574 while (x[i] >= 1.)
575 x[i] -= 1.;
576 while (x[i] < 0.)
577 x[i] += 1.;
578 }
579 MatrixMultiplication(M);
580};
581
582/** Do a matrix multiplication.
583 * \param *matrix NDIM_NDIM array
584 */
585void Vector::MatrixMultiplication(const double * const M)
586{
587 Vector C;
588 // do the matrix multiplication
589 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
590 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
591 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
592 // transfer the result into this
593 for (int i=NDIM;i--;)
594 x[i] = C.x[i];
595};
596
597/** Do a matrix multiplication with the \a *A' inverse.
598 * \param *matrix NDIM_NDIM array
599 */
600bool Vector::InverseMatrixMultiplication(const double * const A)
601{
602 Vector C;
603 double B[NDIM*NDIM];
604 double detA = RDET3(A);
605 double detAReci;
606
607 // calculate the inverse B
608 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
609 detAReci = 1./detA;
610 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
611 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
612 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
613 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
614 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
615 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
616 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
617 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
618 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
619
620 // do the matrix multiplication
621 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
622 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
623 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
624 // transfer the result into this
625 for (int i=NDIM;i--;)
626 x[i] = C.x[i];
627 return true;
628 } else {
629 return false;
630 }
631};
632
633
634/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
635 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
636 * \param *x1 first vector
637 * \param *x2 second vector
638 * \param *x3 third vector
639 * \param *factors three-component vector with the factor for each given vector
640 */
641void Vector::LinearCombinationOfVectors(const Vector * const x1, const Vector * const x2, const Vector * const x3, const double * const factors)
642{
643 for(int i=NDIM;i--;)
644 x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
645};
646
647/** Mirrors atom against a given plane.
648 * \param n[] normal vector of mirror plane.
649 */
650void Vector::Mirror(const Vector * const n)
651{
652 double projection;
653 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
654 // withdraw projected vector twice from original one
655 for (int i=NDIM;i--;)
656 x[i] -= 2.*projection*n->x[i];
657};
658
659
660/** Calculates orthonormal vector to one given vector.
661 * Just subtracts the projection onto the given vector from this vector.
662 * The removed part of the vector is Vector::Projection()
663 * \param *x1 vector
664 * \return true - success, false - vector is zero
665 */
666bool Vector::MakeNormalTo(const Vector &y1)
667{
668 bool result = false;
669 double factor = y1.ScalarProduct(this)/y1.NormSquared();
670 Vector x1;
671 x1.CopyVector(&y1);
672 x1.Scale(factor);
673 SubtractVector(&x1);
674 for (int i=NDIM;i--;)
675 result = result || (fabs(x[i]) > MYEPSILON);
676
677 return result;
678};
679
680/** Creates this vector as one of the possible orthonormal ones to the given one.
681 * Just scan how many components of given *vector are unequal to zero and
682 * try to get the skp of both to be zero accordingly.
683 * \param *vector given vector
684 * \return true - success, false - failure (null vector given)
685 */
686bool Vector::GetOneNormalVector(const Vector * const GivenVector)
687{
688 int Components[NDIM]; // contains indices of non-zero components
689 int Last = 0; // count the number of non-zero entries in vector
690 int j; // loop variables
691 double norm;
692
693 for (j=NDIM;j--;)
694 Components[j] = -1;
695 // find two components != 0
696 for (j=0;j<NDIM;j++)
697 if (fabs(GivenVector->x[j]) > MYEPSILON)
698 Components[Last++] = j;
699
700 switch(Last) {
701 case 3: // threecomponent system
702 case 2: // two component system
703 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
704 x[Components[2]] = 0.;
705 // in skp both remaining parts shall become zero but with opposite sign and third is zero
706 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
707 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
708 return true;
709 break;
710 case 1: // one component system
711 // set sole non-zero component to 0, and one of the other zero component pendants to 1
712 x[(Components[0]+2)%NDIM] = 0.;
713 x[(Components[0]+1)%NDIM] = 1.;
714 x[Components[0]] = 0.;
715 return true;
716 break;
717 default:
718 return false;
719 }
720};
721
722/** Determines parameter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
723 * \param *A first plane vector
724 * \param *B second plane vector
725 * \param *C third plane vector
726 * \return scaling parameter for this vector
727 */
728double Vector::CutsPlaneAt(const Vector * const A, const Vector * const B, const Vector * const C) const
729{
730// Log() << Verbose(3) << "For comparison: ";
731// Log() << Verbose(0) << "A " << A->Projection(this) << "\t";
732// Log() << Verbose(0) << "B " << B->Projection(this) << "\t";
733// Log() << Verbose(0) << "C " << C->Projection(this) << "\t";
734// Log() << Verbose(0) << endl;
735 return A->ScalarProduct(this);
736};
737
738
739/** Adds vector \a *y componentwise.
740 * \param *y vector
741 */
742void Vector::AddVector(const Vector * const y)
743{
744 for (int i=NDIM;i--;)
745 this->x[i] += y->x[i];
746}
747
748/** Adds vector \a *y componentwise.
749 * \param *y vector
750 */
751void Vector::SubtractVector(const Vector * const y)
752{
753 for (int i=NDIM;i--;)
754 this->x[i] -= y->x[i];
755}
756
757/** Copy vector \a *y componentwise.
758 * \param *y vector
759 */
760void Vector::CopyVector(const Vector * const y)
761{
762 // check for self assignment
763 if(y!=this){
764 for (int i=NDIM;i--;)
765 this->x[i] = y->x[i];
766 }
767}
768
769/** Copy vector \a y componentwise.
770 * \param y vector
771 */
772void Vector::CopyVector(const Vector &y)
773{
774 // check for self assignment
775 if(&y!=this) {
776 for (int i=NDIM;i--;)
777 this->x[i] = y.x[i];
778 }
779}
780
781// this function is completely unused so it is deactivated until new uses arrive and a new
782// place can be found
783#if 0
784/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
785 * This is linear system of equations to be solved, however of the three given (skp of this vector\
786 * with either of the three hast to be zero) only two are linear independent. The third equation
787 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
788 * where very often it has to be checked whether a certain value is zero or not and thus forked into
789 * another case.
790 * \param *x1 first vector
791 * \param *x2 second vector
792 * \param *y third vector
793 * \param alpha first angle
794 * \param beta second angle
795 * \param c norm of final vector
796 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
797 * \bug this is not yet working properly
798 */
799bool Vector::SolveSystem(Vector * x1, Vector * x2, Vector * y, const double alpha, const double beta, const double c)
800{
801 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
802 double ang; // angle on testing
803 double sign[3];
804 int i,j,k;
805 A = cos(alpha) * x1->Norm() * c;
806 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
807 B2 = cos(beta) * x2->Norm() * c;
808 C = c * c;
809 Log() << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
810 int flag = 0;
811 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
812 if (fabs(x1->x[1]) > MYEPSILON) {
813 flag = 1;
814 } else if (fabs(x1->x[2]) > MYEPSILON) {
815 flag = 2;
816 } else {
817 return false;
818 }
819 }
820 switch (flag) {
821 default:
822 case 0:
823 break;
824 case 2:
825 flip(x1->x[0],x1->x[1]);
826 flip(x2->x[0],x2->x[1]);
827 flip(y->x[0],y->x[1]);
828 //flip(x[0],x[1]);
829 flip(x1->x[1],x1->x[2]);
830 flip(x2->x[1],x2->x[2]);
831 flip(y->x[1],y->x[2]);
832 //flip(x[1],x[2]);
833 case 1:
834 flip(x1->x[0],x1->x[1]);
835 flip(x2->x[0],x2->x[1]);
836 flip(y->x[0],y->x[1]);
837 //flip(x[0],x[1]);
838 flip(x1->x[1],x1->x[2]);
839 flip(x2->x[1],x2->x[2]);
840 flip(y->x[1],y->x[2]);
841 //flip(x[1],x[2]);
842 break;
843 }
844 // now comes the case system
845 D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
846 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
847 D3 = y->x[0]/x1->x[0]*A-B1;
848 Log() << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
849 if (fabs(D1) < MYEPSILON) {
850 Log() << Verbose(2) << "D1 == 0!\n";
851 if (fabs(D2) > MYEPSILON) {
852 Log() << Verbose(3) << "D2 != 0!\n";
853 x[2] = -D3/D2;
854 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
855 E2 = -x1->x[1]/x1->x[0];
856 Log() << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
857 F1 = E1*E1 + 1.;
858 F2 = -E1*E2;
859 F3 = E1*E1 + D3*D3/(D2*D2) - C;
860 Log() << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
861 if (fabs(F1) < MYEPSILON) {
862 Log() << Verbose(4) << "F1 == 0!\n";
863 Log() << Verbose(4) << "Gleichungssystem linear\n";
864 x[1] = F3/(2.*F2);
865 } else {
866 p = F2/F1;
867 q = p*p - F3/F1;
868 Log() << Verbose(4) << "p " << p << "\tq " << q << endl;
869 if (q < 0) {
870 Log() << Verbose(4) << "q < 0" << endl;
871 return false;
872 }
873 x[1] = p + sqrt(q);
874 }
875 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
876 } else {
877 Log() << Verbose(2) << "Gleichungssystem unterbestimmt\n";
878 return false;
879 }
880 } else {
881 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
882 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
883 Log() << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
884 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
885 F2 = -(E1*E2 + D2*D3/(D1*D1));
886 F3 = E1*E1 + D3*D3/(D1*D1) - C;
887 Log() << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
888 if (fabs(F1) < MYEPSILON) {
889 Log() << Verbose(3) << "F1 == 0!\n";
890 Log() << Verbose(3) << "Gleichungssystem linear\n";
891 x[2] = F3/(2.*F2);
892 } else {
893 p = F2/F1;
894 q = p*p - F3/F1;
895 Log() << Verbose(3) << "p " << p << "\tq " << q << endl;
896 if (q < 0) {
897 Log() << Verbose(3) << "q < 0" << endl;
898 return false;
899 }
900 x[2] = p + sqrt(q);
901 }
902 x[1] = (-D2 * x[2] - D3)/D1;
903 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
904 }
905 switch (flag) { // back-flipping
906 default:
907 case 0:
908 break;
909 case 2:
910 flip(x1->x[0],x1->x[1]);
911 flip(x2->x[0],x2->x[1]);
912 flip(y->x[0],y->x[1]);
913 flip(x[0],x[1]);
914 flip(x1->x[1],x1->x[2]);
915 flip(x2->x[1],x2->x[2]);
916 flip(y->x[1],y->x[2]);
917 flip(x[1],x[2]);
918 case 1:
919 flip(x1->x[0],x1->x[1]);
920 flip(x2->x[0],x2->x[1]);
921 flip(y->x[0],y->x[1]);
922 //flip(x[0],x[1]);
923 flip(x1->x[1],x1->x[2]);
924 flip(x2->x[1],x2->x[2]);
925 flip(y->x[1],y->x[2]);
926 flip(x[1],x[2]);
927 break;
928 }
929 // one z component is only determined by its radius (without sign)
930 // thus check eight possible sign flips and determine by checking angle with second vector
931 for (i=0;i<8;i++) {
932 // set sign vector accordingly
933 for (j=2;j>=0;j--) {
934 k = (i & pot(2,j)) << j;
935 Log() << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
936 sign[j] = (k == 0) ? 1. : -1.;
937 }
938 Log() << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
939 // apply sign matrix
940 for (j=NDIM;j--;)
941 x[j] *= sign[j];
942 // calculate angle and check
943 ang = x2->Angle (this);
944 Log() << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
945 if (fabs(ang - cos(beta)) < MYEPSILON) {
946 break;
947 }
948 // unapply sign matrix (is its own inverse)
949 for (j=NDIM;j--;)
950 x[j] *= sign[j];
951 }
952 return true;
953};
954
955#endif
956
957/**
958 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
959 * their offset.
960 *
961 * @param offest for the origin of the parallelepiped
962 * @param three vectors forming the matrix that defines the shape of the parallelpiped
963 */
964bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
965{
966 Vector a;
967 a.CopyVector(this);
968 a.SubtractVector(&offset);
969 a.InverseMatrixMultiplication(parallelepiped);
970 bool isInside = true;
971
972 for (int i=NDIM;i--;)
973 isInside = isInside && ((a.x[i] <= 1) && (a.x[i] >= 0));
974
975 return isInside;
976}
Note: See TracBrowser for help on using the repository browser.