Fortran Code Help for Mechanical Engineers | Solving Debugging Issues

  • Fortran
  • Thread starter 1994Bhaskar
  • Start date
  • Tags
    Code Fortran
In summary: The line in red is typo and should be:do j=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 if (c(i,j).lt.1.-1.d-08) then
  • #1
1994Bhaskar
134
0
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 !
 
Technology news on Phys.org
  • #2
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.
 
  • #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.
 
  • Like
Likes 1 person
  • #4
gsal said:
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.

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.
 
  • #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.
 
  • #6
In the Courant subroutine, the line shown below in red seems to have a typo.
Code:
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:
if (c(i,j).lt.1.d-08) then
 
  • #7
Sorry, but i am unable to see the difference ... where did you exactly change ?
 
  • #8
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:
(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.
 
  • #9
Mark44 : Even after i correct as per your possible typo, the same error comes.
Just curious, what made you think of the typo ?
 
  • #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
 
  • #11
1994Bhaskar said:
Mark44 : Even after i correct as per your possible typo, the same error comes.
Just curious, what made you think of the typo ?
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.
 
  • #12
Mark44 said:
In the Courant subroutine, the line shown below in red seems to have a typo.
Code:
[color="red"]if (c(i,j).lt.1.-1.d-08) then[/color]

Possibly that line should be:
Code:
if (c(i,j).lt.1.d-08) then

It might not be a typo. The programmer might have thought it was easier to read than
Code:
if (c(i,j).lt.0.9999999d0) then
 
  • #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.
 
  • #14
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:
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
 
  • #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.
 
  • #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 ?
 
  • #17
1994Bhaskar said:
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 ?
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:
      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.
 
  • #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.
 

1. What is Fortran code and how is it used in mechanical engineering?

Fortran (short for Formula Translation) is a programming language that is commonly used in scientific and engineering applications, including mechanical engineering. It is especially well-suited for numerical computations and is often used to write code for solving complex engineering problems.

2. How can Fortran code help with debugging issues in mechanical engineering?

Fortran code can help with debugging issues by providing a structured and efficient way to write and test code. The language has built-in features for error-checking and debugging, making it easier to identify and fix issues in the code. Additionally, Fortran code often performs faster than other programming languages, allowing for quicker identification and resolution of problems.

3. Is Fortran code difficult to learn for mechanical engineers?

The difficulty of learning Fortran code may vary depending on an individual's prior programming experience and familiarity with mathematics and scientific concepts. However, Fortran has a simple syntax and is designed to be easily readable and understandable for engineers. There are also many resources available, such as tutorials and online communities, to support learning and troubleshooting.

4. Can Fortran code be used for both simple and complex mechanical engineering problems?

Yes, Fortran code can be used for a wide range of mechanical engineering problems, from simple calculations to complex simulations and analyses. Its efficient and powerful capabilities make it a popular choice for solving a variety of engineering problems.

5. Are there any disadvantages to using Fortran code for mechanical engineering?

One potential disadvantage of using Fortran code for mechanical engineering is that it is a legacy language, meaning it has been around for a long time and may not have all the modern features and capabilities of newer programming languages. Additionally, some engineers may find the syntax and structure of Fortran code to be less intuitive compared to other languages. However, the benefits of using Fortran, such as its speed and reliability, often outweigh these potential disadvantages.

Similar threads

  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
9
Views
15K
Back
Top