pietrod21

pianeti(lab1)

Dec 30th, 2011
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <math.h>
  4.  
  5.  
  6. #define M 1.989*pow(10,33)
  7. #define UA 1.496*pow(10,13)          
  8. #define anno 3.156*pow(10,7)
  9. #define gamma (4*pow(M_PI,2)*pow(UA,3))/pow(anno,2)
  10.  
  11.  
  12. //prototipi
  13.  
  14. struct coordinate {
  15.  
  16.   double x;
  17.   double y;
  18.  
  19. };
  20.  
  21. struct rvf {
  22.  
  23.   double ra;
  24.   double rb;
  25.   double va;
  26.   double vb;
  27.   double fia;
  28.   double fib;
  29.  
  30. };
  31.  
  32.  
  33. double stampa(char *messaggio);
  34.  
  35. void verletauto(struct coordinate a0,struct coordinate b0, struct coordinate v0a, struct coordinate v0b,double ma,double mb,double dt,double e,long int numeropassi);
  36.  
  37.  
  38. main(){
  39.  
  40.   double ma,mb,tempototale,dt,e;
  41.   struct coordinate a0,b0,v0a,v0b;
  42.   long int numeropassi;
  43.  
  44.   // chiediamo i dati iniziali
  45.  
  46.   tempototale=stampa("inserire il tempo totale di integrazione");
  47.   dt=stampa("inserire dt");
  48.   numeropassi=(long int)(tempototale/dt);
  49.  
  50.   ma=stampa("inserire la massa del primo pianeta");
  51.   mb=stampa("inserire la massa del secondo pianeta");
  52.  
  53.   a0.x=stampa("inserire la coordinata x del primo pianeta");
  54.   a0.y=stampa("inserire la coordinata y del primo pianeta");
  55.   b0.x=stampa("inserire la coordinata x del secondo pianeta");
  56.   b0.y=stampa("inserire la coordinata y del secondo pianeta");
  57.  
  58.   v0a.x=stampa("inserire la coordinata x della velocita' del primo pianeta");
  59.   v0a.y=stampa("inserire la coordinata y della velocita' del primo pianeta");
  60.   v0b.x=stampa("inserire la coordinata x della velocita' del secondo pianeta");
  61.   v0b.y=stampa("inserire la coordinata y della velocita' del secondo pianeta");
  62.  
  63.   e=stampa("inserire la costante di accoppiamento");
  64.  
  65.   // chiamiamo la funzione che integra la traiettoria dei pianeti  
  66.  
  67.   verletauto(a0,b0,v0a,v0b,ma,mb,dt,e,numeropassi);
  68.  
  69. }
  70.  
  71.  
  72. double stampa(char *messaggio){
  73.  
  74.   double input;
  75.  
  76.   printf("%s\n",messaggio);
  77.   fflush(stdout);
  78.  
  79.   scanf("%lf",&input);
  80.  
  81.   printf("il valore inserito e'%lf\n\n\n",input);
  82.  
  83.   return input;
  84. }
  85.  
  86.  
  87. void verletauto(struct coordinate a0,struct coordinate b0, struct coordinate v0a, struct coordinate v0b,double ma,double mb,double dt,double e,long int numeropassi){
  88.  
  89.    long int i;
  90.    double mua,mub,ra0,rb0,rab0,rabn,anx,any,bnx,bny,abnx,abny,vnax,vnay,vnbx,vnby;
  91.   struct coordinate ab0,fia0,fib0,fian,fibn;
  92.   struct rvf fine;
  93.   FILE *fp;
  94.  
  95.   fp=fopen("pianeti.dat","w");
  96.  
  97.   if((fp=fopen("pianeti.dat","w"))==NULL){
  98.     printf("errore nell'apertura del file pianeti.dat\n");
  99.     exit(EXIT_FAILURE);
  100.   }
  101.  
  102.   fprintf(fp,"# tempo     ax     ay     bx      by       ra      rb       va     vb        fia      fib\n");
  103.  
  104.   mua=ma/M;
  105.   mub=mb/M;
  106.  
  107.  
  108.   for(i=0;i<numeropassi;i++){
  109.    
  110.     ab0.x=b0.x-a0.x;
  111.     ab0.y=b0.y-a0.y;
  112.    
  113.     ra0=sqrt(pow(a0.x,2)+pow(a0.y,2));
  114.     rb0=sqrt(pow(b0.x,2)+pow(b0.y,2));
  115.    
  116.     rab0=rb0-ra0;
  117.    
  118.     fia0.x=((-gamma*a0.x)/pow(ra0,3))+(e*((gamma*mub*ab0.x)/pow(rab0,3)));
  119.     fia0.y=((-gamma*a0.y)/pow(ra0,3))+(e*((gamma*mub*ab0.y)/pow(rab0,3)));   
  120.    
  121.     fib0.x=((-gamma*b0.x)/pow(rb0,3))-(e*((gamma*mua*ab0.x)/pow(rab0,3)));
  122.     fib0.y=((-gamma*b0.y)/pow(rb0,3))-(e*((gamma*mua*ab0.y)/pow(rab0,3)));   
  123.    
  124.    
  125.     anx=a0.x+(v0a.x*dt)+(0.5*fia0.x*dt*dt);
  126.     any=a0.y+(v0a.y*dt)+(0.5*fia0.y*dt*dt);
  127.  
  128.     bnx=b0.x+(v0b.x*dt)+(0.5*fib0.x*dt*dt);
  129.     bny=b0.y+(v0b.y*dt)+(0.5*fib0.y*dt*dt);
  130.    
  131.    
  132.     abnx=bnx-anx;
  133.     abny=bny-any;
  134.    
  135.     fine.ra=sqrt(pow(anx,2)+pow(any,2));
  136.     fine.rb=sqrt(pow(bnx,2)+pow(bny,2));
  137.    
  138.     rabn=fine.rb-fine.ra;
  139.    
  140.    
  141.     fian.x=((-gamma*anx)/pow(fine.ra,3))+(e*((gamma*mub*(abnx))/pow(rabn,3)));
  142.     fian.y=((-gamma*any)/pow(fine.ra,3))+(e*((gamma*mub*(abny))/pow(rabn,3)));   
  143.    
  144.     fibn.x=((-gamma*bnx)/pow(fine.rb,3))-(e*((gamma*mua*(abnx))/pow(rabn,3)));
  145.     fibn.y=((-gamma*bny)/pow(fine.rb,3))-(e*((gamma*mua*(abny))/pow(rabn,3)));   
  146.    
  147.     vnax=v0a.x+(0.5*(fia0.x+fian.x))*dt;
  148.     vnay=v0a.y+(0.5*(fia0.y+fian.y))*dt;
  149.    
  150.     vnbx=v0b.x+(0.5*(fib0.x+fibn.x))*dt;
  151.     vnby=v0b.y+(0.5*(fib0.y+fibn.y))*dt;
  152.    
  153.     fine.va=sqrt(pow(vnax,2)+pow(vnay,2));
  154.     fine.vb=sqrt(pow(vnbx,2)+pow(vnby,2));
  155.    
  156.     fine.fia=sqrt(pow(fian.x,2)+pow(fian.y,2));
  157.     fine.fib=sqrt(pow(fibn.x,2)+pow(fibn.y,2));
  158.    
  159.     fprintf(fp,"%lf %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf\n",(double)i*dt,a0.x,a0.y,b0.x,b0.y,fine.ra,fine.rb,fine.va,fine.vb,fine.fia,fine.fib);
  160.    
  161.     a0.x=anx;
  162.     a0.y=any;
  163.    
  164.     b0.x=bnx;
  165.     b0.y=bny;
  166.    
  167.     ab0.x=abnx;
  168.     ab0.y=abny;
  169.    
  170.     v0a.x=vnax;
  171.     v0a.y=vnay;
  172.    
  173.     v0b.x=vnbx;
  174.     v0b.y=vnby;
  175.    
  176.   }
  177.  
  178.   fclose(fp);
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment