Replacing common blocks with Modules in a subroutine

Replacing common blocks with Modules in a subroutine

Hello all,Firstly, I am not a programmer by trade, so find it challeneging. Secondly, I have read up what I can to help me with this problem, however, I still very much need to help of experts with this problem and would really appreciate it.I have a subroutine (which I use with abaqus), I would like to replace the common blocks I use in the subroutine with modules. The common blocks are currently used in the following way:[fxfortran]subroutine(.....)




B(part)=B(part)-(x*y*z)[/fxfortran] Would really appreciate your help in changing this from common blocks to using modules.Thanks.

7 posts / 0 new
Last post
For more complete information about compiler optimizations, see our Optimization Notice.

One way of doing this is to let every common block of the same name be in a module of
its own and then use that module wherever the common block was used.

Something along these lines:

! Contents of COMMON block "/data/"
module common_data
use module_that_defines_the_parameters !Get yon
implicit none
real, dimension(yon,1) :: A
real, dimension(yon) :: B
end module common_data

and in your subroutine:

subroutine xxx(...)
use common_data
end subroutine

Some notes:
- Make sure you define the variables in the COMMON block explicitly. This is to avoid
nasty mistakes that will make your considerably more difficult.
- Parameters like "yon" need to be defined somewhere - I would go for a separate module that
defines them, if I were to use the above approach
- An alternative is to put all COMMON-blocks (and the parameters they require) in a single
module. This may cause problems, though, as you would introduce extra variables into
routines that already have variables of the same name (but possibly different types)

But a single module is easier to incorporate everywhere you need it.



An additional problem area is when a named common block is declared differently in different source files.

When this happens, the same named area is mapped to differently named variables (in each respective source file).

When this data is temporary for each use of a subroutine/function then there would be no issue (other than a common programming assumption that the data is or is not uninitialized, and to what).

Then there is the use of EQUIVILENCE. You have to be careful about untangling EQUIVILENCE'd variables.

If at all possible, follow Arjen's example of commenting the original code as this will remind you of what used to be.

Jim Dempsey

Yes, Jim is right about COMMON blocks that are defined differently in different program units.
This is actually one of the reasons they should be avoided if at all possible: a COMMON block
merely identifies a memory region. How you partition that region into scalar and array variables
is entirely up to the _individual_ program units that use them. If you are lucky then it is at least
documented in the code via EQUIVALENCE. (I have used such techniques in the distant past when
there was no portable way of allocating memory.)

Yet another thing to watch out for is the BLOCK DATA program unit. It essentially defines the
initial values for variables in a COMMON block. You can include the data statements in the
definition of the module.



Thanks everyone for your input, it is very much appreciated.
1) Firstly I need to use the module directly in the subroutine and not outside it, as I think Abaqus will only recognise the subroutine. Therefore can I place the following code within the subroutine! Contents of COMMON block "/data/"
module common_data
use module_that_defines_the_parameters !Get yon
implicit none
real, dimension(yon,1) :: A
real, dimension(yon) :: B
end module common_dataBy the way the here is the declaration of yonPARAMETER (yon=3000)2) I am not sure what you mean by:use module_that_defines_the_parameters !Get yonCould you please explain, thanks.Really appreciate all your help and advice.Thank-you

If you are able to add only a subroutine it simply means you have no access to the full source code. If this is the case I see no solution but follow the technique used in the program, i.e. use common blocks. I understand the developers of Abaqus provided you with kind of programmers interface to add a subroutine.
You have to have access to all source files in order to change common blocks to modules in every place it is used.
You cannot declare a module in a subroutine but you can declare many subroutines in a module. If you can only add a subroutine to the code you can only use common blocks. Sorry.

Thank you Boguslaw, you post was very helpful and kind. You are right I do not have access to the full source code. I hope that common blocks will solve the problem as I am currently having a few issues with them.Thanks

Leave a Comment

Please sign in to add a comment. Not a member? Join today