How can I fix incorrect output from my Fortran subroutine for a test matrix?

In summary, the problem is that you have two arrays named test_mat - one in the main program, and the other in your subroutine. The only one that gets initialized is the one in the subroutine, so when you print out its values, you get what you expect. You can fix the problem by passing the existing matrix to the subroutine.
  • #1
autobot.d
68
0
Hi all,

So I have a program that calls a module that contains a subroutine so I can make a matrix.
When I write the matrix in the subroutine (commented out part in module) I get the right output, but when the main program tries to output it is some insanely small or large numbers.
I am new to Fortran (not coding) so any help/criticism would be greatly appreciated! Definitely a bit of a learning curve but I am excited to learn fortran.

Code:
program test

use test_mod

implicit none

integer :: k 
real, allocatable, Dimension(:,:) :: test_mat

write(*,*) 'Dimension for square test matrix: '
read (*,*) k

allocate(test_mat(k,k))
call test_matrix(k)

!This gives bad output, but right dimensions
write(*,*) test_mat
deallocate(test_mat)

end program test

Code:
module test_mod

implicit none

contains

    subroutine test_matrix(k)
    
integer :: k
real, dimension(:,:), allocatable :: test_mat
INTEGER :: i,j,m,nallocate(test_mat(k,k))

do i = 1,k
do j = 1,k

test_mat(i,j) = 1.d0*((i+j-1))
end do
end do

!This loop gives correct output
!do m = 1,k
!write(*,*)(test_mat(m,n), n=1,k)
!end do

deallocate(test_mat)

end subroutine test_matrix
end module test_mod
 
Technology news on Phys.org
  • #2
I'm not a Fortran expert, but I think the problem is that you have two arrays named test_mat - one in the main program, and the other in your subroutine. The only one that gets initialized is the one in the subroutine, so when you print out its values, you get what you expect.

The array in the main program is never initialized, so when you print its values, you get garbage.
 
  • #3
autobot.d said:
When I write the matrix in the subroutine (commented out part in module) I get the right output, but when the main program tries to output it is some insanely small or large numbers.
As Mark says, you've actually defined a new matrix in the subroutine. What you need to do is to pass the existing matrix to the subroutine. Try the following code (based on your code) for example and see if you can work out what it's doing

BTW. I put some redundant code in there to assign to "j" in the subroutine. This is just to help you understand how the variables defined in the subroutine are different to those in the main program, even if they have the same name (whereas the "passed" variables are effectively the same, even if they are given different names).

Code:
program alloctest
implicit none
  integer :: i,j 
  real, allocatable, Dimension(:,:) :: test_mat

  print "(a$)",'Dimension for square test matrix: '
  read (*,*) j
  allocate(test_mat(j,j))
  call test_matrix(test_mat,j)

  do i = 1,j
    print *,test_mat(i,:)
  end do

  deallocate(test_mat)

contains

subroutine test_matrix(tm,size)
implicit none
integer :: size
real, dimension(:,:) :: tm

  integer :: i,j
  do i = 1,size
    do j = 1,size
      test_mat(i,j) = 1.d0*((i+j-1))
    end do
  end do
  j = 0          ! Should have no effect in main program.

end subroutine test_matrix
end program alloctest
 
Last edited:
  • #4
Thank you so much. That was very helpful and it worked!
 
  • #5


I would first recommend checking for any errors in your code. It's possible that there may be a mistake in the subroutine that is causing the incorrect output. You can also try using a debugger to step through the code and see where the issue may be occurring.

Another possibility is that the input values for your matrix are not being read correctly in the main program. Make sure that the dimensions you are inputting match the dimensions you are allocating in the subroutine.

If the issue still persists, I would suggest breaking down the code into smaller parts and testing each part separately to identify where the problem may lie. It's also helpful to print out intermediate values or use a visual debugger to see how your matrix is being populated and if there are any unexpected values.

Additionally, you can try using different input values for your matrix to see if the issue is specific to certain values or if it happens with all inputs. This can help narrow down the source of the problem.

Overall, my advice would be to carefully review your code, check for errors, and use debugging techniques to identify and fix the issue. Fortran can be a bit tricky to learn, but with practice and patience, you will be able to successfully debug and improve your code.
 

1. What is a Fortran subroutine output?

A Fortran subroutine output is the result or data that is produced by a subroutine in the Fortran programming language. Subroutines are sections of code that can be called upon to perform specific tasks and can return data to the main program.

2. How do I use a Fortran subroutine output in my main program?

To use a Fortran subroutine output in your main program, you need to declare the subroutine with the "EXTERNAL" statement and then call the subroutine in your main program. The output from the subroutine can then be stored in a variable or used in other calculations.

3. Can a Fortran subroutine have multiple outputs?

Yes, a Fortran subroutine can have multiple outputs. These outputs can be in the form of variables or arrays, and they can be used in the main program for further calculations or analysis.

4. How do I ensure that the Fortran subroutine output is accurate?

To ensure the accuracy of the Fortran subroutine output, it is important to properly initialize and declare all variables within the subroutine. It is also important to thoroughly test the subroutine with different input values to ensure that the output is consistent and correct.

5. Can I pass variables from my main program to a Fortran subroutine?

Yes, variables can be passed from the main program to a Fortran subroutine as input parameters. These variables can then be used in the subroutine to perform specific tasks and return the desired output.

Similar threads

  • Programming and Computer Science
Replies
4
Views
622
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
Back
Top