| [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 |
|
|---|
| [6e5084] | 12 | #include "Helpers/helpers.hpp"
|
|---|
| 13 | #include "Helpers/Log.hpp"
|
|---|
| [112b09] | 14 | #include "Helpers/MemDebug.hpp"
|
|---|
| [6e5084] | 15 | #include "Helpers/Verbose.hpp"
|
|---|
| 16 | #include "LinearAlgebra/Line.hpp"
|
|---|
| 17 | #include "LinearAlgebra/Matrix.hpp"
|
|---|
| 18 | #include "LinearAlgebra/Plane.hpp"
|
|---|
| [112b09] | 19 |
|
|---|
| [f66195] | 20 | #include "atom.hpp"
|
|---|
| 21 | #include "bond.hpp"
|
|---|
| [cee0b57] | 22 | #include "config.hpp"
|
|---|
| [f66195] | 23 | #include "element.hpp"
|
|---|
| 24 | #include "leastsquaremin.hpp"
|
|---|
| [cee0b57] | 25 | #include "molecule.hpp"
|
|---|
| [b34306] | 26 | #include "World.hpp"
|
|---|
| [6e5084] | 27 |
|
|---|
| [84c494] | 28 | #include "Box.hpp"
|
|---|
| [6e5084] | 29 |
|
|---|
| [76c0d6] | 30 | #include <boost/foreach.hpp>
|
|---|
| 31 |
|
|---|
| [aafd77] | 32 | #include <gsl/gsl_eigen.h>
|
|---|
| 33 | #include <gsl/gsl_multimin.h>
|
|---|
| 34 |
|
|---|
| [cee0b57] | 35 |
|
|---|
| 36 | /************************************* Functions for class molecule *********************************/
|
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 | /** Centers the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
|---|
| 40 | * \param *out output stream for debugging
|
|---|
| 41 | */
|
|---|
| [e138de] | 42 | bool molecule::CenterInBox()
|
|---|
| [cee0b57] | 43 | {
|
|---|
| 44 | bool status = true;
|
|---|
| [e138de] | 45 | const Vector *Center = DetermineCenterOfAll();
|
|---|
| [eddea2] | 46 | const Vector *CenterBox = DetermineCenterOfBox();
|
|---|
| [f429d7] | 47 | Box &domain = World::getInstance().getDomain();
|
|---|
| [cee0b57] | 48 |
|
|---|
| 49 | // go through all atoms
|
|---|
| [d0f111] | 50 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| [d74077] | 51 | *iter -= *Center;
|
|---|
| 52 | *iter -= *CenterBox;
|
|---|
| [d0f111] | 53 | }
|
|---|
| [0632c5] | 54 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
|---|
| [cee0b57] | 55 |
|
|---|
| 56 | delete(Center);
|
|---|
| [52d777] | 57 | delete(CenterBox);
|
|---|
| [cee0b57] | 58 | return status;
|
|---|
| 59 | };
|
|---|
| 60 |
|
|---|
| 61 |
|
|---|
| 62 | /** Bounds the molecule in the box whose lengths are defined by vector \a *BoxLengths.
|
|---|
| 63 | * \param *out output stream for debugging
|
|---|
| 64 | */
|
|---|
| [e138de] | 65 | bool molecule::BoundInBox()
|
|---|
| [cee0b57] | 66 | {
|
|---|
| 67 | bool status = true;
|
|---|
| [f429d7] | 68 | Box &domain = World::getInstance().getDomain();
|
|---|
| [cee0b57] | 69 |
|
|---|
| 70 | // go through all atoms
|
|---|
| [0632c5] | 71 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
|---|
| [cee0b57] | 72 |
|
|---|
| 73 | return status;
|
|---|
| 74 | };
|
|---|
| 75 |
|
|---|
| 76 | /** Centers the edge of the atoms at (0,0,0).
|
|---|
| 77 | * \param *out output stream for debugging
|
|---|
| 78 | * \param *max coordinates of other edge, specifying box dimensions.
|
|---|
| 79 | */
|
|---|
| [e138de] | 80 | void molecule::CenterEdge(Vector *max)
|
|---|
| [cee0b57] | 81 | {
|
|---|
| 82 | Vector *min = new Vector;
|
|---|
| 83 |
|
|---|
| [e138de] | 84 | // Log() << Verbose(3) << "Begin of CenterEdge." << endl;
|
|---|
| [9879f6] | 85 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| 86 | if (iter != end()) { //list not empty?
|
|---|
| [cee0b57] | 87 | for (int i=NDIM;i--;) {
|
|---|
| [d74077] | 88 | max->at(i) = (*iter)->at(i);
|
|---|
| 89 | min->at(i) = (*iter)->at(i);
|
|---|
| [cee0b57] | 90 | }
|
|---|
| [9879f6] | 91 | for (; iter != end(); ++iter) {// continue with second if present
|
|---|
| 92 | //(*iter)->Output(1,1,out);
|
|---|
| [cee0b57] | 93 | for (int i=NDIM;i--;) {
|
|---|
| [d74077] | 94 | max->at(i) = (max->at(i) < (*iter)->at(i)) ? (*iter)->at(i) : max->at(i);
|
|---|
| 95 | min->at(i) = (min->at(i) > (*iter)->at(i)) ? (*iter)->at(i) : min->at(i);
|
|---|
| [cee0b57] | 96 | }
|
|---|
| 97 | }
|
|---|
| [e138de] | 98 | // Log() << Verbose(4) << "Maximum is ";
|
|---|
| [cee0b57] | 99 | // max->Output(out);
|
|---|
| [e138de] | 100 | // Log() << Verbose(0) << ", Minimum is ";
|
|---|
| [cee0b57] | 101 | // min->Output(out);
|
|---|
| [e138de] | 102 | // Log() << Verbose(0) << endl;
|
|---|
| [cee0b57] | 103 | min->Scale(-1.);
|
|---|
| [273382] | 104 | (*max) += (*min);
|
|---|
| [cee0b57] | 105 | Translate(min);
|
|---|
| 106 | }
|
|---|
| 107 | delete(min);
|
|---|
| [e138de] | 108 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
|---|
| [cee0b57] | 109 | };
|
|---|
| 110 |
|
|---|
| 111 | /** Centers the center of the atoms at (0,0,0).
|
|---|
| 112 | * \param *out output stream for debugging
|
|---|
| 113 | * \param *center return vector for translation vector
|
|---|
| 114 | */
|
|---|
| [e138de] | 115 | void molecule::CenterOrigin()
|
|---|
| [cee0b57] | 116 | {
|
|---|
| 117 | int Num = 0;
|
|---|
| [9879f6] | 118 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| [1883f9] | 119 | Vector Center;
|
|---|
| [cee0b57] | 120 |
|
|---|
| 121 | Center.Zero();
|
|---|
| [9879f6] | 122 | if (iter != end()) { //list not empty?
|
|---|
| 123 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| [cee0b57] | 124 | Num++;
|
|---|
| [d74077] | 125 | Center += (*iter)->getPosition();
|
|---|
| [cee0b57] | 126 | }
|
|---|
| [bdc91e] | 127 | Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
|
|---|
| [cee0b57] | 128 | Translate(&Center);
|
|---|
| 129 | }
|
|---|
| 130 | };
|
|---|
| 131 |
|
|---|
| 132 | /** Returns vector pointing to center of all atoms.
|
|---|
| 133 | * \return pointer to center of all vector
|
|---|
| 134 | */
|
|---|
| [e138de] | 135 | Vector * molecule::DetermineCenterOfAll() const
|
|---|
| [cee0b57] | 136 | {
|
|---|
| [9879f6] | 137 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| [cee0b57] | 138 | Vector *a = new Vector();
|
|---|
| 139 | double Num = 0;
|
|---|
| 140 |
|
|---|
| 141 | a->Zero();
|
|---|
| 142 |
|
|---|
| [9879f6] | 143 | if (iter != end()) { //list not empty?
|
|---|
| 144 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| [15b670] | 145 | Num++;
|
|---|
| [d74077] | 146 | (*a) += (*iter)->getPosition();
|
|---|
| [cee0b57] | 147 | }
|
|---|
| [bdc91e] | 148 | a->Scale(1./(double)Num); // divide through total mass (and sign for direction)
|
|---|
| [cee0b57] | 149 | }
|
|---|
| 150 | return a;
|
|---|
| 151 | };
|
|---|
| 152 |
|
|---|
| [eddea2] | 153 | /** Returns vector pointing to center of the domain.
|
|---|
| 154 | * \return pointer to center of the domain
|
|---|
| 155 | */
|
|---|
| 156 | Vector * molecule::DetermineCenterOfBox() const
|
|---|
| 157 | {
|
|---|
| 158 | Vector *a = new Vector(0.5,0.5,0.5);
|
|---|
| [84c494] | 159 | const Matrix &M = World::getInstance().getDomain().getM();
|
|---|
| [5108e1] | 160 | (*a) *= M;
|
|---|
| [eddea2] | 161 | return a;
|
|---|
| 162 | };
|
|---|
| 163 |
|
|---|
| [cee0b57] | 164 | /** Returns vector pointing to center of gravity.
|
|---|
| 165 | * \param *out output stream for debugging
|
|---|
| 166 | * \return pointer to center of gravity vector
|
|---|
| 167 | */
|
|---|
| [4bb63c] | 168 | Vector * molecule::DetermineCenterOfGravity() const
|
|---|
| [cee0b57] | 169 | {
|
|---|
| [9879f6] | 170 | molecule::const_iterator iter = begin(); // start at first in list
|
|---|
| [cee0b57] | 171 | Vector *a = new Vector();
|
|---|
| 172 | Vector tmp;
|
|---|
| 173 | double Num = 0;
|
|---|
| 174 |
|
|---|
| 175 | a->Zero();
|
|---|
| 176 |
|
|---|
| [9879f6] | 177 | if (iter != end()) { //list not empty?
|
|---|
| 178 | for (; iter != end(); ++iter) { // continue with second if present
|
|---|
| [d74077] | 179 | Num += (*iter)->getType()->mass;
|
|---|
| 180 | tmp = (*iter)->getType()->mass * (*iter)->getPosition();
|
|---|
| [273382] | 181 | (*a) += tmp;
|
|---|
| [cee0b57] | 182 | }
|
|---|
| [bdc91e] | 183 | a->Scale(1./Num); // divide through total mass
|
|---|
| [cee0b57] | 184 | }
|
|---|
| [e138de] | 185 | // Log() << Verbose(1) << "Resulting center of gravity: ";
|
|---|
| [cee0b57] | 186 | // a->Output(out);
|
|---|
| [e138de] | 187 | // Log() << Verbose(0) << endl;
|
|---|
| [cee0b57] | 188 | return a;
|
|---|
| 189 | };
|
|---|
| 190 |
|
|---|
| 191 | /** Centers the center of gravity of the atoms at (0,0,0).
|
|---|
| 192 | * \param *out output stream for debugging
|
|---|
| 193 | * \param *center return vector for translation vector
|
|---|
| 194 | */
|
|---|
| [e138de] | 195 | void molecule::CenterPeriodic()
|
|---|
| [cee0b57] | 196 | {
|
|---|
| [1883f9] | 197 | Vector NewCenter;
|
|---|
| 198 | DeterminePeriodicCenter(NewCenter);
|
|---|
| 199 | // go through all atoms
|
|---|
| 200 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 201 | *iter -= NewCenter;
|
|---|
| 202 | }
|
|---|
| [cee0b57] | 203 | };
|
|---|
| 204 |
|
|---|
| 205 |
|
|---|
| 206 | /** Centers the center of gravity of the atoms at (0,0,0).
|
|---|
| 207 | * \param *out output stream for debugging
|
|---|
| 208 | * \param *center return vector for translation vector
|
|---|
| 209 | */
|
|---|
| [e138de] | 210 | void molecule::CenterAtVector(Vector *newcenter)
|
|---|
| [cee0b57] | 211 | {
|
|---|
| [1883f9] | 212 | // go through all atoms
|
|---|
| 213 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| 214 | *iter -= *newcenter;
|
|---|
| 215 | }
|
|---|
| [cee0b57] | 216 | };
|
|---|
| 217 |
|
|---|
| 218 |
|
|---|
| 219 | /** Scales all atoms by \a *factor.
|
|---|
| 220 | * \param *factor pointer to scaling factor
|
|---|
| [1bd79e] | 221 | *
|
|---|
| 222 | * TODO: Is this realy what is meant, i.e.
|
|---|
| 223 | * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
|
|---|
| 224 | * or rather
|
|---|
| 225 | * x=(**factor) * x (as suggested by comment)
|
|---|
| [cee0b57] | 226 | */
|
|---|
| [776b64] | 227 | void molecule::Scale(const double ** const factor)
|
|---|
| [cee0b57] | 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).ScaleAll(*factor);
|
|---|
| [d74077] | 232 | (*iter)->ScaleAll(*factor);
|
|---|
| [cee0b57] | 233 | }
|
|---|
| 234 | };
|
|---|
| 235 |
|
|---|
| 236 | /** Translate all atoms by given vector.
|
|---|
| 237 | * \param trans[] translation vector.
|
|---|
| 238 | */
|
|---|
| 239 | void molecule::Translate(const Vector *trans)
|
|---|
| 240 | {
|
|---|
| [9879f6] | 241 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [cee0b57] | 242 | for (int j=0;j<MDSteps;j++)
|
|---|
| [a7b761b] | 243 | (*iter)->Trajectory.R.at(j) += (*trans);
|
|---|
| [d74077] | 244 | *(*iter) += (*trans);
|
|---|
| [cee0b57] | 245 | }
|
|---|
| 246 | };
|
|---|
| 247 |
|
|---|
| 248 | /** Translate the molecule periodically in the box.
|
|---|
| 249 | * \param trans[] translation vector.
|
|---|
| 250 | * TODO treatment of trajetories missing
|
|---|
| 251 | */
|
|---|
| 252 | void molecule::TranslatePeriodically(const Vector *trans)
|
|---|
| 253 | {
|
|---|
| [f429d7] | 254 | Box &domain = World::getInstance().getDomain();
|
|---|
| [cee0b57] | 255 |
|
|---|
| 256 | // go through all atoms
|
|---|
| [d0f111] | 257 | BOOST_FOREACH(atom* iter, atoms){
|
|---|
| [d74077] | 258 | *iter += *trans;
|
|---|
| [d0f111] | 259 | }
|
|---|
| [0632c5] | 260 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
|---|
| [cee0b57] | 261 |
|
|---|
| 262 | };
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 | /** Mirrors all atoms against a given plane.
|
|---|
| 266 | * \param n[] normal vector of mirror plane.
|
|---|
| 267 | */
|
|---|
| 268 | void molecule::Mirror(const Vector *n)
|
|---|
| 269 | {
|
|---|
| [76c0d6] | 270 | OBSERVE;
|
|---|
| [ccf826] | 271 | Plane p(*n,0);
|
|---|
| [0632c5] | 272 | atoms.transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
|
|---|
| [cee0b57] | 273 | };
|
|---|
| 274 |
|
|---|
| 275 | /** Determines center of molecule (yet not considering atom masses).
|
|---|
| 276 | * \param center reference to return vector
|
|---|
| 277 | */
|
|---|
| 278 | void molecule::DeterminePeriodicCenter(Vector ¢er)
|
|---|
| 279 | {
|
|---|
| [84c494] | 280 | const Matrix &matrix = World::getInstance().getDomain().getM();
|
|---|
| 281 | const Matrix &inversematrix = World::getInstance().getDomain().getM();
|
|---|
| [cee0b57] | 282 | double tmp;
|
|---|
| 283 | bool flag;
|
|---|
| 284 | Vector Testvector, Translationvector;
|
|---|
| [1883f9] | 285 | Vector Center;
|
|---|
| [cee0b57] | 286 |
|
|---|
| 287 | do {
|
|---|
| 288 | Center.Zero();
|
|---|
| 289 | flag = true;
|
|---|
| [9879f6] | 290 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [cee0b57] | 291 | #ifdef ADDHYDROGEN
|
|---|
| [d74077] | 292 | if ((*iter)->getType()->Z != 1) {
|
|---|
| [cee0b57] | 293 | #endif
|
|---|
| [d74077] | 294 | Testvector = inversematrix * (*iter)->getPosition();
|
|---|
| [cee0b57] | 295 | Translationvector.Zero();
|
|---|
| [9879f6] | 296 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
|---|
| 297 | if ((*iter)->nr < (*Runner)->GetOtherAtom((*iter))->nr) // otherwise we shift one to, the other fro and gain nothing
|
|---|
| [cee0b57] | 298 | for (int j=0;j<NDIM;j++) {
|
|---|
| [d74077] | 299 | tmp = (*iter)->at(j) - (*Runner)->GetOtherAtom(*iter)->at(j);
|
|---|
| [cee0b57] | 300 | if ((fabs(tmp)) > BondDistance) {
|
|---|
| 301 | flag = false;
|
|---|
| [a7b761b] | 302 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
|---|
| [cee0b57] | 303 | if (tmp > 0)
|
|---|
| [0a4f7f] | 304 | Translationvector[j] -= 1.;
|
|---|
| [cee0b57] | 305 | else
|
|---|
| [0a4f7f] | 306 | Translationvector[j] += 1.;
|
|---|
| [cee0b57] | 307 | }
|
|---|
| 308 | }
|
|---|
| 309 | }
|
|---|
| [273382] | 310 | Testvector += Translationvector;
|
|---|
| [5108e1] | 311 | Testvector *= matrix;
|
|---|
| [273382] | 312 | Center += Testvector;
|
|---|
| [0a4f7f] | 313 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
|---|
| [cee0b57] | 314 | #ifdef ADDHYDROGEN
|
|---|
| 315 | // now also change all hydrogens
|
|---|
| [9879f6] | 316 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
|---|
| [d74077] | 317 | if ((*Runner)->GetOtherAtom((*iter))->getType()->Z == 1) {
|
|---|
| 318 | Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->getPosition();
|
|---|
| [273382] | 319 | Testvector += Translationvector;
|
|---|
| [5108e1] | 320 | Testvector *= matrix;
|
|---|
| [273382] | 321 | Center += Testvector;
|
|---|
| [0a4f7f] | 322 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
|---|
| [cee0b57] | 323 | }
|
|---|
| 324 | }
|
|---|
| 325 | }
|
|---|
| 326 | #endif
|
|---|
| 327 | }
|
|---|
| 328 | } while (!flag);
|
|---|
| [1614174] | 329 |
|
|---|
| [ea7176] | 330 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
|---|
| [1883f9] | 331 | CenterAtVector(&Center);
|
|---|
| [cee0b57] | 332 | };
|
|---|
| 333 |
|
|---|
| 334 | /** Align all atoms in such a manner that given vector \a *n is along z axis.
|
|---|
| 335 | * \param n[] alignment vector.
|
|---|
| 336 | */
|
|---|
| 337 | void molecule::Align(Vector *n)
|
|---|
| 338 | {
|
|---|
| 339 | double alpha, tmp;
|
|---|
| 340 | Vector z_axis;
|
|---|
| [0a4f7f] | 341 | z_axis[0] = 0.;
|
|---|
| 342 | z_axis[1] = 0.;
|
|---|
| 343 | z_axis[2] = 1.;
|
|---|
| [cee0b57] | 344 |
|
|---|
| 345 | // rotate on z-x plane
|
|---|
| [a67d19] | 346 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
|---|
| [0a4f7f] | 347 | alpha = atan(-n->at(0)/n->at(2));
|
|---|
| [a67d19] | 348 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
|---|
| [9879f6] | 349 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [d74077] | 350 | tmp = (*iter)->at(0);
|
|---|
| 351 | (*iter)->set(0, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
|---|
| 352 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
|---|
| [cee0b57] | 353 | for (int j=0;j<MDSteps;j++) {
|
|---|
| [a7b761b] | 354 | tmp = (*iter)->Trajectory.R.at(j)[0];
|
|---|
| 355 | (*iter)->Trajectory.R.at(j)[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| 356 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| [cee0b57] | 357 | }
|
|---|
| 358 | }
|
|---|
| 359 | // rotate n vector
|
|---|
| [0a4f7f] | 360 | tmp = n->at(0);
|
|---|
| 361 | n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
|---|
| 362 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
|---|
| [8cbb97] | 363 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
|---|
| [cee0b57] | 364 |
|
|---|
| 365 | // rotate on z-y plane
|
|---|
| [0a4f7f] | 366 | alpha = atan(-n->at(1)/n->at(2));
|
|---|
| [a67d19] | 367 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
|---|
| [9879f6] | 368 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
|---|
| [d74077] | 369 | tmp = (*iter)->at(1);
|
|---|
| 370 | (*iter)->set(1, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
|---|
| 371 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
|---|
| [cee0b57] | 372 | for (int j=0;j<MDSteps;j++) {
|
|---|
| [a7b761b] | 373 | tmp = (*iter)->Trajectory.R.at(j)[1];
|
|---|
| 374 | (*iter)->Trajectory.R.at(j)[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| 375 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
|---|
| [cee0b57] | 376 | }
|
|---|
| 377 | }
|
|---|
| 378 | // rotate n vector (for consistency check)
|
|---|
| [0a4f7f] | 379 | tmp = n->at(1);
|
|---|
| 380 | n->at(1) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
|---|
| 381 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
|---|
| [cee0b57] | 382 |
|
|---|
| 383 |
|
|---|
| [8cbb97] | 384 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
|
|---|
| [a67d19] | 385 | DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
|
|---|
| [cee0b57] | 386 | };
|
|---|
| 387 |
|
|---|
| 388 |
|
|---|
| 389 | /** Calculates sum over least square distance to line hidden in \a *x.
|
|---|
| 390 | * \param *x offset and direction vector
|
|---|
| 391 | * \param *params pointer to lsq_params structure
|
|---|
| 392 | * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
|
|---|
| 393 | */
|
|---|
| 394 | double LeastSquareDistance (const gsl_vector * x, void * params)
|
|---|
| 395 | {
|
|---|
| 396 | double res = 0, t;
|
|---|
| 397 | Vector a,b,c,d;
|
|---|
| 398 | struct lsq_params *par = (struct lsq_params *)params;
|
|---|
| 399 |
|
|---|
| 400 | // initialize vectors
|
|---|
| [0a4f7f] | 401 | a[0] = gsl_vector_get(x,0);
|
|---|
| 402 | a[1] = gsl_vector_get(x,1);
|
|---|
| 403 | a[2] = gsl_vector_get(x,2);
|
|---|
| 404 | b[0] = gsl_vector_get(x,3);
|
|---|
| 405 | b[1] = gsl_vector_get(x,4);
|
|---|
| 406 | b[2] = gsl_vector_get(x,5);
|
|---|
| [cee0b57] | 407 | // go through all atoms
|
|---|
| [9879f6] | 408 | for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
|
|---|
| [d74077] | 409 | if ((*iter)->getType() == ((struct lsq_params *)params)->type) { // for specific type
|
|---|
| 410 | c = (*iter)->getPosition() - a;
|
|---|
| [273382] | 411 | t = c.ScalarProduct(b); // get direction parameter
|
|---|
| 412 | d = t*b; // and create vector
|
|---|
| 413 | c -= d; // ... yielding distance vector
|
|---|
| 414 | res += d.ScalarProduct(d); // add squared distance
|
|---|
| [cee0b57] | 415 | }
|
|---|
| 416 | }
|
|---|
| 417 | return res;
|
|---|
| 418 | };
|
|---|
| 419 |
|
|---|
| 420 | /** By minimizing the least square distance gains alignment vector.
|
|---|
| 421 | * \bug this is not yet working properly it seems
|
|---|
| 422 | */
|
|---|
| 423 | void molecule::GetAlignvector(struct lsq_params * par) const
|
|---|
| 424 | {
|
|---|
| 425 | int np = 6;
|
|---|
| 426 |
|
|---|
| 427 | const gsl_multimin_fminimizer_type *T =
|
|---|
| 428 | gsl_multimin_fminimizer_nmsimplex;
|
|---|
| 429 | gsl_multimin_fminimizer *s = NULL;
|
|---|
| 430 | gsl_vector *ss;
|
|---|
| 431 | gsl_multimin_function minex_func;
|
|---|
| 432 |
|
|---|
| 433 | size_t iter = 0, i;
|
|---|
| 434 | int status;
|
|---|
| 435 | double size;
|
|---|
| 436 |
|
|---|
| 437 | /* Initial vertex size vector */
|
|---|
| 438 | ss = gsl_vector_alloc (np);
|
|---|
| 439 |
|
|---|
| 440 | /* Set all step sizes to 1 */
|
|---|
| 441 | gsl_vector_set_all (ss, 1.0);
|
|---|
| 442 |
|
|---|
| 443 | /* Starting point */
|
|---|
| 444 | par->x = gsl_vector_alloc (np);
|
|---|
| 445 | par->mol = this;
|
|---|
| 446 |
|
|---|
| 447 | gsl_vector_set (par->x, 0, 0.0); // offset
|
|---|
| 448 | gsl_vector_set (par->x, 1, 0.0);
|
|---|
| 449 | gsl_vector_set (par->x, 2, 0.0);
|
|---|
| 450 | gsl_vector_set (par->x, 3, 0.0); // direction
|
|---|
| 451 | gsl_vector_set (par->x, 4, 0.0);
|
|---|
| 452 | gsl_vector_set (par->x, 5, 1.0);
|
|---|
| 453 |
|
|---|
| 454 | /* Initialize method and iterate */
|
|---|
| 455 | minex_func.f = &LeastSquareDistance;
|
|---|
| 456 | minex_func.n = np;
|
|---|
| 457 | minex_func.params = (void *)par;
|
|---|
| 458 |
|
|---|
| 459 | s = gsl_multimin_fminimizer_alloc (T, np);
|
|---|
| 460 | gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
|
|---|
| 461 |
|
|---|
| 462 | do
|
|---|
| 463 | {
|
|---|
| 464 | iter++;
|
|---|
| 465 | status = gsl_multimin_fminimizer_iterate(s);
|
|---|
| 466 |
|
|---|
| 467 | if (status)
|
|---|
| 468 | break;
|
|---|
| 469 |
|
|---|
| 470 | size = gsl_multimin_fminimizer_size (s);
|
|---|
| 471 | status = gsl_multimin_test_size (size, 1e-2);
|
|---|
| 472 |
|
|---|
| 473 | if (status == GSL_SUCCESS)
|
|---|
| 474 | {
|
|---|
| 475 | printf ("converged to minimum at\n");
|
|---|
| 476 | }
|
|---|
| 477 |
|
|---|
| 478 | printf ("%5d ", (int)iter);
|
|---|
| 479 | for (i = 0; i < (size_t)np; i++)
|
|---|
| 480 | {
|
|---|
| 481 | printf ("%10.3e ", gsl_vector_get (s->x, i));
|
|---|
| 482 | }
|
|---|
| 483 | printf ("f() = %7.3f size = %.3f\n", s->fval, size);
|
|---|
| 484 | }
|
|---|
| 485 | while (status == GSL_CONTINUE && iter < 100);
|
|---|
| 486 |
|
|---|
| 487 | for (i=0;i<(size_t)np;i++)
|
|---|
| 488 | gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
|
|---|
| 489 | //gsl_vector_free(par->x);
|
|---|
| 490 | gsl_vector_free(ss);
|
|---|
| 491 | gsl_multimin_fminimizer_free (s);
|
|---|
| 492 | };
|
|---|