Skip to content

Commit c6f8f11

Browse files
authored
Fixes #3961 statistics gridcomp (#4285)
* - Preserved order of children (order added) * Updating expectations in scenarios. New change results in most unused fields being GRIDSET rather than EMPTY. It is possible that this is not strictly necessary, but seems harmless. * Fixed error handling. - Functions that return allocatable objects must ensure that it is always allocated - even when errors occur. - Improved error message for rank > 4. * Eliminate use of deprecated procedure. * Updated test scenarios. These mostly are due to the fact that unused exports now generally have field status of GRIDSET where before they were EMPTY. This is probably fixable if it is important, but a shallow copy of an ESMF geom seems minor. * Various updates to Field/Bundle access Lots of small changes to ensure compatibility with retrieval to/from state item aspects. * Eliminate unused file. (Functionality is elsewhere now.) * Return POINTER instead of allocatable. Necessary due to use of VerticalGridManager now. * Should have been part of earlier commit. * Important fix - null terminated strings. Might be the fix for #3343. Empirically seems true. * No change in functionality. Was reluctant to delete hard won understanding of useful debugging info. * No change in functionality. Useful bit for debugging. Also should migrate to pflogger. * Vgrid is now POINTER. * Very important change. We need the spec to be properly updated from the payload before analyzing for extensions. This line could possibly move elsewhere though. * Updated to reflect new reliance on ESMF info. Still lots of code that should now be deletable. Next pass. * Added useful debug helper procedure. * Various changes. - updated payload at appropriate point - new diagnostic procedure (print_spec) - pass through treatment of deferred aspects (must be in info now) * Added NOT_FOUND option for grid manager. * Subelements of bundles need another step now. Some refactoring should reduce redundancy. * Various fixes to support use of "info" approach - need to use POINTER in some contexts - more care with detecting mirror case * Added diagnostic procedures. * Added tests for ComponentSpec. * Fields default to GRIDSET now. (was EMPTY) * Use VerticalGridManager now. * Activated statistics_real use case. - Basic use case now functions correctly. - Reordered tests to isolate the ones that sometimes trigger NAG "MAPL_Verify()" bug. (Hopefully now resolved.) * Corrected use of alarm. * Created way to allow for mirrored UngriddedDims. All aspects need superior testing now. * Updated test logic. Works differently now with info and grid manager. * Added necessary step to update payload of goal spec. * Eliminated use of "modify". New FieldSet and FieldBundleSet replace that functionality. (Separate PR will delete the legacy layer.) * Appears to work now. - eleminated some print statements - needs more tests * Added constructor for ModelVerticalGridSpec Necessary for use with VerticalGridManager. * Fixed capitalization issue. Library name error was missed due to case-insensive file system in development environment. * Missed that name is used in one more spot. * Measure twice cut once. * Minor improvements - Not crucial, but found a few irregularities while investigating a different issue. * Simplified support for RegridderParam Retrieving params in FieldSetGet was causing cyclic dependencies. So now we put the burden on the client code to translate to/from an info object. * Fixed yaml. (unused lines) * Missed some earlier commits. * Remove debug prints. * Fixed uninitialized value issue. Surprisingly only showed up with gfortran + release. * Update field/FieldInfo.F90
1 parent b63e778 commit c6f8f11

File tree

62 files changed

+884
-301
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

62 files changed

+884
-301
lines changed

esmf_utils/UngriddedDims.F90

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,14 @@ module mapl3g_UngriddedDims
5252

5353
contains
5454

55-
function new_UngriddedDims_empty() result(spec)
55+
function new_UngriddedDims_empty(is_mirror) result(spec)
5656
type(UngriddedDims) :: spec
57+
logical, optional, intent(in) :: is_mirror
5758

5859
spec%dim_specs = UngriddedDimVector()
60+
if (present(is_mirror)) then
61+
spec%is_mirror = is_mirror
62+
end if
5963

6064
end function new_UngriddedDims_empty
6165

