FV3 Bundle
fv3jedi_tlm_mod.f90
Go to the documentation of this file.
1 ! (C) Copyright 2017 UCAR
2 !
3 ! This software is licensed under the terms of the Apache Licence Version 2.0
4 ! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
5 
7 
8 use iso_c_binding
9 use config_mod
10 use duration_mod
11 
16 use fv3jedi_traj_mod, only: fv3jedi_traj
17 
19 
20 implicit none
21 private
22 
23 public :: fv3jedi_tlm
24 public :: tlm_create
25 public :: tlm_delete
26 public :: tlm_initialize_tl
27 public :: tlm_initialize_ad
28 public :: tlm_step_tl
29 public :: tlm_step_ad
30 public :: tlm_finalize_tl
31 public :: tlm_finalize_ad
32 
33 ! ------------------------------------------------------------------------------
34 
35 !> Fortran derived type to hold tlm definition
36 type:: fv3jedi_tlm
37  type(fv3jedi_lm_type) :: fv3jedi_lm !<Linearized model object
38  logical :: fsoi_mode
39 end type fv3jedi_tlm
40 
41 ! ------------------------------------------------------------------------------
42 
43 contains
44 
45 ! ------------------------------------------------------------------------------
46 
47 subroutine tlm_create(self, geom, c_conf)
48 
49 implicit none
50 type(fv3jedi_tlm), intent(inout) :: self
51 type(fv3jedi_geom), intent(in) :: geom
52 type(c_ptr), intent(in) :: c_conf
53 
54 !Locals
55 character(len=20) :: ststep
56 type(duration) :: dtstep
57 real(kind=kind_real) :: dt
58 integer :: tmp
59 
60 ! Model time step
61 ! ---------------
62 ststep = config_get_string(c_conf,len(ststep),"tstep")
63 dtstep = trim(ststep)
64 dt = real(duration_seconds(dtstep),kind_real)
65 
66 
67 ! Model configuration and creation
68 ! --------------------------------
69 self%fv3jedi_lm%conf%do_dyn = config_get_int(c_conf,"lm_do_dyn")
70 self%fv3jedi_lm%conf%do_phy_trb = config_get_int(c_conf,"lm_do_trb")
71 self%fv3jedi_lm%conf%do_phy_mst = config_get_int(c_conf,"lm_do_mst")
72 
73 call self%fv3jedi_lm%create(dt,geom%npx,geom%npy,geom%npz,geom%ptop,geom%ak,geom%bk)
74 
75 self%fsoi_mode = .false.
76 if (config_element_exists(c_conf,"fsoi_mode")) then
77  tmp = config_get_int(c_conf,"fsoi_mode")
78  if (tmp==1) self%fsoi_mode = .true.
79 endif
80 
81 end subroutine tlm_create
82 
83 ! ------------------------------------------------------------------------------
84 
85 subroutine tlm_delete(self)
86 
87 implicit none
88 type(fv3jedi_tlm), intent(inout) :: self
89 
90 !Delete the model
91 !----------------
92 call self%fv3jedi_lm%delete()
93 
94 end subroutine tlm_delete
95 
96 ! ------------------------------------------------------------------------------
97 
98 subroutine tlm_initialize_ad(geom, self, incr)
99 
100 implicit none
101 type(fv3jedi_geom), intent(inout) :: geom
102 type(fv3jedi_tlm), intent(inout) :: self
103 type(fv3jedi_increment), intent(in) :: incr
104 
105 call self%fv3jedi_lm%init_ad()
106 
107 end subroutine tlm_initialize_ad
108 
109 ! ------------------------------------------------------------------------------
110 
111 subroutine tlm_initialize_tl(geom, self, incr)
113 implicit none
114 type(fv3jedi_geom), intent(inout) :: geom
115 type(fv3jedi_tlm), intent(inout) :: self
116 type(fv3jedi_increment), intent(in) :: incr
117 
118 if (self%fsoi_mode) call incr_to_lm_tl(geom,incr,self%fv3jedi_lm)
119 
120 call self%fv3jedi_lm%init_tl()
121 
122 end subroutine tlm_initialize_tl
123 
124 ! ------------------------------------------------------------------------------
125 
126 subroutine tlm_step_ad(geom, self, incr, traj)
128 implicit none
129 type(fv3jedi_geom), intent(inout) :: geom
130 type(fv3jedi_tlm), intent(inout) :: self
131 type(fv3jedi_increment), intent(inout) :: incr
132 type(fv3jedi_traj), intent(in) :: traj
133 
134 call traj_to_traj(traj,self%fv3jedi_lm)
135 
136 if (.not. self%fsoi_mode) call lm_to_incr_ad(geom,self%fv3jedi_lm,incr)
137 call self%fv3jedi_lm%step_ad()
138 call incr_to_lm_ad(geom,incr,self%fv3jedi_lm)
139 
140 end subroutine tlm_step_ad
141 
142 ! ------------------------------------------------------------------------------
143 
144 subroutine tlm_step_tl(geom, self, incr, traj)
146 implicit none
147 type(fv3jedi_geom), intent(inout) :: geom
148 type(fv3jedi_tlm), intent(inout) :: self
149 type(fv3jedi_increment), intent(inout) :: incr
150 type(fv3jedi_traj), intent(in) :: traj
151 
152 call traj_to_traj(traj,self%fv3jedi_lm)
153 
154 if (.not. self%fsoi_mode) call incr_to_lm_tl(geom,incr,self%fv3jedi_lm)
155 call self%fv3jedi_lm%step_tl()
156 call lm_to_incr_tl(geom,self%fv3jedi_lm,incr)
157 
158 end subroutine tlm_step_tl
159 
160 ! ------------------------------------------------------------------------------
161 
162 subroutine tlm_finalize_ad(geom, self, incr)
164 implicit none
165 type(fv3jedi_geom), intent(inout) :: geom
166 type(fv3jedi_tlm), intent(inout) :: self
167 type(fv3jedi_increment), intent(in) :: incr
168 
169 call self%fv3jedi_lm%final_ad()
170 if (.not. self%fsoi_mode) call lm_to_incr_ad(geom,self%fv3jedi_lm,incr)
171 
172 end subroutine tlm_finalize_ad
173 
174 ! ------------------------------------------------------------------------------
175 
176 subroutine tlm_finalize_tl(geom, self, incr)
178 implicit none
179 type(fv3jedi_geom), intent(inout) :: geom
180 type(fv3jedi_tlm), intent(inout) :: self
181 type(fv3jedi_increment), intent(in) :: incr
182 
183 call self%fv3jedi_lm%final_tl()
184 
185 end subroutine tlm_finalize_tl
186 
187 ! ------------------------------------------------------------------------------
188 
189 subroutine incr_to_lm_tl(geom, incr, lm)
191 use wind_vt_mod, only: a2d
192 
193 implicit none
194 type(fv3jedi_geom), intent(inout) :: geom
195 type(fv3jedi_increment), intent(in) :: incr
196 type(fv3jedi_lm_type), intent(inout) :: lm
197 
198 integer :: k
199 real(kind=kind_real), allocatable, dimension(:,:,:) :: ud,vd
200 
201 allocate(ud(incr%isc:incr%iec ,incr%jsc:incr%jec+1,1:incr%npz))
202 allocate(vd(incr%isc:incr%iec+1,incr%jsc:incr%jec ,1:incr%npz))
203 ud = 0.0_kind_real
204 vd = 0.0_kind_real
205 
206 call a2d(geom, incr%ua(incr%isc:incr%iec,incr%jsc:incr%jec,:), &
207  incr%va(incr%isc:incr%iec,incr%jsc:incr%jec,:), &
208  ud(incr%isc:incr%iec ,incr%jsc:incr%jec+1,:), &
209  vd(incr%isc:incr%iec+1,incr%jsc:incr%jec ,:))
210 
211 lm%pert%u = ud(incr%isc:incr%iec,incr%jsc:incr%jec,:)
212 lm%pert%v = vd(incr%isc:incr%iec,incr%jsc:incr%jec,:)
213 lm%pert%ua = incr%ua
214 lm%pert%va = incr%va
215 lm%pert%t = incr%t
216 do k = 1,geom%npz
217  lm%pert%delp(:,:,k) = (geom%bk(k+1)-geom%bk(k))*incr%ps(:,:)
218 enddo
219 lm%pert%qv = incr%q
220 lm%pert%qi = incr%qi
221 lm%pert%ql = incr%ql
222 lm%pert%o3 = incr%o3
223 
224 if (.not. incr%hydrostatic) then
225  lm%pert%delz = incr%delz
226  lm%pert%w = incr%w
227 endif
228 
229 deallocate(ud,vd)
230 
231 end subroutine incr_to_lm_tl
232 
233 ! ------------------------------------------------------------------------------
234 
235 subroutine lm_to_incr_tl(geom,lm,incr)
237 use wind_vt_mod, only: d2a
238 
239 implicit none
240 type(fv3jedi_geom), intent(inout) :: geom
241 type(fv3jedi_increment), intent(inout) :: incr
242 type(fv3jedi_lm_type), intent(in) :: lm
243 
244 real(kind=kind_real), allocatable, dimension(:,:,:) :: ua,va,ud,vd
245 
246 allocate(ud(incr%isd:incr%ied ,incr%jsd:incr%jed+1,1:incr%npz))
247 allocate(vd(incr%isd:incr%ied+1,incr%jsd:incr%jed ,1:incr%npz))
248 ud = 0.0_kind_real
249 vd = 0.0_kind_real
250 
251 allocate(ua(incr%isd:incr%ied ,incr%jsd:incr%jed ,1:incr%npz))
252 allocate(va(incr%isd:incr%ied ,incr%jsd:incr%jed ,1:incr%npz))
253 ua = 0.0_kind_real
254 va = 0.0_kind_real
255 
256 ud(incr%isc:incr%iec,incr%jsc:incr%jec,:) = lm%pert%u
257 vd(incr%isc:incr%iec,incr%jsc:incr%jec,:) = lm%pert%v
258 
259 call d2a(geom, ud, vd, ua, va)
260 
261 incr%ua = ua(incr%isc:incr%iec,incr%jsc:incr%jec,:)
262 incr%va = va(incr%isc:incr%iec,incr%jsc:incr%jec,:)
263 incr%t = lm%pert%t
264 incr%ps = sum(lm%pert%delp,3)
265 incr%q = lm%pert%qv
266 incr%qi = lm%pert%qi
267 incr%ql = lm%pert%ql
268 incr%o3 = lm%pert%o3
269 if (.not. incr%hydrostatic) then
270  incr%delz = lm%pert%delz
271  incr%w = lm%pert%w
272 endif
273 
274 deallocate(ud,vd,ua,va)
275 
276 end subroutine lm_to_incr_tl
277 
278 ! ------------------------------------------------------------------------------
279 
280 subroutine lm_to_incr_ad(geom,lm,incr)
282 use wind_vt_mod, only: d2a_ad
283 
284 implicit none
285 type(fv3jedi_geom), intent(inout) :: geom
286 type(fv3jedi_increment), intent(in) :: incr
287 type(fv3jedi_lm_type), intent(inout) :: lm
288 
289 integer :: k
290 real(kind=kind_real), allocatable, dimension(:,:,:) :: ua,va,ud,vd
291 
292 allocate(ud(incr%isd:incr%ied ,incr%jsd:incr%jed+1,1:incr%npz))
293 allocate(vd(incr%isd:incr%ied+1,incr%jsd:incr%jed ,1:incr%npz))
294 ud = 0.0_kind_real
295 vd = 0.0_kind_real
296 
297 allocate(ua(incr%isd:incr%ied ,incr%jsd:incr%jed ,1:incr%npz))
298 allocate(va(incr%isd:incr%ied ,incr%jsd:incr%jed ,1:incr%npz))
299 ua = 0.0_kind_real
300 va = 0.0_kind_real
301 
302 ua(incr%isc:incr%iec,incr%jsc:incr%jec,:) = incr%ua
303 va(incr%isc:incr%iec,incr%jsc:incr%jec,:) = incr%va
304 
305 lm%pert%t = incr%t
306 do k = 1,geom%npz
307  lm%pert%delp(:,:,k) = incr%ps(:,:)
308 enddo
309 lm%pert%qv = incr%q
310 lm%pert%qi = incr%qi
311 lm%pert%ql = incr%ql
312 lm%pert%o3 = incr%o3
313 if (.not. incr%hydrostatic) then
314  lm%pert%delz = incr%delz
315  lm%pert%w = incr%w
316 endif
317 
318 call d2a_ad(geom, ud, vd, ua, va)
319 
320 lm%pert%u = ud(incr%isc:incr%iec,incr%jsc:incr%jec,:)
321 lm%pert%v = vd(incr%isc:incr%iec,incr%jsc:incr%jec,:)
322 
323 lm%pert%ua(incr%isc:incr%iec,incr%jsc:incr%jec,:) = 0.0
324 lm%pert%va(incr%isc:incr%iec,incr%jsc:incr%jec,:) = 0.0
325 
326 deallocate(ud,vd,ua,va)
327 
328 end subroutine lm_to_incr_ad
329 
330 ! ------------------------------------------------------------------------------
331 
332 subroutine incr_to_lm_ad(geom,incr,lm)
334 use wind_vt_mod, only: a2d_ad
335 
336 implicit none
337 type(fv3jedi_geom), intent(inout) :: geom
338 type(fv3jedi_increment), intent(inout) :: incr
339 type(fv3jedi_lm_type), intent(in) :: lm
340 
341 integer :: k
342 real(kind=kind_real), allocatable, dimension(:,:,:) :: ud,vd
343 
344 allocate(ud(incr%isc:incr%iec ,incr%jsc:incr%jec+1,1:incr%npz))
345 allocate(vd(incr%isc:incr%iec+1,incr%jsc:incr%jec ,1:incr%npz))
346 ud = 0.0_kind_real
347 vd = 0.0_kind_real
348 
349 incr%ua = 0.0
350 incr%va = 0.0
351 incr%t = 0.0
352 incr%ps = 0.0
353 incr%q = 0.0
354 incr%qi = 0.0
355 incr%ql = 0.0
356 incr%o3 = 0.0
357 if (.not. incr%hydrostatic) then
358  incr%delz = 0.0
359  incr%w = 0.0
360 endif
361 
362 ud(incr%isc:incr%iec,incr%jsc:incr%jec,:) = lm%pert%u
363 vd(incr%isc:incr%iec,incr%jsc:incr%jec,:) = lm%pert%v
364 incr%t = lm%pert%t
365 incr%ps = 0.0_kind_real
366 do k = 1,geom%npz
367  incr%ps(:,:) = incr%ps(:,:) + (geom%bk(k+1)-geom%bk(k))*lm%pert%delp(:,:,k)
368 enddo
369 incr%q = lm%pert%qv
370 incr%qi = lm%pert%qi
371 incr%ql = lm%pert%ql
372 incr%o3 = lm%pert%o3
373 if (.not. incr%hydrostatic) then
374  incr%delz = lm%pert%delz
375  incr%w = lm%pert%w
376 endif
377 
378 !Convert A to D
379 call a2d_ad(geom, incr%ua, incr%va, ud, vd)
380 
381 deallocate(ud,vd)
382 
383 end subroutine incr_to_lm_ad
384 
385 ! ------------------------------------------------------------------------------
386 
387 subroutine traj_to_traj( traj, lm )
389 implicit none
390 type(fv3jedi_traj), intent(in) :: traj
391 type(fv3jedi_lm_type), intent(inout) :: lm
392 
393 lm%traj%u = traj%u
394 lm%traj%v = traj%v
395 lm%traj%ua = traj%ua
396 lm%traj%va = traj%va
397 lm%traj%t = traj%t
398 lm%traj%delp = traj%delp
399 lm%traj%qv = traj%qv
400 lm%traj%ql = traj%ql
401 lm%traj%qi = traj%qi
402 lm%traj%o3 = traj%o3
403 
404 if (.not. lm%conf%hydrostatic) then
405  lm%traj%w = traj%w
406  lm%traj%delz = traj%delz
407 endif
408 
409 if (lm%conf%do_phy_mst .ne. 0) then
410  lm%traj%qls = traj%qls
411  lm%traj%qcn = traj%qcn
412  lm%traj%cfcn = traj%cfcn
413 endif
414 
415 !> Rank two
416 lm%traj%phis = traj%phis
417 lm%traj%frocean = traj%frocean
418 lm%traj%frland = traj%frland
419 lm%traj%varflt = traj%varflt
420 lm%traj%ustar = traj%ustar
421 lm%traj%bstar = traj%bstar
422 lm%traj%zpbl = traj%zpbl
423 lm%traj%cm = traj%cm
424 lm%traj%ct = traj%ct
425 lm%traj%cq = traj%cq
426 lm%traj%kcbl = traj%kcbl
427 lm%traj%ts = traj%ts
428 lm%traj%khl = traj%khl
429 lm%traj%khu = traj%khu
430 
431 end subroutine traj_to_traj
432 
433 ! ------------------------------------------------------------------------------
434 
435 end module fv3jedi_tlm_mod
subroutine lm_to_incr_tl(geom, lm, incr)
Fortran derived type to hold FV3JEDI increment.
Top level for fv3jedi linearized model.
Fortran derived type to hold tlm definition.
Fortran derived type to hold FV3JEDI state.
subroutine traj_to_traj(traj, lm)
subroutine, public tlm_step_tl(geom, self, incr, traj)
subroutine, public tlm_finalize_ad(geom, self, incr)
subroutine, public tlm_delete(self)
subroutine, public tlm_initialize_ad(geom, self, incr)
subroutine, public d2a(geom, u, v, ua, va)
Fortran derived type to hold geometry data for the FV3JEDI model.
subroutine, public a2d_ad(geom, ua_ad, va_ad, ud_ad, vd_ad)
subroutine, public tlm_finalize_tl(geom, self, incr)
subroutine, public tlm_step_ad(geom, self, incr, traj)
subroutine incr_to_lm_tl(geom, incr, lm)
Handle state for the FV3JEDI odel.
subroutine, public tlm_create(self, geom, c_conf)
subroutine incr_to_lm_ad(geom, incr, lm)
subroutine, public tlm_initialize_tl(geom, self, incr)
subroutine, public a2d(geom, ua, va, ud, vd)
Handle increment for the FV3JEDI model.
Variable transforms on wind variables for fv3-jedi Daniel Holdaway, NASA/JCSDA.
Fortran module handling geometry for the FV3 model.
subroutine lm_to_incr_ad(geom, lm, incr)
subroutine, public d2a_ad(geom, u_ad, v_ad, ua_ad, va_ad)
integer, parameter, public kind_real