gl_adjust = 30*(0.0001/gl**2 + 0.0001/(gl-0.15)**2)
    vl_adjust = 0.001*(vl+66)**4
    sp_adjust = 15*(evtime-HH_spike_t)**2
    vec = vl_adjust + gl_adjust + sp_adjust + \
           1/(1+10*sp_adjust) * (s.goalTraj(tmesh,s.depVarNames).toarray() - \
                       s.testModel(s.trajname, tmesh, s.depVarNames).toarray())
    adjusted = sum(vec**2)
##    print vl, gl, vl_adjust, gl_adjust, sp_adjust
    print "adjusted error = ", adjusted, "\n"
    return vec


pest_pars = LMpest(freeParams=['vl', 'gl'],
                 depVarNames=['v'],
                 testModel=HH_test_model,
                 goalTraj=goaltraj,
                 trange=[0,15],
                 residual_fn=residual_fn,
                 usePsyco=True
                )

start = clock()
pestData_par = pest_pars.run(parDict={'ftol':1e-4,
                                      'xtol':1e-4,
                                      'args':(HH_spike_t,)},
                             verbose=True)
print '... finished in %.3f seconds\n' % (clock()-start)


## Finish preparing plots
print '\nPreparing plots'
disp_dt = 0.05
              checklevel=2,
              ics=xic,
              name='testODEsol'
              )
testODE = Generator.Vode_ODEsystem(testDSargs)

genInfoEntry = makeGenInfoEntry(testODE, ['testODEsol'])
genInfoDict = makeGenInfo([genInfoEntry])
modelArgs_par = {'genInfo': genInfoDict,
             'name': 'test_model_par',
             'ics': xic}

testModel_par = Model.Model(modelArgs_par)
pest_par = LMpest(freeParams=['k'],
                 depVarNames=['w'],
                 testModel=testModel_par,
                 goalTraj=goaltraj,
                 trange=trange
                )

start_time = time.clock()
pestData_par = pest_par.run(verbose=True)
bestFitModel_par = pestData_par['sys_fit']
print '... finished in %.4f seconds\n' % (time.clock()-start_time)


## Initial condition estimation
print "Estimating initial condition for w (assuming k is correct)"
print "Goal value is w(0) = ", xic['w'], " ..."
modelArgs_ic = {'genInfo': genInfoDict,
             'name': 'test_model_ic',
             'ics': {'w': 0.0}}
# make test system
est_pars_test = {'h':0.3, 'gl': 38, 'vl': -35, 'gnap': 2.8}
pars_test = copy(pars)
pars_test.update(est_pars_test)
testDS = makeTestDS('ref', pars_test, icdict, tdomain)
testModel = embed(testDS, icdict)

est_parnames = ['gl', 'vl']

# parameter estimation
print 'Starting parameter estimation'
print 'Goal pars are gl = ', est_pars_ref['gl'], ' vl = ', est_pars_ref['vl']
pest_pars = LMpest(freeParams=est_parnames,
                 depVarNames=['v'],
                 testModel=testModel,
                 goalTraj=reftraj,
                 trange=tdomain,
                )

start = clock()
pestData_par = pest_pars.run(parDict={'ftol':3e-3,
                                      'xtol':1e-3},
                             tmesh=tmesh,
                             verbose=True)
print '  ... finished in %.3f seconds.\n' % (clock()-start)

## Finish preparing plots
print '\nPreparing plots'
disp_dt = 0.05
##plotData_goal = reftraj.sample(['v'], disp_dt)
goalleg = "v original"