[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 | Center.Zero();
|
---|
| 107 | }
|
---|
| 108 | delete(min);
|
---|
[e138de] | 109 | // Log() << Verbose(3) << "End of CenterEdge." << endl;
|
---|
[cee0b57] | 110 | };
|
---|
| 111 |
|
---|
| 112 | /** Centers the center of the atoms at (0,0,0).
|
---|
| 113 | * \param *out output stream for debugging
|
---|
| 114 | * \param *center return vector for translation vector
|
---|
| 115 | */
|
---|
[e138de] | 116 | void molecule::CenterOrigin()
|
---|
[cee0b57] | 117 | {
|
---|
| 118 | int Num = 0;
|
---|
[9879f6] | 119 | molecule::const_iterator iter = begin(); // start at first in list
|
---|
[cee0b57] | 120 |
|
---|
| 121 | Center.Zero();
|
---|
| 122 |
|
---|
[9879f6] | 123 | if (iter != end()) { //list not empty?
|
---|
| 124 | for (; iter != end(); ++iter) { // continue with second if present
|
---|
[cee0b57] | 125 | Num++;
|
---|
[d74077] | 126 | Center += (*iter)->getPosition();
|
---|
[cee0b57] | 127 | }
|
---|
[bdc91e] | 128 | Center.Scale(-1./(double)Num); // divide through total number (and sign for direction)
|
---|
[cee0b57] | 129 | Translate(&Center);
|
---|
| 130 | Center.Zero();
|
---|
| 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 | {
|
---|
| 199 | DeterminePeriodicCenter(Center);
|
---|
| 200 | };
|
---|
| 201 |
|
---|
| 202 |
|
---|
| 203 | /** Centers the center of gravity of the atoms at (0,0,0).
|
---|
| 204 | * \param *out output stream for debugging
|
---|
| 205 | * \param *center return vector for translation vector
|
---|
| 206 | */
|
---|
[e138de] | 207 | void molecule::CenterAtVector(Vector *newcenter)
|
---|
[cee0b57] | 208 | {
|
---|
[273382] | 209 | Center = *newcenter;
|
---|
[cee0b57] | 210 | };
|
---|
| 211 |
|
---|
| 212 |
|
---|
| 213 | /** Scales all atoms by \a *factor.
|
---|
| 214 | * \param *factor pointer to scaling factor
|
---|
[1bd79e] | 215 | *
|
---|
| 216 | * TODO: Is this realy what is meant, i.e.
|
---|
| 217 | * x=(x[0]*factor[0],x[1]*factor[1],x[2]*factor[2]) (current impl)
|
---|
| 218 | * or rather
|
---|
| 219 | * x=(**factor) * x (as suggested by comment)
|
---|
[cee0b57] | 220 | */
|
---|
[776b64] | 221 | void molecule::Scale(const double ** const factor)
|
---|
[cee0b57] | 222 | {
|
---|
[9879f6] | 223 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 224 | for (int j=0;j<MDSteps;j++)
|
---|
[a7b761b] | 225 | (*iter)->Trajectory.R.at(j).ScaleAll(*factor);
|
---|
[d74077] | 226 | (*iter)->ScaleAll(*factor);
|
---|
[cee0b57] | 227 | }
|
---|
| 228 | };
|
---|
| 229 |
|
---|
| 230 | /** Translate all atoms by given vector.
|
---|
| 231 | * \param trans[] translation vector.
|
---|
| 232 | */
|
---|
| 233 | void molecule::Translate(const Vector *trans)
|
---|
| 234 | {
|
---|
[9879f6] | 235 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 236 | for (int j=0;j<MDSteps;j++)
|
---|
[a7b761b] | 237 | (*iter)->Trajectory.R.at(j) += (*trans);
|
---|
[d74077] | 238 | *(*iter) += (*trans);
|
---|
[cee0b57] | 239 | }
|
---|
| 240 | };
|
---|
| 241 |
|
---|
| 242 | /** Translate the molecule periodically in the box.
|
---|
| 243 | * \param trans[] translation vector.
|
---|
| 244 | * TODO treatment of trajetories missing
|
---|
| 245 | */
|
---|
| 246 | void molecule::TranslatePeriodically(const Vector *trans)
|
---|
| 247 | {
|
---|
[f429d7] | 248 | Box &domain = World::getInstance().getDomain();
|
---|
[cee0b57] | 249 |
|
---|
| 250 | // go through all atoms
|
---|
[d0f111] | 251 | BOOST_FOREACH(atom* iter, atoms){
|
---|
[d74077] | 252 | *iter += *trans;
|
---|
[d0f111] | 253 | }
|
---|
[0632c5] | 254 | atoms.transformNodes(boost::bind(&Box::WrapPeriodically,domain,_1));
|
---|
[cee0b57] | 255 |
|
---|
| 256 | };
|
---|
| 257 |
|
---|
| 258 |
|
---|
| 259 | /** Mirrors all atoms against a given plane.
|
---|
| 260 | * \param n[] normal vector of mirror plane.
|
---|
| 261 | */
|
---|
| 262 | void molecule::Mirror(const Vector *n)
|
---|
| 263 | {
|
---|
[76c0d6] | 264 | OBSERVE;
|
---|
[ccf826] | 265 | Plane p(*n,0);
|
---|
[0632c5] | 266 | atoms.transformNodes(boost::bind(&Plane::mirrorVector,p,_1));
|
---|
[cee0b57] | 267 | };
|
---|
| 268 |
|
---|
| 269 | /** Determines center of molecule (yet not considering atom masses).
|
---|
| 270 | * \param center reference to return vector
|
---|
| 271 | */
|
---|
| 272 | void molecule::DeterminePeriodicCenter(Vector ¢er)
|
---|
| 273 | {
|
---|
[84c494] | 274 | const Matrix &matrix = World::getInstance().getDomain().getM();
|
---|
| 275 | const Matrix &inversematrix = World::getInstance().getDomain().getM();
|
---|
[cee0b57] | 276 | double tmp;
|
---|
| 277 | bool flag;
|
---|
| 278 | Vector Testvector, Translationvector;
|
---|
| 279 |
|
---|
| 280 | do {
|
---|
| 281 | Center.Zero();
|
---|
| 282 | flag = true;
|
---|
[9879f6] | 283 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[cee0b57] | 284 | #ifdef ADDHYDROGEN
|
---|
[d74077] | 285 | if ((*iter)->getType()->Z != 1) {
|
---|
[cee0b57] | 286 | #endif
|
---|
[d74077] | 287 | Testvector = inversematrix * (*iter)->getPosition();
|
---|
[cee0b57] | 288 | Translationvector.Zero();
|
---|
[9879f6] | 289 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
---|
| 290 | if ((*iter)->nr < (*Runner)->GetOtherAtom((*iter))->nr) // otherwise we shift one to, the other fro and gain nothing
|
---|
[cee0b57] | 291 | for (int j=0;j<NDIM;j++) {
|
---|
[d74077] | 292 | tmp = (*iter)->at(j) - (*Runner)->GetOtherAtom(*iter)->at(j);
|
---|
[cee0b57] | 293 | if ((fabs(tmp)) > BondDistance) {
|
---|
| 294 | flag = false;
|
---|
[a7b761b] | 295 | DoLog(0) && (Log() << Verbose(0) << "Hit: atom " << (*iter)->getName() << " in bond " << *(*Runner) << " has to be shifted due to " << tmp << "." << endl);
|
---|
[cee0b57] | 296 | if (tmp > 0)
|
---|
[0a4f7f] | 297 | Translationvector[j] -= 1.;
|
---|
[cee0b57] | 298 | else
|
---|
[0a4f7f] | 299 | Translationvector[j] += 1.;
|
---|
[cee0b57] | 300 | }
|
---|
| 301 | }
|
---|
| 302 | }
|
---|
[273382] | 303 | Testvector += Translationvector;
|
---|
[5108e1] | 304 | Testvector *= matrix;
|
---|
[273382] | 305 | Center += Testvector;
|
---|
[0a4f7f] | 306 | Log() << Verbose(1) << "vector is: " << Testvector << endl;
|
---|
[cee0b57] | 307 | #ifdef ADDHYDROGEN
|
---|
| 308 | // now also change all hydrogens
|
---|
[9879f6] | 309 | for (BondList::const_iterator Runner = (*iter)->ListOfBonds.begin(); Runner != (*iter)->ListOfBonds.end(); (++Runner)) {
|
---|
[d74077] | 310 | if ((*Runner)->GetOtherAtom((*iter))->getType()->Z == 1) {
|
---|
| 311 | Testvector = inversematrix * (*Runner)->GetOtherAtom((*iter))->getPosition();
|
---|
[273382] | 312 | Testvector += Translationvector;
|
---|
[5108e1] | 313 | Testvector *= matrix;
|
---|
[273382] | 314 | Center += Testvector;
|
---|
[0a4f7f] | 315 | Log() << Verbose(1) << "Hydrogen vector is: " << Testvector << endl;
|
---|
[cee0b57] | 316 | }
|
---|
| 317 | }
|
---|
| 318 | }
|
---|
| 319 | #endif
|
---|
| 320 | }
|
---|
| 321 | } while (!flag);
|
---|
[1614174] | 322 |
|
---|
[ea7176] | 323 | Center.Scale(1./static_cast<double>(getAtomCount()));
|
---|
[cee0b57] | 324 | };
|
---|
| 325 |
|
---|
| 326 | /** Align all atoms in such a manner that given vector \a *n is along z axis.
|
---|
| 327 | * \param n[] alignment vector.
|
---|
| 328 | */
|
---|
| 329 | void molecule::Align(Vector *n)
|
---|
| 330 | {
|
---|
| 331 | double alpha, tmp;
|
---|
| 332 | Vector z_axis;
|
---|
[0a4f7f] | 333 | z_axis[0] = 0.;
|
---|
| 334 | z_axis[1] = 0.;
|
---|
| 335 | z_axis[2] = 1.;
|
---|
[cee0b57] | 336 |
|
---|
| 337 | // rotate on z-x plane
|
---|
[a67d19] | 338 | DoLog(0) && (Log() << Verbose(0) << "Begin of Aligning all atoms." << endl);
|
---|
[0a4f7f] | 339 | alpha = atan(-n->at(0)/n->at(2));
|
---|
[a67d19] | 340 | DoLog(1) && (Log() << Verbose(1) << "Z-X-angle: " << alpha << " ... ");
|
---|
[9879f6] | 341 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[d74077] | 342 | tmp = (*iter)->at(0);
|
---|
| 343 | (*iter)->set(0, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
---|
| 344 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
---|
[cee0b57] | 345 | for (int j=0;j<MDSteps;j++) {
|
---|
[a7b761b] | 346 | tmp = (*iter)->Trajectory.R.at(j)[0];
|
---|
| 347 | (*iter)->Trajectory.R.at(j)[0] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
| 348 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
[cee0b57] | 349 | }
|
---|
| 350 | }
|
---|
| 351 | // rotate n vector
|
---|
[0a4f7f] | 352 | tmp = n->at(0);
|
---|
| 353 | n->at(0) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
---|
| 354 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
---|
[8cbb97] | 355 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after first rotation: " << n << endl);
|
---|
[cee0b57] | 356 |
|
---|
| 357 | // rotate on z-y plane
|
---|
[0a4f7f] | 358 | alpha = atan(-n->at(1)/n->at(2));
|
---|
[a67d19] | 359 | DoLog(1) && (Log() << Verbose(1) << "Z-Y-angle: " << alpha << " ... ");
|
---|
[9879f6] | 360 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
[d74077] | 361 | tmp = (*iter)->at(1);
|
---|
| 362 | (*iter)->set(1, cos(alpha) * tmp + sin(alpha) * (*iter)->at(2));
|
---|
| 363 | (*iter)->set(2, -sin(alpha) * tmp + cos(alpha) * (*iter)->at(2));
|
---|
[cee0b57] | 364 | for (int j=0;j<MDSteps;j++) {
|
---|
[a7b761b] | 365 | tmp = (*iter)->Trajectory.R.at(j)[1];
|
---|
| 366 | (*iter)->Trajectory.R.at(j)[1] = cos(alpha) * tmp + sin(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
| 367 | (*iter)->Trajectory.R.at(j)[2] = -sin(alpha) * tmp + cos(alpha) * (*iter)->Trajectory.R.at(j)[2];
|
---|
[cee0b57] | 368 | }
|
---|
| 369 | }
|
---|
| 370 | // rotate n vector (for consistency check)
|
---|
[0a4f7f] | 371 | tmp = n->at(1);
|
---|
| 372 | n->at(1) = cos(alpha) * tmp + sin(alpha) * n->at(2);
|
---|
| 373 | n->at(2) = -sin(alpha) * tmp + cos(alpha) * n->at(2);
|
---|
[cee0b57] | 374 |
|
---|
| 375 |
|
---|
[8cbb97] | 376 | DoLog(1) && (Log() << Verbose(1) << "alignment vector after second rotation: " << n << endl);
|
---|
[a67d19] | 377 | DoLog(0) && (Log() << Verbose(0) << "End of Aligning all atoms." << endl);
|
---|
[cee0b57] | 378 | };
|
---|
| 379 |
|
---|
| 380 |
|
---|
| 381 | /** Calculates sum over least square distance to line hidden in \a *x.
|
---|
| 382 | * \param *x offset and direction vector
|
---|
| 383 | * \param *params pointer to lsq_params structure
|
---|
| 384 | * \return \f$ sum_i^N | y_i - (a + t_i b)|^2\f$
|
---|
| 385 | */
|
---|
| 386 | double LeastSquareDistance (const gsl_vector * x, void * params)
|
---|
| 387 | {
|
---|
| 388 | double res = 0, t;
|
---|
| 389 | Vector a,b,c,d;
|
---|
| 390 | struct lsq_params *par = (struct lsq_params *)params;
|
---|
| 391 |
|
---|
| 392 | // initialize vectors
|
---|
[0a4f7f] | 393 | a[0] = gsl_vector_get(x,0);
|
---|
| 394 | a[1] = gsl_vector_get(x,1);
|
---|
| 395 | a[2] = gsl_vector_get(x,2);
|
---|
| 396 | b[0] = gsl_vector_get(x,3);
|
---|
| 397 | b[1] = gsl_vector_get(x,4);
|
---|
| 398 | b[2] = gsl_vector_get(x,5);
|
---|
[cee0b57] | 399 | // go through all atoms
|
---|
[9879f6] | 400 | for (molecule::const_iterator iter = par->mol->begin(); iter != par->mol->end(); ++iter) {
|
---|
[d74077] | 401 | if ((*iter)->getType() == ((struct lsq_params *)params)->type) { // for specific type
|
---|
| 402 | c = (*iter)->getPosition() - a;
|
---|
[273382] | 403 | t = c.ScalarProduct(b); // get direction parameter
|
---|
| 404 | d = t*b; // and create vector
|
---|
| 405 | c -= d; // ... yielding distance vector
|
---|
| 406 | res += d.ScalarProduct(d); // add squared distance
|
---|
[cee0b57] | 407 | }
|
---|
| 408 | }
|
---|
| 409 | return res;
|
---|
| 410 | };
|
---|
| 411 |
|
---|
| 412 | /** By minimizing the least square distance gains alignment vector.
|
---|
| 413 | * \bug this is not yet working properly it seems
|
---|
| 414 | */
|
---|
| 415 | void molecule::GetAlignvector(struct lsq_params * par) const
|
---|
| 416 | {
|
---|
| 417 | int np = 6;
|
---|
| 418 |
|
---|
| 419 | const gsl_multimin_fminimizer_type *T =
|
---|
| 420 | gsl_multimin_fminimizer_nmsimplex;
|
---|
| 421 | gsl_multimin_fminimizer *s = NULL;
|
---|
| 422 | gsl_vector *ss;
|
---|
| 423 | gsl_multimin_function minex_func;
|
---|
| 424 |
|
---|
| 425 | size_t iter = 0, i;
|
---|
| 426 | int status;
|
---|
| 427 | double size;
|
---|
| 428 |
|
---|
| 429 | /* Initial vertex size vector */
|
---|
| 430 | ss = gsl_vector_alloc (np);
|
---|
| 431 |
|
---|
| 432 | /* Set all step sizes to 1 */
|
---|
| 433 | gsl_vector_set_all (ss, 1.0);
|
---|
| 434 |
|
---|
| 435 | /* Starting point */
|
---|
| 436 | par->x = gsl_vector_alloc (np);
|
---|
| 437 | par->mol = this;
|
---|
| 438 |
|
---|
| 439 | gsl_vector_set (par->x, 0, 0.0); // offset
|
---|
| 440 | gsl_vector_set (par->x, 1, 0.0);
|
---|
| 441 | gsl_vector_set (par->x, 2, 0.0);
|
---|
| 442 | gsl_vector_set (par->x, 3, 0.0); // direction
|
---|
| 443 | gsl_vector_set (par->x, 4, 0.0);
|
---|
| 444 | gsl_vector_set (par->x, 5, 1.0);
|
---|
| 445 |
|
---|
| 446 | /* Initialize method and iterate */
|
---|
| 447 | minex_func.f = &LeastSquareDistance;
|
---|
| 448 | minex_func.n = np;
|
---|
| 449 | minex_func.params = (void *)par;
|
---|
| 450 |
|
---|
| 451 | s = gsl_multimin_fminimizer_alloc (T, np);
|
---|
| 452 | gsl_multimin_fminimizer_set (s, &minex_func, par->x, ss);
|
---|
| 453 |
|
---|
| 454 | do
|
---|
| 455 | {
|
---|
| 456 | iter++;
|
---|
| 457 | status = gsl_multimin_fminimizer_iterate(s);
|
---|
| 458 |
|
---|
| 459 | if (status)
|
---|
| 460 | break;
|
---|
| 461 |
|
---|
| 462 | size = gsl_multimin_fminimizer_size (s);
|
---|
| 463 | status = gsl_multimin_test_size (size, 1e-2);
|
---|
| 464 |
|
---|
| 465 | if (status == GSL_SUCCESS)
|
---|
| 466 | {
|
---|
| 467 | printf ("converged to minimum at\n");
|
---|
| 468 | }
|
---|
| 469 |
|
---|
| 470 | printf ("%5d ", (int)iter);
|
---|
| 471 | for (i = 0; i < (size_t)np; i++)
|
---|
| 472 | {
|
---|
| 473 | printf ("%10.3e ", gsl_vector_get (s->x, i));
|
---|
| 474 | }
|
---|
| 475 | printf ("f() = %7.3f size = %.3f\n", s->fval, size);
|
---|
| 476 | }
|
---|
| 477 | while (status == GSL_CONTINUE && iter < 100);
|
---|
| 478 |
|
---|
| 479 | for (i=0;i<(size_t)np;i++)
|
---|
| 480 | gsl_vector_set(par->x, i, gsl_vector_get(s->x, i));
|
---|
| 481 | //gsl_vector_free(par->x);
|
---|
| 482 | gsl_vector_free(ss);
|
---|
| 483 | gsl_multimin_fminimizer_free (s);
|
---|
| 484 | };
|
---|