Fortran Fortran Forum for Programming Help: Resources, Tips, and Support

  • Thread starter Thread starter FUNKER
  • Start date Start date
  • Tags Tags
    Fortran Forum
Click For Summary
The discussion revolves around programming in Fortran, specifically addressing issues related to Fortran 77 and 90/95. A user seeking help with array declarations and common blocks encounters multiple errors in their code. Key points include the importance of correctly declaring arrays and understanding the limitations of common blocks in Fortran. It is emphasized that array dimensions must be defined using integer values, and the use of floating-point numbers for array sizes is incorrect. The conversation also touches on the need for proper syntax, such as starting code lines in the correct column to avoid compilation errors. Users are advised to break down their programming tasks into manageable steps, utilize pseudocode for clarity, and compile code frequently to identify errors early. Additionally, there are discussions about the inability to dynamically allocate memory in Fortran 77, contrasting it with more modern versions that allow for such flexibility. The thread concludes with inquiries about breaking loops in subroutines and plotting functions, indicating ongoing challenges faced by users in mastering Fortran programming.
  • #31
Urgent question::can I break a loop in subroutine by return statement, because when some condition is true I need to break the loop and go back to the point in the main program where I have called the subroutine
Here is an example, the output of the program is 5.0000, which correspond to what you'd like to do, I think.
Code:
      FUNCTION F1(n)
      INTEGER n
      DO 10 I=1,n
      IF (I.GE.5) THEN
        F1=I
        RETURN
      ENDIF
10    CONTINUE      
      F1=I
      RETURN
      END
	  PRINT *, F1(10)
      STOP
      END
 
Technology news on Phys.org
  • #32
oh I have got any help on the common block(please have a look at the thread above)
 
  • #33
how can I creat an input file to save my data in F77, and how can I plot a function in F77
 
  • #34
oh I have got any help on the common block(please have a look at the thread above)
Perhaps it would be something trivial and not related to size.
Could you post the code, or part of it, and the related message?
 
  • #35
mathmate said:
Perhaps it would be something trivial and not related to size.
Could you post the code, or part of it, and the related message?
Code:
c 
        implicit none
	integer		i,n
c *      PARAMETER
        parameter       (n=5)
        real*8          sum1,sum2
	real*8		area
c        common Block
        real*8        x(n+1), y(n+1)
        common/alpha/ x(n+1), y(n+1)
        write(*,*) 'Enter the arrays elements'
        do 10 i=1, n
           read(*,*) x(i), y(i)
 10     continue
        call calarea(sum1)
        call calarea(sum2)
        area=(sum1-sum2)/2.0
end 
c
         subroutine calarea(sum)
         implicit none
	 integer	i,n
c *      PARAMETER
         parameter (n=5)
	 real*8 	sum
c        common Block
        real*8        x(n+1), y(n+1)
        common/alpha/ x(n+1), y(n+1)
         sum=0.0
         do  i=1,n-1
           sum=sum+(x(i)*y(i+1))
         end do
         sum=sum+(x(n)*y(1))
         end
and this is error message
A.f: In program `MAIN__':
A.f:14:
real*8 x(n+1), y(n+1)
1
A.f:15: (continued):
common/alpha/ x(n+1), y(n+1)
2
Invalid declaration of or reference to symbol `x' at (2) [initially seen at (1)]
A.f:14:
real*8 x(n+1), y(n+1)
1
A.f:15: (continued):
common/alpha/ x(n+1), y(n+1)
2
Invalid declaration of or reference to symbol `y' at (2) [initially seen at (1)]
A.f: In subroutine `calarea':
A.f:53:
real*8 x(n+1), y(n+1)
1
A.f:54: (continued):
common/alpha/ x(n+1), y(n+1)
2
Invalid declaration of or reference to symbol `x' at (2) [initially seen at (1)]
A.f:53:
real*8 x(n+1), y(n+1)
1
A.f:54: (continued):
common/alpha/ x(n+1), y(n+1)
2
Invalid declaration of or reference to symbol `y' at (2) [initially seen at (1)]
A.f: Outside of any program unit:
In file included from A.f:0:
A.f:15: error: size of variable 'alpha_' is too large
This is a sample, I know I can do it without subroutine and without common blocks, but later in my code I need to include coomon blocks as my data increas and I will add some calculation in my subroutine, the main point I need to know what is wrong with the common blocks, any help would be much appreciated:smile::cry:
 
  • #36
