source: trunk/LMDZ.TITAN/libf/phytitan/physiq_mod.F90 @ 1914

Last change on this file since 1914 was 1914, checked in by jvatant, 7 years ago

Ooops just realized that in r1908 physiq_mod.F90
hasn't been pushed ...
+ Also add "cosmetic" changes for arrays (1:nlayer)->(:)
--JVO

File size: 62.3 KB
Line 
1#define USE_QTEST
2
3      module physiq_mod
4     
5      implicit none
6     
7      contains
8     
9      subroutine physiq(ngrid,nlayer,nq,   &
10                  nametrac,                &
11                  firstcall,lastcall,      &
12                  pday,ptime,ptimestep,    &
13                  pplev,pplay,pphi,        &
14                  pu,pv,pt,pq,             &
15                  flxw,                    &
16                  pdu,pdv,pdt,pdq,pdpsrf)
17 
18      use radinc_h, only : L_NSPECTI,L_NSPECTV
19      use radcommon_h, only: sigma, glat, grav, BWNV
20      use surfdat_h, only: phisfi, zmea, zstd, zsig, zgam, zthe
21      use comchem_h, only: nkim
22      use comdiurn_h, only: coslat, sinlat, coslon, sinlon
23      use comsaison_h, only: mu0, fract, dist_star, declin, right_ascen
24      use comsoil_h, only: nsoilmx, layer, mlayer, inertiedat
25      use datafile_mod, only: datadir, corrkdir, banddir
26      use geometry_mod, only: latitude, longitude, cell_area
27      USE comgeomfi_h, only: totarea, totarea_planet
28      USE tracer_h
29      use time_phylmdz_mod, only: ecritphy, iphysiq, nday
30      use phyetat0_mod, only: phyetat0
31      use phyredem, only: physdem0, physdem1
32      use planetwide_mod, only: planetwide_minval,planetwide_maxval,planetwide_sumval
33      use mod_phys_lmdz_para, only : is_master
34      use planete_mod, only: apoastr, periastr, year_day, peri_day, &
35                            obliquit, nres, z0
36      use comcstfi_mod, only: pi, g, rcp, r, rad, mugaz, cpp
37      use time_phylmdz_mod, only: daysec
38      use logic_mod, only: moyzon_ch
39      use moyzon_mod, only: tmoy, playmoy, zphibar, zphisbar, zplevbar, &
40                            zplaybar, zzlevbar, zzlaybar, ztfibar, zqfibar
41      use callkeys_mod
42      use vertical_layers_mod, only: presnivs, pseudoalt
43      use ioipsl_getin_p_mod, only: getin_p
44      use mod_phys_lmdz_omp_data, ONLY: is_omp_master
45#ifdef CPP_XIOS     
46      use xios_output_mod, only: initialize_xios_output, &
47                                 update_xios_timestep, &
48                                 send_xios_field
49      use wxios, only: wxios_context_init, xios_context_finalize
50#endif
51      use MMP_OPTICS
52      implicit none
53
54
55!==================================================================
56!     
57!     Purpose
58!     -------
59!     Central subroutine for all the physics parameterisations in the
60!     universal model. Originally adapted from the Mars LMDZ model.
61!
62!     The model can be run without or with tracer transport
63!     depending on the value of "tracer" in file "callphys.def".
64!
65!
66!   It includes:
67!
68!      I. Initialization :
69!         I.1 Firstcall initializations.
70!         I.2 Initialization for every call to physiq.
71!
72!      II. Compute radiative transfer tendencies (longwave and shortwave) :
73!         II.a Option 1 : Call correlated-k radiative transfer scheme.
74!         II.b Option 2 : Call Newtonian cooling scheme.
75!         II.c Option 3 : Atmosphere has no radiative effect.
76!
77!      III. Vertical diffusion (turbulent mixing) :
78!
79!      IV. Dry Convective adjustment :
80!
81!      V. Tracers
82!         V.1. Chemistry
83!         V.2. Microphysics
84!         V.3. Updates (pressure variations, surface budget).
85!         V.4. Surface Tracer Update.
86!
87!      VI. Surface and sub-surface soil temperature.
88!
89!      VII. Perform diagnostics and write output files.
90!
91!
92!   arguments
93!   ---------
94!
95!   INPUT
96!   -----
97!
98!    ngrid                 Size of the horizontal grid.
99!    nlayer                Number of vertical layers.
100!    nq                    Number of advected fields.
101!    nametrac              Name of corresponding advected fields.
102!
103!    firstcall             True at the first call.
104!    lastcall              True at the last call.
105!
106!    pday                  Number of days counted from the North. Spring equinoxe.
107!    ptime                 Universal time (0<ptime<1): ptime=0.5 at 12:00 UT.
108!    ptimestep             timestep (s).
109!
110!    pplay(ngrid,nlayer)   Pressure at the middle of the layers (Pa).
111!    pplev(ngrid,nlayer+1) Intermediate pressure levels (Pa).
112!    pphi(ngrid,nlayer)    Geopotential at the middle of the layers (m2.s-2).
113!
114!    pu(ngrid,nlayer)      u, zonal component of the wind (ms-1).
115!    pv(ngrid,nlayer)      v, meridional component of the wind (ms-1).
116!
117!    pt(ngrid,nlayer)      Temperature (K).
118!
119!    pq(ngrid,nlayer,nq)   Advected fields.
120!
121!    pudyn(ngrid,nlayer)    \
122!    pvdyn(ngrid,nlayer)     \ Dynamical temporal derivative for the
123!    ptdyn(ngrid,nlayer)     / corresponding variables.
124!    pqdyn(ngrid,nlayer,nq) /
125!    flxw(ngrid,nlayer)      vertical mass flux (kg/s) at layer lower boundary
126!
127!   OUTPUT
128!   ------
129!
130!    pdu(ngrid,nlayer)        \
131!    pdv(ngrid,nlayer)         \  Temporal derivative of the corresponding
132!    pdt(ngrid,nlayer)         /  variables due to physical processes.
133!    pdq(ngrid,nlayer)        /
134!    pdpsrf(ngrid)             /
135!
136!
137!     Authors
138!     -------
139!           Frederic Hourdin        15/10/93
140!           Francois Forget        1994
141!           Christophe Hourdin        02/1997
142!           Subroutine completely rewritten by F. Forget (01/2000)
143!           Water ice clouds: Franck Montmessin (update 06/2003)
144!           Radiatively active tracers: J.-B. Madeleine (10/2008-06/2009)
145!           New correlated-k radiative scheme: R. Wordsworth (2009)
146!           Many specifically Martian subroutines removed: R. Wordsworth (2009)       
147!           Improved water cycle: R. Wordsworth / B. Charnay (2010)
148!           To F90: R. Wordsworth (2010)
149!           New turbulent diffusion scheme: J. Leconte (2012)
150!           Loops converted to F90 matrix format: J. Leconte (2012)
151!           No more ngridmx/nqmx, F90 commons and adaptation to parallel: A. Spiga (2012)
152!           Purge of the code : M. Turbet (2015)
153!           Fork for Titan : J. Vatant d'Ollone (2017)
154!                + clean of all too-generic (ocean, water, co2 ...) routines
155!                + Titan's chemistry
156!           Microphysical moment model - J.Burgalat / J.Vatant d'Ollone (2017-2018)
157!============================================================================================
158
159
160!    0.  Declarations :
161!    ------------------
162
163include "netcdf.inc"
164
165! Arguments :
166! -----------
167
168!   INPUTS:
169!   -------
170
171
172      integer,intent(in) :: ngrid             ! Number of atmospheric columns.
173      integer,intent(in) :: nlayer            ! Number of atmospheric layers.
174      integer,intent(in) :: nq                ! Number of tracers.
175      character*20,intent(in) :: nametrac(nq) ! Names of the tracers taken from dynamics.
176     
177      logical,intent(in) :: firstcall ! Signals first call to physics.
178      logical,intent(in) :: lastcall  ! Signals last call to physics.
179     
180      real,intent(in) :: pday                  ! Number of elapsed sols since reference Ls=0.
181      real,intent(in) :: ptime                 ! "Universal time", given as fraction of sol (e.g.: 0.5 for noon).
182      real,intent(in) :: ptimestep             ! Physics timestep (s).
183      real,intent(in) :: pplev(ngrid,nlayer+1) ! Inter-layer pressure (Pa).
184      real,intent(in) :: pplay(ngrid,nlayer)   ! Mid-layer pressure (Pa).
185      real,intent(in) :: pphi(ngrid,nlayer)    ! Geopotential at mid-layer (m2s-2).
186      real,intent(in) :: pu(ngrid,nlayer)      ! Zonal wind component (m/s).
187      real,intent(in) :: pv(ngrid,nlayer)      ! Meridional wind component (m/s).
188      real,intent(in) :: pt(ngrid,nlayer)      ! Temperature (K).
189      real,intent(in) :: pq(ngrid,nlayer,nq)   ! Tracers (kg/kg_of_air).
190      real,intent(in) :: flxw(ngrid,nlayer)    ! Vertical mass flux (ks/s) at lower boundary of layer
191
192!   OUTPUTS:
193!   --------
194
195!     Physical tendencies :
196
197      real,intent(out) :: pdu(ngrid,nlayer)    ! Zonal wind tendencies (m/s/s).
198      real,intent(out) :: pdv(ngrid,nlayer)    ! Meridional wind tendencies (m/s/s).
199      real,intent(out) :: pdt(ngrid,nlayer)    ! Temperature tendencies (K/s).
200      real,intent(out) :: pdq(ngrid,nlayer,nq) ! Tracer tendencies (kg/kg_of_air/s).
201      real,intent(out) :: pdpsrf(ngrid)        ! Surface pressure tendency (Pa/s).
202
203! Local saved variables:
204! ----------------------
205
206      integer,save :: day_ini                                      ! Initial date of the run (sol since Ls=0).
207      integer,save :: icount                                       ! Counter of calls to physiq during the run.
208!$OMP THREADPRIVATE(day_ini,icount)
209
210      real, dimension(:),allocatable,save ::  tsurf                ! Surface temperature (K).
211      real, dimension(:,:),allocatable,save ::  tsoil              ! Sub-surface temperatures (K).
212      real, dimension(:,:),allocatable,save :: albedo              ! Surface Spectral albedo. By MT2015.
213      real, dimension(:),allocatable,save :: albedo_equivalent     ! Spectral Mean albedo.
214     
215!$OMP THREADPRIVATE(tsurf,tsoil,albedo,albedo_equivalent)
216
217      real,dimension(:),allocatable,save :: albedo_bareground ! Bare Ground Albedo. By MT 2015.
218     
219!$OMP THREADPRIVATE(albedo_bareground)
220
221      real,dimension(:),allocatable,save :: emis        ! Thermal IR surface emissivity.
222      real,dimension(:,:),allocatable,save :: dtrad     ! Net atmospheric radiative heating rate (K.s-1).
223      real,dimension(:),allocatable,save :: fluxrad_sky ! Radiative flux from sky absorbed by surface (W.m-2).
224      real,dimension(:),allocatable,save :: fluxrad     ! Net radiative surface flux (W.m-2).
225      real,dimension(:),allocatable,save :: capcal      ! Surface heat capacity (J m-2 K-1).
226      real,dimension(:),allocatable,save :: fluxgrd     ! Surface conduction flux (W.m-2).
227      real,dimension(:,:),allocatable,save :: qsurf     ! Tracer on surface (e.g. kg.m-2).
228      real,dimension(:,:),allocatable,save :: q2        ! Turbulent Kinetic Energy.
229     
230!$OMP THREADPRIVATE(emis,dtrad,fluxrad_sky,fluxrad,capcal,fluxgrd,qsurf,q2)
231
232
233! Local variables :
234! -----------------
235
236      real zh(ngrid,nlayer)               ! Potential temperature (K).
237      real pw(ngrid,nlayer)               ! Vertical velocity (m/s). (NOTE : >0 WHEN DOWNWARDS !!)
238
239      integer l,ig,ierr,iq,nw,isoil
240     
241      ! FOR DIAGNOSTIC :
242     
243      real,dimension(:),allocatable,save :: fluxsurf_lw     ! Incident Long Wave (IR) surface flux (W.m-2).
244      real,dimension(:),allocatable,save :: fluxsurf_sw     ! Incident Short Wave (stellar) surface flux (W.m-2).
245      real,dimension(:),allocatable,save :: fluxsurfabs_sw  ! Absorbed Short Wave (stellar) flux by the surface (W.m-2).
246      real,dimension(:),allocatable,save :: fluxtop_lw      ! Outgoing LW (IR) flux to space (W.m-2).
247      real,dimension(:),allocatable,save :: fluxabs_sw      ! Absorbed SW (stellar) flux (W.m-2).
248      real,dimension(:),allocatable,save :: fluxtop_dn      ! Incoming SW (stellar) radiation at the top of the atmosphere (W.m-2).
249      real,dimension(:),allocatable,save :: fluxdyn         ! Horizontal heat transport by dynamics (W.m-2).
250      real,dimension(:,:),allocatable,save :: OLR_nu        ! Outgoing LW radiation in each band (Normalized to the band width (W/m2/cm-1)).
251      real,dimension(:,:),allocatable,save :: OSR_nu        ! Outgoing SW radiation in each band (Normalized to the band width (W/m2/cm-1)).
252      real,dimension(:,:),allocatable,save :: zdtlw         ! LW heating tendencies (K/s).
253      real,dimension(:,:),allocatable,save :: zdtsw         ! SW heating tendencies (K/s).
254      real,dimension(:),allocatable,save :: sensibFlux      ! Turbulent flux given by the atmosphere to the surface (W.m-2).
255     
256!$OMP THREADPRIVATE(fluxsurf_lw,fluxsurf_sw,fluxsurfabs_sw,fluxtop_lw,fluxabs_sw,fluxtop_dn,fluxdyn,OLR_nu,OSR_nu,&
257       
258        !$OMP zdtlw,zdtsw,sensibFlux)
259
260      real zls                       ! Solar longitude (radians).
261      real zlss                      ! Sub solar point longitude (radians).
262      real zday                      ! Date (time since Ls=0, calculated in sols).
263      real zzlay(ngrid,nlayer)       ! Altitude at the middle of the atmospheric layers.
264      real zzlev(ngrid,nlayer+1)     ! Altitude at the atmospheric layer boundaries.
265
266! TENDENCIES due to various processes :
267
268      ! For Surface Temperature : (K/s)
269      real zdtsurf(ngrid)     ! Cumulated tendencies.
270      real zdtsurfmr(ngrid)   ! Mass_redistribution routine.
271      real zdtsdif(ngrid)     ! Turbdiff/vdifc routines.
272           
273      ! For Atmospheric Temperatures : (K/s)   
274      real zdtdif(ngrid,nlayer)                               ! Turbdiff/vdifc routines.
275      real zdtmr(ngrid,nlayer)                                ! Mass_redistribution routine.
276      real zdtsw1(ngrid,nlayer), zdtlw1(ngrid,nlayer)         ! Callcorrk routine.
277                             
278      ! For Surface Tracers : (kg/m2/s)
279      real dqsurf(ngrid,nq)                 ! Cumulated tendencies.
280      real zdqsdif(ngrid,nq)                ! Turbdiff/vdifc routines.
281      real zdqsurfmr(ngrid,nq)              ! Mass_redistribution routine.
282                 
283      ! For Tracers : (kg/kg_of_air/s)
284      real zdqadj(ngrid,nlayer,nq)    ! Convadj routine.
285      real zdqdif(ngrid,nlayer,nq)    ! Turbdiff/vdifc routines.
286      real zdqevap(ngrid,nlayer)      ! Turbdiff routine.
287      real zdqmr(ngrid,nlayer,nq)     ! Mass_redistribution routine.
288     
289      real zdqchi(ngrid,nlayer,nq)    ! Chemical tendency ( chemistry routine ).
290      real zdqcond(ngrid,nlayer,nq)   ! Condensation tendency ( chemistry routine ).
291     
292      real zdqmufi(ngrid,nlayer,nq)   ! Microphysical tendency.
293                       
294      ! For Winds : (m/s/s)
295      real zdvadj(ngrid,nlayer),zduadj(ngrid,nlayer) ! Convadj routine.
296      real zdumr(ngrid,nlayer),zdvmr(ngrid,nlayer)   ! Mass_redistribution routine.
297      real zdvdif(ngrid,nlayer),zdudif(ngrid,nlayer) ! Turbdiff/vdifc routines.
298      real zdhdif(ngrid,nlayer)                      ! Turbdiff/vdifc routines.
299      real zdhadj(ngrid,nlayer)                      ! Convadj routine.
300     
301      ! For Pressure and Mass :
302      real zdmassmr(ngrid,nlayer) ! Atmospheric Mass tendency for mass_redistribution (kg_of_air/m2/s).
303      real zdmassmr_col(ngrid)    ! Atmospheric Column Mass tendency for mass_redistribution (kg_of_air/m2/s).
304      real zdpsrfmr(ngrid)        ! Pressure tendency for mass_redistribution routine (Pa/s).
305
306     
307   
308! Local variables for LOCAL CALCULATIONS:
309! ---------------------------------------
310      real zflubid(ngrid)
311      real zplanck(ngrid),zpopsk(ngrid,nlayer)
312      real ztim1,ztim2,ztim3, z1,z2
313      real ztime_fin
314      real zdh(ngrid,nlayer)
315      real gmplanet
316      real taux(ngrid),tauy(ngrid)
317
318
319
320! local variables for DIAGNOSTICS : (diagfi & stat)
321! -------------------------------------------------
322      real ps(ngrid)                                     ! Surface Pressure.
323      real zt(ngrid,nlayer)                              ! Atmospheric Temperature.
324      real zu(ngrid,nlayer),zv(ngrid,nlayer)             ! Zonal and Meridional Winds.
325      real zq(ngrid,nlayer,nq)                           ! Atmospheric Tracers.
326      real zdtadj(ngrid,nlayer)                          ! Convadj Diagnostic.
327      real zdtdyn(ngrid,nlayer)                          ! Dynamical Heating (K/s).
328      real zdudyn(ngrid,nlayer)                          ! Dynamical Zonal Wind tendency (m.s-2).
329      real,allocatable,dimension(:,:),save :: ztprevious ! Previous loop Atmospheric Temperature (K)                                                         ! Useful for Dynamical Heating calculation.
330      real,allocatable,dimension(:,:),save :: zuprevious ! Previous loop Zonal Wind (m.s-1)                                                                  ! Useful for Zonal Wind tendency calculation.
331!$OMP THREADPRIVATE(ztprevious,zuprevious)
332
333      real vmr(ngrid,nlayer)                        ! volume mixing ratio
334      real time_phys
335     
336      real ISR,ASR,OLR,GND,DYN,GSR,Ts1,Ts2,Ts3,TsS ! for Diagnostic.
337     
338!     to test energy conservation (RW+JL)
339      real mass(ngrid,nlayer),massarea(ngrid,nlayer)
340      real dEtot, dEtots, AtmToSurf_TurbFlux
341      real,save :: dEtotSW, dEtotsSW, dEtotLW, dEtotsLW
342!$OMP THREADPRIVATE(dEtotSW, dEtotsSW, dEtotLW, dEtotsLW)
343      real dEzRadsw(ngrid,nlayer),dEzRadlw(ngrid,nlayer),dEzdiff(ngrid,nlayer)
344      real dEdiffs(ngrid),dEdiff(ngrid)
345     
346!JL12 conservation test for mean flow kinetic energy has been disabled temporarily
347
348      real dItot, dItot_tmp, dVtot, dVtot_tmp
349     
350      real dWtot, dWtot_tmp, dWtots, dWtots_tmp
351     
352     
353      ! For Clear Sky Case.
354      real fluxsurf_lw1(ngrid), fluxsurf_sw1(ngrid), fluxsurfabs_sw1(ngrid)  ! For SW/LW flux.
355      real fluxtop_lw1(ngrid), fluxabs_sw1(ngrid)                            ! For SW/LW flux.
356      real albedo_equivalent1(ngrid)                                         ! For Equivalent albedo calculation.
357      real tf, ntf   
358
359      real,allocatable,dimension(:,:),save :: qsurf_hist
360!$OMP THREADPRIVATE(qsurf_hist)
361   
362      ! Miscellaneous :
363      character(len=10) :: tmp1
364      character(len=10) :: tmp2
365
366! Local variables for Titan chemistry and microphysics
367! ----------------------------------------------------
368 
369      real,save :: ctimestep ! Chemistry timestep (s)
370!$OMP THREADPRIVATE(ctimestep)
371 
372      ! Chemical tracers in molar fraction
373      real, dimension(ngrid,nlayer,nkim)          :: ychim ! (mol/mol)
374
375      ! Molar fraction tendencies ( chemistry and condensation ) for tracers (mol/mol/s)
376      real, dimension(ngrid,nlayer,nq)            :: dyccond ! All tracers, we want to use indx on it.
377      real, dimension(:,:,:), allocatable, save   :: dycchi  ! Only for chem tracers. Saved since chemistry is not called every step.
378!$OMP THREADPRIVATE(dycchi)
379
380      ! Saturation profiles
381      real, dimension(:,:), allocatable, save     :: qysat ! (mol/mol)
382!$OMP THREADPRIVATE(qysat)
383      real temp_eq(nlayer), press_eq(nlayer) ! Planetary averages for the init. of saturation profiles (K,mbar)
384
385      ! Surface methane tank
386      real,dimension(:),allocatable,save :: tankCH4 ! Depth of surface methane tank (m)
387!$OMP THREADPRIVATE(tankCH4)
388
389      ! -----******----- FOR MUPHYS OPTICS -----******-----
390      integer :: i,j
391      real :: pqo(ngrid,nlayer,nq)   ! Tracers for the optics (X/m2).
392      real :: i2e(nlayer)            ! int 2 ext factor
393      ! -----******----- END FOR MUPHYS OPTICS -----******-----
394
395      real,save,dimension(:,:,:), allocatable :: tpq ! Tracers for decoupled microphysical tests ( temporary in 01/18 )
396!$OMP THREADPRIVATE(tpq)
397
398
399!-----------------------------------------------------------------------------
400    ! Interface to calmufi
401    !   --> needed in order to pass assumed-shape arrays. Otherwise we must put calmufi in a module
402    !       (to have an explicit interface generated by the compiler).
403    !   Or one can put calmufi in MMP_GCM module (in muphytitan).
404    INTERFACE
405      SUBROUTINE calmufi(dt, plev, zlev, play, zlay, temp, pq, zdqfi, zdq)
406        REAL(kind=8), INTENT(IN)                 :: dt    !! Physics timestep (s).
407        REAL(kind=8), DIMENSION(:,:), INTENT(IN) :: plev  !! Pressure levels (Pa).
408        REAL(kind=8), DIMENSION(:,:), INTENT(IN) :: zlev  !! Altitude levels (m).
409        REAL(kind=8), DIMENSION(:,:), INTENT(IN) :: play  !! Pressure layers (Pa).
410        REAL(kind=8), DIMENSION(:,:), INTENT(IN) :: zlay  !! Altitude at the center of each layer (m).
411        REAL(kind=8), DIMENSION(:,:), INTENT(IN) :: temp  !! Temperature at the center of each layer (K). 
412        REAL(kind=8), DIMENSION(:,:,:), INTENT(IN)  :: pq    !! Tracers (\(kg.kg^{-1}}\)).
413        REAL(kind=8), DIMENSION(:,:,:), INTENT(IN)  :: zdqfi !! Tendency from former processes for tracers (\(kg.kg^{-1}}\)).
414        REAL(kind=8), DIMENSION(:,:,:), INTENT(OUT) :: zdq   !! Microphysical tendency for tracers (\(kg.kg^{-1}}\)).
415      END SUBROUTINE calmufi
416    END INTERFACE
417     
418!==================================================================================================
419
420! -----------------
421! I. INITIALISATION
422! -----------------
423
424! --------------------------------
425! I.1   First Call Initialisation.
426! --------------------------------
427      if (firstcall) then
428        allocate(tpq(ngrid,nlayer,nq))
429        tpq(:,:,:) = pq(:,:,:)
430
431        ! Initialisation of nmicro as well as tracers names, indexes ...
432        if (ngrid.ne.1) then ! Already done in rcm1d
433           call initracer2(nq,nametrac)
434        endif
435
436        ! Allocate saved arrays.
437        ALLOCATE(tsurf(ngrid))
438        ALLOCATE(tsoil(ngrid,nsoilmx))   
439        ALLOCATE(albedo(ngrid,L_NSPECTV))
440        ALLOCATE(albedo_equivalent(ngrid))       
441        ALLOCATE(albedo_bareground(ngrid))               
442        ALLOCATE(emis(ngrid))   
443        ALLOCATE(dtrad(ngrid,nlayer))
444        ALLOCATE(fluxrad_sky(ngrid))
445        ALLOCATE(fluxrad(ngrid))   
446        ALLOCATE(capcal(ngrid))   
447        ALLOCATE(fluxgrd(ngrid)) 
448        ALLOCATE(qsurf(ngrid,nq)) 
449        ALLOCATE(q2(ngrid,nlayer+1))
450        ALLOCATE(tankCH4(ngrid))
451        ALLOCATE(ztprevious(ngrid,nlayer))
452        ALLOCATE(zuprevious(ngrid,nlayer))
453        ALLOCATE(qsurf_hist(ngrid,nq))
454        ALLOCATE(fluxsurf_lw(ngrid))
455        ALLOCATE(fluxsurf_sw(ngrid))
456        ALLOCATE(fluxsurfabs_sw(ngrid))
457        ALLOCATE(fluxtop_lw(ngrid))
458        ALLOCATE(fluxabs_sw(ngrid))
459        ALLOCATE(fluxtop_dn(ngrid))
460        ALLOCATE(fluxdyn(ngrid))
461        ALLOCATE(OLR_nu(ngrid,L_NSPECTI))
462        ALLOCATE(OSR_nu(ngrid,L_NSPECTV))
463        ALLOCATE(sensibFlux(ngrid))
464        ALLOCATE(zdtlw(ngrid,nlayer))
465        ALLOCATE(zdtsw(ngrid,nlayer))
466 
467        ! This is defined in comsaison_h
468        ALLOCATE(mu0(ngrid))
469        ALLOCATE(fract(ngrid))           
470         ! This is defined in radcommon_h
471        ALLOCATE(glat(ngrid))
472       
473
474!        Variables set to 0
475!        ~~~~~~~~~~~~~~~~~~
476         dtrad(:,:) = 0.0
477         fluxrad(:) = 0.0
478         zdtsw(:,:) = 0.0
479         zdtlw(:,:) = 0.0
480
481!        Initialize setup for correlated-k radiative transfer
482!        JVO 17 : Was in callcorrk firstcall, but we need spectral intervals for microphysics
483!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
484
485         if (corrk) then
486         
487           call system('rm -f surf_vals_long.out')
488
489           write( tmp1, '(i3)' ) L_NSPECTI
490           write( tmp2, '(i3)' ) L_NSPECTV
491           banddir=trim(adjustl(tmp1))//'x'//trim(adjustl(tmp2))
492           banddir=trim(adjustl(corrkdir))//'/'//trim(adjustl(banddir))
493
494           call setspi            ! Basic infrared properties.
495           call setspv            ! Basic visible properties.
496           call sugas_corrk       ! Set up gaseous absorption properties.
497         
498           OLR_nu(:,:) = 0.
499           OSR_nu(:,:) = 0.
500           
501         endif
502
503!        Initialize names, timestep and saturation profiles for chemistry
504!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
505
506         if ( callchim ) then
507
508            if ( moyzon_ch .and. ngrid.eq.1 ) then
509               print *, "moyzon_ch=",moyzon_ch," and ngrid=1"
510               print *, "Please desactivate zonal mean for 1D !"
511               stop
512            endif
513
514            allocate(dycchi(ngrid,nlayer,nkim)) ! only for chemical tracers
515            allocate(qysat(nlayer,nkim))
516           
517            ! Chemistry timestep
518            ctimestep = ptimestep*REAL(ichim)
519
520            ! qysat is taken at the equator ( small variations of t,p )
521            if (ngrid.ne.1) then ! TODO : a patcher (lecture d'un profil?) si jamais on a plus acces a moyzon_mod !
522              temp_eq(:)  = tmoy(:)
523              press_eq(:) = playmoy(:)/100. ! in mbar
524            else
525              temp_eq(:)  = pt(1,:)
526              press_eq(:) = pplay(1,:)/100.
527            endif
528           
529            call inicondens(press_eq,temp_eq,qysat)
530         
531            zdqchi(:,:,:)  = 0.0
532            zdqcond(:,:,:) = 0.0
533
534         endif
535
536!        Initialize microphysics.
537!        ~~~~~~~~~~~~~~~~~~~~~~~~
538
539         IF ( callmufi ) THEN
540
541           call inimufi(nq,ptimestep)
542           
543           ! Optical coupling of YAMMS is plugged but inactivated for now
544           ! as long as the microphysics only isn't fully debugged -- JVO 01/18
545           IF (.NOT.uncoupl_optic_haze) call mmp_initialize_optics("/path/to/mmp_optic_table.nc")
546
547         ENDIF
548
549
550#ifdef CPP_XIOS
551        ! Initialize XIOS context
552        write(*,*) "physiq: call wxios_context_init"
553        CALL wxios_context_init
554#endif
555
556!        Read 'startfi.nc' file.
557!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
558         call phyetat0(startphy_file,ngrid,nlayer,"startfi.nc",0,0,nsoilmx,nq,      &
559                       day_ini,time_phys,tsurf,tsoil,emis,q2,qsurf,tankCH4)                       
560         if (.not.startphy_file) then
561           ! additionnal "academic" initialization of physics
562           if (is_master) write(*,*) "Physiq: initializing tsurf(:) to pt(:,1) !!"
563           tsurf(:)=pt(:,1)
564           if (is_master) write(*,*) "Physiq: initializing tsoil(:) to pt(:,1) !!"
565           do isoil=1,nsoilmx
566             tsoil(:,isoil)=tsurf(:)
567           enddo
568           if (is_master) write(*,*) "Physiq: initializing day_ini to pdat !"
569           day_ini=pday
570         endif
571
572         if (pday.ne.day_ini) then
573           write(*,*) "ERROR in physiq.F90:"
574           write(*,*) "bad synchronization between physics and dynamics"
575           write(*,*) "dynamics day: ",pday
576           write(*,*) "physics day:  ",day_ini
577           stop
578         endif
579         write (*,*) 'In physiq day_ini =', day_ini
580
581!        Initialize albedo calculation.
582!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
583         albedo(:,:)=0.0
584          albedo_bareground(:)=0.0
585         call surfini(ngrid,nq,qsurf,albedo,albedo_bareground)
586         
587!        Initialize orbital calculation.
588!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
589         call iniorbit(apoastr,periastr,year_day,peri_day,obliquit)
590
591         if(tlocked)then
592            print*,'Planet is tidally locked at resonance n=',nres
593            print*,'Make sure you have the right rotation rate!!!'
594         endif
595
596
597!        Initialize soil.
598!        ~~~~~~~~~~~~~~~~
599         if (callsoil) then
600         
601            call soil(ngrid,nsoilmx,firstcall,lastcall,inertiedat, &
602                      ptimestep,tsurf,tsoil,capcal,fluxgrd)
603
604         else ! else of 'callsoil'.
605         
606            print*,'WARNING! Thermal conduction in the soil turned off'
607            capcal(:)=1.e6
608            fluxgrd(:)=intheat
609            print*,'Flux from ground = ',intheat,' W m^-2'
610           
611         endif ! end of 'callsoil'.
612         
613         icount=1
614           
615
616!        Initialize surface history variable.
617!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
618         qsurf_hist(:,:)=qsurf(:,:)
619
620!        Initialize variable for dynamical heating and zonal wind tendency diagnostic
621!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
622         ztprevious(:,:)=pt(:,:)
623         zuprevious(:,:)=pu(:,:)
624
625
626         if(meanOLR)then         
627            call system('rm -f rad_bal.out') ! to record global radiative balance.           
628            call system('rm -f tem_bal.out') ! to record global mean/max/min temperatures.           
629            call system('rm -f h2o_bal.out') ! to record global hydrological balance.
630         endif
631
632         if (ngrid.ne.1) then
633            ! Note : no need to create a restart file in 1d.
634            call physdem0("restartfi.nc",longitude,latitude,nsoilmx,ngrid,nlayer,nq, &
635                          ptimestep,pday+nday,time_phys,cell_area,          &
636                          albedo_bareground,inertiedat,zmea,zstd,zsig,zgam,zthe)
637         endif
638         
639         ! XIOS outputs
640#ifdef CPP_XIOS
641
642         write(*,*) "physiq: call initialize_xios_output"
643         call initialize_xios_output(pday,ptime,ptimestep,daysec, &
644                                     presnivs,pseudoalt)
645#endif
646         write(*,*) "physiq: end of firstcall"
647      endif ! end of 'firstcall'
648
649! ------------------------------------------------------
650! I.2   Initializations done at every physical timestep:
651! ------------------------------------------------------
652
653#ifdef CPP_XIOS     
654      ! update XIOS time/calendar
655      call update_xios_timestep
656#endif     
657
658      ! Initialize various variables
659      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
660
661      pdt(:,:)    = 0.0     
662      zdtsurf(:)  = 0.0
663      pdq(:,:,:)  = 0.0
664      dqsurf(:,:) = 0.0
665      pdu(:,:)    = 0.0
666      pdv(:,:)    = 0.0
667      pdpsrf(:)   = 0.0
668      zflubid(:)  = 0.0     
669      taux(:)     = 0.0
670      tauy(:)     = 0.0
671
672      zday=pday+ptime ! Compute time, in sols (and fraction thereof).
673
674      ! Compute Stellar Longitude (Ls), and orbital parameters.
675      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
676      if (season) then
677         call stellarlong(zday,zls)
678      else
679         call stellarlong(float(day_ini),zls)
680      end if
681
682      call orbite(zls,dist_star,declin,right_ascen)
683     
684      if (tlocked) then
685              zlss=Mod(-(2.*pi*(zday/year_day)*nres - right_ascen),2.*pi)
686      elseif (diurnal) then
687              zlss=-2.*pi*(zday-.5)
688      else if(diurnal .eqv. .false.) then
689              zlss=9999.
690      endif
691
692
693      ! Compute variations of g with latitude (oblate case).
694      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
695      if (oblate .eqv. .false.) then     
696         glat(:) = g         
697      else if (flatten .eq. 0.0 .or. J2 .eq. 0.0 .or. Rmean .eq. 0.0 .or. MassPlanet .eq. 0.0) then     
698         print*,'I need values for flatten, J2, Rmean and MassPlanet to compute glat (else set oblate=.false.)'
699         call abort
700      else
701         gmplanet = MassPlanet*grav*1e24
702         do ig=1,ngrid
703            glat(ig)= gmplanet/(Rmean**2) * (1.D0 + 0.75 *J2 - 2.0*flatten/3. + (2.*flatten - 15./4.* J2) * cos(2. * (pi/2. - latitude(ig))))
704         end do
705      endif
706
707      ! Compute geopotential between layers.
708      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
709      zzlay(:,:)=pphi(:,:)
710      do l=1,nlayer         
711         zzlay(:,l)= zzlay(:,l)/glat(:)
712      enddo
713
714      zzlev(:,1)=0.
715      zzlev(:,nlayer+1)=1.e7 ! Dummy top of last layer above 10000 km...
716
717      do l=2,nlayer
718         do ig=1,ngrid
719            z1=(pplay(ig,l-1)+pplev(ig,l))/(pplay(ig,l-1)-pplev(ig,l))
720            z2=(pplev(ig,l)+pplay(ig,l))/(pplev(ig,l)-pplay(ig,l))
721            zzlev(ig,l)=(z1*zzlay(ig,l-1)+z2*zzlay(ig,l))/(z1+z2)
722         enddo
723      enddo     
724
725      ! Zonal averages needed for chemistry
726      ! ~~~~~~~ Taken from old Titan ~~~~~~
727      if (moyzon_ch) then
728         
729         zzlaybar(1,:)=(zphibar(1,:)+zphisbar(1))/g
730         ! SI ON TIENT COMPTE DE LA VARIATION DE G AVEC L'ALTITUDE:
731         !       zzlaybar(1,:)=RG*RA*RA/(RG*RA-(zphibar(1,:)+zphisbar(1)))-RA
732         zzlevbar(1,1)=zphisbar(1)/g
733         DO l=2,nlayer
734            z1=(zplaybar(1,l-1)+zplevbar(1,l))/(zplevbar(1,l-1)-zplevbar(1,l))
735            z2=(zplevbar(1,l)  +zplaybar(1,l))/(zplevbar(1,l)  -zplaybar(1,l))
736            zzlevbar(1,l)=(z1*zzlaybar(1,l-1)+z2*zzlaybar(1,l))/(z1+z2)
737         ENDDO
738         zzlevbar(1,nlayer+1)=zzlaybar(1,nlayer)+(zzlaybar(1,nlayer)-zzlevbar(1,nlayer))
739
740         DO ig=2,ngrid
741            if (latitude(ig).ne.latitude(ig-1)) then
742               DO l=1,nlayer
743                  zzlaybar(ig,l)=(zphibar(ig,l)+zphisbar(ig))/g
744                  ! SI ON TIENT COMPTE DE LA VARIATION DE G AVEC L'ALTITUDE:
745                  !zzlaybar(ig,l)=RG*RA*RA/(RG*RA-(zphibar(ig,l)+zphisbar(ig)))-RA
746               ENDDO
747               zzlevbar(ig,1)=zphisbar(ig)/g
748               DO l=2,nlayer
749                  z1=(zplaybar(ig,l-1)+zplevbar(ig,l))/ (zplevbar(ig,l-1)-zplevbar(ig,l))
750                  z2=(zplevbar(ig,l)  +zplaybar(ig,l))/(zplevbar(ig,l)  -zplaybar(ig,l))
751                  zzlevbar(ig,l)=(z1*zzlaybar(ig,l-1)+z2*zzlaybar(ig,l))/(z1+z2)
752               ENDDO
753               zzlevbar(ig,nlayer+1)=zzlaybar(ig,nlayer)+(zzlaybar(ig,nlayer)-zzlevbar(ig,nlayer))
754            else
755               zzlaybar(ig,:)=zzlaybar(ig-1,:)
756               zzlevbar(ig,:)=zzlevbar(ig-1,:)
757            endif
758         ENDDO
759
760      endif  ! moyzon
761
762      ! -------------------------------------------------------------------------------------
763      ! Compute potential temperature
764      ! Note : Potential temperature calculation may not be the same in physiq and dynamic...
765      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
766      do l=1,nlayer         
767         do ig=1,ngrid
768            zpopsk(ig,l)=(pplay(ig,l)/pplev(ig,1))**rcp
769            zh(ig,l)=pt(ig,l)/zpopsk(ig,l)
770            mass(ig,l)  = (pplev(ig,l) - pplev(ig,l+1))/glat(ig)
771            massarea(ig,l)=mass(ig,l)*cell_area(ig)
772         enddo
773      enddo
774
775     ! Compute vertical velocity (m/s) from vertical mass flux
776     ! w = F / (rho*area) and rho = P/(r*T)
777     ! But first linearly interpolate mass flux to mid-layers
778      do l=1,nlayer-1
779         pw(:,l)=0.5*(flxw(:,l)+flxw(:,l+1))
780      enddo
781      pw(:,nlayer)=0.5*flxw(:,nlayer) ! since flxw(nlayer+1)=0
782      do l=1,nlayer
783         pw(:,l)=(pw(:,l)*r*pt(:,l)) / (pplay(:,l)*cell_area(:))
784      enddo
785
786!---------------------------------
787! II. Compute radiative tendencies
788!---------------------------------
789
790      if (callrad) then
791         if( mod(icount-1,iradia).eq.0.or.lastcall) then
792
793          ! Compute local stellar zenith angles
794          ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
795            if (tlocked) then
796            ! JL14 corrects tidally resonant (and inclined) cases. nres=omega_rot/omega_orb
797               ztim1=SIN(declin)
798               ztim2=COS(declin)*COS(zlss)
799               ztim3=COS(declin)*SIN(zlss)
800
801               call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
802                            ztim1,ztim2,ztim3,mu0,fract, flatten)
803
804            elseif (diurnal) then
805               ztim1=SIN(declin)
806               ztim2=COS(declin)*COS(2.*pi*(zday-.5))
807               ztim3=-COS(declin)*SIN(2.*pi*(zday-.5))
808
809               call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
810                            ztim1,ztim2,ztim3,mu0,fract, flatten)
811            else if(diurnal .eqv. .false.) then
812
813               call mucorr(ngrid,declin,latitude,mu0,fract,10000.,rad,flatten)
814               ! WARNING: this function appears not to work in 1D
815
816            endif
817           
818            ! Eclipse incoming sunlight (e.g. Saturn ring shadowing).       
819            if(rings_shadow) then
820                call call_rings(ngrid, ptime, pday, diurnal)
821            endif   
822
823
824            if (corrk) then
825           
826! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
827! II.a Call correlated-k radiative transfer scheme
828! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
829
830               call call_profilgases(nlayer)
831
832               ! Convert (microphysical) tracers for optics: X.kg-1 --> X.m-2_
833               ! NOTE: it should be moved somewhere else: calmufi performs the same kind of
834               ! computations... waste of time...
835               DO i = 1, ngrid
836                 i2e(1:nlayer) = ( pplev(i,1:nlayer)-pplev(i,2:nlayer+1) ) / g
837                 pqo(i,:,:) = 0.0
838                 DO j=1,nmicro-nice
839                   pqo(i,:,j) = pq(i,:,j)*i2e(:)
840                 ENDDO
841               ENDDO
842
843
844               ! standard callcorrk
845               call callcorrk(ngrid,nlayer,pqo,nq,qsurf,                          &
846                              albedo,albedo_equivalent,emis,mu0,pplev,pplay,pt,   &
847                              tsurf,fract,dist_star,                              &
848                              zdtlw,zdtsw,fluxsurf_lw,fluxsurf_sw,                &
849                              fluxsurfabs_sw,fluxtop_lw,                          &
850                              fluxabs_sw,fluxtop_dn,OLR_nu,OSR_nu,                &
851                              lastcall)
852
853               ! Radiative flux from the sky absorbed by the surface (W.m-2).
854               GSR=0.0
855               fluxrad_sky(:)=emis(:)*fluxsurf_lw(:)+fluxsurfabs_sw(:)
856
857                            !if(noradsurf)then ! no lower surface; SW flux just disappears
858                            !   GSR = SUM(fluxsurf_sw(:)*cell_area(:))/totarea
859                            !   fluxrad_sky(:)=emis(:)*fluxsurf_lw(:)
860                            !   print*,'SW lost in deep atmosphere = ',GSR,' W m^-2'
861                            !endif
862
863               ! Net atmospheric radiative heating rate (K.s-1)
864               dtrad(:,:)=zdtsw(:,:)+zdtlw(:,:)
865               
866            elseif(newtonian)then
867           
868! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
869! II.b Call Newtonian cooling scheme
870! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
871               call newtrelax(ngrid,nlayer,mu0,sinlat,zpopsk,pt,pplay,pplev,dtrad,firstcall)
872
873               zdtsurf(:) = +(pt(:,1)-tsurf(:))/ptimestep
874               ! e.g. surface becomes proxy for 1st atmospheric layer ?
875
876            else
877           
878! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
879! II.c Atmosphere has no radiative effect
880! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
881               fluxtop_dn(:)  = fract(:)*mu0(:)*Fat1AU/dist_star**2
882               if(ngrid.eq.1)then ! / by 4 globally in 1D case...
883                  fluxtop_dn(1)  = fract(1)*Fat1AU/dist_star**2/2.0
884               endif
885               fluxsurf_sw(:) = fluxtop_dn(:)
886               print*,'------------WARNING---WARNING------------' ! by MT2015.
887               print*,'You are in corrk=false mode, '
888               print*,'and the surface albedo is taken equal to the first visible spectral value'               
889               
890               fluxsurfabs_sw(:) = fluxtop_dn(:)*(1.-albedo(:,1))
891               fluxrad_sky(:)    = fluxsurfabs_sw(:)
892               fluxtop_lw(:)     = emis(:)*sigma*tsurf(:)**4
893
894               dtrad(:,:)=0.0 ! no atmospheric radiative heating
895
896            endif ! end of corrk
897
898         endif ! of if(mod(icount-1,iradia).eq.0)
899       
900
901         ! Transformation of the radiative tendencies
902         ! ------------------------------------------
903         zplanck(:)=tsurf(:)*tsurf(:)
904         zplanck(:)=emis(:)*sigma*zplanck(:)*zplanck(:)
905         fluxrad(:)=fluxrad_sky(:)-zplanck(:)
906         pdt(:,:)=pdt(:,:)+dtrad(:,:)
907         
908         ! Test of energy conservation
909         !----------------------------
910         if(enertest)then
911            call planetwide_sumval(cpp*massarea(:,:)*zdtsw(:,:)/totarea_planet,dEtotSW)
912            call planetwide_sumval(cpp*massarea(:,:)*zdtlw(:,:)/totarea_planet,dEtotLW)
913            !call planetwide_sumval(fluxsurf_sw(:)*(1.-albedo_equivalent(:))*cell_area(:)/totarea_planet,dEtotsSW) !JL13 carefull, albedo can have changed since the last time we called corrk
914            call planetwide_sumval(fluxsurfabs_sw(:)*cell_area(:)/totarea_planet,dEtotsSW) !JL13 carefull, albedo can have changed since the last time we called corrk
915            call planetwide_sumval((fluxsurf_lw(:)*emis(:)-zplanck(:))*cell_area(:)/totarea_planet,dEtotsLW)
916            dEzRadsw(:,:)=cpp*mass(:,:)*zdtsw(:,:)
917            dEzRadlw(:,:)=cpp*mass(:,:)*zdtlw(:,:)
918            if (is_master) then
919               print*,'---------------------------------------------------------------'
920               print*,'In corrk SW atmospheric heating       =',dEtotSW,' W m-2'
921               print*,'In corrk LW atmospheric heating       =',dEtotLW,' W m-2'
922               print*,'atmospheric net rad heating (SW+LW)   =',dEtotLW+dEtotSW,' W m-2'
923               print*,'In corrk SW surface heating           =',dEtotsSW,' W m-2'
924               print*,'In corrk LW surface heating           =',dEtotsLW,' W m-2'
925               print*,'surface net rad heating (SW+LW)       =',dEtotsLW+dEtotsSW,' W m-2'
926            endif
927         endif ! end of 'enertest'
928
929      endif ! of if (callrad)
930
931
932
933!  --------------------------------------------
934!  III. Vertical diffusion (turbulent mixing) :
935!  --------------------------------------------
936
937      if (calldifv) then
938     
939         zflubid(:)=fluxrad(:)+fluxgrd(:)
940
941         ! JL12 the following if test is temporarily there to allow us to compare the old vdifc with turbdiff.
942         if (UseTurbDiff) then
943         
944            call turbdiff(ngrid,nlayer,nq,                       &
945                          ptimestep,capcal,lwrite,               &
946                          pplay,pplev,zzlay,zzlev,z0,            &
947                          pu,pv,pt,zpopsk,pq,tsurf,emis,qsurf,   &
948                          pdt,pdq,zflubid,                       &
949                          zdudif,zdvdif,zdtdif,zdtsdif,          &
950                          sensibFlux,q2,zdqdif,zdqsdif,          &
951                          taux,tauy,lastcall)
952
953         else
954         
955            zdh(:,:)=pdt(:,:)/zpopsk(:,:)
956 
957            call vdifc(ngrid,nlayer,nq,zpopsk,                &
958                       ptimestep,capcal,lwrite,               &
959                       pplay,pplev,zzlay,zzlev,z0,            &
960                       pu,pv,zh,pq,tsurf,emis,qsurf,          &
961                       zdh,pdq,zflubid,                       &
962                       zdudif,zdvdif,zdhdif,zdtsdif,          &
963                       sensibFlux,q2,zdqdif,zdqsdif,          &
964                       taux,tauy,lastcall)
965
966            zdtdif(:,:)=zdhdif(:,:)*zpopsk(:,:) ! for diagnostic only
967            zdqevap(:,:)=0.
968
969         end if !end of 'UseTurbDiff'
970
971
972         pdv(:,:)=pdv(:,:)+zdvdif(:,:)
973         pdu(:,:)=pdu(:,:)+zdudif(:,:)
974         pdt(:,:)=pdt(:,:)+zdtdif(:,:)
975         zdtsurf(:)=zdtsurf(:)+zdtsdif(:)
976
977         if (tracer) then
978           pdq(:,:,:)=pdq(:,:,:)+ zdqdif(:,:,:)
979           dqsurf(:,:)=dqsurf(:,:) + zdqsdif(:,:)
980         end if ! of if (tracer)
981
982
983         ! test energy conservation
984         !-------------------------
985         if(enertest)then
986         
987            dEzdiff(:,:)=cpp*mass(:,:)*zdtdif(:,:)
988            do ig = 1, ngrid
989               dEdiff(ig)=SUM(dEzdiff (ig,:))+ sensibFlux(ig)! subtract flux to the ground
990               dEzdiff(ig,1)= dEzdiff(ig,1)+ sensibFlux(ig)! subtract flux to the ground
991            enddo
992           
993            call planetwide_sumval(dEdiff(:)*cell_area(:)/totarea_planet,dEtot)
994            dEdiffs(:)=capcal(:)*zdtsdif(:)-zflubid(:)-sensibFlux(:)
995            call planetwide_sumval(dEdiffs(:)*cell_area(:)/totarea_planet,dEtots)
996            call planetwide_sumval(sensibFlux(:)*cell_area(:)/totarea_planet,AtmToSurf_TurbFlux)
997           
998            if (is_master) then
999           
1000               if (UseTurbDiff) then
1001                         print*,'In TurbDiff sensible flux (atm=>surf) =',AtmToSurf_TurbFlux,' W m-2'
1002                         print*,'In TurbDiff non-cons atm nrj change   =',dEtot,' W m-2'
1003                  print*,'In TurbDiff (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1004               else
1005                         print*,'In vdifc sensible flux (atm=>surf)    =',AtmToSurf_TurbFlux,' W m-2'
1006                         print*,'In vdifc non-cons atm nrj change      =',dEtot,' W m-2'
1007                         print*,'In vdifc (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1008               end if
1009            endif ! end of 'is_master'
1010           
1011         ! JL12 : note that the black body radiative flux emitted by the surface has been updated by the implicit scheme but not given back elsewhere.
1012         endif ! end of 'enertest'
1013
1014      else ! calldifv
1015
1016         if(.not.newtonian)then
1017
1018            zdtsurf(:) = zdtsurf(:) + (fluxrad(:) + fluxgrd(:))/capcal(:)
1019
1020         endif
1021
1022      endif ! end of 'calldifv'
1023
1024
1025!----------------------------------
1026!   IV. Dry convective adjustment :
1027!----------------------------------
1028
1029      if(calladj) then
1030
1031         zdh(:,:) = pdt(:,:)/zpopsk(:,:)
1032         zduadj(:,:)=0.0
1033         zdvadj(:,:)=0.0
1034         zdhadj(:,:)=0.0
1035
1036
1037         call convadj(ngrid,nlayer,nq,ptimestep,            &
1038                      pplay,pplev,zpopsk,                   &
1039                      pu,pv,zh,pq,                          &
1040                      pdu,pdv,zdh,pdq,                      &
1041                      zduadj,zdvadj,zdhadj,                 &
1042                      zdqadj)
1043
1044         pdu(:,:) = pdu(:,:) + zduadj(:,:)
1045         pdv(:,:) = pdv(:,:) + zdvadj(:,:)
1046         pdt(:,:)    = pdt(:,:) + zdhadj(:,:)*zpopsk(:,:)
1047         zdtadj(:,:) = zdhadj(:,:)*zpopsk(:,:) ! for diagnostic only
1048
1049         if(tracer) then
1050            pdq(:,:,:) = pdq(:,:,:) + zdqadj(:,:,:)
1051         end if
1052
1053         ! Test energy conservation
1054         if(enertest)then
1055            call planetwide_sumval(cpp*massarea(:,:)*zdtadj(:,:)/totarea_planet,dEtot)
1056            if (is_master) print*,'In convadj atmospheric energy change  =',dEtot,' W m-2'
1057         endif
1058
1059         
1060      endif ! end of 'calladj'
1061     
1062
1063!---------------------------------------------
1064!   V. Specific parameterizations for tracers
1065!---------------------------------------------
1066
1067      if (tracer) then
1068
1069  ! -------------------------
1070  !   V.1. Chemistry
1071  ! -------------------------
1072
1073         if (callchim) then
1074
1075            ! o. Convert updated tracers to molar fraction
1076            ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1077            do iq = 1,nkim
1078               ychim(:,:,iq) = ( pq(:,:,iq+nmicro) + pdq(:,:,iq+nmicro) ) / rat_mmol(iq+nmicro)
1079            enddo
1080
1081            ! i. Condensation after the transport
1082            ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1083            do iq=1,nkim
1084               do l=1,nlayer
1085                  do ig=1,ngrid
1086                     if ( ychim(ig,l,iq).gt.qysat(l,iq) ) then
1087                        dyccond(ig,l,iq+nmicro) = ( -ychim(ig,l,iq)+qysat(l,iq) ) / ptimestep
1088                     else
1089                        dyccond(ig,l,iq+nmicro) = 0.0 ! since array not saved
1090                     endif
1091                  enddo
1092               enddo
1093            enddo
1094
1095            if ( callclouds ) dyccond(:,:,ices_indx) = 0.0 ! Condensation will be calculated in the cloud microphysics
1096
1097            do iq=1,nkim
1098              ychim(:,:,iq) = ychim(:,:,iq) + dyccond(:,:,iq+nmicro) ! update molar ychim for following calchim
1099              zdqcond(:,:,iq+nmicro) = dyccond(:,:,iq+nmicro)*rat_mmol(iq+nmicro) ! convert tendencies to mass mixing ratio
1100            enddo
1101           
1102            pdq(:,:,:) = pdq(:,:,:) + zdqcond(:,:,:)
1103
1104            ! ii. Photochemistry
1105            ! ~~~~~~~~~~~~~~~~~~
1106            if( mod(icount-1,ichim).eq.0. ) then
1107               
1108               print *, "We enter in the chemistry ..."
1109
1110               if (moyzon_ch) then ! 2D zonally averaged chemistry
1111
1112                 do iq = 1,nkim ! In this case we send zonal average from dynamics to chem. module
1113                   ychim(:,:,iq) = zqfibar(:,:,iq+nmicro) / rat_mmol(iq+nmicro)
1114                 enddo
1115
1116                 call calchim(ngrid,ychim,declin,ctimestep,ztfibar,zphibar,  &
1117                              zplaybar,zplevbar,zzlaybar,zzlevbar,dycchi)
1118
1119               else ! 3D chemistry (or 1D run)
1120                 call calchim(ngrid,ychim,declin,ctimestep,pt,pphi,  &
1121                              pplay,pplev,zzlay,zzlev,dycchi)
1122               endif ! if moyzon
1123            endif
1124           
1125            do iq=1,nkim
1126               zdqchi(:,:,iq+nmicro) = dycchi(:,:,iq)*rat_mmol(iq+nmicro) ! convert tendencies to mass mixing ratio
1127 
1128               where( (pq(:,:,iq+nmicro)+zdqchi(:,:,iq+nmicro) ).LT.1e-40)  & ! When using zonal means we set the same tendency
1129                       zdqchi(:,:,iq+nmicro) = 1e-40 - ( pq(:,:,iq+nmicro) )  ! everywhere in longitude -> can lead to negs !
1130             enddo
1131
1132            pdq(:,:,:) = pdq(:,:,:) + zdqchi(:,:,:)
1133           
1134         endif ! end of 'callchim'
1135
1136  ! -------------------
1137  !   V.2 Microphysics
1138  ! -------------------
1139 
1140         if (callmufi) then
1141#ifdef USE_QTEST
1142            dtpq(:,:,:) = 0.0 ! we want tpq to go only through mufi
1143            call calmufi(ptimestep,pplev,zzlev,pplay,zzlay,pt,tpq,dtpq,zdqmufi)
1144            tpq(:,:,:) = tpq(:,:,:) + zdqmufi(:,:,:)*ptimestep ! only manipulation of tpq->*ptimestep here
1145#else
1146            call calmufi(ptimestep,pplev,zzlev,pplay,zzlay,pt,pq,pdq,zdqmufi)
1147            pdq(:,:,:) = pdq(:,:,:) + zdqmufi(:,:,:)
1148#endif
1149         endif
1150     
1151  ! ---------------
1152  !   V.3 Updates
1153  ! ---------------
1154
1155         ! Updating Atmospheric Mass and Tracers budgets.
1156         if(mass_redistrib) then
1157
1158            zdmassmr(:,:) = mass(:,:) * zdqevap(:,:)
1159
1160            do ig = 1, ngrid
1161               zdmassmr_col(ig)=SUM(zdmassmr(ig,:))
1162            enddo
1163           
1164            call writediagfi(ngrid,"mass_evap","mass gain"," ",3,zdmassmr)
1165            call writediagfi(ngrid,"mass_evap_col","mass gain col"," ",2,zdmassmr_col)
1166            call writediagfi(ngrid,"mass","mass","kg/m2",3,mass)
1167
1168            call mass_redistribution(ngrid,nlayer,nq,ptimestep,                     &
1169                                     capcal,pplay,pplev,pt,tsurf,pq,qsurf,          &
1170                                     pu,pv,pdt,zdtsurf,pdq,pdu,pdv,zdmassmr,        &
1171                                     zdtmr,zdtsurfmr,zdpsrfmr,zdumr,zdvmr,zdqmr,zdqsurfmr)
1172         
1173            pdq(:,:,:)  = pdq(:,:,:)  + zdqmr(:,:,:)
1174            dqsurf(:,:) = dqsurf(:,:) + zdqsurfmr(:,:)
1175            pdt(:,:)    = pdt(:,:)    + zdtmr(:,:)
1176            pdu(:,:)    = pdu(:,:)    + zdumr(:,:)
1177            pdv(:,:)    = pdv(:,:)    + zdvmr(:,:)
1178            pdpsrf(:)   = pdpsrf(:)   + zdpsrfmr(:)
1179            zdtsurf(:)  = zdtsurf(:)  + zdtsurfmr(:)
1180           
1181         endif
1182
1183  ! -----------------------------
1184  !   V.4. Surface Tracer Update
1185  ! -----------------------------
1186
1187        qsurf(:,:) = qsurf(:,:) + ptimestep*dqsurf(:,:)
1188
1189         ! Add qsurf to qsurf_hist, which is what we save in diagfi.nc. At the same time, we set the water
1190         ! content of ocean gridpoints back to zero, in order to avoid rounding errors in vdifc, rain.
1191         qsurf_hist(:,:) = qsurf(:,:)
1192
1193      endif! end of if 'tracer'
1194
1195
1196!------------------------------------------------
1197!   VI. Surface and sub-surface soil temperature
1198!------------------------------------------------
1199
1200
1201      ! Increment surface temperature
1202
1203      tsurf(:)=tsurf(:)+ptimestep*zdtsurf(:)
1204
1205      ! Compute soil temperatures and subsurface heat flux.
1206      if (callsoil) then
1207         call soil(ngrid,nsoilmx,.false.,lastcall,inertiedat,   &
1208                   ptimestep,tsurf,tsoil,capcal,fluxgrd)     
1209      endif
1210
1211
1212      ! Test energy conservation
1213      if(enertest)then
1214         call planetwide_sumval(cell_area(:)*capcal(:)*zdtsurf(:)/totarea_planet,dEtots)         
1215         if (is_master) print*,'Surface energy change                 =',dEtots,' W m-2'
1216      endif
1217
1218
1219!---------------------------------------------------
1220!   VII. Perform diagnostics and write output files
1221!---------------------------------------------------
1222
1223      ! Note : For output only: the actual model integration is performed in the dynamics.
1224
1225
1226 
1227      ! Temperature, zonal and meridional winds.
1228      zt(:,:) = pt(:,:) + pdt(:,:)*ptimestep
1229      zu(:,:) = pu(:,:) + pdu(:,:)*ptimestep
1230      zv(:,:) = pv(:,:) + pdv(:,:)*ptimestep
1231
1232      ! Diagnostic.
1233      zdtdyn(:,:)     = (pt(:,:)-ztprevious(:,:)) / ptimestep
1234      ztprevious(:,:) = zt(:,:)
1235
1236      zdudyn(:,:)     = (pu(:,:)-zuprevious(:,:)) / ptimestep
1237      zuprevious(:,:) = zu(:,:)
1238
1239      if(firstcall)then
1240         zdtdyn(:,:)=0.0
1241         zdudyn(:,:)=0.0
1242      endif
1243
1244      ! Dynamical heating diagnostic.
1245      do ig=1,ngrid
1246         fluxdyn(ig)= SUM(zdtdyn(ig,:) *mass(ig,:))*cpp
1247      enddo
1248
1249      ! Tracers.
1250      zq(:,:,:) = pq(:,:,:) + pdq(:,:,:)*ptimestep
1251
1252      ! Surface pressure.
1253      ps(:) = pplev(:,1) + pdpsrf(:)*ptimestep
1254
1255
1256
1257      ! Surface and soil temperature information
1258      call planetwide_sumval(cell_area(:)*tsurf(:)/totarea_planet,Ts1)
1259      call planetwide_minval(tsurf(:),Ts2)
1260      call planetwide_maxval(tsurf(:),Ts3)
1261      if(callsoil)then
1262         TsS = SUM(cell_area(:)*tsoil(:,nsoilmx))/totarea        ! mean temperature at bottom soil layer
1263         if (is_master) then
1264            print*,'          ave[Tsurf]             min[Tsurf]             max[Tsurf]             ave[Tdeep]'
1265            print*,Ts1,Ts2,Ts3,TsS
1266         end if
1267      else
1268         if (is_master) then
1269            print*,'          ave[Tsurf]             min[Tsurf]             max[Tsurf]'
1270            print*,Ts1,Ts2,Ts3
1271         endif
1272      end if
1273
1274
1275      ! Check the energy balance of the simulation during the run
1276      if(corrk)then
1277
1278         call planetwide_sumval(cell_area(:)*fluxtop_dn(:)/totarea_planet,ISR)
1279         call planetwide_sumval(cell_area(:)*fluxabs_sw(:)/totarea_planet,ASR)
1280         call planetwide_sumval(cell_area(:)*fluxtop_lw(:)/totarea_planet,OLR)
1281         call planetwide_sumval(cell_area(:)*fluxgrd(:)/totarea_planet,GND)
1282         call planetwide_sumval(cell_area(:)*fluxdyn(:)/totarea_planet,DYN)
1283         do ig=1,ngrid
1284            if(fluxtop_dn(ig).lt.0.0)then
1285               print*,'fluxtop_dn has gone crazy'
1286               print*,'fluxtop_dn=',fluxtop_dn(ig)
1287               print*,'temp=   ',pt(ig,:)
1288               print*,'pplay=  ',pplay(ig,:)
1289               call abort
1290            endif
1291         end do
1292                     
1293         if(ngrid.eq.1)then
1294            DYN=0.0
1295         endif
1296         
1297         if (is_master) then
1298            print*,'  ISR            ASR            OLR            GND            DYN [W m^-2]'
1299            print*, ISR,ASR,OLR,GND,DYN
1300         endif
1301
1302         if(enertest .and. is_master)then
1303            print*,'SW flux/heating difference SW++ - ASR = ',dEtotSW+dEtotsSW-ASR,' W m-2'
1304            print*,'LW flux/heating difference LW++ - OLR = ',dEtotLW+dEtotsLW+OLR,' W m-2'
1305            print*,'LW energy balance LW++ + ASR = ',dEtotLW+dEtotsLW+ASR,' W m-2'
1306         endif
1307
1308         if(meanOLR .and. is_master)then
1309            if((ngrid.gt.1) .or. (mod(icount-1,ecritphy).eq.0))then
1310               ! to record global radiative balance
1311               open(92,file="rad_bal.out",form='formatted',position='append')
1312               write(92,*) zday,ISR,ASR,OLR
1313               close(92)
1314               open(93,file="tem_bal.out",form='formatted',position='append')
1315               if(callsoil)then
1316                         write(93,*) zday,Ts1,Ts2,Ts3,TsS
1317               else
1318                  write(93,*) zday,Ts1,Ts2,Ts3
1319               endif
1320               close(93)
1321            endif
1322         endif
1323
1324      endif ! end of 'corrk'
1325
1326
1327      ! Diagnostic to test radiative-convective timescales in code.
1328      if(testradtimes)then
1329         open(38,file="tau_phys.out",form='formatted',position='append')
1330         ig=1
1331         do l=1,nlayer
1332            write(38,*) -1./pdt(ig,l),pt(ig,l),pplay(ig,l)
1333         enddo
1334         close(38)
1335         print*,'As testradtimes enabled,'
1336         print*,'exiting physics on first call'
1337         call abort
1338      endif
1339
1340
1341      if (is_master) print*,'--> Ls =',zls*180./pi
1342     
1343     
1344!----------------------------------------------------------------------
1345!        Writing NetCDF file  "RESTARTFI" at the end of the run
1346!----------------------------------------------------------------------
1347
1348!        Note: 'restartfi' is stored just before dynamics are stored
1349!              in 'restart'. Between now and the writting of 'restart',
1350!              there will have been the itau=itau+1 instruction and
1351!              a reset of 'time' (lastacll = .true. when itau+1= itaufin)
1352!              thus we store for time=time+dtvr
1353
1354
1355
1356      if(lastcall) then
1357         ztime_fin = ptime + ptimestep/(float(iphysiq)*daysec)
1358
1359         if (ngrid.ne.1) then
1360            write(*,*)'PHYSIQ: for physdem ztime_fin =',ztime_fin
1361           
1362            call physdem1("restartfi.nc",nsoilmx,ngrid,nlayer,nq, &
1363                          ptimestep,ztime_fin,                    &
1364                          tsurf,tsoil,emis,q2,qsurf_hist,tankCH4)
1365         endif
1366    endif ! end of 'lastcall'
1367
1368
1369!-----------------------------------------------------------------------------------------------------
1370!           OUTPUT in netcdf file "DIAGFI.NC", containing any variable for diagnostic
1371!
1372!             Note 1 : output with  period "ecritphy", set in "run.def"
1373!
1374!             Note 2 : writediagfi can also be called from any other subroutine for any variable,
1375!                      but its preferable to keep all the calls in one place ...
1376!-----------------------------------------------------------------------------------------------------
1377
1378
1379      call writediagfi(ngrid,"Ls","solar longitude","deg",0,zls*180./pi)
1380      call writediagfi(ngrid,"Lss","sub solar longitude","deg",0,zlss*180./pi)
1381      call writediagfi(ngrid,"RA","right ascension","deg",0,right_ascen*180./pi)
1382      call writediagfi(ngrid,"Declin","solar declination","deg",0,declin*180./pi)
1383      call writediagfi(ngrid,"tsurf","Surface temperature","K",2,tsurf)
1384      call writediagfi(ngrid,"ps","Surface pressure","Pa",2,ps)
1385      call writediagfi(ngrid,"temp","temperature","K",3,zt)
1386      call writediagfi(ngrid,"teta","potential temperature","K",3,zh)
1387      call writediagfi(ngrid,"u","Zonal wind","m.s-1",3,zu)
1388      call writediagfi(ngrid,"v","Meridional wind","m.s-1",3,zv)
1389      call writediagfi(ngrid,"w","Vertical wind","m.s-1",3,pw)
1390      call writediagfi(ngrid,"p","Pressure","Pa",3,pplay)
1391
1392!     Subsurface temperatures
1393!        call writediagsoil(ngrid,"tsurf","Surface temperature","K",2,tsurf)
1394!        call writediagsoil(ngrid,"temp","temperature","K",3,tsoil)
1395
1396     
1397
1398      ! Total energy balance diagnostics
1399      if(callrad.and.(.not.newtonian))then
1400     
1401         !call writediagfi(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
1402         call writediagfi(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
1403         call writediagfi(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
1404         call writediagfi(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
1405         
1406!           call writediagfi(ngrid,"ASRcs","absorbed stellar rad (cs).","W m-2",2,fluxabs_sw1)
1407!           call writediagfi(ngrid,"OLRcs","outgoing longwave rad (cs).","W m-2",2,fluxtop_lw1)
1408!           call writediagfi(ngrid,"fluxsurfsw","sw surface flux.","W m-2",2,fluxsurf_sw)
1409!           call writediagfi(ngrid,"fluxsurflw","lw back radiation.","W m-2",2,fluxsurf_lw)
1410!           call writediagfi(ngrid,"fluxsurfswcs","sw surface flux (cs).","W m-2",2,fluxsurf_sw1)
1411!           call writediagfi(ngrid,"fluxsurflwcs","lw back radiation (cs).","W m-2",2,fluxsurf_lw1)
1412
1413
1414         call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrd)
1415         
1416         call writediagfi(ngrid,"DYN","dynamical heat input","W m-2",2,fluxdyn)
1417         
1418      endif ! end of 'callrad'
1419       
1420      if(enertest) then
1421     
1422         if (calldifv) then
1423         
1424            call writediagfi(ngrid,"q2","turbulent kinetic energy","J.kg^-1",3,q2)
1425            call writediagfi(ngrid,"sensibFlux","sensible heat flux","w.m^-2",2,sensibFlux)
1426           
1427!             call writediagfi(ngrid,"dEzdiff","turbulent diffusion heating (-sensible flux)","w.m^-2",3,dEzdiff)
1428!             call writediagfi(ngrid,"dEdiff","integrated turbulent diffusion heating (-sensible flux)","w.m^-2",2,dEdiff)
1429!             call writediagfi(ngrid,"dEdiffs","In TurbDiff (correc rad+latent heat) surf nrj change","w.m^-2",2,dEdiffs)
1430             
1431         endif
1432         
1433         if (corrk) then
1434            call writediagfi(ngrid,"dEzradsw","radiative heating","w.m^-2",3,dEzradsw)
1435            call writediagfi(ngrid,"dEzradlw","radiative heating","w.m^-2",3,dEzradlw)
1436         endif
1437         
1438      endif ! end of 'enertest'
1439
1440        ! Temporary inclusions for winds diagnostics.
1441        call writediagfi(ngrid,"zdudif","Turbdiff tend. zon. wind","m s-2",3,zdudif)
1442        call writediagfi(ngrid,"zdudyn","Dyn. tend. zon. wind","m s-2",3,zdudyn)
1443
1444        ! Temporary inclusions for heating diagnostics.
1445        call writediagfi(ngrid,"zdtsw","SW heating","T s-1",3,zdtsw)
1446        call writediagfi(ngrid,"zdtlw","LW heating","T s-1",3,zdtlw)
1447        call writediagfi(ngrid,"dtrad","radiative heating","K s-1",3,dtrad)
1448        call writediagfi(ngrid,"zdtdyn","Dyn. heating","T s-1",3,zdtdyn)
1449       
1450        ! For Debugging.
1451        call writediagfi(ngrid,'pphi','Geopotential',' ',3,pphi)
1452       
1453
1454      ! Output tracers.
1455      if (tracer) then
1456
1457         if (callmufi) then ! For now we assume an given order for tracers !
1458#ifdef USE_QTEST
1459            ! Microphysical tracers passed through dyn+phys(except mufi)
1460            call writediagfi(ngrid,"mu_m0as_dp","Dynphys only spherical mode 0th order moment",'kg/kg',3,zq(:,:,1))
1461            call writediagfi(ngrid,"mu_m3as_dp","Dynphys only spherical mode 3rd order moment",'kg/kg',3,zq(:,:,2))
1462            call writediagfi(ngrid,"mu_m0af_dp","Dynphys only fractal mode 0th order moment",'kg/kg',3,zq(:,:,3))
1463            call writediagfi(ngrid,"mu_m3af_dp","Dynphys only fractal mode 3rd order moment",'kg/kg',3,zq(:,:,4))
1464            ! Microphysical tracers passed through mufi only
1465            call writediagfi(ngrid,"mu_m0as_mo","Mufi only spherical mode 0th order moment",'kg/kg',3,tpq(:,:,1))
1466            call writediagfi(ngrid,"mu_m3as_mo","Mufi only spherical mode 3rd order moment",'kg/kg',3,tpq(:,:,2))
1467            call writediagfi(ngrid,"mu_m0af_mo","Mufi only fractal mode 0th order moment",'kg/kg',3,tpq(:,:,3))
1468            call writediagfi(ngrid,"mu_m3af_mo","Mufi only fractal mode 3rd order moment",'kg/kg',3,tpq(:,:,4))
1469#else
1470            call writediagfi(ngrid,"mu_m0as","Spherical mode 0th order moment",'kg/kg',3,zq(:,:,1))
1471            call writediagfi(ngrid,"mu_m3as","Spherical mode 3rd order moment",'kg/kg',3,zq(:,:,2))
1472            call writediagfi(ngrid,"mu_m0af","Fractal mode 0th order moment",'kg/kg',3,zq(:,:,3))
1473            call writediagfi(ngrid,"mu_m3af","Fractal mode 3rd order moment",'kg/kg',3,zq(:,:,4))
1474#endif
1475         endif ! end of 'callmufi'
1476
1477        if ( callchim .and. (.not. callmufi) ) then
1478           call writediagfi(ngrid,"C2H2","C2H2",'kg/kg',3,zq(:,:,10))
1479           call writediagfi(ngrid,"C2H4","C2H4",'kg/kg',3,zq(:,:,12))
1480           call writediagfi(ngrid,"C2H6","C2H6",'kg/kg',3,zq(:,:,14))
1481           call writediagfi(ngrid,"C4H2","C4H2",'kg/kg',3,zq(:,:,26))
1482           call writediagfi(ngrid,"HCN","HCN",'kg/kg',3,zq(:,:,36))
1483           call writediagfi(ngrid,"HC3N","HC3N",'kg/kg',3,zq(:,:,42))
1484        else if ( callchim .and. callmufi ) then
1485           call writediagfi(ngrid,"C2H2","C2H2",'kg/kg',3,zq(:,:,14))
1486           call writediagfi(ngrid,"C2H4","C2H4",'kg/kg',3,zq(:,:,16))
1487           call writediagfi(ngrid,"C2H6","C2H6",'kg/kg',3,zq(:,:,18))
1488           call writediagfi(ngrid,"C4H2","C4H2",'kg/kg',3,zq(:,:,30))
1489           call writediagfi(ngrid,"HCN","HCN",'kg/kg',3,zq(:,:,40))
1490           call writediagfi(ngrid,"HC3N","HC3N",'kg/kg',3,zq(:,:,46))
1491        endif
1492
1493       endif ! end of 'tracer'
1494
1495! XIOS outputs
1496#ifdef CPP_XIOS     
1497      ! Send fields to XIOS: (NB these fields must also be defined as
1498      ! <field id="..." /> in context_lmdz_physics.xml to be correctly used)
1499      CALL send_xios_field("ls",zls)
1500     
1501      CALL send_xios_field("ps",ps)
1502      CALL send_xios_field("area",cell_area)
1503
1504      CALL send_xios_field("temperature",zt)
1505      CALL send_xios_field("u",zu)
1506      CALL send_xios_field("v",zv)
1507
1508      if (lastcall.and.is_omp_master) then
1509        write(*,*) "physiq: call xios_context_finalize"
1510        call xios_context_finalize
1511      endif
1512#endif
1513
1514      icount=icount+1
1515
1516    end subroutine physiq
1517   
1518    end module physiq_mod
Note: See TracBrowser for help on using the repository browser.