source: src/molecule_geometry.cpp@ 76c0d6

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 76c0d6 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
RevLine 
[cee0b57]1/*
2 * molecule_geometry.cpp
3 *
4 * Created on: Oct 5, 2009
5 * Author: heber
6 */
7
[f66195]8#include "atom.hpp"
9#include "bond.hpp"
[cee0b57]10#include "config.hpp"
[f66195]11#include "element.hpp"
12#include "helpers.hpp"
13#include "leastsquaremin.hpp"
[e138de]14#include "log.hpp"
[cee0b57]15#include "memoryallocator.hpp"
16#include "molecule.hpp"
[b34306]17#include "World.hpp"
[ccf826]18#include "Plane.hpp"
[76c0d6]19#include <boost/foreach.hpp>
20
[cee0b57]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 */
[e138de]28bool molecule::CenterInBox()
[cee0b57]29{
30 bool status = true;
[e138de]31 const Vector *Center = DetermineCenterOfAll();
[eddea2]32 const Vector *CenterBox = DetermineCenterOfBox();
[5f612ee]33 double * const cell_size = World::getInstance().getDomain();
[cee0b57]34 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]35 double *Minv = InverseMatrix(M);
[cee0b57]36
37 // go through all atoms
[273382]38 ActOnAllVectors( &Vector::SubtractVector, *Center);
[eddea2]39 ActOnAllVectors( &Vector::SubtractVector, *CenterBox);
[cee0b57]40 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
41
[920c70]42 delete[](M);
43 delete[](Minv);
[cee0b57]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 */
[e138de]52bool molecule::BoundInBox()
[cee0b57]53{
54 bool status = true;
[5f612ee]55 double * const cell_size = World::getInstance().getDomain();
[cee0b57]56 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]57 double *Minv = InverseMatrix(M);
[cee0b57]58
59 // go through all atoms
60 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
61
[920c70]62 delete[](M);
63 delete[](Minv);
[cee0b57]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 */
[e138de]71void molecule::CenterEdge(Vector *max)
[cee0b57]72{
73 Vector *min = new Vector;
74
[e138de]75// Log() << Verbose(3) << "Begin of CenterEdge." << endl;
[9879f6]76 molecule::const_iterator iter = begin(); // start at first in list
77 if (iter != end()) { //list not empty?
[cee0b57]78 for (int i=NDIM;i--;) {
[a7b761b]79 max->at(i) = (*iter)->x[i];
80 min->at(i) = (*iter)->x[i];
[cee0b57]81 }
[9879f6]82 for (; iter != end(); ++iter) {// continue with second if present
83 //(*iter)->Output(1,1,out);
[cee0b57]84 for (int i=NDIM;i--;) {
[a7b761b]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);
[cee0b57]87 }
88 }
[e138de]89// Log() << Verbose(4) << "Maximum is ";
[cee0b57]90// max->Output(out);
[e138de]91// Log() << Verbose(0) << ", Minimum is ";
[cee0b57]92// min->Output(out);
[e138de]93// Log() << Verbose(0) << endl;
[cee0b57]94 min->Scale(-1.);
[273382]95 (*max) += (*min);
[cee0b57]96 Translate(min);
97 Center.Zero();
98 }
99 delete(min);
[e138de]100// Log() << Verbose(3) << "End of CenterEdge." << endl;
[cee0b57]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 */
[e138de]107void molecule::CenterOrigin()
[cee0b57]108{
109 int Num = 0;
[9879f6]110 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]111
112 Center.Zero();
113
[9879f6]114 if (iter != end()) { //list not empty?
115 for (; iter != end(); ++iter) { // continue with second if present
[cee0b57]116 Num++;
[a7b761b]117 Center += (*iter)->x;
[cee0b57]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 */
[e138de]128Vector * molecule::DetermineCenterOfAll() const
[cee0b57]129{
[9879f6]130 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]131 Vector *a = new Vector();
132 double Num = 0;
133
134 a->Zero();
135
[9879f6]136 if (iter != end()) { //list not empty?
137 for (; iter != end(); ++iter) { // continue with second if present
[15b670]138 Num++;
[1024cb]139 (*a) += (*iter)->x;
[cee0b57]140 }
141 a->Scale(1./Num); // divide through total mass (and sign for direction)
142 }
143 return a;
144};
145
[eddea2]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);
[920c70]156 delete[](M);
[eddea2]157
158 return a;
159};
160
[cee0b57]161/** Returns vector pointing to center of gravity.
162 * \param *out output stream for debugging
163 * \return pointer to center of gravity vector
164 */
[e138de]165Vector * molecule::DetermineCenterOfGravity()
[cee0b57]166{
[9879f6]167 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]168 Vector *a = new Vector();
169 Vector tmp;
170 double Num = 0;
171
172 a->Zero();
173
[9879f6]174 if (iter != end()) { //list not empty?
175 for (; iter != end(); ++iter) { // continue with second if present
176 Num += (*iter)->type->mass;
[a7b761b]177 tmp = (*iter)->type->mass * (*iter)->x;
[273382]178 (*a) += tmp;
[cee0b57]179 }
[15b670]180 a->Scale(1./Num); // divide through total mass (and sign for direction)
[cee0b57]181 }
[e138de]182// Log() << Verbose(1) << "Resulting center of gravity: ";
[cee0b57]183// a->Output(out);
[e138de]184// Log() << Verbose(0) << endl;
[cee0b57]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 */
[e138de]192void molecule::CenterPeriodic()
[cee0b57]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 */
[e138de]202void molecule::CenterAtVector(Vector *newcenter)
[cee0b57]203{
[273382]204 Center = *newcenter;
[cee0b57]205};
206
207
208/** Scales all atoms by \a *factor.
209 * \param *factor pointer to scaling factor
[1bd79e]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)
[cee0b57]215 */
[776b64]216void molecule::Scale(const double ** const factor)
[cee0b57]217{
[9879f6]218 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]219 for (int j=0;j<MDSteps;j++)
[a7b761b]220 (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
221 (*iter)->x.ScaleAll(*factor);
[cee0b57]222 }
223};
224
225/** Translate all atoms by given vector.
226 * \param trans[] translation vector.
227 */
228void molecule::Translate(const Vector *trans)
229{
[9879f6]230 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]231 for (int j=0;j<MDSteps;j++)
[a7b761b]232 (*iter)->Trajectory.R.at(j) += (*trans);
233 (*iter)->x += (*trans);
[cee0b57]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{
[5f612ee]243 double * const cell_size = World::getInstance().getDomain();
[cee0b57]244 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]245 double *Minv = InverseMatrix(M);
[cee0b57]246
247 // go through all atoms
[eddea2]248 ActOnAllVectors( &Vector::AddVector, *trans);
[cee0b57]249 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
250
[920c70]251 delete[](M);
252 delete[](Minv);
[cee0b57]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{
[76c0d6]261 OBSERVE;
[ccf826]262 Plane p(*n,0);
[76c0d6]263 BOOST_FOREACH( atom* iter, atoms ){
264 (*iter->node) = p.mirrorVector(*iter->node);
[ccf826]265 }
[cee0b57]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{
[5f612ee]273 double * const cell_size = World::getInstance().getDomain();
[cee0b57]274 double *matrix = ReturnFullMatrixforSymmetric(cell_size);
[7fd416]275 double *inversematrix = InverseMatrix(matrix);
[cee0b57]276 double tmp;
277 bool flag;
278 Vector Testvector, Translationvector;
279
280 do {
281 Center.Zero();
282 flag = true;
[9879f6]283 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]284#ifdef ADDHYDROGEN
[9879f6]285 if ((*iter)->type->Z != 1) {
[cee0b57]286#endif
[a7b761b]287 Testvector = (*iter)->x;
[1614174]288 Testvector.MatrixMultiplication(inversematrix);
[cee0b57]289 Translationvector.Zero();
[9879f6]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
[cee0b57]292 for (int j=0;j<NDIM;j++) {
[a7b761b]293 tmp = (*iter)->x[j] - (*Runner)->GetOtherAtom(*iter)->x[j];
[cee0b57]294 if ((fabs(tmp)) > BondDistance) {
295 flag = false;
[a7b761b]296 DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
[cee0b57]297 if (tmp > 0)
[0a4f7f]298 Translationvector[j] -= 1.;
[cee0b57]299 else
[0a4f7f]300 Translationvector[j] += 1.;
[cee0b57]301 }
302 }
303 }
[273382]304 Testvector += Translationvector;
[cee0b57]305 Testvector.MatrixMultiplication(matrix);
[273382]306 Center += Testvector;
[0a4f7f]307 Log() << Verbose(1) << "vector is: " << Testvector << endl;
[cee0b57]308#ifdef ADDHYDROGEN
309 // now also change all hydrogens
[9879f6]310 for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
311 if ((*Runner)->GetOtherAtom((*iter))->type->Z == 1) {
[a7b761b]312 Testvector = (*Runner)->GetOtherAtom((*iter))->x;
[1614174]313 Testvector.MatrixMultiplication(inversematrix);
[273382]314 Testvector += Translationvector;
[cee0b57]315 Testvector.MatrixMultiplication(matrix);
[273382]316 Center += Testvector;
[0a4f7f]317 Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
[cee0b57]318 }
319 }
320 }
321#endif
322 }
323 } while (!flag);
[920c70]324 delete[](matrix);
325 delete[](inversematrix);
[1614174]326
[ea7176]327 Center.Scale(1./static_cast<double>(getAtomCount()));
[cee0b57]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 */
[e138de]335void molecule::PrincipalAxisSystem(bool DoRotate)
[cee0b57]336{
337 double InertiaTensor[NDIM*NDIM];
[e138de]338 Vector *CenterOfGravity = DetermineCenterOfGravity();
[cee0b57]339
[e138de]340 CenterPeriodic();
[cee0b57]341
342 // reset inertia tensor
343 for(int i=0;i<NDIM*NDIM;i++)
344 InertiaTensor[i] = 0.;
345
346 // sum up inertia tensor
[9879f6]347 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]348 Vector x = (*iter)->x;
[cee0b57]349 //x.SubtractVector(CenterOfGravity);
[a7b761b]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]);
[cee0b57]359 }
360 // print InertiaTensor for debugging
[a67d19]361 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
[cee0b57]362 for(int i=0;i<NDIM;i++) {
363 for(int j=0;j<NDIM;j++)
[a67d19]364 DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
365 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]366 }
[a67d19]367 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]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++) {
[a67d19]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);
[cee0b57]381 }
382
383 // check whether we rotate or not
384 if (DoRotate) {
[a67d19]385 DoLog(1) && (Log() << Verbose(1) << "Transforming molecule into PAS ... ");
[cee0b57]386 // the eigenvectors specify the transformation matrix
387 ActOnAllVectors( &Vector::MatrixMultiplication, (const double *) evec->data );
[a67d19]388 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[cee0b57]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
[9879f6]396 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]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]);
[cee0b57]407 }
408 // print InertiaTensor for debugging
[a67d19]409 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
[cee0b57]410 for(int i=0;i<NDIM;i++) {
411 for(int j=0;j<NDIM;j++)
[a67d19]412 DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
413 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]414 }
[a67d19]415 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]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;
[0a4f7f]432 z_axis[0] = 0.;
433 z_axis[1] = 0.;
434 z_axis[2] = 1.;
[cee0b57]435
436 // rotate on z-x plane
[a67d19]437 DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
[0a4f7f]438 alpha = atan(-n->at(0)/n->at(2));
[a67d19]439 DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
[9879f6]440 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]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];
[cee0b57]444 for (int j=0;j<MDSteps;j++) {
[a7b761b]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];
[cee0b57]448 }
449 }
450 // rotate n vector
[0a4f7f]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);
[8cbb97]454 DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
[cee0b57]455
456 // rotate on z-y plane
[0a4f7f]457 alpha = atan(-n->at(1)/n->at(2));
[a67d19]458 DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
[9879f6]459 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]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];
[cee0b57]463 for (int j=0;j<MDSteps;j++) {
[a7b761b]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];
[cee0b57]467 }
468 }
469 // rotate n vector (for consistency check)
[0a4f7f]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);
[cee0b57]473
474
[8cbb97]475 DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
[a67d19]476 DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
[cee0b57]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
[0a4f7f]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);
[cee0b57]498 // go through all atoms
[9879f6]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
[a7b761b]501 c = (*iter)->x - a;
[273382]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
[cee0b57]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.