Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28632 Discussions

Replacing common blocks with Modules in a subroutine

mechprog
Beginner
1,847 Views
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(.....) DECLARATIONS common/data/A(yon,1),B(yon) -------- A(part,tun)=A(part,tun)-(x*y*z) B(part)=B(part)-(x*y*z)[/fxfortran]
Would really appreciate your help in changing this from common blocks to using modules.
Thanks.
0 Kudos
6 Replies
Arjen_Markus
Honored Contributor I
1,847 Views
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.

Regards,

Arjen
0 Kudos
jimdempseyatthecove
Honored Contributor III
1,847 Views
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
0 Kudos
IDZ_A_Intel
Employee
1,847 Views
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.

Regards,

Arjen
0 Kudos
mechprog
Beginner
1,847 Views
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_data
By the way the here is the declaration of yon
PARAMETER (yon=3000)
2) I am not sure what you mean by:
use module_that_defines_the_parameters !Get yon
Could you please explain, thanks.
Really appreciate all your help and advice.
Thank-you
0 Kudos
Boguslaw_Bochenski
1,847 Views
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.
0 Kudos
mechprog
Beginner
1,847 Views
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
0 Kudos
Reply