FV3 Bundle
unstructured_grid_mod.F90
Go to the documentation of this file.
1 ! (C) Copyright 2017 UCAR
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 
6 !> Fortran module for handling generic unstructured grid
7 
9 
10 use iso_c_binding
11 use config_mod
12 use kinds
13 
14 implicit none
15 private
18 
19 ! ------------------------------------------------------------------------------
20 
21 !> Derived type containing the data
22 
24  integer :: igrid !> Index of the grid
25  integer :: nmga !> Number of gridpoints (on a given MPI task)
26  integer :: nl0 !> Number of levels
27  integer :: nv !> Number of variables
28  integer :: nts !> Number of timeslots
29  real(kind=kind_real),allocatable :: lon(:) !> Longitude (in degrees: -180 to 180)
30  real(kind=kind_real),allocatable :: lat(:) !> Latitude (in degrees: -90 to 90)
31  real(kind=kind_real),allocatable :: area(:) !> Area (in m^2)
32  real(kind=kind_real), allocatable :: vunit(:,:) !> Vertical unit
33  logical,allocatable :: lmask(:,:) !> Mask
34  real(kind=kind_real),allocatable :: fld(:,:,:,:) !> Data
35 end type grid_type
36 
38  integer :: colocated !> Colocation flag
39  integer :: ngrid !> Number of different grids
40  type(grid_type),allocatable :: grid(:) !> Grid instance
41 end type unstructured_grid
42 
43 ! ------------------------------------------------------------------------------
44 
45 #define LISTED_TYPE unstructured_grid
46 
47 !> Linked list interface - defines registry_t type
48 #include "oops/util/linkedList_i.f"
49 
50 !> Global registry
51 type(registry_t) :: unstructured_grid_registry
52 
53 !-------------------------------------------------------------------------------
54 contains
55 !-------------------------------------------------------------------------------
56 
57 !> Linked list implementation
58 #include "oops/util/linkedList_c.f"
59 
60 ! ------------------------------------------------------------------------------
61 ! C++ interfaces
62 ! ------------------------------------------------------------------------------
63 
64 subroutine create_ug_c(key) bind(c, name='create_ug_f90')
65 implicit none
66 integer(c_int), intent(inout) :: key
67 
69 call unstructured_grid_registry%add(key)
70 
71 end subroutine
72 
73 ! ------------------------------------------------------------------------------
74 
75 subroutine delete_ug_c(key) bind(c, name='delete_ug_f90')
76 implicit none
77 integer(c_int), intent(inout) :: key
78 
79 type(unstructured_grid), pointer :: self
80 
81 call unstructured_grid_registry%get(key,self)
82 call delete_unstructured_grid(self)
83 call unstructured_grid_registry%remove(key)
84 
85 end subroutine
86 
87 ! ------------------------------------------------------------------------------
88 
89 subroutine allocate_unstructured_grid_coord(self)
90 implicit none
91 type(unstructured_grid), intent(inout) :: self
92 integer :: igrid
93 
94 ! Allocation
95 do igrid=1,self%ngrid
96  if (.not.allocated(self%grid(igrid)%lon)) allocate(self%grid(igrid)%lon(self%grid(igrid)%nmga))
97  if (.not.allocated(self%grid(igrid)%lat)) allocate(self%grid(igrid)%lat(self%grid(igrid)%nmga))
98  if (.not.allocated(self%grid(igrid)%area)) allocate(self%grid(igrid)%area(self%grid(igrid)%nmga))
99  if (.not.allocated(self%grid(igrid)%vunit)) allocate(self%grid(igrid)%vunit(self%grid(igrid)%nmga,self%grid(igrid)%nl0))
100  if (.not.allocated(self%grid(igrid)%lmask)) allocate(self%grid(igrid)%lmask(self%grid(igrid)%nmga,self%grid(igrid)%nl0))
101 enddo
102 
104 
105 ! ------------------------------------------------------------------------------
106 
107 subroutine allocate_unstructured_grid_field(self)
108 implicit none
109 type(unstructured_grid), intent(inout) :: self
110 integer :: igrid
111 
112 ! Allocation
113 do igrid=1,self%ngrid
114  if (.not.allocated(self%grid(igrid)%fld)) allocate(self%grid(igrid)%fld(self%grid(igrid)%nmga,self%grid(igrid)%nl0, &
115  & self%grid(igrid)%nv,self%grid(igrid)%nts))
116 enddo
117 
119 
120 !-------------------------------------------------------------------------------
121 
122 subroutine delete_unstructured_grid(self)
123 implicit none
124 type(unstructured_grid), intent(inout) :: self
125 integer :: igrid
126 
127 ! Release memory
128 if (allocated(self%grid)) then
129  do igrid=1,self%ngrid
130  if (allocated(self%grid(igrid)%lon)) deallocate(self%grid(igrid)%lon)
131  if (allocated(self%grid(igrid)%lat)) deallocate(self%grid(igrid)%lat)
132  if (allocated(self%grid(igrid)%area)) deallocate(self%grid(igrid)%area)
133  if (allocated(self%grid(igrid)%vunit)) deallocate(self%grid(igrid)%vunit)
134  if (allocated(self%grid(igrid)%lmask)) deallocate(self%grid(igrid)%lmask)
135  if (allocated(self%grid(igrid)%fld)) deallocate(self%grid(igrid)%fld)
136  enddo
137  deallocate(self%grid)
138 endif
139 
140 end subroutine delete_unstructured_grid
141 
142 !-------------------------------------------------------------------------------
143 
144 end module unstructured_grid_mod
subroutine, public allocate_unstructured_grid_coord(self)
subroutine create_ug_c(key)
Linked list implementation.
subroutine, public allocate_unstructured_grid_field(self)
type(registry_t), public unstructured_grid_registry
Linked list interface - defines registry_t type.
Fortran module for handling generic unstructured grid.
subroutine, public delete_unstructured_grid(self)
Derived type containing the data.