BaseInteractable.h
Go to the documentation of this file.
1 // This file is part of the MercuryDPM project (https://www.mercurydpm.org).
2 // Copyright (c), The MercuryDPM Developers Team. All rights reserved.
3 // License: BSD 3-Clause License; see the LICENSE file in the root directory.
4 
5 #ifndef BASEINTERACTABLE_H
6 #define BASEINTERACTABLE_H
7 
8 #include <functional>
9 #include <vector>
10 #include "BaseObject.h"
11 #include "Math/Vector.h"
12 #include "Math/Quaternion.h"
13 
14 class BaseParticle;
15 
16 class ParticleSpecies;
17 
18 class BaseInteraction;
19 
20 class InteractionHandler;
21 
34 {
35 public:
40 
45 
50  ~BaseInteractable() override;
51 
55  void read(std::istream& is) override;
56 
61  void write(std::ostream& os) const override;
62 
67  unsigned int getIndSpecies() const
68  { return indSpecies_; }
69 
77  virtual void setIndSpecies(unsigned int indSpecies)
78  { indSpecies_ = indSpecies; }
79 
87  const ParticleSpecies* getSpecies() const
88  {
89  return species_;
90  }
91 
95  void setSpecies(const ParticleSpecies* species);
96 
105  const Vec3D& getForce() const
106  { return force_; }
107 
117  const Vec3D& getTorque() const
118  { return torque_; }
119 
128  void setForce(const Vec3D& force)
129  { force_ = force; }
130 
140  void setTorque(const Vec3D& torque)
141  { torque_ = torque; }
142 
150  void addForce(const Vec3D& addForce);
151 
159  void addTorque(const Vec3D& addTorque);
160 
161  /*
162  * This function sets the forces and torques acting on an interactable (particle/wall) to zero at teh beginning of computeOneTimeStep.
163  *
164  * For omp simulations, it further sets does the first step of summing up all forces (and torques) acting on an interactable.
165  * It is step 1 of the add-by-reduction approach:
166  * 1. Declare a vector forceOMP_, one element per thread, and set it to zero.
167  * 2. When a new force needs to be added to the interactable, don't add it immediately.
168  * Add it to forceOMP_[threadNum] instead
169  * 3. Add all forces in forceOMP_ together to force_.
170  * This reduction approach allows step 2 to be run in parallel, while step 3 needs to be done in sequence.
171  * It is efficient, since step 3 is much quicker than step 2.
172  */
173  virtual void resetForceTorque(int numberOfOMPthreads);
174 
175  /*
176  * This function does the last step of summing up all forces (and torques) acting on an interactable.
177  * It is step 3 of the add-by-reduction approach:
178  * 1. Declare a vector forceOMP_, one element per thread, and set it to zero.
179  * 2. When a new force needs to be added to the interactable, don't add it immediately.
180  * Add it to forceOMP_[threadNum] instead
181  * 3. Add all forces in forceOMP_ together to force_.
182  * This reduction approach allows step 2 to be run in parallel, while step 3 needs to be done in sequence.
183  * It is efficient, since step 3 is much quicker than step 2.
184  */
185  void sumForceTorqueOMP();
186 
197  const Vec3D& getPosition() const
198  { return position_; }
199 
209  const Quaternion& getOrientation() const
210  { return orientation_; }
211 
218  virtual void setPosition(const Vec3D& position)
219  { position_ = position; }
220 
225 
229  void setOrientationViaEuler(Vec3D eulerAngle);
230 
239  virtual void setOrientation(const Quaternion& orientation)
240  { orientation_ = orientation; }
241 
245  virtual void move(const Vec3D& move);
246 
250  virtual void rotate(const Vec3D& angularVelocityDt);
251 
256  const std::vector<BaseInteraction*>& getInteractions() const
257  { return interactions_; }
258 
263 
268 
274 
278  void setVelocity(const Vec3D& velocity);
279 
283  void setAngularVelocity(const Vec3D& angularVelocity);
284 
292  { velocity_ += velocity; }
293 
297  void addAngularVelocity(const Vec3D& angularVelocity);
298 
302  virtual const Vec3D& getVelocity() const;
303 
307  virtual const Vec3D& getAngularVelocity() const;
308 
313  void setPrescribedPosition(const std::function<Vec3D(double)>& prescribedPosition);
314 
319  void applyPrescribedPosition(double time);
320 
325  void setPrescribedVelocity(const std::function<Vec3D(double)>& prescribedVelocity);
326 
331  void applyPrescribedVelocity(double time);
332 
337  void setPrescribedOrientation(const std::function<Quaternion(double)>& prescribedOrientation);
338 
343  void applyPrescribedOrientation(double time);
344 
349  void setPrescribedAngularVelocity(const std::function<Vec3D(double)>& prescribedAngularVelocity);
350 
355  void applyPrescribedAngularVelocity(double time);
356 
363  virtual BaseInteraction* getInteractionWith(BaseParticle* P, unsigned timeStamp, InteractionHandler* interactionHandler)=0;
364 
368  virtual const Vec3D getVelocityAtContact(const Vec3D& contact) const;
369 
373  void integrateBeforeForceComputation(double time, double timeStep);
374 
379  void integrateAfterForceComputation(double time, double timeStep);
380 
384  virtual bool isFixed() const =0;
385 
387  virtual Mdouble getInvMass() const
388  { return 0.0; }
389 
393  virtual Mdouble getCurvature(const Vec3D& labFixedCoordinates) const
394  { return 0.0; }
395 
396  virtual bool isFaceContact(const Vec3D& normal) const {return true;}
397 
398 private:
403  std::function<Vec3D(double)> prescribedPosition_;
404 
409  std::function<Vec3D(double)> prescribedVelocity_;
410 
415  std::function<Quaternion(double)> prescribedOrientation_;
416 
421  std::function<Vec3D(double)> prescribedAngularVelocity_;
422 
428 
434 
439 
444 
449 
450  /*
451  * This variable is needed in omp simulations to sum up all forces (and torques) acting on an interactable.
452  * It is used in an add-by-reduction approach:
453  * 1. Declare a vector forceOMP_, one element per thread, and set it to zero.
454  * 2. When a new force needs to be added to the interactable, don't add it immediately.
455  * Add it to forceOMP_[threadNum] instead
456  * 3. Add all forces in forceOMP_ together to force_.
457  * This reduction approach allows step 2 to be run in parallel, while step 3 needs to be done in sequence.
458  * It is efficient, since step 3 is much quicker than step 2.
459  */
460  std::vector<Vec3D> forceOMP_;
461 
465  std::vector<Vec3D> torqueOMP_;
466 
472 
476  unsigned int indSpecies_;
477 
482 
486  std::vector<BaseInteraction*> interactions_;
487 };
488 
489 #endif
490 
float * p
Definition: Tutorial_Map_using.cpp:9
Defines the basic properties that a interactable object can have.
Definition: BaseInteractable.h:34
void setPrescribedOrientation(const std::function< Quaternion(double)> &prescribedOrientation)
Allows the orientation of the infinite mass interactbale to be prescribed.
Definition: BaseInteractable.cc:452
const Quaternion & getOrientation() const
Returns the orientation of this BaseInteractable.
Definition: BaseInteractable.h:209
unsigned int indSpecies_
Definition: BaseInteractable.h:476
const Vec3D & getForce() const
Returns the force on this BaseInteractable.
Definition: BaseInteractable.h:105
virtual const Vec3D & getAngularVelocity() const
Returns the angular velocity of this interactable.
Definition: BaseInteractable.cc:319
void sumForceTorqueOMP()
Definition: BaseInteractable.cc:140
void applyPrescribedAngularVelocity(double time)
Computes the angular velocity from the user defined prescribed angular velocity.
Definition: BaseInteractable.cc:490
virtual void setOrientation(const Quaternion &orientation)
Sets the orientation of this BaseInteractable.
Definition: BaseInteractable.h:239
~BaseInteractable() override
Destructor, it simply destructs the BaseInteractable and all the objects it contains.
Definition: BaseInteractable.cc:75
void setAngularVelocity(const Vec3D &angularVelocity)
set the angular velocity of the BaseInteractble.
Definition: BaseInteractable.cc:338
void copyInteractionsForPeriodicParticles(const BaseInteractable &p)
Copies interactions to this BaseInteractable whenever a periodic copy made.
Definition: BaseInteractable.cc:364
void setForce(const Vec3D &force)
Sets the force on this BaseInteractable.
Definition: BaseInteractable.h:128
void setOrientationViaNormal(Vec3D normal)
Sets the orientation of this BaseInteractable by defining the vector that results from the rotation o...
Definition: BaseInteractable.cc:177
std::vector< Vec3D > torqueOMP_
Definition: BaseInteractable.h:465
void addTorque(const Vec3D &addTorque)
Adds an amount to the torque on this BaseInteractable.
Definition: BaseInteractable.cc:110
void addAngularVelocity(const Vec3D &angularVelocity)
add an increment to the angular velocity.
Definition: BaseInteractable.cc:348
std::function< Vec3D(double)> prescribedAngularVelocity_
Definition: BaseInteractable.h:421
std::function< Vec3D(double)> prescribedVelocity_
Definition: BaseInteractable.h:409
void addVelocity(const Vec3D &velocity)
adds an increment to the velocity.
Definition: BaseInteractable.h:291
virtual void resetForceTorque(int numberOfOMPthreads)
Definition: BaseInteractable.cc:119
const ParticleSpecies * species_
Definition: BaseInteractable.h:471
void applyPrescribedPosition(double time)
Computes the position from the user defined prescribed position function.
Definition: BaseInteractable.cc:401
virtual const Vec3D & getVelocity() const
Returns the velocity of this interactable.
Definition: BaseInteractable.cc:307
std::function< Vec3D(double)> prescribedPosition_
Definition: BaseInteractable.h:403
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
Definition: BaseInteractable.cc:328
void setSpecies(const ParticleSpecies *species)
Sets the species of this BaseInteractable.
Definition: BaseInteractable.cc:163
void applyPrescribedVelocity(double time)
Computes the velocity from the user defined prescribed velocity function.
Definition: BaseInteractable.cc:432
virtual BaseInteraction * getInteractionWith(BaseParticle *P, unsigned timeStamp, InteractionHandler *interactionHandler)=0
Returns the interaction between this object and a given BaseParticle.
void setTorque(const Vec3D &torque)
Sets the torque on this BaseInteractable.
Definition: BaseInteractable.h:140
BaseInteractable()
Default BaseInteractable constructor.
Definition: BaseInteractable.cc:20
std::vector< Vec3D > forceOMP_
Definition: BaseInteractable.h:460
void setOrientationViaEuler(Vec3D eulerAngle)
Sets the orientation of this BaseInteractable by defining the euler angles.
Definition: BaseInteractable.cc:182
bool removeInteraction(BaseInteraction *I)
Removes an interaction from this BaseInteractable.
Definition: BaseInteractable.cc:286
Vec3D position_
Definition: BaseInteractable.h:427
void applyPrescribedOrientation(double time)
Computes the orientation from the user defined prescribed orientation function.
Definition: BaseInteractable.cc:463
void setPrescribedAngularVelocity(const std::function< Vec3D(double)> &prescribedAngularVelocity)
Allows the angular velocity of the infinite mass interactable to be prescribed.
Definition: BaseInteractable.cc:479
void addInteraction(BaseInteraction *I)
Adds an interaction to this BaseInteractable.
Definition: BaseInteractable.cc:270
void write(std::ostream &os) const override
Write a BaseInteractable to an output stream.
Definition: BaseInteractable.cc:252
virtual Mdouble getCurvature(const Vec3D &labFixedCoordinates) const
Definition: BaseInteractable.h:393
virtual bool isFixed() const =0
void read(std::istream &is) override
Reads a BaseInteractable from an input stream.
Definition: BaseInteractable.cc:222
virtual void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
Definition: BaseInteractable.h:218
void integrateAfterForceComputation(double time, double timeStep)
This is part of the integration routine for objects with infinite mass.
Definition: BaseInteractable.cc:589
std::vector< BaseInteraction * > interactions_
Definition: BaseInteractable.h:486
virtual bool isFaceContact(const Vec3D &normal) const
Definition: BaseInteractable.h:396
const std::vector< BaseInteraction * > & getInteractions() const
Returns a list of interactions which belong to this interactable.
Definition: BaseInteractable.h:256
const ParticleSpecies * getSpecies() const
Returns a pointer to the species of this BaseInteractable.
Definition: BaseInteractable.h:87
void setPrescribedPosition(const std::function< Vec3D(double)> &prescribedPosition)
Allows the position of an infinite mass interactable to be prescribed.
Definition: BaseInteractable.cc:391
void integrateBeforeForceComputation(double time, double timeStep)
This is part of integrate routine for objects with infinite mass.
Definition: BaseInteractable.cc:516
virtual Mdouble getInvMass() const
Definition: BaseInteractable.h:387
virtual void move(const Vec3D &move)
Moves this BaseInteractable by adding an amount to the position.
Definition: BaseInteractable.cc:193
const Vec3D & getTorque() const
Returns the torque on this BaseInteractable.
Definition: BaseInteractable.h:117
virtual void rotate(const Vec3D &angularVelocityDt)
Rotates this BaseInteractable.
Definition: BaseInteractable.cc:208
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
Definition: BaseInteractable.h:197
void setPrescribedVelocity(const std::function< Vec3D(double)> &prescribedVelocity)
Allows the velocity of an infinite mass interactable to be prescribed.
Definition: BaseInteractable.cc:422
Vec3D velocity_
Definition: BaseInteractable.h:481
Quaternion orientation_
Definition: BaseInteractable.h:433
void addForce(const Vec3D &addForce)
Adds an amount to the force on this BaseInteractable.
Definition: BaseInteractable.cc:94
virtual void setIndSpecies(unsigned int indSpecies)
Sets the index of the Species of this BaseInteractable.
Definition: BaseInteractable.h:77
Vec3D torque_
Definition: BaseInteractable.h:448
Vec3D angularVelocity_
Definition: BaseInteractable.h:438
virtual const Vec3D getVelocityAtContact(const Vec3D &contact) const
Returns the velocity at the contact point, use by many force laws.
Definition: BaseInteractable.cc:353
unsigned int getIndSpecies() const
Returns the index of the species associated with the interactable object.
Definition: BaseInteractable.h:67
std::function< Quaternion(double)> prescribedOrientation_
Definition: BaseInteractable.h:415
Vec3D force_
Definition: BaseInteractable.h:443
Stores information about interactions between two interactable objects; often particles but could be ...
Definition: BaseInteraction.h:39
It is an abstract base class due to the purely virtual functions declared below. Even if the function...
Definition: BaseObject.h:30
Definition: BaseParticle.h:33
Container to store Interaction objects.
Definition: InteractionHandler.h:25
Definition: ParticleSpecies.h:16
This class contains the 4 components of a quaternion and the standard operators and functions needed ...
Definition: Kernel/Math/Quaternion.h:42
Definition: Kernel/Math/Vector.h:30
#define I
Definition: main.h:127
double P
Uniform pressure.
Definition: TwenteMeshGluing.cpp:77
double velocity(const double &t)
Angular velocity as function of time t.
Definition: jeffery_orbit.cc:107
void normal(const Vector< double > &x, Vector< double > &normal)
Definition: free_surface_rotation.cc:65