Advertisement
Miki76

Untitled

Apr 4th, 2016
193
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.36 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. FILE *f;
  85. f = fopen("new2.txt", "a");
  86.  
  87. nx = NINT((xmax - x0)/dx);
  88. ny = NINT((ymax - y0)/dy);
  89. nz = NINT((zmax - z0)/dz);
  90.  
  91.  
  92. if (!xflg)
  93. {
  94. jnk_i = nx;
  95. nx = ny;
  96. ny = jnk_i;
  97. jnk_f = x0;
  98. x0 = y0;
  99. y0 = jnk_f;
  100. jnk_f = xmax;
  101. xmax = ymax;
  102. ymax = jnk_f;
  103. jnk_f = dx;
  104. dx = dy;
  105. dy = jnk_f;
  106. }
  107. nxy = nx * ny;
  108. nxyz = nx * ny * nz;
  109. if ((vel = (float *) malloc(nxyz*sizeof(float))) == (float *) NULL)
  110. {
  111. fprintf(stderr,"%s: Couldn't allocate memory for vel.\n", progname);
  112. return (-1);
  113. }
  114.  
  115. if ((vu = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  116. {
  117. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  118. return (-1);
  119. }
  120. if ((vl = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  121. {
  122. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  123. return (-1);
  124. }
  125. if ((zu = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  126. {
  127. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  128. return (-1);
  129. }
  130. if ((zl = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  131. {
  132. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  133. return (-1);
  134. }
  135. if ((x = (float *) malloc(nc*sizeof(float))) == (float *) NULL)
  136. {
  137. fprintf(stderr,"%s: Couldn't allocate memory.\n", progname);
  138. return (-1);
  139. }
  140.  
  141. /* open files */
  142. if ((f1_out = fopen(vfname, "w")) == (FILE *) NULL)
  143. {
  144. fprintf(stderr, "%s: Can't open file %s.\n", progname, vfname);
  145. return (-1);
  146. }
  147. if ((f2_out = fopen(cfname, "w")) == (FILE *) NULL)
  148. {
  149. fprintf(stderr, "%s: Can't open file %s.\n", progname, cfname);
  150. return (-1);
  151. }
  152.  
  153. for (j=0; j<nc; j++)
  154. {
  155. fscanf(stdin,"%f", &x[j]);
  156. x[j] -= x0;
  157. }
  158. fprintf(stdout, "Node locations (x):\n");
  159. for (i=0; i<nc; i++)
  160. fprintf(stdout,"%.1f\t", x[i]);
  161. fprintf(stdout, "\nVelocity profiles (z v):\n");
  162. /* read first layer before entering loop */
  163. for (j=0; j<nc; j++)
  164. {
  165. fscanf(stdin,"%f %f", &zl[j], &vl[j]);
  166. zl[j] -= z0;
  167. fprintf(stdout, "%4.1f %4.1f\t", zl[j]+z0, vl[j]);
  168. }
  169. fprintf(stdout,"\n");
  170.  
  171. for (i=1; i<nd; i++)
  172. { /* repeat over all layers */
  173. for (j=0; j<nc; j++)
  174. { /* lower boundary is now top boundary */
  175. zu[j] = zl[j];
  176. vu[j] = vl[j];
  177. }
  178. for (j=0; j<nc; j++)
  179. {
  180. fscanf(stdin,"%f %f", &zl[j], &vl[j]);
  181. zl[j] -= z0;
  182. fprintf(stdout, "%4.1f %4.1f\t", zl[j]+z0, vl[j]);
  183. }
  184. fprintf(stdout,"\n");
  185. for (k=0,j=1; k<nx; k++)
  186. {
  187. this_x = k*dx;
  188. if (this_x > x[j])
  189. j++;
  190. /* check a second time */
  191. if (this_x > x[j])
  192. j++;
  193. v1 = vu[j-1] + (this_x - x[j-1]) * (vu[j] - vu[j-1]) / (x[j] - x[j-1]);
  194. v2 = vl[j-1] + (this_x - x[j-1]) * (vl[j] - vl[j-1]) / (x[j] - x[j-1]);
  195. zxu = zu[j-1] + (this_x - x[j-1]) * (zu[j] - zu[j-1]) / (x[j] - x[j-1]);
  196. zxl = zl[j-1] + (this_x - x[j-1]) * (zl[j] - zl[j-1]) / (x[j] - x[j-1]);
  197. for (m=0; m<nz; m++)
  198. {
  199. this_z = m*dz;
  200. if (this_z >= zxu && this_z < zxl)
  201. {
  202. velocity = v1 + (this_z - zxu) * (v2 - v1) / (zxl - zxu);
  203. /* fprintf(stdout, "this_x=%f, x[%d]=%f\n", this_x, j-1, x[j-1]); */
  204. if (this_x == x[j-1])
  205. fprintf(f2_out, "%.1f %.1f %.1f\n", this_x, this_z, velocity);
  206. for (l=0; l<ny; l++)
  207. {
  208. if (xflg){
  209. index = m * nxy + l*nx + k;
  210. fprintf(f,"%5d\n",index);
  211. } else {
  212. index = m * nxy + k*ny + l;
  213. vel[index] = velocity;
  214. }
  215. }
  216. }
  217. }
  218. }
  219.  
  220. } /* closer for (i) - all layers*/
  221. yes = fwrite(vel, nxyz*sizeof(float), 1, f1_out);
  222. if (!xflg)
  223. {
  224. jnk_i = nx;
  225. nx = ny;
  226. ny = jnk_i;
  227. }
  228. if (yes != 1) fprintf(stdout,"%s: Hey, fwrite error, code %d.\n", progname, yes);
  229. fprintf(stdout,"\nnx = %d\nny = %d\nnz = %d\n", nx, ny, nz);
  230. return (0);
  231. } /* the end */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement