MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseInteraction.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2014, The MercuryDPM Developers Team. All rights reserved.
2 //For the list of developers, see <http://www.MercuryDPM.org/Team>.
3 //
4 //Redistribution and use in source and binary forms, with or without
5 //modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name MercuryDPM nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 //DISCLAIMED. IN NO EVENT SHALL THE MERCURYDPM DEVELOPERS TEAM BE LIABLE FOR ANY
19 //DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 //(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 //ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 //(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 //SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 
26 
27 #include "BaseInteraction.h"
28 #include "InteractionHandler.h"
29 #include "Particles/BaseParticle.h"
30 #include "Species/BaseSpecies.h"
31 #include "DPMBase.h"
32 #include<iomanip>
33 #include<fstream>
34 
45  : BaseObject()
46 {
47  P_ = P;
48  P->addInteraction(this);
49  I_ = I;
50  I->addInteraction(this);
51  normal_.setZero();
52  overlap_ = 0;
53  timeStamp_ = timeStamp;
54  species_ = 0;
55  force_.setZero();
56  torque_.setZero();
57 #ifdef DEBUG_CONSTRUCTOR
58  std::cout<<"BaseInteraction::BaseInteraction() finished"<<std::endl;
59 #endif
60 }
61 
67  : BaseObject(p)
68 {
69  P_ = p.P_;
70  I_ = p.I_;
71  normal_ = p.normal_;
72  overlap_ = p.overlap_;
73  force_ = p.force_;
74  torque_ = p.torque_;
75  species_ = p.species_;
77 }
78 
84 {
85  P_->removeInteraction(this);
86  I_->removeInteraction(this);
87 }
88 
97 void BaseInteraction::write(std::ostream& os) const
98 {
99  os << getName();
100  if (dynamic_cast<BaseParticle*>(I_) != nullptr)
101  os << " particleIds " << P_->getId() << " " << I_->getId();
103  else
104  os << " particleWallIds " << P_->getId() << " " << I_->getId();
105  os <<" timeStamp "<<timeStamp_<< " force " << force_ << " torque " << torque_;
106 }
107 
115 void BaseInteraction::read(std::istream& is)
116 {
117  //the rest gets read by the interaction handler
118  std::string dummy;
119  is >> dummy >> force_ >> dummy >> torque_;
120 }
121 
127 std::string BaseInteraction::getName() const
128 {
129  return "BaseInteraction";
130 }
131 
138 {
139  normal_ = normal;
140 }
141 
147 {
148  distance_ = distance;
149 }
150 
157 {
158  overlap_ = overlap;
159 }
160 
161 /*
162  * \details set the contact point between the two interactable objects involved
163  * \param[in] contactPoint Vec3D vector which will become the new contact point.
164  */
166 {
167  contactPoint_ = contactPoint;
168 }
169 
177 {
178  timeStamp_ = timeStamp;
179 }
180 
187 {
188  handler_ = handler;
189 }
190 
197 {
198  return handler_;
199 }
200 
206 {
208 }
209 
225 {
226  //Copy the interaction of ghost
227  BaseInteraction* C = copy();
228  //Finds out which of P and I is that the particle from whom the ghost is begin created.
229  //The object being interacted with is set to P
230  if (C->getP() == original)
231  {
232  //Reverse some force history
233  C->reverseHistory();
234  //Set the P to the original particle
235  C->P_ =C->getI();
236  }
237  //The new ghost particle is set to I in the interaction.
238  C->I_ = ghost;
239 
240  //Add the the interaction to both original and the ghost
241  C->getP()->addInteraction(C);
242  C->getI()->addInteraction(C);
243  handler_->addObject(C);
244 }
245 
252 {
253  return force_;
254 }
255 
262 {
263  return torque_;
264 }
265 
272 {
273  return normal_;
274 }
275 
283 {
284  return contactPoint_;
285 }
286 
293 {
294  return overlap_;
295 }
296 
303 {
304  return P_;
305 }
306 
313 {
314  return I_;
315 }
316 
323 {
324  return P_;
325 }
326 
333 {
334  return I_;
335 }
336 
344 {
345  return timeStamp_;
346 }
347 
360 {
361 
362 }
363 
372 {
373  species_ = species;
374 }
375 
385 {
386  P_->removeInteraction(this);
387  P_=P;
388  P_->addInteraction(this);
389 }
390 
400 {
401  I_->removeInteraction(this);
402  I_=I;
403  I_->addInteraction(this);
404 }
405 
415 void BaseInteraction::writeToFStat(std::ostream& os) const
416 {
417  BaseParticle* IParticle = dynamic_cast<BaseParticle*>(I_);
418  BaseParticle* PParticle = dynamic_cast<BaseParticle*>(P_);
419 
420  Vec3D tangentialForce = getTangentialForce();
421  Mdouble tangentialOverlap = getTangentialOverlap();
422 
423  Mdouble scalarNormalForce = Vec3D::dot(force_, getNormal());
424  Mdouble scalarTangentialForce = tangentialForce.getLength();
425  Vec3D tangential;
426  if (scalarTangentialForce!=0.0)
427  tangential = tangentialForce/scalarTangentialForce;
428  else
429  tangential = Vec3D(0.0,0.0,0.0);
430 
433  Vec3D centre;
434  if (IParticle!=0)
435  centre = 0.5 * (getP()->getPosition() + getI()->getPosition());
436  else
437  centre = getP()->getPosition() - normal_ * (PParticle->getRadius() - overlap_);
438 
439  if (PParticle!=0 && !PParticle->isFixed())
440  {
441  os << timeStamp_
442  << " " << P_->getIndex()
443  << " " << static_cast<int>((IParticle==0?(-I_->getIndex()-1):I_->getIndex()))
444  << " " << centre
445  << " " << getOverlap()
446  << " " << tangentialOverlap
447  << " " << scalarNormalForce
448  << " " << scalarTangentialForce
449  << " " << (IParticle==0?-normal_:normal_)
450  << " " << (IParticle==0?-tangential:tangential) << std::endl;
452  }
453  if (IParticle!=0 && !IParticle->isFixed() && IParticle->getPeriodicFromParticle()==0)
454  {
455  os << timeStamp_
456  << " " << I_->getIndex()
457  << " " << P_->getIndex()
458  << " " << centre
459  << " " << getOverlap()
460  << " " << tangentialOverlap
461  << " " << scalarNormalForce
462  << " " << scalarTangentialForce
463  << " " << -normal_
464  << " " << -tangential << std::endl;
465  }
466 
467 }
468 
469 /*
470  * \details Returns the distance between the two interactable objects involved
471  * in the interaction.
472  * \return Mdouble which is the distance between the two interacting objects.
473  */
475 {
476  return distance_;
477 }
478 
487 {
488  return 0;
489 }
490 
499 {
500  return Vec3D(0.0,0.0,0.0);
501 }
502 
509 {
510  return relativeVelocity_;
511 }
512 
520 {
522 }
523 
532 {
533  return absoluteNormalForce_;
534 }
535 
542 {
543  force_+=force;
544 }
545 
552 {
553  torque_+=torque;
554 }
555 
563 {
564  force_=force;
565 }
566 
574 {
575  torque_=torque;
576 }
577 
585 {
586  relativeVelocity_=relativeVelocity;
587 }
588 
596 {
597  normalRelativeVelocity_=normalRelativeVelocity;
598 }
599 
606 {
607  absoluteNormalForce_ = absoluteNormalForce;
608 }
609 
618 {
619  return species_;
620 }
621 
630 {}
631 
639 {
640  return 0.0;
641 }
642 
654 {
655 }
656 
658 {
659  contactPoint_=rotationMatrix*contactPoint_;
660  relativeVelocity_=rotationMatrix*relativeVelocity_;
661  force_=rotationMatrix*force_;
662  torque_=rotationMatrix*torque_;
663  normal_=rotationMatrix*normal_;
665 }
666 
677 {
678  const BaseParticle* PParticle = dynamic_cast<const BaseParticle*>(getP());
679  const BaseParticle* IParticle = dynamic_cast<const BaseParticle*>(getI());
680  if (PParticle==nullptr)
681  {
682  std::cerr << "BaseInteraction::getEffectiveCorrectedRadius(): first interactable P is not a particle" << std::endl;
683  exit(-1);
684  }
685  //Compute the reduced diameter
686  if (IParticle==nullptr) //if particle-wall
687  {
688  return PParticle->getRadius();
689  }
690  else
691  {
692  Mdouble radiusP = PParticle->getRadius();
693  Mdouble radiusI = IParticle->getRadius();
694  return radiusP * radiusI / (radiusP + radiusI);
695  }
696 }
697 
708 {
709  BaseParticle* PParticle = dynamic_cast<BaseParticle*>(getP());
710  BaseParticle* IParticle = dynamic_cast<BaseParticle*>(getI());
711  if (PParticle==nullptr)
712  {
713  std::cerr << "BaseInteraction::getEffectiveCorrectedRadius(): first interactable P is not a particle" << std::endl;
714  exit(-1);
715  }
716  //Compute the reduced diameter
717  if (IParticle==nullptr) //if particle-wall
718  {
719  return PParticle->getRadius() - 0.5*getOverlap();
720  }
721  else
722  {
723  Mdouble radiusP = PParticle->getRadius() - 0.5*getOverlap();
724  Mdouble radiusI = IParticle->getRadius() - 0.5*getOverlap();
725  return radiusP * radiusI / (radiusP + radiusI);
726  }
727 }
728 
733 {
734  BaseParticle* IParticle = dynamic_cast<BaseParticle*>(I_);
735  BaseParticle* PParticle = dynamic_cast<BaseParticle*>(P_);
736 
737  Vec3D tangentialForce = getTangentialForce();
738  Mdouble tangentialOverlap = getTangentialOverlap();
739 
740  Mdouble scalarNormalForce = Vec3D::dot(force_, getNormal());
741  Mdouble scalarTangentialForce = tangentialForce.getLength();
742  Vec3D tangential;
743  if (scalarTangentialForce!=0.0)
744  tangential = tangentialForce/scalarTangentialForce;
745  else
746  tangential = Vec3D(0.0,0.0,0.0);
747 
750  Vec3D centre;
751  if (IParticle!=0)
752  centre = 0.5 * (getP()->getPosition() + getI()->getPosition());
753  else
754  centre = getP()->getPosition() - normal_ * (PParticle->getRadius() - overlap_);
755 
756  if (PParticle!=0 && !PParticle->isFixed())
757  {
759  P_->getIndex(),
760  static_cast<int>((IParticle==0?(-I_->getIndex()-1):I_->getIndex())),
761  centre,
762  getOverlap(),
763  tangentialOverlap,
764  scalarNormalForce,
765  scalarTangentialForce,
766  (IParticle==0?-normal_:normal_),
767  (IParticle==0?-tangential:tangential));
768  }
769  if (IParticle!=0 && !IParticle->isFixed() && IParticle->getPeriodicFromParticle()==0)
770  {
772  I_->getIndex(),
773  static_cast<int>(P_->getIndex()),
774  centre,
775  getOverlap(),
776  tangentialOverlap,
777  scalarNormalForce,
778  scalarTangentialForce,
779  -normal_,
780  -tangential);
781 
782  }
783 }
const Vec3D & getForce() const
Gets the current force (vector) between the two interacting objects.
BaseInteractable * I_
unsigned int getId() const
Returns the unique identifier of any particular object.
Definition: BaseObject.cc:113
unsigned int getIndex() const
Returns the index of the object in the handler.
Definition: BaseObject.cc:106
Mdouble getEffectiveRadius() const
Returns a Mdouble to the effective radius of the interaction. (Not corrected for the overlap) ...
void setNormal(Vec3D normal)
Sets the normal vector between the two interacting objects.
void copySwitchPointer(const BaseInteractable *original, BaseInteractable *ghost) const
This copies the interactions of the original particle and replaces the original with the ghost copy...
virtual void gatherContactStatistics(unsigned int index1 UNUSED, int index2 UNUSED, Vec3D Contact UNUSED, Mdouble delta UNUSED, Mdouble ctheta UNUSED, Mdouble fdotn UNUSED, Mdouble fdott UNUSED, Vec3D P1_P2_normal_ UNUSED, Vec3D P1_P2_tangential UNUSED)
//Not unsigned index because of possible wall collisions.
Definition: DPMBase.cc:696
BaseSpecies is the class from which all other species are derived.
Definition: BaseSpecies.h:44
InteractionHandler * getHandler() const
Gets a point to the interaction handlers to which this interaction belongs.
void setOverlap(Mdouble overlap)
Set the overlap between the two interacting object.
virtual BaseInteraction * copy() const =0
Makes a copy of the interaction and returns a pointer to the copy.
bool removeInteraction(BaseInteraction *I)
Removes an interaction from this BaseInteractable.
void addInteraction(BaseInteraction *I)
Adds an interaction to this BaseInteractable.
BaseSpecies * species_
void setI(BaseInteractable *I)
Sets the second object involved in the interaction (often particle or wall).
It is an abstract base class due to the purely virtual functions declared below. Even if the function...
Definition: BaseObject.h:49
const Vec3D & getRelativeVelocity() const
Returns a constant reference to a vector of relative velocity.
double Mdouble
virtual void reverseHistory()
When periodic particles some interaction need certain history properties reversing. This is the function for that.
void setRelativeVelocity(Vec3D relativeVelocity)
set the relative velocity of the current of the interactions.
void setZero()
Sets all elements to zero.
Definition: Vector.cc:52
void setContactPoint(Vec3D contactPoint)
Set the location of the contact point between the two interacting objects.
void addTorque(Vec3D torque)
void removeFromHandler()
Removes this interaction from its interaction hander.
void setForce(Vec3D force)
set total force (this is used by the normal force, tangential forces are added use addForce) ...
static Mdouble dot(const Vec3D &a, const Vec3D &b)
Calculates the dot product of two Vec3D: .
Definition: Vector.cc:187
void setNormalRelativeVelocity(Mdouble normalRelativeVelocit)
set the normal component of the relative velocity.
void setDistance(Mdouble distance)
Sets the interaction distance between the two interacting objects.
Mdouble getTimeStamp() const
Returns an Mdouble which is the time stamp of the interaction.
void setSpecies(BaseSpecies *species)
Set the Species of the interaction; note this can either be a Species or MixedSpecies.
void gatherContactStatistics()
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Stores information about interactions between two interactable objects; often particles but could be ...
static Mdouble getLength(const Vec3D &a)
Calculates the length of a Vec3D: .
Definition: Vector.cc:427
BaseParticle * getPeriodicFromParticle() const
Returns the 'original' particle this one's a periodic copy of.
BaseInteractable * P_
const Vec3D & getContactPoint() const
Gets constant reference to contact point (vector).
Mdouble getNormalRelativeVelocity() const
Returns a double which is the norm (length) of the relative velocity vector.
virtual ~BaseInteraction()
The default destructor.
BaseInteraction(BaseInteractable *P, BaseInteractable *I, Mdouble timeStamp)
A constructor takes the BaseInteractable objects which are interacting (come into contact) and time t...
void setTimeStamp(Mdouble timeStamp)
Updates the time step of the interacting. Note, timesteps used to find completed interactions.
virtual void rotateHistory(Matrix3D &rotationMatrix)
When periodic particles are used, some interactions need certain history properties rotated (e...
const BaseSpecies * getBaseSpecies() const
Return a constant point to BaseSpecies of the interaction.
virtual void removeObject(unsigned const int id)
Removes an Object from the BaseHandler.
Definition: BaseHandler.h:303
Container to store Interaction objects.
const Vec3D & getNormal() const
Gets the normal vector between the two interacting objects.
void setHandler(InteractionHandler *handler)
Sets the pointer to the interaction hander which is storing this interaction.
void setP(BaseInteractable *P)
Sets the first object involved in the interaction (normally a particle).
#define UNUSED
Definition: GeneralDefine.h:37
void setTorque(Vec3D torque)
set the total force (this is used by the normal force, tangential torques are added use addTorque) ...
virtual const Vec3D getTangentialForce() const
Mdouble getRadius() const
Returns the particle's radius_.
Mdouble getOverlap() const
Returns a Mdouble with the current overlap between the two interacting objects.
void addForce(Vec3D force)
add an force increment to the total force.
void setAbsoluteNormalForce(Mdouble absoluteNormalForce)
the absolute values of the norm (length) of the normal force
Mdouble normalRelativeVelocity_
BaseInteractable * getI()
Mdouble getDistance() const
Returns an Mdouble which is the norm (length) of distance vector.
virtual void integrate(Mdouble timeStep)
integrates variables of the interaction which need to be integrate e.g. the tangential overlap...
virtual std::string getName() const
Virtual function which allows interactions to be named.
const Vec3D & getTorque() const
Gets the current torque (vector) between the two interacting objects.
virtual void read(std::istream &is)
Interaction read function, which accepts an std::istream as input.
Defines the basic properties that a interactable object can have.
virtual Mdouble getElasticEnergy() const
Returns a Mdouble which is the current about of Elastic energy in the interaction.
Mdouble absoluteNormalForce_
virtual void computeForce()
Virtual function that contains the force law between the two objects interacting. ...
BaseInteractable * getP()
Returns a pointer to first object involved in the interaction (normally a particle).
Implementation of a 3D matrix.
Definition: Matrix.h:36
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
DPMBase * getDPMBase()
Gets the problem that is solved using this handler.
Definition: BaseHandler.h:512
void addObject(BaseInteraction *I)
Adds an Interaction to the InteractionHandler.
virtual Mdouble getTangentialOverlap() const
get the length of the current tangential overlap
virtual void write(std::ostream &os) const
Interaction print function, which accepts an std::ostream as input.
bool isFixed() const
Is fixed Particle function. It returns whether a Particle is fixed or not, by checking its inverse Ma...
Mdouble getEffectiveCorrectedRadius()
Returns a Mdouble to the effective radius corrected for the overlaps of the particles.
void writeToFStat(std::ostream &os) const
Writes forces data to the FStat file.
InteractionHandler * handler_
Mdouble getAbsoluteNormalForce() const
Returns the absolute value of the norm (length) of the Normal force vector.