-1

I am trying to calculate the moment of inertia in fortran. The formula I am using is following: The code I am using:

 program moment
    implicit none
    
        real :: cR,h,rho0,a,b,c,d,resultV,pi,resultMI,aMass,exactresMI,exactresV,r,res,z,rho
        integer :: m,n
    
    ! rho0 = density, cR=capital R( radius),h= height )
    rho0=10000
    cR=0.05
    h=0.1
    a=0.d0
    b=h
    c=0.d0
    d=cR
    m=1000
    n=1000
    call cheb2(a,b,m,c,d,n,funV,res)
    pi=4*datan(1.d0)
    resultV=res*2*pi
    exactresV= pi/3*cR**2*h
    write(*,*)
    write(*,*) "Numerical volume result =", resultV
    write(*,*) "Exact volume result = ",exactresV
    
    
    call cheb2(a,b,m,c,d,n,funV,res)
    resultMI=res*2*pi
    aMass=exactresV*rho0
    exactresMI=3/10.*aMass*cR**2
    write(*,*)
    write(*,*) "Numerical Moment of Inertia result =", resultMI
    write(*,*) "Exact Moment of Inertia  result = ",exactresMI

end program
    
    function funV(z,r)
    if (r.gt.z*cR/h) then
        rho=0.d0
    else
        rho=1.d0
    end if
    funV=rho*r
    return
    end
    
    
    
    
    function funMI(z,r)
    if (r.gt.z*cR/h) then
        rho=rho0
    else
        rho=1.d0
    endif
    funMI=rho*r**3
    return
    end
    include "CHEB.FOR"

Our instructor does not use "implicit none" , so I am really new to this operator. Out instructor gave us CHEB.FOR code for calculating 2 dimensional integrals. I am writing it here:

    subroutine ch4xy(al,bl,cl,dl,f,ri)
       implicit double precision (a-h,o-z)
       common/ttxy/ t1,t2
       dimension xx(4),yy(4)
       c1=(al+bl)/2.d0
       c2=(dl+cl)/2.d0
       d1=(-al+bl)/2.d0
       d2=(dl-cl)/2.d0
       xx(1)=c1+d1*t1
       xx(2)=c1+d1*t2
       yy(1)=c2+d2*t1
       yy(2)=c2+d2*t2
       xx(3)=c1-d1*t1
       xx(4)=c1-d1*t2
       yy(3)=c2-d2*t1
       yy(4)=c2-d2*t2
       ss=0
       do 3 i=1,4
       do 3 j=1,4
       ss=ss+f(xx(i),yy(j))
3      continue
       ri=ss*d1*d2/4.d0
       return
       end
      
       subroutine cheb2(a,b,m,c,d,n,f,r)
       implicit double precision (a-h,o-z)
       external f
       common/ttxy/ t1,t2
    t1=0.187592
    t2=0.794654
    hx=(b-a)/m
    hy=(d-c)/n
    rr=0
    do 5 i=1,m
    do 5 j=1,n
    aa=a+(i-1)*hx
    bb=aa+hx
    cc=c+(j-1)*hy
    dd=cc+hy
    call ch4xy(aa,bb,cc,dd,f,ri)
    rr=rr+ri
5   continue
    r=rr
    return
    end

When I compile the file, a couple of errors and a warning appear:

CHEB.FOR:19:17:

   19 |        do 3 j=1,4
      |                 1
Warning: Fortran 2018 deleted feature: Shared DO termination label 3 at (1)
CHEB.FOR:36:11:

   36 |  do 5 j=1,n
      |           1
Warning: Fortran 2018 deleted feature: Shared DO termination label 5 at (1)
momentOFinertia.f95:17:27:

   17 | call cheb2(a,b,m,c,d,n,funV,res)
      |                           1
Error: Symbol 'funv' at (1) has no IMPLICIT type

First, I dont understand why funV is unclassifiable statement, it classifies as a function. Second, our instructor used some old operations which is apparently not valid in new fortran. I dont know what could replace "shared do".

gray KK
  • 21
  • 4
  • 3
    Please never upload error messages es pictures. It is *very important* for them to be copy-pasted *as text* so that they are searcheable. For example, for people with a similar problem. You also should search if questions a similar message were asked before. Please se [ask] and upload the error messages **as text**. – Vladimir F Героям слава Nov 23 '21 at 06:32
  • 1
    Also, please distinguish *errors* and *warnings*. The errors block your compilation, the warnings just warn you, but let you compile your code. – Vladimir F Героям слава Nov 23 '21 at 06:35
  • @VladimirF Oh I see! I will upload the error message in the text form. So if a warning notifies me , that I am using a deleted feature, will it just skip that part of the code? – gray KK Nov 23 '21 at 07:02

1 Answers1

1

The main problem (fixed by your edit)is that your code misses an end or end program at the end. Alternatively, you could put an end program after your functions and contains between the subroutine and the main program body. That would make the functions to be internal subprograms and would fix your other problem - no implicit type for the functions.

This - putting the functions inside the program as internal subprograms, allows the program to "see" them and then the program can correctly pass them to other procedures. Alternatively, you could make an interface block for them or declare their type and let them be external. See Why do I have to specify implicitly for a double precision return value of a function in Fortran? for more.


You also have a type mismatch. The code you got from your instructor uses double precision but your code uses the default real. You have to synchronize that. Update your code to double precision, either using double precision or using real kinds.


The compiler also warns you that your program is using deleted features. These features were deleted in modern revisions of the Fortran standards. However, the compiler remain largely backwards compatible and will compile the code including those features anyway, unless you request strictly a certain standard revision.

In this case two do-loops use one common continue statement

    do 5 ...
      do 5 ...
 5  continue

This is not allowed and can be fixed by inserting another continue with another label or, better, by using end do.

  • Thank you so much! I added "end program " into the code and updated the errors. Now my only problem is "funV has no implicit type". If I assign real value to funV it shows two more errors related with "argument 'a' at (1); passed REAL(4) to REAL(8)" . Could you give me a hint how to resolve this issue? – gray KK Nov 23 '21 at 07:16
  • Please see the full answer, but please do not rewrite yor code when you already have an anwer about that problem, making the answer useless. – Vladimir F Героям слава Nov 23 '21 at 07:22