Fortran, subroutine with allocatable, intent(out) array

In summary, the individual is struggling to bring dynamic arrays from a subroutine back into the main program. They have tried defining the arrays as allocatable and using common blocks, but have not been successful. They are considering using interface blocks but are unsure about how to do so. They have also tried defining the arrays as INTENT(INOUT) in the subroutine, but it did not work.
  • #1
alyflex
9
0
Hi,

I have a rather large program written in fortran 77/90. In one of the subroutines a lot of local dynamic arrays are declared.

In order to make this program able to calculate more stuff, I need to bring these dynamic arrays back into the main program.

I have tried to define the arrays allocatable in the main program and then passing them on to the subroutine which sets them as allocatable. But when I allocate them in the subroutine the program crashes.

I have tried to do it with common blocks, but as far as I can see that's a dead end.

I have considered trying interface blocks, but I am still a bit uncertain as to how they work and if it is possible to do it with those.

Does anyone know a way to do this, I have been stuck with this problem for 2 days now, so any help would be much appreciated.

A short example of what I am trying to do

Main.f90
Program main
double complex, allocatable :: Vlm(:)
...
Call init_potential(input,output,Vlm)
...
End main



init_potential.f90
subroutine init_potential(input,output,Vlm)
double complex, allocatable, intent(out) :: Vlm(:)
...
allocate(Vlm(sizeofVlm))
...
end init_potential
 
Technology news on Phys.org
  • #2
If you're allocating in the main program, have you tried defining it as INTENT(INOUT) in the subroutine?
 
  • #3
minger said:
If you're allocating in the main program, have you tried defining it as INTENT(INOUT) in the subroutine?

I'm not allocating it in the main program, but I have tried it anyways, with no succes.
 

1. What is Fortran?

Fortran is a programming language commonly used for scientific and mathematical computations. It was first developed in the 1950s and has gone through several revisions since then.

2. What is a subroutine in Fortran?

A subroutine is a section of code that performs a specific task and can be called from other parts of a program. It is useful for organizing and reusing code.

3. What does "allocatable" mean in Fortran?

In Fortran, "allocatable" refers to the ability to dynamically allocate memory for arrays at runtime. This allows for more flexibility in the size and shape of arrays.

4. What is the purpose of the "intent(out)" keyword in Fortran?

The "intent(out)" keyword is used to specify that an argument in a subroutine is only used to return a value and not to receive any input. This allows the compiler to optimize the code and can also help with error checking.

5. How do you declare an allocatable, intent(out) array in Fortran?

To declare an allocatable, intent(out) array in Fortran, you would use the "allocatable" keyword before the array name and include "intent(out)" in the argument list. For example: "subroutine my_subroutine(array, intent(out))".

Similar threads

  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
5
Views
7K
  • Programming and Computer Science
Replies
4
Views
615
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
14
Views
3K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top