fortran modules' subroutine name conflict - fortran

I have two different modules each one has the same subroutine. main program will call one of them based on a certain condition. I want to avoid renaming each of these subroutine to a different name.

If you have two modules A and B containing the same method foo() then first you can create a local alias with
program SOModNames
use A, fooA => foo
use B, fooB => foo
implicit none
! Variables
real X(10), Y(10)
call fooA(X,10)
call fooB(Y,10)
end program SOModNames
Unofrtunatelty you cannot scope to a module with call A::foo(X,10) for example.

Related

How to avoid rewriting the same interface for a deferred type-bound procedure implemented in a submodule?

Consider the following code, which defines an abstract type foo with a deferred procedure sub, and a type foo2 which extends foo:
MODULE m
TYPE, ABSTRACT:: foo
CONTAINS
PROCEDURE(sub_int), DEFERRED:: sub
END TYPE
INTERFACE
SUBROUTINE sub_int(THIS, x)
IMPORT:: foo
CLASS(foo), INTENT(IN):: THIS
DOUBLE PRECISION, INTENT(INOUT):: x
END SUBROUTINE
END INTERFACE
TYPE, EXTENDS(foo):: foo2
CONTAINS
PROCEDURE:: sub
END TYPE
INTERFACE
MODULE SUBROUTINE sub(THIS, x)
CLASS(foo2), INTENT(IN):: THIS
DOUBLE PRECISION, INTENT(INOUT):: x
END SUBROUTINE
END INTERFACE
END MODULE
SUBMODULE (m) s
CONTAINS
MODULE PROCEDURE sub
x= x**2
END PROCEDURE
END SUBMODULE
Is there a way to avoid writing the second interface?
I understand that it is needed in order to declare sub as a module procedure (otherwise the implementation would need to be done in the module, and not in the submodule), but is this the only way to do it?
In other words, is it possible to implement the procedure sub for foo2 without rewriting the entire interface for it?
It is not possible. A separate module subprogram requires an interface block, either in its defining submodule or an ancestor of its defining submodule.
Whether a procedure happens to be nominated by one (or more!) bindings of one (or more!) types is totally irrelevant.

How to point with one method to two different methods?

