4 !TBH: $$$ move this to ../frame?
8 ! This module defines top-level wrf_init(), wrf_run(), and wrf_finalize()
15 USE module_driver_constants
22 USE module_dm, ONLY : wrf_dm_initialize
32 TYPE (domain) , POINTER :: keep_grid, grid_ptr, null_domain
33 TYPE (grid_config_rec_type), SAVE :: config_flags
34 INTEGER :: number_at_same_level
35 INTEGER :: time_step_begin_restart
37 INTEGER :: max_dom , domain_id , fid , oid , idum1 , idum2 , ierr
38 INTEGER :: debug_level
39 LOGICAL :: input_from_file
43 INTEGER, PARAMETER :: configbuflen = 4* CONFIG_BUF_LEN
44 INTEGER :: configbuf( configbuflen )
45 LOGICAL , EXTERNAL :: wrf_dm_on_monitor
48 CHARACTER (LEN=80) :: rstname
49 CHARACTER (LEN=80) :: message
52 SUBROUTINE Setup_Timekeeping( grid )
54 TYPE(domain), POINTER :: grid
55 END SUBROUTINE Setup_Timekeeping
58 SUBROUTINE wrf_dfi_write_initialized_state( )
59 END SUBROUTINE wrf_dfi_write_initialized_state
61 SUBROUTINE wrf_dfi_bck_init( )
62 END SUBROUTINE wrf_dfi_bck_init
64 SUBROUTINE wrf_dfi_fwd_init( )
65 END SUBROUTINE wrf_dfi_fwd_init
67 SUBROUTINE wrf_dfi_fst_init( )
68 END SUBROUTINE wrf_dfi_fst_init
70 SUBROUTINE wrf_dfi_array_reset ( )
71 END SUBROUTINE wrf_dfi_array_reset
79 SUBROUTINE wrf_init( no_init1 )
81 ! WRF initialization routine.
83 LOGICAL, OPTIONAL, INTENT(IN) :: no_init1
84 INTEGER i, myproc, nproc, hostid, loccomm, ierr, buddcounter, mydevice
85 INTEGER, ALLOCATABLE :: hostids(:), budds(:)
86 CHARACTER*512 hostname
87 #if defined(DM_PARALLEL) && defined(RUN_ON_GPU)
90 #include "version_decl"
94 ! Program_name, a global variable defined in frame/module_domain.F, is
95 ! set, then a routine <a href=init_modules.html>init_modules</a> is
96 ! called. This calls all the init programs that are provided by the
97 ! modules that are linked into WRF. These include initialization of
98 ! external I/O packages. Also, some key initializations for
99 ! distributed-memory parallelism occur here if DM_PARALLEL is specified
100 ! in the compile: setting up I/O quilt processes to act as I/O servers
101 ! and dividing up MPI communicators among those as well as initializing
102 ! external communication packages such as RSL or RSL_LITE.
106 program_name = "WRF " // TRIM(release_version) // " MODEL"
108 ! Initialize WRF modules:
109 ! Phase 1 returns after MPI_INIT() (if it is called)
111 IF ( .NOT. PRESENT( no_init1 ) ) THEN
112 ! Initialize utilities (time manager, etc.)
113 CALL WRFU_Initialize( defaultCalendar=WRFU_CAL_GREGORIAN )
115 ! Phase 2 resumes after MPI_INIT() (if it is called)
119 ! The wrf namelist.input file is read and stored in the USE associated
120 ! structure model_config_rec, defined in frame/module_configure.F, by the
121 ! call to <a href=initial_config.html>initial_config</a>. On distributed
122 ! memory parallel runs this is done only on one processor, and then
123 ! broadcast as a buffer. For distributed-memory, the broadcast of the
124 ! configuration information is accomplished by first putting the
125 ! configuration information into a buffer (<a
126 ! href=get_config_as_buffer.html>get_config_as_buffer</a>), broadcasting
127 ! the buffer, then setting the configuration information (<a
128 ! href=set_config_as_buffer.html>set_config_as_buffer</a>).
133 IF ( wrf_dm_on_monitor() ) THEN
136 CALL get_config_as_buffer( configbuf, configbuflen, nbytes )
137 CALL wrf_dm_bcast_bytes( configbuf, nbytes )
138 CALL set_config_as_buffer( configbuf, configbuflen )
139 CALL wrf_dm_initialize
144 CALL set_derived_rconfigs
147 CALL wrf_get_myproc( myproc )
148 CALL wrf_get_nproc( nproc )
149 CALL wrf_get_hostid ( hostid )
151 CALL wrf_get_dm_communicator ( loccomm )
152 ALLOCATE( hostids(nproc) )
153 ALLOCATE( budds(nproc) )
154 CALL mpi_allgather( hostid, 1, MPI_INTEGER, hostids, 1, MPI_INTEGER, loccomm, ierr )
155 if ( ierr .NE. 0 ) write(0,*)__FILE__,__LINE__,'error in mpi_allgather ',ierr
158 ! mark the ones i am on the same node with
160 IF ( hostid .EQ. hostids(i) ) THEN
161 budds(i) = buddcounter
162 buddcounter = buddcounter + 1
165 mydevice = budds(myproc+1)
166 DEALLOCATE( hostids )
171 CALL wsm5_gpu_init( myproc, nproc, mydevice )
175 ! Among the configuration variables read from the namelist is
176 ! debug_level. This is retrieved using nl_get_debug_level (Registry
177 ! generated and defined in frame/module_configure.F). The value is then
178 ! used to set the debug-print information level for use by <a
179 ! href=wrf_debug.html>wrf_debug</a> throughout the code. Debug_level
180 ! of zero (the default) causes no information to be printed when the
181 ! model runs. The higher the number (up to 1000) the more information is
186 CALL nl_get_debug_level ( 1, debug_level )
187 CALL set_wrf_debug_level ( debug_level )
189 ! allocated and configure the mother domain
191 NULLIFY( null_domain )
194 ! RSL is required for WRF nesting options.
195 ! The non-MPI build that allows nesting is only supported on machines
196 ! with the -DSTUBMPI option. Check to see if the WRF model is being asked
197 ! for a for a multi-domain run (max_dom > 1, from the namelist). If so,
198 ! then we check to make sure that we are under the parallel
199 ! run option or we are on an acceptable machine.
202 CALL nl_get_max_dom( 1, max_dom )
203 IF ( max_dom > 1 ) THEN
204 #if ( ! defined(DM_PARALLEL) && ! defined(STUBMPI) )
205 CALL wrf_error_fatal( &
206 'nesting requires either an MPI build or use of the -DSTUBMPI option' )
211 ! The top-most domain in the simulation is then allocated and configured
212 ! by calling <a href=alloc_and_configure_domain.html>alloc_and_configure_domain</a>.
213 ! Here, in the case of this root domain, the routine is passed the
214 ! globally accessible pointer to TYPE(domain), head_grid, defined in
215 ! frame/module_domain.F. The parent is null and the child index is given
216 ! as negative, signifying none. Afterwards, because the call to
217 ! alloc_and_configure_domain may modify the model's configuration data
218 ! stored in model_config_rec, the configuration information is again
219 ! repacked into a buffer, broadcast, and unpacked on each task (for
220 ! DM_PARALLEL compiles). The call to <a
221 ! href=setup_timekeeping.html>setup_timekeeping</a> for head_grid relies
222 ! on this configuration information, and it must occur after the second
223 ! broadcast of the configuration information.
226 CALL wrf_message ( program_name )
227 CALL wrf_debug ( 100 , 'wrf: calling alloc_and_configure_domain ' )
228 CALL alloc_and_configure_domain ( domain_id = 1 , &
230 parent = null_domain , &
233 CALL wrf_debug ( 100 , 'wrf: calling model_to_grid_config_rec ' )
234 CALL model_to_grid_config_rec ( head_grid%id , model_config_rec , config_flags )
235 CALL wrf_debug ( 100 , 'wrf: calling set_scalar_indices_from_config ' )
236 CALL set_scalar_indices_from_config ( head_grid%id , idum1, idum2 )
237 CALL wrf_debug ( 100 , 'wrf: calling init_wrfio' )
241 CALL get_config_as_buffer( configbuf, configbuflen, nbytes )
242 CALL wrf_dm_bcast_bytes( configbuf, nbytes )
243 CALL set_config_as_buffer( configbuf, configbuflen )
247 ! In case we are doing digital filter initialization, set dfi_stage = DFI_SETUP
248 ! to indicate in Setup_Timekeeping that we want forecast start and
249 ! end times at this point
250 IF ( head_grid%dfi_opt .NE. DFI_NODFI ) head_grid%dfi_stage = DFI_SETUP
253 CALL Setup_Timekeeping (head_grid)
256 ! The head grid is initialized with read-in data through the call to <a
257 ! href=med_initialdata_input.html>med_initialdata_input</a>, which is
258 ! passed the pointer head_grid and a locally declared configuration data
259 ! structure, config_flags, that is set by a call to <a
260 ! href=model_to_grid_config_rec.html>model_to_grid_config_rec</a>. It is
261 ! also necessary that the indices into the 4d tracer arrays such as
262 ! moisture be set with a call to <a
263 ! href=set_scalar_indices_from_config.html>set_scalar_indices_from_config</a>
264 ! prior to the call to initialize the domain. Both of these calls are
265 ! told which domain they are setting up for by passing in the integer id
266 ! of the head domain as <tt>head_grid%id</tt>, which is 1 for the
269 ! In the case that write_restart_at_0h is set to true in the namelist,
270 ! the model simply generates a restart file using the just read-in data
271 ! and then shuts down. This is used for ensemble breeding, and is not
276 CALL med_initialdata_input( head_grid , config_flags )
278 IF ( config_flags%write_restart_at_0h ) THEN
279 CALL med_restart_out ( head_grid, config_flags )
280 #ifndef AUTODOC_BUILD
281 ! prevent this from showing up before the call to integrate in the autogenerated call tree
282 CALL wrf_debug ( 0 , ' 0 h restart only wrf: SUCCESS COMPLETE WRF' )
283 ! TBH: $$$ Unscramble this later...
284 ! TBH: $$$ Need to add state to avoid calling wrf_finalize() twice when ESMF
285 ! TBH: $$$ library is used. Maybe just set clock stop_time=start_time and
286 ! TBH: $$$ do not call wrf_finalize here...
291 ! set default values for subtimes
292 head_grid%start_subtime = domain_get_start_time ( head_grid )
293 head_grid%stop_subtime = domain_get_stop_time ( head_grid )
295 END SUBROUTINE wrf_init
299 SUBROUTINE wrf_run( )
305 ! Once the top-level domain has been allocated, configured, and
306 ! initialized, the model time integration is ready to proceed. The start
307 ! and stop times for the domain are set to the start and stop time of the
308 ! model run, and then <a href=integrate.html>integrate</a> is called to
309 ! advance the domain forward through that specified time interval. On
310 ! return, the simulation is completed.
314 ! The forecast integration for the most coarse grid is now started. The
315 ! integration is from the first step (1) to the last step of the simulation.
317 CALL wrf_debug ( 100 , 'wrf: calling integrate' )
318 CALL integrate ( head_grid )
319 CALL wrf_debug ( 100 , 'wrf: back from integrate' )
321 END SUBROUTINE wrf_run
325 SUBROUTINE wrf_finalize( no_shutdown )
327 ! WRF finalize routine.
331 ! A Mediation Layer-provided
332 ! subroutine, <a href=med_shutdown_io.html>med_shutdown_io</a> is called
333 ! to allow the the model to do any I/O specific cleanup and shutdown, and
334 ! then the WRF Driver Layer routine <a
335 ! href=wrf_shutdown.html>wrf_shutdown</a> (quilt servers would be
336 ! directed to shut down here) is called to properly end the run,
337 ! including shutting down the communications (for example, most comm
338 ! layers would call MPI_FINALIZE at this point if they're using MPI).
341 LOGICAL, OPTIONAL, INTENT(IN) :: no_shutdown
344 CALL med_shutdown_io ( head_grid , config_flags )
345 CALL wrf_debug ( 100 , 'wrf: back from med_shutdown_io' )
347 CALL wrf_debug ( 0 , 'wrf: SUCCESS COMPLETE WRF' )
349 ! Call wrf_shutdown() (which calls MPI_FINALIZE()
350 ! for DM parallel runs).
351 IF ( .NOT. PRESENT( no_shutdown ) ) THEN
352 ! Finalize time manager
357 END SUBROUTINE wrf_finalize
362 ! Runs a digital filter initialization procedure.
367 ! Initialization procedure
368 IF ( config_flags%dfi_opt .NE. DFI_NODFI ) THEN
370 SELECT CASE ( config_flags%dfi_opt )
373 wrf_err_message = 'Initializing with DFL'
374 CALL wrf_message(TRIM(wrf_err_message))
376 wrf_err_message = ' Filtering forward in time'
377 CALL wrf_message(TRIM(wrf_err_message))
379 CALL wrf_dfi_fwd_init()
382 CALL wrf_dfi_array_reset()
384 CALL wrf_dfi_fst_init()
386 IF ( config_flags%dfi_write_filtered_input ) THEN
387 CALL wrf_dfi_write_initialized_state()
391 wrf_err_message = 'Initializing with DDFI'
392 CALL wrf_message(TRIM(wrf_err_message))
394 wrf_err_message = ' Integrating backward in time'
395 CALL wrf_message(TRIM(wrf_err_message))
397 CALL wrf_dfi_bck_init()
400 wrf_err_message = ' Filtering forward in time'
401 CALL wrf_message(TRIM(wrf_err_message))
403 CALL wrf_dfi_fwd_init()
406 CALL wrf_dfi_array_reset()
408 CALL wrf_dfi_fst_init()
410 IF ( config_flags%dfi_write_filtered_input ) THEN
411 CALL wrf_dfi_write_initialized_state()
415 wrf_err_message = 'Initializing with TDFI'
416 CALL wrf_message(TRIM(wrf_err_message))
418 wrf_err_message = ' Integrating backward in time'
419 CALL wrf_message(TRIM(wrf_err_message))
421 CALL wrf_dfi_bck_init()
424 CALL wrf_dfi_array_reset()
426 wrf_err_message = ' Filtering forward in time'
427 CALL wrf_message(TRIM(wrf_err_message))
429 CALL wrf_dfi_fwd_init()
432 CALL wrf_dfi_array_reset()
434 CALL wrf_dfi_fst_init()
436 IF ( config_flags%dfi_write_filtered_input ) THEN
437 CALL wrf_dfi_write_initialized_state()
441 wrf_err_message = 'Unrecognized DFI_OPT in namelist'
442 CALL wrf_error_fatal(TRIM(wrf_err_message))
449 END SUBROUTINE wrf_dfi
451 SUBROUTINE set_derived_rconfigs
453 ! Some derived rconfig entries need to be set based on the value of other,
454 ! non-derived entries before package-dependent memory allocation takes place.
455 ! This might be employed when, for example, we want to allocate arrays in
456 ! a package that depends on the setting of two or more namelist variables.
457 ! In this subroutine, we do just that.
466 IF ( model_config_rec % dfi_opt .EQ. DFI_NODFI ) THEN
467 DO i = 1, model_config_rec % max_dom
468 model_config_rec % mp_physics_dfi(i) = -1
471 DO i = 1, model_config_rec % max_dom
472 model_config_rec % mp_physics_dfi(i) = model_config_rec % mp_physics(i)
478 IF ( model_config_rec % dyn_opt .EQ. 2 ) THEN
479 DO i = 1, model_config_rec % max_dom
480 model_config_rec % mp_physics_4dvar(i) = -1
483 DO i = 1, model_config_rec % max_dom
484 model_config_rec % mp_physics_4dvar(i) = model_config_rec % mp_physics(i)
489 END SUBROUTINE set_derived_rconfigs
491 END MODULE module_wrf_top