source: src/molecule_geometry.cpp@ d193a2

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

Removed some BOOST_FOREACH iterations and replaced with simpler mapping on the Set

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