source: src/vector.cpp@ 10af0d

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 10af0d was e4ea46, checked in by Christian Neuen <neuen@…>, 16 years ago

Tesselation starts to look good, minor discrepancies are still there and a segmentation fault.

  • Property mode set to 100644
File size: 25.2 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(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
315ofstream& operator<<(ofstream& ost,Vector& m)
316{
317 m.Output(&ost);
318 return ost;
319};
320
321/** Scales each atom coordinate by an individual \a factor.
322 * \param *factor pointer to scaling factor
323 */
324void Vector::Scale(double **factor)
325{
326 for (int i=NDIM;i--;)
327 x[i] *= (*factor)[i];
328};
329
330void Vector::Scale(double *factor)
331{
332 for (int i=NDIM;i--;)
333 x[i] *= *factor;
334};
335
336void Vector::Scale(double factor)
337{
338 for (int i=NDIM;i--;)
339 x[i] *= factor;
340};
341
342/** Translate atom by given vector.
343 * \param trans[] translation vector.
344 */
345void Vector::Translate(const Vector *trans)
346{
347 for (int i=NDIM;i--;)
348 x[i] += trans->x[i];
349};
350
351/** Do a matrix multiplication.
352 * \param *matrix NDIM_NDIM array
353 */
354void Vector::MatrixMultiplication(double *M)
355{
356 Vector C;
357 // do the matrix multiplication
358 C.x[0] = M[0]*x[0]+M[3]*x[1]+M[6]*x[2];
359 C.x[1] = M[1]*x[0]+M[4]*x[1]+M[7]*x[2];
360 C.x[2] = M[2]*x[0]+M[5]*x[1]+M[8]*x[2];
361 // transfer the result into this
362 for (int i=NDIM;i--;)
363 x[i] = C.x[i];
364};
365
366/** Do a matrix multiplication with \a *matrix' inverse.
367 * \param *matrix NDIM_NDIM array
368 */
369void Vector::InverseMatrixMultiplication(double *A)
370{
371 Vector C;
372 double B[NDIM*NDIM];
373 double detA = RDET3(A);
374 double detAReci;
375
376 // calculate the inverse B
377 if (fabs(detA) > MYEPSILON) {; // RDET3(A) yields precisely zero if A irregular
378 detAReci = 1./detA;
379 B[0] = detAReci*RDET2(A[4],A[5],A[7],A[8]); // A_11
380 B[1] = -detAReci*RDET2(A[1],A[2],A[7],A[8]); // A_12
381 B[2] = detAReci*RDET2(A[1],A[2],A[4],A[5]); // A_13
382 B[3] = -detAReci*RDET2(A[3],A[5],A[6],A[8]); // A_21
383 B[4] = detAReci*RDET2(A[0],A[2],A[6],A[8]); // A_22
384 B[5] = -detAReci*RDET2(A[0],A[2],A[3],A[5]); // A_23
385 B[6] = detAReci*RDET2(A[3],A[4],A[6],A[7]); // A_31
386 B[7] = -detAReci*RDET2(A[0],A[1],A[6],A[7]); // A_32
387 B[8] = detAReci*RDET2(A[0],A[1],A[3],A[4]); // A_33
388
389 // do the matrix multiplication
390 C.x[0] = B[0]*x[0]+B[3]*x[1]+B[6]*x[2];
391 C.x[1] = B[1]*x[0]+B[4]*x[1]+B[7]*x[2];
392 C.x[2] = B[2]*x[0]+B[5]*x[1]+B[8]*x[2];
393 // transfer the result into this
394 for (int i=NDIM;i--;)
395 x[i] = C.x[i];
396 } else {
397 cerr << "ERROR: inverse of matrix does not exists!" << endl;
398 }
399};
400
401
402/** Creates this vector as the b y *factors' components scaled linear combination of the given three.
403 * this vector = x1*factors[0] + x2* factors[1] + x3*factors[2]
404 * \param *x1 first vector
405 * \param *x2 second vector
406 * \param *x3 third vector
407 * \param *factors three-component vector with the factor for each given vector
408 */
409void Vector::LinearCombinationOfVectors(const Vector *x1, const Vector *x2, const Vector *x3, double *factors)
410{
411 for(int i=NDIM;i--;)
412 x[i] = factors[0]*x1->x[i] + factors[1]*x2->x[i] + factors[2]*x3->x[i];
413};
414
415/** Mirrors atom against a given plane.
416 * \param n[] normal vector of mirror plane.
417 */
418void Vector::Mirror(const Vector *n)
419{
420 double projection;
421 projection = ScalarProduct(n)/n->ScalarProduct(n); // remove constancy from n (keep as logical one)
422 // withdraw projected vector twice from original one
423 cout << Verbose(1) << "Vector: ";
424 Output((ofstream *)&cout);
425 cout << "\t";
426 for (int i=NDIM;i--;)
427 x[i] -= 2.*projection*n->x[i];
428 cout << "Projected vector: ";
429 Output((ofstream *)&cout);
430 cout << endl;
431};
432
433/** Calculates normal vector for three given vectors (being three points in space).
434 * Makes this vector orthonormal to the three given points, making up a place in 3d space.
435 * \param *y1 first vector
436 * \param *y2 second vector
437 * \param *y3 third vector
438 * \return true - success, vectors are linear independent, false - failure due to linear dependency
439 */
440bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2, const Vector *y3)
441{
442 Vector x1, x2;
443
444 x1.CopyVector(y1);
445 x1.SubtractVector(y2);
446 x2.CopyVector(y3);
447 x2.SubtractVector(y2);
448 if ((x1.Norm()==0) || (x2.Norm()==0)) {
449 cout << Verbose(4) << "Given vectors are linear dependent." << endl;
450 return false;
451 }
452// cout << Verbose(4) << "relative, first plane coordinates:";
453// x1.Output((ofstream *)&cout);
454// cout << endl;
455// cout << Verbose(4) << "second plane coordinates:";
456// x2.Output((ofstream *)&cout);
457// cout << endl;
458
459 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
460 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
461 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
462 Normalize();
463
464 return true;
465};
466
467
468/** Calculates orthonormal vector to two given vectors.
469 * Makes this vector orthonormal to two given vectors. This is very similar to the other
470 * vector::MakeNormalVector(), only there three points whereas here two difference
471 * vectors are given.
472 * \param *x1 first vector
473 * \param *x2 second vector
474 * \return true - success, vectors are linear independent, false - failure due to linear dependency
475 */
476bool Vector::MakeNormalVector(const Vector *y1, const Vector *y2)
477{
478 Vector x1,x2;
479 x1.CopyVector(y1);
480 x2.CopyVector(y2);
481 Zero();
482 if ((x1.Norm()==0) || (x2.Norm()==0)) {
483 cout << Verbose(4) << "Given vectors are linear dependent." << endl;
484 return false;
485 }
486// cout << Verbose(4) << "relative, first plane coordinates:";
487// x1.Output((ofstream *)&cout);
488// cout << endl;
489// cout << Verbose(4) << "second plane coordinates:";
490// x2.Output((ofstream *)&cout);
491// cout << endl;
492
493 this->x[0] = (x1.x[1]*x2.x[2] - x1.x[2]*x2.x[1]);
494 this->x[1] = (x1.x[2]*x2.x[0] - x1.x[0]*x2.x[2]);
495 this->x[2] = (x1.x[0]*x2.x[1] - x1.x[1]*x2.x[0]);
496 Normalize();
497
498 return true;
499};
500
501/** Calculates orthonormal vector to one given vectors.
502 * Just subtracts the projection onto the given vector from this vector.
503 * \param *x1 vector
504 * \return true - success, false - vector is zero
505 */
506bool Vector::MakeNormalVector(const Vector *y1)
507{
508 bool result = false;
509 Vector x1;
510 x1.CopyVector(y1);
511 x1.Scale(x1.Projection(this));
512 SubtractVector(&x1);
513 for (int i=NDIM;i--;)
514 result = result || (fabs(x[i]) > MYEPSILON);
515
516 return result;
517};
518
519/** Creates this vector as one of the possible orthonormal ones to the given one.
520 * Just scan how many components of given *vector are unequal to zero and
521 * try to get the skp of both to be zero accordingly.
522 * \param *vector given vector
523 * \return true - success, false - failure (null vector given)
524 */
525bool Vector::GetOneNormalVector(const Vector *GivenVector)
526{
527 int Components[NDIM]; // contains indices of non-zero components
528 int Last = 0; // count the number of non-zero entries in vector
529 int j; // loop variables
530 double norm;
531
532 cout << Verbose(4);
533 GivenVector->Output((ofstream *)&cout);
534 cout << endl;
535 for (j=NDIM;j--;)
536 Components[j] = -1;
537 // find two components != 0
538 for (j=0;j<NDIM;j++)
539 if (fabs(GivenVector->x[j]) > MYEPSILON)
540 Components[Last++] = j;
541 cout << Verbose(4) << Last << " Components != 0: (" << Components[0] << "," << Components[1] << "," << Components[2] << ")" << endl;
542
543 switch(Last) {
544 case 3: // threecomponent system
545 case 2: // two component system
546 norm = sqrt(1./(GivenVector->x[Components[1]]*GivenVector->x[Components[1]]) + 1./(GivenVector->x[Components[0]]*GivenVector->x[Components[0]]));
547 x[Components[2]] = 0.;
548 // in skp both remaining parts shall become zero but with opposite sign and third is zero
549 x[Components[1]] = -1./GivenVector->x[Components[1]] / norm;
550 x[Components[0]] = 1./GivenVector->x[Components[0]] / norm;
551 return true;
552 break;
553 case 1: // one component system
554 // set sole non-zero component to 0, and one of the other zero component pendants to 1
555 x[(Components[0]+2)%NDIM] = 0.;
556 x[(Components[0]+1)%NDIM] = 1.;
557 x[Components[0]] = 0.;
558 return true;
559 break;
560 default:
561 return false;
562 }
563};
564
565/** Determines paramter needed to multiply this vector to obtain intersection point with plane defined by \a *A, \a *B and \a *C.
566 * \param *A first plane vector
567 * \param *B second plane vector
568 * \param *C third plane vector
569 * \return scaling parameter for this vector
570 */
571double Vector::CutsPlaneAt(Vector *A, Vector *B, Vector *C)
572{
573// cout << Verbose(3) << "For comparison: ";
574// cout << "A " << A->Projection(this) << "\t";
575// cout << "B " << B->Projection(this) << "\t";
576// cout << "C " << C->Projection(this) << "\t";
577// cout << endl;
578 return A->Projection(this);
579};
580
581/** Creates a new vector as the one with least square distance to a given set of \a vectors.
582 * \param *vectors set of vectors
583 * \param num number of vectors
584 * \return true if success, false if failed due to linear dependency
585 */
586bool Vector::LSQdistance(Vector **vectors, int num)
587{
588 int j;
589
590 for (j=0;j<num;j++) {
591 cout << Verbose(1) << j << "th atom's vector: ";
592 (vectors[j])->Output((ofstream *)&cout);
593 cout << endl;
594 }
595
596 int np = 3;
597 struct LSQ_params par;
598
599 const gsl_multimin_fminimizer_type *T =
600 gsl_multimin_fminimizer_nmsimplex;
601 gsl_multimin_fminimizer *s = NULL;
602 gsl_vector *ss, *y;
603 gsl_multimin_function minex_func;
604
605 size_t iter = 0, i;
606 int status;
607 double size;
608
609 /* Initial vertex size vector */
610 ss = gsl_vector_alloc (np);
611 y = gsl_vector_alloc (np);
612
613 /* Set all step sizes to 1 */
614 gsl_vector_set_all (ss, 1.0);
615
616 /* Starting point */
617 par.vectors = vectors;
618 par.num = num;
619
620 for (i=NDIM;i--;)
621 gsl_vector_set(y, i, (vectors[0]->x[i] - vectors[1]->x[i])/2.);
622
623 /* Initialize method and iterate */
624 minex_func.f = &LSQ;
625 minex_func.n = np;
626 minex_func.params = (void *)&par;
627
628 s = gsl_multimin_fminimizer_alloc (T, np);
629 gsl_multimin_fminimizer_set (s, &minex_func, y, ss);
630
631 do
632 {
633 iter++;
634 status = gsl_multimin_fminimizer_iterate(s);
635
636 if (status)
637 break;
638
639 size = gsl_multimin_fminimizer_size (s);
640 status = gsl_multimin_test_size (size, 1e-2);
641
642 if (status == GSL_SUCCESS)
643 {
644 printf ("converged to minimum at\n");
645 }
646
647 printf ("%5d ", (int)iter);
648 for (i = 0; i < (size_t)np; i++)
649 {
650 printf ("%10.3e ", gsl_vector_get (s->x, i));
651 }
652 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
653 }
654 while (status == GSL_CONTINUE && iter < 100);
655
656 for (i=(size_t)np;i--;)
657 this->x[i] = gsl_vector_get(s->x, i);
658 gsl_vector_free(y);
659 gsl_vector_free(ss);
660 gsl_multimin_fminimizer_free (s);
661
662 return true;
663};
664
665/** Adds vector \a *y componentwise.
666 * \param *y vector
667 */
668void Vector::AddVector(const Vector *y)
669{
670 for (int i=NDIM;i--;)
671 this->x[i] += y->x[i];
672}
673
674/** Adds vector \a *y componentwise.
675 * \param *y vector
676 */
677void Vector::SubtractVector(const Vector *y)
678{
679 for (int i=NDIM;i--;)
680 this->x[i] -= y->x[i];
681}
682
683/** Copy vector \a *y componentwise.
684 * \param *y vector
685 */
686void Vector::CopyVector(const Vector *y)
687{
688 for (int i=NDIM;i--;)
689 this->x[i] = y->x[i];
690}
691
692
693/** Asks for position, checks for boundary.
694 * \param cell_size unitary size of cubic cell, coordinates must be within 0...cell_size
695 * \param check whether bounds shall be checked (true) or not (false)
696 */
697void Vector::AskPosition(double *cell_size, bool check)
698{
699 char coords[3] = {'x','y','z'};
700 int j = -1;
701 for (int i=0;i<3;i++) {
702 j += i+1;
703 do {
704 cout << Verbose(0) << coords[i] << "[0.." << cell_size[j] << "]: ";
705 cin >> x[i];
706 } while (((x[i] < 0) || (x[i] >= cell_size[j])) && (check));
707 }
708};
709
710/** Solves a vectorial system consisting of two orthogonal statements and a norm statement.
711 * This is linear system of equations to be solved, however of the three given (skp of this vector\
712 * with either of the three hast to be zero) only two are linear independent. The third equation
713 * is that the vector should be of magnitude 1 (orthonormal). This all leads to a case-based solution
714 * where very often it has to be checked whether a certain value is zero or not and thus forked into
715 * another case.
716 * \param *x1 first vector
717 * \param *x2 second vector
718 * \param *y third vector
719 * \param alpha first angle
720 * \param beta second angle
721 * \param c norm of final vector
722 * \return a vector with \f$\langle x1,x2 \rangle=A\f$, \f$\langle x1,y \rangle = B\f$ and with norm \a c.
723 * \bug this is not yet working properly
724 */
725bool Vector::SolveSystem(Vector *x1, Vector *x2, Vector *y, double alpha, double beta, double c)
726{
727 double D1,D2,D3,E1,E2,F1,F2,F3,p,q=0., A, B1, B2, C;
728 double ang; // angle on testing
729 double sign[3];
730 int i,j,k;
731 A = cos(alpha) * x1->Norm() * c;
732 B1 = cos(beta + M_PI/2.) * y->Norm() * c;
733 B2 = cos(beta) * x2->Norm() * c;
734 C = c * c;
735 cout << Verbose(2) << "A " << A << "\tB " << B1 << "\tC " << C << endl;
736 int flag = 0;
737 if (fabs(x1->x[0]) < MYEPSILON) { // check for zero components for the later flipping and back-flipping
738 if (fabs(x1->x[1]) > MYEPSILON) {
739 flag = 1;
740 } else if (fabs(x1->x[2]) > MYEPSILON) {
741 flag = 2;
742 } else {
743 return false;
744 }
745 }
746 switch (flag) {
747 default:
748 case 0:
749 break;
750 case 2:
751 flip(&x1->x[0],&x1->x[1]);
752 flip(&x2->x[0],&x2->x[1]);
753 flip(&y->x[0],&y->x[1]);
754 //flip(&x[0],&x[1]);
755 flip(&x1->x[1],&x1->x[2]);
756 flip(&x2->x[1],&x2->x[2]);
757 flip(&y->x[1],&y->x[2]);
758 //flip(&x[1],&x[2]);
759 case 1:
760 flip(&x1->x[0],&x1->x[1]);
761 flip(&x2->x[0],&x2->x[1]);
762 flip(&y->x[0],&y->x[1]);
763 //flip(&x[0],&x[1]);
764 flip(&x1->x[1],&x1->x[2]);
765 flip(&x2->x[1],&x2->x[2]);
766 flip(&y->x[1],&y->x[2]);
767 //flip(&x[1],&x[2]);
768 break;
769 }
770 // now comes the case system
771 D1 = -y->x[0]/x1->x[0]*x1->x[1]+y->x[1];
772 D2 = -y->x[0]/x1->x[0]*x1->x[2]+y->x[2];
773 D3 = y->x[0]/x1->x[0]*A-B1;
774 cout << Verbose(2) << "D1 " << D1 << "\tD2 " << D2 << "\tD3 " << D3 << "\n";
775 if (fabs(D1) < MYEPSILON) {
776 cout << Verbose(2) << "D1 == 0!\n";
777 if (fabs(D2) > MYEPSILON) {
778 cout << Verbose(3) << "D2 != 0!\n";
779 x[2] = -D3/D2;
780 E1 = A/x1->x[0] + x1->x[2]/x1->x[0]*D3/D2;
781 E2 = -x1->x[1]/x1->x[0];
782 cout << Verbose(3) << "E1 " << E1 << "\tE2 " << E2 << "\n";
783 F1 = E1*E1 + 1.;
784 F2 = -E1*E2;
785 F3 = E1*E1 + D3*D3/(D2*D2) - C;
786 cout << Verbose(3) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
787 if (fabs(F1) < MYEPSILON) {
788 cout << Verbose(4) << "F1 == 0!\n";
789 cout << Verbose(4) << "Gleichungssystem linear\n";
790 x[1] = F3/(2.*F2);
791 } else {
792 p = F2/F1;
793 q = p*p - F3/F1;
794 cout << Verbose(4) << "p " << p << "\tq " << q << endl;
795 if (q < 0) {
796 cout << Verbose(4) << "q < 0" << endl;
797 return false;
798 }
799 x[1] = p + sqrt(q);
800 }
801 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
802 } else {
803 cout << Verbose(2) << "Gleichungssystem unterbestimmt\n";
804 return false;
805 }
806 } else {
807 E1 = A/x1->x[0]+x1->x[1]/x1->x[0]*D3/D1;
808 E2 = x1->x[1]/x1->x[0]*D2/D1 - x1->x[2];
809 cout << Verbose(2) << "E1 " << E1 << "\tE2 " << E2 << "\n";
810 F1 = E2*E2 + D2*D2/(D1*D1) + 1.;
811 F2 = -(E1*E2 + D2*D3/(D1*D1));
812 F3 = E1*E1 + D3*D3/(D1*D1) - C;
813 cout << Verbose(2) << "F1 " << F1 << "\tF2 " << F2 << "\tF3 " << F3 << "\n";
814 if (fabs(F1) < MYEPSILON) {
815 cout << Verbose(3) << "F1 == 0!\n";
816 cout << Verbose(3) << "Gleichungssystem linear\n";
817 x[2] = F3/(2.*F2);
818 } else {
819 p = F2/F1;
820 q = p*p - F3/F1;
821 cout << Verbose(3) << "p " << p << "\tq " << q << endl;
822 if (q < 0) {
823 cout << Verbose(3) << "q < 0" << endl;
824 return false;
825 }
826 x[2] = p + sqrt(q);
827 }
828 x[1] = (-D2 * x[2] - D3)/D1;
829 x[0] = A/x1->x[0] - x1->x[1]/x1->x[0]*x[1] + x1->x[2]/x1->x[0]*x[2];
830 }
831 switch (flag) { // back-flipping
832 default:
833 case 0:
834 break;
835 case 2:
836 flip(&x1->x[0],&x1->x[1]);
837 flip(&x2->x[0],&x2->x[1]);
838 flip(&y->x[0],&y->x[1]);
839 flip(&x[0],&x[1]);
840 flip(&x1->x[1],&x1->x[2]);
841 flip(&x2->x[1],&x2->x[2]);
842 flip(&y->x[1],&y->x[2]);
843 flip(&x[1],&x[2]);
844 case 1:
845 flip(&x1->x[0],&x1->x[1]);
846 flip(&x2->x[0],&x2->x[1]);
847 flip(&y->x[0],&y->x[1]);
848 //flip(&x[0],&x[1]);
849 flip(&x1->x[1],&x1->x[2]);
850 flip(&x2->x[1],&x2->x[2]);
851 flip(&y->x[1],&y->x[2]);
852 flip(&x[1],&x[2]);
853 break;
854 }
855 // one z component is only determined by its radius (without sign)
856 // thus check eight possible sign flips and determine by checking angle with second vector
857 for (i=0;i<8;i++) {
858 // set sign vector accordingly
859 for (j=2;j>=0;j--) {
860 k = (i & pot(2,j)) << j;
861 cout << Verbose(2) << "k " << k << "\tpot(2,j) " << pot(2,j) << endl;
862 sign[j] = (k == 0) ? 1. : -1.;
863 }
864 cout << Verbose(2) << i << ": sign matrix is " << sign[0] << "\t" << sign[1] << "\t" << sign[2] << "\n";
865 // apply sign matrix
866 for (j=NDIM;j--;)
867 x[j] *= sign[j];
868 // calculate angle and check
869 ang = x2->Angle (this);
870 cout << Verbose(1) << i << "th angle " << ang << "\tbeta " << cos(beta) << " :\t";
871 if (fabs(ang - cos(beta)) < MYEPSILON) {
872 break;
873 }
874 // unapply sign matrix (is its own inverse)
875 for (j=NDIM;j--;)
876 x[j] *= sign[j];
877 }
878 return true;
879};
Note: See TracBrowser for help on using the repository browser.