Help needed

New to FreeBASIC? Post your questions here.
LawrenceG
Posts: 4
Joined: Mar 04, 2021 16:38

Help needed

Postby LawrenceG » Mar 04, 2021 18:19

My program crashes, as in Windows reports it has a problem that caused it to stop working correctly.

If I comment out line 63 the problem goes away.
But if I leave it in, the problem does not manifest until execution reaches line 100 or 101. Line 102 is not executed.

I can't see anything wrong. What am I missing?

Code: Select all

' Tessendorf  ocean surfcace model
'     

Type complex
   real As Double
   imag As Double
End Type

Dim As Long N,M
Dim As Integer n1,nn,m1,mm,iz,ix,ii,ixminus,izminus,ms,ns,izk,ixk,i,j,kloop
Dim As Double k(Any,Any,Any),pi,w(2),Phk,xir,xii,ksqrd,hbrt,L,h(Any,Any),theta,x,z,tstart,tend
Dim As Double Lx,Lz,A,hmax,hmin,hrange,lg_spectrum(Any,Any), specmin, specmax
Dim As complex hbar0k(Any,Any), hbark(Any,Any),thexp
Dim As String ff,fpath,fname

Declare Function randn(sd As Single)As Double

hmax=0
hmin=0
specmin=100
specmax=0
pi=ATan2(1,1)*4
Print pi
w(1)=0         'normalised wind direction vector
w(2)=-1   

L=100        'maximum wind-generated wavelength = windspeed^2/g

'N=64
'M=64
Input "N,M ";N,M
tstart=Timer()

ReDim k(2,N+1,M+1)
ReDim hbar0k(N+1,M+1),hbark(N+1,M+1)
ReDim h(N,M),lg_spectrum(N,M)


Lx=N/150*25.4*2/.75        ' pixels converted to metres
Lz=M/150*25.4*2/.75
A=1       ' amplitude scale factor
n1=-N/2
nn=N/2-1
m1=-M/2
mm=M/2-1
Print "line 45 OK"
'GoTo skip
for iz=1 To M+1            'note go to M+1 so hbar(-k) works when iz=1
   ms=m1+(iz-1)
   For ix=1 To N+1
      ns=n1+(ix-1)
      k(1,ix,iz)=2*pi/Lx*ns
      k(2,ix,iz)=2*pi/Lz*ms
      
      ksqrd=k(1,ix,iz)^2+k(2,ix,iz)^2
      'Print ksqrd;" ";
      If ksqrd>0 Then
        Phk=A/(ksqrd^2)*Exp(-1/(ksqrd*L^2))*((k(1,ix,iz)*w(1)+k(2,ix,iz)*w(2))^2)/ksqrd  ' To normalise k in . product
      Else
        Phk=0
      EndIf
'      Print "line 60 OK"
      lg_spectrum(ix,iz)=Phk                                           '  NO PROBLEM IF THIS LINE COMMENTED OUT
'      Print "line 62 OK"
      If Phk>specmax Then specmax=Phk
      If Phk<specmin Then specmin=Phk
      hbrt=Sqr(Phk/2)
      'Print hbrt
      xir=randn(1)
      xii=randn(1)
      hbar0k(ix,iz).real=xir*hbrt
      hbar0k(ix,iz).imag=xii*hbrt
      
   Next
Next
Print "line 72 OK"
for iz=1 To M                     '  set up hbar(_x_)
   ms=m1+(iz-1)
   izminus= M/2+1-ms
   For ix=1 To N
      ns=n1+(ix-1)
      ixminus=N/2+1-ns
      hbark(ix,iz).real=hbar0k(ix,iz).real-hbar0k(ixminus,izminus).real
      hbark(ix,iz).imag=hbar0k(ix,iz).imag+hbar0k(ixminus,izminus).imag
   Next
Next

' Now cycle through each _x_ position and sum the waves
                                     ' deleted while testing

skip:

tend=Timer()
hrange=hmax-hmin
Print "hmin, hrange=";hmin;" ";hrange
Print "time elapsed ";tend-tstart


Print "line 123 OK"
'                                                                                                                      CRASHES HERE
fpath = "C:\Users\Toughbook\Documents\wargames\terrain\sea\"
Print "line 126 OK"
fname="tessspec.bin"
Print "default output to ";fname
Input "new output filename or press return "; ff
If ff <>"" Then fname=ff
'****************************************************
Open fpath+fname For Binary As #2
Put #2,,N
Put #2,,M
For kloop=1 To 3
   For j=1 To M
      For i=1 To N
'        Put #2, ,CUByte((h(i,j)-hmin)*255/hrange),1
       Put #2, ,CUByte((lg_spectrum(i,j)-specmin)*255/(specmax-specmin)),1
      Next
   Next
Next
Close #2

Print "run complete"
Sleep


End

 

Function randn(sd As Single)As Double         '  returns normally distributed Random variable, mean =0, sigma=1
                                              '  sd = seed-related argument for RND()
Dim As Double u1,u2,z1,pi

pi=ATan2(1,1)*4

u1=Rnd(sd)
u2=Rnd(sd)
'Print u1;Log(u1)
z1 = Sqr(-2 *Log(u1))*cos(2 *pi *u2)
'z_2 = \sqrt{-2 \ln (u_1)} \sin (2 \pi u_2)
Return z1
End Function
fxm
Moderator
Posts: 10369
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: Help needed

Postby fxm » Mar 04, 2021 19:06

Compiling with option '-exx', I get the runtime error:
Aborting due to runtime error 6 (out of bounds array access) at line 63 of .....

ReDim lg_spectrum(N,M)
.....
for iz=1 To M+1 'note go to M+1 so hbar(-k) works when iz=1
ms=m1+(iz-1)
For ix=1 To N+1
.....
lg_spectrum(ix,iz)=Phk ' NO PROBLEM IF THIS LINE COMMENTED OUT
.....


When iz=M+1 and/or ix=N+1, out bounds 'lg_spectrum' array access.
LawrenceG
Posts: 4
Joined: Mar 04, 2021 16:38

Re: Help needed

Postby LawrenceG » Mar 04, 2021 19:21

Thanks very much , that was it. Must look up the -exx option.

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 3 guests