Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

error with write function- fixed format fortran 2

Status
Not open for further replies.

johnnydarten

Programmer
Jan 7, 2012
5
EG
Hi, I have a problem in my project code. The code computes a certain function by reading from a file the inputs to the code and writing some outputs into another file. The code is very large (4000+ lines) so I pasted here the part which causes a run time error problem:

Code:
         program matfcn
        real*8 cm,qm,arg1,darg,x1,xx
        character*10 time,date,zone
        integer dt(8)
        ndec=16
        nex=306
c  open input file
        open(10,file='c:\matfcn\matfcn.dat',status='old')
c  read input data
        read(10,*) lnum,icq,isq
        if(icq.eq.1) read(10,*) cm
        if(icq.eq.2) read(10,*) qm
        read(10,*) ioprad,iopang,minacc
        read(10,*) x1

        if(iopang.ne.0) read(10,*) arg1,darg,narg
        if(icq.eq.2) cm=2.0d0*dsqrt(qm)
        maxj=1
        if(ioprad.ne.0) maxj=1.5*lnum+4*ndec+idint(cm)+105
        ismax=idint(0.85d0*cm)
        if(x1.lt.1.0d0) ismax=ismax*1.5
        if(x1.lt.0.1d0) ismax=ismax*idint(3.0d0**(-1-dlog10(x1)))
        if(isq.eq.-1) maxj=max(maxj,ismax+idint(cm/3.14159)+4*ndec+
     1                     idint(cm)+105)
        maxp=2*lnum+2*cm+4*ndec+105
        maxn=maxj
        if(ioprad.eq.0.or.isq.eq.1.or.cm.le.10.0d0) go to 15
        maxn=1.5*cm+100
        if(x1.lt.1.0d0.and.x1.ge.0.01d0) maxn=40/x1+cm/dsqrt(x1)+
     1                                        1.5*cm+200
        if(x1.lt.0.01d0.and.x1.ge.0.0001d0) maxn=34/x1+1.5*cm+
     1                                          1.4*cm/dsqrt(x1)+200
        if(x1.lt.0.0001d0) maxn=30/x1+cm/dsqrt(x1)+200
        maxn=max(maxn,maxj)
15      maxd=max(maxj,maxn,maxp)/2+1
        maxlp=lnum+3
        if(isq.eq.-1) maxlp=max(maxlp,ismax+idint(cm/3.14159)+3)
        ngau=200
        call main (lnum,cm,isq,ioprad,iopang,minacc,x1,arg1,darg,narg,
     1             maxd,maxj,maxlp,maxn,maxp,ndec,nex,dt,ngau)
        end


        subroutine main (lnum,cm,isq,ioprad,iopang,minacc,x1,
     1                   arg1,darg,narg,maxd,maxj,maxlp,
     2                   maxn,maxp,ndec,nex,dt,ngau)
        real*8 api,arg1,a01,b12,cm,cepio2,cedpio2,ce0,corfac,
     1         darg,dec,dnuma,dnumb,eigaval,eigbval,eigavalp,eigbvalp,
     2         eiga1,eiga2,eiga3,eiga4,eiga5,eigb1,eigb2,eigb3,eigb4,
     3         eigb5,esa,esb,gamma,m1botc,m1bots,mc1c,mc1dc,mc2c,mc2dc,
     4         mc3c,mc3dc,ms1c,ms1dc,ms2c,ms2dc,ms3c,ms3dc,m3bot,pi,rl,
     5         sed0,sepio2,sedpio2,sgna,sgnb,wroncc,wroncs,wront,x,xb,
     6         x1,x10
        real*8 enra(maxd),blista(maxd),glista(maxd),enrb(maxd),
     1         blistb(maxd),glistb(maxd)
        real*8 cbesf(maxj),cbesf1(maxj),cbesf2(maxj),cbesdf(maxj),
     1         cbesdf1(maxj),cbesdf2(maxj),cbesdr(maxj),cbesdr1(maxj),
     2         cbesdr2(maxj)
        dimension ibese(maxlp),ibese1(maxlp),ibese2(maxlp),
     1            ineue(maxlp),ineue2(maxlp)
        real*8 cbesn(maxlp),cbesn1(maxlp),cbesn2(maxlp),cneun(maxlp),
     1         cneun2(maxlp)
        real*8 cneudf(maxn),cneudf2(maxn),cneuf(maxn),cneuf2(maxn),
     1         cneudr(maxn),cneudr2(maxn)
        real*8 cosi(narg,maxp),sine(narg,maxp)
        dimension nacca(narg),naccc(narg),naccs(narg)
        real*8 arg(narg),barg(narg),ce(narg),ced(narg),
     1         se(narg),sed(narg)
        real*8 wr(ngau),xr(ngau)
        character*10 time,date,zone
        integer dt(8),jetasav(1000)
        dec=10.0d0**(-ndec-1)
        x=x1+1.0d0
        pi=3.1415926535897932384626433832795028841971d0
        api=pi/180.0d0
        gamma=0.577215664901532860606512090082402431042d0
        igauleg=0
