source: src/atom.cpp@ d74077

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

Member variable Vector and element of class atom are now private.

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