Run-time polymorphism in fortran 2003

I am writing code in Fortran 2003 which makes a lot of linear sparse matrix algebra. I'm trying to use some of the more abstract functions of the new standard, so I have simpler programs without too much repetitive code.

I have a procedure solverthat takes in a matrix, some vectors, validity for the iterative method used, etc. I pass him a pointer to a procedure called matvec; matvecis a routine used to multiply matrix vectors.

The problem is that sometimes it matvecis a procedure that takes extra arguments colorlist, color1, color2above the regular ones sent to this procedure. I can think of several ways to deal with this.

First idea: define two different abstract interfaces matvec1, matvec2and two different solvers. This works, but it means duplicating some code, which I am trying to avoid.

Another idea: keep the same abstract interface matvec, and make additional arguments colorlist, color1, color2optional. This means that they become optional in every matvec procedure - even for those for which they are not optional, and for procedures where they are not even used at all. Pretty sure that I will go to hell if I do this.

I can think of many other less optimal solutions. I would like some of them to be introduced - I'm sure there is an elegant way to do this, I'm just not sure what it is.

+5
2

, ( ), - , . , matvec . , . matvec ( ), , , matvec.

( , module rechercheRacine).

+5

, , matvec :

interface matvec
  module procedure matvec1, matvec2
end interface

solver . , solver , :

type :: solver
  real, allocatable :: matrix(:,:), v1(:), v2(:)
contains
  procedure, pass :: matvec1
  procedure, pass :: matvec2
  generic :: matvec => matvec1, matvec2
end type

, , .

; (, , "undefined" ), - , . , ( ) ( Bรกlint). generic .

+2

All Articles