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