| 1 |
! (C) Copyright 2013 ECMWF. |
| 2 |
! |
| 3 |
! This software is licensed under the terms of the Apache Licence Version 2.0 |
| 4 |
! which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 5 |
! In applying this licence, ECMWF does not waive the privileges and immunities |
| 6 |
! granted to it by virtue of its status as an intergovernmental organisation nor |
| 7 |
! does it submit to any jurisdiction. |
| 8 |
|
| 9 |
#include "atlas/atlas_f.h" |
| 10 |
|
| 11 |
module atlas_StencilComputer_module |
| 12 |
|
| 13 |
use fckit_shared_object_module, only: fckit_shared_object, fckit_c_deleter |
| 14 |
use atlas_kinds_module, only : ATLAS_KIND_IDX |
| 15 |
use, intrinsic :: iso_c_binding, only : c_ptr, c_int |
| 16 |
|
| 17 |
implicit none |
| 18 |
|
| 19 |
private :: fckit_shared_object, fckit_c_deleter |
| 20 |
private :: c_ptr, c_int |
| 21 |
private :: ATLAS_KIND_IDX |
| 22 |
|
| 23 |
public :: atlas_StructuredGrid_ComputeNorth |
| 24 |
public :: atlas_StructuredGrid_ComputeWest |
| 25 |
public :: atlas_StructuredGrid_ComputeStencil |
| 26 |
public :: atlas_StructuredGrid_Stencil |
| 27 |
|
| 28 |
private |
| 29 |
|
| 30 |
!------------------------------------------------------------------------------ |
| 31 |
TYPE, extends(fckit_shared_object) :: atlas_StructuredGrid_ComputeNorth |
| 32 |
|
| 33 |
! Purpose : |
| 34 |
! ------- |
| 35 |
! *atlas_StructuredGrid_ComputeNorth* : To compute latitude index north of latitude |
| 36 |
|
| 37 |
! Methods : |
| 38 |
! ------- |
| 39 |
|
| 40 |
! Author : |
| 41 |
! ------ |
| 42 |
! 9-Jan-2024 Willem Deconinck *ECMWF* |
| 43 |
|
| 44 |
!------------------------------------------------------------------------------ |
| 45 |
contains |
| 46 |
|
| 47 |
procedure, private :: atlas_StructuredGrid_ComputeNorth__execute_real32 |
| 48 |
procedure, private :: atlas_StructuredGrid_ComputeNorth__execute_real64 |
| 49 |
generic :: execute => atlas_StructuredGrid_ComputeNorth__execute_real32, & |
| 50 |
& atlas_StructuredGrid_ComputeNorth__execute_real64 |
| 51 |
|
| 52 |
#if FCKIT_FINAL_NOT_INHERITING |
| 53 |
final :: atlas_StructuredGrid_ComputeNorth__final_auto |
| 54 |
#endif |
| 55 |
|
| 56 |
END TYPE atlas_StructuredGrid_ComputeNorth |
| 57 |
|
| 58 |
interface atlas_StructuredGrid_ComputeNorth |
| 59 |
module procedure atlas_StructuredGrid_ComputeNorth__ctor |
| 60 |
end interface |
| 61 |
|
| 62 |
!------------------------------------------------------------------------------ |
| 63 |
|
| 64 |
!------------------------------------------------------------------------------ |
| 65 |
TYPE, extends(fckit_shared_object) :: atlas_StructuredGrid_ComputeWest |
| 66 |
|
| 67 |
! Purpose : |
| 68 |
! ------- |
| 69 |
! *atlas_StructuredGrid_ComputeWest* : To compute longitude index west of longitude at given latitude index |
| 70 |
|
| 71 |
! Methods : |
| 72 |
! ------- |
| 73 |
|
| 74 |
! Author : |
| 75 |
! ------ |
| 76 |
! 9-Jan-2024 Willem Deconinck *ECMWF* |
| 77 |
|
| 78 |
!------------------------------------------------------------------------------ |
| 79 |
contains |
| 80 |
|
| 81 |
procedure, private :: atlas_StructuredGrid_ComputeWest__execute_real32 |
| 82 |
procedure, private :: atlas_StructuredGrid_ComputeWest__execute_real64 |
| 83 |
generic :: execute => atlas_StructuredGrid_ComputeWest__execute_real32, & |
| 84 |
& atlas_StructuredGrid_ComputeWest__execute_real64 |
| 85 |
|
| 86 |
#if FCKIT_FINAL_NOT_INHERITING |
| 87 |
final :: atlas_StructuredGrid_ComputeWest__final_auto |
| 88 |
#endif |
| 89 |
|
| 90 |
END TYPE atlas_StructuredGrid_ComputeWest |
| 91 |
|
| 92 |
interface atlas_StructuredGrid_ComputeWest |
| 93 |
module procedure atlas_StructuredGrid_ComputeWest__ctor |
| 94 |
end interface |
| 95 |
|
| 96 |
!------------------------------------------------------------------------------ |
| 97 |
|
| 98 |
integer(c_int), parameter, private :: STENCIL_MAX_WIDTH = 6 !-> maximum 6x6 stencil |
| 99 |
|
| 100 |
TYPE atlas_StructuredGrid_Stencil |
| 101 |
integer(ATLAS_KIND_IDX) :: width |
| 102 |
integer(ATLAS_KIND_IDX) :: j_begin |
| 103 |
integer(ATLAS_KIND_IDX) :: i_begin(STENCIL_MAX_WIDTH) ! on stack |
| 104 |
contains |
| 105 |
procedure, pass :: write => atlas_StructuredGrid_Stencil__write |
| 106 |
generic, public :: write(FORMATTED) => write |
| 107 |
procedure, private :: i_int32 => atlas_StructuredGrid_Stencil__i_int32 |
| 108 |
procedure, private :: i_int64 => atlas_StructuredGrid_Stencil__i_int64 |
| 109 |
generic, public :: i => i_int32, i_int64 |
| 110 |
procedure, public :: j => atlas_StructuredGrid_Stencil__j |
| 111 |
END TYPE atlas_StructuredGrid_Stencil |
| 112 |
|
| 113 |
TYPE :: atlas_StructuredGrid_ComputeStencil |
| 114 |
integer(c_int) :: halo |
| 115 |
integer(ATLAS_KIND_IDX) :: stencil_width |
| 116 |
integer(ATLAS_KIND_IDX) :: stencil_offset |
| 117 |
type(atlas_StructuredGrid_ComputeNorth) :: compute_north |
| 118 |
type(atlas_StructuredGrid_ComputeWest) :: compute_west |
| 119 |
contains |
| 120 |
procedure :: setup_int32 => atlas_StructuredGrid_ComputeStencil__setup_int32 |
| 121 |
procedure :: setup_int64 => atlas_StructuredGrid_ComputeStencil__setup_int64 |
| 122 |
generic, public :: setup => setup_int32, setup_int64 |
| 123 |
|
| 124 |
procedure :: execute => atlas_StructuredGrid_ComputeStencil__execute_real64 |
| 125 |
|
| 126 |
procedure :: assignment_operator => atlas_StructuredGrid_ComputeStencil__assignment |
| 127 |
generic, public :: assignment(=) => assignment_operator |
| 128 |
|
| 129 |
procedure :: final => atlas_StructuredGrid_ComputeStencil__final |
| 130 |
END TYPE |
| 131 |
|
| 132 |
! Better not use, use setup member function instead ! |
| 133 |
!interface atlas_StructuredGrid_ComputeStencil |
| 134 |
! module procedure atlas_StructuredGrid_ComputeStencil__ctor |
| 135 |
!end interface |
| 136 |
|
| 137 |
|
| 138 |
!======================================================== |
| 139 |
contains |
| 140 |
!======================================================== |
| 141 |
|
| 142 |
|
| 143 |
! ----------------------------------------------------------------------------- |
| 144 |
! Destructor |
| 145 |
|
| 146 |
#if FCKIT_FINAL_NOT_INHERITING |
| 147 |
ATLAS_FINAL subroutine atlas_StructuredGrid_ComputeNorth__final_auto(this) |
| 148 |
type(atlas_StructuredGrid_ComputeNorth), intent(inout) :: this |
| 149 |
#if FCKIT_FINAL_NOT_PROPAGATING |
| 150 |
call this%final() |
| 151 |
#endif |
| 152 |
FCKIT_SUPPRESS_UNUSED( this ) |
| 153 |
end subroutine |
| 154 |
#endif |
| 155 |
|
| 156 |
#if FCKIT_FINAL_NOT_INHERITING |
| 157 |
ATLAS_FINAL subroutine atlas_StructuredGrid_ComputeWest__final_auto(this) |
| 158 |
type(atlas_StructuredGrid_ComputeWest), intent(inout) :: this |
| 159 |
#if FCKIT_FINAL_NOT_PROPAGATING |
| 160 |
call this%final() |
| 161 |
#endif |
| 162 |
FCKIT_SUPPRESS_UNUSED( this ) |
| 163 |
end subroutine |
| 164 |
#endif |
| 165 |
|
| 166 |
|
| 167 |
! ----------------------------------------------------------------------------- |
| 168 |
! Constructors |
| 169 |
|
| 170 |
function atlas_StructuredGrid_ComputeNorth__ctor(grid, halo) result(this) |
| 171 |
use, intrinsic :: iso_c_binding, only : c_int |
| 172 |
use fckit_c_interop_module, only: c_str |
| 173 |
use atlas_grid_StencilComputer_c_binding, only : atlas__grid__ComputeNorth__new, atlas__grid__ComputeNorth__delete |
| 174 |
use atlas_grid_module, only : atlas_StructuredGrid |
| 175 |
implicit none |
| 176 |
type(atlas_StructuredGrid_ComputeNorth) :: this |
| 177 |
class(atlas_StructuredGrid), intent(in) :: grid |
| 178 |
integer(c_int), intent(in) :: halo |
| 179 |
call this%reset_c_ptr( atlas__grid__ComputeNorth__new(grid%CPTR_PGIBUG_B, halo), & |
| 180 |
& fckit_c_deleter(atlas__grid__ComputeNorth__delete) ) |
| 181 |
call this%return() |
| 182 |
end function |
| 183 |
|
| 184 |
function atlas_StructuredGrid_ComputeWest__ctor(grid, halo) result(this) |
| 185 |
use, intrinsic :: iso_c_binding, only : c_int |
| 186 |
use fckit_c_interop_module, only: c_str |
| 187 |
use atlas_grid_StencilComputer_c_binding, only : atlas__grid__ComputeWest__new, atlas__grid__ComputeWest__delete |
| 188 |
use atlas_grid_module, only : atlas_StructuredGrid |
| 189 |
implicit none |
| 190 |
type(atlas_StructuredGrid_ComputeWest) :: this |
| 191 |
class(atlas_StructuredGrid), intent(in) :: grid |
| 192 |
integer(c_int), intent(in) :: halo |
| 193 |
call this%reset_c_ptr( atlas__grid__ComputeWest__new(grid%CPTR_PGIBUG_B, halo), & |
| 194 |
& fckit_c_deleter(atlas__grid__ComputeWest__delete) ) |
| 195 |
call this%return() |
| 196 |
end function |
| 197 |
|
| 198 |
subroutine atlas_StructuredGrid_ComputeStencil__setup_int32(this, grid, stencil_width) |
| 199 |
use, intrinsic :: iso_c_binding, only : c_double, c_int |
| 200 |
use atlas_grid_module, only : atlas_StructuredGrid |
| 201 |
implicit none |
| 202 |
class(atlas_StructuredGrid_ComputeStencil) :: this |
| 203 |
class(atlas_StructuredGrid), intent(in) :: grid |
| 204 |
integer(c_int), intent(in) :: stencil_width |
| 205 |
this%stencil_width = stencil_width |
| 206 |
this%halo = (stencil_width + 1) / 2 |
| 207 |
this%stencil_offset = stencil_width - floor(real(stencil_width,c_double) / 2._c_double + 1._c_double, ATLAS_KIND_IDX) |
| 208 |
this%compute_north = atlas_StructuredGrid_ComputeNorth(grid, this%halo) |
| 209 |
this%compute_west = atlas_StructuredGrid_ComputeWest(grid, this%halo) |
| 210 |
end subroutine |
| 211 |
|
| 212 |
subroutine atlas_StructuredGrid_ComputeStencil__setup_int64(this, grid, stencil_width) |
| 213 |
use, intrinsic :: iso_c_binding, only : c_double, c_long |
| 214 |
use atlas_grid_module, only : atlas_StructuredGrid |
| 215 |
implicit none |
| 216 |
class(atlas_StructuredGrid_ComputeStencil) :: this |
| 217 |
class(atlas_StructuredGrid), intent(in) :: grid |
| 218 |
integer(c_long), intent(in) :: stencil_width |
| 219 |
this%stencil_width = stencil_width |
| 220 |
this%halo = (stencil_width + 1) / 2 |
| 221 |
this%stencil_offset = stencil_width - floor(real(stencil_width,c_double) / 2._c_double + 1._c_double, ATLAS_KIND_IDX) |
| 222 |
this%compute_north = atlas_StructuredGrid_ComputeNorth(grid, this%halo) |
| 223 |
this%compute_west = atlas_StructuredGrid_ComputeWest(grid, this%halo) |
| 224 |
end subroutine |
| 225 |
|
| 226 |
|
| 227 |
! ---------------------------------------------------------------------------------------- |
| 228 |
|
| 229 |
function atlas_StructuredGrid_ComputeNorth__execute_real32(this, y) result(index) |
| 230 |
use, intrinsic :: iso_c_binding, only : c_float |
| 231 |
use atlas_grid_StencilComputer_c_binding, only : atlas__grid__ComputeNorth__execute_real32 |
| 232 |
implicit none |
| 233 |
integer(ATLAS_KIND_IDX) :: index |
| 234 |
class(atlas_StructuredGrid_ComputeNorth), intent(in) :: this |
| 235 |
real(c_float), intent(in) :: y |
| 236 |
index = atlas__grid__ComputeNorth__execute_real32(this%CPTR_PGIBUG_B, y) + 1 |
| 237 |
end function |
| 238 |
|
| 239 |
function atlas_StructuredGrid_ComputeNorth__execute_real64(this, y) result(index) |
| 240 |
use, intrinsic :: iso_c_binding, only : c_double |
| 241 |
use atlas_grid_StencilComputer_c_binding, only : atlas__grid__ComputeNorth__execute_real64 |
| 242 |
implicit none |
| 243 |
integer(ATLAS_KIND_IDX) :: index |
| 244 |
class(atlas_StructuredGrid_ComputeNorth), intent(in) :: this |
| 245 |
real(c_double), intent(in) :: y |
| 246 |
index = atlas__grid__ComputeNorth__execute_real64(this%CPTR_PGIBUG_B, y) + 1 |
| 247 |
end function |
| 248 |
! ---------------------------------------------------------------------------------------- |
| 249 |
|
| 250 |
function atlas_StructuredGrid_ComputeWest__execute_real32(this, x, j) result(index) |
| 251 |
use, intrinsic :: iso_c_binding, only : c_float |
| 252 |
use atlas_grid_StencilComputer_c_binding, only : atlas__grid__ComputeWest__execute_real32 |
| 253 |
implicit none |
| 254 |
integer(ATLAS_KIND_IDX) :: index |
| 255 |
class(atlas_StructuredGrid_ComputeWest), intent(in) :: this |
| 256 |
real(c_float), intent(in) :: x |
| 257 |
integer(ATLAS_KIND_IDX), intent(in) :: j |
| 258 |
index = atlas__grid__ComputeWest__execute_real32(this%CPTR_PGIBUG_B, x, j-int(1,ATLAS_KIND_IDX)) + 1 |
| 259 |
end function |
| 260 |
|
| 261 |
function atlas_StructuredGrid_ComputeWest__execute_real64(this, x, j) result(index) |
| 262 |
use, intrinsic :: iso_c_binding, only : c_double |
| 263 |
use atlas_grid_StencilComputer_c_binding, only : atlas__grid__ComputeWest__execute_real64 |
| 264 |
implicit none |
| 265 |
integer(ATLAS_KIND_IDX) :: index |
| 266 |
class(atlas_StructuredGrid_ComputeWest), intent(in) :: this |
| 267 |
real(c_double), intent(in) :: x |
| 268 |
integer(ATLAS_KIND_IDX), intent(in) :: j |
| 269 |
index = atlas__grid__ComputeWest__execute_real64(this%CPTR_PGIBUG_B, x, j-int(1,ATLAS_KIND_IDX)) + 1 |
| 270 |
end function |
| 271 |
! ---------------------------------------------------------------------------------------- |
| 272 |
|
| 273 |
|
| 274 |
subroutine atlas_StructuredGrid_ComputeStencil__execute_real64(this, x, y, stencil) |
| 275 |
use, intrinsic :: iso_c_binding, only : c_double |
| 276 |
implicit none |
| 277 |
class(atlas_StructuredGrid_ComputeStencil), intent(in) :: this |
| 278 |
real(c_double), intent(in) :: x, y |
| 279 |
type(atlas_StructuredGrid_Stencil), intent(inout) :: stencil |
| 280 |
|
| 281 |
integer(ATLAS_KIND_IDX) :: jj |
| 282 |
stencil%width = this%stencil_width |
| 283 |
|
| 284 |
stencil%j_begin = this%compute_north%execute(y) - this%stencil_offset |
| 285 |
do jj = 1_ATLAS_KIND_IDX, this%stencil_width |
| 286 |
stencil%i_begin(jj) = this%compute_west%execute(x, stencil%j_begin + jj - 1) - this%stencil_offset |
| 287 |
enddo |
| 288 |
end subroutine |
| 289 |
! ---------------------------------------------------------------------------------------- |
| 290 |
|
| 291 |
subroutine atlas_StructuredGrid_Stencil__write (stencil, unit, iotype, v_list, iostat, iomsg) |
| 292 |
implicit none |
| 293 |
class(atlas_StructuredGrid_Stencil), intent(in) :: stencil |
| 294 |
INTEGER, INTENT(IN) :: unit |
| 295 |
CHARACTER(*), INTENT(IN) :: iotype |
| 296 |
INTEGER, INTENT(IN) :: v_list(:) |
| 297 |
INTEGER, INTENT(OUT) :: iostat |
| 298 |
CHARACTER(*), INTENT(INOUT) :: iomsg |
| 299 |
integer(ATLAS_KIND_IDX) :: jlat, jlon |
| 300 |
do jlat = 1, stencil%width |
| 301 |
write(unit,'(a,I0,a)',advance='no',IOSTAT=iostat) " j: ", stencil%j(jlat), " i = " |
| 302 |
do jlon = 1, stencil%width |
| 303 |
write(unit,'(I3)',advance='no',IOSTAT=iostat) stencil%i(jlon,jlat) |
| 304 |
enddo |
| 305 |
write(0,'(a)',IOSTAT=iostat) new_line('a') |
| 306 |
enddo |
| 307 |
end subroutine |
| 308 |
|
| 309 |
function atlas_StructuredGrid_Stencil__j(this, j_index) result(j) |
| 310 |
integer(ATLAS_KIND_IDX) :: j |
| 311 |
class(atlas_StructuredGrid_Stencil), intent(in) :: this |
| 312 |
integer(ATLAS_KIND_IDX) :: j_index |
| 313 |
j = this%j_begin + (j_index-1) |
| 314 |
end function |
| 315 |
|
| 316 |
function atlas_StructuredGrid_Stencil__i_int32(this, i_index, j_index) result(i) |
| 317 |
use, intrinsic :: iso_c_binding, only : c_int |
| 318 |
integer(ATLAS_KIND_IDX) :: i |
| 319 |
class(atlas_StructuredGrid_Stencil), intent(in) :: this |
| 320 |
integer(c_int) :: i_index |
| 321 |
integer(ATLAS_KIND_IDX) :: j_index |
| 322 |
i = this%i_begin(j_index) + (i_index-1) |
| 323 |
end function |
| 324 |
|
| 325 |
function atlas_StructuredGrid_Stencil__i_int64(this, i_index, j_index) result(i) |
| 326 |
use, intrinsic :: iso_c_binding, only : c_long |
| 327 |
integer(ATLAS_KIND_IDX) :: i |
| 328 |
class(atlas_StructuredGrid_Stencil), intent(in) :: this |
| 329 |
integer(c_long) :: i_index |
| 330 |
integer(ATLAS_KIND_IDX) :: j_index |
| 331 |
i = this%i_begin(j_index) + (i_index-1) |
| 332 |
end function |
| 333 |
|
| 334 |
! ---------------------------------------------------------------------------------------- |
| 335 |
|
| 336 |
function atlas_StructuredGrid_ComputeStencil__ctor(grid, stencil_width) result(this) |
| 337 |
use atlas_grid_module, only : atlas_StructuredGrid |
| 338 |
implicit none |
| 339 |
type(atlas_StructuredGrid_ComputeStencil) :: this |
| 340 |
class(atlas_StructuredGrid), intent(in) :: grid |
| 341 |
integer(ATLAS_KIND_IDX), intent(in) :: stencil_width |
| 342 |
call this%setup(grid, stencil_width) |
| 343 |
end function |
| 344 |
|
| 345 |
|
| 346 |
subroutine atlas_StructuredGrid_ComputeStencil__assignment(this, other) |
| 347 |
implicit none |
| 348 |
class(atlas_StructuredGrid_ComputeStencil), intent(inout) :: this |
| 349 |
class(atlas_StructuredGrid_ComputeStencil), intent(in) :: other |
| 350 |
call this%final() |
| 351 |
write(0,*) "owners = ", other%compute_north%owners() |
| 352 |
this%compute_north = other%compute_north |
| 353 |
this%compute_west = other%compute_west |
| 354 |
this%stencil_width = other%stencil_width |
| 355 |
this%stencil_offset = other%stencil_offset |
| 356 |
this%halo = other%halo |
| 357 |
end subroutine |
| 358 |
|
| 359 |
subroutine atlas_StructuredGrid_ComputeStencil__final(this) |
| 360 |
class(atlas_StructuredGrid_ComputeStencil), intent(inout) :: this |
| 361 |
call this%compute_north%final() |
| 362 |
call this%compute_west%final() |
| 363 |
end subroutine |
| 364 |
|
| 365 |
! ---------------------------------------------------------------------------------------- |
| 366 |
|
| 367 |
end module atlas_StencilComputer_module |