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