Skip to content

Commit 5101237

Browse files
Fix bug in chgres_cube subroutine search_many (#808)
The routine is called from all MPI tasks, but some arrays passed to the routine were not allocated on all tasks. This problem was only seen when compiling/running under 'Debug' mode (all regression tests crashed at the call to search_many). Fixes #797.
1 parent 4f613fe commit 5101237

File tree

2 files changed

+59
-44
lines changed

2 files changed

+59
-44
lines changed

sorc/chgres_cube.fd/surface.F90

+50-34
Original file line numberDiff line numberDiff line change
@@ -851,11 +851,12 @@ subroutine interp(localpet)
851851
if (localpet == 0) then
852852
where(mask_target_one_tile == 1) mask_target_one_tile = 0
853853
where(mask_target_one_tile == 2) mask_target_one_tile = 1
854+
call search_many(num_fields,bundle_seaice_target,tile, search_nums,localpet, &
855+
mask=mask_target_one_tile)
856+
else
857+
call search_many(num_fields,bundle_seaice_target, tile,search_nums,localpet)
854858
endif
855859

856-
857-
call search_many(num_fields,bundle_seaice_target,data_one_tile, mask_target_one_tile,tile,search_nums,localpet, &
858-
field_data_3d=data_one_tile_3d)
859860
enddo
860861

861862
deallocate(search_nums)
@@ -977,10 +978,12 @@ subroutine interp(localpet)
977978
allocate(water_target_one_tile(i_target,j_target))
978979
water_target_one_tile = 0
979980
where(mask_target_one_tile == 0) water_target_one_tile = 1
980-
endif
981981

982-
call search_many(num_fields,bundle_water_target,data_one_tile, water_target_one_tile,&
983-
tile,search_nums,localpet,latitude=latitude_one_tile)
982+
call search_many(num_fields,bundle_water_target, tile,search_nums,localpet, &
983+
latitude=latitude_one_tile,mask=water_target_one_tile)
984+
else
985+
call search_many(num_fields,bundle_water_target, tile,search_nums,localpet)
986+
endif
984987

985988
if (localpet == 0) deallocate(water_target_one_tile)
986989

@@ -1068,10 +1071,12 @@ subroutine interp(localpet)
10681071
allocate(land_target_one_tile(i_target,j_target))
10691072
land_target_one_tile = 0
10701073
where(mask_target_one_tile == 1) land_target_one_tile = 1
1071-
endif
10721074

1073-
call search_many(num_fields,bundle_allland_target,data_one_tile, land_target_one_tile,&
1074-
tile,search_nums,localpet)
1075+
call search_many(num_fields,bundle_allland_target, &
1076+
tile,search_nums,localpet, mask=land_target_one_tile)
1077+
else
1078+
call search_many(num_fields,bundle_allland_target, tile,search_nums,localpet)
1079+
endif
10751080

10761081
if (localpet == 0) deallocate(land_target_one_tile)
10771082
enddo
@@ -1202,8 +1207,12 @@ subroutine interp(localpet)
12021207
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
12031208
call error_handler("IN FieldGather", rc)
12041209

1205-
call search_many(num_fields,bundle_landice_target,data_one_tile, land_target_one_tile,&
1206-
tile,search_nums,localpet,terrain_land=data_one_tile2,field_data_3d=data_one_tile_3d)
1210+
if (localpet==0) then
1211+
call search_many(num_fields,bundle_landice_target,tile,search_nums,localpet,&
1212+
terrain_land=data_one_tile2,mask=land_target_one_tile)
1213+
else
1214+
call search_many(num_fields,bundle_landice_target,tile,search_nums,localpet)
1215+
endif
12071216
enddo
12081217

12091218
deallocate (veg_type_target_one_tile)
@@ -1416,9 +1425,12 @@ subroutine interp(localpet)
14161425
call ESMF_FieldGather(soil_type_target_grid, data_one_tile2, rootPet=0,tile=tile, rc=rc)
14171426
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
14181427
call error_handler("IN FieldGather", rc)
1419-
1420-
call search_many(num_fields,bundle_nolandice_target,data_one_tile, mask_target_one_tile,&
1421-
tile,search_nums,localpet,soilt_climo=data_one_tile2, field_data_3d=data_one_tile_3d)
1428+
if (localpet==0) then
1429+
call search_many(num_fields,bundle_nolandice_target,tile,search_nums,localpet, &
1430+
soilt_climo=data_one_tile2, mask=mask_target_one_tile)
1431+
else
1432+
call search_many(num_fields,bundle_nolandice_target, tile,search_nums,localpet)
1433+
endif
14221434

14231435
print*,"- CALL FieldGather FOR TARGET GRID TOTAL SOIL MOISTURE, TILE: ", tile
14241436
call ESMF_FieldGather(soilm_tot_target_grid, data_one_tile_3d, rootPet=0, tile=tile, rc=rc)
@@ -3290,20 +3302,18 @@ end subroutine regrid_many
32903302
!!
32913303
!! @param[in] num_field Number of fields to process.
32923304
!! @param[inout] bundle_target ESMF FieldBundle holding target fields to search
3293-
!! @param[inout] field_data_2d A real array of size i_target,j_target to temporarily hold data for searching
3294-
!! @param[inout] mask An integer array of size i_target,j_target that holds masked (0) and unmasked (1)
3295-
!! values indicating where to execute search (only at unmasked points).
32963305
!! @param[in] tile Current cubed sphere tile.
32973306
!! @param[inout] search_nums Array length num_field holding search field numbers corresponding to each field provided for searching.
32983307
!! @param[in] localpet ESMF local persistent execution thread.
32993308
!! @param[in] latitude (optional) A real array size i_target,j_target of latitude on the target grid
33003309
!! @param[in] terrain_land (optional) A real array size i_target,j_target of terrain height (m) on the target grid
33013310
!! @param[in] soilt_climo (optional) A real array size i_target,j_target of climatological soil type on the target grid
3302-
!! @param[in] field_data_3d (optional) An empty real array of size i_target,j_target,lsoil_target to temporarily hold soil data for searching
3311+
!! @param[inout] mask (optional) An integer array of size i_target,j_target that holds masked (0) and unmasked (1)
3312+
!! values indicating where to execute search (only at
3313+
!unmasked points).
33033314
!! @author Larissa Reames, OU CIMMS/NOAA/NSSL
3304-
subroutine search_many(num_field,bundle_target,field_data_2d,mask, tile, &
3305-
search_nums,localpet,latitude,terrain_land,soilt_climo,&
3306-
field_data_3d)
3315+
subroutine search_many(num_field,bundle_target,tile,search_nums,localpet,latitude, &
3316+
terrain_land,soilt_climo, mask)
33073317

33083318
use model_grid, only : i_target,j_target, lsoil_target
33093319
use program_setup, only : external_model, input_type
@@ -3313,14 +3323,14 @@ subroutine search_many(num_field,bundle_target,field_data_2d,mask, tile, &
33133323

33143324
integer, intent(in) :: num_field
33153325
type(esmf_fieldbundle), intent(inout) :: bundle_target
3316-
real(esmf_kind_r8), intent(inout) :: field_data_2d(i_target,j_target)
3317-
real(esmf_kind_r8), intent(inout), optional :: field_data_3d(i_target,j_target,lsoil_target)
3326+
33183327
real(esmf_kind_r8), intent(inout), optional :: latitude(i_target,j_target)
33193328
real(esmf_kind_r8), intent(inout), optional :: terrain_land(i_target,j_target)
33203329
real(esmf_kind_r8), intent(inout), optional :: soilt_climo(i_target,j_target)
3321-
integer(esmf_kind_i8), intent(inout) :: mask(i_target,j_target)
3330+
integer(esmf_kind_i8), intent(inout), optional :: mask(i_target,j_target)
33223331

3323-
3332+
real(esmf_kind_r8), allocatable :: field_data_2d(:,:)
3333+
real(esmf_kind_r8), allocatable :: field_data_3d(:,:,:)
33243334
integer, intent(in) :: tile,localpet
33253335
integer, intent(inout) :: search_nums(num_field)
33263336

@@ -3331,46 +3341,45 @@ subroutine search_many(num_field,bundle_target,field_data_2d,mask, tile, &
33313341
integer, parameter :: TERRAIN_FIELD_NUM= 7
33323342
integer :: j,k, rc, ndims
33333343

3344+
33343345
do k = 1,num_field
33353346
call ESMF_FieldBundleGet(bundle_target,k,temp_field, rc=rc)
33363347
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
33373348
call error_handler("IN FieldGet", rc)
33383349
call ESMF_FieldGet(temp_field, name=fname, dimcount=ndims,rc=rc)
33393350
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
33403351
call error_handler("IN FieldGet", rc)
3352+
if (localpet==0) then
3353+
allocate(field_data_2d(i_target,j_target))
3354+
else
3355+
allocate(field_data_2d(0,0))
3356+
endif
33413357
if (ndims .eq. 2) then
3342-
print*, "processing 2d field ", trim(fname)
3343-
print*, "FieldGather"
33443358
call ESMF_FieldGather(temp_field,field_data_2d,rootPet=0,tile=tile, rc=rc)
33453359
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
33463360
call error_handler("IN FieldGather", rc)
33473361
if (localpet == 0) then
33483362
if (present(latitude) .and. search_nums(k).eq.SST_FIELD_NUM) then
33493363
! Sea surface temperatures; pass latitude field to search
3350-
print*, "search1"
33513364
call search(field_data_2d, mask, i_target, j_target, tile,search_nums(k),latitude=latitude)
33523365
elseif (present(terrain_land) .and. search_nums(k) .eq. TERRAIN_FIELD_NUM) then
33533366
! Terrain height; pass optional climo terrain array to search
3354-
print*, "search2"
33553367
call search(field_data_2d, mask, i_target, j_target, tile,search_nums(k),terrain_land=terrain_land)
33563368
elseif (search_nums(k) .eq. SOTYP_LAND_FIELD_NUM) then
33573369
! Soil type over land
33583370
if (fname .eq. "soil_type_target_grid") then
33593371
! Soil type over land when interpolating input data to target grid
33603372
! *with* the intention of retaining interpolated data in output
3361-
print*, "search3"
33623373
call search(field_data_2d, mask, i_target, j_target, tile,search_nums(k),soilt_climo=soilt_climo)
33633374
elseif (present(soilt_climo)) then
33643375
if (maxval(field_data_2d) > 0 .and. (trim(external_model) .ne. "GFS" .or. trim(input_type) .ne. "grib2")) then
33653376
! Soil type over land when interpolating input data to target grid
33663377
! *without* the intention of retaining data in output file
3367-
print*, "search4"
33683378
call search(field_data_2d, mask, i_target, j_target, tile, search_nums(k))
33693379
else
33703380
! If no soil type field exists in input data (e.g., GFS grib2) then don't search
33713381
! but simply set data to the climo field. This may result in
33723382
! somewhat inaccurate soil moistures as no scaling will occur
3373-
print*, "search5"
33743383
field_data_2d = soilt_climo
33753384
endif !check field value
33763385
endif !sotype from target grid
@@ -3384,12 +3393,17 @@ subroutine search_many(num_field,bundle_target,field_data_2d,mask, tile, &
33843393
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
33853394
call error_handler("IN FieldScatter", rc)
33863395
else
3396+
if (localpet==0) then
3397+
allocate(field_data_3d(i_target,j_target,lsoil_target))
3398+
else
3399+
allocate(field_data_3d(0,0,0))
3400+
endif
3401+
33873402
! Process 3d fields soil temperature, moisture, and liquid
3388-
print*, "FieldGather"
33893403
call ESMF_FieldGather(temp_field,field_data_3d,rootPet=0,tile=tile,rc=rc)
33903404
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
33913405
call error_handler("IN FieldGather", rc)
3392-
print*, "processing 3d field ", trim(fname)
3406+
33933407
if (localpet==0) then
33943408
do j = 1, lsoil_target
33953409
field_data_2d = field_data_3d(:,:,j)
@@ -3400,7 +3414,9 @@ subroutine search_many(num_field,bundle_target,field_data_2d,mask, tile, &
34003414
call ESMF_FieldScatter(temp_field, field_data_3d, rootPet=0, tile=tile,rc=rc)
34013415
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
34023416
call error_handler("IN FieldScatter", rc)
3417+
deallocate(field_data_3d)
34033418
endif !ndims
3419+
deallocate(field_data_2d)
34043420
end do !fields
34053421

34063422
end subroutine search_many

tests/chgres_cube/ftst_surface_search_many.F90

+9-10
Original file line numberDiff line numberDiff line change
@@ -295,8 +295,8 @@ program surface_interp
295295
input_type="restart"
296296

297297
!Call the search many routine to test search and replace
298-
call search_many(num_fields,bundle_search1,dummy_2d,mask_target_search,1,field_nums,localpet, &
299-
soilt_climo=soilt_climo)
298+
call search_many(num_fields,bundle_search1,1,field_nums,localpet, &
299+
soilt_climo=soilt_climo,mask=mask_target_search)
300300

301301
call ESMF_FieldBundleDestroy(bundle_search1,rc=rc)
302302
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
@@ -342,8 +342,8 @@ program surface_interp
342342
external_model="HRRR"
343343

344344
!Call the search many routine to test search and replace
345-
call search_many(num_fields,bundle_search2,dummy_2d,mask_target_search,1,field_nums,localpet, &
346-
soilt_climo=soilt_climo)
345+
call search_many(num_fields,bundle_search2,1,field_nums,localpet, &
346+
soilt_climo=soilt_climo,mask=mask_target_search)
347347

348348
call ESMF_FieldBundleDestroy(bundle_search2,rc=rc)
349349
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__)) &
@@ -385,8 +385,8 @@ program surface_interp
385385
allocate(field_nums(num_fields))
386386
field_nums = (/11,7,224/)
387387
!Call the search many routine to test some branches of default behavior
388-
call search_many(num_fields,bundle_default1,dummy_2d,mask_default,1,field_nums,localpet, &
389-
latitude=latitude_default,terrain_land=terrain_land,soilt_climo=soilt_climo)
388+
call search_many(num_fields,bundle_default1,1,field_nums,localpet, &
389+
latitude=latitude_default,terrain_land=terrain_land,soilt_climo=soilt_climo,mask=mask_default)
390390

391391
print*,"Check results for bundle_default1."
392392

@@ -441,8 +441,8 @@ program surface_interp
441441
input_type="grib2"
442442
external_model="GFS"
443443
!Call the search many routine to test behavior for GFS grib2 soil type
444-
call search_many(num_fields,bundle_default2,dummy_2d,mask_default,1,field_nums,localpet,&
445-
soilt_climo=soilt_climo)
444+
call search_many(num_fields,bundle_default2,1,field_nums,localpet,&
445+
soilt_climo=soilt_climo,mask=mask_default)
446446

447447
call ESMF_FieldBundleDestroy(bundle_default2,rc=rc)
448448
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&
@@ -478,8 +478,7 @@ program surface_interp
478478
field_nums(:) = (/21/)
479479

480480
!Call the search many routine to test behavior for GFS grib2 soil type
481-
call search_many(num_fields,bundle_3d_search,dummy_2d,mask_target_search,1,field_nums,localpet,&
482-
field_data_3d=dummy_3d)
481+
call search_many(num_fields,bundle_3d_search,1,field_nums,localpet,mask=mask_target_search)
483482

484483
call ESMF_FieldBundleDestroy(bundle_3d_search,rc=rc)
485484
if(ESMF_logFoundError(rcToCheck=rc,msg=ESMF_LOGERR_PASSTHRU,line=__LINE__,file=__FILE__))&

0 commit comments

Comments
 (0)