source: codes/icosagcm/devel/src/time/timeloop_gcm.f90 @ 949

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

devel : generic compute_caldyn_slow_hydro + fixes

File size: 15.9 KB
Line 
1MODULE timeloop_gcm_mod
2  USE profiling_mod
3  USE icosa
4  USE disvert_mod
5  USE trace
6  USE omp_para
7  USE compute_diagnostics_mod, ONLY : compute_rhodz
8  USE euler_scheme_mod
9  USE explicit_scheme_mod
10  USE hevi_scheme_mod
11  USE caldyn_vars_mod
12  IMPLICIT NONE
13  PRIVATE
14 
15  INTEGER, PARAMETER :: itau_sync=10
16  TYPE(t_message),SAVE :: req_ps0, req_mass0, req_theta_rhodz0, req_u0, req_q0, req_W0, req_geopot0
17  LOGICAL, SAVE :: positive_theta
18  INTEGER :: itau_prof, id_timeloop, id_dyn, id_phys, id_dissip, id_adv, id_diags
19  PUBLIC :: init_timeloop, timeloop
20
21CONTAINS
22 
23  SUBROUTINE init_timeloop
24    USE dissip_gcm_mod
25    USE observable_mod
26    USE caldyn_mod
27    USE etat0_mod
28    USE guided_mod
29    USE advect_tracer_mod
30    USE check_conserve_mod
31    USE output_field_mod
32    USE theta2theta_rhodz_mod
33    USE sponge_mod
34
35    CHARACTER(len=255) :: def
36
37    CALL register_id('timeloop',id_timeloop)
38    CALL register_id('dyn',id_dyn)
39    CALL register_id('dissip',id_dissip)
40    CALL register_id('phys',id_phys)
41    CALL register_id('adv',id_adv)
42    CALL register_id('diags',id_diags)
43
44    CALL init_caldyn
45   
46!    IF (xios_output) itau_out=1
47    IF (.NOT. enable_io) itau_out=HUGE(itau_out)
48
49    itau_prof=100
50    CALL getin('itau_profiling',itau_prof)
51
52    positive_theta=.FALSE.
53    CALL getin('positive_theta',positive_theta)
54    IF(positive_theta .AND. nqtot<1) THEN
55       PRINT *, 'nqtot must be >0 if positive_theta is .TRUE.'
56       STOP
57    END IF
58
59    def='ARK2.3'
60    CALL getin('time_scheme',def)
61    SELECT CASE (TRIM(def))
62    CASE('euler')
63       scheme_family=explicit
64       scheme=euler
65       nb_stage=1
66    CASE ('runge_kutta')
67       scheme_family=explicit
68       scheme=rk4
69       nb_stage=4
70    CASE ('RK2.5')
71       scheme_family=explicit
72       scheme=rk25
73       nb_stage=5
74    CASE ('leapfrog_matsuno')
75       scheme_family=explicit
76       scheme=mlf
77       matsuno_period=5
78       CALL getin('matsuno_period',matsuno_period)
79       nb_stage=matsuno_period+1
80    CASE('ARK2.3')
81       scheme_family=hevi
82       scheme=ark23
83       nb_stage=3
84       CALL set_coefs_ark23(dt)
85    CASE('ARK3.3')
86       scheme_family=hevi
87       scheme=ark33
88       nb_stage=3
89       CALL set_coefs_ark33(dt)
90    CASE ('none')
91       nb_stage=0
92    CASE default
93       PRINT*,'Bad selector for variable scheme : <', TRIM(def),             &
94            ' > options are <euler>, <runge_kutta>, <leapfrog_matsuno>,<RK2.5>,<ARK2.3>'
95       STOP
96    END SELECT
97   
98    ! Time-independant orography
99    CALL allocate_field(f_phis,field_t,type_real,name='phis')
100    ! Model state at current time step
101    CALL allocate_field(f_ps,field_t,type_real, name='ps')
102    CALL allocate_field(f_mass,field_t,type_real,llm,name='mass')
103    CALL allocate_field(f_rhodz,field_t,type_real,llm,name='rhodz')
104    CALL allocate_field(f_theta_rhodz,field_t,type_real,llm,nqdyn,name='theta_rhodz')
105    CALL allocate_field(f_u,field_u,type_real,llm,name='u')
106    CALL allocate_field(f_geopot,field_t,type_real,llm+1,name='geopot')
107    CALL allocate_field(f_W,field_t,type_real,llm+1,name='W')
108    CALL allocate_field(f_q,field_t,type_real,llm,nqtot,'q')
109    ! Mass fluxes
110    CALL allocate_field(f_hflux,field_u,type_real,llm)    ! instantaneous mass fluxes
111    CALL allocate_field(f_hfluxt,field_u,type_real,llm)   ! mass "fluxes" accumulated in time
112    CALL allocate_field(f_wflux,field_t,type_real,llm+1)  ! vertical mass fluxes
113    CALL allocate_field(f_wfluxt,field_t,type_real,llm+1,name='wfluxt')
114   
115    SELECT CASE(scheme_family)
116    CASE(explicit)
117       IF(caldyn_conserv == conserv_gassmann) THEN
118          PRINT *, 'caldyn_conserv = energy_gassmann is not implemented for RK time schemes, use time_scheme=ARK2.3 .'
119          STOP
120       END IF
121       ! Trends
122       CALL allocate_field(f_dps,field_t,type_real,name='dps')
123       CALL allocate_field(f_dmass,field_t,type_real,llm, name='dmass')
124       CALL allocate_field(f_dtheta_rhodz,field_t,type_real,llm,nqdyn,name='dtheta_rhodz')
125       CALL allocate_field(f_du,field_u,type_real,llm,name='du')
126       ! Model state at previous time step (RK/MLF)
127       CALL allocate_field(f_psm1,field_t,type_real,name='psm1')
128       CALL allocate_field(f_massm1,field_t,type_real,llm, name='massm1')
129       CALL allocate_field(f_theta_rhodzm1,field_t,type_real,llm,nqdyn,name='theta_rhodzm1')
130       CALL allocate_field(f_um1,field_u,type_real,llm,name='um1')
131    CASE(hevi)
132       ! Trends
133       CALL allocate_fields(nb_stage,f_dps_slow, field_t,type_real,name='dps_slow')
134       CALL allocate_fields(nb_stage,f_dmass_slow, field_t,type_real,llm, name='dmass_slow')
135       CALL allocate_fields(nb_stage,f_dtheta_rhodz_slow, field_t,type_real,llm,nqdyn,name='dtheta_rhodz_fast')
136       CALL allocate_fields(nb_stage,f_du_slow, field_u,type_real,llm,name='du_slow')
137       CALL allocate_fields(nb_stage,f_du_fast, field_u,type_real,llm,name='du_fast')
138       CALL allocate_fields(nb_stage,f_dW_slow, field_t,type_real,llm+1,name='dW_slow')
139       CALL allocate_fields(nb_stage,f_dW_fast, field_t,type_real,llm+1,name='dW_fast')
140       CALL allocate_fields(nb_stage,f_dPhi_slow, field_t,type_real,llm+1,name='dPhi_slow')
141       CALL allocate_fields(nb_stage,f_dPhi_fast, field_t,type_real,llm+1,name='dPhi_fast')
142       f_dps => f_dps_slow(:,1)
143       f_du => f_du_slow(:,1)
144       f_dtheta_rhodz => f_dtheta_rhodz_slow(:,1)
145    END SELECT
146
147    SELECT CASE(scheme)
148    CASE(mlf)
149       ! Model state 2 time steps ago (MLF)
150       CALL allocate_field(f_psm2,field_t,type_real)
151       CALL allocate_field(f_massm2,field_t,type_real,llm)
152       CALL allocate_field(f_theta_rhodzm2,field_t,type_real,llm,nqdyn)
153       CALL allocate_field(f_um2,field_u,type_real,llm)
154    END SELECT
155
156    CALL init_theta2theta_rhodz
157    CALL init_dissip
158    CALL init_sponge
159    CALL init_observable
160    CALL init_advect_tracer
161    CALL init_check_conserve
162
163    CALL etat0(f_ps,f_mass,f_phis,f_theta_rhodz,f_u, f_geopot,f_W, f_q)
164   
165    CALL init_guided(f_u,f_theta_rhodz)
166
167    CALL transfert_request(f_phis,req_i0) 
168    CALL transfert_request(f_phis,req_i1) 
169
170    CALL init_message(f_ps,req_i0,req_ps0)
171    CALL init_message(f_mass,req_i0,req_mass0)
172    CALL init_message(f_theta_rhodz,req_i0,req_theta_rhodz0)
173    CALL init_message(f_u,req_e0_vect,req_u0)
174    CALL init_message(f_q,req_i0,req_q0)
175    CALL init_message(f_geopot,req_i0,req_geopot0)
176    CALL init_message(f_W,req_i0,req_W0)
177
178  END SUBROUTINE init_timeloop
179 
180  SUBROUTINE timeloop
181    USE dissip_gcm_mod
182    USE sponge_mod
183    USE observable_mod
184    USE etat0_mod
185    USE guided_mod
186    USE caldyn_mod
187    USE advect_tracer_mod
188    USE diagflux_mod
189    USE physics_mod
190    USE mpipara
191    USE transfert_mod
192    USE check_conserve_mod
193    USE xios_mod
194    USE output_field_mod
195    USE write_etat0_mod
196    USE restart_mod
197    USE checksum_mod
198    USE explicit_scheme_mod
199    USE hevi_scheme_mod
200    REAL(rstd),POINTER :: rhodz(:,:), mass(:,:), ps(:)
201
202    REAL(rstd) :: adv_over_out ! ratio itau_adv/itau_out
203    INTEGER :: ind, it,i,j,l,n,  stage
204    LOGICAL :: fluxt_zero(ndomain) ! set to .TRUE. to start accumulating mass fluxes in time
205    LOGICAL, PARAMETER :: check_rhodz=.FALSE.
206    INTEGER :: start_clock, stop_clock, rate_clock
207    LOGICAL,SAVE :: first_physic=.TRUE.
208    !$OMP THREADPRIVATE(first_physic)   
209
210    CALL switch_omp_distrib_level
211    CALL caldyn_BC(f_phis, f_geopot, f_wflux) ! set constant values in first/last interfaces
212
213    !$OMP BARRIER
214    DO ind=1,ndomain
215       IF (.NOT. assigned_domain(ind)) CYCLE
216       CALL swap_dimensions(ind)
217       CALL swap_geometry(ind)
218       rhodz=f_rhodz(ind); mass=f_mass(ind); ps=f_ps(ind)
219       IF(caldyn_eta==eta_mass) THEN
220          CALL compute_rhodz(.TRUE., ps, rhodz) ! save rhodz for transport scheme before dynamics update ps
221       ELSE
222          DO l=ll_begin,ll_end
223             rhodz(:,l)=mass(:,l)
224          ENDDO
225       END IF
226    END DO
227    !$OMP BARRIER
228    fluxt_zero=.TRUE.
229
230    IF(positive_theta) CALL copy_theta_to_q(f_theta_rhodz,f_rhodz,f_q)
231    IF(diagflux_on) THEN
232       adv_over_out = itau_adv*(1./itau_out)
233    ELSE
234       adv_over_out = 0.
235    END IF
236
237    CALL check_conserve(f_ps,f_dps,f_u,f_theta_rhodz,f_phis,itau0) 
238
239    Call trace_on
240
241    IF (xios_output) THEN ! we must call update_calendar before any XIOS output
242      IF (is_omp_master) CALL xios_update_calendar(1)
243    END IF
244   
245!    IF (write_start) CALL write_etat0(itau0,f_ps, f_phis,f_theta_rhodz,f_u,f_q)
246    IF (write_start) CALL write_etat0(itau0,f_ps, f_phis,f_theta_rhodz,f_u,f_q, f_geopot, f_W)
247   
248    CALL write_output_fields_basic(.TRUE., f_phis, f_ps, f_mass, f_geopot, f_theta_rhodz, f_u, f_W, f_q)
249
250    !$OMP MASTER
251    CALL SYSTEM_CLOCK(start_clock, rate_clock)
252    !$OMP END MASTER   
253
254    DO it=itau0+1,itau0+itaumax
255       IF (is_master) CALL print_iteration(it, itau0, itaumax, start_clock, rate_clock)
256
257       CALL enter_profile(id_timeloop)
258
259       IF (xios_output) THEN
260
261          IF(it>itau0+1 .AND. is_omp_master) CALL xios_update_calendar(it-itau0)
262       ELSE
263          CALL update_time_counter(dt*it)
264       END IF
265
266       IF (it==itau0+1 .OR. MOD(it,itau_sync)==0) THEN
267          CALL send_message(f_ps,req_ps0)
268          CALL wait_message(req_ps0)
269          CALL send_message(f_mass,req_mass0)
270          CALL wait_message(req_mass0)
271          CALL send_message(f_theta_rhodz,req_theta_rhodz0) 
272          CALL wait_message(req_theta_rhodz0) 
273          CALL send_message(f_u,req_u0)
274          CALL wait_message(req_u0)
275          CALL send_message(f_q,req_q0) 
276          CALL wait_message(req_q0)
277          IF(.NOT. hydrostatic) THEN
278             CALL send_message(f_geopot,req_geopot0)
279             CALL wait_message(req_geopot0)
280             CALL send_message(f_W,req_W0)
281             CALL wait_message(req_W0)
282          END IF
283       ENDIF
284
285       CALL guided(it*dt,f_ps,f_theta_rhodz,f_u,f_q)
286
287       CALL enter_profile(id_dyn)
288       SELECT CASE(scheme_family)
289       CASE(explicit)
290          CALL explicit_scheme(it, fluxt_zero)
291       CASE(hevi)
292          CALL HEVI_scheme(it, fluxt_zero)
293       END SELECT
294       CALL exit_profile(id_dyn)
295
296       CALL enter_profile(id_dissip)
297       IF (MOD(it,itau_dissip)==0) THEN
298         
299          IF(caldyn_eta==eta_mass) THEN
300             !ym flush ps
301             !$OMP BARRIER
302             DO ind=1,ndomain
303                IF (.NOT. assigned_domain(ind)) CYCLE
304                CALL swap_dimensions(ind)
305                CALL swap_geometry(ind)
306                mass=f_mass(ind); ps=f_ps(ind);
307                CALL compute_rhodz(.TRUE., ps, mass)
308             END DO
309          ENDIF
310         
311          CALL enter_profile(id_diags)
312          CALL check_conserve_detailed(it, AAM_dyn, &
313               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
314          CALL exit_profile(id_diags)
315       
316          CALL dissip(f_ps,f_mass,f_phis,f_geopot,f_theta_rhodz,f_u, f_dtheta_rhodz,f_du)
317         
318          CALL euler_scheme(.FALSE.)  ! update only u, theta
319          IF (iflag_sponge > 0) THEN
320             CALL sponge(f_u,f_du,f_theta_rhodz,f_dtheta_rhodz)
321             CALL euler_scheme(.FALSE.)  ! update only u, theta
322          END IF
323
324          CALL enter_profile(id_diags)
325          CALL check_conserve_detailed(it, AAM_dissip, &
326               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
327          CALL exit_profile(id_diags)
328       END IF
329       CALL exit_profile(id_dissip)
330       
331       CALL enter_profile(id_adv)
332       IF(MOD(it,itau_adv)==0) THEN
333          CALL advect_tracer(f_hfluxt,f_wfluxt,f_u, f_q,f_rhodz, & ! update q and rhodz after RK step
334               adv_over_out, f_masst,f_qmasst,f_massfluxt, f_qfluxt)  ! accumulate mass and fluxes if diagflux_on
335          fluxt_zero=.TRUE. ! restart accumulation of hfluxt and qfluxt at next call to explicit_scheme / HEVI_scheme
336          ! At this point advect_tracer has obtained the halos of u and rhodz,
337          ! needed for correct computation of kinetic energy
338          IF(diagflux_on) CALL diagflux_energy(adv_over_out, f_phis,f_rhodz,f_theta_rhodz,f_u, f_geopot,f_theta,f_buf_i, f_hfluxt)
339
340          IF (check_rhodz) THEN ! check that rhodz is consistent with ps
341             DO ind=1,ndomain
342                IF (.NOT. assigned_domain(ind)) CYCLE
343                CALL swap_dimensions(ind)
344                CALL swap_geometry(ind)
345                rhodz=f_rhodz(ind); ps=f_ps(ind);
346                CALL compute_rhodz(.FALSE., ps, rhodz)   
347             END DO
348          ENDIF
349          IF(positive_theta) CALL copy_q_to_theta(f_theta_rhodz,f_rhodz,f_q)
350       END IF
351       CALL exit_profile(id_adv)
352       
353       CALL enter_profile(id_diags)
354       IF (MOD(it,itau_physics)==0) THEN
355          CALL check_conserve_detailed(it, AAM_dyn, &
356            f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
357          CALL enter_profile(id_phys)
358          CALL physics(it, f_phis, f_geopot, f_ps, f_theta_rhodz, f_u, f_wflux, f_q)
359          CALL exit_profile(id_phys)
360          CALL check_conserve_detailed(it, AAM_phys, &
361               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
362          !$OMP MASTER
363          IF (first_physic) CALL SYSTEM_CLOCK(start_clock)
364          !$OMP END MASTER   
365          first_physic=.FALSE.
366       END IF
367
368       IF (MOD(it,itau_check_conserv)==0) THEN
369          CALL check_conserve_detailed(it, AAM_dyn, &
370               f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
371          CALL check_conserve(f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it) 
372       ENDIF
373
374       IF (mod(it,itau_out)==0 ) THEN
375          CALL transfert_request(f_u,req_e1_vect)
376          CALL write_output_fields_basic(.FALSE.,f_phis, f_ps, f_mass, f_geopot, f_theta_rhodz, f_u, f_W, f_q)
377       ENDIF
378       CALL exit_profile(id_diags)
379
380       CALL exit_profile(id_timeloop)
381    END DO
382   
383!    CALL write_etat0(itau0+itaumax,f_ps, f_phis,f_theta_rhodz,f_u,f_q)
384    CALL write_etat0(itau0+itaumax,f_ps, f_phis,f_theta_rhodz,f_u,f_q, f_geopot, f_W) 
385   
386    CALL check_conserve_detailed(it, AAM_dyn, &
387         f_ps,f_dps,f_u,f_theta_rhodz,f_phis)
388    CALL check_conserve(f_ps,f_dps,f_u,f_theta_rhodz,f_phis,it) 
389   
390    !$OMP MASTER
391    CALL SYSTEM_CLOCK(stop_clock)
392   
393    IF (mpi_rank==0) THEN
394       PRINT *,"Time elapsed : ",(stop_clock-start_clock)*1./rate_clock 
395    ENDIF
396    !$OMP END MASTER
397   
398    ! CONTAINS
399  END SUBROUTINE timeloop
400
401  SUBROUTINE print_iteration(it,itau0,itaumax,start_clock,rate_clock)
402    INTEGER :: it, itau0, itaumax, start_clock, stop_clock, rate_clock, throughput
403    REAL :: per_step,total, elapsed
404    WRITE(*,'(A,I7,A,F14.1)') "It No :",it,"   t :",dt*it
405    IF(MOD(it,10)==0) THEN
406       CALL SYSTEM_CLOCK(stop_clock)
407       elapsed = (stop_clock-start_clock)*1./rate_clock
408       per_step = elapsed/(it-itau0)
409       throughput = dt/per_step
410       total = per_step*itaumax
411       WRITE(*,'(A,I5,A,F6.2,A,I6)') 'Time spent (s):',INT(elapsed), &
412            '  -- ms/step : ', 1000*per_step, &
413            '  -- Throughput :', throughput
414       WRITE(*,'(A,I5,A,I5)') 'Whole job (min) :', INT(total/60.), &
415            '  -- Completion in (min) : ', INT((total-elapsed)/60.)
416    END IF
417    IF(MOD(it,itau_prof)==0) CALL print_profile
418
419  END SUBROUTINE print_iteration
420
421  SUBROUTINE copy_theta_to_q(f_theta_rhodz,f_rhodz,f_q)
422    TYPE(t_field),POINTER :: f_theta_rhodz(:),f_rhodz(:), f_q(:)
423    REAL(rstd), POINTER :: theta_rhodz(:,:,:), rhodz(:,:), q(:,:,:)
424    INTEGER :: ind, iq
425    DO ind=1, ndomain
426       IF (.NOT. assigned_domain(ind)) CYCLE
427       CALL swap_dimensions(ind)
428       CALL swap_geometry(ind)
429       theta_rhodz=f_theta_rhodz(ind)
430       rhodz=f_rhodz(ind)
431       q=f_q(ind)
432       DO iq=1, nqdyn
433          q(:,:,iq)  = theta_rhodz(:,:,iq)/rhodz(:,:)
434       END DO
435    END DO
436  END SUBROUTINE copy_theta_to_q
437
438  SUBROUTINE copy_q_to_theta(f_theta_rhodz,f_rhodz,f_q)
439    TYPE(t_field),POINTER :: f_theta_rhodz(:),f_rhodz(:), f_q(:)
440    REAL(rstd), POINTER :: theta_rhodz(:,:,:), rhodz(:,:), q(:,:,:)
441    INTEGER :: ind, iq
442    DO ind=1, ndomain
443       IF (.NOT. assigned_domain(ind)) CYCLE
444       CALL swap_dimensions(ind)
445       CALL swap_geometry(ind)
446       theta_rhodz=f_theta_rhodz(ind)
447       rhodz=f_rhodz(ind)
448       q=f_q(ind)
449       DO iq=1,nqdyn
450          theta_rhodz(:,:,iq) = rhodz(:,:)*q(:,:,iq)
451       END DO
452    END DO
453  END SUBROUTINE copy_q_to_theta
454
455END MODULE timeloop_gcm_mod
Note: See TracBrowser for help on using the repository browser.