Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

errors and debugging in modern Fortran

Status
Not open for further replies.

Kjamil05

Programmer
Jun 25, 2014
2
US
Hello,

I have recently begun to learn coding in Fortran for a work project but am stuck on the debugging phase. I have tried to create a class using a module file with a derived type to hold object variables which "contains" several subroutines. Should the subroutines be able to access the variables without my having to declare them in each subroutine all over again?

Some of the common errors I am getting are "invalid delimiter" and notifications that a variable name has already been used or that an array has already been used as a function name. It seems as though the compiler is mistaking an array for a function in some cases, not sure why.

The 'invalid delimiter' issue usually comes up with statements accessing elements of arrays, such as "Psi(1)" or "Theta(I)" where I is an integer loop control variable.
This seems to happen both in my work project and a simpler program I created just to test these things.

Here is the main file of the simple program:

Code:
program Test
use Test_it

    implicit none

    type(resistance) :: thing

    call thing%Display(Psi, Theta)

    end program Test

The 'invalid delimiter' error highlights "thing&Display(Psi, Theta) as the problem area.

And here is the module file for the above program:

Code:
module Test_it

    implicit none

    ! place global type definitions, generic interfaces, 
    ! variable declarations and initializations here

    Type resistance
        real, dimension(10) :: Psi
        real, dimension(10) :: Theta

        end type resistance

contains

subroutine Display (Psi, Theta)
    real, dimension(10) :: Psi, Theta
       
        write (*,*) 'Enter a real number for Psi(1)'
        read (*,*) Psi(1)

        write (*,*) 'Enter a real number for Theta(1)'
        read (*,*) Theta(1)

        write (*,*) 'Psi(1):', Psi(1)
        write (*,*) 'Theta(1):', Theta(1)
       
end subroutine Display

end module Test_it

I would greatly appreciate anyone's insight into this matter and advice on how to best debug a lengthy, complex Fortran program.

Thank you,

Kei
 
Hint 1: Please re-read how to use module; I am afraid you are misunderstanding how to use them.

Hint 2: everything in the module is available to you at a single level as if type resistance and subroutine display were siblings. So, no need to do thing%display, in fact it is wrong. All you need to do is "call display(Psi, Theta)".

Hint 3: yes, variables declared in the module are available inside the contained subroutines.

Try this module:
Code:
module Test_it

    implicit none
    real, dimension(10) :: Psi
    real, dimension(10) :: Theta

contains

    subroutine ReadValues ()   
        write (*,*) 'Enter a real number for Psi(1)'
        read (*,*) Psi(1)
        write (*,*) 'Enter a real number for Theta(1)'
        read (*,*) Theta(1)
    end subroutine ReadValues

    subroutine Display ()
        write (*,*) 'Psi(1):', Psi(1)
        write (*,*) 'Theta(1):', Theta(1)      
    end subroutine Display

end module Test_it
with this main program:
Code:
program Test
use Test_it
    implicit none
    call ReadValues()
    call Display()
end program Test
 
hello and thanks for your response.

I did try typing in what you suggested but I still get various errors. The IDE is Lahey Fortran 95 using Microsoft Visual 2012 as the shell. We tried compiling code featuring a class from Lahey's website and even that didn't work, so now we think there may be something wrong with the IDE software itself.

Anyway, isn't it usually necessary to declare an object of the class type in the main file? That's why I use the % accessor. Some of the references I have been looking at make use of it as well.
 
I don't usually program using classes or with anything beyond Fortran 95. By the way, what you seem to be calling class is actually called "derived data type" in Fortran; I think is kind of equivalent to structures from C...not necessarily what understand a class to be.

In any case, I use gfortran to compile the code provided.

 
I have no much experience with this OOP stuff in Fortran, but IMO If you want call the procedure like this
Code:
type(resistance) :: thing
...
call thing%Display(Psi, Theta)
you have to define procedure pointer in your structure resistance.

For example see this thread:

But note that this approach doesn't work in all current Fortran compilers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top