source: src/molecule.cpp@ 4b5cf8

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 4b5cf8 was 4b5cf8, checked in by Frederik Heber <heber@…>, 14 years ago

BondedParticle::OutputBondOfAtom() now takes stream to print to.

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