source: src/molecule.cpp@ f71baf

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 Candidate_v1.7.0 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 f71baf was 735b1c, checked in by Frederik Heber <heber@…>, 15 years ago

ParticleInfo::ParticleInfo_nr is protected and accessed via getter/setter.

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