I pass an optional mask to a subroutine. Within this routine I then have many statements of the kind
...
if (present(Mask)) then
where(Mask)
y(:) = A(:)*B(:)
end where
else
y(:) = A(:)*B(:)
end if
...
This is very ugly in regards of code duplication. Are there any advices to do this in a better way? I have many statements like this in my code and I need to perform the operations on whole arrays.
Edit:
At least a part of the problem can be solved through elemental functions. Because I have the same operations on the right side I can write
...
if (present(Mask)) then
where(Mask)
y(:) = multiply(A(:),B(:))
end where
else
y(:) = multiply(A(:),B(:))
end if
...
elemental real function multiply(a,b)
real, intent(in) :: a,b
multiply = a*b
end function
This way, I have at least a single place for the operations.
There is the option (pun intended) to no use optional arguments, and instead, write a generic interface. For example, instead of writing
subroutine bar(arg0, opt)
integer, intent(inout) :: arg0
integer, intent(in), optional :: opt
arg0 = int(4 * atan(1.))
if (present(opt)) arg0 = arg0 + opt
module foo
You can do
interface bar
module procedure bar0
module procedure bar1
end interface bar
contains
subroutine bar0(arg0)
integer, intent(inout) :: arg0
arg0 = int(4 * atan(1.))
end subroutine bar0
subroutine bar1(arg0, opt)
integer, intent(inout) :: arg0
integer, intent(in) :: opt
arg0 = arg0 + opt
end subroutine bar1
end module foo
program bah
use foo
integer :: a0 = 1, a1 = 42
call bar(a0)
print *, a0
call bar(a0, a1)
print *, a0
end program bah
One advantage that you may gain from this approach is that bar0
and bar1 may allow a compiler to do a better job of optimizing
the code. The if (present(opt)) construct may get in the way.
Related
My scenario: I would like to have my Fortran (>=95) program choose one of two subroutines in a calculation, based on a parameter. As an example, let's have two subroutines, foo, which subtracts; and bar, which adds its two integer arguments. I furthermore have a subroutine callingsub which gets either foo or bar as an argument. Full program could look like
program choice
implicit none
integer :: a,b
a=3
b=4
call callingsub(a,b,foo)
contains
subroutine foo(a,b,c)
integer, intent(in) :: a,b
integer, intent(out) :: c
c=a-b
end subroutine foo
subroutine bar(a,b,c)
integer, intent(in) :: a,b
integer, intent(out) :: c
c=a+b
end subroutine bar
subroutine callingsub(a,b,sub)
integer, intent(in) :: a,b
interface
subroutine sub(a,b,c)
integer, intent(in) :: a,b
integer, intent(out) :: c
end subroutine sub
end interface
integer :: c
call sub(a,b,c)
write(*,*) 'Your answer is ',c
end subroutine callingsub
end program choice
Now to switch between foo and bar I have to recompile, but I would rather have a choice at run time. I imagine having an integer flag, which, if 0 chooses foo and if 1 chooses bar. I could of course write a subroutine
subroutine baz(a,b,c,flag)
integer, intent(in) :: a,b
integer, intent(out) :: c
integer, intent(in) :: flag
if (flag==0) then
c=a-b
else if (flag==1) then
c=a+b
else
write(0,*) 'illegal flag ', flag
stop 1
end if
end subroutine baz
which uses the flag to decide, however, the call to callingsub will be in a huge loop, and my feeling tells me, that it would be better to have the decision on foo or bar before the loop.
Is there any possibility to have a conditional to decide in the main program? I imagine something like
if (flag==0) then
chosensub=foo
elseif (flag==1) then
chosensub=bar
else
!error and exit
end if
and then call callingsub(a,b,chosensub), which unfortunately does not work. I cannot put interfaces into a conditional either.
I appreciate any help on this, and hope I made myself clear enough!
PS I have access to Intel ifort 18.0.5 20180823, so I am not limited to F95.
OK, for future reference here is what I did, after following #M.S.B s answer here, so thanks #HighPerformanceMark and #IanBush for pointing (haha) in that direction:
program choice
implicit none
integer :: a,b,flag
interface
subroutine chosensub(a,b,c)
integer, intent(in) :: a,b
integer, intent(out) :: c
end subroutine chosensub
end interface
procedure(chosensub), pointer :: subptr=>null()
read(*,*) flag
if (flag==0) then
subptr=>foo
else if (flag==1) then
subptr=>bar
else
write(0,*) 'error message'
stop 1
end if
a=3
b=4
call callingsub(a,b,subptr)
contains
! as in OP
end program choice
I am using Fortran 90 and the Intel compiler.
I am very confused using a function in a subroutine. My code is (I deleted everything unimportant):
program test
INTEGER :: seed=5
REAL :: nor_ran_number1, nor_ran_number2
CALL Box_Muller_transform(seed,nor_ran_number1,nor_ran_number2)
end program test
double precision function grnd(SEED)
grnd=5
return
end
SUBROUTINE Box_Muller_transform (seed,nor_ran_number1,nor_ran_number2)
implicit none
INTEGER, INTENT(in) :: seed
REAL, INTENT(out) :: nor_ran_number1, nor_ran_number2
nor_ran_number1 = grnd(seed)
nor_ran_number2 = grnd(seed)
end SUBROUTINE Box_Muller_transform
The compiler returns:
error #6404: This name does not have a type, and must have an explicit
type. [GRND]
nor_ran_number1 = grnd(seed)
------------------^
I found this and understand that the function "grad" is not visible inside "Box_Muller_transform". However then I would expect the following code to produce the same error:
program test
INTEGER ::a=5, b
call sub(a,b)
write(*,*) b
end program
SUBROUTINE sub(a,b)
INTEGER, INTENT(in) ::a
INTEGER, INTENT(out) ::b
b = fun(a)
end subroutine sub
function fun(a)
INTEGER :: fun
INTEGER :: a
fun = a*a
end function fun
But this is working.
I would be very happy if someone could point out the difference and explain the simplest way to solve this problem.
Functions must have their return value defined. Since you are using implicit none in your first example, the type of the return value of grnd must be defined explicitly:
SUBROUTINE Box_Muller_transform (seed,nor_ran_number1,nor_ran_number2)
implicit none
INTEGER, INTENT(in) :: seed
REAL, INTENT(out) :: nor_ran_number1, nor_ran_number2
double precision :: grnd
nor_ran_number1 = grnd(seed)
nor_ran_number2 = grnd(seed)
end SUBROUTINE Box_Muller_transform
In the second example, you have not specified implicit none in sub, therefore fun is assumed to be of (implicit) type real. The compiler seems to silently cast this to integer.
I try the following codes, and find the OPTIONAL keyword does not work. The compile is ok, but the runtime error will prompt.
I know usually the INTERFACE should be used in the module to provide enough information for the routines. I also try that, but failed to finish the compile no matter where I put the INTERFACE.
I have read some codes which use OPTIONAL in the TYPE declaration. https://www.pgroup.com/lit/articles/insider/v3n1a3.htm
Now I am using intel visual fortran, so is there any difference?
module testA_m
implicit none
type :: onion_c
contains
procedure :: testA
end type
contains
subroutine testA(this, a,b)
implicit none
class(onion_c) :: this
real*8 :: a
real*8, optional :: b
write(*,*) a,b
end subroutine
end module
program main
call testIt()
end program
subroutine testIt()
use testA_m
implicit none
type(onion_c) :: onion
real*8 :: c1
real*8 :: c2
c1 = 1.0d0
c2 = 2.0d0
call onion.testA(c1)
end subroutine
Well, you are trying to print b, which is not passed to the subroutine. Hence the access violation.
You should check for b first:
subroutine testA(this, a,b)
implicit none
class(onion_c) :: this
real*8 :: a
real*8, optional :: b
if ( present(b) ) then
write(*,*) a,b
else
write(*,*) a
endif
end subroutine
Maybe I need another variable for the real operation. Like the following.
I am still looking forward the better solution to use b directly.
subroutine testA(this, a,b)
implicit none
class(onion_c) :: this
real*8 :: a
real*8, optional :: b
real*8 :: bUsed
if ( present(b) ) then
bUsed = b
write(*,*) a,bUsed
else
bUsed = 2.00d0
write(*,*) a,bUsed
endif
end subroutine
Because Fortran does not support a program like
subroutine testA( this, a, b=10.0d0 )
I usually define a macro like the following in a common header file
#define _optval_(x,xopt,default) x = default; if (present(xopt)) x = xopt
and then use it at the top of a subroutine like
subroutine testA(this, a,b_)
class(onion_c) :: this
real*8 :: a
real*8, optional :: b_
real*8 b
_optval_( b, b_, 10.0d0 ) !! use only b from here on
Although this is not essentially different from writing several IF constructs, I feel it is a bit more convenient (at least for simple variables) because no need to worry about whether b is optional or not in the subsequent code. (But frankly, I hope Fortran2020 or so will support a syntax like the first example...)
Is it possible to pass real part of a complex array to a subroutine in Fortran without storing the real part in another array and pass that? e.g. instead of
Z = complex array;
X = real(Z)
call foo(X)
Do the following
Z = complex array
call foo(real(Z))
This gives me a compiler error! I am using an intel compiler ifort.
Sure, it works:
module testmod
implicit none
integer, parameter :: dp = kind(1.0d0)
contains
subroutine realsub(array)
real(dp), intent(in) :: array(:)
print *, array
end subroutine realsub
end module testmod
program testprog
use testmod
implicit none
complex(dp) :: array(3)
array(:) = [ (1.0_dp, 1.0_dp), (3.0_dp, 2.0_dp), (-1.0_dp, 3.0_dp) ]
call realsub(real(array))
end program testprog
I'm enough of a novice to not know the terminology, so I can't search the Web for the answer to this.
More than once, in programming, I've wanted to do something like this.
A and B are subroutines, c and d are functions. A and B each call a function multiple times inside them.
call A(c(x))
call A(d(x))
call B(c(x))
call B(d(x))
This structure doesn't work. I'm told that Fortran doesn't support aliasing of functions, at least in this context. (Most search results involving "aliasing" refer to aliasing variables rather than functions, which is why I haven't found an answer.)
So what structure can I use to do this without having to write multiple versions of A and B?
Not totally sure I understand what you want, but is it something like the following?
Program f
Implicit None
Interface
Integer Function c( x )
Implicit None
Integer, Intent( In ) :: x
End Function c
Integer Function d( x )
Implicit None
Integer, Intent( In ) :: x
End Function d
End Interface
Call a( 3, c )
Call a( 4, d )
Call b( 5, c )
Call b( 6, d )
Contains
Subroutine a( x, func )
Integer, Intent( In ) :: x
Interface
Integer Function func( x )
Implicit None
Integer, Intent( In ) :: x
End Function func
End Interface
Write( *, * ) 'In a. Func = ', func( x )
End Subroutine a
Subroutine b( x, func )
Integer, Intent( In ) :: x
Interface
Integer Function func( x )
Implicit None
Integer, Intent( In ) :: x
End Function func
End Interface
Write( *, * ) 'In b. Func = ', func( x )
End Subroutine b
End Program f
Integer Function c( x )
Implicit None
Integer, Intent( In ) :: x
c = 2 * x
End Function c
Integer Function d( x )
Implicit None
Integer, Intent( In ) :: x
d = 10 * x
End Function d
Wot now? gfortran -std=f95 f.f90
Wot now? ./a.out
In a. Func = 6
In a. Func = 40
In b. Func = 10
In b. Func = 60
Wot now?
An alternative is procedure pointers, but you'll need a f2003 compiler for that and those are not so common yet - the above is fine back to f90 and even earlier than that External will do what you want, but has less error checking capabilities
call A(c(x)) looks like evaluate c(x) and pass that to subroutine A, as IanH says in his comment.
If you want to pass a function "C" that takes an argument of type such as X to subroutine A, there are several ways to do that.
As already mentioned, procedure pointers are a new way. While extremely few compilers exist that support all of the Fortran 2003 standard, this portion is widely supported.
Here is an example adapted from Function pointer arrays in Fortran
module ProcsMod
implicit none
contains
function f1 (x)
real :: f1
real, intent (in) :: x
f1 = 2.0 * x
return
end function f1
function f2 (x)
real :: f2
real, intent (in) :: x
f2 = 3.0 * x**2
return
end function f2
subroutine fancy (func, x, answer)
real, intent (in) :: x
real, intent (out) :: answer
interface AFunc
function func (y)
real :: func
real, intent (in) ::y
end function func
end interface AFunc
answer = func (x)
end subroutine fancy
end module ProcsMod
program test_proc_ptr
use ProcsMod
implicit none
interface
function func (z)
real :: func
real, intent (in) :: z
end function func
end interface
procedure (func), pointer :: f_ptr => null ()
real :: answer
f_ptr => f1
call fancy (f_ptr, 2.0, answer)
write (*, *) answer
f_ptr => f2
call fancy (f_ptr, 2.0, answer)
write (*, *) answer
stop
end program test_proc_ptr
The calls "call fancy (f_ptr, 2.0, answer)" look the same, but by changing the function that the function pointer f_ptr points to, a different function is passed to the subroutine fancy.
This compiles with both gfortran (versions 4.4 to 4.7) and ifort.
I think M.S.B.'s answer describes what you mean by aliasing of functions; the Fortran terminology is "procedure pointers". As an alternative to this and Ian's answer, you can also use procedure dummy arguments (which are not necessarily pointers). Note that any procedure declaration is only supported since F2003, but gfortran 4.7 and ifort 13 both support this. It can be done with or without an (abstract) interface block:
module dummy_procedure
implicit none
abstract interface
real function myfunc(x)
real, intent(in) :: x
end function
end interface
contains
subroutine a(func)
! Using the interface block:
procedure(myfunc) :: func
print*, 'a:', func(.5)
end subroutine
subroutine b(func)
! Using the explicit interface of a known procedure:
procedure(f1) :: func
print*, 'b:', func(.5)
end subroutine
real function f1(x)
real, intent(in) :: x
f1 = 2.0 * x
end function
real function f2(x)
real, intent(in) :: x
f2 = 3.0 * x**2
end function
end module
Now you can pass f1 and f2 directly into a and b, and the output is as expected:
program main
use dummy_procedure
call a(f1) ! a: 1.0
call a(f2) ! a: 0.75
call b(f1) ! b: 1.0
call b(f2) ! b: 0.75
end program
If I understand what you're trying to do:
1) Define your functions in a module.
2) Use the module.
3) Supply the function and the input data to the subroutine as separate arguments.
Here's a code that worked for me:
module iterfuncs
contains
! two example functions:
function approach_golden_ratio(a) result(agr)
agr=1./a+1.
end function approach_golden_ratio
function approach_other_ratio(a) result(aor)
aor=1./(a-1)
end function approach_other_ratio
end module
program use_some_functions
use iterfuncs
real :: final_res
final_res=2.3
! call subroutine with 1st function
call iterate(final_res,approach_golden_ratio)
print *,final_res
final_res=2.3
! call subroutine with 2nd function
call iterate(final_res,approach_other_ratio)
print *,final_res
end program
subroutine iterate(res,func)
use iterfuncs
do n=1,100
res=func(res)
enddo
return
end subroutine