source: src/molecule_geometry.cpp@ 5605793

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

Moved bond.* to Bond/, new class GraphEdge which contains graph parts of bond.

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