source: src/vector.cpp@ a98603

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

Just a temporary commit

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