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; }
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); }