source: src/molecule.cpp@ 0e2031

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 0e2031 was a564be, checked in by Frederik Heber <heber@…>, 14 years ago

Removed ancient StackClass, replaced by std::deque.

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