Skip to content

Commit cb942b4

Browse files
mlee03mlee03
authored andcommitted
Merge remote-tracking branch 'mkl/read_conserve_remap' into read_conserve_remap
2 parents 29c9dfc + 5e14d59 commit cb942b4

File tree

4 files changed

+84
-71
lines changed

4 files changed

+84
-71
lines changed

horiz_interp/horiz_interp_conserve.F90

Lines changed: 51 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,10 @@ subroutine horiz_interp_read_weights_conserve(Interp, weight_filename, weight_fi
215215
integer, intent(in) :: isw, iew, jsw, jew
216216
integer, intent(in), optional :: src_tile
217217

218-
integer :: i, j, ncells, mpi_ncells
219-
integer :: tile_id(1) !< tile id for saving xgrid for the correct tile
220-
integer :: istart(1), iend(1), i_dst, j_dst, index
221-
real(8), allocatable :: dst_area2(:,:), dst_area1(:), read1(:), read1_element
222-
integer, allocatable :: tile1(:), indices(:), read2(:,:)
218+
integer :: i, j, ncells, domain_ncells
219+
integer :: istart, iend, i_dst, j_dst, index
220+
real(8), allocatable :: dst_area2(:,:), dst_area1(:), read1(:), xarea(:)
221+
integer, allocatable :: tile1(:), read2(:,:)
223222
logical, allocatable :: mask(:)
224223

225224
type(FmsNetcdfFile_t) :: weight_fileobj !< FMS2io fileob for the weight file
@@ -235,91 +234,93 @@ subroutine horiz_interp_read_weights_conserve(Interp, weight_filename, weight_fi
235234
! get ncells
236235
call get_dimension_size(weight_fileobj, "ncells", ncells)
237236

237+
istart = 1
238+
iend = ncells
239+
238240
!get section of xgrid on src_tile
239241
if(present(src_tile)) then
240242
allocate(tile1(ncells))
241243
call read_data(weight_fileobj, "tile1", tile1)
242-
istart = FINDLOC(tile1, src_tile)
243-
iend = FINDLOC(tile1, src_tile, back=.true.)
244-
ncells = iend(1) - istart(1) + 1
244+
!find istart
245+
do i=1, ncells
246+
if(tile1(i) == src_tile) then
247+
istart = i
248+
exit
249+
end if
250+
end do
251+
!find iend
252+
do i=istart, ncells
253+
if(tile1(i) /= src_tile) then
254+
iend = i - 1
255+
exit
256+
end if
257+
end do
258+
ncells = iend - istart + 1
245259
deallocate(tile1)
246-
else
247-
istart(1) = 1
248-
iend(1) = ncells
249260
end if
250261

251262
! allocate arrays for reading data
252-
allocate(read2(2, ncells), indices(ncells))
263+
allocate(read2(2, ncells))
253264

254265
! get section of xgrid for the specified window (compute domain) on the tgt grid
255-
call read_data(weight_fileobj, "tile2_cell", read2, corner=[1,istart(1)], edge_lengths=[2,iend(1)])
266+
call read_data(weight_fileobj, "tile2_cell", read2, corner=[1,istart], edge_lengths=[2,ncells])
267+
268+
! get xgrid indices, used copilot for this section of code
256269
allocate(mask(ncells))
257-
mpi_ncells = 0
258-
mask = (read2(1,:) >= isw) .and. (read2(1,:) <= iew) .and. (read2(2,:) >= jsw) .and. (read2(2,:) <= jew)
259-
do i = 1, ncells
260-
if (mask(i)) then
261-
mpi_ncells = mpi_ncells + 1
262-
indices(mpi_ncells) = i
263-
end if
264-
end do
265-
deallocate(mask)
270+
mask = (read2(1,:) >= isw .and. read2(1,:) <= iew .and. read2(2,:) >= jsw .and. read2(2,:) <= jew)
271+
272+
domain_ncells = count(mask)
273+
274+
write(*,*) istart, iend, ncells, "and", isw, iew, jsw, jew, domain_ncells
266275

267276
! allocate data to store xgrid
268-
allocate(Interp%i_src(mpi_ncells))
269-
allocate(Interp%j_src(mpi_ncells))
270-
allocate(Interp%i_dst(mpi_ncells))
271-
allocate(Interp%j_dst(mpi_ncells))
272-
allocate(Interp%horizInterpReals8_type%area_frac_dst(mpi_ncells))
273-
274-
!save dst parent cell indices on pe domain
275-
do i=1, mpi_ncells
276-
index = indices(i)
277-
Interp%i_dst(i) = read2(1,index) - isw + 1
278-
Interp%j_dst(i) = read2(2,index) - jsw + 1
279-
end do
277+
allocate(Interp%i_src(domain_ncells))
278+
allocate(Interp%j_src(domain_ncells))
279+
allocate(Interp%i_dst(domain_ncells))
280+
allocate(Interp%j_dst(domain_ncells))
281+
allocate(Interp%horizInterpReals8_type%area_frac_dst(domain_ncells))
282+
283+
Interp%i_dst = pack(read2(1,:), mask) - isw + 1
284+
Interp%j_dst = pack(read2(2,:), mask) - jsw + 1
280285

281286
!save src parent cell indices
282-
call read_data(weight_fileobj, "tile1_cell", read2, corner=[1,istart(1)], edge_lengths=[2,iend(1)])
283-
do i=1, mpi_ncells
284-
index = indices(i)
285-
Interp%i_src(i) = read2(1, index)
286-
Interp%j_src(i) = read2(2, index)
287-
end do
287+
call read_data(weight_fileobj, "tile1_cell", read2, corner=[1,istart], edge_lengths=[2,ncells])
288+
Interp%i_src = pack(read2(1,:), mask)
289+
Interp%j_src = pack(read2(2,:), mask)
288290

289291
deallocate(read2)
290292

291293
! allocate arrays to compute weights
292-
allocate(read1(ncells), dst_area1(mpi_ncells), dst_area2(nlon_dst, nlat_dst))
294+
allocate(read1(ncells), dst_area1(domain_ncells), dst_area2(nlon_dst, nlat_dst), xarea(domain_ncells))
293295

294296
! read xgrid area
295-
call read_data(weight_fileobj, "xgrid_area", read1, corner=[istart(1)], edge_lengths=[iend(1)])
297+
call read_data(weight_fileobj, "xgrid_area", read1, corner=[istart], edge_lengths=[ncells])
298+
299+
xarea = pack(read1, mask)
296300

297301
!sum over xgrid area to get destination grid area
298302
dst_area2 = 0.0
299-
do i = 1, mpi_ncells
300-
index = indices(i)
303+
do i = 1, domain_ncells
301304
i_dst = Interp%i_dst(i)
302305
j_dst = Interp%j_dst(i)
303-
read1_element = read1(index)
304-
dst_area2(i_dst, j_dst) =+ read1_element
306+
dst_area2(i_dst, j_dst) = dst_area2(i_dst, j_dst) + xarea(i)
305307
dst_area1(i) = dst_area2(i_dst, j_dst)
306-
Interp%horizInterpReals8_type%area_frac_dst(i) = read1_element
307308
end do
308309

309-
Interp%horizInterpReals8_type%area_frac_dst = dst_area1/Interp%horizInterpReals8_type%area_frac_dst
310+
Interp%horizInterpReals8_type%area_frac_dst = xarea/dst_area1
310311

311312
deallocate(read1)
312313
deallocate(dst_area1)
313314
deallocate(dst_area2)
314-
deallocate(indices)
315+
deallocate(xarea)
315316

316317
call close_file(weight_fileobj)
317318

318319
else
319320
call mpp_error(FATAL, "cannot open weight file")
320321
end if
321322

322-
Interp%nxgrid = mpi_ncells
323+
Interp%nxgrid = domain_ncells
323324
Interp%nlon_src = nlon_src
324325
Interp%nlat_src = nlat_src
325326
Interp%nlon_dst = nlon_dst

libFMS.F90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,8 @@ module fms
402402
!> horiz_interp
403403
use horiz_interp_mod, only: fms_horiz_interp => horiz_interp, fms_horiz_interp_new => horiz_interp_new, &
404404
fms_horiz_interp_del => horiz_interp_del, fms_horiz_interp_init => horiz_interp_init, &
405-
fms_horiz_interp_end => horiz_interp_end
405+
fms_horiz_interp_end => horiz_interp_end, &
406+
fms_horiz_interp_read_weights_conserve => horiz_interp_read_weights_conserve
406407
use horiz_interp_type_mod, only: FmsHorizInterp_type => horiz_interp_type, &
407408
assignment(=), fms_horiz_interp_type_stats => stats
408409
!! used via horiz_interp

test_fms/horiz_interp/Makefile.am

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,4 @@ TESTS = test_horiz_interp2.sh test_create_xgrid_order2.sh
5656
EXTRA_DIST = test_horiz_interp2.sh test_create_xgrid_order2.sh
5757

5858
# Clean up
59-
CLEANFILES = input.nml *.out* *.dpi *.spi *.dyn *.spl
59+
CLEANFILES = input.nml *.out* *.dpi *.spi *.dyn *.spl *nc

test_fms/horiz_interp/test_horiz_interp_read.F90

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ program main
1616

1717
implicit none
1818

19-
integer, parameter :: nlon_dst = 32 !< number of lon cells in destination grid
20-
integer, parameter :: nlat_dst = 16 !< number of lat cells in destionation grid
21-
integer, parameter :: nlon_src = nlon_dst !< number of lon cells in src grid
22-
integer, parameter :: nlat_src = nlat_dst !< number of lat cells in src_grid
19+
integer, parameter :: nlon_src = 32 !< number of lon cells in src grid
20+
integer, parameter :: nlat_src = 16 !< number of lat cells in src_grid
21+
integer, parameter :: nlon_dst = nlon_src*2 !< number of lon cells in destination grid
22+
integer, parameter :: nlat_dst = nlat_src*2 !< number of lat cells in destionation grid
2323
integer, parameter :: ncells = nlon_dst*nlat_dst !< number of exchange grid cells
2424

2525
type(horiz_interp_type) :: Interp !< Interp to be populated
@@ -28,7 +28,7 @@ program main
2828
type(domain2d) :: domain !< domain
2929
integer :: global_indices(4) = [1, nlon_dst, 1, nlat_dst] !< global_indices
3030
integer :: layout(2) !< layout
31-
integer :: isc, iec, jsc, jec !< compute domain indices on dst grid
31+
integer :: isc, iec, jsc, jec, pe !< compute domain indices on dst grid
3232
integer :: nlon_dst_d, nlat_dst_d !< dst grid size on pe domain
3333

3434
real(8), allocatable :: data_src(:,:), data_dst(:,:) !< data for remapping
@@ -41,34 +41,36 @@ program main
4141

4242
! get domain
4343
call mpp_define_layout(global_indices, mpp_npes(), layout)
44-
call mpp_define_domains(global_indices, layout, domain, tile_id=1)
44+
call mpp_define_domains(global_indices, layout, domain)
4545
call mpp_get_compute_domain(domain, isc, iec, jsc, jec, xsize=nlon_dst_d, ysize=nlat_dst_d)
4646

47+
pe = mpp_pe()
48+
4749
! write remap file to test
48-
if(mpp_pe() == mpp_root_pe()) call write_remap(nlon_dst, nlat_dst, ncells)
50+
if(pe == mpp_root_pe()) call write_remap(nlon_dst, nlat_dst, ncells)
4951
call mpp_sync()
5052

5153
! allocate src data
5254
allocate(data_src(nlon_src, nlat_src))
5355
do j=1, nlat_src
5456
do i=1, nlon_src
55-
data_src(i,j) = j*nlon_src + i
57+
data_src(i,j) = pe*10000 + j*100 + i
5658
end do
5759
end do
5860

5961
! allocate dst data on domain
60-
allocate(data_dst(isc:iec, jsc:jec))
62+
allocate(data_dst(nlon_dst_d, nlat_dst_d))
6163
data_dst = -99.0
6264

6365
! read remap file
6466
call horiz_interp_read_weights_conserve(Interp, remap, "fregrid", nlon_src, nlat_src, &
65-
nlon_dst_d, nlat_dst_d, isc, iec, jsc, jec, 1)
67+
nlon_dst_d, nlat_dst_d, isc, iec, jsc, jec, mpp_pe()+1)
6668

6769
! remap
6870
call horiz_interp(Interp, data_src, data_dst)
6971

7072
! check answers
71-
if(.not.all(data_src(isc:iec, jsc:jec) == data_dst)) then
73+
if(.not.all(data_src == data_dst)) then
7274
call mpp_error(FATAL, "error remapping with read-in exchange grid")
7375
end if
7476

@@ -85,7 +87,7 @@ subroutine write_remap(nlon_dst, nlat_dst, ncells)
8587
integer, allocatable :: tile1(:), tile1_cell(:, :), tile2_cell(:, :)
8688
real(r8_kind), allocatable :: lon_dst(:,:), lat_dst(:,:), xgrid_area(:)
8789

88-
integer :: i, j, ij
90+
integer :: i, j, ij, itile_x, itile_y, count
8991

9092
allocate(lon_dst(nlon_dst+1, nlat_dst+1), lat_dst(nlon_dst+1, nlat_dst+1))
9193
! create grid in radians
@@ -106,15 +108,24 @@ subroutine write_remap(nlon_dst, nlat_dst, ncells)
106108

107109
! xgrid
108110
ij = 1
109-
do j=1, nlat_src
110-
do i=1, nlon_src
111-
tile1_cell(1, ij) = i
112-
tile1_cell(2, ij) = j
113-
ij = ij + 1
111+
do itile_y=0, 1
112+
do itile_x=0, 1
113+
do j=1, nlat_src
114+
do i=1, nlon_src
115+
tile1_cell(1, ij) = i
116+
tile1_cell(2, ij) = j
117+
tile2_cell(1, ij) = itile_x*nlon_src + i
118+
tile2_cell(2, ij) = itile_y*nlat_src + j
119+
ij = ij + 1
120+
end do
121+
end do
114122
end do
115123
end do
116-
tile2_cell = tile1_cell
117-
tile1 = 1
124+
125+
tile1(1:ncells/4) = 1
126+
tile1(ncells/4+1:ncells/2) = 2
127+
tile1(ncells/2+1:ncells*3/4) = 3
128+
tile1(ncells*3/4+1:ncells) = 4
118129

119130
!write remap file
120131
if(open_file(fileobj, "remap.nc", "overwrite")) then

0 commit comments

Comments
 (0)