I have intention to point with the same procedure to two different procedures but I have not experienced programmer in Fortran so I need help.
This is my simple code:
module types
type :: type_one
integer, private :: a1,a2
contains
procedure, public :: f_data => set_data_a1
procedure, public :: f_data => cal_data_a2
end type type_one
private :: set_data_a1,cal_data_a2
contains
integer function set_data_a1(this)
class(type_one) :: this
this%a1 = 2
end function set_data_a1
integer function calc_data_a2(this)
class(type_one) :: this
this%a2 = this%a1 + 3
end function calc_data_a2
end module types
program types_pro
implicit none
type(type_one) :: type_obj
type_obj%f_data()
end program types_pro
I got this error:
`There is already a procedure with binding name 'f_data' for the derived type 'type_one' |
Is it possible to call both procedures at the same time with type_obj%f_data()?
A generic name, such as your f_data, allows calling procedures with different input signatures (rank, type, kind, and number of arguments) by the same name. But, because they have the same argument signatures the compiler can't determine which of calc_data_a1 and calc_data_a2 to execute when your code makes a call to f_data.
Which leads me to a question for you: How do you expect the compiler or code to behave ? What do you want the compiler to do ? (OK that was two questions.)
As a general rule, if you want the compiler to execute two procedures you have to make two calls. You could, I suppose, have one procedure call the other if you want both to run when one is called.
If you want to wrap multiple functions behind the same name they have to have different input signatures so that the compiler can work out which one to call.
Obviously my earlier version of this answer wasn't explicit enough:
No, there is no way to write your code to execute two different procedures in response to a single call to one of them. (Unless, that is, the one calls the other.) Furthermore, it is unreasonable to expect a single (procedure) pointer to point, at any one time, at more than one target.

Fortran doesn't call FINAL on arrays or program variables [duplicate]

I have an object parsing a textfile. Here's my main program:
program main
use Parser_class
implicit none
type(Parser) :: Parser
call Parser%ProcessFile('data.txt')
call Parser%Deallocate
end program main
where the type definition is
module Parser_class
type :: Parser
contains
procedure, public :: ProcessFile
procedure, public :: Deallocate
end type Parser
contains
subroutine ProcessFile(self)
...
end subroutine
subroutine Deallocate(self)
class(Parser) :: self
...
end subroutine
end module Parser_class
I read about the final keyword and altered the type definition to
module Parser_class
type :: Parser
contains
procedure, public :: ProcessFile
final :: Deallocate
end type Parser
contains
subroutine ProcessFile(self)
...
end subroutine
subroutine Deallocate(self)
type(Parser) :: self
...
end subroutine
end module Parser_class
Additionally, in the main program I don't have call Parser%Deallocate anymore. The finalizer doesn't get called now at any time. I somehow get this is because I never destroy or overwrite the Parser object. But how could I do this, or what is the proper way to handle the deallocation process?
In the Fortran 2008 standard, when finalization comes about is given in section 4.5.6.31. I won't copy all the times here, but I will summarize.
What is clearly mentioned following from when, is when not:
If image execution is terminated, either by an error (e.g. an allocation failure) or by execution of a stop-stmt, error-stop-stmt, or end-program-stmt, entities existing immediately prior to termination are not finalized.
This covers your program. Parser is in the program's scope and it still exists at the end of the program. There are no apparent other things which would cause finalization.
If Deallocate is a final procedure for the type, then there are subtle ways in which finalization of an object of that type differ from a call of the type-bound procedure. In finalization, the process is recursive: components and parents are themselves subject to finalization. With a subroutine call that recursion must appear manually in some way.
In many cases one doesn't care that an entity isn't finalized at the end of the program. After all, any deallocation is then the operating system's problem, not the programmer's. However, there are times other forms of tidying up are indeed desirable.
True finalization can be forced in some ways. If the list below is examined, two options come to mind:
make the Parser object allocatable and explicitly deallocate it;
wrap the whole thing in a block construct.
To crudely summarize when finalization happens:
when there's deallocation (pointer or allocatable);
as procedure start-up with intent(out) arguments;
when reaching the end of an executable construct or subprogram, for non-saved local objects;
just before intrinsic assignment to a variable;
after the value of a function's result is finished with.
1If you aren't reading the final form of the document you'll want to pretend paragraphs 5 and 7 don't exist.

In Fortran, when in extension definition, how to set a public procedure into private?

Assume I first defined a type A in which a public procedure f is defined, and may also be bonded to A. In another module I have this type extended into B. However, when I use type B, I do not want f to be exposed.
By the way, I don't want to use the submod technique.
complement:
Assume type(A) is already defined:
module mA
type::A
...
contains
procedure::f
endtype
endmodule
In another module B, we extend A as:
module mB
use mA
type,extends(A)::B
...
endtype
endmodule
In this module, f may still be used. However, next, in module mC I will use(declare)
type(B)::Ob
and I wish "call Ob%f()" to be illegal. Or equivalently speaking, I want to ban some of the function when I extend a class.
It is hard to understand your descriiption, but if I understand it correctly it is not possible.
Consider you have a variable class(A) :: o. You are allowed to call
call o%f()
class(A) is polymorphic and can be any extended type of A so its dynamic type can be type(B). So B MUST provide publicly accessible procedure f to stay compatible with the parent.

fortran module use wrong

The following is the code skeleton from VASP. My work is that porting it on one new platform. And when I use the compiler of that platform, I got the error. If I comment out the use m and I also get "It must have an explicit interface specified." I just want to make a minimum modification to avoid incurring errors into other parts of the program.
module m
interface
subroutine a
end subroutine
end interface
interface
subroutine b
end subroutine
end interface
end module
subroutine a
use m
call b
end subroutine
In the code as shown, the interface for subroutine a defined in the module is accessible inside subroutine a via use association. Because a subprogram already defines an explicit interface for any procedures that it defines, this means that you have two separately defined explicit interfaces for the same procedure accessible in the same scope. This is a violation of the rules of the language (F2008 12.4.3.2p7).
You can use the ONLY clause on the USE statement to exclude the interface body variant of the subroutines interface from being made accessible by use association inside the subroutine.
module m
interface
subroutine a
end subroutine
end interface
interface
subroutine b
end subroutine
end interface
end module
subroutine a
use m, only: b
call b
end subroutine
Why not simply:
module m
contains
subroutine b
! code
end subroutine b
subroutine a
! code
call subroutine b
end subroutine a
end module m
From what you've posted I don't see any recursion (neither subroutine calls itself, a calls b but b doesn't call a) nor any need for it. Nor do I see a need for explicitly writing interface blocks; package the subroutines into a module and let the compiler take care of checking interface specifications and agreement.