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

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

Pluto PCM:
Get rid of real_area (should use cell_area probably)
AF

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