On Sunday 02 September 2007 21:10, Sergey Plis wrote:
> On Sunday 02 September 2007 11:16:00 Bernd Paysan wrote:
> > Duh, there's a word conflict between FREE for bigForth's own memory
> > handling and the Unix FREE. So you ALLOCATE from bigForth's memory
> > pool and you FREE from POSIX' memory pool. A crash is inevitable.
> > Changes checked in.
>
> Please find attached an archive with 3 files in it. In order to
> reproduce the bug I am having do:
>
> bigforth ode_example_fixed_interval.fs
>
> In gdb you'll see this:
>
> Program received signal SIGSEGV, Segmentation fault.
> gsl_block_free (b=0x64) at init_source.c:77
> 77      init_source.c: No such file or directory.
>         in init_source.c
>
> Bernd, I cc this to your personal address just in case the mailing
> list does not take my attachment.

I received it only once, so the list probably refused the complete mail.

I see that the gsl library contains symbols which should give you just a 
pointer, and I've added a func' word to callback.fs (I think it's 
appropriate there), which turns a library binding into the C function 
pointer (i.e. the address inside the library). That should help you to 
get access to those step types without creating and compiling a helper 
library. I modified the use of your pointer library into straight 
forward (int) declarations, and use func' <step type name> @ to get the 
type.

Further help to track down bugs: Insert a "traceall" into your source 
somewhere before you suspect it to crash. This will output everything 
that's interpreted before, even when the crash itself is corrupting the 
system.

Next thing: Insert ~~ into the program parts you suspect of crashing. 
Each ~~ will print out the source location and the stack (and it works 
even within a callback). With that, I've located the problem: 
The "y[ ]free" in _func is causing the problem. Removing that one makes 
your example run. There's no memory leak observable during the run, so 
the other callback function takes care of freeing that memory. I'm not 
sure if the result is right, but the result oscillates.

BTW: For numerical stability in algorithms like that, don't increment 
your floating point variable with a delta step (f:=f+deltaf), but 
instead use an integer index, and multiply it with the delta step to 
obtain the current position (f:=f0+i*deltaf).

-- 
Bernd Paysan
"If you want it done right, you have to do it yourself"
http://www.jwdt.com/~paysan/
\ ------------------------------------------------------------------------------
\                          ** Loading required modules **
\needs callback include callback.fs
\needs gsl include gsl.fs
\needs locals include locals.fs
\needs vectors include vtest.fs

\ also dos
also vectors also gsl also float 

\ ------------------------------------------------------------------------------
/* from GSL reference:
The following program solves the second-order nonlinear Van der Pol oscillator
equation,

x''(t) + ÎŒx'(t)(x(t)2 − 1) + x(t) = 0

This can be converted into a first order system suitable for use with the
ODE GSL routines by introducing a separate variable for the velocity, y = x'(t),

x' = y
y' = −x + ÎŒy( 1 − 2x )

The program begins by defining functions for these derivatives and their
Jacobian. This example uses fixed interval. */

: _func ( t y[ f[ mu -- r )
    fdrop f@
    2 pvector swap 2 pvector swap
    { y[ f[ |
    y[ 1 ]@ f[ 0 ]!
    y[ 0 ]@ fdup f* !1 f- y[ 1 ]@ f* f* fnegate
    y[ 0 ]@ f- f[ 1 ]!
    ( y[ ]free ) f[ ]free
    GSL_SUCCESS } ;
: _jac ( t y[ dfdy dfdt[ mu -- r )
    fdrop f@
    2 pvector
    swap 2 2 2 pmatrix
    rot 2 pvector
    { dfdt[ m[[ y[ f: mu |
    !0.0 m[[ 0 0 ]]!  !1.0 m[[ 0 1 ]]!
    y[ 0 ]@ y[ 1 ]@ f* mu f* !-2.0 f* !1 f-
    m[[ 1 0 ]]!
    y[ 0 ]@ fdup f* !1 f- mu f* fnegate
    m[[ 1 1 ]]!    
    !0 dfdt[ 0 ]! !0 dfdt[ 1 ]!    
    m[[ ]]free dfdt[ ]free y[ ]free
    GSL_SUCCESS } ;

' _func gsl_odeiv_func4:1 func
' _jac  gsl_odeiv_jac5:1 jac

func' gsl_odeiv_step_rk4 @ 2 gsl_odeiv_step_alloc constant s

fvariable mu !10 mu f!

sizeof gsl_odeiv_system allocate throw constant sys
func sys gsl_odeiv_system func !
jac sys gsl_odeiv_system jac !
2 sys gsl_odeiv_system dim !
mu sys gsl_odeiv_system params !

fvariable t !0 t df!
!100 fconstant t1
fvariable h 1e-2 h df!
2 fvector y[ !1 y[ 0 ]! !0 y[ 1 ]!
2 fvector y_err[
2 fvector dydt_in[
2 fvector dydt_out[

\ * initialise dydt_in from system parameters * /
t df@ y[ ]data dydt_in[ ]data mu _func drop

: iterate ( -- )
    begin t df@ t1 f< while
	    s t df@ h df@ y[ ]data y_err[ ]data dydt_in[ ]data
	    dydt_out[ ]data sys gsl_odeiv_step_apply
	    IF LEAVE THEN
	    dydt_in[ dydt_out[ ]copy] h df@ t df@ f+ t df!
	    t df@ f. y[ ]print cr
    repeat ;

100 vector* dup 1 swap 0 )! )free

iterate

s gsl_odeiv_step_free
bye

Attachment: pgpbZ7SxdDg5J.pgp
Description: PGP signature

Reply via email to