1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
|
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
/*
* Summary of Data Members
* t It is static member and counts for time of the complete system.
* Resets to zero when a new oscillator attaches.
* m Mass of individual Oscillator Unit
* S Spring Constant of individual Oscillator Unit
* l Lenghth of unstretched spring of individual Oscillator Unit
* x Position of individual Oscillator Unit
* x1 Velocity of individual Oscillator Unit
* x2 Acceleration of of individual Oscillator Unit
* *os Identity of Oscillator with which it is attched to.
*/
class osc
{
osc* os;
static long double t;
long double m,S,l,x,x1,x2;
public:
osc(){m=S=l=x=x1=x2=0; os=NULL;}
osc(double M, double s, double L, double X, double X1, double X2){m=M; S=s; l=L; x=X; x1=X1; x2=X2; os=NULL;}
osc operator =(osc b){return b;}
osc operator +(osc b){osc a=*this; a.attachToIt(b); return a;}
osc operator +=(osc b){attachToIt(b); return *this;}
void chOsc(double M, double s, double L, double X, double X1, double X2){m=M; S=s; l=L; x=X; x1=X1; x2=X2; os=NULL;}
void attachToIt(osc b){os=&b; t=0;}
void resetTime(void){t=0;}
string getParam();
void increment(double Sn, double ln, double xn, double dt, ofstream &output);
void evolve(double dt, double timeToEvolve, ofstream &output);
};
string osc::getParam()
{
ostringstream out;
string a;
out<<"Mass="<<m<<", Spring Constant="<<S<<", Unstretched Length="<<l<<", Position="<<x<<", Velocity="<<x1<<", Acceleration="<<x2<<endl;
if(os!=NULL)
{
a=os->getParam();
out<<"Next Unit\n"<<a;
}
else
{
out<<"Wall Encountered. End!\n";
}
return out.str();
}
void osc::evolve(double dt, double timeToEvolve, ofstream &output)
{
double tf=t+timeToEvolve;
while (t<tf)
{
output<<t;
x+=x1*dt;
x1+=x2*dt;
if(os!=NULL)
{
x2+=dt*(x-os->x-l)*S/m;
output<<"\t"<<x;
os->increment(S,l,x,dt,output);
cout<<endl;
}
else
{
x2+=((x-l)*S/m)*dt;
output<<"\t"<<x;
}
t+=dt;
}
}
void osc::increment(double Sn, double ln, double xn, double dt, ofstream &output)
{
x+=x1*dt;
x1+=x2*dt;
if(os!=NULL)
{
x2+=((x-os->x-l)*S/m+(x-xn-ln)*Sn/m)*dt;
output<<"\t"<<x;
os->increment(S,l,x,dt,output);
}
else
{
x2+=((x-l)*S/m+(x-xn-ln)*Sn/m)*dt;
output<<"\t"<<x;
}
}
long double osc::t=0;
//osc os;
|