MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
InteractionHandler Class Reference

Container to store Interaction objects. More...

#include <InteractionHandler.h>

+ Inheritance diagram for InteractionHandler:

Public Member Functions

 InteractionHandler ()
 Default constructor, it creates an empty InteractionHandler. More...
 
 InteractionHandler (const InteractionHandler &IH)
 Copy constructor, but since interactions must not be copied, it creates an empty InteractionHandler. More...
 
InteractionHandler operator= (const InteractionHandler &rhs)
 Assignment operator. More...
 
 ~InteractionHandler ()
 Destructor, it destructs the InteractionHandler and all BaseInteraction it contains. More...
 
void addObject (BaseInteraction *I)
 Adds an Interaction to the InteractionHandler. More...
 
void readObject (std::istream &is)
 Reads an Interaction into the InteractionHandler from restart data. More...
 
BaseInteractiongetExistingInteraction (BaseInteractable *P, BaseInteractable *I)
 Returns the Interaction between the BaseInteractable's P and I if it exists, otherwise returns a null pointer. More...
 
BaseInteractiongetInteraction (BaseInteractable *P, BaseInteractable *I, Mdouble timeStamp)
 Returns the Interaction between the BaseInteractable's P and I. More...
 
void removeObjectKeepingPeriodics (unsigned const int id)
 Removes interactions of periodic particles when the periodic particles get deleted (see DPMBase::removeDuplicatePeriodicParticles) More...
 
void eraseOldInteractions (Mdouble lastTimeStep)
 erases interactions which have an old timestamp. More...
 
void write (std::ostream &os) const
 Writes the InteractionHandler to an output stream, for example a restart file. More...
 
std::string getName () const
 Returns the name of the object. More...
 
- Public Member Functions inherited from BaseHandler< BaseInteraction >
 BaseHandler ()
 Default BaseHandler constructor, it creates an empty BaseHandler and assigns DPMBase_ to a null pointer. More...
 
 BaseHandler (const BaseHandler< BaseInteraction > &BH)
 Constructor that copies the objects of the given handler into itself and sets other variables to 0/nullptr. More...
 
virtual ~BaseHandler ()
 Destructor, it destructs the BaseHandler and all Object it contains. More...
 
void copyContentsFromOtherHandler (const BaseHandler< BaseInteraction > &BH)
 Function that copies the contents (vector of pointers, maxObject_, nextId_, DPMBase_) from one handler (container) to the other. More...
 
U * copyAndAddObject (const U &O)
 Creates a copy of a Object and adds it to the BaseHandler. More...
 
U * copyAndAddObject (U *O)
 Creates a copy of a Object and adds it to the BaseHandler. More...
 
virtual void removeObject (unsigned const int id)
 Removes an Object from the BaseHandler. More...
 
void removeLastObject ()
 Removes the last Object from the BaseHandler. More...
 
void clear ()
 Empties the whole BaseHandler by removing all Objects and setting all other variables to 0. More...
 
void read (std::istream &is)
 Reads all objects from restart data. More...
 
BaseInteractiongetObjectById (const unsigned int id)
 Gets a pointer to the Object at the specified index in the BaseHandler. More...
 
BaseInteractiongetObject (const unsigned int id)
 Gets a pointer to the Object at the specified index in the BaseHandler. More...
 
const BaseInteractiongetObject (const unsigned int id) const
 Gets a constant pointer to the Object at the specified index in the BaseHandler. More...
 
BaseInteractiongetLastObject ()
 Gets a pointer to the last Object in this BaseHandler. More...
 
const BaseInteractiongetLastObject () const
 Gets a constant pointer to the last Object in this BaseHandler. More...
 
unsigned int getNumberOfObjects () const
 Gets the number of Object in this BaseHandler. More...
 
unsigned int getStorageCapacity () const
 Gets the storage capacity of this BaseHandler. More...
 
void setStorageCapacity (const unsigned int N)
 Sets the storage capacity of this BaseHandler. More...
 
const std::vector
< BaseInteraction * >
::const_iterator 
begin () const
 Gets the begin of the const_iterator over all Object in this BaseHandler. More...
 
const std::vector
< BaseInteraction * >
::iterator 
begin ()
 Gets the begin of the iterator over all BaseBoundary in this BaseHandler. More...
 
