source: src/vector.cpp@ dfe8ef

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

Log() and eLog() are prepended by a DoLog()/DoeLog() construct.

  • Most of the run time (95%) is spent on verbosity that it is discarded anyway due to a low verbosity setting. However, the operator << is evaluated from the right-hand side, hence the whole message is constructed and then thrown away.
  • DoLog() and DoeLog() are new functions that check the verbosity beforehand and are used as follows: DoLog(2) && (Log() << verbose(2) << "message" << endl);

Signed-off-by: Frederik Heber <heber@…>

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