source: trunk/LMDZ.PLUTO/libf/phypluto/physiq_mod.F90 @ 3244

Last change on this file since 3244 was 3244, checked in by afalco, 9 months ago

Pluto PCM:
Give priority to vdifc from pluto.old & fixed it.
AF

  • Property svn:executable set to *
File size: 99.8 KB
Line 
1      module physiq_mod
2
3      implicit none
4
5      contains
6
7      subroutine physiq(ngrid,nlayer,nq,   &
8                  firstcall,lastcall,      &
9                  pday,ptime,ptimestep,    &
10                  pplev,pplay,pphi,        &
11                  pu,pv,pt,pq,             &
12                  flxw,                    &
13                  pdu,pdv,pdt,pdq,pdpsrf)
14
15!!
16      use write_field_phy, only: Writefield_phy
17!!
18      use ioipsl_getin_p_mod, only: getin_p
19      use radinc_h, only : L_NSPECTI,L_NSPECTV,naerkind, corrkdir, banddir
20      use generic_cloud_common_h, only : epsi_generic, Psat_generic
21      use gases_h, only: gnom, gfrac
22      use radcommon_h, only: sigma, glat, grav, BWNV, WNOI, DWNI, DWNV, WNOV
23      use suaer_corrk_mod, only: suaer_corrk
24      use radii_mod, only: su_aer_radii,haze_reffrad_fix
25      use aerosol_mod, only: iaero_haze, i_haze, haze_prof
26      use surfdat_h, only: phisfi, zmea, zstd, zsig, zgam, zthe, &
27                           dryness
28      use comdiurn_h, only: coslat, sinlat, coslon, sinlon
29      use comsaison_h, only: mu0, fract, dist_star, declin, right_ascen
30      use comsoil_h, only: nsoilmx, layer, mlayer, inertiedat
31      use geometry_mod, only: latitude, longitude, cell_area
32      USE comgeomfi_h, only: totarea, totarea_planet
33      USE tracer_h, only: noms, mmol, radius, rho_q, qext, &
34                          igcm_n2,igcm_ch4_gas,igcm_ch4_ice,igcm_haze,&
35                          igcm_co_gas,igcm_co_ice,igcm_prec_haze,lw_n2,&
36                          alpha_lift, alpha_devil, qextrhor, &
37                          nesp, is_chim, is_condensable,constants_epsi_generic
38      use time_phylmdz_mod, only: ecritphy, iphysiq, nday
39      use phyetat0_mod, only: phyetat0
40      use wstats_mod, only: callstats, wstats, mkstats
41      use phyredem, only: physdem0, physdem1
42      use planetwide_mod, only: planetwide_minval,planetwide_maxval,planetwide_sumval
43      use mod_phys_lmdz_para, only : is_master
44      use planete_mod, only: apoastr, periastr, year_day, peri_day, &
45                            obliquit, nres, z0, adjust, tpal
46      use comcstfi_mod, only: pi, g, rcp, r, rad, mugaz, cpp
47      use time_phylmdz_mod, only: daysec
48      use callkeys_mod, only: albedo_spectral_mode, calladj, calldifv, &
49                              callrad, callsoil, nosurf, &
50                              aerohaze, corrk, diagdtau,&
51                              diurnal, enertest, fat1au, &
52                              icetstep, intheat, iradia, kastprof, &
53                              lwrite, mass_redistrib, meanOLR, &
54                              fast,fasthaze,haze,metcloud,monoxcloud,&
55                              n2cond,nearn2cond,noseason_day,conservn2, &
56                              kbo,triton,paleo,paleoyears, &
57                              carbox, methane, UseVdifcPlutold, &
58                              aerohaze,haze_proffix,source_haze, &
59                              season, sedimentation,generic_condensation, &
60                              specOLR, &
61                              startphy_file, testradtimes, &
62                              tracer, UseTurbDiff, &
63                              global1d, szangle
64      use generic_tracer_index_mod, only: generic_tracer_index
65      use check_fields_mod, only: check_physics_fields
66      use conc_mod, only: rnew, cpnew, ini_conc_mod
67      use phys_state_var_mod
68      use callcorrk_mod, only: callcorrk
69      use vdifc_mod, only: vdifc
70      use vdifc_pluto_mod, only: vdifc_pluto
71      use turbdiff_mod, only: turbdiff
72      use turb_mod, only : q2,sensibFlux,turb_resolved
73      use mass_redistribution_mod, only: mass_redistribution
74      use condensation_generic_mod, only: condensation_generic
75      use datafile_mod, only: datadir
76#ifndef MESOSCALE
77      use vertical_layers_mod, only: presnivs, pseudoalt
78      use mod_phys_lmdz_omp_data, ONLY: is_omp_master
79#else
80      use comm_wrf, only : comm_HR_SW, comm_HR_LW,  &
81         comm_CLOUDFRAC,comm_TOTCLOUDFRAC, comm_RH, &
82         comm_HR_DYN,     &
83         comm_DQICE,comm_DQVAP,comm_ALBEQ,          &
84         comm_FLUXTOP_DN,comm_FLUXABS_SW,           &
85         comm_FLUXTOP_LW,comm_FLUXSURF_SW,          &
86         comm_FLUXSURF_LW,comm_FLXGRD,              &
87         comm_DTRAIN,comm_DTLSC,                    &
88         comm_LATENT_HF
89
90#endif
91
92#ifdef CPP_XIOS
93      use xios_output_mod, only: initialize_xios_output, &
94                                 update_xios_timestep, &
95                                 send_xios_field
96      use wxios, only: wxios_context_init, xios_context_finalize
97#endif
98
99      implicit none
100
101
102!==================================================================
103!
104!     Purpose
105!     -------
106!     Central subroutine for all the physics parameterisations in the
107!     universal model. Originally adapted from the Mars LMDZ model.
108!
109!     The model can be run without or with tracer transport
110!     depending on the value of "tracer" in file "callphys.def".
111!
112!
113!   It includes:
114!
115!      I. Initialization :
116!         I.1 Firstcall initializations.
117!         I.2 Initialization for every call to physiq.
118!
119!      II. Compute radiative transfer tendencies (longwave and shortwave) :
120!         II.a Option 1 : Call correlated-k radiative transfer scheme.
121!         II.b Option 2 : Call Newtonian cooling scheme.
122!         II.! Option 3 : Atmosphere has no radiative effect.
123!
124!      III. Vertical diffusion (turbulent mixing) :
125!
126!      IV. Convection :
127!         IV.a Thermal plume model
128!         IV.b Dry convective adjusment
129!
130!      V. Condensation and sublimation of gases (currently just N2).
131!
132!      VI. Tracers
133!         VI.1. Water and water ice.
134!         VI.2  Photochemistry
135!         VI.3. Aerosols and particles.
136!         VI.4. Updates (pressure variations, surface budget).
137!         VI.5. Slab Ocean.
138!         VI.6. Surface Tracer Update.
139!
140!      VII. Surface and sub-surface soil temperature.
141!
142!      VIII. Perform diagnostics and write output files.
143!
144!
145!   arguments
146!   ---------
147!
148!   INPUT
149!   -----
150!
151!    ngrid                 Size of the horizontal grid.
152!    nlayer                Number of vertical layers.
153!    nq                    Number of advected fields.
154!
155!    firstcall             True at the first call.
156!    lastcall              True at the last call.
157!
158!    pday                  Number of days counted from the North. Spring equinoxe.
159!    ptime                 Universal time (0<ptime<1): ptime=0.5 at 12:00 UT.
160!    ptimestep             timestep (s).
161!
162!    pplay(ngrid,nlayer)   Pressure at the middle of the layers (Pa).
163!    pplev(ngrid,nlayer+1) Intermediate pressure levels (Pa).
164!    pphi(ngrid,nlayer)    Geopotential at the middle of the layers (m2.s-2).
165!
166!    pu(ngrid,nlayer)      u, zonal component of the wind (ms-1).
167!    pv(ngrid,nlayer)      v, meridional component of the wind (ms-1).
168!
169!    pt(ngrid,nlayer)      Temperature (K).
170!
171!    pq(ngrid,nlayer,nq)   Advected fields.
172!
173!    pudyn(ngrid,nlayer)    \
174!    pvdyn(ngrid,nlayer)     \ Dynamical temporal derivative for the
175!    ptdyn(ngrid,nlayer)     / corresponding variables.
176!    pqdyn(ngrid,nlayer,nq) /
177!    flxw(ngrid,nlayer)      vertical mass flux (kg/s) at layer lower boundary
178!
179!   OUTPUT
180!   ------
181!
182!    pdu(ngrid,nlayer)        \
183!    pdv(ngrid,nlayer)         \  Temporal derivative of the corresponding
184!    pdt(ngrid,nlayer)         /  variables due to physical processes.
185!    pdq(ngrid,nlayer)        /
186!    pdpsrf(ngrid)             /
187!
188!
189!     Authors
190!     -------
191!           Frederic Hourdin        15/10/93
192!           Francois Forget        1994
193!           Christophe Hourdin        02/1997
194!           Subroutine completely rewritten by F. Forget (01/2000)
195!           Water ice clouds: Franck Montmessin (update 06/2003)
196!           Radiatively active tracers: J.-B. Madeleine (10/2008-06/2009)
197!           New correlated-k radiative scheme: R. Wordsworth (2009)
198!           Many specifically Martian subroutines removed: R. Wordsworth (2009)
199!           Improved water cycle: R. Wordsworth / B. Charnay (2010)
200!           To F90: R. Wordsworth (2010)
201!           New turbulent diffusion scheme: J. Leconte (2012)
202!           Loops converted to F90 matrix format: J. Leconte (2012)
203!           No more ngrid/nq, F90 commons and adaptation to parallel: A. Spiga (2012)
204!           Purge of the code : M. Turbet (2015)
205!           Photochemical core developped by F. Lefevre: B. Charnay (2017)
206!           Purge for Pluto model : A. Falco (2024)
207!==================================================================
208
209
210!    0.  Declarations :
211!    ------------------
212
213include "netcdf.inc"
214
215! Arguments :
216! -----------
217
218!   INPUTS:
219!   -------
220
221      integer,intent(in) :: ngrid             ! Number of atmospheric columns.
222      integer,intent(in) :: nlayer            ! Number of atmospheric layers.
223      integer,intent(in) :: nq                ! Number of tracers.
224
225      logical,intent(in) :: firstcall ! Signals first call to physics.
226      logical,intent(in) :: lastcall  ! Signals last call to physics.
227
228      real,intent(in) :: pday                  ! Number of elapsed sols since reference Ls=0.
229      real,intent(in) :: ptime                 ! "Universal time", given as fraction of sol (e.g.: 0.5 for noon).
230      real,intent(in) :: ptimestep             ! Physics timestep (s).
231      real,intent(in) :: pplev(ngrid,nlayer+1) ! Inter-layer pressure (Pa).
232      real,intent(in) :: pplay(ngrid,nlayer)   ! Mid-layer pressure (Pa).
233      real,intent(in) :: pphi(ngrid,nlayer)    ! Geopotential at mid-layer (m2s-2).
234      real,intent(in) :: pu(ngrid,nlayer)      ! Zonal wind component (m/s).
235      real,intent(in) :: pv(ngrid,nlayer)      ! Meridional wind component (m/s).
236      real,intent(in) :: pt(ngrid,nlayer)      ! Temperature (K).
237      real,intent(in) :: pq(ngrid,nlayer,nq)   ! Tracers (kg/kg_of_air).
238      real,intent(in) :: flxw(ngrid,nlayer)    ! Vertical mass flux (ks/s) at lower boundary of layer
239
240!   OUTPUTS:
241!   --------
242
243!     Physical tendencies :
244
245      real,intent(out) :: pdu(ngrid,nlayer)    ! Zonal wind tendencies (m/s/s).
246      real,intent(out) :: pdv(ngrid,nlayer)    ! Meridional wind tendencies (m/s/s).
247      real,intent(out) :: pdt(ngrid,nlayer)    ! Temperature tendencies (K/s).
248      real,intent(out) :: pdq(ngrid,nlayer,nq) ! Tracer tendencies (kg/kg_of_air/s).
249      real,intent(out) :: pdpsrf(ngrid)        ! Surface pressure tendency (Pa/s).
250
251! Local saved variables:
252! ----------------------
253      integer,save :: day_ini                                      ! Initial date of the run (sol since Ls=0).
254      integer,save :: icount                                       ! Counter of calls to physiq during the run.
255!$OMP THREADPRIVATE(day_ini,icount)
256
257      !Pluto specific
258      REAL,save  :: acond,bcond
259      REAL,save  :: tcond1p4Pa
260      DATA tcond1p4Pa/38/
261
262
263
264! Local variables :
265! -----------------
266      !     Tendencies for the paleoclimate mode
267      REAL qsurfyear(ngrid,nq)  ! kg.m-2 averaged mass of ice lost/gained in the last Pluto year of the run
268      REAL phisfinew(ngrid)       ! geopotential of the bedrock (= phisfi-qsurf/1000*g)
269      REAL qsurfpal(ngrid,nq)           ! qsurf after a paleoclimate step : for physdem1 and restartfi
270      REAL phisfipal(ngrid)               ! geopotential after a paleoclimate step : for physdem1 and restartfi
271      REAL oblipal                   ! change of obliquity
272      REAL peri_daypal               ! new periday
273      REAL eccpal                    ! change of eccentricity
274      REAL tpalnew                   ! change of time
275      REAL adjustnew                 ! change in N2 ice albedo
276      REAL pdaypal                   ! new pday = day_ini + step
277      REAL zdt_tot                   ! time range corresponding to the flux of qsurfyear
278      REAL massacc(nq)             ! accumulated mass
279      REAL masslost(nq)            ! accumulated mass
280
281      REAL globave                   ! globalaverage 2D ps
282      REAL globaveice(nq)          ! globalaverage 2D ice
283      REAL globavenewice(nq)          ! globalaverage 2D ice
284      INTEGER lecttsoil     ! lecture of tsoil from proftsoil
285      REAL qsurf1(ngrid,nq)      ! saving qsurf to calculate flux over long timescales kg.m-2
286      REAL flusurf(ngrid,nq)     ! flux cond/sub kg.m-2.s-1
287      REAL flusurfold(ngrid,nq)  ! old flux cond/sub kg.m-2.s-1
288      REAL zplev(ngrid,nlayer+1),zplay(ngrid,nlayer)
289
290
291
292!     Aerosol (dust or ice) extinction optical depth  at reference wavelength
293!     for the "naerkind" optically active aerosols:
294
295      real,save,allocatable :: aerosol(:,:,:) ! Aerosols
296!$OMP THREADPRIVATE(aerosol)
297      real zh(ngrid,nlayer)               ! Potential temperature (K).
298      real pw(ngrid,nlayer)               ! Vertical velocity (m/s). (NOTE : >0 WHEN DOWNWARDS !!)
299      real omega(ngrid,nlayer)            ! omega velocity (Pa/s, >0 when downward)
300
301      integer i,l,ig,ierr,iq,nw,isoil,iesp, igcm_generic_vap, igcm_generic_ice
302      logical call_ice_vap_generic ! to call only one time the ice/vap pair of a tracer
303
304      real zls                       ! Solar longitude (radians).
305      real zlss                      ! Sub solar point longitude (radians).
306      real zday                      ! Date (time since Ls=0, calculated in sols).
307      real zzlay(ngrid,nlayer)       ! Altitude at the middle of the atmospheric layers.
308      real zzlev(ngrid,nlayer+1)     ! Altitude at the atmospheric layer boundaries.
309
310
311! VARIABLES for the thermal plume model (AF24: deleted)
312
313! TENDENCIES due to various processes :
314
315      ! For Surface Temperature : (K/s)
316      real zdtsurf(ngrid)     ! Cumulated tendencies.
317      real zdtsurfmr(ngrid)   ! Mass_redistribution routine.
318      real zdtsurfc(ngrid)    ! Condense_n2 routine.
319      real zdtsdif(ngrid)     ! Turbdiff/vdifc routines.
320      ! real zdtsurf_hyd(ngrid) ! Hydrol routine.
321
322      ! For Atmospheric Temperatures : (K/s)
323      real dtlscale(ngrid,nlayer)                             ! Largescale routine.
324      real dt_generic_condensation(ngrid,nlayer)              ! condensation_generic routine.
325      real zdtc(ngrid,nlayer)                                 ! Condense_n2 routine.
326      real zdtdif(ngrid,nlayer)                               ! Turbdiff/vdifc routines.
327      real zdtmr(ngrid,nlayer)                                ! Mass_redistribution routine.
328      real zdtsw1(ngrid,nlayer), zdtlw1(ngrid,nlayer)         ! Callcorrk routine.
329      real zdtchim(ngrid,nlayer)                              ! Calchim routine.
330
331      ! For Surface Tracers : (kg/m2/s)
332      real dqsurf(ngrid,nq)                 ! Cumulated tendencies.
333      !real zdqsurfc(ngrid)                  ! Condense_n2 routine.
334      REAL zdqsc(ngrid,nq)                  ! Condense_n2 routine.
335      real zdqsdif(ngrid,nq)                ! Turbdiff/vdifc routines.
336      real zdqssed(ngrid,nq)                ! Callsedim routine.
337      real zdqsurfmr(ngrid,nq)              ! Mass_redistribution routine.
338
339      ! For Tracers : (kg/kg_of_air/s)
340      real zdqc(ngrid,nlayer,nq)      ! Condense_n2 routine.
341      real zdqadj(ngrid,nlayer,nq)    ! Convadj routine.
342      real zdqdif(ngrid,nlayer,nq)    ! Turbdiff/vdifc routines.
343      real zdqevap(ngrid,nlayer)      ! Turbdiff routine.
344      real zdqsed(ngrid,nlayer,nq)    ! Callsedim routine.
345      real zdqmr(ngrid,nlayer,nq)     ! Mass_redistribution routine.
346      real dqvaplscale_generic(ngrid,nlayer,nq) ! condensation_generic routine.
347      real dqcldlscale_generic(ngrid,nlayer,nq) ! condensation_generic routine.
348      REAL,allocatable,save :: zdqchim(:,:,:) ! Calchim_asis routine
349      REAL,allocatable,save :: zdqschim(:,:)  ! Calchim_asis routine
350!$OMP THREADPRIVATE(zdqchim,zdqschim)
351
352
353      !! PLUTO variables
354      REAL zdqch4cloud(ngrid,nlayer,nq)
355      REAL zdqsch4cloud(ngrid,nq)
356      REAL zdtch4cloud(ngrid,nlayer)
357      REAL zdqcocloud(ngrid,nlayer,nq)
358      REAL zdqscocloud(ngrid,nq)
359      REAL zdtcocloud(ngrid,nlayer)
360      REAL rice_ch4(ngrid,nlayer)    ! Methane ice geometric mean radius (m)
361      REAL rice_co(ngrid,nlayer)     ! CO ice geometric mean radius (m)
362
363      REAL zdqsch4fast(ngrid)    ! used only for fast model nogcm
364      REAL zdqch4fast(ngrid)    ! used only for fast model nogcm
365      REAL zdqscofast(ngrid)    ! used only for fast model nogcm
366      REAL zdqcofast(ngrid)    ! used only for fast model nogcm
367      REAL zdqflow(ngrid,nq)
368
369      REAL zdteuv(ngrid,nlayer)    ! (K/s)
370      REAL zdtconduc(ngrid,nlayer) ! (K/s)
371      REAL zdumolvis(ngrid,nlayer)
372      REAL zdvmolvis(ngrid,nlayer)
373      real zdqmoldiff(ngrid,nlayer,nq)
374
375      ! Haze relatated tendancies
376      REAL zdqhaze(ngrid,nlayer,nq)
377      REAL zdqprodhaze(ngrid,nq)
378      REAL zdqsprodhaze(ngrid)
379      REAL zdqhaze_col(ngrid)
380      REAL zdqphot_prec(ngrid,nlayer)
381      REAL zdqphot_ch4(ngrid,nlayer)
382      REAL zdqconv_prec(ngrid,nlayer)
383      REAL zdq_source(ngrid,nlayer,nq)
384      ! Fast Haze relatated tendancies
385      REAL fluxbot(ngrid)
386      REAL gradflux(ngrid)
387      REAL fluxlym_sol_bot(ngrid)      ! Solar flux Lyman alpha ph.m-2.s-1 reaching the surface
388      REAL fluxlym_ipm_bot(ngrid)      ! IPM (Interplanetary) flux Lyman alpha ph.m-2.s-1 reaching the surface
389      REAL flym_sol(ngrid)      ! Incident Solar flux Lyman alpha ph.m-2.s-1
390      REAL flym_ipm(ngrid)      ! Incident IPM (Interplanetary) flux Lyman alpha ph.m-2.s-1
391      REAL zfluxuv                     ! Lyman alpha flux at 1AU
392
393
394
395      REAL array_zero1(ngrid)
396      REAL array_zero2(ngrid,nlayer)
397
398      ! For Winds : (m/s/s)
399      real zdvadj(ngrid,nlayer), zduadj(ngrid,nlayer)       ! Convadj routine.
400      real zdumr(ngrid,nlayer), zdvmr(ngrid,nlayer)         ! Mass_redistribution routine.
401      real zdvdif(ngrid,nlayer), zdudif(ngrid,nlayer)       ! Turbdiff/vdifc routines.
402      real zdhdif(ngrid,nlayer)                             ! Turbdiff/vdifc routines.
403      real zdhadj(ngrid,nlayer)                             ! Convadj routine.
404      REAL zdvc(ngrid,nlayer),zduc(ngrid,nlayer)            ! condense_n2 routine.
405
406      ! For Pressure and Mass :
407      real zdmassmr(ngrid,nlayer) ! Atmospheric Mass tendency for mass_redistribution (kg_of_air/m2/s).
408      real zdmassmr_col(ngrid)    ! Atmospheric Column Mass tendency for mass_redistribution (kg_of_air/m2/s).
409      real zdpsrfmr(ngrid)        ! Pressure tendency for mass_redistribution routine (Pa/s).
410
411
412
413! Local variables for LOCAL CALCULATIONS:
414! ---------------------------------------
415      real zflubid(ngrid)
416      real zplanck(ngrid),zpopsk(ngrid,nlayer)
417      REAL zdum1(ngrid,nlayer)
418      REAL zdum2(ngrid,nlayer)
419      real ztim1,ztim2,ztim3, z1,z2
420      real ztime_fin
421      real zdh(ngrid,nlayer)
422      real gmplanet
423      real taux(ngrid),tauy(ngrid)
424
425
426! local variables for DIAGNOSTICS : (diagfi & stat)
427! -------------------------------------------------
428      real ps(ngrid)                                     ! Surface Pressure.
429      real zt(ngrid,nlayer)                              ! Atmospheric Temperature.
430      real zu(ngrid,nlayer),zv(ngrid,nlayer)             ! Zonal and Meridional Winds.
431      real zq(ngrid,nlayer,nq)                           ! Atmospheric Tracers.
432      real zdtadj(ngrid,nlayer)                          ! Convadj Diagnostic.
433      real zdtdyn(ngrid,nlayer)                          ! Dynamical Heating (K/s).
434      real zdudyn(ngrid,nlayer)                          ! Dynamical Zonal Wind tendency (m.s-2).
435
436      real reff(ngrid,nlayer)                       ! Effective dust radius (used if doubleq=T).
437      real vmr(ngrid,nlayer)                        ! volume mixing ratio
438      real time_phys
439
440      real ISR,ASR,OLR,GND,DYN,GSR,Ts1,Ts2,Ts3,TsS ! for Diagnostic.
441
442      real qcol(ngrid,nq) ! Tracer Column Mass (kg/m2).
443
444      !     Pluto adding variables
445      real vmr_ch4(ngrid)  ! vmr ch4
446      real vmr_co(ngrid)  ! vmr co
447      real rho(ngrid,nlayer) ! density
448      real zrho_ch4(ngrid,nlayer) ! density methane kg.m-3
449      real zrho_co(ngrid,nlayer) ! density CO kg.m-3
450      real zrho_haze(ngrid,nlayer) ! density haze kg.m-3
451      real zdqrho_photprec(ngrid,nlayer) !photolysis rate kg.m-3.s-1
452      real zq1temp_ch4(ngrid) !
453      real qsat_ch4(ngrid) !
454      real qsat_ch4_l1(ngrid) !
455!      CHARACTER(LEN=20) :: txt ! to temporarly store text for eddy tracers
456      real profmmr(ngrid,nlayer) ! fixed profile of haze if haze_proffix
457      real sensiblehf1(ngrid) ! sensible heat flux
458      real sensiblehf2(ngrid) ! sensible heat flux
459
460!     included by RW for H2O Manabe scheme
461      real rneb_man(ngrid,nlayer) ! H2O cloud fraction (moistadj).
462      real rneb_lsc(ngrid,nlayer) ! H2O cloud fraction (large scale).
463
464
465!     to test energy conservation (RW+JL)
466      real mass(ngrid,nlayer),massarea(ngrid,nlayer)
467      real dEtot, dEtots, AtmToSurf_TurbFlux
468      real,save :: dEtotSW, dEtotsSW, dEtotLW, dEtotsLW
469!$OMP THREADPRIVATE(dEtotSW, dEtotsSW, dEtotLW, dEtotsLW)
470
471!JL12 conservation test for mean flow kinetic energy has been disabled temporarily
472
473      real dtmoist_max,dtmoist_min
474      real dItot, dItot_tmp, dVtot, dVtot_tmp
475
476
477      real dWtot, dWtot_tmp, dWtots, dWtots_tmp
478
479      real psat_tmp ! AF24: to remove?
480
481      real qsat_generic(ngrid,nlayer,nq) ! generic condensable tracers (GCS) specific concentration at saturation (kg/kg_of_air).
482      real RH_generic(ngrid,nlayer,nq)   ! generic condensable tracers (GCS) Relative humidity.
483      real rneb_generic(ngrid,nlayer,nq) ! GCS cloud fraction (generic condensation).
484      real psat_tmp_generic
485      real, save :: metallicity ! metallicity of planet --- is not used here, but necessary to call function Psat_generic
486!$OMP THREADPRIVATE(metallicity)
487
488      real reffrad_generic_zeros_for_wrf(ngrid,nlayer) !  !!! this is temporary, it is only a list of zeros, it will be replaced when a generic aerosol will be implemented
489
490      ! For Clear Sky Case. (AF24: deleted)
491
492      real nconsMAX, vdifcncons(ngrid), cadjncons(ngrid) ! Vdfic water conservation test. By RW
493
494      real muvar(ngrid,nlayer+1) ! For Runaway Greenhouse 1D study. By RW
495
496      real,save,allocatable :: reffcol(:,:)
497!$OMP THREADPRIVATE(reffcol)
498
499!  Non-oro GW tendencies
500      REAL d_u_hin(ngrid,nlayer), d_v_hin(ngrid,nlayer)
501      REAL d_t_hin(ngrid,nlayer)
502!  Diagnostics 2D of gw_nonoro
503      REAL zustrhi(ngrid), zvstrhi(ngrid)
504
505
506      real :: tsurf2(ngrid)
507!!      real :: flux_o(ngrid),flux_g(ngrid)
508      real :: flux_g(ngrid)
509      real :: flux_sens_lat(ngrid)
510      real :: qsurfint(ngrid,nq)
511#ifdef MESOSCALE
512      REAL :: lsf_dt(nlayer)
513      REAL :: lsf_dq(nlayer)
514#endif
515
516      ! flags to trigger extra sanity checks
517      logical, save ::  check_physics_inputs=.false.
518      logical, save ::  check_physics_outputs=.false.
519!$OPM THREADPRIVATE(check_physics_inputs,check_physics_outputs)
520
521      ! Misc
522      character*2 :: str2
523      character(len=10) :: tmp1
524      character(len=10) :: tmp2
525!==================================================================================================
526
527! -----------------
528! I. INITIALISATION
529! -----------------
530
531! --------------------------------
532! I.1   First Call Initialisation.
533! --------------------------------
534      if (firstcall) then
535         call getin_p("check_physics_inputs", check_physics_inputs)
536         call getin_p("check_physics_outputs", check_physics_outputs)
537
538         ! Allocate saved arrays (except for 1D model, where this has already
539         ! been done)
540#ifndef MESOSCALE
541         if (ngrid>1) call phys_state_var_init(nq)
542#endif
543
544!        Variables set to 0
545!        ~~~~~~~~~~~~~~~~~~
546         dtrad(:,:) = 0.0
547         fluxrad(:) = 0.0
548         tau_col(:) = 0.0
549         zdtsw(:,:) = 0.0
550         zdtlw(:,:) = 0.0
551
552!        Initialize tracer names, indexes and properties.
553!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
554         IF (.NOT.ALLOCATED(noms)) ALLOCATE(noms(nq)) ! (because noms is an argument of physdem1 whether or not tracer is on)
555         if (tracer) then
556            call initracer(ngrid,nq)
557            ! if(photochem) then !AF24: removed
558         endif
559!        Initialize aerosol indexes.
560!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
561         ! call iniaerosol
562         ! allocate related local arrays
563         ! (need be allocated instead of automatic because of "naerkind")
564         allocate(aerosol(ngrid,nlayer,naerkind))
565         allocate(reffcol(ngrid,naerkind))
566
567#ifdef CPP_XIOS
568         ! Initialize XIOS context
569         write(*,*) "physiq: call wxios_context_init"
570         CALL wxios_context_init
571#endif
572
573!        Read 'startfi.nc' file.
574!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
575#ifndef MESOSCALE
576         call phyetat0(startphy_file,                                 &
577                       ngrid,nlayer,"startfi.nc",0,0,nsoilmx,nq,      &
578                       day_ini,time_phys,tsurf,tsoil,emis,q2,qsurf)
579
580#else
581
582         day_ini = pday
583#endif
584
585#ifndef MESOSCALE
586         if (.not.startphy_file) then
587           ! additionnal "academic" initialization of physics
588           if (is_master) write(*,*) "Physiq: initializing tsurf(:) to pt(:,1) !!"
589           tsurf(:)=pt(:,1)
590           if (is_master) write(*,*) "Physiq: initializing tsoil(:) to pt(:,1) !!"
591           do isoil=1,nsoilmx
592             tsoil(1:ngrid,isoil)=tsurf(1:ngrid)
593           enddo
594           if (is_master) write(*,*) "Physiq: initializing day_ini to pday !"
595           day_ini=pday
596         endif
597#endif
598         if (pday.ne.day_ini) then
599             write(*,*) "ERROR in physiq.F90:"
600             write(*,*) "bad synchronization between physics and dynamics"
601             write(*,*) "dynamics day: ",pday
602             write(*,*) "physics day:  ",day_ini
603             stop
604         endif
605
606         write (*,*) 'In physiq day_ini =', day_ini
607
608!        Initialize albedo calculation.
609!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
610         albedo(:,:)=0.0
611         albedo_bareground(:)=0.0
612         albedo_snow_SPECTV(:)=0.0
613         albedo_n2_ice_SPECTV(:)=0.0
614         call surfini(ngrid,nq,qsurf,albedo,albedo_bareground,albedo_snow_SPECTV,albedo_n2_ice_SPECTV)
615
616!        Initialize orbital calculation.
617!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
618         call iniorbit(apoastr,periastr,year_day,peri_day,obliquit)
619
620
621!        Initialize soil.
622!        ~~~~~~~~~~~~~~~~
623         if (callsoil) then
624            call soil(ngrid,nsoilmx,firstcall,lastcall,inertiedat, &
625                      ptimestep,tsurf,tsoil,capcal,fluxgrd)
626         else ! else of 'callsoil'.
627            print*,'WARNING! Thermal conduction in the soil turned off'
628            capcal(:)=1.e6
629            fluxgrd(:)=intheat
630            print*,'Flux from ground = ',intheat,' W m^-2'
631         endif ! end of 'callsoil'.
632
633         icount=1
634
635!        Initialize surface history variable.
636!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
637         qsurf_hist(:,:)=qsurf(:,:)
638
639!!         call WriteField_phy("post_qsurf_hist_qsurf",qsurf(1:ngrid,igcm_h2o_vap),1)
640
641!        Initialize variable for dynamical heating and zonal wind tendency diagnostic
642!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
643         ztprevious(:,:)=pt(:,:)
644         zuprevious(:,:)=pu(:,:)
645
646!        Set temperature just above condensation temperature (for Early Mars)
647!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
648         if(nearn2cond) then
649            write(*,*)' WARNING! Starting at Tcond+1K'
650            do l=1, nlayer
651               do ig=1,ngrid
652                  pdt(ig,l)= ((-3167.8)/(log(.01*pplay(ig,l))-23.23)+4     &
653                      -pt(ig,l)) / ptimestep
654               enddo
655            enddo
656         endif
657
658         if(meanOLR)then
659            call system('rm -f rad_bal.out') ! to record global radiative balance.
660            call system('rm -f tem_bal.out') ! to record global mean/max/min temperatures.
661            call system('rm -f h2o_bal.out') ! to record global hydrological balance.
662         endif
663
664
665         !! Initialize variables for water cycle ! AF24: removed
666
667!        Set metallicity for GCS
668!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
669         metallicity=0.0 ! default value --- is not used here but necessary to call function Psat_generic
670         call getin_p("metallicity",metallicity) ! --- is not used here but necessary to call function Psat_generic
671
672!        Set some parameters for the thermal plume model !AF24: removed
673!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
674
675#ifndef MESOSCALE
676         if (ngrid.ne.1) then ! Note : no need to create a restart file in 1d.
677            call physdem0("restartfi.nc",longitude,latitude,nsoilmx,ngrid,nlayer,nq, &
678                         ptimestep,pday+nday,time_phys,cell_area,          &
679                         albedo_bareground,inertiedat,zmea,zstd,zsig,zgam,zthe)
680         endif
681
682!!         call WriteField_phy("post_physdem_qsurf",qsurf(1:ngrid,igcm_h2o_vap),1)
683#endif
684         if (corrk) then
685            ! We initialise the spectral grid here instead of
686            ! at firstcall of callcorrk so we can output XspecIR, XspecVI
687            ! when using Dynamico
688            print*, "physiq_mod: Correlated-k data base folder:",trim(datadir)
689            call getin_p("corrkdir",corrkdir)
690            print*,"corrkdir = ", corrkdir
691            write (tmp1, '(i4)') L_NSPECTI
692            write (tmp2, '(i4)') L_NSPECTV
693            banddir=trim(trim(adjustl(tmp1))//'x'//trim(adjustl(tmp2)))
694            banddir=trim(trim(adjustl(corrkdir))//'/'//trim(adjustl(banddir)))
695            call setspi !Basic infrared properties.
696            call setspv ! Basic visible properties.
697            call sugas_corrk       ! Set up gaseous absorption properties.
698            if (aerohaze) then
699               call suaer_corrk       ! Set up aerosol optical properties.
700            endif
701         endif
702
703!!         call WriteField_phy("post_corrk_firstcall_qsurf",qsurf(1:ngrid,igcm_h2o_vap),1)
704         ! XIOS outputs
705#ifdef CPP_XIOS
706
707         write(*,*) "physiq: call initialize_xios_output"
708         call initialize_xios_output(pday,ptime,ptimestep,daysec, &
709                                     year_day,presnivs,pseudoalt,WNOI,WNOV)
710#endif
711
712!!         call WriteField_phy("post_xios_qsurf",qsurf(1:ngrid,igcm_h2o_vap),1)
713
714         write(*,*) "physiq: end of firstcall"
715      endif ! end of 'firstcall'
716
717!!      call WriteField_phy("post_firstcall_qsurf",qsurf(1:ngrid,igcm_h2o_vap),1)
718!!      call writediagfi(ngrid,"firstcall_post_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
719
720      if (check_physics_inputs) then
721         !check the validity of input fields coming from the dynamics
722         call check_physics_fields("begin physiq:", pt, pu, pv, pplev, pq)
723      endif
724
725!      call writediagfi(ngrid,"pre_physical_rnat"," "," ",2,rnat)
726!      call writediagfi(ngrid,"pre_physical_capcal"," "," ",2,capcal)
727
728! ------------------------------------------------------
729! I.2   Initializations done at every physical timestep:
730! ------------------------------------------------------
731
732#ifdef CPP_XIOS
733      ! update XIOS time/calendar
734      call update_xios_timestep
735#endif
736
737      ! Initialize various variables
738      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
739
740      if ( .not.nearn2cond ) then
741         pdt(1:ngrid,1:nlayer) = 0.0
742      endif
743      zdtsurf(1:ngrid)      = 0.0
744      pdq(1:ngrid,1:nlayer,1:nq) = 0.0
745      dqsurf(1:ngrid,1:nq)= 0.0
746      pdu(1:ngrid,1:nlayer) = 0.0
747      pdv(1:ngrid,1:nlayer) = 0.0
748      pdpsrf(1:ngrid)       = 0.0
749      zflubid(1:ngrid)      = 0.0
750      flux_sens_lat(1:ngrid) = 0.0
751      taux(1:ngrid) = 0.0
752      tauy(1:ngrid) = 0.0
753
754      zday=pday+ptime ! Compute time, in sols (and fraction thereof).
755
756      ! Compute Stellar Longitude (Ls), and orbital parameters.
757      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
758      if (season) then
759         call stellarlong(zday,zls)
760      else
761         call stellarlong(noseason_day,zls)
762      end if
763
764
765!     Get Lyman alpha flux at specific Ls
766      if (haze) then
767         call lymalpha(zls,zfluxuv)
768         print*, 'Haze lyman-alpha zls,zfluxuv=',zls,zfluxuv
769      end if
770
771
772      IF (triton) then
773         CALL orbitetriton(zls,zday,dist_star,declin)
774      ELSE
775         call orbite(zls,dist_star,declin,right_ascen)
776      ENDIF
777
778
779      if (diurnal) then
780              zlss=-2.*pi*(zday-.5)
781      else if(diurnal .eqv. .false.) then
782              zlss=9999.
783      endif
784
785
786      glat(:) = g !AF24: removed oblateness
787
788      ! Compute geopotential between layers.
789      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
790      zzlay(1:ngrid,1:nlayer)=pphi(1:ngrid,1:nlayer)
791      do l=1,nlayer
792         zzlay(1:ngrid,l)= zzlay(1:ngrid,l)/glat(1:ngrid)
793      enddo
794
795      zzlev(1:ngrid,1)=0.
796
797      do l=2,nlayer
798         do ig=1,ngrid
799            z1=(pplay(ig,l-1)+pplev(ig,l))/(pplay(ig,l-1)-pplev(ig,l))
800            z2=(pplev(ig,l)+pplay(ig,l))/(pplev(ig,l)-pplay(ig,l))
801            zzlev(ig,l)=(z1*zzlay(ig,l-1)+z2*zzlay(ig,l))/(z1+z2)
802         enddo
803      enddo
804
805      !Altitude of top interface (nlayer+1), using the thicknesss of the level below the top one. LT22
806
807      zzlev(1:ngrid,nlayer+1) = 2*zzlev(1:ngrid,nlayer)-zzlev(1:ngrid,nlayer-1)
808
809      ! Compute potential temperature
810      ! Note : Potential temperature calculation may not be the same in physiq and dynamic...
811      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
812      do l=1,nlayer
813         do ig=1,ngrid
814            zpopsk(ig,l)=(pplay(ig,l)/pplev(ig,1))**rcp
815            zh(ig,l)=pt(ig,l)/zpopsk(ig,l)
816            mass(ig,l)  = (pplev(ig,l) - pplev(ig,l+1))/glat(ig)
817            massarea(ig,l)=mass(ig,l)*cell_area(ig)
818         enddo
819      enddo
820
821     ! Compute vertical velocity (m/s) from vertical mass flux
822     ! w = F / (rho*area) and rho = P/(r*T)
823     ! But first linearly interpolate mass flux to mid-layers
824      if (.not.fast) then
825       do l=1,nlayer-1
826         pw(1:ngrid,l)=0.5*(flxw(1:ngrid,l)+flxw(1:ngrid,l+1))
827       enddo
828       pw(1:ngrid,nlayer)=0.5*flxw(1:ngrid,nlayer) ! since flxw(nlayer+1)=0
829       do l=1,nlayer
830         pw(1:ngrid,l)=(pw(1:ngrid,l)*r*pt(1:ngrid,l)) /  &
831                       (pplay(1:ngrid,l)*cell_area(1:ngrid))
832       enddo
833       ! omega in Pa/s
834       do l=1,nlayer-1
835         omega(1:ngrid,l)=0.5*(flxw(1:ngrid,l)+flxw(1:ngrid,l+1))
836       enddo
837       omega(1:ngrid,nlayer)=0.5*flxw(1:ngrid,nlayer) ! since flxw(nlayer+1)=0
838       do l=1,nlayer
839         omega(1:ngrid,l)=g*omega(1:ngrid,l)/cell_area(1:ngrid)
840       enddo
841      endif
842!---------------------------------
843! II. Compute radiative tendencies
844!---------------------------------
845!     Saving qsurf to compute paleo flux condensation/sublimation
846      DO iq=1, nq
847         DO ig=1,ngrid
848             IF (qsurf(ig,iq).lt.0.) then
849                qsurf(ig,iq)=0.
850             ENDIF
851             qsurf1(ig,iq)=qsurf(ig,iq)
852         ENDDO
853      ENDDO
854
855
856      ! Compute local stellar zenith angles
857      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
858      if (diurnal) then
859         ztim1=SIN(declin)
860         ztim2=COS(declin)*COS(2.*pi*(zday-.5))
861         ztim3=-COS(declin)*SIN(2.*pi*(zday-.5))
862
863         call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
864                        ztim1,ztim2,ztim3,mu0,fract)
865      else if(diurnal .eqv. .false.) then
866
867         call mucorr(ngrid,declin,latitude,mu0,fract,10000.,rad)
868         ! WARNING: this function appears not to work in 1D
869
870         if ((ngrid.eq.1).and.(global1d)) then ! Fixed zenith angle 'szangle' in 1D simulations w/ globally-averaged sunlight.
871            mu0 = cos(pi*szangle/180.0)
872         endif
873
874      endif
875
876      ! AF24: TODO insert surfprop for pluto & triton around here
877
878      if (callrad) then
879         if( mod(icount-1,iradia).eq.0.or.lastcall) then
880
881            ! Eclipse incoming sunlight !AF24: removed
882
883!!            call writediagfi(ngrid,"corrk_pre_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
884!!            call writediagfi(ngrid,"corrk_pre_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
885
886
887            if (corrk) then
888
889! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
890! II.a Call correlated-k radiative transfer scheme
891! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
892               if(kastprof)then
893                  print*,'kastprof should not = true here'
894                  call abort
895               endif
896               ! if(water) then !AF24: removed
897
898               if(generic_condensation) then
899                  do iq=1,nq
900
901                     call generic_tracer_index(nq,iq,igcm_generic_vap,igcm_generic_ice,call_ice_vap_generic)
902
903                     if (call_ice_vap_generic) then ! to call only one time the ice/vap pair of a tracer
904
905                        epsi_generic=constants_epsi_generic(iq)
906
907                        muvar(1:ngrid,1:nlayer)=mugaz/(1.e0+(1.e0/epsi_generic-1.e0)*pq(1:ngrid,1:nlayer,igcm_generic_vap))
908                        muvar(1:ngrid,nlayer+1)=mugaz/(1.e0+(1.e0/epsi_generic-1.e0)*pq(1:ngrid,nlayer,igcm_generic_vap))
909
910                     endif
911                  end do ! do iq=1,nq loop on tracers
912               ! take into account generic condensable specie (GCS) effect on mean molecular weight
913
914               else
915                  muvar(1:ngrid,1:nlayer+1)=mugaz
916               endif
917
918               ! if(ok_slab_ocean) then !AF24: removed
919
920               ! standard callcorrk
921               ! clearsky=.false.
922               call callcorrk(ngrid,nlayer,pq,nq,qsurf,                           &
923                              albedo,albedo_equivalent,emis,mu0,pplev,pplay,pt,   &
924                              tsurf,fract,dist_star,aerosol,muvar,                &
925                              zdtlw,zdtsw,fluxsurf_lw,fluxsurf_sw,                &
926                              fluxsurfabs_sw,fluxtop_lw,                          &
927                              fluxabs_sw,fluxtop_dn,OLR_nu,OSR_nu,GSR_nu,         &
928                              int_dtaui,int_dtauv,                                &
929                              tau_col,cloudfrac,totcloudfrac,                     &
930                              .false.,firstcall,lastcall)
931
932                !GG (feb2021): Option to "artificially" decrease the raditive time scale in
933                !the deep atmosphere  press > 0.1 bar. Suggested by MT.
934                !! COEFF_RAD to be "tuned" to facilitate convergence of tendency
935
936                !coeff_rad=0.   ! 0 values, it doesn't accelerate the convergence
937                !coeff_rad=0.5
938                !coeff_rad=1.
939                !do l=1, nlayer
940                !  do ig=1,ngrid
941                !    if(pplay(ig,l).ge.1.d4) then
942                !      zdtsw(ig,l)=zdtsw(ig,l)*(pplay(ig,l)/1.d4)**coeff_rad
943                !      zdtlw(ig,l)=zdtlw(ig,l)*(pplay(ig,l)/1.d4)**coeff_rad
944                !    endif
945                !  enddo
946                !enddo
947
948                ! AF24: removed CLFvarying Option
949
950               ! if(ok_slab_ocean) then
951               !    tsurf(:)=tsurf2(:)
952               ! endif
953
954
955               ! Radiative flux from the sky absorbed by the surface (W.m-2).
956               GSR=0.0
957               fluxrad_sky(1:ngrid)=emis(1:ngrid)*fluxsurf_lw(1:ngrid)+fluxsurfabs_sw(1:ngrid)
958
959                            !if(noradsurf)then ! no lower surface; SW flux just disappears
960                            !   GSR = SUM(fluxsurf_sw(1:ngrid)*cell_area(1:ngrid))/totarea
961                            !   fluxrad_sky(1:ngrid)=emis(1:ngrid)*fluxsurf_lw(1:ngrid)
962                            !   print*,'SW lost in deep atmosphere = ',GSR,' W m^-2'
963                            !endif
964
965               ! Net atmospheric radiative heating rate (K.s-1)
966               dtrad(1:ngrid,1:nlayer)=zdtsw(1:ngrid,1:nlayer)+zdtlw(1:ngrid,1:nlayer)
967
968               ! Late initialization of the Ice Spectral Albedo. We needed the visible bands to do that !
969               if (firstcall .and. albedo_spectral_mode) then
970                  call spectral_albedo_calc(albedo_snow_SPECTV)
971               endif
972
973! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
974! II.b Call Newtonian cooling scheme !AF24: removed
975! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
976            else
977! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
978! II.! Atmosphere has no radiative effect
979! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
980               fluxtop_dn(1:ngrid)  = fract(1:ngrid)*mu0(1:ngrid)*Fat1AU/dist_star**2
981               if(ngrid.eq.1)then ! / by 4 globally in 1D case...
982                  fluxtop_dn(1)  = fract(1)*Fat1AU/dist_star**2/2.0
983               endif
984               fluxsurf_sw(1:ngrid) = fluxtop_dn(1:ngrid)
985               print*,'------------WARNING---WARNING------------' ! by MT2015.
986               print*,'You are in corrk=false mode, '
987               print*,'and the surface albedo is taken equal to the first visible spectral value'
988
989               albedo_equivalent(1:ngrid)=albedo(1:ngrid,1)
990               fluxsurfabs_sw(1:ngrid) = fluxtop_dn(1:ngrid)*(1.-albedo(1:ngrid,1))
991               fluxabs_sw(1:ngrid)=fluxsurfabs_sw(1:ngrid)
992               fluxrad_sky(1:ngrid)    = fluxsurfabs_sw(1:ngrid)
993               fluxtop_lw(1:ngrid)  = emis(1:ngrid)*sigma*tsurf(1:ngrid)**4
994
995               dtrad(1:ngrid,1:nlayer)=0.0 ! no atmospheric radiative heating
996
997            endif ! end of corrk
998
999         endif ! of if(mod(icount-1,iradia).eq.0)
1000
1001
1002         ! Transformation of the radiative tendencies
1003         ! ------------------------------------------
1004         zplanck(1:ngrid)=tsurf(1:ngrid)*tsurf(1:ngrid)
1005         zplanck(1:ngrid)=emis(1:ngrid)*sigma*zplanck(1:ngrid)*zplanck(1:ngrid)
1006         fluxrad(1:ngrid)=fluxrad_sky(1:ngrid)-zplanck(1:ngrid)
1007         pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+dtrad(1:ngrid,1:nlayer)
1008
1009         ! Test of energy conservation
1010         !----------------------------
1011         if(enertest)then
1012            call planetwide_sumval(cpp*massarea(:,:)*zdtsw(:,:)/totarea_planet,dEtotSW)
1013            call planetwide_sumval(cpp*massarea(:,:)*zdtlw(:,:)/totarea_planet,dEtotLW)
1014            !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
1015            call planetwide_sumval(fluxsurfabs_sw(:)*cell_area(:)/totarea_planet,dEtotsSW) !JL13 carefull, albedo can have changed since the last time we called corrk
1016            call planetwide_sumval((fluxsurf_lw(:)*emis(:)-zplanck(:))*cell_area(:)/totarea_planet,dEtotsLW)
1017            dEzRadsw(:,:)=cpp*mass(:,:)*zdtsw(:,:)
1018            dEzRadlw(:,:)=cpp*mass(:,:)*zdtlw(:,:)
1019            if (is_master) then
1020               print*,'---------------------------------------------------------------'
1021               print*,'In corrk SW atmospheric heating       =',dEtotSW,' W m-2'
1022               print*,'In corrk LW atmospheric heating       =',dEtotLW,' W m-2'
1023               print*,'atmospheric net rad heating (SW+LW)   =',dEtotLW+dEtotSW,' W m-2'
1024               print*,'In corrk SW surface heating           =',dEtotsSW,' W m-2'
1025               print*,'In corrk LW surface heating           =',dEtotsLW,' W m-2'
1026               print*,'surface net rad heating (SW+LW)       =',dEtotsLW+dEtotsSW,' W m-2'
1027            endif
1028         endif ! end of 'enertest'
1029
1030      endif ! of if (callrad)
1031
1032!!      call writediagfi(ngrid,"vdifc_pre_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1033!!      call writediagfi(ngrid,"vdifc_pre_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
1034
1035
1036!  --------------------------------------------
1037!  III. Vertical diffusion (turbulent mixing) :
1038!  --------------------------------------------
1039
1040      if (calldifv) then
1041
1042         zflubid(1:ngrid)=fluxrad(1:ngrid)+fluxgrd(1:ngrid)
1043
1044         if (UseVdifcPlutold) then
1045            zflubid(1:ngrid)=fluxrad(1:ngrid)+fluxgrd(1:ngrid)
1046            zdum1(:,:) = 0
1047            zdum2(:,:) = 0
1048            zdh(:,:)=pdt(:,:)/zpopsk(:,:)
1049
1050            ! Calling vdif (Martian version WITH N2 condensation)
1051            CALL vdifc_pluto(ngrid,nlayer,nq,zpopsk,       &
1052                    ptimestep,capcal,lwrite,         &
1053                    pplay,pplev,zzlay,zzlev,z0,      &
1054                    pu,pv,zh,pq,pt,tsurf,emis,qsurf, &
1055                    zdum1,zdum2,zdh,pdq,pdt,zflubid, &
1056                    zdudif,zdvdif,zdhdif,zdtsdif,q2, &
1057                    zdqdif,zdqsdif,qsat_ch4,qsat_ch4_l1) !,zq1temp_ch4,qsat_ch4)
1058
1059            pdv(1:ngrid,1:nlayer)=pdv(1:ngrid,1:nlayer)+zdvdif(1:ngrid,1:nlayer)
1060            pdu(1:ngrid,1:nlayer)=pdu(1:ngrid,1:nlayer)+zdudif(1:ngrid,1:nlayer)
1061            pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+zdhdif(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer)
1062
1063            zdtdif(1:ngrid,1:nlayer)=zdhdif(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1064            zdtsurf(1:ngrid)=zdtsurf(1:ngrid)+zdtsdif(1:ngrid)
1065
1066            bcond=1./tcond1p4Pa
1067            acond=r/lw_n2
1068
1069            if (tracer) then
1070               pdq(1:ngrid,1:nlayer,1:nq)=pdq(1:ngrid,1:nlayer,1:nq)+ zdqdif(1:ngrid,1:nlayer,1:nq)
1071               dqsurf(1:ngrid,1:nq)=dqsurf(1:ngrid,1:nq) + zdqsdif(1:ngrid,1:nq)
1072            end if ! of if (tracer)
1073
1074            !------------------------------------------------------------------
1075            ! test methane conservation
1076            !         if(methane)then
1077            !           call testconserv(ngrid,nlayer,nq,igcm_ch4_gas,igcm_ch4_ice,
1078            !     &          ptimestep,pplev,zdqdif,zdqsdif,'CH4',' vdifc ')
1079            !         endif  ! methane
1080            !------------------------------------------------------------------
1081            ! test CO conservation
1082            !         if(carbox)then
1083            !           call testconserv(ngrid,nlayer,nq,igcm_co_gas,igcm_co_ice,
1084            !     &          ptimestep,pplev,zdqdif,zdqsdif,'CO ',' vdifc ')
1085            !         endif  ! carbox
1086            !------------------------------------------------------------------
1087
1088         ! JL12 the following if test is temporarily there to allow us to compare the old vdifc with turbdiff.
1089         else if (UseTurbDiff) then
1090
1091            call turbdiff(ngrid,nlayer,nq,                  &
1092                          ptimestep,capcal,                      &
1093                          pplay,pplev,zzlay,zzlev,z0,            &
1094                          pu,pv,pt,zpopsk,pq,tsurf,emis,qsurf,   &
1095                          pdt,pdq,zflubid,                       &
1096                          zdudif,zdvdif,zdtdif,zdtsdif,          &
1097                          sensibFlux,q2,zdqdif,zdqevap,zdqsdif,  &
1098                          taux,tauy)
1099
1100         else ! if (UseVdifcPlutold) .and. (UseTurbDiff)
1101
1102            zdh(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)/zpopsk(1:ngrid,1:nlayer)
1103
1104            call vdifc(ngrid,nlayer,nq,zpopsk,           &
1105                       ptimestep,capcal,lwrite,               &
1106                       pplay,pplev,zzlay,zzlev,z0,            &
1107                       pu,pv,zh,pq,tsurf,emis,qsurf,          &
1108                       zdh,pdq,zflubid,                       &
1109                       zdudif,zdvdif,zdhdif,zdtsdif,          &
1110                       sensibFlux,q2,zdqdif,zdqsdif)
1111
1112            zdtdif(1:ngrid,1:nlayer)=zdhdif(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1113            zdqevap(1:ngrid,1:nlayer)=0.
1114
1115         end if !end of 'UseTurbDiff'
1116
1117         zdtsurf(1:ngrid)=zdtsurf(1:ngrid)+zdtsdif(1:ngrid)
1118
1119         !!! this is always done, except for turbulence-resolving simulations
1120         if (.not. turb_resolved) then
1121           pdv(1:ngrid,1:nlayer)=pdv(1:ngrid,1:nlayer)+zdvdif(1:ngrid,1:nlayer)
1122           pdu(1:ngrid,1:nlayer)=pdu(1:ngrid,1:nlayer)+zdudif(1:ngrid,1:nlayer)
1123           pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+zdtdif(1:ngrid,1:nlayer)
1124         endif
1125
1126         ! if(ok_slab_ocean)then !AF24: removed
1127         !    flux_sens_lat(1:ngrid)=(zdtsdif(1:ngrid)*capcal(1:ngrid)-fluxrad(1:ngrid))
1128         ! endif
1129
1130!!         call writediagfi(ngrid,"vdifc_post_zdqsdif"," "," ",2,zdqsdif(1:ngrid,igcm_h2o_vap))
1131
1132         if (tracer) then
1133           pdq(1:ngrid,1:nlayer,1:nq)=pdq(1:ngrid,1:nlayer,1:nq)+ zdqdif(1:ngrid,1:nlayer,1:nq)
1134           dqsurf(1:ngrid,1:nq)=dqsurf(1:ngrid,1:nq) + zdqsdif(1:ngrid,1:nq)
1135         end if ! of if (tracer)
1136
1137!!         call writediagfi(ngrid,"vdifc_post_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1138!!         call writediagfi(ngrid,"vdifc_post_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
1139
1140         ! test energy conservation
1141         !-------------------------
1142         if(enertest)then
1143
1144            dEzdiff(:,:)=cpp*mass(:,:)*zdtdif(:,:)
1145            do ig = 1, ngrid
1146               dEdiff(ig)=SUM(dEzdiff (ig,:))+ sensibFlux(ig)! subtract flux to the ground
1147               dEzdiff(ig,1)= dEzdiff(ig,1)+ sensibFlux(ig)! subtract flux to the ground
1148            enddo
1149
1150            call planetwide_sumval(dEdiff(:)*cell_area(:)/totarea_planet,dEtot)
1151            dEdiffs(:)=capcal(:)*zdtsdif(:)-zflubid(:)-sensibFlux(:)
1152            call planetwide_sumval(dEdiffs(:)*cell_area(:)/totarea_planet,dEtots)
1153            call planetwide_sumval(sensibFlux(:)*cell_area(:)/totarea_planet,AtmToSurf_TurbFlux)
1154
1155            if (is_master) then
1156
1157               if (UseTurbDiff) then
1158                         print*,'In TurbDiff sensible flux (atm=>surf) =',AtmToSurf_TurbFlux,' W m-2'
1159                         print*,'In TurbDiff non-cons atm nrj change   =',dEtot,' W m-2'
1160                  print*,'In TurbDiff (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1161               else
1162                         print*,'In vdifc sensible flux (atm=>surf)    =',AtmToSurf_TurbFlux,' W m-2'
1163                         print*,'In vdifc non-cons atm nrj change      =',dEtot,' W m-2'
1164                         print*,'In vdifc (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1165               end if
1166            endif ! end of 'is_master'
1167
1168         ! JL12 : note that the black body radiative flux emitted by the surface has been updated by the implicit scheme but not given back elsewhere.
1169         endif ! end of 'enertest'
1170
1171
1172         ! ! Test water conservation. !AF24: removed
1173
1174      else ! calldifv
1175
1176         ! if(.not.newtonian)then
1177            zdtsurf(1:ngrid) = zdtsurf(1:ngrid) + (fluxrad(1:ngrid) + fluxgrd(1:ngrid))/capcal(1:ngrid)
1178
1179      endif ! end of 'calldifv'
1180
1181
1182!-------------------
1183!   IV. Convection :
1184!-------------------
1185
1186! ~~~~~~~~~~~~~~~~~~~~~~~~~~
1187! IV.a Thermal plume model : AF24: removed
1188! ~~~~~~~~~~~~~~~~~~~~~~~~~~
1189
1190! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1191! IV.b Dry convective adjustment :
1192! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1193
1194      if(calladj) then
1195
1196         zdh(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)/zpopsk(1:ngrid,1:nlayer)
1197         zduadj(1:ngrid,1:nlayer)=0.0
1198         zdvadj(1:ngrid,1:nlayer)=0.0
1199         zdhadj(1:ngrid,1:nlayer)=0.0
1200
1201
1202         call convadj(ngrid,nlayer,nq,ptimestep,            &
1203                      pplay,pplev,zpopsk,                   &
1204                      pu,pv,zh,pq,                          &
1205                      pdu,pdv,zdh,pdq,                      &
1206                      zduadj,zdvadj,zdhadj,                 &
1207                      zdqadj)
1208
1209         pdu(1:ngrid,1:nlayer) = pdu(1:ngrid,1:nlayer) + zduadj(1:ngrid,1:nlayer)
1210         pdv(1:ngrid,1:nlayer) = pdv(1:ngrid,1:nlayer) + zdvadj(1:ngrid,1:nlayer)
1211         pdt(1:ngrid,1:nlayer)    = pdt(1:ngrid,1:nlayer) + zdhadj(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer)
1212         zdtadj(1:ngrid,1:nlayer) = zdhadj(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1213
1214         if(tracer) then
1215            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqadj(1:ngrid,1:nlayer,1:nq)
1216         end if
1217
1218         ! Test energy conservation
1219         if(enertest)then
1220            call planetwide_sumval(cpp*massarea(:,:)*zdtadj(:,:)/totarea_planet,dEtot)
1221            if (is_master) print*,'In convadj atmospheric energy change  =',dEtot,' W m-2'
1222         endif
1223
1224         ! ! Test water conservation !AF24: removed
1225
1226      endif ! end of 'calladj'
1227!----------------------------------------------
1228!   Non orographic Gravity Waves: AF24: removed
1229!---------------------------------------------
1230
1231!-----------------------------------------------
1232!   V. Nitrogen condensation-sublimation :
1233!-----------------------------------------------
1234
1235      if (n2cond) then
1236
1237         if (.not.tracer) then
1238            print*,'We need a N2 ice tracer to condense N2'
1239            call abort
1240         endif
1241
1242         call condense_n2(ngrid,nlayer,nq,ptimestep,                    &
1243                           capcal,pplay,pplev,tsurf,pt,                 &
1244                           pphi,pdt,pdu,pdv,zdtsurf,pu,pv,pq,pdq,       &
1245                           qsurf(1,igcm_n2),albedo,emis,                &
1246                           zdtc,zdtsurfc,pdpsrf,zduc,zdvc,              &
1247                           zdqc,zdqsc(1,igcm_n2))
1248
1249         pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+zdtc(1:ngrid,1:nlayer)
1250         pdv(1:ngrid,1:nlayer) = pdv(1:ngrid,1:nlayer)+zdvc(1:ngrid,1:nlayer)
1251         pdu(1:ngrid,1:nlayer) = pdu(1:ngrid,1:nlayer)+zduc(1:ngrid,1:nlayer)
1252         zdtsurf(1:ngrid)      = zdtsurf(1:ngrid) + zdtsurfc(1:ngrid)
1253
1254         pdq(1:ngrid,1:nlayer,1:nq)   = pdq(1:ngrid,1:nlayer,1:nq)+ zdqc(1:ngrid,1:nlayer,1:nq)
1255         dqsurf(1:ngrid,igcm_n2) = dqsurf(1:ngrid,igcm_n2) + zdqsc(1:ngrid,igcm_n2)
1256
1257!!         call writediagfi(ngrid,"condense_n2_post_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1258!!         call writediagfi(ngrid,"condense_n2_post_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
1259
1260         ! test energy conservation
1261         if(enertest)then
1262            call planetwide_sumval(cpp*massarea(:,:)*zdtc(:,:)/totarea_planet,dEtot)
1263            call planetwide_sumval(capcal(:)*zdtsurfc(:)*cell_area(:)/totarea_planet,dEtots)
1264            if (is_master) then
1265               print*,'In n2cloud atmospheric energy change   =',dEtot,' W m-2'
1266               print*,'In n2cloud surface energy change       =',dEtots,' W m-2'
1267            endif
1268         endif
1269
1270      endif  ! end of 'n2cond'
1271
1272
1273!---------------------------------------------
1274!   VI. Specific parameterizations for tracers
1275!---------------------------------------------
1276
1277      if (tracer) then
1278
1279
1280
1281!   7a. Methane, CO, and ice
1282!      ---------------------------------------
1283!      Methane ice condensation in the atmosphere
1284!      ----------------------------------------
1285         zdqch4cloud(:,:,:)=0.
1286         if ((methane).and.(metcloud).and.(.not.fast)) THEN
1287            call ch4cloud(ngrid,nlayer,naerkind,ptimestep,  &
1288                      pplev,pplay,pdpsrf,zzlev,zzlay, pt,pdt,  &
1289                      pq,pdq,zdqch4cloud,zdqsch4cloud,zdtch4cloud,   &
1290                      nq,rice_ch4)
1291
1292            DO l=1,nlayer
1293               DO ig=1,ngrid
1294                  pdq(ig,l,igcm_ch4_gas)=pdq(ig,l,igcm_ch4_gas)+  &
1295                                         zdqch4cloud(ig,l,igcm_ch4_gas)
1296                  pdq(ig,l,igcm_ch4_ice)=pdq(ig,l,igcm_ch4_ice)+  &
1297                                         zdqch4cloud(ig,l,igcm_ch4_ice)
1298               ENDDO
1299            ENDDO
1300
1301            ! Increment methane ice surface tracer tendency
1302            DO ig=1,ngrid
1303               dqsurf(ig,igcm_ch4_ice)=dqsurf(ig,igcm_ch4_ice)+   &
1304                                     zdqsch4cloud(ig,igcm_ch4_ice)
1305            ENDDO
1306
1307            ! update temperature tendancy
1308            DO ig=1,ngrid
1309               DO l=1,nlayer
1310               pdt(ig,l)=pdt(ig,l)+zdtch4cloud(ig,l)
1311               ENDDO
1312            ENDDO
1313         else
1314         rice_ch4(:,:)=0 ! initialization needed for callsedim
1315         end if
1316
1317!      ---------------------------------------
1318!      CO ice condensation in the atmosphere
1319!      ----------------------------------------
1320         zdqcocloud(:,:,:)=0.
1321         IF ((carbox).and.(monoxcloud).and.(.not.fast)) THEN
1322            call cocloud(ngrid,nlayer,naerkind,ptimestep,   &
1323                      pplev,pplay,pdpsrf,zzlev,zzlay, pt,pdt,  &
1324                      pq,pdq,zdqcocloud,zdqscocloud,zdtcocloud,   &
1325                      nq,rice_co,qsurf(1,igcm_n2),dqsurf(1,igcm_n2))
1326
1327            DO l=1,nlayer
1328               DO ig=1,ngrid
1329                  pdq(ig,l,igcm_co_gas)=pdq(ig,l,igcm_co_gas)+ &
1330                                         zdqcocloud(ig,l,igcm_co_gas)
1331                  pdq(ig,l,igcm_co_ice)=pdq(ig,l,igcm_co_ice)+ &
1332                                         zdqcocloud(ig,l,igcm_co_ice)
1333               ENDDO
1334            ENDDO
1335
1336            ! Increment CO ice surface tracer tendency
1337            DO ig=1,ngrid
1338            dqsurf(ig,igcm_co_ice)=dqsurf(ig,igcm_co_ice)+  &
1339                                     zdqscocloud(ig,igcm_co_ice)
1340            ENDDO
1341
1342            ! update temperature tendancy
1343            DO ig=1,ngrid
1344               DO l=1,nlayer
1345               pdt(ig,l)=pdt(ig,l)+zdtcocloud(ig,l)
1346               ENDDO
1347            ENDDO
1348         ELSE
1349         rice_co(:,:)=0 ! initialization needed for callsedim
1350         END IF  ! of IF (carbox)
1351
1352!------------------------------------------------------------------
1353! test methane conservation
1354!         if(methane)then
1355!           call testconserv(ngrid,nlayer,nq,igcm_ch4_gas,igcm_ch4_ice,
1356!     &         ptimestep,pplev,zdqch4cloud,zdqsch4cloud,'CH4','ch4clou')
1357!         endif  ! methane
1358!------------------------------------------------------------------
1359! test CO conservation
1360!         if(carbox)then
1361!           call testconserv(ngrid,nlayer,nq,igcm_co_gas,igcm_co_ice,
1362!     &          ptimestep,pplev,zdqcocloud,zdqscocloud,'CO ','cocloud')
1363!         endif  ! carbox
1364!------------------------------------------------------------------
1365
1366!   7b. Haze particle production
1367!     -------------------
1368      IF (haze) THEN
1369
1370         zdqphot_prec(:,:)=0.
1371         zdqphot_ch4(:,:)=0.
1372         ! Forcing to a fixed haze profile if haze_proffix
1373         if (haze_proffix.and.i_haze.gt.0.) then
1374         call haze_prof(ngrid,nlayer,zzlay,pplay,pt,  &
1375                                  reffrad,profmmr)
1376         zdqhaze(:,:,i_haze)=(profmmr(:,:)-pq(:,:,igcm_haze))  &
1377                                                  /ptimestep
1378         else
1379         call hazecloud(ngrid,nlayer,nq,ptimestep, &
1380            pplay,pplev,pq,pdq,dist_star,mu0,zfluxuv,zdqhaze,   &
1381            zdqphot_prec,zdqphot_ch4,zdqconv_prec,declin)
1382         endif
1383
1384         DO iq=1, nq ! should be updated
1385            DO l=1,nlayer
1386            DO ig=1,ngrid
1387                  pdq(ig,l,iq)=pdq(ig,l,iq)+ zdqhaze(ig,l,iq)
1388            ENDDO
1389            ENDDO
1390         ENDDO
1391
1392      ENDIF
1393
1394      IF (fast.and.fasthaze) THEN
1395      call prodhaze(ngrid,nlayer,nq,ptimestep,pplev,pq,pdq,dist_star, &
1396                mu0,declin,zdqprodhaze,zdqsprodhaze,gradflux,fluxbot,   &
1397                fluxlym_sol_bot,fluxlym_ipm_bot,flym_sol,flym_ipm)
1398
1399      DO ig=1,ngrid
1400         pdq(ig,1,igcm_ch4_gas)=pdq(ig,1,igcm_ch4_gas)+  &
1401                                       zdqprodhaze(ig,igcm_ch4_gas)
1402         pdq(ig,1,igcm_prec_haze)=pdq(ig,1,igcm_prec_haze)+ &
1403                                     zdqprodhaze(ig,igcm_prec_haze)
1404         pdq(ig,1,igcm_haze)=abs(pdq(ig,1,igcm_haze)+ &
1405                                     zdqprodhaze(ig,igcm_haze))
1406         qsurf(ig,igcm_haze)= qsurf(ig,igcm_haze)+ &
1407                                         zdqsprodhaze(ig)*ptimestep
1408      ENDDO
1409
1410      ENDIF
1411
1412
1413
1414  ! -------------------------
1415  !   VI.3. Aerosol particles
1416  ! -------------------------
1417
1418         !Generic Condensation
1419         if (generic_condensation) then
1420            call condensation_generic(ngrid,nlayer,nq,ptimestep,pplev,pplay,   &
1421                                          pt,pq,pdt,pdq,dt_generic_condensation, &
1422                                          dqvaplscale_generic,dqcldlscale_generic,rneb_generic)
1423            pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+dt_generic_condensation(1:ngrid,1:nlayer)
1424            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq)+dqvaplscale_generic(1:ngrid,1:nlayer,1:nq)
1425            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq)+dqcldlscale_generic(1:ngrid,1:nlayer,1:nq)
1426
1427            if(enertest)then
1428               do ig=1,ngrid
1429                  genericconddE(ig) = cpp*SUM(mass(:,:)*dt_generic_condensation(:,:))
1430               enddo
1431
1432               call planetwide_sumval(cpp*massarea(:,:)*dt_generic_condensation(:,:)/totarea_planet,dEtot)
1433
1434               if (is_master) print*,'In generic condensation atmospheric energy change =',dEtot,' W m-2'
1435            end if
1436
1437            ! if (.not. water) then
1438               ! Compute GCS (Generic Condensable Specie) cloud fraction. For now we can not have both water cloud fraction and GCS cloud fraction
1439               ! Water is the priority
1440               ! If you have set water and generic_condensation, then cloudfrac will be water cloudfrac
1441               !
1442               ! If you have set generic_condensation (and not water) and you have set several GCS
1443               ! then cloudfrac will be only the cloudfrac of the last generic tracer
1444               ! (Because it is rewritten every tracer in the loop)
1445               !
1446               ! Maybe one should create a cloudfrac_generic(ngrid,nlayer,nq) with 3 dimensions, the last one for tracers
1447
1448               ! Let's loop on tracers
1449               cloudfrac(:,:)=0.0
1450               do iq=1,nq
1451                  call generic_tracer_index(nq,iq,igcm_generic_vap,igcm_generic_ice,call_ice_vap_generic)
1452                  if (call_ice_vap_generic) then ! to call only one time the ice/vap pair of a tracer
1453                     do l = 1, nlayer
1454                        do ig=1,ngrid
1455                           cloudfrac(ig,l)=rneb_generic(ig,l,iq)
1456                        enddo
1457                     enddo
1458                  endif
1459            end do ! do iq=1,nq loop on tracers
1460            ! endif ! .not. water
1461
1462         endif !generic_condensation
1463
1464         !Generic Rain !AF24: removed
1465
1466         ! Sedimentation.
1467         if (sedimentation) then
1468
1469            zdqsed(1:ngrid,1:nlayer,1:nq) = 0.0
1470            zdqssed(1:ngrid,1:nq)  = 0.0
1471
1472            ! if(watertest)then !AF24: removed
1473
1474            call callsedim(ngrid,nlayer,ptimestep,           &
1475                          pplev,zzlev,pt,pdt,pq,pdq,        &
1476                          zdqsed,zdqssed,nq)
1477
1478            ! if(watertest)then !AF24: removed
1479
1480            ! Whether it falls as rain or snow depends only on the surface temperature
1481            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqsed(1:ngrid,1:nlayer,1:nq)
1482            dqsurf(1:ngrid,1:nq) = dqsurf(1:ngrid,1:nq) + zdqssed(1:ngrid,1:nq)
1483
1484!!            call writediagfi(ngrid,"callsedim_post_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1485
1486            ! ! Test water conservation !AF24: removed
1487
1488         end if ! end of 'sedimentation'
1489
1490!!         call writediagfi(ngrid,"mass_redist_pre_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1491!!         call writediagfi(ngrid,"mass_redist_pre_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
1492
1493  ! ---------------
1494  !   VI.4. Updates
1495  ! ---------------
1496
1497         ! Updating Atmospheric Mass and Tracers budgets.
1498         if(mass_redistrib) then
1499
1500            zdmassmr(1:ngrid,1:nlayer) = mass(1:ngrid,1:nlayer) * 0
1501            !    (   zdqevap(1:ngrid,1:nlayer)                          &
1502            !    !   + zdqrain(1:ngrid,1:nlayer,igcm_h2o_vap)             &
1503            !    !   + dqmoist(1:ngrid,1:nlayer,igcm_h2o_vap)             &
1504            !      + dqvaplscale(1:ngrid,1:nlayer) )
1505
1506            do ig = 1, ngrid
1507               zdmassmr_col(ig)=SUM(zdmassmr(ig,1:nlayer))
1508            enddo
1509
1510            ! call writediagfi(ngrid,"mass_evap","mass gain"," ",3,zdmassmr)
1511            ! call writediagfi(ngrid,"mass_evap_col","mass gain col"," ",2,zdmassmr_col)
1512            call writediagfi(ngrid,"mass","mass","kg/m2",3,mass)
1513
1514            call mass_redistribution(ngrid,nlayer,nq,ptimestep,                     &
1515                                     capcal,pplay,pplev,pt,tsurf,pq,qsurf,     &
1516                                     pu,pv,pdt,zdtsurf,pdq,pdu,pdv,zdmassmr,        &
1517                                     zdtmr,zdtsurfmr,zdpsrfmr,zdumr,zdvmr,zdqmr,zdqsurfmr)
1518
1519            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqmr(1:ngrid,1:nlayer,1:nq)
1520            dqsurf(1:ngrid,1:nq)       = dqsurf(1:ngrid,1:nq) + zdqsurfmr(1:ngrid,1:nq)
1521            pdt(1:ngrid,1:nlayer)      = pdt(1:ngrid,1:nlayer) + zdtmr(1:ngrid,1:nlayer)
1522            pdu(1:ngrid,1:nlayer)      = pdu(1:ngrid,1:nlayer) + zdumr(1:ngrid,1:nlayer)
1523            pdv(1:ngrid,1:nlayer)      = pdv(1:ngrid,1:nlayer) + zdvmr(1:ngrid,1:nlayer)
1524            pdpsrf(1:ngrid)            = pdpsrf(1:ngrid) + zdpsrfmr(1:ngrid)
1525            zdtsurf(1:ngrid)           = zdtsurf(1:ngrid) + zdtsurfmr(1:ngrid)
1526
1527         endif
1528
1529!         call writediagfi(ngrid,"mass_redistribution_post_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1530
1531!!         call writediagfi(ngrid,"slab_pre_dqsurf"," "," ",2,dqsurf(1:ngrid,igcm_h2o_vap))
1532!!         call writediagfi(ngrid,"slab_pre_qsurf"," "," ",2,qsurf(1:ngrid,igcm_h2o_vap))
1533
1534
1535! ------------------
1536!   VI.5. Slab Ocean !AF24: removed
1537! ------------------
1538
1539  ! -----------------------------
1540  !   VI.6. Surface Tracer Update
1541  ! -----------------------------
1542
1543         ! AF24: deleted hydrology
1544
1545         qsurf(1:ngrid,1:nq) = qsurf(1:ngrid,1:nq) + ptimestep*dqsurf(1:ngrid,1:nq)
1546
1547         ! Add qsurf to qsurf_hist, which is what we save in diagfi.nc. At the same time, we set the water
1548         ! content of ocean gridpoints back to zero, in order to avoid rounding errors in vdifc, rain.
1549         qsurf_hist(:,:) = qsurf(:,:)
1550
1551         ! if(ice_update)then
1552         !    ice_min(1:ngrid)=min(ice_min(1:ngrid),qsurf(1:ngrid,igcm_h2o_ice))
1553         ! endif
1554
1555      endif! end of if 'tracer'
1556
1557      if (conservn2) then
1558         write(*,*) 'conservn2 tracer'
1559         ! call testconservmass(ngrid,nlayer,pplev(:,1)+   &
1560         !    pdpsrf(:)*ptimestep,qsurf(:,1))
1561      endif
1562
1563      DO ig=1,ngrid
1564        flusurf(ig,igcm_n2)=(qsurf(ig,igcm_n2)- &
1565                                   qsurf1(ig,igcm_n2))/ptimestep
1566        flusurfold(ig,igcm_n2)=flusurf(ig,igcm_n2)
1567        if (methane) then
1568          flusurf(ig,igcm_ch4_ice)=(qsurf(ig,igcm_ch4_ice)- &
1569                                   qsurf1(ig,igcm_ch4_ice))/ptimestep
1570          flusurfold(ig,igcm_ch4_ice)=flusurf(ig,igcm_ch4_ice)
1571        endif
1572        if (carbox) then
1573          flusurf(ig,igcm_co_ice)=(qsurf(ig,igcm_co_ice)-   &
1574                                   qsurf1(ig,igcm_co_ice))/ptimestep
1575          !flusurfold(ig,igcm_co_ice)=flusurf(ig,igcm_co_ice)
1576        endif
1577      ENDDO
1578
1579      !! Special source of haze particle !
1580      ! todo: should be placed in haze and use tendency of n2 instead of flusurf
1581      IF (source_haze) THEN
1582            !  call hazesource(ngrid,nlayer,nq,ptimestep,  &
1583            !                 pplev,flusurf,mu0,zdq_source)
1584
1585             DO iq=1, nq
1586               DO l=1,nlayer
1587                 DO ig=1,ngrid
1588                    pdq(ig,l,iq)=pdq(ig,l,iq)+zdq_source(ig,l,iq)
1589                 ENDDO
1590               ENDDO
1591             ENDDO
1592      ENDIF
1593
1594
1595!------------------------------------------------
1596!   VII. Surface and sub-surface soil temperature
1597!------------------------------------------------
1598
1599!   For diagnostic
1600!   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1601      DO ig=1,ngrid
1602         rho(ig,1) = pplay(ig,1)/(r*pt(ig,1))
1603         sensiblehf1(ig)=rho(ig,1)*cpp*(0.4/log(zzlay(ig,1)/z0))**2* &
1604                        (pu(ig,1)*pu(ig,1)+pv(ig,1)*pv(ig,1))**0.5*  &
1605                        (tsurf(ig)-pt(ig,1))
1606         sensiblehf2(ig)=zflubid(ig)-capcal(ig)*zdtsdif(ig)
1607
1608      ENDDO
1609
1610
1611      ! ! Increment surface temperature
1612      ! if(ok_slab_ocean)then  !AF24: removed
1613
1614      tsurf(1:ngrid)=tsurf(1:ngrid)+ptimestep*zdtsurf(1:ngrid)
1615      ! Compute soil temperatures and subsurface heat flux.
1616      if (callsoil) then
1617         call soil(ngrid,nsoilmx,.false.,lastcall,inertiedat,   &
1618                   ptimestep,tsurf,tsoil,capcal,fluxgrd)
1619      endif
1620
1621
1622!       if (ok_slab_ocean) then !AF24: removed
1623
1624      ! Test energy conservation
1625      if(enertest)then
1626         call planetwide_sumval(cell_area(:)*capcal(:)*zdtsurf(:)/totarea_planet,dEtots)
1627         if (is_master) print*,'Surface energy change                 =',dEtots,' W m-2'
1628      endif
1629
1630
1631!---------------------------------------------------
1632!   VIII. Perform diagnostics and write output files
1633!---------------------------------------------------
1634
1635      ! Note : For output only: the actual model integration is performed in the dynamics.
1636
1637
1638      ! Temperature, zonal and meridional winds.
1639      zt(1:ngrid,1:nlayer) = pt(1:ngrid,1:nlayer) + pdt(1:ngrid,1:nlayer)*ptimestep
1640      zu(1:ngrid,1:nlayer) = pu(1:ngrid,1:nlayer) + pdu(1:ngrid,1:nlayer)*ptimestep
1641      zv(1:ngrid,1:nlayer) = pv(1:ngrid,1:nlayer) + pdv(1:ngrid,1:nlayer)*ptimestep
1642
1643      !! Recast thermal plume vertical velocity array for outputs
1644      !! AF24: removed
1645
1646      ! Diagnostic.
1647      zdtdyn(1:ngrid,1:nlayer)     = (pt(1:ngrid,1:nlayer)-ztprevious(1:ngrid,1:nlayer)) / ptimestep
1648      ztprevious(1:ngrid,1:nlayer) = zt(1:ngrid,1:nlayer)
1649
1650      zdudyn(1:ngrid,1:nlayer)     = (pu(1:ngrid,1:nlayer)-zuprevious(1:ngrid,1:nlayer)) / ptimestep
1651      zuprevious(1:ngrid,1:nlayer) = zu(1:ngrid,1:nlayer)
1652
1653      if(firstcall)then
1654         zdtdyn(1:ngrid,1:nlayer)=0.0
1655         zdudyn(1:ngrid,1:nlayer)=0.0
1656      endif
1657
1658      ! Dynamical heating diagnostic.
1659      do ig=1,ngrid
1660         fluxdyn(ig)= SUM(zdtdyn(ig,:) *mass(ig,:))*cpp
1661      enddo
1662
1663      ! Tracers.
1664      zq(1:ngrid,1:nlayer,1:nq) = pq(1:ngrid,1:nlayer,1:nq) + pdq(1:ngrid,1:nlayer,1:nq)*ptimestep
1665
1666      ! Surface pressure.
1667      ps(1:ngrid) = pplev(1:ngrid,1) + pdpsrf(1:ngrid)*ptimestep
1668
1669      ! pressure density !pluto specific
1670      IF (.not.fast) then !
1671         do ig=1,ngrid
1672            do l=1,nlayer
1673               zplev(ig,l)=pplev(ig,l)/pplev(ig,1)*ps(ig)
1674               zplay(ig,l)=pplay(ig,l)/pplev(ig,1)*ps(ig)
1675               rho(ig,l) = zplay(ig,l)/(r*zt(ig,l))
1676            enddo
1677            zplev(ig,nlayer+1)=pplev(ig,nlayer+1)/pplev(ig,1)*ps(ig)
1678         enddo
1679      ENDIF
1680
1681
1682      ! Surface and soil temperature information
1683      call planetwide_sumval(cell_area(:)*tsurf(:)/totarea_planet,Ts1)
1684      call planetwide_minval(tsurf(:),Ts2)
1685      call planetwide_maxval(tsurf(:),Ts3)
1686      if(callsoil)then
1687         TsS = SUM(cell_area(:)*tsoil(:,nsoilmx))/totarea        ! mean temperature at bottom soil layer
1688         if (is_master) then
1689            print*,'          ave[Tsurf]             min[Tsurf]             max[Tsurf]             ave[Tdeep]'
1690            print*,Ts1,Ts2,Ts3,TsS
1691         end if
1692      else
1693         if (is_master) then
1694            print*,'          ave[Tsurf]             min[Tsurf]             max[Tsurf]'
1695            print*,Ts1,Ts2,Ts3
1696         endif
1697      end if
1698
1699
1700      ! Check the energy balance of the simulation during the run
1701      if(corrk)then
1702
1703         call planetwide_sumval(cell_area(:)*fluxtop_dn(:)/totarea_planet,ISR)
1704         call planetwide_sumval(cell_area(:)*fluxabs_sw(:)/totarea_planet,ASR)
1705         call planetwide_sumval(cell_area(:)*fluxtop_lw(:)/totarea_planet,OLR)
1706         call planetwide_sumval(cell_area(:)*fluxgrd(:)/totarea_planet,GND)
1707         call planetwide_sumval(cell_area(:)*fluxdyn(:)/totarea_planet,DYN)
1708         do ig=1,ngrid
1709            if(fluxtop_dn(ig).lt.0.0)then
1710               print*,'fluxtop_dn has gone crazy'
1711               print*,'fluxtop_dn=',fluxtop_dn(ig)
1712               print*,'tau_col=',tau_col(ig)
1713               print*,'aerosol=',aerosol(ig,:,:)
1714               print*,'temp=   ',pt(ig,:)
1715               print*,'pplay=  ',pplay(ig,:)
1716               call abort
1717            endif
1718         end do
1719
1720         if(ngrid.eq.1)then
1721            DYN=0.0
1722         endif
1723
1724         if (is_master) then
1725            print*,'  ISR            ASR            OLR            GND            DYN [W m^-2]'
1726            print*, ISR,ASR,OLR,GND,DYN
1727         endif
1728
1729         if(enertest .and. is_master)then
1730            print*,'SW flux/heating difference SW++ - ASR = ',dEtotSW+dEtotsSW-ASR,' W m-2'
1731            print*,'LW flux/heating difference LW++ - OLR = ',dEtotLW+dEtotsLW+OLR,' W m-2'
1732            print*,'LW energy balance LW++ + ASR = ',dEtotLW+dEtotsLW+ASR,' W m-2'
1733         endif
1734
1735         if(meanOLR .and. is_master)then
1736            if((ngrid.gt.1) .or. (mod(icount-1,ecritphy).eq.0))then
1737               ! to record global radiative balance
1738               open(92,file="rad_bal.out",form='formatted',position='append')
1739               write(92,*) zday,ISR,ASR,OLR
1740               close(92)
1741               open(93,file="tem_bal.out",form='formatted',position='append')
1742               if(callsoil)then
1743                         write(93,*) zday,Ts1,Ts2,Ts3,TsS
1744               else
1745                  write(93,*) zday,Ts1,Ts2,Ts3
1746               endif
1747               close(93)
1748            endif
1749         endif
1750
1751      endif ! end of 'corrk'
1752
1753
1754      ! Diagnostic to test radiative-convective timescales in code.
1755      if(testradtimes)then
1756         open(38,file="tau_phys.out",form='formatted',position='append')
1757         ig=1
1758         do l=1,nlayer
1759            write(38,*) -1./pdt(ig,l),pt(ig,l),pplay(ig,l)
1760         enddo
1761         close(38)
1762         print*,'As testradtimes enabled,'
1763         print*,'exiting physics on first call'
1764         call abort
1765      endif
1766
1767
1768      ! Compute column amounts (kg m-2) if tracers are enabled.
1769      if(tracer)then
1770         qcol(1:ngrid,1:nq)=0.0
1771         do iq=1,nq
1772            do ig=1,ngrid
1773               qcol(ig,iq) = SUM( zq(ig,1:nlayer,iq) * mass(ig,1:nlayer))
1774            enddo
1775         enddo
1776
1777      endif ! end of 'tracer'
1778
1779
1780      ! ! Test for water conservation. !AF24: removed
1781
1782      ! Calculate RH_generic (Generic Relative Humidity) for diagnostic.
1783      if(generic_condensation)then
1784         RH_generic(:,:,:)=0.0
1785         do iq=1,nq
1786
1787            call generic_tracer_index(nq,iq,igcm_generic_vap,igcm_generic_ice,call_ice_vap_generic)
1788
1789            if (call_ice_vap_generic) then ! to call only one time the ice/vap pair of a tracer
1790
1791               do l = 1, nlayer
1792                  do ig=1,ngrid
1793                     call Psat_generic(zt(ig,l),pplay(ig,l),metallicity,psat_tmp_generic,qsat_generic(ig,l,iq))
1794                     RH_generic(ig,l,iq) = zq(ig,l,igcm_generic_vap) / qsat_generic(ig,l,iq)
1795                  enddo
1796               enddo
1797
1798            end if
1799
1800         end do ! iq=1,nq
1801
1802      endif ! end of 'generic_condensation'
1803
1804
1805      if (methane) then
1806         IF (fast) then ! zq is the mixing ratio supposingly mixed in all atmosphere
1807           DO ig=1,ngrid
1808             vmr_ch4(ig)=zq(ig,1,igcm_ch4_gas)* &
1809                              mmol(igcm_n2)/mmol(igcm_ch4_gas)*100.
1810           ENDDO
1811         ELSE
1812           DO ig=1,ngrid
1813 !           compute vmr methane
1814             vmr_ch4(ig)=qcol(ig,igcm_ch4_gas)* &
1815                   g/ps(ig)*mmol(igcm_n2)/mmol(igcm_ch4_gas)*100.
1816 !           compute density methane
1817             DO l=1,nlayer
1818                zrho_ch4(ig,l)=zq(ig,l,igcm_ch4_gas)*rho(ig,l)
1819             ENDDO
1820           ENDDO
1821         ENDIF
1822       endif
1823
1824       if (carbox) then
1825         IF (fast) then
1826           DO ig=1,ngrid
1827             vmr_co(ig)=zq(ig,1,igcm_co_gas)*   &
1828                              mmol(igcm_n2)/mmol(igcm_co_gas)*100.
1829           ENDDO
1830         ELSE
1831          DO ig=1,ngrid
1832 !           compute vmr CO
1833             vmr_co(ig)=qcol(ig,igcm_co_gas)*   &
1834                   g/ps(ig)*mmol(igcm_n2)/mmol(igcm_co_gas)*100.
1835 !           compute density CO
1836             DO l=1,nlayer
1837                zrho_co(ig,l)=zq(ig,l,igcm_co_gas)*rho(ig,l)
1838             ENDDO
1839          ENDDO
1840         ENDIF
1841       endif
1842
1843       zrho_haze(:,:)=0.
1844       zdqrho_photprec(:,:)=0.
1845       IF (haze.and.aerohaze) then
1846        DO ig=1,ngrid
1847         DO l=1,nlayer
1848                zrho_haze(ig,l)=zq(ig,l,igcm_haze)*rho(ig,l)
1849                zdqrho_photprec(ig,l)=zdqphot_prec(ig,l)*rho(ig,l)
1850             ENDDO
1851         ENDDO
1852       ENDIF
1853
1854       IF (fasthaze) then
1855        DO ig=1,ngrid
1856           qcol(ig,igcm_haze)=zq(ig,1,igcm_haze)*pplev(ig,1)/g
1857           qcol(ig,igcm_prec_haze)=zq(ig,1,igcm_prec_haze)*pplev(ig,1)/g
1858        ENDDO
1859       ENDIF
1860
1861 !     Info about Ls, declin...
1862       IF (fast) THEN
1863         if (is_master) write(*,*),'Ls=',zls*180./pi,' dec=',declin*180./pi
1864         if (is_master) write(*,*),'zday=',zday,' ps=',globave
1865        IF (lastcall) then
1866         if (is_master) write(*,*),'lastcall'
1867        ENDIF
1868       ELSE
1869         if (is_master) write(*,*),'Ls=',zls*180./pi,'decli=',declin*180./pi,'zday=',zday
1870       ENDIF
1871
1872       lecttsoil=0 ! default value for lecttsoil
1873       call getin_p("lecttsoil",lecttsoil)
1874       IF (lastcall.and.(ngrid.EQ.1).and.(lecttsoil.eq.1)) THEN
1875       ! save tsoil temperature profile for 1D profile
1876          OPEN(13,file='proftsoil.out',form='formatted')
1877          DO i=1,nsoilmx
1878             write(13,*) tsoil(1,i)
1879          ENDDO
1880          CLOSE(13)
1881       ENDIF
1882
1883
1884      if (is_master) print*,'--> Ls =',zls*180./pi
1885
1886
1887!----------------------------------------------------------------------
1888!        Writing NetCDF file  "RESTARTFI" at the end of the run
1889!----------------------------------------------------------------------
1890
1891!        Note: 'restartfi' is stored just before dynamics are stored
1892!              in 'restart'. Between now and the writting of 'restart',
1893!              there will have been the itau=itau+1 instruction and
1894!              a reset of 'time' (lastacll = .true. when itau+1= itaufin)
1895!              thus we store for time=time+dtvr
1896
1897
1898
1899      if(lastcall) then
1900         ztime_fin = ptime + ptimestep/(float(iphysiq)*daysec)
1901
1902         !! Update surface ice distribution to iterate to steady state if requested
1903         !! AF24: removed
1904
1905         ! endif
1906         if (paleo) then
1907            ! time range for tendencies of ice flux qsurfyear
1908            zdt_tot=year_day   ! Last year of simulation
1909
1910            masslost(:)=0.
1911            massacc(:)=0.
1912
1913            DO ig=1,ngrid
1914               ! update new reservoir of ice on the surface
1915               DO iq=1,nq
1916                ! kg/m2 to be sublimed or condensed during paleoyears
1917                qsurfyear(ig,iq)=qsurfyear(ig,iq)* &
1918                           paleoyears*365.25/(zdt_tot*daysec/86400.)
1919
1920               ! special case if we sublime the entire reservoir
1921               !! AF: TODO : fix following lines (real_area), using line below:
1922            ! call planetwide_sumval((-qsurfyear(:,iq)-qsurf(:,iq))*cell_area(:),masslost)
1923
1924               !  IF (-qsurfyear(ig,iq).gt.qsurf(ig,iq)) THEN
1925               !    masslost(iq)=masslost(iq)+real_area(ig)*   &
1926               !          (-qsurfyear(ig,iq)-qsurf(ig,iq))
1927               !    qsurfyear(ig,iq)=-qsurf(ig,iq)
1928               !  ENDIF
1929
1930               !  IF (qsurfyear(ig,iq).gt.0.) THEN
1931               !    massacc(iq)=massacc(iq)+real_area(ig)*qsurfyear(ig,iq)
1932               !  ENDIF
1933
1934
1935               ENDDO
1936            ENDDO
1937
1938            DO ig=1,ngrid
1939               DO iq=1,nq
1940                 qsurfpal(ig,iq)=qsurf(ig,iq)+qsurfyear(ig,iq)
1941                 IF (qsurfyear(ig,iq).gt.0.) THEN
1942                  qsurfpal(ig,iq)=qsurfpal(ig,iq)- &
1943                       qsurfyear(ig,iq)*masslost(iq)/massacc(iq)
1944                 ENDIF
1945               ENDDO
1946            ENDDO
1947            ! Finally ensure conservation of qsurf
1948            DO iq=1,nq
1949              call globalaverage2d(ngrid,qsurf(:,iq),globaveice(iq))
1950              call globalaverage2d(ngrid,qsurfpal(:,iq), &
1951                                             globavenewice(iq))
1952              IF (globavenewice(iq).gt.0.) THEN
1953                 qsurfpal(:,iq)=qsurfpal(:,iq)* &
1954                                   globaveice(iq)/globavenewice(iq)
1955              ENDIF
1956            ENDDO
1957
1958            ! update new geopotential depending on the ice reservoir
1959            phisfipal(:)=phisfinew(:)+qsurfpal(:,igcm_n2)*g/1000.
1960            !phisfipal(ig)=phisfi(ig)
1961
1962
1963            if (kbo.or.triton) then !  case of Triton : we do not change the orbital parameters
1964
1965              pdaypal=pday ! no increment of pdaypal to keep same evolution of the subsolar point
1966              eccpal=1.-periastr/((periastr+apoastr)/2.)    !no change of ecc
1967              peri_daypal=peri_day ! no change
1968              oblipal=obliquit     ! no change
1969              tpalnew=tpal
1970              adjustnew=adjust
1971
1972            else  ! Pluto
1973              ! update new pday and tpal (Myr) to be set in startfi controle
1974              pdaypal=int(day_ini+paleoyears*365.25/6.3872)
1975              tpalnew=tpal+paleoyears*1.e-6  ! Myrs
1976
1977              ! update new N2 ice adjustment (not tested yet on Pluto)
1978              adjustnew=adjust
1979
1980              ! update milankovitch parameters : obliquity,Lsp,ecc
1981              call calcmilank(tpalnew,oblipal,peri_daypal,eccpal)
1982              !peri_daypal=peri_day
1983              !eccpal=0.009
1984
1985            endif
1986
1987            if (is_master) write(*,*) "Paleo peri=",peri_daypal,"  tpal=",tpalnew
1988            if (is_master) write(*,*) "Paleo eccpal=",eccpal,"  tpal=",tpalnew
1989
1990
1991#ifndef MESOSCALE
1992             ! create restartfi
1993         if (ngrid.ne.1) then
1994            !TODO: import this routine from pluto.old
1995            ! call physdem1pal("restartfi.nc",long,lati,nsoilmx,nq, &
1996            !      ptimestep,pdaypal, &
1997            !      ztime_fin,tsurf,tsoil,emis,q2,qsurfpal, &
1998            !      cell_area,albedodat,tidat,zmea,zstd,zsig, &
1999            !      zgam,zthe,oblipal,eccpal,tpalnew,adjustnew,phisfipal,  &
2000            !      peri_daypal)
2001         endif
2002      else ! 'paleo'
2003
2004         if (ngrid.ne.1) then
2005            write(*,*)'PHYSIQ: for physdem ztime_fin =',ztime_fin
2006
2007            call physdem1("restartfi.nc",nsoilmx,ngrid,nlayer,nq, &
2008                          ptimestep,ztime_fin,                    &
2009                          tsurf,tsoil,emis,q2,qsurf_hist)
2010         endif
2011#endif
2012      endif ! end of 'paleo'
2013         ! if(ok_slab_ocean) then
2014         !    call ocean_slab_final!(tslab, seaice)
2015         ! end if
2016
2017    endif ! end of 'lastcall'
2018
2019
2020! -----------------------------------------------------------------
2021! WSTATS: Saving statistics
2022! -----------------------------------------------------------------
2023! ("stats" stores and accumulates key variables in file "stats.nc"
2024!  which can later be used to make the statistic files of the run:
2025!  if flag "callstats" from callphys.def is .true.)
2026
2027
2028         call wstats(ngrid,"ps","Surface pressure","Pa",2,ps)
2029         call wstats(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2030         call wstats(ngrid,"fluxsurf_lw",                               &
2031                     "Thermal IR radiative flux to surface","W.m-2",2,  &
2032                     fluxsurf_lw)
2033         call wstats(ngrid,"fluxtop_lw",                                &
2034                     "Thermal IR radiative flux to space","W.m-2",2,    &
2035                     fluxtop_lw)
2036
2037!            call wstats(ngrid,"fluxsurf_sw",                               &
2038!                        "Solar radiative flux to surface","W.m-2",2,       &
2039!                         fluxsurf_sw_tot)
2040!            call wstats(ngrid,"fluxtop_sw",                                &
2041!                        "Solar radiative flux to space","W.m-2",2,         &
2042!                        fluxtop_sw_tot)
2043
2044
2045         call wstats(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
2046         call wstats(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
2047         call wstats(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
2048         !call wstats(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
2049         !call wstats(ngrid,"ALB_1st","First Band Surface albedo"," ",2,albedo(:,1))
2050         call wstats(ngrid,"p","Pressure","Pa",3,pplay)
2051         call wstats(ngrid,"temp","Atmospheric temperature","K",3,zt)
2052         call wstats(ngrid,"u","Zonal (East-West) wind","m.s-1",3,zu)
2053         call wstats(ngrid,"v","Meridional (North-South) wind","m.s-1",3,zv)
2054         call wstats(ngrid,"w","Vertical (down-up) wind","m.s-1",3,pw)
2055         call wstats(ngrid,"q2","Boundary layer eddy kinetic energy","m2.s-2",3,q2)
2056
2057         if (tracer) then
2058            do iq=1,nq
2059               call wstats(ngrid,noms(iq),noms(iq),'kg/kg',3,zq(1,1,iq))
2060               call wstats(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2061                           'kg m^-2',2,qsurf(1,iq) )
2062               call wstats(ngrid,trim(noms(iq))//'_col',trim(noms(iq))//'_col',    &
2063                          'kg m^-2',2,qcol(1,iq) )
2064
2065!              call wstats(ngrid,trim(noms(iq))//'_reff',                          &
2066!                          trim(noms(iq))//'_reff',                                   &
2067!                          'm',3,reffrad(1,1,iq))
2068
2069            end do
2070
2071         endif ! end of 'tracer'
2072
2073         !AF24: deleted slab ocean and water
2074
2075         if(lastcall.and.callstats) then
2076            write (*,*) "Writing stats..."
2077            call mkstats(ierr)
2078         endif
2079
2080
2081#ifndef MESOSCALE
2082
2083!-----------------------------------------------------------------------------------------------------
2084!           OUTPUT in netcdf file "DIAGFI.NC", containing any variable for diagnostic
2085!
2086!             Note 1 : output with  period "ecritphy", set in "run.def"
2087!
2088!             Note 2 : writediagfi can also be called from any other subroutine for any variable,
2089!                      but its preferable to keep all the calls in one place ...
2090!-----------------------------------------------------------------------------------------------------
2091
2092      call writediagfi(ngrid,"Ls","solar longitude","deg",0,zls*180./pi)
2093      call writediagfi(ngrid,"Lss","sub solar longitude","deg",0,zlss*180./pi)
2094      call writediagfi(ngrid,"RA","right ascension","deg",0,right_ascen*180./pi)
2095      call writediagfi(ngrid,"Declin","solar declination","deg",0,declin*180./pi)
2096      call writediagfi(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2097      call writediagfi(ngrid,"ps","Surface pressure","Pa",2,ps)
2098
2099      !! Pluto outputs
2100      ! call writediagfi(ngrid,"rice_ch4","ch4 ice mass mean radius","m",3,rice_ch4)
2101      call writediagfi(ngrid,"dist_star","dist_star","AU",0,dist_star)
2102
2103      if (fast) then
2104         call writediagfi(ngrid,"globave","surf press","Pa",0,globave)
2105         !AF: TODO which outputs?
2106      else
2107         if (check_physics_outputs) then
2108            ! Check the validity of updated fields at the end of the physics step
2109            call check_physics_fields("HERE physiq:", zt, zu, zv, pplev, zq)
2110         endif
2111
2112       call writediagfi(ngrid,"temp","temperature","K",3,zt)
2113       call writediagfi(ngrid,"teta","potential temperature","K",3,zh)
2114       call writediagfi(ngrid,"u","Zonal wind","m.s-1",3,zu)
2115       call writediagfi(ngrid,"v","Meridional wind","m.s-1",3,zv)
2116       call writediagfi(ngrid,"w","Vertical wind","m.s-1",3,pw)
2117       call writediagfi(ngrid,"p","Pressure","Pa",3,pplay)
2118      endif
2119
2120!     Subsurface temperatures
2121!        call writediagsoil(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2122!        call writediagsoil(ngrid,"temp","temperature","K",3,tsoil)
2123
2124!       ! Oceanic layers !AF24: removed
2125
2126      ! ! Thermal plume model  !AF24: removed
2127
2128!        GW non-oro outputs  !AF24: removed
2129
2130      ! Total energy balance diagnostics
2131      if(callrad)then
2132
2133         call writediagfi(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
2134         !call writediagfi(ngrid,"ALB_1st","First Band Surface albedo"," ",2,albedo(:,1))
2135         call writediagfi(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
2136         call writediagfi(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
2137         call writediagfi(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
2138         call writediagfi(ngrid,"shad","rings"," ", 2, fract)
2139
2140!           call writediagfi(ngrid,"ASRcs","absorbed stellar rad (cs).","W m-2",2,fluxabs_sw1)
2141!           call writediagfi(ngrid,"OLRcs","outgoing longwave rad (cs).","W m-2",2,fluxtop_lw1)
2142!           call writediagfi(ngrid,"fluxsurfsw","sw surface flux.","W m-2",2,fluxsurf_sw)
2143!           call writediagfi(ngrid,"fluxsurflw","lw back radiation.","W m-2",2,fluxsurf_lw)
2144!           call writediagfi(ngrid,"fluxsurfswcs","sw surface flux (cs).","W m-2",2,fluxsurf_sw1)
2145!           call writediagfi(ngrid,"fluxsurflwcs","lw back radiation (cs).","W m-2",2,fluxsurf_lw1)
2146
2147         ! if(ok_slab_ocean) then
2148         !    call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrdocean)
2149         ! else
2150         call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrd)
2151         ! endif
2152
2153         call writediagfi(ngrid,"DYN","dynamical heat input","W m-2",2,fluxdyn)
2154
2155      endif ! end of 'callrad'
2156
2157      if(enertest) then
2158
2159         if (calldifv) then
2160
2161            call writediagfi(ngrid,"q2","turbulent kinetic energy","J.kg^-1",3,q2)
2162            call writediagfi(ngrid,"sensibFlux","sensible heat flux","w.m^-2",2,sensibFlux)
2163
2164!             call writediagfi(ngrid,"dEzdiff","turbulent diffusion heating (-sensible flux)","w.m^-2",3,dEzdiff)
2165!             call writediagfi(ngrid,"dEdiff","integrated turbulent diffusion heating (-sensible flux)","w.m^-2",2,dEdiff)
2166!             call writediagfi(ngrid,"dEdiffs","In TurbDiff (correc rad+latent heat) surf nrj change","w.m^-2",2,dEdiffs)
2167
2168         endif
2169
2170         if (corrk) then
2171            call writediagfi(ngrid,"dEzradsw","radiative heating","w.m^-2",3,dEzradsw)
2172            call writediagfi(ngrid,"dEzradlw","radiative heating","w.m^-2",3,dEzradlw)
2173         endif
2174
2175!          if(watercond) then  !AF24: removed
2176
2177         if (generic_condensation) then
2178
2179            call writediagfi(ngrid,"genericconddE","heat from generic condensation","W m-2",2,genericconddE)
2180            call writediagfi(ngrid,"dt_generic_condensation","heating from generic condensation","K s-1",3,dt_generic_condensation)
2181
2182         endif
2183
2184      endif ! end of 'enertest'
2185
2186        ! Diagnostics of optical thickness
2187        ! Warning this is exp(-tau), I let you postproc with -log to have tau itself - JVO 19
2188        if (diagdtau) then
2189          do nw=1,L_NSPECTV
2190            write(str2,'(i2.2)') nw
2191            call writediagfi(ngrid,'dtauv'//str2,'Layer optical thickness attenuation in VI band '//str2,'',1,int_dtauv(:,nlayer:1:-1,nw))
2192          enddo
2193          do nw=1,L_NSPECTI
2194            write(str2,'(i2.2)') nw
2195            call writediagfi(ngrid,'dtaui'//str2,'Layer optical thickness attenuation in IR band '//str2,'',1,int_dtaui(:,nlayer:1:-1,nw))
2196          enddo
2197        endif
2198
2199
2200        ! Temporary inclusions for heating diagnostics.
2201        call writediagfi(ngrid,"zdtsw","SW heating","T s-1",3,zdtsw)
2202        call writediagfi(ngrid,"zdtlw","LW heating","T s-1",3,zdtlw)
2203        call writediagfi(ngrid,"dtrad","radiative heating","K s-1",3,dtrad)
2204        call writediagfi(ngrid,"zdtdyn","Dyn. heating","T s-1",3,zdtdyn)
2205
2206        !Pluto specific
2207        call writediagfi(ngrid,"zdtc","tendancy T cond N2","K",3,zdtc)
2208      !   call writediagfi(ngrid,"zdtch4cloud","tendancy T ch4cloud","K",3,zdtch4cloud)
2209      !   call writediagfi(ngrid,"zdtcocloud","tendancy T cocloud","K",3,zdtcocloud)
2210      !   call writediagfi(ngrid,"zq1temp_ch4"," "," ",2,zq1temp_ch4)
2211      !   call writediagfi(ngrid,"qsat_ch4"," "," ",2,qsat_ch4)
2212      !   call writediagfi(ngrid,"qsat_ch4_l1"," "," ",2,qsat_ch4_l1)
2213      !   call writediagfi(ngrid,"senshf1","senshf1"," ",2,sensiblehf1)
2214      !   call writediagfi(ngrid,"senshf2","senshf2"," ",2,sensiblehf2)
2215
2216
2217        ! For Debugging.
2218        !call writediagfi(ngrid,'rnat','Terrain type',' ',2,real(rnat))
2219        !call writediagfi(ngrid,'pphi','Geopotential',' ',3,pphi)
2220
2221
2222      ! Output aerosols.!AF: TODO: write haze aerosols
2223      ! if (igcm_n2_ice.ne.0.and.iaero_haze.ne.0) &
2224      !    call writediagfi(ngrid,'N2ice_reff','N2ice_reff','m',3,reffrad(1,1,iaero_haze))
2225      ! if (igcm_n2_ice.ne.0.and.iaero_haze.ne.0) &
2226      !    call writediagfi(ngrid,'N2ice_reffcol','N2ice_reffcol','um kg m^-2',2,reffcol(1,iaero_haze))
2227      ! if (igcm_h2o_ice.ne.0.and.iaero_h2o.ne.0) &  !AF24: removed
2228
2229      ! Output tracers.
2230      if (tracer) then
2231
2232         do iq=1,nq
2233            call writediagfi(ngrid,noms(iq),noms(iq),'kg/kg',3,zq(1,1,iq))
2234            call writediagfi(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2235                             'kg m^-2',2,qsurf_hist(1,iq) )
2236            call writediagfi(ngrid,trim(noms(iq))//'_col',trim(noms(iq))//'_col',    &
2237                            'kg m^-2',2,qcol(1,iq) )
2238!          call writediagfi(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2239!                          'kg m^-2',2,qsurf(1,iq) )
2240
2241            ! if(watercond.or.CLFvarying)then  !AF24: removed
2242
2243            if(generic_condensation)then
2244               call writediagfi(ngrid,"rneb_generic","GCS cloud fraction (generic condensation)"," ",3,rneb_generic)
2245               call writediagfi(ngrid,"CLF","GCS cloud fraction"," ",3,cloudfrac)
2246               call writediagfi(ngrid,"RH_generic","GCS relative humidity"," ",3,RH_generic)
2247            endif
2248
2249            ! if(generic_rain)then  !AF24: removed
2250            ! if((hydrology).and.(.not.ok_slab_ocean))then  !AF24: removed
2251
2252            call writediagfi(ngrid,"tau_col","Total aerosol optical depth","[]",2,tau_col)
2253
2254         enddo ! end of 'nq' loop
2255
2256         !Pluto specific
2257         call writediagfi(ngrid,'n2_iceflux','n2_iceflux',"kg m^-2 s^-1",2,flusurf(1,igcm_n2) )
2258         ! call writediagfi(ngrid,'haze_reff','haze_reff','m',3,reffrad(1,1,1))
2259         if (methane) then
2260            call writediagfi(ngrid,'ch4_iceflux','ch4_iceflux',&
2261                              "kg m^-2 s^-1",2,flusurf(1,igcm_ch4_ice) )
2262            call writediagfi(ngrid,"vmr_ch4","vmr_ch4","%",2,vmr_ch4)
2263            if (.not.fast) then
2264               call writediagfi(ngrid,"zrho_ch4","zrho_ch4","kg.m-3",3,zrho_ch4(:,:))
2265            endif
2266
2267             ! Tendancies
2268             call writediagfi(ngrid,"zdqch4cloud","ch4 cloud","T s-1",&
2269                           3,zdqch4cloud(1,1,igcm_ch4_gas))
2270             call writediagfi(ngrid,"zdqcn2_ch4","zdq condn2 ch4","",&
2271                            3,zdqc(:,:,igcm_ch4_gas))
2272             call writediagfi(ngrid,"zdqdif_ch4","zdqdif ch4","",&
2273                            3,zdqdif(:,:,igcm_ch4_gas))
2274             call writediagfi(ngrid,"zdqsdif_ch4_ice","zdqsdif ch4","",&
2275                            2,zdqsdif(:,igcm_ch4_ice))
2276             call writediagfi(ngrid,"zdqadj_ch4","zdqadj ch4","",&
2277                            3,zdqadj(:,:,igcm_ch4_gas))
2278             if (sedimentation) then
2279               call writediagfi(ngrid,"zdqsed_ch4","zdqsed ch4","",&
2280                              3,zdqsed(:,:,igcm_ch4_gas))
2281               call writediagfi(ngrid,"zdqssed_ch4","zdqssed ch4","",&
2282                              2,zdqssed(:,igcm_ch4_gas))
2283             endif
2284             if (metcloud) then
2285               call writediagfi(ngrid,"zdtch4cloud","ch4 cloud","T s-1",&
2286                           3,zdtch4cloud)
2287             endif
2288
2289         endif
2290
2291         if (carbox) then
2292            call writediagfi(ngrid,'co_iceflux','co_iceflux',&
2293                               "kg m^-2 s^-1",2,flusurf(1,igcm_co_ice) )
2294            call writediagfi(ngrid,"vmr_co","vmr_co","%",2,vmr_co)
2295            if (.not.fast) THEN
2296               call writediagfi(ngrid,"zrho_co","zrho_co","kg.m-3",3,zrho_co(:,:))
2297            endif
2298         endif
2299
2300         if (haze) then
2301   !         call writediagfi(ngrid,"zrho_haze","zrho_haze","kg.m-3",3,zrho_haze(:,:))
2302            call writediagfi(ngrid,"zdqrho_photprec","zdqrho_photprec",&
2303                        "kg.m-3.s-1",3,zdqrho_photprec(:,:))
2304            call writediagfi(ngrid,"zdqphot_prec","zdqphot_prec","",&
2305                                                3,zdqphot_prec(:,:))
2306            call writediagfi(ngrid,"zdqhaze_ch4","zdqhaze_ch4","",&
2307                     3,zdqhaze(:,:,igcm_ch4_gas))
2308            call writediagfi(ngrid,"zdqhaze_prec","zdqhaze_prec","",&
2309                     3,zdqhaze(:,:,igcm_prec_haze))
2310            if (igcm_haze.ne.0) then
2311               call writediagfi(ngrid,"zdqhaze_haze","zdqhaze_haze","",&
2312                        3,zdqhaze(:,:,igcm_haze))
2313               if (sedimentation) then
2314                  call writediagfi(ngrid,"zdqssed_haze","zdqssed haze",&
2315                     "kg/m2/s",2,zdqssed(:,igcm_haze))
2316               endif
2317            endif
2318            call writediagfi(ngrid,"zdqphot_ch4","zdqphot_ch4","",&
2319                                                3,zdqphot_ch4(:,:))
2320            call writediagfi(ngrid,"zdqconv_prec","zdqconv_prec","",&
2321                                                3,zdqconv_prec(:,:))
2322   !         call writediagfi(ngrid,"zdqhaze_col","zdqhaze col","kg/m2/s",
2323   !     &                   2,zdqhaze_col(:))
2324         endif
2325
2326         if (aerohaze) then
2327            call writediagfi(ngrid,"tau_col",&
2328               "Total aerosol optical depth","opacity",2,tau_col)
2329         endif
2330
2331      endif ! end of 'tracer'
2332
2333
2334      ! Output spectrum.
2335      if(specOLR.and.corrk)then
2336         call writediagspecIR(ngrid,"OLR3D","OLR(lon,lat,band)","W/m^2/cm^-1",3,OLR_nu)
2337         call writediagspecVI(ngrid,"OSR3D","OSR(lon,lat,band)","W/m^2/cm^-1",3,OSR_nu)
2338         call writediagspecVI(ngrid,"GSR3D","GSR(lon,lat,band)","W/m^2/cm^-1",3,GSR_nu)
2339      endif
2340
2341#else
2342   comm_HR_SW(1:ngrid,1:nlayer) = zdtsw(1:ngrid,1:nlayer)
2343   comm_HR_LW(1:ngrid,1:nlayer) = zdtlw(1:ngrid,1:nlayer)
2344   comm_ALBEQ(1:ngrid)=albedo_equivalent(1:ngrid)
2345   if (.not.calldifv) comm_LATENT_HF(:)=0.0
2346   ! if ((tracer).and.(water)) then  !AF24: removed
2347
2348   if ((tracer).and.(generic_condensation)) then
2349   ! .and.(.not. water)
2350
2351      ! If you have set generic_condensation (and not water) and you have set several GCS
2352      ! then the outputs given to WRF will be only the ones for the last generic tracer
2353      ! (Because it is rewritten every tracer in the loop)
2354      ! WRF can take only one moist tracer
2355
2356      do iq=1,nq
2357         call generic_tracer_index(nq,iq,igcm_generic_vap,igcm_generic_ice,call_ice_vap_generic)
2358
2359         if (call_ice_vap_generic) then ! to call only one time the ice/vap pair of a tracer
2360
2361            reffrad_generic_zeros_for_wrf(:,:) = 1.
2362
2363            comm_CLOUDFRAC(1:ngrid,1:nlayer) = cloudfrac(1:ngrid,1:nlayer)
2364            comm_TOTCLOUDFRAC(1:ngrid) = totcloudfrac(1:ngrid) !??????
2365            comm_SURFRAIN(1:ngrid) = zdqsrain_generic(1:ngrid,iq)
2366            comm_DQVAP(1:ngrid,1:nlayer) = pdq(1:ngrid,1:nlayer,igcm_generic_vap)
2367            comm_DQICE(1:ngrid,1:nlayer)=pdq(1:ngrid,1:nlayer,igcm_generic_ice)
2368            ! comm_H2OICE_REFF(1:ngrid,1:nlayer) = reffrad_generic_zeros_for_wrf(1:ngrid,1:nlayer) ! for now zeros !
2369            !comm_H2OICE_REFF(1:ngrid,1:nlayer) = 0*zdtrain_generic(1:ngrid,1:nlayer) ! for now zeros !
2370            comm_REEVAP(1:ngrid) = reevap_precip_generic(1:ngrid,iq)
2371            comm_DTRAIN(1:ngrid,1:nlayer) = zdtrain_generic(1:ngrid,1:nlayer)
2372            comm_DTLSC(1:ngrid,1:nlayer) = dt_generic_condensation(1:ngrid,1:nlayer)
2373            comm_RH(1:ngrid,1:nlayer) = RH_generic(1:ngrid,1:nlayer,iq)
2374
2375         endif
2376      end do ! do iq=1,nq loop on tracers
2377
2378   else
2379      comm_CLOUDFRAC(1:ngrid,1:nlayer)=0.
2380      comm_TOTCLOUDFRAC(1:ngrid)=0.
2381      comm_SURFRAIN(1:ngrid)=0.
2382      comm_DQVAP(1:ngrid,1:nlayer)=0.
2383      comm_DQICE(1:ngrid,1:nlayer)=0.
2384      ! comm_H2OICE_REFF(1:ngrid,1:nlayer)=0.
2385      comm_REEVAP(1:ngrid)=0.
2386      comm_DTRAIN(1:ngrid,1:nlayer)=0.
2387      comm_DTLSC(1:ngrid,1:nlayer)=0.
2388      comm_RH(1:ngrid,1:nlayer)=0.
2389
2390   endif ! if water, if generic_condensation, else
2391
2392   comm_FLUXTOP_DN(1:ngrid)=fluxtop_dn(1:ngrid)
2393   comm_FLUXABS_SW(1:ngrid)=fluxabs_sw(1:ngrid)
2394   comm_FLUXTOP_LW(1:ngrid)=fluxtop_lw(1:ngrid)
2395   comm_FLUXSURF_SW(1:ngrid)=fluxsurf_sw(1:ngrid)
2396   comm_FLUXSURF_LW(1:ngrid)=fluxsurf_lw(1:ngrid)
2397   comm_FLXGRD(1:ngrid)=fluxgrd(1:ngrid)
2398   sensibFlux(1:ngrid) = zflubid(1:ngrid) - capcal(1:ngrid)*zdtsdif(1:ngrid) !!! ????
2399   comm_HR_DYN(1:ngrid,1:nlayer) = zdtdyn(1:ngrid,1:nlayer)
2400
2401!      if (turb_resolved) then
2402!        open(17,file='lsf.txt',form='formatted',status='old')
2403!        rewind(17)
2404!        DO l=1,nlayer
2405!          read(17,*) lsf_dt(l),lsf_dq(l)
2406!        ENDDO
2407!        close(17)
2408!        do ig=1,ngrid
2409!          if ((tracer).and.(water)) then
2410!           pdq(ig,:,igcm_h2o_vap) = pdq(ig,:,igcm_h2o_vap) + lsf_dq(:)
2411!          endif
2412!          pdt(ig,:) = pdt(ig,:) + lsf_dt(:)
2413!          comm_HR_DYN(ig,:) = lsf_dt(:)
2414!        enddo
2415!      endif
2416#endif
2417
2418! XIOS outputs
2419#ifdef CPP_XIOS
2420      ! Send fields to XIOS: (NB these fields must also be defined as
2421      ! <field id="..." /> in context_lmdz_physics.xml to be correctly used)
2422      CALL send_xios_field("ls",zls)
2423
2424      CALL send_xios_field("ps",ps)
2425      CALL send_xios_field("area",cell_area)
2426      CALL send_xios_field("p",pplay)
2427      CALL send_xios_field("temperature",zt)
2428      CALL send_xios_field("u",zu)
2429      CALL send_xios_field("v",zv)
2430      CALL send_xios_field("omega",omega)
2431
2432!       IF (calltherm) THEN  !AF24: removed
2433      ! IF (water) THEN  !AF24: removed
2434
2435      CALL send_xios_field("ISR",fluxtop_dn)
2436      CALL send_xios_field("OLR",fluxtop_lw)
2437      CALL send_xios_field("ASR",fluxabs_sw)
2438
2439      if (specOLR .and. corrk) then
2440         call send_xios_field("OLR3D",OLR_nu)
2441         call send_xios_field("IR_Bandwidth",DWNI)
2442         call send_xios_field("VI_Bandwidth",DWNV)
2443         call send_xios_field("OSR3D",OSR_nu)
2444         call send_xios_field("GSR3D",GSR_nu)
2445      endif
2446
2447      if (lastcall.and.is_omp_master) then
2448        write(*,*) "physiq: call xios_context_finalize"
2449        call xios_context_finalize
2450      endif
2451#endif
2452
2453      if (check_physics_outputs) then
2454         ! Check the validity of updated fields at the end of the physics step
2455         call check_physics_fields("end of physiq:", zt, zu, zv, pplev, zq)
2456      endif
2457
2458      icount=icount+1
2459
2460      end subroutine physiq
2461
2462   end module physiq_mod
Note: See TracBrowser for help on using the repository browser.