source: src/vector.cpp@ d74077

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

Member variable Vector and element of class atom are now private.

  • Property mode set to 100644
File size: 12.8 KB
RevLine 
[6ac7ee]1/** \file vector.cpp
2 *
3 * Function implementations for the class vector.
4 *
5 */
6
[112b09]7#include "Helpers/MemDebug.hpp"
[edb93c]8
[54a746]9#include "vector.hpp"
[ce3d2b]10#include "VectorContent.hpp"
[54a746]11#include "verbose.hpp"
[b34306]12#include "World.hpp"
[0a4f7f]13#include "Helpers/Assert.hpp"
[753f02]14#include "Helpers/fast_functions.hpp"
[325390]15#include "Exceptions/MathException.hpp"
[6ac7ee]16
[1bd79e]17#include <iostream>
[923b6c]18#include <gsl/gsl_blas.h>
19
[1bd79e]20
21using namespace std;
[6ac7ee]22
[97498a]23
[6ac7ee]24/************************************ Functions for class vector ************************************/
25
26/** Constructor of class vector.
27 */
[753f02]28Vector::Vector()
29{
[ce3d2b]30 content = new VectorContent();
[753f02]31};
[6ac7ee]32
[753f02]33/**
34 * Copy constructor
[821907]35 */
[1bd79e]36
[753f02]37Vector::Vector(const Vector& src)
[821907]38{
[ce3d2b]39 content = new VectorContent();
40 gsl_vector_memcpy(content->content, src.content->content);
[1bd79e]41}
[821907]42
43/** Constructor of class vector.
44 */
[753f02]45Vector::Vector(const double x1, const double x2, const double x3)
[821907]46{
[ce3d2b]47 content = new VectorContent();
48 gsl_vector_set(content->content,0,x1);
49 gsl_vector_set(content->content,1,x2);
50 gsl_vector_set(content->content,2,x3);
[821907]51};
52
[d74077]53/** Constructor of class vector.
54 */
55Vector::Vector(const double x[3])
56{
57 content = new VectorContent();
58 gsl_vector_set(content->content,0,x[0]);
59 gsl_vector_set(content->content,1,x[1]);
60 gsl_vector_set(content->content,2,x[2]);
61};
62
[ce3d2b]63Vector::Vector(VectorContent *_content) :
[325390]64 content(_content)
65{}
66
[0a4f7f]67/**
68 * Assignment operator
[6ac7ee]69 */
[0a4f7f]70Vector& Vector::operator=(const Vector& src){
71 // check for self assignment
72 if(&src!=this){
[ce3d2b]73 gsl_vector_memcpy(content->content, src.content->content);
[0a4f7f]74 }
75 return *this;
76}
[6ac7ee]77
78/** Desctructor of class vector.
79 */
[d466f0]80Vector::~Vector() {
[ce3d2b]81 delete content;
[d466f0]82};
[6ac7ee]83
84/** Calculates square of distance between this and another vector.
85 * \param *y array to second vector
86 * \return \f$| x - y |^2\f$
87 */
[273382]88double Vector::DistanceSquared(const Vector &y) const
[6ac7ee]89{
[042f82]90 double res = 0.;
91 for (int i=NDIM;i--;)
[d466f0]92 res += (at(i)-y[i])*(at(i)-y[i]);
[042f82]93 return (res);
[6ac7ee]94};
95
96/** Calculates distance between this and another vector.
97 * \param *y array to second vector
98 * \return \f$| x - y |\f$
99 */
[1513a74]100double Vector::distance(const Vector &y) const
[6ac7ee]101{
[273382]102 return (sqrt(DistanceSquared(y)));
[6ac7ee]103};
104
[1513a74]105Vector Vector::getClosestPoint(const Vector &point) const{
106 // the closest point to a single point space is always the single point itself
107 return *this;
108}
109
[6ac7ee]110/** Calculates scalar product between this and another vector.
111 * \param *y array to second vector
112 * \return \f$\langle x, y \rangle\f$
113 */
[273382]114double Vector::ScalarProduct(const Vector &y) const
[6ac7ee]115{
[042f82]116 double res = 0.;
[ce3d2b]117 gsl_blas_ddot(content->content, y.content->content, &res);
[042f82]118 return (res);
[6ac7ee]119};
120
121
122/** Calculates VectorProduct between this and another vector.
[042f82]123 * -# returns the Product in place of vector from which it was initiated
124 * -# ATTENTION: Only three dim.
125 * \param *y array to vector with which to calculate crossproduct
126 * \return \f$ x \times y \f&
[6ac7ee]127 */
[273382]128void Vector::VectorProduct(const Vector &y)
[6ac7ee]129{
[042f82]130 Vector tmp;
[d466f0]131 for(int i=NDIM;i--;)
132 tmp[i] = at((i+1)%NDIM)*y[(i+2)%NDIM] - at((i+2)%NDIM)*y[(i+1)%NDIM];
[753f02]133 (*this) = tmp;
[6ac7ee]134};
135
136
137/** projects this vector onto plane defined by \a *y.
138 * \param *y normal vector of plane
139 * \return \f$\langle x, y \rangle\f$
140 */
[273382]141void Vector::ProjectOntoPlane(const Vector &y)
[6ac7ee]142{
[042f82]143 Vector tmp;
[753f02]144 tmp = y;
[042f82]145 tmp.Normalize();
[753f02]146 tmp.Scale(ScalarProduct(tmp));
147 *this -= tmp;
[2319ed]148};
149
[821907]150/** Calculates the minimum distance of this vector to the plane.
151 * \sa Vector::GetDistanceVectorToPlane()
152 * \param *out output stream for debugging
153 * \param *PlaneNormal normal of plane
154 * \param *PlaneOffset offset of plane
155 * \return distance to plane
156 */
[d4c9ae]157double Vector::DistanceToSpace(const Space &space) const
[821907]158{
[d4c9ae]159 return space.distance(*this);
[c4d4df]160};
161
[6ac7ee]162/** Calculates the projection of a vector onto another \a *y.
163 * \param *y array to second vector
164 */
[273382]165void Vector::ProjectIt(const Vector &y)
[6ac7ee]166{
[753f02]167 (*this) += (-ScalarProduct(y))*y;
[ef9df36]168};
169
170/** Calculates the projection of a vector onto another \a *y.
171 * \param *y array to second vector
172 * \return Vector
173 */
[273382]174Vector Vector::Projection(const Vector &y) const
[ef9df36]175{
[753f02]176 Vector helper = y;
177 helper.Scale((ScalarProduct(y)/y.NormSquared()));
[ef9df36]178
179 return helper;
[6ac7ee]180};
181
182/** Calculates norm of this vector.
183 * \return \f$|x|\f$
184 */
185double Vector::Norm() const
186{
[273382]187 return (sqrt(NormSquared()));
[6ac7ee]188};
189
[d4d0dd]190/** Calculates squared norm of this vector.
191 * \return \f$|x|^2\f$
192 */
193double Vector::NormSquared() const
194{
[273382]195 return (ScalarProduct(*this));
[d4d0dd]196};
197
[6ac7ee]198/** Normalizes this vector.
199 */
200void Vector::Normalize()
201{
[1bd79e]202 double factor = Norm();
203 (*this) *= 1/factor;
[6ac7ee]204};
205
206/** Zeros all components of this vector.
207 */
208void Vector::Zero()
209{
[753f02]210 at(0)=at(1)=at(2)=0;
[6ac7ee]211};
212
213/** Zeros all components of this vector.
214 */
[776b64]215void Vector::One(const double one)
[6ac7ee]216{
[753f02]217 at(0)=at(1)=at(2)=one;
[6ac7ee]218};
219
[9c20aa]220/** Checks whether vector has all components zero.
221 * @return true - vector is zero, false - vector is not
222 */
[54a746]223bool Vector::IsZero() const
[9c20aa]224{
[d466f0]225 return (fabs(at(0))+fabs(at(1))+fabs(at(2)) < MYEPSILON);
[54a746]226};
227
228/** Checks whether vector has length of 1.
229 * @return true - vector is normalized, false - vector is not
230 */
231bool Vector::IsOne() const
232{
233 return (fabs(Norm() - 1.) < MYEPSILON);
[9c20aa]234};
235
[ef9df36]236/** Checks whether vector is normal to \a *normal.
237 * @return true - vector is normalized, false - vector is not
238 */
[273382]239bool Vector::IsNormalTo(const Vector &normal) const
[ef9df36]240{
241 if (ScalarProduct(normal) < MYEPSILON)
242 return true;
243 else
244 return false;
245};
246
[b998c3]247/** Checks whether vector is normal to \a *normal.
248 * @return true - vector is normalized, false - vector is not
249 */
[273382]250bool Vector::IsEqualTo(const Vector &a) const
[b998c3]251{
252 bool status = true;
253 for (int i=0;i<NDIM;i++) {
[d466f0]254 if (fabs(at(i) - a[i]) > MYEPSILON)
[b998c3]255 status = false;
256 }
257 return status;
258};
259
[6ac7ee]260/** Calculates the angle between this and another vector.
261 * \param *y array to second vector
262 * \return \f$\acos\bigl(frac{\langle x, y \rangle}{|x||y|}\bigr)\f$
263 */
[273382]264double Vector::Angle(const Vector &y) const
[6ac7ee]265{
[753f02]266 double norm1 = Norm(), norm2 = y.Norm();
[ef9df36]267 double angle = -1;
[d4d0dd]268 if ((fabs(norm1) > MYEPSILON) && (fabs(norm2) > MYEPSILON))
269 angle = this->ScalarProduct(y)/norm1/norm2;
[02da9e]270 // -1-MYEPSILON occured due to numerical imprecision, catch ...
[e138de]271 //Log() << Verbose(2) << "INFO: acos(-1) = " << acos(-1) << ", acos(-1+MYEPSILON) = " << acos(-1+MYEPSILON) << ", acos(-1-MYEPSILON) = " << acos(-1-MYEPSILON) << "." << endl;
[02da9e]272 if (angle < -1)
273 angle = -1;
274 if (angle > 1)
275 angle = 1;
[042f82]276 return acos(angle);
[6ac7ee]277};
278
[0a4f7f]279
280double& Vector::operator[](size_t i){
[753f02]281 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
[ce3d2b]282 return *gsl_vector_ptr (content->content, i);
[0a4f7f]283}
284
285const double& Vector::operator[](size_t i) const{
[753f02]286 ASSERT(i<=NDIM && i>=0,"Vector Index out of Range");
[ce3d2b]287 return *gsl_vector_ptr (content->content, i);
[0a4f7f]288}
289
290double& Vector::at(size_t i){
291 return (*this)[i];
292}
293
294const double& Vector::at(size_t i) const{
295 return (*this)[i];
296}
297
[ce3d2b]298VectorContent* Vector::get(){
[0c7ed8]299 return content;
[0a4f7f]300}
[6ac7ee]301
[ef9df36]302/** Compares vector \a to vector \a b component-wise.
303 * \param a base vector
304 * \param b vector components to add
305 * \return a == b
306 */
[72e7fa]307bool Vector::operator==(const Vector& b) const
[ef9df36]308{
[1bd79e]309 return IsEqualTo(b);
[ef9df36]310};
311
[fa5a6a]312bool Vector::operator!=(const Vector& b) const
313{
314 return !IsEqualTo(b);
315}
316
[6ac7ee]317/** Sums vector \a to this lhs component-wise.
318 * \param a base vector
319 * \param b vector components to add
320 * \return lhs + a
321 */
[72e7fa]322const Vector& Vector::operator+=(const Vector& b)
[6ac7ee]323{
[273382]324 this->AddVector(b);
[72e7fa]325 return *this;
[6ac7ee]326};
[54a746]327
328/** Subtracts vector \a from this lhs component-wise.
329 * \param a base vector
330 * \param b vector components to add
331 * \return lhs - a
332 */
[72e7fa]333const Vector& Vector::operator-=(const Vector& b)
[54a746]334{
[273382]335 this->SubtractVector(b);
[72e7fa]336 return *this;
[54a746]337};
338
[6ac7ee]339/** factor each component of \a a times a double \a m.
340 * \param a base vector
341 * \param m factor
342 * \return lhs.x[i] * m
343 */
[b84d5d]344const Vector& operator*=(Vector& a, const double m)
[6ac7ee]345{
[042f82]346 a.Scale(m);
347 return a;
[6ac7ee]348};
349
[042f82]350/** Sums two vectors \a and \b component-wise.
[6ac7ee]351 * \param a first vector
352 * \param b second vector
353 * \return a + b
354 */
[72e7fa]355Vector const Vector::operator+(const Vector& b) const
[6ac7ee]356{
[72e7fa]357 Vector x = *this;
[273382]358 x.AddVector(b);
[b84d5d]359 return x;
[6ac7ee]360};
361
[54a746]362/** Subtracts vector \a from \b component-wise.
363 * \param a first vector
364 * \param b second vector
365 * \return a - b
366 */
[72e7fa]367Vector const Vector::operator-(const Vector& b) const
[54a746]368{
[72e7fa]369 Vector x = *this;
[273382]370 x.SubtractVector(b);
[b84d5d]371 return x;
[54a746]372};
373
[6ac7ee]374/** Factors given vector \a a times \a m.
375 * \param a vector
376 * \param m factor
[54a746]377 * \return m * a
[6ac7ee]378 */
[b84d5d]379Vector const operator*(const Vector& a, const double m)
[6ac7ee]380{
[b84d5d]381 Vector x(a);
382 x.Scale(m);
383 return x;
[6ac7ee]384};
385
[54a746]386/** Factors given vector \a a times \a m.
387 * \param m factor
388 * \param a vector
389 * \return m * a
390 */
[b84d5d]391Vector const operator*(const double m, const Vector& a )
[54a746]392{
[b84d5d]393 Vector x(a);
394 x.Scale(m);
395 return x;
[54a746]396};
397
[9c20aa]398ostream& operator<<(ostream& ost, const Vector& m)
[6ac7ee]399{
[042f82]400 ost << "(";
401 for (int i=0;i<NDIM;i++) {
[0a4f7f]402 ost << m[i];
[042f82]403 if (i != 2)
404 ost << ",";
405 }
406 ost << ")";
407 return ost;
[6ac7ee]408};
409
410
[1bd79e]411void Vector::ScaleAll(const double *factor)
[6ac7ee]412{
[042f82]413 for (int i=NDIM;i--;)
[d466f0]414 at(i) *= factor[i];
[6ac7ee]415};
416
[b5bf84]417void Vector::ScaleAll(const Vector &factor){
[ce3d2b]418 gsl_vector_mul(content->content, factor.content->content);
[b5bf84]419}
[6ac7ee]420
[1bd79e]421
[776b64]422void Vector::Scale(const double factor)
[6ac7ee]423{
[ce3d2b]424 gsl_vector_scale(content->content,factor);
[6ac7ee]425};
426
[45ef76]427std::pair<Vector,Vector> Vector::partition(const Vector &rhs) const{
428 double factor = ScalarProduct(rhs)/rhs.NormSquared();
429 Vector res= factor * rhs;
430 return make_pair(res,(*this)-res);
431}
432
433std::pair<pointset,Vector> Vector::partition(const pointset &points) const{
434 Vector helper = *this;
435 pointset res;
436 for(pointset::const_iterator iter=points.begin();iter!=points.end();++iter){
437 pair<Vector,Vector> currPart = helper.partition(*iter);
438 res.push_back(currPart.first);
439 helper = currPart.second;
440 }
441 return make_pair(res,helper);
442}
443
[6ac7ee]444/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
445 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
446 * \param *x1 first vector
447 * \param *x2 second vector
448 * \param *x3 third vector
449 * \param *factors three-component vector with the factor for each given vector
450 */
[273382]451void Vector::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
[6ac7ee]452{
[273382]453 (*this) = (factors[0]*x1) +
454 (factors[1]*x2) +
455 (factors[2]*x3);
[6ac7ee]456};
457
458/** Calculates orthonormal vector to one given vectors.
459 * Just subtracts the projection onto the given vector from this vector.
[ef9df36]460 * The removed part of the vector is Vector::Projection()
[6ac7ee]461 * \param *x1 vector
462 * \return true - success, false - vector is zero
463 */
[0a4f7f]464bool Vector::MakeNormalTo(const Vector &y1)
[6ac7ee]465{
[042f82]466 bool result = false;
[753f02]467 double factor = y1.ScalarProduct(*this)/y1.NormSquared();
[45ef76]468 Vector x1 = factor * y1;
[753f02]469 SubtractVector(x1);
[042f82]470 for (int i=NDIM;i--;)
[d466f0]471 result = result || (fabs(at(i)) > MYEPSILON);
[6ac7ee]472
[042f82]473 return result;
[6ac7ee]474};
475
476/** Creates this vector as one of the possible orthonormal ones to the given one.
477 * Just scan how many components of given *vector are unequal to zero and
478 * try to get the skp of both to be zero accordingly.
479 * \param *vector given vector
480 * \return true - success, false - failure (null vector given)
481 */
[273382]482bool Vector::GetOneNormalVector(const Vector &GivenVector)
[6ac7ee]483{
[042f82]484 int Components[NDIM]; // contains indices of non-zero components
485 int Last = 0; // count the number of non-zero entries in vector
486 int j; // loop variables
487 double norm;
488
489 for (j=NDIM;j--;)
490 Components[j] = -1;
[1829c4]491
492 // in two component-systems we need to find the one position that is zero
493 int zeroPos = -1;
[042f82]494 // find two components != 0
[1829c4]495 for (j=0;j<NDIM;j++){
[753f02]496 if (fabs(GivenVector[j]) > MYEPSILON)
[042f82]497 Components[Last++] = j;
[1829c4]498 else
499 // this our zero Position
500 zeroPos = j;
501 }
[042f82]502
503 switch(Last) {
504 case 3: // threecomponent system
[1829c4]505 // the position of the zero is arbitrary in three component systems
506 zeroPos = Components[2];
[042f82]507 case 2: // two component system
[753f02]508 norm = sqrt(1./(GivenVector[Components[1]]*GivenVector[Components[1]]) + 1./(GivenVector[Components[0]]*GivenVector[Components[0]]));
[1829c4]509 at(zeroPos) = 0.;
[042f82]510 // in skp both remaining parts shall become zero but with opposite sign and third is zero
[1829c4]511 at(Components[1]) = -1./GivenVector[Components[1]] / norm;
512 at(Components[0]) = 1./GivenVector[Components[0]] / norm;
[042f82]513 return true;
514 break;
515 case 1: // one component system
516 // set sole non-zero component to 0, and one of the other zero component pendants to 1
[1829c4]517 at((Components[0]+2)%NDIM) = 0.;
518 at((Components[0]+1)%NDIM) = 1.;
519 at(Components[0]) = 0.;
[042f82]520 return true;
521 break;
522 default:
523 return false;
524 }
[6ac7ee]525};
526
527/** Adds vector \a *y componentwise.
528 * \param *y vector
529 */
[273382]530void Vector::AddVector(const Vector &y)
[6ac7ee]531{
[ce3d2b]532 gsl_vector_add(content->content, y.content->content);
[6ac7ee]533}
534
535/** Adds vector \a *y componentwise.
536 * \param *y vector
537 */
[273382]538void Vector::SubtractVector(const Vector &y)
[6ac7ee]539{
[ce3d2b]540 gsl_vector_sub(content->content, y.content->content);
[ef9df36]541}
542
[005e18]543
544// some comonly used vectors
545const Vector zeroVec(0,0,0);
546const Vector e1(1,0,0);
547const Vector e2(0,1,0);
548const Vector e3(0,0,1);
Note: See TracBrowser for help on using the repository browser.