source: src/atom.cpp@ 443547

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

AtomInfo: AtomicPosition, ..Velocity, ..Force are protected to allow inheriting classes access.

  • This is necessary for atom::clone() have access to full trajectory.
  • Property mode set to 100644
File size: 11.8 KB
Line 
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
8/** \file atom.cpp
9 *
10 * Function implementations for the class atom.
11 *
12 */
13
14// include config.h
15#ifdef HAVE_CONFIG_H
16#include <config.h>
17#endif
18
19#include "CodePatterns/MemDebug.hpp"
20
21#include "atom.hpp"
22#include "bond.hpp"
23#include "config.hpp"
24#include "element.hpp"
25#include "lists.hpp"
26#include "parser.hpp"
27#include "LinearAlgebra/Vector.hpp"
28#include "World.hpp"
29#include "molecule.hpp"
30#include "Shapes/Shape.hpp"
31
32#include <iomanip>
33#include <iostream>
34
35/************************************* Functions for class atom *************************************/
36
37
38/** Constructor of class atom.
39 */
40atom::atom() :
41 father(this),
42 sort(&nr),
43 mol(0)
44{};
45
46/** Constructor of class atom.
47 */
48atom::atom(atom *pointer) :
49 ParticleInfo(pointer),
50 father(pointer),
51 sort(&nr),
52 mol(0)
53{
54 setType(pointer->getType()); // copy element of atom
55 AtomicPosition = pointer->AtomicPosition; // copy trajectory of coordination
56 AtomicVelocity = pointer->AtomicVelocity; // copy trajectory of velocity
57 AtomicForce = pointer->AtomicForce;
58 setFixedIon(pointer->getFixedIon());
59};
60
61atom *atom::clone(){
62 atom *res = new atom(this);
63 World::getInstance().registerAtom(res);
64 return res;
65}
66
67
68/** Destructor of class atom.
69 */
70atom::~atom()
71{
72 removeFromMolecule();
73 for(BondList::iterator iter=ListOfBonds.begin(); iter!=ListOfBonds.end();){
74 // deleting the bond will invalidate the iterator !!!
75 bond *bond =*(iter++);
76 delete(bond);
77 }
78};
79
80
81/** Climbs up the father list until NULL, last is returned.
82 * \return true father, i.e. whose father points to itself, NULL if it could not be found or has none (added hydrogen)
83 */
84atom *atom::GetTrueFather()
85{
86 if(father == this){ // top most father is the one that points on itself
87 return this;
88 }
89 else if(!father) {
90 return 0;
91 }
92 else {
93 return father->GetTrueFather();
94 }
95};
96
97/** Sets father to itself or its father in case of copying a molecule.
98 */
99void atom::CorrectFather()
100{
101 if (father->father == father) // same atom in copy's father points to itself
102 father = this; // set father to itself (copy of a whole molecule)
103 else
104 father = father->father; // set father to original's father
105
106};
107
108/** Check whether father is equal to given atom.
109 * \param *ptr atom to compare father to
110 * \param **res return value (only set if atom::father is equal to \a *ptr)
111 */
112void atom::EqualsFather ( const atom *ptr, const atom **res ) const
113{
114 if ( ptr == father )
115 *res = this;
116};
117
118bool atom::isFather(const atom *ptr){
119 return ptr==father;
120}
121
122/** Checks whether atom is within the given box.
123 * \param offset offset to box origin
124 * \param *parallelepiped box matrix
125 * \return true - is inside, false - is not
126 */
127bool atom::IsInShape(const Shape& shape) const
128{
129 return shape.isInside(getPosition());
130};
131
132/** Counts the number of bonds weighted by bond::BondDegree.
133 * \param bonds times bond::BondDegree
134 */
135int BondedParticle::CountBonds() const
136{
137 int NoBonds = 0;
138 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
139 NoBonds += (*Runner)->BondDegree;
140 return NoBonds;
141};
142
143/** Output of a single atom with given numbering.
144 * \param ElementNo cardinal number of the element
145 * \param AtomNo cardinal number among these atoms of the same element
146 * \param *out stream to output to
147 * \param *comment commentary after '#' sign
148 * \return true - \a *out present, false - \a *out is NULL
149 */
150bool atom::OutputIndexed(ofstream * const out, const int ElementNo, const int AtomNo, const char *comment) const
151{
152 if (out != NULL) {
153 *out << "Ion_Type" << ElementNo << "_" << AtomNo << "\t" << fixed << setprecision(9) << showpoint;
154 *out << at(0) << "\t" << at(1) << "\t" << at(2);
155 *out << "\t" << (int)(getFixedIon());
156 if (getAtomicVelocity().Norm() > MYEPSILON)
157 *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
158 if (comment != NULL)
159 *out << " # " << comment << endl;
160 else
161 *out << " # molecule nr " << nr << endl;
162 return true;
163 } else
164 return false;
165};
166
167/** Output of a single atom with numbering from array according to atom::type.
168 * \param *ElementNo cardinal number of the element
169 * \param *AtomNo cardinal number among these atoms of the same element
170 * \param *out stream to output to
171 * \param *comment commentary after '#' sign
172 * \return true - \a *out present, false - \a *out is NULL
173 */
174bool atom::OutputArrayIndexed(ostream * const out,const enumeration<const element*> &elementLookup, int *AtomNo, const char *comment) const
175{
176 AtomNo[getType()->getAtomicNumber()]++; // increment number
177 if (out != NULL) {
178 const element *elemental = getType();
179 ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
180 *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[elemental->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
181 *out << at(0) << "\t" << at(1) << "\t" << at(2);
182 *out << "\t" << getFixedIon();
183 if (getAtomicVelocity().Norm() > MYEPSILON)
184 *out << "\t" << scientific << setprecision(6) << getAtomicVelocity()[0] << "\t" << getAtomicVelocity()[1] << "\t" << getAtomicVelocity()[2] << "\t";
185 if (comment != NULL)
186 *out << " # " << comment << endl;
187 else
188 *out << " # molecule nr " << nr << endl;
189 return true;
190 } else
191 return false;
192};
193
194/** Output of a single atom as one line in xyz file.
195 * \param *out stream to output to
196 * \return true - \a *out present, false - \a *out is NULL
197 */
198bool atom::OutputXYZLine(ofstream *out) const
199{
200 if (out != NULL) {
201 *out << getType()->getSymbol() << "\t" << at(0) << "\t" << at(1) << "\t" << at(2) << "\t" << endl;
202 return true;
203 } else
204 return false;
205};
206
207/** Output of a single atom as one line in xyz file.
208 * \param *out stream to output to
209 * \param *ElementNo array with ion type number in the config file this atom's element shall have
210 * \param *AtomNo array with atom number in the config file this atom shall have, is increase by one automatically
211 * \param step Trajectory time step to output
212 * \return true - \a *out present, false - \a *out is NULL
213 */
214bool atom::OutputTrajectory(ofstream * const out, const enumeration<const element*> &elementLookup, int *AtomNo, const int step) const
215{
216 AtomNo[getType()->getAtomicNumber()]++;
217 if (out != NULL) {
218 const element *elemental = getType();
219 ASSERT(elementLookup.there.find(elemental)!=elementLookup.there.end(),"Type of this atom was not in the formula upon enumeration");
220 *out << "Ion_Type" << elementLookup.there.find(elemental)->second << "_" << AtomNo[getType()->getAtomicNumber()] << "\t" << fixed << setprecision(9) << showpoint;
221 *out << getPositionAtStep(step)[0] << "\t" << getPositionAtStep(step)[1] << "\t" << getPositionAtStep(step)[2];
222 *out << "\t" << (int)(getFixedIon());
223 if (getAtomicVelocityAtStep(step).Norm() > MYEPSILON)
224 *out << "\t" << scientific << setprecision(6) << getAtomicVelocityAtStep(step)[0] << "\t" << getAtomicVelocityAtStep(step)[1] << "\t" << getAtomicVelocityAtStep(step)[2] << "\t";
225 if (getAtomicForceAtStep(step).Norm() > MYEPSILON)
226 *out << "\t" << scientific << setprecision(6) << getAtomicForceAtStep(step)[0] << "\t" << getAtomicForceAtStep(step)[1] << "\t" << getAtomicForceAtStep(step)[2] << "\t";
227 *out << "\t# Number in molecule " << nr << endl;
228 return true;
229 } else
230 return false;
231};
232
233/** Output of a single atom as one lin in xyz file.
234 * \param *out stream to output to
235 * \param step Trajectory time step to output
236 * \return true - \a *out present, false - \a *out is NULL
237 */
238bool atom::OutputTrajectoryXYZ(ofstream * const out, const int step) const
239{
240 if (out != NULL) {
241 *out << getType()->getSymbol() << "\t";
242 *out << getPositionAtStep(step)[0] << "\t";
243 *out << getPositionAtStep(step)[1] << "\t";
244 *out << getPositionAtStep(step)[2] << endl;
245 return true;
246 } else
247 return false;
248};
249
250/** Outputs the MPQC configuration line for this atom.
251 * \param *out output stream
252 * \param *center center of molecule subtracted from position
253 * \param *AtomNo pointer to atom counter that is increased by one
254 */
255void atom::OutputMPQCLine(ostream * const out, const Vector *center) const
256{
257 Vector recentered(getPosition());
258 recentered -= *center;
259 *out << "\t\t" << getType()->getSymbol() << " [ " << recentered[0] << "\t" << recentered[1] << "\t" << recentered[2] << " ]" << endl;
260};
261
262/** Compares the indices of \a this atom with a given \a ptr.
263 * \param ptr atom to compare index against
264 * \return true - this one's is smaller, false - not
265 */
266bool atom::Compare(const atom &ptr) const
267{
268 if (nr < ptr.nr)
269 return true;
270 else
271 return false;
272};
273
274/** Returns squared distance to a given vector.
275 * \param origin vector to calculate distance to
276 * \return distance squared
277 */
278double atom::DistanceSquaredToVector(const Vector &origin) const
279{
280 return DistanceSquared(origin);
281};
282
283/** Returns distance to a given vector.
284 * \param origin vector to calculate distance to
285 * \return distance
286 */
287double atom::DistanceToVector(const Vector &origin) const
288{
289 return distance(origin);
290};
291
292/** Initialises the component number array.
293 * Size is set to atom::ListOfBonds.size()+1 (last is th encode end by -1)
294 */
295void atom::InitComponentNr()
296{
297 if (ComponentNr != NULL)
298 delete[](ComponentNr);
299 ComponentNr = new int[ListOfBonds.size()+1];
300 for (int i=ListOfBonds.size()+1;i--;)
301 ComponentNr[i] = -1;
302};
303
304void atom::resetGraphNr(){
305 GraphNr=-1;
306}
307
308std::ostream & atom::operator << (std::ostream &ost) const
309{
310 ParticleInfo::operator<<(ost);
311 ost << "," << getPosition();
312 return ost;
313}
314
315std::ostream & operator << (std::ostream &ost, const atom &a)
316{
317 a.ParticleInfo::operator<<(ost);
318 ost << "," << a.getPosition();
319 return ost;
320}
321
322bool operator < (atom &a, atom &b)
323{
324 return a.Compare(b);
325};
326
327World *atom::getWorld(){
328 return world;
329}
330
331void atom::setWorld(World* _world){
332 world = _world;
333}
334
335bool atom::changeId(atomId_t newId){
336 // first we move ourselves in the world
337 // the world lets us know if that succeeded
338 if(world->changeAtomId(id,newId,this)){
339 id = newId;
340 return true;
341 }
342 else{
343 return false;
344 }
345}
346
347void atom::setId(atomId_t _id) {
348 id=_id;
349}
350
351atomId_t atom::getId() const {
352 return id;
353}
354
355/** Makes the atom be contained in the new molecule \a *_mol.
356 * Uses atom::removeFromMolecule() to delist from old molecule.
357 * \param *_mol pointer to new molecule
358 */
359void atom::setMolecule(molecule *_mol){
360 // take this atom from the old molecule
361 removeFromMolecule();
362 mol = _mol;
363 if(!mol->containsAtom(this)){
364 mol->insert(this);
365 }
366}
367
368/** Returns pointer to the molecule which atom belongs to.
369 * \return containing molecule
370 */
371molecule* atom::getMolecule() const {
372 return mol;
373}
374
375/** Erases the atom in atom::mol's list of atoms and sets it to zero.
376 */
377void atom::removeFromMolecule(){
378 if(mol){
379 if(mol->containsAtom(this)){
380 mol->erase(this);
381 }
382 mol=0;
383 }
384}
385
386int atom::getNr() const{
387 return nr;
388}
389
390atom* NewAtom(atomId_t _id){
391 atom * res =new atom();
392 res->setId(_id);
393 return res;
394}
395
396void DeleteAtom(atom* atom){
397 delete atom;
398}
399
400bool compareAtomElements(atom* atom1,atom* atom2){
401 return atom1->getType()->getNumber() < atom2->getType()->getNumber();
402}
Note: See TracBrowser for help on using the repository browser.