Skip to content

Commit ab5de00

Browse files
authored
Improve vector field naming and expose vector state item support (#4926)
* Vector field - derived field names from short name * Add mapl3g_Utilities to MAPL * Updated vector scenarios test * Add vector component support in MAPL_GridCompAddSpec * scenarios test vector_1 - revert to the original version * VectorClassAspec - mirroring short names
1 parent 26d2daa commit ab5de00

5 files changed

Lines changed: 79 additions & 80 deletions

File tree

mapl3g/MAPL.F90

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ module MAPL
1212
use mapl3g_UngriddedDims, only: UngriddedDims
1313
use mapl3g_Field_API
1414
use mapl3g_FieldBundle_API
15+
use mapl3g_Utilities
1516
use MAPL_PythonBridge
1617
use mapl_base3g
1718
use mapl_Profiler, initialize_profiler => initialize, finalize_profiler => finalize

superstructure/generic/MAPL_Generic.F90

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ module mapl3g_Generic
3333
use mapl3g_HorizontalDimsSpec, only: HorizontalDimsSpec, HORIZONTAL_DIMS_NONE, HORIZONTAL_DIMS_GEOM
3434
use mapl3g_UngriddedDim, only: UngriddedDim
3535
use mapl3g_UngriddedDims, only: UngriddedDims
36-
use mapl3g_StateItem, only: MAPL_STATEITEM_STATE, MAPL_STATEITEM_FIELDBUNDLE, MAPL_STATEITEM_SERVICE
36+
use mapl3g_StateItem, only: MAPL_STATEITEM_STATE, MAPL_STATEITEM_FIELDBUNDLE
37+
use mapl3g_StateItem, only: MAPL_STATEITEM_SERVICE, MAPL_STATEITEM_VECTOR
3738
use mapl3g_ESMF_Utilities, only: esmf_state_intent_to_string
3839
use mapl3g_ESMF_Interfaces, only: MAPL_UserCompGetInternalState, MAPL_UserCompSetInternalState
3940
use mapl3g_hconfig_get
@@ -108,7 +109,8 @@ module mapl3g_Generic
108109
public :: MAPL_GridCompTimerStop
109110

110111
! Spec types
111-
public :: MAPL_STATEITEM_STATE, MAPL_STATEITEM_FIELDBUNDLE, MAPL_STATEITEM_SERVICE
112+
public :: MAPL_STATEITEM_STATE, MAPL_STATEITEM_FIELDBUNDLE
113+
public :: MAPL_STATEITEM_SERVICE, MAPL_STATEITEM_VECTOR
112114

113115
public :: MAPL_UserCompGetInternalState, MAPL_UserCompSetInternalState
114116

@@ -549,6 +551,7 @@ subroutine gridcomp_add_spec( &
549551
vstagger, &
550552
! OPTIONAL
551553
unusable, &
554+
vector_component_names, &
552555
ungridded_dims, &
553556
units, &
554557
restart, &
@@ -568,6 +571,7 @@ subroutine gridcomp_add_spec( &
568571
type(VerticalStaggerLoc), intent(in) :: vstagger
569572
! OPTIONAL
570573
class(KeywordEnforcer), optional, intent(in) :: unusable
574+
character(*), optional, intent(in) :: vector_component_names(:)
571575
type(UngriddedDim), optional, intent(in) :: ungridded_dims(:)
572576
character(*), optional, intent(in) :: units
573577
type(RestartMode), optional, intent(in) :: restart
@@ -586,6 +590,7 @@ subroutine gridcomp_add_spec( &
586590
type(ComponentSpec), pointer :: component_spec
587591
character(len=:), allocatable :: units_
588592
type(UngriddedDims), allocatable :: dim_specs_vec
593+
type(StringVector) :: vector_component_names_vec
589594
integer :: status
590595

591596
_ASSERT((dims=="xyz") .or. (dims=="xy") .or. (dims=="z"), "dims can be one of xyz/xy/z")
@@ -597,6 +602,14 @@ subroutine gridcomp_add_spec( &
597602
! If input units is present, override using input values
598603
if (present(units)) units_ = units
599604
if (present(ungridded_dims)) dim_specs_vec = UngriddedDims(ungridded_dims)
605+
! vector_component_names
606+
if (present(vector_component_names)) then
607+
_ASSERT(present(itemType), "itemType must be present if vector_component_names is present")
608+
_ASSERT((itemType==MAPL_STATEITEM_VECTOR), "valid only for vector items")
609+
_ASSERT((size(vector_component_names)==2), "vector_component_names must have 2 components")
610+
call vector_component_names_vec%push_back(trim(vector_component_names(1)))
611+
call vector_component_names_vec%push_back(trim(vector_component_names(2)))
612+
end if
600613
var_spec = make_VariableSpec( &
601614
state_intent, &
602615
short_name, &
@@ -611,6 +624,7 @@ subroutine gridcomp_add_spec( &
611624
has_deferred_aspects=has_deferred_aspects, &
612625
service_items=service_items, &
613626
restart_mode=restart, &
627+
vector_component_names=vector_component_names_vec, &
614628
_RC)
615629
call MAPL_GridCompGetOuterMeta(gridcomp, outer_meta, _RC)
616630
component_spec => outer_meta%get_component_spec()

superstructure/generic/RestartHandler.F90

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ subroutine write_bundle_(this, bundle, filename, rc)
116116
call o_Clients%post_wait()
117117

118118
_RETURN(_SUCCESS)
119-
120119
end subroutine write_bundle_
121120

122121
subroutine read_bundle_(this, filename, bundle, rc)

superstructure/generic/specs/VariableSpec.F90

Lines changed: 37 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function make_VariableSpec( &
222222

223223
integer :: status
224224

225-
var_spec%short_name = short_name
225+
var_spec%short_name = short_name
226226
var_spec%state_intent = state_intent
227227

228228
#if defined(_SET_OPTIONAL)
@@ -248,15 +248,15 @@ function make_VariableSpec( &
248248
_SET_OPTIONAL(regrid_param)
249249
_SET_OPTIONAL(horizontal_dims_spec)
250250
_SET_OPTIONAL(vector_component_names)
251-
_SET_OPTIONAL(has_deferred_aspects)
252-
_SET_OPTIONAL(use_field_dictionary)
253-
_SET_OPTIONAL(restart_mode)
254-
255-
var_spec%vector_basis_kind = VECTOR_BASIS_KIND_NS
256-
if (present(vector_basis_kind)) then
257-
_ASSERT(any(var_spec%itemType == [MAPL_STATEITEM_VECTOR, MAPL_STATEITEM_VECTORBRACKET]), 'vector_basis_kind can only be specified for vectors')
258-
var_spec%vector_basis_kind = VectorBasisKind(vector_basis_kind)
259-
end if
251+
_SET_OPTIONAL(has_deferred_aspects)
252+
_SET_OPTIONAL(use_field_dictionary)
253+
_SET_OPTIONAL(restart_mode)
254+
255+
var_spec%vector_basis_kind = VECTOR_BASIS_KIND_NS
256+
if (present(vector_basis_kind)) then
257+
_ASSERT(any(var_spec%itemType == [MAPL_STATEITEM_VECTOR, MAPL_STATEITEM_VECTORBRACKET]), 'vector_basis_kind can only be specified for vectors')
258+
var_spec%vector_basis_kind = VectorBasisKind(vector_basis_kind)
259+
end if
260260

261261
if (var_spec%use_field_dictionary) then
262262
call apply_field_dictionary_defaults_(var_spec, short_name, standard_name, units, long_name, _RC)
@@ -647,10 +647,10 @@ function make_ClassAspect(this, registry, rc) result(aspect)
647647
type(StateRegistry), pointer, optional, intent(in) :: registry
648648
integer, optional, intent(out) :: rc
649649

650-
integer :: status
651-
character(:), allocatable :: std_name_1, std_name_2
652-
type(StringVector) :: vector_component_names
653-
type(VectorBasisKind) :: basis_kind
650+
integer :: status
651+
character(:), allocatable :: std_name_1, std_name_2
652+
type(StringVector) :: vector_component_names
653+
type(VectorBasisKind) :: basis_kind
654654

655655
select case (this%itemType%ot)
656656
case (MAPL_STATEITEM_FIELD%ot)
@@ -662,30 +662,29 @@ function make_ClassAspect(this, registry, rc) result(aspect)
662662
aspect = FieldBundleClassAspect(standard_name=this%standard_name)
663663
case (MAPL_STATEITEM_STATE%ot)
664664
aspect = StateClassAspect(state_intent=this%state_intent, standard_name=this%standard_name)
665-
case (MAPL_STATEITEM_VECTOR%ot)
666-
std_name_1 = 'unknown'
667-
std_name_2 = 'unknown'
668-
if (allocated(this%standard_name)) then
669-
call split_name(this%standard_name, std_name_1, std_name_2, _RC)
670-
end if
671-
if (this%vector_component_names%size() == 0) then
672-
call vector_component_names%push_back('unknown')
673-
call vector_component_names%push_back('unknown')
674-
else
675-
vector_component_names = this%vector_component_names
676-
end if
677-
678-
if (allocated(this%vector_basis_kind)) then
679-
basis_kind = this%vector_basis_kind
680-
else
681-
basis_kind = VECTOR_BASIS_KIND_NS
682-
end if
683-
aspect = VectorClassAspect(this%vector_component_names, &
684-
[ &
685-
FieldClassAspect(standard_name=std_name_1, fill_value=this%fill_value), &
686-
FieldClassAspect(standard_name=std_name_2, fill_value=this%fill_value) &
687-
], &
688-
basis_kind)
665+
case (MAPL_STATEITEM_VECTOR%ot)
666+
std_name_1 = 'unknown'
667+
std_name_2 = 'unknown'
668+
if (allocated(this%standard_name)) then
669+
call split_name(this%standard_name, std_name_1, std_name_2, _RC)
670+
end if
671+
if (this%vector_component_names%size() == 0) then
672+
call vector_component_names%push_back("unknown1")
673+
call vector_component_names%push_back("unknown2")
674+
else
675+
vector_component_names = this%vector_component_names
676+
end if
677+
if (allocated(this%vector_basis_kind)) then
678+
basis_kind = this%vector_basis_kind
679+
else
680+
basis_kind = VECTOR_BASIS_KIND_NS
681+
end if
682+
aspect = VectorClassAspect(this%vector_component_names, &
683+
[ &
684+
FieldClassAspect(standard_name=std_name_1, fill_value=this%fill_value), &
685+
FieldClassAspect(standard_name=std_name_2, fill_value=this%fill_value) &
686+
], &
687+
basis_kind)
689688
case (MAPL_STATEITEM_BRACKET%ot)
690689
aspect = BracketClassAspect(this%bracket_size, this%standard_name, fill_value=this%fill_value)
691690
case (MAPL_STATEITEM_VECTORBRACKET%ot)
@@ -707,7 +706,6 @@ function make_ClassAspect(this, registry, rc) result(aspect)
707706
end select
708707

709708
_RETURN(_SUCCESS)
710-
711709
end function make_ClassAspect
712710

713711
subroutine verify_variable_spec(spec, rc)

superstructure/generic/specs/VectorClassAspect.F90

Lines changed: 25 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "MAPL.h"
22

33
module mapl3g_VectorClassAspect
4+
45
use mapl3g_Field_API
56
use mapl3g_FieldBundle_API
67
use mapl3g_ActualConnectionPt
@@ -33,6 +34,7 @@ module mapl3g_VectorClassAspect
3334
use mapl_ErrorHandling
3435
use gftl2_StringVector
3536
use esmf
37+
3638
implicit none(type,external)
3739
private
3840

@@ -74,7 +76,6 @@ module mapl3g_VectorClassAspect
7476
procedure :: new_VectorClassAspect_basic
7577
end interface VectorClassAspect
7678

77-
7879
contains
7980

8081
function new_VectorClassAspect_basic(short_names, component_specs, basis_kind) result(aspect)
@@ -86,10 +87,8 @@ function new_VectorClassAspect_basic(short_names, component_specs, basis_kind) r
8687
aspect%short_names = short_names
8788
aspect%component_specs = component_specs
8889
aspect%basis_kind = basis_kind
89-
9090
end function new_VectorClassAspect_basic
9191

92-
9392
! Should always be the same as for Field
9493
function get_aspect_order(this, goal_aspects, rc) result(aspect_ids)
9594
type(AspectId), allocatable :: aspect_ids(:)
@@ -98,11 +97,10 @@ function get_aspect_order(this, goal_aspects, rc) result(aspect_ids)
9897
integer, optional, intent(out) :: rc
9998

10099
integer :: status
101-
100+
102101
aspect_ids = this%component_specs(1)%get_aspect_order(goal_aspects, _RC)
103102

104103
_RETURN(_SUCCESS)
105-
106104
_UNUSED_DUMMY(goal_aspects)
107105
end function get_aspect_order
108106

@@ -129,12 +127,12 @@ subroutine create(this, other_aspects, rc)
129127
type(ESMF_Info) :: info
130128

131129
this%payload = MAPL_FieldBundleCreate(fieldBundleType=FIELDBUNDLETYPE_VECTOR, _RC)
132-
130+
133131
call ESMF_InfoGetFromHost(this%payload, info, _RC)
134132
call MAPL_FieldBundleSet(this%payload, &
135-
allocation_status=STATEITEM_ALLOCATION_CREATED, &
136-
vector_basis_kind=this%basis_kind, &
137-
_RC)
133+
allocation_status=STATEITEM_ALLOCATION_CREATED, &
134+
vector_basis_kind=this%basis_kind, &
135+
_RC)
138136

139137
_RETURN(ESMF_SUCCESS)
140138
_UNUSED_DUMMY(other_aspects)
@@ -181,7 +179,7 @@ subroutine update_payload(field_aspect, other_aspects, rc)
181179
type(esmf_Field), allocatable :: field
182180

183181
call field_aspect%get_payload(field=field, _RC)
184-
182+
185183
associate(e => other_aspects%ftn_end())
186184
iter = other_aspects%ftn_begin()
187185
do while (iter /= e)
@@ -192,7 +190,6 @@ subroutine update_payload(field_aspect, other_aspects, rc)
192190
end associate
193191

194192
_RETURN(_SUCCESS)
195-
196193
end subroutine update_payload
197194

198195
subroutine destroy(this, rc)
@@ -239,27 +236,11 @@ subroutine connect_to_export(this, export, actual_pt, rc)
239236
call this%destroy(_RC) ! import is replaced by export/extension
240237
this%payload = export_%payload
241238

239+
! mirror short names since they are required in add_to_state routine
240+
this%short_names = export_%short_names
241+
242242
_RETURN(_SUCCESS)
243243
_UNUSED_DUMMY(actual_pt)
244-
245-
contains
246-
247-
subroutine mirror(dst, src)
248-
real, allocatable, intent(inout) :: dst
249-
real, allocatable, intent(in) :: src
250-
251-
if (.not. allocated(src)) return
252-
253-
if (.not. allocated(dst)) then
254-
dst = src
255-
return
256-
end if
257-
258-
! TODO: Problematic case: both allocated with different values.
259-
if (dst /= src) then
260-
end if
261-
end subroutine mirror
262-
263244
end subroutine connect_to_export
264245

265246
function to_vectorclassaspect_from_poly(aspect, rc) result(vector_aspect)
@@ -297,7 +278,7 @@ function make_transform(src, dst, other_aspects, rc) result(transform)
297278
class(StateItemAspect), intent(in) :: dst
298279
type(AspectMap), target, intent(in) :: other_aspects
299280
integer, optional, intent(out) :: rc
300-
281+
301282
transform = NullTransform()
302283

303284
_RETURN(_SUCCESS)
@@ -332,13 +313,12 @@ subroutine add_to_state(this, multi_state, actual_pt, rc)
332313

333314
type(ESMF_FieldBundle) :: alias, existing_bundle
334315
type(esmf_StateItem_Flag) :: itemType
335-
logical :: is_alias
336-
integer :: status
337316
type(ESMF_State) :: state, substate
338-
character(:), allocatable :: full_name, inner_name
339-
integer :: idx
340-
character(:), allocatable :: intent
341-
317+
type(ESMF_Field), allocatable :: field_list(:)
318+
logical :: is_alias
319+
character(:), allocatable :: full_name, inner_name, intent
320+
integer :: idx, status
321+
342322
intent = actual_pt%get_state_intent()
343323
call multi_state%get_state(state, intent, _RC)
344324

@@ -358,6 +338,13 @@ subroutine add_to_state(this, multi_state, actual_pt, rc)
358338
end if
359339
call ESMF_StateAddReplace(substate, [alias], _RC)
360340

341+
! Also update the names of the components
342+
call MAPL_FieldBundleGet(this%payload, fieldList=field_list, _RC)
343+
if (size(field_list) > 0) then ! might be empty if import item
344+
call ESMF_FieldSet(field_list(1), name=trim(this%short_names%at(1)), _RC)
345+
call ESMF_FieldSet(field_list(2), name=trim(this%short_names%at(2)), _RC)
346+
end if
347+
361348
_RETURN(_SUCCESS)
362349
end subroutine add_to_state
363350

@@ -376,7 +363,7 @@ subroutine get_payload(this, unusable, field, bundle, state, rc)
376363
_UNUSED_DUMMY(field)
377364
_UNUSED_DUMMY(state)
378365
end subroutine get_payload
379-
366+
380367
function get_aspect_id() result(aspect_id)
381368
type(AspectId) :: aspect_id
382369
aspect_id = CLASS_ASPECT_ID

0 commit comments

Comments
 (0)