source: src/molecule.cpp@ 0adac1

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 0adac1 was a7a087, checked in by Frederik Heber <heber@…>, 15 years ago

new function World::changeMoleculeId().

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