source: src/Atom/atom_bondedparticle.cpp@ 7b9fe0

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 7b9fe0 was 52ed5b, checked in by Frederik Heber <heber@…>, 13 years ago

Ids handed out by molecule now start with 1.

  • Property mode set to 100644
File size: 10.3 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * atom_bondedparticle.cpp
10 *
11 * Created on: Oct 19, 2009
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include "atom.hpp"
23#include "atom_bondedparticle.hpp"
24#include "Bond/bond.hpp"
25#include "CodePatterns/Assert.hpp"
26#include "CodePatterns/Log.hpp"
27#include "CodePatterns/Verbose.hpp"
28#include "Element/element.hpp"
29#include "WorldTime.hpp"
30
31/** Constructor of class BondedParticle.
32 */
33BondedParticle::BondedParticle()
34{
35 ListOfBonds.push_back(BondList());
36};
37
38/** Destructor of class BondedParticle.
39 */
40BondedParticle::~BondedParticle()
41{
42 removeAllBonds();
43};
44
45/** Outputs the current atom::AdaptiveOrder and atom::MaxOrder to \a *file.
46 * \param *file output stream
47 */
48void BondedParticle::OutputOrder(ofstream *file) const
49{
50 *file << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << endl;
51 //LOG(2, "Storing: " << getNr() << "\t" << (int)AdaptiveOrder << "\t" << (int)MaxOrder << ".");
52};
53
54/** Prints all bonds of this atom with total degree.
55 */
56void BondedParticle::OutputBondOfAtom(std::ostream &ost) const
57{
58 const BondList& ListOfBonds = getListOfBonds();
59 ost << "Atom " << getName() << "/" << getNr() << " with " << ListOfBonds.size() << " bonds: ";
60 int TotalDegree = 0;
61 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); ++Runner) {
62 ost << **Runner << "\t";
63 TotalDegree += (*Runner)->BondDegree;
64 }
65 ost << " -- TotalDegree: " << TotalDegree;
66};
67
68/** Output of atom::Nr along with all bond partners.
69 * \param *AdjacencyFile output stream
70 */
71void BondedParticle::OutputAdjacency(ofstream * const AdjacencyFile) const
72{
73 const BondList& ListOfBonds = getListOfBonds();
74 *AdjacencyFile << getNr() << "\t";
75 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
76 *AdjacencyFile << (*Runner)->GetOtherAtom(this)->getNr() << "\t";
77 *AdjacencyFile << endl;
78};
79
80/** Output of atom::Nr along each bond partner per line.
81 * Only bonds are printed where atom::Nr is smaller than the one of the bond partner.
82 * \param *AdjacencyFile output stream
83 */
84void BondedParticle::OutputBonds(ofstream * const BondFile) const
85{
86 const BondList& ListOfBonds = getListOfBonds();
87 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner))
88 if (getNr() < (*Runner)->GetOtherAtom(this)->getNr())
89 *BondFile << getNr() << "\t" << (*Runner)->GetOtherAtom(this)->getNr() << "\n";
90};
91
92/**
93 * Adds a bond between this bonded particle and another. Returns present instance if this
94 * bond already exists.
95 *
96 * @param _step time step to access
97 * @param bonding partner
98 * @return const reference to created bond or to already present bonds
99 */
100const bond * BondedParticle::addBond(const unsigned int _step, BondedParticle* Partner)
101{
102 const BondList &bondlist = getListOfBondsAtStep(_step);
103 for (BondList::const_iterator runner = bondlist.begin();
104 runner != bondlist.end();
105 runner++) {
106 if ((*runner)->Contains(Partner))
107 return *runner;
108 }
109
110 bond* newBond = new bond((atom*) this, (atom*) Partner, 1);
111 RegisterBond(_step, newBond);
112 Partner->RegisterBond(_step, newBond);
113
114 return newBond;
115}
116
117/** Removes a bond for this atom.
118 *
119 * @param Binder bond to remove
120 */
121void BondedParticle::removeBond(bond * binder)
122{
123 UnregisterBond(binder);
124}
125
126/** Removes all bonds and their instances, too.
127 *
128 */
129void BondedParticle::removeAllBonds()
130{
131 for (size_t index = 0; index < ListOfBonds.size(); ++index)
132 {
133 for (BondList::iterator iter = ListOfBonds[index].begin();
134 !ListOfBonds[index].empty();
135 iter = ListOfBonds[index].begin()) {
136 delete (*iter);
137 // unregister/NOTIFY is done by bond::~bond()
138 }
139 }
140}
141
142/** Puts a given bond into atom::ListOfBonds.
143 * @param _step time step to access
144 * \param *Binder bond to insert
145 */
146bool BondedParticle::RegisterBond(const unsigned int _step, bond *Binder)
147{
148 OBSERVE;
149 bool status = false;
150 if (Binder != NULL) {
151 if (Binder->Contains(this)) {
152 //LOG(3,"INFO: Registering bond "<< *Binder << " with atom " << *this << " at step " << _step);
153 if (ListOfBonds.size() <= _step)
154 ListOfBonds.resize(_step+1);
155 ListOfBonds[_step].push_back(Binder);
156 if (WorldTime::getTime() == _step)
157 NOTIFY(AtomObservable::BondsAdded);
158 status = true;
159 } else {
160 ELOG(1, *Binder << " does not contain " << *this << ".");
161 }
162 } else {
163 ELOG(1, "Binder is " << Binder << ".");
164 }
165 return status;
166};
167
168/** Removes a given bond from atom::ListOfBonds.
169 * @param _step time step to access
170 * \param *Binder bond to remove
171 */
172bool BondedParticle::UnregisterBond(bond *Binder)
173{
174 OBSERVE;
175 bool status = false;
176 ASSERT(Binder != NULL, "BondedParticle::UnregisterBond() - Binder is NULL.");
177 const int step = ContainsBondAtStep(Binder);
178 if (step != -1) {
179 //LOG(0,"INFO: Unregistering bond "<< *Binder << " from list " << &ListOfBonds << " of atom " << *this << " at step " << step);
180 ListOfBonds[step].remove(Binder);
181 status = true;
182 } else {
183 ELOG(1, *Binder << " does not contain " << *this << ".");
184 }
185 return status;
186};
187
188/** Removes all bonds from atom::ListOfBonds.
189 * \note Does not do any memory de-allocation.
190 */
191void BondedParticle::UnregisterAllBond(const unsigned int _step)
192{
193 OBSERVE;
194 ListOfBonds[_step].clear();
195}
196
197/** Removes all bonds of given \a _step with freeing memory.
198 *
199 * @param _step time step whose bonds to free
200 */
201void BondedParticle::ClearBondsAtStep(const unsigned int _step)
202{
203 OBSERVE;
204 //LOG(3,"INFO: Clearing all bonds of " << *this << ": " << ListOfBonds[_step]);
205 for (BondList::iterator iter = (ListOfBonds[_step]).begin();
206 !(ListOfBonds[_step]).empty();
207 iter = (ListOfBonds[_step]).begin()) {
208 //LOG(3,"INFO: Clearing bond (" << *iter << ") " << *(*iter) << " of list " << &ListOfBonds);
209 delete((*iter)); // will also unregister with us and remove from list
210 }
211}
212
213/** Searches for the time step where the given bond \a *Binder is a bond of this particle.
214 *
215 * @param Binder bond to check
216 * @return >=0 - first time step where bond appears, -1 - bond not present in lists
217 */
218int BondedParticle::ContainsBondAtStep(bond *Binder) const
219{
220 int step = -1;
221 int tempstep = 0;
222 for(std::vector<BondList>::const_iterator iter = ListOfBonds.begin();
223 iter != ListOfBonds.end();
224 ++iter,++tempstep) {
225 for (BondList::const_iterator bonditer = iter->begin();
226 bonditer != iter->end();
227 ++bonditer) {
228 if ((*bonditer) == Binder) {
229 step = tempstep;
230 break;
231 }
232 }
233 if (step != -1)
234 break;
235 }
236
237 return step;
238}
239
240/** Corrects the bond degree by one at most if necessary.
241 * \return number of corrections done
242 */
243int BondedParticle::CorrectBondDegree()
244{
245 OBSERVE;
246 NOTIFY(AtomObservable::BondDegreeChanged);
247 int NoBonds = 0;
248 int OtherNoBonds = 0;
249 int FalseBondDegree = 0;
250 atom *OtherWalker = NULL;
251 bond *CandidateBond = NULL;
252
253 NoBonds = CountBonds();
254 //LOG(3, "Walker " << *this << ": " << (int)this->type->NoValenceOrbitals << " > " << NoBonds << "?");
255 if ((int)(getType()->getNoValenceOrbitals()) > NoBonds) { // we have a mismatch, check all bonding partners for mismatch
256 const BondList& ListOfBonds = getListOfBonds();
257 for (BondList::const_iterator Runner = ListOfBonds.begin(); Runner != ListOfBonds.end(); (++Runner)) {
258 OtherWalker = (*Runner)->GetOtherAtom(this);
259 OtherNoBonds = OtherWalker->CountBonds();
260 //LOG(3, "OtherWalker " << *OtherWalker << ": " << (int)OtherWalker->type->NoValenceOrbitals << " > " << OtherNoBonds << "?");
261 if ((int)(OtherWalker->getType()->getNoValenceOrbitals()) > OtherNoBonds) { // check if possible candidate
262 const BondList& OtherListOfBonds = OtherWalker->getListOfBonds();
263 if ((CandidateBond == NULL) || (ListOfBonds.size() > OtherListOfBonds.size())) { // pick the one with fewer number of bonds first
264 CandidateBond = (*Runner);
265 //LOG(3, "New candidate is " << *CandidateBond << ".");
266 }
267 }
268 }
269 if ((CandidateBond != NULL)) {
270 CandidateBond->BondDegree++;
271 //LOG(2, "Increased bond degree for bond " << *CandidateBond << ".");
272 } else {
273 ELOG(2, "Could not find correct degree for atom " << *this << ".");
274 FalseBondDegree++;
275 }
276 }
277 return FalseBondDegree;
278};
279
280/** Sets the weight of all connected bonds to one.
281 */
282void BondedParticle::resetBondDegree()
283{
284 OBSERVE;
285 NOTIFY(BondedParticle::BondDegreeChanged);
286 for (std::vector<BondList>::iterator Runner = ListOfBonds.begin();
287 Runner != ListOfBonds.end();
288 ++Runner)
289 for (BondList::iterator BondRunner = (*Runner).begin();
290 BondRunner != (*Runner).end();
291 ++BondRunner)
292 (*BondRunner)->BondDegree = 1;
293};
294
295/** Counts the number of bonds weighted by bond::BondDegree.
296 * @param _step time step to access
297 * \param bonds times bond::BondDegree
298 */
299int BondedParticle::CountBonds() const
300{
301 int NoBonds = 0;
302 const BondList& ListOfBonds = getListOfBonds();
303 for (BondList::const_iterator Runner = ListOfBonds.begin();
304 Runner != ListOfBonds.end();
305 (++Runner))
306 NoBonds += (*Runner)->BondDegree;
307 return NoBonds;
308};
309
310/** Checks whether there is a bond between \a this atom and the given \a *BondPartner.
311 * @param _step time step to access
312 * \param *BondPartner atom to check for
313 * \return true - bond exists, false - bond does not exist
314 */
315bool BondedParticle::IsBondedTo(const unsigned int _step, BondedParticle * const BondPartner) const
316{
317 bool status = false;
318
319 const BondList& ListOfBonds = getListOfBondsAtStep(_step);
320 for (BondList::const_iterator runner = ListOfBonds.begin();
321 runner != ListOfBonds.end();
322 runner++) {
323 status = status || ((*runner)->Contains(BondPartner));
324 }
325 return status;
326};
327
328std::ostream & BondedParticle::operator << (std::ostream &ost) const
329{
330 ParticleInfo::operator<<(ost);
331 ost << "," << getPosition();
332 return ost;
333}
334
335std::ostream & operator << (std::ostream &ost, const BondedParticle &a)
336{
337 a.ParticleInfo::operator<<(ost);
338 ost << "," << a.getPosition();
339 return ost;
340}
341
Note: See TracBrowser for help on using the repository browser.