int main(int argc,char *argv[]) { /* rotates input vector by specified angle around given rotation axis */ VECTOR r,n,nxr,rp; double phi,cphi,sphi,ndotr; if (argc != 8) { (void) fprintf(stderr,"Usage: %s x y z phi rx ry rz\n",argv[0]); return 1; } SET_VEC(r,atof(argv[1]),atof(argv[2]),atof(argv[3])); assert(MAG(r) > 0.0); phi = atof(argv[4]); SET_VEC(n,atof(argv[5]),atof(argv[6]),atof(argv[7])); assert(MAG(n) > 0.0); ndotr = DOT(n,r); CROSS(n,r,nxr); cphi = cos(phi); sphi = sin(phi); SCALE_VEC(r,cphi); SCALE_VEC(n,ndotr*(1.0-cphi)); SCALE_VEC(nxr,sphi); ADD_VEC(r,n,rp); ADD_VEC(rp,nxr,rp); (void) printf("%g %g %g\n",rp[X],rp[Y],rp[Z]); return 0; }
void reset_vel(PARAMS *p,double f,SSDATA *d) { VECTOR r_hat,vr_vec,t_hat,vt_vec; double v,vr,vt; /* set speed scaled by desired virial fraction */ SCALE_VEC(d->vel,sqrt(f)); /* adjust radial and tangential components as desired */ if (p->radf < 0) return; COPY_VEC(d->pos,r_hat); NORM_VEC(r_hat,MAG(r_hat)); /* radial unit vector */ vr = DOT(d->vel,r_hat); COPY_VEC(r_hat,vr_vec); SCALE_VEC(vr_vec,vr); /* current radial component of vel */ SUB_VEC(d->vel,vr_vec,t_hat); NORM_VEC(t_hat,MAG(t_hat)); /* tangential unit vector */ v = MAG(d->vel); vr = sqrt(p->radf)*v; vt = sqrt(1 - p->radf)*v; COPY_VEC(r_hat,vr_vec); SCALE_VEC(vr_vec,vr); /* new radial component */ COPY_VEC(t_hat,vt_vec); SCALE_VEC(vt_vec,vt); /* new tangential component */ ADD_VEC(vr_vec,vt_vec,d->vel); }
int decode_direct(const struct VEC* in_vec, struct VEC* out_vec ,const BIT** inv_submatrix) { if(!in_vec || !out_vec || !inv_submatrix) return ERR_Pointer; int i,j; size_t dsize = in_vec->d_size; BYTE* a =NULL; for(i=0; i<CLOUMN ; ++i) { a = out_vec->base[i]; memset(a,0,dsize); for(j=0; j<CLOUMN; ++j) { if(inv_submatrix[i][j]) { ADD_VEC(a, (in_vec->base)[j] , dsize); } } out_vec->base[i] = a; } }
int decode(const struct VEC* in_vec, struct VEC* out_vec , BIT** submatrix) { if(!in_vec || !out_vec || !submatrix) return ERR_Pointer; size_t i,j; size_t dsize = in_vec->d_size; BIT **inv_submatrix = NULL; BYTE* a = NULL; inv_submatrix = inv(submatrix, CLOUMN); for(i=0; i<CLOUMN ; ++i) { a = out_vec->base[i]; memset(a,0,dsize); for(j=0; j<CLOUMN; ++j) { if(inv_submatrix[i][j]) { ADD_VEC(a, (in_vec->base)[j] , dsize); } } out_vec->base[i] = a; } FREE_MATRIX(inv_submatrix,CLOUMN,CLOUMN); return 0; }
static void adj_com_vel(SSDATA *d,int n,PROPERTIES *p,VECTOR v) { int i; for (i=0;i<n;i++) { SUB_VEC(d[i].vel,p->com_vel,d[i].vel); ADD_VEC(d[i].vel,v,d[i].vel); } }
static void adj_com_pos(SSDATA *d,int n,PROPERTIES *p,VECTOR v) { int i; for (i=0;i<n;i++) { SUB_VEC(d[i].pos,p->com_pos,d[i].pos); ADD_VEC(d[i].pos,v,d[i].pos); } }
static void scale_vel_dsp(SSDATA *d,int n,PROPERTIES *p,VECTOR v) { int i,k; for (i=0;i<n;i++) { SUB_VEC(d[i].vel,p->com_vel,d[i].vel); for (k=0;k<N_DIM;k++) d[i].vel[k] *= v[k]; } for (i=0;i<n;i++) { ADD_VEC(d[i].vel,p->com_vel,d[i].vel); } }
static void adj_ang_mom(SSDATA *d,int n,PROPERTIES *p,VECTOR v) { VECTOR u,w; int i; invert(p->inertia); SUB_VEC(v,p->ang_mom,v); Transform(p->inertia,v,u); SCALE_VEC(u,p->total_mass); for (i=0;i<n;i++) { CROSS(u,d[i].pos,w); ADD_VEC(d[i].vel,w,d[i].vel); } }
int encode(const struct VEC *in_vec, struct VEC* out_vec) //数据编码 { if(!in_vec || !out_vec ) return ERR_Pointer; BYTE* a =NULL; for(int i=0;i<ROW;++i) //G的每一行 { a = out_vec->base[i]; memset(a,0,in_vec->d_size); for(int j=0; j<CLOUMN;j++) //该行的每一个元素 { if(G[i][j]) { ADD_VEC(a, (in_vec->base)[j] , in_vec->d_size ); } } out_vec->base[i] = a; } return 0; }
int main(int argc,char *argv[]) { FILE *fp; BOOLEAN sim_units; RUBBLE_PILE rp[MAX_NUM_FILES],*p; char infile[MAXPATHLEN],last_infile[MAXPATHLEN],outfile[MAXPATHLEN]; double time = 0.0,old_time = 0.0; int n_files; setbuf(stdout,(char *)NULL); srand(getpid()); if (argc > 1) { (void) fprintf(stderr,"%s takes no arguments\n",argv[0]); return 1; } fp = fopen(LOG_FILE,"r"); if (fp) { (void) fclose(fp); if (!get_yn("Overwrite log file","y")) return 0; } fp = fopen(LOG_FILE,"w"); if (!fp) { (void) fprintf(stderr,"Unable to open %s for writing\n",LOG_FILE); return 1; } sim_units = get_yn("Use simulation units (AU, M_sun, etc.)","n"); n_files = 0; while (n_files < MAX_NUM_FILES) { infile[0] = '\0'; (void) printf("File %i [or RETURN to quit]: ",n_files + 1); (void) fgets(infile,MAXPATHLEN,stdin); assert(strlen(infile)); infile[strlen(infile) - 1] = '\0'; /* get rid of newline at end */ if (!strlen(infile)) break; p = &rp[n_files]; if (process(infile,p,sim_units,&time)) continue; if (n_files == 0) old_time = time; else if (time != old_time) time = 0.0; /* unless all times are the same, set to zero */ (void) fprintf(fp, "File number\t\t%i\n" "Filename\t\t%s\n" "Mass\t\t\t%e\n" "Bulk radius\t\t%e\n" "Bulk density\t\t%e\n" "Position\t\t%+e\t%+e\t%+e\n" "Velocity\t\t%+e\t%+e\t%+e\n" "Spin\t\t\t%+e\t%+e\t%+e\n" "Major axis\t\t%+f\t%+f\t%+f\n" "Inter axis\t\t%+f\t%+f\t%+f\n" "Minor axis\t\t%+f\t%+f\t%+f\n" "Color\t\t\t%i\n" "Aggregate ID\t\t%i\n\n", n_files,infile,p->mass,p->radius,p->density, p->pos[X],p->pos[Y],p->pos[Z], p->vel[X],p->vel[Y],p->vel[Z], p->spin[X],p->spin[Y],p->spin[Z], p->axes[rp->axis_ord[X]][X], p->axes[rp->axis_ord[X]][Y], p->axes[rp->axis_ord[X]][Z], p->axes[rp->axis_ord[Y]][X], p->axes[rp->axis_ord[Y]][Y], p->axes[rp->axis_ord[Y]][Z], p->axes[rp->axis_ord[Z]][X], p->axes[rp->axis_ord[Z]][Y], p->axes[rp->axis_ord[Z]][Z], p->color,p->agg_id); (void) strcpy(last_infile,infile); if (++n_files == MAX_NUM_FILES) (void) printf("File limit reached\n"); } if (n_files == 0) { (void) fprintf(stderr,"No files specified!\n"); return 1; } if (get_yn("Recenter barycentric position and velocity","y")) { VECTOR pos,vel,r,v; double total_mass; int i; total_mass = 0; ZERO_VEC(pos); ZERO_VEC(vel); for (i=0;i<n_files;i++) { COPY_VEC(rp[i].pos,r); SCALE_VEC(rp[i].pos,-1); rpuApplyPos(&rp[i]); SCALE_VEC(r,rp[i].mass); ADD_VEC(pos,r,pos); COPY_VEC(rp[i].vel,v); SCALE_VEC(rp[i].vel,-1); rpuApplyVel(&rp[i]); SCALE_VEC(v,rp[i].mass); ADD_VEC(vel,v,vel); total_mass += rp[i].mass; } NORM_VEC(pos,total_mass); NORM_VEC(vel,total_mass); for (i=0;i<n_files;i++) { SCALE_VEC(rp[i].pos,-1); SUB_VEC(rp[i].pos,pos,rp[i].pos); rpuApplyPos(&rp[i]); SCALE_VEC(rp[i].vel,-1); SUB_VEC(rp[i].vel,vel,rp[i].vel); rpuApplyVel(&rp[i]); } (void) fprintf(fp,"BARYCENTRIC CORRECTION APPLIED\n"); } (void) fclose(fp); if (n_files == 2) show_encounter(rp,n_files,sim_units); do { (void) printf("Output file [default %s]: ",last_infile); (void) fgets(outfile,MAXPATHLEN,stdin); assert(strlen(outfile)); outfile[strlen(outfile) - 1] = '\0'; if (!strlen(outfile)) (void) strcpy(outfile,last_infile); outfile[MAXPATHLEN - 1] = '\0'; if ((fp = fopen(outfile,"r"))) { (void) fclose(fp); if (!get_yn("Output file already exists...overwrite","n")) continue; } break; } while (/*CONSTCOND*/1); write_data(outfile,rp,n_files,time); for (--n_files;n_files>=0;n_files--) rpuFree(&rp[n_files]); (void) printf("Done!\n"); return 0; }
static int process(char *filename,RUBBLE_PILE *rp,BOOLEAN sim_units,double *time) { enum {next,mass,radius,density,pos,vel,orient,spin,color,agg_id,par_id}; SSIO ssio; SSHEAD h; int i,choice; assert(rp != NULL); *time = 0.0; if (ssioOpen(filename,&ssio,SSIO_READ)) { (void) fprintf(stderr,"Unable to open \"%s\"\n",filename); return 1; } if (ssioHead(&ssio,&h) || h.n_data < 0) { (void) fprintf(stderr,"Corrupt header\n"); (void) ssioClose(&ssio); return 1; } if (h.n_data == 0) { (void) fprintf(stderr,"No data found!"); (void) ssioClose(&ssio); return 1; } switch(h.iMagicNumber) { case SSIO_MAGIC_STANDARD: break; case SSIO_MAGIC_REDUCED: (void) fprintf(stderr,"Reduced ss format not supported.\n"); ssioClose(&ssio); return 1; default: (void) fprintf(stderr,"Unrecognized ss file magic number (%i).\n",h.iMagicNumber); ssioClose(&ssio); return 1; } rp->n_particles = h.n_data; *time = h.time; (void) printf("Number of particles = %i (time %g)\n",rp->n_particles,*time); rpuMalloc(rp); for (i=0;i<rp->n_particles;i++) if (ssioData(&ssio,&rp->data[i])) { (void) fprintf(stderr,"Corrupt data\n"); (void) ssioClose(&ssio); return 1; } (void) ssioClose(&ssio); while (/*CONSTCOND*/1) { rpuAnalyze(rp); (void) printf("%i. Total mass = ",mass); if (sim_units) (void) printf("%g M_sun",rp->mass); else (void) printf("%g kg",rp->mass*M_SCALE); (void) printf("\n"); (void) printf("%i. Bulk radius = ",radius); if (sim_units) (void) printf("%g AU",rp->radius); else (void) printf("%g km",rp->radius*0.001*L_SCALE); (void) printf("\n"); (void) printf(" [Bulk semi-axes: "); if (sim_units) (void) printf("%g %g %g AU", rp->axis_len[rp->axis_ord[X]], rp->axis_len[rp->axis_ord[Y]], rp->axis_len[rp->axis_ord[Z]]); else (void) printf("%g %g %g km", rp->axis_len[rp->axis_ord[X]]*0.001*L_SCALE, rp->axis_len[rp->axis_ord[Y]]*0.001*L_SCALE, rp->axis_len[rp->axis_ord[Z]]*0.001*L_SCALE); (void) printf("]\n"); (void) printf("%i. Bulk density = ",density); if (sim_units) (void) printf("%g M_sun/AU^3",rp->density); else (void) printf("%g g/cc",rp->density*0.001*D_SCALE); (void) printf("\n"); (void) printf("%i. Centre-of-mass position = ",pos); if (sim_units) (void) printf("%g %g %g AU",rp->pos[X],rp->pos[Y], rp->pos[Z]); else (void) printf("%.2f %.2f %.2f km",rp->pos[X]*0.001*L_SCALE, rp->pos[Y]*0.001*L_SCALE,rp->pos[Z]*0.001*L_SCALE); (void) printf("\n"); (void) printf("%i. Centre-of-mass velocity = ",vel); if (sim_units) (void) printf("%g %g %g x 30 km/s",rp->vel[X],rp->vel[Y], rp->vel[Z]); else (void) printf("%.2f %.2f %.2f m/s",rp->vel[X]*V_SCALE, rp->vel[Y]*V_SCALE,rp->vel[Z]*V_SCALE); (void) printf("\n"); (void) printf("%i. Orientation: a1 = %6.3f %6.3f %6.3f\n",orient, rp->axes[rp->axis_ord[X]][X], rp->axes[rp->axis_ord[X]][Y], rp->axes[rp->axis_ord[X]][Z]); (void) printf(" a2 = %6.3f %6.3f %6.3f\n", rp->axes[rp->axis_ord[Y]][X], rp->axes[rp->axis_ord[Y]][Y], rp->axes[rp->axis_ord[Y]][Z]); (void) printf(" a3 = %6.3f %6.3f %6.3f\n", rp->axes[rp->axis_ord[Z]][X], rp->axes[rp->axis_ord[Z]][Y], rp->axes[rp->axis_ord[Z]][Z]); (void) printf("%i. Spin = ",spin); if (sim_units) (void) printf("%g %g %g x 2pi rad/yr", rp->spin[X],rp->spin[Y],rp->spin[Z]); else { double scale = 3600/(TWO_PI*T_SCALE),w; (void) printf("%.2f %.2f %.2f 1/h (",rp->spin[X]*scale, rp->spin[Y]*scale,rp->spin[Z]*scale); w = MAG(rp->spin); if (w) (void) printf("period %g h",1/(w*scale)); else (void) printf("no spin"); (void) printf(")"); } (void) printf("\n"); (void) printf(" [Ang mom = "); if (sim_units) (void) printf("%g %g %g (sys units)",rp->ang_mom[X], rp->ang_mom[Y],rp->ang_mom[Z]); else { double scale = M_SCALE*SQ(L_SCALE)/T_SCALE; (void) printf("%.5e %.5e %.5e N m/s",rp->ang_mom[X]*scale, rp->ang_mom[Y]*scale,rp->ang_mom[Z]*scale); } (void) printf("]\n"); (void) printf(" [Effective spin = "); if (sim_units) (void) printf("%g x 2pi rad/yr",rp->eff_spin); else { double scale = 3600/(TWO_PI*T_SCALE); (void) printf("%.2f 1/h (",rp->eff_spin*scale); if (rp->eff_spin) printf("period %g h",1/(rp->eff_spin*scale)); else (void) printf("no spin"); (void) printf(")"); } (void) printf("]\n"); (void) printf(" [Rotation index = %.2f (",rp->rot_idx); if (rp->eff_spin == 0.0) (void) printf("undefined"); else if (rp->rot_idx == 1.0) (void) printf("unif rot about max moment"); else if (rp->rot_idx < 1.0 && rp->rot_idx > 0.0) (void) printf("SAM"); else if (rp->rot_idx == 0.0) (void) printf("unif rot about mid moment"); else if (rp->rot_idx < 0.0 && rp->rot_idx > -1.0) (void) printf("LAM"); else if (rp->rot_idx == -1.0) (void) printf("unif rot about min moment"); else assert(0); (void) printf(")]\n"); (void) printf("%i. Color = %i (%s)\n",color,(int) rp->color, color_str(rp->color)); (void) printf("%i. Aggregate ID = ",agg_id); if (rp->agg_id < 0) (void) printf("N/A"); else (void) printf("%i",(int) rp->agg_id); (void) printf("\n"); (void) printf("%i. Particle ID\n",par_id); do { (void) printf("Enter number to change (or 0 to continue): "); (void) scanf("%i",&choice); (void) getchar(); } while (choice < next || choice > par_id); if (choice == next) return 0; switch(choice) { case mass: { double f; BOOLEAN const_den = get_yn("Keep bulk density constant","n"),proceed=TRUE; do { (void) printf("Enter mass scaling factor (-ve ==> abs val): "); (void) scanf("%lf",&f); if (f == 0.0) { getchar(); proceed = get_yn("WARNING: This will set all particle masses to zero...continue","n"); } } while (!proceed); if (f < 0) { if (!sim_units) f /= M_SCALE; f = -f/rp->mass; } rpuScaleMass(rp,f); if (const_den) rpuScaleRadius(rp,pow(f,1.0/3),FALSE); break; } case radius: { double f; BOOLEAN just_particles = get_yn("Just scale particles","n"),const_den = FALSE; if (!just_particles) const_den = get_yn("Keep bulk density constant","n"); do { (void) printf("Enter radius scaling factor " "(-ve ==> abs val): "); (void) scanf("%lf",&f); } while (f == 0); getchar(); if (f < 0) { if (!sim_units) f *= 1000/L_SCALE; if (!just_particles) f = -f/rp->radius; } rpuScaleRadius(rp,f,just_particles); if (just_particles) rpuCalcRadius(rp); /* because outer edge may have changed */ if (const_den) rpuScaleMass(rp,CUBE(f)); break; } case density: { double f; BOOLEAN const_radius = get_yn("Keep radius constant","y"); do { (void) printf("Enter density scaling factor (-ve ==> abs val): "); (void) scanf("%lf",&f); } while (f == 0); getchar(); if (f < 0) { if (!sim_units) f *= 1000/D_SCALE; f = -f/rp->density; } if (const_radius) rpuScaleMass(rp,f); else rpuScaleRadius(rp,pow(f,-1.0/3),FALSE); break; } case pos: { BOOLEAN absolute = get_yn("Specify absolute position","y"); if (absolute) { SCALE_VEC(rp->pos,-1.0); rpuApplyPos(rp); /* reset COM to (0,0,0) first */ (void) printf("Enter new position [x y z in "); } else (void) printf("Enter position offset [x y z in "); if (sim_units) (void) printf("AU"); else (void) printf("km"); (void) printf("]: "); (void) scanf("%lf%lf%lf",&rp->pos[X],&rp->pos[Y],&rp->pos[Z]); (void) getchar(); if (!sim_units) NORM_VEC(rp->pos,0.001*L_SCALE); rpuApplyPos(rp); break; } case vel: { BOOLEAN absolute = get_yn("Specify absolute velocity","y"); if (absolute) { SCALE_VEC(rp->vel,-1.0); rpuApplyVel(rp); (void) printf("Enter new velocity [vx vy vz in "); } else (void) printf("Enter velocity offset [vx vy vz in "); if (sim_units) (void) printf("units of 30 km/s"); else (void) printf("m/s"); (void) printf("]: "); (void) scanf("%lf%lf%lf",&rp->vel[X],&rp->vel[Y],&rp->vel[Z]); (void) getchar(); if (!sim_units) NORM_VEC(rp->vel,V_SCALE); rpuApplyVel(rp); break; } case orient: { enum {x=1,y,z}; MATRIX rot; double angle; BOOLEAN align,body,rndm; int choice; align = get_yn("Align body axes with coordinate axes","n"); if (align) { MATRIX m; COPY_VEC(rp->axes[MAJOR(rp)],m[X]); COPY_VEC(rp->axes[INTER(rp)],m[Y]); COPY_VEC(rp->axes[MINOR(rp)],m[Z]); rpuRotate(rp,m,FALSE); break; } body = get_yn("Use body axes","n"); (void) printf("%i. Rotate about %s axis\n",x,body?"major":"x"); (void) printf("%i. Rotate about %s axis\n",y,body?"intermediate":"y"); (void) printf("%i. Rotate about %s axis\n",z,body?"minor":"z"); do { (void) printf("Enter choice: "); (void) scanf("%i",&choice); } while (choice < x || choice > z); --choice; /* to conform with X,Y,Z macros */ (void) getchar(); rndm = get_yn("Use random angle","n"); if (rndm) angle = TWO_PI*rand()/RAND_MAX; else { (void) printf("Enter rotation angle in "); if (sim_units) (void) printf("radians"); else (void) printf("degrees"); (void) printf(" (-ve=clockwise): "); (void) scanf("%lf",&angle); (void) getchar(); if (!sim_units) angle *= DEG_TO_RAD; } UNIT_MAT(rot); if (body) choice = rp->axis_ord[choice]; switch (choice) { case X: rot[Y][Y] = cos(angle); rot[Y][Z] = -sin(angle); rot[Z][Y] = -rot[Y][Z]; rot[Z][Z] = rot[Y][Y]; break; case Y: rot[X][X] = cos(angle); rot[X][Z] = sin(angle); rot[Z][X] = -rot[X][Z]; rot[Z][Z] = rot[X][X]; break; case Z: rot[X][X] = cos(angle); rot[X][Y] = -sin(angle); rot[Y][X] = -rot[X][Y]; rot[Y][Y] = rot[X][X]; break; default: assert(0); } rpuRotate(rp,rot,body); break; } case spin: { VECTOR old_spin,d; double w_max = 2*PI/sqrt(3*PI/rp->density); BOOLEAN incr_only,ang_mom,body; COPY_VEC(rp->spin,old_spin); /* needed for spin increment */ /*DEBUG following only removes net spin---it does not ensure that every particle has wxr_i = 0 and w_i = 0*/ SCALE_VEC(rp->spin,-1.0); rpuAddSpin(rp,FALSE); /* remove current spin */ (void) printf("Classical breakup limit for measured density = "); if (sim_units) (void) printf("%g x 2pi rad/yr",w_max); else (void) printf("%g 1/h (P_min = %g h)", 3600*w_max/(TWO_PI*T_SCALE), TWO_PI*T_SCALE/(3600*w_max)); (void) printf("\n"); incr_only = get_yn("Specify increment only, instead of absolute value","n"); if (incr_only) ang_mom = get_yn("Specify angular momentum increment","n"); else ang_mom = get_yn("Specify angular momentum","n"); if (ang_mom) { if (incr_only) (void) printf("Enter angular momentum increment [dlx dly dlz in "); else (void) printf("Enter new angular momentum [lx ly lz in "); if (sim_units) (void) printf("sys units"); else (void) printf("N m/s"); (void) printf("]: "); (void) scanf("%lf%lf%lf",&d[X],&d[Y],&d[Z]); (void) getchar(); if (!sim_units) SCALE_VEC(d,T_SCALE/(SQ(L_SCALE)*M_SCALE)); if (incr_only) { ADD_VEC(rp->ang_mom,d,rp->ang_mom); } else { COPY_VEC(d,rp->ang_mom); } rpuAddAngMom(rp); if (MAG(rp->spin) > w_max) (void) printf("WARNING: exceeds classical breakup limit\n"); break; } body = get_yn("Use body axes","n"); if (incr_only) (void) printf("Enter spin increment [dwx dwy dwz in "); else (void) printf("Enter new spin [wx wy wz in "); if (sim_units) (void) printf("units of 2pi rad/yr"); else (void) printf("1/h"); (void) printf("]: "); (void) scanf("%lf%lf%lf",&d[X],&d[Y],&d[Z]); (void) getchar(); if (!sim_units) SCALE_VEC(d,TWO_PI*T_SCALE/3600); if (incr_only) { ADD_VEC(old_spin,d,rp->spin); } else { COPY_VEC(d,rp->spin); } if (MAG(rp->spin) > w_max) (void) printf("WARNING: exceeds classical breakup limit\n"); rpuAddSpin(rp,body); break; } case color: { BOOLEAN invalid_color; int c; (void) printf("Color scheme:\n"); for (i=BLACK;i<FIRST_GRAY;i++) (void) printf("%2i. %s\n",i,color_str(i)); do { invalid_color = FALSE; (void) printf("Enter new color: "); (void) scanf("%i",&c); (void) getchar(); if (c >= NUM_COLORS) { /* allow negative colors */ (void) printf("Invalid color\n"); invalid_color = TRUE; continue; } if (c == BLACK || c == FIRST_GRAY) (void) printf("WARNING: Particles may be invisible!\n"); } while (invalid_color); rp->color = c; rpuApplyColor(rp); break; } case agg_id: { do { (void) printf("Enter new aggregate ID (or -1 to reset): "); (void) scanf("%i",&rp->agg_id); (void) getchar(); if (rp->agg_id < -1) (void) printf("Invalid ID\n"); else rpuApplyAggID(rp); } while(rp->agg_id < -1); break; } case par_id: { SSDATA *p,*pmax; VECTOR r; double lng,lat,dmax,d; int c; (void) printf("Enter angular coordinates [lng lat in deg]: "); (void) scanf("%lf%lf",&lng,&lat); (void) getchar(); lng *= DEG_TO_RAD; lat *= DEG_TO_RAD; SET_VEC(r,cos(lng)*cos(lat),sin(lng)*cos(lat),sin(lat)); dmax = 0.0; pmax = NULL; for (i=0;i<rp->n_particles;i++) { p = &rp->data[i]; /* projected distance along vector minus distance perpendicular to vector favors particles closest to vector */ d = DOT(p->pos,r) - sqrt(MAG_SQ(p->pos) - SQ(DOT(p->pos,r))); if (d > dmax) { dmax = d; pmax = p; } } if (!pmax) { (void) printf("No particle found.\n"); continue; } (void) printf("Found particle (original index %i, color %i [%s])\n", pmax->org_idx,pmax->color,color_str(pmax->color)); while (/*CONSTCOND*/1) { (void) printf("Enter new color: "); (void) scanf("%i",&c); (void) getchar(); if (c >= NUM_COLORS) { /* allow negative colors */ (void) printf("Invalid color\n"); goto invalid; } if (c == BLACK || c == RED || c == YELLOW || c == MAGENTA || c == CYAN || c == KHAKI || c == FIRST_GRAY) { (void) printf("Color %i is reserved\n",c); goto invalid; } break; invalid: (void) printf("Color scheme:\n"); for (i=BLACK;i<FIRST_GRAY;i++) (void) printf("%2i. %s\n",i,color_str(i)); }; /* while */ pmax->color = c; break; } default: assert(0); } } }
static void process(SSDATA *d,int n,double *t) { PROPERTIES p; int choice; enum {Next,Time,Mass,Bounds,ComPos,ComVel,AngMom,VelDsp,Color,Units, Offsets,Masses,Radii,End}; while (/*CONSTCOND*/1) { ss_analyze(d,n,&p); (void) printf("%2i. Time = %g\n",Time,*t); (void) printf("%2i. Total mass = %g\n",Mass,p.total_mass); (void) printf("%2i. Bounds: x=[%g,%g]\n" " y=[%g,%g]\n" " z=[%g,%g]\n",Bounds, p.bnd_min[X],p.bnd_max[X], p.bnd_min[Y],p.bnd_max[Y], p.bnd_min[Z],p.bnd_max[Z]); (void) printf("%2i. Centre-of-mass position = %g %g %g\n",ComPos, p.com_pos[X],p.com_pos[Y],p.com_pos[Z]); (void) printf("%2i. Centre-of-mass velocity = %g %g %g\n",ComVel, p.com_vel[X],p.com_vel[Y],p.com_vel[Z]); (void) printf("%2i. Specific angular momentum = %g %g %g\n",AngMom, p.ang_mom[X],p.ang_mom[Y],p.ang_mom[Z]); (void) printf("%2i. Velocity dispersion = %g %g %g\n",VelDsp, p.vel_dsp[X],p.vel_dsp[Y],p.vel_dsp[Z]); (void) printf("%2i. Dominant color = %i (%s)\n",Color,p.color, color_str(p.color)); (void) printf("%2i. Units\n",Units); (void) printf("%2i. Offsets\n",Offsets); (void) printf("%2i. Particle masses\n",Masses); (void) printf("%2i. Particle radii\n",Radii); do { (void) printf("Enter number to change (or 0 to continue): "); (void) scanf("%i",&choice); } while (choice < Next || choice >= End); (void) getchar(); if (choice == Next) return; switch(choice) { case Time: do { (void) printf("Enter new time: "); (void) scanf("%lf",t); (void) getchar(); } while (*t < 0); break; case Mass: { double f; do get_scaling(&f,NegativeOK); while (f == 0); if (f < 0) f = -f/p.total_mass; scale_mass(d,n,f); break; } case Bounds: { double f,min,max; int i,choice; do { do { (void) printf("%i. Change x bounds (now [%g,%g])\n",X + 1, p.bnd_min[X],p.bnd_max[X]); (void) printf("%i. Change y bounds (now [%g,%g])\n",Y + 1, p.bnd_min[Y],p.bnd_max[Y]); (void) printf("%i. Change z bounds (now [%g,%g])\n",Z + 1, p.bnd_min[Z],p.bnd_max[Z]); (void) printf("Your choice (or 0 when done): "); (void) scanf("%i",&choice); (void) getchar(); } while (choice < 0 || choice > N_DIM); if (choice == 0) break; --choice; /* put back in range [X,Z] */ if (p.bnd_min[choice] == p.bnd_max[choice]) { (void) printf("Chosen dimension is degenerate\n"); continue; } do { (void) printf("Enter new bounds (min max): "); (void) scanf("%lf%lf",&min,&max); (void) getchar(); } while (min > max); if (min == max && get_yn("Zero velocities for this component","y")) for (i=0;i<n;i++) d[i].vel[choice] = 0; f = (max - min)/(p.bnd_max[choice] - p.bnd_min[choice]); for (i=0;i<n;i++) d[i].pos[choice] = (d[i].pos[choice] - p.bnd_min[choice])*f + min; p.bnd_min[choice] = min; p.bnd_max[choice] = max; } while (/*CONSTCOND*/1); break; } case ComPos: { VECTOR v; if (MAG(p.com_pos) && get_yn("Scale the magnitude","y")) { double f; get_scaling(&f,NegativeOK); COPY_VEC(p.com_pos,v); if (f < 0) f = -f/MAG(v); SCALE_VEC(v,f); } else get_components(v); adj_com_pos(d,n,&p,v); break; } case ComVel: { VECTOR v; if (MAG(p.com_vel) && get_yn("Scale the magnitude","y")) { double f; get_scaling(&f,NegativeOK); COPY_VEC(p.com_vel,v); if (f < 0) f = -f/MAG(v); SCALE_VEC(v,f); } else get_components(v); adj_com_vel(d,n,&p,v); break; } case AngMom: { VECTOR v; (void) printf("NOTE: specific angular momentum is measured with\n" "respect to fixed space frame centred at (0,0,0)\n" "and does not take particle spins into account\n"); if (MAG(p.ang_mom) && get_yn("Scale the magnitude","y")) { double f; get_scaling(&f,NegativeOK); COPY_VEC(p.ang_mom,v); if (f < 0) f = -f/MAG(p.ang_mom); SCALE_VEC(v,f); } else if (get_yn("Scale the components","y")) { VECTOR u; int k; get_component_scaling(u); for (k=0;k<N_DIM;k++) v[k] = u[k]*p.ang_mom[k]; } else get_components(v); adj_ang_mom(d,n,&p,v); break; } case VelDsp: { VECTOR v; (void) printf("NOTE: velocity dispersion is context dependent,\n" "for now relative ONLY to center-of-mass velocity,\n" "i.e. without considering bulk rotation or shear\n"); if (!MAG(p.vel_dsp)) { (void) printf("Zero velocity dispersion -- cannot adjust\n"); break; } if (get_yn("Scale the magnitude","y")) { double f; get_scaling(&f,NegativeOK); if (f < 0) f = -f/MAG(p.vel_dsp); SET_VEC(v,f,f,f); } else get_component_scaling(v); scale_vel_dsp(d,n,&p,v); break; } case Color: { int c; (void) printf("Color scheme:\n"); for (c=BLACK;c<FIRST_GRAY;c++) (void) printf("%2i. %s\n",c,color_str(c)); (void) printf("[values from %i to %i are levels of gray]\n", FIRST_GRAY,LAST_GRAY); do { (void) printf("Enter new color: "); (void) scanf("%i",&c); (void) getchar(); } while (c < 0 || c >= NUM_COLORS); change_color(d,n,c); break; } case Units: { enum {N,M,L,T,V,E}; double f; int i,choice; (void) printf("NOTE: It is up to you to ensure dimensions are\n" "internally consistent. pkdgrav assumes G == 1.\n"); do { do { (void) printf("%i. Mass (particle masses)\n",M); (void) printf("%i. Length (particle radii, pos'ns)\n",L); (void) printf("%i. Time (time,particle spins)\n",T); (void) printf("%i. Velocity (particle velocities)\n",V); (void) printf("Select dimension to scale " "(or 0 when done): "); (void) scanf("%i",&choice); (void) getchar(); } while (choice < N || choice >= E); if (choice == N) break; switch (choice) { case M: (void) printf("M_Sun = 1.9891e30 kg\n" "M_Earth = 5.9742e24 kg\n" "M_Jupiter = 1.8992e27 kg\n" "M_Saturn = 5.6864e26 kg\n"); get_scaling(&f,PositiveOnly); for (i=0;i<n;i++) d[i].mass *= f; break; case L: (void) printf("1 AU = 1.49597892e11 m\n" "R_Earth = 6.37814e6 m\n"); get_scaling(&f,PositiveOnly); for (i=0;i<n;i++) { d[i].radius *= f; SCALE_VEC(d[i].pos,f); } break; case T: (void) printf("1 yr = 3.15576e7 s\n" "1 yr / 2 pi = 5.02255e6 s\n"); get_scaling(&f,PositiveOnly); *t *= f; for (i=0;i<n;i++) NORM_VEC(d[i].spin,f); break; case V: (void) printf("V_Earth = 2.97852586e4 m/s\n"); get_scaling(&f,PositiveOnly); for (i=0;i<n;i++) SCALE_VEC(d[i].vel,f); break; default: assert(0); } } while (/*CONSTCOND*/1); break; } case Offsets: { VECTOR v; int i; (void) printf("POSITION OFFSET (0 0 0 for none)...\n"); get_components(v); for (i=0;i<n;i++) ADD_VEC(d[i].pos,v,d[i].pos); (void) printf("VELOCITY OFFSET (0 0 0 for none)...\n"); get_components(v); for (i=0;i<n;i++) ADD_VEC(d[i].vel,v,d[i].vel); break; } case Masses: { double f; do get_scaling(&f,NegativeOK); while (f == 0); scale_masses(d,n,f); break; } case Radii: { double f; do get_scaling(&f,NegativeOK); while (f == 0); scale_radii(d,n,f); break; } default: assert(0); } } }
static void ss_analyze(SSDATA *data,int n_data,PROPERTIES *p) { SSDATA *d; VECTOR r,v,l; int i,k,nc[NUM_COLORS],ncmax; assert(n_data > 0); p->total_mass = 0; ZERO_VEC(p->bnd_min); ZERO_VEC(p->bnd_max); ZERO_VEC(p->com_pos); ZERO_VEC(p->com_vel); ZERO_VEC(p->ang_mom); ZERO_VEC(p->vel_dsp); p->color = 0; for (i=0;i<NUM_COLORS;i++) nc[i] = 0; for (i=0;i<n_data;i++) { d = &data[i]; p->total_mass += d->mass; COPY_VEC(d->pos,r); COPY_VEC(d->vel,v); if (i==0) { COPY_VEC(r,p->bnd_min); COPY_VEC(r,p->bnd_max); } else for (k=0;k<N_DIM;k++) { if (r[k] < p->bnd_min[k]) p->bnd_min[k] = r[k]; if (r[k] > p->bnd_max[k]) p->bnd_max[k] = r[k]; } SCALE_VEC(r,d->mass); ADD_VEC(p->com_pos,r,p->com_pos); SCALE_VEC(v,d->mass); ADD_VEC(p->com_vel,v,p->com_vel); CROSS(d->pos,d->vel,l); SCALE_VEC(l,d->mass); ADD_VEC(p->ang_mom,l,p->ang_mom); ++nc[(int) d->color]; } if (p->total_mass == 0) { (void) printf("WARNING: total mass = 0...will divide by N\n"); p->total_mass = n_data; } NORM_VEC(p->com_pos,p->total_mass); NORM_VEC(p->com_vel,p->total_mass); NORM_VEC(p->ang_mom,p->total_mass); for (i=0;i<n_data;i++) { d = &data[i]; SUB_VEC(d->vel,p->com_vel,v); for (k=0;k<N_DIM;k++) p->vel_dsp[k] += d->mass*SQ(v[k]); } for (k=0;k<N_DIM;k++) p->vel_dsp[k] = sqrt(p->vel_dsp[k]/p->total_mass); ncmax = 0; for (i=0;i<NUM_COLORS;i++) if (nc[i] > ncmax) { p->color = i; ncmax = nc[i]; } calc_inertia(data,n_data,p->inertia); }