source: src/molecule_geometry.cpp@ c49c96

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 c49c96 was 5108e1, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Removed MatrixMultiplication() method from Vector class

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