# This is a test script for the Euler solver written in Fortran # To use it, simply do # >> import test_f2pyEuler # >> test_f2pyEuler.run() import Numeric # we need the Numeric module for proper interfacing with f2py import f2pyEuler # importing the Fortran extension module # defining the derivative functions: def f1(x,t): return x[0]*x[1] def f2(x,t): return -x[0]*t L = [f1,f2] # this is the array of derivatives (represents the ODE) # we now put a wrapper around the L array of functions so that we obtain # a function compatible with our Fortran interface [result returned in place # by an argument; index of function used also as argument] def f(i,x,t): return L[i](x,t) def fun(i,x,t,res): """fun(i,x,t,res) does res[0] = f(i,x,t)""" res[0] = f(i,x,t) return # setting the initial conditions # [the difference with before is that we now need to use Numeric.array # since this is the data type used by f2py] x = Numeric.array([1.0,2.0]) newx = Numeric.array(x) t = 1.0 dt = 5.0 # the testing routine: def run(): f2pyEuler.euler(fun,x,t,dt,newx) # testing f2pyEuler extension! print "f2pyEuler.euler(fun,x,t,dt,newx) with fun(i,x,t,res) -> res[0] = L[i](x,t)" print "where L = [x[0]*x[1], -x[0]*t], x =", x, "t =", t, "and dt =", dt print "yields newx = ", newx print "This should be [11.0, -3.0] if everything works."