source: src/molecule_geometry.cpp@ 3dcb1f

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

Made the world store the cell_size within a Box object.

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