void warn(const char *fmt, ...) { va_list ap; va_start(ap, fmt); vwarn(fmt, ap); va_end(ap); }
{ vfprintf (stderr, format, ap); fputs_unlocked (": ", stderr); } __set_errno (error); fprintf (stderr, "%m\n"); } funlockfile (stderr); } libc_hidden_def (vwarn) void warn (const char *format, ...) { VA (vwarn (format, ap)) } libc_hidden_def (warn) void warnx (const char *format, ...) { VA (vwarnx (format, ap)) } libc_hidden_def (warnx) void verr (int status, const char *format, __gnuc_va_list ap) { vwarn (format, ap); exit (status);
void verr (int status, const char *format, __gnuc_va_list ap) { vwarn (format, ap); exit (status); }
int recvPar(recPar *rec, float sub_x0, float sub_z0, float dx, float dz, int nx, int nz) { float *xrcv1, *xrcv2, *zrcv1, *zrcv2; int ix0, ix1, iz0, iz1, i, ix, iz, ir, isign, verbose; float dxrcv, dzrcv, *dxr, *dzr, r, rr; float rrcv, dphi, oxrcv, ozrcv; float xrange, zrange; int Nx1, Nx2, Nz1, Nz2, Ndx, Ndz, iarray, nskip, nrec; int nxrcv, nzrcv, ncrcv, nrcv, max_nrec; float *xrcva, *zrcva; if(!getparint("verbose", &verbose)) verbose = 0; if (!getparint("max_nrec",&max_nrec)) max_nrec=15000; nrec=0; /* check if receiver positions on a circle are defined */ if (getparfloat("rrcv", &rrcv)) { if (!getparfloat("dphi",&dphi)) dphi=2.0; if (!getparfloat("oxrcv",&oxrcv)) oxrcv=0.0; if (!getparfloat("ozrcv",&ozrcv)) ozrcv=0.0; ncrcv = NINT(360.0/dphi); for (ix=0; ix<ncrcv; ix++) { rec->xr[ix] = oxrcv-sub_x0+rrcv*cos(((ix*dphi)/360.0)*(2.0*M_PI)); rec->zr[ix] = ozrcv-sub_z0+rrcv*sin(((ix*dphi)/360.0)*(2.0*M_PI)); rec->x[ix] = NINT((oxrcv-sub_x0+rrcv*cos(((ix*dphi)/360.0)*(2.0*M_PI)))/dx); rec->z[ix] = NINT((ozrcv-sub_z0+rrcv*sin(((ix*dphi)/360.0)*(2.0*M_PI)))/dz); if (verbose>4) fprintf(stderr,"Receiver Circle: xrcv[%d]=%f zrcv=%f\n", ix, rec->xr[ix]+sub_x0, rec->zr[ix]+sub_z0); } nrec += ncrcv; } /* check if receiver array is defined */ nxrcv = countparval("xrcva"); nzrcv = countparval("zrcva"); if (nxrcv != nzrcv) { verr("Number of receivers in array xrcva (%d), zrcva(%d) are not equal",nxrcv, nzrcv); } if (nxrcv != 0) { /* receiver array is defined */ xrcva = (float *)malloc(nxrcv*sizeof(float)); zrcva = (float *)malloc(nxrcv*sizeof(float)); getparfloat("xrcva", xrcva); getparfloat("zrcva", zrcva); for (ix=0; ix<nxrcv; ix++) { rec->xr[nrec+ix] = xrcva[ix]-sub_x0; rec->zr[nrec+ix] = zrcva[ix]-sub_z0; rec->x[nrec+ix] = NINT((xrcva[ix]-sub_x0)/dx); rec->z[nrec+ix] = NINT((zrcva[ix]-sub_z0)/dz); if (verbose>4) fprintf(stderr,"Receiver Array: xrcv[%d]=%f zrcv=%f\n", ix, rec->xr[nrec+ix]+sub_x0, rec->zr[nrec+ix]+sub_z0); } nrec += nxrcv; free(xrcva); free(zrcva); } /* linear receiver arrays */ Nx1 = countparval("xrcv1"); Nx2 = countparval("xrcv2"); Nz1 = countparval("zrcv1"); Nz2 = countparval("zrcv2"); assert(Nx1==Nx2); assert(Nz1==Nz2); assert(Nx1==Nz1); if (nrec==0 && Nx1==0) { /* no receivers are defined use default linear array of receivers on top of model */ Nx1=1; } if (Nx1!=0) { xrcv1 = (float *)malloc(Nx1*sizeof(float)); xrcv2 = (float *)malloc(Nx1*sizeof(float)); zrcv1 = (float *)malloc(Nx1*sizeof(float)); zrcv2 = (float *)malloc(Nx1*sizeof(float)); if(!getparfloat("xrcv1", xrcv1)) xrcv1[0]=sub_x0; if(!getparfloat("xrcv2", xrcv2)) xrcv2[0]=(nx-1)*dx+sub_x0; if(!getparfloat("zrcv1", zrcv1)) zrcv1[0]=sub_z0; if(!getparfloat("zrcv2", zrcv2)) zrcv2[0]=zrcv1[0]; Ndx = countparval("dxrcv"); Ndz = countparval("dzrcv"); dxr = (float *)malloc(Nx1*sizeof(float)); dzr = (float *)malloc(Nx1*sizeof(float)); if(!getparfloat("dxrcv", dxr)) dxr[0]=dx; if(!getparfloat("dzrcv", dzr)) dzr[0]=0.0; if ( (Ndx<=1) && (Ndz==0) ){ /* default values are set */ for (i=1; i<Nx1; i++) { dxr[i] = dxr[0]; dzr[i] = dzr[0]; } Ndx=1; } else if ( (Ndz==1) && (Ndx==0) ){ /* default values are set */ for (i=1; i<Nx1; i++) { dxr[i] = dxr[0]; dzr[i] = dzr[0]; } Ndz=1; } else { /* make sure that each array has dzrcv or dxrcv defined for each line or receivers */ if (Ndx>1) assert(Ndx==Nx1); if (Ndz>1) assert(Ndz==Nx1); } /* if ( (Ndx!=0) && (Ndz!=0) ) { vwarn("Both dzrcv and dxrcv are set: dxrcv value is used"); Ndz=0; for (i=0; i<Nx1; i++) dzr[i] = 0.0; } */ /* check if receiver arrays fit into model */ for (iarray=0; iarray<Nx1; iarray++) { xrcv1[iarray] = MAX(sub_x0, xrcv1[iarray]); xrcv1[iarray] = MIN(sub_x0+nx*dx,xrcv1[iarray]); xrcv2[iarray] = MAX(sub_x0, xrcv2[iarray]); xrcv2[iarray] = MIN(sub_x0+nx*dx,xrcv2[iarray]); zrcv1[iarray] = MAX(sub_z0, zrcv1[iarray]); zrcv1[iarray] = MIN(sub_z0+nz*dz,zrcv1[iarray]); zrcv2[iarray] = MAX(sub_z0, zrcv2[iarray]); zrcv2[iarray] = MIN(sub_z0+nz*dz,zrcv2[iarray]); } /* calculate receiver array and store into rec->x,z */ for (iarray=0; iarray<Nx1; iarray++) { xrange = (xrcv2[iarray]-xrcv1[iarray]); zrange = (zrcv2[iarray]-zrcv1[iarray]); if (dxr[iarray] != 0.0) { nrcv = NINT(abs(xrange/dxr[iarray]))+1; dxrcv=dxr[iarray]; dzrcv = zrange/(nrcv-1); if (dzrcv != dzr[iarray]) { vwarn("For receiver array %d: calculated dzrcv=%f given=%f", iarray, dzrcv, dzr[iarray]); vwarn("The calculated receiver distance %f is used", dzrcv); } } else { if (dzr[iarray] == 0) { verr("For receiver array %d: receiver distance dzrcv is not given", iarray); } nrcv=NINT(abs(zrange/dzr[iarray]))+1; dxrcv = xrange/(nrcv-1); dzrcv = dzr[iarray]; if (dxrcv != dxr[iarray]) { vwarn("For receiver array %d: calculated dxrcv=%f given=%f", iarray, dxrcv, dxr[iarray]); vwarn("The calculated receiver distance %f is used", dxrcv); } } // calculate coordinates for (ir=0; ir<nrcv; ir++) { rec->xr[nrec]=xrcv1[iarray]-sub_x0+ir*dxrcv; rec->zr[nrec]=zrcv1[iarray]-sub_z0+ir*dzrcv; rec->x[nrec]=NINT((rec->xr[nrec])/dx); rec->z[nrec]=NINT((rec->zr[nrec])/dz); nrec++; } if (nrec >= max_nrec) { verr("Number of receivers in arrays xrcv1,xrcv2,zrcv1,zrcv2 are larger than max: increas max_nrec %d",max_nrec); } } free(xrcv1); free(xrcv2); free(zrcv1); free(zrcv2); free(dxr); free(dzr); } rec->n = nrec; return 0; }
void verr(int e,const char* f,va_list ap) { vwarn(f,ap); exit(e); }
int main(int argc, char **argv) { FILE *fpint, *fpcp, *fpcs, *fpro; int example, verbose, writeint, nb; int above, diffrwidth, dtype; int Ngp, Ngs, Ngr, Np, Ns, Nr, Ng, Ni, Nv, Nvi, No, Noi; int jint, jcount, j, ix, iz, nx, nz, nxp, nzp, *zp, nmaxx, nminx, optgrad, poly, gradt; int ncp, nro, ncs, nvel, skip, rayfile, store_int; long lseed; size_t nwrite; float *data_out, orig[2], cp0, cs0, ro0; float *x, *z, *var, *interface, **inter; float back[3], sizex, sizez, dx, dz; float **cp ,**cs, **ro, aver, gradlen, gradcp, gradcs, gradro; /* Gradient unit flag */ /* ------------------ */ /* - 0 Unit : m/s per dz */ /* - 1 Unit : m/s per m */ int gradunit; /* Number of Z-reference points (one per layer) */ int Nzr=0; float **gridcp, **gridcs, **gridro; segy *hdrs; FILE *fpout; char *file, intt[10], *file_base, filename[150]; initargs(argc, argv); requestdoc(1); if (!getparint("example", &example)) example=0; else { plotexample(); exit(0); } if(getparstring("file",&file_base)) vwarn("parameters file is changed to file_base"); else { if(!getparstring("file_base",&file_base)) verr("file_base not specified."); } if(getparfloat("back", back)) { vwarn("parameters back is not used anymore"); vwarn("it has changed into cp0 (ro0,cs0 are optional)"); nb = countparval("back"); if (nb == 1) { vwarn("The new call should be cp0=%.1f",back[0]); cp0 = back[0]; } if (nb == 2) { vwarn("The new call should be cp0=%.1f",back[0]); vwarn(" ro0=%.1f",back[1]); cp0 = back[0]; ro0 = back[1]; } if (nb == 3) { vwarn("The new call should be cp0=%.1f",back[0]); vwarn(" cs0=%.1f",back[1]); vwarn(" ro0=%.1f",back[2]); cp0 = back[0]; cs0 = back[1]; ro0 = back[2]; } vmess("Don't worry everything still works fine"); } else { if(!getparfloat("cp0", &cp0)) verr("cp0 not specified."); if(!getparfloat("cs0", &cs0)) cs0 = -1; if(!getparfloat("ro0", &ro0)) ro0 = -1; } if(!getparfloat("sizex", &sizex)) verr("x-model size not specified."); if(!getparfloat("sizez", &sizez)) verr("z-model size not specified."); if(!getparfloat("dx", &dx)) verr("grid distance dx not specified."); if(!getparfloat("dz", &dz)) verr("grid distance dz not specified."); if(!getparfloat("orig", orig)) orig[0] = orig[1] = 0.0; if(!getparint("gradt", &gradt)) gradt = 1; if(!getparint("gradunit", &gradunit)) gradunit = 0; if(!getparint("writeint", &writeint)) writeint = 0; if(!getparint("rayfile", &rayfile)) rayfile = 0; if(!getparint("skip", &skip)) skip = 5; if(!getparint("above", &above)) above=0; if(!getparint("verbose", &verbose)) verbose=0; if(!getparint("dtype", &dtype)) dtype = 0; if ((writeint == 1) || (rayfile == 1)) store_int = 1; else store_int = 0; /*=================== check parameters =================*/ Np = countparname("cp"); Ns = countparname("cs"); Nr = countparname("ro"); Ng = countparname("grad"); No = countparname("poly"); Ni = countparname("intt"); Nv = countparname("var"); Ngp = countparname("gradcp"); Ngs = countparname("gradcs"); Ngr = countparname("gradro"); Nvi = 0; for (jint = 1; jint <= Ni; jint++) { getnparstring(jint,"intt", &file); strcpy(intt, file); if (strstr(intt,"sin") != NULL) Nvi++; if (strstr(intt,"rough") != NULL) Nvi++; if (strstr(intt,"fract") != NULL) Nvi++; if (strstr(intt,"elipse") != NULL) Nvi++; if (strstr(intt,"random") != NULL) Nvi++; // if (strstr(intt,"randdf") != NULL) Nvi++; if (strstr(intt,"diffr") != NULL || strstr(intt,"randdf") != NULL) { Nvi++; // if (Ng != 0) Ng++; // if (No != 0) No++; } } // fprintf(stderr,"Nvi=%d ng=%d No=%d np=%d,", Nvi,Ng,No,Np); if (Np != Nr && ro0 > 0) verr("number of cp and ro not equal."); if (Np != Ni) verr("number of cp and interfaces not equal."); if (Nvi != Nv) verr("number of interface variables(var) not correct."); if (Ns == 0 && Nr == 0) if (verbose>=2) vmess("Velocity model."); if (Ns == 0) { if (verbose>=2) vmess("Acoustic model."); } else { if (verbose>=2) vmess("Elastic model."); if (Np != Ns) verr("number of cp and cs not equal"); } if (Ng == 0) { if (verbose>=2) vmess("No boundary gradients are defined."); } else if (Ng != Np) { verr("number of boundary gradients and interfaces are not equal."); } if (Ngp == 0) { if (verbose>=2) vmess("No interface gradients for cp defined."); } else if (Ngp != Np) { verr("gradcp gradients and interfaces are not equal."); } if (Ngs == 0) { if (verbose>=2) vmess("No interface gradients for cs defined."); } else if (Ngs != Np) { verr("gradcs gradients and interfaces are not equal."); } if (Ngr == 0) { if (verbose>=2) vmess("No interface gradients for rho defined."); } else if (Ngr != Np) { verr("gradro gradients and interfaces are not equal."); } if (No == 0) { if (verbose>=2) vmess("All interfaces are linear."); } // else if (No != Np) { // verr("number of poly variables and interfaces are not equal."); // } if (Np > 0) { if (countparname("x") != Np) verr("a x array must be specified for each interface."); if (countparname("z") != Np) verr("a z array must be specified for each interface."); } else Np = 1; if (Nzr != Np && Nzr !=0) { verr("number of zref gradients not equal to number of interfaces"); } /*=================== initialization of arrays =================*/ nz = NINT(sizez/dz)+1; nx = NINT(sizex/dx)+1; zp = (int *)malloc(nx*sizeof(int)); interface = (float *)malloc(nx*sizeof(float)); var = (float *)malloc(8*sizeof(float)); gridcp = alloc2float(nz, nx); if(gridcp == NULL) verr("memory allocation error gridcp"); if (Ns || (NINT(cs0*1e3) >= 0)) { gridcs = alloc2float(nz, nx); if(gridcs == NULL) verr("memory allocation error gridcs"); } else gridcs = NULL; if (Nr || (NINT(ro0*1e3) >= 0)) { gridro = alloc2float(nz, nx); if(gridro == NULL) verr("memory allocation error gridro"); } else gridro = NULL; cp = alloc2float(nx,3); cs = alloc2float(nx,3); ro = alloc2float(nx,3); if (store_int == 1) inter = alloc2float(nx, 2*Np); if (verbose) { vmess("Origin top left (x,z) . = %.1f, %.1f", orig[0], orig[1]); vmess("Base name ............. = %s", file_base); vmess("Number of interfaces .. = %d", Np); vmess("length in x ........... = %f (=%d)", sizex, nx); vmess("length in z ........... = %f (=%d)", sizez, nz); vmess("delta x ............... = %f", dx); vmess("delta z ............... = %f", dz); vmess("cp0 ................... = %f", cp0); if (Ns) vmess("cs0 ................... = %f", cs0); if (Nr) vmess("ro0 ................... = %f", ro0); vmess("write interfaces ...... = %d", writeint); vmess("store interfaces ...... = %d", store_int); if (above) vmess("define model above interface"); else vmess("define model below interface"); } /*========== initializing for homogeneous background =============*/ nminx = 0; nmaxx = nx; for (j = nminx; j < nmaxx; j++) { cp[0][j] = cp0; cs[0][j] = cs0; ro[0][j] = ro0; zp[j] = 0; cp[1][j] = cp0; cs[1][j] = cs0; ro[1][j] = ro0; } gradlen = 0.0; gradcp = gradcs = gradro = 0.0; optgrad = 3; if (above == 0) { Nvi = 1; Noi = 1; } else { Nvi = Ngp; Noi = Ngp; } grid(gridcp, gridcs, gridro, zp, cp, cs, ro, nminx, nmaxx, optgrad, gradlen, gradcp, gradcs, gradro, dx, dz, nz); nxp = nzp = 2; x = (float *)malloc(nxp*sizeof(float)); z = (float *)malloc(nzp*sizeof(float)); if (Ni == 0) { if (verbose) vmess("No interfaces are defined, Homogeneous model."); Np = 0; } /*========== filling gridded model with interfaces =============*/ for (jcount = 1; jcount <= Np; jcount++) { /* for above interface definition reverse */ /* order of interfaces to scan */ if (above == 0) jint=jcount; else jint=Np+1-jcount; if (verbose) vmess("***** Interface number %d *****", jint); getnparstring(jint,"intt", &file); strcpy(intt, file); nxp = countnparval(jint,"x"); nzp = countnparval(jint,"z"); if (nxp != nzp) { vmess("nxp = %d nzp =%d for interface %d",nxp, nzp, jint); verr("Number of x and z values not equal for interface %d",jint); } ncp = countnparval(jint,"cp"); nro = countnparval(jint,"ro"); ncs = countnparval(jint,"cs"); if (ncp == 1) { if (verbose>=2) vmess("No lateral gradient in P velocity"); } else if (ncp == 2) { if (verbose) vmess("lateral P-gradient from begin to end"); } else if (ncp != nxp) { vmess("ncp = %d nxp =%d for interface %d",ncp, nxp, jint); verr("Number of cp's and x not equal for interface %d",jint); } if (nro <= 1) { if (verbose>=2) vmess("No lateral gradient in density"); } else if (nro == 2) { if (verbose) vmess("lateral Rho-gradient from begin to end"); } else if (nro != nxp) { vmess("nro = %d nxp =%d for interface %d",nro, nxp, jint); verr("Number of ro's and x not equal for interface %d",jint); } if (ncs <= 1) { if (verbose>=2) vmess("No lateral gradient in S velocity"); } else if (ncs == 2) { if (verbose) vmess("lateral S-gradient from begin to end"); } else if (ncs != nxp) { vmess("ncs = %d nxp =%d for interface %d",ncs, nxp, jint); verr("Number of cs's and x not equal for interface %d",jint); } nvel = MAX(ncp, MAX(nro, ncs)); free(x); free(z); x = (float *)malloc(nxp*sizeof(float)); z = (float *)malloc(nzp*sizeof(float)); memset(interface, 0, nx*sizeof(float)); getnparfloat(jint,"x",x); getnparfloat(jint,"z",z); getnparfloat(jint,"cp",cp[2]); if (Nr == 0) ro[2][0] = 0.0; else getnparfloat(jint,"ro", ro[2]); if (Ns == 0) cs[2][0] = 0.0; else getnparfloat(jint,"cs", cs[2]); if (Ng == 0) gradlen = 0.0; else getnparfloat(Noi,"grad", &gradlen); if (No == 0) poly = 0; else getnparint(Noi,"poly", &poly); if (Ngp == 0) gradcp = 0.0; else getnparfloat(Noi,"gradcp", &gradcp); if (Ngs == 0) gradcs = 0.0; else getnparfloat(Noi,"gradcs", &gradcs); if (Ngr == 0) gradro = 0.0; else getnparfloat(Noi,"gradro", &gradro); /* if gradunit is in meters, recalculate gradcp,gradcs and gradro */ if (gradunit > 0) { gradcs = gradcs * dz; gradcp = gradcp * dz; gradro = gradro * dz; } if (nvel != 1) { if (ncp == 1) { for (j = 1; j < nvel; j++) cp[2][j] = cp[2][0]; } if (ncs == 1) { for (j = 1; j < nvel; j++) cs[2][j] = cs[2][0]; } if (nro == 1) { for (j = 1; j < nvel; j++) ro[2][j] = ro[2][0]; } } if (verbose) { vmess("Interface type .......... = %s", intt); vmess("Boundary gradient ....... = %f", gradlen); vmess("Interface gradient cp ... = %f", gradcp); if (Ns) vmess("Interface gradient cs ... = %f", gradcs); if (Nr) vmess("Interface gradient ro ... = %f", gradro); if (verbose>=2) { vmess("Polynomal ............... = %d", poly); vmess("Number of (x,z) points... = %d", nxp); vmess("P-wave velocities ....... = %d", ncp); if (Ns) vmess("S-wave velocities ....... = %d", ncs); if (Nr) vmess("Densities ............... = %d", nro); } for (j = 0; j < nxp; j++) { vmess("x = %6.1f \t z = %6.1f", x[j], z[j]); if (nvel != 1) { vmess(" cp = %f", cp[2][j]); if (Ns) vmess(" cs = %f", cs[2][j]); if (Nr) vmess(" rho = %f", ro[2][j]); } } if (nvel == 1) { vmess(" cp = %f", cp[2][0]); if (Ns) vmess(" cs = %f", cs[2][0]); if (Nr) vmess(" rho = %f", ro[2][0]); } } for (j = 0; j < nxp; j++) { x[j] -= orig[0]; z[j] -= orig[1]; } for (j = 0; j < nxp; j++) { if(x[j] > sizex) verr("x coordinate bigger than model"); if(z[j] > sizez) verr("z coordinate bigger than model"); } if (gradlen > 0) optgrad = gradt; else optgrad = 3; if (strstr(intt,"random") != NULL) { Nv = countnparval(Nvi,"var"); if (Nv != 1) verr("Random interface must have 1 variables."); getnparfloat(Nvi,"var", var); lseed = (long)var[0]; srand48(lseed); gradcp=gradcs=gradro=var[0]; optgrad = 4; if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } if ((strstr(intt,"diffr") == NULL) && (strstr(intt,"randdf") == NULL)) { interpolation(x, z, nxp, nx, poly, &nminx, &nmaxx, dx, cp, cs, ro, nvel, interface); } if ( (strstr(intt,"def") != NULL) || (strstr(intt,"random") != NULL) ) { linearint(zp, nminx, nmaxx, dz, interface); if (above == 0) Noi++; else Noi--; } if (strstr(intt,"sin") != NULL) { Nv = countnparval(Nvi,"var"); if (Nv != 2) verr("Sinus interface must have 2 variables."); getnparfloat(Nvi,"var", var); sinusint(zp, nminx, nmaxx, dz, interface, dx, var[0], var[1]); if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } else if (strstr(intt,"rough") != NULL) { Nv = countnparval(Nvi,"var"); if (Nv != 3) verr("Rough interface must have 3 variables."); getnparfloat(Nvi,"var", var); roughint(zp, nminx, nmaxx, dz, interface, var[0],var[1],var[2]); if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } else if (strstr(intt,"fract") != NULL) { Nv = countnparval(Nvi, "var"); if (Nv != 6) verr("Fractal interface must have 6 variables."); getnparfloat(Nvi,"var", var); fractint(zp, nminx, nmaxx, dx, dz, interface, var[0], var[1], var[2], var[3], var[4], var[5]); if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } else if (strstr(intt,"randdf") != NULL) { float x0, z0, dsx, dsz; int i; Nv = countnparval(Nvi, "var"); if (Nv != 2) verr("randdf interface must have 2 variables: number of points, width."); getnparfloat(Nvi,"var", var); if(!getparint("dtype", &dtype)) dtype = -1; randdf(x, z, nxp, dx, dz, gridcp, gridcs, gridro, cp, cs, ro, interface, zp, nx, sizex, sizez, var[0], (int)var[1], dtype); if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } else if (strstr(intt,"elipse") != NULL) { Nv = countnparval(Nvi, "var"); if (Nv != 2) verr("Elipse interface must have 2 variables."); getnparfloat(Nvi,"var", var); elipse(x, z, nxp, dx, dz, gridcp, gridcs, gridro, cp, cs, ro, interface, zp, nz, nx, var[0], var[1], gradcp, gradcs, gradro); if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } else if ((strstr(intt,"diffr") != NULL)) { Nv = countnparval(Nvi, "var"); if (Nv == 2 || Nv == 1) { getnparfloat(Nvi,"var", var); diffrwidth=(int)var[0]; if (Nv==1) { if(!getparint("dtype", &dtype)) dtype = 0; } else dtype=(int)var[1]; } else { verr("diffr interface must have 1 or 2 variables: width,type."); } diffraction(x, z, nxp, dx, dz, gridcp, gridcs, gridro, cp, cs, ro, interface, zp, nx, diffrwidth, dtype); if (above == 0) Noi++; else Noi--; if (above == 0) Nvi++; else Nvi--; } else { if (above == 0) { grid(gridcp, gridcs, gridro, zp, cp, cs, ro, nminx, nmaxx, optgrad, gradlen, gradcp, gradcs, gradro, dx, dz, nz); } else { gridabove(gridcp, gridcs, gridro, zp, cp, cs, ro, nminx, nmaxx, optgrad, gradlen, gradcp, gradcs, gradro, dx, dz, nz); } } if (store_int == 1) { for(j = 0; j < nminx; j++) inter[jint-1][j] = 0.0; for(j = nminx; j < nmaxx; j++) inter[jint-1][j] = interface[j]; for(j = nmaxx; j < nx; j++) inter[jint-1][j] = 0.0; for(j = 0; j < nminx; j++) inter[jint-1+Np][j] = 0.0; for(j = nminx; j < nmaxx; j++) inter[jint-1+Np][j] = zp[j]*dz; for(j = nmaxx; j < nx; j++) inter[jint-1+Np][j] = 0.0; } } /* end of loop over interfaces */ if (verbose) vmess("Writing data to disk."); hdrs = (segy *) calloc(nx,sizeof(segy)); for(j = 0; j < nx; j++) { hdrs[j].f1= orig[1]; hdrs[j].f2= orig[0]; hdrs[j].d1= dz; hdrs[j].d2= dx; hdrs[j].ns= nz; hdrs[j].trwf= nx; hdrs[j].tracl= j; hdrs[j].tracf= j; hdrs[j].gx = (orig[0] + j*dx)*1000; hdrs[j].scalco = -1000; hdrs[j].timbas = 25; hdrs[j].trid = TRID_DEPTH; } /* due to bug in SU, int-file has to be opened before other files are closed */ if (writeint == 1) { strcpy(filename, file_base); name_ext(filename, "_int"); fpint = fopen(filename,"w"); assert(fpint != NULL); } /* write P-velocities in file */ strcpy(filename, file_base); name_ext(filename, "_cp"); fpcp = fopen(filename,"w"); assert(fpcp != NULL); data_out = (float *)malloc(nx*nz*sizeof(float)); for(ix = 0; ix < nx; ix++) { for(iz = 0; iz < nz; iz++) { data_out[ix*nz+iz] = gridcp[ix][iz]; } nwrite = fwrite( &hdrs[ix], 1, TRCBYTES, fpcp); assert(nwrite == TRCBYTES); nwrite = fwrite( &data_out[ix*nz], sizeof(float), nz, fpcp); assert(nwrite == nz); } fclose(fpcp); free2float(gridcp); /* write S-velocities in file */ if (Ns > 0 || getparfloat("cs0", &cs0)) { strcpy(filename, file_base); name_ext(filename, "_cs"); fpcs = fopen(filename,"w"); assert(fpcs != NULL); for(ix = 0; ix < nx; ix++) { for(iz = 0; iz < nz; iz++) { data_out[ix*nz+iz] = gridcs[ix][iz]; } nwrite = fwrite( &hdrs[ix], 1, TRCBYTES, fpcs); assert(nwrite == TRCBYTES); nwrite = fwrite( &data_out[ix*nz], sizeof(float), nz, fpcs); assert(nwrite == nz); } fclose(fpcs); free2float(gridcs); } /* end of writing S-velocity file */ /* write densities in file */ if (Nr > 0 || getparfloat("ro0", &ro0)) { strcpy(filename, file_base); name_ext(filename, "_ro"); fpro = fopen(filename,"w"); assert(fpro != NULL); for(ix = 0; ix < nx; ix++) { for(iz = 0; iz < nz; iz++) { data_out[ix*nz+iz] = gridro[ix][iz]; } nwrite = fwrite( &hdrs[ix], 1, TRCBYTES, fpro); assert(nwrite == TRCBYTES); nwrite = fwrite( &data_out[ix*nz], sizeof(float), nz, fpro); assert(nwrite == nz); } fclose(fpro); free2float(gridro); } /* end of writing density file */ /* write depths of the interfaces */ if (writeint == 1) { free(hdrs); hdrs = (segy *) calloc(Np,sizeof(segy)); for(j = 0; j < Np; j++) { hdrs[j].fldr = 1; hdrs[j].timbas = 25; hdrs[j].f1= orig[0]; hdrs[j].f2= 0.0; hdrs[j].d1= dx; hdrs[j].d2= dz; hdrs[j].ns= nx; hdrs[j].trwf= Np; hdrs[j].tracl= j; hdrs[j].tracf= j; hdrs[j].trid= TRID_DEPTH; } /* note that due to bug in SU, interface file ha salready been opened */ strcpy(filename, file_base); name_ext(filename, "_int"); free(data_out); data_out = (float *)malloc(nx*Np*sizeof(float)); for(jint = 0; jint < Np; jint++) { for(j = 0; j < nx; j++) { data_out[jint*nx+j] = inter[jint][j]+orig[1]; } nwrite = fwrite( &hdrs[ix], 1, TRCBYTES, fpint); assert(nwrite == TRCBYTES); nwrite = fwrite( &data_out[jint*nx], sizeof(float), nx, fpint); assert(nwrite == nx); } for(j = 0; j < Np; j++) hdrs[j].fldr = 2; for(jint = 0; jint < Np; jint++) { for(j = 0; j < nx; j++) { data_out[jint*nx+j] = inter[jint+Np][j]+orig[1]; } nwrite = fwrite( &hdrs[ix], 1, TRCBYTES, fpint); assert(nwrite == TRCBYTES); nwrite = fwrite( &data_out[jint*nx], sizeof(float), nx, fpint); assert(nwrite == nx); } fclose(fpint); } /* end of writing interface file */ if (rayfile == 1) { strcpy(filename, file_base); strcpy(strrchr(filename, '.'), ".mod"); fpout = fopen(filename, "w+"); fprintf(fpout,"RAYTRACE MODEL FILE\n"); fprintf(fpout,"# ASCII file for ray-tracer\n\n"); fprintf(fpout,"# Top interface\n\n"); fprintf(fpout,"x=0,%.1f\n", sizex); fprintf(fpout,"z=0.,0.\n"); /* for(i = 1; i <= Np; i++) { fprintf(fpout,"\n# %d th interface\n\nx=",i); nxp = countnparval(i,"x"); nzp = countnparval(i,"z"); free(x); free(z); x = (float *)malloc(nxp*sizeof(float)); z = (float *)malloc(nzp*sizeof(float)); getnparfloat(i,"x",x); getnparfloat(i,"z",z); for(j = 0; j < (nxp-1); j ++) fprintf(fpout,"%.1f,", x[j]); fprintf(fpout,"%.1f\nz=", x[nxp-1]); for(j = 0; j < (nxp-1); j ++) fprintf(fpout,"%.1f,", z[j]); fprintf(fpout,"%.1f\n", z[nxp-1]); } */ for(jint = 0; jint < Np; jint++) { fprintf(fpout,"\n# %d th interface\n\nx=0",jint+1); for(j = skip; j < nx; j += skip) fprintf(fpout,",%.1f", (float)j*dx); fprintf(fpout,"\nz=%.1f", inter[jint][0]); for(j = skip; j < nx; j += skip) fprintf(fpout,",%.1f", inter[jint][j]); fprintf(fpout,"\n"); } fprintf(fpout,"\n# %d th interface\n\nx=0",jint+1); for(j = skip; j < nx; j += skip) fprintf(fpout,",%.1f", (float)j*dx); fprintf(fpout,"\nz=%.1f", sizez); for(j = skip; j < nx; j += skip) fprintf(fpout,",%.1f", sizez); fprintf(fpout,"\n"); /**/ fprintf(fpout,"\n\n"); fprintf(fpout,"cp=%.1f,", back[0]); for(jint = 1; jint <= Np; jint++) { aver = 0.0; ncp = countnparval(jint,"cp"); getnparfloat(jint,"cp",cp[2]); for(j = 0; j < ncp; j++) aver += cp[2][j]; aver = aver/((float)ncp); if (jint == Np ) fprintf(fpout,"%.1f", aver); else fprintf(fpout,"%.1f,", aver); } fclose(fpout); free2float(inter); } free(hdrs); return 0; }
void verr(int status, const char *fmt, va_list ap) { vwarn(fmt, ap); exit(status); }
/********************************************************************************************* * * Function: save_recording_queue_chunk * Purpose: saves one chunk of the queue to the recording file, using run-length encoding. * *********************************************************************************************/ void save_recording_queue_chunk( short player_index) { long *location; long last_flag, count, flag = 0; short i, run_count, num_flags_saved, max_flags; static long *buffer= NULL; ActionQueue *queue; if (buffer == NULL) { buffer = (long *)malloc((RECORD_CHUNK_SIZE * sizeof(long)) + RECORD_CHUNK_SIZE * sizeof(short)); } location= buffer; count= 0; // keeps track of how many bytes we'll save. last_flag= NONE; queue= get_player_recording_queue(player_index); // don't want to save too much stuff max_flags= MIN(RECORD_CHUNK_SIZE, get_recording_queue_size(player_index)); // save what's in the queue run_count= num_flags_saved= 0; for (i = 0; i<max_flags; i++) { flag = *(queue->buffer + queue->read_index); INCREMENT_QUEUE_COUNTER(queue->read_index); if (i && flag != last_flag) { *(short*)location = run_count; ((short*)location)++; *location++ = last_flag; count += sizeof(short) + sizeof(long); num_flags_saved += run_count; run_count = 1; } else { run_count++; } last_flag = flag; } // now save the final run *(short*)location = run_count; ((short*)location)++; *location++ = last_flag; count += sizeof(short) + sizeof(long); num_flags_saved += run_count; if (max_flags<RECORD_CHUNK_SIZE) { *(short*)location = END_OF_RECORDING_INDICATOR; ((short*)location)++; *location++ = 0; count += sizeof(short) + sizeof(long); num_flags_saved += RECORD_CHUNK_SIZE-max_flags; } write_file(replay.recording_file_refnum, count, buffer); replay.header.length+= count; vwarn(num_flags_saved == RECORD_CHUNK_SIZE, csprintf(temporary, "bad recording: %d flags, max=%d, count = %d;dm #%d #%d", num_flags_saved, max_flags, count, buffer, count)); }
static void test_vwarn_helper(const char *fmt, ...) { va_list args; va_start(args, fmt); vwarn(fmt, args); va_end(args); }
void vdie(const struct where *w, const char *fmt, va_list l) { vwarn(w, VWARN_ERR, fmt, l); exit(1); }
/* can be called at interrupt time */ void queue_network_speaker_data( byte *buffer, short count) { if (speaker) { switch (speaker->state) { case _speaker_is_off: /* we were off but now weÕre getting data; fill one double buffer with static and change our state to _turning_on (weÕll wait until we receive another full double buffer worth of data before beginning to replay) */ // ZZZ: CarbonSndPlayDB emulation layer specifically warns against calling // SndDoImmediate() with a quietCmd at interrupt time - which is what // quiet_network_speaker() would do. So instead here we try resetting // the speaker (which ought to be safe I think) now, but delay issuing the // quiet commands until just before we start playing again. #if defined(TARGET_API_MAC_CARBON) reset_network_speaker(); #else quiet_network_speaker(); /* we could be playing trailing static */ #endif speaker->state= _speaker_is_turning_on; fill_network_speaker_buffer(speaker->header->dbhBufferPtr[0]); break; case _speaker_is_on: case _speaker_is_turning_on: speaker->connection_status= 0; break; default: vhalt(csprintf(temporary, "what the hell is #%d!?", speaker->state)); } /* move incoming data into queue, NULL buffer means static */ if (speaker->queue_size+count<=MAXIMUM_QUEUE_SIZE) { if (buffer) { BlockMove(buffer, speaker->queue+speaker->queue_size, count); } else { fill_buffer_with_static((byte *)speaker->queue+speaker->queue_size, count); } speaker->queue_size+= count; } else { // This really shouldn't log in the non-main thread yet... logAnomaly3("queue_net_speaker_data() is ignoring data: #%d+#%d>#%d", speaker->queue_size, count, MAXIMUM_QUEUE_SIZE); } #ifdef SNDPLAYDOUBLEBUFFER_DOESNT_SUCK switch (speaker->state) { case _speaker_is_turning_on: /* check and see if we have enough data to turn on */ if (speaker->queue_size>=speaker->block_size) { OSErr error; error= SndPlayDoubleBuffer(speaker->channel, speaker->header); vwarn(error==noErr, csprintf(temporary, "SndPlayDoubleBuffer(%p,%p)==#%d", speaker->channel, speaker->header, error)); speaker->state= _speaker_is_on; } break; } #endif } }
void verr(int eval, const char *fmt, va_list args) { vwarn(fmt, args); exit(eval); }
void verr(int eval, const char *format, va_list ap) { vwarn(format, ap); exit(eval); }
int main(int argc, char **argv) { FILE *fp_in, *fp_out; int ret, verbose, i, j; size_t size, nread; int n1, n2, n3, stype, num; float d1, d2, d3; double *ddata; float *data; char *file_in, *file_out; segy *hdrs; initargs(argc, argv); requestdoc(1); if(!getparint("verbose", &verbose)) verbose = 0; if(!getparstring("file_in", &file_in)) { if (verbose) vwarn("parameter file_in not found, assume pipe"); file_in = NULL; } if(!getparstring("file_out", &file_out)){ if (verbose) vwarn("parameter file_out not found, assume pipe"); file_out = NULL; } if(!getparint("stype", &stype)) stype=1; if(!getparint("n1", &n1)) verr("n1 must be given"); if(!getparint("n2", &n2)) verr("n2 must be given"); if(!getparint("n3", &n3)) n3=1; if(!getparfloat("d1", &d1)) d1=1.0; if(!getparfloat("d2", &d2)) d2=1.0; if(!getparfloat("d3", &d3)) d3=1.0; /* Opening input file */ if (file_in != NULL) fp_in = fopen(file_in, "r"); else fp_in=stdin; if (fp_in == NULL) verr("error on opening input file_in=%s", file_in); size = n1 * n2; ddata = (double *)malloc(size*sizeof(double)*stype); hdrs = (segy *) calloc(n2,sizeof(segy)); if (ddata == NULL || hdrs==NULL ) verr("memory allocation error for input data"); for (i = 0; i < n2; i++) { hdrs[i].ns = stype*n1; hdrs[i].f1 = 0.0; hdrs[i].f2 = 0.0; hdrs[i].d1 = d1; hdrs[i].d2 = d2; hdrs[i].dt = d1*1e+6; hdrs[i].tracl = i+1; if (stype==2) { hdrs[i].trid=FUNPACKNYQ; } else { hdrs[i].trid=TREAL; } } nread = fread(&ddata[0], sizeof(double), size*stype, fp_in); if (nread == 0) { fclose(fp_in); if (verbose) verr("error in reading data of file %s", file_in); } /* allocate data array */ data = (float *)malloc(stype*size*sizeof(float)); if (data == NULL) verr("memory allocation error for data"); /* create output file */ if (file_out==NULL) fp_out = stdout; else { fp_out = fopen(file_out, "w+"); if (fp_out==NULL) verr("error on creating output file"); } /* loop for processing all data gathers */ num = 1; while (n3 > 0) { /* convert data and fill headers */ for (i = 0; i < n2; i++) { hdrs[i].fldr = num; for (j = 0; j < stype*n1; j++) { data[i*stype*n1+j] = (float)ddata[i*stype*n1+j]; } } /* write result to output file */ ret = writeData(fp_out, data, hdrs, stype*n1, n2); if (ret < 0 ) verr("error on writing output file."); nread = fread(ddata, sizeof(double), size*stype, fp_in); if (nread == 0) { fclose(fp_in); fclose(fp_out); if (verbose) vmess("end of data reached"); free(hdrs); free(data); free(ddata); return 0; } n3--; num++; } return 0; }
static void RemoteMemorySpyLoop () { struct rm_pkt pkt, reply; LispObj buffer[PageSize]; LispObj *bufferp; unsigned char *p; unsigned int vma, operand; int nwords, nchunks, i, pkt_length, reply_length, sinlen; int booted = 0; struct sockaddr_in pkt_source; struct pollfd pollSpy; boolean forciblyHalted = FALSE; pollSpy.fd = spy; pollSpy.events = POLLIN; while (1) { pthread_testcancel(); if (!Runningp()) { if (send_trap) { PushOneFakeFrame(); PushOneFakeFrame(); /* this does "remote system halted" */ if (trap_sinValid) { reply.rm_opcode = rm_trap; spy_transmit(&reply, REMOTE_MEMORY_PACKET_HEADER, &trap_sin); } send_trap = 0; } } if (0 == (i = poll(&pollSpy, 1, 1000))) continue; else if (i < 0) vpunt ("spy", "Waiting for a packet from the remote debugger"); else if (pollSpy.revents & (POLLHUP | POLLNVAL)) /* Spy port has vanished -- Assume that the emulator is shutting down ... */ break; sinlen = sizeof(struct sockaddr_in); if ((pkt_length = recvfrom(spy, &pkt.rm_pad[0], REMOTE_MEMORY_PACKET_HEADER + REMOTE_MEMORY_PACKET_DATA, 0, (struct sockaddr*)&pkt_source, &sinlen)) < 0) vpunt ("spy", "Reading packet from remote debugger"); reply.rm_operand[0] = 0; reply.rm_operand[1] = 0; reply.rm_operand[2] = 0; reply_length = REMOTE_MEMORY_PACKET_HEADER; reply.rm_opcode = rm_ack; memcpy(&reply.rm_id[0], &pkt.rm_id[0], 4); operand = read_long(&pkt.rm_operand[0]) & 0xffffff; switch (pkt.rm_opcode) { case rm_boot: SuspendVLM (); spy_transmit(&reply, reply_length, &pkt_source); InitializeIvoryProcessor(MapVirtualAddressData (0), MapVirtualAddressTag (0)); booted = 1; ResumeVLM (TRUE); break; case rm_create_pages: SuspendVLM (); vma = read_long(&pkt.data[0]); nwords = read_long(&pkt.data[4]); EnsureVirtualAddressRange(vma, nwords, FALSE); spy_transmit(&reply, reply_length, &pkt_source); ResumeVLM (TRUE); break; case rm_noop: spy_transmit(&reply, reply_length, &pkt_source); break; case rm_read: SuspendVLM (); vma = read_long(&pkt.data[0]); nwords = operand & 0x3ff; nchunks = (nwords+3)/4; switch ((operand>>10) & 3) { case rm_physical: #ifdef _C_EMULATOR_ goto READ_WRITE_MEMORY_ERROR; #else /* Use physical addresses to read uncached data */ { void (*old_segv_handler) () = signal(SIGSEGV, segv_handler); if (_setjmp(trap_environment)) { signal(SIGSEGV, old_segv_handler); goto READ_WRITE_MEMORY_ERROR; } for (i = 0; i < nwords; i++) { if (!CreatedP(vma+i)) goto READ_WRITE_MEMORY_ERROR; /* KLUDGE! */ buffer[i] = (VirtualMemoryReadUncached(vma + i)); } signal(SIGSEGV, old_segv_handler); } break; #endif case rm_virtual: { void (*old_segv_handler) () = signal(SIGSEGV, segv_handler); if (_setjmp(trap_environment)) { signal(SIGSEGV, old_segv_handler); goto READ_WRITE_MEMORY_ERROR; } for (i = 0; i < nwords; i++) { #ifdef _C_EMULATOR_ if (ReadVirtualMemory (vma+i, &buffer[i])) goto read_error; #else if (!CreatedP(vma+i)) goto READ_WRITE_MEMORY_ERROR; /* KLUDGE! */ buffer[i] = (VirtualMemoryRead(vma + i)); #endif } signal(SIGSEGV, old_segv_handler); } break; case rm_register: for (i = 0; i < nwords; i++) { #ifdef _C_EMULATOR_ ReadInternalRegister (vma+i, &buffer[i]); #else buffer[i] = ReadInternalRegister(vma + i); #endif } break; case rm_coprocessor: for (i = 0; i < nwords; i++) { #ifdef _C_EMULATOR_ vwarn ("spy", "Read of coprocessor register %d failed.", (vma + i)); #else buffer[i] = CoprocessorRead(vma + i); #endif } break; } for (i = 0, bufferp = &buffer[0], p = &reply.data[0]; i < nchunks; i++, bufferp += 4, p += 20) { p[0] = LispObjTag(bufferp[0]); p[1] = LispObjTag(bufferp[1]); p[2] = LispObjTag(bufferp[2]); p[3] = LispObjTag(bufferp[3]); write_long(&p[4], LispObjData(bufferp[0])); write_long(&p[8], LispObjData(bufferp[1])); write_long(&p[12], LispObjData(bufferp[2])); write_long(&p[16], LispObjData(bufferp[3])); } reply_length += nchunks*20; spy_transmit(&reply, reply_length, &pkt_source); ResumeVLM (TRUE); break; READ_WRITE_MEMORY_ERROR: reply.rm_operand[0] = 1; spy_transmit(&reply, reply_length, &pkt_source); ResumeVLM (rm_read == pkt.rm_opcode); break; case rm_stop: forciblyHalted = FALSE; HaltMachine(); spy_transmit(&reply, reply_length, &pkt_source); break; case rm_system_startup: spy_transmit(&reply, reply_length, &pkt_source); memcpy(&trap_sin, &pkt_source, sizeof(struct sockaddr_in)); trap_sinValid = TRUE; memcpy (&mbin_sin, &pkt_source, sizeof(struct sockaddr_in)); mbin_sinValid = TRUE; if (!IvoryProcessorSystemStartup (booted)) vwarn ("spy", "Bad start routine."); send_trap = 1; break; case rm_write: SuspendVLM (); vma = read_long(&pkt.data[0]); nwords = operand & 0x3ff; nchunks = (nwords+3)/4; for (i = 0, bufferp = &buffer[0], p = &pkt.data[4]; i < nchunks; i++, bufferp += 4, p += 20) { LispObjTag(bufferp[0]) = p[0]; LispObjTag(bufferp[1]) = p[1]; LispObjTag(bufferp[2]) = p[2]; LispObjTag(bufferp[3]) = p[3]; LispObjData(bufferp[0]) = read_long(&p[4]); LispObjData(bufferp[1]) = read_long(&p[8]); LispObjData(bufferp[2]) = read_long(&p[12]); LispObjData(bufferp[3]) = read_long(&p[16]); } switch ((operand>>10) & 3) { case rm_physical: goto READ_WRITE_MEMORY_ERROR; case rm_virtual: { void (*old_segv_handler) () = signal(SIGSEGV, segv_handler); if (_setjmp(trap_environment)) { signal(SIGSEGV, old_segv_handler); goto READ_WRITE_MEMORY_ERROR; } for (i = 0; i < nwords; i++) { #ifdef _C_EMULATOR_ if (WriteVirtualMemory (vma+i, &buffer[i])) goto read_error; #else if (!CreatedP(vma+i)) goto READ_WRITE_MEMORY_ERROR; /* KLUDGE! */ VirtualMemoryWrite(vma + i, buffer[i]); #endif } signal(SIGSEGV, old_segv_handler); } break; case rm_register: for (i = 0; i < nwords; i++) { #ifdef _C_EMULATOR_ if (!WriteInternalRegister(vma + i,&buffer[i])) vwarn ("spy", "Write of internal register %d failed", vma + i); #else if (WriteInternalRegister(vma + i,buffer[i])==-1) vwarn ("spy", "Write of internal register %d failed", vma + i); #endif } break; case rm_coprocessor: for (i = 0; i < nwords; i++) { #ifdef _C_EMULATOR_ vwarn ("spy", "Write of coprocessor register %d failed.", (vma + i)); #else if (!CoprocessorWrite(vma + i, buffer[i])) vwarn ("spy", "Write of coprocessor register %d failed.", (vma + i)); #endif } break; }; spy_transmit(&reply, reply_length, &pkt_source); ResumeVLM (FALSE); break; case rm_mbin: { EmbPtr bufferEmbPtr; struct rm_aligned_pkt *buffer; unsigned int id; int historyID; memcpy (&mbin_sin, &pkt_source, sizeof(struct sockaddr_in)); mbin_sinValid = TRUE; if (activeMBINChannel) { id = read_long(&pkt.rm_id[0]); historyID = id & 0xF; if (MBINHistory[historyID].id == id) { /* ... */ if (MBINHistory[historyID].acked) spy_transmit(&reply, reply_length, &pkt_source); } else if (EmbQueueFilled (activeMBINChannel->hostToGuestSupplyQueue) && EmbQueueSpace (activeMBINChannel->hostToGuestQueue)) { /* ... */ bufferEmbPtr = EmbQueueTakeWord (activeMBINChannel->hostToGuestSupplyQueue); if (bufferEmbPtr && (bufferEmbPtr != NullEmbPtr)) { buffer = (struct rm_aligned_pkt*) HostPointer (bufferEmbPtr); memcpy (&buffer->rm_id[0],&pkt.rm_id[0],REMOTE_MEMORY_ALIGNED_PACKET_HEADER); memcpy (&buffer->data[0], &pkt.data[0], operand); EmbQueuePutWord (activeMBINChannel->hostToGuestQueue, bufferEmbPtr); MBINHistory[historyID].id = id; MBINHistory[historyID].acked = FALSE; } } } } break; case rm_discard: break; } } }
void src3d(float *time0, float *slow0, int nz, int nx, int ny, float h, float ox, float oy, float oz, int *pxs, int *pys, int *pzs, int *cube) { int srctype=1, /* if 1, source is a point; 2, source is on the walls of the data volume; 3, source on wall, time field known; */ srcwall, /* if 1, source on x=0 wall, if 2, on x=nx-1 wall if 3, source on y=0 wall, if 4, on y=ny-1 wall if 5, source on z=0 wall, if 6, on z=nz-1 wall */ xs, /* shot x position (in grid points) */ ys, /* shot y position */ zs, /* shot depth */ xx, yy, zz, /* Used to loop around xs, ys, zs coordinates */ ii, i, j, k, wfint, ofint, nxy, nyz, nxz, nxyz, nwall, NCUBE=2; float fxs, /* shot position in X (in real units)*/ fys, /* shot position in Y (in real units)*/ fzs, /* shot position in Z (in real units)*/ *wall, /* maximum offset (real units) to compute */ /* used in linear velocity gradient cube source */ rx, ry, rz, dvz, dv, v0, rzc, rxyc, rz1, rxy1, rho, theta1, theta2, xsrc1, ysrc1, zsrc1; char *oldtfile, /* file through which old travel times are input */ *wallfile; /* file containing input wall values of traveltimes */ if(!getparint("NCUBE",&NCUBE)) NCUBE=2; if(!getparint("srctype",&srctype)) srctype=1; if(srctype==1) { if(!getparfloat("xsrc1",&xsrc1)) verr("xsrc1 not given"); if(!getparfloat("ysrc1",&ysrc1)) verr("ysrc1 not given"); if(!getparfloat("zsrc1",&zsrc1)) verr("zsrc1 not given"); fxs = (xsrc1-ox)/h; fys = (ysrc1-oy)/h; fzs = (zsrc1-oz)/h; xs = (int)(fxs + 0.5); ys = (int)(fys + 0.5); zs = (int)(fzs + 0.5); if(xs<2 || ys<2 || zs<2 || xs>nx-3 || ys>ny-3 || zs>nz-3){ vwarn("Source near an edge, beware of traveltime errors"); vwarn("for raypaths that travel parallel to edge "); vwarn("while wavefronts are strongly curved, (JV, 8/17/88)\n"); } *pxs = xs; *pys = ys, *pzs = zs, *cube = NCUBE; } else if (srctype==2) { if (!getparint("srcwall",&srcwall)) verr("srcwall not given"); if (!getparstring("wallfile",&wallfile)) verr("wallfile not given"); if((wfint=open(wallfile,O_RDONLY,0664))<=1) { fprintf(stderr,"cannot open %s\n",wallfile); exit(-1); } } else if (srctype==3) { if (!getparint("srcwall",&srcwall)) verr("srcwall not given"); if (!getparstring("oldtfile",&oldtfile)) verr("oldtfile not given"); if((ofint=open(oldtfile,O_RDONLY,0664))<=1) { fprintf(stderr,"cannot open %s\n",oldtfile); exit(-1); } } else { verr("ERROR: incorrect value of srctype"); } nxy = nx * ny; nyz = ny * nz; nxz = nx * nz; nxyz = nx * ny * nz; /* SET TIMES TO DUMMY VALUE */ for(i=0;i<nxyz;i++) time0[i] = 1.0e10; if (srctype == 1) { /* VIDALE'S POINT SOURCE */ /* FILL IN CUBE AROUND SOURCE POINT */ /* HOLE'S NEW LINEAR VELOCITY GRADIENT CUBE (APRIL 1991)*/ v0 = h/s0(xs,ys,zs); for (xx = xs-NCUBE; xx <= xs+NCUBE; xx++) { if (xx < 0 || xx >= nx) continue; for (yy = ys-NCUBE; yy <= ys+NCUBE; yy++) { if (yy < 0 || yy >= ny) continue; for (zz = zs-NCUBE; zz <= zs+NCUBE; zz++) { if (zz < 0 || zz >= nz) continue; if (zz == zs) dvz = 1/s0(xx,yy,zz+1)-1/s0(xs,ys,zs); else dvz = (1/s0(xx,yy,zz)-1/s0(xs,ys,zs))/(zz-zs); dv = fabs(dvz); if (dv == 0.) { t0(xx,yy,zz) = s0(xs,ys,zs)*DIST(fxs,fys,fzs,xx,yy,zz); continue; } rzc = -v0/dv; rx = h*(xx - fxs); ry = h*(yy - fys); rz = h*(zz - fzs); rz1 = rz*dvz/dv; rxy1 = sqrt(rx*rx+ry*ry+rz*rz-rz1*rz1); if (rxy1<=h/1.e6) t0(xx,yy,zz) = fabs(log((v0+dv*rz1)/v0)/dv); else { rxyc = (rz1*rz1+rxy1*rxy1-2*rz1*rzc)/(2*rxy1); rho = sqrt(rzc*rzc+rxyc*rxyc); theta1 = asin(-rzc/rho); /* can't handle asin(1.) ! */ if (fabs(rz1-rzc)>=rho) rho=1.0000001*fabs(rz1-rzc); theta2 = asin((rz1-rzc)/rho); if (rxyc<0) theta1=M_PI-theta1; if (rxyc<rxy1) theta2=M_PI-theta2; t0(xx,yy,zz) = log(tan(theta2/2)/tan(theta1/2)) / dv; } } } } } else if (srctype == 2) { /* HOLE'S EXTERNAL SOURCE */ /* FILL IN WALLS' TIMES FROM EXTERNAL DATAFILE */ read (wfint,wall,4*nwall); /* READ X=0 WALL */ if (wall[0]>-1.e-20) { ii = 0; for (k=0; k<nz; k++) { for (j=0; j<ny; j++) { t0(0,j,k) = wall[ii]; ii++; } } } read (wfint,wall,4*nwall); /* READ X=NX-1 WALL */ if (wall[0]>-1.e-20) { ii = 0; for (k=0; k<nz; k++) { for (j=0; j<ny; j++) { t0(nx-1,j,k) = wall[ii]; ii++; } } } read (wfint,wall,4*nwall); /* READ Y=0 WALL */ if (wall[0]>-1.e-20) { ii = 0; for (k=0; k<nz; k++) { for (i=0; i<nx; i++) { t0(i,0,k) = wall[ii]; ii++; } } } read (wfint,wall,4*nwall); /* READ Y=NY-1 WALL */ if (wall[0]>-1.e-20) { ii = 0; for (k=0; k<nz; k++) { for (i=0; i<nx; i++) { t0(i,ny-1,k) = wall[ii]; ii++; } } } read (wfint,wall,4*nwall); /* READ Z=0 WALL */ if (wall[0]>-1.e-20) { ii = 0; for (j=0; j<ny; j++) { for (i=0; i<nx; i++) { t0(i,j,0) = wall[ii]; ii++; } } } read (wfint,wall,4*nwall); /* READ Z=NZ-1 WALL */ if (wall[0]>-1.e-20) { ii = 0; for (j=0; j<ny; j++) { for (i=0; i<nx; i++) { t0(i,j,nz-1) = wall[ii]; ii++; } } } } else if (srctype == 3) { /* HOLE'S REDO OLD TIMES */ /* READ IN OLD TIME FILE */ if (srctype == 3) read(ofint,time0,nxyz*4); } return; }
void vdie(const char *format, va_list alist) { vwarn(format, alist); exit(E_ERROR); }
/********************************************************************************************* * * Function: save_recording_queue_chunk * Purpose: saves one chunk of the queue to the recording file, using run-length encoding. * *********************************************************************************************/ void save_recording_queue_chunk( short player_index) { uint8 *location; uint32 last_flag, count, flag = 0; int16 i, run_count, num_flags_saved, max_flags; static uint8 *buffer= NULL; ActionQueue *queue; // The data format is (run length (int16)) + (action flag (uint32)) int DataSize = sizeof(int16) + sizeof(uint32); if (buffer == NULL) buffer = new byte[RECORD_CHUNK_SIZE * DataSize]; location= buffer; count= 0; // keeps track of how many bytes we'll save. last_flag= (uint32)NONE; queue= get_player_recording_queue(player_index); // don't want to save too much stuff max_flags= MIN(RECORD_CHUNK_SIZE, get_recording_queue_size(player_index)); // save what's in the queue run_count= num_flags_saved= 0; for (i = 0; i<max_flags; i++) { flag = queue->buffer[queue->read_index]; INCREMENT_QUEUE_COUNTER(queue->read_index); if (i && flag != last_flag) { ValueToStream(location,run_count); ValueToStream(location,last_flag); count += DataSize; num_flags_saved += run_count; run_count = 1; } else { run_count++; } last_flag = flag; } // now save the final run ValueToStream(location,run_count); ValueToStream(location,last_flag); count += DataSize; num_flags_saved += run_count; if (max_flags<RECORD_CHUNK_SIZE) { short end_indicator = END_OF_RECORDING_INDICATOR; ValueToStream(location,end_indicator); int32 end_flag = 0; ValueToStream(location,end_flag); count += DataSize; num_flags_saved += RECORD_CHUNK_SIZE-max_flags; } FilmFile.Write(count,buffer); replay.header.length+= count; vwarn(num_flags_saved == RECORD_CHUNK_SIZE, csprintf(temporary, "bad recording: %d flags, max=%d, count = %u;dm #%p #%u", num_flags_saved, max_flags, count, buffer, count)); }