source: src/molecule_geometry.cpp@ 0a4f7f

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

Made data internal data-structure of vector class private

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