D.8.6 Combine_DV_from_CA Procedure

The main documentation of the Combine_DV_from_CA Procedure contains additional explanation of this code listing.

  define([COMBINE_ROUTINE],[
    pushdef([OP], [$1])
    pushdef([Combine_with_OP_DV_from_CA], expand(Combine_with_$1_DV_from_CA))

    subroutine Combine_with_OP_DV_from_CA (DV, CA)

      ! Input variable.
  
      type(Collected_Array_type), intent(in) :: CA ! Variable to be combined.
  
      ! Input/Output variable.
      
      type(Distributed_Vector_type), intent(inout) :: DV ! Resultant DV.
  
      !~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

      ! Verify requirements.
  
      VERIFY(Valid_State(CA),5)                           ! CA is valid.
      VERIFY(Valid_State(DV),5)                           ! DV is valid.
      VERIFY(CA%Dimensionality == DV%Dimensionality,5)    ! Same dimensionality.
      VERIFY(CA%Dimensions(1:CA%Dimensionality) == dnl
             DV%Dimensions(1:DV%Dimensionality),5)        ! Same dimensions.
      VERIFY(ASSOCIATED(CA%One_Structure,DV%Structure),5) ! Same one-structure.

      ! Combine the values. There are different versions based on the
      ! dimensionality of the Index and on the dimensionality of the "Array"
      ! itself.

      select case (CA%Many_of_One_Index%Dimensionality)
      
      ! Vector Index - no combination necessary.

      case (1)

        ! Switch on the dimensionality of the data itself.

        select case (CA%Dimensionality)
        case (1)
          DV%Values1 = CA%Values1
        case (2)
          DV%Values2 = CA%Values2
        case (3)
          DV%Values3 = CA%Values3
        case (4)
          DV%Values4 = CA%Values4
        end select

      ! Array Index. Shape of CA%Values must be:
      !
      !     CA%Values ( [dim1, [dim2, [dim3, ]]] One_Axis, Many_Axis )

      case (2)

        ! Switch on the dimensionality of the data itself.
  
        select case (CA%Dimensionality)

        case (1)

          DV%Values1(:) = OPERATION(CA%Values2, 2)
          ifelse(OP, [Average], [
            DV%Values1 = DV%Values1 / changetype(real,SIZE(CA%Values2, 2))
          ])

        case (2)

          DV%Values2(:,:) = OPERATION(CA%Values3, 3)
          ifelse(OP, [Average], [
            DV%Values2 = DV%Values2 / changetype(real,SIZE(CA%Values3, 3))
          ])

        case (3)

          DV%Values3(:,:,:) = OPERATION(CA%Values4, 4)
          ifelse(OP, [Average], [
            DV%Values3 = DV%Values3 / changetype(real,SIZE(CA%Values4, 4))
          ])

        case (4)

          DV%Values4(:,:,:,:) = OPERATION(CA%Values5, 5)
          ifelse(OP, [Average], [
            DV%Values4 = DV%Values4 / changetype(real,SIZE(CA%Values5, 5))
          ])

        end select

      end select

      ! Set version number.

      DV = Version(CA)
  
      ! Verify guarantees.

      VERIFY(Valid_State(CA),5)    ! CA is still valid.
      VERIFY(Valid_State(DV),5)    ! DV is valid.

      return
    end subroutine Combine_with_OP_DV_from_CA

    popdef([OP])
    popdef([OPERATION])
    popdef([Combine_with_OP_DV_from_CA])
  ])

  ! Add "Conserve" later if needed.

  fortext([Op],[Average SUM MAX MIN],[
    ifelse(
      Op, [MAX], [
        pushdef([OPERATION], [MAXVAL[]($1, $2)])
      ], Op, [MIN], [
        pushdef([OPERATION], [MINVAL[]($1, $2)])
      ], [
        pushdef([OPERATION], [SUM[]($1, $2)])
      ]
    )
    COMBINE_ROUTINE(Op)
  ])



Michael L. Hall