miniWeather_openacc.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. //////////////////////////////////////////////////////////////////////////////////////////
  2. // miniWeather
  3. // Author: Matt Norman <normanmr@ornl.gov> , Oak Ridge National Laboratory
  4. // This code simulates dry, stratified, compressible, non-hydrostatic fluid flows
  5. // For documentation, please see the attached documentation in the "documentation" folder
  6. //////////////////////////////////////////////////////////////////////////////////////////
  7. #include <stdlib.h>
  8. #include <math.h>
  9. #include <stdio.h>
  10. #include <nvtx3/nvToolsExt.h>
  11. const double pi = 3.14159265358979323846264338327; //Pi
  12. const double grav = 9.8; //Gravitational acceleration (m / s^2)
  13. const double cp = 1004.; //Specific heat of dry air at constant pressure
  14. const double rd = 287.; //Dry air constant for equation of state (P=rho*rd*T)
  15. const double p0 = 1.e5; //Standard pressure at the surface in Pascals
  16. const double C0 = 27.5629410929725921310572974482; //Constant to translate potential temperature into pressure (P=C0*(rho*theta)**gamma)
  17. const double gamm = 1.40027894002789400278940027894; //gamma=cp/Rd , have to call this gamm because "gamma" is taken (I hate C so much)
  18. //Define domain and stability-related constants
  19. const double xlen = 2.e4; //Length of the domain in the x-direction (meters)
  20. const double zlen = 1.e4; //Length of the domain in the z-direction (meters)
  21. const double hv_beta = 0.25; //How strong to diffuse the solution: hv_beta \in [0:1]
  22. const double cfl = 1.50; //"Courant, Friedrichs, Lewy" number (for numerical stability)
  23. const double max_speed = 450; //Assumed maximum wave speed during the simulation (speed of sound + speed of wind) (meter / sec)
  24. const int hs = 2; //"Halo" size: number of cells needed for a full "stencil" of information for reconstruction
  25. const int sten_size = 4; //Size of the stencil used for interpolation
  26. //Parameters for indexing and flags
  27. const int NUM_VARS = 4; //Number of fluid state variables
  28. const int ID_DENS = 0; //index for density ("rho")
  29. const int ID_UMOM = 1; //index for momentum in the x-direction ("rho * u")
  30. const int ID_WMOM = 2; //index for momentum in the z-direction ("rho * w")
  31. const int ID_RHOT = 3; //index for density * potential temperature ("rho * theta")
  32. const int DIR_X = 1; //Integer constant to express that this operation is in the x-direction
  33. const int DIR_Z = 2; //Integer constant to express that this operation is in the z-direction
  34. const int nqpoints = 3;
  35. double qpoints[] = {0.112701665379258311482073460022E0, 0.500000000000000000000000000000E0, 0.887298334620741688517926539980E0};
  36. double qweights[] = {0.277777777777777777777777777779E0, 0.444444444444444444444444444444E0, 0.277777777777777777777777777779E0};
  37. ///////////////////////////////////////////////////////////////////////////////////////
  38. // Variables that are initialized but remain static over the course of the simulation
  39. ///////////////////////////////////////////////////////////////////////////////////////
  40. double sim_time; //total simulation time in seconds
  41. double output_freq; //frequency to perform output in seconds
  42. double dt; //Model time step (seconds)
  43. int nx, nz; //Number of local grid cells in the x- and z- dimensions
  44. double dx, dz; //Grid space length in x- and z-dimension (meters)
  45. int nx_glob, nz_glob; //Number of total grid cells in the x- and z- dimensions
  46. int i_beg, k_beg; //beginning index in the x- and z-directions
  47. int nranks, myrank; //my rank id
  48. int left_rank, right_rank; //Rank IDs that exist to my left and right in the global domain
  49. double *hy_dens_cell; //hydrostatic density (vert cell avgs). Dimensions: (1-hs:nz+hs)
  50. double *hy_dens_theta_cell; //hydrostatic rho*t (vert cell avgs). Dimensions: (1-hs:nz+hs)
  51. double *hy_dens_int; //hydrostatic density (vert cell interf). Dimensions: (1:nz+1)
  52. double *hy_dens_theta_int; //hydrostatic rho*t (vert cell interf). Dimensions: (1:nz+1)
  53. double *hy_pressure_int; //hydrostatic press (vert cell interf). Dimensions: (1:nz+1)
  54. ///////////////////////////////////////////////////////////////////////////////////////
  55. // Variables that are dynamics over the course of the simulation
  56. ///////////////////////////////////////////////////////////////////////////////////////
  57. double etime; //Elapsed model time
  58. double output_counter; //Helps determine when it's time to do output
  59. //Runtime variable arrays
  60. double *state; //Fluid state. Dimensions: (1-hs:nx+hs,1-hs:nz+hs,NUM_VARS)
  61. double *state_tmp; //Fluid state. Dimensions: (1-hs:nx+hs,1-hs:nz+hs,NUM_VARS)
  62. double *flux; //Cell interface fluxes. Dimensions: (nx+1,nz+1,NUM_VARS)
  63. double *tend; //Fluid state tendencies. Dimensions: (nx,nz,NUM_VARS)
  64. int num_out = 0; //The number of outputs performed so far
  65. int direction_switch = 1;
  66. //How is this not in the standard?!
  67. double dmin(double a, double b)
  68. {
  69. if (a < b)
  70. {
  71. return a;
  72. }
  73. else
  74. {
  75. return b;
  76. }
  77. };
  78. //Declaring the functions defined after "main"
  79. void init();
  80. void finalize();
  81. void injection(double x, double z, double &r, double &u, double &w, double &t, double &hr, double &ht);
  82. void hydro_const_theta(double z, double &r, double &t);
  83. void output(double *state, double etime);
  84. void ncwrap(int ierr, int line);
  85. void perform_timestep(double *state, double *state_tmp, double *flux, double *tend, double dt);
  86. void semi_discrete_step(double *state_init, double *state_forcing, double *state_out, double dt, int dir, double *flux, double *tend);
  87. void compute_tendencies_x(double *state, double *flux, double *tend);
  88. void compute_tendencies_z(double *state, double *flux, double *tend);
  89. void set_halo_values_x(double *state);
  90. void set_halo_values_z(double *state);
  91. ///////////////////////////////////////////////////////////////////////////////////////
  92. // THE MAIN PROGRAM STARTS HERE
  93. ///////////////////////////////////////////////////////////////////////////////////////
  94. int main(int argc, char **argv)
  95. {
  96. ///////////////////////////////////////////////////////////////////////////////////////
  97. // BEGIN USER-CONFIGURABLE PARAMETERS
  98. ///////////////////////////////////////////////////////////////////////////////////////
  99. //The x-direction length is twice as long as the z-direction length
  100. //So, you'll want to have nx_glob be twice as large as nz_glob
  101. nx_glob = 40; //Number of total cells in the x-dirction
  102. nz_glob = 20; //Number of total cells in the z-dirction
  103. sim_time = 1000; //How many seconds to run the simulation
  104. output_freq = 100; //How frequently to output data to file (in seconds)
  105. ///////////////////////////////////////////////////////////////////////////////////////
  106. // END USER-CONFIGURABLE PARAMETERS
  107. ///////////////////////////////////////////////////////////////////////////////////////
  108. if (argc == 4)
  109. {
  110. printf("The arguments supplied are %s %s %s\n", argv[1], argv[2], argv[3]);
  111. nx_glob = atoi(argv[1]);
  112. nz_glob = atoi(argv[2]);
  113. sim_time = atoi(argv[3]);
  114. }
  115. else
  116. {
  117. printf("Using default values ...\n");
  118. }
  119. nvtxRangePushA("Total");
  120. init();
  121. #pragma acc data copyin(state_tmp[(nz + 2 * hs) * (nx + 2 * hs) * NUM_VARS], hy_dens_cell[nz + 2 * hs], hy_dens_theta_cell[nz + 2 * hs], hy_dens_int[nz + 1], hy_dens_theta_int[nz + 1], hy_pressure_int[nz + 1]) \
  122. create(flux[(nz + 1) * (nx + 1) * NUM_VARS], tend[nz * nx * NUM_VARS]) \
  123. copy(state [0:(nz + 2 * hs) * (nx + 2 * hs) * NUM_VARS])
  124. {
  125. //Output the initial state
  126. //output(state, etime);
  127. ////////////////////////////////////////////////////
  128. // MAIN TIME STEP LOOP
  129. ////////////////////////////////////////////////////
  130. nvtxRangePushA("while");
  131. while (etime < sim_time)
  132. {
  133. //If the time step leads to exceeding the simulation time, shorten it for the last step
  134. if (etime + dt > sim_time)
  135. {
  136. dt = sim_time - etime;
  137. }
  138. //Perform a single time step
  139. nvtxRangePushA("perform_timestep");
  140. perform_timestep(state, state_tmp, flux, tend, dt);
  141. nvtxRangePop();
  142. //Inform the user
  143. printf("Elapsed Time: %lf / %lf\n", etime, sim_time);
  144. //Update the elapsed time and output counter
  145. etime = etime + dt;
  146. output_counter = output_counter + dt;
  147. //If it's time for output, reset the counter, and do output
  148. if (output_counter >= output_freq)
  149. {
  150. output_counter = output_counter - output_freq;
  151. #pragma acc update host(state[(nz + 2 * hs) * (nx + 2 * hs) * NUM_VARS])
  152. //output(state, etime);
  153. }
  154. }
  155. nvtxRangePop();
  156. }
  157. finalize();
  158. nvtxRangePop();
  159. }
  160. //Performs a single dimensionally split time step using a simple low-storate three-stage Runge-Kutta time integrator
  161. //The dimensional splitting is a second-order-accurate alternating Strang splitting in which the
  162. //order of directions is alternated each time step.
  163. //The Runge-Kutta method used here is defined as follows:
  164. // q* = q[n] + dt/3 * rhs(q[n])
  165. // q** = q[n] + dt/2 * rhs(q* )
  166. // q[n+1] = q[n] + dt/1 * rhs(q** )
  167. void perform_timestep(double *state, double *state_tmp, double *flux, double *tend, double dt)
  168. {
  169. if (direction_switch)
  170. {
  171. //x-direction first
  172. semi_discrete_step(state, state, state_tmp, dt / 3, DIR_X, flux, tend);
  173. semi_discrete_step(state, state_tmp, state_tmp, dt / 2, DIR_X, flux, tend);
  174. semi_discrete_step(state, state_tmp, state, dt / 1, DIR_X, flux, tend);
  175. //z-direction second
  176. semi_discrete_step(state, state, state_tmp, dt / 3, DIR_Z, flux, tend);
  177. semi_discrete_step(state, state_tmp, state_tmp, dt / 2, DIR_Z, flux, tend);
  178. semi_discrete_step(state, state_tmp, state, dt / 1, DIR_Z, flux, tend);
  179. }
  180. else
  181. {
  182. //z-direction second
  183. semi_discrete_step(state, state, state_tmp, dt / 3, DIR_Z, flux, tend);
  184. semi_discrete_step(state, state_tmp, state_tmp, dt / 2, DIR_Z, flux, tend);
  185. semi_discrete_step(state, state_tmp, state, dt / 1, DIR_Z, flux, tend);
  186. //x-direction first
  187. semi_discrete_step(state, state, state_tmp, dt / 3, DIR_X, flux, tend);
  188. semi_discrete_step(state, state_tmp, state_tmp, dt / 2, DIR_X, flux, tend);
  189. semi_discrete_step(state, state_tmp, state, dt / 1, DIR_X, flux, tend);
  190. }
  191. if (direction_switch)
  192. {
  193. direction_switch = 0;
  194. }
  195. else
  196. {
  197. direction_switch = 1;
  198. }
  199. }
  200. //Perform a single semi-discretized step in time with the form:
  201. //state_out = state_init + dt * rhs(state_forcing)
  202. //Meaning the step starts from state_init, computes the rhs using state_forcing, and stores the result in state_out
  203. void semi_discrete_step(double *state_init, double *state_forcing, double *state_out, double dt, int dir, double *flux, double *tend)
  204. {
  205. int i, k, ll, inds, indt;
  206. if (dir == DIR_X)
  207. {
  208. //Set the halo values in the x-direction
  209. set_halo_values_x(state_forcing);
  210. //Compute the time tendencies for the fluid state in the x-direction
  211. compute_tendencies_x(state_forcing, flux, tend);
  212. }
  213. else if (dir == DIR_Z)
  214. {
  215. //Set the halo values in the z-direction
  216. set_halo_values_z(state_forcing);
  217. //Compute the time tendencies for the fluid state in the z-direction
  218. compute_tendencies_z(state_forcing, flux, tend);
  219. }
  220. /////////////////////////////////////////////////
  221. // TODO: THREAD ME
  222. /////////////////////////////////////////////////
  223. //Apply the tendencies to the fluid state
  224. #pragma acc parallel loop collapse(3) private(inds, indt) default(present)
  225. for (ll = 0; ll < NUM_VARS; ll++)
  226. {
  227. for (k = 0; k < nz; k++)
  228. {
  229. for (i = 0; i < nx; i++)
  230. {
  231. inds = ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + i + hs;
  232. indt = ll * nz * nx + k * nx + i;
  233. state_out[inds] = state_init[inds] + dt * tend[indt];
  234. }
  235. }
  236. }
  237. }
  238. //Compute the time tendencies of the fluid state using forcing in the x-direction
  239. //First, compute the flux vector at each cell interface in the x-direction (including hyperviscosity)
  240. //Then, compute the tendencies using those fluxes
  241. void compute_tendencies_x(double *state, double *flux, double *tend)
  242. {
  243. int i, k, ll, s, inds, indf1, indf2, indt;
  244. double r, u, w, t, p, stencil[4], d3_vals[NUM_VARS], vals[NUM_VARS], hv_coef;
  245. //Compute the hyperviscosity coeficient
  246. hv_coef = -hv_beta * dx / (16 * dt);
  247. /////////////////////////////////////////////////
  248. // TODO: THREAD ME
  249. /////////////////////////////////////////////////
  250. //Compute fluxes in the x-direction for each cell
  251. #pragma acc parallel loop collapse(2) private(ll, s, inds, stencil, vals, d3_vals, r, u, w, t, p) default(present)
  252. for (k = 0; k < nz; k++)
  253. {
  254. for (i = 0; i < nx + 1; i++)
  255. {
  256. //Use fourth-order interpolation from four cell averages to compute the value at the interface in question
  257. for (ll = 0; ll < NUM_VARS; ll++)
  258. {
  259. for (s = 0; s < sten_size; s++)
  260. {
  261. inds = ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + i + s;
  262. stencil[s] = state[inds];
  263. }
  264. //Fourth-order-accurate interpolation of the state
  265. vals[ll] = -stencil[0] / 12 + 7 * stencil[1] / 12 + 7 * stencil[2] / 12 - stencil[3] / 12;
  266. //First-order-accurate interpolation of the third spatial derivative of the state (for artificial viscosity)
  267. d3_vals[ll] = -stencil[0] + 3 * stencil[1] - 3 * stencil[2] + stencil[3];
  268. }
  269. //Compute density, u-wind, w-wind, potential temperature, and pressure (r,u,w,t,p respectively)
  270. r = vals[ID_DENS] + hy_dens_cell[k + hs];
  271. u = vals[ID_UMOM] / r;
  272. w = vals[ID_WMOM] / r;
  273. t = (vals[ID_RHOT] + hy_dens_theta_cell[k + hs]) / r;
  274. p = C0 * pow((r * t), gamm);
  275. //Compute the flux vector
  276. flux[ID_DENS * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * u - hv_coef * d3_vals[ID_DENS];
  277. flux[ID_UMOM * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * u * u + p - hv_coef * d3_vals[ID_UMOM];
  278. flux[ID_WMOM * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * u * w - hv_coef * d3_vals[ID_WMOM];
  279. flux[ID_RHOT * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * u * t - hv_coef * d3_vals[ID_RHOT];
  280. }
  281. }
  282. /////////////////////////////////////////////////
  283. // TODO: THREAD ME
  284. /////////////////////////////////////////////////
  285. //Use the fluxes to compute tendencies for each cell
  286. #pragma acc parallel loop collapse(3) private(indt, indf1, indf2) default(present)
  287. for (ll = 0; ll < NUM_VARS; ll++)
  288. {
  289. for (k = 0; k < nz; k++)
  290. {
  291. for (i = 0; i < nx; i++)
  292. {
  293. indt = ll * nz * nx + k * nx + i;
  294. indf1 = ll * (nz + 1) * (nx + 1) + k * (nx + 1) + i;
  295. indf2 = ll * (nz + 1) * (nx + 1) + k * (nx + 1) + i + 1;
  296. tend[indt] = -(flux[indf2] - flux[indf1]) / dx;
  297. }
  298. }
  299. }
  300. }
  301. //Compute the time tendencies of the fluid state using forcing in the z-direction
  302. //First, compute the flux vector at each cell interface in the z-direction (including hyperviscosity)
  303. //Then, compute the tendencies using those fluxes
  304. void compute_tendencies_z(double *state, double *flux, double *tend)
  305. {
  306. int i, k, ll, s, inds, indf1, indf2, indt;
  307. double r, u, w, t, p, stencil[4], d3_vals[NUM_VARS], vals[NUM_VARS], hv_coef;
  308. //Compute the hyperviscosity coeficient
  309. hv_coef = -hv_beta * dx / (16 * dt);
  310. /////////////////////////////////////////////////
  311. // TODO: THREAD ME
  312. /////////////////////////////////////////////////
  313. //Compute fluxes in the x-direction for each cell
  314. #pragma acc parallel loop collapse(2) private(ll, s, inds, stencil, vals, d3_vals, r, u, w, t, p) default(present)
  315. for (k = 0; k < nz + 1; k++)
  316. {
  317. for (i = 0; i < nx; i++)
  318. {
  319. //Use fourth-order interpolation from four cell averages to compute the value at the interface in question
  320. for (ll = 0; ll < NUM_VARS; ll++)
  321. {
  322. for (s = 0; s < sten_size; s++)
  323. {
  324. inds = ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + s) * (nx + 2 * hs) + i + hs;
  325. stencil[s] = state[inds];
  326. }
  327. //Fourth-order-accurate interpolation of the state
  328. vals[ll] = -stencil[0] / 12 + 7 * stencil[1] / 12 + 7 * stencil[2] / 12 - stencil[3] / 12;
  329. //First-order-accurate interpolation of the third spatial derivative of the state
  330. d3_vals[ll] = -stencil[0] + 3 * stencil[1] - 3 * stencil[2] + stencil[3];
  331. }
  332. //Compute density, u-wind, w-wind, potential temperature, and pressure (r,u,w,t,p respectively)
  333. r = vals[ID_DENS] + hy_dens_int[k];
  334. u = vals[ID_UMOM] / r;
  335. w = vals[ID_WMOM] / r;
  336. t = (vals[ID_RHOT] + hy_dens_theta_int[k]) / r;
  337. p = C0 * pow((r * t), gamm) - hy_pressure_int[k];
  338. //Compute the flux vector with hyperviscosity
  339. flux[ID_DENS * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * w - hv_coef * d3_vals[ID_DENS];
  340. flux[ID_UMOM * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * w * u - hv_coef * d3_vals[ID_UMOM];
  341. flux[ID_WMOM * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * w * w + p - hv_coef * d3_vals[ID_WMOM];
  342. flux[ID_RHOT * (nz + 1) * (nx + 1) + k * (nx + 1) + i] = r * w * t - hv_coef * d3_vals[ID_RHOT];
  343. }
  344. }
  345. /////////////////////////////////////////////////
  346. // TODO: THREAD ME
  347. /////////////////////////////////////////////////
  348. //Use the fluxes to compute tendencies for each cell
  349. #pragma acc parallel loop collapse(3) private(indt, indf1, indf2) default(present)
  350. for (ll = 0; ll < NUM_VARS; ll++)
  351. {
  352. for (k = 0; k < nz; k++)
  353. {
  354. for (i = 0; i < nx; i++)
  355. {
  356. indt = ll * nz * nx + k * nx + i;
  357. indf1 = ll * (nz + 1) * (nx + 1) + (k) * (nx + 1) + i;
  358. indf2 = ll * (nz + 1) * (nx + 1) + (k + 1) * (nx + 1) + i;
  359. tend[indt] = -(flux[indf2] - flux[indf1]) / dz;
  360. if (ll == ID_WMOM)
  361. {
  362. inds = ID_DENS * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + i + hs;
  363. tend[indt] = tend[indt] - state[inds] * grav;
  364. }
  365. }
  366. }
  367. }
  368. }
  369. void set_halo_values_x(double *state)
  370. {
  371. int k, ll, ind_r, ind_u, ind_t, i;
  372. double z;
  373. #pragma acc parallel loop collapse(2) default(present)
  374. for (ll = 0; ll < NUM_VARS; ll++)
  375. {
  376. for (k = 0; k < nz; k++)
  377. {
  378. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + 0] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + nx + hs - 2];
  379. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + 1] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + nx + hs - 1];
  380. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + nx + hs] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + hs];
  381. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + nx + hs + 1] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + hs + 1];
  382. }
  383. }
  384. ////////////////////////////////////////////////////
  385. if (myrank == 0)
  386. {
  387. for (k = 0; k < nz; k++)
  388. {
  389. for (i = 0; i < hs; i++)
  390. {
  391. z = (k_beg + k + 0.5) * dz;
  392. if (abs(z - 3 * zlen / 4) <= zlen / 16)
  393. {
  394. ind_r = ID_DENS * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + i;
  395. ind_u = ID_UMOM * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + i;
  396. ind_t = ID_RHOT * (nz + 2 * hs) * (nx + 2 * hs) + (k + hs) * (nx + 2 * hs) + i;
  397. state[ind_u] = (state[ind_r] + hy_dens_cell[k + hs]) * 50.;
  398. state[ind_t] = (state[ind_r] + hy_dens_cell[k + hs]) * 298. - hy_dens_theta_cell[k + hs];
  399. }
  400. }
  401. }
  402. }
  403. }
  404. //Set this task's halo values in the z-direction.
  405. //decomposition in the vertical direction.
  406. void set_halo_values_z(double *state)
  407. {
  408. int i, ll;
  409. const double mnt_width = xlen / 8;
  410. double x, xloc, mnt_deriv;
  411. /////////////////////////////////////////////////
  412. // TODO: THREAD ME
  413. /////////////////////////////////////////////////
  414. #pragma acc parallel loop private(x, xloc, mnt_deriv) default(present)
  415. for (ll = 0; ll < NUM_VARS; ll++)
  416. {
  417. for (i = 0; i < nx + 2 * hs; i++)
  418. {
  419. if (ll == ID_WMOM)
  420. {
  421. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (0) * (nx + 2 * hs) + i] = 0.;
  422. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (1) * (nx + 2 * hs) + i] = 0.;
  423. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (nz + hs) * (nx + 2 * hs) + i] = 0.;
  424. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (nz + hs + 1) * (nx + 2 * hs) + i] = 0.;
  425. }
  426. else
  427. {
  428. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (0) * (nx + 2 * hs) + i] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (hs) * (nx + 2 * hs) + i];
  429. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (1) * (nx + 2 * hs) + i] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (hs) * (nx + 2 * hs) + i];
  430. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (nz + hs) * (nx + 2 * hs) + i] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (nz + hs - 1) * (nx + 2 * hs) + i];
  431. state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (nz + hs + 1) * (nx + 2 * hs) + i] = state[ll * (nz + 2 * hs) * (nx + 2 * hs) + (nz + hs - 1) * (nx + 2 * hs) + i];
  432. }
  433. }
  434. }
  435. }
  436. void init()
  437. {
  438. int i, k, ii, kk, ll, inds, i_end;
  439. double x, z, r, u, w, t, hr, ht, nper;
  440. //Set the cell grid size
  441. dx = xlen / nx_glob;
  442. dz = zlen / nz_glob;
  443. nranks = 1;
  444. myrank = 0;
  445. // For simpler version, replace i_beg = 0, nx = nx_glob, left_rank = 0, right_rank = 0;
  446. nper = ((double)nx_glob) / nranks;
  447. i_beg = round(nper * (myrank));
  448. i_end = round(nper * ((myrank) + 1)) - 1;
  449. nx = i_end - i_beg + 1;
  450. left_rank = myrank - 1;
  451. if (left_rank == -1)
  452. left_rank = nranks - 1;
  453. right_rank = myrank + 1;
  454. if (right_rank == nranks)
  455. right_rank = 0;
  456. ////////////////////////////////////////////////////////////////////////////////
  457. ////////////////////////////////////////////////////////////////////////////////
  458. // YOU DON'T NEED TO ALTER ANYTHING BELOW THIS POINT IN THE CODE
  459. ////////////////////////////////////////////////////////////////////////////////
  460. ////////////////////////////////////////////////////////////////////////////////
  461. k_beg = 0;
  462. nz = nz_glob;
  463. //Allocate the model data
  464. state = (double *)malloc((nx + 2 * hs) * (nz + 2 * hs) * NUM_VARS * sizeof(double));
  465. state_tmp = (double *)malloc((nx + 2 * hs) * (nz + 2 * hs) * NUM_VARS * sizeof(double));
  466. flux = (double *)malloc((nx + 1) * (nz + 1) * NUM_VARS * sizeof(double));
  467. tend = (double *)malloc(nx * nz * NUM_VARS * sizeof(double));
  468. hy_dens_cell = (double *)malloc((nz + 2 * hs) * sizeof(double));
  469. hy_dens_theta_cell = (double *)malloc((nz + 2 * hs) * sizeof(double));
  470. hy_dens_int = (double *)malloc((nz + 1) * sizeof(double));
  471. hy_dens_theta_int = (double *)malloc((nz + 1) * sizeof(double));
  472. hy_pressure_int = (double *)malloc((nz + 1) * sizeof(double));
  473. //Define the maximum stable time step based on an assumed maximum wind speed
  474. dt = dmin(dx, dz) / max_speed * cfl;
  475. //Set initial elapsed model time and output_counter to zero
  476. etime = 0.;
  477. output_counter = 0.;
  478. // Display grid information
  479. printf("nx_glob, nz_glob: %d %d\n", nx_glob, nz_glob);
  480. printf("dx,dz: %lf %lf\n", dx, dz);
  481. printf("dt: %lf\n", dt);
  482. //////////////////////////////////////////////////////////////////////////
  483. // Initialize the cell-averaged fluid state via Gauss-Legendre quadrature
  484. //////////////////////////////////////////////////////////////////////////
  485. for (k = 0; k < nz + 2 * hs; k++)
  486. {
  487. for (i = 0; i < nx + 2 * hs; i++)
  488. {
  489. //Initialize the state to zero
  490. for (ll = 0; ll < NUM_VARS; ll++)
  491. {
  492. inds = ll * (nz + 2 * hs) * (nx + 2 * hs) + k * (nx + 2 * hs) + i;
  493. state[inds] = 0.;
  494. }
  495. //Use Gauss-Legendre quadrature to initialize a hydrostatic balance + temperature perturbation
  496. for (kk = 0; kk < nqpoints; kk++)
  497. {
  498. for (ii = 0; ii < nqpoints; ii++)
  499. {
  500. //Compute the x,z location within the global domain based on cell and quadrature index
  501. x = (i_beg + i - hs + 0.5) * dx + (qpoints[ii] - 0.5) * dx;
  502. z = (k_beg + k - hs + 0.5) * dz + (qpoints[kk] - 0.5) * dz;
  503. //Set the fluid state based on the user's specification (default is injection in this example)
  504. injection(x, z, r, u, w, t, hr, ht);
  505. //Store into the fluid state array
  506. inds = ID_DENS * (nz + 2 * hs) * (nx + 2 * hs) + k * (nx + 2 * hs) + i;
  507. state[inds] = state[inds] + r * qweights[ii] * qweights[kk];
  508. inds = ID_UMOM * (nz + 2 * hs) * (nx + 2 * hs) + k * (nx + 2 * hs) + i;
  509. state[inds] = state[inds] + (r + hr) * u * qweights[ii] * qweights[kk];
  510. inds = ID_WMOM * (nz + 2 * hs) * (nx + 2 * hs) + k * (nx + 2 * hs) + i;
  511. state[inds] = state[inds] + (r + hr) * w * qweights[ii] * qweights[kk];
  512. inds = ID_RHOT * (nz + 2 * hs) * (nx + 2 * hs) + k * (nx + 2 * hs) + i;
  513. state[inds] = state[inds] + ((r + hr) * (t + ht) - hr * ht) * qweights[ii] * qweights[kk];
  514. }
  515. }
  516. for (ll = 0; ll < NUM_VARS; ll++)
  517. {
  518. inds = ll * (nz + 2 * hs) * (nx + 2 * hs) + k * (nx + 2 * hs) + i;
  519. state_tmp[inds] = state[inds];
  520. }
  521. }
  522. }
  523. //Compute the hydrostatic background state over vertical cell averages
  524. for (k = 0; k < nz + 2 * hs; k++)
  525. {
  526. hy_dens_cell[k] = 0.;
  527. hy_dens_theta_cell[k] = 0.;
  528. for (kk = 0; kk < nqpoints; kk++)
  529. {
  530. z = (k_beg + k - hs + 0.5) * dz;
  531. //Set the fluid state based on the user's specification (default is injection in this example)
  532. injection(0., z, r, u, w, t, hr, ht);
  533. hy_dens_cell[k] = hy_dens_cell[k] + hr * qweights[kk];
  534. hy_dens_theta_cell[k] = hy_dens_theta_cell[k] + hr * ht * qweights[kk];
  535. }
  536. }
  537. //Compute the hydrostatic background state at vertical cell interfaces
  538. for (k = 0; k < nz + 1; k++)
  539. {
  540. z = (k_beg + k) * dz;
  541. //Set the fluid state based on the user's specification (default is injection in this example)
  542. injection(0., z, r, u, w, t, hr, ht);
  543. hy_dens_int[k] = hr;
  544. hy_dens_theta_int[k] = hr * ht;
  545. hy_pressure_int[k] = C0 * pow((hr * ht), gamm);
  546. }
  547. }
  548. //This test case is initially balanced but injects fast, cold air from the left boundary near the model top
  549. //x and z are input coordinates at which to sample
  550. //r,u,w,t are output density, u-wind, w-wind, and potential temperature at that location
  551. //hr and ht are output background hydrostatic density and potential temperature at that location
  552. void injection(double x, double z, double &r, double &u, double &w, double &t, double &hr, double &ht)
  553. {
  554. hydro_const_theta(z, hr, ht);
  555. r = 0.;
  556. t = 0.;
  557. u = 0.;
  558. w = 0.;
  559. }
  560. //Establish hydrstatic balance using constant potential temperature (thermally neutral atmosphere)
  561. //z is the input coordinate
  562. //r and t are the output background hydrostatic density and potential temperature
  563. void hydro_const_theta(double z, double &r, double &t)
  564. {
  565. const double theta0 = 300.; //Background potential temperature
  566. const double exner0 = 1.; //Surface-level Exner pressure
  567. double p, exner, rt;
  568. //Establish hydrostatic balance first using Exner pressure
  569. t = theta0; //Potential Temperature at z
  570. exner = exner0 - grav * z / (cp * theta0); //Exner pressure at z
  571. p = p0 * pow(exner, (cp / rd)); //Pressure at z
  572. rt = pow((p / C0), (1. / gamm)); //rho*theta at z
  573. r = rt / t; //Density at z
  574. }
  575. void finalize()
  576. {
  577. free(state);
  578. free(state_tmp);
  579. free(flux);
  580. free(tend);
  581. free(hy_dens_cell);
  582. free(hy_dens_theta_cell);
  583. free(hy_dens_int);
  584. free(hy_dens_theta_int);
  585. free(hy_pressure_int);
  586. }