source: src/molecule_geometry.cpp@ d0f111

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

Changed Vector::WrapPeriodically to use matrixes instead of double*

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