//genesis - Purkinje cell M9 genesis2 script
/* Copyright E. De Schutter (Caltech and BBF-UIA) */

/**********************************************************************
** Set of currents developed for rat cerebellum Purkinje neuron by
**  and dendritic spine prototypes.
** This version actually computes all the tables and saves it to files
** E. De Schutter, Caltech, 1991-1992
**********************************************************************/

/* Reference:
** E. De Schutter and J.M. Bower: An active membrane model of the
** cerebellar Purkinje cell. I. Simulation of current clamps in slice.
** Journal of Neurophysiology  71: 375-400 (1994).
** http://www.bbf.uia.ac.be/TNB/TNB_pub8.html
** Consult this reference for sources of equations and experimental data.
*/


/* The data on which these currents are based were collected at room
**   temperature (anything in the range 20 to 25C).  All rate factors were
**   adapted for body temperature (37C), assuming a Q10 of about 3;
**   in practice all rate factors were multiplied by a factor of 5 */

// CONSTANTS
/* should be defined by calling routine (all correctly scaled):
**	ENa, GNa
**	ECa, GCa
**	EK, GK 
**  	Eh, Gh */

include defaults.g
include Purk_const.g


int include_Purk_chansave

if ( {include_Purk_chansave} == 0 )

	include_Purk_chansave = 1


/*********************************************************************
**                  Central channel creation routine
*********************************************************************/

/* Make tabchannels */
function make_chan(chan, Ec, Gc, Xp, XAA, XAB, XAC, XAD, XAF, XBA, XBB,  \
    XBC, XBD, XBF, Yp, YAA, YAB, YAC, YAD, YAF, YBA, YBB, YBC, YBD, YBF \
    , Zp, ZAA, ZAB, ZAC, ZAD, ZAF, ZBA, ZBB, ZBC, ZBD, ZBF)
    str chan
    int Xp, Yp, Zp
    float Ec, Gc
    float XAA, XAB, XAC, XAD, XAF, XBA, XBB, XBC, XBD, XBF
    float YAA, YAB, YAC, YAD, YAF, YBA, YBB, YBC, YBD, YBF
    float ZAA, ZAB, ZAC, ZAD, ZAF, ZBA, ZBB, ZBC, ZBD, ZBF

    if ({exists {chan}})
        return
    end

    create tabchannel {chan}
    setfield {chan} Ek {Ec} Gbar {Gc} Ik 0 Gk 0 Xpower {Xp} Ypower {Yp}  \
        Zpower {Zp}
    if (Xp > 0)
        setupalpha {chan} X {XAA} {XAB} {XAC} {XAD} {XAF} {XBA} {XBB} {XBC} \
			{XBD} {XBF} -size {tab_xdivs+1} -range {tab_xmin} {tab_xmax}
    end
    if (Yp > 0)
        setupalpha {chan} Y {YAA} {YAB} {YAC} {YAD} {YAF} {YBA} {YBB} {YBC} \
			{YBD} {YBF} -size {tab_xdivs+1} -range {tab_xmin} {tab_xmax}
	end
    if (Zp != 0)
        setupalpha {chan} Z {ZAA} {ZAB} {ZAC} {ZAD} {ZAF} {ZBA} {ZBB} {ZBC} \
			{ZBD} {ZBF} -size {tab_xdivs+1} -range {tab_xmin} {tab_xmax}
    end

end


/******************************************************************* \
**
**               The current equations themselves 
*********************************************************************/

function make_Purkinje_chans_KC
    int i, cdivs
    float zinf, ztau, c, dc, cmin, cmax
    float x, dx, y
    float a, b

echo making and saving Purkinje channel library....


    /* non-inactivating BK-type Ca-dependent K current eq 3 (Gruol K1)
    /* scaled for units: V, sec, mM */
    make_chan Purk_KC {EK} {GK} 1 7.5e3 0.0 0.0 0.0 1.0e12 0.110e3 0.0  \
        0.0 -0.035 14.9e-3 0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0  \
        0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0

        x = {tab_xmin}
        dx = ({tab_xmax} - {tab_xmin})/{tab_xdivs + 1}

        openfile Purk_KC_a_max_37.test w
        openfile Purk_KC_a_tau_37.test w

        for (i = 0; i <= ({tab_xdivs + 1}); i = i + 1)

                a = {getfield Purk_KC X_A->table[{i}]}
                b = {getfield Purk_KC X_B->table[{i}]}

                writefile Purk_KC_a_max_37.test {x} {a / b}
                writefile Purk_KC_a_tau_37.test {x} {1 / b}

                x = {x + dx}
        end

        closefile Purk_KC_a_max_37.test
        closefile Purk_KC_a_tau_37.test


    /* Z: Ca-activation, tabchannel specific implementation */
    cmin = ({CCaI}) // will create a z-range from 0.0021 to 0.9727
    cmax = ({Ca_tab_max})
    cdivs = tab_xfills+1
    c = cmin
    dc = cdivs
    dc = (cmax - cmin)/cdivs
    //slow (Gola, Ikemoto:50-280ms delay)
    ztau = 0.010
    call Purk_KC TABCREATE Z {cdivs} {cmin} {cmax}
    for (i = 0; i <= (cdivs); i = i + 1)
	    zinf = 1/(1 + (4.0e-3/c))
	    setfield Purk_KC Z_A->table[{i}] {zinf/ztau}
	    setfield Purk_KC Z_B->table[{i}] {1/ztau}
	    c = c + dc
    end
    setfield Purk_KC Zpower 2
    setfield Purk_KC Z_A->calc_mode 0
    setfield Purk_KC Z_B->calc_mode 0
//    call Purk_KC TABSAVE Purk_KC.tab


        x = {cmin}

        openfile Purk_KC_Ca_max_37.test w
        openfile Purk_KC_Ca_tau_37.test w

        for (i = 0; i <= ({cdivs}); i = i + 1)

                a = {getfield Purk_KC Z_A->table[{i}]}
                b = {getfield Purk_KC Z_B->table[{i}]}

                writefile Purk_KC_Ca_max_37.test {x} {a / b}
                writefile Purk_KC_Ca_tau_37.test {x} {1 / b}

                x = {x + dc}
        end

        closefile Purk_KC_Ca_max_37.test
        closefile Purk_KC_Ca_tau_37.test


end
/*********************************************************************/



end