MeanAction.F90 Source File


This file depends on

sourcefile~~meanaction.f90~~EfferentGraph sourcefile~meanaction.f90 MeanAction.F90 sourcefile~accumulatoraction.f90 AccumulatorAction.F90 sourcefile~meanaction.f90->sourcefile~accumulatoraction.f90 sourcefile~fieldcreate.f90 FieldCreate.F90 sourcefile~meanaction.f90->sourcefile~fieldcreate.f90 sourcefile~fieldget.f90 FieldGet.F90 sourcefile~meanaction.f90->sourcefile~fieldget.f90 sourcefile~fieldpointerutilities.f90 FieldPointerUtilities.F90 sourcefile~meanaction.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldutilities.f90 FieldUtilities.F90 sourcefile~meanaction.f90->sourcefile~fieldutilities.f90 sourcefile~internalconstants.f90 InternalConstants.F90 sourcefile~meanaction.f90->sourcefile~internalconstants.f90 sourcefile~mapl_exceptionhandling.f90 MAPL_ExceptionHandling.F90 sourcefile~meanaction.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~accumulatoraction.f90->sourcefile~fieldpointerutilities.f90 sourcefile~accumulatoraction.f90->sourcefile~fieldutilities.f90 sourcefile~accumulatoraction.f90->sourcefile~internalconstants.f90 sourcefile~accumulatoraction.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~extensionaction.f90 ExtensionAction.F90 sourcefile~accumulatoraction.f90->sourcefile~extensionaction.f90 sourcefile~errorhandling.f90 ErrorHandling.F90 sourcefile~fieldcreate.f90->sourcefile~errorhandling.f90 sourcefile~fieldinfo.f90 FieldInfo.F90 sourcefile~fieldcreate.f90->sourcefile~fieldinfo.f90 sourcefile~keywordenforcer.f90 KeywordEnforcer.F90 sourcefile~fieldcreate.f90->sourcefile~keywordenforcer.f90 sourcefile~lu_bound.f90 LU_Bound.F90 sourcefile~fieldcreate.f90->sourcefile~lu_bound.f90 sourcefile~ungriddeddims.f90 UngriddedDims.F90 sourcefile~fieldcreate.f90->sourcefile~ungriddeddims.f90 sourcefile~verticalstaggerloc.f90 VerticalStaggerLoc.F90 sourcefile~fieldcreate.f90->sourcefile~verticalstaggerloc.f90 sourcefile~fieldget.f90->sourcefile~errorhandling.f90 sourcefile~fieldget.f90->sourcefile~fieldinfo.f90 sourcefile~fieldget.f90->sourcefile~keywordenforcer.f90 sourcefile~fieldget.f90->sourcefile~ungriddeddims.f90 sourcefile~fieldget.f90->sourcefile~verticalstaggerloc.f90 sourcefile~fieldpointerutilities.f90->sourcefile~mapl_exceptionhandling.f90 sourcefile~fieldutilities.f90->sourcefile~fieldpointerutilities.f90 sourcefile~fieldutilities.f90->sourcefile~errorhandling.f90 sourcefile~fieldutilities.f90->sourcefile~fieldinfo.f90 sourcefile~infoutilities.f90 InfoUtilities.F90 sourcefile~fieldutilities.f90->sourcefile~infoutilities.f90 sourcefile~fieldutilities.f90->sourcefile~keywordenforcer.f90 sourcefile~fieldutilities.f90->sourcefile~lu_bound.f90 sourcefile~fieldutilities.f90->sourcefile~ungriddeddims.f90 sourcefile~mapl_exceptionhandling.f90->sourcefile~errorhandling.f90 sourcefile~mapl_throw.f90 MAPL_Throw.F90 sourcefile~mapl_exceptionhandling.f90->sourcefile~mapl_throw.f90 sourcefile~errorhandling.f90->sourcefile~mapl_throw.f90 sourcefile~extensionaction.f90->sourcefile~errorhandling.f90 sourcefile~fieldinfo.f90->sourcefile~errorhandling.f90 sourcefile~fieldinfo.f90->sourcefile~infoutilities.f90 sourcefile~fieldinfo.f90->sourcefile~keywordenforcer.f90 sourcefile~fieldinfo.f90->sourcefile~ungriddeddims.f90 sourcefile~fieldinfo.f90->sourcefile~verticalstaggerloc.f90 sourcefile~mapl_esmf_infokeys.f90 MAPL_ESMF_InfoKeys.F90 sourcefile~fieldinfo.f90->sourcefile~mapl_esmf_infokeys.f90 sourcefile~infoutilities.f90->sourcefile~errorhandling.f90 sourcefile~infoutilities.f90->sourcefile~keywordenforcer.f90 sourcefile~infoutilities.f90->sourcefile~mapl_esmf_infokeys.f90 sourcefile~ungriddeddims.f90->sourcefile~errorhandling.f90 sourcefile~ungriddeddims.f90->sourcefile~infoutilities.f90 sourcefile~ungriddeddims.f90->sourcefile~lu_bound.f90 sourcefile~ungriddeddims.f90->sourcefile~mapl_esmf_infokeys.f90 sourcefile~ungriddeddim.f90 UngriddedDim.F90 sourcefile~ungriddeddims.f90->sourcefile~ungriddeddim.f90 sourcefile~ungriddeddimvector.f90 UngriddedDimVector.F90 sourcefile~ungriddeddims.f90->sourcefile~ungriddeddimvector.f90 sourcefile~mapl_esmf_infokeys.f90->sourcefile~errorhandling.f90 sourcefile~ungriddeddim.f90->sourcefile~errorhandling.f90 sourcefile~ungriddeddim.f90->sourcefile~infoutilities.f90 sourcefile~ungriddeddim.f90->sourcefile~lu_bound.f90 sourcefile~ungriddeddimvector.f90->sourcefile~ungriddeddim.f90

