source: src/molecule_geometry.cpp@ cd5047

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 cd5047 was 76c0d6, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'StructureRefactoring' into stable

Conflicts:

molecuilder/src/Makefile.am
molecuilder/src/periodentafel.cpp

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