source: src/molecule_geometry.cpp@ a7b761b

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 Candidate_v1.7.0 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 a7b761b was a7b761b, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Merge branch 'MoleculeStartEndSwitch' into StructureRefactoring

Conflicts:

molecuilder/src/Helpers/Assert.cpp
molecuilder/src/Helpers/Assert.hpp
molecuilder/src/Legacy/oldmenu.cpp
molecuilder/src/Makefile.am
molecuilder/src/Patterns/Cacheable.hpp
molecuilder/src/Patterns/Observer.cpp
molecuilder/src/Patterns/Observer.hpp
molecuilder/src/analysis_correlation.cpp
molecuilder/src/boundary.cpp
molecuilder/src/builder.cpp
molecuilder/src/config.cpp
molecuilder/src/helpers.hpp
molecuilder/src/molecule.cpp
molecuilder/src/molecule.hpp
molecuilder/src/molecule_dynamics.cpp
molecuilder/src/molecule_fragmentation.cpp
molecuilder/src/molecule_geometry.cpp
molecuilder/src/molecule_graph.cpp
molecuilder/src/moleculelist.cpp
molecuilder/src/tesselation.cpp
molecuilder/src/unittests/AnalysisCorrelationToSurfaceUnitTest.cpp
molecuilder/src/unittests/ObserverTest.cpp
molecuilder/src/unittests/ObserverTest.hpp

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