HG-MD  1
CTangentialSpring.h
Go to the documentation of this file.
00001 #ifndef CTANGENTIALSPRING_H
00002 #define CTANGENTIALSPRING_H
00003 
00004 #include "Vector.h"
00005 #include <algorithm>
00006 #include <vector>
00007 #include <iostream>
00008 #include <iomanip>
00009 
00010 using namespace std;
00011 
00012 class CWall;
00013 class Particle;
00014 
00017 class CTangentialSpring {
00018 public:
00020         CTangentialSpring() {
00021                 delta.set_zero(); 
00022                 RollingSpring.set_zero(); 
00023                 TorsionSpring.set_zero(); 
00024                 pParticle=-1; 
00025                 pWall=-1; 
00026                 time=-1; 
00027                 sliding = false; 
00028         }
00029         
00030         CTangentialSpring(int pParticle_, int pWall_, Mdouble time_) {  
00031                 delta.set_zero();
00032                 RollingSpring.set_zero();
00033                 TorsionSpring.set_zero();
00034                 pParticle = pParticle_;
00035                 pWall = pWall_;
00036                 time = time_;
00037                 sliding = false;
00038                 //~ cout << endl << "D" << endl;
00039         }
00040         
00042         CTangentialSpring (const CTangentialSpring &ts) {
00043                 delta = ts.delta;
00044                 RollingSpring = ts.RollingSpring;
00045                 TorsionSpring = ts.TorsionSpring;
00046                 pParticle = ts.pParticle;
00047                 pWall = ts.pWall;
00048                 time = ts.time;
00049                 sliding = ts.sliding;
00050                 //~ cout << "c";
00051         }
00052         
00054         void print(std::ostream& os) {
00055                 os << "delta:" << delta 
00056                         << ", particle:" << pParticle
00057                         << ", wall:" << pWall 
00058                         << ", time:" << time;
00059         }
00060         
00062         friend inline std::ostream& operator<<(std::ostream& os, const CTangentialSpring &p) 
00063         {
00064                 os << p.delta << " " << p.RollingSpring << " " << p.TorsionSpring << " " << p.pParticle << " " << p.pWall << " " << p.time;
00065                 return os;
00066         }       
00067         
00069         friend inline std::istream& operator>>(std::istream& is, CTangentialSpring &p) 
00070         {
00071                 is >> p.delta >> p.RollingSpring >> p.TorsionSpring >> p.pParticle >> p.pWall >> p.time;
00072                 return is;
00073         }
00074         
00075         Vec3D delta;
00076         Vec3D forcedelta;
00077         Vec3D RollingSpring;
00078         Vec3D TorsionSpring;
00080         int pParticle;
00082         int pWall;
00084         Mdouble time;
00085 
00086         bool sliding;
00087 };
00088 
00097 class CTangentialSprings : public vector<CTangentialSpring> {
00098 public:
00099 
00101         struct check_spring_time : public binary_function<CTangentialSpring,Mdouble,bool>
00102         {
00103                 bool operator() (const CTangentialSpring a, const Mdouble b) const {return(a.time<b);}
00104         };
00105         
00107         vector<int> to_be_erased(Mdouble time) {
00108                 vector<int> retval;
00109                 for (unsigned int i=0; i<size(); i++)
00110                         if (operator[](i).time<time) {
00111                                 retval.push_back(i);
00112                                 //cout << "to be erased " << time << " " << operator[](i).pParticle << " " << operator[](i).pWall << " " << size() << endl;
00113                         }
00114                 return retval;
00115         }
00116 
00118         void to_erase(Mdouble time) {
00119                 erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time)),end());        
00120         }
00121 
00123         CTangentialSpring* select_particle_spring(int P, Mdouble time_, Mdouble dt) {
00124                 //Remove_if reconstructs the vector with only elements passing the check_spring_time function
00125                 //Erase removes the end of the vector
00126                 erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time_)),end());       
00127                 
00128                 //Loops over all Springs ant returns the correct one (if it exists)
00129                 for (CTangentialSprings::iterator it=begin(); it!=end();it++){
00130                         if (it->pParticle==P)
00131                         {
00132                                 it->time=time_+dt; 
00133                                 return &(*it);
00134                         }
00135                 }
00136                 
00137                 //if not, create it
00138                 push_back(CTangentialSpring(P,-1,time_+dt));
00139                 return &(back());
00140         }
00141 
00143         CTangentialSpring* select_wall_spring(int W, Mdouble time_, Mdouble dt) {
00144                 //Remove_if reconstructs the vector with only elements passing the check_spring_time function
00145                 //Erase removes the end of the vector
00146                 erase(remove_if(begin(),end(),bind2nd(check_spring_time(),time_)),end());       
00147                 
00148                 //Loops over all Springs ant returns the correct one (if it exists)
00149                 for (CTangentialSprings::iterator it=begin(); it!=end();it++){
00150                         if (it->pWall==W)
00151                         {
00152                                 it->time=time_+dt; 
00153                                 return &(*it);
00154                         }
00155                 }
00156                 
00157                 //if not, create it
00158                 //~ cout << "created pw " << time_ << " " << W << endl;
00159                 push_back(CTangentialSpring(-1,W,time_+dt));
00160                 return &(back());
00161         }
00162 
00164         void reset() {
00165                 clear();
00166                 reserve(13);
00167         }
00168         
00170         void print(std::ostream& os, Mdouble time_) {
00171                 os << "Tangential Springs: N=" << size() << endl;
00172                 for (CTangentialSprings::iterator it=begin(); it!=end(); it++)
00173                         if (it->time>=time_) {
00174                                         it->print(os); os << endl; 
00175                         }
00176         }
00177 
00178 public:
00180         friend inline std::ostream& operator<<(std::ostream& os, const CTangentialSprings &p) 
00181         {
00182                 os << p.size() << " ";
00183                 for (unsigned int i=0; i<p.size(); i++) os << p[i] << " ";
00184                 return os;
00185         }       
00186         
00188         friend inline std::istream& operator>>(std::istream& is, CTangentialSprings &p) 
00189         {
00190                 //should return empty spring if read from is failed
00191                 int n; is >> n; 
00192                 if (is.fail()) {
00193                         p.resize(0);
00194                 } else {
00195                         p.resize(n);
00196                         for (unsigned int i=0; i<p.size(); i++) is >> p[i];
00197                 }
00198                 return is;
00199         }
00200 
00201 };
00202 
00203 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines