source: src/molecule_geometry.cpp@ 796aa6

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 796aa6 was 0632c5, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Removed some BOOST_FOREACH iterations and replaced with simpler mapping on the Set

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