I've come across a bit of a strange bug, and I'm really not sure what's causing it.
I have a list containing lambda functions, and i have set this list to be a global variable as shown below. The global variables are then referenced within the "residual" function with the required function inputs.
from numpy import array
from math import exp
from guppy import hpy
hp = hpy()
hp.setrelheap()
rate_array = [[0, "4.91*(10**-22)*(T_m**4)"],
[1, "1.4*(10**-18)*(T_m**0.928)*exp(-T_m/16200)"]]
global k
k = [0,0]
for i in range(0,2):
k[i] = (eval('lambda T_m,T_r,Z_initial: ' + rate_array[i][1]))
def residual(t,y,yd):
Z_initial = 10
res_0 = -k[1](y[2],y[3], Z_initial)*y[0]
res_1 = -k[0](y[2],y[3],Z_initial)*y[1]
return array([res_0,res_1 ])
y = [0,0,0,0]
yd= [0,0,0,0]
t=0
residual(t,y,yd)
print("\nMemory statistics are as follows:\n")
print hp.heap()
Running the residual function seems to give a invalid pointer error, or a segmentation fault. Every now and then the code runs, but most of the time it does not. I don't see anything wrong with the code, so im not sure whats going on. Is there anything glaringly obvious?
EXTRA: I realize this is a strange way to go about it, but to explain: the "rate_array" contains strings as it is pickled/unpickled depending on whether or not the user wants to edit it.
Also the residual function is integrated by 3rd party software and it only allows the 3 inputs shown, so i can't just pass the array as an input into the function as normal. I also cannot append it to include the rate_array, as it wont take lambda functions as an acceptable type.
Normally the lambda functions/expressions could just be in the residual function itself, as just extra lines of code, but then its not possible for the user to edit them before the code runs.....Its such a mess!
EDIT: apologies, i heavily cut down the code to try and make it simpler to explain, but in the process just typed stuff that was plain wrong, now corrected.
The error code:
*** glibc detected *** python: free(): invalid pointer: 0x0000000002a7aa50 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x7eb96)[0x7fd857ddbb96]
/usr/local/lib/python2.7/dist-packages/guppy/sets/setsc.so(+0xbeeb)[0x7fd841bd1eeb]
/usr/local/lib/python2.7/dist-packages/guppy/sets/setsc.so(+0xbf73)[0x7fd841bd1f73]
python[0x48abf9]
python[0x48a9de]
python(PyEval_EvalFrameEx+0x52c)[0x45fb2c]
python(PyEval_EvalFrameEx+0xcb7)[0x4602b7]
python(PyEval_EvalCodeEx+0x199)[0x467209]
python(PyEval_EvalCode+0x32)[0x4d0242]
python[0x5102bb]
python(PyRun_FileExFlags+0x9a)[0x44a466]
python(PyRun_SimpleFileExFlags+0x2bc)[0x44a97a]
python(Py_Main+0xb36)[0x44b6bc]
/lib/x86_64-linux-gnu/libc.so.6(__libc_start_main+0xed)[0x7fd857d7e76d]
python[0x4ce0ad]
from numpy import *
? It'd be nice to try to reproduce this ... – mgilson Mar 5 '13 at 14:24res_0 = -k[1](y[8],y[7], Z _initial)*y[4]
– mgilson Mar 5 '13 at 14:24global
doesn't go there - it goes in the function using the global. Also there appear to be some typos in the code. I'd suggest giving a complete example with all variables defined. – Douglas Leeder Mar 5 '13 at 14:27Z_initial
in theresidual
function? If I try to run the above code I get the error that it's not defined. Please make sure the code sample is runnable! – Claudiu Mar 5 '13 at 14:36