source: src/molecule_geometry.cpp@ e138de

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 e138de was e138de, checked in by Frederik Heber <heber@…>, 16 years ago

Huge change from ofstream * (const) out --> Log().

  • first shift was done via regular expressions
  • then via error messages from the code
  • note that class atom, class element and class molecule kept in parts their output stream, was they print to file.
  • make check runs fine
  • MISSING: Verbosity is not fixed for everything (i.e. if no endl; is present and next has Verbose(0) ...)

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

  • Property mode set to 100644
File size: 17.5 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 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()
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 delete(M);
55 delete(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->x[i] = ptr->x.x[i];
72 min->x[i] = ptr->x.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->x[i] = (max->x[i] < ptr->x.x[i]) ? ptr->x.x[i] : max->x[i];
79 min->x[i] = (min->x[i] > ptr->x.x[i]) ? ptr->x.x[i] : min->x[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->next; // start at first in list
104
105 Center.Zero();
106
107 if (ptr != 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 delete(M);
236 delete(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 tmp;
256 bool flag;
257 Vector Testvector, Translationvector;
258
259 do {
260 Center.Zero();
261 flag = true;
262 while (Walker->next != end) {
263 Walker = Walker->next;
264#ifdef ADDHYDROGEN
265 if (Walker->type->Z != 1) {
266#endif
267 Testvector.CopyVector(&Walker->x);
268 Testvector.InverseMatrixMultiplication(matrix);
269 Translationvector.Zero();
270 for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
271 if (Walker->nr < (*Runner)->GetOtherAtom(Walker)->nr) // otherwise we shift one to, the other fro and gain nothing
272 for (int j=0;j<NDIM;j++) {
273 tmp = Walker->x.x[j] - (*Runner)->GetOtherAtom(Walker)->x.x[j];
274 if ((fabs(tmp)) > BondDistance) {
275 flag = false;
276 Log() << Verbose(0) << "Hit: atom " << Walker->Name << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl;
277 if (tmp > 0)
278 Translationvector.x[j] -= 1.;
279 else
280 Translationvector.x[j] += 1.;
281 }
282 }
283 }
284 Testvector.AddVector(&Translationvector);
285 Testvector.MatrixMultiplication(matrix);
286 Center.AddVector(&Testvector);
287 Log() << Verbose(1) << "vector is: ";
288 Testvector.Output();
289 Log() << Verbose(0) << endl;
290#ifdef ADDHYDROGEN
291 // now also change all hydrogens
292 for (BondList::const_iterator Runner = Walker->ListOfBonds.begin(); Runner != Walker->ListOfBonds.end(); (++Runner)) {
293 if ((*Runner)->GetOtherAtom(Walker)->type->Z == 1) {
294 Testvector.CopyVector(&(*Runner)->GetOtherAtom(Walker)->x);
295 Testvector.InverseMatrixMultiplication(matrix);
296 Testvector.AddVector(&Translationvector);
297 Testvector.MatrixMultiplication(matrix);
298 Center.AddVector(&Testvector);
299 Log() << Verbose(1) << "Hydrogen vector is: ";
300 Testvector.Output();
301 Log() << Verbose(0) << endl;
302 }
303 }
304 }
305#endif
306 }
307 } while (!flag);
308 Free(&matrix);
309 Center.Scale(1./(double)AtomCount);
310};
311
312/** Transforms/Rotates the given molecule into its principal axis system.
313 * \param *out output stream for debugging
314 * \param DoRotate whether to rotate (true) or only to determine the PAS.
315 * TODO treatment of trajetories missing
316 */
317void molecule::PrincipalAxisSystem(bool DoRotate)
318{
319 atom *ptr = start; // start at first in list
320 double InertiaTensor[NDIM*NDIM];
321 Vector *CenterOfGravity = DetermineCenterOfGravity();
322
323 CenterPeriodic();
324
325 // reset inertia tensor
326 for(int i=0;i<NDIM*NDIM;i++)
327 InertiaTensor[i] = 0.;
328
329 // sum up inertia tensor
330 while (ptr->next != end) {
331 ptr = ptr->next;
332 Vector x;
333 x.CopyVector(&ptr->x);
334 //x.SubtractVector(CenterOfGravity);
335 InertiaTensor[0] += ptr->type->mass*(x.x[1]*x.x[1] + x.x[2]*x.x[2]);
336 InertiaTensor[1] += ptr->type->mass*(-x.x[0]*x.x[1]);
337 InertiaTensor[2] += ptr->type->mass*(-x.x[0]*x.x[2]);
338 InertiaTensor[3] += ptr->type->mass*(-x.x[1]*x.x[0]);
339 InertiaTensor[4] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[2]*x.x[2]);
340 InertiaTensor[5] += ptr->type->mass*(-x.x[1]*x.x[2]);
341 InertiaTensor[6] += ptr->type->mass*(-x.x[2]*x.x[0]);
342 InertiaTensor[7] += ptr->type->mass*(-x.x[2]*x.x[1]);
343 InertiaTensor[8] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[1]*x.x[1]);
344 }
345 // print InertiaTensor for debugging
346 Log() << Verbose(0) << "The inertia tensor is:" << endl;
347 for(int i=0;i<NDIM;i++) {
348 for(int j=0;j<NDIM;j++)
349 Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ";
350 Log() << Verbose(0) << endl;
351 }
352 Log() << Verbose(0) << endl;
353
354 // diagonalize to determine principal axis system
355 gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
356 gsl_matrix_view m = gsl_matrix_view_array(InertiaTensor, NDIM, NDIM);
357 gsl_vector *eval = gsl_vector_alloc(NDIM);
358 gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
359 gsl_eigen_symmv(&m.matrix, eval, evec, T);
360 gsl_eigen_symmv_free(T);
361 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);
362
363 for(int i=0;i<NDIM;i++) {
364 Log() << Verbose(1) << "eigenvalue = " << gsl_vector_get(eval, i);
365 Log() << Verbose(0) << ", eigenvector = (" << evec->data[i * evec->tda + 0] << "," << evec->data[i * evec->tda + 1] << "," << evec->data[i * evec->tda + 2] << ")" << endl;
366 }
367
368 // check whether we rotate or not
369 if (DoRotate) {
370 Log() << Verbose(1) << "Transforming molecule into PAS ... ";
371 // the eigenvectors specify the transformation matrix
372 ActOnAllVectors( &Vector::MatrixMultiplication, (const double *) evec->data );
373 Log() << Verbose(0) << "done." << endl;
374
375 // summing anew for debugging (resulting matrix has to be diagonal!)
376 // reset inertia tensor
377 for(int i=0;i<NDIM*NDIM;i++)
378 InertiaTensor[i] = 0.;
379
380 // sum up inertia tensor
381 ptr = start;
382 while (ptr->next != end) {
383 ptr = ptr->next;
384 Vector x;
385 x.CopyVector(&ptr->x);
386 //x.SubtractVector(CenterOfGravity);
387 InertiaTensor[0] += ptr->type->mass*(x.x[1]*x.x[1] + x.x[2]*x.x[2]);
388 InertiaTensor[1] += ptr->type->mass*(-x.x[0]*x.x[1]);
389 InertiaTensor[2] += ptr->type->mass*(-x.x[0]*x.x[2]);
390 InertiaTensor[3] += ptr->type->mass*(-x.x[1]*x.x[0]);
391 InertiaTensor[4] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[2]*x.x[2]);
392 InertiaTensor[5] += ptr->type->mass*(-x.x[1]*x.x[2]);
393 InertiaTensor[6] += ptr->type->mass*(-x.x[2]*x.x[0]);
394 InertiaTensor[7] += ptr->type->mass*(-x.x[2]*x.x[1]);
395 InertiaTensor[8] += ptr->type->mass*(x.x[0]*x.x[0] + x.x[1]*x.x[1]);
396 }
397 // print InertiaTensor for debugging
398 Log() << Verbose(0) << "The inertia tensor is:" << endl;
399 for(int i=0;i<NDIM;i++) {
400 for(int j=0;j<NDIM;j++)
401 Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ";
402 Log() << Verbose(0) << endl;
403 }
404 Log() << Verbose(0) << endl;
405 }
406
407 // free everything
408 delete(CenterOfGravity);
409 gsl_vector_free(eval);
410 gsl_matrix_free(evec);
411};
412
413
414/** Align all atoms in such a manner that given vector \a *n is along z axis.
415 * \param n[] alignment vector.
416 */
417void molecule::Align(Vector *n)
418{
419 atom *ptr = start;
420 double alpha, tmp;
421 Vector z_axis;
422 z_axis.x[0] = 0.;
423 z_axis.x[1] = 0.;
424 z_axis.x[2] = 1.;
425
426 // rotate on z-x plane
427 Log() << Verbose(0) << "Begin of Aligning all atoms." << endl;
428 alpha = atan(-n->x[0]/n->x[2]);
429 Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ";
430 while (ptr->next != end) {
431 ptr = ptr->next;
432 tmp = ptr->x.x[0];
433 ptr->x.x[0] = cos(alpha) * tmp + sin(alpha) * ptr->x.x[2];
434 ptr->x.x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->x.x[2];
435 for (int j=0;j<MDSteps;j++) {
436 tmp = ptr->Trajectory.R.at(j).x[0];
437 ptr->Trajectory.R.at(j).x[0] = cos(alpha) * tmp + sin(alpha) * ptr->Trajectory.R.at(j).x[2];
438 ptr->Trajectory.R.at(j).x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->Trajectory.R.at(j).x[2];
439 }
440 }
441 // rotate n vector
442 tmp = n->x[0];
443 n->x[0] = cos(alpha) * tmp + sin(alpha) * n->x[2];
444 n->x[2] = -sin(alpha) * tmp + cos(alpha) * n->x[2];
445 Log() << Verbose(1) << "alignment vector after first rotation: ";
446 n->Output();
447 Log() << Verbose(0) << endl;
448
449 // rotate on z-y plane
450 ptr = start;
451 alpha = atan(-n->x[1]/n->x[2]);
452 Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ";
453 while (ptr->next != end) {
454 ptr = ptr->next;
455 tmp = ptr->x.x[1];
456 ptr->x.x[1] = cos(alpha) * tmp + sin(alpha) * ptr->x.x[2];
457 ptr->x.x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->x.x[2];
458 for (int j=0;j<MDSteps;j++) {
459 tmp = ptr->Trajectory.R.at(j).x[1];
460 ptr->Trajectory.R.at(j).x[1] = cos(alpha) * tmp + sin(alpha) * ptr->Trajectory.R.at(j).x[2];
461 ptr->Trajectory.R.at(j).x[2] = -sin(alpha) * tmp + cos(alpha) * ptr->Trajectory.R.at(j).x[2];
462 }
463 }
464 // rotate n vector (for consistency check)
465 tmp = n->x[1];
466 n->x[1] = cos(alpha) * tmp + sin(alpha) * n->x[2];
467 n->x[2] = -sin(alpha) * tmp + cos(alpha) * n->x[2];
468
469 Log() << Verbose(1) << "alignment vector after second rotation: ";
470 n->Output();
471 Log() << Verbose(1) << endl;
472 Log() << Verbose(0) << "End of Aligning all atoms." << endl;
473};
474
475
476/** Calculates sum over least square distance to line hidden in \a *x.
477 * \param *x offset and direction vector
478 * \param *params pointer to lsq_params structure
479 * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
480 */
481double LeastSquareDistance (const gsl_vector * x, void * params)
482{
483 double res = 0, t;
484 Vector a,b,c,d;
485 struct lsq_params *par = (struct lsq_params *)params;
486 atom *ptr = par->mol->start;
487
488 // initialize vectors
489 a.x[0] = gsl_vector_get(x,0);
490 a.x[1] = gsl_vector_get(x,1);
491 a.x[2] = gsl_vector_get(x,2);
492 b.x[0] = gsl_vector_get(x,3);
493 b.x[1] = gsl_vector_get(x,4);
494 b.x[2] = gsl_vector_get(x,5);
495 // go through all atoms
496 while (ptr != par->mol->end) {
497 ptr = ptr->next;
498 if (ptr->type == ((struct lsq_params *)params)->type) { // for specific type
499 c.CopyVector(&ptr->x); // copy vector to temporary one
500 c.SubtractVector(&a); // subtract offset vector
501 t = c.ScalarProduct(&b); // get direction parameter
502 d.CopyVector(&b); // and create vector
503 d.Scale(&t);
504 c.SubtractVector(&d); // ... yielding distance vector
505 res += d.ScalarProduct((const Vector *)&d); // add squared distance
506 }
507 }
508 return res;
509};
510
511/** By minimizing the least square distance gains alignment vector.
512 * \bug this is not yet working properly it seems
513 */
514void molecule::GetAlignvector(struct lsq_params * par) const
515{
516 int np = 6;
517
518 const gsl_multimin_fminimizer_type *T =
519 gsl_multimin_fminimizer_nmsimplex;
520 gsl_multimin_fminimizer *s = NULL;
521 gsl_vector *ss;
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
531 /* Set all step sizes to 1 */
532 gsl_vector_set_all (ss, 1.0);
533
534 /* Starting point */
535 par->x = gsl_vector_alloc (np);
536 par->mol = this;
537
538 gsl_vector_set (par->x, 0, 0.0); // offset
539 gsl_vector_set (par->x, 1, 0.0);
540 gsl_vector_set (par->x, 2, 0.0);
541 gsl_vector_set (par->x, 3, 0.0); // direction
542 gsl_vector_set (par->x, 4, 0.0);
543 gsl_vector_set (par->x, 5, 1.0);
544
545 /* Initialize method and iterate */
546 minex_func.f = &LeastSquareDistance;
547 minex_func.n = np;
548 minex_func.params = (void *)par;
549
550 s = gsl_multimin_fminimizer_alloc (T, np);
551 gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
552
553 do
554 {
555 iter++;
556 status = gsl_multimin_fminimizer_iterate(s);
557
558 if (status)
559 break;
560
561 size = gsl_multimin_fminimizer_size (s);
562 status = gsl_multimin_test_size (size, 1e-2);
563
564 if (status == GSL_SUCCESS)
565 {
566 printf ("converged to minimum at\n");
567 }
568
569 printf ("%5d ", (int)iter);
570 for (i = 0; i < (size_t)np; i++)
571 {
572 printf ("%10.3e ", gsl_vector_get (s->x, i));
573 }
574 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
575 }
576 while (status == GSL_CONTINUE && iter < 100);
577
578 for (i=0;i<(size_t)np;i++)
579 gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
580 //gsl_vector_free(par->x);
581 gsl_vector_free(ss);
582 gsl_multimin_fminimizer_free (s);
583};
Note: See TracBrowser for help on using the repository browser.