const std::vector
< BaseInteraction * >
::const_iterator 
end () const
 Gets the end of the const_iterator over all BaseBoundary in this BaseHandler. More...
 
const std::vector
< BaseInteraction * >
::iterator 
end ()
 Gets the end of the iterator over all BaseBoundary in this BaseHandler. More...
 
void setDPMBase (DPMBase *DPMBase)
 Sets the problem that is solved using this handler. More...
 
DPMBasegetDPMBase ()
 Gets the problem that is solved using this handler. More...
 
DPMBasegetDPMBase () const
 Gets the problem that is solved using this handler and does not change the class. More...
 

Additional Inherited Members

- Protected Attributes inherited from BaseHandler< BaseInteraction >
std::vector< BaseInteraction * > objects_
 The actual list of Object pointers. More...
 

Detailed Description

Container to store Interaction objects.

The InteractionHandler is a container to store all Interaction objects. It is implemented as a vector of BaseInteraction pointers.

Definition at line 36 of file InteractionHandler.h.

Constructor & Destructor Documentation

InteractionHandler::InteractionHandler ( )

Default constructor, it creates an empty InteractionHandler.

Constructor of the ParticleHandler class. It creates and empty ParticleHandler.

Definition at line 39 of file InteractionHandler.cc.

References DEBUG, and logger.