Files dependent on this one

sourcefile~~meanaction.f90~~AfferentGraph sourcefile~meanaction.f90 MeanAction.F90 sourcefile~accumulatoractioninterface.f90 AccumulatorActionInterface.F90 sourcefile~accumulatoractioninterface.f90->sourcefile~meanaction.f90 sourcefile~test_meanaction.pf Test_MeanAction.pf sourcefile~test_meanaction.pf->sourcefile~meanaction.f90 sourcefile~frequencyaspect.f90 FrequencyAspect.F90 sourcefile~frequencyaspect.f90->sourcefile~accumulatoractioninterface.f90 sourcefile~aspectcollection.f90 AspectCollection.F90 sourcefile~aspectcollection.f90->sourcefile~frequencyaspect.f90 sourcefile~fieldspec.f90 FieldSpec.F90 sourcefile~fieldspec.f90->sourcefile~frequencyaspect.f90 sourcefile~fieldspec.f90->sourcefile~aspectcollection.f90 sourcefile~variablespec.f90 VariableSpec.F90 sourcefile~fieldspec.f90->sourcefile~variablespec.f90 sourcefile~test_fieldspec.pf Test_FieldSpec.pf sourcefile~test_fieldspec.pf->sourcefile~frequencyaspect.f90 sourcefile~test_fieldspec.pf->sourcefile~aspectcollection.f90 sourcefile~test_fieldspec.pf->sourcefile~fieldspec.f90 sourcefile~variablespec.f90->sourcefile~frequencyaspect.f90 sourcefile~variablespec.f90->sourcefile~aspectcollection.f90 sourcefile~bracketspec.f90 BracketSpec.F90 sourcefile~bracketspec.f90->sourcefile~fieldspec.f90 sourcefile~componentspec.f90 ComponentSpec.F90 sourcefile~componentspec.f90->sourcefile~variablespec.f90 sourcefile~componentspecparser.f90 ComponentSpecParser.F90 sourcefile~componentspecparser.f90->sourcefile~variablespec.f90 sourcefile~historycollectiongridcomp_private.f90 HistoryCollectionGridComp_private.F90 sourcefile~historycollectiongridcomp_private.f90->sourcefile~variablespec.f90 sourcefile~initialize_advertise.f90 initialize_advertise.F90 sourcefile~initialize_advertise.f90->sourcefile~variablespec.f90 sourcefile~make_itemspec.f90 make_itemSpec.F90 sourcefile~make_itemspec.f90->sourcefile~fieldspec.f90 sourcefile~make_itemspec.f90->sourcefile~variablespec.f90 sourcefile~mapl_generic.f90~2 MAPL_Generic.F90 sourcefile~mapl_generic.f90~2->sourcefile~variablespec.f90 sourcefile~mockitemspec.f90 MockItemSpec.F90 sourcefile~mockitemspec.f90->sourcefile~aspectcollection.f90 sourcefile~mockitemspec.f90->sourcefile~variablespec.f90 sourcefile~modelverticalgrid.f90 ModelVerticalGrid.F90 sourcefile~modelverticalgrid.f90->sourcefile~fieldspec.f90 sourcefile~servicespec.f90 ServiceSpec.F90 sourcefile~servicespec.f90->sourcefile~variablespec.f90 sourcefile~stateitemspec.f90 StateItemSpec.F90 sourcefile~stateitemspec.f90->sourcefile~aspectcollection.f90 sourcefile~statespec.f90 StateSpec.F90 sourcefile~statespec.f90->sourcefile~variablespec.f90 sourcefile~test_addfieldspec.pf Test_AddFieldSpec.pf sourcefile~test_addfieldspec.pf->sourcefile~fieldspec.f90 sourcefile~test_bracketspec.pf Test_BracketSpec.pf sourcefile~test_bracketspec.pf->sourcefile~fieldspec.f90 sourcefile~test_modelverticalgrid.pf Test_ModelVerticalGrid.pf sourcefile~test_modelverticalgrid.pf->sourcefile~variablespec.f90 sourcefile~variablespecvector.f90 VariableSpecVector.F90 sourcefile~variablespecvector.f90->sourcefile~variablespec.f90 sourcefile~wildcardspec.f90 WildcardSpec.F90 sourcefile~wildcardspec.f90->sourcefile~aspectcollection.f90

