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

In summary, this conversation is about a closed thread on Fortran programming and the suggestion to visit a different forum for help. A new user also asks for help with Fortran 77 and is given a definition of a common block and how to use it. An example of a code using a common block is provided, but the user encounters errors when compiling.
  • #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.
 
Technology news on Phys.org
  • #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?

oh:yuck:sorry 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.
 
  • #61
mathmate said:
Try it with the corrected parameter values and post what you see!
I TRIED THIS CODE
PHP:
mplicit none
	integer		i,j,n,m,mp,np
        parameter    (mp = 450,np = 450)     
        REAL*8       x(mp,np),v(np,np),w(np)
c        
        write(*,*) 'Enter the arrays elements'
        read(*,*) m,n
        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
c-----------------------------------
        include 'SVDCMP.F'[PHP]

actually I have tried already and I've got this error
[PHP]PAUSE  no convergence in svdcmp statement executed
To resume execution, type go.  Other input will terminate the job.
 
Last edited:
  • #62
PHP:
URGENT
anyone can tell me how to include NAG FORTRAN Library Routinein my code?I mean for example for the numerical recipes we just do include 'file name.f' at the end of the main program, but for NAG ROUTINES I know I need to amend the parameters of the subroutine according to my needs, but how to put them in the main program??/
 
  • #63
anyone can tell me how to include NAG FORTRAN Library Routinein my code?

NAG comes with installation manuals, depending on the version (MARK 19,20 or 21) and the computer (CRAY, Linux, DEC/VMS, Sun/Sparc, Siligon Graphics, etc.)
The installation instructions can be found here:
http://www.nag.co.uk/numeric/FL/FLinuns.asp

The routines are generally compiled for use on the respective machines in object/binary form. So if it is properly installed, you just have to make the calls with the right parameters as shown in the users manual, in much the same way you'd make the calls to sin and cos. You would not be tagging a source code at the end of your program nless you paid for the source code although I don't see that option on the price list. In any case, many of these routines have dependencies and many of them could have been written in assembler or C.

Which fortran are you running, fortran 77 or fortran 90? I don't see F77 on the NAG product list, but it doesn't mean that it is not available.
 
  • #64
PAUSE no convergence in svdcmp statement executed
To resume execution, type go. Other input will terminate the job.

Usually this means a logical error, meaning that
1. The input data is incorrect
2. The input data is correct, but the solution does not converge.
3. There is a programming error, or the inappropriate routine has been called.

It would be difficult for a third party to figure out which problem you have without a knowledge of the problem you are trying to solve, a proper understanding of your data, and a verification of the program that makes the call.

We can give it a try if you provide more information, perhaps someone else can help too.
 
  • #65
mathmate said:
We can give it a try if you provide more information, perhaps someone else can help too.
I ma trying to calculate the singular value decomposition of a given matrix, that is what I NEED TO DO, so any help to figure out the mistake in the programme in thread 61
 
Last edited:
  • #66
Do you use a Cray? Sometimes when the computer's registers have more precision than the variable, the comparison of REAL values could be thrown off. One suggested way to get around the problem is to store an intermediate value as a temporary variable before the comparison, thus forcing a rounding to the normal precision.

This rounding problem has been known to cause convergence problems in the SVDCMP code. You could try replacing lines 138 and 139 by
Code:
             TMP=(abs(rv1(l))+anorm)
             if(TMP.eq.anorm)  goto 2
C            if((abs(rv1(l))+anorm).eq.anorm)  goto 2
             TMP=(abs(w(nm))+anorm)
             if(TMP.eq.anorm)  goto 1
C            if((abs(w(nm))+anorm).eq.anorm)  goto 1
and somewhere at the beginning you need to declare TMP as REAL or REAL*8 accordingly.

How big is the matrix? Can you dump the matrix and run it with the c version of svdcmp, just as another possibility?
 
  • #67
mathmate said:
Do you use a Cray? Sometimes when the computer's registers have more precision than the variable, the comparison of REAL values could be thrown off. One suggested way to get around the problem is to store an intermediate value as a temporary variable before the comparison, thus forcing a rounding to the normal precision.

This rounding problem has been known to cause convergence problems in the SVDCMP code. You could try replacing lines 138 and 139 by
Code:
             TMP=(abs(rv1(l))+anorm)
             if(TMP.eq.anorm)  goto 2
C            if((abs(rv1(l))+anorm).eq.anorm)  goto 2
             TMP=(abs(w(nm))+anorm)
             if(TMP.eq.anorm)  goto 1
C            if((abs(w(nm))+anorm).eq.anorm)  goto 1
and somewhere at the beginning you need to declare TMP as REAL or REAL*8 accordingly.

How big is the matrix? Can you dump the matrix and run it with the c version of svdcmp, just as another possibility?
No I am using Linux CenTOS 5, I have give another try with lots of changes ,But IT STILL GIVING ME the same error message
PHP:
PAUSE  no convergence in svdcmp statement executed
To resume execution, type go.  Other input will terminate the job.
[PHP], I had entered a matrix which has a singular value decomposition
 
  • #68
Did you implement the seemingly trivial code change I suggested previously?

Do you have the line:
Code:
         IF (ITS.EQ.30) PAUSE 'No convergence in 30 iterations'
in you code?
Is ITS compared to 30 or is ITS compared to N?
If N exceeds 30, it is possible that the convergence message is false.
 
Last edited:
  • #69
2. is there any suggestion form you experienced guys about rewriting
f77 codes complying with f95 styles?
You can use Michael Metcalf's convert.f90 program at
http://www.slac.stanford.edu/comp/fortran/convert.f90 to convert fixed
to free source form. I think free source form is more readable and
less error−prone. Alan Miller's to_f90.f90 program
Anyone can tell me how to use this suggestion,i mean how to write the command for this job
 
Last edited by a moderator:
  • #70
I am trying to migrate my code from f77 to f95, so need to use the module instead of common blocks, but when I have looked at it, they define the global variable which is contained in the module in a separate file, which I guess does benefit me cos I need to transfer some variables and arrays which I have got them from my main program, then I need to use them in a subroutine, as I need to share these data with such routine

PHP:
need to create a module of variables resulted from my main program then transfer it to the subroutine
 

Similar threads

  • Programming and Computer Science
Replies
17
Views
4K
  • Programming and Computer Science
Replies
7
Views
6K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
16
Views
4K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
3K
  • Programming and Computer Science
Replies
14
Views
3K
  • Sticky
  • DIY Projects
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
11
Views
5K
Back
Top