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