source: src/Atom/atom_atominfo.cpp@ 8ac6d0e

Candidate_v1.6.1 ChemicalSpaceEvaluator Gui_displays_atomic_force_velocity PythonUI_with_named_parameters TremoloParser_IncreasedPrecision
Last change on this file since 8ac6d0e was 8ac6d0e, checked in by Frederik Heber <frederik.heber@…>, 7 years ago

FIX: atominfo now provides correct TrajectoryUpdate notifications.

  • Property mode set to 100644
File size: 18.6 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 * Copyright (C) 2014 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * atom_atominfo.cpp
26 *
27 * Created on: Oct 19, 2009
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36//#include "CodePatterns/MemDebug.hpp"
37
38#include "CodePatterns/Verbose.hpp"
39
40#include "atom_atominfo.hpp"
41#include "CodePatterns/Log.hpp"
42#include "config.hpp"
43#include "Element/element.hpp"
44#include "Element/periodentafel.hpp"
45#include "Fragmentation/ForceMatrix.hpp"
46#include "World.hpp"
47#include "WorldTime.hpp"
48
49#include <iomanip>
50
51/** Constructor of class AtomInfo.
52 */
53AtomInfo::AtomInfo() :
54 AtomicElement(1),
55 FixedIon(false),
56 charge(0.)
57{
58 AtomicPosition.insert( std::make_pair(0, zeroVec) );
59 AtomicVelocity.insert( std::make_pair(0, zeroVec) );
60 AtomicForce.insert( std::make_pair(0, zeroVec) );
61}
62
63/** Copy constructor of class AtomInfo.
64 */
65AtomInfo::AtomInfo(const AtomInfo &_atom) :
66 AtomicPosition(_atom.AtomicPosition),
67 AtomicVelocity(_atom.AtomicVelocity),
68 AtomicForce(_atom.AtomicForce),
69 AtomicElement(_atom.AtomicElement),
70 FixedIon(_atom.FixedIon),
71 charge(_atom.charge)
72{
73}
74
75AtomInfo::AtomInfo(const VectorInterface &_v) :
76 AtomicElement(1),
77 FixedIon(false),
78 charge(0.)
79{
80 AtomicPosition.insert( std::make_pair(0, _v.getPosition()) );
81 AtomicVelocity.insert( std::make_pair(0, zeroVec) );
82 AtomicForce.insert( std::make_pair(0, zeroVec) );
83};
84
85/** Destructor of class AtomInfo.
86 */
87AtomInfo::~AtomInfo()
88{
89};
90
91void AtomInfo::AppendTrajectoryStep(const unsigned int _step)
92{
93 NOTIFY(TrajectoryChanged);
94 AtomicPosition.insert( std::make_pair(_step, zeroVec) );
95 AtomicVelocity.insert( std::make_pair(_step, zeroVec) );
96 AtomicForce.insert( std::make_pair(_step, zeroVec) );
97 LOG(5,"AtomInfo::AppendTrajectoryStep() called, size is ("
98 << AtomicPosition.size() << ","
99 << AtomicVelocity.size() << ","
100 << AtomicForce.size() << ")");
101}
102
103void AtomInfo::eraseInTrajctory(
104 VectorTrajectory_t &_trajectory,
105 const unsigned int _firststep, const unsigned int _laststep)
106{
107 const VectorTrajectory_t::iterator firstiter = _trajectory.lower_bound(_firststep);
108 const VectorTrajectory_t::iterator lastiter = _trajectory.upper_bound(_laststep);
109 _trajectory.erase(firstiter, lastiter);
110}
111
112void AtomInfo::removeTrajectorySteps(const unsigned int _firststep, const unsigned int _laststep)
113{
114 NOTIFY(TrajectoryChanged);
115 eraseInTrajctory(AtomicPosition, _firststep, _laststep);
116 eraseInTrajctory(AtomicVelocity, _firststep, _laststep);
117 eraseInTrajctory(AtomicForce, _firststep, _laststep);
118 LOG(5,"AtomInfo::removeTrajectorySteps() called, size is ("
119 << AtomicPosition.size() << ","
120 << AtomicVelocity.size() << ","
121 << AtomicForce.size() << ")");
122}
123
124const element *AtomInfo::getType() const
125{
126 const element *elem = World::getInstance().getPeriode()->FindElement(AtomicElement);
127 return elem;
128}
129
130const element &AtomInfo::getElement() const
131{
132 const element &elem = *World::getInstance().getPeriode()->FindElement(AtomicElement);
133 return elem;
134}
135
136atomicNumber_t AtomInfo::getElementNo() const
137{
138 return AtomicElement;
139}
140
141const std::string &AtomInfo::getParticleName() const
142{
143 return particlename;
144}
145
146void AtomInfo::setParticleName(const std::string & _name)
147{
148 particlename = _name;
149}
150
151const double& AtomInfo::operator[](size_t i) const
152{
153 return atStep(i, WorldTime::getTime());
154}
155
156const double& AtomInfo::at(size_t i) const
157{
158 return atStep(i, WorldTime::getTime());
159}
160
161const double& AtomInfo::atStep(size_t i, unsigned int _step) const
162{
163 ASSERT(!AtomicPosition.empty(),
164 "AtomInfo::operator[]() - AtomicPosition is empty.");
165 VectorTrajectory_t::const_iterator iter =
166 AtomicPosition.lower_bound(_step);
167 return iter->second[i];
168}
169
170void AtomInfo::set(size_t i, const double value)
171{
172 OBSERVE;
173 NOTIFY(AtomObservable::PositionChanged);
174 VectorTrajectory_t::iterator iter = AtomicPosition.find(WorldTime::getTime());
175 if (iter != AtomicPosition.end()) {
176 iter->second[i] = value;
177 } else {
178 Vector newPos;
179 newPos[i] = value;
180#ifndef NDEBUG
181 std::pair<VectorTrajectory_t::iterator, bool> inserter =
182#endif
183 AtomicPosition.insert( std::make_pair(WorldTime::getTime(), newPos) );
184 ASSERT( inserter.second,
185 "AtomInfo::set() - time step "+toString(WorldTime::getTime())
186 +" present after all?");
187 }
188}
189
190void AtomInfo::setAtStep(size_t i, unsigned int _step, const double value)
191{
192 OBSERVE;
193 VectorTrajectory_t::iterator iter = AtomicPosition.find(_step);
194 if (iter != AtomicPosition.end()) {
195 iter->second[i] = value;
196 } else {
197 NOTIFY(TrajectoryChanged);
198 Vector newPos;
199 newPos[i] = value;
200#ifndef NDEBUG
201 std::pair<VectorTrajectory_t::iterator, bool> inserter =
202#endif
203 AtomicPosition.insert( std::make_pair(_step, newPos) );
204 ASSERT( inserter.second,
205 "AtomInfo::setAtStep() - time step "+toString(_step)
206 +" present after all?");
207 }
208 if (WorldTime::getTime() == _step)
209 NOTIFY(AtomObservable::PositionChanged);
210}
211
212/** Helps to determine whether the current step really exists or getPosition() has just
213 * delivered the one closest to it in the past.
214 *
215 * \param _step step to check
216 * \param true - step exists, false - step does not exist, getPosition() delivers closest
217 */
218bool AtomInfo::isStepPresent(const unsigned int _step) const
219{
220 VectorTrajectory_t::const_iterator iter =
221 AtomicPosition.find(_step);
222 return iter != AtomicPosition.end();
223}
224
225const Vector& AtomInfo::getPosition() const
226{
227 return getPositionAtStep(WorldTime::getTime());
228}
229
230const Vector& AtomInfo::getPositionAtStep(const unsigned int _step) const
231{
232 ASSERT(!AtomicPosition.empty(),
233 "AtomInfo::operator[]() - AtomicPosition is empty.");
234 VectorTrajectory_t::const_iterator iter =
235 AtomicPosition.lower_bound(_step);
236 return iter->second;
237}
238
239void AtomInfo::setType(const element* _type)
240{
241 OBSERVE;
242 NOTIFY(AtomObservable::ElementChanged);
243 AtomicElement = _type->getAtomicNumber();
244}
245
246void AtomInfo::setType(const int Z)
247{
248 const element *elem = World::getInstance().getPeriode()->FindElement(Z);
249 setType(elem);
250}
251
252const Vector& AtomInfo::getAtomicVelocity() const
253{
254 return getAtomicVelocityAtStep(WorldTime::getTime());
255}
256
257const Vector& AtomInfo::getAtomicVelocityAtStep(const unsigned int _step) const
258{
259 ASSERT(!AtomicVelocity.empty(),
260 "AtomInfo::operator[]() - AtomicVelocity is empty.");
261 VectorTrajectory_t::const_iterator iter =
262 AtomicVelocity.lower_bound(_step);
263 // special, we only interpolate between present time steps not into the future
264 if (_step > AtomicVelocity.begin()->first)
265 return zeroVec;
266 else
267 return iter->second;
268}
269
270void AtomInfo::setAtomicVelocity(const Vector &_newvelocity)
271{
272 setAtomicVelocityAtStep(WorldTime::getTime(), _newvelocity);
273}
274
275void AtomInfo::setAtomicVelocityAtStep(const unsigned int _step, const Vector &_newvelocity)
276{
277 OBSERVE;
278 VectorTrajectory_t::iterator iter = AtomicVelocity.find(_step);
279 if (iter != AtomicVelocity.end()) {
280 iter->second = _newvelocity;
281 } else {
282 NOTIFY(TrajectoryChanged);
283#ifndef NDEBUG
284 std::pair<VectorTrajectory_t::iterator, bool> inserter =
285#endif
286 AtomicVelocity.insert( std::make_pair(_step, _newvelocity) );
287 ASSERT( inserter.second,
288 "AtomInfo::set() - time step "+toString(_step)
289 +" present after all?");
290 }
291 if (WorldTime::getTime() == _step)
292 NOTIFY(AtomObservable::VelocityChanged);
293}
294
295const Vector& AtomInfo::getAtomicForce() const
296{
297 return getAtomicForceAtStep(WorldTime::getTime());
298}
299
300const Vector& AtomInfo::getAtomicForceAtStep(const unsigned int _step) const
301{
302 ASSERT(!AtomicForce.empty(),
303 "AtomInfo::operator[]() - AtomicForce is empty.");
304 VectorTrajectory_t::const_iterator iter =
305 AtomicForce.lower_bound(_step);
306 // special, we only interpolate between present time steps not into the future
307 if (_step > AtomicForce.begin()->first)
308 return zeroVec;
309 else
310 return iter->second;
311}
312
313void AtomInfo::setAtomicForce(const Vector &_newforce)
314{
315 setAtomicForceAtStep(WorldTime::getTime(), _newforce);
316}
317
318void AtomInfo::setAtomicForceAtStep(const unsigned int _step, const Vector &_newforce)
319{
320 OBSERVE;
321 VectorTrajectory_t::iterator iter = AtomicForce.find(_step);
322 if (iter != AtomicForce.end()) {
323 iter->second = _newforce;
324 } else {
325 NOTIFY(TrajectoryChanged);
326#ifndef NDEBUG
327 std::pair<VectorTrajectory_t::iterator, bool> inserter =
328#endif
329 AtomicForce.insert( std::make_pair(_step, _newforce) );
330 ASSERT( inserter.second,
331 "AtomInfo::set() - time step "+toString(_step)
332 +" present after all?");
333 }
334 if (WorldTime::getTime() == _step)
335 NOTIFY(AtomObservable::ForceChanged);
336}
337
338bool AtomInfo::getFixedIon() const
339{
340 return FixedIon;
341}
342
343void AtomInfo::setFixedIon(const bool _fixedion)
344{
345 OBSERVE;
346 NOTIFY(AtomObservable::PropertyChanged);
347 FixedIon = _fixedion;
348}
349
350void AtomInfo::setPosition(const Vector& _vector)
351{
352 setPositionAtStep(WorldTime::getTime(), _vector);
353}
354
355void AtomInfo::setPositionAtStep(unsigned int _step, const Vector& _vector)
356{
357 OBSERVE;
358 VectorTrajectory_t::iterator iter = AtomicPosition.find(_step);
359 if (iter != AtomicPosition.end()) {
360 iter->second = _vector;
361 } else {
362#ifndef NDEBUG
363 std::pair<VectorTrajectory_t::iterator, bool> inserter =
364#endif
365 AtomicPosition.insert( std::make_pair(_step, _vector) );
366 ASSERT( inserter.second,
367 "AtomInfo::set() - time step "+toString(_step)
368 +" present after all?");
369 }
370 if (WorldTime::getTime() == _step)
371 NOTIFY(AtomObservable::PositionChanged);
372}
373
374const VectorInterface& AtomInfo::operator+=(const Vector& b)
375{
376 setPosition(getPosition()+b);
377 return *this;
378}
379
380const VectorInterface& AtomInfo::operator-=(const Vector& b)
381{
382 setPosition(getPosition()-b);
383 return *this;
384}
385
386Vector const AtomInfo::operator+(const Vector& b) const
387{
388 Vector a(getPosition());
389 a += b;
390 return a;
391}
392
393Vector const AtomInfo::operator-(const Vector& b) const
394{
395 Vector a(getPosition());
396 a -= b;
397 return a;
398}
399
400double AtomInfo::distance(const Vector &point) const
401{
402 return getPosition().distance(point);
403}
404
405double AtomInfo::DistanceSquared(const Vector &y) const
406{
407 return getPosition().DistanceSquared(y);
408}
409
410double AtomInfo::distance(const VectorInterface &_atom) const
411{
412 return _atom.distance(getPosition());
413}
414
415double AtomInfo::DistanceSquared(const VectorInterface &_atom) const
416{
417 return _atom.DistanceSquared(getPosition());
418}
419
420VectorInterface &AtomInfo::operator=(const Vector& _vector)
421{
422 setPosition(_vector);
423 return *this;
424}
425
426void AtomInfo::ScaleAll(const double *factor)
427{
428 Vector temp(getPosition());
429 temp.ScaleAll(factor);
430 setPosition(temp);
431}
432
433void AtomInfo::ScaleAll(const Vector &factor)
434{
435 Vector temp(getPosition());
436 temp.ScaleAll(factor);
437 setPosition(temp);
438}
439
440void AtomInfo::Scale(const double factor)
441{
442 Vector temp(getPosition());
443 temp.Scale(factor);
444 setPosition(temp);
445}
446
447void AtomInfo::Zero()
448{
449 setPosition(zeroVec);
450}
451
452void AtomInfo::One(const double one)
453{
454 setPosition(Vector(one,one,one));
455}
456
457void AtomInfo::LinearCombinationOfVectors(const Vector &x1, const Vector &x2, const Vector &x3, const double * const factors)
458{
459 Vector newPos;
460 newPos.LinearCombinationOfVectors(x1,x2,x3,factors);
461 setPosition(newPos);
462}
463
464/**
465 * returns the kinetic energy of this atom at a given time step
466 */
467double AtomInfo::getKineticEnergy(const unsigned int _step) const
468{
469 return getMass() * getAtomicVelocityAtStep(_step).NormSquared();
470}
471
472Vector AtomInfo::getMomentum(const unsigned int _step) const
473{
474 return getMass() * getAtomicVelocityAtStep(_step);
475}
476
477/** Decrease the trajectory if given \a MaxSteps is smaller.
478 * Does nothing if \a MaxSteps is larger than current size.
479 *
480 * \param MaxSteps
481 */
482void AtomInfo::ResizeTrajectory(size_t MaxSteps)
483{
484 // mind the reverse ordering due to std::greater, latest time steps are at beginning
485 VectorTrajectory_t::iterator positer = AtomicPosition.lower_bound(MaxSteps);
486 if (positer != AtomicPosition.begin()) {
487 if (positer->first == MaxSteps)
488 --positer;
489 AtomicPosition.erase(AtomicPosition.begin(), positer);
490 }
491 VectorTrajectory_t::iterator veliter = AtomicVelocity.lower_bound(MaxSteps);
492 if (veliter != AtomicVelocity.begin()) {
493 if (veliter->first == MaxSteps)
494 --veliter;
495 AtomicVelocity.erase(AtomicVelocity.begin(), veliter);
496 }
497 VectorTrajectory_t::iterator forceiter = AtomicForce.lower_bound(MaxSteps);
498 if (forceiter != AtomicForce.begin()) {
499 if (forceiter->first == MaxSteps)
500 --forceiter;
501 AtomicForce.erase(AtomicForce.begin(), forceiter);
502 }
503}
504
505size_t AtomInfo::getTrajectorySize() const
506{
507 // mind greater comp for map here: first element is latest in time steps!
508 return AtomicPosition.begin()->first+1;
509}
510
511double AtomInfo::getMass() const
512{
513 return getType()->getMass();
514}
515
516/** Helper function to either insert or assign, depending on the element being
517 * present already.
518 *
519 * \param _trajectory vector of Vectors to assign
520 * \param dest step to insert/assign to
521 * \param _newvalue new Vector value
522 */
523void assignTrajectoryElement(
524 std::map<unsigned int, Vector, std::greater<unsigned int> > &_trajectory,
525 const unsigned int dest,
526 const Vector &_newvalue)
527{
528 std::pair<std::map<unsigned int, Vector, std::greater<unsigned int> >::iterator, bool> inserter =
529 _trajectory.insert( std::make_pair(dest, _newvalue) );
530 if (!inserter.second)
531 inserter.first->second = _newvalue;
532}
533
534/** Copies a given trajectory step \a src onto another \a dest
535 * \param dest index of destination step
536 * \param src index of source step
537 */
538void AtomInfo::CopyStepOnStep(const unsigned int dest, const unsigned int src)
539{
540 if (dest == src) // self assignment check
541 return;
542
543 if (WorldTime::getTime() == dest){
544 NOTIFY(AtomObservable::PositionChanged);
545 NOTIFY(AtomObservable::VelocityChanged);
546 NOTIFY(AtomObservable::ForceChanged);
547 }
548
549 VectorTrajectory_t::iterator positer = AtomicPosition.find(src);
550 ASSERT( positer != AtomicPosition.end(),
551 "AtomInfo::CopyStepOnStep() - step "
552 +toString(src)+" to copy from not present in AtomicPosition.");
553 assignTrajectoryElement(AtomicPosition, dest, positer->second);
554 VectorTrajectory_t::iterator veliter = AtomicVelocity.find(src);
555 if (veliter != AtomicVelocity.end())
556 assignTrajectoryElement(AtomicVelocity, dest, veliter->second);
557 VectorTrajectory_t::iterator forceiter = AtomicForce.find(src);
558 if (forceiter != AtomicForce.end())
559 assignTrajectoryElement(AtomicForce, dest, forceiter->second);
560};
561
562/** Performs a velocity verlet update of the position at \a NextStep from \a LastStep information only.
563 *
564 * We calculate \f$x(t + \delta t) = x(t) + v(t)* \delta t + .5 * \delta t * \delta t * F(t)/m \f$.
565 *
566 *
567 * \param NextStep index of sequential step to set
568 * \param Deltat time step width
569 * \param IsAngstroem whether the force's underlying unit of length is angstroem or bohr radii
570 */
571void AtomInfo::VelocityVerletUpdateX(int nr, const unsigned int NextStep, double Deltat, bool IsAngstroem)
572{
573 const unsigned int LastStep = NextStep == 0 ? 0 : NextStep-1;
574
575 LOG(2, "INFO: Particle that currently " << *this);
576 LOG(2, "INFO: Integrating position with mass=" << getMass() << " and Deltat="
577 << Deltat << " at NextStep=" << NextStep);
578
579 // update position
580 {
581 Vector tempVector = getPositionAtStep(LastStep);
582 LOG(4, "INFO: initial position from last step " << setprecision(4) << tempVector);
583 tempVector += Deltat*(getAtomicVelocityAtStep(LastStep)); // s(t) = s(0) + v * deltat + 1/2 a * deltat^2
584 LOG(4, "INFO: position with velocity " << getAtomicVelocityAtStep(LastStep) << " from last step " << tempVector);
585 tempVector += .5*Deltat*Deltat*(getAtomicForceAtStep(LastStep))*(1./getMass()); // F = m * a and s =
586 LOG(4, "INFO: position with force " << getAtomicForceAtStep(LastStep) << " from last step " << tempVector);
587 setPositionAtStep(NextStep, tempVector);
588 LOG(3, "INFO: Position at step " << NextStep << " set to " << tempVector);
589 }
590};
591
592/** Performs a velocity verlet update of the velocity at \a NextStep.
593 *
594 * \note forces at NextStep should have been calculated based on position at NextStep prior
595 * to calling this function.
596 *
597 * We calculate \f$v(t) = v(t - \delta t) + \delta _t * .5 * (F(t - \delta t) + F(t))/m \f$.
598 *
599 * Parameters are according to those in configuration class.
600 * \param NextStep index of sequential step to set
601 * \param Deltat time step width
602 * \param IsAngstroem whether the force's underlying unit of length is angstroem or bohr radii
603 */
604void AtomInfo::VelocityVerletUpdateU(int nr, const unsigned int NextStep, double Deltat, bool IsAngstroem)
605{
606 const unsigned int LastStep = NextStep == 0 ? 0 : NextStep-1;
607
608 LOG(2, "INFO: Particle that currently " << *this);
609 LOG(2, "INFO: Integrating velocity with mass=" << getMass() << " and Deltat="
610 << Deltat << " at NextStep=" << NextStep);
611
612 // Update U
613 {
614 Vector tempVector = getAtomicVelocityAtStep(LastStep);
615 LOG(4, "INFO: initial velocity from last step " << tempVector);
616 tempVector += Deltat * .5*(getAtomicForceAtStep(LastStep)+getAtomicForceAtStep(NextStep))*(1./getMass()); // v = F/m * t
617 LOG(4, "INFO: Velocity with force from last " << getAtomicForceAtStep(LastStep)
618 << " and present " << getAtomicForceAtStep(NextStep) << " step " << tempVector);
619 setAtomicVelocityAtStep(NextStep, tempVector);
620 LOG(3, "INFO: Velocity at step " << NextStep << " set to " << tempVector);
621 }
622};
623
624std::ostream & AtomInfo::operator << (std::ostream &ost) const
625{
626 return (ost << getPosition());
627}
628
629std::ostream & operator << (std::ostream &ost, const AtomInfo &a)
630{
631 const size_t terminalstep = a.getTrajectorySize()-1;
632 if (terminalstep) {
633 ost << "starts at "
634 << a.getPositionAtStep(0) << " and ends at "
635 << a.getPositionAtStep(terminalstep)
636 << " at time step " << terminalstep;
637 } else {
638 ost << "is at "
639 << a.getPositionAtStep(0) << " with a single time step only";
640 }
641 return ost;
642}
643
Note: See TracBrowser for help on using the repository browser.