Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Help with fortran code

  1. Sep 6, 2014 #1
    Hey everybody, I am a mechanical engineer and new to Fortran(I have decent experience in programming in C, Matlab and understand languages similar to them). However the Fortran programming structure is new to me. Hence I am facing problems debugging a Fortran code.
    Here is the link of the code which I want to run.

    http://www2.meteo.uni-bonn.de/forschung/gruppen/tgwww/people/abott/fortran/coad1d.f

    I am using a Plato Fortran Compiler.

    The error message i get is :
    " error 399 - Unrecognised statement, did you forget to add /FIXED_FORMAT? "

    It comes for line no 10 which is :

    c dt : time step (input in sec)

    Can anyone please explain what's the reason of this message and is this the correct way of declaring variables in fortran ???

    In case if you are unable to view the code then i shall post it in the reply thread.
    Thanks for any help in advance !!!
     
  2. jcsd
  3. Sep 6, 2014 #2

    jtbell

    User Avatar

    Staff: Mentor

    I suggest you try adding the /FIXED_FORMAT option when compiling, as the error message suggests. I've never used the Plato compiler, so I can't tell you how to actually do it.

    The code that you posted clearly uses the old-style "fixed format" which was once standard for Fortran:

    • A C in character position 1 indicates that the line is a "comment" which is not to be compiled.
    • Statements start in position 7.
    • Positions 1-5 are for the occasionally-used "statement numbers".
    • Position 6 is for a "continuation character" that signals that this line is a continuation of the previous statement.
     
  4. Sep 6, 2014 #3
    Yes, what jtbell said.

    Don't know anything about Plato, either, but a quick google says something about FTN95...so, you may need to look into your user's manual and see how Plato supports Fortran 77 source code.

    It could be that the first indication to the compiler of Fortran 77 source file is its file extension; you may need to set to *.f77 or *.for...maybe *.f alone does the trick; to be sure, do not make it *.f90

    Also, I see a large number of continuation marks ('&' in column 6); in Fortran 77, there is a default value for the maximum number of continuation marks...don't remember what that is ...about 20 or 50...just watch out for it if it complains...you may need another compiler flag to extend this limit.
     
  5. Sep 6, 2014 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    The original Fortran 77 standard allowed for a maximum of 19 continuation lines. Some compilers allow the maximum number of continuation lines to be set with a compiler option.
     
  6. Sep 8, 2014 #5
    Sorry for again disturbing all of you guys, but there seems to be another problem in the program.
    It runs initially, but when I enter the values initially asked, the program stops mid-way showing an error:
    Run-time Error
    *** Error 112, Reference to undefined variable, array element or function result (/UNDEF)

    COURANT - in file help.for at line 149 [+01de]

    main - in file help.for at line 45 [+05dc]

    So there is an error in the Courant subroutine or error in calling it. Again i am a little unfamiliar to fortran subroutines but is it possible for you to also check what the problem here is ???
    As far as i could see the variables inside the subroutine were declared beforehand. And the logic looks correct.
    Also is the declaration line 45 calling courant subroutine done correctly ???

    Any help is appreciated. I have already received awesome help from you all here.
     
  7. Sep 8, 2014 #6

    Mark44

    Staff: Mentor

    In the Courant subroutine, the line shown below in red seems to have a typo.
    Code (Text):
    do i=1,n
          do j=i,n
             x0=e(i)+e(j)
             do k=j,n
                if (e(k).ge.x0.and.e(k-1).lt.x0) then
                   [color="red"]if (c(i,j).lt.1.-1.d-08) then[/color]
                      kk=k-1
                      c(i,j)=dlog(x0/e(k-1))/(3.d0*dlnr)
                   else
                      c(i,j)=0.
                      kk=k
                   endif
                   ima(i,j)=min(n-1,kk)
                   go to 2000
                endif
             enddo
     2000    continue
             c(j,i)=c(i,j)
             ima(j,i)=ima(i,j)
          enddo
          enddo
     
    Possibly that line should be:
    Code (Text):
    if (c(i,j).lt.1.d-08) then
     
  8. Sep 8, 2014 #7
    Sorry, but i am unable to see the difference ... where did you exactly change ???
     
  9. Sep 8, 2014 #8

    Mark44

    Staff: Mentor

    The original code has c(i,j) .lt. 1.-1.d-08

    The part in bold above is a typo, I believe, and should be removed.

    One reason it's difficult to make sense of is that Fortran programmers seem unaware of the benefits of using white space to make the code more readable. For example, the line above the one I marked in red contains this code:
    Code (Text):
    (e(k).ge.x0.and.e(k-1).lt.x0)
    There is not a single space character here, making it much more difficult to spot anything that might be incorrect.
     
  10. Sep 8, 2014 #9
    Mark44 : Even after i correct as per your possible typo, the same error comes.
    Just curious, what made you think of the typo ???
     
  11. Sep 8, 2014 #10
    In case if you guys are running the code, here are the sample values:
    dt:1
    r0:10
    xmw:1
    scal:2
    isw:2
     
  12. Sep 8, 2014 #11

    Mark44

    Staff: Mentor

    I just looked at your Courant routine and spotted it.

    Nothing else in that routine jumps out at me, so I would suggest you use a debugger to find out what's going on to produce that error.
     
  13. Sep 8, 2014 #12

    AlephZero

    User Avatar
    Science Advisor
    Homework Helper

    It might not be a typo. The programmer might have thought it was easier to read than
    Code (Text):

    if (c(i,j).lt.0.9999999d0) then
     
     
  14. Sep 9, 2014 #13
    Do any of you guys have experience with f2c program ???
    I was wondering whether I could translate the given FORTRAN code to C, which will make debugging and changing code easier.
     
  15. Sep 9, 2014 #14

    Mark44

    Staff: Mentor

    Just my opinion, but I don't think translating this code to C is a good idea.

    After taking another look at your courant subroutine, this line of code might be causing problems if the argument to dlog happens to be ≤ 0.
    Code (Text):
    c(i,j)=dlog(x0/e(k-1))/(3.d0*dlnr)
     
    To determine whether this line is the culprit, put a write statement before that line, having it print the values of x0, e(k - 1), and x0/e(k - 1). In any case, you don't want to be trying to take the log of zero or a negative number.

    If this is what's happening, you can guard against this occurring by placing the assignment statement that I listed inside a suitable if statement.

    The logic might be something like this:
    if |e(k - 1)| < 1.0 d-08 .or. x0/e(k - 1) < 1.0 d-08 then set c(i, j) to 0.0
    endif
     
  16. Sep 9, 2014 #15
    Hey Mark44, that's a great idea. I did, as you said and added the print statement.
    print*, x0, e(k-1), x0/(e(k-1)
    Well none of the values were equal to 0 or negative, hence i don't think that was the error in code.
    But it's a start in debugging, and I would love to hear more such checks from you and rest of the guys here.
     
  17. Sep 9, 2014 #16
    Just a thought, do you guys see a possible error in this line???
    open (16,file='boplot00.out',status='old')
    write (16,6100) rri,eei
    6100 format (5e16.8)
    Is the correct way to open a new file for storing results ???
     
  18. Sep 9, 2014 #17

    DrClaude

    User Avatar

    Staff: Mentor

    Not a new file, as it explicitly mention that the status be 'old', meaning an already existing file. Since the file is used only for output, you can change the line to
    Code (Text):

          open (16,file='boplot00.out')
     
    Otherwise, the program runs fine when I try it with the input data you gave in post #10, and without the modification of Mark44.
     
  19. Sep 9, 2014 #18
    Yup, well that was the error.
    Thank you all of you, this program has been fun to debug. I don't know why the compiler didn't show this error or why the hell it was pointing to courant sub routine. I had to find this one manually by reading it.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Help with fortran code
  1. Help with fortran code (Replies: 4)

Loading...