experchange fortran in not writing to a file

 BaRud (02-05-19, 10:59 AM)
I am clueless why this code is not writing anything on the file (unit 21), but in terminal (*), working fine. The file is created, working otherwise fine.

Check the line number 50 and 51.

implicit none
integer i,k,j,l,c,e, seed, irange
real p,t,s,w,m,n,r,a,u,z1,o,f,d,pi,temp
real b(1000)
irange = 10000
open(21,file="JC31.dat", status="replace")
pi=4.0*atan(1.0)
n=50
m=2
r=1.0
call srand(seed)
seed = 23
t=2
!\$OMP PARALLEL DO
do e=1,10
f=0
d=0
do i=1,irange
do j=1,101
w=0
do k=38,62
z1=((k-n)**2)/(2*m*m)
p=(1/((sqrt(2*pi))*m))*exp(-z1)
o=4*pi*sqrt(n*k)
a=r*tan(pi*((rand())-(1./2)))
u=cos(o*(1+a)*t)
s=p*u
w=w+s
enddo
b(j)=w
enddo
do c=1,10
do j=1,100
do l=j+1,101
if (b(j).gt.b(l))then
temp=b(l)
b(l)=b(j)
b(j)=temp
endif
enddo
enddo
enddo
!do j=1,101
!write(20,*) b(j)
!enddo
d=d+b(51)
if (mod(i,1000)==0) write(*,*) i
enddo
f=f+d/irange
write(21,*)e,t,f !this line is not working
write(*,*)e,t,f
t=2+rand()
enddo
!\$OMP END PARALLEL DO
close(21)
end
 mecejfour (02-05-19, 04:34 PM)
On Tue, 05 Feb 2019 00:59:52 -0800, BaRud wrote:

> I am clueless why this code is not writing anything on the file (unit
> 21), but in terminal (*), working fine. The file is created, working
> otherwise fine.

The file was written using GCC-8 on Windows+MinGW64, and contained ten
lines with three numbers per line. If you had trouble with this, please
provide details on platform, compiler, etc.

> ...
> call srand(seed)
> seed = 23
> ...

You should set the value of seed BEFORE calling srand().

Note that srand() and rand() are not standard Fortran routines.

Furthermore, the function rand() of the C library is of type INTEGER,
whereas it is taken to be of type REAL under implicit typing. This may be
a major source of error in your program.

--mecej4
 BaRud (02-05-19, 07:33 PM)

I am using gfortran 8.2.1 in Linux. The gfortran document says: RAND(FLAG) returns a pseudo-random number from a uniform distribution between 0 and 1.

But irrespective of the result, shouldn't it write the data in the file, what it is writing in stdout?
 mecejfour (02-05-19, 08:54 PM)
On Tue, 05 Feb 2019 09:33:21 -0800, BaRud wrote:

> I am using gfortran 8.2.1 in Linux. The gfortran document says:
> RAND(FLAG) returns a pseudo-random number from a uniform distribution
> between 0 and 1.

OK, you are using the G77 non-standard function RAND(). You still need to
call SRAND with the seed value set.

> But irrespective of the result, shouldn't it write the data in the file,
> what it is writing in stdout?

Yes, but if you use OpenMP and your program is made to run with several
threads, don't expect the different threads to output to the shared file
in the same (logical) order as with a serial program.

--mecej4