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+
89function 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
0 commit comments