source: src/molecule_geometry.cpp@ 435065

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

Moved files bondgraph.?pp -> Graph/BondGraph.?pp.

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