source: src/molecule_geometry.cpp@ 05a97c

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 05a97c was ea7176, checked in by Tillmann Crueger <crueger@…>, 15 years ago

FIX: Made AtomCount a Cacheable so that the number of atoms in a molecule will always be correct

All unittests working.
All Complete testcases fail.

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