source: src/molecule_geometry.cpp@ 4a611e

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 4a611e was bdc91e, checked in by Frederik Heber <heber@…>, 15 years ago

MEMFIXES: Tesselation routines were leaking memory.

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 18.5 KB
RevLine 
[cee0b57]1/*
2 * molecule_geometry.cpp
3 *
4 * Created on: Oct 5, 2009
5 * Author: heber
6 */
7
[112b09]8#include "Helpers/MemDebug.hpp"
9
[f66195]10#include "atom.hpp"
11#include "bond.hpp"
[cee0b57]12#include "config.hpp"
[f66195]13#include "element.hpp"
14#include "helpers.hpp"
15#include "leastsquaremin.hpp"
[e138de]16#include "log.hpp"
[cee0b57]17#include "memoryallocator.hpp"
18#include "molecule.hpp"
[b34306]19#include "World.hpp"
[ccf826]20#include "Plane.hpp"
[76c0d6]21#include <boost/foreach.hpp>
22
[cee0b57]23
24/************************************* Functions for class molecule *********************************/
25
26
27/** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
28 * \param *out output stream for debugging
29 */
[e138de]30bool molecule::CenterInBox()
[cee0b57]31{
32 bool status = true;
[e138de]33 const Vector *Center = DetermineCenterOfAll();
[eddea2]34 const Vector *CenterBox = DetermineCenterOfBox();
[5f612ee]35 double * const cell_size = World::getInstance().getDomain();
[cee0b57]36 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]37 double *Minv = InverseMatrix(M);
[cee0b57]38
39 // go through all atoms
[273382]40 ActOnAllVectors( &Vector::SubtractVector, *Center);
[eddea2]41 ActOnAllVectors( &Vector::SubtractVector, *CenterBox);
[cee0b57]42 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
43
[920c70]44 delete[](M);
45 delete[](Minv);
[cee0b57]46 delete(Center);
[52d777]47 delete(CenterBox);
[cee0b57]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 */
[e138de]55bool molecule::BoundInBox()
[cee0b57]56{
57 bool status = true;
[5f612ee]58 double * const cell_size = World::getInstance().getDomain();
[cee0b57]59 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]60 double *Minv = InverseMatrix(M);
[cee0b57]61
62 // go through all atoms
63 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
64
[920c70]65 delete[](M);
66 delete[](Minv);
[cee0b57]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 */
[e138de]74void molecule::CenterEdge(Vector *max)
[cee0b57]75{
76 Vector *min = new Vector;
77
[e138de]78// Log() << Verbose(3) << "Begin of CenterEdge." << endl;
[9879f6]79 molecule::const_iterator iter = begin(); // start at first in list
80 if (iter != end()) { //list not empty?
[cee0b57]81 for (int i=NDIM;i--;) {
[a7b761b]82 max->at(i) = (*iter)->x[i];
83 min->at(i) = (*iter)->x[i];
[cee0b57]84 }
[9879f6]85 for (; iter != end(); ++iter) {// continue with second if present
86 //(*iter)->Output(1,1,out);
[cee0b57]87 for (int i=NDIM;i--;) {
[a7b761b]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);
[cee0b57]90 }
91 }
[e138de]92// Log() << Verbose(4) << "Maximum is ";
[cee0b57]93// max->Output(out);
[e138de]94// Log() << Verbose(0) << ", Minimum is ";
[cee0b57]95// min->Output(out);
[e138de]96// Log() << Verbose(0) << endl;
[cee0b57]97 min->Scale(-1.);
[273382]98 (*max) += (*min);
[cee0b57]99 Translate(min);
100 Center.Zero();
101 }
102 delete(min);
[e138de]103// Log() << Verbose(3) << "End of CenterEdge." << endl;
[cee0b57]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 */
[e138de]110void molecule::CenterOrigin()
[cee0b57]111{
112 int Num = 0;
[9879f6]113 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]114
115 Center.Zero();
116
[9879f6]117 if (iter != end()) { //list not empty?
118 for (; iter != end(); ++iter) { // continue with second if present
[cee0b57]119 Num++;
[a7b761b]120 Center += (*iter)->x;
[cee0b57]121 }
[bdc91e]122 Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
[cee0b57]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 */
[e138de]131Vector * molecule::DetermineCenterOfAll() const
[cee0b57]132{
[9879f6]133 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]134 Vector *a = new Vector();
135 double Num = 0;
136
137 a->Zero();
138
[9879f6]139 if (iter != end()) { //list not empty?
140 for (; iter != end(); ++iter) { // continue with second if present
[15b670]141 Num++;
[1024cb]142 (*a) += (*iter)->x;
[cee0b57]143 }
[bdc91e]144 a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
[cee0b57]145 }
146 return a;
147};
148
[eddea2]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 double *M = ReturnFullMatrixforSymmetric(cell_size);
158 a->MatrixMultiplication(M);
[920c70]159 delete[](M);
[eddea2]160
161 return a;
162};
163
[cee0b57]164/** Returns vector pointing to center of gravity.
165 * \param *out output stream for debugging
166 * \return pointer to center of gravity vector
167 */
[e138de]168Vector * molecule::DetermineCenterOfGravity()
[cee0b57]169{
[9879f6]170 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]171 Vector *a = new Vector();
172 Vector tmp;
173 double Num = 0;
174
175 a->Zero();
176
[9879f6]177 if (iter != end()) { //list not empty?
178 for (; iter != end(); ++iter) { // continue with second if present
179 Num += (*iter)->type->mass;
[a7b761b]180 tmp = (*iter)->type->mass * (*iter)->x;
[273382]181 (*a) += tmp;
[cee0b57]182 }
[bdc91e]183 a->Scale(1./Num); // divide through total mass
[cee0b57]184 }
[e138de]185// Log() << Verbose(1) << "Resulting center of gravity: ";
[cee0b57]186// a->Output(out);
[e138de]187// Log() << Verbose(0) << endl;
[cee0b57]188 return a;
189};
190
191/** Centers the center of gravity of the atoms at (0,0,0).
192 * \param *out output stream for debugging
193 * \param *center return vector for translation vector
194 */
[e138de]195void molecule::CenterPeriodic()
[cee0b57]196{
197 DeterminePeriodicCenter(Center);
198};
199
200
201/** Centers the center of gravity of the atoms at (0,0,0).
202 * \param *out output stream for debugging
203 * \param *center return vector for translation vector
204 */
[e138de]205void molecule::CenterAtVector(Vector *newcenter)
[cee0b57]206{
[273382]207 Center = *newcenter;
[cee0b57]208};
209
210
211/** Scales all atoms by \a *factor.
212 * \param *factor pointer to scaling factor
[1bd79e]213 *
214 * TODO: Is this realy what is meant, i.e.
215 * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
216 * or rather
217 * x=(**factor) * x (as suggested by comment)
[cee0b57]218 */
[776b64]219void molecule::Scale(const double ** const factor)
[cee0b57]220{
[9879f6]221 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]222 for (int j=0;j<MDSteps;j++)
[a7b761b]223 (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
224 (*iter)->x.ScaleAll(*factor);
[cee0b57]225 }
226};
227
228/** Translate all atoms by given vector.
229 * \param trans[] translation vector.
230 */
231void molecule::Translate(const Vector *trans)
232{
[9879f6]233 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]234 for (int j=0;j<MDSteps;j++)
[a7b761b]235 (*iter)->Trajectory.R.at(j) += (*trans);
236 (*iter)->x += (*trans);
[cee0b57]237 }
238};
239
240/** Translate the molecule periodically in the box.
241 * \param trans[] translation vector.
242 * TODO treatment of trajetories missing
243 */
244void molecule::TranslatePeriodically(const Vector *trans)
245{
[5f612ee]246 double * const cell_size = World::getInstance().getDomain();
[cee0b57]247 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]248 double *Minv = InverseMatrix(M);
[cee0b57]249
250 // go through all atoms
[eddea2]251 ActOnAllVectors( &Vector::AddVector, *trans);
[cee0b57]252 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
253
[920c70]254 delete[](M);
255 delete[](Minv);
[cee0b57]256};
257
258
259/** Mirrors all atoms against a given plane.
260 * \param n[] normal vector of mirror plane.
261 */
262void molecule::Mirror(const Vector *n)
263{
[76c0d6]264 OBSERVE;
[ccf826]265 Plane p(*n,0);
[76c0d6]266 BOOST_FOREACH( atom* iter, atoms ){
267 (*iter->node) = p.mirrorVector(*iter->node);
[ccf826]268 }
[cee0b57]269};
270
271/** Determines center of molecule (yet not considering atom masses).
272 * \param center reference to return vector
273 */
274void molecule::DeterminePeriodicCenter(Vector &center)
275{
[5f612ee]276 double * const cell_size = World::getInstance().getDomain();
[cee0b57]277 double *matrix = ReturnFullMatrixforSymmetric(cell_size);
[7fd416]278 double *inversematrix = InverseMatrix(matrix);
[cee0b57]279 double tmp;
280 bool flag;
281 Vector Testvector, Translationvector;
282
283 do {
284 Center.Zero();
285 flag = true;
[9879f6]286 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]287#ifdef ADDHYDROGEN
[9879f6]288 if ((*iter)->type->Z != 1) {
[cee0b57]289#endif
[a7b761b]290 Testvector = (*iter)->x;
[1614174]291 Testvector.MatrixMultiplication(inversematrix);
[cee0b57]292 Translationvector.Zero();
[9879f6]293 for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
294 if ((*iter)->nr < (*Runner)->GetOtherAtom((*iter))->nr) // otherwise we shift one to, the other fro and gain nothing
[cee0b57]295 for (int j=0;j<NDIM;j++) {
[a7b761b]296 tmp = (*iter)->x[j] - (*Runner)->GetOtherAtom(*iter)->x[j];
[cee0b57]297 if ((fabs(tmp)) > BondDistance) {
298 flag = false;
[a7b761b]299 DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
[cee0b57]300 if (tmp > 0)
[0a4f7f]301 Translationvector[j] -= 1.;
[cee0b57]302 else
[0a4f7f]303 Translationvector[j] += 1.;
[cee0b57]304 }
305 }
306 }
[273382]307 Testvector += Translationvector;
[cee0b57]308 Testvector.MatrixMultiplication(matrix);
[273382]309 Center += Testvector;
[0a4f7f]310 Log() << Verbose(1) << "vector is: " << Testvector << endl;
[cee0b57]311#ifdef ADDHYDROGEN
312 // now also change all hydrogens
[9879f6]313 for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
314 if ((*Runner)->GetOtherAtom((*iter))->type->Z == 1) {
[a7b761b]315 Testvector = (*Runner)->GetOtherAtom((*iter))->x;
[1614174]316 Testvector.MatrixMultiplication(inversematrix);
[273382]317 Testvector += Translationvector;
[cee0b57]318 Testvector.MatrixMultiplication(matrix);
[273382]319 Center += Testvector;
[0a4f7f]320 Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
[cee0b57]321 }
322 }
323 }
324#endif
325 }
326 } while (!flag);
[920c70]327 delete[](matrix);
328 delete[](inversematrix);
[1614174]329
[ea7176]330 Center.Scale(1./static_cast<double>(getAtomCount()));
[cee0b57]331};
332
333/** Transforms/Rotates the given molecule into its principal axis system.
334 * \param *out output stream for debugging
335 * \param DoRotate whether to rotate (true) or only to determine the PAS.
336 * TODO treatment of trajetories missing
337 */
[e138de]338void molecule::PrincipalAxisSystem(bool DoRotate)
[cee0b57]339{
340 double InertiaTensor[NDIM*NDIM];
[e138de]341 Vector *CenterOfGravity = DetermineCenterOfGravity();
[cee0b57]342
[e138de]343 CenterPeriodic();
[cee0b57]344
345 // reset inertia tensor
346 for(int i=0;i<NDIM*NDIM;i++)
347 InertiaTensor[i] = 0.;
348
349 // sum up inertia tensor
[9879f6]350 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]351 Vector x = (*iter)->x;
[cee0b57]352 //x.SubtractVector(CenterOfGravity);
[a7b761b]353 InertiaTensor[0] += (*iter)->type->mass*(x[1]*x[1] + x[2]*x[2]);
354 InertiaTensor[1] += (*iter)->type->mass*(-x[0]*x[1]);
355 InertiaTensor[2] += (*iter)->type->mass*(-x[0]*x[2]);
356 InertiaTensor[3] += (*iter)->type->mass*(-x[1]*x[0]);
357 InertiaTensor[4] += (*iter)->type->mass*(x[0]*x[0] + x[2]*x[2]);
358 InertiaTensor[5] += (*iter)->type->mass*(-x[1]*x[2]);
359 InertiaTensor[6] += (*iter)->type->mass*(-x[2]*x[0]);
360 InertiaTensor[7] += (*iter)->type->mass*(-x[2]*x[1]);
361 InertiaTensor[8] += (*iter)->type->mass*(x[0]*x[0] + x[1]*x[1]);
[cee0b57]362 }
363 // print InertiaTensor for debugging
[a67d19]364 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
[cee0b57]365 for(int i=0;i<NDIM;i++) {
366 for(int j=0;j<NDIM;j++)
[a67d19]367 DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
368 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]369 }
[a67d19]370 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]371
372 // diagonalize to determine principal axis system
373 gsl_eigen_symmv_workspace *T = gsl_eigen_symmv_alloc(NDIM);
374 gsl_matrix_view m = gsl_matrix_view_array(InertiaTensor, NDIM, NDIM);
375 gsl_vector *eval = gsl_vector_alloc(NDIM);
376 gsl_matrix *evec = gsl_matrix_alloc(NDIM, NDIM);
377 gsl_eigen_symmv(&m.matrix, eval, evec, T);
378 gsl_eigen_symmv_free(T);
379 gsl_eigen_symmv_sort(eval, evec, GSL_EIGEN_SORT_ABS_DESC);
380
381 for(int i=0;i<NDIM;i++) {
[a67d19]382 DoLog(1) && (Log() << Verbose(1) << "eigenvalue = " << gsl_vector_get(eval, i));
383 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);
[cee0b57]384 }
385
386 // check whether we rotate or not
387 if (DoRotate) {
[a67d19]388 DoLog(1) && (Log() << Verbose(1) << "Transforming molecule into PAS ... ");
[cee0b57]389 // the eigenvectors specify the transformation matrix
390 ActOnAllVectors( &Vector::MatrixMultiplication, (const double *) evec->data );
[a67d19]391 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[cee0b57]392
393 // summing anew for debugging (resulting matrix has to be diagonal!)
394 // reset inertia tensor
395 for(int i=0;i<NDIM*NDIM;i++)
396 InertiaTensor[i] = 0.;
397
398 // sum up inertia tensor
[9879f6]399 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]400 Vector x = (*iter)->x;
401 InertiaTensor[0] += (*iter)->type->mass*(x[1]*x[1] + x[2]*x[2]);
402 InertiaTensor[1] += (*iter)->type->mass*(-x[0]*x[1]);
403 InertiaTensor[2] += (*iter)->type->mass*(-x[0]*x[2]);
404 InertiaTensor[3] += (*iter)->type->mass*(-x[1]*x[0]);
405 InertiaTensor[4] += (*iter)->type->mass*(x[0]*x[0] + x[2]*x[2]);
406 InertiaTensor[5] += (*iter)->type->mass*(-x[1]*x[2]);
407 InertiaTensor[6] += (*iter)->type->mass*(-x[2]*x[0]);
408 InertiaTensor[7] += (*iter)->type->mass*(-x[2]*x[1]);
409 InertiaTensor[8] += (*iter)->type->mass*(x[0]*x[0] + x[1]*x[1]);
[cee0b57]410 }
411 // print InertiaTensor for debugging
[a67d19]412 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
[cee0b57]413 for(int i=0;i<NDIM;i++) {
414 for(int j=0;j<NDIM;j++)
[a67d19]415 DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
416 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]417 }
[a67d19]418 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]419 }
420
421 // free everything
422 delete(CenterOfGravity);
423 gsl_vector_free(eval);
424 gsl_matrix_free(evec);
425};
426
427
428/** Align all atoms in such a manner that given vector \a *n is along z axis.
429 * \param n[] alignment vector.
430 */
431void molecule::Align(Vector *n)
432{
433 double alpha, tmp;
434 Vector z_axis;
[0a4f7f]435 z_axis[0] = 0.;
436 z_axis[1] = 0.;
437 z_axis[2] = 1.;
[cee0b57]438
439 // rotate on z-x plane
[a67d19]440 DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
[0a4f7f]441 alpha = atan(-n->at(0)/n->at(2));
[a67d19]442 DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
[9879f6]443 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]444 tmp = (*iter)->x[0];
445 (*iter)->x[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->x[2];
446 (*iter)->x[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->x[2];
[cee0b57]447 for (int j=0;j<MDSteps;j++) {
[a7b761b]448 tmp = (*iter)->Trajectory.R.at(j)[0];
449 (*iter)->Trajectory.R.at(j)[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
450 (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
[cee0b57]451 }
452 }
453 // rotate n vector
[0a4f7f]454 tmp = n->at(0);
455 n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
456 n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
[8cbb97]457 DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
[cee0b57]458
459 // rotate on z-y plane
[0a4f7f]460 alpha = atan(-n->at(1)/n->at(2));
[a67d19]461 DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
[9879f6]462 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]463 tmp = (*iter)->x[1];
464 (*iter)->x[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->x[2];
465 (*iter)->x[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->x[2];
[cee0b57]466 for (int j=0;j<MDSteps;j++) {
[a7b761b]467 tmp = (*iter)->Trajectory.R.at(j)[1];
468 (*iter)->Trajectory.R.at(j)[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
469 (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
[cee0b57]470 }
471 }
472 // rotate n vector (for consistency check)
[0a4f7f]473 tmp = n->at(1);
474 n->at(1) = cos(alpha) * tmp + sin(alpha) * n->at(2);
475 n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
[cee0b57]476
477
[8cbb97]478 DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
[a67d19]479 DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
[cee0b57]480};
481
482
483/** Calculates sum over least square distance to line hidden in \a *x.
484 * \param *x offset and direction vector
485 * \param *params pointer to lsq_params structure
486 * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
487 */
488double LeastSquareDistance (const gsl_vector * x, void * params)
489{
490 double res = 0, t;
491 Vector a,b,c,d;
492 struct lsq_params *par = (struct lsq_params *)params;
493
494 // initialize vectors
[0a4f7f]495 a[0] = gsl_vector_get(x,0);
496 a[1] = gsl_vector_get(x,1);
497 a[2] = gsl_vector_get(x,2);
498 b[0] = gsl_vector_get(x,3);
499 b[1] = gsl_vector_get(x,4);
500 b[2] = gsl_vector_get(x,5);
[cee0b57]501 // go through all atoms
[9879f6]502 for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
503 if ((*iter)->type == ((struct lsq_params *)params)->type) { // for specific type
[a7b761b]504 c = (*iter)->x - a;
[273382]505 t = c.ScalarProduct(b); // get direction parameter
506 d = t*b; // and create vector
507 c -= d; // ... yielding distance vector
508 res += d.ScalarProduct(d); // add squared distance
[cee0b57]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.