Skip to content

Commit

Permalink
Merge pull request #2993 from GEOS-ESM/feature/wdboggs/#2980_refactor…
Browse files Browse the repository at this point in the history
…_state_item_spec

Feature/wdboggs/#2980 refactor state item spec
  • Loading branch information
tclune committed Aug 26, 2024
2 parents 95e003a + 8b17d82 commit b4266d2
Show file tree
Hide file tree
Showing 13 changed files with 459 additions and 238 deletions.
11 changes: 8 additions & 3 deletions generic3g/OuterMetaComponent/initialize_advertise.F90
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#include "MAPL_Generic.h"

submodule (mapl3g_OuterMetaComponent) initialize_advertise_smod
implicit none
use mapl3g_make_ItemSpec
implicit none (type, external)


contains

Expand Down Expand Up @@ -78,7 +80,7 @@ end subroutine self_advertise

subroutine advertise_variable(var_spec, registry, geom, vertical_grid, unusable, rc)
type(VariableSpec), intent(in) :: var_spec
type(StateRegistry), intent(inout) :: registry
type(StateRegistry), target, intent(inout) :: registry
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
class(KE), optional, intent(in) :: unusable
Expand All @@ -91,8 +93,11 @@ subroutine advertise_variable(var_spec, registry, geom, vertical_grid, unusable,

_ASSERT(var_spec%itemtype /= MAPL_STATEITEM_UNKNOWN, 'Invalid type id in variable spec <'//var_spec%short_name//'>.')

allocate(item_spec, source=var_spec%make_ItemSpec(geom, vertical_grid, registry, rc=status)); _VERIFY(status)
allocate(item_spec, source=make_ItemSpec(var_spec, registry, rc=status))
_VERIFY(status)
call item_spec%create(_RC)
call item_spec%initialize(geom, vertical_grid, _RC)


virtual_pt = var_spec%make_virtualPt()
call registry%add_primary_spec(virtual_pt, item_spec)
Expand Down
10 changes: 10 additions & 0 deletions generic3g/specs/BracketSpec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ module mapl3g_BracketSpec

procedure :: extension_cost
procedure :: make_extension
procedure :: initialize => initialize_bracket_spec
end type BracketSpec

interface BracketSpec
Expand Down Expand Up @@ -291,5 +292,14 @@ subroutine make_extension(this, dst_spec, new_spec, action, rc)
_FAIL('not implemented')
end subroutine make_extension

subroutine initialize_bracket_spec(this, geom, vertical_grid, rc)
class(BracketSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
integer, optional, intent(out) :: rc
integer :: status

_RETURN(_SUCCESS)
end subroutine initialize_bracket_spec

end module mapl3g_BracketSpec
1 change: 1 addition & 0 deletions generic3g/specs/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ target_sources(MAPL.generic3g PRIVATE
ComponentSpec.F90

AbstractActionSpec.F90
make_itemSpec.F90
)
68 changes: 66 additions & 2 deletions generic3g/specs/FieldSpec.F90
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#include "MAPL_Generic.h"

#if defined _SET_FIELD
# undef _SET_FIELD
#endif
#define _SET_FIELD(A, B, F) A%F = B%F

#if defined(_SET_ALLOCATED_FIELD)
# undef _SET_ALLOCATED_FIELD
#endif
#define _SET_ALLOCATED_FIELD(A, B, F) if(allocated(B%F)) _SET_FIELD(A, B, F)

module mapl3g_FieldSpec

use mapl3g_StateItemSpec
Expand Down Expand Up @@ -27,6 +37,7 @@ module mapl3g_FieldSpec
use mapl3g_geom_mgr, only: MAPL_SameGeom
use mapl3g_FieldDictionary
use mapl3g_GriddedComponentDriver
use mapl3g_VariableSpec
use udunits2f, only: UDUNITS_are_convertible => are_convertible, udunit
use gftl2_StringVector
use esmf
Expand Down Expand Up @@ -77,6 +88,7 @@ module mapl3g_FieldSpec

type(ESMF_Field) :: payload
real, allocatable :: default_value
type(VariableSpec) :: variable_spec

logical :: is_created = .false.

Expand All @@ -96,11 +108,13 @@ module mapl3g_FieldSpec
procedure :: make_extension

procedure :: set_info
procedure :: initialize => initialize_field_spec

end type FieldSpec

interface FieldSpec
module procedure new_FieldSpec_geom
module procedure new_FieldSpec_varspec
!# module procedure new_FieldSpec_defaults
end interface FieldSpec

Expand Down Expand Up @@ -131,7 +145,6 @@ module mapl3g_FieldSpec

contains


function new_FieldSpec_geom(unusable, geom, vertical_grid, vertical_dim_spec, typekind, ungridded_dims, &
standard_name, long_name, units, &
attributes, regrid_param, default_value) result(field_spec)
Expand Down Expand Up @@ -176,6 +189,17 @@ function new_FieldSpec_geom(unusable, geom, vertical_grid, vertical_dim_spec, ty

end function new_FieldSpec_geom

function new_FieldSpec_varspec(variable_spec) result(field_spec)
type(FieldSpec) :: field_spec
class(VariableSpec), intent(in) :: variable_spec

field_spec%variable_spec = variable_spec
field_spec%long_name = ' '
!wdb fixme deleteme long_name is set here based on the VariableSpec
! make_FieldSpec method

end function new_FieldSpec_varspec

function get_regrid_method_(stdname, rc) result(regrid_method)
type(ESMF_RegridMethod_Flag) :: regrid_method
character(:), allocatable, intent(in) :: stdname
Expand All @@ -198,6 +222,44 @@ function get_regrid_method_(stdname, rc) result(regrid_method)
_RETURN(_SUCCESS)
end function get_regrid_method_

subroutine initialize_field_spec(this, geom, vertical_grid, rc)
class(FieldSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
integer, optional, intent(out) :: rc
integer :: status
type(ESMF_RegridMethod_Flag), allocatable :: regrid_method
type(ActualPtVector) :: dependencies

associate (variable_spec => this%variable_spec)
if (present(geom)) this%geom = geom
if (present(vertical_grid)) this%vertical_grid = vertical_grid

_SET_FIELD(this, variable_spec, vertical_dim_spec)
_SET_FIELD(this, variable_spec, typekind)
_SET_FIELD(this, variable_spec, ungridded_dims)
_SET_FIELD(this, variable_spec, attributes)
_SET_ALLOCATED_FIELD(this, variable_spec, standard_name)
_SET_ALLOCATED_FIELD(this, variable_spec, units)
_SET_ALLOCATED_FIELD(this, variable_spec, default_value)

this%regrid_param = EsmfRegridderParam() ! use default regrid method
regrid_method = get_regrid_method_(this%standard_name)
this%regrid_param = EsmfRegridderParam(regridmethod=regrid_method)

dependencies = variable_spec%make_dependencies(_RC)
call this%set_dependencies(dependencies)
call this%set_raw_dependencies(variable_spec%dependencies)

if (variable_spec%state_intent == ESMF_STATEINTENT_INTERNAL) then
call this%set_active()
end if
end associate

_RETURN(_SUCCESS)

end subroutine initialize_field_spec

!# function new_FieldSpec_defaults(ungridded_dims, geom, units) result(field_spec)
!# type(FieldSpec) :: field_spec
!# type(ExtraDimsSpec), intent(in) :: ungridded_dims
Expand Down Expand Up @@ -961,5 +1023,7 @@ subroutine set_info(this, field, rc)

_RETURN(_SUCCESS)
end subroutine set_info

end module mapl3g_FieldSpec
#undef _SET_FIELD
#undef _SET_ALLOCATED_FIELD
14 changes: 14 additions & 0 deletions generic3g/specs/InvalidSpec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module mapl3g_InvalidSpec
use mapl3g_ActualPtVector
use mapl3g_ActualPtSpecPtrMap
use mapl3g_NullAction
use mapl3g_VerticalGrid
use esmf, only: ESMF_FieldBundle
use esmf, only: ESMF_Geom
use esmf, only: ESMF_State
Expand All @@ -35,6 +36,7 @@ module mapl3g_InvalidSpec

procedure :: make_extension
procedure :: extension_cost
procedure :: initialize => initialize_invalid_spec
end type InvalidSpec


Expand Down Expand Up @@ -154,4 +156,16 @@ integer function extension_cost(this, src_spec, rc) result(cost)

end function extension_cost

subroutine initialize_invalid_spec(this, geom, vertical_grid, rc)
class(InvalidSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
integer, optional, intent(out) :: rc

integer :: status

_FAIL('Attempt to initialize item of type InvalidSpec')

end subroutine initialize_invalid_spec

end module mapl3g_InvalidSpec
45 changes: 39 additions & 6 deletions generic3g/specs/ServiceSpec.F90
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include "MAPL_Generic.h"

module mapl3g_ServiceSpec
use mapl3g_StateRegistry
use mapl3g_VariableSpec
use mapl3g_StateItemSpec
use mapl3g_MultiState
use mapl3g_ActualConnectionPt
use mapl3g_StateItemExtension
use mapl3g_ExtensionAction
use mapl3g_NullAction
use mapl3g_AbstractActionSpec
Expand All @@ -14,6 +17,7 @@ module mapl3g_ServiceSpec
use mapl3g_ActualPtVector
use mapl3g_ActualConnectionPt
use mapl3g_VirtualConnectionPt
use mapl3g_VerticalGrid
use esmf
use gftl2_StringVector
implicit none
Expand All @@ -23,6 +27,8 @@ module mapl3g_ServiceSpec

type, extends(StateItemSpec) :: ServiceSpec
private
type(StateRegistry), pointer :: registry
type(VariableSpec) :: variable_spec
type(ESMF_Typekind_Flag), allocatable :: typekind
type(ESMF_FieldBundle) :: payload
type(StateItemSpecPtr), allocatable :: dependency_specs(:)
Expand All @@ -38,6 +44,7 @@ module mapl3g_ServiceSpec
procedure :: extension_cost
procedure :: add_to_state
procedure :: add_to_bundle
procedure :: initialize => initialize_service_spec
!!$ procedure :: check_complete
end type ServiceSpec

Expand All @@ -47,13 +54,13 @@ module mapl3g_ServiceSpec

contains

function new_ServiceSpec(service_item_specs) result(spec)
function new_ServiceSpec(variable_spec, registry) result(spec)
type(ServiceSpec) :: spec
type(StateItemSpecPtr), intent(in) :: service_item_specs(:)
type(VariableSpec), intent(in) :: variable_spec
type(StateRegistry), target, intent(in) :: registry

integer :: status

spec%dependency_specs = service_item_specs
spec%variable_spec = variable_spec
spec%registry => registry

end function new_ServiceSpec

Expand Down Expand Up @@ -197,7 +204,33 @@ integer function extension_cost(this, src_spec, rc) result(cost)
cost = 0
_RETURN(_SUCCESS)
end function extension_cost


subroutine initialize_service_spec(this, geom, vertical_grid, rc)
class(ServiceSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
integer, optional, intent(out) :: rc
integer :: status

integer :: i, n
type(StateItemSpecPtr), allocatable :: specs(:)
type(VirtualConnectionPt) :: v_pt
type(StateItemExtension), pointer :: primary

associate (var_spec => this%variable_spec)
n = var_spec%service_items%size()
allocate(specs(n))

do i = 1, n
v_pt = VirtualConnectionPt(ESMF_STATEINTENT_INTERNAL, var_spec%service_items%of(i))
! Internal items are always unique and "primary" (owned by user)
primary => this%registry%get_primary_extension(v_pt, _RC)
specs(i)%ptr => primary%get_spec()
end do
end associate
this%dependency_specs = specs

_RETURN(_SUCCESS)
end subroutine initialize_service_spec

end module mapl3g_ServiceSpec
12 changes: 11 additions & 1 deletion generic3g/specs/StateItemSpec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ module mapl3g_StateItemSpec

procedure(I_add_to_state), deferred :: add_to_state
procedure(I_add_to_bundle), deferred :: add_to_bundle
procedure(I_initialize), deferred :: initialize

procedure, non_overridable :: set_allocated
procedure, non_overridable :: is_allocated
Expand All @@ -47,7 +48,6 @@ module mapl3g_StateItemSpec
class(StateItemSpec), pointer :: ptr => null()
end type StateItemSpecPtr


abstract interface

subroutine I_connect(this, src_spec, actual_pt, rc)
Expand Down Expand Up @@ -122,6 +122,16 @@ subroutine I_add_to_bundle(this, bundle, rc)
integer, optional, intent(out) :: rc
end subroutine I_add_to_bundle

subroutine I_initialize(this, geom, vertical_grid, rc)
use esmf, only: ESMF_Geom
use mapl3g_VerticalGrid, only: VerticalGrid
import StateItemSpec
class(StateItemSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
integer, optional, intent(out) :: rc
end subroutine I_initialize

end interface

contains
Expand Down
29 changes: 14 additions & 15 deletions generic3g/specs/StateSpec.F90
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module mapl3g_StateSpec
use mapl3g_AbstractActionSpec
use mapl3g_StateItemSpecMap
use mapl3g_VariableSpec
use mapl3g_VerticalGrid
use mapl3g_MultiState
use mapl3g_ActualConnectionPt
use mapl3g_ActualPtVector
Expand All @@ -22,7 +23,7 @@ module mapl3g_StateSpec
type(ESMF_State) :: payload
type(StateItemSpecMap) :: item_specs
contains
!!$ procedure :: initialize
procedure :: initialize
procedure :: add_item
procedure :: get_item

Expand All @@ -42,20 +43,18 @@ module mapl3g_StateSpec

contains

!!$ ! Nothing defined at this time.
!!$ subroutine initialize(this, geom, var_spec, unusable, rc)
!!$ class(StateSpec), intent(inout) :: this
!!$ type(ESMF_Geom), intent(in) :: geom
!!$ type(VariableSpec), intent(in) :: var_spec
!!$ class(KeywordEnforcer), optional, intent(in) :: unusable
!!$ integer, optional, intent(out) :: rc
!!$
!!$ character(:), allocatable :: units
!!$ integer :: status
!!$
!!$ _RETURN(_SUCCESS)
!!$ _UNUSED_DUMMY(unusable)
!!$ end subroutine initialize
! Nothing defined at this time.
subroutine initialize(this, geom, vertical_grid, rc)
class(StateSpec), intent(inout) :: this
type(ESMF_Geom), optional, intent(in) :: geom
class(VerticalGrid), optional, intent(in) :: vertical_grid
integer, optional, intent(out) :: rc

character(:), allocatable :: units
integer :: status

_RETURN(_SUCCESS)
end subroutine initialize

subroutine add_item(this, name, item)
class(StateSpec), target, intent(inout) :: this
Expand Down
Loading

0 comments on commit b4266d2

Please sign in to comment.