Code:
        real*8        x(n+1), y(n+1)
        common/alpha/ x(n+1), y(n+1)

I would change it to:
Code:
        real*8        x(n+1), y(n+1)
        common/alpha/ x, y
because what we declare in REAL*8 cannot be redimensioned in COMMON.

If the implicit for variables starting with X and Y is set to REAL*8, then you don't need the REAL*8 line, just the COMMON line as you had written.

Hope that solves your problem with the COMMON block.
 
  • #37
REALLY APPRECIATE YOUR HELP, here I need to plot a matrix with complex elements, SO i TRY THE FOLLOWING CODE but it gives me error
Code:
print 8,X, Y, Z,A(i,j),A(j,i) 
8             FORMAT(3F8.5,1X, 2'(',F6.4,',',F6.4,')')

here the error message
PHP:
A.f:131:
   8             FORMAT(3F8.5,1X, 2'(',F6.4,',',F6.4,')')
                                  ^
Spurious number in FORMAT statement at (^)
also I tried
Code:
FORMAT(3F8.5,1X, '(',F6.4,',',F6.4,')',1X, '(',F6.4,',',F6.4,')')
but it gives me the error
PHP:
A.f:131:
   8             FORMAT(3F8.5,1X,'(',F6.4,',',F6.4,')',1X,'(',F6.4,',',F6.4
                                                                           ^
Missing close-parenthese(s) in FORMAT statement at (^)

also how can I plot a function, can anyone give me a useful link for that or example??/
 
  • #38
FORMAT(3F8.5,1X, 2'(',F6.4,',',F6.4,')')
Spurious number in FORMAT statement at (^)

The apostrophe field is a nonrepeatable edit descriptor, so it is not possible to multiply by putting an integer constant before it like the numeric ones. In this case, it is simpler to write '((' than to write 2'('.


Missing close-parenthese(s) in FORMAT statement at (^)
Here you have 65 characters from the first to the last non-blank characters. If the line started in column 7 as is normally done, there should be no problem. If the line finishes in column 73 (as I believe is the case), the compiler will not <see> the closing parenthesis.
 
  • #39
PLOTTING
About plotting in Fortran, it is an issue dependent on the compiler and the libraries available. The only plotting I have done using Fortran is plotting on a drum plotter using Calcomp subroutines. It is also possible to do print-plots by neatly arranging characters printed in the courrier font.
Apparently if you are using Fortran on a unix system, the gnu-plot libraries are available to make nice plots on the screen. If not, you can always format and export the data to a data file and use the good old Microsoft graphing packages like excel or other ones.

Hope someone else familiar with this subject can give you a suggestion.
 
  • #40
mathmate said:
PLOTTING

Apparently if you are using Fortran on a unix system, the gnu-plot libraries are available to make nice plots on the screen.

yes, that is what I have. :shy:sorry if my question seem to be silly, cos no idea about plotting in FORTRAN as I am still beginner, do I need to write a code for that? and how can I do a data file or input file cos each time I enter them manually...:smile:
 
  • #41
mathmate said:
The apostrophe field is a nonrepeatable edit descriptor, so it is not possible to multiply by putting an integer constant before it like the numeric ones. In this case, it is simpler to write '((' than to write 2'('.
i HAVE TRIED SOMETHING DIFFERENT with write staement
Code:
           write(*,'(F6.3,1X,F6.3,1X,F6.3)')
        &  x,y,r
           write(10,'(F6.3,1X,F6.3,1X,F6.3)')
        &  x,y,r
but it gives me
A.f:130:
& x,y,r
^
Statement at (^) begins with invalid token [info -f g77 M LEX]
 
  • #42
Code:
           write(*,'(F6.3,1X,F6.3,1X,F6.3)')
        &  x,y,r
           write(10,'(F6.3,1X,F6.3,1X,F6.3)')
        &  x,y,r

I believe the only problem is the continuation line & started past column 6.
According to the usual fortran convention, columns 1 to 5 are for labels, 6 for continuation lines, 7-72 for statements, and 73-80 for comments. All you need to do is to remove three characters before the & to shift it to column 6.
You also need to define 10 as a disk file, which you probably did elsewhere in the code.

You can also optionally simplify the format by merging the 1X with the following floating point parameter to give
Code:
write(10,'(F6.3,F7.3,F7.3)')
which also makes it easier to count the columns and less chance of number overflow.
 
  • #43
please any help on thread 41, would be much appreciated I am really get stuck in it:cry:
 
  • #44
Did you try what was suggested in thread #42? If it did not work, what message was displayed?
 
  • #45
mathmate said:
Did you try what was suggested in thread #42? If it did not work, what message was displayed?

ohsorry I mean thread 39 on plotting, cos I do not want to plot output, cos I n this case I would use excel,or ...,but I need to plot a function in some variable, id anyone could show me how to plot sin(x), as an example, should be some subroutine but I do not know how to implement them?
 
  • #46
I had created an data file with 2 column and 8 rows as m=8, but I could not see my mistake ?the strange thing it does work fine for the first time, then when I compile the file again it gives me the error message below, oh when I checked appears the data in the input file has been changed after executing the file, how can I stop this thing
Code:
open(unit=10, file='M.dat', status='old')
do  i=1, m
           read(10,*) x(i), y(i)
end do
but it gives me this error message
PHP:
read start: end of file
apparent state: unit 10 named M.dat
last format: list io
lately reading direct formatted external IO
Aborted
 
Last edited:
  • #47
Fortran thinks of a file as a tape.
If you watched your tape once, and want to watch again, what do you do?>>>> rewind it.
This is precisely what you do in Fortran.
Rewind 10
before you start reading from the beginning.

Here is an example reading a file with two lines, 10 and 20, twice.
Code:
      Real*8 x,y
      open(10, file='x.dat',status='old')
      read(10,*)x
      read(10,*)y
      print *,x,y
      rewind 10
      read(10,*)x
      read(10,*)y
      print *,x,y
      stop
      end
 
  • #48
I am trying to plot some data, below the code and the errors
Code:
gnuplot> set xrange[1:120]
gnuplot> set yrange[1:120]
gnuplot> set title ''Table''
undefined variable: Table
gnuplot> p 'x^2'
         non-integer passed to boolean operator
gnuplot> plot 'x'
              ^
         can't read data file "x"
         util.c: No such file or directory
Do I need to open a data file to plot each time?/
 
  • #49
gnuplot> set xrange[1:120]
gnuplot> set yrange[1:120]
gnuplot> set title ''Table''
undefined variable: Table
gnuplot> p 'x^2'
non-integer passed to boolean operator
gnuplot> plot 'x'
^
can't read data file "x"
util.c: No such file or directory
Note: the double quote is a single character, not two apostrophes. To be corrected around Table,
Try:
Code:
set xrange[0:11]
set yrange[1:130]
set title "Table"
f(x)=x^2
plot f(x)

For more inforation, check the link:
http://www.gnuplot.info/docs/node100.html
 
Last edited by a moderator:
  • #50
just I need to check if that is correct or no?to plot a complex matrix, can I create a data file of 2 columns the first the real part and the second is the imaginary part, then plot the file? Another thing if I need to save all the setting for the plot in a file,then use l0oad command, what extension of such file should be??
 
Last edited:
  • #51
From what I can see, the graph for a complex number is nothing more than using the x-axis for the real axis, and y-axis for the imaginary. So it should be OK.
As for the format for data input, I am not experienced with it.
Hope someone who has used GNUPLOT can help you out.
 
  • #52
hi guyes

please can anyone help me how to plot a data saved in text file as 2 column and 10 rows,THE FIRST COLUMN RESENT r and the second column represent h, let us think about it as a matrix M(10,2) , but what I need is in the figure the x-axis represent r and the y-axis represent h i,e so I will need to plot the data as a points.
After a hard digging in many manual on GNUPLOT i found this command which plot a data file, which as I understand take the data as a points(r,h)
gnuplot> plot 's.dat' every 2
but it does not give me what I should get, cos I had tested for small data.
should be something silly lieing around, buy where it is?
 
Last edited:
  • #53
mathmate said:
Code:
      Real*8 x,y
      open(10, file='x.dat',status='old')
      read(10,*)x
      read(10,*)y
      print *,x,y
      rewind 10
      read(10,*)x
      read(10,*)y
      print *,x,y
      stop
      end
Hi Mathmate, really appreciate your time, to open an output data to save my outputs, I did
Code:
open(unit=5,file='c.dat', status='new')
write(5,*), x,y
close(unit=5)
I have tried a stop as well instead of close, but give me the same error
I t does work fine for the first time and create a c.dat file correctly, but when I compile the file again it stops when it reach the open statement and the erroe message says
PHP:
open: 'new' file exists
apparent state: unit 5 named c.dat
last format: list io
lately writing direct formatted external IO
Aborted
 
  • #54
Code:
open(unit=5,file='c.dat', status='new')
write(5,*) x,y
close(unit=5)

In the above example, the new is a parameter that indicates the status of the file. The values allowed are new, old, unknown or scratch.
If you don't want to be bothered to change from new to old after a first run, you can put the status as 'unknown', and all will be well, as follows:

Code:
      REAL x,y
      x=0
      y=0
      open(unit=5,file='test.dat', status='unknown')
      write(5,*) x,y
      close(unit=5)
 
  • #55
I am using a code calling a numerical recipes, and I need to change this recpe for complex argument, But the problem in the code there is a sign(x,y) , which take the sign of the 2nd argument and put it in front of the first argument . the problem is that my argument x, y are complex nor real.

so can anyone please tell me how find something equivalent to sign function?
 
Last edited:
  • #56
I suggest you check the manual or do a test to see if a CSIGN() function already exists. If not, you can try the following.
Here's a home-made CSIGN function that you can use.
Notice that you have to declare the type of CSIGN as complex in the calling program or else it will be taken as of type REAL.
Code:
      COMPLEX A,B
      COMPLEX CSIGN
      A=(11,-12)
      B=(-16,19)
      print *,A,B, CSIGN(A,B)
      STOP
      END
      COMPLEX FUNCTION CSIGN(A,B)
      COMPLEX A,B
      CSIGN=CMPLX(SIGN(REAL(A),REAL(B)),SIGN(AIMAG(A),AIMAG(B)))
      END

The output is:

Code:
 (11.0000,-12.0000) (-16.0000,19.0000) (-11.0000,12.0000)
 
  • #57
Is there anything equivalent to if (z.lt.zero) for complex numbers, as the rational operator .lt. not define for complex arguments.

I've done this
PHP:
if((DREAL(z).lt.zero).or.(DIMAG(z).lt.zero))then
wondering if it is really equivalent
 
Last edited:
  • #58
Code:
implicit none
	integer		i,j,n,m,mp,np
c *      PARAMETER
        parameter    (m=3,n=3,mp = 10,np = 10)     
        REAL*8      x(m,n),v(n,n),w(n)
c
c
        do  i=1, m
           do  j=1, n
               read(*,*) x(i,j)
           end do
       end do 
       call svdcmp(x,m,n,mp,np,w,v)
       do  i=1, n
           print*, w(i)
           print*, v(i,i)
       end do 
       end
c
c-----------------------------------
        include 'SVDCMP.F'
the Num. recipe (SVDCMP.F) can be found from the web, when I executed the file for the first time it gave me a Segmentation fault , then it does not give me any more error messages, but it seem to be giving me wrong results by default,(4.45618367E-270
2.12199579E-314
3.08265917E-306
4.85242026E-270)


anyone can see the mistake?/
 
  • #59
A segmentation fault usually means (in an unfriendly way) the program has accessed a part of the memory that it does not have control of.

The code that I found from
http://sd-www.jhuapl.edu/IMP/data/spec_req/NR/code/SVDCMP.F
is
Code:
SUBROUTINE svdcmp(a,m,n,mp,np,w,v)
...
REAL a(mp,np),v(np,np),w(np)
]
It expects the dimension of a (the first argument) to be mp and np (4th and 5th argument).

which when compared to the subroutine call you posted:

parameter (m=3,n=3,mp = 10,np = 10)
REAL*8 x(m,n),v(n,n),w(n)
...
call svdcmp(x,m,n,mp,np,w,v)
the first argument x has been dimensioned x(3,3), while the subroutine expects it to be x(10,10). So it would be likely that the programme will try to access memory locations outside of the dimensioned x(3,3), probably explaining the segmentation fault.

Try it with the corrected parameter values and post what you see!
 
  • #60
Is there anything equivalent to if (z.lt.zero) for complex numbers, as the rational operator .lt. not define for complex arguments.
A complex number has two axes, the real and the imaginary axes.
What you do is OK if you'd like to test the value of either axis is less than zero.
 

Similar threads

  • · Replies 17 ·
Replies
17
Views
4K
  • · Replies 7 ·
Replies
7
Views
6K
  • · Replies 16 ·
Replies
16
Views
5K
  • · Replies 13 ·
Replies
13
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
Replies
6
Views
4K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 4 ·
Replies
4
Views
38K
  • · Replies 11 ·
Replies
11
Views
6K