How to fix errors in my Fortran code - fortran

I am not familiar with errors in Fortran. Could you please explain what are these errors?
int.f90(18): error #5082: Syntax error, found IDENTIFIER 'K' when expecting one of: ( % : . = =>
do k=0.0 to m
---^
int.f90(19): error #5082: Syntax error, found IDENTIFIER 'Q' when expecting one of: ( % : . = =>
do q=1 to m-1
----^
int.f90(20): error #5082: Syntax error, found END-OF-STATEMENT when expecting one of: ) ,
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
----------------------------------------------^
int.f90(24): error #5082: Syntax error, found IDENTIFIER 'I' when expecting one of: ( % : . = =>
do i=1 to m-1
----^
int.f90(4): error #6406: Conflicting attributes or multiple declaration of name. [INT]
function int(f,a,b,int,m)
-------------------^
int.f90(9): error #6418: This name has already been assigned a data type. [M]
integer:: i,m
------------^
int.f90(10): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [PI]
real,parameter:: pi,pi=3.14
-----------------^
int.f90(10): error #6418: This name has already been assigned a data type. [PI]
real,parameter:: pi,pi=3.14
--------------------^
int.f90(11): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [EPS]
real,parameter:: eps,eps=1.89
-----------------^
int.f90(11): error #6418: This name has already been assigned a data type. [EPS]
real,parameter:: eps,eps=1.89
---------------------^
int.f90(12): error #6557: An =initialization-expr is missing; an initialization expression is required when using the PARAMETER attribute. [E]
real,parameter:: e,e=1.602*((10)**(-19))
-----------------^
int.f90(12): error #6418: This name has already been assigned a data type. [E]
real,parameter:: e,e=1.602*((10)**(-19))
-------------------^
int.f90(21): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
int.f90(23): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
int.f90(26): error #6410: This name has not been declared as an array or a function. [F2]
s=2*f2(t)+4*f2(t+h)
-----^
int.f90(27): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
end do
-^
and this is my code:
function int(f,a,b,int,m)
implicite none
double precision f1,f2,a,b,m,int,s
double precision h,t
integer:: k,q
integer:: i,m
real,parameter:: pi,pi=3.14
real,parameter:: eps,eps=1.89
real,parameter:: e,e=1.602*((10)**(-19))
a=0.0
b=pi
m=150
s=0.0
h=(b-a)/m
do k=0.0 to m
do q=1 to m-1
f1=1/(2*pi)*(sqrt((k**2)+(Q**2)-(2*k*cos(t)))
end do
f2=((2*pi*(e**2))/eps)*f
end do
do i=1 to m-1
t=a+(i*h)
s=2*f2(t)+4*f2(t+h)
end do
int=(h/3)*(s+f2(a)+f2(b)+4*f2(a+h))
print*,int
return
end function int

Your errors are:
Your loop-control in every one of your do loops is malformed. (Fortran 2008 Cl. 8.1.6.2 R818)
DO loops are specified as (neglecting optional syntax):
do variable=start, end
Note the comma rather than the word to.
Parenthesis must be balanced. (See Fortran 2008 7.1.2.2 R701 ( expr ))
Every left parenthesis ( must have a corresponding right parenthesis ). Your statements assigning f1 and f2 both have more left parenthesis than right parenthesis.
Declaring a variable twice in the same scoping unit is wrong. (Fortran 2008 Cl. 16.3.1 paragraph 3, see also: Cl. 5.2)
To delcare a variable and initialize it, you need only use its name once
real, parameter :: pi=3.14
You call the function int and a dummy variable int. You need to use different names for these.
You declare m as both integer and double precision. You can only declare it as one type, not two.
Of note:
Fix errors starting with the first ones, as later ones can be caused by the compiler throwing out earlier bad statements and will go away when those are fixed.
Real values in do loop variables are deleted in the latest standard, and should be avoided in new code. (Fortran 2008 Annex B.1 2(1)).
Your code demonstrates a lack of understanding in Fortran and you will benefit greatly from finding a Fortran tutorial that covers the basics of variables and loops.

Related

Using an optional parameter with value attribute when it has not been passed [duplicate]

This question already has answers here:
Segmentation fault with optional arguments in Fortran functions
(1 answer)
Fortran 2003 / 2008: Elegant default arguments?
(7 answers)
Closed 4 years ago.
I want to use a function's parameter with attribute "value". It is an optional parameter and I want to use it when it has not been passed. I expect that there is a local variable associated with the optional parameter, because the optional paramter is passed by value and not by reference. But strange things occurr when I try to do it.
In the example below, the string "Hello" should be printed, but the contents of the character parameter "Str" seems to be empty.
module test_value_mod
implicit none
contains
subroutine printStr(Str, AddTip)
! Declaration of parameters
character(len=*), intent(in) :: Str
logical, optional, value :: AddTip
if (.not. present(AddTip)) AddTip = .False.
write (*,*) 'Str: ', Str
if (AddTip) write (*,*) 'Tip is printed.'
end subroutine printStr
end module test_value_mod
program test_value_prog
use test_value_mod
implicit none
call printStr('Hello')
end program test_value_prog
However, when I pass the charaacter length as another parameter, there is no problem:
module test_value_mod
implicit none
contains
subroutine printStr(Str, LenStr, AddTip)
! Declaration of parameters
integer, intent(in) :: LenStr
character(len=LenStr), intent(in) :: Str
logical, optional, value :: AddTip
if (.not. present(AddTip)) AddTip = .False.
write (*,*) 'Str: ', Str
if (AddTip) write (*,*) 'Tip is printed.'
end subroutine printStr
end module test_value_mod
program test_value_prog
use test_value_mod
implicit none
call printStr('Hello', len_trim('Hello'))
end program test_value_prog
I use gfortran 7.3.0. But if I compile with ifort 17.0.4, I get the following error for the two examples given above:
forrtl: severe (174): SIGSEGV, segmentation fault occurred
Image PC Routine Line Source
test_value_passin 0000000000402C64 Unknown Unknown Unknown
libpthread-2.17.s 00007EFDA90825E0 Unknown Unknown Unknown
test_value_passin 000000000040288D test_value_mod_mp 15 test_value_passing.f08
test_value_passin 0000000000402A08 MAIN__ 29 test_value_passing.f08
test_value_passin 00000000004027CE Unknown Unknown Unknown
libc-2.17.so 00007EFDA8CD1C05 __libc_start_main Unknown Unknown
test_value_passin 00000000004026E9 Unknown Unknown Unknown
Line 15 in "test_value_mod_mp" is:
if (.not. present(AddTip)) AddTip = .False.
So, I think that the use of an optional paramter pased by value when it has not been actually provided, is invalid. But, however, gcc permits it, failing only if you provide another input paramter of type character of assumed length.
Could anybody clarify?

Invalid or Missing Repeat Count in Fortran

I am writing a Fortran program to read data from 35 files with similar size(8 columns and 8784 lines in each file) and write to a single file. I wrote the following code and when compiling "Invalid or missing repeat count" error is appearing.
Program Basic
implicit none
Character*20,dimension(35)::filename(/'1979.txt','1980.txt','1981.txt','1982.txt','1983.txt','1984.txt','1985.txt','1986.txt','1987.txt','1988.txt','1989.txt','1990.txt','1991.txt','1992.txt','1993.txt','1994.txt','1995.txt','1996.txt','1997.txt','1998.txt','1999.txt','2000.txt','2001.txt','2002.txt','2003.txt','2004.txt','2005.txt','2006.txt','2007.txt','2008.txt','2009.txt','2010.txt','2011.txt','2012.txt','2013.txt'/)
real,dimension(1:8784)::Db,Dp,WS
integer,dimension(1:8784)::a,b,c,d,SR
integer::i,file
do file=1,35
open(7000,file=filename(file))
open(7001,file='Final.txt')
do i=1,8784
read(7000,*)a(i),b(i),c(i),d(i),Db(i),Dp(i),WS(i),SR(i)
write(7001,*)a(i),b(i),c(i),d(i),Db(i),Dp(i),WS(i),SR(i)
end do
end do
end Program Basic
The output from the compiler (Silverfrost FTN95) is
Runtime error from program:c:\users\aadhikari2\desktop\trial 1\freeformat1.exe
Run-time Error *** Error 62, Invalid or missing repeat count BASIC - in file freeformat1.f95 at line 19 [+0325]
The declaration line
Character*20,dimension(35)::filename(...
is too long for free form Fortran (see Line truncated, Syntax error in argument list ). You must split the line.
But also the array declaration is not syntactically correct. You should use the = assignment as in
Character*20,dimension(35) :: filename = (/'1979.txt','1980.txt','1981.txt', &
'1982.txt','1983.txt','1984.txt', &
'1985.txt','1986.txt','1987.txt', &
'1988.txt','1989.txt','1990.txt', &
'1991.txt','1992.txt','1993.txt', &
'1994.txt','1995.txt','1996.txt', &
'1997.txt','1998.txt','1999.txt', &
'2000.txt','2001.txt','2002.txt', &
'2003.txt','2004.txt','2005.txt', &
'2006.txt','2007.txt','2008.txt', &
'2009.txt','2010.txt','2011.txt', &
'2012.txt','2013.txt'/)
Also, don't forget to close your files at the end.

What is the "select when" syntax for?

Experimenting with the language I've found that select is defined in the global scope and its precedence is higher than local variables.
def example(select)
puts select
end
example 3
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
So experimenting with select step by step I get this:
select 1 end
# Syntax error in eval:3: unexpected token: end (expecting when, else or end)
and then
select when 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x = 1 end
# Syntax error in eval:1: invalid select when expression: must be an assignment or call
then
select when x
# Syntax error in eval:1: unexpected token: EOF (expecting ',', ';' or '
I'll skip ahead a few steps as you should have an idea of how I've come to my question…
select when x;
else y
end
# Error in line 1: undefined local variable or method 'x_select_action'
and lastly
x_select_action = 4
select when x;
else y
end
# Error in line 3: undefined method 'x_select_action' (If you declared 'x_select_action' in a suffix if, declare it in a regular if for this to work. If the variable was declared in a macro it's not visible outside it)
So there is this keyword in the language which precedes local variables precedence and I don't know what it's for. But apparently it looks for x_select_action when x is given as a when clause. What is this select for and how is it meant to be used?
Searching online I see select defined on Enumerable, Hash, Channel, and Array… but at first glance these don't seem to be it.
Thanks for the help!
It's similar to Go's select: https://tour.golang.org/concurrency/5
But it still needs some tweaks to be finished, that's why there are no docs about it yet.

Syntax error in function arguments in fortran

I have a sub routine file as follows
subroutine grids(Ngrids,gridsize,boundx,boundy,boundz,occmatrix,myid)
implicit NONE
integer i,j,k,Ngrids, occmatrix(14,14,10)
integer locx,locy,locz,myid
double precision gridsize,boundx,boundy,boundz
do i = 1, 14
do j = 1, 14
do k = 1, 10
occmatrix(i,j,k) = 0
enddo
enddo
enddo
open (13, file = 'grid_data.9deg')
write(*,'(A,i2)'),' READING GRID FILE ON PROC.....',myid
read(13,*) Ngrids,gridsize
read(13,*) boundx,boundy,boundz
do i = 1, Ngrids
read(13,*) locx, locy, locz
occmatrix(locx,locy,locz) = 1
enddo
close(13)
return
end
It gives the following syntax error in compiling
subroutine grids(Ngrids,gridsize,boundx,boundy,boundz,occmatrix,my
1
Error: Unexpected junk in formal argument list at (1)
It used to run well before
I would believe, your line is to long. Did you add a new argument? Your code looks like free form, but it might be the compiler tried to apply fixed form due to a .f suffix in the filename or something like that. Convince the compiler to assume free formatted source code (by compiler options or usually a .f90 suffix).
Even in free formatted files your line width is limited and you should break longer lines, which would for example look like:
subroutine grids( Ngrids,gridsize,boundx,boundy,boundz, &
& occmatrix,myid )
If you are stuck with fixed format you need to indicate a continuation line by a non blank character in column 6.
Here is how it looks like in fixed form:
subroutine grids(Ngrids,gridsize,boundx,boundy,boundz,
& occmatrix,myid)
Please do not use fixed form anymore! Instead, change your files to end with .f90, most compilers recognize this for free formatted code.

Simple SML code error

I have just started learning SML and still in the process of making sense of its error messages.
when trying to input the function definition below
val rec : real->real = fn 0.0 => 0.0 | n:real => 1.0/n;
i get the following error :
stdIn:25.9-25.17 Error: syntax error: deleting COLON ID ARROW
stdIn:25.24-25.33 Error: syntax error: deleting FN REAL DARROW
stdIn:25.38 Error: syntax error found at BAR
can someone point out what i am doing wrong ?
thank you.
You have two errors in your code:
Between val rec and the type annotation there should be the name of the value you're defining.
You can't use pattern matching on reals. Since reals are inexact, they aren't equality types, so you can't use = on them either. You need to use Real.== to compare reals for equality (or better: don't compare them for equality, but compare them against some delta instead).