Unclassifiable statement and other errors in an IF in Fortran - if-statement

I have the code:
if i < n then
x = topsep(1)
y = topsep(2)
realvor(n,1) = x + dx
realvor(n,2) = x + dy
imvor(n,1) = (realvor(n,1)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
imvor(n,2) = (realvor(n,2)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
tf = .TRUE.
else
x = botsep(1)
y = botsep(2)
realvor(n,1) = x + dx
realvor(n,2) = y - dy
imvor(n,1) = (realvor(n,1)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
imvor(n,2) = (realvor(n,2)*(a**2))/((realvor(n,1))**2+(realvor(n,2))**2)
tf = .FALSE.
endif
Both i and n are defined as integers and I am inside a do loop for n = 1,100. This throws up the following errors:
Error: Unclassifiable statement at (1) at the 'if i< n then'
Error: Unexpected ELSE statement at (1) at the 'else'
Error: Expecting END DO statement at (1) at the 'endif'
I can't see where these errors are coming from, no matter how I write the if statement (.NE. etc.) it seems to throw up the same things.

You forgot the parenthesis! According to the Fortran standard (2008, ch. 8.1.7.4), the if statement should read
if ( i < n ) then

Related

Increase Steps in 2D Self Avoiding Random Walk

I am trying to increase the number of possible steps in the following Fortran self avoiding random walk program.
Increasing the number of steps would result in more accuracy regarding the square mean distance
I would appropriate your solutions and suggestions.
PROGRAM Two_dimensional_Self_Avoiding__Random_Walks
implicit none
integer, dimension(:,:), allocatable :: lattice
integer, dimension(1:46):: na
integer :: i,x,y,xt,yt,id,step,xx, ns,n,ii,III,choice
real :: r,dis,dis2,square,d,d2
Logical :: terminate,newsite
CALL RANDOM_SEED()
! intial values for end to end distance
read(*,*) choice
if (choice == 1) then
print*, ' Enter ns and n '
read(*,*) ns
na = (/(III, III=5, 50, 1)/)
do ii= 1, 46
dis = 0.0; dis2 = 0.0
n = na(ii)
allocate(lattice(-n:n,-n:n))
CALL walks() ! self avoiding walks
IF (ALLOCATED (lattice)) DEALLOCATE (lattice)
enddo
elseif (choice == 2) then
print*, ' Enter ns and n '
read(*,*) ns , n
dis = 0.0; dis2 = 0.0
allocate(lattice(-n:n,-n:n))
CALL walks()
endif
CONTAINS
SUBROUTINE walks
DO i = 1,ns
lattice = 0; x = 0; y = 0
step = 0; terminate = .FALSE.
DO WHILE ((.NOT. terminate) .AND. (step <= n))
xt = x; yt = y
xx = lattice(x+1,y)+lattice(x-1,y) &
+lattice(x,y+1)+lattice(x,y-1)
IF (xx == 4) THEN
terminate = .TRUE.
ELSE
newsite = .FALSE.
DO WHILE (.NOT. newsite)
CALL RANDOM_NUMBER(r)
id = INT(r*4.0)
IF (id == 0) THEN
x = xt + 1; y = yt
ELSEIF (id == 1) THEN
x = xt - 1; y = yt
ELSEIF (id == 2) THEN
x = xt; y = yt + 1
ELSEIF (id == 3) THEN
x = xt; y = yt - 1
ENDIF
IF (lattice(x,y) == 0) newsite = .TRUE.
ENDDO
step = step + 1; lattice(x,y) = 1
ENDIF
write(7,*) x,y
ENDDO
write(10,*),step
square = float(x**2+y**2)
dis = dis + sqrt(square); dis2 = dis2 + square
d = dis/ns; d2=dis2/ns
ENDDO
write(11,*), ns,n, d, d2
print*, ns,n, d, d2
END SUBROUTINE walks
END PROGRAM Two_dimensional_Self_Avoiding__Random_Walks

How take more than one input and run the program for each one

The following is a program for self avoiding random walk. The program works fine but I need to make a minor modification but I do not know how.
Currently the program receives n and ns as inputs and then calculates a distance (dis). I want the program to receive more than one n and calculate the distance for each n.
Example of current output
n = 100 ns = 100 dis = 10.8
I want the program to output
n = 100 ns = 100 dis = 10.8
n = 200 ns = 100 dis = 11.6
and go on for all input vales of n.
This can be done by running the program every time with different n but I need to do it with one run.
PROGRAM Two_dimensional_Self_Avoiding__Random_Walks
implicit none
integer, dimension(:,:), allocatable :: lattice
integer :: i,x,y,xt,yt,id,step,xx, ns,n
real :: r,dis,dis2,square,d,d2
Logical :: terminate,newsite
print*, ' Enter ns and n '
read(*,*) ns,n
allocate(lattice(-n:n,-n:n))
CALL RANDOM_SEED()
dis = 0.0; dis2 = 0.0 ! intial values for end to end distance
CALL walks() ! self avoiding walks
dis = dis/float(ns); dis2 = dis2/float(ns)
print*,ns,n,dis,dis2
CONTAINS
SUBROUTINE walks
DO i = 1,ns
lattice = 0; x = 0; y = 0
step = 0; terminate = .FALSE.
!do ii = 1, n
DO WHILE ((.NOT. terminate) .AND. (step <= n))
xt = x; yt = y
xx = lattice(x+1,y)+lattice(x-1,y) &
+lattice(x,y+1)+lattice(x,y-1)
IF (xx == 4) THEN
terminate = .TRUE.
ELSE
newsite = .FALSE.
DO WHILE (.NOT. newsite)
CALL RANDOM_NUMBER(r)
id = INT(r*4.0)
IF (id == 0) THEN
x = xt + 1; y = yt
ELSEIF (id == 1) THEN
x = xt - 1; y = yt
ELSEIF (id == 2) THEN
x = xt; y = yt + 1
ELSEIF (id == 3) THEN
x = xt; y = yt - 1
ENDIF
IF (lattice(x,y) == 0) newsite = .TRUE.
ENDDO
step = step + 1; lattice(x,y) = 1
ENDIF
write(10,*),step
!print*, x,y
write(7,*) x,y
ENDDO
square = float(x**2+y**2)
dis = dis + sqrt(square); dis2 = dis2 + square
d = dis/ns; d2=dis2/ns
write(8,*) step, d, d2
!enddo
ENDDO
END SUBROUTINE walks
END PROGRAM Two_dimensional_Self_Avoiding__Random_Walks

Error: Syntax error in argument list at (1)

implicit none
character*20 fflname, oflname, oflname2
integer i, length, rn, s(100)
real*8 phase_shift
parameter ( length = 32768, phase_shift = 0.02 )
real*8 num, real_coeff, imag_coeff
real*8 amplitude(length), phase(length)
& ,imag_coeff_ps(length), real_coeff_ps(length)
oflname = "wvlt_coeff.data"
oflname2 = "selection.data"
fflname = "wvlt_coeff_ps.data"
open(12, file = oflname)
do i=1, length
read(12, *) num, real_coeff, imag_coeff
real_coeff_ps(i) = real_coeff
imag_coeff_ps(i) = imag_coeff
enddo
close(12)
open(13, file = oflname2)
do i=1, 100
read(13, *) rn
s(i) = rn
enddo
close(13)
do i=1, 100
amplitude(i) = sqrt( real_coeff(s(i))**2 + imag_coeff(s(i))**2 )
phase(i) = atan( imag_coeff(s(i))/real_coeff(s(i)) ) + phase_shift
real_coeff_ps(s(i)) = amplitude(i) * cos( phase(i) )
imag_coeff_ps(s(i)) = amplitude(i) * sin( phase(i) )
enddo
open(15, file = fflname)
do i=1, length
write(15, *) i, real_coeff_ps(i), imag_coeff_ps(i)
enddo
close(15)
stop
end
Errors:
hyxie#ubuntu:~$ gfortran '/home/hyxie/Documents/20161012/phase_shift2.f'
/home/hyxie/Documents/20161012/phase_shift2.f:35:40:
amplitude(i) = sqrt( real_coeff(s(i))**2 + imag_coeff(s(i))**2 )
1
Error: Syntax error in argument list at (1)
/home/hyxie/Documents/20161012/phase_shift2.f:36:36:
phase(i) = atan( imag_coeff(s(i))/real_coeff(s(i)) ) + phase_shift
1
Error: Syntax error in argument list at (1)
What is wrong with my coding?
real_coeff and image_coeff are not arrays, but you are accessing them as if they were. This results in a syntax error. Perhaps you intended to use real_coeff_ps and image_coeff_ps instead.

Unable to access all the related predicates in Prolog from C++

I am trying to access all the related predicates of a prolog file from C++.
I have the prolog file as,
"new_gryffindor.pl"
sits_right_of(parvati,lavender).
sits_right_of(lavender,neville).
sits_right_of(neville,alicia).
sits_right_of(alicia,fred).
sits_right_of(fred,george).
sits_right_of(george,lee).
sits_right_of(lee,dennis).
sits_right_of(dennis,dean).
sits_right_of(dean,ginny).
sits_right_of(ginny,angelina).
sits_right_of(angelina,seamus).
sits_right_of(seamus,colin).
sits_right_of(colin,harry).
sits_right_of(harry,hermoine).
sits_right_of(hermoine,ron).
sits_right_of(ron,natalie).
sits_right_of(natalie,katie).
sits_right_of(katie,parvati).
sits_left_of(X,Y) :- sits_right_of(Y,X).
are_neighbours_of(X,Y,Z) :- sits_left_of(X,Z),
sits_right_of(Y,Z).
next_to_each_other(X,Y) :- sits_left_of(X,Y);
sits_right_of(X,Y).
And I have integrated C++ and Prolog file with the C++ code,
term_t a = PL_new_term_ref();
term_t b = PL_new_term_ref();
term_t ans = PL_new_term_ref();
PL_put_variable(ans);
predicate_t p_consult = PL_predicate("consult", 1, "database");
term_t t = PL_new_term_ref();
PL_put_string_chars(t, "new_gryffindor.pl");
PL_call_predicate(NULL, 0, p_consult, t);
fun = PL_new_functor(PL_new_atom("sits_right_of"),2);
PL_cons_functor(ans, fun, a, b);
char *fact1;
char *fact2;
if(PL_call(ans, NULL)) {
PL_get_atom_chars(a, &fact1);
PL_get_atom_chars(b, &fact2);
cout << fact1 << " sits right of " << fact2;
}
This C++ Code is giving me the result for the very first predicate of "sits_right_of" ,i.e, as "parvati sits right of lavender". But I want to print all the related predicates of "sits_right_of" in C++ like the prolog which gives next similar predicate's values by using semicolon ";".
sits_right_of(X,Y).
X = parvati,
Y = lavender ;
X = lavender,
Y = neville ;
X = neville,
Y = alicia ;
X = alicia,
Y = fred ;
X = fred,
Y = george ;
X = george,
Y = lee ;
X = lee,
Y = dennis ;
X = dennis,
Y = dean ;
X = dean,
Y = ginny ;
X = ginny,
Y = angelina ;
X = angelina,
Y = seamus ;
X = seamus,
Y = colin ;
X = colin,
Y = harry ;
X = harry,
Y = hermoine ;
X = hermoine,
Y = ron ;
X = ron,
Y = natalie ;
X = natalie,
Y = katie ;
X = katie,
Y = parvati.
I tried looping statements in C++ but it is printing the same output for multiple times..
So please help me out to print the values of other similar predicates of prolog file in C++.

Fortran Error - attempt to call a routine with argument number three as a real(kind =1) when procedure was required

I have a fortran project linked to various subroutines, which are called from the main program. Variables are passed using modules. I can compile the code without any error. When I run the code, during the subroutine call i get an error "attempt to call a routine with argument number three as a real(kind =1) when procedure was required. I am not sure where i am going wrong. Can someone point out the error? Your help is very much appreciated. The error appears when the subroutine 'ncalc' is called inside the loop
program partbal
use const
use times
use density
use parameters
use rateconst
use ploss
implicit none
integer :: i
real :: nclp_init, ncl2p_init, ncln_init, ne_init
real :: ncl_init, ncl2_init, Te_init, neTe_init
open (10,file='in.dat')
read (10,*)
read (10,*) pressure
read (10,*)
read (10,*) P, pfreq, duty
read (10,*)
read (10,*) nclp_init, ncl2p_init, ncln_init, Te_init, Ti
pi = 3.14159265
R = 0.043
L = 0.1778
Al = 2*pi*R*R
Ar = 2*pi*R*L
V = pi*R*R*L
S = 0.066
e = 1.6e-19
me = 9.1e-31
mCl = 35.5/(6.023e26)
mCl2 = 2*mCl
k = 1.3806e-23
vi = (3*Ti*e/(53.25/6.023e26))**0.5
ncl2_init = pressure*0.1333/(1.3806e-23*298)/2
ncl_init = ncl2_init
ne_init = nclp_init + ncl2p_init - ncln_init
tot_time = 1/(pfreq*1000)
off_time = duty*tot_time
npoints = 10000
dt = tot_time/npoints
neTe_init = ne_init*Te_init
t_step = 0
call kcalc(Te_init)
call param(nclp_init, ncl2p_init, ncln_init, ne_init, ncl_init,ncl2_init, Te_init, Ti)
do i = 1, npoints, 1
t_step = i*dt + t_step
if (t_step > 0 .and. t_step <= 500) then
Pabs = 500
else if (t_step > 500) then
Pabs = 0
end if
if (i <= 1) then
call ncalc(ne_init, ncl_init, ncl2_init, nclp_init, ncln_init, ncl2p_init)
call powerloss(ne_init, ncl_init, ncl2_init, Pabs, neTe_init)
Te = neTe/ne
call kcalc(Te)
call param(nclp, ncl2p, ncln, ne, ncl, ncl2, Te, Ti)
else
call ncalc(ne, ncl, ncl2, nclp, ncln, ncl2p)
call powerloss(ne, ncl, ncl2, Pabs, neTe)
Te = neTe/ne
call kcalc(Te)
call param(nclp, ncl2p, ncln, ne, ncl, ncl2, Te, Ti)
end if
!open( 70, file = 'density.txt' )
!open( 80, file = 'Te.txt')
!do i = 1, 1001, 1
! np(i) = ncl2p(i) + nclp(i)
!write (70, *) ncl(i), ncl2(i), ncl2p(i), nclp(i), np(i), ncln(i), ne(i)
!close(70)
!write (80, *) Te(i), phi(i)
!close(80)
!end do
end do
end program partbal
subroutine ncalc(n_e, n_cl, n_cl2, n_clp, n_cln, n_cl2p)
use parameters
use const
use density
use rateconst
use times
implicit none
real :: n_e, n_cl, n_cl2, n_clp, n_cln, n_cl2p
nclp = (((kCliz*n_e*n_cl)+((kpair+kdisiz)*n_e*n_cl2)-(5e-14*n_clp*n_cln)-(S*n_clp/V)-((hlclp*Al+hrclp*Ar)*n_clp*ubclp))*dt)+n_clp
ncl2p = (((kCl2iz*n_e*n_cl2)-(5e-14*n_cl2p*n_cln) - (((hlcl2p*Al + hrcl2p*Ar)*n_cl2p*ubcl2p)/V)-(S*n_cl2p/V))*dt)+n_cl2p
ncln = ((((katt+kpair)*n_e*n_cl2)-(5e-14*n_clp*n_cln)-(5e-14*n_cl2p*n_cln)-(kdet*n_e*n_cln)-(S*n_cln/V)-(taun*(Al+Ar)/V))*dt)+n_cln
ne = ncl2p+nclp-ncln
ncl = ((((2*kdis+katt+kdisiz)*n_e*n_cl2)-(kCliz*n_e*n_cl)+(5e-14*n_cl2p *n_cln)+(2*5e-14*n_clp*n_cln)+ (kdet*n_e*n_cln) - (300*n_cl) + ((hlclp*Al + hrclp*Ar)*n_clp*ubclp/V)-(S*n_cl/V))*dt)+n_cl
ncl2 = ((n_cl2(1) + (5e-14*n_cl2p*n_cln) - ((kCl2iz+kdis+katt+kpair+kdisiz)*n_e*n_cl2) + (0.5*300*n_cl) + ((hlcl2p*Al + hrcl2p*Ar)*n_cl2p*ubcl2p/V)-(S*n_cl2/V))*dt)+n_cl2
return
end subroutine ncalc
In the line in subroutine ncalc immediately before the return statement, you have a reference to n_cl2(1) very early in the right hand side of the assignment statement. n_cl2 has not been declared as an array, therefore the compiler assumes that it must be a reference to a function that takes a single default integer argument. Because n_cl2 is a dummy argument, the it then expects you to provide a function for the corresponding actual argument when the routine is called.
(How your compiler manages to compile the preceding references to n_cl2 is a bit of a mystery - I suspect this error violates the syntax rules and hence you should see some sort of compile time diagnostic.)
Given you are using modules, it seems odd that you have not placed the ncalc routine in a module. If you did so, the error would probably become a compile time error rather than a runtime.