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