MPAS-JEDI
mpas_state_interface_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 
6 ! ------------------------------------------------------------------------------
7 
9 
10 use fckit_configuration_module, only: fckit_configuration
11 use iso_c_binding
12 
13 !oops
14 use datetime_mod
15 use kinds, only: kind_real
16 use oops_variables_mod
17 
18 !ufo
19 use ufo_vars_mod, only: ufo_vars_getindex
20 
21 !MPAS-Model
22 use mpas_kind_types, only: strkind
23 use mpas_pool_routines, only: mpas_pool_get_config
24 
25 !mpas-jedi
27 use mpas_geom_mod
30 
31 implicit none
32 private
33 
34 ! ------------------------------------------------------------------------------
35 
36 contains
37 
38 ! ------------------------------------------------------------------------------
39 
40 subroutine mpas_state_create_c(c_key_self, c_key_geom, c_state_vars, c_inc_vars) &
41  bind(c,name='mpas_state_create_f90')
42 implicit none
43 integer(c_int), intent(inout) :: c_key_self
44 integer(c_int), intent(in) :: c_key_geom !< Geometry
45 type(c_ptr), value, intent(in) :: c_state_vars !< List of state variables
46 type(c_ptr), value, intent(in) :: c_inc_vars !< List of increment variables
47 
48 type(mpas_fields), pointer :: self
49 type(mpas_geom), pointer :: geom
50 type(oops_variables) :: state_vars
51 type(oops_variables) :: inc_vars
52 character(len=StrKIND), pointer :: config_microp_scheme, &
53  config_radt_cld_scheme
54 logical, pointer :: config_microp_re
55 integer :: ivar, jvar
56 
57 call mpas_fields_registry%init()
58 call mpas_fields_registry%add(c_key_self)
59 call mpas_fields_registry%get(c_key_self,self)
60 call mpas_geom_registry%get(c_key_geom, geom)
61 
62 state_vars = oops_variables(c_state_vars)
63 inc_vars = oops_variables(c_inc_vars)
64 
65 call mpas_pool_get_config(geom % domain % blocklist % configs, 'config_microp_re', config_microp_re)
66 call mpas_pool_get_config(geom % domain % blocklist % configs, 'config_microp_scheme', config_microp_scheme)
67 call mpas_pool_get_config(geom % domain % blocklist % configs, 'config_radt_cld_scheme', config_radt_cld_scheme)
68 
69 ! TODO(JJG): mpas_re_fields should be shifted to the state variables in the yaml file
70 if (config_microp_re) then
71  do ivar = 1, state_vars % nvars()
72  ! Only need re when hydrometeors are in state
73  if ( ufo_vars_getindex( mpas_hydrometeor_fields, &
74  state_vars%variable(ivar) ) > 0 ) then
75  do jvar = 1, size(mpas_re_fields, 1)
76  if (.not. state_vars%has(mpas_re_fields(jvar))) &
77  call state_vars%push_back(mpas_re_fields(jvar))
78  end do
79  exit
80  end if
81  end do
82 end if
83 
84 ! TODO(JJG): "nr" should be shifted to the state variables in the yaml file
85 if (trim(config_microp_scheme) == 'mp_thompson') then
86  do ivar = 1, state_vars % nvars()
87  ! Only need nr when qr is in state
88  if ( trim(state_vars%variable(ivar)) == 'qr' ) then
89  call state_vars%push_back("nr")
90  exit
91  end if
92  end do
93 end if
94 
95 call self%create(geom, state_vars, inc_vars)
96 
97 end subroutine mpas_state_create_c
98 
99 ! ------------------------------------------------------------------------------
100 
101 subroutine mpas_state_delete_c(c_key_self) &
102  bind(c,name='mpas_state_delete_f90')
103 implicit none
104 integer(c_int), intent(inout) :: c_key_self
105 type(mpas_fields), pointer :: self
106 
107 call mpas_fields_registry%get(c_key_self,self)
108 
109 call self%delete()
110 
111 call mpas_fields_registry%remove(c_key_self)
112 
113 end subroutine mpas_state_delete_c
114 
115 ! ------------------------------------------------------------------------------
116 
117 subroutine mpas_state_zero_c(c_key_self) &
118  bind(c,name='mpas_state_zero_f90')
119 implicit none
120 integer(c_int), intent(in) :: c_key_self
121 type(mpas_fields), pointer :: self
122 
123 call mpas_fields_registry%get(c_key_self,self)
124 call self%zeros()
125 
126 end subroutine mpas_state_zero_c
127 
128 ! ------------------------------------------------------------------------------
129 
130 subroutine mpas_state_copy_c(c_key_self,c_key_rhs) &
131  bind(c,name='mpas_state_copy_f90')
132 implicit none
133 integer(c_int), intent(in) :: c_key_self
134 integer(c_int), intent(in) :: c_key_rhs
135 
136 type(mpas_fields), pointer :: self
137 type(mpas_fields), pointer :: rhs
138 call mpas_fields_registry%get(c_key_self,self)
139 call mpas_fields_registry%get(c_key_rhs,rhs)
140 
141 call self%copy(rhs)
142 
143 end subroutine mpas_state_copy_c
144 
145 ! ------------------------------------------------------------------------------
146 
147 subroutine mpas_state_axpy_c(c_key_self,c_zz,c_key_rhs) &
148  bind(c,name='mpas_state_axpy_f90')
149 implicit none
150 integer(c_int), intent(in) :: c_key_self
151 real(c_double), intent(in) :: c_zz
152 integer(c_int), intent(in) :: c_key_rhs
153 
154 type(mpas_fields), pointer :: self
155 type(mpas_fields), pointer :: rhs
156 real(kind=kind_real) :: zz
157 
158 call mpas_fields_registry%get(c_key_self,self)
159 call mpas_fields_registry%get(c_key_rhs,rhs)
160 zz = c_zz
161 
162 call self%axpy(zz,rhs)
163 
164 end subroutine mpas_state_axpy_c
165 
166 ! ------------------------------------------------------------------------------
167 
168 subroutine mpas_state_add_incr_c(c_key_self,c_key_rhs) &
169  bind(c,name='mpas_state_add_incr_f90')
170 implicit none
171 integer(c_int), intent(in) :: c_key_self
172 integer(c_int), intent(in) :: c_key_rhs
173 type(mpas_fields), pointer :: self
174 type(mpas_fields), pointer :: rhs
175 
176 call mpas_fields_registry%get(c_key_self,self)
177 call mpas_fields_registry%get(c_key_rhs,rhs)
178 
179 call add_incr(self,rhs)
180 
181 end subroutine mpas_state_add_incr_c
182 
183 ! ------------------------------------------------------------------------------
184 
185 subroutine mpas_state_change_resol_c(c_key_state,c_key_rhs) &
186  bind(c,name='mpas_state_change_resol_f90')
187 implicit none
188 integer(c_int), intent(in) :: c_key_state
189 integer(c_int), intent(in) :: c_key_rhs
190 type(mpas_fields), pointer :: state, rhs
191 
192 call mpas_fields_registry%get(c_key_state,state)
193 call mpas_fields_registry%get(c_key_rhs,rhs)
194 
195 call state%change_resol(rhs)
196 
197 end subroutine mpas_state_change_resol_c
198 
199 ! ------------------------------------------------------------------------------
200 
201 subroutine mpas_state_read_file_c(c_key_state, c_conf, c_dt) &
202  bind(c,name='mpas_state_read_file_f90')
203 implicit none
204 integer(c_int), intent(in) :: c_key_state !< State
205 type(c_ptr), value, intent(in) :: c_conf !< Configuration
206 type(c_ptr), value, intent(in) :: c_dt !< DateTime
207 
208 type(mpas_fields), pointer :: self
209 type(datetime) :: fdate
210 type(fckit_configuration) :: f_conf
211 
212 call mpas_fields_registry%get(c_key_state,self)
213 call c_f_datetime(c_dt, fdate)
214 f_conf = fckit_configuration(c_conf)
215 call self%read_file(f_conf, fdate)
216 
217 end subroutine mpas_state_read_file_c
218 
219 ! ------------------------------------------------------------------------------
220 
221 subroutine mpas_state_analytic_init_c(c_key_state, c_key_geom, c_conf, c_dt) &
222  bind(c,name='mpas_state_analytic_init_f90')
223 implicit none
224 integer(c_int), intent(in) :: c_key_state !< State
225 integer(c_int), intent(in) :: c_key_geom !< Geometry
226 type(c_ptr), value, intent(in) :: c_conf !< Configuration
227 type(c_ptr), value, intent(in) :: c_dt !< DateTime
228 
229 type(mpas_fields), pointer :: state
230 type(mpas_geom), pointer :: geom
231 type(datetime) :: fdate
232 type(fckit_configuration) :: f_conf
233 
234 call mpas_fields_registry%get(c_key_state,state)
235 call mpas_geom_registry%get(c_key_geom,geom)
236 call c_f_datetime(c_dt, fdate)
237 f_conf = fckit_configuration(c_conf)
238 call analytic_ic(state, geom, f_conf, fdate)
239 
240 end subroutine mpas_state_analytic_init_c
241 
242 ! ------------------------------------------------------------------------------
243 
244 subroutine mpas_state_write_file_c(c_key_state, c_conf, c_dt) &
245  bind(c,name='mpas_state_write_file_f90')
246 implicit none
247 integer(c_int), intent(in) :: c_key_state !< State
248 type(c_ptr), value, intent(in) :: c_conf !< Configuration
249 type(c_ptr), value, intent(in) :: c_dt !< DateTime
250 
251 type(mpas_fields), pointer :: self
252 type(datetime) :: fdate
253 type(fckit_configuration) :: f_conf
254 
255 call mpas_fields_registry%get(c_key_state,self)
256 call c_f_datetime(c_dt, fdate)
257 f_conf = fckit_configuration(c_conf)
258 call self%write_file( f_conf, fdate)
259 
260 end subroutine mpas_state_write_file_c
261 
262 ! ------------------------------------------------------------------------------
263 
264 subroutine mpas_state_gpnorm_c(c_key_state, kf, pstat) &
265  bind(c,name='mpas_state_gpnorm_f90')
266 implicit none
267 integer(c_int), intent(in) :: c_key_state
268 integer(c_int), intent(in) :: kf
269 real(c_double), intent(inout) :: pstat(3*kf)
270 
271 type(mpas_fields), pointer :: self
272 real(kind=kind_real) :: zstat(3, kf)
273 integer :: jj, js, jf
274 
275 call mpas_fields_registry%get(c_key_state,self)
276 
277 call self%gpnorm(kf, zstat)
278 jj=0
279 do jf = 1, kf
280  do js = 1, 3
281  jj=jj+1
282  pstat(jj) = zstat(js,jf)
283  enddo
284 enddo
285 
286 end subroutine mpas_state_gpnorm_c
287 
288 ! ------------------------------------------------------------------------------
289 
290 subroutine mpas_state_rms_c(c_key_state, prms) &
291  bind(c,name='mpas_state_rms_f90')
292 implicit none
293 integer(c_int), intent(in) :: c_key_state
294 real(c_double), intent(inout) :: prms
295 
296 type(mpas_fields), pointer :: self
297 real(kind=kind_real) :: zz
298 
299 call mpas_fields_registry%get(c_key_state,self)
300 
301 call self%rms(zz)
302 
303 prms = zz
304 
305 end subroutine mpas_state_rms_c
306 
307 ! ------------------------------------------------------------------------------
308 
309 subroutine mpas_state_sizes_c(c_key_self,nc,nf) &
310  bind(c,name='mpas_state_sizes_f90')
311 implicit none
312 integer(c_int), intent(in) :: c_key_self
313 integer(c_int), intent(inout) :: nc,nf
314 type(mpas_fields), pointer :: self
315 
316 call mpas_fields_registry%get(c_key_self,self)
317 
318 nf = self%nf_ci
319 nc = self%geom%nCellsGlobal
320 
321 end subroutine mpas_state_sizes_c
322 
323 ! --------------------------------------------------------------------------------------------------
324 
325 subroutine mpas_state_serial_size_c(c_key_self,c_vsize) &
326  bind(c,name='mpas_state_serial_size_f90')
327 
328 implicit none
329 
330 ! Passed variables
331 integer(c_int),intent(in) :: c_key_self !< State
332 integer(c_size_t),intent(out) :: c_vsize !< Size
333 
334 type(mpas_fields),pointer :: self
335 
336 call mpas_fields_registry%get(c_key_self, self)
337 call self%serial_size(c_vsize)
338 
339 end subroutine mpas_state_serial_size_c
340 
341 ! --------------------------------------------------------------------------------------------------
342 
343 subroutine mpas_state_serialize_c(c_key_self,c_vsize,c_vect_inc) &
344  bind(c,name='mpas_state_serialize_f90')
345 
346 implicit none
347 
348 ! Passed variables
349 integer(c_int),intent(in) :: c_key_self !< State
350 integer(c_size_t),intent(in) :: c_vsize !< Size
351 real(c_double),intent(out) :: c_vect_inc(c_vsize) !< Vector
352 
353 type(mpas_fields),pointer :: self
354 
355 call mpas_fields_registry%get(c_key_self, self)
356 call self%serialize(c_vsize, c_vect_inc)
357 
358 end subroutine mpas_state_serialize_c
359 
360 ! --------------------------------------------------------------------------------------------------
361 
362 subroutine mpas_state_deserialize_c(c_key_self,c_vsize,c_vect_inc,c_index) &
363  bind(c,name='mpas_state_deserialize_f90')
364 
365 implicit none
366 
367 ! Passed variables
368 integer(c_int),intent(in) :: c_key_self !< State
369 integer(c_size_t),intent(in) :: c_vsize !< Size
370 real(c_double),intent(in) :: c_vect_inc(c_vsize) !< Vector
371 integer(c_size_t), intent(inout):: c_index !< Index
372 
373 type(mpas_fields),pointer :: self
374 
375 call mpas_fields_registry%get(c_key_self, self)
376 call self%deserialize(c_vsize, c_vect_inc, c_index)
377 
378 end subroutine mpas_state_deserialize_c
379 
380 ! ------------------------------------------------------------------------------
381 
382 end module mpas_state_interface_mod
383 
type(registry_t), public mpas_fields_registry
Linked list interface - defines registry_t type.
character(len=maxvarlen), dimension(3), public mpas_re_fields
character(len=maxvarlen), dimension(6), public mpas_hydrometeor_fields
type(registry_t), public mpas_geom_registry
Linked list interface - defines registry_t type.
subroutine mpas_state_serial_size_c(c_key_self, c_vsize)
subroutine mpas_state_delete_c(c_key_self)
subroutine mpas_state_analytic_init_c(c_key_state, c_key_geom, c_conf, c_dt)
subroutine mpas_state_change_resol_c(c_key_state, c_key_rhs)
subroutine mpas_state_create_c(c_key_self, c_key_geom, c_state_vars, c_inc_vars)
subroutine mpas_state_rms_c(c_key_state, prms)
subroutine mpas_state_serialize_c(c_key_self, c_vsize, c_vect_inc)
subroutine mpas_state_copy_c(c_key_self, c_key_rhs)
subroutine mpas_state_read_file_c(c_key_state, c_conf, c_dt)
subroutine mpas_state_add_incr_c(c_key_self, c_key_rhs)
subroutine mpas_state_gpnorm_c(c_key_state, kf, pstat)
subroutine mpas_state_deserialize_c(c_key_self, c_vsize, c_vect_inc, c_index)
subroutine mpas_state_sizes_c(c_key_self, nc, nf)
subroutine mpas_state_zero_c(c_key_self)
subroutine mpas_state_write_file_c(c_key_state, c_conf, c_dt)
subroutine mpas_state_axpy_c(c_key_self, c_zz, c_key_rhs)
subroutine, public add_incr(self, increment)
add increment to state
subroutine, public analytic_ic(self, geom, f_conf, vdate)
Analytic Initialization for the MPAS Model.
Fortran derived type to hold MPAS field.
Fortran derived type to hold geometry definition.