dofine
dofine

Reputation: 883

Fortran interface, procedure and function relationship

I'm reading some Fortran 90 code and come across some concepts that I'm not familiar with. Part of the code, which is defined to calculate dot product of two vectors, dmf_dotp for real value, zmf_dotp for complex ones.

mesh.F90:

#include mesh_inc.F90

interface dmf_dotp
  module procedure dmf_dotp_1, dmf_dotp_2
end interface dmf_dotp

interface zmf_dotp
  module procedure zmf_dotp_1, zmf_dotp_2
end interface zmf_dotp

In another file there are functions like:

R_TYPE function X(mf_dotp_1)(mesh, f1, f2, reduce, dotu) result(dotp)
R_TYPE function X(mf_dotp_2)(mesh, dim, f1, f2, reduce, dotu) result(dotp)

Then one can call these functions(or interfaces?) with zmf_dotp or dmf_dotp. So what's really going on here?


Edit thanks to Vladimir F. It turns out there are some preprocessor macros defined else:

#define X(x)        z ## x
#define R_TYPE      CMPLX

Upvotes: 1

Views: 338

Answers (1)

This is one of the ways one can do generic programming in Fortran without copy and paste everything.

I assume it actually looks similar to:

#define X(x)        z ## x
#define R_TYPE      CMPLX
#include mesh_inc.F90

and

#define X(x)        d ## x
#define R_TYPE      FLOAT
#include mesh_inc.F90

or similar, it seems that CMPLX is another macro which leads to some kind of complex and therefore I expect FLOAT to be some kind of real, possibly double precision.

This will actually lead for the preprocessed source code as

 double precision function dmf_dotp_1(...)

and

 double complex function zmf_dotp_1(...)

with the functions having similar or even identical body.

This enables you to write the relevant code only once and create reuse with under different names with different types.

Beware! The very popular gfortran compiler runs the C preprocessor in the "traditional mode" which does not support the ## operator.

Upvotes: 7

Related Questions