COMMENT
	calcium accumulation into a volume of area*depth next to the
	membrane with a decay (time constant tau) to resting level
	given by the global calcium variable cai0_ca_ion
	Modified to include a resting current (irest) and peak value
	(cmax)
	i is a dummy current needed to force a BREAKPOINT
	Note: irest cancels out resting calcium current contributions
	by mechanisms that write ica.  To initialize irest properly
	use a custom proc init that assigns values to irest as illustrated
	in this excerpt:
	finitialize(Vrest) // use v_init if want to change.
        fcurrent()
	forall if (ismembrane("cacum")) {
	       for(x,0) irest_cacum(x)=ica(x) // (fixed from =-ica minus sign bug)
	}

ENDCOMMENT

NEURON {
    SUFFIX ca_acc
    USEION ca READ ica WRITE cai
    RANGE tau, cai0, cmax, irest, ca_tmax
    NONSPECIFIC_CURRENT dummy
}

UNITS {
    (um) = (micron)
    (mM) = (milli/liter)
    (mA) = (milliamp)
    F = (faraday) (coulombs)
}

PARAMETER {
    irest = 0  (mA/cm2)		: to be initialized in hoc	
    tau = 350 (ms) :  ~40 ms phenomenologically fits dendrite shafts, ~350 ms fits spines (murthy et al 2000 PNAS, earlier model default was 100 (ms)
    cai0 = 50e-6 (mM)	: Requires explicit use in INITIAL
    : block for it to take precedence over cai0_ca_ion
    : Do not forget to initialize in hoc if different
    : from this default.
}

ASSIGNED {
    ica      (mA/cm2)
    dummy (mA/cm2)
    cmax     (milli/liter)
    ca_tmax  (ms)
    d (um)
}

STATE {
    cai (mM)
}

INITIAL {
    cai = cai0
    :	irest = ica : this make simulations depend on end result of prior simulations
    cmax=cai
    ca_tmax=0
}

BREAKPOINT {
    SOLVE integrate METHOD derivimplicit
    if (cai>cmax) {cmax=cai ca_tmax=t}
    dummy=0
}

DERIVATIVE integrate {
    cai' = 2*(irest-ica)/d/F * (1e4) + (cai0 - cai)/tau
}