Skip to content

Commit 06ad4c0

Browse files
mlee03mlee03
authored andcommitted
its working..
1 parent 0ad37d5 commit 06ad4c0

File tree

3 files changed

+81
-70
lines changed

3 files changed

+81
-70
lines changed

horiz_interp/horiz_interp_conserve.F90

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

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

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

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

250261
! allocate arrays for reading data
251-
allocate(read2(2, ncells), indices(ncells))
262+
allocate(read2(2, ncells))
252263

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

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

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

288288
deallocate(read2)
289289

290290
! allocate arrays to compute weights
291-
allocate(read1(ncells), dst_area1(mpi_ncells), dst_area2(nlon_dst, nlat_dst))
291+
allocate(read1(ncells), dst_area1(domain_ncells), dst_area2(nlon_dst, nlat_dst), xarea(domain_ncells))
292292

293293
! read xgrid area
294-
call read_data(weight_fileobj, "xgrid_area", read1, corner=[istart(1)], edge_lengths=[iend(1)])
294+
call read_data(weight_fileobj, "xgrid_area", read1, corner=[istart], edge_lengths=[ncells])
295+
296+
xarea = pack(read1, mask)
295297

296298
!sum over xgrid area to get destination grid area
297299
dst_area2 = 0.0
298-
do i = 1, mpi_ncells
299-
index = indices(i)
300+
do i = 1, domain_ncells
300301
i_dst = Interp%i_dst(i)
301302
j_dst = Interp%j_dst(i)
302-
read1_element = read1(index)
303-
dst_area2(i_dst, j_dst) =+ read1_element
303+
dst_area2(i_dst, j_dst) = dst_area2(i_dst, j_dst) + xarea(i)
304304
dst_area1(i) = dst_area2(i_dst, j_dst)
305-
Interp%horizInterpReals8_type%area_frac_dst(i) = read1_element
306305
end do
307306

308-
Interp%horizInterpReals8_type%area_frac_dst = dst_area1/Interp%horizInterpReals8_type%area_frac_dst
307+
Interp%horizInterpReals8_type%area_frac_dst = xarea/dst_area1
309308

310309
deallocate(read1)
311310
deallocate(dst_area1)
312311
deallocate(dst_area2)
313-
deallocate(indices)
312+
deallocate(xarea)
314313

315314
call close_file(weight_fileobj)
316315

317316
else
318317
call mpp_error(FATAL, "cannot open weight file")
319318
end if
320319

321-
Interp%nxgrid = mpi_ncells
320+
Interp%nxgrid = domain_ncells
322321
Interp%nlon_src = nlon_src
323322
Interp%nlat_src = nlat_src
324323
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/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)