source: src/vector.cpp@ f714979

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 f714979 was 69eb71, checked in by Christian Neuen <neuen@…>, 17 years ago

Multiple changes to boundary, currently not fully operational.
Molecules has a new routine to create adjacency lists, reading bonds from a dbond file instead of looking for the distances by itself.
Vector function Project onto plane has been updated.

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