Fortran 90: dinamic matrix valued function as argument

Click For Summary
SUMMARY

This discussion focuses on creating a Fortran 90 program that utilizes dynamic matrix-valued functions as arguments. The user encountered a compiler error related to the module file "sub_mod.mod" not being found. Solutions provided include rearranging the module definitions to precede the main program, using prototype headers with the "interface" keyword, or separating the modules into different files. The corrected code structure is shared to resolve the issue effectively.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with modules and subroutines in Fortran
  • Knowledge of dynamic arrays in Fortran
  • Experience with compiler error troubleshooting
NEXT STEPS
  • Explore Fortran 90 module organization and best practices
  • Learn about the "interface" keyword for function prototypes in Fortran
  • Investigate dynamic memory allocation techniques in Fortran 90
  • Review error handling and debugging methods in Fortran compilers
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with dynamic arrays and modular programming, as well as anyone troubleshooting compiler errors in Fortran 90.

Taff
Messages
1
Reaction score
0
hello, I'm trying to write a program with a subroutine that can handle array valued functions of different dimensions, I'm new to fortran 90 so i have some troubles.
Here's what i tried:

program passing
use functions
use sub_mod
implicit none
integer, dimension(2,2):: a2
integer, dimension(3,3):: a3
call prova(a2,g2)
!print*,a2
call prova(a3,g3)
!print*,a3
end program passing

module functions
implicit none
contains
function g2(x)
implicit none
!result
integer,dimension(2,2) :: g2
!dummy
integer x
g2(1,1)=x+2
g2(1,2)=x+2
g2(2,1)=x+2
g2(2,2)=x+2
end function g2

function g3(x)
implicit none
!result
integer,dimension(3,3) :: g3
!dummy
integer x
g3(1,1)=x+3
g3(1,2)=x+3
g3(2,1)=x+3
g3(2,2)=x+3
end function g3

end module functions

module sub_mod
implicit none
contains
subroutine prova(a,f)
!dummy
integer,dimension(:,:) :: a
integer,dimension(size(a,1),size(a,2)) :: f
external f
a=f(0)
end subroutine prova
end module sub_modi got 1 compiler error "can't open module file sub_mod.mod..."
 
Technology news on Phys.org
Hi Taff. I'm a pretty infrequent Fortran user, but I think that you have three options with modules

1. Put them ahead of the code that uses them.

2. Put a "prototype" header before the code that uses them. (I think it's the "interface" keyword, but I can't remember the exact syntax).

3. Put them is a separate file.

So the easiest fix for you is to just put them ahead of the main program, Eg

Code:
module functions
implicit none
contains
function g2(x)
implicit none
!result
integer,dimension(2,2) :: g2
!dummy
integer x
g2(1,1)=x+2
g2(1,2)=x+2
g2(2,1)=x+2
g2(2,2)=x+2
end function g2

function g3(x)
implicit none
!result
integer,dimension(3,3) :: g3
!dummy
integer x
g3(1,1)=x+3
g3(1,2)=x+3
g3(2,1)=x+3
g3(2,2)=x+3
end function g3

end module functions

module sub_mod
implicit none
contains
subroutine prova(a,f)
!dummy
integer,dimension(:,:) :: a
integer,dimension(size(a,1),size(a,2)) :: f
external f
a=f(0)
end subroutine prova
end module sub_mod

program passing
use functions
use sub_mod
implicit none
integer, dimension(2,2):: a2
integer, dimension(3,3):: a3
call prova(a2,g2)
!print*,a2
call prova(a3,g3)
!print*,a3
end program passing

BTW. Please note that you can use "code" tag to preserve formatting (indentation). I didn't put any indentation into your code because I just cut and pasted it and am too lazy. But next time you post code, using the code tag will greatly increase the chance that someone will bother reading it. :)
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
8K