source: src/molecule_geometry.cpp@ 70ff32

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 70ff32 was 266237, checked in by Frederik Heber <heber@…>, 15 years ago

Huge refactoring: molecule::ListOfBondsPerAtom and molecule::NumberOfBondsPerAtom removed, atom::ListOfBonds introduced. Unit Test for ListOfBonds manipulation introduced.

  • changes to builder.cpp: removed CreateListOfBondsPerAtom() calls, as the creation of the global arrays is not necessary anymore
  • changes to LinkedCell: LinkedCell::CheckBounds(int[NDIM]) does not admonish out of bonds as this is not desired for the local offset which may become out of bounds.
  • changes to lists.hpp templates: BUGFIX: unlink() now sets ->next and ->previous to NULL, cleanup() uses removedwithoutcheck()
  • new templates for molecule.hpp: SumPerAtom() allows for summation of the return value of atom:...() member fiunctions. This is needed e.g. for atom::CorrectBondDegree()

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

  • Property mode set to 100644
File size: 17.6 KB
Line 
1/*
2 * molecule_geometry.cpp
3 *
4 * Created on: Oct 5, 2009
5 * Author: heber
6 */
7
8#include "atom.hpp"
9#include "bond.hpp"
10#include "config.hpp"
11#include "element.hpp"
12#include "helpers.hpp"
13#include "leastsquaremin.hpp"
14#include "memoryallocator.hpp"
15#include "molecule.hpp"
16
17/************************************* Functions for class molecule *********************************/
18
19
20/** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
21 * \param *out output stream for debugging
22 */
23bool molecule::CenterInBox(ofstream *out)
24{
25 bool status = true;
26 Vector x;
27 const Vector *Center = DetermineCenterOfAll(out);
28 double *M = ReturnFullMatrixforSymmetric(cell_size);
29 double *Minv = x.InverseMatrix(M);
30
31 // go through all atoms
32 ActOnAllVectors( &Vector::SubtractVector, Center);
33 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
34
35 delete(M);
36 delete(Minv);
37 delete(Center);
38 return status;
39};
40
41
42/** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
43 * \param *out output stream for debugging
44 */
45bool molecule::BoundInBox(ofstream *out)
46{
47 bool status = true;
48 Vector x;
49 double *M = ReturnFullMatrixforSymmetric(cell_size);
50 double *Minv = x.InverseMatrix(M);
51
52 // go through all atoms
53 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
54
55 delete(M);
56 delete(Minv);
57 return status;
58};
59
60/** Centers the edge of the atoms at (0,0,0).
61 * \param *out output stream for debugging
62 * \param *max coordinates of other edge, specifying box dimensions.
63 */
64void molecule::CenterEdge(ofstream *out, Vector *max)
65{
66 Vector *min = new Vector;
67
68// *out << Verbose(3) << "Begin of CenterEdge." << endl;
69 atom *ptr = start->next; // start at first in list
70 if (ptr != end) { //list not empty?
71 for (int i=NDIM;i--;) {
72 max->x[i] = ptr->x.x[i];
73 min->x[i] = ptr->x.x[i];
74 }
75 while (ptr->next != end) { // continue with second if present
76 ptr = ptr->next;
77 //ptr->Output(1,1,out);
78 for (int i=NDIM;i--;) {
79 max->x[i] = (max->x[i] < ptr->x.x[i]) ? ptr->x.x[i] : max->x[i];
80 min->x[i] = (min->x[i] > ptr->x.x[i]) ? ptr->x.x[i] : min->x[i];
81 }
82 }
83// *out << Verbose(4) << "Maximum is ";
84// max->Output(out);
85// *out << ", Minimum is ";
86// min->Output(out);
87// *out << endl;
88 min->Scale(-1.);
89 max->AddVector(min);
90 Translate(min);
91 Center.Zero();
92 }
93 delete(min);
94// *out << Verbose(3) << "End of CenterEdge." << endl;
95};
96
97/** Centers the center of the atoms at (0,0,0).
98 * \param *out output stream for debugging
99 * \param *center return vector for translation vector
100 */
101void molecule::CenterOrigin(ofstream *out)
102{
103 int Num = 0;
104 atom *ptr = start->next; // start at first in list
105
106 Center.Zero();
107
108 if (ptr != end) { //list not empty?
109 while (ptr->next != end) { // continue with second if present
110 ptr = ptr->next;
111 Num++;
112 Center.AddVector(&ptr->x);
113 }
114 Center.Scale(-1./Num); // divide through total number (and sign for direction)
115 Translate(&Center);
116 Center.Zero();
117 }
118};
119
120/** Returns vector pointing to center of all atoms.
121 * \param *out output stream for debugging
122 * \return pointer to center of all vector
123 */
124Vector * molecule::DetermineCenterOfAll(ofstream *out)
125{
126 atom *ptr = start->next; // start at first in list
127 Vector *a = new Vector();
128 Vector tmp;
129 double Num = 0;
130
131 a->Zero();
132
133 if (ptr != end) { //list not empty?
134 while (ptr->next != end) { // continue with second if present
135 ptr = ptr->next;
136 Num += 1.;
137 tmp.CopyVector(&ptr->x);
138 a->AddVector(&tmp);
139 }
140 a->Scale(1./Num); // divide through total mass (and sign for direction)
141 }
142 //cout << Verbose(1) << "Resulting center of gravity: ";
143 //a->Output(out);
144 //cout << endl;
145 return a;
146};
147
148/** Returns vector pointing to center of gravity.
149 * \param *out output stream for debugging
150 * \return pointer to center of gravity vector
151 */
152Vector * molecule::DetermineCenterOfGravity(ofstream *out)
153{
154 atom *ptr = start->next; // start at first in list
155 Vector *a = new Vector();
156 Vector tmp;
157 double Num = 0;
158
159 a->Zero();
160
161 if (ptr != end) { //list not empty?
162 while (ptr->next != end) { // continue with second if present
163 ptr = ptr->next;
164 Num += ptr->type->mass;
165 tmp.CopyVector(&ptr->x);
166 tmp.Scale(ptr->type->mass); // scale by mass
167 a->AddVector(&tmp);
168 }
169 a->Scale(-1./Num); // divide through total mass (and sign for direction)
170 }
171// *out << Verbose(1) << "Resulting center of gravity: ";
172// a->Output(out);
173// *out << endl;
174 return a;
175};
176
177/** Centers the center of gravity of the atoms at (0,0,0).
178 * \param *out output stream for debugging
179 * \param *center return vector for translation vector
180 */
181void molecule::CenterPeriodic(ofstream *out)
182{
183 DeterminePeriodicCenter(Center);
184};
185
186
187/** Centers the center of gravity of the atoms at (0,0,0).
188 * \param *out output stream for debugging
189 * \param *center return vector for translation vector
190 */
191void molecule::CenterAtVector(ofstream *out, Vector *newcenter)
192{
193 Center.CopyVector(newcenter);
194};
195
196
197/** Scales all atoms by \a *factor.
198 * \param *factor pointer to scaling factor
199 */
200void molecule::Scale(double **factor)
201{
202 atom *ptr = start;
203
204 while (ptr->next != end) {
205 ptr = ptr->next;
206 for (int j=0;j<MDSteps;j++)
207 ptr->Trajectory.R.at(j).Scale(factor);
208 ptr->x.Scale(factor);
209 }
210};
211
212/** Translate all atoms by given vector.
213 * \param trans[] translation vector.
214 */
215void molecule::Translate(const Vector *trans)
216{
217 atom *ptr = start;
218
219 while (ptr->next != end) {
220 ptr = ptr->next;
221 for (int j=0;j<MDSteps;j++)
222 ptr->Trajectory.R.at(j).Translate(trans);
223 ptr->x.Translate(trans);
224 }
225};
226
227/** Translate the molecule periodically in the box.
228 * \param trans[] translation vector.
229 * TODO treatment of trajetories missing
230 */
231void molecule::TranslatePeriodically(const Vector *trans)
232{
233 Vector x;
234 double *M = ReturnFullMatrixforSymmetric(cell_size);
235 double *Minv = x.InverseMatrix(M);
236
237 // go through all atoms
238 ActOnAllVectors( &Vector::SubtractVector, trans);
239 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
240
241 delete(M);
242 delete(Minv);
243};
244
245
246/** Mirrors all atoms against a given plane.
247 * \param n[] normal vector of mirror plane.
248 */
249void molecule::Mirror(const Vector *n)
250{
251 ActOnAllVectors( &Vector::Mirror, n );
252};
253
254/** Determines center of molecule (yet not considering atom masses).
255 * \param center reference to return vector
256 */
257void molecule::DeterminePeriodicCenter(Vector &center)
258{
259 atom *Walker = start;
260 double *matrix = ReturnFullMatrixforSymmetric(cell_size);
261 double tmp;
262 bool flag;
263 Vector Testvector, Translationvector;
264
265 do {
266 Center.Zero();
267 flag = true;
268 while (Walker->next != end) {
269 Walker = Walker->next;
270#ifdef ADDHYDROGEN
271 if (Walker->type->Z != 1) {
272#endif
273 Testvector.CopyVector(&Walker->x);
274 Testvector.InverseMatrixMultiplication(matrix);
275 Translationvector.Zero();
276 for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
277 if (Walker->nr < (*Runner)->GetOtherAtom(Walker)->nr) // otherwise we shift one to, the other fro and gain nothing
278 for (int j=0;j<NDIM;j++) {
279 tmp = Walker->x.x[j] - (*Runner)->GetOtherAtom(Walker)->x.x[j];
280 if ((fabs(tmp)) > BondDistance) {
281 flag = false;
282 cout << Verbose(0) << "Hit: atom " << Walker->Name << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl;
283 if (tmp > 0)
284 Translationvector.x[j] -= 1.;
285 else
286 Translationvector.x[j] += 1.;
287 }
288 }
289 }
290 Testvector.AddVector(&Translationvector);
291 Testvector.MatrixMultiplication(matrix);
292 Center.AddVector(&Testvector);
293 cout << Verbose(1) << "vector is: ";
294 Testvector.Output((ofstream *)&cout);
295 cout << endl;
296#ifdef ADDHYDROGEN
297 // now also change all hydrogens
298 for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
299 if ((*Runner)->GetOtherAtom(Walker)->type->Z == 1) {
300 Testvector.CopyVector(&(*Runner)->GetOtherAtom(Walker)->x);
301 Testvector.InverseMatrixMultiplication(matrix);
302 Testvector.AddVector(&Translationvector);
303 Testvector.MatrixMultiplication(matrix);
304 Center.AddVector(&Testvector);
305 cout << Verbose(1) << "Hydrogen vector is: ";
306 Testvector.Output((ofstream *)&cout);
307 cout << endl;
308 }
309 }
310 }
311#endif
312 }
313 } while (!flag);
314 Free(&matrix);
315 Center.Scale(1./(double)AtomCount);
316};
317
318/** Transforms/Rotates the given molecule into its principal axis system.
319 * \param *out output stream for debugging
320 * \param DoRotate whether to rotate (true) or only to determine the PAS.
321 * TODO treatment of trajetories missing
322 */
323void molecule::PrincipalAxisSystem(ofstream *out, bool DoRotate)
324{
325 atom *ptr = start; // start at first in list
326 double InertiaTensor[NDIM*NDIM];
327 Vector *CenterOfGravity = DetermineCenterOfGravity(out);
328
329 CenterPeriodic(out);
330
331 // reset inertia tensor
332 for(int i=0;i<NDIM*NDIM;i++)
333 InertiaTensor[i] = 0.;
334
335 // sum up inertia tensor
336 while (ptr->next != end) {
337 ptr = ptr->next;
338 Vector x;
339 x.CopyVector(&ptr->x);
340 //x.SubtractVector(CenterOfGravity);
341 InertiaTensor[0] += ptr->type->mass*(x.x[1]*x.x[1] + x.x[2]*x.x[2]);
342 InertiaTensor[1] += ptr->type->mass*(-x.x[0]*x.x[1]);
343 InertiaTensor[2] += ptr->type->mass*(-x.x[0]*x.x[2]);
344 InertiaTensor[3] += ptr->type->mass*(-x.x[1]*x.x[0]);
345 InertiaTensor[4] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[2]*x.x[2]);
346 InertiaTensor[5] += ptr->type->mass*(-x.x[1]*x.x[2]);
347 InertiaTensor[6] += ptr->type->mass*(-x.x[2]*x.x[0]);
348 InertiaTensor[7] += ptr->type->mass*(-x.x[2]*x.x[1]);
349 InertiaTensor[8] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[1]*x.x[1]);
350 }
351 // print InertiaTensor for debugging
352 *out << "The inertia tensor is:" << endl;
353 for(int i=0;i<NDIM;i++) {
354 for(int j=0;j<NDIM;j++)
355 *out << InertiaTensor[i*NDIM+j] << " ";
356 *out << endl;
357 }
358 *out << endl;
359
360 // diagonalize to determine principal axis system
361 gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
362 gsl_matrix_view m = gsl_matrix_view_array(InertiaTensor, NDIM, NDIM);
363 gsl_vector *eval = gsl_vector_alloc(NDIM);
364 gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
365 gsl_eigen_symmv(&m.matrix, eval, evec, T);
366 gsl_eigen_symmv_free(T);
367 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);
368
369 for(int i=0;i<NDIM;i++) {
370 *out << Verbose(1) << "eigenvalue = " << gsl_vector_get(eval, i);
371 *out << ", eigenvector = (" << evec->data[i * evec->tda + 0] << "," << evec->data[i * evec->tda + 1] << "," << evec->data[i * evec->tda + 2] << ")" << endl;
372 }
373
374 // check whether we rotate or not
375 if (DoRotate) {
376 *out << Verbose(1) << "Transforming molecule into PAS ... ";
377 // the eigenvectors specify the transformation matrix
378 ActOnAllVectors( &Vector::MatrixMultiplication, (const double *) evec->data );
379 *out << "done." << endl;
380
381 // summing anew for debugging (resulting matrix has to be diagonal!)
382 // reset inertia tensor
383 for(int i=0;i<NDIM*NDIM;i++)
384 InertiaTensor[i] = 0.;
385
386 // sum up inertia tensor
387 ptr = start;
388 while (ptr->next != end) {
389 ptr = ptr->next;
390 Vector x;
391 x.CopyVector(&ptr->x);
392 //x.SubtractVector(CenterOfGravity);
393 InertiaTensor[0] += ptr->type->mass*(x.x[1]*x.x[1] + x.x[2]*x.x[2]);
394 InertiaTensor[1] += ptr->type->mass*(-x.x[0]*x.x[1]);
395 InertiaTensor[2] += ptr->type->mass*(-x.x[0]*x.x[2]);
396 InertiaTensor[3] += ptr->type->mass*(-x.x[1]*x.x[0]);
397 InertiaTensor[4] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[2]*x.x[2]);
398 InertiaTensor[5] += ptr->type->mass*(-x.x[1]*x.x[2]);
399 InertiaTensor[6] += ptr->type->mass*(-x.x[2]*x.x[0]);
400 InertiaTensor[7] += ptr->type->mass*(-x.x[2]*x.x[1]);
401 InertiaTensor[8] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[1]*x.x[1]);
402 }
403 // print InertiaTensor for debugging
404 *out << "The inertia tensor is:" << endl;
405 for(int i=0;i<NDIM;i++) {
406 for(int j=0;j<NDIM;j++)
407 *out << InertiaTensor[i*NDIM+j] << " ";
408 *out << endl;
409 }
410 *out << endl;
411 }
412
413 // free everything
414 delete(CenterOfGravity);
415 gsl_vector_free(eval);
416 gsl_matrix_free(evec);
417};
418
419
420/** Align all atoms in such a manner that given vector \a *n is along z axis.
421 * \param n[] alignment vector.
422 */
423void molecule::Align(Vector *n)
424{
425 atom *ptr = start;
426 double alpha, tmp;
427 Vector z_axis;
428 z_axis.x[0] = 0.;
429 z_axis.x[1] = 0.;
430 z_axis.x[2] = 1.;
431
432 // rotate on z-x plane
433 cout << Verbose(0) << "Begin of Aligning all atoms." << endl;
434 alpha = atan(-n->x[0]/n->x[2]);
435 cout << Verbose(1) << "Z-X-angle: " << alpha << " ... ";
436 while (ptr->next != end) {
437 ptr = ptr->next;
438 tmp = ptr->x.x[0];
439 ptr->x.x[0] = cos(alpha) * tmp + sin(alpha) * ptr->x.x[2];
440 ptr->x.x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->x.x[2];
441 for (int j=0;j<MDSteps;j++) {
442 tmp = ptr->Trajectory.R.at(j).x[0];
443 ptr->Trajectory.R.at(j).x[0] = cos(alpha) * tmp + sin(alpha) * ptr->Trajectory.R.at(j).x[2];
444 ptr->Trajectory.R.at(j).x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->Trajectory.R.at(j).x[2];
445 }
446 }
447 // rotate n vector
448 tmp = n->x[0];
449 n->x[0] = cos(alpha) * tmp + sin(alpha) * n->x[2];
450 n->x[2] = -sin(alpha) * tmp + cos(alpha) * n->x[2];
451 cout << Verbose(1) << "alignment vector after first rotation: ";
452 n->Output((ofstream *)&cout);
453 cout << endl;
454
455 // rotate on z-y plane
456 ptr = start;
457 alpha = atan(-n->x[1]/n->x[2]);
458 cout << Verbose(1) << "Z-Y-angle: " << alpha << " ... ";
459 while (ptr->next != end) {
460 ptr = ptr->next;
461 tmp = ptr->x.x[1];
462 ptr->x.x[1] = cos(alpha) * tmp + sin(alpha) * ptr->x.x[2];
463 ptr->x.x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->x.x[2];
464 for (int j=0;j<MDSteps;j++) {
465 tmp = ptr->Trajectory.R.at(j).x[1];
466 ptr->Trajectory.R.at(j).x[1] = cos(alpha) * tmp + sin(alpha) * ptr->Trajectory.R.at(j).x[2];
467 ptr->Trajectory.R.at(j).x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->Trajectory.R.at(j).x[2];
468 }
469 }
470 // rotate n vector (for consistency check)
471 tmp = n->x[1];
472 n->x[1] = cos(alpha) * tmp + sin(alpha) * n->x[2];
473 n->x[2] = -sin(alpha) * tmp + cos(alpha) * n->x[2];
474
475 cout << Verbose(1) << "alignment vector after second rotation: ";
476 n->Output((ofstream *)&cout);
477 cout << Verbose(1) << endl;
478 cout << Verbose(0) << "End of Aligning all atoms." << endl;
479};
480
481
482/** Calculates sum over least square distance to line hidden in \a *x.
483 * \param *x offset and direction vector
484 * \param *params pointer to lsq_params structure
485 * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
486 */
487double LeastSquareDistance (const gsl_vector * x, void * params)
488{
489 double res = 0, t;
490 Vector a,b,c,d;
491 struct lsq_params *par = (struct lsq_params *)params;
492 atom *ptr = par->mol->start;
493
494 // initialize vectors
495 a.x[0] = gsl_vector_get(x,0);
496 a.x[1] = gsl_vector_get(x,1);
497 a.x[2] = gsl_vector_get(x,2);
498 b.x[0] = gsl_vector_get(x,3);
499 b.x[1] = gsl_vector_get(x,4);
500 b.x[2] = gsl_vector_get(x,5);
501 // go through all atoms
502 while (ptr != par->mol->end) {
503 ptr = ptr->next;
504 if (ptr->type == ((struct lsq_params *)params)->type) { // for specific type
505 c.CopyVector(&ptr->x); // copy vector to temporary one
506 c.SubtractVector(&a); // subtract offset vector
507 t = c.ScalarProduct(&b); // get direction parameter
508 d.CopyVector(&b); // and create vector
509 d.Scale(&t);
510 c.SubtractVector(&d); // ... yielding distance vector
511 res += d.ScalarProduct((const Vector *)&d); // add squared distance
512 }
513 }
514 return res;
515};
516
517/** By minimizing the least square distance gains alignment vector.
518 * \bug this is not yet working properly it seems
519 */
520void molecule::GetAlignvector(struct lsq_params * par) const
521{
522 int np = 6;
523
524 const gsl_multimin_fminimizer_type *T =
525 gsl_multimin_fminimizer_nmsimplex;
526 gsl_multimin_fminimizer *s = NULL;
527 gsl_vector *ss;
528 gsl_multimin_function minex_func;
529
530 size_t iter = 0, i;
531 int status;
532 double size;
533
534 /* Initial vertex size vector */
535 ss = gsl_vector_alloc (np);
536
537 /* Set all step sizes to 1 */
538 gsl_vector_set_all (ss, 1.0);
539
540 /* Starting point */
541 par->x = gsl_vector_alloc (np);
542 par->mol = this;
543
544 gsl_vector_set (par->x, 0, 0.0); // offset
545 gsl_vector_set (par->x, 1, 0.0);
546 gsl_vector_set (par->x, 2, 0.0);
547 gsl_vector_set (par->x, 3, 0.0); // direction
548 gsl_vector_set (par->x, 4, 0.0);
549 gsl_vector_set (par->x, 5, 1.0);
550
551 /* Initialize method and iterate */
552 minex_func.f = &LeastSquareDistance;
553 minex_func.n = np;
554 minex_func.params = (void *)par;
555
556 s = gsl_multimin_fminimizer_alloc (T, np);
557 gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
558
559 do
560 {
561 iter++;
562 status = gsl_multimin_fminimizer_iterate(s);
563
564 if (status)
565 break;
566
567 size = gsl_multimin_fminimizer_size (s);
568 status = gsl_multimin_test_size (size, 1e-2);
569
570 if (status == GSL_SUCCESS)
571 {
572 printf ("converged to minimum at\n");
573 }
574
575 printf ("%5d ", (int)iter);
576 for (i = 0; i < (size_t)np; i++)
577 {
578 printf ("%10.3e ", gsl_vector_get (s->x, i));
579 }
580 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
581 }
582 while (status == GSL_CONTINUE && iter < 100);
583
584 for (i=0;i<(size_t)np;i++)
585 gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
586 //gsl_vector_free(par->x);
587 gsl_vector_free(ss);
588 gsl_multimin_fminimizer_free (s);
589};
Note: See TracBrowser for help on using the repository browser.