c
c  begin loops
        if(iopang.eq.0) go to 10
          do jarg=1,narg
          arg(jarg)=arg1+(jarg-1)*darg
          barg(jarg)=arg(jarg)*api
          end do
        limsc=2*(lnum+cm+2*ndec+50)
10      continue
          call date_and_time(date,time,zone,dt)
          time1=60*dt(6)+dt(7)+0.001*dt(8)
          if(ioprad.ne.0.and.isq.eq.1) write(40,20) x,cm
20        format(1x,'x = ',e23.15,/,1x,'c = ',e23.15)
          if(ioprad.ne.0.and.isq.eq.-1) write(40,30) x,cm
30        format(1x,'x = ',e23.15,/,1x,'c = -i times',e23.15)
          if(iopang.ne.0.and.isq.eq.1) write(50,40) cm
40        format(1x,'c = ',e23.15)
          if(iopang.ne.0.and.isq.eq.-1) write(50,50) cm
50        format(1x,'c = -i times',e23.15)
          wront=2.0d0/pi
          if(iopang.ne.0.and.isq.eq.1) write(30,40) cm
          if(iopang.ne.0.and.isq.eq.-1) write(30,50) cm
          ibflag=1
          iopbpe=1
          iopneu=0
          iopbp1=0
          iopbes=1
        return
        end

Error occurs at the first if after the "10" label. It says "Error 94 :error 94: unit has neither been opened or preconnected"
I also attached the file that contains the data to be read by the program. Just for the file to work, Be sure you add it in the same directory as that in the line following the comment "c open input file". This will make no errors at this part. You can also modify the line itself to match your own directory.

Can somebody help?
 
Well, you don't open (connect) unit 40 (and unit 50, and unit 30 - see the code later;). Read error message again...
 
Thanks ArkM...I have another question...Is it possibel for fixed format fortran to try using a variable without initializing it...i.e. it is defined as real*8 for example but no value was assigned to it. If it is possible then please tell me the compiler that can be used for this. Thanks very much for the help.
 
An undefined variable does not have a predictable value. For example, undefined real*8 variable may contain not-a-number or infinity. It's a very serious error to involve undefined variables into any kind of calculations.

Good compilers (and debuggers) can detect (to some extent) undefined variable status. For example, Silverfrost FTN95 compiler detects many cases of undefined variable values.

Alas, sometimes it's (very) hard to detect such errors in Fortran (and other languages) codes...
 
I have not moved on beyond g95. It has compiler flags to initialize variables to zero; also (with warnings all -fwall?), it lets you know if they are never being used, used without being initialized, etc.

The way I see it, using a variable without first initializing is a programmer's oversight.
 
So a variable cannot be used before being initialized in F95 no matter what the compiler is...
Thanks Arkm and Salgerman for your help
 
It's more like a "shouldn't be used" rather than a "cannot be used"...It CAN be used, but some compiler cannot guarantee its value (I think g95 has a flag -fzero that "initializes" everything to zero at compile time).



 
Some clarification:
1. If the program uses undefined variables it's ill-formed. This program behaviour is undefined.
2. Flag fzero is harm. The program compiled with this flag is not portable and does not conform to all Fortran standards. In essence, it looks like ostrich effect switch on.
 
IMO, it's good practice to initialize variables - always and in every programming language
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top