source: src/molecule_geometry.cpp@ 7326b2

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

Extension to the periodic boundary case for analysis_correlation.cpp

other stuff:

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