/* Rigid body motion, tracing angular momentum, both on body and in lab.... compile using "cc -o main main.c -lm" run with ./main problems: - crushes if you give lengths of inertia ellipsoid axes <=0 - probably also similar problem with omega - when change velocity of integration the integration loop stops - need to clean up the main menu of geomview from all these body_curves */ #include #include float I[3]; /* Inertia tensor eigenvalues */ FILE *gv, *tk, *cf ; int body_curve_num=0; # define DIM 12 # define LENGTH 1000 # define sqr(x) ((x)*(x)) # define min(x,y,z) ((x)<(y)? ((x)<(z)? (x):(z)) : ((y)<(z)? (y):(z))) float body_curve[LENGTH][3], lab_curve[LENGTH][3]; # include "../lib/full_euler_equations.c" # include "../lib/runge_kutta_step.c" /********************************************************************* ****************** main **************************** **********************************************************************/ main() {float y[12],t, dt=.05 ,v , L[3]; int curve_length=0, i; char command; void start_geomview(void); void scale_I(void); void update_ellipsoid_shape(void); void scale_angular_velocity(float*); void update_angular_momentum(float*); void update_geomview(float*,int); void quit(void); /* make pipes to Geomview and the Tk interface */ gv=popen("geomview -c - -wpos 500,500@285,70", "w"); tk=popen("/usr/local/etc/httpd/htdocs/talleres/escuela_verano/mecanica_clasica/rigid_body/tk","r"); start_geomview(); /* set initial rotation matrix to identity matrix */ y[3] = 1; y[4] = 0; y[5] = 0; y[6] = 0; y[7]= 1; y[8]= 0; y[9]= 0; y[10]= 0; y[11]= 1; /* set initial eigenvalues of the inertia tensor */ I[0]= 1.; I[1]=1./4. ; I[2]=1./9.; /* scale inertia tensor so that longests axes of inertia elipsiod has length=1 */ scale_I(); /* tell geomview to draw the corresponding inertia tensor */ update_ellipsoid_shape(); /* set initial angular velocity */ y[0]=.2; y[1]=1.; y[2]=.2; /* scale angular velocity so that its tip lies on the inertia ellipsoid */ scale_angular_velocity(y); /* tell geomview to draw ang vel vector and some other stuff */ update_geomview(y,0); /* tell geomview to draw the corresponding angular momentum vector and the poinsoit invariant plane (it is the tangent plane to the ellipsoid at the pt thhe ang vel vect touches it; the ang mom vect is normal to this plane) */ update_angular_momentum(y); /* tell geomview to adjust size of picture */ fprintf(gv,"(look world)"); fflush(gv); while(1) { switch (fgetc(tk)) { case 'v': /* change velocity of integration */ fscanf(tk, "%f", &v); dt=v/20.; break; case 'y': /* change initial ang velocity */ fscanf(tk, "%f %f %f",y,y+1,y+2); scale_angular_velocity(y); update_angular_momentum(y); if (body_curve_num>0) {body_curve_num++; curve_length=0; update_geomview(y,curve_length);} break; case 'L': /* change lengths of axes of inertia tensor */ fscanf(tk, "%f %f %f",L, L+1, L+2); for (i=0;i<3;I[i]=1./sqr(L[i]), i++); scale_I(); update_ellipsoid_shape(); scale_angular_velocity(y); update_angular_momentum(y); if (body_curve_num>0) {for (i=0;i<=body_curve_num;i++) fprintf(gv,"(geometry body_curve%i)",i); curve_length=body_curve_num=0; update_geomview(y,curve_length);}; break; case 'q': quit(); case 'g': /* start integrfation loop */ command='g'; if (body_curve_num==0) {body_curve_num=1;}; for(t=0.; (command=='g') && (curve_length