field/FieldCreate.F90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ subroutine field_empty_complete(field, &
112112
allocate(grid_to_field_map(dim_count), source=[(idim, idim=1,dim_count)])
113113
end if
114114
bounds = make_bounds(num_levels=num_levels, ungridded_dims=ungridded_dims)
115+
115116
call ESMF_FieldEmptyComplete( &
116117
field, &
117118
typekind=typekind, &

field/FieldGet.F90

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ subroutine field_get(field, unusable, &
2828
ungridded_dims, &
2929
units, standard_name, long_name, &
3030
allocation_status, &
31+
has_deferred_aspects, &
32+
regridder_param_info, &
3133
rc)
3234
type(ESMF_Field), intent(in) :: field
3335
class(KeywordEnforcer), optional, intent(in) :: unusable
@@ -43,13 +45,15 @@ subroutine field_get(field, unusable, &
4345
character(len=:), optional, allocatable, intent(out) :: standard_name
4446
character(len=:), optional, allocatable, intent(out) :: long_name
4547
type(StateItemAllocation), optional, intent(out) :: allocation_status
48+
logical, optional, intent(out) :: has_deferred_aspects
49+
type(esmf_Info), optional, allocatable, intent(out) :: regridder_param_info
4650
integer, optional, intent(out) :: rc
4751

4852
integer :: status
4953
type(ESMF_Info) :: field_info
5054
character(len=ESMF_MAXSTR) :: fname
5155
type(ESMF_FieldStatus_Flag) :: fstatus
52-
integer, allocatable :: vgrid_id
56+
integer :: vgrid_id
5357
type(VerticalGridManager), pointer :: vgrid_manager
5458

5559
if (present(short_name)) then
@@ -68,28 +72,31 @@ subroutine field_get(field, unusable, &
6872
end if
6973
end if
7074

71-
if (present(vgrid)) then
72-
allocate(vgrid_id) ! trigger "is present"
73-
end if
74-
7575
if (present(typekind)) then
76-
call ESMF_FieldGet(field, typekind=typekind, _RC)
76+
!# call ESMF_FieldGet(field, typekind=typekind, _RC)
7777
end if
7878

7979
call ESMF_InfoGetFromHost(field, field_info, _RC)
8080
call FieldInfoGetInternal(field_info, &
81+
typekind=typekind, &
8182
num_levels=num_levels, &
8283
vert_staggerloc=vert_staggerloc, &
8384
num_vgrid_levels=num_vgrid_levels, &
8485
ungridded_dims=ungridded_dims, &
8586
units=units, standard_name=standard_name, long_name=long_name, &
86-
allocation_status=allocation_status, &
8787
vgrid_id=vgrid_id, &
88+
allocation_status=allocation_status, &
89+
has_deferred_aspects=has_deferred_aspects, &
90+
regridder_param_info=regridder_param_info, &
8891
_RC)
8992

9093
if (present(vgrid)) then
91-
vgrid_manager => get_vertical_grid_manager()
92-
vgrid => vgrid_manager%get_grid(id=vgrid_id, _RC)
94+
if (vgrid_id == VERTICAL_GRID_NOT_FOUND) then
95+
vgrid => null()
96+
else
97+
vgrid_manager => get_vertical_grid_manager()
98+
vgrid => vgrid_manager%get_grid(id=vgrid_id, _RC)
99+
end if
93100
end if
94101

95102
_RETURN(_SUCCESS)

field/FieldInfo.F90

Lines changed: 61 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module mapl3g_FieldInfo
66
use mapl3g_esmf_info_keys, only: INFO_INTERNAL_NAMESPACE
77
use mapl3g_esmf_info_keys, only: INFO_PRIVATE_NAMESPACE
88
use mapl3g_InfoUtilities
9+
use mapl3g_VerticalGrid_API
910
use mapl3g_UngriddedDims
1011
use mapl3g_VerticalStaggerLoc
1112
use mapl3g_StateItemAllocation
@@ -59,13 +60,15 @@ module mapl3g_FieldInfo
5960
character(*), parameter :: KEY_VERT_DIM = "/vert_dim"
6061
character(*), parameter :: KEY_UNGRIDDED_DIMS = "/ungridded_dims"
6162
character(*), parameter :: KEY_ALLOCATION_STATUS = "/allocation_status"
63+
character(*), parameter :: KEY_REGRIDDER_PARAM = "/EsmfRegridderParam"
6264

6365
character(*), parameter :: KEY_UNDEF_VALUE = "/undef_value"
6466
character(*), parameter :: KEY_MISSING_VALUE = "/missing_value"
6567
character(*), parameter :: KEY_FILL_VALUE = "/_FillValue"
6668

6769
character(*), parameter :: KEY_SPEC_HANDLE = "/spec_handle"
6870
character(*), parameter :: KEY_RESTART_MODE = "/restart_mode"
71+
character(*), parameter :: KEY_HAS_DEFERRED_ASPECTS = "/has_deferred_aspects"
6972

7073
contains
7174

@@ -75,9 +78,11 @@ subroutine field_info_set_internal(info, unusable, &
7578
num_levels, vert_staggerloc, &
7679
ungridded_dims, &
7780
units, long_name, standard_name, &
78-
allocation_status, &
7981
vgrid_id, &
8082
spec_handle, &
83+
allocation_status, &
84+
has_deferred_aspects, &
85+
regridder_param_info, &
8186
rc)
8287
type(ESMF_Info), intent(inout) :: info
8388
class(KeywordEnforcer), optional, intent(in) :: unusable
@@ -91,7 +96,9 @@ subroutine field_info_set_internal(info, unusable, &
9196
character(*), optional, intent(in) :: long_name
9297
character(*), optional, intent(in) :: standard_name
9398
type(StateItemAllocation), optional, intent(in) :: allocation_status
99+
logical, optional, intent(in) :: has_deferred_aspects
94100
integer, optional, intent(in) :: spec_handle(:)
101+
type(esmf_info), optional, intent(in) :: regridder_param_info
95102
integer, optional, intent(out) :: rc
96103

97104
integer :: status
@@ -116,6 +123,7 @@ subroutine field_info_set_internal(info, unusable, &
116123
if (present(ungridded_dims)) then
117124
ungridded_info = ungridded_dims%make_info(_RC)
118125
call MAPL_InfoSet(info, namespace_ // KEY_UNGRIDDED_DIMS, ungridded_info, _RC)
126+
call esmf_InfoDestroy(ungridded_info, _RC)
119127
end if
120128

121129
if (present(units)) then
@@ -134,13 +142,18 @@ subroutine field_info_set_internal(info, unusable, &
134142
call MAPL_InfoSet(info, namespace_ // KEY_NUM_LEVELS, num_levels, _RC)
135143
end if
136144

145+
if (present(regridder_param_info)) then
146+
call MAPL_InfoSet(info, namespace_ // KEY_REGRIDDER_PARAM, regridder_param_info, _RC)
147+
_HERE
148+
end if
149+
137150
if (present(vert_staggerloc)) then
138151
call MAPL_InfoSet(info, namespace_ // KEY_VERT_STAGGERLOC, vert_staggerloc%to_string(), _RC)
139152

140153
! Delete later - needed for transition
141154

142155
if (present(num_levels) .and. present(vert_staggerloc)) then
143-
156+
144157
if (vert_staggerLoc == VERTICAL_STAGGER_NONE) then
145158
call MAPL_InfoSet(info, namespace_ // KEY_VERT_DIM, "VERTICAL_DIM_NONE", _RC)
146159
call MAPL_InfoSet(info, namespace_ // KEY_NUM_VGRID_LEVELS, 0, _RC)
@@ -161,6 +174,10 @@ subroutine field_info_set_internal(info, unusable, &
161174
call MAPL_InfoSet(info, namespace_ // KEY_ALLOCATION_STATUS, allocation_status%to_string(), _RC)
162175
end if
163176

177+
if (present(has_deferred_aspects)) then
178+
call MAPL_InfoSet(info, namespace_ // KEY_HAS_DEFERRED_ASPECTS, has_deferred_aspects, _RC)
179+
end if
180+
164181
if (present(spec_handle)) then
165182
call MAPL_InfoSet(info, namespace_ // KEY_SPEC_HANDLE, spec_handle, _RC)
166183
end if
@@ -179,6 +196,8 @@ subroutine field_info_get_internal(info, unusable, &
179196
ungridded_dims, &
180197
allocation_status, &
181198
spec_handle, &
199+
has_deferred_aspects, &
200+
regridder_param_info, &
182201
rc)
183202
type(ESMF_Info), intent(in) :: info
184203
class(KeywordEnforcer), optional, intent(in) :: unusable
@@ -194,16 +213,17 @@ subroutine field_info_get_internal(info, unusable, &
194213
type(UngriddedDims), optional, intent(out) :: ungridded_dims
195214
type(StateItemAllocation), optional, intent(out) :: allocation_status
196215
integer, optional, allocatable, intent(out) :: spec_handle(:)
216+
logical, optional, intent(out) :: has_deferred_aspects
217+
type(esmf_Info), allocatable, optional, intent(out) :: regridder_param_info
197218
integer, optional, intent(out) :: rc
198219

199220
integer :: status
200221
integer :: num_levels_
201-
type(ESMF_Info) :: ungridded_info
222+
type(esmf_Info) :: ungridded_info
202223
character(:), allocatable :: vert_staggerloc_str, allocation_status_str
203224
type(VerticalStaggerLoc) :: vert_staggerloc_
204225
character(:), allocatable :: namespace_
205226
character(:), allocatable :: str
206-
logical :: key_is_present
207227
logical :: is_present
208228

209229
namespace_ = INFO_INTERNAL_NAMESPACE
@@ -212,7 +232,8 @@ subroutine field_info_get_internal(info, unusable, &
212232
end if
213233

214234
if (present(vgrid_id)) then
215-
call mapl_InfoGet(info, namespace_ // KEY_VGRID_ID, vgrid_id, _RC)
235+
call esmf_InfoGet(info, key=namespace_ // KEY_VGRID_ID, &
236+
value=vgrid_id, default=VERTICAL_GRID_NOT_FOUND, _RC)
216237
end if
217238

218239
if (present(typekind)) then
@@ -221,14 +242,32 @@ subroutine field_info_get_internal(info, unusable, &
221242
end if
222243

223244
if (present(ungridded_dims)) then
224-
ungridded_info = ESMF_InfoCreate(info, namespace_ // KEY_UNGRIDDED_DIMS, _RC)
225-
ungridded_dims = make_UngriddedDims(ungridded_info, _RC)
245+
is_present = esmf_InfoIsPresent(info, namespace_ // KEY_UNGRIDDED_DIMS, _RC)
246+
if (is_present) then
247+
ungridded_info = ESMF_InfoCreate(info, namespace_ // KEY_UNGRIDDED_DIMS, _RC)
248+
ungridded_dims = make_UngriddedDims(ungridded_info, _RC)
249+
call esmf_InfoDestroy(ungridded_info, _RC)
250+
else
251+
ungridded_dims = UngriddedDims(is_mirror=.true.)
252+
end if
253+
end if
254+
255+
if (present(regridder_param_info)) then
256+
is_present = esmf_InfoIsPresent(info, namespace_ // KEY_REGRIDDER_PARAM, _RC)
257+
if (is_present) then
258+
regridder_param_info = esmf_InfoCreate(info, namespace_ // KEY_REGRIDDER_PARAM, _RC)
259+
end if
226260
end if
227261

228262
if (present(num_levels) .or. present(num_vgrid_levels)) then
229-
call MAPL_InfoGet(info, namespace_ // KEY_NUM_LEVELS, num_levels_, _RC)
230-
if (present(num_levels)) then
231-
num_levels = num_levels_
263+
is_present = esmf_InfoIsPresent(info, namespace_ // KEY_NUM_LEVELS, _RC)
264+
if (is_present) then
265+
call MAPL_InfoGet(info, namespace_ // KEY_NUM_LEVELS, num_levels_, _RC)
266+
if (present(num_levels)) then
267+
num_levels = num_levels_
268+
end if
269+
else
270+
num_levels = 0
232271
end if
233272
end if
234273

@@ -242,7 +281,7 @@ subroutine field_info_get_internal(info, unusable, &
242281

243282
if (present(num_vgrid_levels)) then
244283
if (vert_staggerloc_ == VERTICAL_STAGGER_NONE) then
245-
num_vgrid_levels = 0
284+
num_vgrid_levels = 0 ! num_levels_ must not be used here
246285
else if (vert_staggerloc_ == VERTICAL_STAGGER_EDGE) then
247286
num_vgrid_levels = num_levels_ - 1
248287
else if (vert_staggerloc_ == VERTICAL_STAGGER_CENTER) then
@@ -252,8 +291,11 @@ subroutine field_info_get_internal(info, unusable, &
252291
end if
253292
end if
254293

255-
if (present(units)) then
256-
call MAPL_InfoGet(info, namespace_ // KEY_UNITS, units, _RC)
294+
if (present(units)) then ! leave unallocated unless found
295+
is_present = esmf_InfoIsPresent(info, key=namespace_ // KEY_UNITS, _RC)
296+
if (is_present) then
297+
call MAPL_InfoGet(info, namespace_ // KEY_UNITS, units, _RC)
298+
end if
257299
end if
258300

259301
if (present(long_name)) then
@@ -273,7 +315,12 @@ subroutine field_info_get_internal(info, unusable, &
273315
call MAPL_InfoGet(info, namespace_ // KEY_SPEC_HANDLE, spec_handle, _RC)
274316
end if
275317

276-
_RETURN(_SUCCESS)
318+
if (present(has_deferred_aspects)) then
319+
call esmf_InfoGet(info, key=namespace_ // KEY_HAS_DEFERRED_ASPECTS, &
320+
value=has_deferred_aspects, default=.false., _RC)
321+
end if
322+
323+
_RETURN(_SUCCESS)
277324
_UNUSED_DUMMY(unusable)
278325
end subroutine field_info_get_internal
279326

field/FieldPointerUtilities.F90

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,10 @@ function get_local_element_count(x, rc) result(element_count)
383383
integer :: status
384384
integer :: rank
385385

386+
element_count = [integer :: ] ! must allocate even under failure
386387
call ESMF_FieldGet(x, rank=rank, _RC)
388+
389+
deallocate(element_count)
387390
allocate(element_count(rank))
388391
! ESMF has a big fat bug with multi tile grids and loal element count
389392
!call ESMF_FieldGet(x, localElementCount=element_count, _RC)
@@ -482,7 +485,6 @@ subroutine clone(x, y, rc)
482485
call ESMF_InfoGetFromHost(x, x_info, _RC)
483486
call ESMF_InfoGetFromHost(y, y_info, _RC)
484487
call ESMF_InfoUpdate(y_info, x_info, recursive=.true., _RC)
485-
y_info = x_info
486488

487489
_RETURN(_SUCCESS)
488490
end subroutine clone
@@ -891,8 +893,13 @@ subroutine MAPL_FieldGetLocalElementCount(field,local_count,rc)
891893
integer(kind=ESMF_KIND_I4), pointer :: i4_1d(:),i4_2d(:,:),i4_3d(:,:,:),i4_4d(:,:,:,:)
892894
integer(kind=ESMF_KIND_I8), pointer :: i8_1d(:),i8_2d(:,:),i8_3d(:,:,:),i8_4d(:,:,:,:)
893895

896+
integer, parameter :: MAX_RANK = 4
897+
898+
local_count = [integer :: ] ! default in case of failure
894899
call ESMF_FieldGet(field,rank=rank,typekind=tk,_RC)
900+
_ASSERT(rank <= MAX_RANK, 'Need to extend FieldPointerUtilities for rank > 4.')
895901

902+
deallocate(local_count)
896903
if (tk == ESMF_TypeKind_R4) then
897904
select case(rank)
898905
case(1)

field/FieldSet.F90

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ module mapl3g_FieldSet
44
use mapl3g_VerticalGrid_API
55
use mapl3g_FieldInfo
66
use mapl3g_FieldDelta
7+
use mapl3g_StateItemAllocation
78
use mapl_KeywordEnforcer
89
use mapl_ErrorHandling
910
use mapl3g_UngriddedDims
@@ -28,9 +29,12 @@ subroutine field_set(field, &
2829
typekind, &
2930
unusable, &
3031
num_levels, &
31-
units, &
32+
units, standard_name, long_name, &
3233
ungridded_dims, &
3334
attributes, &
35+
allocation_status, &
36+
has_deferred_aspects, &
37+
regridder_param_info, &
3438
rc)
3539

3640

@@ -42,8 +46,13 @@ subroutine field_set(field, &
4246
type(esmf_TypeKind_Flag), optional, intent(in) :: typekind
4347
integer, optional, intent(in) :: num_levels
4448
character(len=*), optional, intent(in) :: units
49+
character(len=*), optional, intent(in) :: standard_name
50+
character(len=*), optional, intent(in) :: long_name
4551
type(UngriddedDims), optional, intent(in) :: ungridded_dims
4652
type(StringVector), optional, intent(in) :: attributes
53+
type(StateItemAllocation), optional, intent(in) :: allocation_status
54+
logical, optional, intent(in) :: has_deferred_aspects
55+
type(esmf_Info), optional, intent(in) :: regridder_param_info
4756
integer, optional, intent(out) :: rc
4857

4958
integer :: status
@@ -58,6 +67,11 @@ subroutine field_set(field, &
5867
call field_delta%update_field(field, _RC)
5968
end if
6069

70+
if (fstatus /= ESMF_FIELDSTATUS_COMPLETE .and. present(geom)) then
71+
call esmf_FieldEmptyReset(field, status=ESMF_FIELDSTATUS_EMPTY, _RC)
72+
call esmf_FieldEmptySet(field, geom=geom, _RC)
73+
end if
74+
6175
if (present(vgrid)) then
6276
vgrid_id = vgrid%get_id() ! allocate so "present" below
6377
end if
@@ -66,8 +80,14 @@ subroutine field_set(field, &
6680
call FieldInfoSetInternal(field_info, &
6781
vgrid_id=vgrid_id, &
6882
vert_staggerloc=vert_staggerloc, &
69-
typekind=typekind, units=units, &
70-
ungridded_dims=ungridded_dims, _RC)
83+
num_levels=num_levels, &
84+
typekind=typekind, &
85+
units=units, standard_name=standard_name, long_name=long_name, &
86+
ungridded_dims=ungridded_dims, &
87+
allocation_status=allocation_status, &
88+
has_deferred_aspects=has_deferred_aspects, &
89+
regridder_param_info=regridder_param_info, &
90+
_RC)
7191

7292
_RETURN(_SUCCESS)
7393
end subroutine field_set

0 commit comments

Comments
 (0)