40 {
41  logger(DEBUG, "InteractionHandler::InteractionHandler() finished");
42 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
InteractionHandler::InteractionHandler ( const InteractionHandler IH)

Copy constructor, but since interactions must not be copied, it creates an empty InteractionHandler.

InteractionHandler::~InteractionHandler ( )

Destructor, it destructs the InteractionHandler and all BaseInteraction it contains.

Definition at line 72 of file InteractionHandler.cc.

References DEBUG, and logger.

73 {
74  logger(DEBUG, "InteractionHandler::~InteractionHandler() finished");
75 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")

Member Function Documentation

void InteractionHandler::addObject ( BaseInteraction I)
virtual

Adds an Interaction to the InteractionHandler.

Parameters
[in]PA pointer to the BaseInteraction (or derived class) that has to be added.

Reimplemented from BaseHandler< BaseInteraction >.

Definition at line 80 of file InteractionHandler.cc.

References BaseHandler< T >::addObject(), and BaseInteraction::setHandler().

Referenced by BaseInteraction::copySwitchPointer(), and getInteraction().

81 {
82  //Puts the particle in the Particle list
84  //set the particleHandler pointer
85  I->setHandler(this);
86 }
void setHandler(InteractionHandler *handler)
Sets the pointer to the interaction hander which is storing this interaction.
virtual void addObject(T *O)
Adds a new Object to the BaseHandler.
Definition: BaseHandler.h:284
void InteractionHandler::eraseOldInteractions ( Mdouble  lastTimeStep)

erases interactions which have an old timestamp.

Each interaction contains a time stamp, which stores the last time that an interaction object has been called. Thus, one can see that an interaction has ended by comparing the time stamp with the last value of DPMBase::time_. This function erases all interactions that have ended.

Parameters
[in]lastTimeStepthe last used value of DPMBase::time_.
Todo:
TW: this function has to be sped up with sth like this: erase(remove_if(begin(), end(), bind2nd(checkSpringTime(), lastTimeStep)), end());

Definition at line 212 of file InteractionHandler.cc.

References BaseHandler< BaseInteraction >::getNumberOfObjects(), BaseHandler< BaseInteraction >::getObject(), BaseInteraction::getTimeStamp(), and BaseHandler< BaseInteraction >::removeObject().

Referenced by DPMBase::solve().

213 {
214  //std::cout<<"void InteractionHandler::eraseOldInteractions(Mdouble lastTimeStep)"<<std::endl;
215  //std::cout<<"Current interactions="<<getNumberOfObjects()<<std::endl;
216  //Remove_if reconstructs the vector with only elements passing the check_spring_time function
217  //Erase removes the end of the vector
219  for (unsigned int id = 0; id < getNumberOfObjects(); id++)
220  {
221  if (getObject(id)->getTimeStamp() < lastTimeStep)
222  {
223  removeObject(id);
224  --id;
225  }
226  }
227 }
Mdouble getTimeStamp() const
Returns an Mdouble which is the time stamp of the interaction.
virtual void removeObject(unsigned const int id)
Removes an Object from the BaseHandler.
BaseInteraction * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
unsigned int getNumberOfObjects() const
Gets the number of Object in this BaseHandler.
BaseInteraction * InteractionHandler::getExistingInteraction ( BaseInteractable P,
BaseInteractable I 
)

Returns the Interaction between the BaseInteractable's P and I if it exists, otherwise returns a null pointer.

Parameters
[in]Pthe first BaseInteractable by which the interaction is defined.
[in]Ithe first BaseInteractable by which the interaction is defined.
Returns
the Interaction between the BaseInteractable's P and I, if it exists.

Definition at line 93 of file InteractionHandler.cc.

References BaseInteractable::getInteractions().

Referenced by getInteraction(), and removeObjectKeepingPeriodics().

94 {
95  //for particle-particle collision it is assumed BaseInteractable P has a lower index then I, so we only have to check for I, not P
96  for (std::list<BaseInteraction*>::const_iterator it = P->getInteractions().begin(); it != P->getInteractions().end(); ++it)
97  {
98  if ((*it)->getI() == I)
99  {
100  return *it;
101  }
102  }
103  return nullptr;
104 }
const std::list< BaseInteraction * > & getInteractions() const
Returns a reference to the list of interactions in this BaseInteractable.
BaseInteraction * InteractionHandler::getInteraction ( BaseInteractable P,
BaseInteractable I,
Mdouble  timeStamp 
)

Returns the Interaction between the BaseInteractable's P and I.

Returns a pointer to the existing Interaction, if the Interaction already exists otherwise creates a new Interaction and returns a pointer to it.

Parameters
[in]Pthe first BaseInteractable by which the interaction is defined.
[in]Ithe first BaseInteractable by which the interaction is defined.
[in]timeStampthe current value of DPMBase::time_.
Returns
the Interaction between the BaseInteractable's P and I

Definition at line 114 of file InteractionHandler.cc.

References addObject(), BaseHandler< BaseInteraction >::getDPMBase(), getExistingInteraction(), BaseInteractable::getIndSpecies(), SpeciesHandler::getMixedObject(), BaseSpecies::getNewInteraction(), BaseInteraction::setSpecies(), BaseInteraction::setTimeStamp(), and DPMBase::speciesHandler.

Referenced by Coil::getInteractionWith(), AxisymmetricIntersectionOfWalls::getInteractionWith(), Screw::getInteractionWith(), CylindricalWall::getInteractionWith(), InfiniteWallWithHole::getInteractionWith(), InfiniteWall::getInteractionWith(), IntersectionOfWalls::getInteractionWith(), BaseParticle::getInteractionWith(), and readObject().

115 {
117 
118  //std::cout << "Trying to reconnect to BaseInteraction between P=" << P->getId() << " and " << I->getId() << std::endl;
120  if (!C)
121  {
122  C = species->getNewInteraction(P, I, timeStamp);
123  addObject(C);
124  //std::cout << "Creating new interaction with index=" << getLastObject()->getIndex() << " id=" << getLastObject()->getId() << std::endl;
125  }
126 
127  //set timeStamp
128  C->setTimeStamp(timeStamp);
129 
130  //set species of collision
131  C->setSpecies(species);
132 
133  return C;
134 }
BaseSpecies is the class from which all other species are derived.
Definition: BaseSpecies.h:44
virtual BaseInteraction * getNewInteraction(BaseInteractable *P, BaseInteractable *I, Mdouble timeStamp)=0
returns new Interaction object.
void setSpecies(BaseSpecies *species)
Set the Species of the interaction; note this can either be a Species or MixedSpecies.
Stores information about interactions between two interactable objects; often particles but could be ...
void setTimeStamp(Mdouble timeStamp)
Updates the time step of the interacting. Note, timesteps used to find completed interactions.
BaseInteraction * getExistingInteraction(BaseInteractable *P, BaseInteractable *I)
Returns the Interaction between the BaseInteractable's P and I if it exists, otherwise returns a null...
SpeciesHandler speciesHandler
A handler to that stores the species type i.e. elastic, linear visco-elastic... et cetera...
Definition: DPMBase.h:868
U::MixedSpeciesType * getMixedObject(const U *S, const U *T)
unsigned int getIndSpecies() const
Returns the index of the Species of this BaseInteractable.
DPMBase * getDPMBase()
Gets the problem that is solved using this handler.
void addObject(BaseInteraction *I)
Adds an Interaction to the InteractionHandler.
std::string InteractionHandler::getName ( ) const
virtual

Returns the name of the object.

Returns
the string InteractionHandler

Implements BaseHandler< BaseInteraction >.

Definition at line 232 of file InteractionHandler.cc.

233 {
234  return "InteractionHandler";
235 }
InteractionHandler InteractionHandler::operator= ( const InteractionHandler rhs)

Assignment operator.

Parameters
[in]rhsThe BoundaryHandler on the right hand side of the assignment.

Definition at line 62 of file InteractionHandler.cc.

References BaseHandler< BaseInteraction >::clear(), DEBUG, and logger.

63 {
64  if (this != &rhs)
65  {
66  clear();
67  }
68  logger(DEBUG, "InteractionHandler::operator =(const InteractionHandler& rhs) finished.");
69  return *this;
70 }
Logger< MERCURY_LOGLEVEL > logger("MercuryKernel")
void clear()
Empties the whole BaseHandler by removing all Objects and setting all other variables to 0...
void InteractionHandler::readObject ( std::istream &  is)
virtual

Reads an Interaction into the InteractionHandler from restart data.

Parameters
[in]isThe input stream from which the information is read.
Todo:
TW: Change identifier in restart file from id to index; is there any reason the id should be kept after restarting, once this is done? (Note, the id is set to the old one in the particle handler because interactions store id, not indices; also note id's are slow

Implements BaseHandler< BaseInteraction >.

Definition at line 251 of file InteractionHandler.cc.

References BaseHandler< BaseInteraction >::getDPMBase(), getInteraction(), helpers::getLineFromStringStream(), BaseHandler< T >::getObjectById(), DPMBase::particleHandler, and DPMBase::wallHandler.

252 {
253  std::string type, dummy, idType;
254  unsigned int id0, id1;
255  Mdouble timeStamp;
256 
257  std::stringstream line(std::stringstream::in | std::stringstream::out);
259  line >> type >> idType >> id0 >> id1 >> dummy >> timeStamp;
261  BaseInteraction* C;
262  if (idType.compare("particleIds") == 0)
264  else
265  C = getInteraction(getDPMBase()->particleHandler.getObjectById(id0), getDPMBase()->wallHandler.getObjectById(id1), timeStamp);
266  line >> (*C);
267 }
T * getObjectById(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
Definition: BaseHandler.h:394
BaseInteraction * getInteraction(BaseInteractable *P, BaseInteractable *I, Mdouble timeStamp)
Returns the Interaction between the BaseInteractable's P and I.
double Mdouble
Stores information about interactions between two interactable objects; often particles but could be ...
void getLineFromStringStream(std::istream &in, std::stringstream &out)
Reads a line from one stringstream into another, and prepares the latter for reading in...
Definition: Helpers.cc:389
ParticleHandler particleHandler
An object of the class ParticleHandler, contains the pointers to all the particles created...
Definition: DPMBase.h:878
WallHandler wallHandler
An object of the class WallHandler. Contains pointers to all the walls created.
Definition: DPMBase.h:883
DPMBase * getDPMBase()
Gets the problem that is solved using this handler.
void InteractionHandler::removeObjectKeepingPeriodics ( unsigned const int  id)

Removes interactions of periodic particles when the periodic particles get deleted (see DPMBase::removeDuplicatePeriodicParticles)

Deleting the three periodic interactions between two real particles is difficult, because its interaction information has to be saved. If the two real particles interacted (which can be checked by looking at the time stamp), the interaction between the real particles is kept, and all interactions that involve ghost particles gets removed; otherwise, the interaction between the lower-indexed real particle with the ghost particle of the higher indexed particles is saved (with the ghost particle replaced by the real particle), and all other interactions removed.

This is what this function is intended for, and it does it in the following way: When an interaction is removed the periodic particle has to be stored in the I pointer So when an interaction is removed where P is normal and I is periodic, and the information is new it will be transfered when the index of P is lower than the index of the real particle of I.

periodicBoundary.pdf
Parameters
[in]theid of the Interaction that needs to be deleted.
Todo:
this function will create an error if the timeStamp is in the future! This should not happen (ever), but who knows.

Definition at line 154 of file InteractionHandler.cc.

References getExistingInteraction(), BaseInteraction::getI(), BaseObject::getIndex(), BaseHandler< BaseInteraction >::getObject(), BaseInteraction::getP(), BaseParticle::getPeriodicFromParticle(), BaseInteraction::getTimeStamp(), BaseHandler< T >::removeObject(), BaseHandler< BaseInteraction >::removeObject(), and BaseInteraction::setI().

Referenced by DPMBase::removeDuplicatePeriodicParticles().

155 {
156  BaseInteraction* iMain = getObject(id);
157 
158  BaseParticle* P = dynamic_cast<BaseParticle*>(iMain->getP());
159  BaseParticle* I = dynamic_cast<BaseParticle*>(iMain->getI());
160  if (P && I)
161  {
162  //std::cout<<"Both are particles"<<std::endl;
164  if (realI && !P->getPeriodicFromParticle())
165  {
166  //std::cout << "P is normal and I periodic P=" << P->getId() << " I=" << I->getId() << " I real=" << I->getPeriodicFromParticle()->getId() << std::endl;
167  if (P->getIndex() < realI->getIndex())
168  {
169  //std::cout << "Reconnecting to spring" << std::endl;
170  BaseInteraction* iOther = getExistingInteraction(P, realI);
171  if (iOther)
172  {
173  //Here we decide which of the two interactions should be kept:
174  //the interaction between the two real particles (iMain), or
175  //the interaction between the real and a ghost particle (iOther).
176  //It picks the one for which a collision has happened,
177  //i.e. the one with the newer timeStamp.
179  if (iOther->getTimeStamp() < iMain->getTimeStamp())
180  {
181  //std::cout << "Switching" << std::endl;
182  iMain->setI(realI);
183  removeObject(iOther->getIndex());
184  return;
185  }
186  else
187  {
188  //std::cout << "Not switching" << std::endl;
190  return;
191  }
192  }
193  else
194  {
195  //std::cout << "Not found, so just move" << std::endl;
196  iMain->setI(realI);
197  return;
198  }
199  }
200  }
201  }
203 }
unsigned int getIndex() const
Returns the index of the object in the handler.
Definition: BaseObject.cc:106
void setI(BaseInteractable *I)
Sets the second object involved in the interaction (often particle or wall).
Mdouble getTimeStamp() const
Returns an Mdouble which is the time stamp of the interaction.
Stores information about interactions between two interactable objects; often particles but could be ...
BaseParticle * getPeriodicFromParticle() const
Returns the 'original' particle this one's a periodic copy of.
BaseInteraction * getExistingInteraction(BaseInteractable *P, BaseInteractable *I)
Returns the Interaction between the BaseInteractable's P and I if it exists, otherwise returns a null...
virtual void removeObject(unsigned const int id)
Removes an Object from the BaseHandler.
BaseInteraction * getObject(const unsigned int id)
Gets a pointer to the Object at the specified index in the BaseHandler.
BaseInteractable * getI()
Defines the basic properties that a interactable object can have.
BaseInteractable * getP()
Returns a pointer to first object involved in the interaction (normally a particle).
void InteractionHandler::write ( std::ostream &  os) const

Writes the InteractionHandler to an output stream, for example a restart file.

Parameters
[in]osThe output stream where the InteractionHandler must be written to, usually a restart file.

Definition at line 241 of file InteractionHandler.cc.

References BaseHandler< BaseInteraction >::getNumberOfObjects().

Referenced by DPMBase::write().

242 {
243  os << "Interactions " << getNumberOfObjects() << std::endl;
244  for (BaseInteraction* i : *this)
245  os << (*i) << std::endl;
246 }
Stores information about interactions between two interactable objects; often particles but could be ...
unsigned int getNumberOfObjects() const
Gets the number of Object in this BaseHandler.

The documentation for this class was generated from the following files: