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
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"
[cee0b57]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 */
[e138de]25bool molecule::CenterInBox()
[cee0b57]26{
27 bool status = true;
[e138de]28 const Vector *Center = DetermineCenterOfAll();
[5f612ee]29 double * const cell_size = World::getInstance().getDomain();
[cee0b57]30 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]31 double *Minv = InverseMatrix(M);
[cee0b57]32
33 // go through all atoms
[273382]34 ActOnAllVectors( &Vector::SubtractVector, *Center);
[cee0b57]35 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
36
[1614174]37 Free(&M);
38 Free(&Minv);
[cee0b57]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 */
[e138de]47bool molecule::BoundInBox()
[cee0b57]48{
49 bool status = true;
[5f612ee]50 double * const cell_size = World::getInstance().getDomain();
[cee0b57]51 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]52 double *Minv = InverseMatrix(M);
[cee0b57]53
54 // go through all atoms
55 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
56
[1614174]57 Free(&M);
58 Free(&Minv);
[cee0b57]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 */
[e138de]66void molecule::CenterEdge(Vector *max)
[cee0b57]67{
68 Vector *min = new Vector;
69
[e138de]70// Log() << Verbose(3) << "Begin of CenterEdge." << endl;
[9879f6]71 molecule::const_iterator iter = begin(); // start at first in list
72 if (iter != end()) { //list not empty?
[cee0b57]73 for (int i=NDIM;i--;) {
[a7b761b]74 max->at(i) = (*iter)->x[i];
75 min->at(i) = (*iter)->x[i];
[cee0b57]76 }
[9879f6]77 for (; iter != end(); ++iter) {// continue with second if present
78 //(*iter)->Output(1,1,out);
[cee0b57]79 for (int i=NDIM;i--;) {
[a7b761b]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);
[cee0b57]82 }
83 }
[e138de]84// Log() << Verbose(4) << "Maximum is ";
[cee0b57]85// max->Output(out);
[e138de]86// Log() << Verbose(0) << ", Minimum is ";
[cee0b57]87// min->Output(out);
[e138de]88// Log() << Verbose(0) << endl;
[cee0b57]89 min->Scale(-1.);
[273382]90 (*max) += (*min);
[cee0b57]91 Translate(min);
92 Center.Zero();
93 }
94 delete(min);
[e138de]95// Log() << Verbose(3) << "End of CenterEdge." << endl;
[cee0b57]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 */
[e138de]102void molecule::CenterOrigin()
[cee0b57]103{
104 int Num = 0;
[9879f6]105 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]106
107 Center.Zero();
108
[9879f6]109 if (iter != end()) { //list not empty?
110 for (; iter != end(); ++iter) { // continue with second if present
[cee0b57]111 Num++;
[a7b761b]112 Center += (*iter)->x;
[cee0b57]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 */
[e138de]123Vector * molecule::DetermineCenterOfAll() const
[cee0b57]124{
[9879f6]125 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]126 Vector *a = new Vector();
127 Vector tmp;
128 double Num = 0;
129
130 a->Zero();
131
[9879f6]132 if (iter != end()) { //list not empty?
133 for (; iter != end(); ++iter) { // continue with second if present
[cee0b57]134 Num += 1.;
[a7b761b]135 tmp = (*iter)->x;
[273382]136 (*a) += tmp;
[cee0b57]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 */
[e138de]147Vector * molecule::DetermineCenterOfGravity()
[cee0b57]148{
[9879f6]149 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]150 Vector *a = new Vector();
151 Vector tmp;
152 double Num = 0;
153
154 a->Zero();
155
[9879f6]156 if (iter != end()) { //list not empty?
157 for (; iter != end(); ++iter) { // continue with second if present
158 Num += (*iter)->type->mass;
[a7b761b]159 tmp = (*iter)->type->mass * (*iter)->x;
[273382]160 (*a) += tmp;
[cee0b57]161 }
162 a->Scale(-1./Num); // divide through total mass (and sign for direction)
163 }
[e138de]164// Log() << Verbose(1) << "Resulting center of gravity: ";
[cee0b57]165// a->Output(out);
[e138de]166// Log() << Verbose(0) << endl;
[cee0b57]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 */
[e138de]174void molecule::CenterPeriodic()
[cee0b57]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 */
[e138de]184void molecule::CenterAtVector(Vector *newcenter)
[cee0b57]185{
[273382]186 Center = *newcenter;
[cee0b57]187};
188
189
190/** Scales all atoms by \a *factor.
191 * \param *factor pointer to scaling factor
[1bd79e]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)
[cee0b57]197 */
[776b64]198void molecule::Scale(const double ** const factor)
[cee0b57]199{
[9879f6]200 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]201 for (int j=0;j<MDSteps;j++)
[a7b761b]202 (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
203 (*iter)->x.ScaleAll(*factor);
[cee0b57]204 }
205};
206
207/** Translate all atoms by given vector.
208 * \param trans[] translation vector.
209 */
210void molecule::Translate(const Vector *trans)
211{
[9879f6]212 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]213 for (int j=0;j<MDSteps;j++)
[a7b761b]214 (*iter)->Trajectory.R.at(j) += (*trans);
215 (*iter)->x += (*trans);
[cee0b57]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{
[5f612ee]225 double * const cell_size = World::getInstance().getDomain();
[cee0b57]226 double *M = ReturnFullMatrixforSymmetric(cell_size);
[99593f]227 double *Minv = InverseMatrix(M);
[cee0b57]228
229 // go through all atoms
[273382]230 ActOnAllVectors( &Vector::SubtractVector, *trans);
[cee0b57]231 ActOnAllVectors( &Vector::WrapPeriodically, (const double *)M, (const double *)Minv);
232
[1614174]233 Free(&M);
234 Free(&Minv);
[cee0b57]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{
[273382]243 ActOnAllVectors( &Vector::Mirror, *n );
[cee0b57]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{
[5f612ee]251 double * const cell_size = World::getInstance().getDomain();
[cee0b57]252 double *matrix = ReturnFullMatrixforSymmetric(cell_size);
[1614174]253 double *inversematrix = InverseMatrix(cell_size);
[cee0b57]254 double tmp;
255 bool flag;
256 Vector Testvector, Translationvector;
257
258 do {
259 Center.Zero();
260 flag = true;
[9879f6]261 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]262#ifdef ADDHYDROGEN
[9879f6]263 if ((*iter)->type->Z != 1) {
[cee0b57]264#endif
[a7b761b]265 Testvector = (*iter)->x;
[1614174]266 Testvector.MatrixMultiplication(inversematrix);
[cee0b57]267 Translationvector.Zero();
[9879f6]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
[cee0b57]270 for (int j=0;j<NDIM;j++) {
[a7b761b]271 tmp = (*iter)->x[j] - (*Runner)->GetOtherAtom(*iter)->x[j];
[cee0b57]272 if ((fabs(tmp)) > BondDistance) {
273 flag = false;
[a7b761b]274 DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
[cee0b57]275 if (tmp > 0)
[0a4f7f]276 Translationvector[j] -= 1.;
[cee0b57]277 else
[0a4f7f]278 Translationvector[j] += 1.;
[cee0b57]279 }
280 }
281 }
[273382]282 Testvector += Translationvector;
[cee0b57]283 Testvector.MatrixMultiplication(matrix);
[273382]284 Center += Testvector;
[0a4f7f]285 Log() << Verbose(1) << "vector is: " << Testvector << endl;
[cee0b57]286#ifdef ADDHYDROGEN
287 // now also change all hydrogens
[9879f6]288 for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
289 if ((*Runner)->GetOtherAtom((*iter))->type->Z == 1) {
[a7b761b]290 Testvector = (*Runner)->GetOtherAtom((*iter))->x;
[1614174]291 Testvector.MatrixMultiplication(inversematrix);
[273382]292 Testvector += Translationvector;
[cee0b57]293 Testvector.MatrixMultiplication(matrix);
[273382]294 Center += Testvector;
[0a4f7f]295 Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
[cee0b57]296 }
297 }
298 }
299#endif
300 }
301 } while (!flag);
302 Free(&matrix);
[1614174]303 Free(&inversematrix);
304
[ea7176]305 Center.Scale(1./static_cast<double>(getAtomCount()));
[cee0b57]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 */
[e138de]313void molecule::PrincipalAxisSystem(bool DoRotate)
[cee0b57]314{
315 double InertiaTensor[NDIM*NDIM];
[e138de]316 Vector *CenterOfGravity = DetermineCenterOfGravity();
[cee0b57]317
[e138de]318 CenterPeriodic();
[cee0b57]319
320 // reset inertia tensor
321 for(int i=0;i<NDIM*NDIM;i++)
322 InertiaTensor[i] = 0.;
323
324 // sum up inertia tensor
[9879f6]325 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]326 Vector x = (*iter)->x;
[cee0b57]327 //x.SubtractVector(CenterOfGravity);
[a7b761b]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]);
[cee0b57]337 }
338 // print InertiaTensor for debugging
[a67d19]339 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
[cee0b57]340 for(int i=0;i<NDIM;i++) {
341 for(int j=0;j<NDIM;j++)
[a67d19]342 DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
343 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]344 }
[a67d19]345 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]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++) {
[a67d19]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);
[cee0b57]359 }
360
361 // check whether we rotate or not
362 if (DoRotate) {
[a67d19]363 DoLog(1) && (Log() << Verbose(1) << "Transforming molecule into PAS ... ");
[cee0b57]364 // the eigenvectors specify the transformation matrix
365 ActOnAllVectors( &Vector::MatrixMultiplication, (const double *) evec->data );
[a67d19]366 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
[cee0b57]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
[9879f6]374 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]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]);
[cee0b57]385 }
386 // print InertiaTensor for debugging
[a67d19]387 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor is:" << endl);
[cee0b57]388 for(int i=0;i<NDIM;i++) {
389 for(int j=0;j<NDIM;j++)
[a67d19]390 DoLog(0) && (Log() << Verbose(0) << InertiaTensor[i*NDIM+j] << " ");
391 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]392 }
[a67d19]393 DoLog(0) && (Log() << Verbose(0) << endl);
[cee0b57]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;
[0a4f7f]410 z_axis[0] = 0.;
411 z_axis[1] = 0.;
412 z_axis[2] = 1.;
[cee0b57]413
414 // rotate on z-x plane
[a67d19]415 DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
[0a4f7f]416 alpha = atan(-n->at(0)/n->at(2));
[a67d19]417 DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
[9879f6]418 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]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];
[cee0b57]422 for (int j=0;j<MDSteps;j++) {
[a7b761b]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];
[cee0b57]426 }
427 }
428 // rotate n vector
[0a4f7f]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);
[8cbb97]432 DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
[cee0b57]433
434 // rotate on z-y plane
[0a4f7f]435 alpha = atan(-n->at(1)/n->at(2));
[a67d19]436 DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
[9879f6]437 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[a7b761b]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];
[cee0b57]441 for (int j=0;j<MDSteps;j++) {
[a7b761b]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];
[cee0b57]445 }
446 }
447 // rotate n vector (for consistency check)
[0a4f7f]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);
[cee0b57]451
452
[8cbb97]453 DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
[a67d19]454 DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
[cee0b57]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
[0a4f7f]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);
[cee0b57]476 // go through all atoms
[9879f6]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
[a7b761b]479 c = (*iter)->x - a;
[273382]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
[cee0b57]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.