1

I have a solver that iterates for a good amount of time (several hours) and I'm trying to remove several if statements from the main loop in order to save time.

What I'm essentially trying to do here is to create a routine, updateGhosts, which points to an assigned routine. This routine belongs to a derived data type which contains several other properties and routines. I want to use a routine, setGhosts, to set updateGhost to be either ghostOne or ghostTwo, which will be routines that properly update some conditions.

I can't seem to figure out a method that leads the code to compile, though I've tried several different things to no avail.

I have attempted to reduce the code sample down as far as possible for simplicity, but in reality types GridPoint and BlockType have many more parameters to deal with, so simple refactoring is not an option.

Here's the reduced code:

module BlockModule
  implicit none

  type GridPoint
    real(kind=8) :: x, y, T
  end type GridPoint

  type BlockType
    integer :: BC
    type (GridPoint) :: Points(0:102,0:102)
  contains
    procedure :: setGhosts, updateGhosts
    procedure :: ghostOne, ghostTwo
  end type BlockType

contains
  subroutine setGhosts(this)
    class(BlockType), intent(inout) :: this

    if (this%BC == -1) then
      ! We want to assign updateGhosts to ghostOne.
      this%updateGhosts => this%ghostOne
    else
      ! We want to assign updateGhosts to ghostTwo.
      this%updateGhosts => this%ghostTwo
    end if
  end subroutine

  ! Routine that will be either ghostOne or ghostTwo.
  subroutine updateGhosts(this)
    class(BlockType), intent(inout) :: this
  end subroutine

  ! Routine will do something.
  subroutine ghostOne(this)
    class(BlockType), intent(inout) :: this
  end subroutine

  ! Routine will do something completely different, with same inputs.
  subroutine ghostTwo(this)
    class(BlockType), intent(inout) :: this
  end subroutine
end module

How can I assign a routine name to point to a different routine in Fortran90/95/03? (The oldest version possible is ideal, but not necessary.) Sorry if similar questions have been asked before, I tried a search but I'm not quite sure what I need to look for.

Thanks for reading!

John Karasinski
  • 977
  • 7
  • 16

1 Answers1

0

(Question was answered in a comment. See Question with no answers, but issue solved in the comments (or extended in chat) )

@SuperCow wrote:

Does this help? Procedure Pointer, Derived Type

The OP wrote:

Yes! This was the trick.

Community
  • 1
  • 1
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129