source: src/vector.cpp@ 6b33de

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 6b33de was 7f3b9d, checked in by Frederik Heber <heber@…>, 17 years ago

Lots of for loops now count in reverse order where it does not matter, some 3 -> NDIM

for(i=0;i<var;i++) is slower than for (i=var;i--;) if the order of the i's is not important (note: i-- is also a value and it stops when on i == 0 automatically)
in builder.cpp there were some remnant 3 actually meant to be NDIM

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