source: palm/trunk/SOURCE/palm.f90 @ 4525

Last change on this file since 4525 was 4496, checked in by raasch, 5 years ago

bugfixes for restart with MPI-IO: problem with posix read arguments for surface data fixed, MPI barrier removed, coupling character added to restart input and output filename

  • Property svn:keywords set to Id
File size: 16.2 KB
Line 
1! !> @file palm.f90
2!------------------------------------------------------------------------------!
3! This file is part of the PALM model system.
4!
5! PALM is free software: you can redistribute it and/or modify it under the
6! terms of the GNU General Public License as published by the Free Software
7! Foundation, either version 3 of the License, or (at your option) any later
8! version.
9!
10! PALM is distributed in the hope that it will be useful, but WITHOUT ANY
11! WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12! A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
13!
14! You should have received a copy of the GNU General Public License along with
15! PALM. If not, see <http://www.gnu.org/licenses/>.
16!
17! Copyright 1997-2020 Leibniz Universitaet Hannover
18!------------------------------------------------------------------------------!
19!
20! Current revisions:
21! -----------------
22!
23!
24! Former revisions:
25! -----------------
26! $Id: palm.f90 4496 2020-04-15 08:37:26Z raasch $
27! bugfix: coupling character added to restart output filename
28!
29! 4495 2020-04-13 20:11:20Z raasch
30! restart data handling with MPI-IO added
31!
32! 4457 2020-03-11 14:20:43Z raasch
33! use statement for exchange horiz added
34!
35! 4444 2020-03-05 15:59:50Z raasch
36! bugfix: cpp-directives for serial mode added
37!
38! 4414 2020-02-19 20:16:04Z suehring
39! Call to module_interface_init_numerics
40!
41! 4400 2020-02-10 20:32:41Z suehring
42! Add interface to initialize data output with dom
43!
44! 4360 2020-01-07 11:25:50Z suehring
45! implement new palm_date_time_mod
46!
47! 4094 2019-07-12 09:24:21Z gronemeier
48! Corrected "Former revisions" section
49!
50! 4039 2019-06-18 10:32:41Z suehring
51! Rename subroutines in module for diagnostic quantities
52!
53! 4017 2019-06-06 12:16:46Z schwenkel
54! new module for calculation and output of diagnostic quantities added
55!
56! 3885 2019-04-11 11:29:34Z kanani
57! Changes related to global restructuring of location messages and introduction
58! of additional debug messages
59!
60! 3761 2019-02-25 15:31:42Z raasch
61! unused variable removed
62!
63! 3719 2019-02-06 13:10:18Z kanani
64! Included cpu measurement for wall/soil spinup
65!
66! 3703 2019-01-29 16:43:53Z knoop
67! Some interface calls moved to module_interface + cleanup
68!
69! 3648 2019-01-02 16:35:46Z suehring
70! Rename subroutines for surface-data output
71!
72! Revision 1.1  1997/07/24 11:23:35  raasch
73! Initial revision
74!
75!
76! Description:
77! ------------
78!> Large-Eddy Simulation (LES) model for atmospheric and oceanic boundary-layer
79!> flows
80!> see the PALM homepage https://palm-model.org for further information
81!------------------------------------------------------------------------------!
82 PROGRAM palm
83 
84
85    USE arrays_3d
86
87#if defined( __parallel )
88    USE bulk_cloud_model_mod,                                                  &
89        ONLY: bulk_cloud_model, microphysics_morrison, microphysics_seifert
90#endif
91
92    USE control_parameters,                                                    &
93        ONLY:  coupling_char, do2d_at_begin, do3d_at_begin, io_blocks,         &
94               io_group, message_string, restart_data_format_output, runnr, simulated_time_chr, spinup,   &
95               time_since_reference_point, user_interface_current_revision,    &
96               user_interface_required_revision, version, write_binary
97
98#if defined( __parallel )
99    USE control_parameters,                                                    &
100        ONLY:  child_domain, constant_diffusion, humidity,                     &
101               initializing_actions, neutral, passive_scalar
102#endif
103
104    USE cpulog,                                                                &
105        ONLY:  cpu_log, log_point, cpu_statistics
106
107#if defined( __parallel )
108    USE cpulog,                                                                &
109        ONLY:  log_point_s
110#endif
111
112    USE diagnostic_output_quantities_mod,                                      &
113        ONLY:  doq_calculate
114
115#if defined( __parallel )
116    USE exchange_horiz_mod,                                                    &
117        ONLY:  exchange_horiz
118
119    USE indices,                                                               &
120        ONLY:  nbgp
121#endif
122
123    USE kinds
124
125    USE module_interface,                                                      &
126        ONLY:  module_interface_init_numerics,                                 &
127               module_interface_init_output,                                   &
128               module_interface_last_actions
129
130
131    USE multi_agent_system_mod,                                                &
132        ONLY:  agents_active, mas_last_actions
133
134    USE netcdf_data_input_mod,                                                 &
135        ONLY:  netcdf_data_input_inquire_file, netcdf_data_input_init,         &
136               netcdf_data_input_surface_data, netcdf_data_input_topo
137
138    USE pegrid
139
140#if defined( __parallel )
141    USE pmc_particle_interface,                                                &
142        ONLY: pmcp_g_alloc_win
143
144    USE pmc_interface,                                                         &
145        ONLY:  nested_run, pmci_child_initialize, pmci_init,                   &
146               pmci_modelconfiguration, pmci_parent_initialize
147#endif
148
149    USE restart_data_mpi_io_mod,                                               &
150        ONLY:  rd_mpi_io_close, rd_mpi_io_open
151
152    USE surface_data_output_mod,                                               &
153        ONLY:  surface_data_output_last_action
154
155    USE write_restart_data_mod,                                                &
156        ONLY:  wrd_global, wrd_local
157
158#if defined( __parallel )  &&  defined( _OPENACC )
159    USE openacc
160#endif
161
162
163    IMPLICIT NONE
164
165!
166!-- Local variables
167    CHARACTER(LEN=9) ::  time_to_string  !<
168    INTEGER(iwp)     ::  i               !< loop counter for blocked I/O
169#if defined( __parallel) && defined( _OPENACC )
170    INTEGER(iwp)     :: local_comm       !< local communicator (shared memory)
171    INTEGER(iwp)     :: local_num_procs  !< local number of processes
172    INTEGER(iwp)     :: local_id         !< local id
173    INTEGER(acc_device_kind) :: device_type !< device type for OpenACC
174    INTEGER(iwp)     ::  num_devices     !< number of devices visible to OpenACC
175    INTEGER(iwp)     ::  my_device       !< device used by this process
176#endif
177
178    version = 'PALM 6.0'
179    user_interface_required_revision = 'r4495'
180
181#if defined( __parallel )
182!
183!-- MPI initialisation. comm2d is preliminary set, because
184!-- it will be defined in init_pegrid but is used before in cpu_log.
185    CALL MPI_INIT( ierr )
186
187!
188!-- Initialize the coupling for nested-domain runs
189!-- comm_palm is the communicator which includes all PEs (MPI processes)
190!-- available for this (nested) model. If it is not a nested run, comm_palm
191!-- is returned as MPI_COMM_WORLD
192    CALL cpu_log( log_point_s(70), 'pmci_init', 'start' )
193    CALL pmci_init( comm_palm )
194    CALL cpu_log( log_point_s(70), 'pmci_init', 'stop' )
195    comm2d = comm_palm
196!
197!-- Get the (preliminary) number of MPI processes and the local PE-id (in case
198!-- of a further communicator splitting in init_coupling, these numbers will
199!-- be changed in init_pegrid).
200    IF ( nested_run )  THEN
201
202       CALL MPI_COMM_SIZE( comm_palm, numprocs, ierr )
203       CALL MPI_COMM_RANK( comm_palm, myid, ierr )
204
205    ELSE
206
207       CALL MPI_COMM_SIZE( MPI_COMM_WORLD, numprocs, ierr )
208       CALL MPI_COMM_RANK( MPI_COMM_WORLD, myid, ierr )
209!
210!--    Initialize PE topology in case of coupled atmosphere-ocean runs (comm_palm
211!--    will be splitted in init_coupling)
212       CALL init_coupling
213    ENDIF
214
215#ifdef _OPENACC
216!
217!-- Select OpenACC device to use in this process. For this find out how many
218!-- neighbors there are running on the same node and which id this process is.
219    IF ( nested_run )  THEN
220       CALL MPI_COMM_SPLIT_TYPE( comm_palm, MPI_COMM_TYPE_SHARED, 0,           &
221                                 MPI_INFO_NULL, local_comm, ierr )
222    ELSE
223       CALL MPI_COMM_SPLIT_TYPE( MPI_COMM_WORLD, MPI_COMM_TYPE_SHARED, 0,      &
224                                 MPI_INFO_NULL, local_comm, ierr )
225    ENDIF
226    CALL MPI_COMM_SIZE( local_comm, local_num_procs, ierr )
227    CALL MPI_COMM_RANK( local_comm, local_id, ierr )
228
229!
230!-- This loop including the barrier is a workaround for PGI compiler versions
231!-- up to and including 18.4. Later releases are able to select their GPUs in
232!-- parallel, without running into spurious errors.
233    DO i = 0, local_num_procs-1
234       CALL MPI_BARRIER( local_comm, ierr )
235
236       IF ( i == local_id )  THEN
237          device_type = acc_get_device_type()
238          num_devices = acc_get_num_devices( device_type )
239          my_device = MOD( local_id, num_devices )
240          CALL acc_set_device_num( my_device, device_type )
241       ENDIF
242    ENDDO
243
244    CALL MPI_COMM_FREE( local_comm, ierr )
245#endif
246#endif
247
248!
249!-- Initialize measuring of the CPU-time remaining to the run
250    CALL local_tremain_ini
251
252!
253!-- Start of total CPU time measuring.
254    CALL cpu_log( log_point(1), 'total', 'start' )
255    CALL cpu_log( log_point(2), 'initialisation', 'start' )
256
257!
258!-- Open a file for debug output
259    WRITE (myid_char,'(''_'',I6.6)')  myid
260    OPEN( 9, FILE='DEBUG'//TRIM( coupling_char )//myid_char, FORM='FORMATTED' )
261
262!
263!-- Initialize dvrp logging. Also, one PE maybe split from the global
264!-- communicator for doing the dvrp output. In that case, the number of
265!-- PEs available for PALM is reduced by one and communicator comm_palm
266!-- is changed respectively.
267#if defined( __parallel )
268    CALL MPI_COMM_RANK( comm_palm, myid, ierr )
269#endif
270
271!
272!-- Read control parameters from NAMELIST files and read environment-variables
273    CALL parin
274
275!
276!-- Check for the user's interface version
277    IF ( user_interface_current_revision /= user_interface_required_revision )  &
278    THEN
279       message_string = 'current user-interface revision "' //                  &
280                        TRIM( user_interface_current_revision ) // '" does ' // &
281                        'not match the required revision ' //                   &
282                        TRIM( user_interface_required_revision )
283        CALL message( 'palm', 'PA0169', 1, 2, 0, 6, 0 )
284    ENDIF
285
286!
287!-- Determine processor topology and local array indices
288    CALL init_pegrid
289!
290!-- Check if input file according to input-data standard exists
291    CALL netcdf_data_input_inquire_file
292!
293!-- Read topography input data if required. This is required before the
294!-- numerical grid is finally created in init_grid
295    CALL netcdf_data_input_topo 
296!
297!-- Generate grid parameters, initialize generic topography and further process
298!-- topography information if required
299    CALL init_grid
300!
301!-- Initialize boundary conditions and numerics such as the multigrid solver or
302!-- the advection routine
303    CALL module_interface_init_numerics
304!
305!-- Read global attributes if available. 
306    CALL netcdf_data_input_init 
307!
308!-- Read surface classification data, e.g. vegetation and soil types, water
309!-- surfaces, etc., if available. Some of these data is required before
310!-- check parameters is invoked.     
311    CALL netcdf_data_input_surface_data
312!
313!-- Check control parameters and deduce further quantities
314    CALL check_parameters
315
316    CALL init_3d_model
317
318    CALL module_interface_init_output
319
320#if defined( __parallel )
321!
322!-- Coupling protocol setup for nested-domain runs
323    IF ( nested_run )  THEN
324       CALL pmci_modelconfiguration
325!
326!--    Receive and interpolate initial data on children.
327!--    Child initialization must be made first if the model is both child and
328!--    parent if necessary
329       IF ( TRIM( initializing_actions ) /= 'read_restart_data' )  THEN
330          CALL pmci_child_initialize
331!
332!--       Send initial condition data from parent to children
333          CALL pmci_parent_initialize
334!
335!--       Exchange_horiz is needed after the nest initialization
336          IF ( child_domain )  THEN
337             CALL exchange_horiz( u, nbgp )
338             CALL exchange_horiz( v, nbgp )
339             CALL exchange_horiz( w, nbgp )
340             IF ( .NOT. neutral )  THEN
341                CALL exchange_horiz( pt, nbgp )
342             ENDIF
343             IF ( .NOT. constant_diffusion )  CALL exchange_horiz( e, nbgp )
344             IF ( humidity )  THEN
345                CALL exchange_horiz( q, nbgp )
346                IF ( bulk_cloud_model  .AND.  microphysics_morrison )  THEN
347                  CALL exchange_horiz( qc, nbgp )
348                  CALL exchange_horiz( nc, nbgp )
349                ENDIF
350                IF ( bulk_cloud_model  .AND.  microphysics_seifert )  THEN
351                   CALL exchange_horiz( qr, nbgp ) 
352                   CALL exchange_horiz( nr, nbgp )
353                ENDIF
354             ENDIF
355             IF ( passive_scalar )  CALL exchange_horiz( s, nbgp )
356          ENDIF
357       ENDIF
358
359       CALL pmcp_g_alloc_win                    ! Must be called after pmci_child_initialize and pmci_parent_initialize
360    ENDIF
361#endif
362
363!
364!-- Output of program header
365    IF ( myid == 0 )  CALL header
366
367    CALL cpu_log( log_point(2), 'initialisation', 'stop' )
368
369!
370!-- Integration of the non-atmospheric equations (land surface model, urban
371!-- surface model)
372    IF ( spinup )  THEN
373       CALL cpu_log( log_point(41), 'wall/soil spinup', 'start' )
374       CALL time_integration_spinup
375       CALL cpu_log( log_point(41), 'wall/soil spinup', 'stop' )
376    ENDIF
377
378!
379!-- Set start time in format hh:mm:ss
380    simulated_time_chr = time_to_string( time_since_reference_point )
381
382!
383!-- If required, output of initial arrays
384    IF ( do2d_at_begin )  THEN
385       CALL doq_calculate    !TODO, will be called twice
386
387       CALL data_output_2d( 'xy', 0 )
388       CALL data_output_2d( 'xz', 0 )
389       CALL data_output_2d( 'yz', 0 )
390    ENDIF
391
392    IF ( do3d_at_begin )  THEN
393       CALL doq_calculate    !TODO, will be called twice
394
395       CALL data_output_3d( 0 )
396    ENDIF
397
398!
399!-- Integration of the model equations using timestep-scheme
400    CALL time_integration
401
402!
403!-- If required, write binary data for restart runs
404    IF ( write_binary )  THEN
405
406       CALL cpu_log( log_point(22), 'wrd_local', 'start' )
407
408       CALL location_message( 'writing restart data', 'start' )
409
410       IF ( TRIM( restart_data_format_output ) == 'fortran_binary' )  THEN
411
412          DO  i = 0, io_blocks-1
413             IF ( i == io_group )  THEN
414
415!
416!--             Open binary file
417                CALL check_open( 14 )
418!
419!--             Write control parameters and other global variables for restart.
420                IF ( myid == 0 )  CALL wrd_global
421!
422!--             Write processor specific flow field data for restart runs
423                CALL wrd_local
424!
425!--             Close binary file
426                CALL close_file( 14 )
427
428             ENDIF
429#if defined( __parallel )
430             CALL MPI_BARRIER( comm2d, ierr )
431#endif
432          ENDDO
433
434       ELSEIF ( TRIM( restart_data_format_output ) == 'mpi' )  THEN
435!
436!--       Open MPI-IO restart file
437          CALL rd_mpi_io_open( 'write', 'BINOUT' // TRIM( coupling_char ) )
438!
439!--       Write control parameters and other global variables for restart.
440          CALL wrd_global
441!
442!--       Write processor specific flow field data for restart runs
443          CALL wrd_local
444!
445!--       Close restart File
446          CALL rd_mpi_io_close
447
448       ENDIF
449
450       CALL location_message( 'writing restart data', 'finished' )
451
452       CALL cpu_log( log_point(22), 'wrd_local', 'stop' )
453       
454    ENDIF
455!
456!-- Last actions for surface output, for instantaneous and time-averaged data
457    CALL surface_data_output_last_action( 0 )
458    CALL surface_data_output_last_action( 1 )
459
460!
461!-- If required, repeat output of header including the required CPU-time
462    IF ( myid == 0 )  CALL header
463!
464!-- Perform module specific last actions
465    CALL cpu_log( log_point(4), 'last actions', 'start' )
466
467    IF ( myid == 0 .AND. agents_active ) CALL mas_last_actions ! ToDo: move to module_interface
468
469    CALL module_interface_last_actions
470
471    CALL cpu_log( log_point(4), 'last actions', 'stop' )
472
473!
474!-- Close files
475    CALL close_file( 0 )
476
477!
478!-- Write run number to file (used by palmrun to create unified cycle numbers
479!-- for output files
480    IF ( myid == 0  .AND.  runnr > 0 )  THEN
481       OPEN( 90, FILE='RUN_NUMBER', FORM='FORMATTED' )
482       WRITE( 90, '(I4)' )  runnr
483       CLOSE( 90 )
484    ENDIF
485
486!
487!-- Take final CPU-time for CPU-time analysis
488    CALL cpu_log( log_point(1), 'total', 'stop' )
489    CALL cpu_statistics
490
491#if defined( __parallel )
492    CALL MPI_FINALIZE( ierr )
493#endif
494
495 END PROGRAM palm
Note: See TracBrowser for help on using the repository browser.