FV3 Bundle
fv3jedi_varcha_c2m_mod.f90
Go to the documentation of this file.
1 ! (C) Copyright 2018 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 
11 use iso_c_binding
12 use config_mod
14 
18 use wind_vt_mod
19 
20 implicit none
21 private
22 
23 public :: fv3jedi_varcha_c2m
24 public :: create
25 public :: delete
26 public :: multiply
27 public :: multiplyadjoint
28 public :: multiplyinverse
29 public :: multiplyinverseadjoint
30 
31 !> Fortran derived type to hold configuration data for the B mat variable change
33  integer :: degsubs = 100
34  real(8) :: tmintbl = 150.0_8, tmaxtbl = 333.0_8
35  integer :: tablesize
36  real(8), allocatable :: estblx(:)
37  real(kind=kind_real), allocatable :: ttraj(:,:,:)
38  real(kind=kind_real), allocatable :: tvtraj(:,:,:)
39  real(kind=kind_real), allocatable :: qtraj(:,:,:)
40  real(kind=kind_real), allocatable :: qsattraj(:,:,:)
41 end type fv3jedi_varcha_c2m
42 
43 ! ------------------------------------------------------------------------------
44 
45 contains
46 
47 ! ------------------------------------------------------------------------------
48 
49 subroutine create(self, bg, fg, geom, c_conf)
50 
51 implicit none
52 type(fv3jedi_varcha_c2m), intent(inout) :: self !< Change variable structure
53 type(fv3jedi_state), target, intent(in) :: bg
54 type(fv3jedi_state), target, intent(in) :: fg
55 type(fv3jedi_geom), target, intent(in) :: geom
56 type(c_ptr), intent(in) :: c_conf !< Configuration
57 
58 real(kind=kind_real), allocatable :: pe(:,:,:)
59 real(kind=kind_real), allocatable :: pm(:,:,:)
60 real(kind=kind_real), allocatable :: dqsatdt(:,:,:)
61 
62 !Create lookup table for computing saturation specific humidity
63 self%tablesize = nint(self%tmaxtbl-self%tmintbl)*self%degsubs + 1
64 allocate(self%estblx(self%tablesize))
65 call esinit(self%tablesize,self%degsubs,self%tmintbl,self%tmaxtbl,self%estblx)
66 
67 !> Virtual temperature trajectory
68 allocate(self%tvtraj (geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz))
69 call t_to_tv(geom,bg%t,bg%q,self%tvtraj)
70 
71 !> Temperature trajectory
72 allocate(self%ttraj (geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz))
73 self%ttraj = bg%t
74 
75 !> Specific humidity trajecotory
76 allocate(self%qtraj (geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz))
77 self%qtraj = bg%q
78 
79 !> Compute saturation specific humidity for q to RH transform
80 allocate(self%qsattraj(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz))
81 
82 allocate(pe(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz+1))
83 allocate(pm(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz ))
84 allocate(dqsatdt(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz ))
85 
86 call delp_to_pe_p_logp(geom,bg%delp,pe,pm)
87 call dqsat( geom,bg%t,pm,self%degsubs,self%tmintbl,self%tmaxtbl,&
88  self%tablesize,self%estblx,dqsatdt,self%qsattraj)
89 
90 deallocate(dqsatdt)
91 deallocate(pm)
92 deallocate(pe)
93 
94 end subroutine create
95 
96 ! ------------------------------------------------------------------------------
97 
98 subroutine delete(self)
99 
100 implicit none
101 type(fv3jedi_varcha_c2m), intent(inout) :: self
102 
103 if (allocated(self%estblx)) deallocate(self%estblx)
104 if (allocated(self%tvtraj)) deallocate(self%tvtraj)
105 if (allocated(self%ttraj)) deallocate(self%ttraj)
106 if (allocated(self%qtraj)) deallocate(self%qtraj)
107 if (allocated(self%qsattraj)) deallocate(self%qsattraj)
108 
109 end subroutine delete
110 
111 ! ------------------------------------------------------------------------------
112 
113 subroutine multiply(self,geom,xctl,xmod)
115 implicit none
116 type(fv3jedi_varcha_c2m), intent(inout) :: self
117 type(fv3jedi_geom), target, intent(inout) :: geom
118 type(fv3jedi_increment), intent(inout) :: xctl
119 type(fv3jedi_increment), intent(inout) :: xmod
120 
121 !Ps
122 xmod%ps = xctl%ps
123 
124 !Tracers
125 xmod%qi = xctl%qic
126 xmod%ql = xctl%qlc
127 xmod%o3 = xctl%o3c
128 
129 !Tangent linear of analysis (control) to model variables
130 call control_to_model_tlm(geom, xctl%psi, xctl%chi, xctl%tv, xctl%qc, &
131  xmod%ua , xmod%va , xmod%t , xmod%q , &
132  self%tvtraj,self%qtraj,self%qsattraj )
133 
134 end subroutine multiply
135 
136 ! ------------------------------------------------------------------------------
137 
138 subroutine multiplyadjoint(self,geom,xmod,xctl)
140 implicit none
141 type(fv3jedi_varcha_c2m), intent(inout) :: self
142 type(fv3jedi_geom), target, intent(inout) :: geom
143 type(fv3jedi_increment), intent(inout) :: xmod
144 type(fv3jedi_increment), intent(inout) :: xctl
145 
146 !Ps
147 xctl%ps = xmod%ps
148 
149 !Tracers
150 xctl%qic = xmod%qi
151 xctl%qlc = xmod%ql
152 xctl%o3c = xmod%o3
153 
154 !Adjoint of analysis (control) to model variables
155 call control_to_model_adm(geom, xctl%psi, xctl%chi, xctl%tv, xctl%qc, &
156  xmod%ua , xmod%va , xmod%t , xmod%q , &
157  self%tvtraj,self%qtraj,self%qsattraj )
158 
159 end subroutine multiplyadjoint
160 
161 ! ------------------------------------------------------------------------------
162 
163 subroutine multiplyinverse(self,geom,xmod,xctr)
165 implicit none
166 type(fv3jedi_varcha_c2m), intent(inout) :: self
167 type(fv3jedi_geom), target, intent(inout) :: geom
168 type(fv3jedi_increment), intent(inout) :: xmod
169 type(fv3jedi_increment), intent(inout) :: xctr
170 
171 real(kind=kind_real), allocatable, dimension(:,:,:) :: vort, divg, ua, va
172 
173 !Tangent linear inverse (model to control)
174 
175 xctr%psi = xmod%ua
176 xctr%chi = xmod%va
177 xctr%tv = xmod%t
178 xctr%ps = xmod%ps
179 xctr%qc = xmod%q
180 xctr%qic = xmod%qi
181 xctr%qlc = xmod%ql
182 xctr%o3c = xmod%o3
183 
184 !allocate (vort(geom%isc:geom%iec,geom%jsc:geom%jec,geom%npz))
185 !allocate (divg(geom%isc:geom%iec,geom%jsc:geom%jec,geom%npz))
186 !allocate ( ua(geom%isc:geom%iec,geom%jsc:geom%jec,geom%npz))
187 !allocate ( va(geom%isc:geom%iec,geom%jsc:geom%jec,geom%npz))
188 !
189 !!> Convert u,v to vorticity and divergence
190 !call uv_to_vortdivg(geom,xmod%ua,xmod%va,ua,va,vort,divg)
191 !
192 !!> Poisson solver for vorticity and divergence to psi and chi
193 !call vortdivg_to_psichi(geom,vort,divg,xctr%psi,xctr%chi)
194 !
195 !!> T to Tv
196 !call t_to_tv_tl(geom,self%ttraj,xmod%t,self%qtraj,xmod%q)
197 !xctr%tv = xmod%t
198 !
199 !!> q to RH
200 !call q_to_rh_tl(geom,self%qsattraj,xmod%q,xctr%qc)
201 !
202 !!Deallocate
203 !deallocate(vort,divg,ua,va)
204 
205 end subroutine multiplyinverse
206 
207 ! ------------------------------------------------------------------------------
208 
209 subroutine multiplyinverseadjoint(self,geom,xctr,xmod)
211 implicit none
212 type(fv3jedi_varcha_c2m), intent(inout) :: self
213 type(fv3jedi_geom), target, intent(inout) :: geom
214 type(fv3jedi_increment), intent(inout) :: xctr
215 type(fv3jedi_increment), intent(inout) :: xmod
216 
217 !xmod%ua = xctr%psi
218 !xmod%va = xctr%chi
219 !xmod%t = xctr%tv
220 !xmod%ps = xctr%ps
221 !xmod%q = xctr%qc
222 !xmod%qi = xctr%qic
223 !xmod%ql = xctr%qlc
224 !xmod%o3 = xctr%o3c
225 
226 end subroutine multiplyinverseadjoint
227 
228 ! ------------------------------------------------------------------------------
229 
230 subroutine control_to_model_tlm(geom,psi, chi, tv, qc, &
231  ua , va , t , qs, &
232  tvt, qt, qsat)
234  implicit none
235  type(fv3jedi_geom), intent(inout) :: geom
236 
237  !Input: control vector
238  real(kind=kind_real), intent(inout) :: psi(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Stream function
239  real(kind=kind_real), intent(inout) :: chi(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Velocity potential
240  real(kind=kind_real), intent(inout) :: tv(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Virtual temp
241  real(kind=kind_real), intent(inout) :: qc(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Specific humidity
242 
243  !Output: state/model vector
244  real(kind=kind_real), intent(inout) :: ua(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !A-grid winds (ua)
245  real(kind=kind_real), intent(inout) :: va(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !A-grid winds (va)
246  real(kind=kind_real), intent(inout) :: t(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Dry temperature
247  real(kind=kind_real), intent(inout) :: qs(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Specific humidity
248 
249  !Trajectory for virtual temperature to temperature
250  real(kind=kind_real), intent(in ) :: tvt(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !VTemperature traj
251  real(kind=kind_real), intent(in ) :: qt(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Specific humidity traj
252  real(kind=kind_real), intent(in ) :: qsat(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Sat spec hum
253 
254  real(kind=kind_real), allocatable, dimension(:,:,:) :: psi_dom, chi_dom
255 
256  ua = 0.0_kind_real
257  va = 0.0_kind_real
258  t = 0.0_kind_real
259  qs = 0.0_kind_real
260 
261  !psi and chi to A-grid u and v
262  !-----------------------------
263  allocate(psi_dom(geom%isd:geom%ied,geom%jsd:geom%jed,1:geom%npz))
264  allocate(chi_dom(geom%isd:geom%ied,geom%jsd:geom%jed,1:geom%npz))
265  psi_dom = 0.0_kind_real
266  chi_dom = 0.0_kind_real
267 
268  psi_dom(geom%isc:geom%iec,geom%jsc:geom%jec,:) = psi
269  chi_dom(geom%isc:geom%iec,geom%jsc:geom%jec,:) = chi
270 
271  call psichi_to_uava(geom,psi_dom,chi_dom,ua,va)
272 
273  deallocate(psi_dom, chi_dom)
274 
275  !Relative humidity to specific humidity
276  !--------------------------------------
277  call rh_to_q_tl(geom,qsat,qc,qs)
278 
279  !Virtual temperature to temperature
280  !----------------------------------
281  call tv_to_t_tl(geom,tvt,tv,qt,qs,t)
282 
283 endsubroutine control_to_model_tlm
284 
285 ! ------------------------------------------------------------------------------
286 
287 !> Control variables to state variables - Adjoint
288 
289 subroutine control_to_model_adm(geom,psi, chi, tv, qc, &
290  ua , va , t , qs, &
291  tvt, qt, qsat)
293  implicit none
294  type(fv3jedi_geom), intent(inout) :: geom
295 
296  !Input: control vector
297  real(kind=kind_real), intent(inout) :: psi(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Stream function
298  real(kind=kind_real), intent(inout) :: chi(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Velocity potential
299  real(kind=kind_real), intent(inout) :: tv(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Virtual temp
300  real(kind=kind_real), intent(inout) :: qc(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Specific humidity
301 
302  !Output: state/model vector
303  real(kind=kind_real), intent(inout) :: ua(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Dgrid winds (u)
304  real(kind=kind_real), intent(inout) :: va(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Dgrid winds (v)
305  real(kind=kind_real), intent(inout) :: t(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Dry temperature
306  real(kind=kind_real), intent(inout) :: qs(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Specific humidity
307 
308  !Trajectory for virtual temperature to temperaturc
309  real(kind=kind_real), intent(in ) :: tvt(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !VTemperature traj
310  real(kind=kind_real), intent(in ) :: qt(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Specific humidity traj
311  real(kind=kind_real), intent(in ) :: qsat(geom%isc:geom%iec,geom%jsc:geom%jec,1:geom%npz) !Sat spec hum
312 
313  real(kind=kind_real), allocatable, dimension(:,:,:) :: psi_dom, chi_dom
314 
315  psi = 0.0_kind_real
316  chi = 0.0_kind_real
317  tv = 0.0_kind_real
318  qc = 0.0_kind_real
319 
320  !Virtual temperature to temperature
321  !----------------------------------
322  call tv_to_t_ad(geom,tvt,tv,qt,qs,t)
323 
324  !Relative humidity to specific humidity
325  !--------------------------------------
326  call rh_to_q_ad(geom,qsat,qc,qs)
327 
328  !psi and chi to D-grid u and v
329  !-----------------------------
330  allocate(psi_dom(geom%isd:geom%ied,geom%jsd:geom%jed,1:geom%npz))
331  allocate(chi_dom(geom%isd:geom%ied,geom%jsd:geom%jed,1:geom%npz))
332  psi_dom = 0.0_kind_real
333  chi_dom = 0.0_kind_real
334 
335  call psichi_to_uava_adm(geom,psi_dom,chi_dom,ua,va)
336 
337  psi = psi_dom(geom%isc:geom%iec,geom%jsc:geom%jec,:)
338  chi = chi_dom(geom%isc:geom%iec,geom%jsc:geom%jec,:)
339 
340  deallocate(psi_dom, chi_dom)
341 
342 endsubroutine control_to_model_adm
343 
344 ! ------------------------------------------------------------------------------
345 
346 end module fv3jedi_varcha_c2m_mod
Fortran derived type to hold FV3JEDI increment.
subroutine, public tv_to_t_tl(geom, Tv, Tv_tl, q, q_tl, T_tl)
subroutine, public psichi_to_uava_adm(geom, psi_ad, chi_ad, ua_ad, va_ad)
subroutine, public delete(self)
subroutine, public delp_to_pe_p_logp(geom, delp, pe, p, logp)
Fortran derived type to hold FV3JEDI state.
Fortran derived type to hold geometry data for the FV3JEDI model.
Variable transforms on moisture variables for fv3-jedi Daniel Holdaway, NASA/JCSDA.
subroutine, public multiplyinverse(self, geom, xmod, xctr)
subroutine, public multiplyadjoint(self, geom, xmod, xctl)
subroutine, public multiply(self, geom, xctl, xmod)
Fortran derived type to hold configuration data for the B mat variable change.
Variable transforms on temperature variables for fv3-jedi Daniel Holdaway, NASA/JCSDA.
subroutine, public t_to_tv(geom, T, q, Tv)
subroutine, public rh_to_q_tl(geom, qsat, rh, q)
Handle state for the FV3JEDI odel.
subroutine, public tv_to_t_ad(geom, Tv, Tv_ad, q, q_ad, T_ad)
subroutine, public rh_to_q_ad(geom, qsat, rh, q)
subroutine, public psichi_to_uava(geom, psi, chi, ua, va)
subroutine, public dqsat(geom, temp, pmid, degsubs, tmintbl, tmaxtbl, tablesize, estblx, dqsi, qssi)
Handle increment for the FV3JEDI model.
subroutine control_to_model_tlm(geom, psi, chi, tv, qc, ua, va, t, qs, tvt, qt, qsat)
Variable transforms on wind variables for fv3-jedi Daniel Holdaway, NASA/JCSDA.
subroutine control_to_model_adm(geom, psi, chi, tv, qc, ua, va, t, qs, tvt, qt, qsat)
Control variables to state variables - Adjoint.
Fortran module handling geometry for the FV3 model.
Variable transforms on pressure variables for fv3-jedi Daniel Holdaway, NASA/JCSDA.
subroutine, public multiplyinverseadjoint(self, geom, xctr, xmod)
subroutine, public esinit(TABLESIZE, DEGSUBS, TMINTBL, TMAXTBL, ESTBLX)
subroutine, public create(self, bg, fg, geom, c_conf)