source: src/molecule_geometry.cpp@ 552597

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 552597 was 255829, checked in by Frederik Heber <heber@…>, 14 years ago

Removed Helpers.hpp, deleted Helpers.cpp and libMoleCuilderHelpers.la is history.

  • defs.cpp is now compiled into libmolecuilder.la.
  • ShapeUnitTest alone needs defs.cpp.
  • Most changes are removal of Helpers/helpers.hpp.
  • performCriticalExit() now inline function in Helpers/helpers.hpp.
  • also inclusion possible where performCriticalExit() is needed.
  • Helpers/helpers.hpp does not include defs.hpp anymore and this causes lots of missing Helpers/defs.hpp, CodePatterns/Log.hpp and alikes.
  • removed src/Helpers from configure.ac.
  • Property mode set to 100644
File size: 18.0 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
[cee0b57]8/*
9 * molecule_geometry.cpp
10 *
11 * Created on: Oct 5, 2009
12 * Author: heber
13 */
14
[bf3817]15// include config.h
[aafd77]16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
[bf3817]19
[ad011c]20#include "CodePatterns/MemDebug.hpp"
[aafd77]21
[129204]22#include "atom.hpp"
23#include "Bond/bond.hpp"
24#include "Box.hpp"
[ad011c]25#include "CodePatterns/Log.hpp"
26#include "CodePatterns/Verbose.hpp"
[cee0b57]27#include "config.hpp"
[f66195]28#include "element.hpp"
[129204]29#include "Graph/BondGraph.hpp"
[13d150]30#include "LinearAlgebra/leastsquaremin.hpp"
[129204]31#include "LinearAlgebra/Line.hpp"
32#include "LinearAlgebra/RealSpaceMatrix.hpp"
33#include "LinearAlgebra/Plane.hpp"
[cee0b57]34#include "molecule.hpp"
[b34306]35#include "World.hpp"
[6e5084]36
[76c0d6]37#include <boost/foreach.hpp>
38
[aafd77]39#include <gsl/gsl_eigen.h>
40#include <gsl/gsl_multimin.h>
41
[cee0b57]42
43/************************************* Functions for class molecule *********************************/
44
45
46/** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
47 * \param *out output stream for debugging
48 */
[e138de]49bool molecule::CenterInBox()
[cee0b57]50{
51 bool status = true;
[e138de]52 const Vector *Center = DetermineCenterOfAll();
[eddea2]53 const Vector *CenterBox = DetermineCenterOfBox();
[f429d7]54 Box &domain = World::getInstance().getDomain();
[cee0b57]55
56 // go through all atoms
[d0f111]57 BOOST_FOREACH(atom* iter, atoms){
[6625c3]58 std::cout << "atom before is at " << *iter << std::endl;
[d74077]59 *iter -= *Center;
[6625c3]60 *iter += *CenterBox;
61 std::cout << "atom after is at " << *iter << std::endl;
[d0f111]62 }
[0632c5]63 atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
[cee0b57]64
65 delete(Center);
[52d777]66 delete(CenterBox);
[cee0b57]67 return status;
68};
69
70
71/** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
72 * \param *out output stream for debugging
73 */
[e138de]74bool molecule::BoundInBox()
[cee0b57]75{
76 bool status = true;
[f429d7]77 Box &domain = World::getInstance().getDomain();
[cee0b57]78
79 // go through all atoms
[0632c5]80 atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
[cee0b57]81
82 return status;
83};
84
85/** Centers the edge of the atoms at (0,0,0).
86 * \param *out output stream for debugging
87 * \param *max coordinates of other edge, specifying box dimensions.
88 */
[e138de]89void molecule::CenterEdge(Vector *max)
[cee0b57]90{
91 Vector *min = new Vector;
92
[e138de]93// Log() << Verbose(3) << "Begin of CenterEdge." << endl;
[9879f6]94 molecule::const_iterator iter = begin(); // start at first in list
95 if (iter != end()) { //list not empty?
[cee0b57]96 for (int i=NDIM;i--;) {
[d74077]97 max->at(i) = (*iter)->at(i);
98 min->at(i) = (*iter)->at(i);
[cee0b57]99 }
[9879f6]100 for (; iter != end(); ++iter) {// continue with second if present
101 //(*iter)->Output(1,1,out);
[cee0b57]102 for (int i=NDIM;i--;) {
[d74077]103 max->at(i) = (max->at(i) < (*iter)->at(i)) ? (*iter)->at(i) : max->at(i);
104 min->at(i) = (min->at(i) > (*iter)->at(i)) ? (*iter)->at(i) : min->at(i);
[cee0b57]105 }
106 }
[e138de]107// Log() << Verbose(4) << "Maximum is ";
[cee0b57]108// max->Output(out);
[e138de]109// Log() << Verbose(0) << ", Minimum is ";
[cee0b57]110// min->Output(out);
[e138de]111// Log() << Verbose(0) << endl;
[cee0b57]112 min->Scale(-1.);
[273382]113 (*max) += (*min);
[cee0b57]114 Translate(min);
115 }
116 delete(min);
[e138de]117// Log() << Verbose(3) << "End of CenterEdge." << endl;
[cee0b57]118};
119
120/** Centers the center of the atoms at (0,0,0).
121 * \param *out output stream for debugging
122 * \param *center return vector for translation vector
123 */
[e138de]124void molecule::CenterOrigin()
[cee0b57]125{
126 int Num = 0;
[9879f6]127 molecule::const_iterator iter = begin(); // start at first in list
[1883f9]128 Vector Center;
[cee0b57]129
130 Center.Zero();
[9879f6]131 if (iter != end()) { //list not empty?
132 for (; iter != end(); ++iter) { // continue with second if present
[cee0b57]133 Num++;
[d74077]134 Center += (*iter)->getPosition();
[cee0b57]135 }
[bdc91e]136 Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
[cee0b57]137 Translate(&Center);
138 }
139};
140
141/** Returns vector pointing to center of all atoms.
142 * \return pointer to center of all vector
143 */
[e138de]144Vector * molecule::DetermineCenterOfAll() const
[cee0b57]145{
[9879f6]146 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]147 Vector *a = new Vector();
148 double Num = 0;
149
150 a->Zero();
151
[9879f6]152 if (iter != end()) { //list not empty?
153 for (; iter != end(); ++iter) { // continue with second if present
[15b670]154 Num++;
[d74077]155 (*a) += (*iter)->getPosition();
[cee0b57]156 }
[bdc91e]157 a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
[cee0b57]158 }
159 return a;
160};
161
[eddea2]162/** Returns vector pointing to center of the domain.
163 * \return pointer to center of the domain
164 */
165Vector * molecule::DetermineCenterOfBox() const
166{
167 Vector *a = new Vector(0.5,0.5,0.5);
[cca9ef]168 const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
[5108e1]169 (*a) *= M;
[eddea2]170 return a;
171};
172
[cee0b57]173/** Returns vector pointing to center of gravity.
174 * \param *out output stream for debugging
175 * \return pointer to center of gravity vector
176 */
[4bb63c]177Vector * molecule::DetermineCenterOfGravity() const
[cee0b57]178{
[9879f6]179 molecule::const_iterator iter = begin(); // start at first in list
[cee0b57]180 Vector *a = new Vector();
181 Vector tmp;
182 double Num = 0;
183
184 a->Zero();
185
[9879f6]186 if (iter != end()) { //list not empty?
187 for (; iter != end(); ++iter) { // continue with second if present
[83f176]188 Num += (*iter)->getType()->getMass();
189 tmp = (*iter)->getType()->getMass() * (*iter)->getPosition();
[273382]190 (*a) += tmp;
[cee0b57]191 }
[bdc91e]192 a->Scale(1./Num); // divide through total mass
[cee0b57]193 }
[e138de]194// Log() << Verbose(1) << "Resulting center of gravity: ";
[cee0b57]195// a->Output(out);
[e138de]196// Log() << Verbose(0) << endl;
[cee0b57]197 return a;
198};
199
200/** Centers the center of gravity of the atoms at (0,0,0).
201 * \param *out output stream for debugging
202 * \param *center return vector for translation vector
203 */
[e138de]204void molecule::CenterPeriodic()
[cee0b57]205{
[1883f9]206 Vector NewCenter;
207 DeterminePeriodicCenter(NewCenter);
208 // go through all atoms
209 BOOST_FOREACH(atom* iter, atoms){
210 *iter -= NewCenter;
211 }
[cee0b57]212};
213
214
215/** Centers the center of gravity of the atoms at (0,0,0).
216 * \param *out output stream for debugging
217 * \param *center return vector for translation vector
218 */
[e138de]219void molecule::CenterAtVector(Vector *newcenter)
[cee0b57]220{
[1883f9]221 // go through all atoms
222 BOOST_FOREACH(atom* iter, atoms){
223 *iter -= *newcenter;
224 }
[cee0b57]225};
226
[1f91f4]227/** Calculate the inertia tensor of a the molecule.
228 *
229 * @return inertia tensor
230 */
231RealSpaceMatrix molecule::getInertiaTensor() const
232{
233 RealSpaceMatrix InertiaTensor;
234 Vector *CenterOfGravity = DetermineCenterOfGravity();
235
236 // reset inertia tensor
237 InertiaTensor.setZero();
238
239 // sum up inertia tensor
240 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
241 Vector x = (*iter)->getPosition();
242 x -= *CenterOfGravity;
243 const double mass = (*iter)->getType()->getMass();
244 InertiaTensor.at(0,0) += mass*(x[1]*x[1] + x[2]*x[2]);
245 InertiaTensor.at(0,1) += mass*(-x[0]*x[1]);
246 InertiaTensor.at(0,2) += mass*(-x[0]*x[2]);
247 InertiaTensor.at(1,0) += mass*(-x[1]*x[0]);
248 InertiaTensor.at(1,1) += mass*(x[0]*x[0] + x[2]*x[2]);
249 InertiaTensor.at(1,2) += mass*(-x[1]*x[2]);
250 InertiaTensor.at(2,0) += mass*(-x[2]*x[0]);
251 InertiaTensor.at(2,1) += mass*(-x[2]*x[1]);
252 InertiaTensor.at(2,2) += mass*(x[0]*x[0] + x[1]*x[1]);
253 }
254 // print InertiaTensor
255 DoLog(0) && (Log() << Verbose(0) << "The inertia tensor of molecule "
256 << getName() << " is:"
257 << InertiaTensor << endl);
258
259 delete CenterOfGravity;
260 return InertiaTensor;
261}
262
263/** Rotates the molecule in such a way that biggest principal axis corresponds
264 * to given \a Axis.
265 *
266 * @param Axis Axis to align with biggest principal axis
267 */
268void molecule::RotateToPrincipalAxisSystem(Vector &Axis)
269{
270 Vector *CenterOfGravity = DetermineCenterOfGravity();
271 RealSpaceMatrix InertiaTensor = getInertiaTensor();
272
273 // diagonalize to determine principal axis system
274 Vector Eigenvalues = InertiaTensor.transformToEigenbasis();
275
276 for(int i=0;i<NDIM;i++)
277 DoLog(0) && (Log() << Verbose(0) << "eigenvalue = " << Eigenvalues[i] << ", eigenvector = " << InertiaTensor.column(i) << endl);
278
279 DoLog(0) && (Log() << Verbose(0) << "Transforming to PAS ... ");
280
281 // obtain first column, eigenvector to biggest eigenvalue
282 Vector BiggestEigenvector(InertiaTensor.column(Eigenvalues.SmallestComponent()));
283 Vector DesiredAxis(Axis);
284
285 // Creation Line that is the rotation axis
286 DesiredAxis.VectorProduct(BiggestEigenvector);
287 Line RotationAxis(Vector(0.,0.,0.), DesiredAxis);
288
289 // determine angle
290 const double alpha = BiggestEigenvector.Angle(Axis);
291
292 DoLog(0) && (Log() << Verbose(0) << "Rotation angle is " << alpha << endl);
293
294 // and rotate
295 for (molecule::iterator iter = begin(); iter != end(); ++iter) {
296 *(*iter) -= *CenterOfGravity;
297 (*iter)->setPosition(RotationAxis.rotateVector((*iter)->getPosition(), alpha));
298 *(*iter) += *CenterOfGravity;
299 }
300 DoLog(0) && (Log() << Verbose(0) << "done." << endl);
301
302 delete CenterOfGravity;
303}
[cee0b57]304
305/** Scales all atoms by \a *factor.
306 * \param *factor pointer to scaling factor
[1bd79e]307 *
308 * TODO: Is this realy what is meant, i.e.
309 * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
310 * or rather
311 * x=(**factor) * x (as suggested by comment)
[cee0b57]312 */
[776b64]313void molecule::Scale(const double ** const factor)
[cee0b57]314{
[9879f6]315 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[6625c3]316 for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
[056e70]317 Vector temp = (*iter)->getPositionAtStep(j);
[6625c3]318 temp.ScaleAll(*factor);
[056e70]319 (*iter)->setPositionAtStep(j,temp);
[6625c3]320 }
[cee0b57]321 }
322};
323
324/** Translate all atoms by given vector.
325 * \param trans[] translation vector.
326 */
327void molecule::Translate(const Vector *trans)
328{
[9879f6]329 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[6625c3]330 for (size_t j=0;j<(*iter)->getTrajectorySize();j++) {
[056e70]331 (*iter)->setPositionAtStep(j, (*iter)->getPositionAtStep(j) + (*trans));
[6625c3]332 }
[cee0b57]333 }
334};
335
336/** Translate the molecule periodically in the box.
337 * \param trans[] translation vector.
[6625c3]338 * TODO treatment of trajectories missing
[cee0b57]339 */
340void molecule::TranslatePeriodically(const Vector *trans)
341{
[f429d7]342 Box &domain = World::getInstance().getDomain();
[cee0b57]343
344 // go through all atoms
[d0f111]345 BOOST_FOREACH(atom* iter, atoms){
[d74077]346 *iter += *trans;
[d0f111]347 }
[0632c5]348 atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
[cee0b57]349
350};
351
352
353/** Mirrors all atoms against a given plane.
354 * \param n[] normal vector of mirror plane.
355 */
356void molecule::Mirror(const Vector *n)
357{
[76c0d6]358 OBSERVE;
[ccf826]359 Plane p(*n,0);
[0632c5]360 atoms.transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
[cee0b57]361};
362
363/** Determines center of molecule (yet not considering atom masses).
364 * \param center reference to return vector
365 */
366void molecule::DeterminePeriodicCenter(Vector &center)
367{
[cca9ef]368 const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
369 const RealSpaceMatrix &inversematrix = World::getInstance().getDomain().getM();
[cee0b57]370 double tmp;
371 bool flag;
372 Vector Testvector, Translationvector;
[1883f9]373 Vector Center;
[7adf0f]374 BondGraph *BG = World::getInstance().getBondGraph();
[cee0b57]375
376 do {
377 Center.Zero();
378 flag = true;
[9879f6]379 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[cee0b57]380#ifdef ADDHYDROGEN
[83f176]381 if ((*iter)->getType()->getAtomicNumber() != 1) {
[cee0b57]382#endif
[d74077]383 Testvector = inversematrix * (*iter)->getPosition();
[cee0b57]384 Translationvector.Zero();
[9d83b6]385 const BondList& ListOfBonds = (*iter)->getListOfBonds();
386 for (BondList::const_iterator Runner = ListOfBonds.begin();
387 Runner != ListOfBonds.end();
388 ++Runner) {
[735b1c]389 if ((*iter)->getNr() < (*Runner)->GetOtherAtom((*iter))->getNr()) // otherwise we shift one to, the other fro and gain nothing
[cee0b57]390 for (int j=0;j<NDIM;j++) {
[d74077]391 tmp = (*iter)->at(j) - (*Runner)->GetOtherAtom(*iter)->at(j);
[607eab]392 const range<double> MinMaxBondDistance(
393 BG->getMinMaxDistance((*iter), (*Runner)->GetOtherAtom(*iter)));
[300220]394 if (fabs(tmp) > MinMaxBondDistance.last) { // check against Min is not useful for components
[cee0b57]395 flag = false;
[a7b761b]396 DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
[cee0b57]397 if (tmp > 0)
[0a4f7f]398 Translationvector[j] -= 1.;
[cee0b57]399 else
[0a4f7f]400 Translationvector[j] += 1.;
[cee0b57]401 }
402 }
403 }
[273382]404 Testvector += Translationvector;
[5108e1]405 Testvector *= matrix;
[273382]406 Center += Testvector;
[0a4f7f]407 Log() << Verbose(1) << "vector is: " << Testvector << endl;
[cee0b57]408#ifdef ADDHYDROGEN
409 // now also change all hydrogens
[9d83b6]410 for (BondList::const_iterator Runner = ListOfBonds.begin();
411 Runner != ListOfBonds.end();
412 ++Runner) {
[83f176]413 if ((*Runner)->GetOtherAtom((*iter))->getType()->getAtomicNumber() == 1) {
[d74077]414 Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->getPosition();
[273382]415 Testvector += Translationvector;
[5108e1]416 Testvector *= matrix;
[273382]417 Center += Testvector;
[0a4f7f]418 Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
[cee0b57]419 }
420 }
421 }
422#endif
423 }
424 } while (!flag);
[1614174]425
[ea7176]426 Center.Scale(1./static_cast<double>(getAtomCount()));
[1883f9]427 CenterAtVector(&Center);
[cee0b57]428};
429
430/** Align all atoms in such a manner that given vector \a *n is along z axis.
431 * \param n[] alignment vector.
432 */
433void molecule::Align(Vector *n)
434{
435 double alpha, tmp;
436 Vector z_axis;
[0a4f7f]437 z_axis[0] = 0.;
438 z_axis[1] = 0.;
439 z_axis[2] = 1.;
[cee0b57]440
441 // rotate on z-x plane
[a67d19]442 DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
[0a4f7f]443 alpha = atan(-n->at(0)/n->at(2));
[a67d19]444 DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
[9879f6]445 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[d74077]446 tmp = (*iter)->at(0);
447 (*iter)->set(0, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
448 (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
[cee0b57]449 for (int j=0;j<MDSteps;j++) {
[6625c3]450 Vector temp;
[056e70]451 temp[0] = cos(alpha) * (*iter)->getPositionAtStep(j)[0] + sin(alpha) * (*iter)->getPositionAtStep(j)[2];
452 temp[2] = -sin(alpha) * (*iter)->getPositionAtStep(j)[0] + cos(alpha) * (*iter)->getPositionAtStep(j)[2];
453 (*iter)->setPositionAtStep(j,temp);
[cee0b57]454 }
455 }
456 // rotate n vector
[0a4f7f]457 tmp = n->at(0);
458 n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
459 n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
[8cbb97]460 DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
[cee0b57]461
462 // rotate on z-y plane
[0a4f7f]463 alpha = atan(-n->at(1)/n->at(2));
[a67d19]464 DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
[9879f6]465 for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
[d74077]466 tmp = (*iter)->at(1);
467 (*iter)->set(1, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
468 (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
[cee0b57]469 for (int j=0;j<MDSteps;j++) {
[6625c3]470 Vector temp;
[056e70]471 temp[1] = cos(alpha) * (*iter)->getPositionAtStep(j)[1] + sin(alpha) * (*iter)->getPositionAtStep(j)[2];
472 temp[2] = -sin(alpha) * (*iter)->getPositionAtStep(j)[1] + cos(alpha) * (*iter)->getPositionAtStep(j)[2];
473 (*iter)->setPositionAtStep(j,temp);
[cee0b57]474 }
475 }
476 // rotate n vector (for consistency check)
[0a4f7f]477 tmp = n->at(1);
478 n->at(1) = cos(alpha) * tmp + sin(alpha) * n->at(2);
479 n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
[cee0b57]480
481
[8cbb97]482 DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
[a67d19]483 DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
[cee0b57]484};
485
486
487/** Calculates sum over least square distance to line hidden in \a *x.
488 * \param *x offset and direction vector
489 * \param *params pointer to lsq_params structure
490 * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
491 */
492double LeastSquareDistance (const gsl_vector * x, void * params)
493{
494 double res = 0, t;
495 Vector a,b,c,d;
496 struct lsq_params *par = (struct lsq_params *)params;
497
498 // initialize vectors
[0a4f7f]499 a[0] = gsl_vector_get(x,0);
500 a[1] = gsl_vector_get(x,1);
501 a[2] = gsl_vector_get(x,2);
502 b[0] = gsl_vector_get(x,3);
503 b[1] = gsl_vector_get(x,4);
504 b[2] = gsl_vector_get(x,5);
[cee0b57]505 // go through all atoms
[9879f6]506 for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
[d74077]507 if ((*iter)->getType() == ((struct lsq_params *)params)->type) { // for specific type
508 c = (*iter)->getPosition() - a;
[273382]509 t = c.ScalarProduct(b); // get direction parameter
510 d = t*b; // and create vector
511 c -= d; // ... yielding distance vector
512 res += d.ScalarProduct(d); // add squared distance
[cee0b57]513 }
514 }
515 return res;
516};
517
518/** By minimizing the least square distance gains alignment vector.
519 * \bug this is not yet working properly it seems
520 */
521void molecule::GetAlignvector(struct lsq_params * par) const
522{
523 int np = 6;
524
525 const gsl_multimin_fminimizer_type *T =
526 gsl_multimin_fminimizer_nmsimplex;
527 gsl_multimin_fminimizer *s = NULL;
528 gsl_vector *ss;
529 gsl_multimin_function minex_func;
530
531 size_t iter = 0, i;
532 int status;
533 double size;
534
535 /* Initial vertex size vector */
536 ss = gsl_vector_alloc (np);
537
538 /* Set all step sizes to 1 */
539 gsl_vector_set_all (ss, 1.0);
540
541 /* Starting point */
542 par->x = gsl_vector_alloc (np);
543 par->mol = this;
544
545 gsl_vector_set (par->x, 0, 0.0); // offset
546 gsl_vector_set (par->x, 1, 0.0);
547 gsl_vector_set (par->x, 2, 0.0);
548 gsl_vector_set (par->x, 3, 0.0); // direction
549 gsl_vector_set (par->x, 4, 0.0);
550 gsl_vector_set (par->x, 5, 1.0);
551
552 /* Initialize method and iterate */
553 minex_func.f = &LeastSquareDistance;
554 minex_func.n = np;
555 minex_func.params = (void *)par;
556
557 s = gsl_multimin_fminimizer_alloc (T, np);
558 gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
559
560 do
561 {
562 iter++;
563 status = gsl_multimin_fminimizer_iterate(s);
564
565 if (status)
566 break;
567
568 size = gsl_multimin_fminimizer_size (s);
569 status = gsl_multimin_test_size (size, 1e-2);
570
571 if (status == GSL_SUCCESS)
572 {
573 printf ("converged to minimum at\n");
574 }
575
576 printf ("%5d ", (int)iter);
577 for (i = 0; i < (size_t)np; i++)
578 {
579 printf ("%10.3e ", gsl_vector_get (s->x, i));
580 }
581 printf ("f() = %7.3f size = %.3f\n", s->fval, size);
582 }
583 while (status == GSL_CONTINUE && iter < 100);
584
585 for (i=0;i<(size_t)np;i++)
586 gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
587 //gsl_vector_free(par->x);
588 gsl_vector_free(ss);
589 gsl_multimin_fminimizer_free (s);
590};
Note: See TracBrowser for help on using the repository browser.