[14de469] | 1 | /** \file molecules.cpp
|
---|
[69eb71] | 2 | *
|
---|
[14de469] | 3 | * Functions for the class molecule.
|
---|
[69eb71] | 4 | *
|
---|
[14de469] | 5 | */
|
---|
| 6 |
|
---|
[49e1ae] | 7 | #include <cstring>
|
---|
[ac9b56] | 8 | #include <boost/bind.hpp>
|
---|
[49e1ae] | 9 |
|
---|
[46d958] | 10 | #include "World.hpp"
|
---|
[f66195] | 11 | #include "atom.hpp"
|
---|
| 12 | #include "bond.hpp"
|
---|
[a80fbdf] | 13 | #include "config.hpp"
|
---|
[f66195] | 14 | #include "element.hpp"
|
---|
| 15 | #include "graph.hpp"
|
---|
[e9f8f9] | 16 | #include "helpers.hpp"
|
---|
[f66195] | 17 | #include "leastsquaremin.hpp"
|
---|
| 18 | #include "linkedcell.hpp"
|
---|
| 19 | #include "lists.hpp"
|
---|
[e138de] | 20 | #include "log.hpp"
|
---|
[cee0b57] | 21 | #include "molecule.hpp"
|
---|
[f66195] | 22 | #include "memoryallocator.hpp"
|
---|
| 23 | #include "periodentafel.hpp"
|
---|
| 24 | #include "stackclass.hpp"
|
---|
| 25 | #include "tesselation.hpp"
|
---|
| 26 | #include "vector.hpp"
|
---|
[14de469] | 27 |
|
---|
| 28 | /************************************* Functions for class molecule *********************************/
|
---|
| 29 |
|
---|
| 30 | /** Constructor of class molecule.
|
---|
| 31 | * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
|
---|
| 32 | */
|
---|
[d3347e] | 33 | molecule::molecule(const periodentafel * const teil) : elemente(teil),
|
---|
[bd58fb] | 34 | first(new bond(0, 0, 1, -1)), last(new bond(0, 0, 1, -1)), MDSteps(0), AtomCount(0),
|
---|
[fa649a] | 35 | BondCount(0), ElementCount(0), NoNonHydrogen(0), NoNonBonds(0), NoCyclicBonds(0), BondDistance(0.),
|
---|
[24a5e0] | 36 | ActiveFlag(false), IndexNr(-1),
|
---|
| 37 | formula(this,boost::bind(&molecule::calcFormula,this)),
|
---|
| 38 | last_atom(0),
|
---|
[9879f6] | 39 | InternalPointer(begin())
|
---|
[69eb71] | 40 | {
|
---|
[042f82] | 41 | // init bond chain list
|
---|
| 42 | link(first,last);
|
---|
[fa649a] | 43 |
|
---|
[042f82] | 44 | // other stuff
|
---|
| 45 | for(int i=MAX_ELEMENTS;i--;)
|
---|
| 46 | ElementsInMolecule[i] = 0;
|
---|
| 47 | cell_size[0] = cell_size[2] = cell_size[5]= 20.;
|
---|
| 48 | cell_size[1] = cell_size[3] = cell_size[4]= 0.;
|
---|
| 49 | strcpy(name,"none");
|
---|
[14de469] | 50 | };
|
---|
| 51 |
|
---|
[cbc5fb] | 52 | molecule *NewMolecule(){
|
---|
| 53 | return new molecule(World::get()->getPeriode());
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[14de469] | 56 | /** Destructor of class molecule.
|
---|
| 57 | * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
|
---|
| 58 | */
|
---|
[69eb71] | 59 | molecule::~molecule()
|
---|
[14de469] | 60 | {
|
---|
[042f82] | 61 | CleanupMolecule();
|
---|
| 62 | delete(first);
|
---|
| 63 | delete(last);
|
---|
[14de469] | 64 | };
|
---|
| 65 |
|
---|
[357fba] | 66 |
|
---|
[cbc5fb] | 67 | void DeleteMolecule(molecule *mol){
|
---|
| 68 | delete mol;
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[520c8b] | 71 | // getter and setter
|
---|
| 72 | const std::string molecule::getName(){
|
---|
| 73 | return std::string(name);
|
---|
| 74 | }
|
---|
| 75 |
|
---|
| 76 | void molecule::setName(const std::string _name){
|
---|
[2ba827] | 77 | OBSERVE;
|
---|
[520c8b] | 78 | strncpy(name,_name.c_str(),MAXSTRINGSIZE);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[cbc5fb] | 81 | moleculeId_t molecule::getId(){
|
---|
| 82 | return id;
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | void molecule::setId(moleculeId_t _id){
|
---|
| 86 | id =_id;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[ac9b56] | 89 | const std::string molecule::getFormula(){
|
---|
| 90 | return *formula;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | std::string molecule::calcFormula(){
|
---|
| 94 | int Counts[MAX_ELEMENTS];
|
---|
| 95 | stringstream sstr;
|
---|
| 96 | for (int j = 0; j<MAX_ELEMENTS;j++)
|
---|
| 97 | Counts[j] = 0;
|
---|
[9879f6] | 98 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
| 99 | Counts[(*iter)->type->Z]++;
|
---|
[ac9b56] | 100 | }
|
---|
| 101 | for(element* Elemental = elemente->end; Elemental != elemente->start; Elemental = Elemental->previous) {
|
---|
| 102 | if (Counts[Elemental->Z] != 0)
|
---|
| 103 | sstr << Elemental->symbol << Counts[Elemental->Z];
|
---|
| 104 | }
|
---|
| 105 | return sstr.str();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
[bd58fb] | 108 | /************************** Access to the List of Atoms ****************/
|
---|
| 109 |
|
---|
| 110 |
|
---|
| 111 | molecule::iterator molecule::begin(){
|
---|
| 112 | return molecule::iterator(atoms.begin(),this);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | molecule::const_iterator molecule::begin() const{
|
---|
| 116 | return atoms.begin();
|
---|
| 117 | }
|
---|
| 118 |
|
---|
[9879f6] | 119 | molecule::iterator molecule::end(){
|
---|
[bd58fb] | 120 | return molecule::iterator(atoms.end(),this);
|
---|
| 121 | }
|
---|
| 122 |
|
---|
[9879f6] | 123 | molecule::const_iterator molecule::end() const{
|
---|
[bd58fb] | 124 | return atoms.end();
|
---|
| 125 | }
|
---|
[520c8b] | 126 |
|
---|
[9879f6] | 127 | bool molecule::empty() const
|
---|
| 128 | {
|
---|
| 129 | return (begin() == end());
|
---|
| 130 | }
|
---|
| 131 |
|
---|
| 132 | size_t molecule::size() const
|
---|
| 133 | {
|
---|
| 134 | size_t counter = 0;
|
---|
| 135 | for (molecule::const_iterator iter = begin(); iter != end (); ++iter)
|
---|
| 136 | counter++;
|
---|
| 137 | return counter;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | molecule::const_iterator molecule::erase( const_iterator loc )
|
---|
| 141 | {
|
---|
| 142 | molecule::const_iterator iter = loc;
|
---|
| 143 | iter--;
|
---|
| 144 | atoms.erase( loc );
|
---|
| 145 | return iter;
|
---|
| 146 | }
|
---|
| 147 |
|
---|
| 148 | molecule::const_iterator molecule::erase( atom *& key )
|
---|
| 149 | {
|
---|
| 150 | molecule::const_iterator iter = find(key);
|
---|
| 151 | molecule::const_iterator runner = iter;
|
---|
| 152 | if (runner != begin()) {
|
---|
| 153 | runner--;
|
---|
| 154 | if (iter != end())
|
---|
| 155 | atoms.erase( key );
|
---|
| 156 | return runner;
|
---|
| 157 | } else
|
---|
| 158 | return end();
|
---|
| 159 | }
|
---|
| 160 |
|
---|
| 161 | molecule::const_iterator molecule::find ( atom *& key ) const
|
---|
| 162 | {
|
---|
| 163 | return atoms.find( key );
|
---|
| 164 | }
|
---|
| 165 |
|
---|
| 166 | pair<molecule::iterator,bool> molecule::insert ( atom * const key )
|
---|
| 167 | {
|
---|
[d3347e] | 168 | pair<atomSet::iterator,bool> res = atoms.insert(key);
|
---|
| 169 | return pair<iterator,bool>(iterator(res.first,this),res.second);
|
---|
[9879f6] | 170 | }
|
---|
| 171 |
|
---|
[14de469] | 172 | /** Adds given atom \a *pointer from molecule list.
|
---|
[69eb71] | 173 | * Increases molecule::last_atom and gives last number to added atom and names it according to its element::abbrev and molecule::AtomCount
|
---|
[14de469] | 174 | * \param *pointer allocated and set atom
|
---|
| 175 | * \return true - succeeded, false - atom not found in list
|
---|
| 176 | */
|
---|
| 177 | bool molecule::AddAtom(atom *pointer)
|
---|
[69eb71] | 178 | {
|
---|
[2ba827] | 179 | OBSERVE;
|
---|
[042f82] | 180 | if (pointer != NULL) {
|
---|
| 181 | pointer->sort = &pointer->nr;
|
---|
| 182 | AtomCount++;
|
---|
| 183 | if (pointer->type != NULL) {
|
---|
| 184 | if (ElementsInMolecule[pointer->type->Z] == 0)
|
---|
| 185 | ElementCount++;
|
---|
| 186 | ElementsInMolecule[pointer->type->Z]++; // increase number of elements
|
---|
| 187 | if (pointer->type->Z != 1)
|
---|
| 188 | NoNonHydrogen++;
|
---|
| 189 | if (pointer->Name == NULL) {
|
---|
[29812d] | 190 | Free(&pointer->Name);
|
---|
| 191 | pointer->Name = Malloc<char>(6, "molecule::AddAtom: *pointer->Name");
|
---|
[042f82] | 192 | sprintf(pointer->Name, "%2s%02d", pointer->type->symbol, pointer->nr+1);
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
[9879f6] | 195 | insert(pointer);
|
---|
[f721c6] | 196 | }
|
---|
[9879f6] | 197 | return true;
|
---|
[14de469] | 198 | };
|
---|
| 199 |
|
---|
| 200 | /** Adds a copy of the given atom \a *pointer from molecule list.
|
---|
| 201 | * Increases molecule::last_atom and gives last number to added atom.
|
---|
| 202 | * \param *pointer allocated and set atom
|
---|
[89c8b2] | 203 | * \return pointer to the newly added atom
|
---|
[14de469] | 204 | */
|
---|
| 205 | atom * molecule::AddCopyAtom(atom *pointer)
|
---|
[69eb71] | 206 | {
|
---|
[f721c6] | 207 | atom *retval = NULL;
|
---|
[2ba827] | 208 | OBSERVE;
|
---|
[042f82] | 209 | if (pointer != NULL) {
|
---|
[46d958] | 210 | atom *walker = pointer->clone();
|
---|
[29812d] | 211 | walker->Name = Malloc<char>(strlen(pointer->Name) + 1, "atom::atom: *Name");
|
---|
[042f82] | 212 | strcpy (walker->Name, pointer->Name);
|
---|
[9879f6] | 213 | insert(walker);
|
---|
[042f82] | 214 | if ((pointer->type != NULL) && (pointer->type->Z != 1))
|
---|
| 215 | NoNonHydrogen++;
|
---|
| 216 | AtomCount++;
|
---|
[f721c6] | 217 | retval=walker;
|
---|
| 218 | }
|
---|
| 219 | return retval;
|
---|
[14de469] | 220 | };
|
---|
| 221 |
|
---|
| 222 | /** Adds a Hydrogen atom in replacement for the given atom \a *partner in bond with a *origin.
|
---|
| 223 | * Here, we have to distinguish between single, double or triple bonds as stated by \a BondDegree, that each demand
|
---|
| 224 | * a different scheme when adding \a *replacement atom for the given one.
|
---|
| 225 | * -# Single Bond: Simply add new atom with bond distance rescaled to typical hydrogen one
|
---|
| 226 | * -# Double Bond: Here, we need the **BondList of the \a *origin atom, by scanning for the other bonds instead of
|
---|
[042f82] | 227 | * *Bond, we use the through these connected atoms to determine the plane they lie in, vector::MakeNormalvector().
|
---|
| 228 | * The orthonormal vector to this plane along with the vector in *Bond direction determines the plane the two
|
---|
| 229 | * replacing hydrogens shall lie in. Now, all remains to do is take the usual hydrogen double bond angle for the
|
---|
| 230 | * element of *origin and form the sin/cos admixture of both plane vectors for the new coordinates of the two
|
---|
| 231 | * hydrogens forming this angle with *origin.
|
---|
[14de469] | 232 | * -# Triple Bond: The idea is to set up a tetraoid (C1-H1-H2-H3) (however the lengths \f$b\f$ of the sides of the base
|
---|
[042f82] | 233 | * triangle formed by the to be added hydrogens are not equal to the typical bond distance \f$l\f$ but have to be
|
---|
| 234 | * determined from the typical angle \f$\alpha\f$ for a hydrogen triple connected to the element of *origin):
|
---|
| 235 | * We have the height \f$d\f$ as the vector in *Bond direction (from triangle C1-H1-H2).
|
---|
| 236 | * \f[ h = l \cdot \cos{\left (\frac{\alpha}{2} \right )} \qquad b = 2l \cdot \sin{\left (\frac{\alpha}{2} \right)} \quad \rightarrow \quad d = l \cdot \sqrt{\cos^2{\left (\frac{\alpha}{2} \right)}-\frac{1}{3}\cdot\sin^2{\left (\frac{\alpha}{2}\right )}}
|
---|
| 237 | * \f]
|
---|
| 238 | * vector::GetNormalvector() creates one orthonormal vector from this *Bond vector and vector::MakeNormalvector creates
|
---|
| 239 | * the third one from the former two vectors. The latter ones form the plane of the base triangle mentioned above.
|
---|
| 240 | * The lengths for these are \f$f\f$ and \f$g\f$ (from triangle H1-H2-(center of H1-H2-H3)) with knowledge that
|
---|
| 241 | * the median lines in an isosceles triangle meet in the center point with a ratio 2:1.
|
---|
| 242 | * \f[ f = \frac{b}{\sqrt{3}} \qquad g = \frac{b}{2}
|
---|
| 243 | * \f]
|
---|
| 244 | * as the coordination of all three atoms in the coordinate system of these three vectors:
|
---|
| 245 | * \f$\pmatrix{d & f & 0}\f$, \f$\pmatrix{d & -0.5 \cdot f & g}\f$ and \f$\pmatrix{d & -0.5 \cdot f & -g}\f$.
|
---|
[69eb71] | 246 | *
|
---|
[14de469] | 247 | * \param *out output stream for debugging
|
---|
[69eb71] | 248 | * \param *Bond pointer to bond between \a *origin and \a *replacement
|
---|
| 249 | * \param *TopOrigin son of \a *origin of upper level molecule (the atom added to this molecule as a copy of \a *origin)
|
---|
[14de469] | 250 | * \param *origin pointer to atom which acts as the origin for scaling the added hydrogen to correct bond length
|
---|
| 251 | * \param *replacement pointer to the atom which shall be copied as a hydrogen atom in this molecule
|
---|
| 252 | * \param isAngstroem whether the coordination of the given atoms is in AtomicLength (false) or Angstrom(true)
|
---|
| 253 | * \return number of atoms added, if < bond::BondDegree then something went wrong
|
---|
| 254 | * \todo double and triple bonds splitting (always use the tetraeder angle!)
|
---|
| 255 | */
|
---|
[e138de] | 256 | bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
|
---|
[14de469] | 257 | {
|
---|
[f721c6] | 258 | bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
|
---|
[2ba827] | 259 | OBSERVE;
|
---|
[042f82] | 260 | double bondlength; // bond length of the bond to be replaced/cut
|
---|
| 261 | double bondangle; // bond angle of the bond to be replaced/cut
|
---|
| 262 | double BondRescale; // rescale value for the hydrogen bond length
|
---|
| 263 | bond *FirstBond = NULL, *SecondBond = NULL; // Other bonds in double bond case to determine "other" plane
|
---|
| 264 | atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
|
---|
| 265 | double b,l,d,f,g, alpha, factors[NDIM]; // hold temporary values in triple bond case for coordination determination
|
---|
| 266 | Vector Orthovector1, Orthovector2; // temporary vectors in coordination construction
|
---|
| 267 | Vector InBondvector; // vector in direction of *Bond
|
---|
[1614174] | 268 | double *matrix = NULL;
|
---|
[266237] | 269 | bond *Binder = NULL;
|
---|
[042f82] | 270 |
|
---|
[e138de] | 271 | // Log() << Verbose(3) << "Begin of AddHydrogenReplacementAtom." << endl;
|
---|
[042f82] | 272 | // create vector in direction of bond
|
---|
| 273 | InBondvector.CopyVector(&TopReplacement->x);
|
---|
| 274 | InBondvector.SubtractVector(&TopOrigin->x);
|
---|
| 275 | bondlength = InBondvector.Norm();
|
---|
| 276 |
|
---|
| 277 | // is greater than typical bond distance? Then we have to correct periodically
|
---|
| 278 | // the problem is not the H being out of the box, but InBondvector have the wrong direction
|
---|
| 279 | // due to TopReplacement or Origin being on the wrong side!
|
---|
| 280 | if (bondlength > BondDistance) {
|
---|
[e138de] | 281 | // Log() << Verbose(4) << "InBondvector is: ";
|
---|
[042f82] | 282 | // InBondvector.Output(out);
|
---|
[e138de] | 283 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 284 | Orthovector1.Zero();
|
---|
| 285 | for (int i=NDIM;i--;) {
|
---|
| 286 | l = TopReplacement->x.x[i] - TopOrigin->x.x[i];
|
---|
| 287 | if (fabs(l) > BondDistance) { // is component greater than bond distance
|
---|
| 288 | Orthovector1.x[i] = (l < 0) ? -1. : +1.;
|
---|
| 289 | } // (signs are correct, was tested!)
|
---|
| 290 | }
|
---|
| 291 | matrix = ReturnFullMatrixforSymmetric(cell_size);
|
---|
| 292 | Orthovector1.MatrixMultiplication(matrix);
|
---|
| 293 | InBondvector.SubtractVector(&Orthovector1); // subtract just the additional translation
|
---|
[29812d] | 294 | Free(&matrix);
|
---|
[042f82] | 295 | bondlength = InBondvector.Norm();
|
---|
[e138de] | 296 | // Log() << Verbose(4) << "Corrected InBondvector is now: ";
|
---|
[042f82] | 297 | // InBondvector.Output(out);
|
---|
[e138de] | 298 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 299 | } // periodic correction finished
|
---|
| 300 |
|
---|
| 301 | InBondvector.Normalize();
|
---|
| 302 | // get typical bond length and store as scale factor for later
|
---|
| 303 | BondRescale = TopOrigin->type->HBondDistance[TopBond->BondDegree-1];
|
---|
| 304 | if (BondRescale == -1) {
|
---|
[717e0c] | 305 | eLog() << Verbose(1) << "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;
|
---|
[2ba827] | 306 | return false;
|
---|
[042f82] | 307 | BondRescale = bondlength;
|
---|
| 308 | } else {
|
---|
| 309 | if (!IsAngstroem)
|
---|
| 310 | BondRescale /= (1.*AtomicLengthToAngstroem);
|
---|
| 311 | }
|
---|
| 312 |
|
---|
| 313 | // discern single, double and triple bonds
|
---|
| 314 | switch(TopBond->BondDegree) {
|
---|
| 315 | case 1:
|
---|
[46d958] | 316 | FirstOtherAtom = World::get()->createAtom(); // new atom
|
---|
[042f82] | 317 | FirstOtherAtom->type = elemente->FindElement(1); // element is Hydrogen
|
---|
| 318 | FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
|
---|
| 319 | FirstOtherAtom->FixedIon = TopReplacement->FixedIon;
|
---|
| 320 | if (TopReplacement->type->Z == 1) { // neither rescale nor replace if it's already hydrogen
|
---|
| 321 | FirstOtherAtom->father = TopReplacement;
|
---|
| 322 | BondRescale = bondlength;
|
---|
| 323 | } else {
|
---|
| 324 | FirstOtherAtom->father = NULL; // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
|
---|
| 325 | }
|
---|
| 326 | InBondvector.Scale(&BondRescale); // rescale the distance vector to Hydrogen bond length
|
---|
| 327 | FirstOtherAtom->x.CopyVector(&TopOrigin->x); // set coordination to origin ...
|
---|
| 328 | FirstOtherAtom->x.AddVector(&InBondvector); // ... and add distance vector to replacement atom
|
---|
| 329 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
[e138de] | 330 | // Log() << Verbose(4) << "Added " << *FirstOtherAtom << " at: ";
|
---|
[042f82] | 331 | // FirstOtherAtom->x.Output(out);
|
---|
[e138de] | 332 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 333 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
| 334 | Binder->Cyclic = false;
|
---|
| 335 | Binder->Type = TreeEdge;
|
---|
| 336 | break;
|
---|
| 337 | case 2:
|
---|
| 338 | // determine two other bonds (warning if there are more than two other) plus valence sanity check
|
---|
[266237] | 339 | for (BondList::const_iterator Runner = TopOrigin->ListOfBonds.begin(); Runner != TopOrigin->ListOfBonds.end(); (++Runner)) {
|
---|
| 340 | if ((*Runner) != TopBond) {
|
---|
[042f82] | 341 | if (FirstBond == NULL) {
|
---|
[266237] | 342 | FirstBond = (*Runner);
|
---|
| 343 | FirstOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
|
---|
[042f82] | 344 | } else if (SecondBond == NULL) {
|
---|
[266237] | 345 | SecondBond = (*Runner);
|
---|
| 346 | SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
|
---|
[042f82] | 347 | } else {
|
---|
[717e0c] | 348 | eLog() << Verbose(2) << "Detected more than four bonds for atom " << TopOrigin->Name;
|
---|
[042f82] | 349 | }
|
---|
| 350 | }
|
---|
| 351 | }
|
---|
| 352 | if (SecondOtherAtom == NULL) { // then we have an atom with valence four, but only 3 bonds: one to replace and one which is TopBond (third is FirstBond)
|
---|
| 353 | SecondBond = TopBond;
|
---|
| 354 | SecondOtherAtom = TopReplacement;
|
---|
| 355 | }
|
---|
| 356 | if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
|
---|
[e138de] | 357 | // Log() << Verbose(3) << "Regarding the double bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << TopOrigin->Name << " to determine orthogonal plane." << endl;
|
---|
[042f82] | 358 |
|
---|
| 359 | // determine the plane of these two with the *origin
|
---|
| 360 | AllWentWell = AllWentWell && Orthovector1.MakeNormalVector(&TopOrigin->x, &FirstOtherAtom->x, &SecondOtherAtom->x);
|
---|
| 361 | } else {
|
---|
| 362 | Orthovector1.GetOneNormalVector(&InBondvector);
|
---|
| 363 | }
|
---|
[e138de] | 364 | //Log() << Verbose(3)<< "Orthovector1: ";
|
---|
[042f82] | 365 | //Orthovector1.Output(out);
|
---|
[e138de] | 366 | //Log() << Verbose(0) << endl;
|
---|
[042f82] | 367 | // orthogonal vector and bond vector between origin and replacement form the new plane
|
---|
| 368 | Orthovector1.MakeNormalVector(&InBondvector);
|
---|
| 369 | Orthovector1.Normalize();
|
---|
[e138de] | 370 | //Log() << Verbose(3) << "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << "." << endl;
|
---|
[042f82] | 371 |
|
---|
| 372 | // create the two Hydrogens ...
|
---|
[46d958] | 373 | FirstOtherAtom = World::get()->createAtom();
|
---|
| 374 | SecondOtherAtom = World::get()->createAtom();
|
---|
[042f82] | 375 | FirstOtherAtom->type = elemente->FindElement(1);
|
---|
| 376 | SecondOtherAtom->type = elemente->FindElement(1);
|
---|
| 377 | FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
|
---|
| 378 | FirstOtherAtom->FixedIon = TopReplacement->FixedIon;
|
---|
| 379 | SecondOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
|
---|
| 380 | SecondOtherAtom->FixedIon = TopReplacement->FixedIon;
|
---|
| 381 | FirstOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
| 382 | SecondOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
| 383 | bondangle = TopOrigin->type->HBondAngle[1];
|
---|
| 384 | if (bondangle == -1) {
|
---|
[717e0c] | 385 | eLog() << Verbose(1) << "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->Name << "<->" << TopReplacement->Name << ") of degree " << TopBond->BondDegree << "!" << endl;
|
---|
[2ba827] | 386 | return false;
|
---|
[042f82] | 387 | bondangle = 0;
|
---|
| 388 | }
|
---|
| 389 | bondangle *= M_PI/180./2.;
|
---|
[e138de] | 390 | // Log() << Verbose(3) << "ReScaleCheck: InBondvector ";
|
---|
[042f82] | 391 | // InBondvector.Output(out);
|
---|
[e138de] | 392 | // Log() << Verbose(0) << endl;
|
---|
| 393 | // Log() << Verbose(3) << "ReScaleCheck: Orthovector ";
|
---|
[042f82] | 394 | // Orthovector1.Output(out);
|
---|
[e138de] | 395 | // Log() << Verbose(0) << endl;
|
---|
| 396 | // Log() << Verbose(3) << "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle) << endl;
|
---|
[042f82] | 397 | FirstOtherAtom->x.Zero();
|
---|
| 398 | SecondOtherAtom->x.Zero();
|
---|
| 399 | for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
|
---|
| 400 | FirstOtherAtom->x.x[i] = InBondvector.x[i] * cos(bondangle) + Orthovector1.x[i] * (sin(bondangle));
|
---|
| 401 | SecondOtherAtom->x.x[i] = InBondvector.x[i] * cos(bondangle) + Orthovector1.x[i] * (-sin(bondangle));
|
---|
| 402 | }
|
---|
| 403 | FirstOtherAtom->x.Scale(&BondRescale); // rescale by correct BondDistance
|
---|
| 404 | SecondOtherAtom->x.Scale(&BondRescale);
|
---|
[e138de] | 405 | //Log() << Verbose(3) << "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << "." << endl;
|
---|
[042f82] | 406 | for(int i=NDIM;i--;) { // and make relative to origin atom
|
---|
| 407 | FirstOtherAtom->x.x[i] += TopOrigin->x.x[i];
|
---|
| 408 | SecondOtherAtom->x.x[i] += TopOrigin->x.x[i];
|
---|
| 409 | }
|
---|
| 410 | // ... and add to molecule
|
---|
| 411 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
| 412 | AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
|
---|
[e138de] | 413 | // Log() << Verbose(4) << "Added " << *FirstOtherAtom << " at: ";
|
---|
[042f82] | 414 | // FirstOtherAtom->x.Output(out);
|
---|
[e138de] | 415 | // Log() << Verbose(0) << endl;
|
---|
| 416 | // Log() << Verbose(4) << "Added " << *SecondOtherAtom << " at: ";
|
---|
[042f82] | 417 | // SecondOtherAtom->x.Output(out);
|
---|
[e138de] | 418 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 419 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
| 420 | Binder->Cyclic = false;
|
---|
| 421 | Binder->Type = TreeEdge;
|
---|
| 422 | Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
|
---|
| 423 | Binder->Cyclic = false;
|
---|
| 424 | Binder->Type = TreeEdge;
|
---|
| 425 | break;
|
---|
| 426 | case 3:
|
---|
| 427 | // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
|
---|
[46d958] | 428 | FirstOtherAtom = World::get()->createAtom();
|
---|
| 429 | SecondOtherAtom = World::get()->createAtom();
|
---|
| 430 | ThirdOtherAtom = World::get()->createAtom();
|
---|
[042f82] | 431 | FirstOtherAtom->type = elemente->FindElement(1);
|
---|
| 432 | SecondOtherAtom->type = elemente->FindElement(1);
|
---|
| 433 | ThirdOtherAtom->type = elemente->FindElement(1);
|
---|
| 434 | FirstOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
|
---|
| 435 | FirstOtherAtom->FixedIon = TopReplacement->FixedIon;
|
---|
| 436 | SecondOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
|
---|
| 437 | SecondOtherAtom->FixedIon = TopReplacement->FixedIon;
|
---|
| 438 | ThirdOtherAtom->v.CopyVector(&TopReplacement->v); // copy velocity
|
---|
| 439 | ThirdOtherAtom->FixedIon = TopReplacement->FixedIon;
|
---|
| 440 | FirstOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
| 441 | SecondOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
| 442 | ThirdOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
| 443 |
|
---|
| 444 | // we need to vectors orthonormal the InBondvector
|
---|
| 445 | AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(&InBondvector);
|
---|
[e138de] | 446 | // Log() << Verbose(3) << "Orthovector1: ";
|
---|
[042f82] | 447 | // Orthovector1.Output(out);
|
---|
[e138de] | 448 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 449 | AllWentWell = AllWentWell && Orthovector2.MakeNormalVector(&InBondvector, &Orthovector1);
|
---|
[e138de] | 450 | // Log() << Verbose(3) << "Orthovector2: ";
|
---|
[042f82] | 451 | // Orthovector2.Output(out);
|
---|
[e138de] | 452 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 453 |
|
---|
| 454 | // create correct coordination for the three atoms
|
---|
| 455 | alpha = (TopOrigin->type->HBondAngle[2])/180.*M_PI/2.; // retrieve triple bond angle from database
|
---|
| 456 | l = BondRescale; // desired bond length
|
---|
| 457 | b = 2.*l*sin(alpha); // base length of isosceles triangle
|
---|
| 458 | d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.); // length for InBondvector
|
---|
| 459 | f = b/sqrt(3.); // length for Orthvector1
|
---|
| 460 | g = b/2.; // length for Orthvector2
|
---|
[e138de] | 461 | // Log() << Verbose(3) << "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", " << endl;
|
---|
| 462 | // Log() << Verbose(3) << "The three Bond lengths: " << sqrt(d*d+f*f) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << endl;
|
---|
[042f82] | 463 | factors[0] = d;
|
---|
| 464 | factors[1] = f;
|
---|
| 465 | factors[2] = 0.;
|
---|
| 466 | FirstOtherAtom->x.LinearCombinationOfVectors(&InBondvector, &Orthovector1, &Orthovector2, factors);
|
---|
| 467 | factors[1] = -0.5*f;
|
---|
| 468 | factors[2] = g;
|
---|
| 469 | SecondOtherAtom->x.LinearCombinationOfVectors(&InBondvector, &Orthovector1, &Orthovector2, factors);
|
---|
| 470 | factors[2] = -g;
|
---|
| 471 | ThirdOtherAtom->x.LinearCombinationOfVectors(&InBondvector, &Orthovector1, &Orthovector2, factors);
|
---|
| 472 |
|
---|
| 473 | // rescale each to correct BondDistance
|
---|
| 474 | // FirstOtherAtom->x.Scale(&BondRescale);
|
---|
| 475 | // SecondOtherAtom->x.Scale(&BondRescale);
|
---|
| 476 | // ThirdOtherAtom->x.Scale(&BondRescale);
|
---|
| 477 |
|
---|
| 478 | // and relative to *origin atom
|
---|
| 479 | FirstOtherAtom->x.AddVector(&TopOrigin->x);
|
---|
| 480 | SecondOtherAtom->x.AddVector(&TopOrigin->x);
|
---|
| 481 | ThirdOtherAtom->x.AddVector(&TopOrigin->x);
|
---|
| 482 |
|
---|
| 483 | // ... and add to molecule
|
---|
| 484 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
| 485 | AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
|
---|
| 486 | AllWentWell = AllWentWell && AddAtom(ThirdOtherAtom);
|
---|
[e138de] | 487 | // Log() << Verbose(4) << "Added " << *FirstOtherAtom << " at: ";
|
---|
[042f82] | 488 | // FirstOtherAtom->x.Output(out);
|
---|
[e138de] | 489 | // Log() << Verbose(0) << endl;
|
---|
| 490 | // Log() << Verbose(4) << "Added " << *SecondOtherAtom << " at: ";
|
---|
[042f82] | 491 | // SecondOtherAtom->x.Output(out);
|
---|
[e138de] | 492 | // Log() << Verbose(0) << endl;
|
---|
| 493 | // Log() << Verbose(4) << "Added " << *ThirdOtherAtom << " at: ";
|
---|
[042f82] | 494 | // ThirdOtherAtom->x.Output(out);
|
---|
[e138de] | 495 | // Log() << Verbose(0) << endl;
|
---|
[042f82] | 496 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
| 497 | Binder->Cyclic = false;
|
---|
| 498 | Binder->Type = TreeEdge;
|
---|
| 499 | Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
|
---|
| 500 | Binder->Cyclic = false;
|
---|
| 501 | Binder->Type = TreeEdge;
|
---|
| 502 | Binder = AddBond(BottomOrigin, ThirdOtherAtom, 1);
|
---|
| 503 | Binder->Cyclic = false;
|
---|
| 504 | Binder->Type = TreeEdge;
|
---|
| 505 | break;
|
---|
| 506 | default:
|
---|
[717e0c] | 507 | eLog() << Verbose(1) << "BondDegree does not state single, double or triple bond!" << endl;
|
---|
[042f82] | 508 | AllWentWell = false;
|
---|
| 509 | break;
|
---|
| 510 | }
|
---|
[1614174] | 511 | Free(&matrix);
|
---|
[042f82] | 512 |
|
---|
[e138de] | 513 | // Log() << Verbose(3) << "End of AddHydrogenReplacementAtom." << endl;
|
---|
[042f82] | 514 | return AllWentWell;
|
---|
[14de469] | 515 | };
|
---|
| 516 |
|
---|
| 517 | /** Adds given atom \a *pointer from molecule list.
|
---|
| 518 | * Increases molecule::last_atom and gives last number to added atom.
|
---|
| 519 | * \param filename name and path of xyz file
|
---|
| 520 | * \return true - succeeded, false - file not found
|
---|
| 521 | */
|
---|
| 522 | bool molecule::AddXYZFile(string filename)
|
---|
[69eb71] | 523 | {
|
---|
[f721c6] | 524 |
|
---|
[042f82] | 525 | istringstream *input = NULL;
|
---|
| 526 | int NumberOfAtoms = 0; // atom number in xyz read
|
---|
| 527 | int i, j; // loop variables
|
---|
| 528 | atom *Walker = NULL; // pointer to added atom
|
---|
| 529 | char shorthand[3]; // shorthand for atom name
|
---|
| 530 | ifstream xyzfile; // xyz file
|
---|
| 531 | string line; // currently parsed line
|
---|
| 532 | double x[3]; // atom coordinates
|
---|
| 533 |
|
---|
| 534 | xyzfile.open(filename.c_str());
|
---|
| 535 | if (!xyzfile)
|
---|
| 536 | return false;
|
---|
| 537 |
|
---|
[2ba827] | 538 | OBSERVE;
|
---|
[042f82] | 539 | getline(xyzfile,line,'\n'); // Read numer of atoms in file
|
---|
| 540 | input = new istringstream(line);
|
---|
| 541 | *input >> NumberOfAtoms;
|
---|
[e138de] | 542 | Log() << Verbose(0) << "Parsing " << NumberOfAtoms << " atoms in file." << endl;
|
---|
[042f82] | 543 | getline(xyzfile,line,'\n'); // Read comment
|
---|
[e138de] | 544 | Log() << Verbose(1) << "Comment: " << line << endl;
|
---|
[042f82] | 545 |
|
---|
| 546 | if (MDSteps == 0) // no atoms yet present
|
---|
| 547 | MDSteps++;
|
---|
| 548 | for(i=0;i<NumberOfAtoms;i++){
|
---|
[46d958] | 549 | Walker = World::get()->createAtom();
|
---|
[042f82] | 550 | getline(xyzfile,line,'\n');
|
---|
| 551 | istringstream *item = new istringstream(line);
|
---|
| 552 | //istringstream input(line);
|
---|
[e138de] | 553 | //Log() << Verbose(1) << "Reading: " << line << endl;
|
---|
[042f82] | 554 | *item >> shorthand;
|
---|
| 555 | *item >> x[0];
|
---|
| 556 | *item >> x[1];
|
---|
| 557 | *item >> x[2];
|
---|
| 558 | Walker->type = elemente->FindElement(shorthand);
|
---|
| 559 | if (Walker->type == NULL) {
|
---|
[e359a8] | 560 | eLog() << Verbose(1) << "Could not parse the element at line: '" << line << "', setting to H.";
|
---|
[042f82] | 561 | Walker->type = elemente->FindElement(1);
|
---|
| 562 | }
|
---|
[fcd7b6] | 563 | if (Walker->Trajectory.R.size() <= (unsigned int)MDSteps) {
|
---|
| 564 | Walker->Trajectory.R.resize(MDSteps+10);
|
---|
| 565 | Walker->Trajectory.U.resize(MDSteps+10);
|
---|
| 566 | Walker->Trajectory.F.resize(MDSteps+10);
|
---|
[042f82] | 567 | }
|
---|
| 568 | for(j=NDIM;j--;) {
|
---|
| 569 | Walker->x.x[j] = x[j];
|
---|
[fcd7b6] | 570 | Walker->Trajectory.R.at(MDSteps-1).x[j] = x[j];
|
---|
| 571 | Walker->Trajectory.U.at(MDSteps-1).x[j] = 0;
|
---|
| 572 | Walker->Trajectory.F.at(MDSteps-1).x[j] = 0;
|
---|
[042f82] | 573 | }
|
---|
| 574 | AddAtom(Walker); // add to molecule
|
---|
| 575 | delete(item);
|
---|
| 576 | }
|
---|
| 577 | xyzfile.close();
|
---|
| 578 | delete(input);
|
---|
| 579 | return true;
|
---|
[14de469] | 580 | };
|
---|
| 581 |
|
---|
| 582 | /** Creates a copy of this molecule.
|
---|
| 583 | * \return copy of molecule
|
---|
| 584 | */
|
---|
| 585 | molecule *molecule::CopyMolecule()
|
---|
| 586 | {
|
---|
[042f82] | 587 | molecule *copy = new molecule(elemente);
|
---|
| 588 | atom *LeftAtom = NULL, *RightAtom = NULL;
|
---|
| 589 |
|
---|
| 590 | // copy all atoms
|
---|
[e9f8f9] | 591 | ActOnCopyWithEachAtom ( &molecule::AddCopyAtom, copy );
|
---|
[042f82] | 592 |
|
---|
| 593 | // copy all bonds
|
---|
| 594 | bond *Binder = first;
|
---|
| 595 | bond *NewBond = NULL;
|
---|
| 596 | while(Binder->next != last) {
|
---|
| 597 | Binder = Binder->next;
|
---|
[cee0b57] | 598 |
|
---|
[042f82] | 599 | // get the pendant atoms of current bond in the copy molecule
|
---|
[b453f9] | 600 | copy->ActOnAllAtoms( &atom::EqualsFather, (const atom *)Binder->leftatom, (const atom **)&LeftAtom );
|
---|
| 601 | copy->ActOnAllAtoms( &atom::EqualsFather, (const atom *)Binder->rightatom, (const atom **)&RightAtom );
|
---|
[cee0b57] | 602 |
|
---|
[042f82] | 603 | NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
|
---|
| 604 | NewBond->Cyclic = Binder->Cyclic;
|
---|
| 605 | if (Binder->Cyclic)
|
---|
| 606 | copy->NoCyclicBonds++;
|
---|
| 607 | NewBond->Type = Binder->Type;
|
---|
| 608 | }
|
---|
| 609 | // correct fathers
|
---|
[cee0b57] | 610 | ActOnAllAtoms( &atom::CorrectFather );
|
---|
| 611 |
|
---|
[042f82] | 612 | // copy values
|
---|
[e138de] | 613 | copy->CountAtoms();
|
---|
[042f82] | 614 | copy->CountElements();
|
---|
| 615 | if (first->next != last) { // if adjaceny list is present
|
---|
| 616 | copy->BondDistance = BondDistance;
|
---|
| 617 | }
|
---|
| 618 |
|
---|
| 619 | return copy;
|
---|
[14de469] | 620 | };
|
---|
| 621 |
|
---|
[89c8b2] | 622 |
|
---|
| 623 | /**
|
---|
| 624 | * Copies all atoms of a molecule which are within the defined parallelepiped.
|
---|
| 625 | *
|
---|
| 626 | * @param offest for the origin of the parallelepiped
|
---|
| 627 | * @param three vectors forming the matrix that defines the shape of the parallelpiped
|
---|
| 628 | */
|
---|
[b453f9] | 629 | molecule* molecule::CopyMoleculeFromSubRegion(const Vector offset, const double *parallelepiped) const {
|
---|
[89c8b2] | 630 | molecule *copy = new molecule(elemente);
|
---|
| 631 |
|
---|
[e9f8f9] | 632 | ActOnCopyWithEachAtomIfTrue ( &molecule::AddCopyAtom, copy, &atom::IsInParallelepiped, offset, parallelepiped );
|
---|
[89c8b2] | 633 |
|
---|
[e138de] | 634 | //TODO: copy->BuildInducedSubgraph(this);
|
---|
[89c8b2] | 635 |
|
---|
| 636 | return copy;
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[14de469] | 639 | /** Adds a bond to a the molecule specified by two atoms, \a *first and \a *second.
|
---|
| 640 | * Also updates molecule::BondCount and molecule::NoNonBonds.
|
---|
| 641 | * \param *first first atom in bond
|
---|
| 642 | * \param *second atom in bond
|
---|
| 643 | * \return pointer to bond or NULL on failure
|
---|
| 644 | */
|
---|
[cee0b57] | 645 | bond * molecule::AddBond(atom *atom1, atom *atom2, int degree)
|
---|
[14de469] | 646 | {
|
---|
[042f82] | 647 | bond *Binder = NULL;
|
---|
| 648 | if ((atom1 != NULL) && (FindAtom(atom1->nr) != NULL) && (atom2 != NULL) && (FindAtom(atom2->nr) != NULL)) {
|
---|
| 649 | Binder = new bond(atom1, atom2, degree, BondCount++);
|
---|
[266237] | 650 | atom1->RegisterBond(Binder);
|
---|
| 651 | atom2->RegisterBond(Binder);
|
---|
[042f82] | 652 | if ((atom1->type != NULL) && (atom1->type->Z != 1) && (atom2->type != NULL) && (atom2->type->Z != 1))
|
---|
| 653 | NoNonBonds++;
|
---|
| 654 | add(Binder, last);
|
---|
| 655 | } else {
|
---|
[717e0c] | 656 | eLog() << Verbose(1) << "Could not add bond between " << atom1->Name << " and " << atom2->Name << " as one or both are not present in the molecule." << endl;
|
---|
[042f82] | 657 | }
|
---|
| 658 | return Binder;
|
---|
[14de469] | 659 | };
|
---|
| 660 |
|
---|
[fa649a] | 661 | /** Remove bond from bond chain list and from the both atom::ListOfBonds.
|
---|
[69eb71] | 662 | * \todo Function not implemented yet
|
---|
[14de469] | 663 | * \param *pointer bond pointer
|
---|
| 664 | * \return true - bound found and removed, false - bond not found/removed
|
---|
| 665 | */
|
---|
| 666 | bool molecule::RemoveBond(bond *pointer)
|
---|
| 667 | {
|
---|
[e138de] | 668 | //eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;
|
---|
[fa649a] | 669 | pointer->leftatom->RegisterBond(pointer);
|
---|
| 670 | pointer->rightatom->RegisterBond(pointer);
|
---|
[042f82] | 671 | removewithoutcheck(pointer);
|
---|
| 672 | return true;
|
---|
[14de469] | 673 | };
|
---|
| 674 |
|
---|
| 675 | /** Remove every bond from bond chain list that atom \a *BondPartner is a constituent of.
|
---|
[69eb71] | 676 | * \todo Function not implemented yet
|
---|
[14de469] | 677 | * \param *BondPartner atom to be removed
|
---|
| 678 | * \return true - bounds found and removed, false - bonds not found/removed
|
---|
| 679 | */
|
---|
| 680 | bool molecule::RemoveBonds(atom *BondPartner)
|
---|
| 681 | {
|
---|
[e138de] | 682 | //eLog() << Verbose(1) << "molecule::RemoveBond: Function not implemented yet." << endl;
|
---|
[266237] | 683 | BondList::const_iterator ForeRunner;
|
---|
| 684 | while (!BondPartner->ListOfBonds.empty()) {
|
---|
| 685 | ForeRunner = BondPartner->ListOfBonds.begin();
|
---|
| 686 | RemoveBond(*ForeRunner);
|
---|
| 687 | }
|
---|
[042f82] | 688 | return false;
|
---|
[14de469] | 689 | };
|
---|
| 690 |
|
---|
[1907a7] | 691 | /** Set molecule::name from the basename without suffix in the given \a *filename.
|
---|
| 692 | * \param *filename filename
|
---|
| 693 | */
|
---|
[d67150] | 694 | void molecule::SetNameFromFilename(const char *filename)
|
---|
[1907a7] | 695 | {
|
---|
| 696 | int length = 0;
|
---|
[f7f7a4] | 697 | const char *molname = strrchr(filename, '/');
|
---|
| 698 | if (molname != NULL)
|
---|
| 699 | molname += sizeof(char); // search for filename without dirs
|
---|
| 700 | else
|
---|
| 701 | molname = filename; // contains no slashes
|
---|
[49e1ae] | 702 | const char *endname = strchr(molname, '.');
|
---|
[1907a7] | 703 | if ((endname == NULL) || (endname < molname))
|
---|
| 704 | length = strlen(molname);
|
---|
| 705 | else
|
---|
| 706 | length = strlen(molname) - strlen(endname);
|
---|
| 707 | strncpy(name, molname, length);
|
---|
[d67150] | 708 | name[length]='\0';
|
---|
[1907a7] | 709 | };
|
---|
| 710 |
|
---|
[14de469] | 711 | /** Sets the molecule::cell_size to the components of \a *dim (rectangular box)
|
---|
| 712 | * \param *dim vector class
|
---|
| 713 | */
|
---|
[e9b8bb] | 714 | void molecule::SetBoxDimension(Vector *dim)
|
---|
[14de469] | 715 | {
|
---|
[042f82] | 716 | cell_size[0] = dim->x[0];
|
---|
| 717 | cell_size[1] = 0.;
|
---|
| 718 | cell_size[2] = dim->x[1];
|
---|
| 719 | cell_size[3] = 0.;
|
---|
| 720 | cell_size[4] = 0.;
|
---|
| 721 | cell_size[5] = dim->x[2];
|
---|
[14de469] | 722 | };
|
---|
| 723 |
|
---|
[cee0b57] | 724 | /** Removes atom from molecule list and deletes it.
|
---|
| 725 | * \param *pointer atom to be removed
|
---|
| 726 | * \return true - succeeded, false - atom not found in list
|
---|
[a9d254] | 727 | */
|
---|
[cee0b57] | 728 | bool molecule::RemoveAtom(atom *pointer)
|
---|
[a9d254] | 729 | {
|
---|
[cee0b57] | 730 | if (ElementsInMolecule[pointer->type->Z] != 0) { // this would indicate an error
|
---|
| 731 | ElementsInMolecule[pointer->type->Z]--; // decrease number of atom of this element
|
---|
| 732 | AtomCount--;
|
---|
| 733 | } else
|
---|
[717e0c] | 734 | eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
|
---|
[cee0b57] | 735 | if (ElementsInMolecule[pointer->type->Z] == 0) // was last atom of this element?
|
---|
| 736 | ElementCount--;
|
---|
[266237] | 737 | RemoveBonds(pointer);
|
---|
[9879f6] | 738 | erase(pointer);
|
---|
| 739 | return true;
|
---|
[a9d254] | 740 | };
|
---|
| 741 |
|
---|
[cee0b57] | 742 | /** Removes atom from molecule list, but does not delete it.
|
---|
| 743 | * \param *pointer atom to be removed
|
---|
| 744 | * \return true - succeeded, false - atom not found in list
|
---|
[f3278b] | 745 | */
|
---|
[cee0b57] | 746 | bool molecule::UnlinkAtom(atom *pointer)
|
---|
[f3278b] | 747 | {
|
---|
[cee0b57] | 748 | if (pointer == NULL)
|
---|
| 749 | return false;
|
---|
| 750 | if (ElementsInMolecule[pointer->type->Z] != 0) // this would indicate an error
|
---|
| 751 | ElementsInMolecule[pointer->type->Z]--; // decrease number of atom of this element
|
---|
| 752 | else
|
---|
[717e0c] | 753 | eLog() << Verbose(1) << "Atom " << pointer->Name << " is of element " << pointer->type->Z << " but the entry in the table of the molecule is 0!" << endl;
|
---|
[cee0b57] | 754 | if (ElementsInMolecule[pointer->type->Z] == 0) // was last atom of this element?
|
---|
| 755 | ElementCount--;
|
---|
[9879f6] | 756 | erase(pointer);
|
---|
[cee0b57] | 757 | return true;
|
---|
[f3278b] | 758 | };
|
---|
| 759 |
|
---|
[cee0b57] | 760 | /** Removes every atom from molecule list.
|
---|
| 761 | * \return true - succeeded, false - atom not found in list
|
---|
[14de469] | 762 | */
|
---|
[cee0b57] | 763 | bool molecule::CleanupMolecule()
|
---|
[14de469] | 764 | {
|
---|
[9879f6] | 765 | for (molecule::iterator iter = begin(); !empty(); iter = begin())
|
---|
| 766 | erase(iter);
|
---|
| 767 | return (cleanup(first,last));
|
---|
[69eb71] | 768 | };
|
---|
[14de469] | 769 |
|
---|
[cee0b57] | 770 | /** Finds an atom specified by its continuous number.
|
---|
| 771 | * \param Nr number of atom withim molecule
|
---|
| 772 | * \return pointer to atom or NULL
|
---|
[14de469] | 773 | */
|
---|
[9879f6] | 774 | atom * molecule::FindAtom(int Nr) const
|
---|
| 775 | {
|
---|
| 776 | molecule::const_iterator iter = begin();
|
---|
| 777 | for (; iter != end(); ++iter)
|
---|
| 778 | if ((*iter)->nr == Nr)
|
---|
| 779 | break;
|
---|
| 780 | if (iter != end()) {
|
---|
[e138de] | 781 | //Log() << Verbose(0) << "Found Atom Nr. " << walker->nr << endl;
|
---|
[9879f6] | 782 | return (*iter);
|
---|
[cee0b57] | 783 | } else {
|
---|
[e138de] | 784 | Log() << Verbose(0) << "Atom not found in list." << endl;
|
---|
[cee0b57] | 785 | return NULL;
|
---|
[042f82] | 786 | }
|
---|
[69eb71] | 787 | };
|
---|
[14de469] | 788 |
|
---|
[cee0b57] | 789 | /** Asks for atom number, and checks whether in list.
|
---|
| 790 | * \param *text question before entering
|
---|
[a6b7fb] | 791 | */
|
---|
[cee0b57] | 792 | atom * molecule::AskAtom(string text)
|
---|
[a6b7fb] | 793 | {
|
---|
[cee0b57] | 794 | int No;
|
---|
| 795 | atom *ion = NULL;
|
---|
| 796 | do {
|
---|
[e138de] | 797 | //Log() << Verbose(0) << "============Atom list==========================" << endl;
|
---|
[cee0b57] | 798 | //mol->Output((ofstream *)&cout);
|
---|
[e138de] | 799 | //Log() << Verbose(0) << "===============================================" << endl;
|
---|
| 800 | Log() << Verbose(0) << text;
|
---|
[cee0b57] | 801 | cin >> No;
|
---|
| 802 | ion = this->FindAtom(No);
|
---|
| 803 | } while (ion == NULL);
|
---|
| 804 | return ion;
|
---|
[a6b7fb] | 805 | };
|
---|
| 806 |
|
---|
[cee0b57] | 807 | /** Checks if given coordinates are within cell volume.
|
---|
| 808 | * \param *x array of coordinates
|
---|
| 809 | * \return true - is within, false - out of cell
|
---|
[14de469] | 810 | */
|
---|
[cee0b57] | 811 | bool molecule::CheckBounds(const Vector *x) const
|
---|
[14de469] | 812 | {
|
---|
[cee0b57] | 813 | bool result = true;
|
---|
| 814 | int j =-1;
|
---|
| 815 | for (int i=0;i<NDIM;i++) {
|
---|
| 816 | j += i+1;
|
---|
| 817 | result = result && ((x->x[i] >= 0) && (x->x[i] < cell_size[j]));
|
---|
[042f82] | 818 | }
|
---|
[cee0b57] | 819 | //return result;
|
---|
| 820 | return true; /// probably not gonna use the check no more
|
---|
[69eb71] | 821 | };
|
---|
[14de469] | 822 |
|
---|
[cee0b57] | 823 | /** Prints molecule to *out.
|
---|
| 824 | * \param *out output stream
|
---|
[14de469] | 825 | */
|
---|
[e138de] | 826 | bool molecule::Output(ofstream * const output)
|
---|
[14de469] | 827 | {
|
---|
[cee0b57] | 828 | int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
|
---|
| 829 | CountElements();
|
---|
[042f82] | 830 |
|
---|
[cee0b57] | 831 | for (int i=0;i<MAX_ELEMENTS;++i) {
|
---|
| 832 | AtomNo[i] = 0;
|
---|
| 833 | ElementNo[i] = 0;
|
---|
[042f82] | 834 | }
|
---|
[e138de] | 835 | if (output == NULL) {
|
---|
[cee0b57] | 836 | return false;
|
---|
| 837 | } else {
|
---|
[e138de] | 838 | *output << "#Ion_TypeNr._Nr.R[0] R[1] R[2] MoveType (0 MoveIon, 1 FixedIon)" << endl;
|
---|
[e9f8f9] | 839 | SetIndexedArrayForEachAtomTo ( ElementNo, &element::Z, &AbsoluteValue, 1);
|
---|
[cee0b57] | 840 | int current=1;
|
---|
| 841 | for (int i=0;i<MAX_ELEMENTS;++i) {
|
---|
| 842 | if (ElementNo[i] == 1)
|
---|
| 843 | ElementNo[i] = current++;
|
---|
| 844 | }
|
---|
[e138de] | 845 | ActOnAllAtoms( &atom::OutputArrayIndexed, output, (const int *)ElementNo, (int *)AtomNo, (const char *) NULL );
|
---|
[cee0b57] | 846 | return true;
|
---|
[042f82] | 847 | }
|
---|
[14de469] | 848 | };
|
---|
| 849 |
|
---|
[cee0b57] | 850 | /** Prints molecule with all atomic trajectory positions to *out.
|
---|
| 851 | * \param *out output stream
|
---|
[21c017] | 852 | */
|
---|
[e138de] | 853 | bool molecule::OutputTrajectories(ofstream * const output)
|
---|
[21c017] | 854 | {
|
---|
[cee0b57] | 855 | int ElementNo[MAX_ELEMENTS], AtomNo[MAX_ELEMENTS];
|
---|
| 856 | CountElements();
|
---|
[21c017] | 857 |
|
---|
[e138de] | 858 | if (output == NULL) {
|
---|
[cee0b57] | 859 | return false;
|
---|
| 860 | } else {
|
---|
| 861 | for (int step = 0; step < MDSteps; step++) {
|
---|
| 862 | if (step == 0) {
|
---|
[e138de] | 863 | *output << "#Ion_TypeNr._Nr.R[0] R[1] R[2] MoveType (0 MoveIon, 1 FixedIon)" << endl;
|
---|
[205ccd] | 864 | } else {
|
---|
[e138de] | 865 | *output << "# ====== MD step " << step << " =========" << endl;
|
---|
[cee0b57] | 866 | }
|
---|
| 867 | for (int i=0;i<MAX_ELEMENTS;++i) {
|
---|
| 868 | AtomNo[i] = 0;
|
---|
| 869 | ElementNo[i] = 0;
|
---|
[205ccd] | 870 | }
|
---|
[e9f8f9] | 871 | SetIndexedArrayForEachAtomTo ( ElementNo, &element::Z, &AbsoluteValue, 1);
|
---|
| 872 | int current=1;
|
---|
| 873 | for (int i=0;i<MAX_ELEMENTS;++i) {
|
---|
| 874 | if (ElementNo[i] == 1)
|
---|
| 875 | ElementNo[i] = current++;
|
---|
| 876 | }
|
---|
[e138de] | 877 | ActOnAllAtoms( &atom::OutputTrajectory, output, (const int *)ElementNo, AtomNo, (const int)step );
|
---|
[21c017] | 878 | }
|
---|
[cee0b57] | 879 | return true;
|
---|
[21c017] | 880 | }
|
---|
| 881 | };
|
---|
| 882 |
|
---|
[266237] | 883 | /** Outputs contents of each atom::ListOfBonds.
|
---|
[cee0b57] | 884 | * \param *out output stream
|
---|
[14de469] | 885 | */
|
---|
[e138de] | 886 | void molecule::OutputListOfBonds() const
|
---|
[14de469] | 887 | {
|
---|
[e138de] | 888 | Log() << Verbose(2) << endl << "From Contents of ListOfBonds, all non-hydrogen atoms:" << endl;
|
---|
| 889 | ActOnAllAtoms (&atom::OutputBondOfAtom );
|
---|
| 890 | Log() << Verbose(0) << endl;
|
---|
[14de469] | 891 | };
|
---|
| 892 |
|
---|
[cee0b57] | 893 | /** Output of element before the actual coordination list.
|
---|
| 894 | * \param *out stream pointer
|
---|
[14de469] | 895 | */
|
---|
[e138de] | 896 | bool molecule::Checkout(ofstream * const output) const
|
---|
[14de469] | 897 | {
|
---|
[e138de] | 898 | return elemente->Checkout(output, ElementsInMolecule);
|
---|
[6e9353] | 899 | };
|
---|
| 900 |
|
---|
[cee0b57] | 901 | /** Prints molecule with all its trajectories to *out as xyz file.
|
---|
| 902 | * \param *out output stream
|
---|
[d7e30c] | 903 | */
|
---|
[e138de] | 904 | bool molecule::OutputTrajectoriesXYZ(ofstream * const output)
|
---|
[d7e30c] | 905 | {
|
---|
[cee0b57] | 906 | time_t now;
|
---|
[042f82] | 907 |
|
---|
[e138de] | 908 | if (output != NULL) {
|
---|
[681a8a] | 909 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
[cee0b57] | 910 | for (int step=0;step<MDSteps;step++) {
|
---|
[e138de] | 911 | *output << AtomCount << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
|
---|
| 912 | ActOnAllAtoms( &atom::OutputTrajectoryXYZ, output, step );
|
---|
[042f82] | 913 | }
|
---|
[cee0b57] | 914 | return true;
|
---|
| 915 | } else
|
---|
| 916 | return false;
|
---|
[14de469] | 917 | };
|
---|
| 918 |
|
---|
[cee0b57] | 919 | /** Prints molecule to *out as xyz file.
|
---|
| 920 | * \param *out output stream
|
---|
[69eb71] | 921 | */
|
---|
[e138de] | 922 | bool molecule::OutputXYZ(ofstream * const output) const
|
---|
[4aa03a] | 923 | {
|
---|
[cee0b57] | 924 | time_t now;
|
---|
[042f82] | 925 |
|
---|
[e138de] | 926 | if (output != NULL) {
|
---|
[23b830] | 927 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
[e138de] | 928 | *output << AtomCount << "\n\tCreated by molecuilder on " << ctime(&now);
|
---|
| 929 | ActOnAllAtoms( &atom::OutputXYZLine, output );
|
---|
[042f82] | 930 | return true;
|
---|
[cee0b57] | 931 | } else
|
---|
| 932 | return false;
|
---|
| 933 | };
|
---|
[4aa03a] | 934 |
|
---|
[cee0b57] | 935 | /** Brings molecule::AtomCount and atom::*Name up-to-date.
|
---|
[14de469] | 936 | * \param *out output stream for debugging
|
---|
| 937 | */
|
---|
[e138de] | 938 | void molecule::CountAtoms()
|
---|
[14de469] | 939 | {
|
---|
[d3347e] | 940 | cout << "!!!!!!!!!!! Assigning Numbers to atoms " << endl;
|
---|
[9879f6] | 941 | int i = size();
|
---|
[cee0b57] | 942 | if ((AtomCount == 0) || (i != AtomCount)) {
|
---|
[d3347e] | 943 | cout << "!!!!!!!!! Counting needed" << endl;
|
---|
[e138de] | 944 | Log() << Verbose(3) << "Mismatch in AtomCount " << AtomCount << " and recounted number " << i << ", renaming all." << endl;
|
---|
[cee0b57] | 945 | AtomCount = i;
|
---|
[042f82] | 946 |
|
---|
[cee0b57] | 947 | // count NonHydrogen atoms and give each atom a unique name
|
---|
| 948 | if (AtomCount != 0) {
|
---|
| 949 | i=0;
|
---|
| 950 | NoNonHydrogen = 0;
|
---|
[9879f6] | 951 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
| 952 | (*iter)->nr = i; // update number in molecule (for easier referencing in FragmentMolecule lateron)
|
---|
| 953 | if ((*iter)->type->Z != 1) // count non-hydrogen atoms whilst at it
|
---|
[cee0b57] | 954 | NoNonHydrogen++;
|
---|
[9879f6] | 955 | Free(&(*iter)->Name);
|
---|
| 956 | (*iter)->Name = Malloc<char>(6, "molecule::CountAtoms: *walker->Name");
|
---|
| 957 | sprintf((*iter)->Name, "%2s%02d", (*iter)->type->symbol, (*iter)->nr+1);
|
---|
| 958 | Log() << Verbose(3) << "Naming atom nr. " << (*iter)->nr << " " << (*iter)->Name << "." << endl;
|
---|
[cee0b57] | 959 | i++;
|
---|
[042f82] | 960 | }
|
---|
[cee0b57] | 961 | } else
|
---|
[e138de] | 962 | Log() << Verbose(3) << "AtomCount is still " << AtomCount << ", thus counting nothing." << endl;
|
---|
[042f82] | 963 | }
|
---|
[d3347e] | 964 | else {
|
---|
| 965 | cout << "!!!!!!!!! Nothing changed while counting" << endl;
|
---|
| 966 | }
|
---|
[cee0b57] | 967 | };
|
---|
[042f82] | 968 |
|
---|
[cee0b57] | 969 | /** Brings molecule::ElementCount and molecule::ElementsInMolecule up-to-date.
|
---|
| 970 | */
|
---|
| 971 | void molecule::CountElements()
|
---|
| 972 | {
|
---|
[23b830] | 973 | for(int i=MAX_ELEMENTS;i--;)
|
---|
[cee0b57] | 974 | ElementsInMolecule[i] = 0;
|
---|
| 975 | ElementCount = 0;
|
---|
[042f82] | 976 |
|
---|
[23b830] | 977 | SetIndexedArrayForEachAtomTo ( ElementsInMolecule, &element::Z, &Increment, 1);
|
---|
| 978 |
|
---|
| 979 | for(int i=MAX_ELEMENTS;i--;)
|
---|
[cee0b57] | 980 | ElementCount += (ElementsInMolecule[i] != 0 ? 1 : 0);
|
---|
| 981 | };
|
---|
[042f82] | 982 |
|
---|
| 983 |
|
---|
[cee0b57] | 984 | /** Counts necessary number of valence electrons and returns number and SpinType.
|
---|
| 985 | * \param configuration containing everything
|
---|
| 986 | */
|
---|
| 987 | void molecule::CalculateOrbitals(class config &configuration)
|
---|
| 988 | {
|
---|
| 989 | configuration.MaxPsiDouble = configuration.PsiMaxNoDown = configuration.PsiMaxNoUp = configuration.PsiType = 0;
|
---|
| 990 | for(int i=MAX_ELEMENTS;i--;) {
|
---|
| 991 | if (ElementsInMolecule[i] != 0) {
|
---|
[e138de] | 992 | //Log() << Verbose(0) << "CalculateOrbitals: " << elemente->FindElement(i)->name << " has a valence of " << (int)elemente->FindElement(i)->Valence << " and there are " << ElementsInMolecule[i] << " of it." << endl;
|
---|
[cee0b57] | 993 | configuration.MaxPsiDouble += ElementsInMolecule[i]*((int)elemente->FindElement(i)->Valence);
|
---|
[042f82] | 994 | }
|
---|
| 995 | }
|
---|
[cee0b57] | 996 | configuration.PsiMaxNoDown = configuration.MaxPsiDouble/2 + (configuration.MaxPsiDouble % 2);
|
---|
| 997 | configuration.PsiMaxNoUp = configuration.MaxPsiDouble/2;
|
---|
| 998 | configuration.MaxPsiDouble /= 2;
|
---|
| 999 | configuration.PsiType = (configuration.PsiMaxNoDown == configuration.PsiMaxNoUp) ? 0 : 1;
|
---|
| 1000 | if ((configuration.PsiType == 1) && (configuration.ProcPEPsi < 2)) {
|
---|
| 1001 | configuration.ProcPEGamma /= 2;
|
---|
| 1002 | configuration.ProcPEPsi *= 2;
|
---|
| 1003 | } else {
|
---|
| 1004 | configuration.ProcPEGamma *= configuration.ProcPEPsi;
|
---|
| 1005 | configuration.ProcPEPsi = 1;
|
---|
| 1006 | }
|
---|
| 1007 | configuration.InitMaxMinStopStep = configuration.MaxMinStopStep = configuration.MaxPsiDouble;
|
---|
[14de469] | 1008 | };
|
---|
| 1009 |
|
---|
| 1010 | /** Determines whether two molecules actually contain the same atoms and coordination.
|
---|
| 1011 | * \param *out output stream for debugging
|
---|
| 1012 | * \param *OtherMolecule the molecule to compare this one to
|
---|
| 1013 | * \param threshold upper limit of difference when comparing the coordination.
|
---|
| 1014 | * \return NULL - not equal, otherwise an allocated (molecule::AtomCount) permutation map of the atom numbers (which corresponds to which)
|
---|
| 1015 | */
|
---|
[e138de] | 1016 | int * molecule::IsEqualToWithinThreshold(molecule *OtherMolecule, double threshold)
|
---|
[14de469] | 1017 | {
|
---|
[042f82] | 1018 | int flag;
|
---|
| 1019 | double *Distances = NULL, *OtherDistances = NULL;
|
---|
| 1020 | Vector CenterOfGravity, OtherCenterOfGravity;
|
---|
| 1021 | size_t *PermMap = NULL, *OtherPermMap = NULL;
|
---|
| 1022 | int *PermutationMap = NULL;
|
---|
| 1023 | bool result = true; // status of comparison
|
---|
| 1024 |
|
---|
[e138de] | 1025 | Log() << Verbose(3) << "Begin of IsEqualToWithinThreshold." << endl;
|
---|
[042f82] | 1026 | /// first count both their atoms and elements and update lists thereby ...
|
---|
[e138de] | 1027 | //Log() << Verbose(0) << "Counting atoms, updating list" << endl;
|
---|
| 1028 | CountAtoms();
|
---|
| 1029 | OtherMolecule->CountAtoms();
|
---|
[042f82] | 1030 | CountElements();
|
---|
| 1031 | OtherMolecule->CountElements();
|
---|
| 1032 |
|
---|
| 1033 | /// ... and compare:
|
---|
| 1034 | /// -# AtomCount
|
---|
| 1035 | if (result) {
|
---|
| 1036 | if (AtomCount != OtherMolecule->AtomCount) {
|
---|
[e138de] | 1037 | Log() << Verbose(4) << "AtomCounts don't match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
|
---|
[042f82] | 1038 | result = false;
|
---|
[e138de] | 1039 | } else Log() << Verbose(4) << "AtomCounts match: " << AtomCount << " == " << OtherMolecule->AtomCount << endl;
|
---|
[042f82] | 1040 | }
|
---|
| 1041 | /// -# ElementCount
|
---|
| 1042 | if (result) {
|
---|
| 1043 | if (ElementCount != OtherMolecule->ElementCount) {
|
---|
[e138de] | 1044 | Log() << Verbose(4) << "ElementCount don't match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
|
---|
[042f82] | 1045 | result = false;
|
---|
[e138de] | 1046 | } else Log() << Verbose(4) << "ElementCount match: " << ElementCount << " == " << OtherMolecule->ElementCount << endl;
|
---|
[042f82] | 1047 | }
|
---|
| 1048 | /// -# ElementsInMolecule
|
---|
| 1049 | if (result) {
|
---|
| 1050 | for (flag=MAX_ELEMENTS;flag--;) {
|
---|
[e138de] | 1051 | //Log() << Verbose(5) << "Element " << flag << ": " << ElementsInMolecule[flag] << " <-> " << OtherMolecule->ElementsInMolecule[flag] << "." << endl;
|
---|
[042f82] | 1052 | if (ElementsInMolecule[flag] != OtherMolecule->ElementsInMolecule[flag])
|
---|
| 1053 | break;
|
---|
| 1054 | }
|
---|
| 1055 | if (flag < MAX_ELEMENTS) {
|
---|
[e138de] | 1056 | Log() << Verbose(4) << "ElementsInMolecule don't match." << endl;
|
---|
[042f82] | 1057 | result = false;
|
---|
[e138de] | 1058 | } else Log() << Verbose(4) << "ElementsInMolecule match." << endl;
|
---|
[042f82] | 1059 | }
|
---|
| 1060 | /// then determine and compare center of gravity for each molecule ...
|
---|
| 1061 | if (result) {
|
---|
[e138de] | 1062 | Log() << Verbose(5) << "Calculating Centers of Gravity" << endl;
|
---|
[437922] | 1063 | DeterminePeriodicCenter(CenterOfGravity);
|
---|
| 1064 | OtherMolecule->DeterminePeriodicCenter(OtherCenterOfGravity);
|
---|
[e138de] | 1065 | Log() << Verbose(5) << "Center of Gravity: ";
|
---|
| 1066 | CenterOfGravity.Output();
|
---|
| 1067 | Log() << Verbose(0) << endl << Verbose(5) << "Other Center of Gravity: ";
|
---|
| 1068 | OtherCenterOfGravity.Output();
|
---|
| 1069 | Log() << Verbose(0) << endl;
|
---|
[042f82] | 1070 | if (CenterOfGravity.DistanceSquared(&OtherCenterOfGravity) > threshold*threshold) {
|
---|
[e138de] | 1071 | Log() << Verbose(4) << "Centers of gravity don't match." << endl;
|
---|
[042f82] | 1072 | result = false;
|
---|
| 1073 | }
|
---|
| 1074 | }
|
---|
| 1075 |
|
---|
| 1076 | /// ... then make a list with the euclidian distance to this center for each atom of both molecules
|
---|
| 1077 | if (result) {
|
---|
[e138de] | 1078 | Log() << Verbose(5) << "Calculating distances" << endl;
|
---|
[7218f8] | 1079 | Distances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: Distances");
|
---|
| 1080 | OtherDistances = Calloc<double>(AtomCount, "molecule::IsEqualToWithinThreshold: OtherDistances");
|
---|
[b453f9] | 1081 | SetIndexedArrayForEachAtomTo ( Distances, &atom::nr, &atom::DistanceSquaredToVector, (const Vector &)CenterOfGravity);
|
---|
| 1082 | SetIndexedArrayForEachAtomTo ( OtherDistances, &atom::nr, &atom::DistanceSquaredToVector, (const Vector &)CenterOfGravity);
|
---|
[042f82] | 1083 |
|
---|
| 1084 | /// ... sort each list (using heapsort (o(N log N)) from GSL)
|
---|
[e138de] | 1085 | Log() << Verbose(5) << "Sorting distances" << endl;
|
---|
[7218f8] | 1086 | PermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermMap");
|
---|
| 1087 | OtherPermMap = Calloc<size_t>(AtomCount, "molecule::IsEqualToWithinThreshold: *OtherPermMap");
|
---|
[042f82] | 1088 | gsl_heapsort_index (PermMap, Distances, AtomCount, sizeof(double), CompareDoubles);
|
---|
| 1089 | gsl_heapsort_index (OtherPermMap, OtherDistances, AtomCount, sizeof(double), CompareDoubles);
|
---|
[7218f8] | 1090 | PermutationMap = Calloc<int>(AtomCount, "molecule::IsEqualToWithinThreshold: *PermutationMap");
|
---|
[e138de] | 1091 | Log() << Verbose(5) << "Combining Permutation Maps" << endl;
|
---|
[042f82] | 1092 | for(int i=AtomCount;i--;)
|
---|
| 1093 | PermutationMap[PermMap[i]] = (int) OtherPermMap[i];
|
---|
| 1094 |
|
---|
[29812d] | 1095 | /// ... and compare them step by step, whether the difference is individually(!) below \a threshold for all
|
---|
[e138de] | 1096 | Log() << Verbose(4) << "Comparing distances" << endl;
|
---|
[042f82] | 1097 | flag = 0;
|
---|
| 1098 | for (int i=0;i<AtomCount;i++) {
|
---|
[e138de] | 1099 | Log() << Verbose(5) << "Distances squared: |" << Distances[PermMap[i]] << " - " << OtherDistances[OtherPermMap[i]] << "| = " << fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) << " ?<? " << threshold << endl;
|
---|
[042f82] | 1100 | if (fabs(Distances[PermMap[i]] - OtherDistances[OtherPermMap[i]]) > threshold*threshold)
|
---|
| 1101 | flag = 1;
|
---|
| 1102 | }
|
---|
| 1103 |
|
---|
[29812d] | 1104 | // free memory
|
---|
| 1105 | Free(&PermMap);
|
---|
| 1106 | Free(&OtherPermMap);
|
---|
| 1107 | Free(&Distances);
|
---|
| 1108 | Free(&OtherDistances);
|
---|
[042f82] | 1109 | if (flag) { // if not equal
|
---|
[29812d] | 1110 | Free(&PermutationMap);
|
---|
[042f82] | 1111 | result = false;
|
---|
| 1112 | }
|
---|
| 1113 | }
|
---|
| 1114 | /// return pointer to map if all distances were below \a threshold
|
---|
[e138de] | 1115 | Log() << Verbose(3) << "End of IsEqualToWithinThreshold." << endl;
|
---|
[042f82] | 1116 | if (result) {
|
---|
[e138de] | 1117 | Log() << Verbose(3) << "Result: Equal." << endl;
|
---|
[042f82] | 1118 | return PermutationMap;
|
---|
| 1119 | } else {
|
---|
[e138de] | 1120 | Log() << Verbose(3) << "Result: Not equal." << endl;
|
---|
[042f82] | 1121 | return NULL;
|
---|
| 1122 | }
|
---|
[14de469] | 1123 | };
|
---|
| 1124 |
|
---|
| 1125 | /** Returns an index map for two father-son-molecules.
|
---|
| 1126 | * The map tells which atom in this molecule corresponds to which one in the other molecul with their fathers.
|
---|
| 1127 | * \param *out output stream for debugging
|
---|
| 1128 | * \param *OtherMolecule corresponding molecule with fathers
|
---|
| 1129 | * \return allocated map of size molecule::AtomCount with map
|
---|
| 1130 | * \todo make this with a good sort O(n), not O(n^2)
|
---|
| 1131 | */
|
---|
[e138de] | 1132 | int * molecule::GetFatherSonAtomicMap(molecule *OtherMolecule)
|
---|
[14de469] | 1133 | {
|
---|
[e138de] | 1134 | Log() << Verbose(3) << "Begin of GetFatherAtomicMap." << endl;
|
---|
[29812d] | 1135 | int *AtomicMap = Malloc<int>(AtomCount, "molecule::GetAtomicMap: *AtomicMap");
|
---|
[042f82] | 1136 | for (int i=AtomCount;i--;)
|
---|
| 1137 | AtomicMap[i] = -1;
|
---|
| 1138 | if (OtherMolecule == this) { // same molecule
|
---|
| 1139 | for (int i=AtomCount;i--;) // no need as -1 means already that there is trivial correspondence
|
---|
| 1140 | AtomicMap[i] = i;
|
---|
[e138de] | 1141 | Log() << Verbose(4) << "Map is trivial." << endl;
|
---|
[042f82] | 1142 | } else {
|
---|
[e138de] | 1143 | Log() << Verbose(4) << "Map is ";
|
---|
[9879f6] | 1144 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
| 1145 | if ((*iter)->father == NULL) {
|
---|
| 1146 | AtomicMap[(*iter)->nr] = -2;
|
---|
[042f82] | 1147 | } else {
|
---|
[9879f6] | 1148 | for (molecule::const_iterator runner = OtherMolecule->begin(); runner != OtherMolecule->end(); ++runner) {
|
---|
[042f82] | 1149 | //for (int i=0;i<AtomCount;i++) { // search atom
|
---|
| 1150 | //for (int j=0;j<OtherMolecule->AtomCount;j++) {
|
---|
[9879f6] | 1151 | //Log() << Verbose(4) << "Comparing father " << (*iter)->father << " with the other one " << (*runner)->father << "." << endl;
|
---|
| 1152 | if ((*iter)->father == (*runner))
|
---|
| 1153 | AtomicMap[(*iter)->nr] = (*runner)->nr;
|
---|
[042f82] | 1154 | }
|
---|
| 1155 | }
|
---|
[9879f6] | 1156 | Log() << Verbose(0) << AtomicMap[(*iter)->nr] << "\t";
|
---|
[042f82] | 1157 | }
|
---|
[e138de] | 1158 | Log() << Verbose(0) << endl;
|
---|
[042f82] | 1159 | }
|
---|
[e138de] | 1160 | Log() << Verbose(3) << "End of GetFatherAtomicMap." << endl;
|
---|
[042f82] | 1161 | return AtomicMap;
|
---|
[14de469] | 1162 | };
|
---|
| 1163 |
|
---|
[698b04] | 1164 | /** Stores the temperature evaluated from velocities in molecule::Trajectories.
|
---|
| 1165 | * We simply use the formula equivaleting temperature and kinetic energy:
|
---|
| 1166 | * \f$k_B T = \sum_i m_i v_i^2\f$
|
---|
[e138de] | 1167 | * \param *output output stream of temperature file
|
---|
[698b04] | 1168 | * \param startstep first MD step in molecule::Trajectories
|
---|
| 1169 | * \param endstep last plus one MD step in molecule::Trajectories
|
---|
| 1170 | * \return file written (true), failure on writing file (false)
|
---|
[69eb71] | 1171 | */
|
---|
[e138de] | 1172 | bool molecule::OutputTemperatureFromTrajectories(ofstream * const output, int startstep, int endstep)
|
---|
[698b04] | 1173 | {
|
---|
[042f82] | 1174 | double temperature;
|
---|
| 1175 | // test stream
|
---|
| 1176 | if (output == NULL)
|
---|
| 1177 | return false;
|
---|
| 1178 | else
|
---|
| 1179 | *output << "# Step Temperature [K] Temperature [a.u.]" << endl;
|
---|
| 1180 | for (int step=startstep;step < endstep; step++) { // loop over all time steps
|
---|
| 1181 | temperature = 0.;
|
---|
[4455f4] | 1182 | ActOnAllAtoms( &TrajectoryParticle::AddKineticToTemperature, &temperature, step);
|
---|
[042f82] | 1183 | *output << step << "\t" << temperature*AtomicEnergyToKelvin << "\t" << temperature << endl;
|
---|
| 1184 | }
|
---|
| 1185 | return true;
|
---|
[65de9b] | 1186 | };
|
---|
[4a7776a] | 1187 |
|
---|
[b453f9] | 1188 | void molecule::SetIndexedArrayForEachAtomTo ( atom **array, int ParticleInfo::*index) const
|
---|
[4a7776a] | 1189 | {
|
---|
[9879f6] | 1190 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
| 1191 | array[((*iter)->*index)] = (*iter);
|
---|
[4a7776a] | 1192 | }
|
---|
| 1193 | };
|
---|
[c68025] | 1194 |
|
---|
| 1195 | void molecule::flipActiveFlag(){
|
---|
| 1196 | ActiveFlag = !ActiveFlag;
|
---|
| 1197 | }
|
---|