source: src/vector.cpp@ 0e01b4

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 0e01b4 was 1829c4, checked in by Tillmann Crueger <crueger@…>, 15 years ago

FIX: Bug that caused memory corruption in Vector::GetOneNormalVector() when a two component system was given

  • Property mode set to 100644
File size: 18.4 KB
RevLine 
[6ac7ee]1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
[edb93c]7
[54a746]8#include "vector.hpp"
9#include "verbose.hpp"
[b34306]10#include "World.hpp"
[0a4f7f]11#include "Helpers/Assert.hpp"
[753f02]12#include "Helpers/fast_functions.hpp"
[6ac7ee]13
[1bd79e]14#include <iostream>
15
16using namespace std;
[6ac7ee]17
[97498a]18
[6ac7ee]19/************************************ Functions for class vector ************************************/
20
21/** Constructor of class vector.
22 */
[753f02]23Vector::Vector()
24{
25 x[0] = x[1] = x[2] = 0.;
26};
[6ac7ee]27
[753f02]28/**
29 * Copy constructor
[821907]30 */
[1bd79e]31
[753f02]32Vector::Vector(const Vector& src)
[821907]33{
[753f02]34 x[0] = src[0];
35 x[1] = src[1];
36 x[2] = src[2];
[1bd79e]37}
[821907]38
39/** Constructor of class vector.
40 */
[753f02]41Vector::Vector(const double x1, const double x2, const double x3)
[821907]42{
[753f02]43 x[0] = x1;
44 x[1] = x2;
45 x[2] = x3;
[821907]46};
47
[0a4f7f]48/**
49 * Assignment operator
[6ac7ee]50 */
[0a4f7f]51Vector& Vector::operator=(const Vector& src){
52 // check for self assignment
53 if(&src!=this){
[753f02]54 x[0] = src[0];
55 x[1] = src[1];
56 x[2] = src[2];
[0a4f7f]57 }
58 return *this;
59}
[6ac7ee]60
61/** Desctructor of class vector.
62 */
63Vector::~Vector() {};
64
65/** Calculates square of distance between this and another vector.
66 * \param *y array to second vector
67 * \return \f$| x - y |^2\f$
68 */
[273382]69double Vector::DistanceSquared(const Vector &y) const
[6ac7ee]70{
[042f82]71 double res = 0.;
72 for (int i=NDIM;i--;)
[753f02]73 res += (x[i]-y[i])*(x[i]-y[i]);
[042f82]74 return (res);
[6ac7ee]75};
76
77/** Calculates distance between this and another vector.
78 * \param *y array to second vector
79 * \return \f$| x - y |\f$
80 */
[273382]81double Vector::Distance(const Vector &y) const
[6ac7ee]82{
[273382]83 return (sqrt(DistanceSquared(y)));
[6ac7ee]84};
85
86/** Calculates distance between this and another vector in a periodic cell.
87 * \param *y array to second vector
88 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
89 * \return \f$| x - y |\f$
90 */
[273382]91double Vector::PeriodicDistance(const Vector &y, const double * const cell_size) const
[6ac7ee]92{
[042f82]93 double res = Distance(y), tmp, matrix[NDIM*NDIM];
[753f02]94 Vector Shiftedy, TranslationVector;
95 int N[NDIM];
96 matrix[0] = cell_size[0];
97 matrix[1] = cell_size[1];
98 matrix[2] = cell_size[3];
99 matrix[3] = cell_size[1];
100 matrix[4] = cell_size[2];
101 matrix[5] = cell_size[4];
102 matrix[6] = cell_size[3];
103 matrix[7] = cell_size[4];
104 matrix[8] = cell_size[5];
105 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
106 for (N[0]=-1;N[0]<=1;N[0]++)
107 for (N[1]=-1;N[1]<=1;N[1]++)
108 for (N[2]=-1;N[2]<=1;N[2]++) {
109 // create the translation vector
110 TranslationVector.Zero();
111 for (int i=NDIM;i--;)
112 TranslationVector[i] = (double)N[i];
113 TranslationVector.MatrixMultiplication(matrix);
114 // add onto the original vector to compare with
115 Shiftedy = y + TranslationVector;
116 // get distance and compare with minimum so far
117 tmp = Distance(Shiftedy);
118 if (tmp < res) res = tmp;
119 }
120 return (res);
[6ac7ee]121};
122
123/** Calculates distance between this and another vector in a periodic cell.
124 * \param *y array to second vector
125 * \param *cell_size 6-dimensional array with (xx, xy, yy, xz, yz, zz) entries specifying the periodic cell
126 * \return \f$| x - y |^2\f$
127 */
[273382]128double Vector::PeriodicDistanceSquared(const Vector &y, const double * const cell_size) const
[6ac7ee]129{
[042f82]130 double res = DistanceSquared(y), tmp, matrix[NDIM*NDIM];
[753f02]131 Vector Shiftedy, TranslationVector;
132 int N[NDIM];
133 matrix[0] = cell_size[0];
134 matrix[1] = cell_size[1];
135 matrix[2] = cell_size[3];
136 matrix[3] = cell_size[1];
137 matrix[4] = cell_size[2];
138 matrix[5] = cell_size[4];
139 matrix[6] = cell_size[3];
140 matrix[7] = cell_size[4];
141 matrix[8] = cell_size[5];
142 // in order to check the periodic distance, translate one of the vectors into each of the 27 neighbouring cells
143 for (N[0]=-1;N[0]<=1;N[0]++)
144 for (N[1]=-1;N[1]<=1;N[1]++)
145 for (N[2]=-1;N[2]<=1;N[2]++) {
146 // create the translation vector
147 TranslationVector.Zero();
148 for (int i=NDIM;i--;)
149 TranslationVector[i] = (double)N[i];
150 TranslationVector.MatrixMultiplication(matrix);
151 // add onto the original vector to compare with
152 Shiftedy = y + TranslationVector;
153 // get distance and compare with minimum so far
154 tmp = DistanceSquared(Shiftedy);
155 if (tmp < res) res = tmp;
156 }
157 return (res);
[6ac7ee]158};
159
160/** Keeps the vector in a periodic cell, defined by the symmetric \a *matrix.
161 * \param *out ofstream for debugging messages
162 * Tries to translate a vector into each adjacent neighbouring cell.
163 */
[e138de]164void Vector::KeepPeriodic(const double * const matrix)
[6ac7ee]165{
[753f02]166 // int N[NDIM];
167 // bool flag = false;
168 //vector Shifted, TranslationVector;
169 // Log() << Verbose(1) << "Begin of KeepPeriodic." << endl;
170 // Log() << Verbose(2) << "Vector is: ";
171 // Output(out);
172 // Log() << Verbose(0) << endl;
173 InverseMatrixMultiplication(matrix);
174 for(int i=NDIM;i--;) { // correct periodically
175 if (at(i) < 0) { // get every coefficient into the interval [0,1)
176 at(i) += ceil(at(i));
177 } else {
178 at(i) -= floor(at(i));
179 }
[042f82]180 }
[753f02]181 MatrixMultiplication(matrix);
182 // Log() << Verbose(2) << "New corrected vector is: ";
183 // Output(out);
184 // Log() << Verbose(0) << endl;
185 // Log() << Verbose(1) << "End of KeepPeriodic." << endl;
[6ac7ee]186};
187
188/** Calculates scalar product between this and another vector.
189 * \param *y array to second vector
190 * \return \f$\langle x, y \rangle\f$
191 */
[273382]192double Vector::ScalarProduct(const Vector &y) const
[6ac7ee]193{
[042f82]194 double res = 0.;
195 for (int i=NDIM;i--;)
[753f02]196 res += x[i]*y[i];
[042f82]197 return (res);
[6ac7ee]198};
199
200
201/** Calculates VectorProduct between this and another vector.
[042f82]202 * -# returns the Product in place of vector from which it was initiated
203 * -# ATTENTION: Only three dim.
204 * \param *y array to vector with which to calculate crossproduct
205 * \return \f$ x \times y \f&
[6ac7ee]206 */
[273382]207void Vector::VectorProduct(const Vector &y)
[6ac7ee]208{
[042f82]209 Vector tmp;
[753f02]210 tmp[0] = x[1]* (y[2]) - x[2]* (y[1]);
211 tmp[1] = x[2]* (y[0]) - x[0]* (y[2]);
212 tmp[2] = x[0]* (y[1]) - x[1]* (y[0]);
213 (*this) = tmp;
[6ac7ee]214};
215
216
217/** projects this vector onto plane defined by \a *y.
218 * \param *y normal vector of plane
219 * \return \f$\langle x, y \rangle\f$
220 */
[273382]221void Vector::ProjectOntoPlane(const Vector &y)
[6ac7ee]222{
[042f82]223 Vector tmp;
[753f02]224 tmp = y;
[042f82]225 tmp.Normalize();
[753f02]226 tmp.Scale(ScalarProduct(tmp));
227 *this -= tmp;
[2319ed]228};
229
[821907]230/** Calculates the minimum distance vector of this vector to the plane.
[c4d4df]231 * \param *out output stream for debugging
232 * \param *PlaneNormal normal of plane
233 * \param *PlaneOffset offset of plane
234 * \return distance to plane
[821907]235 * \return distance vector onto to plane
[c4d4df]236 */
[8cbb97]237Vector Vector::GetDistanceVectorToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const
[c4d4df]238{
[753f02]239 Vector temp = (*this) - PlaneOffset;
240 temp.MakeNormalTo(PlaneNormal);
[c4d4df]241 temp.Scale(-1.);
242 // then add connecting vector from plane to point
[753f02]243 temp += (*this)-PlaneOffset;
[99593f]244 double sign = temp.ScalarProduct(PlaneNormal);
[7ea9e6]245 if (fabs(sign) > MYEPSILON)
246 sign /= fabs(sign);
247 else
248 sign = 0.;
[c4d4df]249
[821907]250 temp.Normalize();
251 temp.Scale(sign);
252 return temp;
253};
254
[8cbb97]255
[821907]256/** Calculates the minimum distance of this vector to the plane.
257 * \sa Vector::GetDistanceVectorToPlane()
258 * \param *out output stream for debugging
259 * \param *PlaneNormal normal of plane
260 * \param *PlaneOffset offset of plane
261 * \return distance to plane
262 */
[8cbb97]263double Vector::DistanceToPlane(const Vector &PlaneNormal, const Vector &PlaneOffset) const
[821907]264{
265 return GetDistanceVectorToPlane(PlaneNormal,PlaneOffset).Norm();
[c4d4df]266};
267
[6ac7ee]268/** Calculates the projection of a vector onto another \a *y.
269 * \param *y array to second vector
270 */
[273382]271void Vector::ProjectIt(const Vector &y)
[6ac7ee]272{
[753f02]273 (*this) += (-ScalarProduct(y))*y;
[ef9df36]274};
275
276/** Calculates the projection of a vector onto another \a *y.
277 * \param *y array to second vector
278 * \return Vector
279 */
[273382]280Vector Vector::Projection(const Vector &y) const
[ef9df36]281{
[753f02]282 Vector helper = y;
283 helper.Scale((ScalarProduct(y)/y.NormSquared()));
[ef9df36]284
285 return helper;
[6ac7ee]286};
287
288/** Calculates norm of this vector.
289 * \return \f$|x|\f$
290 */
291double Vector::Norm() const
292{
[273382]293 return (sqrt(NormSquared()));
[6ac7ee]294};
295
[d4d0dd]296/** Calculates squared norm of this vector.
297 * \return \f$|x|^2\f$
298 */
299double Vector::NormSquared() const
300{
[273382]301 return (ScalarProduct(*this));
[d4d0dd]302};
303
[6ac7ee]304/** Normalizes this vector.
305 */
306void Vector::Normalize()
307{
[1bd79e]308 double factor = Norm();
309 (*this) *= 1/factor;
[6ac7ee]310};
311
312/** Zeros all components of this vector.
313 */
314void Vector::Zero()
315{
[753f02]316 at(0)=at(1)=at(2)=0;
[6ac7ee]317};
318
319/** Zeros all components of this vector.
320 */
[776b64]321void Vector::One(const double one)
[6ac7ee]322{
[753f02]323 at(0)=at(1)=at(2)=one;
[6ac7ee]324};
325
[9c20aa]326/** Checks whether vector has all components zero.
327 * @return true - vector is zero, false - vector is not
328 */
[54a746]329bool Vector::IsZero() const
[9c20aa]330{
[54a746]331 return (fabs(x[0])+fabs(x[1])+fabs(x[2]) < MYEPSILON);
332};
333
334/** Checks whether vector has length of 1.
335 * @return true - vector is normalized, false - vector is not
336 */
337bool Vector::IsOne() const
338{
339 return (fabs(Norm() - 1.) < MYEPSILON);
[9c20aa]340};
341
[ef9df36]342/** Checks whether vector is normal to \a *normal.
343 * @return true - vector is normalized, false - vector is not
344 */
[273382]345bool Vector::IsNormalTo(const Vector &normal) const
[ef9df36]346{
347 if (ScalarProduct(normal) < MYEPSILON)
348 return true;
349 else
350 return false;
351};
352
[b998c3]353/** Checks whether vector is normal to \a *normal.
354 * @return true - vector is normalized, false - vector is not
355 */
[273382]356bool Vector::IsEqualTo(const Vector &a) const
[b998c3]357{
358 bool status = true;
359 for (int i=0;i<NDIM;i++) {
[753f02]360 if (fabs(x[i] - a[i]) > MYEPSILON)
[b998c3]361 status = false;
362 }
363 return status;
364};
365
[6ac7ee]366/** Calculates the angle between this and another vector.
367 * \param *y array to second vector
368 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
369 */
[273382]370double Vector::Angle(const Vector &y) const
[6ac7ee]371{
[753f02]372 double norm1 = Norm(), norm2 = y.Norm();
[ef9df36]373 double angle = -1;
[d4d0dd]374 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
375 angle = this->ScalarProduct(y)/norm1/norm2;
[02da9e]376 // -1-MYEPSILON occured due to numerical imprecision, catch ...
[e138de]377 //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
[02da9e]378 if (angle < -1)
379 angle = -1;
380 if (angle > 1)
381 angle = 1;
[042f82]382 return acos(angle);
[6ac7ee]383};
384
[0a4f7f]385
386double& Vector::operator[](size_t i){
[753f02]387 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
388 return x[i];
[0a4f7f]389}
390
391const double& Vector::operator[](size_t i) const{
[753f02]392 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
393 return x[i];
[0a4f7f]394}
395
396double& Vector::at(size_t i){
397 return (*this)[i];
398}
399
400const double& Vector::at(size_t i) const{
401 return (*this)[i];
402}
403
404double* Vector::get(){
[753f02]405 return x;
[0a4f7f]406}
[6ac7ee]407
[ef9df36]408/** Compares vector \a to vector \a b component-wise.
409 * \param a base vector
410 * \param b vector components to add
411 * \return a == b
412 */
[72e7fa]413bool Vector::operator==(const Vector& b) const
[ef9df36]414{
[1bd79e]415 return IsEqualTo(b);
[ef9df36]416};
417
[6ac7ee]418/** Sums vector \a to this lhs component-wise.
419 * \param a base vector
420 * \param b vector components to add
421 * \return lhs + a
422 */
[72e7fa]423const Vector& Vector::operator+=(const Vector& b)
[6ac7ee]424{
[273382]425 this->AddVector(b);
[72e7fa]426 return *this;
[6ac7ee]427};
[54a746]428
429/** Subtracts vector \a from this lhs component-wise.
430 * \param a base vector
431 * \param b vector components to add
432 * \return lhs - a
433 */
[72e7fa]434const Vector& Vector::operator-=(const Vector& b)
[54a746]435{
[273382]436 this->SubtractVector(b);
[72e7fa]437 return *this;
[54a746]438};
439
[6ac7ee]440/** factor each component of \a a times a double \a m.
441 * \param a base vector
442 * \param m factor
443 * \return lhs.x[i] * m
444 */
[b84d5d]445const Vector& operator*=(Vector& a, const double m)
[6ac7ee]446{
[042f82]447 a.Scale(m);
448 return a;
[6ac7ee]449};
450
[042f82]451/** Sums two vectors \a and \b component-wise.
[6ac7ee]452 * \param a first vector
453 * \param b second vector
454 * \return a + b
455 */
[72e7fa]456Vector const Vector::operator+(const Vector& b) const
[6ac7ee]457{
[72e7fa]458 Vector x = *this;
[273382]459 x.AddVector(b);
[b84d5d]460 return x;
[6ac7ee]461};
462
[54a746]463/** Subtracts vector \a from \b component-wise.
464 * \param a first vector
465 * \param b second vector
466 * \return a - b
467 */
[72e7fa]468Vector const Vector::operator-(const Vector& b) const
[54a746]469{
[72e7fa]470 Vector x = *this;
[273382]471 x.SubtractVector(b);
[b84d5d]472 return x;
[54a746]473};
474
[6ac7ee]475/** Factors given vector \a a times \a m.
476 * \param a vector
477 * \param m factor
[54a746]478 * \return m * a
[6ac7ee]479 */
[b84d5d]480Vector const operator*(const Vector& a, const double m)
[6ac7ee]481{
[b84d5d]482 Vector x(a);
483 x.Scale(m);
484 return x;
[6ac7ee]485};
486
[54a746]487/** Factors given vector \a a times \a m.
488 * \param m factor
489 * \param a vector
490 * \return m * a
491 */
[b84d5d]492Vector const operator*(const double m, const Vector& a )
[54a746]493{
[b84d5d]494 Vector x(a);
495 x.Scale(m);
496 return x;
[54a746]497};
498
[9c20aa]499ostream& operator<<(ostream& ost, const Vector& m)
[6ac7ee]500{
[042f82]501 ost << "(";
502 for (int i=0;i<NDIM;i++) {
[0a4f7f]503 ost << m[i];
[042f82]504 if (i != 2)
505 ost << ",";
506 }
507 ost << ")";
508 return ost;
[6ac7ee]509};
510
511
[1bd79e]512void Vector::ScaleAll(const double *factor)
[6ac7ee]513{
[042f82]514 for (int i=NDIM;i--;)
[753f02]515 x[i] *= factor[i];
[6ac7ee]516};
517
518
[1bd79e]519
[776b64]520void Vector::Scale(const double factor)
[6ac7ee]521{
[042f82]522 for (int i=NDIM;i--;)
523 x[i] *= factor;
[6ac7ee]524};
525
[d09ff7]526/** Given a box by its matrix \a *M and its inverse *Minv the vector is made to point within that box.
527 * \param *M matrix of box
528 * \param *Minv inverse matrix
529 */
[776b64]530void Vector::WrapPeriodically(const double * const M, const double * const Minv)
[d09ff7]531{
532 MatrixMultiplication(Minv);
533 // truncate to [0,1] for each axis
534 for (int i=0;i<NDIM;i++) {
535 x[i] += 0.5; // set to center of box
536 while (x[i] >= 1.)
537 x[i] -= 1.;
538 while (x[i] < 0.)
539 x[i] += 1.;
540 }
541 MatrixMultiplication(M);
542};
543
[6ac7ee]544/** Do a matrix multiplication.
545 * \param *matrix NDIM_NDIM array
546 */
[776b64]547void Vector::MatrixMultiplication(const double * const M)
[6ac7ee]548{
[042f82]549 // do the matrix multiplication
[753f02]550 at(0) = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
551 at(1) = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
552 at(2) = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
[6ac7ee]553};
554
[2319ed]555/** Do a matrix multiplication with the \a *A' inverse.
[6ac7ee]556 * \param *matrix NDIM_NDIM array
557 */
[0a4f7f]558bool Vector::InverseMatrixMultiplication(const double * const A)
[6ac7ee]559{
[042f82]560 double B[NDIM*NDIM];
561 double detA = RDET3(A);
562 double detAReci;
563
564 // calculate the inverse B
565 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
566 detAReci = 1./detA;
567 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
568 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
569 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
570 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
571 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
572 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
573 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
574 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
575 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
576
577 // do the matrix multiplication
[753f02]578 at(0) = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
579 at(1) = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
580 at(2) = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
581
582 return true;
[042f82]583 } else {
[753f02]584 return false;
[042f82]585 }
[6ac7ee]586};
587
588
589/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
590 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
591 * \param *x1 first vector
592 * \param *x2 second vector
593 * \param *x3 third vector
594 * \param *factors three-component vector with the factor for each given vector
595 */
[273382]596void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
[6ac7ee]597{
[273382]598 (*this) = (factors[0]*x1) +
599 (factors[1]*x2) +
600 (factors[2]*x3);
[6ac7ee]601};
602
603/** Mirrors atom against a given plane.
604 * \param n[] normal vector of mirror plane.
605 */
[273382]606void Vector::Mirror(const Vector &n)
[6ac7ee]607{
[042f82]608 double projection;
[753f02]609 projection = ScalarProduct(n)/n.NormSquared(); // remove constancy from n (keep as logical one)
[042f82]610 // withdraw projected vector twice from original one
611 for (int i=NDIM;i--;)
[8cbb97]612 at(i) -= 2.*projection*n[i];
[6ac7ee]613};
614
615/** Calculates orthonormal vector to one given vectors.
616 * Just subtracts the projection onto the given vector from this vector.
[ef9df36]617 * The removed part of the vector is Vector::Projection()
[6ac7ee]618 * \param *x1 vector
619 * \return true - success, false - vector is zero
620 */
[0a4f7f]621bool Vector::MakeNormalTo(const Vector &y1)
[6ac7ee]622{
[042f82]623 bool result = false;
[753f02]624 double factor = y1.ScalarProduct(*this)/y1.NormSquared();
[042f82]625 Vector x1;
[753f02]626 x1 = factor * y1;
627 SubtractVector(x1);
[042f82]628 for (int i=NDIM;i--;)
629 result = result || (fabs(x[i]) > MYEPSILON);
[6ac7ee]630
[042f82]631 return result;
[6ac7ee]632};
633
634/** Creates this vector as one of the possible orthonormal ones to the given one.
635 * Just scan how many components of given *vector are unequal to zero and
636 * try to get the skp of both to be zero accordingly.
637 * \param *vector given vector
638 * \return true - success, false - failure (null vector given)
639 */
[273382]640bool Vector::GetOneNormalVector(const Vector &GivenVector)
[6ac7ee]641{
[042f82]642 int Components[NDIM]; // contains indices of non-zero components
643 int Last = 0; // count the number of non-zero entries in vector
644 int j; // loop variables
645 double norm;
646
647 for (j=NDIM;j--;)
648 Components[j] = -1;
[1829c4]649
650 // in two component-systems we need to find the one position that is zero
651 int zeroPos = -1;
[042f82]652 // find two components != 0
[1829c4]653 for (j=0;j<NDIM;j++){
[753f02]654 if (fabs(GivenVector[j]) > MYEPSILON)
[042f82]655 Components[Last++] = j;
[1829c4]656 else
657 // this our zero Position
658 zeroPos = j;
659 }
[042f82]660
661 switch(Last) {
662 case 3: // threecomponent system
[1829c4]663 // the position of the zero is arbitrary in three component systems
664 zeroPos = Components[2];
[042f82]665 case 2: // two component system
[753f02]666 norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
[1829c4]667 at(zeroPos) = 0.;
[042f82]668 // in skp both remaining parts shall become zero but with opposite sign and third is zero
[1829c4]669 at(Components[1]) = -1./GivenVector[Components[1]] / norm;
670 at(Components[0]) = 1./GivenVector[Components[0]] / norm;
[042f82]671 return true;
672 break;
673 case 1: // one component system
674 // set sole non-zero component to 0, and one of the other zero component pendants to 1
[1829c4]675 at((Components[0]+2)%NDIM) = 0.;
676 at((Components[0]+1)%NDIM) = 1.;
677 at(Components[0]) = 0.;
[042f82]678 return true;
679 break;
680 default:
681 return false;
682 }
[6ac7ee]683};
684
685/** Adds vector \a *y componentwise.
686 * \param *y vector
687 */
[273382]688void Vector::AddVector(const Vector &y)
[6ac7ee]689{
[753f02]690 for(int i=NDIM;i--;)
691 x[i] += y[i];
[6ac7ee]692}
693
694/** Adds vector \a *y componentwise.
695 * \param *y vector
696 */
[273382]697void Vector::SubtractVector(const Vector &y)
[6ac7ee]698{
[753f02]699 for(int i=NDIM;i--;)
700 x[i] -= y[i];
[ef9df36]701}
702
[89c8b2]703/**
704 * Checks whether this vector is within the parallelepiped defined by the given three vectors and
705 * their offset.
706 *
707 * @param offest for the origin of the parallelepiped
708 * @param three vectors forming the matrix that defines the shape of the parallelpiped
709 */
[776b64]710bool Vector::IsInParallelepiped(const Vector &offset, const double * const parallelepiped) const
[89c8b2]711{
[753f02]712 Vector a = (*this)-offset;
[89c8b2]713 a.InverseMatrixMultiplication(parallelepiped);
714 bool isInside = true;
715
716 for (int i=NDIM;i--;)
[753f02]717 isInside = isInside && ((a[i] <= 1) && (a[i] >= 0));
[89c8b2]718
719 return isInside;
720}
Note: See TracBrowser for help on using the repository browser.