| 1 | /** \file atom.cpp | 
|---|
| 2 | * | 
|---|
| 3 | * Function implementations for the class atom. | 
|---|
| 4 | * | 
|---|
| 5 | */ | 
|---|
| 6 |  | 
|---|
| 7 | #include "atom.hpp" | 
|---|
| 8 | #include "bond.hpp" | 
|---|
| 9 | #include "config.hpp" | 
|---|
| 10 | #include "element.hpp" | 
|---|
| 11 | #include "lists.hpp" | 
|---|
| 12 | #include "memoryallocator.hpp" | 
|---|
| 13 | #include "parser.hpp" | 
|---|
| 14 | #include "vector.hpp" | 
|---|
| 15 | #include "World.hpp" | 
|---|
| 16 |  | 
|---|
| 17 | /************************************* Functions for class atom *************************************/ | 
|---|
| 18 |  | 
|---|
| 19 |  | 
|---|
| 20 | /** Constructor of class atom. | 
|---|
| 21 | */ | 
|---|
| 22 | atom::atom() : | 
|---|
| 23 | previous(NULL), next(NULL), father(this), sort(&nr) | 
|---|
| 24 | { | 
|---|
| 25 | node = &x;  // TesselPoint::x can only be referenced from here | 
|---|
| 26 | }; | 
|---|
| 27 |  | 
|---|
| 28 | /** Constructor of class atom. | 
|---|
| 29 | */ | 
|---|
| 30 | atom::atom(atom *pointer) : | 
|---|
| 31 | ParticleInfo(pointer), | 
|---|
| 32 | previous(NULL), next(NULL), father(pointer), sort(&nr) | 
|---|
| 33 | { | 
|---|
| 34 | type = pointer->type;  // copy element of atom | 
|---|
| 35 | x = pointer->x; // copy coordination | 
|---|
| 36 | v = pointer->v; // copy velocity | 
|---|
| 37 | FixedIon = pointer->FixedIon; | 
|---|
| 38 | node = &x; | 
|---|
| 39 | }; | 
|---|
| 40 |  | 
|---|
| 41 | atom *atom::clone(){ | 
|---|
| 42 | atom *res = new atom(this); | 
|---|
| 43 | res->previous=0; | 
|---|
| 44 | res->next=0; | 
|---|
| 45 | res->father = this; | 
|---|
| 46 | res->sort = &res->nr; | 
|---|
| 47 | res->type = type; | 
|---|
| 48 | res->x = this->x; | 
|---|
| 49 | res->v = this->v; | 
|---|
| 50 | res->FixedIon = FixedIon; | 
|---|
| 51 | res->node = &x; | 
|---|
| 52 | World::getInstance().registerAtom(res); | 
|---|
| 53 | return res; | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 |  | 
|---|
| 57 | /** Destructor of class atom. | 
|---|
| 58 | */ | 
|---|
| 59 | atom::~atom() | 
|---|
| 60 | { | 
|---|
| 61 | }; | 
|---|
| 62 |  | 
|---|
| 63 |  | 
|---|
| 64 | /** Climbs up the father list until NULL, last is returned. | 
|---|
| 65 | * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen) | 
|---|
| 66 | */ | 
|---|
| 67 | atom *atom::GetTrueFather() | 
|---|
| 68 | { | 
|---|
| 69 | atom *walker = this; | 
|---|
| 70 | do { | 
|---|
| 71 | if (walker == walker->father) // top most father is the one that points on itself | 
|---|
| 72 | break; | 
|---|
| 73 | walker = walker->father; | 
|---|
| 74 | } while (walker != NULL); | 
|---|
| 75 | return walker; | 
|---|
| 76 | }; | 
|---|
| 77 |  | 
|---|
| 78 | /** Sets father to itself or its father in case of copying a molecule. | 
|---|
| 79 | */ | 
|---|
| 80 | void atom::CorrectFather() | 
|---|
| 81 | { | 
|---|
| 82 | if (father->father == father)   // same atom in copy's father points to itself | 
|---|
| 83 | father = this;  // set father to itself (copy of a whole molecule) | 
|---|
| 84 | else | 
|---|
| 85 | father = father->father;  // set father to original's father | 
|---|
| 86 |  | 
|---|
| 87 | }; | 
|---|
| 88 |  | 
|---|
| 89 | /** Check whether father is equal to given atom. | 
|---|
| 90 | * \param *ptr atom to compare father to | 
|---|
| 91 | * \param **res return value (only set if atom::father is equal to \a *ptr) | 
|---|
| 92 | */ | 
|---|
| 93 | void atom::EqualsFather ( const atom *ptr, const atom **res ) const | 
|---|
| 94 | { | 
|---|
| 95 | if ( ptr == father ) | 
|---|
| 96 | *res = this; | 
|---|
| 97 | }; | 
|---|
| 98 |  | 
|---|
| 99 | /** Checks whether atom is within the given box. | 
|---|
| 100 | * \param offset offset to box origin | 
|---|
| 101 | * \param *parallelepiped box matrix | 
|---|
| 102 | * \return true - is inside, false - is not | 
|---|
| 103 | */ | 
|---|
| 104 | bool atom::IsInParallelepiped(const Vector offset, const double *parallelepiped) const | 
|---|
| 105 | { | 
|---|
| 106 | return (node->IsInParallelepiped(offset, parallelepiped)); | 
|---|
| 107 | }; | 
|---|
| 108 |  | 
|---|
| 109 | /** Counts the number of bonds weighted by bond::BondDegree. | 
|---|
| 110 | * \param bonds times bond::BondDegree | 
|---|
| 111 | */ | 
|---|
| 112 | int BondedParticle::CountBonds() const | 
|---|
| 113 | { | 
|---|
| 114 | int NoBonds = 0; | 
|---|
| 115 | for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) | 
|---|
| 116 | NoBonds += (*Runner)->BondDegree; | 
|---|
| 117 | return NoBonds; | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | /** Output of a single atom with given numbering. | 
|---|
| 121 | * \param ElementNo cardinal number of the element | 
|---|
| 122 | * \param AtomNo cardinal number among these atoms of the same element | 
|---|
| 123 | * \param *out stream to output to | 
|---|
| 124 | * \param *comment commentary after '#' sign | 
|---|
| 125 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 126 | */ | 
|---|
| 127 | bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const | 
|---|
| 128 | { | 
|---|
| 129 | if (out != NULL) { | 
|---|
| 130 | *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| 131 | *out << x[0] << "\t" << x[1] << "\t" << x[2]; | 
|---|
| 132 | *out << "\t" << FixedIon; | 
|---|
| 133 | if (v.Norm() > MYEPSILON) | 
|---|
| 134 | *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t"; | 
|---|
| 135 | if (comment != NULL) | 
|---|
| 136 | *out << " # " << comment << endl; | 
|---|
| 137 | else | 
|---|
| 138 | *out << " # molecule nr " << nr << endl; | 
|---|
| 139 | return true; | 
|---|
| 140 | } else | 
|---|
| 141 | return false; | 
|---|
| 142 | }; | 
|---|
| 143 |  | 
|---|
| 144 | /** Output of a single atom with numbering from array according to atom::type. | 
|---|
| 145 | * \param *ElementNo cardinal number of the element | 
|---|
| 146 | * \param *AtomNo cardinal number among these atoms of the same element | 
|---|
| 147 | * \param *out stream to output to | 
|---|
| 148 | * \param *comment commentary after '#' sign | 
|---|
| 149 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 150 | */ | 
|---|
| 151 | bool atom::OutputArrayIndexed(ofstream * const out, const int *ElementNo, int *AtomNo, const char *comment) const | 
|---|
| 152 | { | 
|---|
| 153 | AtomNo[type->Z]++;  // increment number | 
|---|
| 154 | if (out != NULL) { | 
|---|
| 155 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| 156 | *out << x[0] << "\t" << x[1] << "\t" << x[2]; | 
|---|
| 157 | *out << "\t" << FixedIon; | 
|---|
| 158 | if (v.Norm() > MYEPSILON) | 
|---|
| 159 | *out << "\t" << scientific << setprecision(6) << v[0] << "\t" << v[1] << "\t" << v[2] << "\t"; | 
|---|
| 160 | if (comment != NULL) | 
|---|
| 161 | *out << " # " << comment << endl; | 
|---|
| 162 | else | 
|---|
| 163 | *out << " # molecule nr " << nr << endl; | 
|---|
| 164 | return true; | 
|---|
| 165 | } else | 
|---|
| 166 | return false; | 
|---|
| 167 | }; | 
|---|
| 168 |  | 
|---|
| 169 | /** Output of a single atom as one lin in xyz file. | 
|---|
| 170 | * \param *out stream to output to | 
|---|
| 171 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 172 | */ | 
|---|
| 173 | bool atom::OutputXYZLine(ofstream *out) const | 
|---|
| 174 | { | 
|---|
| 175 | if (out != NULL) { | 
|---|
| 176 | *out << type->symbol << "\t" << x[0] << "\t" << x[1] << "\t" << x[2] << "\t" << endl; | 
|---|
| 177 | return true; | 
|---|
| 178 | } else | 
|---|
| 179 | return false; | 
|---|
| 180 | }; | 
|---|
| 181 |  | 
|---|
| 182 | /** Output of a single atom as one lin in xyz file. | 
|---|
| 183 | * \param *out stream to output to | 
|---|
| 184 | * \param *ElementNo array with ion type number in the config file this atom's element shall have | 
|---|
| 185 | * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically | 
|---|
| 186 | * \param step Trajectory time step to output | 
|---|
| 187 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 188 | */ | 
|---|
| 189 | bool atom::OutputTrajectory(ofstream * const out, const int *ElementNo, int *AtomNo, const int step) const | 
|---|
| 190 | { | 
|---|
| 191 | AtomNo[type->Z]++; | 
|---|
| 192 | if (out != NULL) { | 
|---|
| 193 | *out << "Ion_Type" << ElementNo[type->Z] << "_" << AtomNo[type->Z] << "\t"  << fixed << setprecision(9) << showpoint; | 
|---|
| 194 | *out << Trajectory.R.at(step)[0] << "\t" << Trajectory.R.at(step)[1] << "\t" << Trajectory.R.at(step)[2]; | 
|---|
| 195 | *out << "\t" << FixedIon; | 
|---|
| 196 | if (Trajectory.U.at(step).Norm() > MYEPSILON) | 
|---|
| 197 | *out << "\t" << scientific << setprecision(6) << Trajectory.U.at(step)[0] << "\t" << Trajectory.U.at(step)[1] << "\t" << Trajectory.U.at(step)[2] << "\t"; | 
|---|
| 198 | if (Trajectory.F.at(step).Norm() > MYEPSILON) | 
|---|
| 199 | *out << "\t" << scientific << setprecision(6) << Trajectory.F.at(step)[0] << "\t" << Trajectory.F.at(step)[1] << "\t" << Trajectory.F.at(step)[2] << "\t"; | 
|---|
| 200 | *out << "\t# Number in molecule " << nr << endl; | 
|---|
| 201 | return true; | 
|---|
| 202 | } else | 
|---|
| 203 | return false; | 
|---|
| 204 | }; | 
|---|
| 205 |  | 
|---|
| 206 | /** Output of a single atom as one lin in xyz file. | 
|---|
| 207 | * \param *out stream to output to | 
|---|
| 208 | * \param step Trajectory time step to output | 
|---|
| 209 | * \return true - \a *out present, false - \a *out is NULL | 
|---|
| 210 | */ | 
|---|
| 211 | bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const | 
|---|
| 212 | { | 
|---|
| 213 | if (out != NULL) { | 
|---|
| 214 | *out << type->symbol << "\t"; | 
|---|
| 215 | *out << Trajectory.R.at(step)[0] << "\t"; | 
|---|
| 216 | *out << Trajectory.R.at(step)[1] << "\t"; | 
|---|
| 217 | *out << Trajectory.R.at(step)[2] << endl; | 
|---|
| 218 | return true; | 
|---|
| 219 | } else | 
|---|
| 220 | return false; | 
|---|
| 221 | }; | 
|---|
| 222 |  | 
|---|
| 223 | /** Outputs the MPQC configuration line for this atom. | 
|---|
| 224 | * \param *out output stream | 
|---|
| 225 | * \param *center center of molecule subtracted from position | 
|---|
| 226 | * \param *AtomNo pointer to atom counter that is increased by one | 
|---|
| 227 | */ | 
|---|
| 228 | void atom::OutputMPQCLine(ofstream * const out, const Vector *center, int *AtomNo = NULL) const | 
|---|
| 229 | { | 
|---|
| 230 | *out << "\t\t" << type->symbol << " [ " << x[0]-center->at(0) << "\t" << x[1]-center->at(1) << "\t" << x[2]-center->at(2) << " ]" << endl; | 
|---|
| 231 | if (AtomNo != NULL) | 
|---|
| 232 | *AtomNo++; | 
|---|
| 233 | }; | 
|---|
| 234 |  | 
|---|
| 235 | /** Compares the indices of \a this atom with a given \a ptr. | 
|---|
| 236 | * \param ptr atom to compare index against | 
|---|
| 237 | * \return true - this one's is smaller, false - not | 
|---|
| 238 | */ | 
|---|
| 239 | bool atom::Compare(const atom &ptr) const | 
|---|
| 240 | { | 
|---|
| 241 | if (nr < ptr.nr) | 
|---|
| 242 | return true; | 
|---|
| 243 | else | 
|---|
| 244 | return false; | 
|---|
| 245 | }; | 
|---|
| 246 |  | 
|---|
| 247 | /** Returns squared distance to a given vector. | 
|---|
| 248 | * \param origin vector to calculate distance to | 
|---|
| 249 | * \return distance squared | 
|---|
| 250 | */ | 
|---|
| 251 | double atom::DistanceSquaredToVector(const Vector &origin) const | 
|---|
| 252 | { | 
|---|
| 253 | return origin.DistanceSquared(x); | 
|---|
| 254 | }; | 
|---|
| 255 |  | 
|---|
| 256 | /** Returns distance to a given vector. | 
|---|
| 257 | * \param origin vector to calculate distance to | 
|---|
| 258 | * \return distance | 
|---|
| 259 | */ | 
|---|
| 260 | double atom::DistanceToVector(const Vector &origin) const | 
|---|
| 261 | { | 
|---|
| 262 | return origin.distance(x); | 
|---|
| 263 | }; | 
|---|
| 264 |  | 
|---|
| 265 | /** Initialises the component number array. | 
|---|
| 266 | * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1) | 
|---|
| 267 | */ | 
|---|
| 268 | void atom::InitComponentNr() | 
|---|
| 269 | { | 
|---|
| 270 | if (ComponentNr != NULL) | 
|---|
| 271 | delete[](ComponentNr); | 
|---|
| 272 | ComponentNr = new int[ListOfBonds.size()+1]; | 
|---|
| 273 | for (int i=ListOfBonds.size()+1;i--;) | 
|---|
| 274 | ComponentNr[i] = -1; | 
|---|
| 275 | }; | 
|---|
| 276 |  | 
|---|
| 277 |  | 
|---|
| 278 | bool operator < (atom &a, atom &b) | 
|---|
| 279 | { | 
|---|
| 280 | return a.Compare(b); | 
|---|
| 281 | }; | 
|---|
| 282 |  | 
|---|
| 283 | World *atom::getWorld(){ | 
|---|
| 284 | return world; | 
|---|
| 285 | } | 
|---|
| 286 |  | 
|---|
| 287 | void atom::setWorld(World* _world){ | 
|---|
| 288 | world = _world; | 
|---|
| 289 | } | 
|---|
| 290 |  | 
|---|
| 291 | bool atom::changeId(atomId_t newId){ | 
|---|
| 292 | // first we move ourselves in the world | 
|---|
| 293 | // the world lets us know if that succeeded | 
|---|
| 294 | if(world->changeAtomId(id,newId,this)){ | 
|---|
| 295 | id = newId; | 
|---|
| 296 | return true; | 
|---|
| 297 | } | 
|---|
| 298 | else{ | 
|---|
| 299 | return false; | 
|---|
| 300 | } | 
|---|
| 301 | } | 
|---|
| 302 |  | 
|---|
| 303 | void atom::setId(atomId_t _id) { | 
|---|
| 304 | id=_id; | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | atomId_t atom::getId() { | 
|---|
| 308 | return id; | 
|---|
| 309 | } | 
|---|
| 310 |  | 
|---|
| 311 | atom* NewAtom(atomId_t _id){ | 
|---|
| 312 | atom * res =new atom(); | 
|---|
| 313 | res->setId(_id); | 
|---|
| 314 | return res; | 
|---|
| 315 | } | 
|---|
| 316 |  | 
|---|
| 317 | void DeleteAtom(atom* atom){ | 
|---|
| 318 | delete atom; | 
|---|
| 319 | } | 
|---|