source: src/LinearAlgebra/Vector.cpp@ 8eca62

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

Merge branch 'AtomicPositionEncapsulation' into stable

Conflicts:

src/Actions/AtomAction/ChangeElementAction.cpp
src/Actions/WorldAction/RemoveSphereOfAtomsAction.cpp
src/Makefile.am
src/UIElements/TextUI/TextDialog.cpp
src/analysis_correlation.hpp
src/atom.cpp
src/atom_atominfo.hpp
src/bond.cpp
src/boundary.cpp
src/molecule_geometry.cpp
src/tesselation.cpp
src/tesselationhelpers.cpp
src/triangleintersectionlist.cpp
src/unittests/Makefile.am

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