Advertisement
Miki76

vel2d

Apr 1st, 2016
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.38 KB | None | 0 0
  1. /* vel2d.c:
  2. * Creates starting 2-D velocity profile as a 3D model. Model
  3. * originates in upper left corner, and variation is in x and z.
  4. * Specify x-locataions along the profile where velocity depth
  5. * profiles are specified....
  6. *
  7. * ari, oct 02
  8. *
  9. * V 1.2
  10. * Option to build the model in y-direction rather than in x....
  11. *
  12. * ari, feb 04
  13. *
  14. * V 1.2.3
  15. * Small fix to allow vertical columns to be "double" (like in horizontal layering) - to allow
  16. * horizontal discontinuities as well.
  17. *
  18. * ari, sep 11
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <math.h>
  24.  
  25. #define VER 1.2
  26. #define FIX 3
  27. #define AUTHOR "Ari Tryggvason"
  28. #define DATE "110908"
  29.  
  30. #define MAX 250
  31. #define MAXL 25
  32. #define NINT(x) (int) floor((x)+0.5)
  33.  
  34. char *progname;
  35. int main(argc, argv)
  36. int argc;
  37. char **argv;
  38. {
  39. char *progname,
  40. string[MAX], vfname[MAX], cfname[MAX];
  41.  
  42. int i, j, k, l, m, nx, ny, nz, nxy, nxyz, nc, nd, yes, index,
  43. xflg, jnk_i;
  44.  
  45. float dx, dy, dz, x0, y0, z0, xmax, ymax, zmax, *x, this_x, this_z,
  46. *vel, *zl, *vl, *zu, *vu, v1, v2, zxu, zxl, velocity, jnk_f;
  47.  
  48. FILE *f1_out, *f2_out;
  49.  
  50. int get_line();
  51.  
  52. /* command line argument(s) */
  53. progname = *argv;
  54.  
  55. fprintf(stderr,"%s V %.1f.%1d by %s %s\n", progname, VER, FIX, AUTHOR, DATE);
  56.  
  57. /* read form stdin */
  58. fprintf(stdout,"Output 2-D velocity file name: ");
  59. get_line(stdin,string, MAX);
  60. sscanf(string,"%s", cfname);
  61. fprintf(stdout,"Output 3-D velocity file name: ");
  62. get_line(stdin,string, MAX);
  63. sscanf(string,"%s", vfname);
  64. fprintf(stdout,"Spatial sample interval in km (x y z): ");
  65. get_line(stdin,string, MAX);
  66. sscanf(string,"%f %f %f", &dx, &dy, &dz);
  67. fprintf(stdout,"Origin of model in km (x y z): ");
  68. get_line(stdin,string, MAX);
  69. sscanf(string,"%f %f %f", &x0, &y0, &z0);
  70. fprintf(stdout,"Opposite corner of model in km (x y z): ");
  71. get_line(stdin,string, MAX);
  72. sscanf(string,"%f %f %f", &xmax, &ymax, &zmax);
  73. fprintf(stdout,"Velocities to vary in X (1) or Y (0) direction: ");
  74. get_line(stdin,string, MAX);
  75. sscanf(string,"%d", &xflg);
  76. fprintf(stdout,"Number of depth nodes and columns: ");
  77. get_line(stdin, string, MAX);
  78. sscanf(string,"%d %d", &nd, &nc);
  79. fprintf(stdout, "x0=%.1f y0=%.1f z0=%.1f\n", x0, y0, z0);
  80. fprintf(stdout, "xm=%.1f ym=%.1f zm=%.1f\n", xmax, ymax, zmax);
  81. fprintf(stdout, "dx=%.1f dy=%.1f dz=%.1f\n", dx, dy, dz);
  82. fprintf(stdout, "nc=%d nd=%d\n", nc, nd);
  83.  
  84. nx = NINT((xmax - x0)/dx);
  85. ny = NINT((ymax - y0)/dy);
  86. nz = NINT((zmax - z0)/dz);
  87. if (!xflg)
  88. {
  89. jnk_i = nx;
  90. nx = ny;
  91. ny = jnk_i;
  92. jnk_f = x0;
  93. x0 = y0;
  94. y0 = jnk_f;
  95. jnk_f = xmax;
  96. xmax = ymax;
  97. ymax = jnk_f;
  98. jnk_f = dx;
  99. dx = dy;
  100. dy = jnk_f;
  101. }
  102. nxy = nx * ny;
  103. nxyz = nx * ny * nz;
  104. if ((vel = (float *) malloc(nxyz*sizeof(float))) == (float *) NULL)
  105. {
  106. fprintf(stderr,"%s: Couldn't allocate memory for vel.\n", progname);
  107. return (-1);
  108. }
  109.  
  110. if ((vu = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  111. {
  112. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  113. return (-1);
  114. }
  115. if ((vl = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  116. {
  117. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  118. return (-1);
  119. }
  120. if ((zu = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  121. {
  122. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  123. return (-1);
  124. }
  125. if ((zl = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  126. {
  127. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  128. return (-1);
  129. }
  130. if ((x = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  131. {
  132. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  133. return (-1);
  134. }
  135.  
  136. /* open files */
  137. if ((f1_out = fopen(vfname, "w")) == (FILE *) NULL)
  138. {
  139. fprintf(stderr, "%s: Can't open file %s.\n", progname, vfname);
  140. return (-1);
  141. }
  142. if ((f2_out = fopen(cfname, "w")) == (FILE *) NULL)
  143. {
  144. fprintf(stderr, "%s: Can't open file %s.\n", progname, cfname);
  145. return (-1);
  146. }
  147.  
  148. for (j=0; j<nc; j++)
  149. {
  150. fscanf(stdin,"%f", &x[j]);
  151. x[j] -= x0;
  152. }
  153. fprintf(stdout, "Node locations (x):\n");
  154. for (i=0; i<nc; i++)
  155. fprintf(stdout,"%.1f\t", x[i]);
  156. fprintf(stdout, "\nVelocity profiles (z v):\n");
  157. /* read first layer before entering loop */
  158. for (j=0; j<nc; j++)
  159. {
  160. fscanf(stdin,"%f %f", &zl[j], &vl[j]);
  161. zl[j] -= z0;
  162. fprintf(stdout, "%4.1f %4.1f\t", zl[j]+z0, vl[j]);
  163. }
  164. fprintf(stdout,"\n");
  165.  
  166. for (i=1; i<nd; i++)
  167. { /* repeat over all layers */
  168. for (j=0; j<nc; j++)
  169. { /* lower boundary is now top boundary */
  170. zu[j] = zl[j];
  171. vu[j] = vl[j];
  172. }
  173. for (j=0; j<nc; j++)
  174. {
  175. fscanf(stdin,"%f %f", &zl[j], &vl[j]);
  176. zl[j] -= z0;
  177. fprintf(stdout, "%4.1f %4.1f\t", zl[j]+z0, vl[j]);
  178. }
  179. fprintf(stdout,"\n");
  180. for (k=0,j=1; k<nx; k++)
  181. {
  182. this_x = k*dx;
  183. if (this_x > x[j])
  184. j++;
  185. /* check a second time */
  186. if (this_x > x[j])
  187. j++;
  188. v1 = vu[j-1] + (this_x - x[j-1]) * (vu[j] - vu[j-1]) / (x[j] - x[j-1]);
  189. v2 = vl[j-1] + (this_x - x[j-1]) * (vl[j] - vl[j-1]) / (x[j] - x[j-1]);
  190. zxu = zu[j-1] + (this_x - x[j-1]) * (zu[j] - zu[j-1]) / (x[j] - x[j-1]);
  191. zxl = zl[j-1] + (this_x - x[j-1]) * (zl[j] - zl[j-1]) / (x[j] - x[j-1]);
  192. for (m=0; m<nz; m++)
  193. {
  194. this_z = m*dz;
  195. if (this_z >= zxu && this_z < zxl)
  196. {
  197. velocity = v1 + (this_z - zxu) * (v2 - v1) / (zxl - zxu);
  198. /* fprintf(stdout, "this_x=%f, x[%d]=%f\n", this_x, j-1, x[j-1]); */
  199. if (this_x == x[j-1])
  200. fprintf(f2_out, "%.1f %.1f %.1f\n", this_x, this_z, velocity);
  201. for (l=0; l<ny; l++)
  202. {
  203. if (xflg)
  204. index = m * nxy + l*nx + k;
  205. fprintf(stdout,"%.1f",index);
  206. else
  207. index = m * nxy + k*ny + l;
  208. vel[index] = velocity;
  209. /* fprintf(stdout,"%.1f %.1f %.1f ", this_z, this_x, velocity); */
  210. }
  211. }
  212. }
  213. }
  214.  
  215. } /* closer for (i) - all layers*/
  216. yes = fwrite(vel, nxyz*sizeof(float), 1, f1_out);
  217. if (!xflg)
  218. {
  219. jnk_i = nx;
  220. nx = ny;
  221. ny = jnk_i;
  222. }
  223. if (yes != 1) fprintf(stdout,"%s: Hey, fwrite error, code %d.\n", progname, yes);
  224. fprintf(stdout,"\nnx = %d\nny = %d\nnz = %d\n", nx, ny, nz);
  225. return (0);
  226. } /* the end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement