1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /** \file molecules.cpp
|
---|
9 | *
|
---|
10 | * Functions for the class molecule.
|
---|
11 | *
|
---|
12 | */
|
---|
13 |
|
---|
14 | // include config.h
|
---|
15 | #ifdef HAVE_CONFIG_H
|
---|
16 | #include <config.h>
|
---|
17 | #endif
|
---|
18 |
|
---|
19 | #include "CodePatterns/MemDebug.hpp"
|
---|
20 |
|
---|
21 | #include <cstring>
|
---|
22 | #include <boost/bind.hpp>
|
---|
23 | #include <boost/foreach.hpp>
|
---|
24 |
|
---|
25 | #include <gsl/gsl_inline.h>
|
---|
26 | #include <gsl/gsl_heapsort.h>
|
---|
27 |
|
---|
28 | #include "molecule.hpp"
|
---|
29 |
|
---|
30 | #include "Atom/atom.hpp"
|
---|
31 | #include "Bond/bond.hpp"
|
---|
32 | #include "Box.hpp"
|
---|
33 | #include "CodePatterns/enumeration.hpp"
|
---|
34 | #include "CodePatterns/Log.hpp"
|
---|
35 | #include "config.hpp"
|
---|
36 | #include "Descriptors/AtomIdDescriptor.hpp"
|
---|
37 | #include "Element/element.hpp"
|
---|
38 | #include "Element/periodentafel.hpp"
|
---|
39 | #include "Graph/BondGraph.hpp"
|
---|
40 | #include "LinearAlgebra/Exceptions.hpp"
|
---|
41 | #include "LinearAlgebra/leastsquaremin.hpp"
|
---|
42 | #include "LinearAlgebra/Plane.hpp"
|
---|
43 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
44 | #include "LinearAlgebra/Vector.hpp"
|
---|
45 | #include "LinkedCell/linkedcell.hpp"
|
---|
46 | #include "IdPool_impl.hpp"
|
---|
47 | #include "Tesselation/tesselation.hpp"
|
---|
48 | #include "World.hpp"
|
---|
49 | #include "WorldTime.hpp"
|
---|
50 |
|
---|
51 |
|
---|
52 | /************************************* Functions for class molecule *********************************/
|
---|
53 |
|
---|
54 | /** Constructor of class molecule.
|
---|
55 | * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
|
---|
56 | */
|
---|
57 | molecule::molecule(const periodentafel * const teil) :
|
---|
58 | Observable("molecule"),
|
---|
59 | elemente(teil),
|
---|
60 | MDSteps(0),
|
---|
61 | NoNonHydrogen(0),
|
---|
62 | NoNonBonds(0),
|
---|
63 | NoCyclicBonds(0),
|
---|
64 | ActiveFlag(false),
|
---|
65 | IndexNr(-1),
|
---|
66 | AtomCount(this,boost::bind(&molecule::doCountAtoms,this),"AtomCount"),
|
---|
67 | BondCount(this,boost::bind(&molecule::doCountBonds,this),"BondCount"),
|
---|
68 | atomIdPool(1, 20, 100),
|
---|
69 | last_atom(0)
|
---|
70 | {
|
---|
71 |
|
---|
72 | strcpy(name,World::getInstance().getDefaultName().c_str());
|
---|
73 | };
|
---|
74 |
|
---|
75 | molecule *NewMolecule(){
|
---|
76 | return new molecule(World::getInstance().getPeriode());
|
---|
77 | }
|
---|
78 |
|
---|
79 | /** Destructor of class molecule.
|
---|
80 | * Initialises molecule list with correctly referenced start and end, and sets molecule::last_atom to zero.
|
---|
81 | */
|
---|
82 | molecule::~molecule()
|
---|
83 | {
|
---|
84 | CleanupMolecule();
|
---|
85 | };
|
---|
86 |
|
---|
87 |
|
---|
88 | void DeleteMolecule(molecule *mol){
|
---|
89 | delete mol;
|
---|
90 | }
|
---|
91 |
|
---|
92 | // getter and setter
|
---|
93 | const std::string molecule::getName() const{
|
---|
94 | return std::string(name);
|
---|
95 | }
|
---|
96 |
|
---|
97 | int molecule::getAtomCount() const{
|
---|
98 | return *AtomCount;
|
---|
99 | }
|
---|
100 |
|
---|
101 | int molecule::getBondCount() const{
|
---|
102 | return *BondCount;
|
---|
103 | }
|
---|
104 |
|
---|
105 | void molecule::setName(const std::string _name){
|
---|
106 | OBSERVE;
|
---|
107 | cout << "Set name of molecule " << getId() << " to " << _name << endl;
|
---|
108 | strncpy(name,_name.c_str(),MAXSTRINGSIZE);
|
---|
109 | }
|
---|
110 |
|
---|
111 | bool molecule::changeAtomNr(int oldNr, int newNr, atom* target){
|
---|
112 | OBSERVE;
|
---|
113 | if(atomIdPool.reserveId(newNr)){
|
---|
114 | if (oldNr != -1) // -1 is reserved and indicates no number
|
---|
115 | atomIdPool.releaseId(oldNr);
|
---|
116 | ASSERT (target,
|
---|
117 | "molecule::changeAtomNr() - given target is NULL, cannot set Nr or name.");
|
---|
118 | target->setNr(newNr);
|
---|
119 | setAtomName(target);
|
---|
120 | return true;
|
---|
121 | } else{
|
---|
122 | return false;
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | bool molecule::changeId(moleculeId_t newId){
|
---|
127 | // first we move ourselves in the world
|
---|
128 | // the world lets us know if that succeeded
|
---|
129 | if(World::getInstance().changeMoleculeId(id,newId,this)){
|
---|
130 | id = newId;
|
---|
131 | return true;
|
---|
132 | }
|
---|
133 | else{
|
---|
134 | return false;
|
---|
135 | }
|
---|
136 | }
|
---|
137 |
|
---|
138 |
|
---|
139 | moleculeId_t molecule::getId() const {
|
---|
140 | return id;
|
---|
141 | }
|
---|
142 |
|
---|
143 | void molecule::setId(moleculeId_t _id){
|
---|
144 | id =_id;
|
---|
145 | }
|
---|
146 |
|
---|
147 | const Formula &molecule::getFormula() const {
|
---|
148 | return formula;
|
---|
149 | }
|
---|
150 |
|
---|
151 | unsigned int molecule::getElementCount() const{
|
---|
152 | return formula.getElementCount();
|
---|
153 | }
|
---|
154 |
|
---|
155 | bool molecule::hasElement(const element *element) const{
|
---|
156 | return formula.hasElement(element);
|
---|
157 | }
|
---|
158 |
|
---|
159 | bool molecule::hasElement(atomicNumber_t Z) const{
|
---|
160 | return formula.hasElement(Z);
|
---|
161 | }
|
---|
162 |
|
---|
163 | bool molecule::hasElement(const string &shorthand) const{
|
---|
164 | return formula.hasElement(shorthand);
|
---|
165 | }
|
---|
166 |
|
---|
167 | /************************** Access to the List of Atoms ****************/
|
---|
168 |
|
---|
169 |
|
---|
170 | molecule::iterator molecule::begin(){
|
---|
171 | return iterator(atomIds.begin(), FromIdToAtom());
|
---|
172 | }
|
---|
173 |
|
---|
174 | molecule::const_iterator molecule::begin() const{
|
---|
175 | return const_iterator(atomIds.begin(), FromIdToAtom());
|
---|
176 | }
|
---|
177 |
|
---|
178 | molecule::iterator molecule::end(){
|
---|
179 | return iterator(atomIds.end(), FromIdToAtom());
|
---|
180 | }
|
---|
181 |
|
---|
182 | molecule::const_iterator molecule::end() const{
|
---|
183 | return const_iterator(atomIds.end(), FromIdToAtom());
|
---|
184 | }
|
---|
185 |
|
---|
186 | bool molecule::empty() const
|
---|
187 | {
|
---|
188 | return (atomIds.empty());
|
---|
189 | }
|
---|
190 |
|
---|
191 | size_t molecule::size() const
|
---|
192 | {
|
---|
193 | size_t counter = 0;
|
---|
194 | for (const_iterator iter = begin(); iter != end (); ++iter)
|
---|
195 | counter++;
|
---|
196 | return counter;
|
---|
197 | }
|
---|
198 |
|
---|
199 | molecule::const_iterator molecule::erase( const_iterator loc )
|
---|
200 | {
|
---|
201 | OBSERVE;
|
---|
202 | const_iterator iter = loc;
|
---|
203 | ++iter;
|
---|
204 | atom * const _atom = const_cast<atom *>(*loc);
|
---|
205 | atomIds.erase( _atom->getId() );
|
---|
206 | formula-=_atom->getType();
|
---|
207 | _atom->removeFromMolecule();
|
---|
208 | return iter;
|
---|
209 | }
|
---|
210 |
|
---|
211 | molecule::const_iterator molecule::erase( atom * key )
|
---|
212 | {
|
---|
213 | OBSERVE;
|
---|
214 | const_iterator iter = find(key);
|
---|
215 | if (iter != end()){
|
---|
216 | ++iter;
|
---|
217 | atomIds.erase( key->getId() );
|
---|
218 | atomIdPool.releaseId(key->getNr());
|
---|
219 | key->setNr(-1);
|
---|
220 | formula-=key->getType();
|
---|
221 | key->removeFromMolecule();
|
---|
222 | }
|
---|
223 | return iter;
|
---|
224 | }
|
---|
225 |
|
---|
226 | molecule::const_iterator molecule::find ( atom * key ) const
|
---|
227 | {
|
---|
228 | return const_iterator(atomIds.find(key->getId()), FromIdToAtom());
|
---|
229 | }
|
---|
230 |
|
---|
231 | pair<molecule::iterator,bool> molecule::insert ( atom * const key )
|
---|
232 | {
|
---|
233 | OBSERVE;
|
---|
234 | pair<atomIdSet::iterator,bool> res = atomIds.insert(key->getId());
|
---|
235 | if (res.second) { // push atom if went well
|
---|
236 | key->setNr(atomIdPool.getNextId());
|
---|
237 | setAtomName(key);
|
---|
238 | formula+=key->getType();
|
---|
239 | return pair<iterator,bool>(iterator(res.first, FromIdToAtom()),res.second);
|
---|
240 | } else {
|
---|
241 | return pair<iterator,bool>(end(),res.second);
|
---|
242 | }
|
---|
243 | }
|
---|
244 |
|
---|
245 | void molecule::setAtomName(atom *_atom) const
|
---|
246 | {
|
---|
247 | std::stringstream sstr;
|
---|
248 | sstr << _atom->getType()->getSymbol() << _atom->getNr();
|
---|
249 | _atom->setName(sstr.str());
|
---|
250 | }
|
---|
251 |
|
---|
252 |
|
---|
253 | bool molecule::containsAtom(atom* key){
|
---|
254 | return (find(key) != end());
|
---|
255 | }
|
---|
256 |
|
---|
257 | World::AtomComposite molecule::getAtomSet() const
|
---|
258 | {
|
---|
259 | World::AtomComposite vector_of_atoms;
|
---|
260 | // std::copy(MyIter(atomIds.begin(), FromIdToAtom()),
|
---|
261 | // MyIter(atomIds.end(), FromIdToAtom()),
|
---|
262 | // vector_of_atoms.begin());
|
---|
263 | // for (MyIter iter = MyIter(atomIds.begin(), FromIdToAtom());
|
---|
264 | // iter != MyIter(atomIds.end(), FromIdToAtom());
|
---|
265 | // ++iter)
|
---|
266 | for (molecule::iterator iter = begin(); iter != end(); ++iter)
|
---|
267 | vector_of_atoms.push_back(*iter);
|
---|
268 | return vector_of_atoms;
|
---|
269 | }
|
---|
270 |
|
---|
271 | /** Adds given atom \a *pointer from molecule list.
|
---|
272 | * Increases molecule::last_atom and gives last number to added atom and names it according to its element::abbrev and molecule::AtomCount
|
---|
273 | * \param *pointer allocated and set atom
|
---|
274 | * \return true - succeeded, false - atom not found in list
|
---|
275 | */
|
---|
276 | bool molecule::AddAtom(atom *pointer)
|
---|
277 | {
|
---|
278 | OBSERVE;
|
---|
279 | if (pointer != NULL) {
|
---|
280 | if (pointer->getType()->getAtomicNumber() != 1)
|
---|
281 | NoNonHydrogen++;
|
---|
282 | insert(pointer);
|
---|
283 | pointer->setMolecule(this);
|
---|
284 | }
|
---|
285 | return true;
|
---|
286 | };
|
---|
287 |
|
---|
288 | /** Adds a copy of the given atom \a *pointer from molecule list.
|
---|
289 | * Increases molecule::last_atom and gives last number to added atom.
|
---|
290 | * \param *pointer allocated and set atom
|
---|
291 | * \return pointer to the newly added atom
|
---|
292 | */
|
---|
293 | atom * molecule::AddCopyAtom(atom *pointer)
|
---|
294 | {
|
---|
295 | atom *retval = NULL;
|
---|
296 | OBSERVE;
|
---|
297 | if (pointer != NULL) {
|
---|
298 | atom *walker = pointer->clone();
|
---|
299 | walker->setName(pointer->getName());
|
---|
300 | walker->setNr(last_atom++); // increase number within molecule
|
---|
301 | insert(walker);
|
---|
302 | if ((pointer->getType() != NULL) && (pointer->getType()->getAtomicNumber() != 1))
|
---|
303 | NoNonHydrogen++;
|
---|
304 | walker->setMolecule(this);
|
---|
305 | retval=walker;
|
---|
306 | }
|
---|
307 | return retval;
|
---|
308 | };
|
---|
309 |
|
---|
310 | /** Adds a Hydrogen atom in replacement for the given atom \a *partner in bond with a *origin.
|
---|
311 | * Here, we have to distinguish between single, double or triple bonds as stated by \a BondDegree, that each demand
|
---|
312 | * a different scheme when adding \a *replacement atom for the given one.
|
---|
313 | * -# Single Bond: Simply add new atom with bond distance rescaled to typical hydrogen one
|
---|
314 | * -# Double Bond: Here, we need the **BondList of the \a *origin atom, by scanning for the other bonds instead of
|
---|
315 | * *Bond, we use the through these connected atoms to determine the plane they lie in, vector::MakeNormalvector().
|
---|
316 | * The orthonormal vector to this plane along with the vector in *Bond direction determines the plane the two
|
---|
317 | * replacing hydrogens shall lie in. Now, all remains to do is take the usual hydrogen double bond angle for the
|
---|
318 | * element of *origin and form the sin/cos admixture of both plane vectors for the new coordinates of the two
|
---|
319 | * hydrogens forming this angle with *origin.
|
---|
320 | * -# 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
|
---|
321 | * triangle formed by the to be added hydrogens are not equal to the typical bond distance \f$l\f$ but have to be
|
---|
322 | * determined from the typical angle \f$\alpha\f$ for a hydrogen triple connected to the element of *origin):
|
---|
323 | * We have the height \f$d\f$ as the vector in *Bond direction (from triangle C1-H1-H2).
|
---|
324 | * \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 )}}
|
---|
325 | * \f]
|
---|
326 | * vector::GetNormalvector() creates one orthonormal vector from this *Bond vector and vector::MakeNormalvector creates
|
---|
327 | * the third one from the former two vectors. The latter ones form the plane of the base triangle mentioned above.
|
---|
328 | * The lengths for these are \f$f\f$ and \f$g\f$ (from triangle H1-H2-(center of H1-H2-H3)) with knowledge that
|
---|
329 | * the median lines in an isosceles triangle meet in the center point with a ratio 2:1.
|
---|
330 | * \f[ f = \frac{b}{\sqrt{3}} \qquad g = \frac{b}{2}
|
---|
331 | * \f]
|
---|
332 | * as the coordination of all three atoms in the coordinate system of these three vectors:
|
---|
333 | * \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$.
|
---|
334 | *
|
---|
335 | * \param *out output stream for debugging
|
---|
336 | * \param *Bond pointer to bond between \a *origin and \a *replacement
|
---|
337 | * \param *TopOrigin son of \a *origin of upper level molecule (the atom added to this molecule as a copy of \a *origin)
|
---|
338 | * \param *origin pointer to atom which acts as the origin for scaling the added hydrogen to correct bond length
|
---|
339 | * \param *replacement pointer to the atom which shall be copied as a hydrogen atom in this molecule
|
---|
340 | * \param isAngstroem whether the coordination of the given atoms is in AtomicLength (false) or Angstrom(true)
|
---|
341 | * \return number of atoms added, if < bond::BondDegree then something went wrong
|
---|
342 | * \todo double and triple bonds splitting (always use the tetraeder angle!)
|
---|
343 | */
|
---|
344 | bool molecule::AddHydrogenReplacementAtom(bond *TopBond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem)
|
---|
345 | {
|
---|
346 | // Info info(__func__);
|
---|
347 | bool AllWentWell = true; // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
|
---|
348 | OBSERVE;
|
---|
349 | double bondlength; // bond length of the bond to be replaced/cut
|
---|
350 | double bondangle; // bond angle of the bond to be replaced/cut
|
---|
351 | double BondRescale; // rescale value for the hydrogen bond length
|
---|
352 | bond *FirstBond = NULL, *SecondBond = NULL; // Other bonds in double bond case to determine "other" plane
|
---|
353 | atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
|
---|
354 | double b,l,d,f,g, alpha, factors[NDIM]; // hold temporary values in triple bond case for coordination determination
|
---|
355 | Vector Orthovector1, Orthovector2; // temporary vectors in coordination construction
|
---|
356 | Vector InBondvector; // vector in direction of *Bond
|
---|
357 | const RealSpaceMatrix &matrix = World::getInstance().getDomain().getM();
|
---|
358 | bond *Binder = NULL;
|
---|
359 |
|
---|
360 | // create vector in direction of bond
|
---|
361 | InBondvector = TopReplacement->getPosition() - TopOrigin->getPosition();
|
---|
362 | bondlength = InBondvector.Norm();
|
---|
363 |
|
---|
364 | // is greater than typical bond distance? Then we have to correct periodically
|
---|
365 | // the problem is not the H being out of the box, but InBondvector have the wrong direction
|
---|
366 | // due to TopReplacement or Origin being on the wrong side!
|
---|
367 | const BondGraph * const BG = World::getInstance().getBondGraph();
|
---|
368 | const range<double> MinMaxBondDistance(
|
---|
369 | BG->getMinMaxDistance(TopOrigin,TopReplacement));
|
---|
370 | if (!MinMaxBondDistance.isInRange(bondlength)) {
|
---|
371 | // LOG(4, "InBondvector is: " << InBondvector << ".");
|
---|
372 | Orthovector1.Zero();
|
---|
373 | for (int i=NDIM;i--;) {
|
---|
374 | l = TopReplacement->at(i) - TopOrigin->at(i);
|
---|
375 | if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
|
---|
376 | Orthovector1[i] = (l < 0) ? -1. : +1.;
|
---|
377 | } // (signs are correct, was tested!)
|
---|
378 | }
|
---|
379 | Orthovector1 *= matrix;
|
---|
380 | InBondvector -= Orthovector1; // subtract just the additional translation
|
---|
381 | bondlength = InBondvector.Norm();
|
---|
382 | // LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
|
---|
383 | } // periodic correction finished
|
---|
384 |
|
---|
385 | InBondvector.Normalize();
|
---|
386 | // get typical bond length and store as scale factor for later
|
---|
387 | ASSERT(TopOrigin->getType() != NULL, "AddHydrogenReplacementAtom: element of TopOrigin is not given.");
|
---|
388 | BondRescale = TopOrigin->getType()->getHBondDistance(TopBond->BondDegree-1);
|
---|
389 | if (BondRescale == -1) {
|
---|
390 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->BondDegree << "!");
|
---|
391 | return false;
|
---|
392 | BondRescale = bondlength;
|
---|
393 | } else {
|
---|
394 | if (!IsAngstroem)
|
---|
395 | BondRescale /= (1.*AtomicLengthToAngstroem);
|
---|
396 | }
|
---|
397 |
|
---|
398 | // discern single, double and triple bonds
|
---|
399 | switch(TopBond->BondDegree) {
|
---|
400 | case 1:
|
---|
401 | FirstOtherAtom = World::getInstance().createAtom(); // new atom
|
---|
402 | FirstOtherAtom->setType(1); // element is Hydrogen
|
---|
403 | FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
404 | FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
405 | if (TopReplacement->getType()->getAtomicNumber() == 1) { // neither rescale nor replace if it's already hydrogen
|
---|
406 | FirstOtherAtom->father = TopReplacement;
|
---|
407 | BondRescale = bondlength;
|
---|
408 | } else {
|
---|
409 | FirstOtherAtom->father = NULL; // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
|
---|
410 | }
|
---|
411 | InBondvector *= BondRescale; // rescale the distance vector to Hydrogen bond length
|
---|
412 | FirstOtherAtom->setPosition(TopOrigin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
|
---|
413 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
414 | // LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
|
---|
415 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
416 | Binder->Cyclic = false;
|
---|
417 | Binder->Type = GraphEdge::TreeEdge;
|
---|
418 | break;
|
---|
419 | case 2:
|
---|
420 | {
|
---|
421 | // determine two other bonds (warning if there are more than two other) plus valence sanity check
|
---|
422 | const BondList& ListOfBonds = TopOrigin->getListOfBonds();
|
---|
423 | for (BondList::const_iterator Runner = ListOfBonds.begin();
|
---|
424 | Runner != ListOfBonds.end();
|
---|
425 | ++Runner) {
|
---|
426 | if ((*Runner) != TopBond) {
|
---|
427 | if (FirstBond == NULL) {
|
---|
428 | FirstBond = (*Runner);
|
---|
429 | FirstOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
|
---|
430 | } else if (SecondBond == NULL) {
|
---|
431 | SecondBond = (*Runner);
|
---|
432 | SecondOtherAtom = (*Runner)->GetOtherAtom(TopOrigin);
|
---|
433 | } else {
|
---|
434 | ELOG(2, "Detected more than four bonds for atom " << TopOrigin->getName());
|
---|
435 | }
|
---|
436 | }
|
---|
437 | }
|
---|
438 | }
|
---|
439 | 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)
|
---|
440 | SecondBond = TopBond;
|
---|
441 | SecondOtherAtom = TopReplacement;
|
---|
442 | }
|
---|
443 | if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
|
---|
444 | // LOG(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.");
|
---|
445 |
|
---|
446 | // determine the plane of these two with the *origin
|
---|
447 | try {
|
---|
448 | Orthovector1 = Plane(TopOrigin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
|
---|
449 | }
|
---|
450 | catch(LinearDependenceException &excp){
|
---|
451 | LOG(0, boost::diagnostic_information(excp));
|
---|
452 | // TODO: figure out what to do with the Orthovector in this case
|
---|
453 | AllWentWell = false;
|
---|
454 | }
|
---|
455 | } else {
|
---|
456 | Orthovector1.GetOneNormalVector(InBondvector);
|
---|
457 | }
|
---|
458 | //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
|
---|
459 | // orthogonal vector and bond vector between origin and replacement form the new plane
|
---|
460 | Orthovector1.MakeNormalTo(InBondvector);
|
---|
461 | Orthovector1.Normalize();
|
---|
462 | //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
|
---|
463 |
|
---|
464 | // create the two Hydrogens ...
|
---|
465 | FirstOtherAtom = World::getInstance().createAtom();
|
---|
466 | SecondOtherAtom = World::getInstance().createAtom();
|
---|
467 | FirstOtherAtom->setType(1);
|
---|
468 | SecondOtherAtom->setType(1);
|
---|
469 | FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
470 | FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
471 | SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
472 | SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
473 | FirstOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
474 | SecondOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
475 | bondangle = TopOrigin->getType()->getHBondAngle(1);
|
---|
476 | if (bondangle == -1) {
|
---|
477 | ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << TopOrigin->getName() << "<->" << TopReplacement->getName() << ") of degree " << TopBond->BondDegree << "!");
|
---|
478 | return false;
|
---|
479 | bondangle = 0;
|
---|
480 | }
|
---|
481 | bondangle *= M_PI/180./2.;
|
---|
482 | // LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << ".");
|
---|
483 | // LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle));
|
---|
484 | FirstOtherAtom->Zero();
|
---|
485 | SecondOtherAtom->Zero();
|
---|
486 | for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
|
---|
487 | FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
|
---|
488 | SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
|
---|
489 | }
|
---|
490 | FirstOtherAtom->Scale(BondRescale); // rescale by correct BondDistance
|
---|
491 | SecondOtherAtom->Scale(BondRescale);
|
---|
492 | //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
|
---|
493 | *FirstOtherAtom += TopOrigin->getPosition();
|
---|
494 | *SecondOtherAtom += TopOrigin->getPosition();
|
---|
495 | // ... and add to molecule
|
---|
496 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
497 | AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
|
---|
498 | // LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
|
---|
499 | // LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
|
---|
500 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
501 | Binder->Cyclic = false;
|
---|
502 | Binder->Type = GraphEdge::TreeEdge;
|
---|
503 | Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
|
---|
504 | Binder->Cyclic = false;
|
---|
505 | Binder->Type = GraphEdge::TreeEdge;
|
---|
506 | break;
|
---|
507 | case 3:
|
---|
508 | // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
|
---|
509 | FirstOtherAtom = World::getInstance().createAtom();
|
---|
510 | SecondOtherAtom = World::getInstance().createAtom();
|
---|
511 | ThirdOtherAtom = World::getInstance().createAtom();
|
---|
512 | FirstOtherAtom->setType(1);
|
---|
513 | SecondOtherAtom->setType(1);
|
---|
514 | ThirdOtherAtom->setType(1);
|
---|
515 | FirstOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
516 | FirstOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
517 | SecondOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
518 | SecondOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
519 | ThirdOtherAtom->setAtomicVelocity(TopReplacement->getAtomicVelocity()); // copy velocity
|
---|
520 | ThirdOtherAtom->setFixedIon(TopReplacement->getFixedIon());
|
---|
521 | FirstOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
522 | SecondOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
523 | ThirdOtherAtom->father = NULL; // we are just an added hydrogen with no father
|
---|
524 |
|
---|
525 | // we need to vectors orthonormal the InBondvector
|
---|
526 | AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
|
---|
527 | // LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
|
---|
528 | try{
|
---|
529 | Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
|
---|
530 | }
|
---|
531 | catch(LinearDependenceException &excp) {
|
---|
532 | LOG(0, boost::diagnostic_information(excp));
|
---|
533 | AllWentWell = false;
|
---|
534 | }
|
---|
535 | // LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
|
---|
536 |
|
---|
537 | // create correct coordination for the three atoms
|
---|
538 | alpha = (TopOrigin->getType()->getHBondAngle(2))/180.*M_PI/2.; // retrieve triple bond angle from database
|
---|
539 | l = BondRescale; // desired bond length
|
---|
540 | b = 2.*l*sin(alpha); // base length of isosceles triangle
|
---|
541 | d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.); // length for InBondvector
|
---|
542 | f = b/sqrt(3.); // length for Orthvector1
|
---|
543 | g = b/2.; // length for Orthvector2
|
---|
544 | // LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", ");
|
---|
545 | // LOG(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));
|
---|
546 | factors[0] = d;
|
---|
547 | factors[1] = f;
|
---|
548 | factors[2] = 0.;
|
---|
549 | FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
|
---|
550 | factors[1] = -0.5*f;
|
---|
551 | factors[2] = g;
|
---|
552 | SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
|
---|
553 | factors[2] = -g;
|
---|
554 | ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
|
---|
555 |
|
---|
556 | // rescale each to correct BondDistance
|
---|
557 | // FirstOtherAtom->x.Scale(&BondRescale);
|
---|
558 | // SecondOtherAtom->x.Scale(&BondRescale);
|
---|
559 | // ThirdOtherAtom->x.Scale(&BondRescale);
|
---|
560 |
|
---|
561 | // and relative to *origin atom
|
---|
562 | *FirstOtherAtom += TopOrigin->getPosition();
|
---|
563 | *SecondOtherAtom += TopOrigin->getPosition();
|
---|
564 | *ThirdOtherAtom += TopOrigin->getPosition();
|
---|
565 |
|
---|
566 | // ... and add to molecule
|
---|
567 | AllWentWell = AllWentWell && AddAtom(FirstOtherAtom);
|
---|
568 | AllWentWell = AllWentWell && AddAtom(SecondOtherAtom);
|
---|
569 | AllWentWell = AllWentWell && AddAtom(ThirdOtherAtom);
|
---|
570 | // LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
|
---|
571 | // LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
|
---|
572 | // LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << ".");
|
---|
573 | Binder = AddBond(BottomOrigin, FirstOtherAtom, 1);
|
---|
574 | Binder->Cyclic = false;
|
---|
575 | Binder->Type = GraphEdge::TreeEdge;
|
---|
576 | Binder = AddBond(BottomOrigin, SecondOtherAtom, 1);
|
---|
577 | Binder->Cyclic = false;
|
---|
578 | Binder->Type = GraphEdge::TreeEdge;
|
---|
579 | Binder = AddBond(BottomOrigin, ThirdOtherAtom, 1);
|
---|
580 | Binder->Cyclic = false;
|
---|
581 | Binder->Type = GraphEdge::TreeEdge;
|
---|
582 | break;
|
---|
583 | default:
|
---|
584 | ELOG(1, "BondDegree does not state single, double or triple bond!");
|
---|
585 | AllWentWell = false;
|
---|
586 | break;
|
---|
587 | }
|
---|
588 |
|
---|
589 | return AllWentWell;
|
---|
590 | };
|
---|
591 |
|
---|
592 | /** Adds given atom \a *pointer from molecule list.
|
---|
593 | * Increases molecule::last_atom and gives last number to added atom.
|
---|
594 | * \param filename name and path of xyz file
|
---|
595 | * \return true - succeeded, false - file not found
|
---|
596 | */
|
---|
597 | bool molecule::AddXYZFile(string filename)
|
---|
598 | {
|
---|
599 |
|
---|
600 | istringstream *input = NULL;
|
---|
601 | int NumberOfAtoms = 0; // atom number in xyz read
|
---|
602 | int i; // loop variables
|
---|
603 | atom *Walker = NULL; // pointer to added atom
|
---|
604 | char shorthand[3]; // shorthand for atom name
|
---|
605 | ifstream xyzfile; // xyz file
|
---|
606 | string line; // currently parsed line
|
---|
607 | double x[3]; // atom coordinates
|
---|
608 |
|
---|
609 | xyzfile.open(filename.c_str());
|
---|
610 | if (!xyzfile)
|
---|
611 | return false;
|
---|
612 |
|
---|
613 | OBSERVE;
|
---|
614 | getline(xyzfile,line,'\n'); // Read numer of atoms in file
|
---|
615 | input = new istringstream(line);
|
---|
616 | *input >> NumberOfAtoms;
|
---|
617 | LOG(0, "Parsing " << NumberOfAtoms << " atoms in file.");
|
---|
618 | getline(xyzfile,line,'\n'); // Read comment
|
---|
619 | LOG(1, "Comment: " << line);
|
---|
620 |
|
---|
621 | if (MDSteps == 0) // no atoms yet present
|
---|
622 | MDSteps++;
|
---|
623 | for(i=0;i<NumberOfAtoms;i++){
|
---|
624 | Walker = World::getInstance().createAtom();
|
---|
625 | getline(xyzfile,line,'\n');
|
---|
626 | istringstream *item = new istringstream(line);
|
---|
627 | //istringstream input(line);
|
---|
628 | //LOG(1, "Reading: " << line);
|
---|
629 | *item >> shorthand;
|
---|
630 | *item >> x[0];
|
---|
631 | *item >> x[1];
|
---|
632 | *item >> x[2];
|
---|
633 | Walker->setType(elemente->FindElement(shorthand));
|
---|
634 | if (Walker->getType() == NULL) {
|
---|
635 | ELOG(1, "Could not parse the element at line: '" << line << "', setting to H.");
|
---|
636 | Walker->setType(1);
|
---|
637 | }
|
---|
638 |
|
---|
639 | Walker->setPosition(Vector(x));
|
---|
640 | Walker->setPositionAtStep(MDSteps-1, Vector(x));
|
---|
641 | Walker->setAtomicVelocityAtStep(MDSteps-1, zeroVec);
|
---|
642 | Walker->setAtomicForceAtStep(MDSteps-1, zeroVec);
|
---|
643 | AddAtom(Walker); // add to molecule
|
---|
644 | delete(item);
|
---|
645 | }
|
---|
646 | xyzfile.close();
|
---|
647 | delete(input);
|
---|
648 | return true;
|
---|
649 | };
|
---|
650 |
|
---|
651 | /** Creates a copy of this molecule.
|
---|
652 | * \return copy of molecule
|
---|
653 | */
|
---|
654 | molecule *molecule::CopyMolecule() const
|
---|
655 | {
|
---|
656 | molecule *copy = World::getInstance().createMolecule();
|
---|
657 |
|
---|
658 | // copy all atoms
|
---|
659 | std::map< const atom *, atom *> FatherFinder;
|
---|
660 | for (iterator iter = begin(); iter != end(); ++iter) {
|
---|
661 | atom * const copy_atom = copy->AddCopyAtom(*iter);
|
---|
662 | FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
|
---|
663 | }
|
---|
664 |
|
---|
665 | // copy all bonds
|
---|
666 | for(const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
|
---|
667 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
668 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
669 | BondRunner != ListOfBonds.end();
|
---|
670 | ++BondRunner)
|
---|
671 | if ((*BondRunner)->leftatom == *AtomRunner) {
|
---|
672 | bond *Binder = (*BondRunner);
|
---|
673 | // get the pendant atoms of current bond in the copy molecule
|
---|
674 | ASSERT(FatherFinder.count(Binder->leftatom),
|
---|
675 | "molecule::CopyMolecule() - No copy of original left atom "
|
---|
676 | +toString(Binder->leftatom)+" for bond copy found");
|
---|
677 | ASSERT(FatherFinder.count(Binder->rightatom),
|
---|
678 | "molecule::CopyMolecule() - No copy of original right atom "
|
---|
679 | +toString(Binder->rightatom)+" for bond copy found");
|
---|
680 | atom * const LeftAtom = FatherFinder[Binder->leftatom];
|
---|
681 | atom * const RightAtom = FatherFinder[Binder->rightatom];
|
---|
682 |
|
---|
683 | bond * const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
|
---|
684 | NewBond->Cyclic = Binder->Cyclic;
|
---|
685 | if (Binder->Cyclic)
|
---|
686 | copy->NoCyclicBonds++;
|
---|
687 | NewBond->Type = Binder->Type;
|
---|
688 | }
|
---|
689 | }
|
---|
690 | // correct fathers
|
---|
691 | //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
|
---|
692 |
|
---|
693 | return copy;
|
---|
694 | };
|
---|
695 |
|
---|
696 |
|
---|
697 | /** Destroys all atoms inside this molecule.
|
---|
698 | */
|
---|
699 | void molecule::removeAtomsinMolecule()
|
---|
700 | {
|
---|
701 | // remove each atom from world
|
---|
702 | for(iterator AtomRunner = begin(); !empty(); AtomRunner = begin())
|
---|
703 | World::getInstance().destroyAtom(*AtomRunner);
|
---|
704 | };
|
---|
705 |
|
---|
706 |
|
---|
707 | /**
|
---|
708 | * Copies all atoms of a molecule which are within the defined parallelepiped.
|
---|
709 | *
|
---|
710 | * @param offest for the origin of the parallelepiped
|
---|
711 | * @param three vectors forming the matrix that defines the shape of the parallelpiped
|
---|
712 | */
|
---|
713 | molecule* molecule::CopyMoleculeFromSubRegion(const Shape ®ion) const {
|
---|
714 | molecule *copy = World::getInstance().createMolecule();
|
---|
715 |
|
---|
716 | // copy all atoms
|
---|
717 | std::map< const atom *, atom *> FatherFinder;
|
---|
718 | for (iterator iter = begin(); iter != end(); ++iter) {
|
---|
719 | if((*iter)->IsInShape(region)){
|
---|
720 | atom * const copy_atom = copy->AddCopyAtom(*iter);
|
---|
721 | FatherFinder.insert( std::make_pair( *iter, copy_atom ) );
|
---|
722 | }
|
---|
723 | }
|
---|
724 |
|
---|
725 | // copy all bonds
|
---|
726 | for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
|
---|
727 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
728 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
729 | BondRunner != ListOfBonds.end();
|
---|
730 | ++BondRunner)
|
---|
731 | if ((*BondRunner)->leftatom == *AtomRunner) {
|
---|
732 | bond *Binder = (*BondRunner);
|
---|
733 | if ((FatherFinder.count(Binder->leftatom))
|
---|
734 | && (FatherFinder.count(Binder->rightatom))) {
|
---|
735 | // if copy present, then it must be from subregion
|
---|
736 | atom * const LeftAtom = FatherFinder[Binder->leftatom];
|
---|
737 | atom * const RightAtom = FatherFinder[Binder->rightatom];
|
---|
738 |
|
---|
739 | bond * const NewBond = copy->AddBond(LeftAtom, RightAtom, Binder->BondDegree);
|
---|
740 | NewBond->Cyclic = Binder->Cyclic;
|
---|
741 | if (Binder->Cyclic)
|
---|
742 | copy->NoCyclicBonds++;
|
---|
743 | NewBond->Type = Binder->Type;
|
---|
744 | }
|
---|
745 | }
|
---|
746 | }
|
---|
747 | // correct fathers
|
---|
748 | //for_each(begin(),end(),mem_fun(&atom::CorrectFather));
|
---|
749 |
|
---|
750 | //TODO: copy->BuildInducedSubgraph(this);
|
---|
751 |
|
---|
752 | return copy;
|
---|
753 | }
|
---|
754 |
|
---|
755 | /** Adds a bond to a the molecule specified by two atoms, \a *first and \a *second.
|
---|
756 | * Also updates molecule::BondCount and molecule::NoNonBonds.
|
---|
757 | * \param *first first atom in bond
|
---|
758 | * \param *second atom in bond
|
---|
759 | * \return pointer to bond or NULL on failure
|
---|
760 | */
|
---|
761 | bond * molecule::AddBond(atom *atom1, atom *atom2, int degree)
|
---|
762 | {
|
---|
763 | OBSERVE;
|
---|
764 | bond *Binder = NULL;
|
---|
765 |
|
---|
766 | // some checks to make sure we are able to create the bond
|
---|
767 | ASSERT(atom1,
|
---|
768 | "molecule::AddBond() - First atom "+toString(atom1)
|
---|
769 | +" is not a invalid pointer");
|
---|
770 | ASSERT(atom2,
|
---|
771 | "molecule::AddBond() - Second atom "+toString(atom2)
|
---|
772 | +" is not a invalid pointer");
|
---|
773 | ASSERT(isInMolecule(atom1),
|
---|
774 | "molecule::AddBond() - First atom "+toString(atom1)
|
---|
775 | +" is not part of molecule");
|
---|
776 | ASSERT(isInMolecule(atom2),
|
---|
777 | "molecule::AddBond() - Second atom "+toString(atom2)
|
---|
778 | +" is not part of molecule");
|
---|
779 |
|
---|
780 | Binder = new bond(atom1, atom2, degree);
|
---|
781 | atom1->RegisterBond(WorldTime::getTime(), Binder);
|
---|
782 | atom2->RegisterBond(WorldTime::getTime(), Binder);
|
---|
783 | if ((atom1->getType() != NULL)
|
---|
784 | && (atom1->getType()->getAtomicNumber() != 1)
|
---|
785 | && (atom2->getType() != NULL)
|
---|
786 | && (atom2->getType()->getAtomicNumber() != 1))
|
---|
787 | NoNonBonds++;
|
---|
788 |
|
---|
789 | return Binder;
|
---|
790 | };
|
---|
791 |
|
---|
792 | /** Remove bond from bond chain list and from the both atom::ListOfBonds.
|
---|
793 | * Bond::~Bond takes care of bond removal
|
---|
794 | * \param *pointer bond pointer
|
---|
795 | * \return true - bound found and removed, false - bond not found/removed
|
---|
796 | */
|
---|
797 | bool molecule::RemoveBond(bond *pointer)
|
---|
798 | {
|
---|
799 | //ELOG(1, "molecule::RemoveBond: Function not implemented yet.");
|
---|
800 | delete(pointer);
|
---|
801 | return true;
|
---|
802 | };
|
---|
803 |
|
---|
804 | /** Remove every bond from bond chain list that atom \a *BondPartner is a constituent of.
|
---|
805 | * \todo Function not implemented yet
|
---|
806 | * \param *BondPartner atom to be removed
|
---|
807 | * \return true - bounds found and removed, false - bonds not found/removed
|
---|
808 | */
|
---|
809 | bool molecule::RemoveBonds(atom *BondPartner)
|
---|
810 | {
|
---|
811 | //ELOG(1, "molecule::RemoveBond: Function not implemented yet.");
|
---|
812 | BondPartner->removeAllBonds();
|
---|
813 | return false;
|
---|
814 | };
|
---|
815 |
|
---|
816 | /** Set molecule::name from the basename without suffix in the given \a *filename.
|
---|
817 | * \param *filename filename
|
---|
818 | */
|
---|
819 | void molecule::SetNameFromFilename(const char *filename)
|
---|
820 | {
|
---|
821 | int length = 0;
|
---|
822 | const char *molname = strrchr(filename, '/');
|
---|
823 | if (molname != NULL)
|
---|
824 | molname += sizeof(char); // search for filename without dirs
|
---|
825 | else
|
---|
826 | molname = filename; // contains no slashes
|
---|
827 | const char *endname = strchr(molname, '.');
|
---|
828 | if ((endname == NULL) || (endname < molname))
|
---|
829 | length = strlen(molname);
|
---|
830 | else
|
---|
831 | length = strlen(molname) - strlen(endname);
|
---|
832 | cout << "Set name of molecule " << getId() << " to " << molname << endl;
|
---|
833 | strncpy(name, molname, length);
|
---|
834 | name[length]='\0';
|
---|
835 | };
|
---|
836 |
|
---|
837 | /** Sets the molecule::cell_size to the components of \a *dim (rectangular box)
|
---|
838 | * \param *dim vector class
|
---|
839 | */
|
---|
840 | void molecule::SetBoxDimension(Vector *dim)
|
---|
841 | {
|
---|
842 | RealSpaceMatrix domain;
|
---|
843 | for(int i =0; i<NDIM;++i)
|
---|
844 | domain.at(i,i) = dim->at(i);
|
---|
845 | World::getInstance().setDomain(domain);
|
---|
846 | };
|
---|
847 |
|
---|
848 | /** Removes atom from molecule list and removes all of its bonds.
|
---|
849 | * \param *pointer atom to be removed
|
---|
850 | * \return true - succeeded, false - atom not found in list
|
---|
851 | */
|
---|
852 | bool molecule::RemoveAtom(atom *pointer)
|
---|
853 | {
|
---|
854 | ASSERT(pointer, "Null pointer passed to molecule::RemoveAtom().");
|
---|
855 | OBSERVE;
|
---|
856 | RemoveBonds(pointer);
|
---|
857 | pointer->removeFromMolecule();
|
---|
858 | return true;
|
---|
859 | };
|
---|
860 |
|
---|
861 | /** Removes atom from molecule list, but does not delete it.
|
---|
862 | * \param *pointer atom to be removed
|
---|
863 | * \return true - succeeded, false - atom not found in list
|
---|
864 | */
|
---|
865 | bool molecule::UnlinkAtom(atom *pointer)
|
---|
866 | {
|
---|
867 | if (pointer == NULL)
|
---|
868 | return false;
|
---|
869 | pointer->removeFromMolecule();
|
---|
870 | return true;
|
---|
871 | };
|
---|
872 |
|
---|
873 | /** Removes every atom from molecule list.
|
---|
874 | * \return true - succeeded, false - atom not found in list
|
---|
875 | */
|
---|
876 | bool molecule::CleanupMolecule()
|
---|
877 | {
|
---|
878 | for (molecule::iterator iter = begin(); !empty(); iter = begin())
|
---|
879 | (*iter)->removeFromMolecule();
|
---|
880 | return empty();
|
---|
881 | };
|
---|
882 |
|
---|
883 | /** Finds an atom specified by its continuous number.
|
---|
884 | * \param Nr number of atom withim molecule
|
---|
885 | * \return pointer to atom or NULL
|
---|
886 | */
|
---|
887 | atom * molecule::FindAtom(int Nr) const
|
---|
888 | {
|
---|
889 | molecule::iterator iter = begin();
|
---|
890 | for (; iter != end(); ++iter)
|
---|
891 | if ((*iter)->getNr() == Nr)
|
---|
892 | break;
|
---|
893 | if (iter != end()) {
|
---|
894 | //LOG(0, "Found Atom Nr. " << walker->getNr());
|
---|
895 | return (*iter);
|
---|
896 | } else {
|
---|
897 | ELOG(1, "Atom not found in molecule " << getName() << "'s list.");
|
---|
898 | return NULL;
|
---|
899 | }
|
---|
900 | }
|
---|
901 |
|
---|
902 | /** Checks whether the given atom is a member of this molecule.
|
---|
903 | *
|
---|
904 | * We make use here of molecule::atomIds to get a result on
|
---|
905 | *
|
---|
906 | * @param _atom atom to check
|
---|
907 | * @return true - is member, false - is not
|
---|
908 | */
|
---|
909 | bool molecule::isInMolecule(const atom * const _atom)
|
---|
910 | {
|
---|
911 | ASSERT(_atom->getMolecule() == this,
|
---|
912 | "molecule::isInMolecule() - atom is not designated to be in molecule '"
|
---|
913 | +toString(this->getName())+"'.");
|
---|
914 | molecule::atomIdSet::const_iterator iter = atomIds.find(_atom->getId());
|
---|
915 | return (iter != atomIds.end());
|
---|
916 | }
|
---|
917 |
|
---|
918 | /** Asks for atom number, and checks whether in list.
|
---|
919 | * \param *text question before entering
|
---|
920 | */
|
---|
921 | atom * molecule::AskAtom(string text)
|
---|
922 | {
|
---|
923 | int No;
|
---|
924 | atom *ion = NULL;
|
---|
925 | do {
|
---|
926 | //std::cout << "============Atom list==========================" << std::endl;
|
---|
927 | //mol->Output((ofstream *)&cout);
|
---|
928 | //std::cout << "===============================================" << std::endl;
|
---|
929 | std::cout << text;
|
---|
930 | cin >> No;
|
---|
931 | ion = this->FindAtom(No);
|
---|
932 | } while (ion == NULL);
|
---|
933 | return ion;
|
---|
934 | };
|
---|
935 |
|
---|
936 | /** Checks if given coordinates are within cell volume.
|
---|
937 | * \param *x array of coordinates
|
---|
938 | * \return true - is within, false - out of cell
|
---|
939 | */
|
---|
940 | bool molecule::CheckBounds(const Vector *x) const
|
---|
941 | {
|
---|
942 | const RealSpaceMatrix &domain = World::getInstance().getDomain().getM();
|
---|
943 | bool result = true;
|
---|
944 | for (int i=0;i<NDIM;i++) {
|
---|
945 | result = result && ((x->at(i) >= 0) && (x->at(i) < domain.at(i,i)));
|
---|
946 | }
|
---|
947 | //return result;
|
---|
948 | return true; /// probably not gonna use the check no more
|
---|
949 | };
|
---|
950 |
|
---|
951 | /** Prints molecule to *out.
|
---|
952 | * \param *out output stream
|
---|
953 | */
|
---|
954 | bool molecule::Output(ostream * const output) const
|
---|
955 | {
|
---|
956 | if (output == NULL) {
|
---|
957 | return false;
|
---|
958 | } else {
|
---|
959 | int AtomNo[MAX_ELEMENTS];
|
---|
960 | memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
|
---|
961 | enumeration<const element*> elementLookup = formula.enumerateElements();
|
---|
962 | *output << "#Ion_TypeNr._Nr.R[0] R[1] R[2] MoveType (0 MoveIon, 1 FixedIon)" << endl;
|
---|
963 | for_each(begin(),end(),boost::bind(&atom::OutputArrayIndexed,_1,output,elementLookup,AtomNo,(const char*)0));
|
---|
964 | return true;
|
---|
965 | }
|
---|
966 | };
|
---|
967 |
|
---|
968 | /** Prints molecule with all atomic trajectory positions to *out.
|
---|
969 | * \param *out output stream
|
---|
970 | */
|
---|
971 | bool molecule::OutputTrajectories(ofstream * const output) const
|
---|
972 | {
|
---|
973 | if (output == NULL) {
|
---|
974 | return false;
|
---|
975 | } else {
|
---|
976 | for (int step = 0; step < MDSteps; step++) {
|
---|
977 | if (step == 0) {
|
---|
978 | *output << "#Ion_TypeNr._Nr.R[0] R[1] R[2] MoveType (0 MoveIon, 1 FixedIon)" << endl;
|
---|
979 | } else {
|
---|
980 | *output << "# ====== MD step " << step << " =========" << endl;
|
---|
981 | }
|
---|
982 | int AtomNo[MAX_ELEMENTS];
|
---|
983 | memset(AtomNo,0,(MAX_ELEMENTS-1)*sizeof(*AtomNo));
|
---|
984 | enumeration<const element*> elementLookup = formula.enumerateElements();
|
---|
985 | for_each(begin(),end(),boost::bind(&atom::OutputTrajectory,_1,output,elementLookup, AtomNo, (const int)step));
|
---|
986 | }
|
---|
987 | return true;
|
---|
988 | }
|
---|
989 | };
|
---|
990 |
|
---|
991 | /** Outputs contents of each atom::ListOfBonds.
|
---|
992 | * \param *out output stream
|
---|
993 | */
|
---|
994 | void molecule::OutputListOfBonds() const
|
---|
995 | {
|
---|
996 | std::stringstream output;
|
---|
997 | LOG(2, "From Contents of ListOfBonds, all atoms:");
|
---|
998 | for (molecule::const_iterator iter = begin();
|
---|
999 | iter != end();
|
---|
1000 | ++iter) {
|
---|
1001 | (*iter)->OutputBondOfAtom(output);
|
---|
1002 | output << std::endl << "\t\t";
|
---|
1003 | }
|
---|
1004 | LOG(2, output.str());
|
---|
1005 | }
|
---|
1006 |
|
---|
1007 | /** Output of element before the actual coordination list.
|
---|
1008 | * \param *out stream pointer
|
---|
1009 | */
|
---|
1010 | bool molecule::Checkout(ofstream * const output) const
|
---|
1011 | {
|
---|
1012 | return formula.checkOut(output);
|
---|
1013 | };
|
---|
1014 |
|
---|
1015 | /** Prints molecule with all its trajectories to *out as xyz file.
|
---|
1016 | * \param *out output stream
|
---|
1017 | */
|
---|
1018 | bool molecule::OutputTrajectoriesXYZ(ofstream * const output)
|
---|
1019 | {
|
---|
1020 | time_t now;
|
---|
1021 |
|
---|
1022 | if (output != NULL) {
|
---|
1023 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
1024 | for (int step=0;step<MDSteps;step++) {
|
---|
1025 | *output << getAtomCount() << "\n\tCreated by molecuilder, step " << step << ", on " << ctime(&now);
|
---|
1026 | for_each(begin(),end(),boost::bind(&atom::OutputTrajectoryXYZ,_1,output,step));
|
---|
1027 | }
|
---|
1028 | return true;
|
---|
1029 | } else
|
---|
1030 | return false;
|
---|
1031 | };
|
---|
1032 |
|
---|
1033 | /** Prints molecule to *out as xyz file.
|
---|
1034 | * \param *out output stream
|
---|
1035 | */
|
---|
1036 | bool molecule::OutputXYZ(ofstream * const output) const
|
---|
1037 | {
|
---|
1038 | time_t now;
|
---|
1039 |
|
---|
1040 | if (output != NULL) {
|
---|
1041 | now = time((time_t *)NULL); // Get the system time and put it into 'now' as 'calender time'
|
---|
1042 | *output << getAtomCount() << "\n\tCreated by molecuilder on " << ctime(&now);
|
---|
1043 | for_each(begin(),end(),bind2nd(mem_fun(&atom::OutputXYZLine),output));
|
---|
1044 | return true;
|
---|
1045 | } else
|
---|
1046 | return false;
|
---|
1047 | };
|
---|
1048 |
|
---|
1049 | /** Brings molecule::AtomCount and atom::*Name up-to-date.
|
---|
1050 | * \param *out output stream for debugging
|
---|
1051 | */
|
---|
1052 | int molecule::doCountAtoms()
|
---|
1053 | {
|
---|
1054 | int res = size();
|
---|
1055 | NoNonHydrogen = 0;
|
---|
1056 | // go through atoms and look for new ones
|
---|
1057 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter)
|
---|
1058 | if ((*iter)->getType()->getAtomicNumber() != 1) // count non-hydrogen atoms whilst at it
|
---|
1059 | NoNonHydrogen++;
|
---|
1060 | return res;
|
---|
1061 | };
|
---|
1062 |
|
---|
1063 | /** Counts the number of present bonds.
|
---|
1064 | * \return number of bonds
|
---|
1065 | */
|
---|
1066 | int molecule::doCountBonds() const
|
---|
1067 | {
|
---|
1068 | unsigned int counter = 0;
|
---|
1069 | for(molecule::const_iterator AtomRunner = begin(); AtomRunner != end(); ++AtomRunner) {
|
---|
1070 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
1071 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
1072 | BondRunner != ListOfBonds.end();
|
---|
1073 | ++BondRunner)
|
---|
1074 | if ((*BondRunner)->leftatom == *AtomRunner)
|
---|
1075 | counter++;
|
---|
1076 | }
|
---|
1077 | return counter;
|
---|
1078 | }
|
---|
1079 |
|
---|
1080 |
|
---|
1081 | /** Returns an index map for two father-son-molecules.
|
---|
1082 | * The map tells which atom in this molecule corresponds to which one in the other molecul with their fathers.
|
---|
1083 | * \param *out output stream for debugging
|
---|
1084 | * \param *OtherMolecule corresponding molecule with fathers
|
---|
1085 | * \return allocated map of size molecule::AtomCount with map
|
---|
1086 | * \todo make this with a good sort O(n), not O(n^2)
|
---|
1087 | */
|
---|
1088 | int * molecule::GetFatherSonAtomicMap(molecule *OtherMolecule)
|
---|
1089 | {
|
---|
1090 | LOG(3, "Begin of GetFatherAtomicMap.");
|
---|
1091 | int *AtomicMap = new int[getAtomCount()];
|
---|
1092 | for (int i=getAtomCount();i--;)
|
---|
1093 | AtomicMap[i] = -1;
|
---|
1094 | if (OtherMolecule == this) { // same molecule
|
---|
1095 | for (int i=getAtomCount();i--;) // no need as -1 means already that there is trivial correspondence
|
---|
1096 | AtomicMap[i] = i;
|
---|
1097 | LOG(4, "Map is trivial.");
|
---|
1098 | } else {
|
---|
1099 | std::stringstream output;
|
---|
1100 | output << "Map is ";
|
---|
1101 | for (molecule::const_iterator iter = begin(); iter != end(); ++iter) {
|
---|
1102 | if ((*iter)->father == NULL) {
|
---|
1103 | AtomicMap[(*iter)->getNr()] = -2;
|
---|
1104 | } else {
|
---|
1105 | for (molecule::const_iterator runner = OtherMolecule->begin(); runner != OtherMolecule->end(); ++runner) {
|
---|
1106 | //for (int i=0;i<AtomCount;i++) { // search atom
|
---|
1107 | //for (int j=0;j<OtherMolecule->getAtomCount();j++) {
|
---|
1108 | //LOG(4, "Comparing father " << (*iter)->father << " with the other one " << (*runner)->father << ".");
|
---|
1109 | if ((*iter)->father == (*runner))
|
---|
1110 | AtomicMap[(*iter)->getNr()] = (*runner)->getNr();
|
---|
1111 | }
|
---|
1112 | }
|
---|
1113 | output << AtomicMap[(*iter)->getNr()] << "\t";
|
---|
1114 | }
|
---|
1115 | LOG(4, output.str());
|
---|
1116 | }
|
---|
1117 | LOG(3, "End of GetFatherAtomicMap.");
|
---|
1118 | return AtomicMap;
|
---|
1119 | };
|
---|
1120 |
|
---|
1121 |
|
---|
1122 | void molecule::flipActiveFlag(){
|
---|
1123 | ActiveFlag = !ActiveFlag;
|
---|
1124 | }
|
---|
1125 |
|
---|
1126 | // construct idpool
|
---|
1127 | CONSTRUCT_IDPOOL(atomId_t, continuousId)
|
---|