0

I am trying to dertmain a coeffecient by fitting experimental data using lmfit.Model.

When I run my code I get the following error:

Traceback (most recent call last):

  File "C:\***.py", line 82, in <module>
    Dc = fit_Dc(t_days, prod, PV, Swi, l, r)

  File "C:\***.py", line 63, in fit_Dc
    result = fmodel.fit(Son, params, t)

  File "C:\***\Anaconda3\lib\site-packages\lmfit\model.py", line 991, in fit
    var_data = kwargs[var]

KeyError: 't_days'

The function is:

I want do determine Dc

I tried to fix the parameters l&r since they're known. t (time) coressponds to my x data, Son is my Y-data I want to fit the model to.

My code is:

t_days = np.array([0, 2.3, 4.33, 6.83])
prod = np.array([0, 3, 3.1, 3.3])
PV = 11.2
Swi = 0.1303

def calc_So(t_days, PV, prod, Swi):
    t = t_days*24
    Sw = Swi + prod/PV
    So = 1 - Sw
    Soi = 1 - Swi
    recovery = prod/Soi/PV
    Son = (So - So[-1])/(Soi - So[-1])
    return recovery, Soi, Sw, So, Son, t
def calc_So_n(t, l, r, Dc):
     c_cyl = []
     #calc C_ps
     for n in range(0, 1200):
         c_psi = ((8/(2*n+1)**2)/np.pi**2)*np.exp(-Dc*((2*n+1)**2*np.pi**2)*t/4/l**2)
         c_ps.append(c_psi)
     c_ps = np.asarray(c_ps, dtype = np.float64)
     return np.sum(c_ps, axis = None)

     #calc c_cyl
     for n in range(0, 1200):
            c_cyli = (4/((jn_zeros(0,n)[n-1])**2))*np.exp(-Dc*t*(jn_zeros(0,n)[n-1]/r)**2)
            c_cyl.append(c_cyli)
     c_cyl = np.asarray(c_cyl, dtype = np.float64)       
     return np.sum(c_cyl, axis = None)

     return c_cyl*c_ps


# fit DC ti experimental data using lmfit model
def fit_Dc(t_days, prod, PV, Swi, l, r):
    recovery, Soi, Sw, So, Son, t = calc_So(t_days, PV, prod, Swi)
    # create model
    fmodel = Model(calc_So_n)
    # giving initial values for parameters
    params = fmodel.make_params(r=r, l=l, Dc = 0)


    # fix l & r:
    params['r'].vary = False
    params['l'].vary = False
    # fit parameters to data with constant values of r, l:
    params['r'].value = r
    params['l'].value = l
    result = fmodel.fit(Son, params, t= t)
    print("Dc, Dc_error, chi-square=%f" % (result.params['Dc'].value, 
                                             result.params['Dc'].stderr,
                                             result.chisqr))
    return result.params['Dc'].value

recovery, Soi, Sw, So, Son, t = calc_So(t_days, PV, prod, Swi)
Dc = fit_Dc(t, prod, PV, Swi, l, r)

t is an np array as well as Son

samhar
  • 13
  • 7
  • Please give a minimal, complete example (https://stackoverflow.com/help/minimal-reproducible-example). There is no line `Dc = fit_Dc(t_days, prod, PV, Swi, l, r)` in your posted code. One hint: You probably want `result = fmodel.fit(Son, params, t=t)` (or is that `t_days`?). Another hint: why does your `calc_So_n()` function have 3 `return` statements and what do you expect it to do? – M Newville Jan 20 '20 at 02:47
  • @MNewville thanks!!, please check the edited version. – samhar Jan 20 '20 at 08:19
  • @MNewville it works now after adding `t=t` Thanks!! – samhar Jan 20 '20 at 09:00
  • Glad that helped. But: your code still does not run. When you ask a question on SO, the motivation for anyone to respond is to help others (plural!). Getting it to work for you, right now, is not the sole motivation. If you do not ask a question in a way that can help others (including: the code you post actually runs and does what you say it does), our motivation for helping you again drops. So: if you think you might want help here in the future, please fix this question to be a better resource that might be able to help someone else in a similar situation. – M Newville Jan 20 '20 at 19:18

0 Answers0