source: codes/icosagcm/devel/src/dynamics/caldyn_gcm.F90 @ 909

Last change on this file since 909 was 909, checked in by dubos, 5 years ago

devel : safeguards when using caldyn_conserv=energy_gassmann

File size: 14.3 KB
Line 
1MODULE caldyn_gcm_mod
2  USE icosa
3  USE transfert_mod
4  USE caldyn_vars_mod
5  USE caldyn_kernels_hevi_mod
6  USE caldyn_kernels_base_mod
7  USE caldyn_kernels_mod
8  USE omp_para
9  USE mpipara
10  IMPLICIT NONE
11  PRIVATE
12
13  PUBLIC init_caldyn, caldyn_BC, caldyn
14 
15CONTAINS
16 
17  SUBROUTINE init_caldyn
18
19    CHARACTER(len=255) :: def
20    INTEGER            :: ind
21    REAL(rstd),POINTER :: planetvel(:)
22
23    hydrostatic=.TRUE.
24    CALL getin("hydrostatic",hydrostatic)
25 
26    dysl=.NOT.hydrostatic ! dysl defaults to .FALSE. if hydrostatic, .TRUE. if NH                                             
27    CALL getin("dysl",dysl)
28    dysl_geopot=dysl
29    CALL getin("dysl_geopot",dysl_geopot)
30    dysl_caldyn_fast=dysl
31    CALL getin("dysl_caldyn_fast",dysl_caldyn_fast)
32    dysl_slow_hydro=dysl
33    CALL getin("dysl_slow_hydro",dysl_slow_hydro)
34    dysl_pvort_only=dysl
35    CALL getin("dysl_pvort_only",dysl_pvort_only)
36    dysl_caldyn_coriolis=dysl
37    CALL getin("dysl_caldyn_coriolis",dysl_caldyn_coriolis)
38    dysl_caldyn_vert=dysl
39    CALL getin("dysl_caldyn_vert",dysl_caldyn_vert)
40
41    def='advective'
42    CALL getin('caldyn_vert',def)
43    SELECT CASE(TRIM(def))
44    CASE('advective')
45       caldyn_vert_variant=caldyn_vert_noncons
46    CASE('conservative')
47       IF(.NOT. dysl_caldyn_vert) THEN
48          IF (is_mpi_root) PRINT *,'caldyn_vert=cons requires dysl_caldyn_vert=.TRUE.'
49          STOP
50       END IF
51       caldyn_vert_variant=caldyn_vert_cons
52    CASE DEFAULT
53       IF (is_mpi_root) PRINT *,'Bad selector for variable caldyn_vert : <', &
54            TRIM(def),'> options are <conservative>, <advective>'
55       STOP
56    END SELECT
57    IF (is_master) PRINT *, 'caldyn_vert=',def
58   
59    def='energy'
60    CALL getin('caldyn_conserv',def)
61    SELECT CASE(TRIM(def))
62    CASE('energy')
63       caldyn_conserv=conserv_energy
64    CASE('energy_gassmann')
65       caldyn_conserv=conserv_gassmann
66    CASE('enstrophy')
67       caldyn_conserv=conserv_enstrophy
68    CASE DEFAULT
69       IF (is_mpi_root) PRINT *,'Bad selector for variable caldyn_conserv : <', &
70            TRIM(def),'> options are <energy>, <energy_gassmann>, <enstrophy>'
71       STOP
72    END SELECT
73    IF (is_master) PRINT *, 'caldyn_conserv=',def
74
75    def='trisk'
76    CALL getin('caldyn_kinetic',def)
77    SELECT CASE(TRIM(def))
78    CASE('trisk')
79       caldyn_kinetic=kinetic_trisk
80    CASE('consistent')
81       caldyn_kinetic=kinetic_consistent
82    CASE DEFAULT
83       IF (is_mpi_root) PRINT *,'Bad selector for variable caldyn_kinetic : <', &
84            TRIM(def),'> options are <trisk>, <consistent>'
85       STOP
86    END SELECT
87    IF (is_master) PRINT *, 'caldyn_kinetic=',def
88
89    nqdyn=1 ! default value
90    physics_thermo = thermo_none
91
92    def='theta'
93    CALL getin('thermo',def)
94    SELECT CASE(TRIM(def))
95    CASE('boussinesq')
96       boussinesq=.TRUE.
97       caldyn_thermo=thermo_boussinesq
98       IF(.NOT. hydrostatic) THEN
99          PRINT *, 'thermo=boussinesq and hydrostatic=.FALSE. : Non-hydrostatic boussinesq equations are not supported'
100          STOP
101       END IF
102    CASE('theta')
103       caldyn_thermo=thermo_theta
104       physics_thermo=thermo_dry
105    CASE('entropy')
106       caldyn_thermo=thermo_entropy
107       physics_thermo=thermo_dry
108    CASE('theta_fake_moist')
109       caldyn_thermo=thermo_theta
110       physics_thermo=thermo_fake_moist
111    CASE('entropy_fake_moist')
112       caldyn_thermo=thermo_entropy
113       physics_thermo=thermo_fake_moist
114    CASE('moist')
115       caldyn_thermo=thermo_moist_debug
116       physics_thermo=thermo_moist
117       nqdyn = 2
118    CASE DEFAULT
119       IF (is_mpi_root) PRINT *,'Bad selector for variable caldyn_thermo : <', &
120            TRIM(def),'> options are <theta>, <entropy>'
121       STOP
122    END SELECT
123
124    IF(is_master) THEN
125       SELECT CASE(caldyn_thermo)
126       CASE(thermo_theta)
127          PRINT *, 'caldyn_thermo = thermo_theta'
128       CASE(thermo_entropy)
129          PRINT *, 'caldyn_thermo = thermo_entropy'
130       CASE(thermo_moist_debug)
131          PRINT *, 'caldyn_thermo = thermo_moist_debug'
132       CASE DEFAULT
133          STOP
134       END SELECT
135
136       SELECT CASE(physics_thermo)
137       CASE(thermo_dry)
138          PRINT *, 'physics_thermo = thermo_dry'
139       CASE(thermo_fake_moist)
140          PRINT *, 'physics_thermo = thermo_fake_moist'
141       CASE(thermo_moist)
142          PRINT *, 'physics_thermo = thermo_moist'
143       END SELECT
144
145       PRINT *, 'nqdyn =', nqdyn
146    END IF
147
148    CALL allocate_caldyn
149   
150    SELECT CASE(grid_type)
151    CASE(grid_ico)
152       ! NB : unused now, would be useful for non-traditional / deep-atmosphere
153       DO ind=1,ndomain
154          IF (.NOT. assigned_domain(ind)) CYCLE
155          CALL swap_dimensions(ind)
156          CALL swap_geometry(ind)
157          planetvel=f_planetvel(ind)
158          CALL compute_planetvel(planetvel)
159       END DO
160    CASE(grid_unst)
161    END SELECT
162
163  END SUBROUTINE init_caldyn
164
165  SUBROUTINE allocate_caldyn
166    CALL allocate_field(f_qu,field_u,type_real,llm, name='qu')
167    CALL allocate_field(f_qv,field_z,type_real,llm, name='qv')
168    CALL allocate_field(f_Kv,field_z,type_real,llm, name='Kv')
169    CALL allocate_field(f_hv,field_z,type_real,llm, name='hv')
170    CALL allocate_field(f_pk,    field_t,type_real,llm,  name='pk')
171    CALL allocate_field(f_wwuu,  field_u,type_real,llm+1,name='wwuu')
172    CALL allocate_field(f_planetvel,  field_u,type_real, name='planetvel') ! planetary velocity at r=a
173    IF(.NOT.hydrostatic) THEN
174       CALL allocate_field(f_Fel,      field_u,type_real,llm+1,name='F_el')
175       CALL allocate_field(f_gradPhi2, field_t,type_real,llm+1,name='gradPhi2')
176       CALL allocate_field(f_wil,      field_t,type_real,llm+1,name='w_il')
177       CALL allocate_field(f_Wetadot,  field_t,type_real,llm,name='W_etadot')
178    END IF
179  END SUBROUTINE allocate_caldyn
180
181  SUBROUTINE caldyn_BC(f_phis, f_geopot, f_wflux)
182    TYPE(t_field),POINTER :: f_phis(:)
183    TYPE(t_field),POINTER :: f_geopot(:)
184    TYPE(t_field),POINTER :: f_wflux(:)
185    REAL(rstd),POINTER  :: phis(:)
186    REAL(rstd),POINTER  :: wflux(:,:)
187    REAL(rstd),POINTER  :: geopot(:,:)
188    REAL(rstd),POINTER  :: wwuu(:,:)
189
190    INTEGER :: ind,i,j,ij,l
191
192    IF (is_omp_first_level) THEN
193       DO ind=1,ndomain
194          IF (.NOT. assigned_domain(ind)) CYCLE
195          CALL swap_dimensions(ind)
196          CALL swap_geometry(ind)
197          geopot=f_geopot(ind)
198          phis=f_phis(ind)
199          wflux=f_wflux(ind)
200          wwuu=f_wwuu(ind)
201         
202          SELECT CASE(grid_type)
203          CASE(grid_unst)
204             ! FIXME OMP
205             DO ij=1, primal_num
206                geopot(1,ij) = phis(ij)
207                wflux(1,ij)=0.
208                wflux(llm+1,ij)=0.
209             END DO
210             DO ij=1, edge_num
211                wwuu(1,ij)=0.
212                wwuu(llm+1,ij)=0.
213             END DO
214          CASE(grid_ico)
215             DO ij=ij_begin_ext,ij_end_ext
216                ! lower BCs : geopot=phis, wflux=0, wwuu=0
217                geopot(ij,1) = phis(ij)
218                wflux(ij,1) = 0.
219                wwuu(ij+u_right,1)=0   
220                wwuu(ij+u_lup,1)=0   
221                wwuu(ij+u_ldown,1)=0
222                ! top BCs : wflux=0, wwuu=0
223                wflux(ij,llm+1)  = 0.
224                wwuu(ij+u_right,llm+1)=0   
225                wwuu(ij+u_lup,llm+1)=0   
226                wwuu(ij+u_ldown,llm+1)=0
227             END DO
228          END SELECT
229       END DO
230    ENDIF
231
232    !$OMP BARRIER
233  END SUBROUTINE caldyn_BC
234   
235  SUBROUTINE caldyn(write_out,f_phis, f_ps, f_mass, f_theta_rhodz, f_u, f_q, &
236       f_geopot, f_hflux, f_wflux, f_dps, f_dmass, f_dtheta_rhodz, f_du)
237    USE observable_mod
238    USE disvert_mod, ONLY : caldyn_eta, eta_mass
239    USE trace
240    LOGICAL,INTENT(IN)    :: write_out
241    TYPE(t_field),POINTER :: f_phis(:)
242    TYPE(t_field),POINTER :: f_ps(:)
243    TYPE(t_field),POINTER :: f_mass(:)
244    TYPE(t_field),POINTER :: f_theta_rhodz(:)
245    TYPE(t_field),POINTER :: f_u(:)
246    TYPE(t_field),POINTER :: f_q(:)
247    TYPE(t_field),POINTER :: f_geopot(:)
248    TYPE(t_field),POINTER :: f_hflux(:), f_wflux(:)
249    TYPE(t_field) :: f_dps(:)
250    TYPE(t_field) :: f_dmass(:)
251    TYPE(t_field) :: f_dtheta_rhodz(:)
252    TYPE(t_field) :: f_du(:)
253   
254    REAL(rstd),POINTER :: ps(:), dps(:)
255    REAL(rstd),POINTER :: mass(:,:), theta_rhodz(:,:,:), dtheta_rhodz(:,:,:)
256    REAL(rstd),POINTER :: u(:,:), du(:,:), hflux(:,:), wflux(:,:)
257    REAL(rstd),POINTER :: qu(:,:)
258    REAL(rstd),POINTER :: qv(:,:)
259
260! temporary shared variable
261    REAL(rstd),POINTER  :: theta(:,:,:) 
262    REAL(rstd),POINTER  :: pk(:,:)
263    REAL(rstd),POINTER  :: geopot(:,:)
264    REAL(rstd),POINTER  :: convm(:,:) 
265    REAL(rstd),POINTER  :: wwuu(:,:)
266       
267    INTEGER :: ind
268    LOGICAL,SAVE :: first=.TRUE.
269!$OMP THREADPRIVATE(first)
270   
271    IF (first) THEN
272      first=.FALSE.
273      IF(caldyn_eta==eta_mass) THEN
274         CALL init_message(f_ps,req_i1,req_ps)
275      ELSE
276         CALL init_message(f_mass,req_i1,req_mass)
277      END IF
278      CALL init_message(f_theta_rhodz,req_i1,req_theta_rhodz)
279      CALL init_message(f_u,req_e1_vect,req_u)
280      CALL init_message(f_qu,req_e1_scal,req_qu)
281      ! Overlapping com/compute (deactivated) : MPI messages need to be sent at first call to caldyn
282      ! This is needed only once : the next ones will be sent by timeloop
283!      IF(caldyn_eta==eta_mass) THEN
284!         CALL send_message(f_ps,req_ps)
285!         CALL wait_message(req_ps) 
286!      ELSE
287!         CALL send_message(f_mass,req_mass)
288!         CALL wait_message(req_mass) 
289!      END IF
290    ENDIF
291   
292    CALL trace_start("caldyn")
293
294    IF(caldyn_eta==eta_mass) THEN
295       CALL send_message(f_ps,req_ps) ! COM00
296       CALL wait_message(req_ps) ! COM00
297    ELSE
298       CALL send_message(f_mass,req_mass) ! COM00
299       CALL wait_message(req_mass) ! COM00
300    END IF
301   
302    CALL send_message(f_theta_rhodz,req_theta_rhodz) ! COM01
303    CALL wait_message(req_theta_rhodz) ! COM01 Moved from caldyn_pvort
304    CALL send_message(f_u,req_u) ! COM02
305    CALL wait_message(req_u) ! COM02
306
307    IF(.NOT.hydrostatic) THEN
308       STOP 'caldyn_gcm may not be used yet when non-hydrostatic'
309    END IF
310
311    SELECT CASE(caldyn_conserv)
312    CASE(conserv_energy, conserv_gassmann) ! energy-conserving
313       DO ind=1,ndomain
314          IF (.NOT. assigned_domain(ind)) CYCLE
315          CALL swap_dimensions(ind)
316          CALL swap_geometry(ind)
317          ps=f_ps(ind)
318          u=f_u(ind)
319          theta_rhodz = f_theta_rhodz(ind)
320          mass=f_mass(ind)
321          theta = f_theta(ind)
322          qu=f_qu(ind)
323          qv=f_qv(ind)
324          pk = f_pk(ind)
325          geopot = f_geopot(ind) 
326          hflux=f_hflux(ind)
327          convm = f_dmass(ind)
328          dtheta_rhodz=f_dtheta_rhodz(ind)
329          du=f_du(ind)
330          CALL compute_pvort(ps,u,theta_rhodz(:,:,1), mass,theta,qu,qv) ! COM00 COM01 COM02
331!          CALL compute_theta(ps,theta_rhodz, mass,theta)
332!          CALL compute_pvort_only(u,mass,qu,qv)
333
334          CALL compute_geopot(mass,theta, ps,pk,geopot)
335!          du(:,:)=0.
336!          CALL compute_caldyn_fast(0.,u,mass,theta,pk,geopot,du)
337       ENDDO       
338
339       CALL send_message(f_u,req_u) ! COM02
340       CALL wait_message(req_u) ! COM02
341       CALL send_message(f_qu,req_qu) ! COM03
342       CALL wait_message(req_qu) ! COM03
343
344       DO ind=1,ndomain
345          IF (.NOT. assigned_domain(ind)) CYCLE
346          CALL swap_dimensions(ind)
347          CALL swap_geometry(ind)
348          ps=f_ps(ind)
349          u=f_u(ind)
350          theta_rhodz = f_theta_rhodz(ind)
351          mass=f_mass(ind)
352          theta = f_theta(ind)
353          qu=f_qu(ind)
354          qv=f_qv(ind)
355          pk = f_pk(ind)
356          geopot = f_geopot(ind) 
357          hflux=f_hflux(ind)
358          convm = f_dmass(ind)
359          dtheta_rhodz=f_dtheta_rhodz(ind)
360          du=f_du(ind)
361
362          CALL compute_caldyn_horiz(u,mass,qu,theta,pk,geopot, hflux,convm,dtheta_rhodz(:,:,1),du)
363!          CALL compute_caldyn_slow_hydro(u,mass,hflux,du, .FALSE.) ! FIXME
364!          CALL compute_caldyn_Coriolis(hflux,theta,qu, convm,dtheta_rhodz,du)
365          IF(caldyn_eta==eta_mass) THEN
366             wflux=f_wflux(ind)
367             wwuu=f_wwuu(ind)
368             dps=f_dps(ind)
369             CALL compute_caldyn_vert(u,theta,mass,convm, wflux,wwuu, dps, dtheta_rhodz(:,:,1), du)
370          END IF
371       ENDDO       
372   
373    CASE(conserv_enstrophy) ! enstrophy-conserving
374       DO ind=1,ndomain
375          IF (.NOT. assigned_domain(ind)) CYCLE
376          CALL swap_dimensions(ind)
377          CALL swap_geometry(ind)
378          ps=f_ps(ind)
379          u=f_u(ind)
380          theta_rhodz=f_theta_rhodz(ind)
381          mass=f_mass(ind)
382          theta = f_theta(ind)
383          qu=f_qu(ind)
384          qv=f_qv(ind)
385          CALL compute_pvort(ps,u,theta_rhodz(:,:,1), mass,theta,qu,qv)
386          pk = f_pk(ind)
387          geopot = f_geopot(ind) 
388          CALL compute_geopot(ps,mass,theta, pk,geopot)
389          hflux=f_hflux(ind)
390          convm = f_dmass(ind)
391          dtheta_rhodz=f_dtheta_rhodz(ind)
392          du=f_du(ind)
393          CALL compute_caldyn_horiz(u,mass,qu,theta,pk,geopot, hflux,convm,dtheta_rhodz(:,:,1),du)
394          IF(caldyn_eta==eta_mass) THEN
395             wflux=f_wflux(ind)
396             wwuu=f_wwuu(ind)
397             dps=f_dps(ind)
398             CALL compute_caldyn_vert(u,theta,mass,convm, wflux,wwuu, dps, dtheta_rhodz, du)
399          END IF
400       ENDDO
401       
402    CASE DEFAULT
403       PRINT *, 'Invalid value of caldyn_conserv : ', caldyn_conserv
404       STOP
405    END SELECT
406
407!$OMP BARRIER
408    !    CALL check_mass_conservation(f_ps,f_dps)
409    CALL trace_end("caldyn")
410!!$OMP BARRIER
411   
412END SUBROUTINE caldyn
413
414!-------------------------------- Diagnostics ----------------------------
415
416  SUBROUTINE check_mass_conservation(f_ps,f_dps)
417    TYPE(t_field),POINTER :: f_ps(:)
418    TYPE(t_field),POINTER :: f_dps(:)
419    REAL(rstd),POINTER :: ps(:)
420    REAL(rstd),POINTER :: dps(:)
421    REAL(rstd) :: mass_tot,dmass_tot
422    INTEGER :: ind,i,j,ij
423   
424    mass_tot=0
425    dmass_tot=0
426   
427    CALL transfert_request(f_dps,req_i1)
428    CALL transfert_request(f_ps,req_i1)
429
430    DO ind=1,ndomain
431      CALL swap_dimensions(ind)
432      CALL swap_geometry(ind)
433
434      ps=f_ps(ind)
435      dps=f_dps(ind)
436
437      DO j=jj_begin,jj_end
438        DO i=ii_begin,ii_end
439          ij=(j-1)*iim+i
440          IF (domain(ind)%own(i,j)) THEN
441            mass_tot=mass_tot+ps(ij)*Ai(ij)/g
442            dmass_tot=dmass_tot+dps(ij)*Ai(ij)/g
443          ENDIF
444        ENDDO
445      ENDDO
446   
447    ENDDO
448    IF (is_mpi_root) PRINT*, "mass_tot ", mass_tot,"      dmass_tot ",dmass_tot       
449
450  END SUBROUTINE check_mass_conservation 
451 
452END MODULE caldyn_gcm_mod
Note: See TracBrowser for help on using the repository browser.