Advertisement
Guest User

Untitled

a guest
Jul 27th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.99 KB | None | 0 0
  1. // orbit - Program to compute the orbit of a comet.
  2.  
  3.  
  4. /***************************************************************
  5.                 -- Code to use gnuplot --
  6. Include these lines of code at the top of your c or c++ program
  7. to use gnuplot for graphical output.  Call open_gnupipe(), then
  8. use gnuplot("command string") to generate plots.  Finally call
  9. close_gnupipe() before exiting the program.  This routine
  10. assumes that your path includes the gnuplot program.
  11.                     (Dr. Mark C. Fair)
  12. ***************************************************************/
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. FILE *gnu_pipe;
  17. void open_gnupipe(){
  18.     if (( gnu_pipe = popen("gnuplot -persist","w")) == NULL){
  19.         perror("popen failed");
  20.         exit(1); }
  21. }
  22. void gnuplot(char *cmdstr){
  23.     char cmdstr1[strlen(cmdstr)+1];
  24.     sprintf(cmdstr1,"%s\n",cmdstr);
  25.     fputs(cmdstr1,gnu_pipe);
  26.     fflush(gnu_pipe);
  27. }
  28. void close_gnupipe(){
  29.     if (pclose(gnu_pipe) == -1)
  30.         printf("Problem closing communication to Gnuplot\n");
  31. }
  32. /***************************************************************
  33.                 -- End of code to use gnuplot --
  34. ***************************************************************/
  35.  
  36.  
  37.  #include <iostream>
  38.  #include <fstream>
  39.  #include <cmath>
  40.  #include <cassert>
  41.  
  42.  
  43.  using std::cout;
  44.  using std::cin;
  45.  using std::endl;
  46.  using std::ofstream;
  47.  
  48.  
  49. void gravrk(double x[],double t,double param[],double deriv[]);
  50. void rk4( double x[],int nx,double t,double tau,
  51.     void (*derivsRK)(double x[],double t,double param[],double deriv[]), double param[]);
  52.    
  53.  
  54.  int main()
  55.  
  56.  {
  57.  
  58.     double r0,theta0;
  59.    
  60.     cout << "enter in the initial height(cm): ";
  61.     cin >> r0;
  62.     cout << "enter in the initial angle(rads): ";
  63.     cin >> theta0;
  64.  
  65.  
  66.     double state[5],r[3],v[3];
  67.     r[1] = r0; r[2] = theta0; v[1] = 0; v[2] = 0;
  68.     state[1] = r[1]; state[2]= r[2]; state[3] = v[1]; state[4] = v[2];
  69.     int nState = 4;
  70.  
  71.     double pi = 3.14159265;
  72.     double time = 0;
  73.     double mass = .5;
  74.     double I = 1e-4;
  75.     double k = 5;
  76.     double delta = 1e-3;
  77.     double e = 1e-2;
  78.    
  79.     double param[6];
  80.     param[1] = mass;
  81.     param[2] = I;
  82.     param[3] = k;
  83.     param[4] = delta;
  84.     param[5] = e;
  85.    
  86.    
  87.    
  88.     double tau = .01;
  89.     int nStep = 4000;
  90.    
  91.     double *tplot,*rplot,*thplot;
  92.     tplot = new double[nStep+1];
  93.     rplot = new double[nStep+1];
  94.     thplot = new double[nStep+1];
  95.      
  96.    
  97.     for(int iStep = 1; iStep <= nStep; ++iStep){
  98.    
  99.             rplot[iStep] = r[1];               // Record position for plotting
  100.             thplot[iStep] = r[2];
  101.         tplot[iStep] = time;
  102.    
  103.         rk4( state, nState, time, tau, gravrk, param );
  104.         r[1] = state[1]; r[2] = state[2];   // 4th order Runge-Kutta
  105.         v[1] = state[3]; v[2] = state[4];      
  106.         time += tau;
  107.    
  108.     }
  109.    
  110.     ofstream fout("12.txt");
  111.    
  112.     for(int i = 1; i <= nStep; ++i)
  113.     {
  114.         fout << 100*rplot[i]<< " " ;
  115.         fout << thplot[i] << " " ;
  116.         fout << tplot[i] << endl;
  117.    
  118.     }
  119.    
  120.     fout.close();
  121.    
  122.     open_gnupipe();
  123.     gnuplot((char *)"set term x11 3 font 'Times,14'; \
  124.             set title 'wilberforce pendulum' font 'Times,18'; \
  125.             set grid; \
  126.             set xlabel 'Time(s)' font 'Times,18'; \
  127.             set ylabel 'z(cm) theta(radians)' font 'Times,18'; \
  128.             plot '12.txt' using 3:1 with lines title 'Z(t)'; \
  129.             replot '12.txt' using 3:2 with lines title 'theta(t)';\
  130.             set term postscript eps color 'Times' 18; \
  131.             set output '12.eps'; \
  132.             replot");  
  133.     close_gnupipe();
  134.    
  135.    
  136.     delete [] tplot,rplot,thplot;
  137.    
  138.     return 0;
  139.  }
  140.  
  141.      
  142. void gravrk(double x[], double t, double param[], double deriv[]) {
  143. //  Returns right-hand side of Kepler ODE; used by Runge-Kutta routines
  144. //  Inputs
  145. //    x      State vector [r(1) r(2) v(1) v(2)]
  146. //    t      Time (not used)
  147. //    param     mass, I, k, delta, e
  148. //  Output
  149. //    deriv  Derivatives [dr(1)/dt dr(2)/dt dv(1)/dt dv(2)/dt]
  150.  
  151.   //* Compute acceleration
  152.   double mass = param[0], I = param[1], k = param[2], delta = param[3], e = param[4];
  153.   double r1 = x[1], r2 = x[2];     // Unravel the vector s into
  154.   double v1 = x[3], v2 = x[4];     // position and velocity
  155.  
  156.   double accel1 = (-k*r1/mass) - (e*r2*.5/mass);  // Gravitational acceleration
  157.   double accel2 = (-delta*r2/I) - (e*r1*.5/I);  
  158.  
  159.  
  160.   //* Return derivatives [dr[1]/dt dr[2]/dt dv[1]/dt dv[2]/dt]
  161.   deriv[1] = v1;       deriv[2] = v2;
  162.   deriv[3] = accel1;   deriv[4] = accel2;
  163. }
  164.  
  165. void rk4(double x[], int nX, double t, double tau,
  166.   void (*derivsRK)(double x[], double t, double param[], double deriv[]),
  167.   double param[]) {
  168. // Runge-Kutta integrator (4th order)
  169. // Inputs
  170. //   x          Current value of dependent variable
  171. //   nX         Number of elements in dependent variable x
  172. //   t          Independent variable (usually time)
  173. //   tau        Step size (usually time step)
  174. //   derivsRK   Right hand side of the ODE; derivsRK is the
  175. //              name of the function which returns dx/dt
  176. //              Calling format derivsRK(x,t,param,dxdt).
  177. //   param      Extra parameters passed to derivsRK
  178. // Output
  179. //   x          New value of x after a step of size tau
  180.  
  181.   double *F1, *F2, *F3, *F4, *xtemp;
  182.   F1 = new double [nX+1];  F2 = new double [nX+1];
  183.   F3 = new double [nX+1];  F4 = new double [nX+1];
  184.   xtemp = new double [nX+1];
  185.  
  186.   //* Evaluate F1 = f(x,t).
  187.   (*derivsRK)( x, t, param, F1 );  
  188.  
  189.   //* Evaluate F2 = f( x+tau*F1/2, t+tau/2 ).
  190.   double half_tau = 0.5*tau;
  191.   double t_half = t + half_tau;
  192.  
  193.   for(int i=1; i<= nX; ++i)
  194.     xtemp[i] = x[i] + half_tau*F1[i];
  195.   (*derivsRK)( xtemp, t_half, param, F2 );  
  196.  
  197.   //* Evaluate F3 = f( x+tau*F2/2, t+tau/2 ).
  198.   for(int i=1; i<= nX; ++i)
  199.     xtemp[i] = x[i] + half_tau*F2[i];
  200.   (*derivsRK)( xtemp, t_half, param, F3 );
  201.  
  202.   //* Evaluate F4 = f( x+tau*F3, t+tau ).
  203.   double t_full = t + tau;
  204.   for(int i=1; i<= nX; ++i)
  205.     xtemp[i] = x[i] + tau*F3[i];
  206.   (*derivsRK)( xtemp, t_full, param, F4 );
  207.  
  208.   //* Return x(t+tau) computed from fourth-order R-K.
  209.   for(int i=1; i<= nX; ++i){
  210.     x[i] += tau/6.*(F1[i] + F4[i] + 2.*(F2[i]+F3[i]));
  211.     //cout << "x[i]: " << x[i] << endl;
  212.     }
  213.    
  214.   delete [] F1, F2, F3, F4, xtemp;
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement