[Fortran] 纯文本查看 复制代码
program main
implicit none
integer,parameter::file_number= 1
character(100)::tmp
character(160)::fileN
character(100)::filename(file_number)
integer::i,j,k,m,n,NUM,step,ios,ntotal
integer::p,q,GetFileN
real,pointer::A(:,:),B(:,:),c(:)
real,pointer::x1(:)
real,pointer::y1(:)
real,pointer::z1(:)
real,pointer::x2(:)
real,pointer::y2(:)
real,pointer::z2(:)
real,pointer::up(:)
real,pointer::below(:)
real,pointer::cos2(:)
real,pointer::s(:)
real,pointer::summ(:)
real,pointer::avg(:)
real*8 time
integer*4 time0, time1, dtime
c Start time
call system_clock(time0)
open(10,file='testdata.dat',status='old')
num=GetFileN(10)
allocate(a(num,3))
allocate(b(num,3))
allocate(c(num))
allocate(x1(num))
allocate(y1(num))
allocate(z1(num))
allocate(x2(num))
allocate(y2(num))
allocate(z2(num))
allocate(up(num))
allocate(below(num))
allocate(cos2(num))
allocate(s(num))
allocate(summ(num))
allocate(avg(num))
do n=1,num
read(10,*,iostat=ios) (a(n,m), m=1,3)
if(ios/=0) then
exit
endif
c write(6,'(f8.3,f8.3,f8.3)')(a(n,m), m=1,3)
enddo
Print*, 'Running...'
do step=1,NUM/2
summ=0
do j=1,NUM-step
c write(tmp,"(i4.4)")step
B(j,:)=A(step+j,:)
c print*, A(j,:)
c print*, B(j,:)
x1(j)=a(j,1)
y1(j)=a(j,2)
z1(j)=a(j,3)
x2(j)=b(j,1)
y2(j)=b(j,2)
z2(j)=b(j,3)
up(j)=(x1(j)*x2(j)+y1(j)*y2(j)+z1(j)*z2(j))**2
below(j)=(x1(j)*x1(j)+y1(j)*y1(j)+z1(j)*z1(j))*(x2(j)*x2(j)
1+y2(j)*y2(j)+z2(j)*z2(j))
cos2(j)=up(j)/below(j)
s(j)=(3*cos2(j)-1)/2
summ=s(j)+summ
enddo
c print*, summ(j)
avg=summ(j)/(num-step)
c print*, (num-step)
c print*, avg(j)
open(20,file='testresult.dat',status='unknown')
write(20,222)avg(j)
enddo ! enddo for 'tmp'=>step
111 format (3f8.3)
222 format (f12.6)
close(10)
deallocate(A,B,C)
deallocate(x1,y1,z1,x2,y2,z2,up,below,cos2,s,summ,avg)
c Finish time
call system_clock(time1, dtime)
c Output time
time = 1d0*(time1-time0)/dtime
write(*,"(a7,f16.7)")"Time = ",time
end
integer function GetFileN(iFileUnit)
implicit none
logical , parameter :: b = .True.
integer , intent( IN ) :: iFileUnit
character*(1) :: c
GetFileN = 0
rewind( iFileUnit )
do while (b)
read( iFileUnit , * ,end =999 ,Err = 999 )c
GetFileN = GetFileN + 1
end Do
999 rewind( iFileUnit )
return
end function GetFileN