Skip to content

Commit 333100c

Browse files
authored
Merge pull request DJ4Earth#12 from swilliamson7/main
updating code
2 parents 06e3898 + 2efdc29 commit 333100c

15 files changed

+349
-598
lines changed

.DS_Store

0 Bytes
Binary file not shown.

explicit_solver/.DS_Store

0 Bytes
Binary file not shown.

explicit_solver/Project.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
[deps]
22
Checkpointing = "eb46d486-4f9c-4c3d-b445-a617f2a2f1ca"
33
Enzyme = "7da242da-08ed-463a-9acd-ee780be4f1d9"
4-
EnzymeCore = "f151be2c-9106-41f4-ab19-57ee4f262869"
54
JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
65
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
76
PProf = "e4faabce-9ead-11e9-39d9-4379958e3056"

explicit_solver/src/.DS_Store

0 Bytes
Binary file not shown.

explicit_solver/src/advance.jl

Lines changed: 138 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# I needed all of the terms that matter to the derivative to appear in a *single* structure, so keeping
66
# RHS_terms separate from the states was no longer a good idea.
77

8+
89
function advance(u_v_eta::gyre_vector,
910
grid::Grid,
1011
rhs::RHS_terms,
@@ -110,4 +111,140 @@ function advance(states_rhs::SWM_pde,
110111

111112
return nothing
112113

113-
end
114+
end
115+
116+
# This function needs to be given
117+
# T - how many days to integrate the model for
118+
# nx, ny - how many grid cells in x and y directions, respectively
119+
# Lx - E-W length of the domain [meters], has a default but can change if wanted
120+
# Ly - N-S length of the domain [meters], also has a default
121+
# mostly keeping because I like having a function that just integrates for some amount
122+
# of time
123+
function integrate(T, nx, ny; Lx = 3840e3, Ly = 3840e3)
124+
125+
grid_params = build_grid(Lx, Ly, nx, ny)
126+
gyre_params = def_params(grid_params)
127+
128+
# building discrete operators
129+
grad_ops = build_derivs(grid_params) # discrete gradient operators
130+
interp_ops = build_interp(grid_params, grad_ops) # discrete interpolation operators (travels between grids)
131+
advec_ops = build_advec(grid_params)
132+
rhs_terms = RHS_terms(Nu = grid_params.Nu, Nv = grid_params.Nv, NT = grid_params.NT, Nq = grid_params.Nq)
133+
134+
# starting from rest ---> all initial conditions are zero
135+
136+
Trun = days_to_seconds(T, gyre_params.dt)
137+
138+
uout = zeros(grid_params.Nu)
139+
vout = zeros(grid_params.Nv)
140+
etaout = zeros(grid_params.NT)
141+
142+
# u = [zeros(grid_params.Nu)]
143+
# v = [zeros(grid_params.Nv)]
144+
# eta = [zeros(grid_params.NT)]
145+
146+
u_v_eta = gyre_vector(uout, vout, etaout)
147+
148+
@time for t in 1:Trun
149+
advance(u_v_eta, grid_params, rhs_terms, gyre_params, interp_ops, grad_ops, advec_ops)
150+
end
151+
152+
# u_v_eta_mat = vec_to_mat(u_v_eta.u, u_v_eta.v, u_v_eta.eta, grid_params)
153+
154+
return u_v_eta
155+
156+
end
157+
158+
# ****IMPORTANT**** not yet sure if I'm moving between high and low res grids, need to check with Patrick
159+
# and come back here if there are issues with how I did it
160+
161+
# This function needs to be given
162+
# days - how many days to integrate the model for
163+
# nx_lowres, ny_lowres - grid resolution (number of cells in the x and y directions
164+
# respectively) of the courser grid
165+
# Lx, Ly - size of the domain, have a default value but can set
166+
# manually if needed
167+
# data_steps - which timesteps we want to store data at
168+
# Theoretically, we should only ever run this function *once*, from there
169+
# the data points will be stored as a JLD2 data file
170+
function create_data(days, nx_lowres, ny_lowres, data_steps; scaling = 4, Lx = 3840e3, Ly = 3840e3)
171+
172+
nx_highres = nx_lowres * scaling
173+
ny_highres = ny_lowres * scaling
174+
175+
grid_highres = build_grid(Lx, Ly, nx_highres, ny_highres)
176+
params = def_params(grid_highres)
177+
178+
# building discrete operators
179+
grad = build_derivs(grid_highres) # discrete gradient operators
180+
interp = build_interp(grid_highres, grad) # discrete interpolation operators (travels between grids)
181+
advec = build_advec(grid_highres)
182+
183+
Trun = days_to_seconds(days, params.dt)
184+
185+
Nu = grid_highres.Nu
186+
Nv = grid_highres.Nv
187+
NT = grid_highres.NT
188+
Nq = grid_highres.Nq
189+
190+
u_v_eta_rhs = SWM_pde(Nu = Nu,
191+
Nv = Nv,
192+
NT = NT,
193+
Nq = Nq
194+
)
195+
196+
# In order to compare high res data to low res velocities I'm going to (1) interpolate
197+
# the velocities to the T-grid (cell centers) (2) average the high
198+
# res data points down to the low res grid (3) interpolate the low
199+
# res results to the cell centers. Then I'll be comparing apples to apples (hopefully)
200+
# will check with Patrick that this is a valid method
201+
202+
# Building the averaging operator needed for step (2) above
203+
diag1 = (1 / scaling^2) .* ones(NT)
204+
M = spdiagm(NT, NT, 0.0 .* diag1)
205+
for k = 1:scaling
206+
for j = 1:scaling
207+
M += spdiagm(NT, NT, j+(k-1)*nx_highres - 1 => diag1[1:end-j-(k-1)*nx_highres + 1])
208+
end
209+
end
210+
M = M[1:scaling:end, :]
211+
index1 = 1:nx_lowres*ny_lowres*scaling
212+
for k in 1:ny_lowres
213+
index1 = filter(x -> x [j for j in ((k-1)*nx_lowres*scaling+nx_lowres+1):((k-1)*nx_lowres*scaling+nx_lowres*scaling)], index1)
214+
end
215+
M = M[index1, :]
216+
217+
# initializing where to store the data
218+
data = zeros(grid_highres.Nu + grid_highres.Nv + grid_highres.NT, length(data_steps))
219+
220+
# the steps where we want data in the high res model correspond to (roughly) scaling * t for t
221+
# in the low res model. for simplicity I'm going to keep the times in the low res where I want to have
222+
# data and then just scale them in the for loop to find corresponding high res data points
223+
224+
# for an initial effort I'm just going to run pretty course resolution models for both the high and low res
225+
226+
if 1 in scaling .* data_steps
227+
data[:, 1] .= [u_v_eta_rhs.u; u_v_eta_rhs.v; u_v_eta_rhs.eta]
228+
j = 2
229+
else
230+
j = 1
231+
end
232+
233+
for t in 2:Trun
234+
235+
advance(u_v_eta_rhs, grid_highres, params, interp, grad, advec)
236+
237+
if t in scaling .* data_steps
238+
data[:, j] .= [u_v_eta_rhs.u; u_v_eta_rhs.v; u_v_eta_rhs.eta]
239+
j += 1
240+
end
241+
242+
copyto!(u_v_eta_rhs.u, u_v_eta_rhs.u0)
243+
copyto!(u_v_eta_rhs.v, u_v_eta_rhs.v0)
244+
copyto!(u_v_eta_rhs.eta, u_v_eta_rhs.eta0)
245+
246+
end
247+
248+
return data, M
249+
250+
end

explicit_solver/src/build_grid.jl

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# This function builds all the parameters relating to the grid. It takes
22
# Lx - the length of the domain from E-W [meters]
33
# Ly - the length of the domain from N-S [meters]
4-
# Nx - the number of cells in the E-W direction
5-
# Ny - the number of cells in the N-S direction
4+
# nx - the number of cells in the E-W direction
5+
# ny - the number of cells in the N-S direction
66
# and returns a structure with a bunch of different parameters all built
77
# from the above inputs.
88
function build_grid(Lx, Ly, nx, ny)
@@ -46,16 +46,6 @@ function build_grid(Lx, Ly, nx, ny)
4646

4747
end
4848

49-
# This function allows me to specify the number of days that we want to run the model,
50-
# and convert that into the total steps to take
51-
function days_to_seconds(T, dt)
52-
53-
Trun_seconds = Int(ceil((T * 24 * 3600) / dt))
54-
55-
return Trun_seconds
56-
57-
end
58-
5949
# This function will just serve to take the vectors containing state information
6050
# and transform them to matrices
6151
function vec_to_mat(u, v, eta, grid)
@@ -69,3 +59,15 @@ function vec_to_mat(u, v, eta, grid)
6959
return state_matrices
7060

7161
end
62+
63+
function mat_to_vec(um, vm, etam, grid)
64+
65+
state_vectors = gyre_vector(
66+
reshape(um', grid.Nu),
67+
reshape(vm', grid.Nv),
68+
reshape(etam', grid.NT)
69+
)
70+
71+
return state_vectors
72+
73+
end

0 commit comments

Comments
 (0)