MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BaseParticle.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 "BaseParticle.h"
28 
29 
30 #include "../ParticleHandler.h"
31 //#include "../HGRID.h"
32 #include "../CSpecies.h"
33 
34 
35 
38 {
42  force.set_zero();
43  radius = 1.0;
44  angle.set_zero();
46  torque.set_zero();
47  mass = invMass = 1.0;
48  inertia = invInertia = 1.0;
49  HGRID_NextObject=NULL;
50 
51  indSpecies = 0;
53 #ifdef ContactListHgrid
54  firstPossibleContact=0;
55 #endif
56  HGRID_NextObject = 0;
57  HGRID_PrevObject = 0;
58  HGRID_x = 99999;
59  HGRID_y = 99999;
60  HGRID_z = 99999;
61  handler=0;
62 
63  #ifdef CONSTUCTOR_OUTPUT
64  std::cerr << "BaseParticle() finished"<<std::endl;
65  #endif
66 }
67 
70 {
71  position = p.position;
72  velocity = p.velocity;
74  force = p.force;
75  radius = p.radius;
76  angle = p.angle;
78  torque = p.torque;
79  mass = p.get_Mass();
80  invMass = p.get_InvMass();
81  inertia = p.get_Inertia();
83 
84  HGRID_NextObject = 0;
85  HGRID_PrevObject = 0;
86  HGRID_x = 99999;
87  HGRID_y = 99999;
88  HGRID_z = 99999;
90 
91  handler=0;
92 
95 #ifdef ContactListHgrid
96  firstPossibleContact=0;
97 #endif
98 
99  #ifdef CONSTUCTOR_OUTPUT
100  std::cerr << "BaseParticle(BaseParticle &p) finished"<<std::endl;
101  #endif
102 }
103 
106 {
107  if(handler)
108  {
110  }
111  #ifdef CONSTUCTOR_OUTPUT
112  std::cerr << "virtual ~BaseParticle finished"<<std::endl;
113  #endif
114 }
115 
118 {
119  return new BaseParticle(*this);
120 }
121 
123 Mdouble BaseParticle::get_Volume(std::vector<CSpecies>& Species) const
124 {
125  switch(Species[indSpecies].dim_particle)
126  {
127  case 3 :
128  return (4.0/3.0 * constants::pi * radius * radius * radius);
129  case 2 :
130  return (constants::pi * radius * radius);
131  case 1 :
132  return (2.0 * radius);
133  default :
134  {
135  std::cerr<<"In get_Volume(vector<CSpecies>& Species) the dimension of the particle is not set"<<std::endl;
136  exit(-1);
137  }
138  }
139 }
140 
144 #ifdef USE_SIMPLE_VERLET_INTEGRATION
145  position = PrevPosition;
146 #endif
147 }
148 
150 bool BaseParticle::isFixed(){return (invMass==0.0);}
151 
153 void BaseParticle::unfix(std::vector<CSpecies>& Species){
154  invMass=1.0;
155  compute_particle_mass(Species);
156 }
157 
159 void BaseParticle::compute_particle_mass(std::vector<CSpecies>& Species) {
160  if (!isFixed()) {
161  switch(Species[indSpecies].dim_particle)
162  {
163  case 3 :
164  {
165  set_Mass(4.0/3.0 * constants::pi * radius * radius * radius * Species[indSpecies].rho);
166  set_inertia(.4 * get_Mass() * sqr(radius));
167  break;
168  }
169  case 2 :
170  {
171  set_Mass(constants::pi * radius * radius * Species[indSpecies].rho);
172  set_inertia(.5 * get_Mass() * sqr(radius));
173  break;
174  }
175  case 1 :
176  {
177  set_Mass(2.0 * radius * Species[indSpecies].rho);
178  set_inertia(0.0);
179  break;
180  }
181  default :
182  {
183  std::cerr<<"In compute_particle_mass(vector<CSpecies>& Species) the dimension of the particle is not set"<<std::endl;
184  exit(-1);
185  }
186  }
187  }
188 }
189 
191 void BaseParticle::print(std::ostream& os) const
192 {
193  os << "BP "<< position << " " << velocity << " " << radius << " " << angle << " " << angularVelocity << " " << invMass << " " << invInertia << " " <<indSpecies;
194 }
195 
196 void BaseParticle::read(std::istream& is)
197 {
199  if (invMass) mass = 1.0/invMass; else mass = 1e20;
200  if (invInertia) inertia = 1.0/invInertia; else inertia = 1e20;
201 }
202 
203 void BaseParticle::print_HGRID(std::ostream& os) {
204  os << "Particle( HGRID_Level:" << HGRID_Level
205  << ", HGRID_x:" << HGRID_x
206  << ", HGRID_y:" << HGRID_y
207  << ", HGRID_z:" << HGRID_z
208  << ")";
209 }
210 
212 {
213  _index=newPos;
214 }
215 
219 #ifdef ContactListHgrid
220 PossibleContact* BaseParticle::get_FirstPossibleContact() const {return firstPossibleContact;}
221 #endif
222 int BaseParticle::get_HGRID_x() const {return HGRID_x;}
223 int BaseParticle::get_HGRID_y() const {return HGRID_y;}
224 int BaseParticle::get_HGRID_z() const {return HGRID_z;}
225 int BaseParticle::get_Index() const {return _index;}
232 const Vec3D& BaseParticle::get_Position() const {return position;}
235  if (handler) return radius + handler->getSpecies(indSpecies).get_InteractionDistance()/2.0; else return radius;
236 }
239 }
242 int BaseParticle::get_Id() const {return _id;}
243 const Vec3D& BaseParticle::get_Velocity() const {return velocity;}
244 const Vec3D& BaseParticle::get_Angle() const {return angle;}
246 const Vec3D& BaseParticle::get_Force() const {return force;}
247 const Vec3D& BaseParticle::get_Torque() const {return torque;}
251 const Vec3D BaseParticle::get_Displacement2(Mdouble xmin, Mdouble xmax, Mdouble ymin, Mdouble ymax, Mdouble zmin, Mdouble zmax, Mdouble t) const
252 {
254  if (xmax>xmin && fabs(disp.X)>.5*(xmax-xmin)) {
255  if (disp.X>0) disp.X -= xmax-xmin;
256  else disp.X += xmax-xmin;
257  }
258  if (ymax>ymin && fabs(disp.Y)>.5*(ymax-ymin)) {
259  if (disp.Y>0) disp.Y -= ymax-ymin;
260  else disp.Y += ymax-ymin;
261  }
262  if (zmax>zmin && fabs(disp.Z)>.5*(zmax-zmin)) {
263  if (disp.Z>0) disp.Z -= zmax-zmin;
264  else disp.Z += zmax-zmin;
265  }
266  disp /= t;
267  return disp;
268 }
269 
270 void BaseParticle::set_inertia (const Mdouble _new) {if (_new>=0){inertia=_new; invInertia=1.0/_new;} else { std::cerr << "Error in set_inertia ("<<_new<<")"<< std::endl; exit(-1); }}
271 void BaseParticle::set_infiniteInertia () {inertia=1e20; invInertia=0;} //> i.e. no rotations
273 void BaseParticle::set_species (const int _new) {indSpecies=_new;}
274 void BaseParticle::set_Index (const int _new) {_index=_new;}
275 void BaseParticle::set_HGRID_x (const int _new) {HGRID_x=_new;}
276 void BaseParticle::set_HGRID_y (const int _new) {HGRID_y=_new;}
277 void BaseParticle::set_HGRID_z (const int _new) {HGRID_z=_new;}
278 void BaseParticle::set_HGRID_Level (const int _new) {HGRID_Level=_new;}
281 #ifdef ContactListHgrid
282 void BaseParticle::set_FirstPossibleContact (const PossibleContact* PC) {firstPossibleContact=PC;}
283 #endif
285 {
286  radius=_new;
287  if(handler)
288  handler->checkExtrema(this);
289 }
290 void BaseParticle::set_IndSpecies (const int _new) {indSpecies=_new;}
291 void BaseParticle::set_Id (const int _new) {_id=_new;}
292 void BaseParticle::set_Mass (const Mdouble _new) {if(_new>=0.0) {if(invMass){mass=_new;invMass=1.0/_new;}} else { std::cerr << "Error in set_Mass(" << _new << ")" << std::endl; exit(-1); }} //InvMass=0 is a flag for a fixed particle
293 void BaseParticle::set_Velocity (const Vec3D& _new) {velocity=_new;}
294 void BaseParticle::set_Position (const Vec3D& _new) {position=_new;}
297 void BaseParticle::set_Angle (const Vec3D& _new) {angle=_new;}
299 void BaseParticle::set_Force (const Vec3D& _new) {force=_new;}
300 void BaseParticle::set_Torque (const Vec3D& _new) {torque=_new;}
302 
303 void BaseParticle::move (const Vec3D& _new) {position+=_new;}
305 void BaseParticle::accelerate (const Vec3D& _new) {velocity+=_new;}
306 void BaseParticle::rotate (const Vec3D& _new) {angle+=_new;}
309 void BaseParticle::add_Force (const Vec3D& _new) {force+=_new;}
310 void BaseParticle::add_Torque (const Vec3D& _new) {torque+=_new;}
311 
312 #ifdef USE_SIMPLE_VERLET_INTEGRATION
313 void BaseParticle::set_PrevPosition (const Vec3D& _new) {prevPosition=_new;}
314 void BaseParticle::prevMove (const Vec3D& _new) {prevPosition+=_new;}
315 const Vec3D& BaseParticle::get_PrevPosition() {return prevPosition;}
316 #endif
int get_Species() const
Vec3D velocity
Current particle position.
Definition: BaseParticle.h:193
void set_inertia(const Mdouble _new)
int HGRID_x
Hgrid attributes.
Definition: BaseParticle.h:174
BaseParticle * periodicFromParticle
Particle radius.
Definition: BaseParticle.h:186
void set_HGRID_x(const int _new)
Mdouble get_KineticEnergy() const
void add_Torque(const Vec3D &_new)
void set_Force(const Vec3D &_new)
void checkExtremaOnDelete(BaseParticle *P)
Checks if the extrema of this ParticleHandler needs updating when a particle is deleted.
void set_species(const int _new)
void set_HGRID_PrevObject(BaseParticle *_new)
BaseParticle * get_HGRID_PrevObject() const
Mdouble X
Definition: Vector.h:44
const Vec3D & get_Velocity() const
ParticleHandler * handler
Definition: BaseParticle.h:170
Mdouble get_InteractionRadius() const
void rotate(const Vec3D &_new)
void set_PreviousPosition(const Vec3D &_new)
void compute_particle_mass(std::vector< CSpecies > &Species)
Compute Particle mass function, which required a reference to the Species vector. It copmuters the Pa...
Mdouble inertia
Inverse Particle mass (for computation optimization)
Definition: BaseParticle.h:183
void checkExtrema(BaseParticle *P)
Checks if the extrema of this ParticleHandler needs updating.
int get_IndSpecies() const
Mdouble mass
Index of the Particle in the ParticleHandler, used for outputting fstat data.
Definition: BaseParticle.h:181
Mdouble radius
Inverse Particle inverse inertia (for computation optimization)
Definition: BaseParticle.h:185
int _id
Index of the Species of this Particle.
Definition: BaseParticle.h:188
void angularAccelerate(const Vec3D &_new)
BaseParticle * get_PeriodicFromParticle() const
void setHandler(ParticleHandler *handler)
#define sqr(a)
Definition: ExtendedMath.h:36
BaseParticle * HGRID_PrevObject
Pointer to the next Particle in the same HGrid cell.
Definition: BaseParticle.h:177
virtual void print(std::ostream &os) const
Particle print function, which accepts an os std::stringstream as input.
void set_HGRID_z(const int _new)
int get_HGRID_z() const
Vec3D angularVelocity
Current angular position.
Definition: BaseParticle.h:191
int get_Index() const
Mdouble get_InvInertia() const
BaseParticle * get_HGRID_NextObject() const
Vec3D angle
non changing identifier of particle
Definition: BaseParticle.h:190
ParticleHandler * getHandler() const
void set_Index(const int _new)
const Vec3D & get_Force() const
Mdouble invInertia
Particle inertia.
Definition: BaseParticle.h:184
friend Mdouble GetLength2(const Vec3D &A)
Definition: Vector.h:183
bool isFixed()
Is fixed Particle function. It returns wether a Particle is fixed or not, by cheking its inverse Mass...
Mdouble get_Radius() const
void set_periodicFromParticle(BaseParticle *_new)
int get_HGRID_x() const
Mdouble get_Mass() const
Mdouble get_InvMass() const
const Vec3D get_Displacement2(Mdouble xmin, Mdouble xmax, Mdouble ymin, Mdouble ymax, Mdouble zmin, Mdouble zmax, Mdouble t) const
virtual void read(std::istream &is)
Particle read function, which accepts an os std::stringstream as input.
Vec3D previousPosition
Displacement (only used in StatisticsVector, StatisticsPoint)
Definition: BaseParticle.h:199
virtual ~BaseParticle()
Particle destructor, needs to be implemented and checked if it removes tangential spring information...
const Mdouble pi
Definition: ExtendedMath.h:54
double Mdouble
Definition: ExtendedMath.h:33
int get_Id() const
void set_IndSpecies(const int _new)
void set_HGRID_y(const int _new)
Mdouble get_WallInteractionRadius() const
int _index
Pointer to the previous Particle in the same HGrid cell.
Definition: BaseParticle.h:180
void add_Force(const Vec3D &_new)
void set_infiniteInertia()
Vec3D torque
Interaction force.
Definition: BaseParticle.h:196
int HGRID_Level
Cell position in the grid.
Definition: BaseParticle.h:175
const Vec3D & get_PreviousPosition() const
void set_HGRID_Level(const int _new)
const Vec3D & get_Displacement() const
void move(const Vec3D &_new)
Vec3D displacement
Torque.
Definition: BaseParticle.h:198
const Vec3D & get_Position() const
Container to store all BaseParticle.
Mdouble Y
Definition: Vector.h:44
void set_Radius(const Mdouble _new)
void set_zero()
Definition: Vector.h:55
BaseParticle * HGRID_NextObject
Grid level for the object.
Definition: BaseParticle.h:176
CSpecies & getSpecies(const int i)
void set_Velocity(const Vec3D &_new)
Mdouble invMass
Particle mass.
Definition: BaseParticle.h:182
BaseParticle()
Basic Particle contructors, creates an Particle at (0,0,0) with radius, mass and inertia equal to 1...
Definition: BaseParticle.cc:37
const Vec3D & get_Torque() const
Vec3D position
Current angular velocity.
Definition: BaseParticle.h:192
void movePrevious(const Vec3D &_new)
void unfix(std::vector< CSpecies > &Species)
Unfix Particle function, which required a reference to the Species vector. It un fixes a Particle by ...
Mdouble get_Volume(std::vector< CSpecies > &Species) const
Get Particle volume function, which required a reference to the Species vector. It returns the volume...
void set_AngularVelocity(const Vec3D &_new)
int get_HGRID_y() const
void print_HGRID(std::ostream &os)
void set_Angle(const Vec3D &_new)
void set_Displacement(const Vec3D &_new)
virtual void moveInHandler(int newPos)
int indSpecies
Pointer to originating Particle.
Definition: BaseParticle.h:187
void set_Torque(const Vec3D &_new)
Mdouble get_Inertia() const
const Vec3D & get_Angle() const
Vec3D force
Current particle velocity.
Definition: BaseParticle.h:195
void set_HGRID_NextObject(BaseParticle *_new)
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:40
void set_Position(const Vec3D &_new)
void accelerate(const Vec3D &_new)
void set_Id(const int _new)
Mdouble Z
Definition: Vector.h:44
int get_HGRID_Level() const
Mdouble get_InteractionDistance()
Definition: CSpecies.h:97
virtual BaseParticle * copy() const
Particle copy method. It calls to copy contrustor of this Particle, usefull for polymorfism.
void add_Displacement(const Vec3D &_new)
void fixParticle()
Fix Particle function. It fixes a Particle by setting its inverse mass and inertia and velocities to ...
void set_Mass(const Mdouble _new)
const Vec3D & get_AngularVelocity() const