MercuryDPM  0.10
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CTangentialSpring.h
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 #ifndef CTANGENTIALSPRING_H
27 #define CTANGENTIALSPRING_H
28 
29 #include "Vector.h"
30 #include <algorithm>
31 #include <vector>
32 #include <iostream>
33 #include <iomanip>
34 #include <functional>
35 
39 public:
42  delta.set_zero();
46  pParticle=-1;
47  pWall=-1;
48  time=-1;
49  sliding = false;
50  slidingRolling = false;
51  slidingTorsion = false;
52  }
53 
54  CTangentialSpring(int pParticle_, int pWall_, Mdouble time_) {
55  delta.set_zero();
59  pParticle = pParticle_;
60  pWall = pWall_;
61  time = time_;
62  sliding = false;
63  slidingRolling = false;
64  slidingTorsion = false;
65  }
66 
69  delta = ts.delta;
73  pParticle = ts.pParticle;
74  pWall = ts.pWall;
75  time = ts.time;
76  sliding = ts.sliding;
79  }
80 
82  void print(std::ostream& os) {
83  os << "delta:" << delta
84  << ", particle:" << pParticle
85  << ", wall:" << pWall
86  << ", time:" << time;
87  }
88 
90  friend inline std::ostream& operator<<(std::ostream& os, const CTangentialSpring &p)
91  {
92  os << p.delta << " " << p.RollingSpring << " " << p.TorsionSpring << " " << p.pParticle << " " << p.pWall << " " << p.time;
93  return os;
94  }
95 
97  friend inline std::istream& operator>>(std::istream& is, CTangentialSpring &p)
98  {
99  is >> p.delta >> p.RollingSpring >> p.TorsionSpring >> p.pParticle >> p.pWall >> p.time;
100  return is;
101  }
102 
103  void reverse()
104  {
105  delta=-delta;
109  }
110 
120  int pWall;
123 
124  bool sliding;
127 };
128 
137 class CTangentialSprings : public std::vector<CTangentialSpring> {
138 public:
139 
141  struct check_spring_time : public std::binary_function<CTangentialSpring,Mdouble,bool>
142  {
143  bool operator() (const CTangentialSpring a, const Mdouble b) const {return(a.time<b);}
144  };
145 
147  std::vector<int> to_be_erased(Mdouble time) {
148  std::vector<int> retval;
149  for (unsigned int i=0; i<size(); i++)
150  if (operator[](i).time<time) {
151  retval.push_back(i);
152  //std::cout << "to be erased " << time << " " << operator[](i).pParticle << " " << operator[](i).pWall << " " << size() << std::endl;
153  }
154  return retval;
155  }
156 
158  void to_erase(Mdouble time) {
159  erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time)),end());
160  }
161 
164  //Remove_if reconstructs the vector with only elements passing the check_spring_time function
165  //Erase removes the end of the vector
166  erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time_)),end());
167 
168  //Loops over all Springs and returns the correct one (if it exists)
169  for (CTangentialSprings::iterator it=begin(); it!=end();it++){
170  if (it->pParticle==P)
171  {
172  it->time=time_+dt;
173  return &(*it);
174  }
175  }
176  return 0;
177  }
178 
180  {
181  //if not, create it
182  push_back(CTangentialSpring(P,-1,time_+dt));
183  return &back();
184  }
185 
187  {
188  //if not, create it
189  push_back(CTangentialSpring(CTS));
190  back().pParticle=P;
191  back().time=time_+dt;
192  return &back();
193  }
194 
196  {
197  //if not, create it
198  push_back(CTangentialSpring(-1,W,time_+dt));
199  return &(back());
200  }
201 
202 
203  bool has_particle_spring(int P, Mdouble time_) {
204  //Remove_if reconstructs the vector with only elements passing the check_spring_time function
205  //Erase removes the end of the vector
206  erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time_)),end());
207  //Loops over all Springs and returns the correct one (if it exists)
208  for (CTangentialSprings::iterator it=begin(); it!=end();it++){
209  if (it->pParticle==P)
210  {
211  return true;
212  }
213  }
214  //if not, create it
215  return false;
216  }
217 
218 
221  //Remove_if reconstructs the vector with only elements passing the check_spring_time function
222  //Erase removes the end of the vector
223  erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time_)),end());
224 
225  //Loops over all Springs ant returns the correct one (if it exists)
226  for (CTangentialSprings::iterator it=begin(); it!=end();it++){
227  if (it->pWall==W)
228  {
229  it->time=time_+dt;
230  return &(*it);
231  }
232  }
233 
234  //if not, create it
235  //~ std::cout << "created pw " << time_ << " " << W << std::endl;
236  return 0;
237  }
238 
240  void reset() {
241  clear();
242  reserve(13);
243  }
244 
246  void print(std::ostream& os, Mdouble time_) {
247  os << "Tangential Springs: N=" << size() << std::endl;
248  for (CTangentialSprings::iterator it=begin(); it!=end(); it++)
249  if (it->time>=time_) {
250  it->print(os); os << std::endl;
251  }
252  }
253 
254 public:
256  friend inline std::ostream& operator<<(std::ostream& os, const CTangentialSprings &p)
257  {
258  os << p.size();
259  for (unsigned int i=0; i<p.size(); i++) os << " " << p[i];
260  return os;
261  }
262 
264  friend inline std::istream& operator>>(std::istream& is, CTangentialSprings &p)
265  {
266  //should return empty spring if read from is failed
267  int n; is >> n;
268  if (is.fail()) {
269  p.resize(0);
270  } else {
271  p.resize(n);
272  for (unsigned int i=0; i<p.size(); i++) is >> p[i];
273  }
274  return is;
275  }
276 
277 };
278 
279 #endif
void to_erase(Mdouble time)
Function selects the tangential spring vector for particle-particle interations (also removed not use...
CTangentialSpring()
constructors
bool operator()(const CTangentialSpring a, const Mdouble b) const
int pParticle
A pointer to the particle in contact; NULL if the contact is with a wall (The other particle is the p...
int pWall
A pointer to the wall in contact; NULL if the contact is with a particle (The other particle is the p...
void print(std::ostream &os, Mdouble time_)
outputs all current active tangential springs
void print(std::ostream &os)
outputs tangential spring
Vec3D delta
stores the spring
CTangentialSpring * select_wall_spring(int W, Mdouble time_, Mdouble dt)
Function selects the tangential spring vector for particle-particle interations (also removed not use...
Construction required for the erase/remove_if stuff.
friend std::ostream & operator<<(std::ostream &os, const CTangentialSpring &p)
writes spring
double Mdouble
Definition: ExtendedMath.h:33
friend std::istream & operator>>(std::istream &is, CTangentialSprings &p)
reads all springs
Member variable of #Particle storing all tangential springs of particle PI with contacting particles...
Stores the tangential spring needed to compute a tangential elastic force between particles PI...
CTangentialSpring * create_new(int P, Mdouble time_, Mdouble dt)
void reset()
Resets the tangential springs.
friend std::ostream & operator<<(std::ostream &os, const CTangentialSprings &p)
writes all springs
std::vector< int > to_be_erased(Mdouble time)
Function selects the tangential spring vector for particle-particle interations (also removed not use...
CTangentialSpring * select_particle_spring(int P, Mdouble time_, Mdouble dt)
Function selects the tangential spring vector for particle-particle interations (also removed not use...
void set_zero()
Definition: Vector.h:55
CTangentialSpring * create_new_wall(int W, Mdouble time_, Mdouble dt)
bool has_particle_spring(int P, Mdouble time_)
Vec3D SlidingForce
Stores the force (for some non-linear, hysteretic spring models)
Mdouble time
stores the last time the tangential spring was read (if it was not read during the last timestep...
CTangentialSpring(int pParticle_, int pWall_, Mdouble time_)
Implementation of a 3D vector (by Vitaliy).
Definition: Vector.h:40
CTangentialSpring * create_new(int P, Mdouble time_, Mdouble dt, CTangentialSpring &CTS)
CTangentialSpring(const CTangentialSpring &ts)
copy constructor
friend std::istream & operator>>(std::istream &is, CTangentialSpring &p)
reads spring