Source Code

#include "MAPL_Generic.h"
module mapl3g_MeanAction
   use mapl3g_AccumulatorAction
   use MAPL_InternalConstantsMod, only: MAPL_UNDEFINED_REAL, MAPL_UNDEFINED_REAL64
   use MAPL_ExceptionHandling
   use MAPL_FieldPointerUtilities, only: assign_fptr
   use mapl3g_FieldCreate, only: MAPL_FieldCreate
   use mapl3g_FieldGet, only: MAPL_FieldGet
   use MAPL_FieldUtilities, only: FieldSet
   use ESMF
   implicit none
   private
   public :: MeanAction
   public :: construct_MeanAction

   type, extends(AccumulatorAction) :: MeanAction
      type(ESMF_Field), allocatable :: counter_field
   contains
      procedure :: clear => clear_mean
      procedure :: create_fields => create_fields_mean
      procedure :: update_result => update_result_mean
      procedure :: calculate_mean
      procedure :: calculate_mean_R4
      procedure :: accumulate_R4
   end type MeanAction

   type(ESMF_TypeKind_Flag), parameter :: COUNTER_TYPEKIND = ESMF_TYPEKIND_I4
   integer, parameter :: COUNTER_KIND = ESMF_KIND_I4

contains

   function construct_MeanAction(typekind) result(acc)
      type(MeanAction) :: acc
      type(ESMF_TypeKind_Flag), intent(in) :: typekind

      acc%typekind = typekind

   end function construct_MeanAction

   subroutine create_fields_mean(this, import_field, export_field, rc)
      class(MeanAction), intent(inout) :: this
      type(ESMF_Field), intent(inout) :: import_field
      type(ESMF_Field), intent(inout) :: export_field
      integer, optional, intent(out) :: rc

      integer :: status
      type(ESMF_Geom) :: geom
      integer, allocatable :: gmap(:)
      integer :: ndims

      _RETURN_IF(this%initialized)
      call this%AccumulatorAction%create_fields(import_field, export_field, _RC)
      associate(f => this%accumulation_field)
         call ESMF_FieldGet(f, dimCount=ndims, _RC)
         allocate(gmap(ndims))
         call ESMF_FieldGet(f, geom=geom, gridToFieldMap=gmap, _RC)
         this%counter_field =  MAPL_FieldCreate(geom, typekind=ESMF_TYPEKIND_I4, gridToFieldMap=gmap, _RC)
      end associate
      _RETURN(_SUCCESS)

   end subroutine create_fields_mean

   subroutine clear_mean(this, rc)
      class(MeanAction), intent(inout) :: this
      integer, optional, intent(out) :: rc
      
      integer :: status
      integer(COUNTER_KIND), pointer :: counter(:)

      call this%AccumulatorAction%clear(_RC)
      counter => null()
      call assign_fptr(this%counter_field, counter, _RC)
      counter = 0_COUNTER_KIND
      _RETURN(_SUCCESS)

   end subroutine clear_mean

   subroutine calculate_mean(this, rc)
      class(MeanAction), intent(inout) :: this
      integer, optional, intent(out) :: rc

      integer :: status

      if(this%typekind == ESMF_TYPEKIND_R4) then
         call this%calculate_mean_R4(_RC)
      else
         _FAIL('Unsupported typekind')
      end if
      _RETURN(_SUCCESS)

   end subroutine calculate_mean

   subroutine update_result_mean(this, rc)
      class(MeanAction), intent(inout) :: this
      integer, optional, intent(out) :: rc

      integer :: status

      call this%calculate_mean(_RC)
      call this%AccumulatorAction%update_result(_RC)
      _RETURN(_SUCCESS)

   end subroutine update_result_mean

   subroutine calculate_mean_R4(this, rc)
      class(MeanAction), intent(inout) :: this
      integer, optional, intent(out) :: rc

      integer :: status
      real(kind=ESMF_KIND_R4), pointer :: current_ptr(:)
      integer(kind=COUNTER_KIND), pointer :: counter(:)
      real(kind=ESMF_KIND_R4), parameter :: UNDEF = MAPL_UNDEFINED_REAL

      current_ptr => null()
      counter => null()
      call assign_fptr(this%accumulation_field, current_ptr, _RC)
      call assign_fptr(this%counter_field, counter, _RC)
      where(counter /= 0)
         current_ptr = current_ptr / counter
      elsewhere
         current_ptr = UNDEF
      end where
      _RETURN(_SUCCESS)

   end subroutine calculate_mean_R4

   subroutine accumulate_R4(this, update_field, rc)
      class(MeanAction), intent(inout) :: this
      type(ESMF_Field), intent(inout) :: update_field
      integer, optional, intent(out) :: rc

      integer :: status
      real(kind=ESMF_KIND_R4), pointer :: current(:)
      real(kind=ESMF_KIND_R4), pointer :: latest(:)
      integer(kind=COUNTER_KIND), pointer :: counter(:)
      real(kind=ESMF_KIND_R4), parameter :: UNDEF = MAPL_UNDEFINED_REAL

      current => null()
      latest => null()
      counter => null()
      call assign_fptr(this%accumulation_field, current, _RC)
      call assign_fptr(update_field, latest, _RC)
      call assign_fptr(this%counter_field, counter, _RC)
      where(latest /= UNDEF)
        current = current + latest
        counter = counter + 1_COUNTER_KIND
      end where
      _RETURN(_SUCCESS)

   end subroutine accumulate_R4

end module mapl3g_MeanAction