main() { xdrstdio_create(&xdrs, stdin, XDR_DECODE); forever { if(xdr_header() != 1) break; if(gas_particles != NULL) free(gas_particles); if(header.nsph != 0) { gas_particles = (struct gas_particle *) malloc(header.nsph*sizeof(*gas_particles)); if(gas_particles == NULL) { printf("<sorry, no memory for gas particles, master>\n") ; return ; } } else gas_particles = NULL; if(dark_particles != NULL) free(dark_particles); if(header.ndark != 0) { dark_particles = (struct dark_particle *) malloc(header.ndark*sizeof(*dark_particles)); if(dark_particles == NULL) { printf("<sorry, no memory for dark particles, master>\n") ; return ; } } else dark_particles = NULL; if(star_particles != NULL) free(star_particles); if(header.nstar != 0) { star_particles = (struct star_particle *) malloc(header.nstar*sizeof(*star_particles)); if(star_particles == NULL) { printf("<sorry, no memory for star particles, master>\n") ; return ; } } else star_particles = NULL; xdr_gas(); xdr_dark(); xdr_star(); fwrite((char *)&header,sizeof(header),1,stdout) ; fwrite((char *)gas_particles,sizeof(struct gas_particle), header.nsph, stdout) ; fwrite((char *)dark_particles,sizeof(struct dark_particle), header.ndark,stdout) ; fwrite((char *)star_particles,sizeof(struct star_particle), header.nstar, stdout) ; fprintf(stderr, "read time %lf\n",header.time) ; } xdr_destroy(&xdrs); }
int main() { struct gas_particle gas; struct dark_particle dark; struct star_particle star; struct dump header; int i; XDR xdrs; setvbuf(stdin, NULL, _IOFBF, 32*4096); setvbuf(stdout, NULL, _IOFBF, 32*4096); xdrstdio_create(&xdrs, stdout, XDR_ENCODE); forever { if(fread((char *)&header,sizeof(header),1,stdin) != 1) break; fprintf(stderr, "reading time %f\n",header.time) ; xdr_header(&xdrs, &header); for(i = 0; i < header.nsph; i++) { fread((char *)&gas,sizeof(struct gas_particle), 1, stdin) ; xdr_gas(&xdrs, &gas); } for(i = 0; i < header.ndark; i++) { fread((char *)&dark,sizeof(struct dark_particle), 1, stdin) ; xdr_dark(&xdrs, &dark); } for(i = 0; i < header.nstar; i++) { fread((char *)&star,sizeof(struct star_particle), 1, stdin) ; xdr_star(&xdrs, &star); } fprintf(stderr, "read time %f\n",header.time) ; } xdr_destroy(&xdrs); return 0; }
int main(int argc, char *argv[]) { int i,j,k ; /*---Arguments-------------------------*/ int geometry,zspace,ifib,tbin_out=0; double rotanglex,rotangley,rotanglez,zmin,zmax,fgot_min ; /*---Particle-distribution-variables---*/ int np,ibyte,*idat ; float *fdat,znow ; float Lbox ; float *x,*y,*z,*vx,*vy,*vz ; /*---Cosmodistfile-variables-----------*/ int Nzdc ; double *zc,*dc ; /*---Redshift-space--------------------*/ double r,vr,*cz1 ; /*---SDSS-mask-------------------------*/ int *ID,Ngal ; double comp1,*comp,*ra,*dec,*cz ; double ra1,dec1,vec[3] ; char *pixfile ; /*---Fiber-collisions------------------*/ int Nfibmax,Nfibgrp,indx,jndx,Ni,indx2,done,ipick ; int **IDfib,*fiblist,*idrank,*fibcollisions ; long seed=-1 ; double *x1,*y1,*z1,length ; double costheta,cosfiber,*costhetarank ; /*---Functions-------------------------*/ void InitializePolygonMask(char *) ; double FindCompleteness(double, double) ; void rotate_x(double,double *) ; void rotate_y(double,double *) ; void rotate_z(double,double *) ; int set_cosmo_dist(const double Omegam, const double OmegaL, const double zmax, const int maxsize, double * zc, double * dc ) ; /*---Read-Arguments---------------------------------------------------------*/ if(argc < 11) { fprintf(stderr, "Usage:\n %s geometry zspace ifib rotanglex rotangley rotanglez zmin zmax maskfile fgot_min tbin_out < partfile > outfile\n", argv[0]) ; exit(1) ; } sscanf(argv[1],"%d",&geometry) ; sscanf(argv[2],"%d",&zspace) ; sscanf(argv[3],"%d",&ifib) ; sscanf(argv[4],"%lf",&rotanglex) ; sscanf(argv[5],"%lf",&rotangley) ; sscanf(argv[6],"%lf",&rotanglez) ; sscanf(argv[7],"%lf",&zmin) ; sscanf(argv[8],"%lf",&zmax) ; pixfile = argv[9]; sscanf(argv[10],"%lf",&fgot_min) ; if(argc >= 12) { sscanf(argv[11],"%d",&tbin_out) ; } /*---Read-particle-file-----------------------------------------------------*/ idat=(int *)calloc(5,sizeof(int)); fdat=(float *) calloc(9,sizeof(float)); ftread(idat,sizeof(int),5,stdin); ftread(fdat,sizeof(float),9,stdin); ftread(&znow,sizeof(float),1,stdin) ; np=idat[1]; Lbox=fdat[0]; x=(float *) calloc(np,sizeof(float)); y=(float *) calloc(np,sizeof(float)); z=(float *) calloc(np,sizeof(float)); vx=(float *) calloc(np,sizeof(float)); vy=(float *) calloc(np,sizeof(float)); vz=(float *) calloc(np,sizeof(float)); ftread(x,sizeof(float),np,stdin); ftread(y,sizeof(float),np,stdin); ftread(z,sizeof(float),np,stdin); ftread(vx,sizeof(float),np,stdin); ftread(vy,sizeof(float),np,stdin); ftread(vz,sizeof(float),np,stdin); // fprintf(stderr,"makemock> Read galaxy distribution, np=%d\n",np) ; // fprintf(stderr,"makemock> Size of box, Lbox=%g\n",Lbox) ; /*---Use-center-of-box-as-origin--------------------------------------------*/ for (i=0;i<np;i++) { x[i] -= 0.5*Lbox ; y[i] -= 0.5*Lbox ; z[i] -= 0.5*Lbox ; } /*--- Calculate cosmo distance ----------------------------------------------------*/ zc=(double *) calloc(COSMO_DIST_SIZE ,sizeof(double)); dc=(double *) calloc(COSMO_DIST_SIZE ,sizeof(double)); Nzdc = set_cosmo_dist(OMEGA_M, OMEGA_L, zmax, COSMO_DIST_SIZE, zc, dc); /*---Compute-cosmological-redshifts-for-galaxies----------------------------*/ cz1=(double *) calloc(np,sizeof(double)); // fprintf(stderr,"makemock> Computing cosmological redshifts\n") ; { double cz_local ; for(i=0;i<np;i++) { cz_local = -1.0; r = (double)sqrt(sqr(x[i])+sqr(y[i])+sqr(z[i])) ; for(j=1;j<Nzdc;j++) { if(r<dc[j]) { cz_local = c*(zc[j] - (dc[j]-r)*(zc[j]-zc[j-1])/(dc[j]-dc[j-1])) ; break ; } } if( cz_local > 0 ) { cz1[i] = cz_local ; } else { fprintf(stderr, "makemock> ERROR! couldn't set cosmological redshift for particle %d\n", i) ; exit(1) ; } } } /*---Put-in-redshift-space-with-observer-at-center--------------------------*/ if(zspace==1) { for(i=0;i<np;i++) { r = (double)sqrt(sqr(x[i])+sqr(y[i])+sqr(z[i])) ; vr = (double)(vx[i]*x[i]+vy[i]*y[i]+vz[i]*z[i])/r ; cz1[i] += vr + vr*cz1[i] / c; /* too many factors of c, have to remove one */ } fprintf(stderr,"makemock> Put galaxies in redshift space\n") ; } /*---Initialize-Mask--------------------------------------------------------*/ if(geometry==1) { // fprintf(stderr,"makemock> Trim to SDSS geometry: filtering completeness < %g\n", fgot_min) ; InitializePolygonMask(pixfile) ; } /*---Only-keep-galaxies-that-are-in-SDSS-volume-----------------------------*/ ID=(int *) calloc(np,sizeof(int)); ra=(double *) calloc(np,sizeof(double)); dec=(double *) calloc(np,sizeof(double)); cz=(double *) calloc(np,sizeof(double)); x1=(double *) calloc(np,sizeof(double)); y1=(double *) calloc(np,sizeof(double)); z1=(double *) calloc(np,sizeof(double)); comp=(double *) calloc(np,sizeof(double)); Ngal=0 ; for(i=0;i<np;i++) { if(cz1[i]>=zmin*c && cz1[i]<=zmax*c) { vec[0] = (double)x[i] ; vec[1] = (double)y[i] ; vec[2] = (double)z[i] ; length = sqrt(sqr(vec[0])+sqr(vec[1])+sqr(vec[2])) ; vec[0] /= length ; vec[1] /= length ; vec[2] /= length ; rotate_x(rotanglex,vec) ; rotate_y(rotangley,vec) ; rotate_z(rotanglez,vec) ; dec1 = 90. - (180./pi)*acos(vec[2]) ; ra1 = (180./pi)*atan(vec[1]/vec[0]) ; if(vec[0]<0) ra1 += 180 ; else if(vec[0]>=0 && vec[1]<0) ra1 += 360 ; if(geometry==0) { comp1=1. ; } else if(geometry==1) { comp1 = FindCompleteness(ra1,dec1) ; } if(comp1 >= fgot_min) { ID[Ngal] = Ngal ; cz[Ngal] = cz1[i] ; dec[Ngal] = dec1 ; ra[Ngal] = ra1 ; comp[Ngal] = comp1 ; if(zspace == 1) { double dist; double z_local = (cz1[i] / c); for(j=1;j<Nzdc;j++) { if(z_local < zc[j]) { dist = dc[j] - (zc[j] - z_local)*(dc[j]-dc[j-1])/(zc[j]-zc[j-1]) ; break ; } } x1[Ngal] = dist*vec[0] ; y1[Ngal] = dist*vec[1] ; z1[Ngal] = dist*vec[2] ; } else { x1[Ngal] = (double)x[i] ; y1[Ngal] = (double)y[i] ; z1[Ngal] = (double)z[i] ; } Ngal++ ; } } } // fprintf(stderr,"makemock> Ngal=%d\n",Ngal) ; { /* begin output */ XDR xdrs; struct dump h; struct dark_particle dp; // fprintf(stderr,"makemock> creating output in %s \n", tbin_out ? "tipsy binary format" : "ascii" ); if(tbin_out) { /* Make TIPSY header */ h.nbodies = Ngal ; h.ndark = h.nbodies ; h.nstar = 0 ; h.nsph = 0 ; h.ndim = 3 ; h.time = 1.0 ; xdrstdio_create(&xdrs, stdout, XDR_ENCODE); if( xdr_header(&xdrs,&h) != 1 ) { fprintf(stderr, "Error writing tipsy binary header!"); exit(2); } dp.mass = 1.0 ; for (i=0; i<3; ++i) { dp.vel[i] = 0.0 ; dp.pos[i] = 0.0 ; } dp.phi = 0.0 ; } for(i=0;i<Ngal; i++) { if(tbin_out) { dp.pos[0] = x1[i] ; dp.pos[1] = y1[i] ; dp.pos[2] = z1[i] ; if( xdr_dark(&xdrs,&dp) != 1 ) { fprintf(stderr, "Error writing tipsy binary data! (particle %d)",i); exit(2); } } else { fprintf(stdout,"%10.6f % 10.6f %10.2f\n",ra[i],dec[i],cz[i]) ; // fprintf(stdout,"%15.6f %15.6f %15.6f \n",x1[i],y1[i],z1[i]) ; // fprintf(stdout,"%6d %10.6f %10.6f %8.2f\n",ID[Ngal],ra[Ngal],dec[Ngal],cz[Ngal]) ; } } if(tbin_out) { /* close XDR file */ xdr_destroy(&xdrs); } } return 0 ; }
void kdWriteTipsyStd(KD kdg, KD kdd, KD kds, char *outFile, float radius, int nSplitting) { struct gas_particle gas; struct dark_particle dark; struct star_particle star; struct dump header; float xcurr, ycurr, zcurr, comr[3], comv[3]; PINIT *pg, *ps, *pd; int i, j, pi, oldgas=0, oldstar=0, olddark=0; XDR xdrs; FILE *fp; if (kdg->bOutDiag) puts(">> kdWriteTipsyStd()"); fflush(stdout); if (outFile) { fp = fopen(outFile,"w"); assert(fp != NULL); } else { fp = stdout; } setvbuf(stdin, NULL, _IOFBF, 32*4096); setvbuf(stdout, NULL, _IOFBF, 32*4096); xdrstdio_create(&xdrs, fp, XDR_ENCODE); /* ** Initialize the header and write to the file */ header.time = (double)kdg->fTime; header.ndim = 3; pg = kdg->pInit; // how many old non-split dark particles pd = kdd->pInit; /* for (pi=0;pi<kdd->nParticles;pi++) */ /* { */ /* if(((pd[pi].r[0]*pd[pi].r[0] + pd[pi].r[1]*pd[pi].r[1] + pd[pi].r[2]*pd[pi].r[2]) */ /* < radius*radius)) */ /* olddark++; */ /* } */ olddark = kdd->nInitActive; fprintf(stderr, "olddark in writetipsy = %d\n", olddark); // how many old non-split star particles ps = kds->pInit; for (pi=0;pi<kds->nParticles;pi++) { if(((ps[pi].r[0]*ps[pi].r[0] + ps[pi].r[1]*ps[pi].r[1] + ps[pi].r[2]*ps[pi].r[2]) < radius*radius) && (ps[pi].fTimeForm < 0.0)) oldstar++; } header.nsph = kdg->nMove; header.ndark = olddark; header.nstar = kds->nMove + oldstar; header.nbodies = header.nsph + header.nstar + header.ndark; xdr_header(&xdrs, &header); /* ** loop over gas particles - only the split particles are used, ** so use only the pMove array */ for (i=0;i<kdg->nMove;i++) { for (j=0;j<3;j++) { gas.pos[j] = kdg->pMove[i].r[j]; gas.vel[j] = kdg->pMove[i].v[j]; } gas.mass = kdg->pMove[i].fMass; gas.rho = kdg->pMove[i].fDensity; gas.temp = kdg->pMove[i].fTemp; assert(kdg->pMove[i].fSoft != 0.0); gas.hsmooth = kdg->pMove[i].fSoft; gas.metals = kdg->pMove[i].fMetals; gas.phi = 0.0; xdr_gas(&xdrs, &gas); } /* ** loop over dark particles - none of the dark particles are split, ** so use only the pInit array */ for (i=0;i<kdd->nInitActive;i++) { xcurr = pd[i].r[0]; ycurr = pd[i].r[1]; zcurr = pd[i].r[2]; if((xcurr*xcurr + ycurr*ycurr + zcurr*zcurr) <= radius*radius) { for (j=0;j<3;j++) { dark.pos[j] = kdd->pInit[i].r[j]; dark.vel[j] = kdd->pInit[i].v[j]; } dark.mass = kdd->pInit[i].fMass; dark.eps = kdd->pInit[i].fSoft; dark.phi = 0.0; xdr_dark(&xdrs, &dark); } else fprintf(stderr, "erm... DARK PARTICLE OUTSIDE RADIUS! \n", (xcurr*xcurr + ycurr*ycurr + zcurr*zcurr)); } /* ** loop over star particles - the old particles aren't relevant for the splitting ** so take those from the pInit array, but take the split particles from the pMove array */ for (i=kds->nInitActive;i<kds->nParticles;i++) { xcurr = ps[i].r[0]; ycurr = ps[i].r[1]; zcurr = ps[i].r[2]; if((xcurr*xcurr + ycurr*ycurr + zcurr*zcurr) < radius*radius) { for (j=0;j<3;j++) { star.pos[j] = kds->pInit[i].r[j]; star.vel[j] = kds->pInit[i].v[j]; } star.mass = kds->pInit[i].fMass; star.tform = kds->pInit[i].fTimeForm; star.metals = kds->pInit[i].fMetals; star.eps = kds->pInit[i].fSoft; star.phi = 0.0; xdr_star(&xdrs, &star); } } for (i=0;i<kds->nMove;i++) { for (j=0;j<3;j++) { star.pos[j] = kds->pMove[i].r[j]; star.vel[j] = kds->pMove[i].v[j]; } star.mass = kds->pMove[i].fMass; star.tform = kds->pMove[i].fTimeForm; star.metals = kds->pMove[i].fMetals; star.eps = kds->pMove[i].fSoft; star.phi = 0.0; xdr_star(&xdrs, &star); } xdr_destroy(&xdrs); if (kdg->bOutDiag) puts("<< kdWriteTipsyStd()"); fflush(stdout); }