Using MODULEs in Fortran 90/95

In summary: I see the code right away. It was just my assumption that I needed to use SPOILER tag to make my post more compact. I will use CODE tag from now on. Thanks for the hint!In summary, the conversation was about a code with FUNCTION definitions in one module and INTERFACEs to the functions in another module. The problem was multiple errors when trying to compile, which was solved by getting rid of the explicit INTERFACE in the second module. Some best practices for using modules were also discussed.
  • #1
ted_kingdom
5
0
Hi!
I require help in writing a code where I want to put FUNCTION definitions in one module and INTERFACEs to the functions (as I use assumed-shape arrays in the functions) in another. But I get multiple errors when trying to compile. Could anyone assist me in solving this problem? Please see program below. Thanks in advance.
Code:
MODULE qwer
	CONTAINS
	PURE FUNCTION BMatVect(ShapeFuncDeriv,JacobiInv)
		IMPLICIT NONE
       Real, Intent(IN) :: JacobiInv(:,:),ShapeFuncDeriv(:,:)
       Real::BMatVect(size(ShapeFuncDeriv,1),size(ShapeFuncDeriv,2))
		BMatVect = Matmul(JacobiInv, ShapeFuncDeriv)
	END FUNCTION BMatVect
END MODULE qwer

MODULE ty
	USE qwer
	INTERFACE BMatVect_function
		PURE FUNCTION BMatVect(ShapeFuncDeriv,JacobiInv)
		 IMPLICIT NONE
       Real, Intent(IN) :: JacobiInv(:,:),ShapeFuncDeriv(:,:)
       Real::BMatVect(size(ShapeFuncDeriv,1),size(ShapeFuncDeriv,2))
		END FUNCTION BMatVect 
	END INTERFACE BMatVect_function
END MODULE ty

PROGRAM MyTest
	USE ty
	IMPLICIT NONE
	REAL :: A(2,2) = reshape((/1,1,1,1/),(/2,2/)), &
                   B(2,4) = reshape((/-0.5,-0.5,0.5,0.0,0.0,0.0,0.0,0.5/),(/2,4/)), &
	           C(2,4)
	C = BMatVect (B, A)
	PRINT*, C(1,:)
	PRINT*, C(2,:)
END PROGRAM MyTest
 
Last edited:
Technology news on Phys.org
  • #2
What FORTRAN are you using?

Have you tried adding a 'use qwer' in the program? Not sure if that's the problem but maybe you can't nest things.

http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=%2Fcom.ibm.xlf101l.doc%2Fxlflr%2Fmodproc.htm

Lastly, can you change your use of the spoiler tag to the the code tag? The spoiler is used for hints and makes your program listing flicker on and off with mouse movement.

Some more best practice comments on module usage:

http://stackoverflow.com/questions/...-modules-subroutines-and-functions-in-fortran
 
Last edited by a moderator:
  • #3
Some compilers don't like you putting actual numbers in arrays which haven't been defined as a fixed size. I would try doing something with declaration of in quer by removing the 1 and 2 when determining array size.
Also, as good practice only use the modules that that sub/function/program requires, ideally with 'only' statements for robustness. Some compilers get confused if there are multiple routes to variables etc through different modules. This will also assist should decided to vectorise your code.
 
  • #4
jedishrfu, Thanks a lot for the link to Stackoverflow! It helped. It turns out that when you use MODULE the interface is being generated automatically, so the program knows it. Ergo, the problem was in my second MODULE 'ty' with explicit INTERFACE. I got rid of it. Now everything runs smoothly. Thanks again!:thumbs:

P.S.: I already use CODE tag. If you mean vice versa, SPOILER tag doesn't hide anything for me.
 
  • #5


Hello,

It seems like there may be a few issues with your code. First, in the qwer module, you have not included a real type for the function BMatVect, which could be causing errors when trying to use it in the ty module. Additionally, in the ty module, you are redefining the BMatVect function without specifying a return type, which could also be causing issues.

To fix these issues, you could try adding a real type to the BMatVect function in the qwer module, like this:

REAL :: BMatVect(size(ShapeFuncDeriv,1),size(ShapeFuncDeriv,2))

And in the ty module, you could specify a return type of real for the BMatVect function, like this:

REAL :: BMatVect(size(ShapeFuncDeriv,1),size(ShapeFuncDeriv,2))

I hope this helps. If you continue to have issues, please provide the specific errors you are receiving so we can better assist you.

Best,
 

1. What is a MODULE in Fortran 90/95?

A MODULE in Fortran 90/95 is a program unit that allows the programmer to group related variables, procedures, and functions together. It serves as a way to organize and encapsulate code, making it easier to maintain and reuse.

2. How do I declare and use a MODULE in Fortran 90/95?

To declare a MODULE, use the MODULE keyword followed by the name of the module. Inside the module, you can declare variables, procedures, and functions. To use the module in your main program, use the USE keyword followed by the name of the module. This will allow you to access the variables and procedures defined in the module.

3. What are the advantages of using MODULEs in Fortran 90/95?

MODULEs offer several advantages, including improved code organization, easier code maintenance, and the ability to share variables and procedures between different program units. They also help to avoid naming conflicts and make debugging easier.

4. Can I define multiple MODULEs in the same source file?

Yes, you can define multiple MODULEs in the same source file. However, it is considered good practice to have each MODULE in its own separate source file. This allows for better organization and makes it easier to reuse the MODULE in other programs.

5. Are there any limitations to using MODULEs in Fortran 90/95?

One limitation of using MODULEs in Fortran 90/95 is that they cannot contain executable code. They can only contain declarations of variables, procedures, and functions. Additionally, the order in which MODULEs are declared and used can affect the behavior of the program, so it is important to pay attention to the order in which they are declared and used.

Similar threads

  • Programming and Computer Science
Replies
5
Views
14K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
4
Views
616
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
16
Views
5K
  • Programming and Computer Science
Replies
5
Views
1K
Back
Top