MercuryDPM  Beta
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseInteractable.cc
Go to the documentation of this file.
1 //Copyright (c) 2013-2015, 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 "BaseInteractable.h"
28 #include "Particles/BaseParticle.h"
31 
39 {
44  force_.setZero();
45  torque_.setZero();
46  indSpecies_ = 0;
47  species_ = nullptr;
48  prescribedPosition_ = nullptr;
49  prescribedVelocity_ = nullptr;
50 #ifdef DEBUG_CONSTRUCTOR
51  std::cout<<"BaseInteractable::BaseInteractable() finished"<<std::endl;
52 #endif
53 }
54 
62  : BaseObject(p)
63 {
64  interactions_.clear();
65  position_ = p.position_;
67  velocity_ = p.velocity_;
69  force_ = p.force_;
70  torque_ = p.torque_;
71  species_ = p.species_;
75 #ifdef DEBUG_CONSTRUCTOR
76  std::cout<<"BaseInteractable::BaseInteractable(const BaseInteractable &p finished"<<std::endl;
77 #endif
78 }
79 
84 {
85  //std::cout<<"Deleting BaseInteractable with index="<<getIndex()<<" and id="<<getId()<<" size="<<interactions_.size()<<std::endl;
86  while (interactions_.size() > 0)
87  {
88  interactions_.front()->removeFromHandler();
89  }
90 #ifdef DEBUG_DESTRUCTOR
91  std::cout<<"BaseInteractable::~BaseInteractable() finished"<<std::endl;
92 #endif
93 }
94 
101 {
102  return indSpecies_;
103 }
104 
112 {
113  indSpecies_ = indSpecies;
114 }
115 
124 {
125  return species_;
126 }
127 
137 {
138  species_ = species;
139  indSpecies_ = species->getIndex();
140 }
141 
151 {
152  return force_;
153 }
154 
164 {
165  return torque_;
166 }
167 
176 {
177  force_ = force;
178 }
179 
189 {
190  torque_ = torque;
191 }
192 
200 {
201  force_ += addForce;
202 }
203 
211 {
212  torque_ += addTorque;
213 }
214 
225 {
226  return position_;
227 }
228 
240 {
241  return orientation_;
242 }
243 
251 {
252  position_ = position;
253 }
254 
255 
263 void BaseInteractable::setOrientation(const Vec3D& orientation)
264 {
265  orientation_ = orientation;
266 }
267 
274 void BaseInteractable::move(const Vec3D& move)
275 {
276  position_ += move;
277 }
278 
285 void BaseInteractable::rotate(const Vec3D& rotate)
286 {
287  orientation_ += rotate;
288 }
289 
297 void BaseInteractable::read(std::istream& is)
298 {
299  BaseObject::read(is);
300  std::string dummy;
301  is >> dummy >> indSpecies_;
302  is >> dummy >> position_;
303  is >> dummy >> orientation_ >> dummy;
304  is >> dummy >> velocity_;
305  is >> dummy >> angularVelocity_ >> dummy;
306  is >> dummy >> force_;
307  is >> dummy >> torque_;
308 }
309 
318 void BaseInteractable::write(std::ostream& os) const
319  {
320  BaseObject::write(os);
321  os << " indSpecies " << indSpecies_
322  << " position " << position_
323  << " orientation " << orientation_ << " " << 0.0
324  << " velocity " << velocity_
325  << " angularVelocity " << angularVelocity_ << " " << 0.0
326  << " force " << force_
327  << " torque " << torque_;
328 }
329 
335 const std::list<BaseInteraction*>& BaseInteractable::getInteractions() const
336 {
337  return interactions_;
338 }
339 
346 {
347  interactions_.push_back(I);
348 }
349 
361 {
362  for (std::list<BaseInteraction*>::iterator it = interactions_.begin(); it != interactions_.end(); ++it)
363  {
364  if (I == (*it))
365  {
366  //std::cout<<"Removing interaction from "<<getId()<<" between "<<I->getI()->getId()<<" and "<<I->getP()->getId()<<std::endl;
367  interactions_.erase(it);
368  return true;
369  }
370  }
371  std::cerr << "Error in BaseInteractable::removeInteraction: Interaction could not be removed" << std::endl;
372  return false;
373 }
374 
383 {
384  return velocity_;
385 }
386 
395 {
396  return angularVelocity_;
397 }
398 
404 {
405  velocity_ = velocity;
406 }
407 
413 void BaseInteractable::setAngularVelocity(const Vec3D& angularVelocity)
414 {
415  angularVelocity_ = angularVelocity;
416 }
417 
424 {
425  velocity_ += velocity;
426 }
427 
433 void BaseInteractable::addAngularVelocity(const Vec3D& angularVelocity)
434 {
435  angularVelocity_ += angularVelocity;
436 }
438  {
439  return getVelocity() - Vec3D::cross(contact - getPosition(), getAngularVelocity());
440 }
441 
449 {
450  for (std::list<BaseInteraction*>::const_iterator it = pOriginal.interactions_.begin(); it != pOriginal.interactions_.end(); ++it)
451  {
452  //So here this is the ghost and it is the interaction of the ghost/
453  (*it)->copySwitchPointer(&pOriginal, this);
454  }
455 }
456 
475 void BaseInteractable::setPrescribedPosition(std::function<Vec3D (double)> prescribedPosition)
476 {
477  prescribedPosition_ = prescribedPosition;
478 }
479 
486 {
488  {
490  }
491 }
492 
506 void BaseInteractable::setPrescribedVelocity(std::function<Vec3D (double)> prescribedVelocity)
507 {
508  prescribedVelocity_ = prescribedVelocity;
509 }
510 
517 {
519  {
521  }
522 }
523 
536 void BaseInteractable::setPrescribedOrientation(std::function<Vec3D (double)> prescribedOrientation)
537 {
538  prescribedOrientation_ = prescribedOrientation;
539 }
540 
548 {
550  {
552  }
553 }
554 
555 /*
556  * \details This functions sets a std::function which specifies the
557  * AngularVeclocity of an interactable. This function takes a double
558  * the time and returns a Vec3D which is the new angular velocity.
559  * See also BaseInteractable::setPrescribedOrientation
560  * Note this functions works the same way as
561  * BaseInteractable::setPosition and BaseInteractable::setVeclocity.
562  */
563 void BaseInteractable::setPrescribedAngularVelocity(std::function<Vec3D (double)> prescribedAngularVelocity)
564 {
565  prescribedAngularVelocity_ = prescribedAngularVelocity;
566 }
567 
575 {
577  {
579  }
580 }
581 
600 void BaseInteractable::integrateBeforeForceComputation(double time, double timeStep)
601 {
602  if (prescribedPosition_ )
603  {
604  if (prescribedVelocity_ )
605  {
606  //Both the velocity and position are defined; as we are using leap
607  //frog method so the velocity is evaluated half a time later.
608  applyPrescribedPosition(time + timeStep);
609  applyPrescribedVelocity(time + 0.5 * timeStep);
610  }
611  else
612  {
613  //Only the position is defined.
614  //Velocity is evaluated from a finite different of the Position
615  //Note, we use 0.5 +- 0.1 timeStep for the velocity eval.
616  applyPrescribedPosition(time + timeStep);
617  setVelocity((prescribedPosition_(time + 0.6 * timeStep) - prescribedPosition_(time + 0.4 * timeStep)) / (0.2 * timeStep));
618  }
619  }
620  else
621  {
622  if (prescribedVelocity_ )
623  {
624  //Only the velocity is set. The position is calculated from the
625  //the integral of velocity.
626  applyPrescribedVelocity(time + 0.5 * timeStep);
627  move(getVelocity() * timeStep);
628  }
629  else
630  {
631  //Neither is set move based on the computed velocity of the object.
632  move(getVelocity() * timeStep);
633  }
634  }
635  if (prescribedOrientation_ != nullptr)
636  {
637  if (prescribedAngularVelocity_ != nullptr)
638  {
639  applyPrescribedOrientation(time + timeStep);
640  applyPrescribedAngularVelocity(time + 0.5 * timeStep);
641  }
642  else
643  {
644  applyPrescribedOrientation(time + timeStep);
645  setAngularVelocity((prescribedOrientation_(time + 0.6 * timeStep) - prescribedOrientation_(time + 0.4 * timeStep)) / (0.2 * timeStep));
646  }
647  }
648  else
649  {
651  {
652  applyPrescribedAngularVelocity(time + 0.5 * timeStep);
653  rotate(getAngularVelocity() * timeStep);
654  }
655  else
656  {
657  rotate(getAngularVelocity() * timeStep);
658  }
659  }
660 }
661 
669 void BaseInteractable::integrateAfterForceComputation(double time, double timeStep)
670 {
672  {
674  {
675  applyPrescribedVelocity(time + timeStep);
676  }
677  else
678  {
679  setVelocity((prescribedPosition_(time + 1.1 * timeStep) - prescribedPosition_(time + 0.9 * timeStep)) / (0.2 * timeStep));
680  }
681  }
682  else
683  {
685  {
686  applyPrescribedVelocity(time + 0.5 * timeStep);
687  }
688  }
690  {
692  {
693  applyPrescribedAngularVelocity(time + timeStep);
694  }
695  else
696  {
697  setAngularVelocity((prescribedOrientation_(time + 1.1 * timeStep) - prescribedOrientation_(time + 0.9 * timeStep)) / (0.2 * timeStep));
698  }
699  }
700  else
701  {
703  {
704  applyPrescribedAngularVelocity(time + 0.5 * timeStep);
705  }
706  }
707 }
unsigned int getIndex() const
Returns the index of the object in the handler.
Definition: BaseObject.cc:106
void rotate(const Vec3D &rotate)
Rotates this BaseInteractable.
std::function< Vec3D(double)> prescribedPosition_
void addVelocity(const Vec3D &velocity)
adds an increment to the velocity.
void setVelocity(const Vec3D &velocity)
set the velocity of the BaseInteractable.
unsigned int indSpecies_
void applyPrescribedPosition(double time)
Computes the position from the user defined prescribed position function.
const Vec3D & getForce() const
Returns the force on this BaseInteractable.
std::function< Vec3D(double)> prescribedVelocity_
void setOrientation(const Vec3D &orientation)
Sets the orientation of this BaseInteractable.
bool removeInteraction(BaseInteraction *I)
Removes an interaction from this BaseInteractable.
void addInteraction(BaseInteraction *I)
Adds an interaction to this BaseInteractable.
const ParticleSpecies * getSpecies() const
Returns a pointer to the species of this BaseInteractable.
void applyPrescribedAngularVelocity(double time)
Computes the angular velocity from the user defined prescribed angular velocity.
void setForce(Vec3D force)
Sets the force on this BaseInteractable.
It is an abstract base class due to the purely virtual functions declared below. Even if the function...
Definition: BaseObject.h:49
virtual const Vec3D getVelocityAtContact(const Vec3D &contact) const
Returns the velocity at the contact point, use by many force laws.
void applyPrescribedOrientation(double time)
Computes the orientation from the user defined prescribed orientation function.
void setPrescribedPosition(std::function< Vec3D(double)> prescribedPosition)
Allows the position of an infinite mass interactable to be prescribed.
void integrateBeforeForceComputation(double time, double timeStep)
This is part of integrate routine for objects with infinite mass.
void setZero()
Sets all elements to zero.
Definition: Vector.cc:52
virtual const Vec3D & getAngularVelocity() const
Returns the angular velocity of this interactable.
virtual ~BaseInteractable()
Destructor, it simply destructs the BaseInteractable and all the objects it contains.
const Vec3D & getPosition() const
Returns the position of this BaseInteractable.
const Vec3D & getOrientation() const
Returns the orientation of this BaseInteractable.
Stores information about interactions between two interactable objects; often particles but could be ...
#define MERCURY_DEPRECATED
Definition: GeneralDefine.h:35
void setPrescribedOrientation(std::function< Vec3D(double)> prescribedOrientation)
Allows the orientation of the infinite mass interactbale to be prescribed.
void addAngularVelocity(const Vec3D &angularVelocity)
add an increment to the angular velocity.
void setSpecies(const ParticleSpecies *species)
Sets the species of this BaseInteractable.
virtual void read(std::istream &is)=0
Reads a BaseInteractable from an input stream.
const ParticleSpecies * species_
void setPrescribedVelocity(std::function< Vec3D(double)> prescribedVelocity)
Allows the velocity of an infinite mass interactable to be prescribed.
const Vec3D & getTorque() const
Returns the torque on this BaseInteractable.
std::list< BaseInteraction * > interactions_
std::function< Vec3D(double)> prescribedOrientation_
static Vec3D cross(const Vec3D &a, const Vec3D &b)
Calculates the cross product of two Vec3D: .
Definition: Vector.cc:268
void addTorque(Vec3D addTorque)
Adds an amount to the torque on this BaseInteractable.
virtual void read(std::istream &is)=0
A purely virtual method with an implementation which reads the index from the stream and assigns it t...
Definition: BaseObject.cc:120
void copyInteractionsForPeriodicParticles(const BaseInteractable &p)
Copies interactions to this BaseInteractable whenever a periodic copy made.
virtual void setIndSpecies(unsigned int indSpecies)
Sets the index of the Species of this BaseInteractable.
void setPosition(const Vec3D &position)
Sets the position of this BaseInteractable.
void applyPrescribedVelocity(double time)
Computes the velocity from the user defined prescribed velocity function.
Defines the basic properties that a interactable object can have.
std::function< Vec3D(double)> prescribedAngularVelocity_
const std::list< BaseInteraction * > & getInteractions() const
Returns a reference to the list of interactions in this BaseInteractable.
unsigned int getIndSpecies() const
Returns the index of the Species of this BaseInteractable.
virtual void write(std::ostream &os) const =0
Write a BaseInteractable to an output stream.
virtual const Vec3D & getVelocity() const
Returns the velocity of this interactable.
void setPrescribedAngularVelocity(std::function< Vec3D(double)> prescribedAngularVelocity)
Allows the angular velocity of the infinite mass interactable to be prescribed.
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:45
virtual void move(const Vec3D &move)
Moves this BaseInteractable by adding an amount to the position.
void addForce(Vec3D addForce)
Adds an amount to the force on this BaseInteractable.
void setAngularVelocity(const Vec3D &angularVelocity)
set the angular velocity of the BaseInteractble.
void setTorque(Vec3D torque)
Sets the torque on this BaseInteractable.
virtual void write(std::ostream &os) const =0
A purely virtual function which has an implementation which writes the name and the object id_ to the...
Definition: BaseObject.cc:128
BaseInteractable()
Default BaseInteractable constructor, it simply creates an empty BaseInteractable.
void integrateAfterForceComputation(double time, double timeStep)
This is part of the integration routine for objects with infinite mass.