source: trunk/LMDZ.GENERIC/libf/phystd/physiq_mod.F90 @ 1677

Last change on this file since 1677 was 1669, checked in by emillour, 9 years ago

Generic GCM:
Added possibility to run without a startfi.nc file (mainly usefull for
tests with coupling with dynamico dynamical core):

  • added flag "startphy_file" flag (.false. if doing an "academic" start on the physics side).
  • turned phyetat0.F90 into module phyetat0_mod.F90
  • turned tabfi.F into module tabfi_mod.F90 and added handling of startphy_file==.false. case
  • extra initializations in physiq_mod for startphy_file==.false. case.

EM

  • Property svn:executable set to *
File size: 93.7 KB
Line 
1      module physiq_mod
2     
3      implicit none
4     
5      contains
6     
7      subroutine physiq(ngrid,nlayer,nq,   &
8                  nametrac,                &
9                  firstcall,lastcall,      &
10                  pday,ptime,ptimestep,    &
11                  pplev,pplay,pphi,        &
12                  pu,pv,pt,pq,             &
13                  flxw,                    &
14                  pdu,pdv,pdt,pdq,pdpsrf)
15 
16      use radinc_h, only : L_NSPECTI,L_NSPECTV,naerkind
17      use watercommon_h, only : RLVTT, Psat_water,epsi
18      use gases_h, only: gnom, gfrac
19      use radcommon_h, only: sigma, glat, grav, BWNV
20      use radii_mod, only: h2o_reffrad, co2_reffrad
21      use aerosol_mod, only: iaero_co2, iaero_h2o
22      use surfdat_h, only: phisfi, zmea, zstd, zsig, zgam, zthe, &
23                           dryness, watercaptag
24      use comdiurn_h, only: coslat, sinlat, coslon, sinlon
25      use comsaison_h, only: mu0, fract, dist_star, declin, right_ascen
26      use comsoil_h, only: nsoilmx, layer, mlayer, inertiedat
27      use geometry_mod, only: latitude, longitude, cell_area
28      USE comgeomfi_h, only: totarea, totarea_planet
29      USE tracer_h, only: noms, mmol, radius, rho_q, qext, &
30                          alpha_lift, alpha_devil, qextrhor, &
31                          igcm_h2o_ice, igcm_h2o_vap, igcm_dustbin, &
32                          igcm_co2_ice
33      use time_phylmdz_mod, only: ecritphy, iphysiq, nday
34      use phyetat0_mod, only: phyetat0
35      use phyredem, only: physdem0, physdem1
36      use slab_ice_h, only: capcalocean, capcalseaice,capcalsno, &
37                            noceanmx
38      use ocean_slab_mod, only :ocean_slab_init, ocean_slab_ice, &
39                                ini_surf_heat_transp_mod, &
40                                ocean_slab_get_vars,ocean_slab_final
41      use surf_heat_transp_mod,only :init_masquv
42      use planetwide_mod, only: planetwide_minval,planetwide_maxval,planetwide_sumval
43      use mod_phys_lmdz_para, only : is_master
44      use planete_mod, only: apoastr, periastr, year_day, peri_day, &
45                            obliquit, nres, z0
46      use comcstfi_mod, only: pi, g, rcp, r, rad, mugaz, cpp
47      use time_phylmdz_mod, only: daysec
48      use callkeys_mod
49      use vertical_layers_mod, only: presnivs, pseudoalt
50#ifdef CPP_XIOS     
51      use xios_output_mod, only: initialize_xios_output, &
52                                 update_xios_timestep, &
53                                 send_xios_field
54#endif
55      implicit none
56
57
58!==================================================================
59!     
60!     Purpose
61!     -------
62!     Central subroutine for all the physics parameterisations in the
63!     universal model. Originally adapted from the Mars LMDZ model.
64!
65!     The model can be run without or with tracer transport
66!     depending on the value of "tracer" in file "callphys.def".
67!
68!
69!   It includes:
70!
71!      I. Initialization :
72!         I.1 Firstcall initializations.
73!         I.2 Initialization for every call to physiq.
74!
75!      II. Compute radiative transfer tendencies (longwave and shortwave) :
76!         II.a Option 1 : Call correlated-k radiative transfer scheme.
77!         II.b Option 2 : Call Newtonian cooling scheme.
78!         II.c Option 3 : Atmosphere has no radiative effect.
79!
80!      III. Vertical diffusion (turbulent mixing) :
81!
82!      IV. Dry Convective adjustment :
83!
84!      V. Condensation and sublimation of gases (currently just CO2).
85!
86!      VI. Tracers
87!         VI.1. Water and water ice.
88!         VI.2. Aerosols and particles.
89!         VI.3. Updates (pressure variations, surface budget).
90!         VI.4. Slab Ocean.
91!         VI.5. Surface Tracer Update.
92!
93!      VII. Surface and sub-surface soil temperature.
94!
95!      VIII. Perform diagnostics and write output files.
96!
97!
98!   arguments
99!   ---------
100!
101!   INPUT
102!   -----
103!
104!    ngrid                 Size of the horizontal grid.
105!    nlayer                Number of vertical layers.
106!    nq                    Number of advected fields.
107!    nametrac              Name of corresponding advected fields.
108!
109!    firstcall             True at the first call.
110!    lastcall              True at the last call.
111!
112!    pday                  Number of days counted from the North. Spring equinoxe.
113!    ptime                 Universal time (0<ptime<1): ptime=0.5 at 12:00 UT.
114!    ptimestep             timestep (s).
115!
116!    pplay(ngrid,nlayer)   Pressure at the middle of the layers (Pa).
117!    pplev(ngrid,nlayer+1) Intermediate pressure levels (Pa).
118!    pphi(ngrid,nlayer)    Geopotential at the middle of the layers (m2.s-2).
119!
120!    pu(ngrid,nlayer)      u, zonal component of the wind (ms-1).
121!    pv(ngrid,nlayer)      v, meridional component of the wind (ms-1).
122!
123!    pt(ngrid,nlayer)      Temperature (K).
124!
125!    pq(ngrid,nlayer,nq)   Advected fields.
126!
127!    pudyn(ngrid,nlayer)    \
128!    pvdyn(ngrid,nlayer)     \ Dynamical temporal derivative for the
129!    ptdyn(ngrid,nlayer)     / corresponding variables.
130!    pqdyn(ngrid,nlayer,nq) /
131!    flxw(ngrid,nlayer)      vertical mass flux (kg/s) at layer lower boundary
132!
133!   OUTPUT
134!   ------
135!
136!    pdu(ngrid,nlayer)        \
137!    pdv(ngrid,nlayer)         \  Temporal derivative of the corresponding
138!    pdt(ngrid,nlayer)         /  variables due to physical processes.
139!    pdq(ngrid,nlayer)        /
140!    pdpsrf(ngrid)             /
141!
142!
143!     Authors
144!     -------
145!           Frederic Hourdin        15/10/93
146!           Francois Forget        1994
147!           Christophe Hourdin        02/1997
148!           Subroutine completely rewritten by F. Forget (01/2000)
149!           Water ice clouds: Franck Montmessin (update 06/2003)
150!           Radiatively active tracers: J.-B. Madeleine (10/2008-06/2009)
151!           New correlated-k radiative scheme: R. Wordsworth (2009)
152!           Many specifically Martian subroutines removed: R. Wordsworth (2009)       
153!           Improved water cycle: R. Wordsworth / B. Charnay (2010)
154!           To F90: R. Wordsworth (2010)
155!           New turbulent diffusion scheme: J. Leconte (2012)
156!           Loops converted to F90 matrix format: J. Leconte (2012)
157!           No more ngridmx/nqmx, F90 commons and adaptation to parallel: A. Spiga (2012)
158!           Purge of the code : M. Turbet (2015)
159!==================================================================
160
161
162!    0.  Declarations :
163!    ------------------
164
165include "netcdf.inc"
166
167! Arguments :
168! -----------
169
170!   INPUTS:
171!   -------
172
173      integer,intent(in) :: ngrid             ! Number of atmospheric columns.
174      integer,intent(in) :: nlayer            ! Number of atmospheric layers.
175      integer,intent(in) :: nq                ! Number of tracers.
176      character*20,intent(in) :: nametrac(nq) ! Names of the tracers taken from dynamics.
177     
178      logical,intent(in) :: firstcall ! Signals first call to physics.
179      logical,intent(in) :: lastcall  ! Signals last call to physics.
180     
181      real,intent(in) :: pday                  ! Number of elapsed sols since reference Ls=0.
182      real,intent(in) :: ptime                 ! "Universal time", given as fraction of sol (e.g.: 0.5 for noon).
183      real,intent(in) :: ptimestep             ! Physics timestep (s).
184      real,intent(in) :: pplev(ngrid,nlayer+1) ! Inter-layer pressure (Pa).
185      real,intent(in) :: pplay(ngrid,nlayer)   ! Mid-layer pressure (Pa).
186      real,intent(in) :: pphi(ngrid,nlayer)    ! Geopotential at mid-layer (m2s-2).
187      real,intent(in) :: pu(ngrid,nlayer)      ! Zonal wind component (m/s).
188      real,intent(in) :: pv(ngrid,nlayer)      ! Meridional wind component (m/s).
189      real,intent(in) :: pt(ngrid,nlayer)      ! Temperature (K).
190      real,intent(in) :: pq(ngrid,nlayer,nq)   ! Tracers (kg/kg_of_air).
191      real,intent(in) :: flxw(ngrid,nlayer)    ! Vertical mass flux (ks/s) at lower boundary of layer
192
193!   OUTPUTS:
194!   --------
195
196!     Physical tendencies :
197
198      real,intent(out) :: pdu(ngrid,nlayer)    ! Zonal wind tendencies (m/s/s).
199      real,intent(out) :: pdv(ngrid,nlayer)    ! Meridional wind tendencies (m/s/s).
200      real,intent(out) :: pdt(ngrid,nlayer)    ! Temperature tendencies (K/s).
201      real,intent(out) :: pdq(ngrid,nlayer,nq) ! Tracer tendencies (kg/kg_of_air/s).
202      real,intent(out) :: pdpsrf(ngrid)        ! Surface pressure tendency (Pa/s).
203
204! Local saved variables:
205! ----------------------
206
207      integer,save :: day_ini                                      ! Initial date of the run (sol since Ls=0).
208      integer,save :: icount                                       ! Counter of calls to physiq during the run.
209!$OMP THREADPRIVATE(day_ini,icount)
210
211      real, dimension(:),allocatable,save ::  tsurf                ! Surface temperature (K).
212      real, dimension(:,:),allocatable,save ::  tsoil              ! Sub-surface temperatures (K).
213      real, dimension(:,:),allocatable,save :: albedo              ! Surface Spectral albedo. By MT2015.
214      real, dimension(:),allocatable,save :: albedo_equivalent     ! Spectral Mean albedo.
215      real, dimension(:),allocatable,save :: albedo_snow_SPECTV    ! Snow Spectral albedo.
216      real, dimension(:),allocatable,save :: albedo_co2_ice_SPECTV ! CO2 ice Spectral albedo.
217     
218!$OMP THREADPRIVATE(tsurf,tsoil,albedo,albedo_equivalent,albedo_snow_SPECTV,albedo_co2_ice_SPECTV)
219
220      real,dimension(:),allocatable,save :: albedo_bareground ! Bare Ground Albedo. By MT 2015.
221      real,dimension(:),allocatable,save :: rnat              ! Defines the type of the grid (ocean,continent,...). By BC.
222     
223!$OMP THREADPRIVATE(albedo_bareground,rnat)
224
225      real,dimension(:),allocatable,save :: emis        ! Thermal IR surface emissivity.
226      real,dimension(:,:),allocatable,save :: dtrad     ! Net atmospheric radiative heating rate (K.s-1).
227      real,dimension(:),allocatable,save :: fluxrad_sky ! Radiative flux from sky absorbed by surface (W.m-2).
228      real,dimension(:),allocatable,save :: fluxrad     ! Net radiative surface flux (W.m-2).
229      real,dimension(:),allocatable,save :: capcal      ! Surface heat capacity (J m-2 K-1).
230      real,dimension(:),allocatable,save :: fluxgrd     ! Surface conduction flux (W.m-2).
231      real,dimension(:,:),allocatable,save :: qsurf     ! Tracer on surface (e.g. kg.m-2).
232      real,dimension(:,:),allocatable,save :: q2        ! Turbulent Kinetic Energy.
233     
234!$OMP THREADPRIVATE(emis,dtrad,fluxrad_sky,fluxrad,capcal,fluxgrd,qsurf,q2)
235
236
237! Local variables :
238! -----------------
239
240!     Aerosol (dust or ice) extinction optical depth  at reference wavelength
241!     for the "naerkind" optically active aerosols:
242
243      real aerosol(ngrid,nlayer,naerkind) ! Aerosols.
244      real zh(ngrid,nlayer)               ! Potential temperature (K).
245      real pw(ngrid,nlayer)               ! Vertical velocity (m/s). (NOTE : >0 WHEN DOWNWARDS !!)
246
247      integer l,ig,ierr,iq,nw,isoil
248     
249      ! FOR DIAGNOSTIC :
250     
251      real,dimension(:),allocatable,save :: fluxsurf_lw     ! Incident Long Wave (IR) surface flux (W.m-2).
252      real,dimension(:),allocatable,save :: fluxsurf_sw     ! Incident Short Wave (stellar) surface flux (W.m-2).
253      real,dimension(:),allocatable,save :: fluxsurfabs_sw  ! Absorbed Short Wave (stellar) flux by the surface (W.m-2).
254      real,dimension(:),allocatable,save :: fluxtop_lw      ! Outgoing LW (IR) flux to space (W.m-2).
255      real,dimension(:),allocatable,save :: fluxabs_sw      ! Absorbed SW (stellar) flux (W.m-2).
256      real,dimension(:),allocatable,save :: fluxtop_dn      ! Incoming SW (stellar) radiation at the top of the atmosphere (W.m-2).
257      real,dimension(:),allocatable,save :: fluxdyn         ! Horizontal heat transport by dynamics (W.m-2).
258      real,dimension(:,:),allocatable,save :: OLR_nu        ! Outgoing LW radiation in each band (Normalized to the band width (W/m2/cm-1)).
259      real,dimension(:,:),allocatable,save :: OSR_nu        ! Outgoing SW radiation in each band (Normalized to the band width (W/m2/cm-1)).
260      real,dimension(:,:),allocatable,save :: zdtlw         ! LW heating tendencies (K/s).
261      real,dimension(:,:),allocatable,save :: zdtsw         ! SW heating tendencies (K/s).
262      real,dimension(:),allocatable,save :: sensibFlux      ! Turbulent flux given by the atmosphere to the surface (W.m-2).
263     
264!$OMP THREADPRIVATE(fluxsurf_lw,fluxsurf_sw,fluxsurfabs_sw,fluxtop_lw,fluxabs_sw,fluxtop_dn,fluxdyn,OLR_nu,OSR_nu,&
265       
266        !$OMP zdtlw,zdtsw,sensibFlux)
267
268      real zls                       ! Solar longitude (radians).
269      real zlss                      ! Sub solar point longitude (radians).
270      real zday                      ! Date (time since Ls=0, calculated in sols).
271      real zzlay(ngrid,nlayer)       ! Altitude at the middle of the atmospheric layers.
272      real zzlev(ngrid,nlayer+1)     ! Altitude at the atmospheric layer boundaries.
273
274! TENDENCIES due to various processes :
275
276      ! For Surface Temperature : (K/s)
277      real zdtsurf(ngrid)     ! Cumulated tendencies.
278      real zdtsurfmr(ngrid)   ! Mass_redistribution routine.
279      real zdtsurfc(ngrid)    ! Condense_co2 routine.
280      real zdtsdif(ngrid)     ! Turbdiff/vdifc routines.
281      real zdtsurf_hyd(ngrid) ! Hydrol routine.
282           
283      ! For Atmospheric Temperatures : (K/s)   
284      real dtlscale(ngrid,nlayer)                             ! Largescale routine.
285      real zdtc(ngrid,nlayer)                                 ! Condense_co2 routine.
286      real zdtdif(ngrid,nlayer)                                      ! Turbdiff/vdifc routines.
287      real zdtmr(ngrid,nlayer)                                ! Mass_redistribution routine.
288      real zdtrain(ngrid,nlayer)                              ! Rain routine.
289      real dtmoist(ngrid,nlayer)                              ! Moistadj routine.
290      real dt_ekman(ngrid,noceanmx), dt_hdiff(ngrid,noceanmx) ! Slab_ocean routine.
291      real zdtsw1(ngrid,nlayer), zdtlw1(ngrid,nlayer)         ! Callcorrk routine.
292                             
293      ! For Surface Tracers : (kg/m2/s)
294      real dqsurf(ngrid,nq)                 ! Cumulated tendencies.
295      real zdqsurfc(ngrid)                  ! Condense_co2 routine.
296      real zdqsdif(ngrid,nq)                ! Turbdiff/vdifc routines.
297      real zdqssed(ngrid,nq)                ! Callsedim routine.
298      real zdqsurfmr(ngrid,nq)              ! Mass_redistribution routine.
299      real zdqsrain(ngrid), zdqssnow(ngrid) ! Rain routine.
300      real dqs_hyd(ngrid,nq)                ! Hydrol routine.
301                 
302      ! For Tracers : (kg/kg_of_air/s)
303      real zdqc(ngrid,nlayer,nq)      ! Condense_co2 routine.
304      real zdqadj(ngrid,nlayer,nq)    ! Convadj routine.
305      real zdqdif(ngrid,nlayer,nq)    ! Turbdiff/vdifc routines.
306      real zdqevap(ngrid,nlayer)      ! Turbdiff routine.
307      real zdqsed(ngrid,nlayer,nq)    ! Callsedim routine.
308      real zdqmr(ngrid,nlayer,nq)     ! Mass_redistribution routine.
309      real zdqrain(ngrid,nlayer,nq)   ! Rain routine.
310      real dqmoist(ngrid,nlayer,nq)   ! Moistadj routine.
311      real dqvaplscale(ngrid,nlayer)  ! Largescale routine.
312      real dqcldlscale(ngrid,nlayer)  ! Largescale routine.
313                       
314      ! For Winds : (m/s/s)
315      real zdvadj(ngrid,nlayer),zduadj(ngrid,nlayer) ! Convadj routine.
316      real zdumr(ngrid,nlayer),zdvmr(ngrid,nlayer)   ! Mass_redistribution routine.
317      real zdvdif(ngrid,nlayer),zdudif(ngrid,nlayer) ! Turbdiff/vdifc routines.
318      real zdhdif(ngrid,nlayer)                      ! Turbdiff/vdifc routines.
319      real zdhadj(ngrid,nlayer)                      ! Convadj routine.
320     
321      ! For Pressure and Mass :
322      real zdmassmr(ngrid,nlayer) ! Atmospheric Mass tendency for mass_redistribution (kg_of_air/m2/s).
323      real zdmassmr_col(ngrid)    ! Atmospheric Column Mass tendency for mass_redistribution (kg_of_air/m2/s).
324      real zdpsrfmr(ngrid)        ! Pressure tendency for mass_redistribution routine (Pa/s).
325
326     
327   
328! Local variables for LOCAL CALCULATIONS:
329! ---------------------------------------
330      real zflubid(ngrid)
331      real zplanck(ngrid),zpopsk(ngrid,nlayer)
332      real ztim1,ztim2,ztim3, z1,z2
333      real ztime_fin
334      real zdh(ngrid,nlayer)
335      real gmplanet
336      real taux(ngrid),tauy(ngrid)
337
338
339! local variables for DIAGNOSTICS : (diagfi & stat)
340! -------------------------------------------------
341      real ps(ngrid)                                     ! Surface Pressure.
342      real zt(ngrid,nlayer)                              ! Atmospheric Temperature.
343      real zu(ngrid,nlayer),zv(ngrid,nlayer)             ! Zonal and Meridional Winds.
344      real zq(ngrid,nlayer,nq)                           ! Atmospheric Tracers.
345      real zdtadj(ngrid,nlayer)                          ! Convadj Diagnostic.
346      real zdtdyn(ngrid,nlayer)                          ! Dynamical Heating (K/s).
347      real zdudyn(ngrid,nlayer)                          ! Dynamical Zonal Wind tendency (m.s-2).
348      real,allocatable,dimension(:,:),save :: ztprevious ! Previous loop Atmospheric Temperature (K)                                                         ! Useful for Dynamical Heating calculation.
349      real,allocatable,dimension(:,:),save :: zuprevious ! Previous loop Zonal Wind (m.s-1)                                                                  ! Useful for Zonal Wind tendency calculation.
350!$OMP THREADPRIVATE(ztprevious,zuprevious)
351
352      real reff(ngrid,nlayer)                       ! Effective dust radius (used if doubleq=T).
353      real vmr(ngrid,nlayer)                        ! volume mixing ratio
354      real time_phys
355      real,allocatable,dimension(:),save :: tau_col ! Total Aerosol Optical Depth.
356!$OMP THREADPRIVATE(tau_col)
357     
358      real ISR,ASR,OLR,GND,DYN,GSR,Ts1,Ts2,Ts3,TsS ! for Diagnostic.
359     
360      real qcol(ngrid,nq) ! Tracer Column Mass (kg/m2).
361
362!     included by RW for H2O Manabe scheme
363      real rneb_man(ngrid,nlayer) ! H2O cloud fraction (moistadj).
364      real rneb_lsc(ngrid,nlayer) ! H2O cloud fraction (large scale).
365
366
367!     to test energy conservation (RW+JL)
368      real mass(ngrid,nlayer),massarea(ngrid,nlayer)
369      real dEtot, dEtots, AtmToSurf_TurbFlux
370      real,save :: dEtotSW, dEtotsSW, dEtotLW, dEtotsLW
371!$OMP THREADPRIVATE(dEtotSW, dEtotsSW, dEtotLW, dEtotsLW)
372      real dEzRadsw(ngrid,nlayer),dEzRadlw(ngrid,nlayer),dEzdiff(ngrid,nlayer)
373      real dEdiffs(ngrid),dEdiff(ngrid)
374      real madjdE(ngrid), lscaledE(ngrid),madjdEz(ngrid,nlayer), lscaledEz(ngrid,nlayer)
375     
376!JL12 conservation test for mean flow kinetic energy has been disabled temporarily
377
378      real dtmoist_max,dtmoist_min     
379      real dItot, dItot_tmp, dVtot, dVtot_tmp
380
381      real,allocatable,save :: hice(:) ! Oceanic Ice height. by BC
382 !$OMP THREADPRIVATE(hice)     
383
384      real h2otot                      ! Total Amount of water. For diagnostic.
385      real icesrf,liqsrf,icecol,vapcol ! Total Amounts of water (ice,liq,vap). For diagnostic.
386      real dWtot, dWtot_tmp, dWtots, dWtots_tmp
387      logical,save :: watertest
388!$OMP THREADPRIVATE(watertest)
389
390      real qsat(ngrid,nlayer) ! Water Vapor Volume Mixing Ratio at saturation (kg/kg_of_air).
391      real RH(ngrid,nlayer)   ! Relative humidity.
392      real H2Omaxcol(ngrid)   ! Maximum possible H2O column amount (at 100% saturation) (kg/m2).
393      real psat_tmp
394     
395      logical clearsky ! For double radiative transfer call. By BC
396     
397      ! For Clear Sky Case.
398      real fluxsurf_lw1(ngrid), fluxsurf_sw1(ngrid), fluxsurfabs_sw1(ngrid)  ! For SW/LW flux.
399      real fluxtop_lw1(ngrid), fluxabs_sw1(ngrid)                            ! For SW/LW flux.
400      real albedo_equivalent1(ngrid)                                         ! For Equivalent albedo calculation.
401      real tau_col1(ngrid)                                                   ! For aerosol optical depth diagnostic.
402      real OLR_nu1(ngrid,L_NSPECTI), OSR_nu1(ngrid,L_NSPECTV)                ! For Outgoing Radiation diagnostics.
403      real tf, ntf
404
405      real,allocatable,dimension(:,:),save :: cloudfrac  ! Fraction of clouds (%).
406      real,allocatable,dimension(:),save :: totcloudfrac ! Column fraction of clouds (%).
407!$OMP THREADPRIVATE(cloudfrac,totcloudfrac)
408
409      real nconsMAX, vdifcncons(ngrid), cadjncons(ngrid) ! Vdfic water conservation test. By RW
410
411      real,allocatable,dimension(:,:),save :: qsurf_hist
412!$OMP THREADPRIVATE(qsurf_hist)
413
414      real muvar(ngrid,nlayer+1) ! For Runaway Greenhouse 1D study. By RW
415
416      real,allocatable,dimension(:,:,:),save :: reffrad  ! Aerosol effective radius (m). By RW
417      real,allocatable,dimension(:,:,:),save :: nueffrad ! Aerosol effective radius variance. By RW
418!$OMP THREADPRIVATE(reffrad,nueffrad)
419
420      real reffcol(ngrid,naerkind)
421
422!     Sourceevol for 'accelerated ice evolution'. By RW
423      real,allocatable,dimension(:),save :: ice_initial
424      real delta_ice,ice_tot
425      real,allocatable,dimension(:),save :: ice_min     
426      integer num_run
427      logical,save :: ice_update
428!$OMP THREADPRIVATE(ice_initial,ice_min,ice_update)
429
430!     For slab ocean. By BC
431      real, dimension(:),allocatable,save ::  pctsrf_sic
432      real, dimension(:,:),allocatable,save :: tslab
433      real, dimension(:),allocatable,save ::  tsea_ice
434      real, dimension(:),allocatable,save :: sea_ice
435      real, dimension(:),allocatable,save :: zmasq
436      integer, dimension(:),allocatable,save ::knindex
437!$OMP THREADPRIVATE(pctsrf_sic,tslab,tsea_ice,sea_ice,zmasq,knindex)
438
439      real :: tsurf2(ngrid)
440      real :: flux_o(ngrid),flux_g(ngrid),fluxgrdocean(ngrid)
441      real :: flux_sens_lat(ngrid)
442      real :: qsurfint(ngrid,nq)
443
444
445!==================================================================================================
446
447! -----------------
448! I. INITIALISATION
449! -----------------
450
451! --------------------------------
452! I.1   First Call Initialisation.
453! --------------------------------
454      if (firstcall) then
455
456        ! Allocate saved arrays.
457        ALLOCATE(tsurf(ngrid))
458        ALLOCATE(tsoil(ngrid,nsoilmx))   
459        ALLOCATE(albedo(ngrid,L_NSPECTV))
460         ALLOCATE(albedo_equivalent(ngrid))       
461         ALLOCATE(albedo_snow_SPECTV(L_NSPECTV))
462         ALLOCATE(albedo_co2_ice_SPECTV(L_NSPECTV))
463         ALLOCATE(albedo_bareground(ngrid))           
464        ALLOCATE(rnat(ngrid))         
465        ALLOCATE(emis(ngrid))   
466        ALLOCATE(dtrad(ngrid,nlayer))
467        ALLOCATE(fluxrad_sky(ngrid))
468        ALLOCATE(fluxrad(ngrid))   
469        ALLOCATE(capcal(ngrid))   
470        ALLOCATE(fluxgrd(ngrid)) 
471        ALLOCATE(qsurf(ngrid,nq)) 
472        ALLOCATE(q2(ngrid,nlayer+1))
473        ALLOCATE(ztprevious(ngrid,nlayer))
474        ALLOCATE(zuprevious(ngrid,nlayer))
475        ALLOCATE(cloudfrac(ngrid,nlayer))
476        ALLOCATE(totcloudfrac(ngrid))
477        ALLOCATE(hice(ngrid))
478        ALLOCATE(qsurf_hist(ngrid,nq))
479        ALLOCATE(reffrad(ngrid,nlayer,naerkind))
480        ALLOCATE(nueffrad(ngrid,nlayer,naerkind))
481        ALLOCATE(ice_initial(ngrid))
482        ALLOCATE(ice_min(ngrid))
483        ALLOCATE(fluxsurf_lw(ngrid))
484        ALLOCATE(fluxsurf_sw(ngrid))
485        ALLOCATE(fluxsurfabs_sw(ngrid))
486        ALLOCATE(fluxtop_lw(ngrid))
487        ALLOCATE(fluxabs_sw(ngrid))
488        ALLOCATE(fluxtop_dn(ngrid))
489        ALLOCATE(fluxdyn(ngrid))
490        ALLOCATE(OLR_nu(ngrid,L_NSPECTI))
491        ALLOCATE(OSR_nu(ngrid,L_NSPECTV))
492        ALLOCATE(sensibFlux(ngrid))
493        ALLOCATE(zdtlw(ngrid,nlayer))
494        ALLOCATE(zdtsw(ngrid,nlayer))
495        ALLOCATE(tau_col(ngrid))
496        ALLOCATE(pctsrf_sic(ngrid))
497        ALLOCATE(tslab(ngrid,noceanmx))
498        ALLOCATE(tsea_ice(ngrid))
499        ALLOCATE(sea_ice(ngrid))
500        ALLOCATE(zmasq(ngrid))
501        ALLOCATE(knindex(ngrid))
502
503        ! This is defined in comsaison_h
504        ALLOCATE(mu0(ngrid))
505        ALLOCATE(fract(ngrid))           
506         ! This is defined in radcommon_h
507        ALLOCATE(glat(ngrid))
508       
509
510!        Variables set to 0
511!        ~~~~~~~~~~~~~~~~~~
512         dtrad(:,:) = 0.0
513         fluxrad(:) = 0.0
514         tau_col(:) = 0.0
515         zdtsw(:,:) = 0.0
516         zdtlw(:,:) = 0.0
517
518
519!        Initialize aerosol indexes.
520!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~
521         call iniaerosol()
522
523     
524!        Initialize tracer names, indexes and properties.
525!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
526         IF (.NOT.ALLOCATED(noms)) ALLOCATE(noms(nq)) ! (because noms is an argument of physdem1 whether or not tracer is on)
527         if (tracer) then
528            call initracer(ngrid,nq,nametrac)
529         endif
530
531
532!        Read 'startfi.nc' file.
533!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
534         call phyetat0(startphy_file,                                 &
535                       ngrid,nlayer,"startfi.nc",0,0,nsoilmx,nq,      &
536                       day_ini,time_phys,tsurf,tsoil,emis,q2,qsurf,   &
537                       cloudfrac,totcloudfrac,hice,                   &
538                       rnat,pctsrf_sic,tslab, tsea_ice,sea_ice)
539         if (.not.startphy_file) then
540           ! additionnal "academic" initialization of physics
541           if (is_master) write(*,*) "Physiq: initializing tsurf(:) to pt(:,1) !!"
542           tsurf(:)=pt(:,1)
543           if (is_master) write(*,*) "Physiq: initializing tsoil(:) to pt(:,1) !!"
544           do isoil=1,nsoilmx
545             tsoil(1:ngrid,isoil)=tsurf(1:ngrid)
546           enddo
547           if (is_master) write(*,*) "Physiq: initializing day_ini to pdat !"
548           day_ini=pday
549         endif
550
551         if (pday.ne.day_ini) then
552           write(*,*) "ERROR in physiq.F90:"
553           write(*,*) "bad synchronization between physics and dynamics"
554           write(*,*) "dynamics day: ",pday
555           write(*,*) "physics day:  ",day_ini
556           stop
557         endif
558
559         write (*,*) 'In physiq day_ini =', day_ini
560
561!        Initialize albedo calculation.
562!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
563         albedo(:,:)=0.0
564          albedo_bareground(:)=0.0
565          albedo_snow_SPECTV(:)=0.0
566          albedo_co2_ice_SPECTV(:)=0.0
567         call surfini(ngrid,nq,qsurf,albedo,albedo_bareground,albedo_snow_SPECTV,albedo_co2_ice_SPECTV)
568         
569!        Initialize orbital calculation.
570!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
571         call iniorbit(apoastr,periastr,year_day,peri_day,obliquit)
572
573
574         if(tlocked)then
575            print*,'Planet is tidally locked at resonance n=',nres
576            print*,'Make sure you have the right rotation rate!!!'
577         endif
578
579!        Initialize oceanic variables.
580!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
581
582         if (ok_slab_ocean)then
583
584           call ocean_slab_init(ngrid,ptimestep, tslab, &
585                                sea_ice, pctsrf_sic)
586
587           call ini_surf_heat_transp_mod()
588           
589           knindex(:) = 0
590
591           do ig=1,ngrid
592              zmasq(ig)=1
593              knindex(ig) = ig
594              if (nint(rnat(ig)).eq.0) then
595                 zmasq(ig)=0
596              endif
597           enddo
598
599           CALL init_masquv(ngrid,zmasq)
600
601         endif ! end of 'ok_slab_ocean'.
602
603
604!        Initialize soil.
605!        ~~~~~~~~~~~~~~~~
606         if (callsoil) then
607         
608            call soil(ngrid,nsoilmx,firstcall,lastcall,inertiedat, &
609                      ptimestep,tsurf,tsoil,capcal,fluxgrd)
610
611            if (ok_slab_ocean) then
612               do ig=1,ngrid
613                  if (nint(rnat(ig)).eq.2) then
614                     capcal(ig)=capcalocean
615                     if (pctsrf_sic(ig).gt.0.5) then
616                        capcal(ig)=capcalseaice
617                        if (qsurf(ig,igcm_h2o_ice).gt.0.) then
618                           capcal(ig)=capcalsno
619                        endif
620                     endif
621                  endif
622               enddo
623            endif ! end of 'ok_slab_ocean'.
624
625         else ! else of 'callsoil'.
626         
627            print*,'WARNING! Thermal conduction in the soil turned off'
628            capcal(:)=1.e6
629            fluxgrd(:)=intheat
630            print*,'Flux from ground = ',intheat,' W m^-2'
631           
632         endif ! end of 'callsoil'.
633         
634         icount=1
635
636!        Decide whether to update ice at end of run.
637!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
638         ice_update=.false.
639         if(sourceevol)then
640!$OMP MASTER
641            open(128,file='num_run',form='formatted', &
642                     status="old",iostat=ierr)
643            if (ierr.ne.0) then
644               write(*,*) "physiq: Error! No num_run file!"
645               write(*,*) " (which is needed for sourceevol option)"
646               stop
647            endif
648            read(128,*) num_run
649            close(128)
650!$OMP END MASTER
651!$OMP BARRIER
652       
653            if(num_run.ne.0.and.mod(num_run,2).eq.0)then
654               print*,'Updating ice at end of this year!'
655               ice_update=.true.
656               ice_min(:)=1.e4
657            endif
658           
659         endif ! end of 'sourceevol'.
660
661
662         ! Here is defined the type of the surface : Continent or Ocean.
663         ! BC2014 : This is now already done in newstart.
664         ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
665         if (.not.ok_slab_ocean) then
666         
667           rnat(:)=1.
668           do ig=1,ngrid
669              if(inertiedat(ig,1).gt.1.E4)then
670                 rnat(ig)=0
671              endif
672           enddo
673
674           print*,'WARNING! Surface type currently decided by surface inertia'
675           print*,'This should be improved e.g. in newstart.F'
676           
677         endif ! end of 'ok_slab_ocean'.
678
679
680!        Initialize surface history variable.
681!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
682         qsurf_hist(:,:)=qsurf(:,:)
683
684!        Initialize variable for dynamical heating and zonal wind tendency diagnostic
685!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
686         ztprevious(:,:)=pt(:,:)
687         zuprevious(:,:)=pu(:,:)
688
689!        Set temperature just above condensation temperature (for Early Mars)
690!        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
691         if(nearco2cond) then
692            write(*,*)' WARNING! Starting at Tcond+1K'
693            do l=1, nlayer
694               do ig=1,ngrid
695                  pdt(ig,l)= ((-3167.8)/(log(.01*pplay(ig,l))-23.23)+4     &
696                      -pt(ig,l)) / ptimestep
697               enddo
698            enddo
699         endif
700
701         if(meanOLR)then         
702            call system('rm -f rad_bal.out') ! to record global radiative balance.           
703            call system('rm -f tem_bal.out') ! to record global mean/max/min temperatures.           
704            call system('rm -f h2o_bal.out') ! to record global hydrological balance.
705         endif
706
707
708         watertest=.false.       
709         if(water)then ! Initialize variables for water cycle
710           
711            if(enertest)then
712               watertest = .true.
713            endif
714
715            if(ice_update)then
716               ice_initial(:)=qsurf(:,igcm_h2o_ice)
717            endif
718
719         endif
720         
721         call su_watercycle ! even if we don't have a water cycle, we might
722                            ! need epsi for the wvp definitions in callcorrk.F
723
724         if (ngrid.ne.1) then ! Note : no need to create a restart file in 1d.
725            call physdem0("restartfi.nc",longitude,latitude,nsoilmx,ngrid,nlayer,nq, &
726                          ptimestep,pday+nday,time_phys,cell_area,          &
727                          albedo_bareground,inertiedat,zmea,zstd,zsig,zgam,zthe)
728         endif
729         
730         ! XIOS outputs
731#ifdef CPP_XIOS
732
733         write(*,*) "physiq: call initialize_xios_output"
734         call initialize_xios_output(pday,ptime,ptimestep,daysec, &
735                                     presnivs,pseudoalt)
736#endif
737      endif ! end of 'firstcall'
738
739! ------------------------------------------------------
740! I.2   Initializations done at every physical timestep:
741! ------------------------------------------------------
742
743#ifdef CPP_XIOS     
744      ! update XIOS time/calendar
745      call update_xios_timestep
746#endif     
747
748      ! Initialize various variables
749      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
750     
751      if ( .not.nearco2cond ) then
752         pdt(1:ngrid,1:nlayer) = 0.0
753      endif     
754      zdtsurf(1:ngrid)      = 0.0
755      pdq(1:ngrid,1:nlayer,1:nq) = 0.0
756      dqsurf(1:ngrid,1:nq)= 0.0
757      pdu(1:ngrid,1:nlayer) = 0.0
758      pdv(1:ngrid,1:nlayer) = 0.0
759      pdpsrf(1:ngrid)       = 0.0
760      zflubid(1:ngrid)      = 0.0     
761      flux_sens_lat(1:ngrid) = 0.0
762      taux(1:ngrid) = 0.0
763      tauy(1:ngrid) = 0.0
764
765      zday=pday+ptime ! Compute time, in sols (and fraction thereof).
766
767      ! Compute Stellar Longitude (Ls), and orbital parameters.
768      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
769      if (season) then
770         call stellarlong(zday,zls)
771      else
772         call stellarlong(float(day_ini),zls)
773      end if
774
775      call orbite(zls,dist_star,declin,right_ascen)
776     
777      if (tlocked) then
778              zlss=Mod(-(2.*pi*(zday/year_day)*nres - right_ascen),2.*pi)
779      elseif (diurnal) then
780              zlss=-2.*pi*(zday-.5)
781      else if(diurnal .eqv. .false.) then
782              zlss=9999.
783      endif
784
785
786      ! Compute variations of g with latitude (oblate case).
787      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
788      if (oblate .eqv. .false.) then     
789         glat(:) = g         
790      else if (flatten .eq. 0.0 .or. J2 .eq. 0.0 .or. Rmean .eq. 0.0 .or. MassPlanet .eq. 0.0) then     
791         print*,'I need values for flatten, J2, Rmean and MassPlanet to compute glat (else set oblate=.false.)'
792         call abort
793      else
794         gmplanet = MassPlanet*grav*1e24
795         do ig=1,ngrid
796            glat(ig)= gmplanet/(Rmean**2) * (1.D0 + 0.75 *J2 - 2.0*flatten/3. + (2.*flatten - 15./4.* J2) * cos(2. * (pi/2. - latitude(ig))))
797         end do
798      endif
799
800      ! Compute geopotential between layers.
801      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
802      zzlay(1:ngrid,1:nlayer)=pphi(1:ngrid,1:nlayer)
803      do l=1,nlayer         
804         zzlay(1:ngrid,l)= zzlay(1:ngrid,l)/glat(1:ngrid)
805      enddo
806
807      zzlev(1:ngrid,1)=0.
808      zzlev(1:ngrid,nlayer+1)=1.e7 ! Dummy top of last layer above 10000 km...
809
810      do l=2,nlayer
811         do ig=1,ngrid
812            z1=(pplay(ig,l-1)+pplev(ig,l))/(pplay(ig,l-1)-pplev(ig,l))
813            z2=(pplev(ig,l)+pplay(ig,l))/(pplev(ig,l)-pplay(ig,l))
814            zzlev(ig,l)=(z1*zzlay(ig,l-1)+z2*zzlay(ig,l))/(z1+z2)
815         enddo
816      enddo     
817
818      ! Compute potential temperature
819      ! Note : Potential temperature calculation may not be the same in physiq and dynamic...
820      ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
821      do l=1,nlayer         
822         do ig=1,ngrid
823            zpopsk(ig,l)=(pplay(ig,l)/pplev(ig,1))**rcp
824            zh(ig,l)=pt(ig,l)/zpopsk(ig,l)
825            mass(ig,l)  = (pplev(ig,l) - pplev(ig,l+1))/glat(ig)
826            massarea(ig,l)=mass(ig,l)*cell_area(ig)
827         enddo
828      enddo
829
830     ! Compute vertical velocity (m/s) from vertical mass flux
831     ! w = F / (rho*area) and rho = P/(r*T)
832     ! But first linearly interpolate mass flux to mid-layers
833      do l=1,nlayer-1
834         pw(1:ngrid,l)=0.5*(flxw(1:ngrid,l)+flxw(1:ngrid,l+1))
835      enddo
836      pw(1:ngrid,nlayer)=0.5*flxw(1:ngrid,nlayer) ! since flxw(nlayer+1)=0
837      do l=1,nlayer
838         pw(1:ngrid,l)=(pw(1:ngrid,l)*r*pt(1:ngrid,l)) /  &
839                       (pplay(1:ngrid,l)*cell_area(1:ngrid))
840      enddo
841
842!---------------------------------
843! II. Compute radiative tendencies
844!---------------------------------
845
846      if (callrad) then
847         if( mod(icount-1,iradia).eq.0.or.lastcall) then
848
849          ! Compute local stellar zenith angles
850          ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
851            if (tlocked) then
852            ! JL14 corrects tidally resonant (and inclined) cases. nres=omega_rot/omega_orb
853               ztim1=SIN(declin)
854               ztim2=COS(declin)*COS(zlss)
855               ztim3=COS(declin)*SIN(zlss)
856
857               call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
858                            ztim1,ztim2,ztim3,mu0,fract, flatten)
859
860            elseif (diurnal) then
861               ztim1=SIN(declin)
862               ztim2=COS(declin)*COS(2.*pi*(zday-.5))
863               ztim3=-COS(declin)*SIN(2.*pi*(zday-.5))
864
865               call stelang(ngrid,sinlon,coslon,sinlat,coslat,    &
866                            ztim1,ztim2,ztim3,mu0,fract, flatten)
867            else if(diurnal .eqv. .false.) then
868
869               call mucorr(ngrid,declin,latitude,mu0,fract,10000.,rad,flatten)
870               ! WARNING: this function appears not to work in 1D
871
872            endif
873           
874            ! Eclipse incoming sunlight (e.g. Saturn ring shadowing).       
875            if(rings_shadow) then
876                call call_rings(ngrid, ptime, pday, diurnal)
877            endif   
878
879
880            if (corrk) then
881           
882! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
883! II.a Call correlated-k radiative transfer scheme
884! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
885               if(kastprof)then
886                  print*,'kastprof should not = true here'
887                  call abort
888               endif
889               if(water) then
890                  muvar(1:ngrid,1:nlayer)=mugaz/(1.e0+(1.e0/epsi-1.e0)*pq(1:ngrid,1:nlayer,igcm_h2o_vap))
891                  muvar(1:ngrid,nlayer+1)=mugaz/(1.e0+(1.e0/epsi-1.e0)*pq(1:ngrid,nlayer,igcm_h2o_vap))
892                  ! take into account water effect on mean molecular weight
893               else
894                  muvar(1:ngrid,1:nlayer+1)=mugaz
895               endif
896     
897
898               if(ok_slab_ocean) then
899                  tsurf2(:)=tsurf(:)
900                  do ig=1,ngrid
901                     if (nint(rnat(ig))==0) then
902                        tsurf(ig)=((1.-pctsrf_sic(ig))*tslab(ig,1)**4+pctsrf_sic(ig)*tsea_ice(ig)**4)**0.25
903                     endif
904                  enddo
905               endif !(ok_slab_ocean)
906               
907               ! standard callcorrk
908               clearsky=.false.
909               call callcorrk(ngrid,nlayer,pq,nq,qsurf,                           &
910                              albedo,albedo_equivalent,emis,mu0,pplev,pplay,pt,   &
911                              tsurf,fract,dist_star,aerosol,muvar,                &
912                              zdtlw,zdtsw,fluxsurf_lw,fluxsurf_sw,                &
913                              fluxsurfabs_sw,fluxtop_lw,                          &
914                              fluxabs_sw,fluxtop_dn,OLR_nu,OSR_nu,                &
915                              tau_col,cloudfrac,totcloudfrac,                     &
916                              clearsky,firstcall,lastcall)     
917
918                ! Option to call scheme once more for clear regions 
919               if(CLFvarying)then
920
921                  ! ---> PROBLEMS WITH ALLOCATED ARRAYS : temporary solution in callcorrk: do not deallocate if CLFvarying ...
922                  clearsky=.true.
923                  call callcorrk(ngrid,nlayer,pq,nq,qsurf,                           &
924                                 albedo,albedo_equivalent1,emis,mu0,pplev,pplay,pt,   &
925                                 tsurf,fract,dist_star,aerosol,muvar,                &
926                                 zdtlw1,zdtsw1,fluxsurf_lw1,fluxsurf_sw1,            &
927                                 fluxsurfabs_sw1,fluxtop_lw1,                        &
928                                 fluxabs_sw1,fluxtop_dn,OLR_nu1,OSR_nu1,             &
929                                 tau_col1,cloudfrac,totcloudfrac,                    &
930                                 clearsky,firstcall,lastcall)
931                  clearsky = .false.  ! just in case.     
932
933                  ! Sum the fluxes and heating rates from cloudy/clear cases
934                  do ig=1,ngrid
935                     tf=totcloudfrac(ig)
936                     ntf=1.-tf                   
937                     fluxsurf_lw(ig)       = ntf*fluxsurf_lw1(ig)       + tf*fluxsurf_lw(ig)
938                     fluxsurf_sw(ig)       = ntf*fluxsurf_sw1(ig)       + tf*fluxsurf_sw(ig)
939                     albedo_equivalent(ig) = ntf*albedo_equivalent1(ig) + tf*albedo_equivalent(ig)
940                     fluxsurfabs_sw(ig)    = ntf*fluxsurfabs_sw1(ig)    + tf*fluxsurfabs_sw(ig)
941                     fluxtop_lw(ig)        = ntf*fluxtop_lw1(ig)        + tf*fluxtop_lw(ig)
942                     fluxabs_sw(ig)        = ntf*fluxabs_sw1(ig)        + tf*fluxabs_sw(ig)
943                     tau_col(ig)           = ntf*tau_col1(ig)           + tf*tau_col(ig)
944                   
945                     zdtlw(ig,1:nlayer) = ntf*zdtlw1(ig,1:nlayer) + tf*zdtlw(ig,1:nlayer)
946                     zdtsw(ig,1:nlayer) = ntf*zdtsw1(ig,1:nlayer) + tf*zdtsw(ig,1:nlayer)
947
948                     OSR_nu(ig,1:L_NSPECTV) = ntf*OSR_nu1(ig,1:L_NSPECTV) + tf*OSR_nu(ig,1:L_NSPECTV)                       
949                     OLR_nu(ig,1:L_NSPECTI) = ntf*OLR_nu1(ig,1:L_NSPECTI) + tf*OLR_nu(ig,1:L_NSPECTI)                       
950                  enddo                               
951
952               endif ! end of CLFvarying.
953
954               if(ok_slab_ocean) then
955                  tsurf(:)=tsurf2(:)
956               endif
957
958
959               ! Radiative flux from the sky absorbed by the surface (W.m-2).
960               GSR=0.0
961               fluxrad_sky(1:ngrid)=emis(1:ngrid)*fluxsurf_lw(1:ngrid)+fluxsurfabs_sw(1:ngrid)
962
963                            !if(noradsurf)then ! no lower surface; SW flux just disappears
964                            !   GSR = SUM(fluxsurf_sw(1:ngrid)*cell_area(1:ngrid))/totarea
965                            !   fluxrad_sky(1:ngrid)=emis(1:ngrid)*fluxsurf_lw(1:ngrid)
966                            !   print*,'SW lost in deep atmosphere = ',GSR,' W m^-2'
967                            !endif
968
969               ! Net atmospheric radiative heating rate (K.s-1)
970               dtrad(1:ngrid,1:nlayer)=zdtsw(1:ngrid,1:nlayer)+zdtlw(1:ngrid,1:nlayer)
971               
972               ! Late initialization of the Ice Spectral Albedo. We needed the visible bands to do that !
973               if (firstcall .and. albedo_spectral_mode) then
974                  call spectral_albedo_calc(albedo_snow_SPECTV)
975               endif
976
977            elseif(newtonian)then
978           
979! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
980! II.b Call Newtonian cooling scheme
981! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
982               call newtrelax(ngrid,nlayer,mu0,sinlat,zpopsk,pt,pplay,pplev,dtrad,firstcall)
983
984               zdtsurf(1:ngrid) = +(pt(1:ngrid,1)-tsurf(1:ngrid))/ptimestep
985               ! e.g. surface becomes proxy for 1st atmospheric layer ?
986
987            else
988           
989! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
990! II.c Atmosphere has no radiative effect
991! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
992               fluxtop_dn(1:ngrid)  = fract(1:ngrid)*mu0(1:ngrid)*Fat1AU/dist_star**2
993               if(ngrid.eq.1)then ! / by 4 globally in 1D case...
994                  fluxtop_dn(1)  = fract(1)*Fat1AU/dist_star**2/2.0
995               endif
996               fluxsurf_sw(1:ngrid) = fluxtop_dn(1:ngrid)
997               print*,'------------WARNING---WARNING------------' ! by MT2015.
998               print*,'You are in corrk=false mode, '
999               print*,'and the surface albedo is taken equal to the first visible spectral value'               
1000               
1001               fluxsurfabs_sw(1:ngrid) = fluxtop_dn(1:ngrid)*(1.-albedo(1:ngrid,1))
1002               fluxrad_sky(1:ngrid)    = fluxsurfabs_sw(1:ngrid)
1003               fluxtop_lw(1:ngrid)  = emis(1:ngrid)*sigma*tsurf(1:ngrid)**4
1004
1005               dtrad(1:ngrid,1:nlayer)=0.0 ! no atmospheric radiative heating
1006
1007            endif ! end of corrk
1008
1009         endif ! of if(mod(icount-1,iradia).eq.0)
1010       
1011
1012         ! Transformation of the radiative tendencies
1013         ! ------------------------------------------
1014         zplanck(1:ngrid)=tsurf(1:ngrid)*tsurf(1:ngrid)
1015         zplanck(1:ngrid)=emis(1:ngrid)*sigma*zplanck(1:ngrid)*zplanck(1:ngrid)
1016         fluxrad(1:ngrid)=fluxrad_sky(1:ngrid)-zplanck(1:ngrid)
1017         pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+dtrad(1:ngrid,1:nlayer)
1018         
1019         ! Test of energy conservation
1020         !----------------------------
1021         if(enertest)then
1022            call planetwide_sumval(cpp*massarea(:,:)*zdtsw(:,:)/totarea_planet,dEtotSW)
1023            call planetwide_sumval(cpp*massarea(:,:)*zdtlw(:,:)/totarea_planet,dEtotLW)
1024            !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
1025            call planetwide_sumval(fluxsurfabs_sw(:)*cell_area(:)/totarea_planet,dEtotsSW) !JL13 carefull, albedo can have changed since the last time we called corrk
1026            call planetwide_sumval((fluxsurf_lw(:)*emis(:)-zplanck(:))*cell_area(:)/totarea_planet,dEtotsLW)
1027            dEzRadsw(:,:)=cpp*mass(:,:)*zdtsw(:,:)
1028            dEzRadlw(:,:)=cpp*mass(:,:)*zdtlw(:,:)
1029            if (is_master) then
1030               print*,'---------------------------------------------------------------'
1031               print*,'In corrk SW atmospheric heating       =',dEtotSW,' W m-2'
1032               print*,'In corrk LW atmospheric heating       =',dEtotLW,' W m-2'
1033               print*,'atmospheric net rad heating (SW+LW)   =',dEtotLW+dEtotSW,' W m-2'
1034               print*,'In corrk SW surface heating           =',dEtotsSW,' W m-2'
1035               print*,'In corrk LW surface heating           =',dEtotsLW,' W m-2'
1036               print*,'surface net rad heating (SW+LW)       =',dEtotsLW+dEtotsSW,' W m-2'
1037            endif
1038         endif ! end of 'enertest'
1039
1040      endif ! of if (callrad)
1041
1042
1043
1044!  --------------------------------------------
1045!  III. Vertical diffusion (turbulent mixing) :
1046!  --------------------------------------------
1047
1048      if (calldifv) then
1049     
1050         zflubid(1:ngrid)=fluxrad(1:ngrid)+fluxgrd(1:ngrid)
1051
1052         ! JL12 the following if test is temporarily there to allow us to compare the old vdifc with turbdiff.
1053         if (UseTurbDiff) then
1054         
1055            call turbdiff(ngrid,nlayer,nq,rnat,                  &
1056                          ptimestep,capcal,lwrite,               &
1057                          pplay,pplev,zzlay,zzlev,z0,            &
1058                          pu,pv,pt,zpopsk,pq,tsurf,emis,qsurf,   &
1059                          pdt,pdq,zflubid,                       &
1060                          zdudif,zdvdif,zdtdif,zdtsdif,          &
1061                          sensibFlux,q2,zdqdif,zdqevap,zdqsdif,  &
1062                          taux,tauy,lastcall)
1063
1064         else
1065         
1066            zdh(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)/zpopsk(1:ngrid,1:nlayer)
1067 
1068            call vdifc(ngrid,nlayer,nq,rnat,zpopsk,           &
1069                       ptimestep,capcal,lwrite,               &
1070                       pplay,pplev,zzlay,zzlev,z0,            &
1071                       pu,pv,zh,pq,tsurf,emis,qsurf,          &
1072                       zdh,pdq,zflubid,                       &
1073                       zdudif,zdvdif,zdhdif,zdtsdif,          &
1074                       sensibFlux,q2,zdqdif,zdqsdif,          &
1075                       taux,tauy,lastcall)
1076
1077            zdtdif(1:ngrid,1:nlayer)=zdhdif(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1078            zdqevap(1:ngrid,1:nlayer)=0.
1079
1080         end if !end of 'UseTurbDiff'
1081
1082
1083         pdv(1:ngrid,1:nlayer)=pdv(1:ngrid,1:nlayer)+zdvdif(1:ngrid,1:nlayer)
1084         pdu(1:ngrid,1:nlayer)=pdu(1:ngrid,1:nlayer)+zdudif(1:ngrid,1:nlayer)
1085         pdt(1:ngrid,1:nlayer)=pdt(1:ngrid,1:nlayer)+zdtdif(1:ngrid,1:nlayer)
1086         zdtsurf(1:ngrid)=zdtsurf(1:ngrid)+zdtsdif(1:ngrid)
1087
1088
1089         if(ok_slab_ocean)then
1090            flux_sens_lat(1:ngrid)=(zdtsdif(1:ngrid)*capcal(1:ngrid)-fluxrad(1:ngrid))
1091         endif
1092
1093
1094         if (tracer) then
1095           pdq(1:ngrid,1:nlayer,1:nq)=pdq(1:ngrid,1:nlayer,1:nq)+ zdqdif(1:ngrid,1:nlayer,1:nq)
1096           dqsurf(1:ngrid,1:nq)=dqsurf(1:ngrid,1:nq) + zdqsdif(1:ngrid,1:nq)
1097         end if ! of if (tracer)
1098
1099
1100         ! test energy conservation
1101         !-------------------------
1102         if(enertest)then
1103         
1104            dEzdiff(:,:)=cpp*mass(:,:)*zdtdif(:,:)
1105            do ig = 1, ngrid
1106               dEdiff(ig)=SUM(dEzdiff (ig,:))+ sensibFlux(ig)! subtract flux to the ground
1107               dEzdiff(ig,1)= dEzdiff(ig,1)+ sensibFlux(ig)! subtract flux to the ground
1108            enddo
1109           
1110            call planetwide_sumval(dEdiff(:)*cell_area(:)/totarea_planet,dEtot)
1111            dEdiffs(:)=capcal(:)*zdtsdif(:)-zflubid(:)-sensibFlux(:)
1112            call planetwide_sumval(dEdiffs(:)*cell_area(:)/totarea_planet,dEtots)
1113            call planetwide_sumval(sensibFlux(:)*cell_area(:)/totarea_planet,AtmToSurf_TurbFlux)
1114           
1115            if (is_master) then
1116           
1117               if (UseTurbDiff) then
1118                         print*,'In TurbDiff sensible flux (atm=>surf) =',AtmToSurf_TurbFlux,' W m-2'
1119                         print*,'In TurbDiff non-cons atm nrj change   =',dEtot,' W m-2'
1120                  print*,'In TurbDiff (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1121               else
1122                         print*,'In vdifc sensible flux (atm=>surf)    =',AtmToSurf_TurbFlux,' W m-2'
1123                         print*,'In vdifc non-cons atm nrj change      =',dEtot,' W m-2'
1124                         print*,'In vdifc (correc rad+latent heat) surf nrj change =',dEtots,' W m-2'
1125               end if
1126            endif ! end of 'is_master'
1127           
1128         ! JL12 : note that the black body radiative flux emitted by the surface has been updated by the implicit scheme but not given back elsewhere.
1129         endif ! end of 'enertest'
1130
1131
1132         ! Test water conservation.
1133         if(watertest.and.water)then
1134         
1135            call planetwide_sumval(massarea(:,:)*zdqdif(:,:,igcm_h2o_vap)*ptimestep/totarea_planet,dWtot_tmp)
1136            call planetwide_sumval(zdqsdif(:,igcm_h2o_vap)*cell_area(:)*ptimestep/totarea_planet,dWtots_tmp)
1137            do ig = 1, ngrid
1138               vdifcncons(ig)=SUM(mass(ig,:)*zdqdif(ig,:,igcm_h2o_vap))
1139            enddo
1140            call planetwide_sumval(massarea(:,:)*zdqdif(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1141            call planetwide_sumval(zdqsdif(:,igcm_h2o_ice)*cell_area(:)*ptimestep/totarea_planet,dWtots)
1142            dWtot = dWtot + dWtot_tmp
1143            dWtots = dWtots + dWtots_tmp
1144            do ig = 1, ngrid
1145               vdifcncons(ig)=vdifcncons(ig) + SUM(mass(ig,:)*zdqdif(ig,:,igcm_h2o_ice))
1146            enddo           
1147            call planetwide_maxval(vdifcncons(:),nconsMAX)
1148
1149            if (is_master) then
1150               print*,'---------------------------------------------------------------'
1151               print*,'In difv atmospheric water change        =',dWtot,' kg m-2'
1152               print*,'In difv surface water change            =',dWtots,' kg m-2'
1153               print*,'In difv non-cons factor                 =',dWtot+dWtots,' kg m-2'
1154               print*,'In difv MAX non-cons factor             =',nconsMAX,' kg m-2 s-1'
1155            endif
1156
1157         endif ! end of 'watertest'
1158         !-------------------------
1159
1160      else ! calldifv
1161
1162         if(.not.newtonian)then
1163
1164            zdtsurf(1:ngrid) = zdtsurf(1:ngrid) + (fluxrad(1:ngrid) + fluxgrd(1:ngrid))/capcal(1:ngrid)
1165
1166         endif
1167
1168      endif ! end of 'calldifv'
1169
1170
1171!----------------------------------
1172!   IV. Dry convective adjustment :
1173!----------------------------------
1174
1175      if(calladj) then
1176
1177         zdh(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)/zpopsk(1:ngrid,1:nlayer)
1178         zduadj(1:ngrid,1:nlayer)=0.0
1179         zdvadj(1:ngrid,1:nlayer)=0.0
1180         zdhadj(1:ngrid,1:nlayer)=0.0
1181
1182
1183         call convadj(ngrid,nlayer,nq,ptimestep,            &
1184                      pplay,pplev,zpopsk,                   &
1185                      pu,pv,zh,pq,                          &
1186                      pdu,pdv,zdh,pdq,                      &
1187                      zduadj,zdvadj,zdhadj,                 &
1188                      zdqadj)
1189
1190         pdu(1:ngrid,1:nlayer) = pdu(1:ngrid,1:nlayer) + zduadj(1:ngrid,1:nlayer)
1191         pdv(1:ngrid,1:nlayer) = pdv(1:ngrid,1:nlayer) + zdvadj(1:ngrid,1:nlayer)
1192         pdt(1:ngrid,1:nlayer)    = pdt(1:ngrid,1:nlayer) + zdhadj(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer)
1193         zdtadj(1:ngrid,1:nlayer) = zdhadj(1:ngrid,1:nlayer)*zpopsk(1:ngrid,1:nlayer) ! for diagnostic only
1194
1195         if(tracer) then
1196            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqadj(1:ngrid,1:nlayer,1:nq)
1197         end if
1198
1199         ! Test energy conservation
1200         if(enertest)then
1201            call planetwide_sumval(cpp*massarea(:,:)*zdtadj(:,:)/totarea_planet,dEtot)
1202            if (is_master) print*,'In convadj atmospheric energy change  =',dEtot,' W m-2'
1203         endif
1204
1205         ! Test water conservation
1206         if(watertest)then
1207            call planetwide_sumval(massarea(:,:)*zdqadj(:,:,igcm_h2o_vap)*ptimestep/totarea_planet,dWtot_tmp)
1208            do ig = 1, ngrid
1209               cadjncons(ig)=SUM(mass(ig,:)*zdqadj(ig,:,igcm_h2o_vap))
1210            enddo
1211            call planetwide_sumval(massarea(:,:)*zdqadj(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1212            dWtot = dWtot + dWtot_tmp
1213            do ig = 1, ngrid
1214               cadjncons(ig)=cadjncons(ig) + SUM(mass(ig,:)*zdqadj(ig,:,igcm_h2o_ice))
1215            enddo           
1216            call planetwide_maxval(cadjncons(:),nconsMAX)
1217
1218            if (is_master) then
1219               print*,'In convadj atmospheric water change     =',dWtot,' kg m-2'
1220               print*,'In convadj MAX non-cons factor          =',nconsMAX,' kg m-2 s-1'
1221            endif
1222           
1223         endif ! end of 'watertest'
1224         
1225      endif ! end of 'calladj'
1226
1227!-----------------------------------------------
1228!   V. Carbon dioxide condensation-sublimation :
1229!-----------------------------------------------
1230
1231      if (co2cond) then
1232     
1233         if (.not.tracer) then
1234            print*,'We need a CO2 ice tracer to condense CO2'
1235            call abort
1236         endif
1237         call condense_co2(ngrid,nlayer,nq,ptimestep,                    &
1238                           capcal,pplay,pplev,tsurf,pt,                  &
1239                           pdt,zdtsurf,pq,pdq,                           &
1240                           qsurf,zdqsurfc,albedo,emis,                   &
1241                           albedo_bareground,albedo_co2_ice_SPECTV,      &
1242                           zdtc,zdtsurfc,pdpsrf,zdqc)
1243
1244         pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+zdtc(1:ngrid,1:nlayer)
1245         zdtsurf(1:ngrid)      = zdtsurf(1:ngrid) + zdtsurfc(1:ngrid)
1246
1247         pdq(1:ngrid,1:nlayer,1:nq)   = pdq(1:ngrid,1:nlayer,1:nq)+ zdqc(1:ngrid,1:nlayer,1:nq)
1248         dqsurf(1:ngrid,igcm_co2_ice) = dqsurf(1:ngrid,igcm_co2_ice) + zdqsurfc(1:ngrid)
1249
1250         ! test energy conservation
1251         if(enertest)then
1252            call planetwide_sumval(cpp*massarea(:,:)*zdtc(:,:)/totarea_planet,dEtot)
1253            call planetwide_sumval(capcal(:)*zdtsurfc(:)*cell_area(:)/totarea_planet,dEtots)
1254            if (is_master) then
1255               print*,'In co2cloud atmospheric energy change   =',dEtot,' W m-2'
1256               print*,'In co2cloud surface energy change       =',dEtots,' W m-2'
1257            endif
1258         endif
1259
1260      endif  ! end of 'co2cond'
1261
1262
1263!---------------------------------------------
1264!   VI. Specific parameterizations for tracers
1265!---------------------------------------------
1266
1267      if (tracer) then
1268     
1269  ! ---------------------
1270  !   VI.1. Water and ice
1271  ! ---------------------
1272         if (water) then
1273
1274            ! Water ice condensation in the atmosphere
1275            if(watercond.and.(RLVTT.gt.1.e-8))then
1276
1277               dqmoist(1:ngrid,1:nlayer,1:nq)=0.
1278               dtmoist(1:ngrid,1:nlayer)=0.
1279               
1280               ! Moist Convective Adjustment.
1281               ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1282               call moistadj(ngrid,nlayer,nq,pt,pq,pdq,pplev,pplay,dtmoist,dqmoist,ptimestep,rneb_man)
1283
1284               pdq(1:ngrid,1:nlayer,igcm_h2o_vap) = pdq(1:ngrid,1:nlayer,igcm_h2o_vap)     &
1285                                                  + dqmoist(1:ngrid,1:nlayer,igcm_h2o_vap)
1286               pdq(1:ngrid,1:nlayer,igcm_h2o_ice) = pdq(1:ngrid,1:nlayer,igcm_h2o_ice)     &
1287                                                  + dqmoist(1:ngrid,1:nlayer,igcm_h2o_ice)
1288               pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+dtmoist(1:ngrid,1:nlayer)
1289
1290               ! Test energy conservation.
1291               if(enertest)then
1292               
1293                  call planetwide_sumval(cpp*massarea(:,:)*dtmoist(:,:)/totarea_planet,dEtot)
1294                  call planetwide_maxval(dtmoist(:,:),dtmoist_max)
1295                  call planetwide_minval(dtmoist(:,:),dtmoist_min)
1296                  madjdEz(:,:)=cpp*mass(:,:)*dtmoist(:,:)
1297                  do ig=1,ngrid
1298                     madjdE(ig) = cpp*SUM(mass(:,:)*dtmoist(:,:))
1299                  enddo
1300                 
1301                  if (is_master) then
1302                     print*,'In moistadj atmospheric energy change   =',dEtot,' W m-2'
1303                     print*,'In moistadj MAX atmospheric energy change   =',dtmoist_max*ptimestep,'K/step'
1304                     print*,'In moistadj MIN atmospheric energy change   =',dtmoist_min*ptimestep,'K/step'
1305                  endif
1306                 
1307                  call planetwide_sumval(massarea(:,:)*dqmoist(:,:,igcm_h2o_vap)*ptimestep/totarea_planet+        &
1308                                           massarea(:,:)*dqmoist(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1309                  if (is_master) print*,'In moistadj atmospheric water change    =',dWtot,' kg m-2'
1310                 
1311               endif ! end of 'enertest'
1312               
1313
1314               ! Large scale condensation/evaporation.
1315               ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1316               call largescale(ngrid,nlayer,nq,ptimestep,pplev,pplay,pt,pq,pdt,pdq,dtlscale,dqvaplscale,dqcldlscale,rneb_lsc)
1317
1318               pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+dtlscale(1:ngrid,1:nlayer)
1319               pdq(1:ngrid,1:nlayer,igcm_h2o_vap) = pdq(1:ngrid,1:nlayer,igcm_h2o_vap)+dqvaplscale(1:ngrid,1:nlayer)
1320               pdq(1:ngrid,1:nlayer,igcm_h2o_ice) = pdq(1:ngrid,1:nlayer,igcm_h2o_ice)+dqcldlscale(1:ngrid,1:nlayer)
1321
1322               ! Test energy conservation.
1323               if(enertest)then
1324                  lscaledEz(:,:) = cpp*mass(:,:)*dtlscale(:,:)
1325                  do ig=1,ngrid
1326                     lscaledE(ig) = cpp*SUM(mass(:,:)*dtlscale(:,:))
1327                  enddo
1328                  call planetwide_sumval(cpp*massarea(:,:)*dtlscale(:,:)/totarea_planet,dEtot)
1329
1330                  if (is_master) print*,'In largescale atmospheric energy change =',dEtot,' W m-2'
1331
1332                  ! Test water conservation.
1333                  call planetwide_sumval(massarea(:,:)*dqvaplscale(:,:)*ptimestep/totarea_planet+        &
1334                                           SUM(massarea(:,:)*dqcldlscale(:,:))*ptimestep/totarea_planet,dWtot)
1335                       
1336                  if (is_master) print*,'In largescale atmospheric water change  =',dWtot,' kg m-2'
1337               endif ! end of 'enertest'
1338
1339               ! Compute cloud fraction.
1340               do l = 1, nlayer
1341                  do ig=1,ngrid
1342                     cloudfrac(ig,l)=MAX(rneb_lsc(ig,l),rneb_man(ig,l))
1343                  enddo
1344               enddo
1345
1346            endif ! end of 'watercond'
1347           
1348            ! Water ice / liquid precipitation.
1349            ! ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1350            if(waterrain)then
1351
1352               zdqrain(1:ngrid,1:nlayer,1:nq) = 0.0
1353               zdqsrain(1:ngrid)    = 0.0
1354               zdqssnow(1:ngrid)    = 0.0
1355
1356               call rain(ngrid,nlayer,nq,ptimestep,pplev,pplay,pt,pdt,pq,pdq,            &
1357                         zdtrain,zdqrain,zdqsrain,zdqssnow,cloudfrac)
1358
1359               pdq(1:ngrid,1:nlayer,igcm_h2o_vap) = pdq(1:ngrid,1:nlayer,igcm_h2o_vap) &
1360                                                  + zdqrain(1:ngrid,1:nlayer,igcm_h2o_vap)
1361               pdq(1:ngrid,1:nlayer,igcm_h2o_ice) = pdq(1:ngrid,1:nlayer,igcm_h2o_ice) &
1362                                                  + zdqrain(1:ngrid,1:nlayer,igcm_h2o_ice)
1363               pdt(1:ngrid,1:nlayer) = pdt(1:ngrid,1:nlayer)+zdtrain(1:ngrid,1:nlayer)
1364               
1365               dqsurf(1:ngrid,igcm_h2o_vap) = dqsurf(1:ngrid,igcm_h2o_vap)+zdqsrain(1:ngrid)
1366               dqsurf(1:ngrid,igcm_h2o_ice) = dqsurf(1:ngrid,igcm_h2o_ice)+zdqssnow(1:ngrid)
1367
1368               ! Test energy conservation.
1369               if(enertest)then
1370               
1371                  call planetwide_sumval(cpp*massarea(:,:)*zdtrain(:,:)/totarea_planet,dEtot)
1372                  if (is_master) print*,'In rain atmospheric T energy change       =',dEtot,' W m-2'
1373                  call planetwide_sumval(massarea(:,:)*zdqrain(:,:,igcm_h2o_ice)/totarea_planet*RLVTT/cpp,dItot_tmp)
1374                  call planetwide_sumval(cell_area(:)*zdqssnow(:)/totarea_planet*RLVTT/cpp,dItot)
1375                  dItot = dItot + dItot_tmp
1376                  call planetwide_sumval(massarea(:,:)*zdqrain(:,:,igcm_h2o_vap)*ptimestep/totarea_planet,dVtot_tmp)
1377                  call planetwide_sumval(cell_area(:)*zdqsrain(:)/totarea_planet*RLVTT/cpp,dVtot)
1378                  dVtot = dVtot + dVtot_tmp
1379                  dEtot = dItot + dVtot
1380                 
1381                  if (is_master) then
1382                     print*,'In rain dItot =',dItot,' W m-2'
1383                     print*,'In rain dVtot =',dVtot,' W m-2'
1384                     print*,'In rain atmospheric L energy change       =',dEtot,' W m-2'
1385                  endif
1386
1387                  ! Test water conservation
1388                  call planetwide_sumval(massarea(:,:)*zdqrain(:,:,igcm_h2o_vap)*ptimestep/totarea_planet+        &
1389                          massarea(:,:)*zdqrain(:,:,igcm_h2o_ice)*ptimestep/totarea_planet,dWtot)
1390                  call planetwide_sumval((zdqsrain(:)+zdqssnow(:))*cell_area(:)*ptimestep/totarea_planet,dWtots)
1391                 
1392                  if (is_master) then
1393                          print*,'In rain atmospheric water change        =',dWtot,' kg m-2'
1394                          print*,'In rain surface water change            =',dWtots,' kg m-2'
1395                          print*,'In rain non-cons factor                 =',dWtot+dWtots,' kg m-2'
1396                  endif
1397                 
1398               endif ! end of 'enertest'
1399
1400            end if ! enf of 'waterrain'
1401           
1402         end if ! end of 'water'
1403
1404  ! -------------------------
1405  !   VI.2. Aerosol particles
1406  ! -------------------------
1407
1408         ! Sedimentation.
1409         if (sedimentation) then
1410       
1411            zdqsed(1:ngrid,1:nlayer,1:nq) = 0.0
1412            zdqssed(1:ngrid,1:nq)  = 0.0
1413
1414            if(watertest)then
1415           
1416               iq=igcm_h2o_ice
1417               call planetwide_sumval(massarea(:,:)*pq(:,:,iq)*ptimestep/totarea_planet,dWtot)
1418               call planetwide_sumval(massarea(:,:)*pdq(:,:,iq)*ptimestep/totarea_planet,dWtots)
1419               if (is_master) then
1420                        print*,'Before sedim pq  =',dWtot,' kg m-2'
1421                  print*,'Before sedim pdq =',dWtots,' kg m-2'
1422               endif
1423            endif
1424           
1425            call callsedim(ngrid,nlayer,ptimestep,           &
1426                          pplev,zzlev,pt,pdt,pq,pdq,        &
1427                          zdqsed,zdqssed,nq)
1428
1429            if(watertest)then
1430               iq=igcm_h2o_ice
1431               call planetwide_sumval(massarea(:,:)*pq(:,:,iq)*ptimestep/totarea_planet,dWtot)
1432               call planetwide_sumval(massarea(:,:)*pdq(:,:,iq)*ptimestep/totarea_planet,dWtots)
1433               if (is_master) then
1434                        print*,'After sedim pq  =',dWtot,' kg m-2'
1435                        print*,'After sedim pdq =',dWtots,' kg m-2'
1436                 endif
1437            endif
1438
1439            ! Whether it falls as rain or snow depends only on the surface temperature
1440            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqsed(1:ngrid,1:nlayer,1:nq)
1441            dqsurf(1:ngrid,1:nq) = dqsurf(1:ngrid,1:nq) + zdqssed(1:ngrid,1:nq)
1442
1443            ! Test water conservation
1444            if(watertest)then
1445               call planetwide_sumval(massarea(:,:)*(zdqsed(:,:,igcm_h2o_vap)+zdqsed(:,:,igcm_h2o_ice))*ptimestep/totarea_planet,dWtot)
1446               call planetwide_sumval((zdqssed(:,igcm_h2o_vap)+zdqssed(:,igcm_h2o_ice))*cell_area(:)*ptimestep/totarea_planet,dWtots)
1447               if (is_master) then
1448                        print*,'In sedim atmospheric ice change         =',dWtot,' kg m-2'
1449                        print*,'In sedim surface ice change             =',dWtots,' kg m-2'
1450                        print*,'In sedim non-cons factor                =',dWtot+dWtots,' kg m-2'
1451               endif
1452            endif
1453
1454         end if ! end of 'sedimentation'
1455
1456
1457  ! ---------------
1458  !   VI.3. Updates
1459  ! ---------------
1460
1461         ! Updating Atmospheric Mass and Tracers budgets.
1462         if(mass_redistrib) then
1463
1464            zdmassmr(1:ngrid,1:nlayer) = mass(1:ngrid,1:nlayer) *     &
1465               (   zdqevap(1:ngrid,1:nlayer)                          &
1466                 + zdqrain(1:ngrid,1:nlayer,igcm_h2o_vap)             &
1467                 + dqmoist(1:ngrid,1:nlayer,igcm_h2o_vap)             &
1468                 + dqvaplscale(1:ngrid,1:nlayer) )
1469
1470            do ig = 1, ngrid
1471               zdmassmr_col(ig)=SUM(zdmassmr(ig,1:nlayer))
1472            enddo
1473           
1474            call writediagfi(ngrid,"mass_evap","mass gain"," ",3,zdmassmr)
1475            call writediagfi(ngrid,"mass_evap_col","mass gain col"," ",2,zdmassmr_col)
1476            call writediagfi(ngrid,"mass","mass","kg/m2",3,mass)
1477
1478            call mass_redistribution(ngrid,nlayer,nq,ptimestep,                     &
1479                                     rnat,capcal,pplay,pplev,pt,tsurf,pq,qsurf,     &
1480                                     pu,pv,pdt,zdtsurf,pdq,pdu,pdv,zdmassmr,        &
1481                                     zdtmr,zdtsurfmr,zdpsrfmr,zdumr,zdvmr,zdqmr,zdqsurfmr)
1482         
1483            pdq(1:ngrid,1:nlayer,1:nq) = pdq(1:ngrid,1:nlayer,1:nq) + zdqmr(1:ngrid,1:nlayer,1:nq)
1484            dqsurf(1:ngrid,1:nq)       = dqsurf(1:ngrid,1:nq) + zdqsurfmr(1:ngrid,1:nq)
1485            pdt(1:ngrid,1:nlayer)      = pdt(1:ngrid,1:nlayer) + zdtmr(1:ngrid,1:nlayer)
1486            pdu(1:ngrid,1:nlayer)      = pdu(1:ngrid,1:nlayer) + zdumr(1:ngrid,1:nlayer)
1487            pdv(1:ngrid,1:nlayer)      = pdv(1:ngrid,1:nlayer) + zdvmr(1:ngrid,1:nlayer)
1488            pdpsrf(1:ngrid)            = pdpsrf(1:ngrid) + zdpsrfmr(1:ngrid)
1489            zdtsurf(1:ngrid)           = zdtsurf(1:ngrid) + zdtsurfmr(1:ngrid)
1490           
1491         endif
1492
1493  ! ------------------
1494  !   VI.4. Slab Ocean
1495  ! ------------------
1496
1497         if (ok_slab_ocean)then
1498
1499            do ig=1,ngrid
1500               qsurfint(:,igcm_h2o_ice)=qsurf(:,igcm_h2o_ice)
1501            enddo
1502
1503            call ocean_slab_ice(ptimestep,                          &
1504                                ngrid, knindex, tsea_ice, fluxrad,  &
1505                                zdqssnow, qsurf(:,igcm_h2o_ice),    &
1506                              - zdqsdif(:,igcm_h2o_vap),            &
1507                                flux_sens_lat,tsea_ice, pctsrf_sic, &
1508                                taux,tauy,icount)
1509
1510
1511            call ocean_slab_get_vars(ngrid,tslab,      &
1512                                     sea_ice, flux_o,  &
1513                                     flux_g, dt_hdiff, &
1514                                     dt_ekman)
1515   
1516            do ig=1,ngrid
1517               if (nint(rnat(ig)).eq.1)then
1518                  tslab(ig,1) = 0.
1519                  tslab(ig,2) = 0.
1520                  tsea_ice(ig) = 0.
1521                  sea_ice(ig) = 0.
1522                  pctsrf_sic(ig) = 0.
1523                  qsurf(ig,igcm_h2o_ice) = qsurfint(ig,igcm_h2o_ice)
1524               endif
1525            enddo
1526
1527         endif ! end of 'ok_slab_ocean'
1528
1529  ! -----------------------------
1530  !   VI.5. Surface Tracer Update
1531  ! -----------------------------
1532
1533         if(hydrology)then
1534           
1535            call hydrol(ngrid,nq,ptimestep,rnat,tsurf,qsurf,dqsurf,dqs_hyd, &
1536                        capcal,albedo,albedo_bareground,                    &
1537                        albedo_snow_SPECTV,albedo_co2_ice_SPECTV,           &
1538                        mu0,zdtsurf,zdtsurf_hyd,hice,pctsrf_sic,            &
1539                        sea_ice)
1540
1541            zdtsurf(1:ngrid)     = zdtsurf(1:ngrid) + zdtsurf_hyd(1:ngrid)
1542            dqsurf(1:ngrid,1:nq) = dqsurf(1:ngrid,1:nq) + dqs_hyd(1:ngrid,1:nq)
1543           
1544            qsurf(1:ngrid,1:nq) = qsurf(1:ngrid,1:nq) + ptimestep*dqsurf(1:ngrid,1:nq)
1545
1546            ! Test energy conservation
1547            if(enertest)then
1548               call planetwide_sumval(cell_area(:)*capcal(:)*zdtsurf_hyd(:)/totarea_planet,dEtots)
1549               if (is_master) print*,'In hydrol surface energy change     =',dEtots,' W m-2'
1550            endif
1551
1552            ! test water conservation
1553            if(watertest)then
1554               call planetwide_sumval(dqs_hyd(:,igcm_h2o_ice)*cell_area(:)*ptimestep/totarea_planet,dWtots)
1555               if (is_master) print*,'In hydrol surface ice change            =',dWtots,' kg m-2'
1556                  call planetwide_sumval(dqs_hyd(:,igcm_h2o_vap)*cell_area(:)*ptimestep/totarea_planet,dWtots)
1557               if (is_master) then
1558                  print*,'In hydrol surface water change          =',dWtots,' kg m-2'
1559                  print*,'---------------------------------------------------------------'
1560               endif
1561            endif
1562
1563         else ! of if (hydrology)
1564
1565            qsurf(1:ngrid,1:nq) = qsurf(1:ngrid,1:nq) + ptimestep*dqsurf(1:ngrid,1:nq)
1566
1567         end if ! of if (hydrology)
1568
1569         ! Add qsurf to qsurf_hist, which is what we save in diagfi.nc. At the same time, we set the water
1570         ! content of ocean gridpoints back to zero, in order to avoid rounding errors in vdifc, rain.
1571         qsurf_hist(:,:) = qsurf(:,:)
1572
1573         if(ice_update)then
1574            ice_min(1:ngrid)=min(ice_min(1:ngrid),qsurf(1:ngrid,igcm_h2o_ice))
1575         endif
1576
1577      endif! end of if 'tracer'
1578
1579
1580!------------------------------------------------
1581!   VII. Surface and sub-surface soil temperature
1582!------------------------------------------------
1583
1584
1585      ! Increment surface temperature
1586      if(ok_slab_ocean)then
1587         do ig=1,ngrid
1588           if (nint(rnat(ig)).eq.0)then
1589             zdtsurf(ig)= (tslab(ig,1)              &
1590             + pctsrf_sic(ig)*(tsea_ice(ig)-tslab(ig,1))-tsurf(ig))/ptimestep
1591           endif
1592           tsurf(ig)=tsurf(ig)+ptimestep*zdtsurf(ig)
1593         enddo
1594   
1595      else
1596        tsurf(1:ngrid)=tsurf(1:ngrid)+ptimestep*zdtsurf(1:ngrid)
1597      endif ! end of 'ok_slab_ocean'
1598
1599
1600      ! Compute soil temperatures and subsurface heat flux.
1601      if (callsoil) then
1602         call soil(ngrid,nsoilmx,.false.,lastcall,inertiedat,   &
1603                   ptimestep,tsurf,tsoil,capcal,fluxgrd)     
1604      endif
1605
1606
1607      if (ok_slab_ocean) then
1608     
1609         do ig=1,ngrid
1610         
1611            fluxgrdocean(ig)=fluxgrd(ig)
1612            if (nint(rnat(ig)).eq.0) then
1613               capcal(ig)=capcalocean
1614               fluxgrd(ig)=0.
1615               fluxgrdocean(ig)=pctsrf_sic(ig)*flux_g(ig)+(1-pctsrf_sic(ig))*(dt_hdiff(ig,1)+dt_ekman(ig,1))
1616               do iq=1,nsoilmx
1617                  tsoil(ig,iq)=tsurf(ig)
1618               enddo
1619               if (pctsrf_sic(ig).gt.0.5) then
1620                  capcal(ig)=capcalseaice
1621                  if (qsurf(ig,igcm_h2o_ice).gt.0.) then
1622                     capcal(ig)=capcalsno
1623                  endif
1624               endif               
1625            endif
1626           
1627         enddo
1628         
1629      endif !end of 'ok_slab_ocean'
1630
1631
1632      ! Test energy conservation
1633      if(enertest)then
1634         call planetwide_sumval(cell_area(:)*capcal(:)*zdtsurf(:)/totarea_planet,dEtots)         
1635         if (is_master) print*,'Surface energy change                 =',dEtots,' W m-2'
1636      endif
1637
1638
1639!---------------------------------------------------
1640!   VIII. Perform diagnostics and write output files
1641!---------------------------------------------------
1642
1643      ! Note : For output only: the actual model integration is performed in the dynamics.
1644
1645
1646 
1647      ! Temperature, zonal and meridional winds.
1648      zt(1:ngrid,1:nlayer) = pt(1:ngrid,1:nlayer) + pdt(1:ngrid,1:nlayer)*ptimestep
1649      zu(1:ngrid,1:nlayer) = pu(1:ngrid,1:nlayer) + pdu(1:ngrid,1:nlayer)*ptimestep
1650      zv(1:ngrid,1:nlayer) = pv(1:ngrid,1:nlayer) + pdv(1:ngrid,1:nlayer)*ptimestep
1651
1652      ! Diagnostic.
1653      zdtdyn(1:ngrid,1:nlayer)     = (pt(1:ngrid,1:nlayer)-ztprevious(1:ngrid,1:nlayer)) / ptimestep
1654      ztprevious(1:ngrid,1:nlayer) = zt(1:ngrid,1:nlayer)
1655
1656      zdudyn(1:ngrid,1:nlayer)     = (pu(1:ngrid,1:nlayer)-zuprevious(1:ngrid,1:nlayer)) / ptimestep
1657      zuprevious(1:ngrid,1:nlayer) = zu(1:ngrid,1:nlayer)
1658
1659      if(firstcall)then
1660         zdtdyn(1:ngrid,1:nlayer)=0.0
1661         zdudyn(1:ngrid,1:nlayer)=0.0
1662      endif
1663
1664      ! Dynamical heating diagnostic.
1665      do ig=1,ngrid
1666         fluxdyn(ig)= SUM(zdtdyn(ig,:) *mass(ig,:))*cpp
1667      enddo
1668
1669      ! Tracers.
1670      zq(1:ngrid,1:nlayer,1:nq) = pq(1:ngrid,1:nlayer,1:nq) + pdq(1:ngrid,1:nlayer,1:nq)*ptimestep
1671
1672      ! Surface pressure.
1673      ps(1:ngrid) = pplev(1:ngrid,1) + pdpsrf(1:ngrid)*ptimestep
1674
1675
1676
1677      ! Surface and soil temperature information
1678      call planetwide_sumval(cell_area(:)*tsurf(:)/totarea_planet,Ts1)
1679      call planetwide_minval(tsurf(:),Ts2)
1680      call planetwide_maxval(tsurf(:),Ts3)
1681      if(callsoil)then
1682         TsS = SUM(cell_area(:)*tsoil(:,nsoilmx))/totarea        ! mean temperature at bottom soil layer
1683         print*,'  ave[Tsurf]     min[Tsurf]     max[Tsurf]     ave[Tdeep]'
1684         print*,Ts1,Ts2,Ts3,TsS
1685      else
1686               if (is_master) then
1687            print*,'  ave[Tsurf]     min[Tsurf]     max[Tsurf]'
1688            print*,Ts1,Ts2,Ts3
1689         endif
1690      end if
1691
1692
1693      ! Check the energy balance of the simulation during the run
1694      if(corrk)then
1695
1696         call planetwide_sumval(cell_area(:)*fluxtop_dn(:)/totarea_planet,ISR)
1697         call planetwide_sumval(cell_area(:)*fluxabs_sw(:)/totarea_planet,ASR)
1698         call planetwide_sumval(cell_area(:)*fluxtop_lw(:)/totarea_planet,OLR)
1699         call planetwide_sumval(cell_area(:)*fluxgrd(:)/totarea_planet,GND)
1700         call planetwide_sumval(cell_area(:)*fluxdyn(:)/totarea_planet,DYN)
1701         do ig=1,ngrid
1702            if(fluxtop_dn(ig).lt.0.0)then
1703               print*,'fluxtop_dn has gone crazy'
1704               print*,'fluxtop_dn=',fluxtop_dn(ig)
1705               print*,'tau_col=',tau_col(ig)
1706               print*,'aerosol=',aerosol(ig,:,:)
1707               print*,'temp=   ',pt(ig,:)
1708               print*,'pplay=  ',pplay(ig,:)
1709               call abort
1710            endif
1711         end do
1712                     
1713         if(ngrid.eq.1)then
1714            DYN=0.0
1715         endif
1716         
1717         if (is_master) then
1718            print*,'  ISR            ASR            OLR            GND            DYN [W m^-2]'
1719            print*, ISR,ASR,OLR,GND,DYN
1720         endif
1721
1722         if(enertest .and. is_master)then
1723            print*,'SW flux/heating difference SW++ - ASR = ',dEtotSW+dEtotsSW-ASR,' W m-2'
1724            print*,'LW flux/heating difference LW++ - OLR = ',dEtotLW+dEtotsLW+OLR,' W m-2'
1725            print*,'LW energy balance LW++ + ASR = ',dEtotLW+dEtotsLW+ASR,' W m-2'
1726         endif
1727
1728         if(meanOLR .and. is_master)then
1729            if((ngrid.gt.1) .or. (mod(icount-1,ecritphy).eq.0))then
1730               ! to record global radiative balance
1731               open(92,file="rad_bal.out",form='formatted',position='append')
1732               write(92,*) zday,ISR,ASR,OLR
1733               close(92)
1734               open(93,file="tem_bal.out",form='formatted',position='append')
1735               if(callsoil)then
1736                         write(93,*) zday,Ts1,Ts2,Ts3,TsS
1737               else
1738                  write(93,*) zday,Ts1,Ts2,Ts3
1739               endif
1740               close(93)
1741            endif
1742         endif
1743
1744      endif ! end of 'corrk'
1745
1746
1747      ! Diagnostic to test radiative-convective timescales in code.
1748      if(testradtimes)then
1749         open(38,file="tau_phys.out",form='formatted',position='append')
1750         ig=1
1751         do l=1,nlayer
1752            write(38,*) -1./pdt(ig,l),pt(ig,l),pplay(ig,l)
1753         enddo
1754         close(38)
1755         print*,'As testradtimes enabled,'
1756         print*,'exiting physics on first call'
1757         call abort
1758      endif
1759
1760
1761      ! Compute column amounts (kg m-2) if tracers are enabled.
1762      if(tracer)then
1763         qcol(1:ngrid,1:nq)=0.0
1764         do iq=1,nq
1765            do ig=1,ngrid
1766               qcol(ig,iq) = SUM( zq(ig,1:nlayer,iq) * mass(ig,1:nlayer))
1767            enddo
1768         enddo
1769
1770         ! Generalised for arbitrary aerosols now. By LK
1771         reffcol(1:ngrid,1:naerkind)=0.0
1772         if(co2cond.and.(iaero_co2.ne.0))then
1773            call co2_reffrad(ngrid,nlayer,nq,zq,reffrad(1,1,iaero_co2))
1774            do ig=1,ngrid
1775               reffcol(ig,iaero_co2) = SUM(zq(ig,1:nlayer,igcm_co2_ice)*reffrad(ig,1:nlayer,iaero_co2)*mass(ig,1:nlayer))
1776            enddo
1777         endif
1778         if(water.and.(iaero_h2o.ne.0))then
1779            call h2o_reffrad(ngrid,nlayer,zq(1,1,igcm_h2o_ice),zt, &
1780                             reffrad(1,1,iaero_h2o),nueffrad(1,1,iaero_h2o))
1781            do ig=1,ngrid
1782               reffcol(ig,iaero_h2o) = SUM(zq(ig,1:nlayer,igcm_h2o_ice)*reffrad(ig,1:nlayer,iaero_h2o)*mass(ig,1:nlayer))
1783            enddo
1784         endif
1785
1786      endif ! end of 'tracer'
1787
1788
1789      ! Test for water conservation.
1790      if(water)then
1791
1792         call planetwide_sumval(cell_area(:)*qsurf_hist(:,igcm_h2o_ice)/totarea_planet,icesrf)
1793         call planetwide_sumval(cell_area(:)*qsurf_hist(:,igcm_h2o_vap)/totarea_planet,liqsrf)
1794         call planetwide_sumval(cell_area(:)*qcol(:,igcm_h2o_ice)/totarea_planet,icecol)
1795         call planetwide_sumval(cell_area(:)*qcol(:,igcm_h2o_vap)/totarea_planet,vapcol)
1796
1797         h2otot = icesrf + liqsrf + icecol + vapcol
1798         
1799         if (is_master) then
1800            print*,' Total water amount [kg m^-2]: ',h2otot
1801            print*,' Surface ice    Surface liq.   Atmos. con.     Atmos. vap. [kg m^-2] '
1802            print*, icesrf,liqsrf,icecol,vapcol
1803         endif
1804
1805         if(meanOLR .and. is_master)then
1806            if((ngrid.gt.1) .or. (mod(icount-1,ecritphy).eq.0))then
1807               ! to record global water balance
1808               open(98,file="h2o_bal.out",form='formatted',position='append')
1809               write(98,*) zday,icesrf,liqsrf,icecol,vapcol
1810               close(98)
1811            endif
1812         endif
1813
1814      endif
1815
1816
1817      ! Calculate RH (Relative Humidity) for diagnostic.
1818      if(water)then
1819     
1820         do l = 1, nlayer
1821            do ig=1,ngrid
1822               call Psat_water(zt(ig,l),pplay(ig,l),psat_tmp,qsat(ig,l))
1823               RH(ig,l) = zq(ig,l,igcm_h2o_vap) / qsat(ig,l)
1824            enddo
1825         enddo
1826
1827         ! Compute maximum possible H2O column amount (100% saturation).
1828         do ig=1,ngrid
1829            H2Omaxcol(ig) = SUM( qsat(ig,:) * mass(ig,:))
1830         enddo
1831
1832      endif ! end of 'water'
1833
1834
1835      print*,'--> Ls =',zls*180./pi
1836     
1837     
1838!----------------------------------------------------------------------
1839!        Writing NetCDF file  "RESTARTFI" at the end of the run
1840!----------------------------------------------------------------------
1841
1842!        Note: 'restartfi' is stored just before dynamics are stored
1843!              in 'restart'. Between now and the writting of 'restart',
1844!              there will have been the itau=itau+1 instruction and
1845!              a reset of 'time' (lastacll = .true. when itau+1= itaufin)
1846!              thus we store for time=time+dtvr
1847
1848
1849
1850      if(lastcall) then
1851         ztime_fin = ptime + ptimestep/(float(iphysiq)*daysec)
1852
1853         ! Update surface ice distribution to iterate to steady state if requested
1854         if(ice_update)then
1855
1856            do ig=1,ngrid
1857
1858               delta_ice = (qsurf(ig,igcm_h2o_ice)-ice_initial(ig))
1859               
1860               ! add multiple years of evolution
1861               qsurf_hist(ig,igcm_h2o_ice) = qsurf_hist(ig,igcm_h2o_ice) + delta_ice*icetstep
1862
1863               ! if ice has gone -ve, set to zero
1864               if(qsurf_hist(ig,igcm_h2o_ice).lt.0.0)then
1865                  qsurf_hist(ig,igcm_h2o_ice) = 0.0
1866               endif
1867
1868               ! if ice is seasonal, set to zero (NEW)
1869               if(ice_min(ig).lt.0.01)then
1870                  qsurf_hist(ig,igcm_h2o_ice) = 0.0
1871               endif
1872
1873            enddo
1874
1875            ! enforce ice conservation
1876            ice_tot= SUM(qsurf_hist(:,igcm_h2o_ice)*cell_area(:) )/SUM(cell_area(:))
1877            qsurf_hist(:,igcm_h2o_ice) = qsurf_hist(:,igcm_h2o_ice)*(icesrf/ice_tot)
1878
1879         endif
1880
1881         if (ngrid.ne.1) then
1882            write(*,*)'PHYSIQ: for physdem ztime_fin =',ztime_fin
1883           
1884            call physdem1("restartfi.nc",nsoilmx,ngrid,nlayer,nq, &
1885                          ptimestep,ztime_fin,                    &
1886                          tsurf,tsoil,emis,q2,qsurf_hist,         &
1887                          cloudfrac,totcloudfrac,hice,            &
1888                          rnat,pctsrf_sic,tslab,tsea_ice,sea_ice)
1889         endif
1890
1891         if(ok_slab_ocean) then
1892            call ocean_slab_final!(tslab, seaice)
1893         end if
1894
1895         
1896      endif ! end of 'lastcall'
1897
1898
1899!-----------------------------------
1900!        Saving statistics :
1901!-----------------------------------
1902
1903!    Note :("stats" stores and accumulates 8 key variables in file "stats.nc"
1904!           which can later be used to make the statistic files of the run:
1905!           "stats")          only possible in 3D runs !!!
1906
1907         
1908      if (callstats) then
1909
1910         call wstats(ngrid,"ps","Surface pressure","Pa",2,ps)
1911         call wstats(ngrid,"tsurf","Surface temperature","K",2,tsurf)
1912         call wstats(ngrid,"fluxsurf_lw",                               &
1913                     "Thermal IR radiative flux to surface","W.m-2",2,  &
1914                     fluxsurf_lw)
1915         call wstats(ngrid,"fluxtop_lw",                                &
1916                     "Thermal IR radiative flux to space","W.m-2",2,    &
1917                     fluxtop_lw)
1918                     
1919!            call wstats(ngrid,"fluxsurf_sw",                               &
1920!                        "Solar radiative flux to surface","W.m-2",2,       &
1921!                         fluxsurf_sw_tot)                     
1922!            call wstats(ngrid,"fluxtop_sw",                                &
1923!                        "Solar radiative flux to space","W.m-2",2,         &
1924!                        fluxtop_sw_tot)
1925
1926
1927         call wstats(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
1928         call wstats(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
1929         call wstats(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
1930         !call wstats(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
1931         !call wstats(ngrid,"ALB_1st","First Band Surface albedo"," ",2,albedo(:,1))
1932         call wstats(ngrid,"p","Pressure","Pa",3,pplay)
1933         call wstats(ngrid,"temp","Atmospheric temperature","K",3,zt)
1934         call wstats(ngrid,"u","Zonal (East-West) wind","m.s-1",3,zu)
1935         call wstats(ngrid,"v","Meridional (North-South) wind","m.s-1",3,zv)
1936         call wstats(ngrid,"w","Vertical (down-up) wind","m.s-1",3,pw)
1937         call wstats(ngrid,"q2","Boundary layer eddy kinetic energy","m2.s-2",3,q2)
1938
1939         if (tracer) then
1940            do iq=1,nq
1941               call wstats(ngrid,noms(iq),noms(iq),'kg/kg',3,zq(1,1,iq))
1942               call wstats(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
1943                           'kg m^-2',2,qsurf(1,iq) )
1944               call wstats(ngrid,trim(noms(iq))//'_col',trim(noms(iq))//'_col',    &
1945                          'kg m^-2',2,qcol(1,iq) )
1946                         
1947!              call wstats(ngrid,trim(noms(iq))//'_reff',                          &
1948!                          trim(noms(iq))//'_reff',                                   &
1949!                          'm',3,reffrad(1,1,iq))
1950
1951            end do
1952           
1953            if (water) then
1954               vmr=zq(1:ngrid,1:nlayer,igcm_h2o_vap)*mugaz/mmol(igcm_h2o_vap)
1955               call wstats(ngrid,"vmr_h2ovapor",                             &
1956                           "H2O vapour volume mixing ratio","mol/mol",       &
1957                           3,vmr)
1958            endif
1959
1960         endif ! end of 'tracer'
1961
1962         if(watercond.and.CLFvarying)then
1963            call wstats(ngrid,"rneb_man","H2O cloud fraction (conv)"," ",3,rneb_man)
1964            call wstats(ngrid,"rneb_lsc","H2O cloud fraction (large scale)"," ",3,rneb_lsc)
1965            call wstats(ngrid,"CLF","H2O cloud fraction"," ",3,cloudfrac)
1966            call wstats(ngrid,"CLFt","H2O column cloud fraction"," ",2,totcloudfrac)
1967            call wstats(ngrid,"RH","relative humidity"," ",3,RH)
1968         endif
1969
1970         if (ok_slab_ocean) then
1971            call wstats(ngrid,"dt_hdiff1","dt_hdiff1","K/s",2,dt_hdiff(:,1))
1972            call wstats(ngrid,"dt_hdiff2","dt_hdiff2","K/s",2,dt_hdiff(:,2))
1973            call wstats(ngrid,"dt_ekman1","dt_ekman1","K/s",2,dt_ekman(:,1))
1974            call wstats(ngrid,"dt_ekman2","dt_ekman2","K/s",2,dt_ekman(:,2))
1975            call wstats(ngrid,"tslab1","tslab1","K",2,tslab(:,1))
1976            call wstats(ngrid,"tslab2","tslab2","K",2,tslab(:,2))
1977            call wstats(ngrid,"pctsrf_sic","pct ice/sea","",2,pctsrf_sic)
1978            call wstats(ngrid,"tsea_ice","tsea_ice","K",2,tsea_ice)
1979            call wstats(ngrid,"sea_ice","sea ice","kg/m2",2,sea_ice)
1980            call wstats(ngrid,"rnat","nature of the surface","",2,rnat)
1981         endif
1982
1983         if(lastcall) then
1984            write (*,*) "Writing stats..."
1985            call mkstats(ierr)
1986         endif
1987         
1988      endif ! end of 'callstats'
1989
1990
1991!-----------------------------------------------------------------------------------------------------
1992!           OUTPUT in netcdf file "DIAGFI.NC", containing any variable for diagnostic
1993!
1994!             Note 1 : output with  period "ecritphy", set in "run.def"
1995!
1996!             Note 2 : writediagfi can also be called from any other subroutine for any variable,
1997!                      but its preferable to keep all the calls in one place ...
1998!-----------------------------------------------------------------------------------------------------
1999
2000
2001      call writediagfi(ngrid,"Ls","solar longitude","deg",0,zls*180./pi)
2002      call writediagfi(ngrid,"Lss","sub solar longitude","deg",0,zlss*180./pi)
2003      call writediagfi(ngrid,"RA","right ascension","deg",0,right_ascen*180./pi)
2004      call writediagfi(ngrid,"Declin","solar declination","deg",0,declin*180./pi)
2005      call writediagfi(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2006      call writediagfi(ngrid,"ps","Surface pressure","Pa",2,ps)
2007      call writediagfi(ngrid,"temp","temperature","K",3,zt)
2008      call writediagfi(ngrid,"teta","potential temperature","K",3,zh)
2009      call writediagfi(ngrid,"u","Zonal wind","m.s-1",3,zu)
2010      call writediagfi(ngrid,"v","Meridional wind","m.s-1",3,zv)
2011      call writediagfi(ngrid,"w","Vertical wind","m.s-1",3,pw)
2012      call writediagfi(ngrid,"p","Pressure","Pa",3,pplay)
2013
2014!     Subsurface temperatures
2015!        call writediagsoil(ngrid,"tsurf","Surface temperature","K",2,tsurf)
2016!        call writediagsoil(ngrid,"temp","temperature","K",3,tsoil)
2017
2018      ! Oceanic layers
2019      if(ok_slab_ocean) then
2020         call writediagfi(ngrid,"pctsrf_sic","pct ice/sea","",2,pctsrf_sic)
2021         call writediagfi(ngrid,"tsea_ice","tsea_ice","K",2,tsea_ice)
2022         call writediagfi(ngrid,"sea_ice","sea ice","kg/m2",2,sea_ice)
2023         call writediagfi(ngrid,"tslab1","tslab1","K",2,tslab(:,1))
2024         call writediagfi(ngrid,"tslab2","tslab2","K",2,tslab(:,2))
2025         call writediagfi(ngrid,"dt_hdiff1","dt_hdiff1","K/s",2,dt_hdiff(:,1))
2026         call writediagfi(ngrid,"dt_hdiff2","dt_hdiff2","K/s",2,dt_hdiff(:,2))
2027         call writediagfi(ngrid,"dt_ekman1","dt_ekman1","K/s",2,dt_ekman(:,1))
2028         call writediagfi(ngrid,"dt_ekman2","dt_ekman2","K/s",2,dt_ekman(:,2))
2029         call writediagfi(ngrid,"rnat","nature of the surface","",2,rnat)
2030         call writediagfi(ngrid,"sensibFlux","sensible heat flux","w.m^-2",2,sensibFlux)
2031         call writediagfi(ngrid,"latentFlux","latent heat flux","w.m^-2",2,zdqsdif(:,igcm_h2o_vap)*RLVTT)
2032      endif
2033     
2034
2035      ! Total energy balance diagnostics
2036      if(callrad.and.(.not.newtonian))then
2037     
2038         !call writediagfi(ngrid,"ALB","Surface albedo"," ",2,albedo_equivalent)
2039         !call writediagfi(ngrid,"ALB_1st","First Band Surface albedo"," ",2,albedo(:,1))
2040         call writediagfi(ngrid,"ISR","incoming stellar rad.","W m-2",2,fluxtop_dn)
2041         call writediagfi(ngrid,"ASR","absorbed stellar rad.","W m-2",2,fluxabs_sw)
2042         call writediagfi(ngrid,"OLR","outgoing longwave rad.","W m-2",2,fluxtop_lw)
2043         call writediagfi(ngrid,"shad","rings"," ", 2, fract)
2044         
2045!           call writediagfi(ngrid,"ASRcs","absorbed stellar rad (cs).","W m-2",2,fluxabs_sw1)
2046!           call writediagfi(ngrid,"OLRcs","outgoing longwave rad (cs).","W m-2",2,fluxtop_lw1)
2047!           call writediagfi(ngrid,"fluxsurfsw","sw surface flux.","W m-2",2,fluxsurf_sw)
2048!           call writediagfi(ngrid,"fluxsurflw","lw back radiation.","W m-2",2,fluxsurf_lw)
2049!           call writediagfi(ngrid,"fluxsurfswcs","sw surface flux (cs).","W m-2",2,fluxsurf_sw1)
2050!           call writediagfi(ngrid,"fluxsurflwcs","lw back radiation (cs).","W m-2",2,fluxsurf_lw1)
2051
2052         if(ok_slab_ocean) then
2053            call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrdocean)
2054         else
2055            call writediagfi(ngrid,"GND","heat flux from ground","W m-2",2,fluxgrd)
2056         endif
2057         
2058         call writediagfi(ngrid,"DYN","dynamical heat input","W m-2",2,fluxdyn)
2059         
2060      endif ! end of 'callrad'
2061       
2062      if(enertest) then
2063     
2064         if (calldifv) then
2065         
2066            call writediagfi(ngrid,"q2","turbulent kinetic energy","J.kg^-1",3,q2)
2067            call writediagfi(ngrid,"sensibFlux","sensible heat flux","w.m^-2",2,sensibFlux)
2068           
2069!             call writediagfi(ngrid,"dEzdiff","turbulent diffusion heating (-sensible flux)","w.m^-2",3,dEzdiff)
2070!             call writediagfi(ngrid,"dEdiff","integrated turbulent diffusion heating (-sensible flux)","w.m^-2",2,dEdiff)
2071!             call writediagfi(ngrid,"dEdiffs","In TurbDiff (correc rad+latent heat) surf nrj change","w.m^-2",2,dEdiffs)
2072             
2073         endif
2074         
2075         if (corrk) then
2076            call writediagfi(ngrid,"dEzradsw","radiative heating","w.m^-2",3,dEzradsw)
2077            call writediagfi(ngrid,"dEzradlw","radiative heating","w.m^-2",3,dEzradlw)
2078         endif
2079         
2080         if(watercond) then
2081
2082            call writediagfi(ngrid,"lscaledE","heat from largescale","W m-2",2,lscaledE)
2083            call writediagfi(ngrid,"madjdE","heat from moistadj","W m-2",2,madjdE)
2084            call writediagfi(ngrid,"qsatatm","atm qsat"," ",3,qsat)
2085             
2086!             call writediagfi(ngrid,"lscaledEz","heat from largescale","W m-2",3,lscaledEz)
2087!             call writediagfi(ngrid,"madjdEz","heat from moistadj","W m-2",3,madjdEz)             
2088!             call writediagfi(ngrid,"h2o_max_col","maximum H2O column amount","kg.m^-2",2,H2Omaxcol)
2089
2090         endif
2091         
2092      endif ! end of 'enertest'
2093
2094
2095        ! Temporary inclusions for heating diagnostics.
2096        !call writediagfi(ngrid,"zdtsw","SW heating","T s-1",3,zdtsw)
2097        !call writediagfi(ngrid,"zdtlw","LW heating","T s-1",3,zdtlw)
2098        !call writediagfi(ngrid,"dtrad","radiative heating","K s-1",3,dtrad)
2099        ! call writediagfi(ngrid,"zdtdyn","Dyn. heating","T s-1",3,zdtdyn)
2100       
2101        ! For Debugging.
2102        !call writediagfi(ngrid,'rnat','Terrain type',' ',2,real(rnat))
2103        !call writediagfi(ngrid,'pphi','Geopotential',' ',3,pphi)
2104       
2105
2106      ! Output aerosols.
2107      if (igcm_co2_ice.ne.0.and.iaero_co2.ne.0) &
2108         call writediagfi(ngrid,'CO2ice_reff','CO2ice_reff','m',3,reffrad(1,1,iaero_co2))
2109      if (igcm_h2o_ice.ne.0.and.iaero_h2o.ne.0) &
2110         call writediagfi(ngrid,'H2Oice_reff','H2Oice_reff','m',3,reffrad(:,:,iaero_h2o))
2111      if (igcm_co2_ice.ne.0.and.iaero_co2.ne.0) &
2112         call writediagfi(ngrid,'CO2ice_reffcol','CO2ice_reffcol','um kg m^-2',2,reffcol(1,iaero_co2))
2113      if (igcm_h2o_ice.ne.0.and.iaero_h2o.ne.0) &
2114         call writediagfi(ngrid,'H2Oice_reffcol','H2Oice_reffcol','um kg m^-2',2,reffcol(1,iaero_h2o))
2115
2116      ! Output tracers.
2117      if (tracer) then
2118     
2119         do iq=1,nq
2120            call writediagfi(ngrid,noms(iq),noms(iq),'kg/kg',3,zq(1,1,iq))
2121            call writediagfi(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2122                             'kg m^-2',2,qsurf_hist(1,iq) )
2123            call writediagfi(ngrid,trim(noms(iq))//'_col',trim(noms(iq))//'_col',    &
2124                            'kg m^-2',2,qcol(1,iq) )
2125                         
2126!          call writediagfi(ngrid,trim(noms(iq))//'_surf',trim(noms(iq))//'_surf',  &
2127!                          'kg m^-2',2,qsurf(1,iq) )                         
2128
2129            if(watercond.or.CLFvarying)then
2130               call writediagfi(ngrid,"rneb_man","H2O cloud fraction (conv)"," ",3,rneb_man)
2131               call writediagfi(ngrid,"rneb_lsc","H2O cloud fraction (large scale)"," ",3,rneb_lsc)
2132               call writediagfi(ngrid,"CLF","H2O cloud fraction"," ",3,cloudfrac)
2133               call writediagfi(ngrid,"CLFt","H2O column cloud fraction"," ",2,totcloudfrac)
2134               call writediagfi(ngrid,"RH","relative humidity"," ",3,RH)
2135            endif
2136
2137            if(waterrain)then
2138               call writediagfi(ngrid,"rain","rainfall","kg m-2 s-1",2,zdqsrain)
2139               call writediagfi(ngrid,"snow","snowfall","kg m-2 s-1",2,zdqssnow)
2140            endif
2141
2142            if((hydrology).and.(.not.ok_slab_ocean))then
2143               call writediagfi(ngrid,"hice","oceanic ice height","m",2,hice)
2144            endif
2145
2146            if(ice_update)then
2147               call writediagfi(ngrid,"ice_min","min annual ice","m",2,ice_min)
2148               call writediagfi(ngrid,"ice_ini","initial annual ice","m",2,ice_initial)
2149            endif
2150
2151            do ig=1,ngrid
2152               if(tau_col(ig).gt.1.e3)then
2153                  print*,'WARNING: tau_col=',tau_col(ig)
2154                  print*,'at ig=',ig,'in PHYSIQ'
2155               endif         
2156            end do
2157
2158            call writediagfi(ngrid,"tau_col","Total aerosol optical depth","[]",2,tau_col)
2159
2160         enddo ! end of 'nq' loop
2161         
2162       endif ! end of 'tracer'
2163
2164
2165      ! Output spectrum.
2166      if(specOLR.and.corrk)then     
2167         call writediagspecIR(ngrid,"OLR3D","OLR(lon,lat,band)","W/m^2/cm^-1",3,OLR_nu)
2168         call writediagspecVI(ngrid,"OSR3D","OSR(lon,lat,band)","W/m^2/cm^-1",3,OSR_nu)
2169      endif
2170
2171! XIOS outputs
2172#ifdef CPP_XIOS     
2173      ! Send fields to XIOS: (NB these fields must also be defined as
2174      ! <field id="..." /> in context_lmdz_physics.xml to be correctly used)
2175      CALL send_xios_field("ls",zls)
2176     
2177      CALL send_xios_field("ps",ps)
2178      CALL send_xios_field("area",cell_area)
2179
2180      CALL send_xios_field("temperature",zt)
2181      CALL send_xios_field("u",zu)
2182      CALL send_xios_field("v",zv)
2183     
2184#endif
2185
2186      icount=icount+1
2187
2188    end subroutine physiq
2189   
2190    end module physiq_mod
Note: See TracBrowser for help on using the repository browser.