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

Last change on this file since 731 was 731, checked in by dubos, 6 years ago

devel : cleanup and reorganization in dynamics/

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