updated top-level README and version_decl for V4.5 (#1847)
[WRF.git] / dyn_em / solve_em.F
blob92d5b73fed85d4e8126430ccdfa70e2b85accc8d
1 !WRF:MEDIATION_LAYER:SOLVER
3 SUBROUTINE solve_em ( grid , config_flags  &
4 ! Arguments generated from Registry
5 #include "dummy_new_args.inc"
7                     )
8 ! Driver layer modules
9    USE module_state_description
10    USE module_domain, ONLY : &
11                   domain, get_ijk_from_grid, get_ijk_from_subgrid                          &
12                  ,domain_get_current_time, domain_get_start_time                           &
13                  ,domain_get_sim_start_time, domain_clock_get,is_alarm_tstep
14    USE module_domain_type, ONLY : history_alarm, restart_alarm, auxinput4_alarm            &
15                  ,boundary_alarm
16    USE module_configure, ONLY : grid_config_rec_type
17    USE module_driver_constants
18    USE module_machine
19    USE module_tiles, ONLY : set_tiles
20 #ifdef DM_PARALLEL
21    USE module_dm, ONLY : &
22                   local_communicator, mytask, ntasks, ntasks_x, ntasks_y                   &
23                  ,local_communicator_periodic, wrf_dm_maxval
24    USE module_comm_dm, ONLY : &
25                   halo_em_a_sub,halo_em_b_sub,halo_em_c2_sub,halo_em_chem_e_3_sub          &
26                  ,halo_em_chem_e_5_sub,halo_em_chem_e_7_sub,halo_em_chem_old_e_5_sub       &
27                  ,halo_em_chem_old_e_7_sub,halo_em_c_sub,halo_em_d2_3_sub                  &
28                  ,halo_em_d2_5_sub,halo_em_d3_3_sub,halo_em_d3_5_sub,halo_em_d_sub         &
29                  ,halo_em_e_3_sub,halo_em_e_5_sub,halo_em_hydro_uv_sub                     &
30                  ,halo_em_moist_e_3_sub,halo_em_moist_e_5_sub,halo_em_moist_e_7_sub        &
31                  ,halo_em_moist_old_e_5_sub,halo_em_moist_old_e_7_sub                      &
32                  ,halo_em_scalar_e_3_sub,halo_em_scalar_e_5_sub,halo_em_scalar_e_7_sub     &
33                  ,halo_em_scalar_old_e_5_sub,halo_em_scalar_old_e_7_sub,halo_em_tke_3_sub  &
34                  ,halo_em_tke_5_sub,halo_em_tke_7_sub,halo_em_tke_advect_3_sub             &
35                  ,halo_em_tke_advect_5_sub,halo_em_tke_old_e_5_sub                         &
36                  ,halo_em_tke_old_e_7_sub,halo_em_tracer_e_3_sub,halo_em_tracer_e_5_sub    &
37                  ,halo_em_tracer_e_7_sub,halo_em_tracer_old_e_5_sub                        &
38                  ,halo_em_tracer_old_e_7_sub,halo_em_sbm_sub,period_bdy_em_a_sub                           &
39                  ,period_bdy_em_b3_sub,period_bdy_em_b_sub,period_bdy_em_chem2_sub         &
40                  ,period_bdy_em_chem_old_sub,period_bdy_em_chem_sub,period_bdy_em_d3_sub   &
41                  ,period_bdy_em_d_sub,period_bdy_em_e_sub,period_bdy_em_moist2_sub         &
42                  ,period_bdy_em_moist_old_sub,period_bdy_em_moist_sub                      &
43                  ,period_bdy_em_scalar2_sub,period_bdy_em_scalar_old_sub                   &
44                  ,period_bdy_em_scalar_sub,period_bdy_em_tke_old_sub, period_bdy_em_tke_sub  &
45                  ,period_bdy_em_tracer2_sub,period_bdy_em_tracer_old_sub                   &
46                  ,period_bdy_em_tracer_sub,period_em_da_sub,period_em_hydro_uv_sub         &
47                  ,period_em_f_sub,period_em_g_sub                                          &
48                  ,halo_em_f_1_sub,halo_em_init_4_sub,halo_em_thetam_sub,period_em_thetam_sub &
49                  ,halo_em_d_pv_sub,halo_firebrand_spotting_5_sub
50 #endif
51    USE module_utility
52 ! Mediation layer modules
53 ! Model layer modules
54    USE module_model_constants
55    USE module_small_step_em
56    USE module_em
57    USE module_big_step_utilities_em
58    USE module_bc
59    USE module_bc_em
60    USE module_solvedebug_em
61    USE module_physics_addtendc
62    USE module_diffusion_em
63    USE module_polarfft
64    USE module_microphysics_driver
65    USE module_microphysics_zero_out
66 !   USE module_lightning_driver, ONLY : lightning_driver
67    USE module_fddaobs_driver
68 !   USE module_diagnostics
69 #if (WRF_CHEM == 1)
70    USE module_input_chem_data
71    USE module_input_tracer
72    USE module_chem_utilities
73 #endif
74    USE module_dust_emis
75    USE module_first_rk_step_part1
76    USE module_first_rk_step_part2
77    USE module_after_all_rk_steps
78    USE module_llxy, ONLY : proj_cassini
79    USE module_avgflx_em, ONLY : zero_avgflx, upd_avgflx
80    USE module_cpl, ONLY : coupler_on, cpl_settime, cpl_store_input
82 #if (WRF_CMAQ == 1)
83    use twoway_data_module
84 #endif
85    USE module_firebrand_spotting, ONLY : firebrand_spotting_em_driver
87    IMPLICIT NONE
89    !  Input data.
91    TYPE(domain) , TARGET          :: grid
93    !  Definitions of dummy arguments to this routine (generated from Registry).
94 #include "dummy_new_decl.inc"
96    !  Structure that contains run-time configuration (namelist) data for domain
97    TYPE (grid_config_rec_type) , INTENT(IN)          :: config_flags
99    ! Local data
101    INTEGER                         :: k_start , k_end, its, ite, jts, jte
102    INTEGER                         :: ids , ide , jds , jde , kds , kde , &
103                                       ims , ime , jms , jme , kms , kme , &
104                                       ips , ipe , jps , jpe , kps , kpe
106    INTEGER                         :: sids , side , sjds , sjde , skds , skde , &
107                                       sims , sime , sjms , sjme , skms , skme , &
108                                       sips , sipe , sjps , sjpe , skps , skpe
111    INTEGER ::              imsx, imex, jmsx, jmex, kmsx, kmex,    &
112                            ipsx, ipex, jpsx, jpex, kpsx, kpex,    &
113                            imsy, imey, jmsy, jmey, kmsy, kmey,    &
114                            ipsy, ipey, jpsy, jpey, kpsy, kpey
116    INTEGER                         :: ij , iteration
117    INTEGER                         :: im , num_3d_m , ic , num_3d_c , is , num_3d_s
118    INTEGER                         :: loop
119    INTEGER                         :: sz
120    INTEGER                         :: iswater
122    LOGICAL                         :: specified_bdy, channel_bdy
124    REAL                            :: t_new, time_duration_of_lbcs
125    
126 ! begin WRF-CMAQ twoway coupled model block
127    integer       :: twoway_jdate,    &  ! CMAQ current job date
128                     twoway_jtime,    &  ! CMAQ current job time
129                     met_file_tstep      ! MCIP like MET file time step
131    integer, save :: cmaq_nstep,      &  ! total number of CMAQ steps
132                     wrf_end_step,    &  ! WRF ending step #
133                     counter = -1,    &  ! step counter
134                     wrf_cmaq_freq,   &  ! call frequency between WRF and CMAQ
135                     wrf_cmaq_option     ! WRF-CMAQ coupled model option
136                                         ! 0 = run WRF only
137                                         ! 1 = run WRF only               w   producing MCIP like GRID and MET files
138                                         ! 2 = run WRF-CMAQ coupled model w/o producing MCIP like GRID and MET files
139                                         ! 3 = run WRF-CMAQ coupled model w   producing MCIP like GRID and MET files
141    logical       :: cmaq_step           ! CMAQ step number
143    logical, save :: firstime = .true.,   &  ! logical variable indicating first time
144                     feedback_is_ready,   &  ! logical variable indicating feedback process can proceed
145                     feedback_restart,    &  ! logical variable indicating feedback information is available
146                     direct_sw_feedback      ! logical variable indicating direct aerosol sw feedback is on or not
147 ! end WRF-CMAQ twoway coupled model block
149    ! Changes in tendency at this timestep
150    real ,DIMENSION(grid%sm31:grid%em31,grid%sm32:grid%em32,grid%sm33:grid%em33) :: h_tendency, &
151                                                                                    z_tendency
152                                                                                    
153    ! Whether advection should produce decoupled horizontal and vertical advective tendency outputs
154    LOGICAL                        :: tenddec
155    
156    ! Flag for producing diagnostic fields (e.g., radar reflectivity)
157    LOGICAL                        :: diag_flag
158    INTEGER                        :: ke_diag ! tells reflectivity calculation whether to do full depth or only k=1
159    LOGICAL                        :: restart_flag !  tells if it is a restart timestep to write restart files. 
160    
161 #if (WRF_CHEM == 1)
162    ! Index cross-referencing array for tendency accumulation
163    INTEGER, DIMENSION( num_chem ) :: adv_ct_indices
164 #endif
166 ! storage for tendencies and decoupled state (generated from Registry)
168 #include "i1_decl.inc"
169 ! Previous time level of tracer arrays now defined as i1 variables;
170 ! the state 4d arrays now redefined as 1-time level arrays in Registry.
171 ! Benefit: save memory in nested runs, since only 1 domain is active at a
172 ! time.  Potential problem on stack-limited architectures: increases
173 ! amount of data on program stack by making these automatic arrays.
175    INTEGER :: rc 
176    INTEGER :: number_of_small_timesteps, rk_step
177    INTEGER :: klevel,ijm,ijp,i,j,k,size1,size2    ! for prints/plots only
178    INTEGER :: idum1, idum2, dynamics_option
180    INTEGER :: rk_order, iwmax, jwmax, kwmax
181    REAL :: dt_rk, dts_rk, dts, dtm, wmax
182    REAL , ALLOCATABLE , DIMENSION(:)  :: max_vert_cfl_tmp, max_horiz_cfl_tmp
183    LOGICAL :: leapfrog
184    INTEGER :: l,kte,kk
185    LOGICAL :: f_flux  ! flag for computing averaged fluxes in cu_gd
186    REAL    :: curr_secs, curr_secs2
187    INTEGER :: num_sound_steps
188    INTEGER :: idex, jdex
189    REAL    :: max_msft
190    REAL    :: spacing
192    INTEGER :: ii, jj !kk is above after l,kte
193    REAL    :: dclat
194    INTEGER :: debug_level
196 ! urban related variables
197    INTEGER :: NUM_ROOF_LAYERS, NUM_WALL_LAYERS, NUM_ROAD_LAYERS   ! urban
199    TYPE(WRFU_TimeInterval)                    :: tmpTimeInterval, tmpTimeInterval2
200    REAL                                       :: real_time
201    LOGICAL                                    :: adapt_step_flag
202    LOGICAL                                    :: fill_w_flag
204 ! variables for flux-averaging code 20091223
205    CHARACTER*256                              :: message, message2, message3
206    REAL                                       :: old_dt
207    TYPE(WRFU_Time)                            :: temp_time, CurrTime, restart_time
208    INTEGER, PARAMETER                         :: precision = 100
209    INTEGER                                    :: num, den
210    TYPE(WRFU_TimeInterval)                    :: dtInterval, intervaltime,restartinterval
212 #if (WRF_CMAQ == 1)
213    interface
214       SUBROUTINE CMAQ_DRIVER ( MODEL_STDATE, MODEL_STTIME, MODEL_TSTEP,  &
215                                MODEL_JDATE, MODEL_JTIME, LAST_STEP,      &
216                                COUPLE_TSTEP, NCOLS_IN, NLAYS_IN)
217         INTEGER, INTENT( IN )  :: MODEL_STDATE, MODEL_STTIME, MODEL_TSTEP
218         INTEGER, INTENT( OUT ) :: MODEL_JDATE, MODEL_JTIME
219         LOGICAL, INTENT( IN )  :: LAST_STEP
220         INTEGER, INTENT( IN ), OPTIONAL :: COUPLE_TSTEP
221         INTEGER, INTENT( IN ), OPTIONAL :: NCOLS_IN, NLAYS_IN
222       END SUBROUTINE CMAQ_DRIVER
223    end interface
224 #endif
226 ! Define benchmarking timers if -DBENCH is compiled
227 #include "bench_solve_em_def.h"
229 !----------------------
230 ! Executable statements
231 !----------------------
233 !<DESCRIPTION>
234 !<pre>
235 ! solve_em is the main driver for advancing a grid a single timestep.
236 ! It is a mediation-layer routine -> DM and SM calls are made where 
237 ! needed for parallel processing.  
239 ! solve_em can integrate the equations using 3 time-integration methods
240 !      
241 !    - 3rd order Runge-Kutta time integration (recommended)
242 !      
243 !    - 2nd order Runge-Kutta time integration
244 !      
245 ! The main sections of solve_em are
246 !     
247 ! (1) Runge-Kutta (RK) loop
248 !     
249 ! (2) Non-timesplit physics (i.e., tendencies computed for updating
250 !     model state variables during the first RK sub-step (loop)
251 !     
252 ! (3) Small (acoustic, sound) timestep loop - within the RK sub-steps
253 !     
254 ! (4) scalar advance for moist and chem scalar variables (and TKE)
255 !     within the RK sub-steps.
256 !     
257 ! (5) time-split physics (after the RK step), currently this includes
258 !     only microphyics
260 ! A more detailed description of these sections follows.
261 !</pre>
262 !</DESCRIPTION>
264 ! Initialize timers if compiled with -DBENCH
265 #include "bench_solve_em_init.h"
267 #if (WRF_CMAQ == 1)
268    if (firstime) then
269       CALL nl_get_feedback_restart ( .false., feedback_restart )
270       if (feedback_restart) then
271          feedback_is_ready = .true.
272       else
273          feedback_is_ready = .false.
274       end if
275    end if
276 #else
277    feedback_is_ready = .false.
278 #endif
280 !  set runge-kutta solver (2nd or 3rd order)
282    dynamics_option = config_flags%rk_ord
284 !  Obtain dimension information stored in the grid data structure.
286    CALL get_ijk_from_grid (  grid ,                   &
287                              ids, ide, jds, jde, kds, kde,    &
288                              ims, ime, jms, jme, kms, kme,    &
289                              ips, ipe, jps, jpe, kps, kpe,    &
290                              imsx, imex, jmsx, jmex, kmsx, kmex,    &
291                              ipsx, ipex, jpsx, jpex, kpsx, kpex,    &
292                              imsy, imey, jmsy, jmey, kmsy, kmey,    &
293                              ipsy, ipey, jpsy, jpey, kpsy, kpey )
295    CALL get_ijk_from_subgrid (  grid ,                   &
296                              sids, side, sjds, sjde, skds, skde,    &
297                              sims, sime, sjms, sjme, skms, skme,    &
298                              sips, sipe, sjps, sjpe, skps, skpe    )
299    k_start         = kps
300    k_end           = kpe
302    num_3d_m        = num_moist
303    num_3d_c        = num_chem
304    num_3d_s        = num_scalar
306 !  backward integration needs to advect only QV
307    if (grid%dfi_stage .EQ. DFI_BCK) then
308        num_3d_m    = P_QV
309        num_3d_s    = PARAM_FIRST_SCALAR - 1
310    endif
312    f_flux = config_flags%do_avgflx_cugd .EQ. 1
314 !  Compute these starting and stopping locations for each tile and number of tiles.
315 !  See: https://www2.mmm.ucar.edu/wrf/WG2/topics/settiles
316    CALL set_tiles ( ZONE_SOLVE_EM, grid , ids , ide , jds , jde , ips , ipe , jps , jpe )
317 !   CALL set_tiles (  grid , ids , ide , jds , jde , ips , ipe , jps , jpe )
319 !  Max values of CFL for adaptive time step scheme
321    ALLOCATE (max_vert_cfl_tmp(grid%num_tiles))
322    ALLOCATE (max_horiz_cfl_tmp(grid%num_tiles))
324   !
325   ! Calculate current time in seconds since beginning of model run.
326   !   Unfortunately, ESMF does not seem to have a way to return
327   !   floating point seconds based on a TimeInterval.  So, we will
328   !   calculate it here--but, this is not clean!!
329   !
330    tmpTimeInterval  = domain_get_current_time ( grid ) - domain_get_sim_start_time ( grid )
331    tmpTimeInterval2 = domain_get_current_time ( grid ) - domain_get_start_time ( grid )
332    curr_secs  = real_time(tmpTimeInterval)
333    curr_secs2 = real_time(tmpTimeInterval2)
335    old_dt = grid%dt   ! store old time step for flux averaging code at end of RK loop
337 !-----------------------------------------------------------------------------
338 ! Adaptive time step: Added by T. Hutchinson, WSI  3/5/07
339 !   In this call, we do the time-step adaptation and set time-dependent lateral
340 !   boundary condition nudging weights.
342    IF ( (config_flags%use_adaptive_time_step) .and. &
343         ( (.not. grid%nested) .or. &
344         ( (grid%nested) .and. (abs(grid%dtbc) < 0.0001) ) ) )THEN
345       CALL adapt_timestep(grid, config_flags)
346       adapt_step_flag = .TRUE.
347    ELSE
348       adapt_step_flag = .FALSE.
349    ENDIF
350 ! End of adaptive time step modifications
351 !-----------------------------------------------------------------------------
353 ! Set restart flag value history output time
354 !-----------------------------------------------------------------------------
355    restart_flag = .false.
356    if ( Is_alarm_tstep(grid%domain_clock, grid%alarms(restart_alarm)) ) then
357       restart_flag = .true.
358    endif
360 ! Set diagnostic flag value history output time
361 !-----------------------------------------------------------------------------
363    ke_diag = kms ! default to ke_diag=1 in case of nwp_diagnostics == 1
364    diag_flag = .false.
365    if ( Is_alarm_tstep(grid%domain_clock, grid%alarms(HISTORY_ALARM)) ) then
366       diag_flag = .true.
367       ke_diag = min(k_end,kde-1) ! set depth to full domain for reflectivity field
368    endif
369    IF (config_flags%nwp_diagnostics == 1) diag_flag = .true.
371    grid%itimestep = grid%itimestep + 1
372    grid%dtbc = grid%dtbc + grid%dt
374    IF( coupler_on ) CALL cpl_store_input( grid, config_flags )
376    IF (config_flags%polar) dclat = 90./REAL(jde-jds) !(0.5 * 180/ny)
378 #if (WRF_CHEM == 1)
380    kte=min(k_end,kde-1)
381 # ifdef DM_PARALLEL
382    if ( num_chem >= PARAM_FIRST_SCALAR ) then
383 !-----------------------------------------------------------------------
384 ! see matching halo calls below for stencils
385 !--------------------------------------------------------------
386      CALL wrf_debug ( 200 , ' call HALO_RK_CHEM' )
387      IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
388 #      include "HALO_EM_CHEM_E_3.inc"
389        IF( config_flags%progn > 0 ) THEN
390 #         include "HALO_EM_SCALAR_E_3.inc"
391        ENDIF
392        IF( config_flags%cu_physics == CAMZMSCHEME ) THEN
393 #         include "HALO_EM_SCALAR_E_3.inc"
394        ENDIF
395      ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
396 #      include "HALO_EM_CHEM_E_5.inc"
397        IF( config_flags%cu_physics == CAMZMSCHEME ) THEN
398 #         include "HALO_EM_SCALAR_E_5.inc"
399        ENDIF
400        IF( config_flags%progn > 0 ) THEN
401 #         include "HALO_EM_SCALAR_E_5.inc"
402       ENDIF
403      ELSE
404        WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
405        CALL wrf_error_fatal(TRIM(wrf_err_message))
406      ENDIF
407    ENDIF
408    if ( num_tracer >= PARAM_FIRST_SCALAR ) then
409 !-----------------------------------------------------------------------
410 ! see matching halo calls below for stencils
411 !--------------------------------------------------------------
412      CALL wrf_debug ( 200 , ' call HALO_RK_tracer' )
413      IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
414 #      include "HALO_EM_TRACER_E_3.inc"
415      ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
416 #      include "HALO_EM_TRACER_E_5.inc"
417      ELSE
418        WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
419        CALL wrf_error_fatal(TRIM(wrf_err_message))
420      ENDIF
421    ENDIF
422 # endif
423 !--------------------------------------------------------------
424    adv_ct_indices(   :  ) = 1
425    IF ( config_flags%chemdiag == USECHEMDIAG ) THEN
426    ! modify tendency list here
427    ! note that the referencing direction here is opposite of that in chem_driver
428        adv_ct_indices(p_co  ) = p_advh_co
429        adv_ct_indices(p_o3  ) = p_advh_o3
430        adv_ct_indices(p_no  ) = p_advh_no
431        adv_ct_indices(p_no2 ) = p_advh_no2
432        adv_ct_indices(p_hno3) = p_advh_hno3
433        adv_ct_indices(p_iso ) = p_advh_iso
434        adv_ct_indices(p_ho  ) = p_advh_ho
435        adv_ct_indices(p_ho2 ) = p_advh_ho2
436    END IF
437 #endif
439    rk_order = config_flags%rk_ord
441    IF ( grid%time_step_sound == 0 ) THEN
442 ! This function will give 4 for 6*dx and 6 for 10*dx and returns even numbers only
443      spacing = min(grid%dx, grid%dy)
444      IF ( ( config_flags%use_adaptive_time_step ) .AND. ( config_flags%map_proj == PROJ_CASSINI ) ) THEN
445        max_msft=MIN ( MAX(grid%max_msftx, grid%max_msfty) , &
446                       1.0/COS(config_flags%fft_filter_lat*degrad) )
447        num_sound_steps = max ( 2 * ( INT (300. * grid%dt / (spacing / max_msft) - 0.01 ) + 1 ), 4 )
448      ELSE IF  ( config_flags%use_adaptive_time_step ) THEN
449        max_msft= MAX(grid%max_msftx, grid%max_msfty)
450        num_sound_steps = max ( 2 * ( INT (300. * grid%dt / (spacing / max_msft) - 0.01 ) + 1 ), 4 )
451      ELSE
452        num_sound_steps = max ( 2 * ( INT (300. * grid%dt /  spacing             - 0.01 ) + 1 ), 4 )
453      END IF
454      WRITE(wrf_err_message,*)'grid spacing, dt, time_step_sound=',spacing,grid%dt,num_sound_steps
455      CALL wrf_debug ( 50 , wrf_err_message )
456    ELSE
457      num_sound_steps = grid%time_step_sound
458    ENDIF
460    dts = grid%dt/float(num_sound_steps)
462    IF (config_flags%use_adaptive_time_step) THEN
463   
464      CALL get_wrf_debug_level( debug_level )
465      IF ((config_flags%time_step < 0) .AND. (debug_level.GE.50)) THEN
466 #ifdef DM_PARALLEL
467        CALL wrf_dm_maxval(grid%max_vert_cfl, idex, jdex)
468 #endif
469        WRITE(wrf_err_message,*)'variable dt, max horiz cfl, max vert cfl: ',&
470             grid%dt, grid%max_horiz_cfl, grid%max_vert_cfl
471        CALL wrf_debug ( 0 , wrf_err_message )
472      ENDIF
474      grid%max_cfl_val = 0
475      grid%max_horiz_cfl = 0
476      grid%max_vert_cfl = 0
477    ENDIF
479 ! setting bdy tendencies to zero for DFI if constant_bc = true
481      !$OMP PARALLEL DO   &
482      !$OMP PRIVATE ( ij )
483      DO ij = 1 , grid%num_tiles
485 !      IF( config_flags%specified .AND. grid%dfi_opt .NE. DFI_NODFI   &
486 !          .AND. config_flags%constant_bc .AND. (grid%dfi_stage .EQ. DFI_BCK .OR. grid%dfi_stage .EQ. DFI_FWD) ) THEN
487        IF( config_flags%specified .AND. config_flags%constant_bc ) THEN
489        CALL zero_bdytend (grid%u_btxs,grid%u_btxe,grid%u_btys,grid%u_btye,     &
490                           grid%v_btxs,grid%v_btxe,grid%v_btys,grid%v_btye,     &
491                           grid%ph_btxs,grid%ph_btxe,grid%ph_btys,grid%ph_btye, &
492                           grid%t_btxs,grid%t_btxe,grid%t_btys,grid%t_btye,     &
493                           grid%w_btxs,grid%w_btxe,grid%w_btys,grid%w_btye,     &
494                           grid%mu_btxs,grid%mu_btxe,grid%mu_btys,grid%mu_btye, &
495                           moist_btxs,moist_btxe,                               &
496                           moist_btys,moist_btye,                               &
497                           scalar_btxs,scalar_btxe,                              &
498                           scalar_btys,scalar_btye,                              &
499                           grid%spec_bdy_width,num_3d_m,num_3d_s,                &
500                           ids,ide, jds,jde, kds,kde,                   &
501                           ims,ime, jms,jme, kms,kme,                   &
502                           ips,ipe, jps,jpe, kps,kpe,                   &
503                           grid%i_start(ij), grid%i_end(ij),            &
504                           grid%j_start(ij), grid%j_end(ij),            &
505                           k_start, k_end                               )
507        ENDIF
509        !  If the user has requested to optionally select the moist theta (use_theta_m==1)
510        !  switch, the first setting of the "old" value of theta_m uses the "old"
511        !  value of Qv.  The moist_old variable does not exist until after the advection
512        !  towards the end of the RK loop.  For the first time in the RK loop, we need
513        !  a reasonable value for moist_old.
515        CALL initialize_moist_old ( moist_old(:,:,:,P_Qv),              &
516                           moist(:,:,:,P_Qv) ,                          &
517                           ids,ide, jds,jde, kds,kde,                   &
518                           ims,ime, jms,jme, kms,kme,                   &
519                           grid%i_start(ij), grid%i_end(ij),            &
520                           grid%j_start(ij), grid%j_end(ij),            &
521                           k_start, k_end                               )
522      ENDDO
523      !$OMP END PARALLEL DO
525      !  Now that we have initialized the moist_old values with P_Qv for
526      !  computing a moist t_tendf after rk_step part2, fill in the halo 
527      !  and period boundaries.
529 #if ( defined( DM_PARALLEL ) && ( ! defined( STUBMPI ) ) )
530 #  include "HALO_EM_MOIST_OLD_E_7.inc"
531 #  include "PERIOD_BDY_EM_MOIST_OLD.inc"
532 #endif
533      !$OMP PARALLEL DO   &
534      !$OMP PRIVATE ( ij )
535      DO ij = 1 , grid%num_tiles
536        im = P_Qv
537        CALL set_physical_bc3d( moist_old(ims,kms,jms,im), 'p', config_flags,  &
538                                ids, ide, jds, jde, kds, kde,                  &
539                                ims, ime, jms, jme, kms, kme,                  &
540                                ips, ipe, jps, jpe, kps, kpe,                  &
541                                grid%i_start(ij), grid%i_end(ij),              &
542                                grid%j_start(ij), grid%j_end(ij),              &
543                                k_start    , k_end                             )
544      END DO
545      !$OMP END PARALLEL DO
547 !**********************************************************************
549 !  LET US BEGIN.......
551 !<DESCRIPTION>
552 !<pre>
553 ! (1) RK integration loop is named the "Runge_Kutta_loop:"
555 !   Predictor-corrector type time integration.
556 !   Advection terms are evaluated at time t for the predictor step,
557 !   and advection is re-evaluated with the latest predicted value for
558 !   each succeeding time corrector step
560 !   2nd order Runge Kutta (rk_order = 2):
561 !   Step 1 is taken to the midpoint predictor, step 2 is the full step.
563 !   3rd order Runge Kutta (rk_order = 3):
564 !   Step 1 is taken to from t to dt/3, step 2 is from t to dt/2,
565 !   and step 3 is from t to dt.
567 !   non-timesplit physics are evaluated during first RK step and
568 !   these physics tendencies are stored for use in each RK pass.
569 !</pre>
570 !</DESCRIPTION>
571 !**********************************************************************
573    Runge_Kutta_loop:  DO rk_step = 1, rk_order
575    !  Set the step size and number of small timesteps for
576    !  each part of the timestep
578      dtm = grid%dt
579      IF ( rk_order == 1 ) THEN   
581        write(wrf_err_message,*)' leapfrog removed, error exit for dynamics_option = ',dynamics_option
582        CALL wrf_error_fatal( wrf_err_message )
584      ELSE IF ( rk_order == 2 ) THEN   ! 2nd order Runge-Kutta timestep
586        IF ( rk_step == 1) THEN
587          dt_rk  = 0.5*grid%dt
588          dts_rk = dts
589          number_of_small_timesteps = num_sound_steps/2
590        ELSE
591          dt_rk = grid%dt
592          dts_rk = dts
593          number_of_small_timesteps = num_sound_steps
594        ENDIF
596      ELSE IF ( rk_order == 3 ) THEN ! third order Runge-Kutta
598        IF ( rk_step == 1) THEN
599          dt_rk = grid%dt/3.
600          dts_rk = dt_rk
601          number_of_small_timesteps = 1
602        ELSE IF (rk_step == 2) THEN
603          dt_rk  = 0.5*grid%dt
604          dts_rk = dts
605          number_of_small_timesteps = num_sound_steps/2
606        ELSE
607          dt_rk = grid%dt
608          dts_rk = dts
609          number_of_small_timesteps = num_sound_steps
610        ENDIF
612      ELSE
614        write(wrf_err_message,*)' unknown solver, error exit for dynamics_option = ',dynamics_option
615        CALL wrf_error_fatal( wrf_err_message )
617      END IF
619 !  Ensure that polar meridional velocity is zero
620      IF (config_flags%polar) THEN 
621        !$OMP PARALLEL DO   &
622        !$OMP PRIVATE ( ij )
623        DO ij = 1 , grid%num_tiles
624          CALL zero_pole ( grid%v_1,                      &
625                           ids, ide, jds, jde, kds, kde,     &
626                           ims, ime, jms, jme, kms, kme,     &
627                           grid%i_start(ij), grid%i_end(ij), &
628                           grid%j_start(ij), grid%j_end(ij), &
629                           k_start, k_end                   )
630          CALL zero_pole ( grid%v_2,                      &
631                           ids, ide, jds, jde, kds, kde,     &
632                           ims, ime, jms, jme, kms, kme,     &
633                           grid%i_start(ij), grid%i_end(ij), &
634                           grid%j_start(ij), grid%j_end(ij), &
635                           k_start, k_end                   )
636        END DO
637        !$OMP END PARALLEL DO
638      END IF
640 !  Time level t is in the *_2 variable in the first part 
641 !  of the step, and in the *_1 variable after the predictor.
642 !  the latest predicted values are stored in the *_2 variables.
644      CALL wrf_debug ( 200 , ' call rk_step_prep ' )
646 BENCH_START(step_prep_tim)
647      !$OMP PARALLEL DO   &
648      !$OMP PRIVATE ( ij )
650      DO ij = 1 , grid%num_tiles
652        CALL rk_step_prep  ( config_flags, rk_step,            &
653                             grid%u_2, grid%v_2, grid%w_2, grid%t_2, grid%ph_2, grid%mu_2,   &
654                             grid%c1h, grid%c2h, grid%c1f, grid%c2f, moist,   &
655                             grid%ru, grid%rv, grid%rw, grid%ww, grid%php, grid%alt, grid%muu, grid%muv,   &
656                             grid%mub, grid%mut, grid%phb, grid%pb, grid%p, grid%al, grid%alb,    &
657                             cqu, cqv, cqw,                    &
658                             grid%msfux, grid%msfuy, grid%msfvx, grid%msfvx_inv,        &
659                             grid%msfvy, grid%msftx, grid%msfty,                        &
660                             grid%fnm, grid%fnp, grid%dnw, grid%rdx, grid%rdy,          &
661                             num_3d_m,                         &
662                             ids, ide, jds, jde, kds, kde,     &
663                             ims, ime, jms, jme, kms, kme,     &
664                             grid%i_start(ij), grid%i_end(ij), &
665                             grid%j_start(ij), grid%j_end(ij), &
666                             k_start, k_end                   )
668      END DO
669      !$OMP END PARALLEL DO
670 BENCH_END(step_prep_tim)
672 #ifdef DM_PARALLEL
673 !-----------------------------------------------------------------------
674 !  Stencils for patch communications  (WCS, 29 June 2001)
675 !  Note:  the small size of this halo exchange reflects the 
676 !         fact that we are carrying the uncoupled variables 
677 !         as state variables in the mass coordinate model, as
678 !         opposed to the coupled variables as in the height
679 !         coordinate model.
681 !                           * * * * *
682 !         *        * * *    * * * * *
683 !       * + *      * + *    * * + * * 
684 !         *        * * *    * * * * *
685 !                           * * * * *
687 !  3D variables - note staggering!  ru(X), rv(Y), ww(Z), php(Z)
689 !  ru     x
690 !  rv     x
691 !  ww     x
692 !  php    x
693 !  alt    x
694 !  ph_2   x
695 !  phb    x
697 !  the following are 2D (xy) variables
699 !  muu    x
700 !  muv    x
701 !  mut    x
702 !--------------------------------------------------------------
703 #    include "HALO_EM_A.inc"
704 #endif
706 ! set boundary conditions on variables 
707 ! from big_step_prep for use in big_step_proc
709 #ifdef DM_PARALLEL
710 #  include "PERIOD_BDY_EM_A.inc"
711 #endif
713 BENCH_START(set_phys_bc_tim)
714      !$OMP PARALLEL DO   &
715      !$OMP PRIVATE ( ij, ii, jj, kk )
717      DO ij = 1 , grid%num_tiles
719        CALL wrf_debug ( 200 , ' call rk_phys_bc_dry_1' )
721        CALL rk_phys_bc_dry_1( config_flags, grid%ru, grid%rv, grid%rw, grid%ww,      & 
722                               grid%muu, grid%muv, grid%mut, grid%php, grid%alt, grid%p,        &
723                               ids, ide, jds, jde, kds, kde,      &
724                               ims, ime, jms, jme, kms, kme,      &
725                               ips, ipe, jps, jpe, kps, kpe,      &
726                               grid%i_start(ij), grid%i_end(ij),  &
727                               grid%j_start(ij), grid%j_end(ij),  &
728                               k_start, k_end                )
729        CALL set_physical_bc3d( grid%rho, 'p', config_flags,            &
730                               ids, ide, jds, jde, kds, kde,     &
731                               ims, ime, jms, jme, kms, kme,     &
732                               ips, ipe, jps, jpe, kps, kpe,     &
733                               grid%i_start(ij), grid%i_end(ij), &
734                               grid%j_start(ij), grid%j_end(ij), &
735                               k_start    , k_end               )
736        CALL set_physical_bc3d( grid%al, 'p', config_flags,            &
737                               ids, ide, jds, jde, kds, kde,     &
738                               ims, ime, jms, jme, kms, kme,     &
739                               ips, ipe, jps, jpe, kps, kpe,     &
740                               grid%i_start(ij), grid%i_end(ij), &
741                               grid%j_start(ij), grid%j_end(ij), &
742                               k_start    , k_end               )
743        CALL set_physical_bc3d( grid%ph_2, 'w', config_flags,            &
744                               ids, ide, jds, jde, kds, kde, &
745                               ims, ime, jms, jme, kms, kme, &
746                               ips, ipe, jps, jpe, kps, kpe, &
747                               grid%i_start(ij), grid%i_end(ij),        &
748                               grid%j_start(ij), grid%j_end(ij),        &
749                               k_start, k_end                )
751        IF (config_flags%polar) THEN 
753 !-------------------------------------------------------
754 ! lat-lon grid pole-point (v) specification (extrapolate v, rv to the pole)
755 !-------------------------------------------------------
757          CALL pole_point_bc ( grid%v_1,                      &
758                               ids, ide, jds, jde, kds, kde,     &
759                               ims, ime, jms, jme, kms, kme,     &
760                               grid%i_start(ij), grid%i_end(ij), &
761                               grid%j_start(ij), grid%j_end(ij), &
762                               k_start, k_end                   )
764          CALL pole_point_bc ( grid%v_2,                      &
765                               ids, ide, jds, jde, kds, kde,     &
766                               ims, ime, jms, jme, kms, kme,     &
767                               grid%i_start(ij), grid%i_end(ij), &
768                               grid%j_start(ij), grid%j_end(ij), &
769                               k_start, k_end                   )
771 !-------------------------------------------------------
772 ! end lat-lon grid pole-point (v) specification
773 !-------------------------------------------------------
775        ENDIF
776      END DO
777      !$OMP END PARALLEL DO
778 BENCH_END(set_phys_bc_tim)
780      rk_step_is_one : IF (rk_step == 1) THEN ! only need to initialize diffusion tendencies
782 BENCH_START(calc_p_rho_tim)
784 !<DESCRIPTION>
785 !<pre>
786 !(2) The non-timesplit physics begins with a call to "phy_prep"
787 !    (which computes some diagnostic variables such as temperature,
788 !    pressure, u and v at p points, etc).  This is followed by
789 !    calls to the physics drivers:
791 !              radiation,
792 !              surface,
793 !              pbl,
794 !              cumulus,
795 !              fddagd,
796 !              3D TKE and mixing.
797 !<pre>
798 !</DESCRIPTION>
800        IF (coupler_on) CALL cpl_settime( curr_secs2 )
802        CALL first_rk_step_part1 (    grid, config_flags         &
803                              , moist , moist_tend               &
804                              , chem  , chem_tend                &
805                              , tracer, tracer_tend              &
806                              , scalar , scalar_tend             &
807                              , fdda3d, fdda2d                   &
808                              , aerod                            &
809                              , ru_tendf, rv_tendf               &
810                              , rw_tendf, t_tendf                &
811                              , ph_tendf, mu_tendf               &
812                              , tke_tend                         &
813                              , config_flags%use_adaptive_time_step &
814                              , curr_secs                        &
815                              , psim , psih , gz1oz0             &
816                              , chklowq                          &
817                              , cu_act_flag , hol , th_phy       &
818                              , pi_phy , p_phy , grid%t_phy      &
819                              , dz8w , p8w , t8w                 &
820                              , ids, ide, jds, jde, kds, kde     &
821                              , ims, ime, jms, jme, kms, kme     &
822                              , ips, ipe, jps, jpe, kps, kpe     &
823                              , imsx, imex, jmsx, jmex, kmsx, kmex    &
824                              , ipsx, ipex, jpsx, jpex, kpsx, kpex    &
825                              , imsy, imey, jmsy, jmey, kmsy, kmey    &
826                              , ipsy, ipey, jpsy, jpey, kpsy, kpey    &
827                              , k_start , k_end                  &
828                              , f_flux=f_flux                    &
829                              , aerocu=aerocu                    &
830                              , restart_flag=restart_flag        &
831                              , feedback_is_ready=feedback_is_ready    &
832                             )
834 #ifdef DM_PARALLEL
835        IF ( config_flags%bl_pbl_physics == MYNNPBLSCHEME ) THEN
836 #        include "HALO_EM_SCALAR_E_5.inc"
837        ENDIF
838 #endif
840        CALL first_rk_step_part2 (    grid, config_flags         &
841                              , moist , moist_old , moist_tend   &
842                              , chem  , chem_tend                &
843                              , tracer, tracer_tend              &
844                              , scalar , scalar_tend             &
845                              , fdda3d, fdda2d                   &
846                              , ru_tendf, rv_tendf               &
847                              , rw_tendf, t_tendf                &
848                              , ph_tendf, mu_tendf               &
849                              , tke_tend                         &
850                              , adapt_step_flag , curr_secs      &
851                              , psim , psih , gz1oz0             &
852                              , chklowq                          &
853                              , cu_act_flag , hol , th_phy       &
854                              , pi_phy , p_phy , grid%t_phy      &
855                              , dz8w , p8w , t8w                 &
856                              , nba_mij, num_nba_mij             & !JDM 
857                              , nba_rij, num_nba_rij             & !JDM 
858                              , ids, ide, jds, jde, kds, kde     &
859                              , ims, ime, jms, jme, kms, kme     &
860                              , ips, ipe, jps, jpe, kps, kpe     &
861                              , imsx, imex, jmsx, jmex, kmsx, kmex    &
862                              , ipsx, ipex, jpsx, jpex, kpsx, kpex    &
863                              , imsy, imey, jmsy, jmey, kmsy, kmey    &
864                              , ipsy, ipey, jpsy, jpey, kpsy, kpey    &
865                              , k_start , k_end                  &
866                             )
868      END IF rk_step_is_one
870 BENCH_START(rk_tend_tim)
871      !$OMP PARALLEL DO   &
872      !$OMP PRIVATE ( ij )
873      DO ij = 1 , grid%num_tiles
875        CALL wrf_debug ( 200 , ' call rk_tendency' )
876        CALL rk_tendency ( config_flags, rk_step                                                                &
877                          ,grid%ru_tend, grid%rv_tend, rw_tend, ph_tend, t_tend                                 &
878                          ,ru_tendf, rv_tendf, rw_tendf, ph_tendf, t_tendf                                      &
879                          ,mu_tend, grid%u_save, grid%v_save, w_save, ph_save                                   &
880                          ,grid%t_save, mu_save, grid%rthften                                                   &
881                          ,grid%ru, grid%rv, grid%rw, grid%ww, wwE, wwI                                         &
882                          ,grid%u_2, grid%v_2, grid%w_2, grid%t_2, grid%ph_2                                    &
883                          ,grid%u_1, grid%v_1, grid%w_1, grid%t_1, grid%ph_1                                    &
884                          ,grid%h_diabatic, grid%phb, grid%t_init                                               &
885                          ,grid%mu_1, grid%mu_2, grid%mut, grid%muu, grid%muv, grid%mub                         & 
886                          ,grid%c1h, grid%c2h, grid%c1f, grid%c2f                                               &
887                          ,grid%al, grid%ht, grid%alt, grid%p, grid%pb, grid%php, cqu, cqv, cqw                 & 
888                          ,grid%u_base, grid%v_base, grid%t_base, grid%qv_base, grid%z_base                     &
889                          ,grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv                                    &
890                          ,grid%msfvy, grid%msftx,grid%msfty, grid%clat, grid%f, grid%e, grid%sina, grid%cosa   &
891                          ,grid%fnm, grid%fnp, grid%rdn, grid%rdnw                                              &
892                          ,grid%dt, grid%rdx, grid%rdy, grid%khdif, grid%kvdif, grid%xkmh, grid%xkhh            &
893                          ,grid%diff_6th_opt, grid%diff_6th_factor                                              &
894                          ,config_flags%momentum_adv_opt                                                        &
895                          ,grid%dampcoef,grid%zdamp,config_flags%damp_opt,config_flags%rad_nudge                &
896                          ,grid%cf1, grid%cf2, grid%cf3, grid%cfn, grid%cfn1, num_3d_m                          &
897                          ,config_flags%non_hydrostatic, config_flags%top_lid                                   &
898                          ,grid%u_frame, grid%v_frame                                                           &
899                          ,ids, ide, jds, jde, kds, kde                                                         &
900                          ,ims, ime, jms, jme, kms, kme                                                         &
901                          ,grid%i_start(ij), grid%i_end(ij)                                                     &
902                          ,grid%j_start(ij), grid%j_end(ij)                                                     &
903                          ,k_start, k_end                                                                       &
904                          ,max_vert_cfl_tmp(ij), max_horiz_cfl_tmp(ij)                                         )
905      END DO
906      !$OMP END PARALLEL DO
907 BENCH_END(rk_tend_tim)
909      IF (config_flags%use_adaptive_time_step) THEN
910        DO ij = 1 , grid%num_tiles
911          IF (max_horiz_cfl_tmp(ij) .GT. grid%max_horiz_cfl) THEN
912            grid%max_horiz_cfl = max_horiz_cfl_tmp(ij)
913          ENDIF
914          IF (max_vert_cfl_tmp(ij) .GT. grid%max_vert_cfl) THEN
915            grid%max_vert_cfl = max_vert_cfl_tmp(ij)
916          ENDIF
917        END DO
918      
919        IF (grid%max_horiz_cfl .GT. grid%max_cfl_val) THEN
920          grid%max_cfl_val = grid%max_horiz_cfl
921        ENDIF
922        IF (grid%max_vert_cfl .GT. grid%max_cfl_val) THEN
923          grid%max_cfl_val = grid%max_vert_cfl
924        ENDIF
925      ENDIF
927 BENCH_START(relax_bdy_dry_tim)
928      !$OMP PARALLEL DO   &
929      !$OMP PRIVATE ( ij )
930      DO ij = 1 , grid%num_tiles
932        IF ( (config_flags%specified .or. config_flags%nested) .and. ( rk_step == 1 ) ) THEN 
934          CALL relax_bdy_dry ( config_flags,                                &
935                               grid%u_save, grid%v_save, ph_save, grid%t_save,             &
936                               w_save, mu_tend, grid%c1h, grid%c2h, grid%c1f, grid%c2f, &
937                               grid%ru, grid%rv, grid%ph_2, grid%t_2,                           &
938                               grid%w_2, grid%mu_2, grid%mut,                              &
939                               grid%u_bxs,grid%u_bxe,grid%u_bys,grid%u_bye, &
940                               grid%v_bxs,grid%v_bxe,grid%v_bys,grid%v_bye, &
941                               grid%ph_bxs,grid%ph_bxe,grid%ph_bys,grid%ph_bye, &
942                               grid%t_bxs,grid%t_bxe,grid%t_bys,grid%t_bye, &
943                               grid%w_bxs,grid%w_bxe,grid%w_bys,grid%w_bye, &
944                               grid%mu_bxs,grid%mu_bxe,grid%mu_bys,grid%mu_bye, &
945                               grid%u_btxs,grid%u_btxe,grid%u_btys,grid%u_btye, &
946                               grid%v_btxs,grid%v_btxe,grid%v_btys,grid%v_btye, &
947                               grid%ph_btxs,grid%ph_btxe,grid%ph_btys,grid%ph_btye, &
948                               grid%t_btxs,grid%t_btxe,grid%t_btys,grid%t_btye, &
949                               grid%w_btxs,grid%w_btxe,grid%w_btys,grid%w_btye, &
950                               grid%mu_btxs,grid%mu_btxe,grid%mu_btys,grid%mu_btye, &
951                               config_flags%spec_bdy_width, grid%spec_zone, grid%relax_zone,       &
952                               grid%dtbc, grid%fcx, grid%gcx,                              &
953                               ids,ide, jds,jde, kds,kde,                   &
954                               ims,ime, jms,jme, kms,kme,                   &
955                               ips,ipe, jps,jpe, kps,kpe,                   &
956                               grid%i_start(ij), grid%i_end(ij),            &
957                               grid%j_start(ij), grid%j_end(ij),            &
958                               k_start, k_end                              )
960        ENDIF
962        CALL rk_addtend_dry( grid%ru_tend,  grid%rv_tend,  rw_tend,  ph_tend,  t_tend,  &
963                             ru_tendf, rv_tendf, rw_tendf, ph_tendf, t_tendf, &
964                             grid%u_save, grid%v_save, w_save, ph_save, grid%t_save, &
965                             mu_tend, mu_tendf, rk_step,                      &
966                             grid%c1h, grid%c2h,                              &
967                             grid%h_diabatic, grid%mut, grid%msftx,           &
968                             grid%msfty, grid%msfux,grid%msfuy,               &
969                             grid%msfvx, grid%msfvx_inv, grid%msfvy,          &
970                             ids,ide, jds,jde, kds,kde,                       &
971                             ims,ime, jms,jme, kms,kme,                       &
972                             ips,ipe, jps,jpe, kps,kpe,                       &
973                             grid%i_start(ij), grid%i_end(ij),                &
974                             grid%j_start(ij), grid%j_end(ij),                &
975                             k_start, k_end                                  )
977        IF( config_flags%specified .or. config_flags%nested ) THEN 
978          CALL spec_bdy_dry ( config_flags,                                    &
979                              grid%ru_tend, grid%rv_tend, ph_tend, t_tend,               &
980                              rw_tend, mu_tend,                                &
981                              grid%u_bxs,grid%u_bxe,grid%u_bys,grid%u_bye, &
982                              grid%v_bxs,grid%v_bxe,grid%v_bys,grid%v_bye, &
983                              grid%ph_bxs,grid%ph_bxe,grid%ph_bys,grid%ph_bye, &
984                              grid%t_bxs,grid%t_bxe,grid%t_bys,grid%t_bye, &
985                              grid%w_bxs,grid%w_bxe,grid%w_bys,grid%w_bye, &
986                              grid%mu_bxs,grid%mu_bxe,grid%mu_bys,grid%mu_bye, &
987                              grid%u_btxs,grid%u_btxe,grid%u_btys,grid%u_btye, &
988                              grid%v_btxs,grid%v_btxe,grid%v_btys,grid%v_btye, &
989                              grid%ph_btxs,grid%ph_btxe,grid%ph_btys,grid%ph_btye, &
990                              grid%t_btxs,grid%t_btxe,grid%t_btys,grid%t_btye, &
991                              grid%w_btxs,grid%w_btxe,grid%w_btys,grid%w_btye, &
992                              grid%mu_btxs,grid%mu_btxe,grid%mu_btys,grid%mu_btye, &
993                              config_flags%spec_bdy_width, grid%spec_zone,                       &
994                              ids,ide, jds,jde, kds,kde,  & ! domain dims
995                              ims,ime, jms,jme, kms,kme,  & ! memory dims
996                              ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
997                              grid%i_start(ij), grid%i_end(ij),                &
998                              grid%j_start(ij), grid%j_end(ij),                &
999                              k_start, k_end                                  )
1002        ENDIF
1004 !---------------------------------------------------------------------------------------------
1005 ! KRS 9/12/2012: Inserted new IF block calls to spec_bdy_dry_perturb. If peturb_bdy=1, SKEBS 
1006 ! pattern passed in for perturbing the specified boundry conditions.  If peturb_bdy=2, user
1007 ! must provide pattern.  mu_2, mub, msf* also passed in for coupling needed for tendecies.
1008 !---------------------------------------------------------------------------------------------
1009        IF( config_flags%specified .and. config_flags%perturb_bdy==1 ) THEN 
1010          CALL spec_bdy_dry_perturb ( config_flags,                                 &
1011                              grid%ru_tend, grid%rv_tend, t_tend,                   &
1012                              grid%mu_2, grid%mub, grid%c1h, grid%c2h,              &
1013                              grid%msfux, grid%msfvx, grid%msft,                    &
1014                              grid%ru_tendf_stoch, grid%rv_tendf_stoch, grid%rt_tendf_stoch, &
1015                              config_flags%spec_bdy_width, grid%spec_zone,                   &
1016                              grid%num_stoch_levels,      & ! stoch dims
1017                              ids,ide, jds,jde, kds,kde,  & ! domain dims
1018                              ims,ime, jms,jme, kms,kme,  & ! memory dims
1019                              ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
1020                              grid%i_start(ij), grid%i_end(ij),                &
1021                              grid%j_start(ij), grid%j_end(ij),                &
1022                              k_start, k_end                                  )
1023      
1024        ENDIF
1026        IF( config_flags%specified .and. config_flags%perturb_bdy==2 ) THEN
1027          CALL spec_bdy_dry_perturb ( config_flags,                                 &
1028                              grid%ru_tend, grid%rv_tend, t_tend,                   &
1029                              grid%mu_2, grid%mub, grid%c1h, grid%c2h,              &
1030                              grid%msfux, grid%msfvx, grid%msft,                    &
1031                              grid%field_u_tend_perturb, grid%field_v_tend_perturb, grid%field_t_tend_perturb, &
1032                              config_flags%spec_bdy_width, grid%spec_zone,                   &
1033                              grid%num_stoch_levels,      & ! stoch dims
1034                              ids,ide, jds,jde, kds,kde,  & ! domain dims
1035                              ims,ime, jms,jme, kms,kme,  & ! memory dims
1036                              ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
1037                              grid%i_start(ij), grid%i_end(ij),                &
1038                              grid%j_start(ij), grid%j_end(ij),                &
1039                              k_start, k_end                                  )
1040   
1041        ENDIF
1043      END DO
1044      !$OMP END PARALLEL DO
1045 BENCH_END(relax_bdy_dry_tim)
1047 !<DESCRIPTION>
1048 !<pre>
1049 ! (3) Small (acoustic,sound) steps.
1051 !    Several acoustic steps are taken each RK pass.  A small step 
1052 !    sequence begins with calculating perturbation variables 
1053 !    and coupling them to the column dry-air-mass mu 
1054 !    (call to small_step_prep).  This is followed by computing
1055 !    coefficients for the vertically implicit part of the
1056 !    small timestep (call to calc_coef_w).  
1058 !    The small steps are taken
1059 !    in the named loop "small_steps:".  In the small_steps loop, first 
1060 !    the horizontal momentum (u and v) are advanced (call to advance_uv),
1061 !    next mu and theta are advanced (call to advance_mu_t) followed by
1062 !    advancing w and the geopotential (call to advance_w).  Diagnostic
1063 !    values for pressure and inverse density are updated at the end of
1064 !    each small_step.
1066 !    The small-step section ends with the change of the perturbation variables
1067 !    back to full variables (call to small_step_finish).
1068 !</pre>
1069 !</DESCRIPTION>
1071 BENCH_START(small_step_prep_tim)
1072      !$OMP PARALLEL DO   &
1073      !$OMP PRIVATE ( ij )
1074      DO ij = 1 , grid%num_tiles
1076     ! Calculate coefficients for the vertically implicit acoustic/gravity wave
1077     ! integration.  We only need calculate these for the first pass through -
1078     ! the predictor step.  They are reused as is for the corrector step.
1079     ! For third-order RK, we need to recompute these after the first 
1080     ! predictor because we may have changed the small timestep -> grid%dts.
1082        CALL wrf_debug ( 200 , ' call small_step_prep ' )
1084        CALL small_step_prep( grid%u_1,grid%u_2,grid%v_1,grid%v_2,grid%w_1,grid%w_2,   &
1085                              grid%t_1,grid%t_2,grid%ph_1,grid%ph_2,                   &
1086                              grid%mub, grid%mu_1, grid%mu_2,                          &
1087                              grid%muu, grid%muus, grid%muv, grid%muvs,                &
1088                              grid%mut, grid%muts, grid%mudf,                          &
1089                              grid%c1h, grid%c2h, grid%c1f, grid%c2f,                  &
1090                              grid%c3h, grid%c4h, grid%c3f, grid%c4f,                  &
1091                              grid%u_save, grid%v_save, w_save,                        &
1092                              grid%t_save, ph_save, mu_save,                           &
1093                              grid%ww, ww1,                                            &
1094                              c2a, grid%pb, grid%p, grid%alt,                          &
1095                              grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv,       &
1096                              grid%msfvy, grid%msftx,grid%msfty,                       &
1097                              grid%rdx, grid%rdy, rk_step,                             &
1098                              ids, ide, jds, jde, kds, kde,                            &
1099                              ims, ime, jms, jme, kms, kme,                            &
1100                              grid%i_start(ij), grid%i_end(ij),                        &
1101                              grid%j_start(ij), grid%j_end(ij),                        &
1102                              k_start    , k_end                                       )
1104        CALL calc_p_rho( grid%al, grid%p, grid%ph_2,                 &
1105                         grid%alt, grid%t_2, grid%t_save, c2a, pm1,  &
1106                         grid%mu_2, grid%muts,                       &
1107                         grid%c1h, grid%c2h, grid%c1f, grid%c2f,     &
1108                         grid%c3h, grid%c4h, grid%c3f, grid%c4f,     &
1109                         grid%znu, t0,                               &
1110                         grid%rdnw, grid%dnw, grid%smdiv,            &
1111                         config_flags%non_hydrostatic, 0,            &
1112                         ids, ide, jds, jde, kds, kde,               &
1113                         ims, ime, jms, jme, kms, kme,               &
1114                         grid%i_start(ij), grid%i_end(ij),           &
1115                         grid%j_start(ij), grid%j_end(ij),           &
1116                         k_start    , k_end                          )
1118        IF (config_flags%non_hydrostatic) THEN
1119          CALL calc_coef_w( a,alpha,gamma,                    &
1120                            grid%mut,                         &
1121                            grid%c1h, grid%c2h, grid%c1f, grid%c2f, &
1122                            grid%c3h, grid%c4h, grid%c3f, grid%c4f, &
1123                            cqw, grid%rdn, grid%rdnw, c2a,    &
1124                            dts_rk, g, grid%epssm,            &
1125                            config_flags%top_lid,             &
1126                            ids, ide, jds, jde, kds, kde,     &
1127                            ims, ime, jms, jme, kms, kme,     &
1128                            grid%i_start(ij), grid%i_end(ij), &
1129                            grid%j_start(ij), grid%j_end(ij), &
1130                            k_start    , k_end               )
1131        ENDIF
1133      ENDDO
1134      !$OMP END PARALLEL DO
1135 BENCH_END(small_step_prep_tim)
1137 #ifdef DM_PARALLEL
1138 !-----------------------------------------------------------------------
1139 !  Stencils for patch communications  (WCS, 29 June 2001)
1140 !  Note:  the small size of this halo exchange reflects the 
1141 !         fact that we are carrying the uncoupled variables 
1142 !         as state variables in the mass coordinate model, as
1143 !         opposed to the coupled variables as in the height
1144 !         coordinate model.
1146 !                              * * * * *
1147 !            *        * * *    * * * * *
1148 !          * + *      * + *    * * + * * 
1149 !            *        * * *    * * * * *
1150 !                              * * * * *
1152 !  3D variables - note staggering!  ph_2(Z), u_save(X), v_save(Y)
1154 !  ph_2      x
1155 !  al        x
1156 !  p         x
1157 !  t_1       x
1158 !  t_save    x
1159 !  u_save    x
1160 !  v_save    x
1162 !  the following are 2D (xy) variables
1164 !  mu_1      x
1165 !  mu_2      x
1166 !  mudf      x
1167 !  php       x
1168 !  alt       x
1169 !  pb        x
1170 !--------------------------------------------------------------
1171 #      include "HALO_EM_B.inc"
1172 #      include "PERIOD_BDY_EM_B.inc"
1173 #endif
1175 BENCH_START(set_phys_bc2_tim)
1176      !$OMP PARALLEL DO   &
1177      !$OMP PRIVATE ( ij )
1179      DO ij = 1 , grid%num_tiles
1181        CALL set_physical_bc3d( grid%ru_tend, 'u', config_flags,      &
1182                                ids, ide, jds, jde, kds, kde,         &
1183                                ims, ime, jms, jme, kms, kme,         &
1184                                ips, ipe, jps, jpe, kps, kpe,         &
1185                                grid%i_start(ij), grid%i_end(ij),     &
1186                                grid%j_start(ij), grid%j_end(ij),     &
1187                                k_start    , k_end                    )
1189        CALL set_physical_bc3d( grid%rv_tend, 'v', config_flags,      &
1190                                ids, ide, jds, jde, kds, kde,         &
1191                                ims, ime, jms, jme, kms, kme,         &
1192                                ips, ipe, jps, jpe, kps, kpe,         &
1193                                grid%i_start(ij), grid%i_end(ij),     &
1194                                grid%j_start(ij), grid%j_end(ij),     &
1195                                k_start    , k_end                    )
1197        CALL set_physical_bc3d( grid%ph_2, 'w', config_flags,         &
1198                                ids, ide, jds, jde, kds, kde,         &
1199                                ims, ime, jms, jme, kms, kme,         &
1200                                ips, ipe, jps, jpe, kps, kpe,         &
1201                                grid%i_start(ij), grid%i_end(ij),     &
1202                                grid%j_start(ij), grid%j_end(ij),     &
1203                                k_start    , k_end                    )
1205        CALL set_physical_bc3d( grid%al, 'p', config_flags,           &
1206                                ids, ide, jds, jde, kds, kde,         &
1207                                ims, ime, jms, jme, kms, kme,         &
1208                                ips, ipe, jps, jpe, kps, kpe,         &
1209                                grid%i_start(ij), grid%i_end(ij),     &
1210                                grid%j_start(ij), grid%j_end(ij),     &
1211                                k_start    , k_end                    )
1213        CALL set_physical_bc3d( grid%p, 'p', config_flags,            &
1214                                ids, ide, jds, jde, kds, kde,         &
1215                                ims, ime, jms, jme, kms, kme,         &
1216                                ips, ipe, jps, jpe, kps, kpe,         &
1217                                grid%i_start(ij), grid%i_end(ij),     &
1218                                grid%j_start(ij), grid%j_end(ij),     &
1219                                k_start    , k_end                    )
1221        CALL set_physical_bc3d( grid%t_1, 'p', config_flags,          &
1222                                ids, ide, jds, jde, kds, kde,         &
1223                                ims, ime, jms, jme, kms, kme,         &
1224                                ips, ipe, jps, jpe, kps, kpe,         &
1225                                grid%i_start(ij), grid%i_end(ij),     &
1226                                grid%j_start(ij), grid%j_end(ij),     &
1227                                k_start    , k_end                    )
1229        CALL set_physical_bc3d( grid%t_save, 't', config_flags,       &
1230                                ids, ide, jds, jde, kds, kde,         &
1231                                ims, ime, jms, jme, kms, kme,         &
1232                                ips, ipe, jps, jpe, kps, kpe,         &
1233                                grid%i_start(ij), grid%i_end(ij),     &
1234                                grid%j_start(ij), grid%j_end(ij),     &
1235                                k_start    , k_end                    )
1237        CALL set_physical_bc2d( grid%mu_1, 't', config_flags,         &
1238                                ids, ide, jds, jde,                   &
1239                                ims, ime, jms, jme,                   &
1240                                ips, ipe, jps, jpe,                   &
1241                                grid%i_start(ij), grid%i_end(ij),     &
1242                                grid%j_start(ij), grid%j_end(ij)      )
1244        CALL set_physical_bc2d( grid%mu_2, 't', config_flags,         &
1245                                ids, ide, jds, jde,                   &
1246                                ims, ime, jms, jme,                   &
1247                                ips, ipe, jps, jpe,                   &
1248                                grid%i_start(ij), grid%i_end(ij),     &
1249                                grid%j_start(ij), grid%j_end(ij)      )
1251        CALL set_physical_bc2d( grid%mudf, 't', config_flags,         &
1252                                ids, ide, jds, jde,                   &
1253                                ims, ime, jms, jme,                   &
1254                                ips, ipe, jps, jpe,                   &
1255                                grid%i_start(ij), grid%i_end(ij),     &
1256                                grid%j_start(ij), grid%j_end(ij)      )
1258      END DO
1259      !$OMP END PARALLEL DO
1260 BENCH_END(set_phys_bc2_tim)
1261      small_steps : DO iteration = 1 , number_of_small_timesteps
1263        ! Boundary condition time (or communication time).  
1264 #ifdef DM_PARALLEL
1265 #      include "PERIOD_BDY_EM_B.inc"
1266 #endif
1268        !$OMP PARALLEL DO   &
1269        !$OMP PRIVATE ( ij )
1271        DO ij = 1 , grid%num_tiles
1273 BENCH_START(advance_uv_tim)
1274          CALL advance_uv ( grid%u_2, grid%ru_tend, grid%v_2, grid%rv_tend,        &
1275                            grid%p, grid%pb,                                       &
1276                            grid%ph_2, grid%php, grid%alt,  grid%al,               &
1277                            grid%mu_2, grid%muu, cqu, grid%muv, cqv, grid%mudf,    &
1278                            grid%c1h, grid%c2h, grid%c1f, grid%c2f,                &
1279                            grid%c3h, grid%c4h, grid%c3f, grid%c4f,                &
1280                            grid%msfux, grid%msfuy, grid%msfvx,                    &
1281                            grid%msfvx_inv, grid%msfvy,                            &
1282                            grid%rdx, grid%rdy, dts_rk,                            &
1283                            grid%cf1, grid%cf2, grid%cf3, grid%fnm, grid%fnp,      &
1284                            grid%emdiv,                                            &
1285                            grid%rdnw, config_flags,grid%spec_zone,                &
1286                            config_flags%non_hydrostatic, config_flags%top_lid,    &
1287                            ids, ide, jds, jde, kds, kde,                          &
1288                            ims, ime, jms, jme, kms, kme,                          &
1289                            grid%i_start(ij), grid%i_end(ij),                      &
1290                            grid%j_start(ij), grid%j_end(ij),                      &
1291                            k_start    , k_end                                     )
1292 BENCH_END(advance_uv_tim)
1294        END DO
1295        !$OMP END PARALLEL DO
1297 !-----------------------------------------------------------
1298 !  acoustic integration polar filter for smallstep u, v
1299 !-----------------------------------------------------------
1301        IF (config_flags%polar) THEN
1303          CALL pxft ( grid=grid                                              &
1304                ,lineno=__LINE__                                             &
1305                ,flag_uv            = 1                                      &
1306                ,flag_rurv          = 0                                      &
1307                ,flag_wph           = 0                                      &
1308                ,flag_ww            = 0                                      &
1309                ,flag_t             = 0                                      &
1310                ,flag_mu            = 0                                      &
1311                ,flag_mut           = 0                                      &
1312                ,flag_moist         = 0                                      &
1313                ,flag_chem          = 0                                      &
1314                ,flag_tracer        = 0                                      &
1315                ,flag_scalar        = 0                                      &
1316                ,actual_distance_average  = .FALSE.                          &
1317                ,pos_def            = .FALSE.                                &
1318                ,swap_pole_with_next_j = .FALSE.                             &
1319                ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
1320                ,fft_filter_lat = config_flags%fft_filter_lat                &
1321                ,dclat = dclat                                               &
1322                ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
1323                ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
1324                ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
1325                ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
1326                ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
1328        END IF
1330 !-----------------------------------------------------------
1331 !  end acoustic integration polar filter for smallstep u, v
1332 !-----------------------------------------------------------
1334        !$OMP PARALLEL DO   &
1335        !$OMP PRIVATE ( ij )
1336        DO ij = 1 , grid%num_tiles
1338 BENCH_START(spec_bdy_uv_tim)
1339          IF( config_flags%specified .or. config_flags%nested ) THEN
1340            CALL spec_bdyupdate(grid%u_2, grid%ru_tend, dts_rk,      &
1341                                'u'         , config_flags, &
1342                                 grid%spec_zone,                  &
1343                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
1344                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
1345                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
1346                                 grid%i_start(ij), grid%i_end(ij),         &
1347                                 grid%j_start(ij), grid%j_end(ij),         &
1348                                 k_start    , k_end             )
1350            CALL spec_bdyupdate(grid%v_2, grid%rv_tend, dts_rk,      &
1351                                 'v'         , config_flags, &
1352                                 grid%spec_zone,                  &
1353                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
1354                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
1355                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
1356                                 grid%i_start(ij), grid%i_end(ij),         &
1357                                 grid%j_start(ij), grid%j_end(ij),         &
1358                                 k_start    , k_end             )
1360          ENDIF
1361 BENCH_END(spec_bdy_uv_tim)
1363        END DO
1364        !$OMP END PARALLEL DO
1366 #ifdef DM_PARALLEL
1368 !  Stencils for patch communications  (WCS, 29 June 2001)
1370 !         *                     *
1371 !       * + *      * + *        +
1372 !         *                     *
1374 !  u_2               x
1375 !  v_2                          x
1377 #     include "HALO_EM_C.inc"
1378 #endif
1380        !$OMP PARALLEL DO   &
1381        !$OMP PRIVATE ( ij )
1382        DO ij = 1 , grid%num_tiles
1384         !  advance the mass in the column, theta, and calculate ww
1386 BENCH_START(advance_mu_t_tim)
1387          CALL advance_mu_t( grid%ww, ww1, grid%u_2, grid%u_save, grid%v_2, grid%v_save, &
1388                           grid%mu_2, grid%mut, muave, grid%muts, grid%muu, grid%muv,    &
1389                           grid%mudf,                                                    &
1390                           grid%c1h, grid%c2h, grid%c1f, grid%c2f,                       &
1391                           grid%c3h, grid%c4h, grid%c3f, grid%c4f,                       &
1392                           grid%ru_m, grid%rv_m, grid%ww_m,                              &
1393                           grid%t_2, grid%t_save, t_2save, t_tend,                       &
1394                           mu_tend,                                                      &
1395                           grid%rdx, grid%rdy, dts_rk, grid%epssm,                       &
1396                           grid%dnw, grid%fnm, grid%fnp, grid%rdnw,                      &
1397                           grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv,            &
1398                           grid%msfvy, grid%msftx,grid%msfty,                            &
1399                           iteration, config_flags,                                      &
1400                           ids, ide, jds, jde, kds, kde,      &
1401                           ims, ime, jms, jme, kms, kme,      &
1402                           grid%i_start(ij), grid%i_end(ij),  &
1403                           grid%j_start(ij), grid%j_end(ij),  &
1404                           k_start    , k_end                )
1405 BENCH_END(advance_mu_t_tim)
1406        ENDDO
1407        !$OMP END PARALLEL DO
1409 !-----------------------------------------------------------
1410 !  acoustic integration polar filter for smallstep mu, t
1411 !-----------------------------------------------------------
1413        IF ( (config_flags%polar) ) THEN
1415          CALL pxft ( grid=grid                                               &
1416                 ,lineno=__LINE__                                             &
1417                 ,flag_uv            = 0                                      &
1418                 ,flag_rurv          = 0                                      &
1419                 ,flag_wph           = 0                                      &
1420                 ,flag_ww            = 0                                      &
1421                 ,flag_t             = 1                                      &
1422                 ,flag_mu            = 1                                      &
1423                 ,flag_mut           = 0                                      &
1424                 ,flag_moist         = 0                                      &
1425                 ,flag_chem          = 0                                      &
1426                 ,flag_tracer        = 0                                      &
1427                 ,flag_scalar        = 0                                      &
1428                 ,actual_distance_average  = .FALSE.                          &
1429                 ,pos_def            = .FALSE.                                &
1430                 ,swap_pole_with_next_j = .FALSE.                             &
1431                 ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
1432                 ,fft_filter_lat = config_flags%fft_filter_lat                &
1433                 ,dclat = dclat                                               &
1434                 ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
1435                 ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
1436                 ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
1437                 ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
1438                 ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
1440          grid%muts = grid%mut + grid%mu_2  ! reset muts using filtered mu_2
1442        END IF
1444 !-----------------------------------------------------------
1445 !  end acoustic integration polar filter for smallstep mu, t
1446 !-----------------------------------------------------------
1448 BENCH_START(spec_bdy_t_tim)
1450        !$OMP PARALLEL DO   &
1451        !$OMP PRIVATE ( ij )
1452        DO ij = 1 , grid%num_tiles
1454          IF( config_flags%specified .or. config_flags%nested ) THEN
1456            CALL spec_bdyupdate(grid%t_2, t_tend, dts_rk,        &
1457                                't'         , config_flags,      &
1458                                grid%spec_zone,                  &
1459                                ids,ide, jds,jde, kds,kde,       &
1460                                ims,ime, jms,jme, kms,kme,       &
1461                                ips,ipe, jps,jpe, kps,kpe,       &
1462                                grid%i_start(ij), grid%i_end(ij),&
1463                                grid%j_start(ij), grid%j_end(ij),&
1464                                k_start    , k_end              )
1466            CALL spec_bdyupdate(grid%mu_2, mu_tend, dts_rk,       &
1467                                'm'         , config_flags,      &
1468                                grid%spec_zone,                  &
1469                                ids,ide, jds,jde, 1  ,1  ,       &
1470                                ims,ime, jms,jme, 1  ,1  ,       &
1471                                ips,ipe, jps,jpe, 1  ,1  ,       &
1472                                grid%i_start(ij), grid%i_end(ij),&
1473                                grid%j_start(ij), grid%j_end(ij),&
1474                                1    , 1             )
1476            CALL spec_bdyupdate(grid%muts, mu_tend, dts_rk,      &
1477                               'm'         , config_flags, &
1478                               grid%spec_zone,                  &
1479                               ids,ide, jds,jde, 1  ,1  ,  & ! domain dims
1480                               ims,ime, jms,jme, 1  ,1  ,  & ! memory dims
1481                               ips,ipe, jps,jpe, 1  ,1  ,  & ! patch  dims
1482                               grid%i_start(ij), grid%i_end(ij),         &
1483                               grid%j_start(ij), grid%j_end(ij),         &
1484                               1    , 1             )
1485          ENDIF
1486 BENCH_END(spec_bdy_t_tim)
1488          ! small (acoustic) step for the vertical momentum,
1489          ! density and coupled potential temperature.
1492 BENCH_START(advance_w_tim)
1493          IF ( config_flags%non_hydrostatic ) THEN
1494            CALL advance_w( grid%w_2, rw_tend, grid%ww, w_save,         &
1495                            grid%u_2, grid%v_2,                         &
1496                            grid%mu_2, grid%mut, muave, grid%muts,      &
1497                            grid%c1h, grid%c2h, grid%c1f, grid%c2f,     &
1498                            grid%c3h, grid%c4h, grid%c3f, grid%c4f,     &
1499                            t_2save, grid%t_2, grid%t_save,             &
1500                            grid%ph_2, ph_save, grid%phb, ph_tend,      &
1501                            grid%ht, c2a, cqw, grid%alt, grid%alb,      &
1502                            a, alpha, gamma,                            &
1503                            grid%rdx, grid%rdy, dts_rk, t0, grid%epssm, &
1504                            grid%dnw, grid%fnm, grid%fnp, grid%rdnw,    &
1505                            grid%rdn, grid%cf1, grid%cf2, grid%cf3,     &
1506                            grid%msftx, grid%msfty,                     &
1507                            config_flags,  config_flags%top_lid,        &
1508                            ids,ide, jds,jde, kds,kde,                  &
1509                            ims,ime, jms,jme, kms,kme,                  &
1510                            grid%i_start(ij), grid%i_end(ij),           &
1511                            grid%j_start(ij), grid%j_end(ij),           &
1512                            k_start    , k_end                          )
1513          ENDIF
1514 BENCH_END(advance_w_tim)
1516        ENDDO
1517        !$OMP END PARALLEL DO
1519 !-----------------------------------------------------------
1520 !  acoustic integration polar filter for smallstep w, geopotential
1521 !-----------------------------------------------------------
1523        IF ( (config_flags%polar) .AND. (config_flags%non_hydrostatic) ) THEN
1525          CALL pxft ( grid=grid                                               &
1526                 ,lineno=__LINE__                                             &
1527                 ,flag_uv            = 0                                      &
1528                 ,flag_rurv          = 0                                      &
1529                 ,flag_wph           = 1                                      &
1530                 ,flag_ww            = 0                                      &
1531                 ,flag_t             = 0                                      &
1532                 ,flag_mu            = 0                                      &
1533                 ,flag_mut           = 0                                      &
1534                 ,flag_moist         = 0                                      &
1535                 ,flag_chem          = 0                                      &
1536                 ,flag_tracer        = 0                                      &
1537                 ,flag_scalar        = 0                                      &
1538                 ,actual_distance_average  = .FALSE.                          &
1539                 ,pos_def            = .FALSE.                                &
1540                 ,swap_pole_with_next_j = .FALSE.                             &
1541                 ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
1542                 ,fft_filter_lat = config_flags%fft_filter_lat                &
1543                 ,dclat = dclat                                               &
1544                 ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
1545                 ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
1546                 ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
1547                 ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
1548                 ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
1550        END IF
1552 !-----------------------------------------------------------
1553 !  end acoustic integration polar filter for smallstep w, geopotential
1554 !-----------------------------------------------------------
1556        !$OMP PARALLEL DO   &
1557        !$OMP PRIVATE ( ij )
1558        DO ij = 1 , grid%num_tiles
1560 BENCH_START(sumflux_tim)
1561          CALL sumflux ( grid%u_2, grid%v_2, grid%ww,          &
1562                         grid%u_save, grid%v_save, ww1,        &
1563                         grid%muu, grid%muv,                   &
1564                         grid%c1h, grid%c2h, grid%c1f, grid%c2f, &
1565                         grid%c3h, grid%c4h, grid%c3f, grid%c4f, &
1566                         grid%ru_m, grid%rv_m, grid%ww_m, grid%epssm,  &
1567                         grid%msfux, grid% msfuy, grid%msfvx,  &
1568                         grid%msfvx_inv, grid%msfvy,           &
1569                         iteration, number_of_small_timesteps, &
1570                         ids, ide, jds, jde, kds, kde,         &
1571                         ims, ime, jms, jme, kms, kme,         &
1572                         grid%i_start(ij), grid%i_end(ij),     &
1573                         grid%j_start(ij), grid%j_end(ij),     &
1574                         k_start    , k_end                   )
1575 BENCH_END(sumflux_tim)
1577          IF( config_flags%specified .or. config_flags%nested ) THEN
1579 BENCH_START(spec_bdynhyd_tim)
1580            IF (config_flags%non_hydrostatic)  THEN
1581              CALL spec_bdyupdate_ph( ph_save, grid%ph_2, ph_tend,     &
1582                                      mu_tend, grid%muts,              &
1583                                      grid%c1f, grid%c2f, dts_rk,      &
1584                                      'h'         , config_flags,      &
1585                                      grid%spec_zone,                  &
1586                                      ids,ide, jds,jde, kds,kde,       &
1587                                      ims,ime, jms,jme, kms,kme,       &
1588                                      ips,ipe, jps,jpe, kps,kpe,       &
1589                                      grid%i_start(ij), grid%i_end(ij),&
1590                                      grid%j_start(ij), grid%j_end(ij),&
1591                                      k_start    , k_end               )
1592              IF( config_flags%specified ) THEN
1593                CALL zero_grad_bdy ( grid%w_2,                         &
1594                                     'w'         , config_flags,       &
1595                                     grid%spec_zone,                   &
1596                                     ids,ide, jds,jde, kds,kde,        &
1597                                     ims,ime, jms,jme, kms,kme,        &
1598                                     ips,ipe, jps,jpe, kps,kpe,        &
1599                                     grid%i_start(ij), grid%i_end(ij), &
1600                                     grid%j_start(ij), grid%j_end(ij), &
1601                                     k_start    , k_end                )
1602              ELSE
1603                CALL spec_bdyupdate ( grid%w_2, rw_tend, dts_rk,       &
1604                                      'h'         , config_flags,      &
1605                                      grid%spec_zone,                  &
1606                                      ids,ide, jds,jde, kds,kde,       &
1607                                      ims,ime, jms,jme, kms,kme,       &
1608                                      ips,ipe, jps,jpe, kps,kpe,       &
1609                                      grid%i_start(ij), grid%i_end(ij),&
1610                                      grid%j_start(ij), grid%j_end(ij),&
1611                                      k_start    , k_end               )
1612              ENDIF
1613            ENDIF
1614 BENCH_END(spec_bdynhyd_tim)
1615          ENDIF
1617 BENCH_START(cald_p_rho_tim)
1618          CALL calc_p_rho( grid%al, grid%p, grid%ph_2,                 &
1619                           grid%alt, grid%t_2, grid%t_save, c2a, pm1,  &
1620                           grid%mu_2, grid%muts,                       &
1621                           grid%c1h, grid%c2h, grid%c1f, grid%c2f,     &
1622                           grid%c3h, grid%c4h, grid%c3f, grid%c4f,     &
1623                           grid%znu, t0,                               &
1624                           grid%rdnw, grid%dnw, grid%smdiv,            &
1625                           config_flags%non_hydrostatic, iteration,    &
1626                           ids, ide, jds, jde, kds, kde,     &
1627                           ims, ime, jms, jme, kms, kme,     &
1628                           grid%i_start(ij), grid%i_end(ij), &
1629                           grid%j_start(ij), grid%j_end(ij), &
1630                           k_start    , k_end               )
1631 BENCH_END(cald_p_rho_tim)
1633        ENDDO
1634        !$OMP END PARALLEL DO
1636 #ifdef DM_PARALLEL
1638 !  Stencils for patch communications  (WCS, 29 June 2001)
1640 !         *                     *
1641 !       * + *      * + *        +
1642 !         *                     *
1644 !  ph_2   x
1645 !  al     x
1646 !  p      x
1648 !  2D variables (x,y)
1650 !  mu_2   x
1651 !  muts   x
1652 !  mudf   x
1654 #      include "HALO_EM_C2.inc"
1655 #      include "PERIOD_BDY_EM_B3.inc"
1656 #endif
1658 BENCH_START(phys_bc_tim)
1659        !$OMP PARALLEL DO   &
1660        !$OMP PRIVATE ( ij )
1661        DO ij = 1 , grid%num_tiles
1663        ! boundary condition set for next small timestep
1665          CALL set_physical_bc3d( grid%ph_2, 'w', config_flags,          &
1666                                  ids, ide, jds, jde, kds, kde,     &
1667                                  ims, ime, jms, jme, kms, kme,     &
1668                                  ips, ipe, jps, jpe, kps, kpe,     &
1669                                  grid%i_start(ij), grid%i_end(ij), &
1670                                  grid%j_start(ij), grid%j_end(ij), &
1671                                  k_start    , k_end               )
1673          CALL set_physical_bc3d( grid%al, 'p', config_flags,            &
1674                                  ids, ide, jds, jde, kds, kde,     &
1675                                  ims, ime, jms, jme, kms, kme,     &
1676                                  ips, ipe, jps, jpe, kps, kpe,     &
1677                                  grid%i_start(ij), grid%i_end(ij), &
1678                                  grid%j_start(ij), grid%j_end(ij), &
1679                                  k_start    , k_end               )
1681          CALL set_physical_bc3d( grid%p, 'p', config_flags,             &
1682                                  ids, ide, jds, jde, kds, kde,     &
1683                                  ims, ime, jms, jme, kms, kme,     &
1684                                  ips, ipe, jps, jpe, kps, kpe,     &
1685                                  grid%i_start(ij), grid%i_end(ij), &
1686                                  grid%j_start(ij), grid%j_end(ij), &
1687                                  k_start    , k_end               )
1689          CALL set_physical_bc2d( grid%muts, 't', config_flags,          &
1690                                  ids, ide, jds, jde,               &
1691                                  ims, ime, jms, jme,               &
1692                                  ips, ipe, jps, jpe,               &
1693                                  grid%i_start(ij), grid%i_end(ij), &
1694                                  grid%j_start(ij), grid%j_end(ij) )
1696          CALL set_physical_bc2d( grid%mu_2, 't', config_flags,          &
1697                                  ids, ide, jds, jde,               &
1698                                  ims, ime, jms, jme,               &
1699                                  ips, ipe, jps, jpe,               &
1700                                  grid%i_start(ij), grid%i_end(ij), &
1701                                  grid%j_start(ij), grid%j_end(ij) )
1703          CALL set_physical_bc2d( grid%mudf, 't', config_flags,          &
1704                                  ids, ide, jds, jde,               &
1705                                  ims, ime, jms, jme,               &
1706                                  ips, ipe, jps, jpe,               &
1707                                  grid%i_start(ij), grid%i_end(ij), &
1708                                  grid%j_start(ij), grid%j_end(ij) )
1710        END DO
1711        !$OMP END PARALLEL DO
1712 BENCH_END(phys_bc_tim)
1714      END DO small_steps
1716      !$OMP PARALLEL DO   &
1717      !$OMP PRIVATE ( ij )
1718      DO ij = 1 , grid%num_tiles
1720        CALL wrf_debug ( 200 , ' call rk_small_finish' )
1722       ! change time-perturbation variables back to 
1723       ! full perturbation variables.
1724       ! first get updated mu at u and v points
1726 BENCH_START(calc_mu_uv_tim)
1727        CALL calc_mu_uv_1 ( config_flags,                     &
1728                            grid%muts, grid%muus, grid%muvs,  &
1729                            ids, ide, jds, jde, kds, kde,     &
1730                            ims, ime, jms, jme, kms, kme,     &
1731                            grid%i_start(ij), grid%i_end(ij), &
1732                            grid%j_start(ij), grid%j_end(ij), &
1733                            k_start    , k_end               )
1734 BENCH_END(calc_mu_uv_tim)
1735 BENCH_START(small_step_finish_tim)
1736        CALL small_step_finish( grid%u_2, grid%u_1, grid%v_2, grid%v_1, grid%w_2, grid%w_1,     &
1737                                grid%t_2, grid%t_1, grid%ph_2, grid%ph_1, grid%ww, ww1,    &
1738                                grid%mu_2, grid%mu_1,                       &
1739                                grid%mut, grid%muts, grid%muu, grid%muus, grid%muv, grid%muvs,  & 
1740                                grid%c1h, grid%c2h, grid%c1f, grid%c2f, &
1741                                grid%c3h, grid%c4h, grid%c3f, grid%c4f, &
1742                                grid%u_save, grid%v_save, w_save,           &
1743                                grid%t_save, ph_save, mu_save,         &
1744                                grid%msfux,grid%msfuy, grid%msfvx,grid%msfvy, grid%msftx,grid%msfty, &
1745                                grid%h_diabatic,                       &
1746                                number_of_small_timesteps,dts_rk, &
1747                                rk_step, rk_order,                &
1748                                ids, ide, jds, jde, kds, kde,     &
1749                                ims, ime, jms, jme, kms, kme,     &
1750                                grid%i_start(ij), grid%i_end(ij), &
1751                                grid%j_start(ij), grid%j_end(ij), &
1752                                k_start    , k_end               )
1753 !  call  to set ru_m, rv_m and ww_m b.c's for PD advection
1755        IF (rk_step == rk_order) THEN
1757          CALL set_physical_bc3d( grid%ru_m, 'u', config_flags,   &
1758                                  ids, ide, jds, jde, kds, kde,      &
1759                                  ims, ime, jms, jme, kms, kme,      &
1760                                  ips, ipe, jps, jpe, kps, kpe,      &
1761                                  grid%i_start(ij), grid%i_end(ij),  &
1762                                  grid%j_start(ij), grid%j_end(ij),  &
1763                                  k_start    , k_end                )
1765          CALL set_physical_bc3d( grid%rv_m, 'v', config_flags,   &
1766                                  ids, ide, jds, jde, kds, kde,      &
1767                                  ims, ime, jms, jme, kms, kme,      &
1768                                  ips, ipe, jps, jpe, kps, kpe,      &
1769                                  grid%i_start(ij), grid%i_end(ij),  &
1770                                  grid%j_start(ij), grid%j_end(ij),  &
1771                                  k_start    , k_end                )
1773          CALL set_physical_bc3d( grid%ww_m, 'w', config_flags,   &
1774                                  ids, ide, jds, jde, kds, kde,      &
1775                                  ims, ime, jms, jme, kms, kme,      &
1776                                  ips, ipe, jps, jpe, kps, kpe,      &
1777                                  grid%i_start(ij), grid%i_end(ij),  &
1778                                  grid%j_start(ij), grid%j_end(ij),  &
1779                                  k_start    , k_end                )
1781          CALL set_physical_bc2d( grid%mut, 't', config_flags,   &
1782                                  ids, ide, jds, jde,               &
1783                                  ims, ime, jms, jme,                &
1784                                  ips, ipe, jps, jpe,                &
1785                                  grid%i_start(ij), grid%i_end(ij),  &
1786                                  grid%j_start(ij), grid%j_end(ij) )
1788          CALL set_physical_bc2d( grid%muts, 't', config_flags,   &
1789                                  ids, ide, jds, jde,               &
1790                                  ims, ime, jms, jme,                &
1791                                  ips, ipe, jps, jpe,                &
1792                                  grid%i_start(ij), grid%i_end(ij),  &
1793                                  grid%j_start(ij), grid%j_end(ij) )
1795        END IF
1797 BENCH_END(small_step_finish_tim)
1799      END DO
1800      !$OMP END PARALLEL DO
1802 !-----------------------------------------------------------
1803 !  polar filter for full dynamics variables and time-averaged mass fluxes 
1804 !-----------------------------------------------------------
1806      IF (config_flags%polar) THEN
1808        CALL pxft ( grid=grid                                                   &
1809                   ,lineno=__LINE__                                             &
1810                   ,flag_uv            = 1                                      &
1811                   ,flag_rurv          = 1                                      &
1812                   ,flag_wph           = 1                                      &
1813                   ,flag_ww            = 1                                      &
1814                   ,flag_t             = 1                                      &
1815                   ,flag_mu            = 1                                      &
1816                   ,flag_mut           = 1                                      &
1817                   ,flag_moist         = 0                                      &
1818                   ,flag_chem          = 0                                      &
1819                   ,flag_tracer        = 0                                      &
1820                   ,flag_scalar        = 0                                      &
1821                   ,actual_distance_average  = .FALSE.                          &
1822                   ,pos_def            = .FALSE.                                &
1823                   ,swap_pole_with_next_j = .FALSE.                             &
1824                   ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
1825                   ,fft_filter_lat = config_flags%fft_filter_lat                &
1826                   ,dclat = dclat                                               &
1827                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
1828                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
1829                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
1830                   ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
1831                   ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
1833      END IF
1835 !-----------------------------------------------------------
1836 !  end polar filter for full dynamics variables and time-averaged mass fluxes 
1837 !-----------------------------------------------------------
1839 !-----------------------------------------------------------------------
1840 !  add in physics tendency first if positive definite advection is used.
1841 !  pd advection applies advective flux limiter on last runge-kutta step
1842 !-----------------------------------------------------------------------
1843 ! first moisture
1845      IF ((config_flags%moist_adv_opt /= ORIGINAL .and. config_flags%moist_adv_opt /= WENO_SCALAR) .and. &
1846          (rk_step == rk_order)) THEN
1848        !$OMP PARALLEL DO   &
1849        !$OMP PRIVATE ( ij )
1850        DO ij = 1 , grid%num_tiles
1851          CALL wrf_debug ( 200 , ' call rk_update_scalar_pd' )
1852          DO im = PARAM_FIRST_SCALAR, num_3d_m
1853            CALL rk_update_scalar_pd( im, im,                                   &
1854                                      moist_old(ims,kms,jms,im),                &
1855                                      moist_tend(ims,kms,jms,im),               &
1856                                      grid%c1h, grid%c2h,                       &
1857                                      grid%mu_1, grid%mu_1, grid%mub,  &
1858                                      rk_step, dt_rk, grid%spec_zone,           &
1859                                      config_flags,                             &
1860                                      ids, ide, jds, jde, kds, kde,             &
1861                                      ims, ime, jms, jme, kms, kme,             &
1862                                      grid%i_start(ij), grid%i_end(ij),         &
1863                                      grid%j_start(ij), grid%j_end(ij),         &
1864                                      k_start    , k_end                       )
1865          ENDDO
1866        END DO
1867        !$OMP END PARALLEL DO
1869 !---------------------- positive definite bc call
1870 #ifdef DM_PARALLEL
1871        IF (config_flags%moist_adv_opt /= ORIGINAL .and. config_flags%moist_adv_opt /= WENO_SCALAR) THEN
1872          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
1873 #     include "HALO_EM_MOIST_OLD_E_5.inc"
1874          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
1875 #     include "HALO_EM_MOIST_OLD_E_7.inc"
1876          ELSE
1877            WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
1878            CALL wrf_error_fatal(TRIM(wrf_err_message))
1879          ENDIF
1880        ENDIF
1881 #endif
1883 #ifdef DM_PARALLEL
1884 #  include "PERIOD_BDY_EM_MOIST_OLD.inc"
1885 #endif
1887        !$OMP PARALLEL DO   &
1888        !$OMP PRIVATE ( ij )
1889        DO ij = 1 , grid%num_tiles
1890          IF (num_3d_m >= PARAM_FIRST_SCALAR) THEN
1891            DO im = PARAM_FIRST_SCALAR , num_3d_m
1892              CALL set_physical_bc3d( moist_old(ims,kms,jms,im), 'p', config_flags,   &
1893                                      ids, ide, jds, jde, kds, kde,                  &
1894                                      ims, ime, jms, jme, kms, kme,                  &
1895                                      ips, ipe, jps, jpe, kps, kpe,                  &
1896                                      grid%i_start(ij), grid%i_end(ij),              &
1897                                      grid%j_start(ij), grid%j_end(ij),              &
1898                                      k_start    , k_end                            )
1899            END DO
1900          ENDIF
1901        END DO
1902        !$OMP END PARALLEL DO
1904      END IF  ! end if for moist_adv_opt
1906 ! scalars
1908      IF ((config_flags%scalar_adv_opt /= ORIGINAL .and. config_flags%scalar_adv_opt /= WENO_SCALAR) .and. &
1909          (rk_step == rk_order)) THEN
1911        !$OMP PARALLEL DO   &
1912        !$OMP PRIVATE ( ij )
1913        DO ij = 1 , grid%num_tiles
1914          CALL wrf_debug ( 200 , ' call rk_update_scalar_pd' )
1915          DO im = PARAM_FIRST_SCALAR, num_3d_s
1916            CALL rk_update_scalar_pd( im, im,                                  &
1917                                      scalar_old(ims,kms,jms,im),              &
1918                                      scalar_tend(ims,kms,jms,im),             &
1919                                      grid%c1h, grid%c2h,                      &
1920                                      grid%mu_1, grid%mu_1, grid%mub, &
1921                                      rk_step, dt_rk, grid%spec_zone,          &
1922                                      config_flags,                            &
1923                                      ids, ide, jds, jde, kds, kde,            &
1924                                      ims, ime, jms, jme, kms, kme,            &
1925                                      grid%i_start(ij), grid%i_end(ij),        &
1926                                      grid%j_start(ij), grid%j_end(ij),        &
1927                                      k_start    , k_end                      )
1928          ENDDO
1929        ENDDO
1930        !$OMP END PARALLEL DO
1932 !---------------------- positive definite bc call
1933 #ifdef DM_PARALLEL
1934        IF (config_flags%scalar_adv_opt /= ORIGINAL .and. config_flags%scalar_adv_opt /= WENO_SCALAR) THEN
1935 #ifndef RSL
1936          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
1937 #     include "HALO_EM_SCALAR_OLD_E_5.inc"
1938          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
1939 #     include "HALO_EM_SCALAR_OLD_E_7.inc"
1940          ELSE
1941            WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
1942            CALL wrf_error_fatal(TRIM(wrf_err_message))
1943          ENDIF
1944 #else
1945          WRITE(wrf_err_message,*)'cannot use pd scheme with RSL - use RSL-LITE'
1946          CALL wrf_error_fatal(TRIM(wrf_err_message))
1947 #endif   
1948   endif
1949 #endif
1951 #ifdef DM_PARALLEL
1952 #  include "PERIOD_BDY_EM_SCALAR_OLD.inc"
1953 #endif
1954          !$OMP PARALLEL DO   &
1955          !$OMP PRIVATE ( ij )
1957          DO ij = 1 , grid%num_tiles
1958            IF (num_3d_s >= PARAM_FIRST_SCALAR) THEN
1959              DO im = PARAM_FIRST_SCALAR , num_3d_s
1960                CALL set_physical_bc3d(  scalar_old(ims,kms,jms,im), 'p', config_flags, &
1961                                         ids, ide, jds, jde, kds, kde,                    &
1962                                         ims, ime, jms, jme, kms, kme,                    &
1963                                         ips, ipe, jps, jpe, kps, kpe,                    &
1964                                         grid%i_start(ij), grid%i_end(ij),                &
1965                                         grid%j_start(ij), grid%j_end(ij),                &
1966                                         k_start    , k_end                              )
1967              END DO
1968            ENDIF
1969          END DO
1970          !$OMP END PARALLEL DO
1972        END IF  ! end if for scalar_adv_opt
1974 ! chem
1976        IF ((config_flags%chem_adv_opt /= ORIGINAL .and. config_flags%chem_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order)) THEN
1978          !$OMP PARALLEL DO   &
1979          !$OMP PRIVATE ( ij )
1980          DO ij = 1 , grid%num_tiles
1981            CALL wrf_debug ( 200 , ' call rk_update_scalar_pd' )
1982            DO im = PARAM_FIRST_SCALAR, num_3d_c
1983              CALL rk_update_scalar_pd( im, im,                                  &
1984                                        chem_old(ims,kms,jms,im),                &
1985                                        chem_tend(ims,kms,jms,im),               &
1986                                        grid%c1h, grid%c2h,                      &
1987                                        grid%mu_1, grid%mu_1, grid%mub, &
1988                                        rk_step, dt_rk, grid%spec_zone,          &
1989                                        config_flags,                            &
1990                                        ids, ide, jds, jde, kds, kde,            &
1991                                        ims, ime, jms, jme, kms, kme,            &
1992                                        grid%i_start(ij), grid%i_end(ij),        &
1993                                        grid%j_start(ij), grid%j_end(ij),        &
1994                                        k_start    , k_end                      )
1995            ENDDO
1996          END DO
1997          !$OMP END PARALLEL DO
1999 !---------------------- positive definite bc call
2000 #ifdef DM_PARALLEL
2001          IF (config_flags%chem_adv_opt /= ORIGINAL .and. config_flags%chem_adv_opt /= WENO_SCALAR) THEN
2002            IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
2003 #     include "HALO_EM_CHEM_OLD_E_5.inc"
2004            ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
2005 #     include "HALO_EM_CHEM_OLD_E_7.inc"
2006            ELSE
2007              WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
2008              CALL wrf_error_fatal(TRIM(wrf_err_message))
2009            ENDIF
2010          ENDIF
2011 #endif
2013 #ifdef DM_PARALLEL
2014 #  include "PERIOD_BDY_EM_CHEM_OLD.inc"
2015 #endif
2017          !$OMP PARALLEL DO   &
2018          !$OMP PRIVATE ( ij )
2019          DO ij = 1 , grid%num_tiles
2020            IF (num_3d_c >= PARAM_FIRST_SCALAR) THEN
2021              DO im = PARAM_FIRST_SCALAR , num_3d_c
2022                CALL set_physical_bc3d(  chem_old(ims,kms,jms,im), 'p', config_flags,     &
2023                                         ids, ide, jds, jde, kds, kde,                    &
2024                                         ims, ime, jms, jme, kms, kme,                    &
2025                                         ips, ipe, jps, jpe, kps, kpe,                    &
2026                                         grid%i_start(ij), grid%i_end(ij),                &
2027                                         grid%j_start(ij), grid%j_end(ij),                &
2028                                         k_start    , k_end                              )
2029              END DO 
2030            ENDIF
2031          END DO
2032          !$OMP END PARALLEL DO
2034        ENDIF  ! end if for chem_adv_opt
2036 ! tracer
2038        IF ((config_flags%tracer_adv_opt /= ORIGINAL .and. config_flags%tracer_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order)) THEN
2040          !$OMP PARALLEL DO   &
2041          !$OMP PRIVATE ( ij )
2042          DO ij = 1 , grid%num_tiles
2043            CALL wrf_debug ( 200 , ' call rk_update_scalar_pd' )
2044            DO im = PARAM_FIRST_SCALAR, num_tracer
2045              CALL rk_update_scalar_pd( im, im,                                  &
2046                                        tracer_old(ims,kms,jms,im),                &
2047                                        tracer_tend(ims,kms,jms,im),               &
2048                                        grid%c1h, grid%c2h,                      &
2049                                        grid%mu_1, grid%mu_1, grid%mub, &
2050                                        rk_step, dt_rk, grid%spec_zone,          &
2051                                        config_flags,                            &
2052                                        ids, ide, jds, jde, kds, kde,            &
2053                                        ims, ime, jms, jme, kms, kme,            &
2054                                        grid%i_start(ij), grid%i_end(ij),        &
2055                                        grid%j_start(ij), grid%j_end(ij),        &
2056                                        k_start    , k_end                      )
2057            ENDDO
2058          END DO
2059          !$OMP END PARALLEL DO
2061 !---------------------- positive definite bc call
2062 #ifdef DM_PARALLEL
2063          IF (config_flags%tracer_adv_opt /= ORIGINAL .and. config_flags%tracer_adv_opt /= WENO_SCALAR) THEN
2064            IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
2065 #     include "HALO_EM_TRACER_OLD_E_5.inc"
2066            ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
2067 #     include "HALO_EM_TRACER_OLD_E_7.inc"
2068            ELSE
2069              WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
2070              CALL wrf_error_fatal(TRIM(wrf_err_message))
2071            ENDIF
2072          ENDIF
2073 #endif
2075 #ifdef DM_PARALLEL
2076 #  include "PERIOD_BDY_EM_TRACER_OLD.inc"
2077 #endif
2079          !$OMP PARALLEL DO   &
2080          !$OMP PRIVATE ( ij )
2081          DO ij = 1 , grid%num_tiles
2082            IF (num_tracer >= PARAM_FIRST_SCALAR) THEN
2083              DO im = PARAM_FIRST_SCALAR , num_tracer
2084                CALL set_physical_bc3d(  tracer_old(ims,kms,jms,im), 'p', config_flags,   &
2085                                         ids, ide, jds, jde, kds, kde,                    &
2086                                         ims, ime, jms, jme, kms, kme,                    &
2087                                         ips, ipe, jps, jpe, kps, kpe,                    &
2088                                         grid%i_start(ij), grid%i_end(ij),                &
2089                                         grid%j_start(ij), grid%j_end(ij),                &
2090                                         k_start    , k_end                              )
2091              END DO 
2092            ENDIF
2093          END DO
2094          !$OMP END PARALLEL DO
2096        ENDIF  ! end if for tracer_adv_opt
2098 ! tke
2100        IF ((config_flags%tke_adv_opt /= ORIGINAL .and. config_flags%tke_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order) &
2101            .and. (config_flags%km_opt .eq. 2)                ) THEN
2103          !$OMP PARALLEL DO   &
2104          !$OMP PRIVATE ( ij )
2105          DO ij = 1 , grid%num_tiles
2106            CALL wrf_debug ( 200 , ' call rk_update_scalar_pd' )
2107            CALL rk_update_scalar_pd( 1, 1,                                    &
2108                                      grid%tke_1,                              &
2109                                      tke_tend(ims,kms,jms),                   &
2110                                      grid%c1h, grid%c2h,                      &
2111                                      grid%mu_1, grid%mu_1, grid%mub,          &
2112                                      rk_step, dt_rk, grid%spec_zone,          &
2113                                      config_flags,                            &
2114                                      ids, ide, jds, jde, kds, kde,            &
2115                                      ims, ime, jms, jme, kms, kme,            &
2116                                      grid%i_start(ij), grid%i_end(ij),        &
2117                                      grid%j_start(ij), grid%j_end(ij),        &
2118                                      k_start    , k_end                       )
2119          ENDDO
2120          !$OMP END PARALLEL DO
2122 !---------------------- positive definite bc call
2123 #ifdef DM_PARALLEL
2124          IF (config_flags%tke_adv_opt /= ORIGINAL .and. config_flags%tke_adv_opt /= WENO_SCALAR) THEN
2125            IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
2126 #     include "HALO_EM_TKE_OLD_E_5.inc"
2127            ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
2128 #     include "HALO_EM_TKE_OLD_E_7.inc"
2129            ELSE
2130              WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
2131              CALL wrf_error_fatal(TRIM(wrf_err_message))
2132            ENDIF
2133          ENDIF
2134 #endif
2136 #ifdef DM_PARALLEL
2137 #  include "PERIOD_BDY_EM_TKE_OLD.inc"
2138 #endif
2140          !$OMP PARALLEL DO   &
2141          !$OMP PRIVATE ( ij )
2142          DO ij = 1 , grid%num_tiles
2143            CALL set_physical_bc3d(  grid%tke_1, 'p', config_flags,  &
2144                                     ids, ide, jds, jde, kds, kde,      &
2145                                     ims, ime, jms, jme, kms, kme,      &
2146                                     ips, ipe, jps, jpe, kps, kpe,      &
2147                                     grid%i_start(ij), grid%i_end(ij),  &
2148                                     grid%j_start(ij), grid%j_end(ij),  &
2149                                     k_start    , k_end                )
2150          END DO
2151          !$OMP END PARALLEL DO
2153 !---  end of positive definite physics tendency update
2155        END IF  ! end if for tke_adv_opt
2157 #ifdef DM_PARALLEL
2159 !  Stencils for patch communications  (WCS, 29 June 2001)
2161 !          * * * * *            
2162 !          * * * * *            
2163 !          * * + * *            
2164 !          * * * * *            
2165 !          * * * * *            
2167 ! ru_m         x
2168 ! rv_m         x
2169 ! ww_m         x
2170 ! mut          x
2172 !--------------------------------------------------------------
2174 #  include "HALO_EM_D.inc"
2175 ! WCS addition 11/19/08
2176 #  include "PERIOD_EM_DA.inc"
2177 #endif
2179 !<DESCRIPTION>
2180 !<pre>
2181 ! (4) Still within the RK loop, the scalar variables are advanced.
2183 !    For the moist and chem variables, each one is advanced
2184 !    individually, using named loops "moist_variable_loop:"
2185 !    and "chem_variable_loop:".  Each RK substep begins by
2186 !    calculating the advective tendency, and, for the first RK step, 
2187 !    3D mixing (calling rk_scalar_tend) followed by an update
2188 !    of the scalar (calling rk_update_scalar).
2189 !</pre>
2190 !</DESCRIPTION>
2193        moist_scalar_advance: IF (num_3d_m >= PARAM_FIRST_SCALAR )  THEN
2195          moist_variable_loop: DO im = PARAM_FIRST_SCALAR, num_3d_m
2197 ! adv_moist_cond is set in module_physics_init based on mp_physics choice
2198 !       true except for Ferrier scheme
2200            IF (grid%adv_moist_cond .or. im==p_qv ) THEN
2202              !$OMP PARALLEL DO   &
2203              !$OMP PRIVATE ( ij, tenddec )
2204              moist_tile_loop_1: DO ij = 1 , grid%num_tiles
2206                CALL wrf_debug ( 200 , ' call rk_scalar_tend' )
2207                tenddec = .false.
2209 BENCH_START(rk_scalar_tend_tim)
2210                CALL rk_scalar_tend (  im, im, config_flags, tenddec,         & 
2211                            rk_step, dt_rk,                                   &
2212                            grid%ru_m, grid%rv_m, grid%ww_m, wwE, wwI,        &
2213                            grid%u_1, grid%v_1,                               &  
2214                            grid%muts, grid%mub, grid%mu_1,                   &
2215                            grid%c1h, grid%c2h,  grid%c1f, grid%c2f,          &
2216                            grid%alt,                                         &
2217                            moist_old(ims,kms,jms,im),                        &
2218                            moist(ims,kms,jms,im),                            &
2219                            moist_tend(ims,kms,jms,im),                       &
2220                            advect_tend,h_tendency,z_tendency,grid%rqvften,   & 
2221                            grid%qv_base, .true., grid%fnm, grid%fnp,         &
2222                            grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv,&
2223                            grid%msfvy, grid%msftx,grid%msfty,                & 
2224                            grid%rdx, grid%rdy, grid%rdn, grid%rdnw, grid%khdif, &
2225                            grid%kvdif, grid%xkhh,                            &
2226                            grid%diff_6th_opt, grid%diff_6th_factor,          &
2227                            config_flags%moist_adv_opt,                       &
2228                            grid%phb, grid%ph_2,                              &
2229                            config_flags%moist_mix2_off,                      &
2230                            config_flags%moist_mix6_off,                      &
2231                            ids, ide, jds, jde, kds, kde,     &
2232                            ims, ime, jms, jme, kms, kme,     &
2233                            grid%i_start(ij), grid%i_end(ij), &
2234                            grid%j_start(ij), grid%j_end(ij), &
2235                            k_start    , k_end               )
2237                IF( rk_step == 1 .AND. config_flags%use_q_diabatic == 1 )THEN
2238                IF( im.eq.p_qv .or. im.eq.p_qc )THEN
2239                    CALL q_diabatic_add  ( im, im,            & 
2240                            dt_rk, grid%mut,                  &
2241                            grid%c1h, grid%c2h,               &
2242                            grid%qv_diabatic,                 &
2243                            grid%qc_diabatic,                 &
2244                            moist_tend(ims,kms,jms,im),       &
2245                            ids, ide, jds, jde, kds, kde,     &
2246                            ims, ime, jms, jme, kms, kme,     &
2247                            grid%i_start(ij), grid%i_end(ij), &
2248                            grid%j_start(ij), grid%j_end(ij), &
2249                            k_start    , k_end               )
2250                ENDIF
2251                ENDIF
2253 BENCH_END(rk_scalar_tend_tim)
2255 BENCH_START(rlx_bdy_scalar_tim)
2256                IF( ( config_flags%specified .or. config_flags%nested ) .and. rk_step == 1 ) THEN 
2257                  IF ( im .EQ. P_QV .OR. config_flags%nested .OR. &
2258                     ( config_flags%specified .AND. config_flags%have_bcs_moist ) ) THEN
2259                    CALL relax_bdy_scalar ( moist_tend(ims,kms,jms,im),            & 
2260                                      moist(ims,kms,jms,im),  grid%mut,         &
2261                                      grid%c1h, grid%c2h,                       &
2262                                      moist_bxs(jms,kms,1,im),moist_bxe(jms,kms,1,im), &
2263                                      moist_bys(ims,kms,1,im),moist_bye(ims,kms,1,im), &
2264                                      moist_btxs(jms,kms,1,im),moist_btxe(jms,kms,1,im), &
2265                                      moist_btys(ims,kms,1,im),moist_btye(ims,kms,1,im), &
2266                                      config_flags%spec_bdy_width, grid%spec_zone, grid%relax_zone, &
2267                                      grid%dtbc, grid%fcx, grid%gcx,             &
2268                                      config_flags,               &
2269                                      ids,ide, jds,jde, kds,kde,  & ! domain dims
2270                                      ims,ime, jms,jme, kms,kme,  & ! memory dims
2271                                      ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2272                                      grid%i_start(ij), grid%i_end(ij),      &
2273                                      grid%j_start(ij), grid%j_end(ij),      &
2274                                      k_start, k_end                        )
2276                    CALL spec_bdy_scalar  ( moist_tend(ims,kms,jms,im),                &
2277                                      moist_bxs(jms,kms,1,im),moist_bxe(jms,kms,1,im), &
2278                                      moist_bys(ims,kms,1,im),moist_bye(ims,kms,1,im), &
2279                                      moist_btxs(jms,kms,1,im),moist_btxe(jms,kms,1,im), &
2280                                      moist_btys(ims,kms,1,im),moist_btye(ims,kms,1,im), &
2281                                      config_flags%spec_bdy_width, grid%spec_zone,                 &
2282                                      config_flags,               &
2283                                      ids,ide, jds,jde, kds,kde,  & ! domain dims
2284                                      ims,ime, jms,jme, kms,kme,  & ! memory dims
2285                                      ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2286                                      grid%i_start(ij), grid%i_end(ij),          &
2287                                      grid%j_start(ij), grid%j_end(ij),          &
2288                                      k_start, k_end                               )
2289                  ENDIF
2290                ENDIF
2291 BENCH_END(rlx_bdy_scalar_tim)
2293              ENDDO moist_tile_loop_1
2294              !$OMP END PARALLEL DO
2296              !$OMP PARALLEL DO   &
2297              !$OMP PRIVATE ( ij, tenddec )
2298              moist_tile_loop_2: DO ij = 1 , grid%num_tiles
2300                CALL wrf_debug ( 200 , ' call rk_update_scalar' )
2301                tenddec = .false.
2303 BENCH_START(update_scal_tim)
2304                CALL rk_update_scalar( scs=im, sce=im,                                  &
2305                                scalar_1=moist_old(ims,kms,jms,im),                     &
2306                                scalar_2=moist(ims,kms,jms,im),                         &
2307                                sc_tend=moist_tend(ims,kms,jms,im),                     &
2308                                advect_tend=advect_tend,                                &
2309                                h_tendency=h_tendency, z_tendency=z_tendency,           & 
2310                                msftx=grid%msftx,msfty=grid%msfty,                      &
2311                                c1=grid%c1h, c2=grid%c2h,                               &
2312                                mu_old=grid%mu_1, mu_new=grid%mu_2, mu_base=grid%mub,   &
2313                                rk_step=rk_step, dt=dt_rk, spec_zone=grid%spec_zone,    &
2314                                config_flags=config_flags, tenddec=tenddec,             & 
2315                                ids=ids, ide=ide, jds=jds, jde=jde, kds=kds, kde=kde,   &
2316                                ims=ims, ime=ime, jms=jms, jme=jme, kms=kms, kme=kme,   &
2317                                its=grid%i_start(ij), ite=grid%i_end(ij),               &
2318                                jts=grid%j_start(ij), jte=grid%j_end(ij),               &
2319                                kts=k_start    , kte=k_end                              )
2320                IF( rk_step == rk_order .AND. config_flags%use_q_diabatic == 1 )THEN
2321                IF( im.eq.p_qv .or. im.eq.p_qc )THEN
2322                    CALL q_diabatic_subtr( im, im,            & 
2323                            dt_rk,                            &
2324                            grid%qv_diabatic,                 &
2325                            grid%qc_diabatic,                 &
2326                            moist(ims,kms,jms,im),            &
2327                            ids, ide, jds, jde, kds, kde,     &
2328                            ims, ime, jms, jme, kms, kme,     &
2329                            grid%i_start(ij), grid%i_end(ij), &
2330                            grid%j_start(ij), grid%j_end(ij), &
2331                            k_start    , k_end               )
2332                ENDIF
2333                ENDIF
2334 BENCH_END(update_scal_tim)
2336 BENCH_START(flow_depbdy_tim)
2337                IF( config_flags%specified .AND. ( .NOT. config_flags%have_bcs_moist ) ) THEN
2338                  IF(im .ne. P_QV)THEN
2339                    CALL flow_dep_bdy  (  moist(ims,kms,jms,im),                 &
2340                                 grid%ru_m, grid%rv_m, config_flags,             &
2341                                 grid%spec_zone,                                 &
2342                                 ids,ide, jds,jde, kds,kde,                      &
2343                                 ims,ime, jms,jme, kms,kme,                      &
2344                                 ips,ipe, jps,jpe, kps,kpe,                      &
2345                                 grid%i_start(ij), grid%i_end(ij),               &
2346                                 grid%j_start(ij), grid%j_end(ij),               &
2347                                 k_start, k_end                               )
2348                  ENDIF
2349                ENDIF
2350 BENCH_END(flow_depbdy_tim)
2352              ENDDO moist_tile_loop_2
2353              !$OMP END PARALLEL DO
2355            ENDIF  !-- if (grid%adv_moist_cond .or. im==p_qv ) then
2357          ENDDO moist_variable_loop
2359        ENDIF moist_scalar_advance
2361 BENCH_START(tke_adv_tim)
2362        TKE_advance: IF (config_flags%km_opt .eq. 2.or.config_flags%km_opt.eq.5) then ! XZ
2363 #ifdef DM_PARALLEL
2364          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
2365 #       include "HALO_EM_TKE_ADVECT_3.inc"
2366          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
2367 #       include "HALO_EM_TKE_ADVECT_5.inc"
2368          ELSE
2369           WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
2370           CALL wrf_error_fatal(TRIM(wrf_err_message))
2371          ENDIF
2372 #endif
2373          !$OMP PARALLEL DO   &
2374          !$OMP PRIVATE ( ij, tenddec )
2375          tke_tile_loop_1: DO ij = 1 , grid%num_tiles
2377            CALL wrf_debug ( 200 , ' call rk_scalar_tend for tke' )
2378            tenddec = .false.
2379            CALL rk_scalar_tend ( 1, 1, config_flags, tenddec,                      & 
2380                             rk_step, dt_rk,                                        &
2381                             grid%ru_m, grid%rv_m, grid%ww_m, wwE, wwI,             &
2382                             grid%u_1, grid%v_1,                                    &  
2383                             grid%muts, grid%mub, grid%mu_1,                        &
2384                             grid%c1h, grid%c2h,  grid%c1f, grid%c2f,               &
2385                             grid%alt,                                              &
2386                             grid%tke_1,                                            &
2387                             grid%tke_2,                                            &
2388                             tke_tend(ims,kms,jms),                                 &
2389                             advect_tend,h_tendency,z_tendency,grid%rqvften,        & 
2390                             grid%qv_base, .false., grid%fnm, grid%fnp,             &
2391                             grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv,     &
2392                             grid%msfvy, grid%msftx,grid%msfty,                     &
2393                             grid%rdx, grid%rdy, grid%rdn, grid%rdnw, grid%khdif,   &
2394                             grid%kvdif, grid%xkhh,                                 &
2395                             grid%diff_6th_opt, grid%diff_6th_factor,               &
2396                             config_flags%tke_adv_opt,                              &
2397                             grid%phb, grid%ph_2,                                   &
2398                             config_flags%tke_mix2_off,                             &
2399                             config_flags%tke_mix6_off,                             &
2400                             ids, ide, jds, jde, kds, kde,     &
2401                             ims, ime, jms, jme, kms, kme,     &
2402                             grid%i_start(ij), grid%i_end(ij), &
2403                             grid%j_start(ij), grid%j_end(ij), &
2404                             k_start    , k_end               )
2406          ENDDO tke_tile_loop_1
2407          !$OMP END PARALLEL DO
2409          !$OMP PARALLEL DO   &
2410          !$OMP PRIVATE ( ij, tenddec )
2411          tke_tile_loop_2: DO ij = 1 , grid%num_tiles
2413            CALL wrf_debug ( 200 , ' call rk_update_scalar' )
2414            tenddec = .false.
2415            CALL rk_update_scalar( scs=1,  sce=1,                                          &
2416                                   scalar_1=grid%tke_1,                                    &
2417                                   scalar_2=grid%tke_2,                                    &
2418                                   sc_tend=tke_tend(ims,kms,jms),                          &
2419                                   advect_tend=advect_tend,                                &
2420                                   h_tendency=h_tendency, z_tendency=z_tendency,           & 
2421                                   msftx=grid%msftx,msfty=grid%msfty,                      &
2422                                   c1=grid%c1h, c2=grid%c2h,                               &
2423                                   mu_old=grid%mu_1, mu_new=grid%mu_2, mu_base=grid%mub,   &
2424                                   rk_step=rk_step, dt=dt_rk, spec_zone=grid%spec_zone,    &
2425                                   config_flags=config_flags, tenddec=tenddec,             & 
2426                                   ids=ids, ide=ide, jds=jds, jde=jde, kds=kds, kde=kde,   &
2427                                   ims=ims, ime=ime, jms=jms, jme=jme, kms=kms, kme=kme,   &
2428                                   its=grid%i_start(ij), ite=grid%i_end(ij),               &
2429                                   jts=grid%j_start(ij), jte=grid%j_end(ij),               &
2430                                   kts=k_start    , kte=k_end                              )
2432 ! bound the tke (greater than 0, less than tke_upper_bound)
2434            CALL bound_tke( grid%tke_2, grid%tke_upper_bound,    &
2435                            ids, ide, jds, jde, kds, kde,        &
2436                            ims, ime, jms, jme, kms, kme,        &
2437                            grid%i_start(ij), grid%i_end(ij),    &
2438                            grid%j_start(ij), grid%j_end(ij),    &
2439                            k_start    , k_end                  )
2441            IF( config_flags%specified .or. config_flags%nested ) THEN
2442               CALL flow_dep_bdy (  grid%tke_2,                     &
2443                                    grid%ru_m, grid%rv_m, config_flags,               &
2444                                    grid%spec_zone,                              &
2445                                    ids,ide, jds,jde, kds,kde,  & ! domain dims
2446                                    ims,ime, jms,jme, kms,kme,  & ! memory dims
2447                                    ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2448                                    grid%i_start(ij), grid%i_end(ij),       &
2449                                    grid%j_start(ij), grid%j_end(ij),       &
2450                                    k_start, k_end                               )
2451            ENDIF
2452          ENDDO tke_tile_loop_2
2453          !$OMP END PARALLEL DO
2455        ENDIF TKE_advance
2456 BENCH_END(tke_adv_tim)
2458 #if (WRF_CHEM == 1)
2459 !  next the chemical species
2460 BENCH_START(chem_adv_tim)
2461        chem_scalar_advance: IF (num_3d_c >= PARAM_FIRST_SCALAR)  THEN
2463          chem_variable_loop: DO ic = PARAM_FIRST_SCALAR, num_3d_c
2465            !$OMP PARALLEL DO   &
2466            !$OMP PRIVATE ( ij, tenddec )
2467            chem_tile_loop_1: DO ij = 1 , grid%num_tiles
2469              CALL wrf_debug ( 200 , ' call rk_scalar_tend in chem_tile_loop_1' )
2470              tenddec = (( config_flags%chemdiag == USECHEMDIAG ) .and. &
2471                         ( adv_ct_indices(ic) >= PARAM_FIRST_SCALAR ))
2472              CALL rk_scalar_tend ( ic, ic, config_flags, tenddec,                & 
2473                               rk_step, dt_rk,                                    &
2474                               grid%ru_m, grid%rv_m, grid%ww_m, wwE, wwI,         &
2475                               grid%u_1, grid%v_1,                                &  
2476                               grid%muts, grid%mub, grid%mu_1,                    &
2477                               grid%c1h, grid%c2h,  grid%c1f, grid%c2f,           &
2478                               grid%alt,                                          &
2479                               chem_old(ims,kms,jms,ic),                          &
2480                               chem(ims,kms,jms,ic),                              &
2481                               chem_tend(ims,kms,jms,ic),                         &
2482                               advect_tend,h_tendency,z_tendency,grid%rqvften,    & 
2483                               grid%qv_base, .false., grid%fnm, grid%fnp,         &
2484                               grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv, &
2485                               grid%msfvy, grid%msftx,grid%msfty,                 &
2486                               grid%rdx, grid%rdy, grid%rdn, grid%rdnw,           &
2487                               grid%khdif, grid%kvdif, grid%xkhh,                 &
2488                               grid%diff_6th_opt, grid%diff_6th_factor,           &
2489                               config_flags%chem_adv_opt,                         &
2490                               grid%phb, grid%ph_2,                               &
2491                               config_flags%chem_mix2_off,                        &
2492                               config_flags%chem_mix6_off,                        &
2493                               ids, ide, jds, jde, kds, kde,                      &
2494                               ims, ime, jms, jme, kms, kme,                      &
2495                               grid%i_start(ij), grid%i_end(ij),                  &
2496                               grid%j_start(ij), grid%j_end(ij),                  &
2497                               k_start    , k_end                                )
2499 ! Currently, chemistry species with specified boundaries (i.e. the mother
2500 ! domain)  are being over written by flow_dep_bdy_chem. So, relax_bdy and
2501 ! spec_bdy are only called for nests. For boundary conditions from global model or larger domain,
2502 ! chem is uncoupled, and only used for one row/column on inflow (if have_bcs_chem=.true.) 
2504            IF( ( config_flags%nested ) .and. rk_step == 1 ) THEN
2505              IF(ic.eq.1)CALL wrf_debug ( 10 , ' have_bcs_chem' )
2506              CALL relax_bdy_scalar ( chem_tend(ims,kms,jms,ic),                                    &
2507                                      chem(ims,kms,jms,ic),  grid%mut,                              &
2508                                      grid%c1h, grid%c2h,                       &
2509                                      chem_bxs(jms,kms,1,ic),chem_bxe(jms,kms,1,ic),                &
2510                                      chem_bys(ims,kms,1,ic),chem_bye(ims,kms,1,ic),                &
2511                                      chem_btxs(jms,kms,1,ic),chem_btxe(jms,kms,1,ic),              &
2512                                      chem_btys(ims,kms,1,ic),chem_btye(ims,kms,1,ic),              &
2513                                      config_flags%spec_bdy_width, grid%spec_zone, grid%relax_zone, &
2514                                      grid%dtbc, grid%fcx, grid%gcx,                                &
2515                                      config_flags,                                                 &
2516                                      ids,ide, jds,jde, kds,kde,                                    &
2517                                      ims,ime, jms,jme, kms,kme,                                    &
2518                                      ips,ipe, jps,jpe, kps,kpe,                                    &
2519                                      grid%i_start(ij), grid%i_end(ij),                             &
2520                                      grid%j_start(ij), grid%j_end(ij),                             &
2521                                      k_start, k_end                                                )
2522              CALL spec_bdy_scalar  ( chem_tend(ims,kms,jms,ic),                 &
2523                                      chem_bxs(jms,kms,1,ic),chem_bxe(jms,kms,1,ic),                &
2524                                      chem_bys(ims,kms,1,ic),chem_bye(ims,kms,1,ic),                &
2525                                      chem_btxs(jms,kms,1,ic),chem_btxe(jms,kms,1,ic),              &
2526                                      chem_btys(ims,kms,1,ic),chem_btye(ims,kms,1,ic),              &
2527                                      config_flags%spec_bdy_width, grid%spec_zone,                  &
2528                                      config_flags,                                                 &
2529                                      ids,ide, jds,jde, kds,kde,                                    &
2530                                      ims,ime, jms,jme, kms,kme,                                    &
2531                                      ips,ipe, jps,jpe, kps,kpe,                                    &
2532                                      grid%i_start(ij), grid%i_end(ij),                             &
2533                                      grid%j_start(ij), grid%j_end(ij),                             &
2534                                      k_start, k_end                                                )
2535            ENDIF
2537          ENDDO chem_tile_loop_1
2538          !$OMP END PARALLEL DO
2540 if ( config_flags%do_pvozone ) then
2541 #ifdef DM_PARALLEL
2542 #  include "HALO_EM_D_PV.inc"
2543 #endif
2544 end if
2546          !$OMP PARALLEL DO   &
2547          !$OMP PRIVATE ( ij, tenddec )
2549          chem_tile_loop_2: DO ij = 1 , grid%num_tiles
2551            CALL wrf_debug ( 200 , ' call rk_update_scalar' )
2552            tenddec = (( config_flags%chemdiag == USECHEMDIAG ) .and. &
2553                       ( adv_ct_indices(ic) >= PARAM_FIRST_SCALAR ))
2554            CALL rk_update_scalar( scs=ic, sce=ic,                                         &
2555                                   scalar_1=chem_old(ims,kms,jms,ic),                      &
2556                                   scalar_2=chem(ims,kms,jms,ic),                          &
2557                                   sc_tend=chem_tend(ims,kms,jms,ic),                      &
2558                                   advh_t=advh_ct(ims,kms,jms,adv_ct_indices(ic)),         &
2559                                   advz_t=advz_ct(ims,kms,jms,adv_ct_indices(ic)),         &
2560                                   advect_tend=advect_tend,                                &
2561                                   h_tendency=h_tendency, z_tendency=z_tendency,           & 
2562                                   msftx=grid%msftx,msfty=grid%msfty,                      &
2563                                   c1=grid%c1h, c2=grid%c2h,                               &
2564                                   mu_old=grid%mu_1, mu_new=grid%mu_2, mu_base=grid%mub,   &
2565                                   rk_step=rk_step, dt=dt_rk, spec_zone=grid%spec_zone,    &
2566                                   config_flags=config_flags, tenddec=tenddec,             & 
2567                                   ids=ids, ide=ide, jds=jds, jde=jde, kds=kds, kde=kde,   &
2568                                   ims=ims, ime=ime, jms=jms, jme=jme, kms=kms, kme=kme,   &
2569                                   its=grid%i_start(ij), ite=grid%i_end(ij),               &
2570                                   jts=grid%j_start(ij), jte=grid%j_end(ij),               &
2571                                   kts=k_start    , kte=k_end                              )
2573            IF( config_flags%specified  ) THEN
2575              IF( config_flags%perturb_chem_bdy==1 ) THEN
2577                  IF(ic.eq.PARAM_FIRST_SCALAR .and. ij.eq.1) &
2578                  CALL wrf_debug (10 , ' spec_bdy_chem_perturb' )
2580                  CALL spec_bdy_chem_perturb ( config_flags%periodic_x,   &
2581                       chem_btxs(jms,kms,1,ic), chem_btxe(jms,kms,1,ic),  &
2582                       chem_btys(ims,kms,1,ic), chem_btye(ims,kms,1,ic),  &
2583                       grid%rand_pert,                                    &
2584                       config_flags%spec_bdy_width, grid%spec_zone,       &
2585                       grid%num_stoch_levels,      & ! stoch  dims
2586                       ids,ide, jds,jde, kds,kde,  & ! domain dims
2587                       ims,ime, jms,jme, kms,kme,  & ! memory dims
2588                       ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2589                       grid%i_start(ij), grid%i_end(ij),                  &
2590                       grid%j_start(ij), grid%j_end(ij),                  &
2591                       k_start, k_end                                  )
2592              ENDIF
2594              CALL flow_dep_bdy_chem( chem(ims,kms,jms,ic),                          &
2595                                      chem_bxs(jms,kms,1,ic), chem_btxs(jms,kms,1,ic),  &
2596                                      chem_bxe(jms,kms,1,ic), chem_btxe(jms,kms,1,ic),  &
2597                                      chem_bys(ims,kms,1,ic), chem_btys(ims,kms,1,ic),  &
2598                                      chem_bye(ims,kms,1,ic), chem_btye(ims,kms,1,ic),  &
2599                                      dt_rk+grid%dtbc,                                  &
2600                                      config_flags%spec_bdy_width,grid%z,      &
2601                                      grid%have_bcs_chem,      &
2602                                      grid%ru_m, grid%rv_m, config_flags,grid%alt,       &
2603                                      grid%t_1,grid%pb,grid%p,t0,p1000mb,rcp,grid%ph_2,grid%phb,g, &
2604                                      grid%spec_zone,ic,grid%julday,      &
2605                                      ids,ide, jds,jde, kds,kde,  & ! domain dims
2606                                      ims,ime, jms,jme, kms,kme,  & ! memory dims
2607                                      ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2608                                      grid%i_start(ij), grid%i_end(ij),   &
2609                                      grid%j_start(ij), grid%j_end(ij),   &
2610                                      k_start, k_end,                     &
2611                                      grid%u_2,grid%v_2,grid%t_2,grid%znu,grid%msft, &
2612                                      grid%msfu,grid%msfv,grid%f,grid%mub,grid%dx,grid%xlat,grid%pv)
2614               ENDIF
2615          ENDDO chem_tile_loop_2
2616          !$OMP END PARALLEL DO
2618        ENDDO chem_variable_loop
2619      ENDIF chem_scalar_advance
2620 BENCH_END(chem_adv_tim)
2621 #endif
2622 !  next the chemical species
2623 BENCH_START(tracer_adv_tim)
2624        tracer_advance: IF (num_tracer >= PARAM_FIRST_SCALAR)  THEN
2626          tracer_variable_loop: DO ic = PARAM_FIRST_SCALAR, num_tracer
2628            !$OMP PARALLEL DO   &
2629            !$OMP PRIVATE ( ij, tenddec )
2630            tracer_tile_loop_1: DO ij = 1 , grid%num_tiles
2632              CALL wrf_debug ( 15 , ' call rk_scalar_tend in tracer_tile_loop_1' )
2633              tenddec = .false.
2634              CALL rk_scalar_tend ( ic, ic, config_flags, tenddec,                & 
2635                               rk_step, dt_rk,                                    &
2636                               grid%ru_m, grid%rv_m, grid%ww_m, wwE, wwI,         &
2637                               grid%u_1, grid%v_1,                                &  
2638                               grid%muts, grid%mub, grid%mu_1,                    &
2639                               grid%c1h, grid%c2h,  grid%c1f, grid%c2f,           &
2640                               grid%alt,                                          &
2641                               tracer_old(ims,kms,jms,ic),                        &
2642                               tracer(ims,kms,jms,ic),                            &
2643                               tracer_tend(ims,kms,jms,ic),                       &
2644                               advect_tend,h_tendency,z_tendency,grid%rqvften,    & 
2645                               grid%qv_base, .false., grid%fnm, grid%fnp,         &
2646                               grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv, &
2647                               grid%msfvy, grid%msftx,grid%msfty,                 &
2648                               grid%rdx, grid%rdy, grid%rdn, grid%rdnw,           &
2649                               grid%khdif, grid%kvdif, grid%xkhh,                 &
2650                               grid%diff_6th_opt, grid%diff_6th_factor,           &
2651                               config_flags%tracer_adv_opt,                       &
2652                               grid%phb, grid%ph_2,                               &
2653                               config_flags%tracer_mix2_off,                      &
2654                               config_flags%tracer_mix6_off,                      &
2655                               ids, ide, jds, jde, kds, kde,                      &
2656                               ims, ime, jms, jme, kms, kme,                      &
2657                               grid%i_start(ij), grid%i_end(ij),                  &
2658                               grid%j_start(ij), grid%j_end(ij),                  &
2659                               k_start    , k_end                                )
2661 ! Currently, chemistry species with specified boundaries (i.e. the mother
2662 ! domain)  are being over written by flow_dep_bdy_chem. So, relax_bdy and
2663 ! spec_bdy are only called for nests. For boundary conditions from global model or larger domain,
2664 ! chem is uncoupled, and only used for one row/column on inflow (if have_bcs_chem=.true.) 
2666            IF( ( config_flags%nested ) .and. rk_step == 1 ) THEN
2667              IF(ic.eq.1)CALL wrf_debug ( 10 , ' have_bcs_tracer' )
2668              CALL relax_bdy_scalar ( tracer_tend(ims,kms,jms,ic),                                    &
2669                                      tracer(ims,kms,jms,ic),  grid%mut,                              &
2670                                      grid%c1h, grid%c2h,                       &
2671                                      tracer_bxs(jms,kms,1,ic),tracer_bxe(jms,kms,1,ic),                &
2672                                      tracer_bys(ims,kms,1,ic),tracer_bye(ims,kms,1,ic),                &
2673                                      tracer_btxs(jms,kms,1,ic),tracer_btxe(jms,kms,1,ic),              &
2674                                      tracer_btys(ims,kms,1,ic),tracer_btye(ims,kms,1,ic),              &
2675                                      config_flags%spec_bdy_width, grid%spec_zone, grid%relax_zone, &
2676                                      grid%dtbc, grid%fcx, grid%gcx,                                &
2677                                      config_flags,                                                 &
2678                                      ids,ide, jds,jde, kds,kde,                                    &
2679                                      ims,ime, jms,jme, kms,kme,                                    &
2680                                      ips,ipe, jps,jpe, kps,kpe,                                    &
2681                                      grid%i_start(ij), grid%i_end(ij),                             &
2682                                      grid%j_start(ij), grid%j_end(ij),                             &
2683                                      k_start, k_end                                                )
2684              CALL spec_bdy_scalar  ( tracer_tend(ims,kms,jms,ic),                 &
2685                                      tracer_bxs(jms,kms,1,ic),tracer_bxe(jms,kms,1,ic),                &
2686                                      tracer_bys(ims,kms,1,ic),tracer_bye(ims,kms,1,ic),                &
2687                                      tracer_btxs(jms,kms,1,ic),tracer_btxe(jms,kms,1,ic),              &
2688                                      tracer_btys(ims,kms,1,ic),tracer_btye(ims,kms,1,ic),              &
2689                                      config_flags%spec_bdy_width, grid%spec_zone,                  &
2690                                      config_flags,                                                 &
2691                                      ids,ide, jds,jde, kds,kde,                                    &
2692                                      ims,ime, jms,jme, kms,kme,                                    &
2693                                      ips,ipe, jps,jpe, kps,kpe,                                    &
2694                                      grid%i_start(ij), grid%i_end(ij),                             &
2695                                      grid%j_start(ij), grid%j_end(ij),                             &
2696                                      k_start, k_end                                                )
2697            ENDIF
2699          ENDDO tracer_tile_loop_1
2700          !$OMP END PARALLEL DO
2702          !$OMP PARALLEL DO   &
2703          !$OMP PRIVATE ( ij, tenddec )
2705          tracer_tile_loop_2: DO ij = 1 , grid%num_tiles
2707            CALL wrf_debug ( 200 , ' call rk_update_scalar' )
2708            tenddec = .false.
2709            CALL rk_update_scalar( scs=ic, sce=ic,                                         &
2710                                   scalar_1=tracer_old(ims,kms,jms,ic),                    &
2711                                   scalar_2=tracer(ims,kms,jms,ic),                        &
2712                                   sc_tend=tracer_tend(ims,kms,jms,ic),                    &
2713 !                                 advh_t=advh_t(ims,kms,jms,1),                           & 
2714 !                                 advz_t=advz_t(ims,kms,jms,1),                           & 
2715                                   advect_tend=advect_tend,                                &
2716                                   h_tendency=h_tendency, z_tendency=z_tendency,           & 
2717                                   msftx=grid%msftx,msfty=grid%msfty,                      &
2718                                   c1=grid%c1h, c2=grid%c2h,                               &
2719                                   mu_old=grid%mu_1, mu_new=grid%mu_2, mu_base=grid%mub,   &
2720                                   rk_step=rk_step, dt=dt_rk, spec_zone=grid%spec_zone,    &
2721                                   config_flags=config_flags, tenddec=tenddec,             & 
2722                                   ids=ids, ide=ide, jds=jds, jde=jde, kds=kds, kde=kde,   &
2723                                   ims=ims, ime=ime, jms=jms, jme=jme, kms=kms, kme=kme,   &
2724                                   its=grid%i_start(ij), ite=grid%i_end(ij),               &
2725                                   jts=grid%j_start(ij), jte=grid%j_end(ij),               &
2726                                   kts=k_start    , kte=k_end                              )
2728            IF( config_flags%specified  ) THEN
2729 #if (WRF_CHEM == 1)
2730              CALL flow_dep_bdy_tracer( tracer(ims,kms,jms,ic),                             &
2731                                      tracer_bxs(jms,kms,1,ic), tracer_btxs(jms,kms,1,ic),  &
2732                                      tracer_bxe(jms,kms,1,ic), tracer_btxe(jms,kms,1,ic),  &
2733                                      tracer_bys(ims,kms,1,ic), tracer_btys(ims,kms,1,ic),  &
2734                                      tracer_bye(ims,kms,1,ic), tracer_btye(ims,kms,1,ic),  &
2735                                      dt_rk+grid%dtbc,                                  &
2736                                      config_flags%spec_bdy_width,grid%z,      &
2737                                      grid%have_bcs_tracer,      &
2738                                      grid%ru_m, grid%rv_m, config_flags%tracer_opt,grid%alt,       &
2739                                      grid%t_1,grid%pb,grid%p,t0,p1000mb,rcp,grid%ph_2,grid%phb,g, &
2740                                      grid%spec_zone,ic,                  &
2741                                      ids,ide, jds,jde, kds,kde,  & ! domain dims
2742                                      ims,ime, jms,jme, kms,kme,  & ! memory dims
2743                                      ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2744                                      grid%i_start(ij), grid%i_end(ij),   &
2745                                      grid%j_start(ij), grid%j_end(ij),   &
2746                                      k_start, k_end                      )
2747 #else
2748              CALL flow_dep_bdy  ( tracer(ims,kms,jms,ic),     &
2749                                   grid%ru_m, grid%rv_m, config_flags,   &
2750                                   grid%spec_zone,                  &
2751                                   ids,ide, jds,jde, kds,kde,  & ! domain dims
2752                                   ims,ime, jms,jme, kms,kme,  & ! memory dims
2753                                   ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2754                                   grid%i_start(ij), grid%i_end(ij),  &
2755                                   grid%j_start(ij), grid%j_end(ij),  &
2756                                   k_start, k_end                    )
2757 #endif
2758            ENDIF
2759          ENDDO tracer_tile_loop_2
2760          !$OMP END PARALLEL DO
2762        ENDDO tracer_variable_loop
2763      ENDIF tracer_advance
2764 BENCH_END(tracer_adv_tim)
2766 !  next the other scalar species
2767      other_scalar_advance: IF (num_3d_s >= PARAM_FIRST_SCALAR)  THEN
2769        scalar_variable_loop: do is = PARAM_FIRST_SCALAR, num_3d_s
2770          !$OMP PARALLEL DO   &
2771          !$OMP PRIVATE ( ij, tenddec )
2772          scalar_tile_loop_1: DO ij = 1 , grid%num_tiles
2774            CALL wrf_debug ( 200 , ' call rk_scalar_tend' )
2775            tenddec = .false.
2776            CALL rk_scalar_tend ( is, is, config_flags, tenddec,                   & 
2777                                  rk_step, dt_rk,                                  &
2778                                  grid%ru_m, grid%rv_m, grid%ww_m, wwE, wwI,       &
2779                                  grid%u_1, grid%v_1,                              &  
2780                                  grid%muts, grid%mub, grid%mu_1,                  &
2781                                  grid%c1h, grid%c2h,  grid%c1f, grid%c2f,         &
2782                                  grid%alt,                                        &
2783                                  scalar_old(ims,kms,jms,is),                      &
2784                                  scalar(ims,kms,jms,is),                          &
2785                                  scalar_tend(ims,kms,jms,is),                     &
2786                                  advect_tend,h_tendency,z_tendency,grid%rqvften,  & 
2787                                  grid%qv_base, .false., grid%fnm, grid%fnp,       &
2788                                  grid%msfux,grid%msfuy, grid%msfvx, grid%msfvx_inv, &
2789                                  grid%msfvy, grid%msftx,grid%msfty,               &
2790                                  grid%rdx, grid%rdy, grid%rdn, grid%rdnw,         &
2791                                  grid%khdif, grid%kvdif, grid%xkhh,               &
2792                                  grid%diff_6th_opt, grid%diff_6th_factor,         &
2793                                  config_flags%scalar_adv_opt,                     &
2794                                  grid%phb, grid%ph_2,                             &
2795                                  config_flags%scalar_mix2_off,                    &
2796                                  config_flags%scalar_mix6_off,                    &
2797                                  ids, ide, jds, jde, kds, kde,     &
2798                                  ims, ime, jms, jme, kms, kme,     &
2799                                  grid%i_start(ij), grid%i_end(ij), &
2800                                  grid%j_start(ij), grid%j_end(ij), &
2801                                  k_start    , k_end               )
2803            IF( rk_step == 1 ) THEN
2804              IF ( config_flags%nested .OR. &
2805                 ( config_flags%specified .AND. config_flags%have_bcs_scalar ) .OR. &
2806                 ( ( is .EQ. P_QNWFA .OR. is .EQ. P_QNIFA .OR. is .EQ. P_QNBCA) .AND. config_flags%aer_init_opt .GT. 0) ) THEN
2808                CALL relax_bdy_scalar ( scalar_tend(ims,kms,jms,is),                            &
2809                                        scalar(ims,kms,jms,is),  grid%mut,                      &
2810                                        grid%c1h, grid%c2h,                       &
2811                                        scalar_bxs(jms,kms,1,is),scalar_bxe(jms,kms,1,is),      &
2812                                        scalar_bys(ims,kms,1,is),scalar_bye(ims,kms,1,is),      &
2813                                        scalar_btxs(jms,kms,1,is),scalar_btxe(jms,kms,1,is),    &
2814                                        scalar_btys(ims,kms,1,is),scalar_btye(ims,kms,1,is),    &
2815                                        config_flags%spec_bdy_width, grid%spec_zone, grid%relax_zone, &
2816                                        grid%dtbc, grid%fcx, grid%gcx,                          &
2817                                        config_flags,                                           &
2818                                        ids,ide, jds,jde, kds,kde,                              &
2819                                        ims,ime, jms,jme, kms,kme,                              &
2820                                        ips,ipe, jps,jpe, kps,kpe,                              &
2821                                        grid%i_start(ij), grid%i_end(ij),                       &
2822                                        grid%j_start(ij), grid%j_end(ij),                       &
2823                                        k_start, k_end                                          )
2825                CALL spec_bdy_scalar  ( scalar_tend(ims,kms,jms,is),                            &
2826                                        scalar_bxs(jms,kms,1,is),scalar_bxe(jms,kms,1,is),      &
2827                                        scalar_bys(ims,kms,1,is),scalar_bye(ims,kms,1,is),      &
2828                                        scalar_btxs(jms,kms,1,is),scalar_btxe(jms,kms,1,is),    &
2829                                        scalar_btys(ims,kms,1,is),scalar_btye(ims,kms,1,is),    &
2830                                        config_flags%spec_bdy_width, grid%spec_zone,            &
2831                                        config_flags,                                           &
2832                                        ids,ide, jds,jde, kds,kde,                              &
2833                                        ims,ime, jms,jme, kms,kme,                              &
2834                                        ips,ipe, jps,jpe, kps,kpe,                              &
2835                                        grid%i_start(ij), grid%i_end(ij),                       &
2836                                        grid%j_start(ij), grid%j_end(ij),                       &
2837                                        k_start, k_end                                          )
2839              ENDIF
2840            ENDIF ! b.c test for scalars
2842          ENDDO scalar_tile_loop_1
2843          !$OMP END PARALLEL DO
2845          !$OMP PARALLEL DO   &
2846          !$OMP PRIVATE ( ij, tenddec )
2847          scalar_tile_loop_2: DO ij = 1 , grid%num_tiles
2849            CALL wrf_debug ( 200 , ' call rk_update_scalar' )
2850            tenddec = .false.
2851            CALL rk_update_scalar( scs=is, sce=is,                                         &
2852                                   scalar_1=scalar_old(ims,kms,jms,is),                    &
2853                                   scalar_2=scalar(ims,kms,jms,is),                        &
2854                                   sc_tend=scalar_tend(ims,kms,jms,is),                    &
2855 !                                 advh_t=advh_t(ims,kms,jms,1),                           & 
2856 !                                 advz_t=advz_t(ims,kms,jms,1),                           & 
2857                                   advect_tend=advect_tend,                                &
2858                                   h_tendency=h_tendency, z_tendency=z_tendency,           & 
2859                                   msftx=grid%msftx,msfty=grid%msfty,                      &
2860                                   c1=grid%c1h, c2=grid%c2h,                               &
2861                                   mu_old=grid%mu_1, mu_new=grid%mu_2, mu_base=grid%mub,   &
2862                                   rk_step=rk_step, dt=dt_rk, spec_zone=grid%spec_zone,    &
2863                                   config_flags=config_flags, tenddec=tenddec,             & 
2864                                   ids=ids, ide=ide, jds=jds, jde=jde, kds=kds, kde=kde,   &
2865                                   ims=ims, ime=ime, jms=jms, jme=jme, kms=kms, kme=kme,   &
2866                                   its=grid%i_start(ij), ite=grid%i_end(ij),               &
2867                                   jts=grid%j_start(ij), jte=grid%j_end(ij),               &
2868                                   kts=k_start    , kte=k_end                              )
2870 ! bound the aerosol fields (greater than 0) when using first guess aerosol
2871 ! as fields may be highly heterogeneous compared to climatology
2873            IF ( ( ( is .EQ. P_QNWFA ) .OR. ( is .EQ. P_QNIFA ) .OR. ( is .EQ. P_QNBCA ) ) .AND. &
2874                          ( config_flags%aer_init_opt .EQ. 2 ) ) THEN
2875              CALL bound_qna( scalar(ims,kms,jms,is),              &
2876                              ids, ide, jds, jde, kds, kde,        &
2877                              ims, ime, jms, jme, kms, kme,        &
2878                              grid%i_start(ij), grid%i_end(ij),    &
2879                              grid%j_start(ij), grid%j_end(ij),    &
2880                              k_start    , k_end                  )
2881            END IF
2883            IF ( config_flags%specified ) THEN
2884               IF (is.EQ.P_QDCN.OR.is.EQ.P_QTCN.OR.is.EQ.P_QNIN) THEN       ! for ntu3m
2885                  CALL flow_dep_bdy_fixed_inflow(scalar(ims,kms,jms,is), &
2886                                        grid%ru_m,grid%rv_m,config_flags,&
2887                                        grid%spec_zone,ids,ide,jds,jde,  &
2888                                        kds,kde,ims,ime,jms,jme,kms,kme, &
2889                                        ips,ipe,jps,jpe,kps,kpe,         &
2890                                        grid%i_start(ij),grid%i_end(ij), &
2891                                        grid%j_start(ij),grid%j_end(ij), &
2892                                        k_start,k_end)
2893               ELSEIF (is.EQ.P_QNN) THEN                                    ! for ntu3m
2894                CALL flow_dep_bdy_qnn  ( scalar(ims,kms,jms,is),     &
2895                                   grid%ru_m, grid%rv_m, config_flags,   &
2896                                   grid%spec_zone,                  &
2897                                   grid%ccn_conc,              & ! RAS
2898                                   ids,ide, jds,jde, kds,kde,  & ! domain dims
2899                                   ims,ime, jms,jme, kms,kme,  & ! memory dims
2900                                   ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2901                                   grid%i_start(ij), grid%i_end(ij),  &
2902                                   grid%j_start(ij), grid%j_end(ij),  &
2903                                   k_start, k_end                    )
2904              ELSE IF ( ( ( ( is .EQ. P_QNWFA ) .OR. ( is .EQ. P_QNIFA ) .OR. ( is .EQ. P_QNBCA ) ) .AND. &
2905                          ( config_flags%aer_init_opt .EQ. 0 ) ) &
2906                          .OR. &
2907                        ( ( .NOT. ( ( is .EQ. P_QNWFA ) .OR. ( is .EQ. P_QNIFA ) .OR. ( is .EQ. P_QNBCA ) ) ) .AND. &
2908                          ( .NOT. config_flags%have_bcs_scalar ) ) ) THEN
2910 !     A = ( is .EQ. P_QNWFA ) .OR. ( is .EQ. P_QNIFA ) .OR. ( is .EQ. P_QNBCA )
2911 !     B = config_flags%aer_init_opt .GT. 0
2912 !     C = config_glags%have_bcs_scalar
2914 ! Test| A  | B  | C | ( A AND NOT B ) OR ( NOT A AND NOT C ) 
2915 ! ----+----+----+---+-----------------------------------------------
2916 !  1  | T  | T  | T |                 F = DO NOT CALL flow_dep_bdy
2917 !  2  | T  | T  | F |                 F = DO NOT CALL flow_dep_bdy
2918 !  3  | T  | F  | T |                 T =        CALL flow_dep_bdy
2919 !  4  | T  | F  | F |                 T =        CALL flow_dep_bdy
2920 !  5  | F  | T  | T |                 F = DO NOT CALL flow_dep_bdy
2921 !  6  | F  | T  | F |                 T =        CALL flow_dep_bdy
2922 !  7  | F  | F  | T |                 F = DO NOT CALL flow_dep_bdy
2923 !  8  | F  | F  | F |                 T =        CALL flow_dep_bdy
2924 ! ----+----+----+---+-----------------------------------------------
2926 !  If this is     the special friendly fields AND are to use the aero icbc, then NO calls to flow dep: tests 1 and 2
2927 !  If this is     the special friendly fields AND do not use the aero icbc, then    call     flow dep: tests 3 and 4
2928 !  If this is not the special friendly fields AND: 
2929 !           If we        have bcs for scalars, do not call flow dep: tests 5 and 7
2930 !           If we do not have bcs for scalars,        call flow dep: tests 6 and 8
2932                CALL flow_dep_bdy  ( scalar(ims,kms,jms,is),     &
2933                                   grid%ru_m, grid%rv_m, config_flags,   &
2934                                   grid%spec_zone,                  &
2935                                   ids,ide, jds,jde, kds,kde,  & ! domain dims
2936                                   ims,ime, jms,jme, kms,kme,  & ! memory dims
2937                                   ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
2938                                   grid%i_start(ij), grid%i_end(ij),  &
2939                                   grid%j_start(ij), grid%j_end(ij),  &
2940                                   k_start, k_end                    )
2941              ENDIF
2943            ENDIF
2945          ENDDO scalar_tile_loop_2
2946          !$OMP END PARALLEL DO
2948        ENDDO scalar_variable_loop
2950      ENDIF other_scalar_advance
2952  !  update the pressure and density at the new time level
2954      !$OMP PARALLEL DO   &
2955      !$OMP PRIVATE ( ij )
2956      DO ij = 1 , grid%num_tiles
2958 BENCH_START(calc_p_rho_tim)
2960        CALL calc_p_rho_phi( moist, num_3d_m, config_flags%hypsometric_opt,                    &
2961                             grid%al, grid%alb, grid%mu_2, grid%muts,                          &
2962                             grid%c1h, grid%c2h, grid%c3h, grid%c4h, grid%c3f, grid%c4f,       &
2963                             grid%ph_2, grid%phb, grid%p, grid%pb, grid%t_2,                   &
2964                             p0, t0, grid%p_top, grid%znu, grid%znw, grid%dnw, grid%rdnw,      &
2965                             grid%rdn, config_flags%non_hydrostatic,config_flags%use_theta_m,  &
2966                             ids, ide, jds, jde, kds, kde,                                     &
2967                             ims, ime, jms, jme, kms, kme,                                     &
2968                             grid%i_start(ij), grid%i_end(ij),                                 &
2969                             grid%j_start(ij), grid%j_end(ij),                                 &
2970                             k_start    , k_end                                                )
2972 BENCH_END(calc_p_rho_tim)
2974      ENDDO
2975      !$OMP END PARALLEL DO
2977 !  Reset the boundary conditions if there is another corrector step.
2978 !  (rk_step < rk_order), else we'll handle it at the end of everything
2979 !  (after the split physics, before exiting the timestep).
2981      rk_step_1_check: IF ( rk_step < rk_order ) THEN
2983 !-----------------------------------------------------------
2984 !  rk3 substep polar filter for scalars (moist,chem,scalar)
2985 !-----------------------------------------------------------
2987        IF (config_flags%polar) THEN 
2988          IF ( num_3d_m >= PARAM_FIRST_SCALAR ) THEN
2989            CALL wrf_debug ( 200 , ' call filter moist ' )
2990            DO im = PARAM_FIRST_SCALAR, num_3d_m
2991              IF ( config_flags%coupled_filtering ) THEN
2992              CALL couple_scalars_for_filter ( FIELD=moist(ims,kms,jms,im)        &
2993                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
2994                     ,C1=grid%c1h , C2=grid%c2h                                   &
2995                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
2996                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
2997                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
2998              END IF
2999              CALL pxft ( grid=grid                                               &
3000                     ,lineno=__LINE__                                             &
3001                     ,flag_uv            = 0                                      &
3002                     ,flag_rurv          = 0                                      &
3003                     ,flag_wph           = 0                                      &
3004                     ,flag_ww            = 0                                      &
3005                     ,flag_t             = 0                                      &
3006                     ,flag_mu            = 0                                      &
3007                     ,flag_mut           = 0                                      &
3008                     ,flag_moist         = im                                     &
3009                     ,flag_chem          = 0                                      &
3010                     ,flag_scalar        = 0                                      &
3011                     ,flag_tracer        = 0                                      &
3012                     ,actual_distance_average=config_flags%actual_distance_average&
3013                     ,pos_def            = config_flags%pos_def                   &
3014                     ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
3015                     ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
3016                     ,fft_filter_lat = config_flags%fft_filter_lat                &
3017                     ,dclat = dclat                                               &
3018                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3019                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3020                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
3021                     ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
3022                     ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
3023              IF ( config_flags%coupled_filtering ) THEN
3024              CALL uncouple_scalars_for_filter ( FIELD=moist(ims,kms,jms,im)      &
3025                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
3026                     ,C1=grid%c1h , C2=grid%c2h                                   &
3027                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3028                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3029                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
3030              END IF
3031            END DO
3032          END IF
3033    
3034          IF ( num_3d_c >= PARAM_FIRST_SCALAR ) THEN
3035            CALL wrf_debug ( 200 , ' call filter chem ' )
3036            DO im = PARAM_FIRST_SCALAR, num_3d_c
3037              IF ( config_flags%coupled_filtering ) THEN
3038              CALL couple_scalars_for_filter ( FIELD=chem(ims,kms,jms,im)               &
3039                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
3040                     ,C1=grid%c1h , C2=grid%c2h                                   &
3041                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3042                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3043                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe           )
3044              END IF
3045              CALL pxft ( grid=grid                                               &
3046                     ,lineno=__LINE__                                             &
3047                     ,flag_uv            = 0                                      &
3048                     ,flag_rurv          = 0                                      &
3049                     ,flag_wph           = 0                                      &
3050                     ,flag_ww            = 0                                      &
3051                     ,flag_t             = 0                                      &
3052                     ,flag_mu            = 0                                      &
3053                     ,flag_mut           = 0                                      &
3054                     ,flag_moist         = 0                                      &
3055                     ,flag_chem          = im                                     &
3056                     ,flag_tracer        = 0                                      &
3057                     ,flag_scalar        = 0                                      &
3058                     ,actual_distance_average=config_flags%actual_distance_average&
3059                     ,pos_def            = config_flags%pos_def                   &
3060                     ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
3061                     ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
3062                     ,fft_filter_lat = config_flags%fft_filter_lat                &
3063                     ,dclat = dclat                                               &
3064                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3065                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3066                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
3067                     ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
3068                     ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
3069              IF ( config_flags%coupled_filtering ) THEN
3070              CALL uncouple_scalars_for_filter ( FIELD=chem(ims,kms,jms,im)       &
3071                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
3072                     ,C1=grid%c1h , C2=grid%c2h                                   &
3073                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3074                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3075                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
3076              END IF
3077            END DO
3078          END IF
3079          IF ( num_tracer >= PARAM_FIRST_SCALAR ) THEN
3080            CALL wrf_debug ( 200 , ' call filter tracer ' )
3081            DO im = PARAM_FIRST_SCALAR, num_tracer
3082              IF ( config_flags%coupled_filtering ) THEN
3083              CALL couple_scalars_for_filter ( FIELD=tracer(ims,kms,jms,im)               &
3084                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
3085                     ,C1=grid%c1h , C2=grid%c2h                                   &
3086                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3087                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3088                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe           )
3089              END IF
3090              CALL pxft ( grid=grid                                               &
3091                     ,lineno=__LINE__                                             &
3092                     ,flag_uv            = 0                                      &
3093                     ,flag_rurv          = 0                                      &
3094                     ,flag_wph           = 0                                      &
3095                     ,flag_ww            = 0                                      &
3096                     ,flag_t             = 0                                      &
3097                     ,flag_mu            = 0                                      &
3098                     ,flag_mut           = 0                                      &
3099                     ,flag_moist         = 0                                      &
3100                     ,flag_chem          = 0                                      &
3101                     ,flag_tracer        = im                                     &
3102                     ,flag_scalar        = 0                                      &
3103                     ,actual_distance_average=config_flags%actual_distance_average&
3104                     ,pos_def            = config_flags%pos_def                   &
3105                     ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
3106                     ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
3107                     ,fft_filter_lat = config_flags%fft_filter_lat                &
3108                     ,dclat = dclat                                               &
3109                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3110                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3111                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
3112                     ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
3113                     ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
3114              IF ( config_flags%coupled_filtering ) THEN
3115              CALL uncouple_scalars_for_filter ( FIELD=tracer(ims,kms,jms,im)     &
3116                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
3117                     ,C1=grid%c1h , C2=grid%c2h                                   &
3118                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3119                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3120                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
3121              END IF
3122            END DO
3123          END IF
3124    
3125          IF ( num_3d_s >= PARAM_FIRST_SCALAR ) THEN
3126            CALL wrf_debug ( 200 , ' call filter scalar ' )
3127            DO im = PARAM_FIRST_SCALAR, num_3d_s
3128              IF ( config_flags%coupled_filtering ) THEN
3129              CALL couple_scalars_for_filter ( FIELD=scalar(ims,kms,jms,im)           &
3130                   ,MU=grid%mu_2 , MUB=grid%mub                                 &
3131                   ,C1=grid%c1h , C2=grid%c2h                                   &
3132                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3133                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3134                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
3135              END IF
3136              CALL pxft ( grid=grid                                             &
3137                   ,lineno=__LINE__                                             &
3138                   ,flag_uv            = 0                                      &
3139                   ,flag_rurv          = 0                                      &
3140                   ,flag_wph           = 0                                      &
3141                   ,flag_ww            = 0                                      &
3142                   ,flag_t             = 0                                      &
3143                   ,flag_mu            = 0                                      &
3144                   ,flag_mut           = 0                                      &
3145                   ,flag_moist         = 0                                      &
3146                   ,flag_chem          = 0                                      &
3147                   ,flag_tracer        = 0                                      &
3148                   ,flag_scalar        = im                                     &
3149                   ,actual_distance_average=config_flags%actual_distance_average&
3150                   ,pos_def            = config_flags%pos_def                   &
3151                   ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
3152                   ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
3153                   ,fft_filter_lat = config_flags%fft_filter_lat                &
3154                   ,dclat = dclat                                               &
3155                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3156                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3157                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
3158                   ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
3159                   ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
3160              IF ( config_flags%coupled_filtering ) THEN
3161              CALL uncouple_scalars_for_filter ( FIELD=scalar(ims,kms,jms,im)   &
3162                   ,MU=grid%mu_2 , MUB=grid%mub                                 &
3163                   ,C1=grid%c1h , C2=grid%c2h                                   &
3164                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
3165                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
3166                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
3167              END IF
3168            END DO
3169          END IF
3170        END IF ! polar filter test
3172 !-----------------------------------------------------------
3173 !  END rk3 substep polar filter for scalars (moist,chem,scalar)
3174 !-----------------------------------------------------------
3176 !-----------------------------------------------------------
3177 !  Stencils for patch communications  (WCS, 29 June 2001)
3179 !  here's where we need a wide comm stencil - these are the 
3180 !  uncoupled variables so are used for high order calc in
3181 !  advection and mixong routines.
3184 !                                  * * * * * * *
3185 !                     * * * * *    * * * * * * *
3186 !            *        * * * * *    * * * * * * *
3187 !          * + *      * * + * *    * * * + * * *
3188 !            *        * * * * *    * * * * * * *
3189 !                     * * * * *    * * * * * * *
3190 !                                  * * * * * * *
3192 ! al        x
3194 !  2D variable
3195 ! mu_2      x
3197 ! (adv order <=4)
3198 ! u_2                     x
3199 ! v_2                     x
3200 ! w_2                     x
3201 ! t_2                     x
3202 ! ph_2                    x
3204 ! (adv order <=6)
3205 ! u_2                                    x
3206 ! v_2                                    x
3207 ! w_2                                    x
3208 ! t_2                                    x
3209 ! ph_2                                   x
3211 !  4D variable
3212 ! moist                   x
3213 ! chem                    x
3214 ! scalar                  x
3216 #ifdef DM_PARALLEL
3217        IF      ( config_flags%h_mom_adv_order <= 4 .and. config_flags%h_sca_adv_order <= 4 ) THEN
3218 #    include "HALO_EM_D2_3.inc"
3219        ELSE IF ( config_flags%h_mom_adv_order <= 6 .and. config_flags%h_sca_adv_order <= 6 ) THEN
3220 #    include "HALO_EM_D2_5.inc"
3221        ELSE 
3222          WRITE(wrf_err_message,*)'solve_em: invalid h_mom_adv_order or h_sca_adv_order = ', &
3223                config_flags%h_mom_adv_order, config_flags%h_sca_adv_order
3224          CALL wrf_error_fatal(TRIM(wrf_err_message))
3225        ENDIF
3226 #  include "PERIOD_BDY_EM_D.inc"
3227 #  include "PERIOD_BDY_EM_MOIST2.inc"
3228 #  include "PERIOD_BDY_EM_CHEM2.inc"
3229 #  include "PERIOD_BDY_EM_TRACER2.inc"
3230 #  include "PERIOD_BDY_EM_SCALAR2.inc"
3231 #  include "PERIOD_BDY_EM_TKE.inc"
3232 #endif
3234 BENCH_START(bc_end_tim)
3235        !$OMP PARALLEL DO   &
3236        !$OMP PRIVATE ( ij )
3237        tile_bc_loop_1: DO ij = 1 , grid%num_tiles
3238          CALL wrf_debug ( 200 , ' call rk_phys_bc_dry_2' )
3240          CALL rk_phys_bc_dry_2( config_flags,                     &
3241                                 grid%u_2, grid%v_2, grid%w_2,     &
3242                                 grid%t_2, grid%ph_2, grid%mu_2,   &
3243                                 ids, ide, jds, jde, kds, kde,     &
3244                                 ims, ime, jms, jme, kms, kme,     &
3245                                 ips, ipe, jps, jpe, kps, kpe,     &
3246                                 grid%i_start(ij), grid%i_end(ij), &
3247                                 grid%j_start(ij), grid%j_end(ij), &
3248                                 k_start    , k_end               )
3250 BENCH_START(diag_w_tim)
3251          IF (.not. config_flags%non_hydrostatic) THEN
3252            CALL diagnose_w( ph_tend, grid%ph_2, grid%ph_1, grid%w_2, grid%muts, &
3253                             grid%c1f, grid%c2f, dt_rk,              &
3254                             grid%u_2, grid%v_2, grid%ht,            &
3255                             grid%cf1, grid%cf2, grid%cf3, grid%rdx, grid%rdy, grid%msftx, grid%msfty, &
3256                             ids, ide, jds, jde, kds, kde,           &
3257                             ims, ime, jms, jme, kms, kme,           &
3258                             grid%i_start(ij), grid%i_end(ij),       &
3259                             grid%j_start(ij), grid%j_end(ij),       &
3260                             k_start    , k_end                     )
3261          ENDIF
3262 BENCH_END(diag_w_tim)
3264          IF (num_3d_m >= PARAM_FIRST_SCALAR) THEN
3266            moisture_loop_bdy_1 : DO im = PARAM_FIRST_SCALAR , num_3d_m
3267   
3268              CALL set_physical_bc3d( moist(ims,kms,jms,im), 'p', config_flags,   &
3269                                      ids, ide, jds, jde, kds, kde,             &
3270                                      ims, ime, jms, jme, kms, kme,             &
3271                                      ips, ipe, jps, jpe, kps, kpe,             &
3272                                      grid%i_start(ij), grid%i_end(ij),                   &
3273                                      grid%j_start(ij), grid%j_end(ij),                   &
3274                                      k_start    , k_end                       )
3275            END DO moisture_loop_bdy_1
3277          ENDIF
3279          IF (num_3d_c >= PARAM_FIRST_SCALAR) THEN
3281            chem_species_bdy_loop_1 : DO ic = PARAM_FIRST_SCALAR , num_3d_c
3283              CALL set_physical_bc3d( chem(ims,kms,jms,ic), 'p', config_flags,   &
3284                                      ids, ide, jds, jde, kds, kde,            &
3285                                      ims, ime, jms, jme, kms, kme,            &
3286                                      ips, ipe, jps, jpe, kps, kpe,            &
3287                                      grid%i_start(ij), grid%i_end(ij),                  &
3288                                      grid%j_start(ij), grid%j_end(ij),                  &
3289                                      k_start    , k_end-1                    )
3291            END DO chem_species_bdy_loop_1
3293          END IF
3295          IF (num_tracer >= PARAM_FIRST_SCALAR) THEN
3297            tracer_species_bdy_loop_1 : DO ic = PARAM_FIRST_SCALAR , num_tracer
3299              CALL set_physical_bc3d( tracer(ims,kms,jms,ic), 'p', config_flags,   &
3300                                      ids, ide, jds, jde, kds, kde,            &
3301                                      ims, ime, jms, jme, kms, kme,            &
3302                                      ips, ipe, jps, jpe, kps, kpe,            &
3303                                      grid%i_start(ij), grid%i_end(ij),                  &
3304                                      grid%j_start(ij), grid%j_end(ij),                  &
3305                                      k_start    , k_end-1                    )
3307            END DO tracer_species_bdy_loop_1
3309          END IF
3311          IF (num_3d_s >= PARAM_FIRST_SCALAR) THEN
3313            scalar_species_bdy_loop_1 : DO is = PARAM_FIRST_SCALAR , num_3d_s
3315              CALL set_physical_bc3d( scalar(ims,kms,jms,is), 'p', config_flags,   &
3316                                      ids, ide, jds, jde, kds, kde,            &
3317                                      ims, ime, jms, jme, kms, kme,            &
3318                                      ips, ipe, jps, jpe, kps, kpe,            &
3319                                      grid%i_start(ij), grid%i_end(ij),                  &
3320                                      grid%j_start(ij), grid%j_end(ij),                  &
3321                                      k_start    , k_end-1                    )
3323            END DO scalar_species_bdy_loop_1
3325          END IF
3327          IF (config_flags%km_opt .eq. 2) THEN
3329            CALL set_physical_bc3d( grid%tke_2 , 'p', config_flags,  &
3330                                    ids, ide, jds, jde, kds, kde,            &
3331                                    ims, ime, jms, jme, kms, kme,            &
3332                                    ips, ipe, jps, jpe, kps, kpe,            &
3333                                    grid%i_start(ij), grid%i_end(ij),        &
3334                                    grid%j_start(ij), grid%j_end(ij),        &
3335                                    k_start    , k_end                      )
3336          END IF
3338        END DO tile_bc_loop_1
3339        !$OMP END PARALLEL DO
3340 BENCH_END(bc_end_tim)
3343 #ifdef DM_PARALLEL
3345 !                           * * * * *
3346 !         *        * * *    * * * * *
3347 !       * + *      * + *    * * + * *
3348 !         *        * * *    * * * * *
3349 !                           * * * * *
3351 ! moist, chem, scalar, tke      x
3354        IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
3355          IF ( (config_flags%tke_adv_opt /= ORIGINAL .and. config_flags%tke_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3356 #         include "HALO_EM_TKE_5.inc"
3357          ELSE
3358 #         include "HALO_EM_TKE_3.inc"
3359          ENDIF
3360        ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
3361          IF ( (config_flags%tke_adv_opt /= ORIGINAL .and. config_flags%tke_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3362 #         include "HALO_EM_TKE_7.inc"
3363          ELSE
3364 #         include "HALO_EM_TKE_5.inc"
3365          ENDIF
3366        ELSE
3367          WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
3368          CALL wrf_error_fatal(TRIM(wrf_err_message))
3369        ENDIF
3371        IF ( num_moist .GE. PARAM_FIRST_SCALAR ) THEN
3372          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
3373            IF ( (config_flags%moist_adv_opt /= ORIGINAL .and. config_flags%moist_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3374 #        include "HALO_EM_MOIST_E_5.inc"
3375            ELSE
3376 #        include "HALO_EM_MOIST_E_3.inc"
3377            END IF
3378          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
3379            IF ( (config_flags%moist_adv_opt /= ORIGINAL .and. config_flags%moist_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3380 #        include "HALO_EM_MOIST_E_7.inc"
3381            ELSE
3382 #        include "HALO_EM_MOIST_E_5.inc"
3383            END IF
3384          ELSE
3385            WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
3386            CALL wrf_error_fatal(TRIM(wrf_err_message))
3387          ENDIF
3388        ENDIF
3389        IF ( num_chem >= PARAM_FIRST_SCALAR ) THEN
3390          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
3391            IF ( (config_flags%chem_adv_opt /= ORIGINAL .and. config_flags%chem_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3392 #        include "HALO_EM_CHEM_E_5.inc"
3393            ELSE
3394 #        include "HALO_EM_CHEM_E_3.inc"
3395            ENDIF
3396          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
3397            IF ( (config_flags%chem_adv_opt /= ORIGINAL .and. config_flags%chem_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3398 #        include "HALO_EM_CHEM_E_7.inc"
3399            ELSE
3400 #        include "HALO_EM_CHEM_E_5.inc"
3401            ENDIF
3402          ELSE
3403            WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
3404            CALL wrf_error_fatal(TRIM(wrf_err_message))
3405          ENDIF
3406        ENDIF
3407        IF ( num_tracer >= PARAM_FIRST_SCALAR ) THEN
3408          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
3409            IF ( (config_flags%tracer_adv_opt /= ORIGINAL .and. config_flags%tracer_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3410 #        include "HALO_EM_TRACER_E_5.inc"
3411            ELSE
3412 #        include "HALO_EM_TRACER_E_3.inc"
3413            ENDIF
3414          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
3415            IF ( (config_flags%tracer_adv_opt /= ORIGINAL .and. config_flags%tracer_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3416 #        include "HALO_EM_TRACER_E_7.inc"
3417            ELSE
3418 #        include "HALO_EM_TRACER_E_5.inc"
3419            ENDIF
3420          ELSE
3421            WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
3422            CALL wrf_error_fatal(TRIM(wrf_err_message))
3423          ENDIF
3424        ENDIF
3425        IF ( num_scalar >= PARAM_FIRST_SCALAR ) THEN
3426          IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
3427            IF ( (config_flags%scalar_adv_opt /= ORIGINAL .and. config_flags%scalar_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3428 #        include "HALO_EM_SCALAR_E_5.inc"
3429            ELSE
3430 #        include "HALO_EM_SCALAR_E_3.inc"
3431            ENDIF
3432          ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
3433            IF ( (config_flags%scalar_adv_opt /= ORIGINAL .and. config_flags%scalar_adv_opt /= WENO_SCALAR) .and. (rk_step == rk_order-1) ) THEN
3434 #        include "HALO_EM_SCALAR_E_7.inc"
3435            ELSE
3436 #        include "HALO_EM_SCALAR_E_5.inc"
3437            ENDIF
3438          ELSE
3439            WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
3440            CALL wrf_error_fatal(TRIM(wrf_err_message))
3441          ENDIF
3442        ENDIF
3443 #endif
3445      ENDIF rk_step_1_check
3448 !**********************************************************
3450 !  end of RK predictor-corrector loop
3452 !**********************************************************
3454    END DO Runge_Kutta_loop
3455 !  grid%dmudt=grid%mu_2 - grid%mu_1
3457 #if ( WRFPLUS != 1 )
3458    IF      ( config_flags%traj_opt .EQ. UM_TRAJECTORY ) THEN
3459 #ifdef DM_PARALLEL
3460 #    include "HALO_EM_F_1.inc"
3461 #    include "HALO_EM_D.inc"
3462 #    include "HALO_EM_INIT_4.inc"
3463      IF( config_flags%periodic_x ) THEN
3464 #    include "PERIOD_EM_DA.inc"
3465 #    include "PERIOD_EM_F.inc"
3466 #    include "PERIOD_EM_G.inc"
3467      ENDIF
3468 #endif
3469      !$OMP PARALLEL DO   &
3470      !$OMP PRIVATE ( ij )
3471        DO ij = 1 , grid%num_tiles
3473            call trajectory (grid,config_flags,                                     &
3474                             grid%dt,grid%itimestep,grid%ru_m, grid%rv_m, grid%ww_m,&
3475                             grid%muts,grid%muus,grid%muvs,                         &
3476                             grid%c1h, grid%c2h, grid%c1f, grid%c2f,                &
3477                             grid%rdx, grid%rdy, grid%rdn, grid%rdnw,grid%rdzw,     &
3478                             grid%traj_i,grid%traj_j,grid%traj_k,                   &
3479                             grid%traj_long,grid%traj_lat,                          &
3480                             grid%xlong,grid%xlat,                                  &
3481                             grid%msftx,grid%msfux,grid%msfvy,                      &
3482                             ids, ide, jds, jde, kds, kde,                          &
3483                             ims, ime, jms, jme, kms, kme,                          &
3484                             grid%i_start(ij), grid%i_end(ij),                      &
3485                             grid%j_start(ij), grid%j_end(ij),                      &
3486                             k_start  , k_end                                       )
3487         ENDDO
3488      !$OMP END PARALLEL DO
3489    ENDIF
3490 #endif
3491 !-----------------------------------------------------------
3493    IF (config_flags%do_avgflx_em .EQ. 1) THEN
3494 ! Reinitialize time-averaged fluxes if history output was written after the previous time step:
3495       CALL WRFU_ALARMGET(grid%alarms( HISTORY_ALARM ),prevringtime=temp_time)
3496       CALL domain_clock_get ( grid, current_time=CurrTime, &
3497            current_timestr=message2 )
3498 ! use overloaded -, .LT. operator to check whether to initialize avgflx:
3499 ! reinitialize after each history output (detect this here by comparing current time
3500 ! against last history time and time step - this code follows what's done in adapt_timestep_em):
3501       WRITE ( message , FMT = '("solve_em: old_dt =",g15.6,", dt=",g15.6," on domain ",I3)' ) &
3502            & old_dt,grid%dt,grid%id
3503       CALL wrf_debug(200,message)
3504       old_dt=min(old_dt,grid%dt)
3505       num = INT(old_dt * precision)
3506       den = precision
3507       CALL WRFU_TimeIntervalSet(dtInterval, Sn=num, Sd=den)
3508       IF (CurrTime .lt. temp_time + dtInterval) THEN
3509          WRITE ( message , FMT = '("solve_em: initializing avgflx at time ",A," on domain ",I3)' ) &
3510               & TRIM(message2), grid%id
3511          CALL wrf_message(trim(message)) 
3512          grid%avgflx_count = 0
3513 !tile-loop for zero_avgflx
3514    !$OMP PARALLEL DO   &
3515    !$OMP PRIVATE ( ij )
3517          DO ij = 1 , grid%num_tiles
3518             CALL wrf_debug(200,'In solve_em, before zero_avgflx call')
3519             CALL zero_avgflx(grid%avgflx_rum,grid%avgflx_rvm,grid%avgflx_wwm, &
3520                  &   ids, ide, jds, jde, kds, kde,           &
3521                  &   ims, ime, jms, jme, kms, kme,           &
3522                  &   grid%i_start(ij), grid%i_end(ij), grid%j_start(ij), grid%j_end(ij), &
3523                  &   k_start    , k_end, f_flux, &
3524                  &   grid%avgflx_cfu1,grid%avgflx_cfd1,grid%avgflx_dfu1, &
3525                  &   grid%avgflx_efu1,grid%avgflx_dfd1,grid%avgflx_efd1 )
3526             CALL wrf_debug(200,'In solve_em, after zero_avgflx call')
3527          ENDDO
3528          !$OMP END PARALLEL DO
3529       ENDIF
3531 ! Update avgflx quantities
3532 !tile-loop for upd_avgflx
3533    !$OMP PARALLEL DO   &
3534    !$OMP PRIVATE ( ij )
3536       DO ij = 1 , grid%num_tiles
3537          CALL wrf_debug(200,'In solve_em, before upd_avgflx call')
3538          CALL upd_avgflx(grid%avgflx_count,grid%avgflx_rum,grid%avgflx_rvm,grid%avgflx_wwm, &
3539               &   grid%ru_m, grid%rv_m, grid%ww_m, &
3540               &   ids, ide, jds, jde, kds, kde,           &
3541               &   ims, ime, jms, jme, kms, kme,           &
3542               &   grid%i_start(ij), grid%i_end(ij), grid%j_start(ij), grid%j_end(ij), &
3543               &   k_start    , k_end, f_flux, &
3544               &   grid%cfu1,grid%cfd1,grid%dfu1,grid%efu1,grid%dfd1,grid%efd1,          &
3545               &   grid%avgflx_cfu1,grid%avgflx_cfd1,grid%avgflx_dfu1, &
3546               &   grid%avgflx_efu1,grid%avgflx_dfd1,grid%avgflx_efd1 )
3547          CALL wrf_debug(200,'In solve_em, after upd_avgflx call')
3548          
3549       ENDDO
3550       !$OMP END PARALLEL DO
3551       grid%avgflx_count = grid%avgflx_count + 1
3552    ENDIF
3554    !$OMP PARALLEL DO   &
3555    !$OMP PRIVATE ( ij )
3556    DO ij = 1 , grid%num_tiles
3558 BENCH_START(advance_ppt_tim)
3559      CALL wrf_debug ( 200 , ' call advance_ppt' )
3560      CALL advance_ppt(grid%rthcuten,grid%rqvcuten,grid%rqccuten,grid%rqrcuten, &
3561                       grid%cldfra_cup,                                         & !BSINGH -  Added for CuP scheme
3562                       grid%rqicuten,grid%rqscuten,           &
3563                       grid%rainc,grid%raincv,grid%rainsh,grid%pratec,grid%pratesh, &
3564                       grid%nca,grid%htop,grid%hbot,grid%cutop,grid%cubot,  &
3565                       grid%cuppt, grid%dt, config_flags,                   &
3566                       ids,ide, jds,jde, kds,kde,             &
3567                       ims,ime, jms,jme, kms,kme,             &
3568                       grid%i_start(ij), grid%i_end(ij),      &
3569                       grid%j_start(ij), grid%j_end(ij),      &
3570                       k_start    , k_end                    )
3571 BENCH_END(advance_ppt_tim)
3573    ENDDO
3574   !$OMP END PARALLEL DO
3576    !$OMP PARALLEL DO   &
3577    !$OMP PRIVATE ( ij )
3578    DO ij = 1 , grid%num_tiles
3579         CALL wrf_debug ( 200 , ' call phy_prep_part2' )
3580         CALL phy_prep_part2 ( config_flags,                              &
3581                         grid%muts, grid%muus, grid%muvs,                 &
3582                         grid%c1h, grid%c2h, grid%c1f, grid%c2f,          &
3583                         grid%rthraten,                                   &
3584                         grid%rthblten, grid%rublten, grid%rvblten,       &
3585                         grid%rqvblten, grid%rqcblten, grid%rqiblten,     &
3586                         grid%rucuten,  grid%rvcuten,  grid%rthcuten,     &
3587                         grid%rqvcuten, grid%rqccuten, grid%rqrcuten,     &
3588                         grid%rqicuten, grid%rqscuten,                    &
3589                         grid%rushten,  grid%rvshten,  grid%rthshten,     &
3590                         grid%rqvshten, grid%rqcshten, grid%rqrshten,     &
3591                         grid%rqishten, grid%rqsshten, grid%rqgshten,     &
3592                         grid%rthften,  grid%rqvften,                     &
3593                         grid%RUNDGDTEN, grid%RVNDGDTEN, grid%RTHNDGDTEN, &
3594                         grid%RPHNDGDTEN,grid%RQVNDGDTEN, grid%RMUNDGDTEN,&
3595                         grid%t_2, th_phy, moist(ims,kms,jms,P_QV),       &
3596                         ids, ide, jds, jde, kds, kde,                    &
3597                         ims, ime, jms, jme, kms, kme,                    &
3598                         grid%i_start(ij), grid%i_end(ij),                &
3599                         grid%j_start(ij), grid%j_end(ij),                &
3600                         k_start, k_end                                   )
3601    ENDDO
3602   !$OMP END PARALLEL DO
3604 !<DESCRIPTION>
3605 !<pre>
3606 ! (5) time-split physics.
3608 !     Microphysics are the only time  split physics in the WRF model 
3609 !     at this time.  Split-physics begins with the calculation of
3610 !     needed diagnostic quantities (pressure, temperature, etc.)
3611 !     followed by a call to the microphysics driver, 
3612 !     and finishes with a clean-up, storing off of a diabatic tendency
3613 !     from the moist physics, and a re-calulation of the  diagnostic
3614 !     quantities pressure and density.
3615 !</pre>
3616 !</DESCRIPTION>
3618    IF( config_flags%specified .or. config_flags%nested ) THEN
3619      sz = grid%spec_zone
3620    ELSE
3621      sz = 0
3622    ENDIF
3624    IF (config_flags%mp_physics /= 0)  then
3626      !$OMP PARALLEL DO   &
3627      !$OMP PRIVATE ( ij, its, ite, jts, jte )
3629      scalar_tile_loop_1a: DO ij = 1 , grid%num_tiles
3631        IF ( config_flags%periodic_x ) THEN
3632          its = max(grid%i_start(ij),ids)
3633          ite = min(grid%i_end(ij),ide-1)
3634        ELSE
3635          its = max(grid%i_start(ij),ids+sz)
3636          ite = min(grid%i_end(ij),ide-1-sz)
3637        ENDIF
3638        jts = max(grid%j_start(ij),jds+sz)
3639        jte = min(grid%j_end(ij),jde-1-sz)
3641        if (config_flags%madwrf_opt == 2) then
3642          CALL wrf_debug ( 200 , ' call cloud_tracer_nudge' )
3644          CALL cloud_tracer_nudge(  dtm, config_flags%madwrf_dt_relax, &
3645                                    config_flags%madwrf_dt_nudge,     &
3646                                    grid%xtime,                       &
3647                                    moist(ims,kms,jms,P_QC),          &
3648                                    moist(ims,kms,jms,P_QI),          &
3649                                    moist(ims,kms,jms,P_QS),          &
3650                                    tracer(ims,kms,jms,P_tr_qc),      &
3651                                    tracer(ims,kms,jms,P_tr_qi),      &
3652                                    tracer(ims,kms,jms,P_tr_qs),      &
3653                                    ids, ide, jds, jde, kds, kde,     &
3654                                    ims, ime, jms, jme, kms, kme,     &
3655                                    its, ite, jts, jte,               &
3656                                    k_start    , k_end                )
3657        end if
3659        CALL wrf_debug ( 200 , ' call moist_physics_prep' )
3660 BENCH_START(moist_physics_prep_tim)
3661        CALL moist_physics_prep_em( grid%t_2, grid%t_1, t0, grid%rho,           &
3662                                    grid%al, grid%alb, grid%p, p8w, p0, grid%pb,          &
3663                                    grid%ph_2, grid%phb, th_phy, pi_phy , p_phy, &
3664                                    grid%z, grid%z_at_w, dz8w,        &
3665                                    dtm, grid%h_diabatic,                       &
3666                                    moist(ims,kms,jms,P_QV),grid%qv_diabatic,   &
3667                                    moist(ims,kms,jms,P_QC),grid%qc_diabatic,   &
3668                                    config_flags,grid%fnm, grid%fnp,            &
3669                                    ids, ide, jds, jde, kds, kde,     &
3670                                    ims, ime, jms, jme, kms, kme,     &
3671                                    its, ite, jts, jte,               &
3672                                    k_start    , k_end               )
3674        IF (config_flags%dust_emis.eq.1 .AND. config_flags%mp_physics.eq.thompsonaero)  then
3675          CALL wrf_debug ( 200 , ' call bulk_dust_emis' )
3676          CALL bulk_dust_emis (grid%itimestep,dtm,config_flags%num_soil_layers  &
3677                ,grid%u_phy,grid%v_phy,grid%rho,grid%alt                        &
3678                ,grid%u10,grid%v10,p8w,dz8w,grid%smois,grid%erod                &
3679                ,grid%ivgtyp,grid%isltyp,grid%vegfra,grid%albbck,grid%xland     &
3680                ,grid%dx, g, grid%qnifa2d, ids,ide, jds,jde, kds,kde            &
3681                ,ims,ime, jms,jme, kms,kme                                      &
3682                ,its,ite, jts,jte, k_start,k_end )
3683        ENDIF
3685 BENCH_END(moist_physics_prep_tim)
3686      END DO scalar_tile_loop_1a
3687      !$OMP END PARALLEL DO
3689      CALL wrf_debug ( 200 , ' call microphysics_driver' )
3691      grid%sr = 0.
3692      specified_bdy = config_flags%specified .OR. config_flags%nested
3693      channel_bdy = config_flags%specified .AND. config_flags%periodic_x
3695 BENCH_START(micro_driver_tim)
3698 ! WRFU_AlarmIsRinging always returned false, so using an alternate  method to find out if it is time 
3699 ! to dump history/restart files so microphysics can be told to calculate things like radar reflectivity.
3701 !     diagflag = .false.
3702 !     CALL WRFU_ALARMGET(grid%alarms( HISTORY_ALARM ),prevringtime=temp_time,RingInterval=intervaltime)
3703 !     CALL WRFU_ALARMGET(grid%alarms( RESTART_ALARM ),prevringtime=restart_time,RingInterval=restartinterval)
3704 !     CALL domain_clock_get ( grid, current_time=CurrTime )
3705 !     old_dt=min(old_dt,grid%dt)
3706 !     num = INT(old_dt * precision)
3707 !     den = precision
3708 !     CALL WRFU_TimeIntervalSet(dtInterval, Sn=num, Sd=den)
3709 !     IF (CurrTime .ge. temp_time + intervaltime - dtInterval .or. &
3710 !         CurrTime .ge. restart_time + restartinterval - dtInterval ) THEN
3711 !       diagflag = .true.
3712 !     ENDIF
3713 !     WRITE(wrf_err_message,*)'diag_flag=',diag_flag
3714 !     CALL wrf_debug ( 0 , wrf_err_message )
3715 #ifdef DM_PARALLEL
3716 #      include "HALO_EM_SBM.inc"
3717 #endif
3720      CALL microphysics_driver(                                            &
3721       &         DT=dtm             ,DX=grid%dx              ,DY=grid%dy   &
3722       &        ,DZ8W=dz8w          ,F_ICE_PHY=grid%f_ice_phy              &
3723       &        ,ITIMESTEP=grid%itimestep             ,LOWLYR=grid%lowlyr  &
3724       &        ,P8W=p8w            ,P=p_phy            ,PI_PHY=pi_phy     &
3725       &        ,RHO=grid%rho       ,SPEC_ZONE=grid%spec_zone              &
3726       &        ,SR=grid%sr              ,TH=th_phy                        &
3727       &        ,refl_10cm=grid%refl_10cm                                  & ! hm, 9/22/09 for refl
3728       &        ,vmi3d=grid%vmi3d                                          & ! for P3
3729       &        ,di3d=grid%di3d                                            & ! for P3
3730       &        ,rhopo3d=grid%rhopo3d                                      & ! for P3
3731       &        ,phii3d=grid%phii3d                                        & ! for Jensen ISHMAEL
3732       &        ,vmi3d_2=grid%vmi3d_2                                      & ! for P3
3733       &        ,di3d_2=grid%di3d_2                                        & ! for P3
3734       &        ,rhopo3d_2=grid%rhopo3d_2                                  & ! for P3
3735       &        ,phii3d_2=grid%phii3d_2                                    & ! for Jensen ISHMAEL
3736       &        ,vmi3d_3=grid%vmi3d_3                                      & ! for Jensen ISHMAEL
3737       &        ,di3d_3=grid%di3d_3                                        & ! for Jensen ISHMAEL
3738       &        ,rhopo3d_3=grid%rhopo3d_3                                  & ! for Jensen ISHMAEL
3739       &        ,phii3d_3=grid%phii3d_3                                    & ! for Jensen ISHMAEL
3740       &        ,itype=grid%itype                                          & ! for Jensen ISHMAEL
3741       &        ,itype_2=grid%itype_2                                      & ! for Jensen ISHMAEL
3742       &        ,itype_3=grid%itype_3                                      & ! for Jensen ISHMAEL
3743       &        ,WARM_RAIN=grid%warm_rain                                  &
3744       &        ,T8W=t8w                                                   &
3745       &        ,CLDFRA=grid%cldfra, EXCH_H=grid%exch_h &
3746       &        ,NSOURCE=grid%qndropsource                                 &
3747 #if (WRF_CHEM == 1)
3748       &        ,QLSINK=grid%qlsink,CLDFRA_OLD=grid%cldfra_old             &
3749       &        ,PRECR=grid%precr, PRECI=grid%preci, PRECS=grid%precs, PRECG=grid%precg &
3750       &        ,CHEM_OPT=config_flags%chem_opt, PROGN=config_flags%progn  &
3751 !======================
3752       ! Variables required for CAMMGMP Scheme when run with WRF_CHEM
3753       &        ,CHEM=chem                                                 &
3754       &        ,QME3D=grid%qme3d,PRAIN3D=grid%prain3d                     &
3755       &        ,NEVAPR3D=grid%nevapr3d                                    &
3756       &        ,RATE1ORD_CW2PR_ST3D=grid%rate1ord_cw2pr_st3d              &
3757       &        ,DGNUM4D=grid%dgnum4d,DGNUMWET4D=grid%dgnumwet4d           &
3758 !======================
3759 #endif
3760       &        ,XLAND=grid%xland,SNOWH=grid%SNOW                           &  !PMA
3761       &        ,SPECIFIED=specified_bdy, CHANNEL_SWITCH=channel_bdy       &
3762       &        ,F_RAIN_PHY=grid%f_rain_phy                                &
3763       &        ,F_RIMEF_PHY=grid%f_rimef_phy                              &
3764       &        ,MP_PHYSICS=config_flags%mp_physics                        &
3765       &        ,ID=grid%id                                                &
3766       &        ,IDS=ids,IDE=ide, JDS=jds,JDE=jde, KDS=kds,KDE=kde         &
3767       &        ,IMS=ims,IME=ime, JMS=jms,JME=jme, KMS=kms,KME=kme         &
3768       &        ,IPS=ips,IPE=ipe, JPS=jps,JPE=jpe, KPS=kps,KPE=kpe         &
3769       &        ,I_START=grid%i_start,I_END=min(grid%i_end, ide-1)         &
3770       &        ,J_START=grid%j_start,J_END=min(grid%j_end, jde-1)         &
3771       &        ,KTS=k_start, KTE=min(k_end,kde-1)                         &
3772       &        ,NUM_TILES=grid%num_tiles                                  &
3773       &        ,NAER=grid%naer                                            &
3774 !===================== IRRIGATION =========================
3775       &        ,IRRIGATION=grid%irrigation                                &
3776       &        ,SF_SURF_IRR_SCHEME=config_flags%sf_surf_irr_scheme        &
3777       &        ,IRR_DAILY_AMOUNT=config_flags%irr_daily_amount            & 
3778       &        ,IRR_START_HOUR=config_flags%irr_start_hour                &
3779       &        ,IRR_NUM_HOURS=config_flags%irr_num_hours                  &
3780       &        ,JULIAN_IN=grid%julian                                     &
3781       &        ,IRR_START_JULIANDAY=config_flags%irr_start_julianday      &
3782       &        ,IRR_END_JULIANDAY=config_flags%irr_end_julianday          &
3783       &        ,IRR_FREQ=config_flags%irr_freq,IRR_PH=config_flags%irr_ph &
3784       &        ,IRR_RAND_FIELD=grid%irr_rand_field                        &
3785       &        ,GMT=grid%gmt,XTIME=grid%xtime                             & 
3786 !======================
3787       ! Variables required for CAMMGMP Scheme
3788       &        ,DLF=grid%dlf,DLF2=grid%dlf2,T_PHY=grid%t_phy,P_HYD=grid%p_hyd  &
3789       &        ,P8W_HYD=grid%p_hyd_w,TKE_PBL=grid%tke_pbl,PBLH=grid%PBLH  &
3790       &        ,Z_AT_MASS=grid%z,Z_AT_W=grid%z_at_w                       &
3791       &        ,QFX=grid%qfx,RLIQ=grid%rliq                               &
3792       &        ,TURBTYPE3D=grid%turbtype3d,SMAW3D=grid%smaw3d             &
3793       &        ,WSEDL3D=grid%wsedl3d,CLDFRA_OLD_MP=grid%cldfra_old_mp     &
3794       &        ,CLDFRA_MP=grid%cldfra_mp,CLDFRA_MP_ALL=grid%cldfra_mp_ALL &
3795       &        ,LRADIUS=grid%LRADIUS, IRADIUS=grid%IRADIUS                & !BSINGH(01/20/2014): Added for RRTMG<->CAMMGMP
3796       &        ,CLDFRAI=grid%cldfrai                                      &
3797       &        ,CLDFRAL=grid%cldfral,CLDFRA_CONV=grid%CLDFRA_CONV         &
3798       &        ,ALT=grid%alt                                              &
3799       &        ,ACCUM_MODE=config_flags%accum_mode                        &
3800       &        ,AITKEN_MODE=config_flags%aitken_mode                      &
3801       &        ,COARSE_MODE=config_flags%coarse_mode                      &
3802       &        ,ICWMRSH3D=grid%icwmrsh,ICWMRDP3D=grid%icwmrdp3d           &
3803       &        ,SHFRC3D=grid%shfrc3d,CMFMC3D=grid%cmfmc                   &
3804       &        ,CMFMC2_3D=grid%cmfmc2,CONFIG_FLAGS=config_flags           &
3805       &        ,FNM=grid%fnm,FNP=grid%fnp,RH_OLD_MP=grid%rh_old_mp        &
3806       &        ,LCD_OLD_MP=grid%lcd_old_mp                                &
3807 !======================
3808                  ! Optional
3809       &        , RAINNC=grid%rainnc, RAINNCV=grid%rainncv                 &
3810       &        , SNOWNC=grid%snownc, SNOWNCV=grid%snowncv                 &
3811       &        , GRAUPELNC=grid%graupelnc, GRAUPELNCV=grid%graupelncv     & ! for milbrandt2mom
3812       &        , HAILNC=grid%hailnc, HAILNCV=grid%hailncv                 &
3813       &        , W=grid%w_2, Z=grid%z, HT=grid%ht                         &
3814       &        , MP_RESTART_STATE=grid%mp_restart_state                   &
3815       &        , TBPVS_STATE=grid%tbpvs_state                             & ! etampnew
3816       &        , TBPVS0_STATE=grid%tbpvs0_state                           & ! etampnew
3817       &        , QV_CURR=moist(ims,kms,jms,P_QV), F_QV=F_QV               &
3818       &        , QC_CURR=moist(ims,kms,jms,P_QC), F_QC=F_QC               &
3819       &        , QR_CURR=moist(ims,kms,jms,P_QR), F_QR=F_QR               &
3820       &        , QI_CURR=moist(ims,kms,jms,P_QI), F_QI=F_QI               &
3821       &        , QS_CURR=moist(ims,kms,jms,P_QS), F_QS=F_QS               &
3822       &        , QG_CURR=moist(ims,kms,jms,P_QG), F_QG=F_QG               &
3823       &        , QH_CURR=moist(ims,kms,jms,P_QH), F_QH=F_QH               & ! for milbrandt2mom
3824       &        , QIC_CURR=moist(ims,kms,jms,P_QIC), F_QIC=F_QIC               &
3825       &        , QIP_CURR=moist(ims,kms,jms,P_QIP), F_QIP=F_QIP               &
3826       &        , QID_CURR=moist(ims,kms,jms,P_QID), F_QID=F_QID               &
3827       &        , QNDROP_CURR=scalar(ims,kms,jms,P_QNDROP), F_QNDROP=F_QNDROP &
3828 #if (WRF_CHEM == 1)
3829       &        , RAINPROD=wetscav_frcing(ims,kms,jms,p_rainprod)          &
3830       &        , EVAPPROD=wetscav_frcing(ims,kms,jms,p_evapprod)          &
3831       &        , QV_B4MP=grid%qv_b4mp,QC_B4MP=grid%qc_b4mp                &
3832       &        , QI_B4MP=grid%qi_b4mp, QS_B4MP=grid%qs_b4mp               &
3833 #endif
3834       &        , QT_CURR=scalar(ims,kms,jms,P_QT), F_QT=F_QT              &
3835       &        , QNN_CURR=scalar(ims,kms,jms,P_QNN), F_QNN=F_QNN          &
3836       &        , QNI_CURR=scalar(ims,kms,jms,P_QNI), F_QNI=F_QNI          &
3837       &        , QNC_CURR=scalar(ims,kms,jms,P_QNC), F_QNC=F_QNC          &
3838       &        , QNR_CURR=scalar(ims,kms,jms,P_QNR), F_QNR=F_QNR          &
3839       &        , QNS_CURR=scalar(ims,kms,jms,P_QNS), F_QNS=F_QNS          &
3840       &        , QNG_CURR=scalar(ims,kms,jms,P_QNG), F_QNG=F_QNG          &
3841       &        , QNWFA_CURR=scalar(ims,kms,jms,P_QNWFA), F_QNWFA=F_QNWFA  & ! for Thompson water-friendly aerosol
3842       &        , QNIFA_CURR=scalar(ims,kms,jms,P_QNIFA), F_QNIFA=F_QNIFA  & ! for Thompson ice-friendly aerosol
3843       &        , QNBCA_CURR=scalar(ims,kms,jms,P_QNBCA), F_QNBCA=F_QNBCA  & ! for Thompson black carbon aerosol
3844       &        , QNH_CURR=scalar(ims,kms,jms,P_QNH), F_QNH=F_QNH          & ! for milbrandt2mom and nssl_2mom
3845       &        , QNIC_CURR=scalar(ims,kms,jms,P_QNIC), F_QNIC=F_QNIC          &
3846       &        , QNIP_CURR=scalar(ims,kms,jms,P_QNIP), F_QNIP=F_QNIP          &
3847       &        , QNID_CURR=scalar(ims,kms,jms,P_QNID), F_QNID=F_QNID          &
3848       &        , QIR_CURR=scalar(ims,kms,jms,P_QIR), F_QIR=F_QIR          & ! for P3
3849       &        , QIB_CURR=scalar(ims,kms,jms,P_QIB), F_QIB=F_QIB          & ! for P3
3850       &        , QVOLI_CURR=scalar(ims,kms,jms,P_QVOLI), F_QVOLI=F_QVOLI  & ! for Jensen ISHMAEL
3851       &        , QAOLI_CURR=scalar(ims,kms,jms,P_QAOLI), F_QAOLI=F_QAOLI  & ! for Jensen ISHMAEL
3852       &        , QI2_CURR=moist(ims,kms,jms,P_QI2), F_QI2=F_QI2           & ! for P3
3853       &        , QNI2_CURR=scalar(ims,kms,jms,P_QNI2), F_QNI2=F_QNI2      & ! for P3
3854       &        , QIR2_CURR=scalar(ims,kms,jms,P_QIR2), F_QIR2=F_QIR2      & ! for P3
3855       &        , QIB2_CURR=scalar(ims,kms,jms,P_QIB2), F_QIB2=F_QIB2      & ! for P3
3856       &        , QVOLI2_CURR=scalar(ims,kms,jms,P_QVOLI2), F_QVOLI2=F_QVOLI2  & ! for Jensen ISHMAEL
3857       &        , QAOLI2_CURR=scalar(ims,kms,jms,P_QAOLI2), F_QAOLI2=F_QAOLI2  & ! for Jensen ISHMAEL
3858       &        , QI3_CURR=moist(ims,kms,jms,P_QI3), F_QI3=F_QI3           & ! for Jensen ISHMAEL
3859       &        , QNI3_CURR=scalar(ims,kms,jms,P_QNI3), F_QNI3=F_QNI3      & ! for Jensen ISHMAEL
3860       &        , QVOLI3_CURR=scalar(ims,kms,jms,P_QVOLI3), F_QVOLI3=F_QVOLI3  & ! for Jensen ISHMAEL
3861       &        , QAOLI3_CURR=scalar(ims,kms,jms,P_QAOLI3), F_QAOLI3=F_QAOLI3  & ! for Jensen ISHMAEL
3862 !       &        , QZR_CURR=scalar(ims,kms,jms,P_QZR), F_QZR=F_QZR          & ! for milbrandt3mom
3863        &        , QZI_CURR=scalar(ims,kms,jms,P_QZI), F_QZI=F_QZI          & ! for 3-moment P3
3864 !       &        , QZS_CURR=scalar(ims,kms,jms,P_QZS), F_QZS=F_QZS          & ! "
3865 !       &        , QZG_CURR=scalar(ims,kms,jms,P_QZG), F_QZG=F_QZG          & ! "
3866 !       &        , QZH_CURR=scalar(ims,kms,jms,P_QZH), F_QZH=F_QZH          & ! "
3867       &        , QVOLG_CURR=scalar(ims,kms,jms,P_QVOLG), F_QVOLG=F_QVOLG    & ! for nssl_2mom
3868       &        , QVOLH_CURR=scalar(ims,kms,jms,P_QVOLH), F_QVOLH=F_QVOLH    & ! for nssl_2mom
3869       &        , QDCN_CURR=scalar(ims,kms,jms,P_QDCN), F_QDCN=F_QDCN      & ! for ntu3m
3870       &        , QTCN_CURR=scalar(ims,kms,jms,P_QTCN), F_QTCN=F_QTCN      & ! for ntu3m
3871       &        , QCCN_CURR=scalar(ims,kms,jms,P_QCCN), F_QCCN=F_QCCN      & ! for ntu3m
3872       &        , QRCN_CURR=scalar(ims,kms,jms,P_QRCN), F_QRCN=F_QRCN      & ! for ntu3m
3873       &        , QNIN_CURR=scalar(ims,kms,jms,P_QNIN), F_QNIN=F_QNIN      & ! for ntu3m
3874       &        , FI_CURR=scalar(ims,kms,jms,P_FI), F_FI=F_FI              & ! for ntu3m
3875       &        , FS_CURR=scalar(ims,kms,jms,P_FS), F_FS=F_FS              & ! for ntu3m
3876       &        , VI_CURR=scalar(ims,kms,jms,P_VI), F_VI=F_VI              & ! for ntu3m
3877       &        , VS_CURR=scalar(ims,kms,jms,P_VS), F_VS=F_VS              & ! for ntu3m
3878       &        , VG_CURR=scalar(ims,kms,jms,P_VG), F_VG=F_VG              & ! for ntu3m
3879       &        , AI_CURR=scalar(ims,kms,jms,P_AI), F_AI=F_AI              & ! for ntu3m
3880       &        , AS_CURR=scalar(ims,kms,jms,P_AS), F_AS=F_AS              & ! for ntu3m
3881       &        , AG_CURR=scalar(ims,kms,jms,P_AG), F_AG=F_AG              & ! for ntu3m
3882       &        , AH_CURR=scalar(ims,kms,jms,P_AH), F_AH=F_AH              & ! for ntu3m
3883       &        , I3M_CURR=scalar(ims,kms,jms,P_I3M), F_I3M=F_I3m          & ! for ntu3m
3884       &        , cu_used=config_flags%cu_used                             &
3885       &        , qrcuten=grid%rqrcuten, qscuten=grid%rqscuten             &
3886       &        , qicuten=grid%rqicuten, qccuten=grid%rqccuten             &
3887       &        , HAIL=config_flags%gsfcgce_hail                           & ! for gsfcgce
3888       &        , ICE2=config_flags%gsfcgce_2ice                           & ! for gsfcgce
3889       &        , PHYS_TOT=grid%phys_tot                                   & ! for gsfcgce
3890       &        , PHYSC=grid%physc                                         & ! for gsfcgce
3891       &        , PHYSE=grid%physe                                         & ! for gsfcgce
3892       &        , PHYSD=grid%physd                                         & ! for gsfcgce
3893       &        , PHYSS=grid%physs                                         & ! for gsfcgce
3894       &        , PHYSM=grid%physm                                         & ! for gsfcgce
3895       &        , PHYSF=grid%physf                                         & ! for gsfcgce
3897       &        , ACPHYS_TOT=grid%acphys_tot                               & ! for gsfcgce
3898       &        , ACPHYSC=grid%acphysc                                     & ! for gsfcgce
3899       &        , ACPHYSE=grid%acphyse                                     & ! for gsfcgce
3900       &        , ACPHYSD=grid%acphysd                                     & ! for gsfcgce
3901       &        , ACPHYSS=grid%acphyss                                     & ! for gsfcgce
3902       &        , ACPHYSM=grid%acphysm                                     & ! for gsfcgce
3903       &        , ACPHYSF=grid%acphysf                                     & ! for gsfcgce
3905       &        , RE_CLOUD_GSFC=grid%re_cloud_gsfc                         & ! for gsfcgce
3906       &        , RE_RAIN_GSFC=grid%re_rain_gsfc                           & ! for gsfcgce
3907       &        , RE_ICE_GSFC=grid%re_ice_gsfc                             & ! for gsfcgce
3908       &        , RE_SNOW_GSFC=grid%re_snow_gsfc                           & ! for gsfcgce
3909       &        , RE_GRAUPEL_GSFC=grid%re_graupel_gsfc                     & ! for gsfcgce
3910       &        , RE_HAIL_GSFC=grid%re_hail_gsfc                           & ! for gsfcgce
3911       &        , PRECR3D=grid%precr3d, PRECI3D=grid%preci3d, PRECS3D=grid%precs3d  &
3912       &        , PRECG3D=grid%precg3d, PRECH3D=grid%prech3d               &
3913 #if ( WRF_CHEM == 1)
3914       &        , GSFCGCE_GOCART_COUPLING=config_flags%gsfcgce_gocart_coupling &
3915       &        , ICN_DIAG=grid%icn_diag                                   & ! inline gocart
3916       &        , NC_DIAG=grid%nc_diag                                     & ! inline gocart
3917 #endif
3918 !NUWRF JJS 20110525 ^^^^^
3919 !     &        , ccntype=config_flags%milbrandt_ccntype                   & ! for milbrandt (2mom)
3920 ! YLIN
3921 ! RI_CURR INPUT
3922       &        , RI_CURR=grid%rimi                                          &
3923       &        , re_cloud=grid%re_cloud, re_ice=grid%re_ice, re_snow=grid%re_snow & ! G. Thompson
3924       &        , has_reqc=grid%has_reqc, has_reqi=grid%has_reqi, has_reqs=grid%has_reqs & ! G. Thompson
3925       &        , qnwfa2d=grid%qnwfa2d, qnifa2d=grid%qnifa2d, qnbca2d=grid%qnbca2d       & ! G. Thompson
3926       &        , qnocbb2d=grid%qnocbb2d, qnbcbb2d=grid%qnbcbb2d             & ! for biomass burning emissions
3927       &        , diagflag=diag_flag, do_radar_ref=config_flags%do_radar_ref &
3928       &        , ke_diag=ke_diag                                           &
3929       &        ,u=grid%u_phy,v=grid%v_phy &
3930       &        ,scalar=scalar,num_scalar=num_scalar                             &
3931       &        ,TH_OLD=grid%th_old                                        &
3932       &        ,QV_OLD=grid%qv_old                                        &
3933       &        ,xlat=grid%xlat,xlong=grid%xlong,IVGTYP=grid%ivgtyp  &
3934       &        , EFFR_CURR=scalar(ims,kms,jms,P_EFFR), F_EFFR=F_EFFR          & ! for SBM
3935       &        , ICE_EFFR_CURR=scalar(ims,kms,jms,P_ICE_EFFR), F_ICE_EFFR=F_ICE_EFFR          & ! for SBM
3936       &        , TOT_EFFR_CURR=scalar(ims,kms,jms,P_TOT_EFFR), F_TOT_EFFR=F_TOT_EFFR          & ! for SBM
3937       &        , QIC_EFFR_CURR=scalar(ims,kms,jms,P_QIC_EFFR), F_QIC_EFFR=F_QIC_EFFR          & ! for SBM
3938       &        , QIP_EFFR_CURR=scalar(ims,kms,jms,P_QIP_EFFR), F_QIP_EFFR=F_QIP_EFFR          & ! for SBM
3939       &        , QID_EFFR_CURR=scalar(ims,kms,jms,P_QID_EFFR), F_QID_EFFR=F_QID_EFFR          & ! for SBM
3940       &        ,kext_ql=grid%kext_ql                                       &
3941       &        ,kext_qs=grid%kext_qs                                       &
3942       &        ,kext_qg=grid%kext_qg                                       &
3943       &        ,kext_qh=grid%kext_qh                                       &
3944       &        ,kext_qa=grid%kext_qa                                       &
3945       &        ,kext_qic=grid%kext_qic                                       &
3946       &        ,kext_qip=grid%kext_qip                                       &
3947       &        ,kext_qid=grid%kext_qid                                       &
3948       &        ,kext_ft_qic=grid%kext_ft_qic                                       &
3949       &        ,kext_ft_qip=grid%kext_ft_qip                                       &
3950       &        ,kext_ft_qid=grid%kext_ft_qid                                       &
3951       &        ,kext_ft_qs=grid%kext_ft_qs                                       &
3952       &        ,kext_ft_qg=grid%kext_ft_qg         &
3953       &        ,height=grid%height                                         &
3954       &        ,tempc=grid%tempc                                         &
3955       &        ,ccn_conc=grid%ccn_conc                                   & ! RAS
3956       &        ,sbmradar=sbmradar,num_sbmradar=num_sbmradar              & ! for SBM
3957       &        ,sbm_diagnostics=config_flags%sbm_diagnostics             & ! for SBM
3958       &        ,aerocu=aerocu                                            &
3959       &        ,aercu_fct=config_flags%aercu_fct                         &
3960       &        ,aercu_opt=config_flags%aercu_opt                         &
3961       &        ,no_src_types_cu=grid%no_src_types_cu                     &
3962       &        ,PBL=grid%bl_pbl_physics,EFCG=grid%EFCG,EFIG=grid%EFIG,EFSG=grid%EFSG &
3963       &        ,WACT=grid%WACT,CCN1_GS=grid%CCN1_GS,CCN2_GS=grid%CCN2_GS,CCN3_GS=grid%CCN3_GS  &
3964       &        ,CCN4_GS=grid%CCN4_GS,CCN5_GS=grid%CCN5_GS,CCN6_GS=grid%CCN6_GS &
3965       &        ,CCN7_GS=grid%CCN7_GS,NR_CU=grid%NR_CU,QR_CU=grid%QR_CU,NS_CU=grid%NS_CU &
3966       &        ,QS_CU=grid%QS_CU,CU_UAF=grid%CU_UAF,mskf_refl_10cm=grid%mskf_refl_10cm  &
3967 ! WRF-Solar EPS
3968       &        ,multi_perturb=config_flags%multi_perturb                 &
3969       &        ,pert_thom=config_flags%pert_thom                         &
3970       &        ,perts_qvapor=grid%pert3d(:,:,:,P_PQVAPOR)                &
3971       &        ,perts_qcloud=grid%pert3d(:,:,:,P_PQCLOUD)                &
3972       &        ,perts_qice=grid%pert3d(:,:,:,P_PQICE)                    &
3973       &        ,perts_qsnow=grid%pert3d(:,:,:,P_PQSNOW)                  &
3974       &        ,perts_ni=grid%pert3d(:,:,:,P_PNI)                        &
3975       &        ,pert_thom_qv=config_flags%pert_thom_qv                   &
3976       &        ,pert_thom_qc=config_flags%pert_thom_qc                   &
3977       &        ,pert_thom_qi=config_flags%pert_thom_qi                   &
3978       &        ,pert_thom_qs=config_flags%pert_thom_qs                   &
3979       &        ,pert_thom_ni=config_flags%pert_thom_ni  )
3980                                                                           
3981 BENCH_END(micro_driver_tim)
3983 #if 0
3984 BENCH_START(microswap_2)
3985 ! for load balancing; communication to redistribute the points
3986       IF ( config_flags%mp_physics .EQ. ETAMPNEW .OR. &
3987      &     config_flags%mp_physics .EQ. FER_MP_HIRES) THEN
3988 #include "SWAP_ETAMP_NEW.inc"
3989      ELSE IF ( config_flags%mp_physics .EQ. WSM3SCHEME ) THEN
3990 #include "SWAP_WSM3.inc"
3991      ENDIF
3992 BENCH_END(microswap_2)
3993 #endif
3995      CALL wrf_debug ( 200 , ' call moist_physics_finish' )
3996 BENCH_START(moist_phys_end_tim)
3998      !$OMP PARALLEL DO   &
3999      !$OMP PRIVATE ( ij, its, ite, jts, jte, im, ii, jj, kk )
4001      DO ij = 1 , grid%num_tiles
4003        its = max(grid%i_start(ij),ids)
4004        ite = min(grid%i_end(ij),ide-1)
4005        jts = max(grid%j_start(ij),jds)
4006        jte = min(grid%j_end(ij),jde-1)
4008        CALL microphysics_zero_outb (                                    &
4009                       moist , num_moist , config_flags ,                &
4010                       ids, ide, jds, jde, kds, kde,                     &
4011                       ims, ime, jms, jme, kms, kme,                     &
4012                       its, ite, jts, jte,                               &
4013                       k_start    , k_end                                )
4015        CALL microphysics_zero_outb (                                    &
4016                       scalar , num_scalar , config_flags ,              &
4017                       ids, ide, jds, jde, kds, kde,                     &
4018                       ims, ime, jms, jme, kms, kme,                     &
4019                       its, ite, jts, jte,                               &
4020                       k_start    , k_end                                )
4022        CALL microphysics_zero_outb (                                    &
4023                       chem , num_chem , config_flags ,              &
4024                       ids, ide, jds, jde, kds, kde,                     &
4025                       ims, ime, jms, jme, kms, kme,                     &
4026                       its, ite, jts, jte,                               &
4027                       k_start    , k_end                                )
4028        CALL microphysics_zero_outb (                                    &
4029                       tracer , num_tracer , config_flags ,              &
4030                       ids, ide, jds, jde, kds, kde,                     &
4031                       ims, ime, jms, jme, kms, kme,                     &
4032                       its, ite, jts, jte,                               &
4033                       k_start    , k_end                                )
4035        IF ( config_flags%periodic_x ) THEN
4036          its = max(grid%i_start(ij),ids)
4037          ite = min(grid%i_end(ij),ide-1)
4038        ELSE
4039          its = max(grid%i_start(ij),ids+sz)
4040          ite = min(grid%i_end(ij),ide-1-sz)
4041        ENDIF
4042        jts = max(grid%j_start(ij),jds+sz)
4043        jte = min(grid%j_end(ij),jde-1-sz)
4045        CALL microphysics_zero_outa (                                    &
4046                       moist , num_moist , config_flags ,                &
4047                       ids, ide, jds, jde, kds, kde,                     &
4048                       ims, ime, jms, jme, kms, kme,                     &
4049                       its, ite, jts, jte,                               &
4050                       k_start    , k_end                                )
4052        CALL microphysics_zero_outa (                                    &
4053                       scalar , num_scalar , config_flags ,              &
4054                       ids, ide, jds, jde, kds, kde,                     &
4055                       ims, ime, jms, jme, kms, kme,                     &
4056                       its, ite, jts, jte,                               &
4057                       k_start    , k_end                                )
4059        CALL microphysics_zero_outa (                                    &
4060                       chem , num_chem , config_flags ,                  &
4061                       ids, ide, jds, jde, kds, kde,                     &
4062                       ims, ime, jms, jme, kms, kme,                     &
4063                       its, ite, jts, jte,                               &
4064                       k_start    , k_end                                )
4066        CALL microphysics_zero_outa (                                    &
4067                       tracer , num_tracer , config_flags ,              &
4068                       ids, ide, jds, jde, kds, kde,                     &
4069                       ims, ime, jms, jme, kms, kme,                     &
4070                       its, ite, jts, jte,                               &
4071                       k_start    , k_end                                )
4073        CALL moist_physics_finish_em( grid%t_2, grid%t_1, t0, grid%muts, th_phy,  &
4074                                      grid%h_diabatic, dtm,                       &
4075                                      moist(ims,kms,jms,P_QV),grid%qv_diabatic,   &
4076                                      moist(ims,kms,jms,P_QC),grid%qc_diabatic,   &
4077                                      grid%th_phy_m_t0,                           &
4078                                      config_flags,                               &
4079 #if ( WRF_DFI_RADAR == 1 )
4080                                      grid%dfi_tten_rad,grid%dfi_stage,           &
4081 #endif
4082                                      ids, ide, jds, jde, kds, kde,               &
4083                                      ims, ime, jms, jme, kms, kme,               &
4084                                      its, ite, jts, jte,                         &
4085                                      k_start    , k_end                          )
4087      END DO
4088      !$OMP END PARALLEL DO
4090 #if ( defined( DM_PARALLEL ) && ( ! defined( STUBMPI ) ) )
4091 #       include "HALO_EM_THETAM.inc"
4092 #       include "PERIOD_EM_THETAM.inc"
4093 #endif
4094         its=ips ; ite = ipe
4095         jts=jps ; jte = jpe
4096         CALL set_physical_bc3d( grid%h_diabatic, 'p', config_flags,      &
4097                                 ids, ide, jds, jde, kds, kde,     &
4098                                 ims, ime, jms, jme, kms, kme,     &
4099                                 ips, ipe, jps, jpe, kps, kpe,     &
4100                                 its, ite, jts, jte,               &
4101                                 k_start    , k_end                )
4102    ENDIF  ! microphysics test
4104 !-----------------------------------------------------------
4105 !  filter for moist variables post-microphysics and end of timestep
4106 !-----------------------------------------------------------
4108    IF (config_flags%polar) THEN
4109      IF ( num_3d_m >= PARAM_FIRST_SCALAR ) THEN
4110        CALL wrf_debug ( 200 , ' call filter moist' )
4111        DO im = PARAM_FIRST_SCALAR, num_3d_m
4112          IF ( config_flags%coupled_filtering ) THEN
4113            CALL couple_scalars_for_filter ( FIELD=moist(ims,kms,jms,im)        &
4114                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
4115                     ,C1=grid%c1h , C2=grid%c2h                                   &
4116                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4117                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4118                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
4119          END IF
4121          CALL pxft ( grid=grid                                                 &
4122                   ,lineno=__LINE__                                             &
4123                   ,flag_uv            = 0                                      &
4124                   ,flag_rurv          = 0                                      &
4125                   ,flag_wph           = 0                                      &
4126                   ,flag_ww            = 0                                      &
4127                   ,flag_t             = 0                                      &
4128                   ,flag_mu            = 0                                      &
4129                   ,flag_mut           = 0                                      &
4130                   ,flag_moist         = im                                     &
4131                   ,flag_chem          = 0                                      &
4132                   ,flag_tracer        = 0                                      &
4133                   ,flag_scalar        = 0                                      &
4134                   ,actual_distance_average=config_flags%actual_distance_average&
4135                   ,pos_def            = config_flags%pos_def                   &
4136                   ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
4137                   ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
4138                   ,fft_filter_lat = config_flags%fft_filter_lat                &
4139                   ,dclat = dclat                                               &
4140                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4141                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4142                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
4143                   ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
4144                   ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
4146          IF ( config_flags%coupled_filtering ) THEN
4147            CALL uncouple_scalars_for_filter ( FIELD=moist(ims,kms,jms,im)      &
4148                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
4149                     ,C1=grid%c1h , C2=grid%c2h                                   &
4150                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4151                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4152                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
4153          END IF
4154        ENDDO
4155      ENDIF
4156    ENDIF
4158 !-----------------------------------------------------------
4159 !  end filter for moist variables post-microphysics and end of timestep
4160 !-----------------------------------------------------------
4163    !$OMP PARALLEL DO   &
4164    !$OMP PRIVATE ( ij, its, ite, jts, jte, im, ii, jj, kk )
4165    scalar_tile_loop_1ba: DO ij = 1 , grid%num_tiles
4167      IF ( config_flags%periodic_x ) THEN
4168        its = max(grid%i_start(ij),ids)
4169        ite = min(grid%i_end(ij),ide-1)
4170      ELSE
4171        its = max(grid%i_start(ij),ids+sz)
4172        ite = min(grid%i_end(ij),ide-1-sz)
4173      ENDIF
4174      jts = max(grid%j_start(ij),jds+sz)
4175      jte = min(grid%j_end(ij),jde-1-sz)
4177      CALL calc_p_rho_phi( moist, num_3d_m, config_flags%hypsometric_opt,                    &
4178                           grid%al, grid%alb, grid%mu_2, grid%muts,                          &
4179                           grid%c1h, grid%c2h, grid%c3h, grid%c4h, grid%c3f, grid%c4f,       &
4180                           grid%ph_2, grid%phb, grid%p, grid%pb, grid%t_2,                   &
4181                           p0, t0, grid%p_top, grid%znu, grid%znw, grid%dnw, grid%rdnw,      &
4182                           grid%rdn, config_flags%non_hydrostatic,config_flags%use_theta_m,  &
4183                           ids, ide, jds, jde, kds, kde,                                     &
4184                           ims, ime, jms, jme, kms, kme,                                     &
4185                           its, ite, jts, jte,                                               &
4186                           k_start    , k_end                                                )
4188    END DO scalar_tile_loop_1ba
4189    !$OMP END PARALLEL DO
4190 BENCH_END(moist_phys_end_tim)
4192    IF (.not. config_flags%non_hydrostatic) THEN
4193 #ifdef DM_PARALLEL
4194 #    include "HALO_EM_HYDRO_UV.inc"
4195 #    include "PERIOD_EM_HYDRO_UV.inc"
4196 #endif
4197      !$OMP PARALLEL DO   &
4198      !$OMP PRIVATE ( ij )
4199      DO ij = 1 , grid%num_tiles
4200        CALL diagnose_w( ph_tend, grid%ph_2, grid%ph_1, grid%w_2, grid%muts, &
4201                        grid%c1f, grid%c2f, dt_rk,              &
4202                        grid%u_2, grid%v_2, grid%ht,            &
4203                        grid%cf1, grid%cf2, grid%cf3, grid%rdx, grid%rdy, grid%msftx, grid%msfty, &
4204                        ids, ide, jds, jde, kds, kde,           &
4205                        ims, ime, jms, jme, kms, kme,           &
4206                        grid%i_start(ij), grid%i_end(ij),       &
4207                        grid%j_start(ij), grid%j_end(ij),       &
4208                        k_start    , k_end                     )
4210      END DO
4211      !$OMP END PARALLEL DO
4213    END IF
4215    CALL wrf_debug ( 200 , ' call chem polar filter ' )
4217 !-----------------------------------------------------------
4218 !  filter for chem and scalar variables at end of timestep
4219 !-----------------------------------------------------------
4221    IF (config_flags%polar) THEN
4223      IF ( num_3d_c >= PARAM_FIRST_SCALAR ) then
4224        chem_filter_loop: DO im = PARAM_FIRST_SCALAR, num_3d_c
4225          IF ( config_flags%coupled_filtering ) THEN
4226              CALL couple_scalars_for_filter ( FIELD=chem(ims,kms,jms,im)               &
4227                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
4228                     ,C1=grid%c1h , C2=grid%c2h                                   &
4229                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4230                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4231                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe           )
4232          END IF
4234          CALL pxft ( grid=grid                                                 &
4235                   ,lineno=__LINE__                                             &
4236                   ,flag_uv            = 0                                      &
4237                   ,flag_rurv          = 0                                      &
4238                   ,flag_wph           = 0                                      &
4239                   ,flag_ww            = 0                                      &
4240                   ,flag_t             = 0                                      &
4241                   ,flag_mu            = 0                                      &
4242                   ,flag_mut           = 0                                      &
4243                   ,flag_moist         = 0                                      &
4244                   ,flag_chem          = im                                     &
4245                   ,flag_tracer        = 0                                      &
4246                   ,flag_scalar        = 0                                      &
4247                   ,actual_distance_average=config_flags%actual_distance_average&
4248                   ,pos_def            = config_flags%pos_def                   &
4249                   ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
4250                   ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
4251                   ,fft_filter_lat = config_flags%fft_filter_lat                &
4252                   ,dclat = dclat                                               &
4253                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4254                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4255                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
4256                   ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
4257                   ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
4259          IF ( config_flags%coupled_filtering ) THEN
4260              CALL uncouple_scalars_for_filter ( FIELD=chem(ims,kms,jms,im)       &
4261                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
4262                     ,C1=grid%c1h , C2=grid%c2h                                   &
4263                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4264                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4265                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
4266          END IF
4267        ENDDO chem_filter_loop
4268      ENDIF
4269      IF ( num_tracer >= PARAM_FIRST_SCALAR ) then
4270        tracer_filter_loop: DO im = PARAM_FIRST_SCALAR, num_tracer
4271          IF ( config_flags%coupled_filtering ) THEN
4272            CALL couple_scalars_for_filter ( FIELD=tracer(ims,kms,jms,im)         &
4273                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
4274                     ,C1=grid%c1h , C2=grid%c2h                                   &
4275                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4276                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4277                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe           )
4278          END IF
4280          CALL pxft ( grid=grid                                                 &
4281                   ,lineno=__LINE__                                             &
4282                   ,flag_uv            = 0                                      &
4283                   ,flag_rurv          = 0                                      &
4284                   ,flag_wph           = 0                                      &
4285                   ,flag_ww            = 0                                      &
4286                   ,flag_t             = 0                                      &
4287                   ,flag_mu            = 0                                      &
4288                   ,flag_mut           = 0                                      &
4289                   ,flag_moist         = 0                                      &
4290                   ,flag_chem          = 0                                      &
4291                   ,flag_tracer        = im                                     &
4292                   ,flag_scalar        = 0                                      &
4293                   ,actual_distance_average=config_flags%actual_distance_average&
4294                   ,pos_def            = config_flags%pos_def                   &
4295                   ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
4296                   ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
4297                   ,fft_filter_lat = config_flags%fft_filter_lat                &
4298                   ,dclat = dclat                                               &
4299                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4300                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4301                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
4302                   ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
4303                   ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
4305          IF ( config_flags%coupled_filtering ) THEN
4306            CALL uncouple_scalars_for_filter ( FIELD=tracer(ims,kms,jms,im)       &
4307                     ,MU=grid%mu_2 , MUB=grid%mub                                 &
4308                     ,C1=grid%c1h , C2=grid%c2h                                   &
4309                     ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4310                     ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4311                     ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
4312          END IF
4313        ENDDO tracer_filter_loop
4314      ENDIF
4316      IF ( num_3d_s >= PARAM_FIRST_SCALAR ) then
4317        scalar_filter_loop: DO im = PARAM_FIRST_SCALAR, num_3d_s
4318          IF ( config_flags%coupled_filtering ) THEN
4319            CALL couple_scalars_for_filter ( FIELD=scalar(ims,kms,jms,im)       &
4320                   ,MU=grid%mu_2 , MUB=grid%mub                                 &
4321                   ,C1=grid%c1h , C2=grid%c2h                                   &
4322                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4323                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4324                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
4325          END IF
4327          CALL pxft ( grid=grid                                                 &
4328                   ,lineno=__LINE__                                             &
4329                   ,flag_uv            = 0                                      &
4330                   ,flag_rurv          = 0                                      &
4331                   ,flag_wph           = 0                                      &
4332                   ,flag_ww            = 0                                      &
4333                   ,flag_t             = 0                                      &
4334                   ,flag_mu            = 0                                      &
4335                   ,flag_mut           = 0                                      &
4336                   ,flag_moist         = 0                                      &
4337                   ,flag_chem          = 0                                      &
4338                   ,flag_tracer        = 0                                      &
4339                   ,flag_scalar        = im                                     &
4340                   ,actual_distance_average=config_flags%actual_distance_average&
4341                   ,pos_def            = config_flags%pos_def                   &
4342                   ,swap_pole_with_next_j = config_flags%swap_pole_with_next_j  &
4343                   ,moist=moist,chem=chem,tracer=tracer,scalar=scalar           &
4344                   ,fft_filter_lat = config_flags%fft_filter_lat                &
4345                   ,dclat = dclat                                               &
4346                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4347                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4348                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe             &
4349                   ,imsx=imsx,imex=imex,jmsx=jmsx,jmex=jmex,kmsx=kmsx,kmex=kmex &
4350                   ,ipsx=ipsx,ipex=ipex,jpsx=jmsx,jpex=jpex,kpsx=kpsx,kpex=kpex )
4352          IF ( config_flags%coupled_filtering ) THEN
4353            CALL uncouple_scalars_for_filter ( FIELD=scalar(ims,kms,jms,im)     &
4354                   ,MU=grid%mu_2 , MUB=grid%mub                                 &
4355                   ,C1=grid%c1h , C2=grid%c2h                                   &
4356                   ,ids=ids,ide=ide,jds=jds,jde=jde,kds=kds,kde=kde             &
4357                   ,ims=ims,ime=ime,jms=jms,jme=jme,kms=kms,kme=kme             &
4358                   ,ips=ips,ipe=ipe,jps=jps,jpe=jpe,kps=kps,kpe=kpe          )
4359          END IF
4360        ENDDO scalar_filter_loop
4361      ENDIF
4362    ENDIF
4364 !-----------------------------------------------------------
4365 !  end filter for chem and scalar variables at end of timestep
4366 !-----------------------------------------------------------
4368    !  We're finished except for boundary condition (and patch) update
4370    ! Boundary condition time (or communication time).  At this time, we have
4371    ! implemented periodic and symmetric physical boundary conditions.
4373    ! b.c. routine for data within patch.
4375    ! we need to do both time levels of 
4376    ! data because the time filter only works in the physical solution space.
4378    ! First, do patch communications for boundary conditions (periodicity)
4380 !-----------------------------------------------------------
4381 !  Stencils for patch communications  (WCS, 29 June 2001)
4383 !  here's where we need a wide comm stencil - these are the 
4384 !  uncoupled variables so are used for high order calc in
4385 !  advection and mixong routines.
4387 !                              * * * * *
4388 !            *        * * *    * * * * *
4389 !          * + *      * + *    * * + * * 
4390 !            *        * * *    * * * * *
4391 !                              * * * * *
4393 !   grid%u_1                            x
4394 !   grid%u_2                            x
4395 !   grid%v_1                            x
4396 !   grid%v_2                            x
4397 !   grid%w_1                            x
4398 !   grid%w_2                            x
4399 !   grid%t_1                            x
4400 !   grid%t_2                            x
4401 !  grid%ph_1                            x
4402 !  grid%ph_2                            x
4403 !  grid%tke_1                           x
4404 !  grid%tke_2                           x
4406 !    2D variables
4407 !  grid%mu_1     x
4408 !  grid%mu_2     x
4410 !    4D variables
4411 !  moist                         x
4412 !   chem                         x
4413 ! scalar                         x
4414 !----------------------------------------------------------
4417 #ifdef DM_PARALLEL
4418    IF      ( config_flags%h_mom_adv_order <= 4 .and. config_flags%h_sca_adv_order <= 4 ) THEN
4419 #    include "HALO_EM_D3_3.inc"
4420    ELSE IF ( config_flags%h_mom_adv_order <= 6 .and. config_flags%h_sca_adv_order <= 6 ) THEN
4421 #    include "HALO_EM_D3_5.inc"
4422    ELSE 
4423       WRITE(wrf_err_message,*)'solve_em: invalid h_mom_adv_order or h_sca_adv_order = ', &
4424                config_flags%h_mom_adv_order, config_flags%h_sca_adv_order
4425       CALL wrf_error_fatal(TRIM(wrf_err_message))
4426    ENDIF
4427 #  include "PERIOD_BDY_EM_D3.inc"
4428 #  include "PERIOD_BDY_EM_MOIST.inc"
4429 #  include "PERIOD_BDY_EM_CHEM.inc"
4430 #  include "PERIOD_BDY_EM_TRACER.inc"
4431 #  include "PERIOD_BDY_EM_SCALAR.inc"
4432 #endif
4434 !  now set physical b.c on a patch
4436 BENCH_START(bc_2d_tim)
4437    !$OMP PARALLEL DO   &
4438    !$OMP PRIVATE ( ij )
4439    tile_bc_loop_2: DO ij = 1 , grid%num_tiles
4441      CALL wrf_debug ( 200 , ' call set_phys_bc_dry_2' )
4443      CALL set_phys_bc_dry_2( config_flags,                           &
4444                              grid%u_1, grid%u_2, grid%v_1, grid%v_2, grid%w_1, grid%w_2,           &
4445                              grid%t_1, grid%t_2, grid%ph_1, grid%ph_2, grid%mu_1, grid%mu_2,       &
4446                              ids, ide, jds, jde, kds, kde,           &
4447                              ims, ime, jms, jme, kms, kme,           &
4448                              ips, ipe, jps, jpe, kps, kpe,           &
4449                              grid%i_start(ij), grid%i_end(ij),       &
4450                              grid%j_start(ij), grid%j_end(ij),       &
4451                              k_start    , k_end                     )
4453      CALL set_physical_bc3d( grid%tke_1, 'p', config_flags,   &
4454                              ids, ide, jds, jde, kds, kde,            &
4455                              ims, ime, jms, jme, kms, kme,            &
4456                              ips, ipe, jps, jpe, kps, kpe,            &
4457                              grid%i_start(ij), grid%i_end(ij),        &
4458                              grid%j_start(ij), grid%j_end(ij),        &
4459                              k_start    , k_end-1                    )
4461      CALL set_physical_bc3d( grid%tke_2 , 'p', config_flags,  &
4462                              ids, ide, jds, jde, kds, kde,            &
4463                              ims, ime, jms, jme, kms, kme,            &
4464                              ips, ipe, jps, jpe, kps, kpe,            &
4465                              grid%i_start(ij), grid%i_end(ij),        &
4466                              grid%j_start(ij), grid%j_end(ij),        &
4467                              k_start    , k_end                      )
4469      moisture_loop_bdy_2 : DO im = PARAM_FIRST_SCALAR , num_3d_m
4471        CALL set_physical_bc3d( moist(ims,kms,jms,im), 'p',           &
4472                                config_flags,                           &
4473                                ids, ide, jds, jde, kds, kde,           &
4474                                ims, ime, jms, jme, kms, kme,           &
4475                                ips, ipe, jps, jpe, kps, kpe,           &
4476                                grid%i_start(ij), grid%i_end(ij),       &
4477                                grid%j_start(ij), grid%j_end(ij),       &
4478                                k_start    , k_end                     )
4480      END DO moisture_loop_bdy_2
4482      chem_species_bdy_loop_2 : DO ic = PARAM_FIRST_SCALAR , num_3d_c
4484        CALL set_physical_bc3d( chem(ims,kms,jms,ic) , 'p', config_flags,  &
4485                                ids, ide, jds, jde, kds, kde,            &
4486                                ims, ime, jms, jme, kms, kme,            &
4487                                ips, ipe, jps, jpe, kps, kpe,            &
4488                                grid%i_start(ij), grid%i_end(ij),                  &
4489                                grid%j_start(ij), grid%j_end(ij),                  &
4490                                k_start    , k_end                      )
4492      END DO chem_species_bdy_loop_2
4494      tracer_species_bdy_loop_2 : DO ic = PARAM_FIRST_SCALAR , num_tracer
4496        CALL set_physical_bc3d( tracer(ims,kms,jms,ic) , 'p', config_flags,  &
4497                                ids, ide, jds, jde, kds, kde,            &
4498                                ims, ime, jms, jme, kms, kme,            &
4499                                ips, ipe, jps, jpe, kps, kpe,            &
4500                                grid%i_start(ij), grid%i_end(ij),                  &
4501                                grid%j_start(ij), grid%j_end(ij),                  &
4502                                k_start    , k_end                      )
4504      END DO tracer_species_bdy_loop_2
4506      scalar_species_bdy_loop_2 : DO is = PARAM_FIRST_SCALAR , num_3d_s
4508        CALL set_physical_bc3d( scalar(ims,kms,jms,is) , 'p', config_flags,  &
4509                                ids, ide, jds, jde, kds, kde,            &
4510                                ims, ime, jms, jme, kms, kme,            &
4511                                ips, ipe, jps, jpe, kps, kpe,            &
4512                                grid%i_start(ij), grid%i_end(ij),                  &
4513                                grid%j_start(ij), grid%j_end(ij),                  &
4514                                k_start    , k_end                      )
4516      END DO scalar_species_bdy_loop_2
4518    END DO tile_bc_loop_2
4519    !$OMP END PARALLEL DO
4520 BENCH_END(bc_2d_tim)
4522 !  this code forces boundary values to specified values to avoid drift
4524    IF( config_flags%specified .or. config_flags%nested ) THEN 
4525    !$OMP PARALLEL DO   &
4526    !$OMP PRIVATE ( ij )
4527    tile_bc_loop_3: DO ij = 1 , grid%num_tiles
4529      CALL wrf_debug ( 200 , ' call spec_bdy_final' )
4531      CALL spec_bdy_final   ( grid%u_2, grid%muus, grid%c1h, grid%c2h, grid%msfuy,     &
4532                                 grid%u_bxs, grid%u_bxe, grid%u_bys, grid%u_bye,  &
4533                                 grid%u_btxs,grid%u_btxe,grid%u_btys,grid%u_btye, &
4534                                 'u', config_flags,                               &
4535                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4536                                 grid%dtbc,                                       &
4537                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4538                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4539                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4540                                 grid%i_start(ij), grid%i_end(ij),       &
4541                                 grid%j_start(ij), grid%j_end(ij),       &
4542                                 k_start    , k_end                     )
4544      CALL spec_bdy_final   ( grid%v_2, grid%muvs, grid%c1h, grid%c2h, grid%msfvx,     &
4545                                 grid%v_bxs, grid%v_bxe, grid%v_bys, grid%v_bye,  &
4546                                 grid%v_btxs,grid%v_btxe,grid%v_btys,grid%v_btye, &
4547                                 'v', config_flags,                               &
4548                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4549                                 grid%dtbc,                                       &
4550                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4551                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4552                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4553                                 grid%i_start(ij), grid%i_end(ij),       &
4554                                 grid%j_start(ij), grid%j_end(ij),       &
4555                                 k_start    , k_end                     )
4557      IF( config_flags%nested) THEN
4558        CALL spec_bdy_final   ( grid%w_2, grid%muts, grid%c1f, grid%c2f, grid%msfty, &
4559                                 grid%w_bxs, grid%w_bxe, grid%w_bys, grid%w_bye,  &
4560                                 grid%w_btxs,grid%w_btxe,grid%w_btys,grid%w_btye, &
4561                                 'w', config_flags,                               &
4562                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4563                                 grid%dtbc,                                       &
4564                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4565                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4566                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4567                                 grid%i_start(ij), grid%i_end(ij),       &
4568                                 grid%j_start(ij), grid%j_end(ij),       &
4569                                 k_start    , k_end                     )
4570      ENDIF
4572      CALL spec_bdy_final   ( grid%t_2, grid%muts, grid%c1h, grid%c2h, grid%msfty,&
4573                                 grid%t_bxs, grid%t_bxe, grid%t_bys, grid%t_bye,  &
4574                                 grid%t_btxs,grid%t_btxe,grid%t_btys,grid%t_btye, &
4575                                 't', config_flags,                               &
4576                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4577                                 grid%dtbc,                                       &
4578                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4579                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4580                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4581                                 grid%i_start(ij), grid%i_end(ij),       &
4582                                 grid%j_start(ij), grid%j_end(ij),       &
4583                                 k_start    , k_end                     )
4585      CALL spec_bdy_final   ( grid%ph_2, grid%muts, grid%c1f, grid%c2f, grid%msfty,   &
4586                                 grid%ph_bxs, grid%ph_bxe, grid%ph_bys, grid%ph_bye,  &
4587                                 grid%ph_btxs,grid%ph_btxe,grid%ph_btys,grid%ph_btye, &
4588                                 'h', config_flags,                               &
4589                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4590                                 grid%dtbc,                                       &
4591                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4592                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4593                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4594                                 grid%i_start(ij), grid%i_end(ij),       &
4595                                 grid%j_start(ij), grid%j_end(ij),       &
4596                                 k_start    , k_end                     )
4598      CALL spec_bdy_final   ( grid%mu_2, grid%muts, grid%c1h, grid%c2h, grid%msfty,   &
4599                                 grid%mu_bxs, grid%mu_bxe, grid%mu_bys, grid%mu_bye,  &
4600                                 grid%mu_btxs,grid%mu_btxe,grid%mu_btys,grid%mu_btye, &
4601                                 'm', config_flags,                               &
4602                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4603                                 grid%dtbc,                                       &
4604                                 ids,ide, jds,jde, 1,  1,    & ! domain dims
4605                                 ims,ime, jms,jme, 1,  1,    & ! memory dims
4606                                 ips,ipe, jps,jpe, 1,  1,    & ! patch  dims
4607                                 grid%i_start(ij), grid%i_end(ij),       &
4608                                 grid%j_start(ij), grid%j_end(ij),       &
4609                                 1  , 1                    )
4611      moisture_loop_bdy_3 : DO im = PARAM_FIRST_SCALAR , num_3d_m
4613      IF ( im .EQ. P_QV .OR. config_flags%nested .OR. &
4614              ( config_flags%specified .AND. config_flags%have_bcs_moist ) ) THEN
4615         CALL spec_bdy_final   ( moist(ims,kms,jms,im), grid%muts,                &
4616                                 grid%c1h, grid%c2h, grid%msfty,                  &
4617                                 moist_bxs(jms,kms,1,im),moist_bxe(jms,kms,1,im), &
4618                                 moist_bys(ims,kms,1,im),moist_bye(ims,kms,1,im), &
4619                                 moist_btxs(jms,kms,1,im),moist_btxe(jms,kms,1,im), &
4620                                 moist_btys(ims,kms,1,im),moist_btye(ims,kms,1,im), &
4621                                 't', config_flags,                               &
4622                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4623                                 grid%dtbc,                                       &
4624                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4625                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4626                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4627                                 grid%i_start(ij), grid%i_end(ij),       &
4628                                 grid%j_start(ij), grid%j_end(ij),       &
4629                                 k_start    , k_end                     )
4630      ENDIF
4632      END DO moisture_loop_bdy_3
4634 #if (WRF_CHEM == 1)
4635      IF (num_3d_c >= PARAM_FIRST_SCALAR)  THEN
4636          chem_species_bdy_loop_3 : DO ic = PARAM_FIRST_SCALAR , num_3d_c
4638      IF( ( config_flags%nested ) ) THEN
4639         CALL spec_bdy_final   ( chem(ims,kms,jms,ic), grid%muts,               &
4640                                 grid%c1h, grid%c2h, grid%msfty,                &
4641                                 chem_bxs(jms,kms,1,ic),chem_bxe(jms,kms,1,ic), &
4642                                 chem_bys(ims,kms,1,ic),chem_bye(ims,kms,1,ic), &
4643                                 chem_btxs(jms,kms,1,ic),chem_btxe(jms,kms,1,ic), &
4644                                 chem_btys(ims,kms,1,ic),chem_btye(ims,kms,1,ic), &
4645                                 't', config_flags,                               &
4646                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4647                                 grid%dtbc,                                       &
4648                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4649                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4650                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4651                                 grid%i_start(ij), grid%i_end(ij),       &
4652                                 grid%j_start(ij), grid%j_end(ij),       &
4653                                 k_start    , k_end                     )
4654      ENDIF
4656          END DO chem_species_bdy_loop_3
4657      ENDIF
4658 #endif
4660      tracer_species_bdy_loop_3 : DO im = PARAM_FIRST_SCALAR , num_tracer
4662      IF( ( config_flags%nested ) ) THEN
4663         CALL spec_bdy_final   ( tracer(ims,kms,jms,im), grid%muts,                 &
4664                                 grid%c1h, grid%c2h, grid%msfty,                    &
4665                                 tracer_bxs(jms,kms,1,im),tracer_bxe(jms,kms,1,im), &
4666                                 tracer_bys(ims,kms,1,im),tracer_bye(ims,kms,1,im), &
4667                                 tracer_btxs(jms,kms,1,im),tracer_btxe(jms,kms,1,im), &
4668                                 tracer_btys(ims,kms,1,im),tracer_btye(ims,kms,1,im), &
4669                                 't', config_flags,                               &
4670                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4671                                 grid%dtbc,                                       &
4672                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4673                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4674                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4675                                 grid%i_start(ij), grid%i_end(ij),       &
4676                                 grid%j_start(ij), grid%j_end(ij),       &
4677                                 k_start    , k_end                     )
4678      ENDIF
4680      END DO tracer_species_bdy_loop_3
4682      scalar_species_bdy_loop_3 : DO is = PARAM_FIRST_SCALAR , num_3d_s
4684      IF( ( config_flags%nested ) ) THEN
4685         CALL spec_bdy_final   ( scalar(ims,kms,jms,is), grid%muts,                 &
4686                                 grid%c1h, grid%c2h, grid%msfty,                    &
4687                                 scalar_bxs(jms,kms,1,is),scalar_bxe(jms,kms,1,is), &
4688                                 scalar_bys(ims,kms,1,is),scalar_bye(ims,kms,1,is), &
4689                                 scalar_btxs(jms,kms,1,is),scalar_btxe(jms,kms,1,is), &
4690                                 scalar_btys(ims,kms,1,is),scalar_btye(ims,kms,1,is), &
4691                                 't', config_flags,                               &
4692                                 config_flags%spec_bdy_width, grid%spec_zone,     &
4693                                 grid%dtbc,                                       &
4694                                 ids,ide, jds,jde, kds,kde,  & ! domain dims
4695                                 ims,ime, jms,jme, kms,kme,  & ! memory dims
4696                                 ips,ipe, jps,jpe, kps,kpe,  & ! patch  dims
4697                                 grid%i_start(ij), grid%i_end(ij),       &
4698                                 grid%j_start(ij), grid%j_end(ij),       &
4699                                 k_start    , k_end                     )
4700      ENDIF
4702      END DO scalar_species_bdy_loop_3
4704    END DO tile_bc_loop_3
4705    !$OMP END PARALLEL DO
4707    ENDIF
4709 ! reset surface w for consistency
4711 #ifdef DM_PARALLEL
4712 #  include "HALO_EM_C.inc"
4713 #  include "PERIOD_BDY_EM_E.inc"
4714 #endif
4716    CALL wrf_debug ( 10 , ' call set_w_surface' )
4717    fill_w_flag = .false.
4719    !$OMP PARALLEL DO   &
4720    !$OMP PRIVATE ( ij )
4721    DO ij = 1 , grid%num_tiles
4722       CALL set_w_surface( config_flags, grid%znw, fill_w_flag,              &
4723                            grid%w_2, grid%ht,  grid%u_2, grid%v_2,          &
4724                            grid%cf1, grid%cf2, grid%cf3, grid%rdx, grid%rdy,&
4725                            grid%msftx, grid%msfty,                          &
4726                            ids, ide, jds, jde, kds, kde,                    &
4727                            ims, ime, jms, jme, kms, kme,                    &
4728                            grid%i_start(ij), grid%i_end(ij),                &
4729                            grid%j_start(ij), grid%j_end(ij),                &
4730                            k_start, k_end                                   )
4731 !                          its, ite, jts, jte, k_start, min(k_end,kde-1),   &
4733    END DO
4734    !$OMP END PARALLEL DO
4736 !-----------------------------------------------------------
4737 !  After all of the RK steps, after the microphysics, after p-rho-phi,
4738 !  after w, after filtering, we have data ready to use.
4739 !-----------------------------------------------------------
4741   CALL after_all_rk_steps ( grid, config_flags,                  &
4742                             moist, chem, tracer, scalar,         &
4743                             th_phy, pi_phy, p_phy,               &
4744                             p8w, t8w, dz8w,                      &
4745                             REAL(curr_secs,8), curr_secs2,       &
4746                             diag_flag,                           &
4747                             ids,  ide,  jds,  jde,  kds,  kde,   &
4748                             ims,  ime,  jms,  jme,  kms,  kme,   &
4749                             ips,  ipe,  jps,  jpe,  kps,  kpe,   &
4750                             imsx, imex, jmsx, jmex, kmsx, kmex,  &
4751                             ipsx, ipex, jpsx, jpex, kpsx, kpex,  &
4752                             imsy, imey, jmsy, jmey, kmsy, kmey,  &
4753                             ipsy, ipey, jpsy, jpey, kpsy, kpey   )
4757 #ifdef DM_PARALLEL
4758 !-----------------------------------------------------------------------
4759 ! see above
4760 !--------------------------------------------------------------
4761    CALL wrf_debug ( 200 , ' call HALO_RK_E' )
4762    IF      ( config_flags%h_mom_adv_order <= 4  .and. config_flags%h_sca_adv_order <= 4 ) THEN
4763 #    include "HALO_EM_E_3.inc"
4764    ELSE IF ( config_flags%h_mom_adv_order <= 6 .and. config_flags%h_sca_adv_order <= 6 ) THEN
4765 #    include "HALO_EM_E_5.inc"
4766    ELSE
4767      WRITE(wrf_err_message,*)'solve_em: invalid h_mom_adv_order or h_sca_adv_order = ', &
4768                config_flags%h_mom_adv_order, config_flags%h_sca_adv_order
4769      CALL wrf_error_fatal(TRIM(wrf_err_message))
4770    ENDIF
4771 #endif
4773 #ifdef DM_PARALLEL
4774    IF ( num_moist >= PARAM_FIRST_SCALAR  ) THEN
4775 !-----------------------------------------------------------------------
4776 ! see above
4777 !--------------------------------------------------------------
4778      CALL wrf_debug ( 200 , ' call HALO_RK_MOIST' )
4779      IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
4780 #      include "HALO_EM_MOIST_E_3.inc"
4781      ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
4782 #      include "HALO_EM_MOIST_E_5.inc"
4783      ELSE
4784        WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
4785        CALL wrf_error_fatal(TRIM(wrf_err_message))
4786      ENDIF
4787    ENDIF
4788    IF ( num_chem >= PARAM_FIRST_SCALAR ) THEN
4789 !-----------------------------------------------------------------------
4790 ! see above
4791 !--------------------------------------------------------------
4792      CALL wrf_debug ( 200 , ' call HALO_RK_CHEM' )
4793      IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
4794 #      include "HALO_EM_CHEM_E_3.inc"
4795      ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
4796 #      include "HALO_EM_CHEM_E_5.inc"
4797      ELSE
4798        WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
4799        CALL wrf_error_fatal(TRIM(wrf_err_message))
4800      ENDIF
4801    ENDIF
4802    IF ( num_tracer >= PARAM_FIRST_SCALAR ) THEN
4803 !-----------------------------------------------------------------------
4804 ! see above
4805 !--------------------------------------------------------------
4806      CALL wrf_debug ( 200 , ' call HALO_RK_TRACER' )
4807      IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
4808 #      include "HALO_EM_TRACER_E_3.inc"
4809      ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
4810 #      include "HALO_EM_TRACER_E_5.inc"
4811      ELSE
4812        WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
4813        CALL wrf_error_fatal(TRIM(wrf_err_message))
4814      ENDIF
4815    ENDIF
4816    IF ( num_scalar >= PARAM_FIRST_SCALAR ) THEN
4817 !-----------------------------------------------------------------------
4818 ! see above
4819 !--------------------------------------------------------------
4820      CALL wrf_debug ( 200 , ' call HALO_RK_SCALAR' )
4821      IF      ( config_flags%h_sca_adv_order <= 4 ) THEN
4822 #      include "HALO_EM_SCALAR_E_3.inc"
4823      ELSE IF ( config_flags%h_sca_adv_order <= 6 ) THEN
4824 #      include "HALO_EM_SCALAR_E_5.inc"
4825      ELSE
4826        WRITE(wrf_err_message,*)'solve_em: invalid h_sca_adv_order = ',config_flags%h_sca_adv_order
4827        CALL wrf_error_fatal(TRIM(wrf_err_message))
4828      ENDIF
4829    ENDIF
4830 #endif
4832    
4833 !-----------------------------------------------------------------------
4834 ! firebrand spotting (passive Lagrangian particle transport, 
4835 ! tracks firebrand physics properties)
4836 !-----------------------------------------------------------------------
4838    IF(config_flags%ifire == 2 .AND. &
4839        ! Check if spotting is on
4840        config_flags%fs_firebrand_gen_lim > 0 .AND. & 
4841        ! Check if this is the inner most grid
4842        config_flags%max_dom == grid%id) THEN 
4844 #ifdef DM_PARALLEL
4845        CALL wrf_debug ( 200 , ' call HALO_FIREBRAND_SPOTTING' )
4846 #      include "HALO_FIREBRAND_SPOTTING_5.inc"
4847 #endif
4849        CALL wrf_debug ( 3 , 'solve: calling firebrand_spotting_em_driver...' )
4850        CALL firebrand_spotting_em_driver (  &
4851                cf            = config_flags,        &
4852                grid          = grid,                &
4853                fs_p_id       = grid%fs_p_id,        &
4854                fs_p_src      = grid%fs_p_src,      &
4855                fs_p_dt       = grid%fs_p_dt,        &
4856                fs_p_x        = grid%fs_p_x,         &
4857                fs_p_y        = grid%fs_p_y,         &
4858                fs_p_z        = grid%fs_p_z,         &
4859                fs_gen_inst   = grid%fs_gen_inst,    &
4860                fs_p_mass     = grid%fs_p_mass,      &  
4861                fs_p_diam     = grid%fs_p_diam,      & 
4862                fs_p_effd     = grid%fs_p_effd,      & 
4863                fs_p_temp     = grid%fs_p_temp,      & 
4864                fs_p_tvel     = grid%fs_p_tvel,      & 
4865                fs_last_gen_dt= grid%fs_last_gen_dt, &
4866                fs_gen_idmax  = grid%fs_gen_idmax,   &
4867                fs_fire_ROSdt = grid%fs_fire_ROSdt,  &
4868                fs_fire_area  = grid%fs_fire_area,   &
4869                fs_count_landed_all   = grid%fs_count_landed_all,   &
4870                fs_count_landed_hist  = grid%fs_count_landed_hist,  &
4871                fs_landing_mask       = grid%fs_landing_mask,       &
4872                fs_spotting_lkhd      = grid%fs_spotting_lkhd,      &
4873                fs_frac_landed        = grid%fs_frac_landed,        &
4874                fs_fuel_spotting_risk = grid%fs_fuel_spotting_risk, &
4875                fs_count_reset        = grid%fs_count_reset)
4877    ENDIF
4878 ! end of firebrand spotting
4879 !-----------------------------------------------------------------------
4881 !  Max values of CFL for adaptive time step scheme
4883    DEALLOCATE(max_vert_cfl_tmp)
4884    DEALLOCATE(max_horiz_cfl_tmp)
4886    CALL wrf_debug ( 200 , ' call end of solve_em' )
4888 !  Are we about to read SST input from the wrflowinput file?  That data is saved
4889 !  for use in fractional merging of external/coupled SST and input SST. 
4890    IF ( coupler_on )   grid%just_read_auxinput4 = Is_alarm_tstep(grid%domain_clock, grid%alarms(AUXINPUT4_ALARM))
4892 !  Are we about to read the lateral boundary file?  This is a domain one action only.
4893    IF ( grid%id .EQ. 1 ) grid%just_read_boundary = Is_alarm_tstep(grid%domain_clock, grid%alarms(BOUNDARY_ALARM))
4895 ! Finish timers if compiled with -DBENCH.
4896 #include "bench_solve_em_end.h"
4898 #if (WRF_CMAQ == 1)
4899    if (firstime) then
4900       CALL nl_get_wrf_cmaq_option ( 1, wrf_cmaq_option )
4901       CALL nl_get_wrf_cmaq_freq ( 1, wrf_cmaq_freq )
4902       CALL nl_get_direct_sw_feedback ( .false., direct_sw_feedback )
4903       CALL nl_get_met_file_tstep ( 1, met_file_tstep )
4905       cmaq_wrf_feedback = direct_sw_feedback
4907       if (wrf_cmaq_option .gt. 0) then
4908          cmaq_nstep = ((grid%run_days * 24 + grid%run_hours) * 3600 + grid%run_minutes * 60 + grid%run_seconds) / &
4909                        (grid%time_step * WRF_CMAQ_FREQ)
4911          wrf_end_step = cmaq_nstep * WRF_CMAQ_FREQ - 1
4912       end if
4913    end if
4915    if (wrf_cmaq_option .gt. 0) then
4916       COUNTER = COUNTER + 1
4918       if ( .not. cmaq_wrf_feedback .and. firstime) then
4919          grid%prev_rainnc = 0.0
4920          grid%prev_rainc  = 0.0
4921       end if
4923       CMAQ_STEP = (mod(COUNTER, WRF_CMAQ_FREQ) .EQ. 0)
4925       if (CMAQ_STEP) then
4926          CALL aqprep (grid, config_flags, grid%t_phy, p_phy,       &
4927                       grid%rho, grid%z_at_w, dz8w, p8w, t8w,       &
4928                       model_config_rec%num_land_cat, 'V4.1.1',     &
4929                       wrf_cmaq_option, wrf_cmaq_freq,              &
4930                       ids, ide, jds, jde, kds, kde,                &
4931                       ims, ime, jms, jme, kms, kme,                &
4932                       ips, ipe, jps, jpe, kps, kpe,                &
4933                       moist(:,:,:,p_qv),                           &  !  optional
4934                       moist(:,:,:,p_qc),                           &  !  optional
4935                       moist(:,:,:,p_qr),                           &  !  optional
4936                       moist(:,:,:,p_qi),                           &  !  optional
4937                       moist(:,:,:,p_qs),                           &  !  optional
4938                       moist(:,:,:,p_qg)                            &  !  optional
4939                      )
4940          grid%prev_rainnc = grid%rainnc
4941          grid%prev_rainc  = grid%rainc
4942       end if
4944       if ((counter >= 1) .and. (CMAQ_STEP) .and. (wrf_cmaq_option .gt. 1)) then
4946          CALL CMAQ_DRIVER (cmaq_sdate, cmaq_stime, grid%time_step*WRF_CMAQ_FREQ,  &
4947                            twoway_jdate, twoway_jtime, .false.)
4949          if (direct_sw_feedback) then
4950             CALL FEEDBACK_READ (grid, twoway_jdate, twoway_jtime)
4951             feedback_is_ready = .true.
4952          end if
4954       end if
4956 ! call aqprep and cmaq one last time before the entire twoway model ends
4957       if (wrf_end_step == counter) then
4958          CALL aqprep (grid, config_flags, grid%t_phy, p_phy,       &
4959                       grid%rho, grid%z_at_w, dz8w, p8w, t8w,       &
4960                       model_config_rec%num_land_cat, 'V4.1.1',     &
4961                       wrf_cmaq_option, wrf_cmaq_freq,              &
4962                       ids, ide, jds, jde, kds, kde,                &
4963                       ims, ime, jms, jme, kms, kme,                &
4964                       ips, ipe, jps, jpe, kps, kpe,                &
4965                       moist(:,:,:,p_qv),                           &  !  optional
4966                       moist(:,:,:,p_qc),                           &  !  optional
4967                       moist(:,:,:,p_qr),                           &  !  optional
4968                       moist(:,:,:,p_qi),                           &  !  optional
4969                       moist(:,:,:,p_qs),                           &  !  optional
4970                       moist(:,:,:,p_qg)                            &  !  optional
4971                      )
4973          if (wrf_cmaq_option .gt. 1) then
4975             CALL CMAQ_DRIVER (cmaq_sdate, cmaq_stime, grid%time_step*WRF_CMAQ_FREQ, &
4976                               twoway_jdate, twoway_jtime, .true.)
4977          end if
4979       end if
4981    end if
4982    firstime = .false.
4983 #endif
4985    RETURN
4987 END SUBROUTINE solve_em