Example #1
0
def problem():
    mesh = dolfin.UnitCubeMesh(MPI.comm_world, 10, 10, 10)
    cell = mesh.ufl_cell()

    vec_element = dolfin.VectorElement("Lagrange", cell, 1)
    # scl_element = dolfin.FiniteElement("Lagrange", cell, 1)

    Q = dolfin.FunctionSpace(mesh, vec_element)
    # Qs = dolfin.FunctionSpace(mesh, scl_element)

    # Coefficients
    v = dolfin.function.argument.TestFunction(Q)  # Test function
    du = dolfin.function.argument.TrialFunction(Q)  # Incremental displacement
    u = dolfin.Function(Q)  # Displacement from previous iteration

    B = dolfin.Constant((0.0, -0.5, 0.0), cell)  # Body force per unit volume
    T = dolfin.Constant((0.1, 0.0, 0.0),
                        cell)  # Traction force on the boundary

    # B, T = dolfin.Function(Q), dolfin.Function(Q)

    # Kinematics
    d = u.geometric_dimension()
    F = ufl.Identity(d) + grad(u)  # Deformation gradient
    C = F.T * F  # Right Cauchy-Green tensor

    # Invariants of deformation tensors
    Ic = tr(C)
    J = det(F)

    # Elasticity parameters
    E, nu = 10.0, 0.3
    mu = dolfin.Constant(E / (2 * (1 + nu)), cell)
    lmbda = dolfin.Constant(E * nu / ((1 + nu) * (1 - 2 * nu)), cell)

    # mu, lmbda = dolfin.Function(Qs), dolfin.Function(Qs)

    # Stored strain energy density (compressible neo-Hookean model)
    psi = (mu / 2) * (Ic - 3) - mu * ln(J) + (lmbda / 2) * (ln(J))**2

    # Total potential energy
    Pi = psi * dx - dot(B, u) * dx - dot(T, u) * ds

    # Compute first variation of Pi (directional derivative about u in the direction of v)
    F = ufl.derivative(Pi, u, v)

    # Compute Jacobian of F
    J = ufl.derivative(F, u, du)

    return J, F
Example #2
0
def freeEnergy(C, Cv):
    J = sqrt(det(C))
    I1 = tr(C)
    Ce = C * inv(Cv)
    Ie1 = tr(Ce)
    Je = J / sqrt(det(Cv))

    psiEq = (3**(1 - alph1) / (2.0 * alph1) * mu1 * (I1**alph1 - 3**alph1) +
             3**(1 - alph2) / (2.0 * alph2) * mu2 * (I1**alph2 - 3**alph2) -
             (mu1 + mu2) * ln(J) + mu_pr / 2 * (J - 1)**2)

    psiNeq = (3**(1 - a1) / (2.0 * a1) * m1 * (Ie1**a1 - 3**a1) + 3**(1 - a2) /
              (2.0 * a2) * m2 * (Ie1**a2 - 3**a2) - (m1 + m2) * ln(Je))

    return psiEq + psiNeq
Example #3
0
def bessk0(x):
    """
    Modified Bessel function of the second kind.

    Code taken from

    [Flannery et al. 1992] B.P. Flannery,
    W.H. Press, S.A. Teukolsky, W. Vetterling,
    "Numerical recipes in C", Press Syndicate
    of the University of Cambridge, New York
    (1992).
    """
    y1 = x * x / 4.0
    expr1 = -ufl.ln(x / 2.0) * bessi0(x) + (
        -0.57721566 + y1 * (0.42278420 + y1 *
                            (0.23069756 + y1 *
                             (0.3488590e-1 + y1 *
                              (0.262698e-2 + y1 *
                               (0.10750e-3 + y1 * 0.74e-5))))))
    y2 = 2.0 / x
    expr2 = (ufl.exp(-x) / ufl.sqrt(x) *
             (1.25331414 + y2 * (-0.7832358e-1 + y2 *
                                 (0.2189568e-1 + y2 *
                                  (-0.1062446e-1 + y2 *
                                   (0.587872e-2 + y2 *
                                    (-0.251540e-2 + y2 * 0.53208e-3)))))))
    return ufl.conditional(x > 2, expr2, expr1)
Example #4
0
    def F(self, v, s, time=None):
        """
        Right hand side for ODE system
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert(len(s) == 7)
        m, h, j, Cai, d, f, x1 = s

        # Assign parameters
        g_s = self._parameters["g_s"]

        # Init return args
        F_expressions = [ufl.zero()]*7

        # Expressions for the Sodium current m gate component
        alpha_m = (-47 - V)/(-1 + 0.0090952771017*ufl.exp(-0.1*V))
        beta_m = 0.709552672749*ufl.exp(-0.056*V)
        F_expressions[0] = -beta_m*m + (1 - m)*alpha_m

        # Expressions for the Sodium current h gate component
        alpha_h = 5.49796243871e-10*ufl.exp(-0.25*V)
        beta_h = 1.7/(1 + 0.15802532089*ufl.exp(-0.082*V))
        F_expressions[1] = (1 - h)*alpha_h - beta_h*h

        # Expressions for the Sodium current j gate component
        alpha_j = 1.86904730072e-10*ufl.exp(-0.25*V)/(1 +\
            1.67882753e-07*ufl.exp(-0.2*V))
        beta_j = 0.3/(1 + 0.0407622039784*ufl.exp(-0.1*V))
        F_expressions[2] = (1 - j)*alpha_j - beta_j*j

        # Expressions for the Slow inward current component
        E_s = -82.3 - 13.0287*ufl.ln(0.001*Cai)
        i_s = g_s*(-E_s + V)*d*f
        F_expressions[3] = 7e-06 - 0.07*Cai - 0.01*i_s

        # Expressions for the Slow inward current d gate component
        alpha_d = 0.095*ufl.exp(1/20 - V/100)/(1 +\
            1.43328813857*ufl.exp(-0.0719942404608*V))
        beta_d = 0.07*ufl.exp(-44/59 - V/59)/(1 + ufl.exp(11/5 + V/20))
        F_expressions[4] = -beta_d*d + (1 - d)*alpha_d

        # Expressions for the Slow inward current f gate component
        alpha_f = 0.012*ufl.exp(-28/125 - V/125)/(1 +\
            66.5465065251*ufl.exp(0.149925037481*V))
        beta_f = 0.0065*ufl.exp(-3/5 - V/50)/(1 + ufl.exp(-6 - V/5))
        F_expressions[5] = (1 - f)*alpha_f - beta_f*f

        # Expressions for the Time dependent outward current x1 gate component
        alpha_x1 = 0.0311584109863*ufl.exp(0.0826446280992*V)/(1 +\
            17.4117080633*ufl.exp(0.0571428571429*V))
        beta_x1 = 0.000391646440562*ufl.exp(-0.0599880023995*V)/(1 +\
            ufl.exp(-4/5 - V/25))
        F_expressions[6] = (1 - x1)*alpha_x1 - beta_x1*x1

        # Return results
        return dolfin.as_vector(F_expressions)
Example #5
0
def hyperelasticity_action_forms(mesh, vec_el):
    cell = mesh.ufl_cell()

    Q = dolfin.FunctionSpace(mesh, vec_el)

    # Coefficients
    v = dolfin.function.argument.TestFunction(Q)  # Test function
    du = dolfin.function.argument.TrialFunction(Q)  # Incremental displacement
    u = dolfin.Function(Q)  # Displacement from previous iteration
    u.vector().set(0.5)

    B = dolfin.Constant((0.0, -0.5, 0.0), cell)  # Body force per unit volume
    T = dolfin.Constant((0.1, 0.0, 0.0), cell)  # Traction force on the boundary

    # Kinematics
    d = u.geometric_dimension()
    F = ufl.Identity(d) + grad(u)  # Deformation gradient
    C = F.T * F  # Right Cauchy-Green tensor

    # Invariants of deformation tensors
    Ic = tr(C)
    J = det(F)

    # Elasticity parameters
    E, nu = 10.0, 0.3
    mu = dolfin.Constant(E / (2 * (1 + nu)), cell)
    lmbda = dolfin.Constant(E * nu / ((1 + nu) * (1 - 2 * nu)), cell)

    # Stored strain energy density (compressible neo-Hookean model)
    psi = (mu / 2) * (Ic - 3) - mu * ln(J) + (lmbda / 2) * (ln(J)) ** 2

    # Total potential energy
    Pi = psi * dx - dot(B, u) * dx - dot(T, u) * ds

    # Compute first variation of Pi (directional derivative about u in the direction of v)
    F = ufl.derivative(Pi, u, v)

    # Compute Jacobian of F
    J = ufl.derivative(F, u, du)

    w = dolfin.Function(Q)
    w.vector().set(1.2)

    L = ufl.action(J, w)

    return None, L, None
def test_dolfin_expression_compilation_of_math_functions(dolfin):

    # Define some PyDOLFIN coefficients
    mesh = dolfin.UnitSquareMesh(3, 3)
    # Using quadratic element deliberately for accuracy
    V = dolfin.FunctionSpace(mesh, "CG", 2)
    u = dolfin.Function(V)
    u.interpolate(dolfin.Expression("x[0]*x[1]"))
    w0 = u

    # Define ufl expression with math functions
    v = abs(ufl.cos(u))/2 + 0.02
    uexpr = ufl.sin(u) + ufl.tan(v) + ufl.exp(u) + ufl.ln(v) + ufl.atan(v) + ufl.acos(v) + ufl.asin(v)

    #print dolfin.assemble(uexpr**2*dolfin.dx, mesh=mesh) # 11.7846508409

    # Define expected output from compilation
    ucode = 'v_w0[0]'
    vcode = '0.02 + fabs(cos(v_w0[0])) / 2'
    funcs = 'asin(%(v)s) + (acos(%(v)s) + (atan(%(v)s) + (log(%(v)s) + (exp(%(u)s) + (sin(%(u)s) + tan(%(v)s))))))'
    oneliner = funcs % {'u':ucode, 'v':vcode}

    # Oneliner version (ignoring reuse):
    expected_lines = ['double s[1];',
                      'Array<double> v_w0(1);',
                      'w0->eval(v_w0, x);',
                      's[0] = %s;' % oneliner,
                      'values[0] = s[0];']
    #cppcode = format_dolfin_expression(classname="DebugExpression", shape=(), eval_body=expected_lines)
    #print '-'*100
    #print cppcode
    #print '-'*100
    #dolfin.plot(dolfin.Expression(cppcode=cppcode, mesh=mesh))
    #dolfin.interactive()

    # Split version (handles reuse of v, no other reuse):
    expected_lines = ['double s[2];',
                      'Array<double> v_w0(1);',
                      'w0->eval(v_w0, x);',
                      's[0] = %s;' % (vcode,),
                      's[1] = %s;' % (funcs % {'u':ucode,'v':'s[0]'},),
                      'values[0] = s[1];']

    # Define expected evaluation values: [(x,value), (x,value), ...]
    import math
    x, y = 0.6, 0.7
    u = x*y
    v = abs(math.cos(u))/2 + 0.02
    v0 = .52
    expected0 = math.tan(v0) + 1 + math.log(v0) + math.atan(v0) + math.acos(v0) + math.asin(v0)
    expected = math.sin(u) + math.tan(v) + math.exp(u) + math.log(v) + math.atan(v) + math.acos(v) + math.asin(v)
    expected_values = [((0.0, 0.0), (expected0,)),
                       ((x, y), (expected,)),
                       ]

    # Execute all tests
    check_dolfin_expression_compilation(uexpr, expected_lines, expected_values, members={'w0':w0})
Example #7
0
    def ogden_vol(self, params, C):

        kappa = params['kappa']

        psi_vol = (kappa / 4.) * (self.IIIc - 2. * ln(sqrt(self.IIIc)) - 1.)

        S = 2. * diff(psi_vol, C)

        return S
Example #8
0
def test_assign_to_mfs_sub(cg1, vcg1):
    W = cg1*vcg1

    w = Function(W)
    u = Function(cg1)
    v = Function(vcg1)
    u.assign(4)
    v.assign(10)

    w.sub(0).assign(u)

    assert np.allclose(w.sub(0).dat.data_ro, 4)
    assert np.allclose(w.sub(1).dat.data_ro, 0)

    w.sub(1).assign(v)
    assert np.allclose(w.sub(0).dat.data_ro, 4)
    assert np.allclose(w.sub(1).dat.data_ro, 10)

    Q = vcg1*cg1
    q = Function(Q)
    q.assign(11)
    w.sub(1).assign(q.sub(0))

    assert np.allclose(w.sub(1).dat.data_ro, 11)
    assert np.allclose(w.sub(0).dat.data_ro, 4)

    with pytest.raises(ValueError):
        w.sub(1).assign(q.sub(1))

    with pytest.raises(ValueError):
        w.sub(1).assign(w.sub(0))

    with pytest.raises(ValueError):
        w.sub(1).assign(u)

    with pytest.raises(ValueError):
        w.sub(0).assign(v)

    w.sub(0).assign(ufl.ln(q.sub(1)))
    assert np.allclose(w.sub(0).dat.data_ro, ufl.ln(11))

    with pytest.raises(ValueError):
        w.assign(q.sub(1))
Example #9
0
def test_assign_to_mfs_sub(cg1, vcg1):
    W = cg1 * vcg1

    w = Function(W)
    u = Function(cg1)
    v = Function(vcg1)
    u.assign(4)
    v.assign(10)

    w.sub(0).assign(u)

    assert np.allclose(w.sub(0).dat.data_ro, 4)
    assert np.allclose(w.sub(1).dat.data_ro, 0)

    w.sub(1).assign(v)
    assert np.allclose(w.sub(0).dat.data_ro, 4)
    assert np.allclose(w.sub(1).dat.data_ro, 10)

    Q = vcg1 * cg1
    q = Function(Q)
    q.assign(11)
    w.sub(1).assign(q.sub(0))

    assert np.allclose(w.sub(1).dat.data_ro, 11)
    assert np.allclose(w.sub(0).dat.data_ro, 4)

    with pytest.raises(ValueError):
        w.sub(1).assign(q.sub(1))

    with pytest.raises(ValueError):
        w.sub(1).assign(w.sub(0))

    with pytest.raises(ValueError):
        w.sub(1).assign(u)

    with pytest.raises(ValueError):
        w.sub(0).assign(v)

    w.sub(0).assign(ufl.ln(q.sub(1)))
    assert np.allclose(w.sub(0).dat.data_ro, ufl.ln(11))

    with pytest.raises(ValueError):
        w.assign(q.sub(1))
Example #10
0
def test_latex_formatting_of_cmath():
    x = ufl.SpatialCoordinate(ufl.triangle)[0]
    assert expr2latex(ufl.exp(x)) == r"e^{x_0}"
    assert expr2latex(ufl.ln(x)) == r"\ln(x_0)"
    assert expr2latex(ufl.sqrt(x)) == r"\sqrt{x_0}"
    assert expr2latex(abs(x)) == r"\|x_0\|"
    assert expr2latex(ufl.sin(x)) == r"\sin(x_0)"
    assert expr2latex(ufl.cos(x)) == r"\cos(x_0)"
    assert expr2latex(ufl.tan(x)) == r"\tan(x_0)"
    assert expr2latex(ufl.asin(x)) == r"\arcsin(x_0)"
    assert expr2latex(ufl.acos(x)) == r"\arccos(x_0)"
    assert expr2latex(ufl.atan(x)) == r"\arctan(x_0)"
Example #11
0
def test_complex_algebra(self):
    z1 = ComplexValue(1j)
    z2 = ComplexValue(1+1j)

    # Remember that ufl.algebra functions return ComplexValues, but ufl.mathfunctions return complex Python scalar
    # Any operations with a ComplexValue and a complex Python scalar promote to ComplexValue
    assert z1*z2 == ComplexValue(-1+1j)
    assert z2/z1 == ComplexValue(1-1j)
    assert pow(z2, z1) == ComplexValue((1+1j)**1j)
    assert sqrt(z2) * as_ufl(1) == ComplexValue(cmath.sqrt(1+1j))
    assert ((sin(z2) + cosh(z2) - atan(z2)) * z1) == ComplexValue((cmath.sin(1+1j) + cmath.cosh(1+1j) - cmath.atan(1+1j))*1j)
    assert (abs(z2) - ln(z2))/exp(z1) == ComplexValue((abs(1+1j) - cmath.log(1+1j))/cmath.exp(1j))
Example #12
0
def test_cpp_formatting_of_cmath():
    x, y = ufl.SpatialCoordinate(ufl.triangle)
    # Test cmath functions
    assert expr2cpp(ufl.exp(x)) == "exp(x[0])"
    assert expr2cpp(ufl.ln(x)) == "log(x[0])"
    assert expr2cpp(ufl.sqrt(x)) == "sqrt(x[0])"
    assert expr2cpp(abs(x)) == "fabs(x[0])"
    assert expr2cpp(ufl.sin(x)) == "sin(x[0])"
    assert expr2cpp(ufl.cos(x)) == "cos(x[0])"
    assert expr2cpp(ufl.tan(x)) == "tan(x[0])"
    assert expr2cpp(ufl.asin(x)) == "asin(x[0])"
    assert expr2cpp(ufl.acos(x)) == "acos(x[0])"
    assert expr2cpp(ufl.atan(x)) == "atan(x[0])"
Example #13
0
    def _I(self, v, s, time):
        """
        Original gotran transmembrane current dV/dt
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert(len(s) == 7)
        m, h, j, Cai, d, f, x1 = s

        # Assign parameters
        E_Na = self._parameters["E_Na"]
        g_Na = self._parameters["g_Na"]
        g_Nac = self._parameters["g_Nac"]
        g_s = self._parameters["g_s"]
        IstimAmplitude = self._parameters["IstimAmplitude"]
        IstimPulseDuration = self._parameters["IstimPulseDuration"]
        IstimStart = self._parameters["IstimStart"]
        C = self._parameters["C"]

        # Init return args
        current = [ufl.zero()]*1

        # Expressions for the Sodium current component
        i_Na = (g_Nac + g_Na*(m*m*m)*h*j)*(-E_Na + V)

        # Expressions for the Slow inward current component
        E_s = -82.3 - 13.0287*ufl.ln(0.001*Cai)
        i_s = g_s*(-E_s + V)*d*f

        # Expressions for the Time dependent outward current component
        i_x1 = 0.00197277571153*(-1 +\
            21.7584023962*ufl.exp(0.04*V))*ufl.exp(-0.04*V)*x1

        # Expressions for the Time independent outward current component
        i_K1 = 0.0035*(-4 +\
            119.85640019*ufl.exp(0.04*V))/(8.33113748769*ufl.exp(0.04*V) +\
            69.4078518388*ufl.exp(0.08*V)) + 0.0035*(4.6 + 0.2*V)/(1 -\
            0.398519041085*ufl.exp(-0.04*V))

        # Expressions for the Stimulus protocol component
        Istim = ufl.conditional(ufl.And(ufl.ge(time, IstimStart),\
            ufl.le(time, IstimPulseDuration + IstimStart)), IstimAmplitude,\
            0)

        # Expressions for the Membrane component
        current[0] = (-i_K1 + Istim - i_Na - i_x1 - i_s)/C

        # Return results
        return current[0]
Example #14
0
 def V(self, Us):
     """Calculate potential due to this group
     
     Required argument:
     Us: an iterable producing the concentrations of the ligands in the 
     group. These may be numbers, but in the most important applcation
     they are UFL expressions.
     """
     if len(Us) != self.nligands:
         raise KSDGException('wrong number of ligands %d, should be %d' %
                             (len(Us), self.nligands))
     if self.nligands == 0:
         return (0.0)
     sU = sum(l.weight * U for l, U in zip(self.ligands, Us))
     return (-self.beta * ufl.ln(self.alpha + sU))
Example #15
0
def strain_energy(i1, i2, i3):
    """Strain energy function
    i1, i2, i3: principal invariants of the Cauchy-Green tensor
    """
    # Determinant of configuration gradient F
    J = ufl.sqrt(i3)  # noqa: F841
    #
    # Classical St. Venant-Kirchhoff
    # Ψ = la / 8 * (i1 - 3)**2 + mu / 4 * ((i1 - 3)**2 + 4 * (i1 - 3) - 2 * (i2 - 3))
    # Modified St. Venant-Kirchhoff
    # Ψ = la / 2 * (ufl.ln(J))**2 + mu / 4 * ((i1 - 3)**2 + 4 * (i1 - 3) - 2 * (i2 - 3))
    # Compressible neo-Hooke
    Ψ = mu / 2 * (i1 - 3 - 2 * ufl.ln(J)) + la / 2 * (J - 1)**2
    # Compressible Mooney-Rivlin (beta = 0)
    # Ψ = mu / 4 * (i1 - 3) + mu / 4 * (i2 - 3) - mu * ufl.ln(J) + la / 2 * (J - 1)**2
    #
    return Ψ
Example #16
0
 def Vfunc(U):
     return -params['beta'] * ufl.ln(U + params['alpha'])
Example #17
0
def test_diff_then_integrate():

    # Define 1D geometry
    n = 21
    mesh = UnitIntervalMesh(MPI.comm_world, n)

    # Shift and scale mesh
    x0, x1 = 1.5, 3.14
    mesh.coordinates()[:] *= (x1 - x0)
    mesh.coordinates()[:] += x0

    x = SpatialCoordinate(mesh)[0]
    xs = 0.1 + 0.8 * x / x1  # scaled to be within [0.1,0.9]

    # Define list of expressions to test, and configure
    # accuracies these expressions are known to pass with.
    # The reason some functions are less accurately integrated is
    # likely that the default choice of quadrature rule is not perfect
    F_list = []

    def reg(exprs, acc=10):
        for expr in exprs:
            F_list.append((expr, acc))

    # FIXME: 0*dx and 1*dx fails in the ufl-ffc-jit framework somewhere
    # reg([Constant(0.0, cell=cell)])
    # reg([Constant(1.0, cell=cell)])
    monomial_list = [x**q for q in range(2, 6)]
    reg(monomial_list)
    reg([2.3 * p + 4.5 * q for p in monomial_list for q in monomial_list])
    reg([x**x])
    reg([x**(x**2)], 8)
    reg([x**(x**3)], 6)
    reg([x**(x**4)], 2)
    # Special functions:
    reg([atan(xs)], 8)
    reg([sin(x), cos(x), exp(x)], 5)
    reg([ln(xs), pow(x, 2.7), pow(2.7, x)], 3)
    reg([asin(xs), acos(xs)], 1)
    reg([tan(xs)], 7)

    try:
        import scipy
    except ImportError:
        scipy = None

    if hasattr(math, 'erf') or scipy is not None:
        reg([erf(xs)])
    else:
        print(
            "Warning: skipping test of erf, old python version and no scipy.")

    # if 0:
    #     print("Warning: skipping tests of bessel functions, doesn't build on all platforms.")
    # elif scipy is None:
    #     print("Warning: skipping tests of bessel functions, missing scipy.")
    # else:
    #     for nu in (0, 1, 2):
    #         # Many of these are possibly more accurately integrated,
    #         # but 4 covers all and is sufficient for this test
    #         reg([bessel_J(nu, xs), bessel_Y(nu, xs), bessel_I(nu, xs), bessel_K(nu, xs)], 4)

    # To handle tensor algebra, make an x dependent input tensor
    # xx and square all expressions
    def reg2(exprs, acc=10):
        for expr in exprs:
            F_list.append((inner(expr, expr), acc))

    xx = as_matrix([[2 * x**2, 3 * x**3], [11 * x**5, 7 * x**4]])
    x3v = as_vector([3 * x**2, 5 * x**3, 7 * x**4])
    cc = as_matrix([[2, 3], [4, 5]])
    reg2([xx])
    reg2([x3v])
    reg2([cross(3 * x3v, as_vector([-x3v[1], x3v[0], x3v[2]]))])
    reg2([xx.T])
    reg2([tr(xx)])
    reg2([det(xx)])
    reg2([dot(xx, 0.1 * xx)])
    reg2([outer(xx, xx.T)])
    reg2([dev(xx)])
    reg2([sym(xx)])
    reg2([skew(xx)])
    reg2([elem_mult(7 * xx, cc)])
    reg2([elem_div(7 * xx, xx + cc)])
    reg2([elem_pow(1e-3 * xx, 1e-3 * cc)])
    reg2([elem_pow(1e-3 * cc, 1e-3 * xx)])
    reg2([elem_op(lambda z: sin(z) + 2, 0.03 * xx)], 2)  # pretty inaccurate...

    # FIXME: Add tests for all UFL operators:
    # These cause discontinuities and may be harder to test in the
    # above fashion:
    # 'inv', 'cofac',
    # 'eq', 'ne', 'le', 'ge', 'lt', 'gt', 'And', 'Or', 'Not',
    # 'conditional', 'sign',
    # 'jump', 'avg',
    # 'LiftingFunction', 'LiftingOperator',

    # FIXME: Test other derivatives: (but algorithms for operator
    # derivatives are the same!):
    # 'variable', 'diff',
    # 'Dx', 'grad', 'div', 'curl', 'rot', 'Dn', 'exterior_derivative',

    # Run through all operators defined above and compare integrals
    debug = 0
    for F, acc in F_list:
        # Apply UFL differentiation
        f = diff(F, SpatialCoordinate(mesh))[..., 0]
        if debug:
            print(F)
            print(x)
            print(f)

        # Apply integration with DOLFIN
        # (also passes through form compilation and jit)
        M = f * dx
        f_integral = assemble_scalar(M)  # noqa
        f_integral = MPI.sum(mesh.mpi_comm(), f_integral)

        # Compute integral of f manually from anti-derivative F
        # (passes through PyDOLFIN interface and uses UFL evaluation)
        F_diff = F((x1, )) - F((x0, ))

        # Compare results. Using custom relative delta instead
        # of decimal digits here because some numbers are >> 1.
        delta = min(abs(f_integral), abs(F_diff)) * 10**-acc
        assert f_integral - F_diff <= delta
Example #18
0
    def F(self, v, s, time=None):
        """
        Right hand side for ODE system
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert(len(s) == 16)
        Xr1, Xr2, Xs, m, h, j, d, f, fCa, s, r, g, Ca_i, Ca_SR, Na_i, K_i = s

        # Assign parameters
        P_kna = self._parameters["P_kna"]
        g_K1 = self._parameters["g_K1"]
        g_Kr = self._parameters["g_Kr"]
        g_Ks = self._parameters["g_Ks"]
        g_Na = self._parameters["g_Na"]
        g_bna = self._parameters["g_bna"]
        g_CaL = self._parameters["g_CaL"]
        g_bca = self._parameters["g_bca"]
        g_to = self._parameters["g_to"]
        K_mNa = self._parameters["K_mNa"]
        K_mk = self._parameters["K_mk"]
        P_NaK = self._parameters["P_NaK"]
        K_NaCa = self._parameters["K_NaCa"]
        K_sat = self._parameters["K_sat"]
        Km_Ca = self._parameters["Km_Ca"]
        Km_Nai = self._parameters["Km_Nai"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        K_pCa = self._parameters["K_pCa"]
        g_pCa = self._parameters["g_pCa"]
        g_pK = self._parameters["g_pK"]
        Buf_c = self._parameters["Buf_c"]
        Buf_sr = self._parameters["Buf_sr"]
        Ca_o = self._parameters["Ca_o"]
        K_buf_c = self._parameters["K_buf_c"]
        K_buf_sr = self._parameters["K_buf_sr"]
        K_up = self._parameters["K_up"]
        V_leak = self._parameters["V_leak"]
        V_sr = self._parameters["V_sr"]
        Vmax_up = self._parameters["Vmax_up"]
        a_rel = self._parameters["a_rel"]
        b_rel = self._parameters["b_rel"]
        c_rel = self._parameters["c_rel"]
        tau_g = self._parameters["tau_g"]
        Na_o = self._parameters["Na_o"]
        Cm = self._parameters["Cm"]
        F = self._parameters["F"]
        R = self._parameters["R"]
        T = self._parameters["T"]
        V_c = self._parameters["V_c"]
        stim_amplitude = self._parameters["stim_amplitude"]
        stim_duration = self._parameters["stim_duration"]
        stim_period = self._parameters["stim_period"]
        stim_start = self._parameters["stim_start"]
        K_o = self._parameters["K_o"]

        # Init return args
        F_expressions = [ufl.zero()]*16

        # Expressions for the Reversal potentials component
        E_Na = R*T*ufl.ln(Na_o/Na_i)/F
        E_K = R*T*ufl.ln(K_o/K_i)/F
        E_Ks = R*T*ufl.ln((K_o + Na_o*P_kna)/(P_kna*Na_i + K_i))/F
        E_Ca = 0.5*R*T*ufl.ln(Ca_o/Ca_i)/F

        # Expressions for the Inward rectifier potassium current component
        alpha_K1 = 0.1/(1.0 + 6.14421235332821e-06*ufl.exp(0.06*V - 0.06*E_K))
        beta_K1 = (0.36787944117144233*ufl.exp(0.1*V - 0.1*E_K) +\
            3.0606040200802673*ufl.exp(0.0002*V - 0.0002*E_K))/(1.0 +\
            ufl.exp(0.5*E_K - 0.5*V))
        xK1_inf = alpha_K1/(alpha_K1 + beta_K1)
        i_K1 = 0.4303314829119352*g_K1*ufl.sqrt(K_o)*(-E_K + V)*xK1_inf

        # Expressions for the Rapid time dependent potassium current component
        i_Kr = 0.4303314829119352*g_Kr*ufl.sqrt(K_o)*(-E_K + V)*Xr1*Xr2

        # Expressions for the Xr1 gate component
        xr1_inf = 1.0/(1.0 +\
            0.02437284407327961*ufl.exp(-0.14285714285714285*V))
        alpha_xr1 = 450.0/(1.0 + 0.011108996538242306*ufl.exp(-0.1*V))
        beta_xr1 = 6.0/(1.0 +\
            13.581324522578193*ufl.exp(0.08695652173913043*V))
        tau_xr1 = 1.0*alpha_xr1*beta_xr1
        F_expressions[0] = (-Xr1 + xr1_inf)/tau_xr1

        # Expressions for the Xr2 gate component
        xr2_inf = 1.0/(1.0 + 39.12128399815321*ufl.exp(0.041666666666666664*V))
        alpha_xr2 = 3.0/(1.0 + 0.049787068367863944*ufl.exp(-0.05*V))
        beta_xr2 = 1.12/(1.0 + 0.049787068367863944*ufl.exp(0.05*V))
        tau_xr2 = 1.0*alpha_xr2*beta_xr2
        F_expressions[1] = (-Xr2 + xr2_inf)/tau_xr2

        # Expressions for the Slow time dependent potassium current component
        i_Ks = g_Ks*ufl.elem_pow(Xs, 2.0)*(-E_Ks + V)

        # Expressions for the Xs gate component
        xs_inf = 1.0/(1.0 + 0.6996725373751304*ufl.exp(-0.07142857142857142*V))
        alpha_xs = 1100.0/ufl.sqrt(1.0 +\
            0.18887560283756186*ufl.exp(-0.16666666666666666*V))
        beta_xs = 1.0/(1.0 + 0.049787068367863944*ufl.exp(0.05*V))
        tau_xs = 1.0*alpha_xs*beta_xs
        F_expressions[2] = (-Xs + xs_inf)/tau_xs

        # Expressions for the Fast sodium current component
        i_Na = g_Na*ufl.elem_pow(m, 3.0)*(-E_Na + V)*h*j

        # Expressions for the m gate component
        m_inf = 1.0*ufl.elem_pow(1.0 +\
            0.0018422115811651339*ufl.exp(-0.1107419712070875*V), -2.0)
        alpha_m = 1.0/(1.0 + 6.14421235332821e-06*ufl.exp(-0.2*V))
        beta_m = 0.1/(1.0 + 1096.6331584284585*ufl.exp(0.2*V)) + 0.1/(1.0 +\
            0.7788007830714049*ufl.exp(0.005*V))
        tau_m = 1.0*alpha_m*beta_m
        F_expressions[3] = (-m + m_inf)/tau_m

        # Expressions for the h gate component
        h_inf = 1.0*ufl.elem_pow(1.0 +\
            15212.593285654404*ufl.exp(0.13458950201884254*V), -2.0)
        alpha_h = ufl.conditional(ufl.lt(V, -40.0),\
            4.4312679295805147e-07*ufl.exp(-0.14705882352941177*V), 0)
        beta_h = ufl.conditional(ufl.lt(V, -40.0), 310000.0*ufl.exp(0.3485*V)\
            + 2.7*ufl.exp(0.079*V), 0.77/(0.13 +\
            0.049758141083938695*ufl.exp(-0.0900900900900901*V)))
        tau_h = 1.0/(alpha_h + beta_h)
        F_expressions[4] = (-h + h_inf)/tau_h

        # Expressions for the j gate component
        j_inf = 1.0*ufl.elem_pow(1.0 +\
            15212.593285654404*ufl.exp(0.13458950201884254*V), -2.0)
        alpha_j = ufl.conditional(ufl.lt(V, -40.0), 1.0*(37.78 +\
            V)*(-25428.0*ufl.exp(0.2444*V) -\
            6.948e-06*ufl.exp(-0.04391*V))/(1.0 +\
            50262745825.95399*ufl.exp(0.311*V)), 0)
        beta_j = ufl.conditional(ufl.lt(V, -40.0),\
            0.02424*ufl.exp(-0.01052*V)/(1.0 +\
            0.003960868339904256*ufl.exp(-0.1378*V)),\
            0.6*ufl.exp(0.057*V)/(1.0 +\
            0.040762203978366204*ufl.exp(-0.1*V)))
        tau_j = 1.0/(alpha_j + beta_j)
        F_expressions[5] = (-j + j_inf)/tau_j

        # Expressions for the Sodium background current component
        i_b_Na = g_bna*(-E_Na + V)

        # Expressions for the L_type Ca current component
        i_CaL = 4.0*g_CaL*ufl.elem_pow(F, 2.0)*(-0.341*Ca_o +\
            Ca_i*ufl.exp(2.0*F*V/(R*T)))*V*d*f*fCa/(R*T*(-1.0 +\
            ufl.exp(2.0*F*V/(R*T))))

        # Expressions for the d gate component
        d_inf = 1.0/(1.0 + 0.513417119032592*ufl.exp(-0.13333333333333333*V))
        alpha_d = 0.25 + 1.4/(1.0 +\
            0.0677244716592409*ufl.exp(-0.07692307692307693*V))
        beta_d = 1.4/(1.0 + 2.718281828459045*ufl.exp(0.2*V))
        gamma_d = 1.0/(1.0 + 12.182493960703473*ufl.exp(-0.05*V))
        tau_d = 1.0*alpha_d*beta_d + gamma_d
        F_expressions[6] = (-d + d_inf)/tau_d

        # Expressions for the f gate component
        f_inf = 1.0/(1.0 + 17.411708063327644*ufl.exp(0.14285714285714285*V))
        tau_f = 80.0 + 165.0/(1.0 + 12.182493960703473*ufl.exp(-0.1*V)) +\
            1125.0*ufl.exp(-0.004166666666666667*ufl.elem_pow(27.0 + V, 2.0))
        F_expressions[7] = (-f + f_inf)/tau_f

        # Expressions for the FCa gate component
        alpha_fCa = 1.0/(1.0 + 8.03402376701711e+27*ufl.elem_pow(Ca_i, 8.0))
        beta_fCa = 0.1/(1.0 + 0.006737946999085467*ufl.exp(10000.0*Ca_i))
        gama_fCa = 0.2/(1.0 + 0.391605626676799*ufl.exp(1250.0*Ca_i))
        fCa_inf = 0.15753424657534246 + 0.684931506849315*alpha_fCa +\
            0.684931506849315*beta_fCa + 0.684931506849315*gama_fCa
        tau_fCa = 2.0
        d_fCa = (-fCa + fCa_inf)/tau_fCa
        F_expressions[8] = ufl.conditional(ufl.And(ufl.gt(V, -60.0),\
            ufl.gt(fCa_inf, fCa)), 0, d_fCa)

        # Expressions for the Calcium background current component
        i_b_Ca = g_bca*(-E_Ca + V)

        # Expressions for the Transient outward current component
        i_to = g_to*(-E_K + V)*r*s

        # Expressions for the s gate component
        s_inf = 1.0/(1.0 + 54.598150033144236*ufl.exp(0.2*V))
        tau_s = 3.0 + 5.0/(1.0 + 0.01831563888873418*ufl.exp(0.2*V)) +\
            85.0*ufl.exp(-0.003125*ufl.elem_pow(45.0 + V, 2.0))
        F_expressions[9] = (-s + s_inf)/tau_s

        # Expressions for the r gate component
        r_inf = 1.0/(1.0 + 28.031624894526125*ufl.exp(-0.16666666666666666*V))
        tau_r = 0.8 + 9.5*ufl.exp(-0.0005555555555555556*ufl.elem_pow(40.0 +\
            V, 2.0))
        F_expressions[10] = (-r + r_inf)/tau_r

        # Expressions for the Sodium potassium pump current component
        i_NaK = K_o*P_NaK*Na_i/((K_mNa + Na_i)*(K_mk + K_o)*(1.0 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the Sodium calcium exchanger current component
        i_NaCa = K_NaCa*(Ca_o*ufl.elem_pow(Na_i,\
            3.0)*ufl.exp(F*gamma*V/(R*T)) - alpha*ufl.elem_pow(Na_o,\
            3.0)*Ca_i*ufl.exp(F*(-1.0 + gamma)*V/(R*T)))/((1.0 +\
            K_sat*ufl.exp(F*(-1.0 + gamma)*V/(R*T)))*(Ca_o +\
            Km_Ca)*(ufl.elem_pow(Km_Nai, 3.0) + ufl.elem_pow(Na_o, 3.0)))

        # Expressions for the Calcium pump current component
        i_p_Ca = g_pCa*Ca_i/(K_pCa + Ca_i)

        # Expressions for the Potassium pump current component
        i_p_K = g_pK*(-E_K + V)/(1.0 +\
            65.40521574193832*ufl.exp(-0.16722408026755853*V))

        # Expressions for the Calcium dynamics component
        i_rel = (c_rel + a_rel*ufl.elem_pow(Ca_SR, 2.0)/(ufl.elem_pow(b_rel,\
            2.0) + ufl.elem_pow(Ca_SR, 2.0)))*d*g
        i_up = Vmax_up/(1.0 + ufl.elem_pow(K_up, 2.0)*ufl.elem_pow(Ca_i, -2.0))
        i_leak = V_leak*(-Ca_i + Ca_SR)
        g_inf = ufl.conditional(ufl.lt(Ca_i, 0.00035), 1.0/(1.0 +\
            5.439910241481018e+20*ufl.elem_pow(Ca_i, 6.0)), 1.0/(1.0 +\
            1.9720198874049195e+55*ufl.elem_pow(Ca_i, 16.0)))
        d_g = (-g + g_inf)/tau_g
        F_expressions[11] = ufl.conditional(ufl.And(ufl.gt(V, -60.0),\
            ufl.gt(g_inf, g)), 0, d_g)
        Ca_i_bufc = 1.0/(1.0 + Buf_c*K_buf_c*ufl.elem_pow(K_buf_c + Ca_i,\
            -2.0))
        Ca_sr_bufsr = 1.0/(1.0 + Buf_sr*K_buf_sr*ufl.elem_pow(K_buf_sr +\
            Ca_SR, -2.0))
        F_expressions[12] = (-i_up - 0.5*Cm*(1.0*i_CaL + 1.0*i_b_Ca +\
            1.0*i_p_Ca - 2.0*i_NaCa)/(F*V_c) + i_leak + i_rel)*Ca_i_bufc
        F_expressions[13] = V_c*(-i_leak - i_rel + i_up)*Ca_sr_bufsr/V_sr

        # Expressions for the Sodium dynamics component
        F_expressions[14] = 1.0*Cm*(-1.0*i_Na - 1.0*i_b_Na - 3.0*i_NaCa -\
            3.0*i_NaK)/(F*V_c)

        # Expressions for the Membrane component
        i_Stim = ufl.conditional(ufl.And(ufl.ge(time -\
            stim_period*ufl.floor(time/stim_period), stim_start), ufl.le(time\
            - stim_period*ufl.floor(time/stim_period), stim_duration +\
            stim_start)), -stim_amplitude, 0)

        # Expressions for the Potassium dynamics component
        F_expressions[15] = 1.0*Cm*(2.0*i_NaK - 1.0*i_K1 - 1.0*i_Kr -\
            1.0*i_Ks - 1.0*i_Stim - 1.0*i_p_K - 1.0*i_to)/(F*V_c)

        # Return results
        return dolfin.as_vector(F_expressions)
Example #19
0
chi = 0.2
po = Gdry/lamo
muo = Rg*T*(math.log(Omega*cod/(1+Omega*cod))+1/(1+Omega*cod)+ chi/pow((1+Omega*cod),2)) \
+Omega*po
mus = muo
mutop = -10000

# Kinematics
I = Identity(3)
F = I + grad(u)
J = det(F)

# Constitutive equations

S = (Gdry/lamo)*F-p*cofac(F)
mu = Rg*T*(ufl.ln(Omega*Jo*c/(1+Omega*Jo*c)) + 1./(1.+Omega*Jo*c)+ \
         chi/pow((1.+Omega*Jo*c),2))+Omega*p

h = g(c)*grad(c)+(-c*D/(Rg*T))*Omega*grad(p)

dmudp = Omega
dmu = mu-mus

# Boundary conditions

def left_boundary(x, on_boundary): # x = 0
    return on_boundary and abs(x[0]) < DOLFIN_EPS

def back_boundary(x, on_boundary): # y = 0
    return on_boundary and abs(x[1]) < DOLFIN_EPS
Example #20
0
def test_div_grad_then_integrate_over_cells_and_boundary():

    # Define 2D geometry
    n = 10
    mesh = RectangleMesh(Point(0.0, 0.0), Point(2.0, 3.0), 2 * n, 3 * n)

    x, y = SpatialCoordinate(mesh)
    xs = 0.1 + 0.8 * x / 2  # scaled to be within [0.1,0.9]
    #    ys = 0.1 + 0.8 * y / 3  # scaled to be within [0.1,0.9]
    n = FacetNormal(mesh)

    # Define list of expressions to test, and configure accuracies
    # these expressions are known to pass with.  The reason some
    # functions are less accurately integrated is likely that the
    # default choice of quadrature rule is not perfect
    F_list = []

    def reg(exprs, acc=10):
        for expr in exprs:
            F_list.append((expr, acc))

    # FIXME: 0*dx and 1*dx fails in the ufl-ffc-jit framework somewhere
    # reg([Constant(0.0, cell=cell)])
    # reg([Constant(1.0, cell=cell)])
    monomial_list = [x**q for q in range(2, 6)]
    reg(monomial_list)
    reg([2.3 * p + 4.5 * q for p in monomial_list for q in monomial_list])
    reg([xs**xs])
    reg(
        [xs**(xs**2)], 8
    )  # Note: Accuracies here are from 1D case, not checked against 2D results.
    reg([xs**(xs**3)], 6)
    reg([xs**(xs**4)], 2)
    # Special functions:
    reg([atan(xs)], 8)
    reg([sin(x), cos(x), exp(x)], 5)
    reg([ln(xs), pow(x, 2.7), pow(2.7, x)], 3)
    reg([asin(xs), acos(xs)], 1)
    reg([tan(xs)], 7)

    # To handle tensor algebra, make an x dependent input tensor
    # xx and square all expressions
    def reg2(exprs, acc=10):
        for expr in exprs:
            F_list.append((inner(expr, expr), acc))

    xx = as_matrix([[2 * x**2, 3 * x**3], [11 * x**5, 7 * x**4]])
    xxs = as_matrix([[2 * xs**2, 3 * xs**3], [11 * xs**5, 7 * xs**4]])
    x3v = as_vector([3 * x**2, 5 * x**3, 7 * x**4])
    cc = as_matrix([[2, 3], [4, 5]])
    reg2(
        [xx]
    )  # TODO: Make unit test for UFL from this, results in listtensor with free indices
    reg2([x3v])
    reg2([cross(3 * x3v, as_vector([-x3v[1], x3v[0], x3v[2]]))])
    reg2([xx.T])
    reg2([tr(xx)])
    reg2([det(xx)])
    reg2([dot(xx, 0.1 * xx)])
    reg2([outer(xx, xx.T)])
    reg2([dev(xx)])
    reg2([sym(xx)])
    reg2([skew(xx)])
    reg2([elem_mult(7 * xx, cc)])
    reg2([elem_div(7 * xx, xx + cc)])
    reg2([elem_pow(1e-3 * xxs, 1e-3 * cc)])
    reg2([elem_pow(1e-3 * cc, 1e-3 * xx)])
    reg2([elem_op(lambda z: sin(z) + 2, 0.03 * xx)], 2)  # pretty inaccurate...

    # FIXME: Add tests for all UFL operators:
    # These cause discontinuities and may be harder to test in the
    # above fashion:
    # 'inv', 'cofac',
    # 'eq', 'ne', 'le', 'ge', 'lt', 'gt', 'And', 'Or', 'Not',
    # 'conditional', 'sign',
    # 'jump', 'avg',
    # 'LiftingFunction', 'LiftingOperator',

    # FIXME: Test other derivatives: (but algorithms for operator
    # derivatives are the same!):
    # 'variable', 'diff',
    # 'Dx', 'grad', 'div', 'curl', 'rot', 'Dn', 'exterior_derivative',

    # Run through all operators defined above and compare integrals
    debug = 0
    if debug:
        F_list = F_list[1:]

    for F, acc in F_list:
        if debug:
            print('\n', "F:", str(F))

        # Integrate over domain and its boundary
        int_dx = assemble(div(grad(F)) * dx(mesh))  # noqa
        int_ds = assemble(dot(grad(F), n) * ds(mesh))  # noqa

        if debug:
            print(int_dx, int_ds)

        # Compare results. Using custom relative delta instead of
        # decimal digits here because some numbers are >> 1.
        delta = min(abs(int_dx), abs(int_ds)) * 10**-acc
        assert int_dx - int_ds <= delta
Example #21
0
    def F(self, v, s, time=None):
        """
        Right hand side for ODE system
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert (len(s) == 18)
        Xr1, Xr2, Xs, m, h, j, d, f, f2, fCass, s, r, Ca_SR, Ca_i, Ca_ss,\
            R_prime, Na_i, K_i = s

        # Assign parameters
        P_kna = self._parameters["P_kna"]
        g_K1 = self._parameters["g_K1"]
        g_Kr = self._parameters["g_Kr"]
        g_Ks = self._parameters["g_Ks"]
        g_Na = self._parameters["g_Na"]
        g_bna = self._parameters["g_bna"]
        g_CaL = self._parameters["g_CaL"]
        g_bca = self._parameters["g_bca"]
        g_to = self._parameters["g_to"]
        K_mNa = self._parameters["K_mNa"]
        K_mk = self._parameters["K_mk"]
        P_NaK = self._parameters["P_NaK"]
        K_NaCa = self._parameters["K_NaCa"]
        K_sat = self._parameters["K_sat"]
        Km_Ca = self._parameters["Km_Ca"]
        Km_Nai = self._parameters["Km_Nai"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        K_pCa = self._parameters["K_pCa"]
        g_pCa = self._parameters["g_pCa"]
        g_pK = self._parameters["g_pK"]
        Buf_c = self._parameters["Buf_c"]
        Buf_sr = self._parameters["Buf_sr"]
        Buf_ss = self._parameters["Buf_ss"]
        Ca_o = self._parameters["Ca_o"]
        EC = self._parameters["EC"]
        K_buf_c = self._parameters["K_buf_c"]
        K_buf_sr = self._parameters["K_buf_sr"]
        K_buf_ss = self._parameters["K_buf_ss"]
        K_up = self._parameters["K_up"]
        V_leak = self._parameters["V_leak"]
        V_rel = self._parameters["V_rel"]
        V_sr = self._parameters["V_sr"]
        V_ss = self._parameters["V_ss"]
        V_xfer = self._parameters["V_xfer"]
        Vmax_up = self._parameters["Vmax_up"]
        k1_prime = self._parameters["k1_prime"]
        k2_prime = self._parameters["k2_prime"]
        k3 = self._parameters["k3"]
        k4 = self._parameters["k4"]
        max_sr = self._parameters["max_sr"]
        min_sr = self._parameters["min_sr"]
        Na_o = self._parameters["Na_o"]
        Cm = self._parameters["Cm"]
        F = self._parameters["F"]
        R = self._parameters["R"]
        T = self._parameters["T"]
        V_c = self._parameters["V_c"]
        K_o = self._parameters["K_o"]

        # Init return args
        F_expressions = [ufl.zero()] * 18

        # Expressions for the Reversal potentials component
        E_Na = R * T * ufl.ln(Na_o / Na_i) / F
        E_K = R * T * ufl.ln(K_o / K_i) / F
        E_Ks = R * T * ufl.ln((Na_o * P_kna + K_o) / (K_i + P_kna * Na_i)) / F
        E_Ca = 0.5 * R * T * ufl.ln(Ca_o / Ca_i) / F

        # Expressions for the Inward rectifier potassium current component
        alpha_K1 = 0.1 / (1 +
                          6.14421235333e-06 * ufl.exp(-0.06 * E_K + 0.06 * V))
        beta_K1 = (3.06060402008*ufl.exp(0.0002*V - 0.0002*E_K) +\
            0.367879441171*ufl.exp(0.1*V - 0.1*E_K))/(1 + ufl.exp(0.5*E_K -\
            0.5*V))
        xK1_inf = alpha_K1 / (alpha_K1 + beta_K1)
        i_K1 = 0.430331482912 * g_K1 * ufl.sqrt(K_o) * (-E_K + V) * xK1_inf

        # Expressions for the Rapid time dependent potassium current component
        i_Kr = 0.430331482912 * g_Kr * ufl.sqrt(K_o) * (-E_K + V) * Xr1 * Xr2

        # Expressions for the Xr1 gate component
        xr1_inf = 1.0 / (1 + ufl.exp(-26 / 7 - V / 7))
        alpha_xr1 = 450 / (1 + ufl.exp(-9 / 2 - V / 10))
        beta_xr1 = 6 / (1 + 13.5813245226 * ufl.exp(0.0869565217391 * V))
        tau_xr1 = alpha_xr1 * beta_xr1
        F_expressions[0] = (xr1_inf - Xr1) / tau_xr1

        # Expressions for the Xr2 gate component
        xr2_inf = 1.0 / (1 + ufl.exp(11 / 3 + V / 24))
        alpha_xr2 = 3 / (1 + ufl.exp(-3 - V / 20))
        beta_xr2 = 1.12 / (1 + ufl.exp(-3 + V / 20))
        tau_xr2 = alpha_xr2 * beta_xr2
        F_expressions[1] = (xr2_inf - Xr2) / tau_xr2

        # Expressions for the Slow time dependent potassium current component
        i_Ks = g_Ks * (Xs * Xs) * (-E_Ks + V)

        # Expressions for the Xs gate component
        xs_inf = 1.0 / (1 + ufl.exp(-5 / 14 - V / 14))
        alpha_xs = 1400 / ufl.sqrt(1 + ufl.exp(5 / 6 - V / 6))
        beta_xs = 1.0 / (1 + ufl.exp(-7 / 3 + V / 15))
        tau_xs = 80 + alpha_xs * beta_xs
        F_expressions[2] = (xs_inf - Xs) / tau_xs

        # Expressions for the Fast sodium current component
        i_Na = g_Na * (m * m * m) * (-E_Na + V) * h * j

        # Expressions for the m gate component
        m_inf = 1.0/((1 + 0.00184221158117*ufl.exp(-0.110741971207*V))*(1 +\
            0.00184221158117*ufl.exp(-0.110741971207*V)))
        alpha_m = 1.0 / (1 + ufl.exp(-12 - V / 5))
        beta_m = 0.1 / (1 + ufl.exp(-1 / 4 + V / 200)) + 0.1 / (
            1 + ufl.exp(7 + V / 5))
        tau_m = alpha_m * beta_m
        F_expressions[3] = (-m + m_inf) / tau_m

        # Expressions for the h gate component
        h_inf = 1.0/((1 + 15212.5932857*ufl.exp(0.134589502019*V))*(1 +\
            15212.5932857*ufl.exp(0.134589502019*V)))
        alpha_h = ufl.conditional(ufl.lt(V, -40),\
            4.43126792958e-07*ufl.exp(-0.147058823529*V), 0)
        beta_h = ufl.conditional(ufl.lt(V, -40), 2.7*ufl.exp(0.079*V) +\
            310000*ufl.exp(0.3485*V), 0.77/(0.13 +\
            0.0497581410839*ufl.exp(-0.0900900900901*V)))
        tau_h = 1.0 / (alpha_h + beta_h)
        F_expressions[4] = (-h + h_inf) / tau_h

        # Expressions for the j gate component
        j_inf = 1.0/((1 + 15212.5932857*ufl.exp(0.134589502019*V))*(1 +\
            15212.5932857*ufl.exp(0.134589502019*V)))
        alpha_j = ufl.conditional(ufl.lt(V, -40), (37.78 +\
            V)*(-25428*ufl.exp(0.2444*V) - 6.948e-06*ufl.exp(-0.04391*V))/(1 +\
            50262745826.0*ufl.exp(0.311*V)), 0)
        beta_j = ufl.conditional(ufl.lt(V, -40),\
            0.02424*ufl.exp(-0.01052*V)/(1 +\
            0.0039608683399*ufl.exp(-0.1378*V)), 0.6*ufl.exp(0.057*V)/(1 +\
            0.0407622039784*ufl.exp(-0.1*V)))
        tau_j = 1.0 / (alpha_j + beta_j)
        F_expressions[5] = (-j + j_inf) / tau_j

        # Expressions for the Sodium background current component
        i_b_Na = g_bna * (-E_Na + V)

        # Expressions for the L_type Ca current component
        i_CaL = 4*g_CaL*(F*F)*(-15 + V)*(0.25*Ca_ss*ufl.exp(F*(-30 +\
            2*V)/(R*T)) - Ca_o)*d*f*f2*fCass/(R*T*(-1 + ufl.exp(F*(-30 +\
            2*V)/(R*T))))

        # Expressions for the d gate component
        d_inf = 1.0 / (1 + 0.344153786865 * ufl.exp(-0.133333333333 * V))
        alpha_d = 0.25 + 1.4 / (1 + ufl.exp(-35 / 13 - V / 13))
        beta_d = 1.4 / (1 + ufl.exp(1 + V / 5))
        gamma_d = 1.0 / (1 + ufl.exp(5 / 2 - V / 20))
        tau_d = alpha_d * beta_d + gamma_d
        F_expressions[6] = (-d + d_inf) / tau_d

        # Expressions for the f gate component
        f_inf = 1.0 / (1 + ufl.exp(20 / 7 + V / 7))
        tau_f = 20 + 1102.5*ufl.exp(-((27 + V)*(27 + V))/225) + 180/(1 +\
            ufl.exp(3 + V/10)) + 200/(1 + ufl.exp(13/10 - V/10))
        F_expressions[7] = (f_inf - f) / tau_f

        # Expressions for the F2 gate component
        f2_inf = 0.33 + 0.67 / (1 + ufl.exp(5 + V / 7))
        tau_f2 = 80/(1 + ufl.exp(3 + V/10)) + 562*ufl.exp(-((27 + V)*(27 +\
            V))/240) + 31/(1 + ufl.exp(5/2 - V/10))
        F_expressions[8] = (-f2 + f2_inf) / tau_f2

        # Expressions for the FCass gate component
        fCass_inf = 0.4 + 0.6 / (1 + 400.0 * (Ca_ss * Ca_ss))
        tau_fCass = 2 + 80 / (1 + 400.0 * (Ca_ss * Ca_ss))
        F_expressions[9] = (fCass_inf - fCass) / tau_fCass

        # Expressions for the Calcium background current component
        i_b_Ca = g_bca * (-E_Ca + V)

        # Expressions for the Transient outward current component
        i_to = g_to * (-E_K + V) * r * s

        # Expressions for the s gate component
        s_inf = 1.0 / (1 + ufl.exp(4 + V / 5))
        tau_s = 3 + 5/(1 + ufl.exp(-4 + V/5)) + 85*ufl.exp(-((45 + V)*(45 +\
            V))/320)
        F_expressions[10] = (s_inf - s) / tau_s

        # Expressions for the r gate component
        r_inf = 1.0 / (1 + ufl.exp(10 / 3 - V / 6))
        tau_r = 0.8 + 9.5 * ufl.exp(-((40 + V) * (40 + V)) / 1800)
        F_expressions[11] = (r_inf - r) / tau_r

        # Expressions for the Sodium potassium pump current component
        i_NaK = K_o*P_NaK*Na_i/((K_mNa + Na_i)*(K_mk + K_o)*(1 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the Sodium calcium exchanger current component
        i_NaCa = K_NaCa*(-alpha*(Na_o*Na_o*Na_o)*Ca_i*ufl.exp(F*(-1 +\
            gamma)*V/(R*T)) +\
            Ca_o*(Na_i*Na_i*Na_i)*ufl.exp(F*gamma*V/(R*T)))/((1 +\
            K_sat*ufl.exp(F*(-1 + gamma)*V/(R*T)))*(Km_Ca +\
            Ca_o)*((Na_o*Na_o*Na_o) + (Km_Nai*Km_Nai*Km_Nai)))

        # Expressions for the Calcium pump current component
        i_p_Ca = g_pCa * Ca_i / (Ca_i + K_pCa)

        # Expressions for the Potassium pump current component
        i_p_K = g_pK * (-E_K +
                        V) / (1 + 65.4052157419 * ufl.exp(-0.167224080268 * V))

        # Expressions for the Calcium dynamics component
        i_up = Vmax_up / (1 + (K_up * K_up) / (Ca_i * Ca_i))
        i_leak = V_leak * (Ca_SR - Ca_i)
        i_xfer = V_xfer * (Ca_ss - Ca_i)
        kcasr = max_sr - (-min_sr + max_sr) / (1 + (EC * EC) / (Ca_SR * Ca_SR))
        Ca_i_bufc = 1.0 / (1 + Buf_c * K_buf_c / ((Ca_i + K_buf_c) *
                                                  (Ca_i + K_buf_c)))
        Ca_sr_bufsr = 1.0/(1 + Buf_sr*K_buf_sr/((Ca_SR + K_buf_sr)*(Ca_SR +\
            K_buf_sr)))
        Ca_ss_bufss = 1.0/(1 + Buf_ss*K_buf_ss/((Ca_ss + K_buf_ss)*(Ca_ss +\
            K_buf_ss)))
        F_expressions[13] = (i_xfer - Cm*(i_b_Ca + i_p_Ca -\
            2*i_NaCa)/(2*F*V_c) + V_sr*(-i_up + i_leak)/V_c)*Ca_i_bufc
        k1 = k1_prime / kcasr
        k2 = k2_prime * kcasr
        O = (Ca_ss * Ca_ss) * R_prime * k1 / ((Ca_ss * Ca_ss) * k1 + k3)
        F_expressions[15] = -Ca_ss * R_prime * k2 + k4 * (1 - R_prime)
        i_rel = V_rel * (Ca_SR - Ca_ss) * O
        F_expressions[12] = (i_up - i_leak - i_rel) * Ca_sr_bufsr
        F_expressions[14] = (-Cm*i_CaL/(2*F*V_ss) - V_c*i_xfer/V_ss +\
            V_sr*i_rel/V_ss)*Ca_ss_bufss

        # Expressions for the Sodium dynamics component
        F_expressions[16] = Cm * (-i_b_Na - i_Na - 3 * i_NaCa -
                                  3 * i_NaK) / (F * V_c)

        # Expressions for the Membrane component
        i_Stim = 0

        # Expressions for the Potassium dynamics component
        F_expressions[17] = Cm*(2*i_NaK - i_Ks - i_Stim - i_Kr - i_to - i_p_K\
            - i_K1)/(F*V_c)

        # Return results
        return as_vector(F_expressions)
# Before defining the energy density and thus the total potential
# energy, it only remains to specify constants for the elasticity
# parameters::

# Elasticity parameters
E = 10.0
nu = 0.3
mu = E/(2*(1 + nu))
lmbda = E*nu/((1 + nu)*(1 - 2*nu))

# Both the first variation of the potential energy, and the Jacobian of
# the variation, can be automatically computed by a call to
# ``derivative``::

# Stored strain energy density (compressible neo-Hookean model)
psi = (mu/2)*(Ic - 3) - mu*ln(J) + (lmbda/2)*(ln(J))**2

# Total potential energy
Pi = psi*dx  # - inner(B, u)*dx - inner(T, u)*ds

# First variation of Pi (directional derivative about u in the direction of v)
F_form = derivative(Pi, u, v)

# Compute Jacobian of F
J_form = derivative(F_form, u, du)

# Compute Cauchy stress
sigma = (1/J)*diff(psi, F)*F.T

forms = [F_form, J_form]
elements = [(element)]
Example #23
0
    def F(self, v, s, time=None):
        """
        Right hand side for ODE system
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert(len(s) == 16)
        Xr1, Xr2, Xs, m, h, j, d, f, fCa, s, r, Ca_SR, Ca_i, g, Na_i, K_i = s

        # Assign parameters
        P_kna = self._parameters["P_kna"]
        g_K1 = self._parameters["g_K1"]
        g_Kr = self._parameters["g_Kr"]
        g_Ks = self._parameters["g_Ks"]
        g_Na = self._parameters["g_Na"]
        g_bna = self._parameters["g_bna"]
        g_CaL = self._parameters["g_CaL"]
        g_bca = self._parameters["g_bca"]
        g_to = self._parameters["g_to"]
        K_mNa = self._parameters["K_mNa"]
        K_mk = self._parameters["K_mk"]
        P_NaK = self._parameters["P_NaK"]
        K_NaCa = self._parameters["K_NaCa"]
        K_sat = self._parameters["K_sat"]
        Km_Ca = self._parameters["Km_Ca"]
        Km_Nai = self._parameters["Km_Nai"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        K_pCa = self._parameters["K_pCa"]
        g_pCa = self._parameters["g_pCa"]
        g_pK = self._parameters["g_pK"]
        Buf_c = self._parameters["Buf_c"]
        Buf_sr = self._parameters["Buf_sr"]
        Ca_o = self._parameters["Ca_o"]
        K_buf_c = self._parameters["K_buf_c"]
        K_buf_sr = self._parameters["K_buf_sr"]
        K_up = self._parameters["K_up"]
        V_leak = self._parameters["V_leak"]
        V_sr = self._parameters["V_sr"]
        Vmax_up = self._parameters["Vmax_up"]
        a_rel = self._parameters["a_rel"]
        b_rel = self._parameters["b_rel"]
        c_rel = self._parameters["c_rel"]
        tau_g = self._parameters["tau_g"]
        Na_o = self._parameters["Na_o"]
        Cm = self._parameters["Cm"]
        F = self._parameters["F"]
        R = self._parameters["R"]
        T = self._parameters["T"]
        V_c = self._parameters["V_c"]
        stim_amplitude = self._parameters["stim_amplitude"]
        stim_duration = self._parameters["stim_duration"]
        stim_start = self._parameters["stim_start"]
        K_o = self._parameters["K_o"]

        # Init return args
        F_expressions = [ufl.zero()]*16

        # Expressions for the Reversal potentials component
        E_Na = R*T*ufl.ln(Na_o/Na_i)/F
        E_K = R*T*ufl.ln(K_o/K_i)/F
        E_Ks = R*T*ufl.ln((Na_o*P_kna + K_o)/(K_i + P_kna*Na_i))/F
        E_Ca = 0.5*R*T*ufl.ln(Ca_o/Ca_i)/F

        # Expressions for the Inward rectifier potassium current component
        alpha_K1 = 0.1/(1 + 6.14421235333e-06*ufl.exp(-0.06*E_K + 0.06*V))
        beta_K1 = (3.06060402008*ufl.exp(0.0002*V - 0.0002*E_K) +\
            0.367879441171*ufl.exp(0.1*V - 0.1*E_K))/(1 + ufl.exp(0.5*E_K -\
            0.5*V))
        xK1_inf = alpha_K1/(alpha_K1 + beta_K1)
        i_K1 = 0.430331482912*g_K1*ufl.sqrt(K_o)*(-E_K + V)*xK1_inf

        # Expressions for the Rapid time dependent potassium current component
        i_Kr = 0.430331482912*g_Kr*ufl.sqrt(K_o)*(-E_K + V)*Xr1*Xr2

        # Expressions for the Xr1 gate component
        xr1_inf = 1.0/(1 + ufl.exp(-26/7 - V/7))
        alpha_xr1 = 450/(1 + ufl.exp(-9/2 - V/10))
        beta_xr1 = 6/(1 + 13.5813245226*ufl.exp(0.0869565217391*V))
        tau_xr1 = alpha_xr1*beta_xr1
        F_expressions[0] = (xr1_inf - Xr1)/tau_xr1

        # Expressions for the Xr2 gate component
        xr2_inf = 1.0/(1 + ufl.exp(11/3 + V/24))
        alpha_xr2 = 3/(1 + ufl.exp(-3 - V/20))
        beta_xr2 = 1.12/(1 + ufl.exp(-3 + V/20))
        tau_xr2 = alpha_xr2*beta_xr2
        F_expressions[1] = (xr2_inf - Xr2)/tau_xr2

        # Expressions for the Slow time dependent potassium current component
        i_Ks = g_Ks*(Xs*Xs)*(-E_Ks + V)

        # Expressions for the Xs gate component
        xs_inf = 1.0/(1 + ufl.exp(-5/14 - V/14))
        alpha_xs = 1100/ufl.sqrt(1 + ufl.exp(-5/3 - V/6))
        beta_xs = 1.0/(1 + ufl.exp(-3 + V/20))
        tau_xs = alpha_xs*beta_xs
        F_expressions[2] = (xs_inf - Xs)/tau_xs

        # Expressions for the Fast sodium current component
        i_Na = g_Na*(m*m*m)*(-E_Na + V)*h*j

        # Expressions for the m gate component
        m_inf = 1.0/((1 + 0.00184221158117*ufl.exp(-0.110741971207*V))*(1 +\
            0.00184221158117*ufl.exp(-0.110741971207*V)))
        alpha_m = 1.0/(1 + ufl.exp(-12 - V/5))
        beta_m = 0.1/(1 + ufl.exp(-1/4 + V/200)) + 0.1/(1 + ufl.exp(7 + V/5))
        tau_m = alpha_m*beta_m
        F_expressions[3] = (-m + m_inf)/tau_m

        # Expressions for the h gate component
        h_inf = 1.0/((1 + 15212.5932857*ufl.exp(0.134589502019*V))*(1 +\
            15212.5932857*ufl.exp(0.134589502019*V)))
        alpha_h = 4.43126792958e-07*ufl.exp(-0.147058823529*V)/(1 +\
            2.35385266837e+17*ufl.exp(1.0*V))
        beta_h = (2.7*ufl.exp(0.079*V) + 310000*ufl.exp(0.3485*V))/(1 +\
            2.35385266837e+17*ufl.exp(1.0*V)) + 0.77*(1 - 1/(1 +\
            2.35385266837e+17*ufl.exp(1.0*V)))/(0.13 +\
            0.0497581410839*ufl.exp(-0.0900900900901*V))
        tau_h = 1.0/(alpha_h + beta_h)
        F_expressions[4] = (-h + h_inf)/tau_h

        # Expressions for the j gate component
        j_inf = 1.0/((1 + 15212.5932857*ufl.exp(0.134589502019*V))*(1 +\
            15212.5932857*ufl.exp(0.134589502019*V)))
        alpha_j = (37.78 + V)*(-25428*ufl.exp(0.2444*V) -\
            6.948e-06*ufl.exp(-0.04391*V))/((1 +\
            2.35385266837e+17*ufl.exp(1.0*V))*(1 +\
            50262745826.0*ufl.exp(0.311*V)))
        beta_j = 0.6*(1 - 1/(1 +\
            2.35385266837e+17*ufl.exp(1.0*V)))*ufl.exp(0.057*V)/(1 +\
            0.0407622039784*ufl.exp(-0.1*V)) +\
            0.02424*ufl.exp(-0.01052*V)/((1 +\
            2.35385266837e+17*ufl.exp(1.0*V))*(1 +\
            0.0039608683399*ufl.exp(-0.1378*V)))
        tau_j = 1.0/(alpha_j + beta_j)
        F_expressions[5] = (-j + j_inf)/tau_j

        # Expressions for the Sodium background current component
        i_b_Na = g_bna*(-E_Na + V)

        # Expressions for the L_type Ca current component
        i_CaL = 4*g_CaL*(F*F)*(Ca_i*ufl.exp(2*F*V/(R*T)) -\
            0.341*Ca_o)*V*d*f*fCa/(R*T*(-1 + ufl.exp(2*F*V/(R*T))))

        # Expressions for the d gate component
        d_inf = 1.0/(1 + 0.513417119033*ufl.exp(-0.133333333333*V))
        alpha_d = 0.25 + 1.4/(1 + ufl.exp(-35/13 - V/13))
        beta_d = 1.4/(1 + ufl.exp(1 + V/5))
        gamma_d = 1.0/(1 + ufl.exp(5/2 - V/20))
        tau_d = alpha_d*beta_d + gamma_d
        F_expressions[6] = (-d + d_inf)/tau_d

        # Expressions for the f gate component
        f_inf = 1.0/(1 + ufl.exp(20/7 + V/7))
        tau_f = 80 + 1125*ufl.exp(-((27 + V)*(27 + V))/240) + 165/(1 +\
            ufl.exp(5/2 - V/10))
        F_expressions[7] = (f_inf - f)/tau_f

        # Expressions for the FCa gate component
        alpha_fCa = 1.0/(1 + 8.03402376702e+27*ufl.elem_pow(Ca_i, 8))
        exp_arg_0 = -5.0 + 10000.0*Ca_i
        exp_arg_00 = ufl.conditional(ufl.lt(exp_arg_0, 500.0), exp_arg_0,\
            500.0)
        beta_fCa = 0.1/(1 + ufl.exp(exp_arg_00))
        exp_arg_1 = -0.9375 + 1250.0*Ca_i
        exp_arg_11 = ufl.conditional(ufl.lt(exp_arg_1, 500.0), exp_arg_1,\
            500.0)
        gama_fCa = 0.2/(1 + ufl.exp(exp_arg_11))
        fCa_inf = 0.157534246575 + 0.684931506849*gama_fCa +\
            0.684931506849*alpha_fCa + 0.684931506849*beta_fCa
        tau_fCa = 2
        d_fCa = (-fCa + fCa_inf)/tau_fCa
        F_expressions[8] = ufl.conditional(ufl.And(ufl.gt(V, -60),\
            ufl.gt(fCa_inf, fCa)), 0, d_fCa)

        # Expressions for the Calcium background current component
        i_b_Ca = g_bca*(-E_Ca + V)

        # Expressions for the Transient outward current component
        i_to = g_to*(-E_K + V)*r*s

        # Expressions for the s gate component
        s_inf = 1.0/(1 + ufl.exp(4 + V/5))
        tau_s = 3 + 5/(1 + ufl.exp(-4 + V/5)) + 85*ufl.exp(-((45 + V)*(45 +\
            V))/320)
        F_expressions[9] = (s_inf - s)/tau_s

        # Expressions for the r gate component
        r_inf = 1.0/(1 + ufl.exp(10/3 - V/6))
        tau_r = 0.8 + 9.5*ufl.exp(-((40 + V)*(40 + V))/1800)
        F_expressions[10] = (r_inf - r)/tau_r

        # Expressions for the Sodium potassium pump current component
        i_NaK = K_o*P_NaK*Na_i/((K_mNa + Na_i)*(K_mk + K_o)*(1 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the Sodium calcium exchanger current component
        i_NaCa = K_NaCa*(-alpha*(Na_o*Na_o*Na_o)*Ca_i*ufl.exp(F*(-1 +\
            gamma)*V/(R*T)) +\
            Ca_o*(Na_i*Na_i*Na_i)*ufl.exp(F*gamma*V/(R*T)))/((1 +\
            K_sat*ufl.exp(F*(-1 + gamma)*V/(R*T)))*(Km_Ca +\
            Ca_o)*((Na_o*Na_o*Na_o) + (Km_Nai*Km_Nai*Km_Nai)))

        # Expressions for the Calcium pump current component
        i_p_Ca = g_pCa*Ca_i/(Ca_i + K_pCa)

        # Expressions for the Potassium pump current component
        i_p_K = g_pK*(-E_K + V)/(1 + 65.4052157419*ufl.exp(-0.167224080268*V))

        # Expressions for the Calcium dynamics component
        i_rel = (c_rel + a_rel*(Ca_SR*Ca_SR)/((b_rel*b_rel) +\
            (Ca_SR*Ca_SR)))*d*g
        i_up = Vmax_up/(1 + (K_up*K_up)/(Ca_i*Ca_i))
        i_leak = V_leak*(Ca_SR - Ca_i)
        g_inf = 1/((1 + 0.0301973834223*ufl.exp(10000.0*Ca_i))*(1 +\
            5.43991024148e+20*ufl.elem_pow(Ca_i, 6))) + (1 - 1/(1 +\
            0.0301973834223*ufl.exp(10000.0*Ca_i)))/(1 +\
            1.9720198874e+55*ufl.elem_pow(Ca_i, 16))
        d_g = (-g + g_inf)/tau_g
        F_expressions[13] = (1 - 1.0/((1 + ufl.exp(60 + V))*(1 +\
            ufl.exp(10.0*g_inf - 10.0*g))))*d_g
        Ca_i_bufc = 1.0/(1 + Buf_c*K_buf_c/((Ca_i + K_buf_c)*(Ca_i + K_buf_c)))
        Ca_sr_bufsr = 1.0/(1 + Buf_sr*K_buf_sr/((Ca_SR + K_buf_sr)*(Ca_SR +\
            K_buf_sr)))
        F_expressions[12] = (i_rel - i_up - Cm*(i_b_Ca + i_p_Ca - 2*i_NaCa +\
            i_CaL)/(2*F*V_c) + i_leak)*Ca_i_bufc
        F_expressions[11] = V_c*(i_up - i_leak - i_rel)*Ca_sr_bufsr/V_sr

        # Expressions for the Sodium dynamics component
        F_expressions[14] = Cm*(-i_b_Na - i_Na - 3*i_NaCa - 3*i_NaK)/(F*V_c)

        # Expressions for the Membrane component
        i_Stim = -stim_amplitude*(1 - 1/(1 + ufl.exp(5.0*time -\
            5.0*stim_start)))/(1 + ufl.exp(5.0*time - 5.0*stim_start -\
            5.0*stim_duration))

        # Expressions for the Potassium dynamics component
        F_expressions[15] = Cm*(2*i_NaK - i_Ks - i_Kr - i_to - i_Stim - i_p_K\
            - i_K1)/(F*V_c)

        # Return results
        return dolfin.as_vector(F_expressions)
Example #24
0
    def _I(self, v, s, time):
        """
        Original gotran transmembrane current dV/dt
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert(len(s) == 16)
        Xr1, Xr2, Xs, m, h, j, d, f, fCa, s, r, g, Ca_i, Ca_SR, Na_i, K_i = s

        # Assign parameters
        P_kna = self._parameters["P_kna"]
        g_K1 = self._parameters["g_K1"]
        g_Kr = self._parameters["g_Kr"]
        g_Ks = self._parameters["g_Ks"]
        g_Na = self._parameters["g_Na"]
        g_bna = self._parameters["g_bna"]
        g_CaL = self._parameters["g_CaL"]
        g_bca = self._parameters["g_bca"]
        g_to = self._parameters["g_to"]
        K_mNa = self._parameters["K_mNa"]
        K_mk = self._parameters["K_mk"]
        P_NaK = self._parameters["P_NaK"]
        K_NaCa = self._parameters["K_NaCa"]
        K_sat = self._parameters["K_sat"]
        Km_Ca = self._parameters["Km_Ca"]
        Km_Nai = self._parameters["Km_Nai"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        K_pCa = self._parameters["K_pCa"]
        g_pCa = self._parameters["g_pCa"]
        g_pK = self._parameters["g_pK"]
        Ca_o = self._parameters["Ca_o"]
        Na_o = self._parameters["Na_o"]
        F = self._parameters["F"]
        R = self._parameters["R"]
        T = self._parameters["T"]
        stim_amplitude = self._parameters["stim_amplitude"]
        stim_duration = self._parameters["stim_duration"]
        stim_period = self._parameters["stim_period"]
        stim_start = self._parameters["stim_start"]
        K_o = self._parameters["K_o"]

        # Init return args
        current = [ufl.zero()]*1

        # Expressions for the Reversal potentials component
        E_Na = R*T*ufl.ln(Na_o/Na_i)/F
        E_K = R*T*ufl.ln(K_o/K_i)/F
        E_Ks = R*T*ufl.ln((K_o + Na_o*P_kna)/(P_kna*Na_i + K_i))/F
        E_Ca = 0.5*R*T*ufl.ln(Ca_o/Ca_i)/F

        # Expressions for the Inward rectifier potassium current component
        alpha_K1 = 0.1/(1.0 + 6.14421235332821e-06*ufl.exp(0.06*V - 0.06*E_K))
        beta_K1 = (0.36787944117144233*ufl.exp(0.1*V - 0.1*E_K) +\
            3.0606040200802673*ufl.exp(0.0002*V - 0.0002*E_K))/(1.0 +\
            ufl.exp(0.5*E_K - 0.5*V))
        xK1_inf = alpha_K1/(alpha_K1 + beta_K1)
        i_K1 = 0.4303314829119352*g_K1*ufl.sqrt(K_o)*(-E_K + V)*xK1_inf

        # Expressions for the Rapid time dependent potassium current component
        i_Kr = 0.4303314829119352*g_Kr*ufl.sqrt(K_o)*(-E_K + V)*Xr1*Xr2

        # Expressions for the Slow time dependent potassium current component
        i_Ks = g_Ks*ufl.elem_pow(Xs, 2.0)*(-E_Ks + V)

        # Expressions for the Fast sodium current component
        i_Na = g_Na*ufl.elem_pow(m, 3.0)*(-E_Na + V)*h*j

        # Expressions for the Sodium background current component
        i_b_Na = g_bna*(-E_Na + V)

        # Expressions for the L_type Ca current component
        i_CaL = 4.0*g_CaL*ufl.elem_pow(F, 2.0)*(-0.341*Ca_o +\
            Ca_i*ufl.exp(2.0*F*V/(R*T)))*V*d*f*fCa/(R*T*(-1.0 +\
            ufl.exp(2.0*F*V/(R*T))))

        # Expressions for the Calcium background current component
        i_b_Ca = g_bca*(-E_Ca + V)

        # Expressions for the Transient outward current component
        i_to = g_to*(-E_K + V)*r*s

        # Expressions for the Sodium potassium pump current component
        i_NaK = K_o*P_NaK*Na_i/((K_mNa + Na_i)*(K_mk + K_o)*(1.0 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the Sodium calcium exchanger current component
        i_NaCa = K_NaCa*(Ca_o*ufl.elem_pow(Na_i,\
            3.0)*ufl.exp(F*gamma*V/(R*T)) - alpha*ufl.elem_pow(Na_o,\
            3.0)*Ca_i*ufl.exp(F*(-1.0 + gamma)*V/(R*T)))/((1.0 +\
            K_sat*ufl.exp(F*(-1.0 + gamma)*V/(R*T)))*(Ca_o +\
            Km_Ca)*(ufl.elem_pow(Km_Nai, 3.0) + ufl.elem_pow(Na_o, 3.0)))

        # Expressions for the Calcium pump current component
        i_p_Ca = g_pCa*Ca_i/(K_pCa + Ca_i)

        # Expressions for the Potassium pump current component
        i_p_K = g_pK*(-E_K + V)/(1.0 +\
            65.40521574193832*ufl.exp(-0.16722408026755853*V))

        # Expressions for the Membrane component
        i_Stim = ufl.conditional(ufl.And(ufl.ge(time -\
            stim_period*ufl.floor(time/stim_period), stim_start), ufl.le(time\
            - stim_period*ufl.floor(time/stim_period), stim_duration +\
            stim_start)), -stim_amplitude, 0)
        current[0] = -1.0*i_CaL - 1.0*i_K1 - 1.0*i_Kr - 1.0*i_Ks - 1.0*i_Na -\
            1.0*i_NaCa - 1.0*i_NaK - 1.0*i_Stim - 1.0*i_b_Ca - 1.0*i_b_Na -\
            1.0*i_p_Ca - 1.0*i_p_K - 1.0*i_to

        # Return results
        return current[0]
Example #25
0
    def F(self, v, s, time=None):
        """
        Right hand side for ODE system
        """
        time = time if time else Constant(0.0)

        # Assign states
        V_m = v
        assert (len(s) == 38)
        h, j, m, x_kr, x_ks, x_to_f, x_to_s, y_to_f, y_to_s, d, f, f_Ca_Bj,\
            f_Ca_Bsl, Ry_Ri, Ry_Ro, Ry_Rr, Na_Bj, Na_Bsl, CaM, Myo_c, Myo_m,\
            SRB, Tn_CHc, Tn_CHm, Tn_CL, SLH_j, SLH_sl, SLL_j, SLL_sl, Ca_sr,\
            Csqn_b, Na_i, Na_j, Na_sl, K_i, Ca_i, Ca_j, Ca_sl = s

        # Assign parameters
        Fjunc = self._parameters["Fjunc"]
        Fjunc_CaL = self._parameters["Fjunc_CaL"]
        cellLength = self._parameters["cellLength"]
        cellRadius = self._parameters["cellRadius"]
        GNa = self._parameters["GNa"]
        GNaB = self._parameters["GNaB"]
        IbarNaK = self._parameters["IbarNaK"]
        KmKo = self._parameters["KmKo"]
        KmNaip = self._parameters["KmNaip"]
        Q10CaL = self._parameters["Q10CaL"]
        pCa = self._parameters["pCa"]
        pNa = self._parameters["pNa"]
        IbarNCX = self._parameters["IbarNCX"]
        Kdact = self._parameters["Kdact"]
        KmCai = self._parameters["KmCai"]
        KmCao = self._parameters["KmCao"]
        KmNai = self._parameters["KmNai"]
        KmNao = self._parameters["KmNao"]
        Q10NCX = self._parameters["Q10NCX"]
        ksat = self._parameters["ksat"]
        nu = self._parameters["nu"]
        IbarSLCaP = self._parameters["IbarSLCaP"]
        KmPCa = self._parameters["KmPCa"]
        Q10SLCaP = self._parameters["Q10SLCaP"]
        GCaB = self._parameters["GCaB"]
        Kmf = self._parameters["Kmf"]
        Kmr = self._parameters["Kmr"]
        MaxSR = self._parameters["MaxSR"]
        MinSR = self._parameters["MinSR"]
        Q10SRCaP = self._parameters["Q10SRCaP"]
        Vmax_SRCaP = self._parameters["Vmax_SRCaP"]
        ec50SR = self._parameters["ec50SR"]
        hillSRCaP = self._parameters["hillSRCaP"]
        kiCa = self._parameters["kiCa"]
        kim = self._parameters["kim"]
        koCa = self._parameters["koCa"]
        kom = self._parameters["kom"]
        ks = self._parameters["ks"]
        Bmax_Naj = self._parameters["Bmax_Naj"]
        Bmax_Nasl = self._parameters["Bmax_Nasl"]
        koff_na = self._parameters["koff_na"]
        kon_na = self._parameters["kon_na"]
        Bmax_CaM = self._parameters["Bmax_CaM"]
        Bmax_SR = self._parameters["Bmax_SR"]
        Bmax_TnChigh = self._parameters["Bmax_TnChigh"]
        Bmax_TnClow = self._parameters["Bmax_TnClow"]
        Bmax_myosin = self._parameters["Bmax_myosin"]
        koff_cam = self._parameters["koff_cam"]
        koff_myoca = self._parameters["koff_myoca"]
        koff_myomg = self._parameters["koff_myomg"]
        koff_sr = self._parameters["koff_sr"]
        koff_tnchca = self._parameters["koff_tnchca"]
        koff_tnchmg = self._parameters["koff_tnchmg"]
        koff_tncl = self._parameters["koff_tncl"]
        kon_cam = self._parameters["kon_cam"]
        kon_myoca = self._parameters["kon_myoca"]
        kon_myomg = self._parameters["kon_myomg"]
        kon_sr = self._parameters["kon_sr"]
        kon_tnchca = self._parameters["kon_tnchca"]
        kon_tnchmg = self._parameters["kon_tnchmg"]
        kon_tncl = self._parameters["kon_tncl"]
        Bmax_SLhighj0 = self._parameters["Bmax_SLhighj0"]
        Bmax_SLhighsl0 = self._parameters["Bmax_SLhighsl0"]
        Bmax_SLlowj0 = self._parameters["Bmax_SLlowj0"]
        Bmax_SLlowsl0 = self._parameters["Bmax_SLlowsl0"]
        koff_slh = self._parameters["koff_slh"]
        koff_sll = self._parameters["koff_sll"]
        kon_slh = self._parameters["kon_slh"]
        kon_sll = self._parameters["kon_sll"]
        Bmax_Csqn0 = self._parameters["Bmax_Csqn0"]
        J_ca_juncsl = self._parameters["J_ca_juncsl"]
        J_ca_slmyo = self._parameters["J_ca_slmyo"]
        koff_csqn = self._parameters["koff_csqn"]
        kon_csqn = self._parameters["kon_csqn"]
        J_na_juncsl = self._parameters["J_na_juncsl"]
        J_na_slmyo = self._parameters["J_na_slmyo"]
        Nao = self._parameters["Nao"]
        Ko = self._parameters["Ko"]
        Cao = self._parameters["Cao"]
        Mgi = self._parameters["Mgi"]
        Cmem = self._parameters["Cmem"]
        Frdy = self._parameters["Frdy"]
        R = self._parameters["R"]
        Temp = self._parameters["Temp"]
        g_K1_factor = self._parameters["g_K1_factor"]
        g_CaL_factor = self._parameters["g_CaL_factor"]
        g_Kr_factor = self._parameters["g_Kr_factor"]
        g_Ks_factor = self._parameters["g_Ks_factor"]
        g_to_factor = self._parameters["g_to_factor"]
        SR_Ca_release_ks_factor = self._parameters["SR_Ca_release_ks_factor"]

        # Init return args
        F_expressions = [ufl.zero()] * 38

        # Expressions for the Geometry component
        Vcell = 1e-15 * ufl.pi * cellLength * (cellRadius * cellRadius)
        Vmyo = 0.65 * Vcell
        Vsr = 0.035 * Vcell
        Vsl = 0.02 * Vcell
        Vjunc = 0.000539 * Vcell
        Fsl = 1 - Fjunc
        Fsl_CaL = 1 - Fjunc_CaL

        # Expressions for the Reversal potentials component
        FoRT = Frdy / (R * Temp)
        ena_junc = ufl.ln(Nao / Na_j) / FoRT
        ena_sl = ufl.ln(Nao / Na_sl) / FoRT
        eca_junc = ufl.ln(Cao / Ca_j) / (2 * FoRT)
        eca_sl = ufl.ln(Cao / Ca_sl) / (2 * FoRT)
        Qpow = -31 + Temp / 10

        # Expressions for the I_Na component
        mss = 1.0/((1 + 0.00184221158117*ufl.exp(-0.110741971207*V_m))*(1 +\
            0.00184221158117*ufl.exp(-0.110741971207*V_m)))
        taum = 0.1292*ufl.exp(-((2.94658944659 +\
            0.0643500643501*V_m)*(2.94658944659 + 0.0643500643501*V_m))) +\
            0.06487*ufl.exp(-((-0.0943466353678 +\
            0.0195618153365*V_m)*(-0.0943466353678 + 0.0195618153365*V_m)))
        ah = ufl.conditional(ufl.ge(V_m, -40), 0,\
            4.43126792958e-07*ufl.exp(-0.147058823529*V_m))
        bh = ufl.conditional(ufl.ge(V_m, -40), 0.77/(0.13 +\
            0.0497581410839*ufl.exp(-0.0900900900901*V_m)),\
            310000.0*ufl.exp(0.3485*V_m) + 2.7*ufl.exp(0.079*V_m))
        tauh = 1.0 / (bh + ah)
        hss = 1.0/((1 + 15212.5932857*ufl.exp(0.134589502019*V_m))*(1 +\
            15212.5932857*ufl.exp(0.134589502019*V_m)))
        aj = ufl.conditional(ufl.ge(V_m, -40), 0, (37.78 +\
            V_m)*(-25428.0*ufl.exp(0.2444*V_m) -\
            6.948e-06*ufl.exp(-0.04391*V_m))/(1 +\
            50262745826.0*ufl.exp(0.311*V_m)))
        bj = ufl.conditional(ufl.ge(V_m, -40), 0.6*ufl.exp(0.057*V_m)/(1 +\
            0.0407622039784*ufl.exp(-0.1*V_m)),\
            0.02424*ufl.exp(-0.01052*V_m)/(1 +\
            0.0039608683399*ufl.exp(-0.1378*V_m)))
        tauj = 1.0 / (bj + aj)
        jss = 1.0/((1 + 15212.5932857*ufl.exp(0.134589502019*V_m))*(1 +\
            15212.5932857*ufl.exp(0.134589502019*V_m)))
        F_expressions[2] = (-m + mss) / taum
        F_expressions[0] = (hss - h) / tauh
        F_expressions[1] = (-j + jss) / tauj
        I_Na_junc = Fjunc * GNa * (m * m * m) * (-ena_junc + V_m) * h * j
        I_Na_sl = GNa * (m * m * m) * (-ena_sl + V_m) * Fsl * h * j

        # Expressions for the I_NaBK component
        I_nabk_junc = Fjunc * GNaB * (-ena_junc + V_m)
        I_nabk_sl = GNaB * (-ena_sl + V_m) * Fsl

        # Expressions for the I_NaK component
        sigma = -1 / 7 + ufl.exp(0.0148588410104 * Nao) / 7
        fnak = 1.0/(1 + 0.1245*ufl.exp(-0.1*FoRT*V_m) +\
            0.0365*ufl.exp(-FoRT*V_m)*sigma)
        I_nak_junc = Fjunc*IbarNaK*Ko*fnak/((1 + ufl.elem_pow(KmNaip,\
            4)/ufl.elem_pow(Na_j, 4))*(KmKo + Ko))
        I_nak_sl = IbarNaK*Ko*Fsl*fnak/((1 + ufl.elem_pow(KmNaip,\
            4)/ufl.elem_pow(Na_sl, 4))*(KmKo + Ko))

        # Expressions for the I_Kr component
        xrss = 1.0 / (1 + ufl.exp(-2 - V_m / 5))
        tauxr = 230/(1 + ufl.exp(2 + V_m/20)) + 3300/((1 + ufl.exp(-22/9 -\
            V_m/9))*(1 + ufl.exp(11/9 + V_m/9)))
        F_expressions[3] = (-x_kr + xrss) / tauxr

        # Expressions for the I_Ks component
        xsss = 1.0 / (1 + 0.765928338365 * ufl.exp(-0.0701754385965 * V_m))
        tauxs = 990.1 / (1 + 0.841540408868 * ufl.exp(-0.070821529745 * V_m))
        F_expressions[4] = (-x_ks + xsss) / tauxs

        # Expressions for the I_to component
        xtoss = 1.0 / (1 + ufl.exp(19 / 13 - V_m / 13))
        ytoss = 1.0 / (1 + 49.4024491055 * ufl.exp(V_m / 5))
        tauxtos = 0.5 + 9 / (1 + ufl.exp(1 / 5 + V_m / 15))
        tauytos = 30 + 800 / (1 + ufl.exp(6 + V_m / 10))
        F_expressions[6] = (-x_to_s + xtoss) / tauxtos
        F_expressions[8] = (ytoss - y_to_s) / tauytos
        tauxtof = 0.5 + 8.5 * ufl.exp(-((9 / 10 + V_m / 50) *
                                        (9 / 10 + V_m / 50)))
        tauytof = 7 + 85 * ufl.exp(-((40 + V_m) * (40 + V_m)) / 220)
        F_expressions[5] = (xtoss - x_to_f) / tauxtof
        F_expressions[7] = (ytoss - y_to_f) / tauytof

        # Expressions for the I_Ca component
        fss = 0.6 / (1 + ufl.exp(5 / 2 - V_m / 20)) + 1.0 / (
            1 + ufl.exp(35 / 9 + V_m / 9))
        dss = 1.0 / (1 + ufl.exp(-5 / 6 - V_m / 6))
        taud = (1 - ufl.exp(-5 / 6 - V_m / 6)) * dss / (0.175 + 0.035 * V_m)
        tauf = 1.0/(0.02 + 0.0197*ufl.exp(-((0.48865 + 0.0337*V_m)*(0.48865 +\
            0.0337*V_m))))
        F_expressions[9] = (-d + dss) / taud
        F_expressions[10] = (fss - f) / tauf
        F_expressions[11] = 1.7 * (1 - f_Ca_Bj) * Ca_j - 0.0119 * f_Ca_Bj
        F_expressions[12] = -0.0119 * f_Ca_Bsl + 1.7 * (1 - f_Ca_Bsl) * Ca_sl
        fcaCaMSL = 0
        fcaCaj = 0
        ibarca_j = 4*Frdy*pCa*(-0.341*Cao +\
            0.341*Ca_j*ufl.exp(2*FoRT*V_m))*FoRT*V_m/(-1 +\
            ufl.exp(2*FoRT*V_m))
        ibarca_sl = 4*Frdy*pCa*(-0.341*Cao +\
            0.341*Ca_sl*ufl.exp(2*FoRT*V_m))*FoRT*V_m/(-1 +\
            ufl.exp(2*FoRT*V_m))
        ibarna_j = Frdy*pNa*(-0.75*Nao +\
            0.75*Na_j*ufl.exp(FoRT*V_m))*FoRT*V_m/(-1 + ufl.exp(FoRT*V_m))
        ibarna_sl = Frdy*pNa*(0.75*Na_sl*ufl.exp(FoRT*V_m) -\
            0.75*Nao)*FoRT*V_m/(-1 + ufl.exp(FoRT*V_m))
        I_Ca_junc = g_CaL_factor*0.45*Fjunc_CaL*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bj +\
            fcaCaj)*d*f*ibarca_j
        I_Ca_sl = g_CaL_factor*0.45*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bsl +\
            fcaCaMSL)*Fsl_CaL*d*f*ibarca_sl
        I_CaNa_junc = g_CaL_factor*0.45*Fjunc_CaL*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bj\
            + fcaCaj)*d*f*ibarna_j
        I_CaNa_sl = g_CaL_factor*0.45*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bsl +\
            fcaCaMSL)*Fsl_CaL*d*f*ibarna_sl

        # Expressions for the I_NCX component
        Ka_junc = 1.0 / (1 + (Kdact * Kdact) / (Ca_j * Ca_j))
        Ka_sl = 1.0 / (1 + (Kdact * Kdact) / (Ca_sl * Ca_sl))
        s1_junc = Cao * (Na_j * Na_j * Na_j) * ufl.exp(nu * FoRT * V_m)
        s1_sl = Cao * (Na_sl * Na_sl * Na_sl) * ufl.exp(nu * FoRT * V_m)
        s2_junc = (Nao * Nao * Nao) * Ca_j * ufl.exp((-1 + nu) * FoRT * V_m)
        s3_junc = KmCao*(Na_j*Na_j*Na_j) + (Nao*Nao*Nao)*Ca_j +\
            Cao*(Na_j*Na_j*Na_j) + KmCai*(Nao*Nao*Nao)*(1 +\
            (Na_j*Na_j*Na_j)/(KmNai*KmNai*KmNai)) + (KmNao*KmNao*KmNao)*(1 +\
            Ca_j/KmCai)*Ca_j
        s2_sl = (Nao * Nao * Nao) * Ca_sl * ufl.exp((-1 + nu) * FoRT * V_m)
        s3_sl = KmCai*(Nao*Nao*Nao)*(1 +\
            (Na_sl*Na_sl*Na_sl)/(KmNai*KmNai*KmNai)) + (Nao*Nao*Nao)*Ca_sl +\
            (KmNao*KmNao*KmNao)*(1 + Ca_sl/KmCai)*Ca_sl +\
            Cao*(Na_sl*Na_sl*Na_sl) + KmCao*(Na_sl*Na_sl*Na_sl)
        I_ncx_junc = Fjunc*IbarNCX*ufl.elem_pow(Q10NCX, Qpow)*(-s2_junc +\
            s1_junc)*Ka_junc/((1 + ksat*ufl.exp((-1 + nu)*FoRT*V_m))*s3_junc)
        I_ncx_sl = IbarNCX*ufl.elem_pow(Q10NCX, Qpow)*(-s2_sl +\
            s1_sl)*Fsl*Ka_sl/((1 + ksat*ufl.exp((-1 + nu)*FoRT*V_m))*s3_sl)

        # Expressions for the I_PCa component
        I_pca_junc = Fjunc*IbarSLCaP*ufl.elem_pow(Q10SLCaP,\
            Qpow)*ufl.elem_pow(Ca_j, 1.6)/(ufl.elem_pow(Ca_j, 1.6) +\
            ufl.elem_pow(KmPCa, 1.6))
        I_pca_sl = IbarSLCaP*ufl.elem_pow(Q10SLCaP, Qpow)*ufl.elem_pow(Ca_sl,\
            1.6)*Fsl/(ufl.elem_pow(Ca_sl, 1.6) + ufl.elem_pow(KmPCa, 1.6))

        # Expressions for the I_CaBK component
        I_cabk_junc = Fjunc * GCaB * (-eca_junc + V_m)
        I_cabk_sl = GCaB * (-eca_sl + V_m) * Fsl

        # Expressions for the SR Fluxes component
        kCaSR = MaxSR - (-MinSR + MaxSR) / (1 +
                                            ufl.elem_pow(ec50SR / Ca_sr, 2.5))
        koSRCa = koCa / kCaSR
        kiSRCa = kiCa * kCaSR
        RI = 1 - Ry_Ro - Ry_Ri - Ry_Rr
        F_expressions[15] = -(Ca_j*Ca_j)*Ry_Rr*koSRCa + kom*Ry_Ro + kim*RI -\
            Ca_j*Ry_Rr*kiSRCa
        F_expressions[14] = -kom*Ry_Ro - Ca_j*Ry_Ro*kiSRCa + kim*Ry_Ri +\
            (Ca_j*Ca_j)*Ry_Rr*koSRCa
        F_expressions[13] = -kim*Ry_Ri + Ca_j*Ry_Ro*kiSRCa - kom*Ry_Ri +\
            (Ca_j*Ca_j)*RI*koSRCa
        J_SRCarel = SR_Ca_release_ks_factor * ks * (Ca_sr - Ca_j) * Ry_Ro
        J_serca = Vmax_SRCaP*ufl.elem_pow(Q10SRCaP,\
            Qpow)*(-ufl.elem_pow(Ca_sr/Kmr, hillSRCaP) +\
            ufl.elem_pow(Ca_i/Kmf, hillSRCaP))/(1 + ufl.elem_pow(Ca_sr/Kmr,\
            hillSRCaP) + ufl.elem_pow(Ca_i/Kmf, hillSRCaP))
        J_SRleak = 5.348e-06 * Ca_sr - 5.348e-06 * Ca_j

        # Expressions for the Na Buffers component
        F_expressions[16] = -koff_na * Na_Bj + kon_na * (-Na_Bj +
                                                         Bmax_Naj) * Na_j
        F_expressions[17] = kon_na * (-Na_Bsl +
                                      Bmax_Nasl) * Na_sl - koff_na * Na_Bsl

        # Expressions for the Cytosolic Ca Buffers component
        F_expressions[24] = kon_tncl*(Bmax_TnClow - Tn_CL)*Ca_i -\
            koff_tncl*Tn_CL
        F_expressions[22] = -koff_tnchca*Tn_CHc + kon_tnchca*(-Tn_CHc +\
            Bmax_TnChigh - Tn_CHm)*Ca_i
        F_expressions[23] = Mgi*kon_tnchmg*(-Tn_CHc + Bmax_TnChigh - Tn_CHm)\
            - koff_tnchmg*Tn_CHm
        F_expressions[18] = kon_cam * (-CaM + Bmax_CaM) * Ca_i - koff_cam * CaM
        F_expressions[19] = -koff_myoca*Myo_c + kon_myoca*(-Myo_c +\
            Bmax_myosin - Myo_m)*Ca_i
        F_expressions[20] = Mgi*kon_myomg*(-Myo_c + Bmax_myosin - Myo_m) -\
            koff_myomg*Myo_m
        F_expressions[21] = kon_sr * (Bmax_SR - SRB) * Ca_i - koff_sr * SRB
        J_CaB_cytosol = -koff_tnchca*Tn_CHc - koff_myoca*Myo_c +\
            Mgi*kon_myomg*(-Myo_c + Bmax_myosin - Myo_m) +\
            Mgi*kon_tnchmg*(-Tn_CHc + Bmax_TnChigh - Tn_CHm) -\
            koff_tnchmg*Tn_CHm + kon_tncl*(Bmax_TnClow - Tn_CL)*Ca_i +\
            kon_sr*(Bmax_SR - SRB)*Ca_i - koff_myomg*Myo_m + kon_cam*(-CaM +\
            Bmax_CaM)*Ca_i - koff_cam*CaM - koff_tncl*Tn_CL +\
            kon_myoca*(-Myo_c + Bmax_myosin - Myo_m)*Ca_i +\
            kon_tnchca*(-Tn_CHc + Bmax_TnChigh - Tn_CHm)*Ca_i - koff_sr*SRB

        # Expressions for the Junctional and SL Ca Buffers component
        Bmax_SLlowsl = Bmax_SLlowsl0 * Vmyo / Vsl
        Bmax_SLlowj = Bmax_SLlowj0 * Vmyo / Vjunc
        Bmax_SLhighsl = Bmax_SLhighsl0 * Vmyo / Vsl
        Bmax_SLhighj = Bmax_SLhighj0 * Vmyo / Vjunc
        F_expressions[27] = kon_sll * (Bmax_SLlowj -
                                       SLL_j) * Ca_j - koff_sll * SLL_j
        F_expressions[28] = kon_sll*(-SLL_sl + Bmax_SLlowsl)*Ca_sl -\
            koff_sll*SLL_sl
        F_expressions[25] = kon_slh*(Bmax_SLhighj - SLH_j)*Ca_j -\
            koff_slh*SLH_j
        F_expressions[26] = kon_slh*(-SLH_sl + Bmax_SLhighsl)*Ca_sl -\
            koff_slh*SLH_sl
        J_CaB_junction = kon_slh*(Bmax_SLhighj - SLH_j)*Ca_j +\
            kon_sll*(Bmax_SLlowj - SLL_j)*Ca_j - koff_slh*SLH_j -\
            koff_sll*SLL_j
        J_CaB_sl = kon_sll*(-SLL_sl + Bmax_SLlowsl)*Ca_sl + kon_slh*(-SLH_sl\
            + Bmax_SLhighsl)*Ca_sl - koff_sll*SLL_sl - koff_slh*SLH_sl

        # Expressions for the SR Ca Concentrations component
        Bmax_Csqn = Bmax_Csqn0 * Vmyo / Vsr
        F_expressions[30] = -koff_csqn*Csqn_b + kon_csqn*(Bmax_Csqn -\
            Csqn_b)*Ca_sr
        F_expressions[29] = -kon_csqn*(Bmax_Csqn - Csqn_b)*Ca_sr -\
            J_SRleak*Vmyo/Vsr + koff_csqn*Csqn_b - J_SRCarel + J_serca

        # Expressions for the Na Concentrations component
        I_Na_tot_junc = 3*I_nak_junc + 3*I_ncx_junc + I_CaNa_junc + I_Na_junc\
            + I_nabk_junc
        I_Na_tot_sl = I_Na_sl + I_nabk_sl + 3 * I_nak_sl + I_CaNa_sl + 3 * I_ncx_sl
        F_expressions[32] = -Cmem*I_Na_tot_junc/(Frdy*Vjunc) +\
            J_na_juncsl*(Na_sl - Na_j)/Vjunc - F_expressions[16]
        F_expressions[33] = -F_expressions[17] + J_na_slmyo*(-Na_sl +\
            Na_i)/Vsl + J_na_juncsl*(-Na_sl + Na_j)/Vsl -\
            Cmem*I_Na_tot_sl/(Frdy*Vsl)
        F_expressions[31] = J_na_slmyo * (Na_sl - Na_i) / Vmyo

        # Expressions for the K Concentration component
        F_expressions[34] = Constant(0.0)

        # Expressions for the Ca Concentrations component
        I_Ca_tot_junc = I_pca_junc + I_cabk_junc + I_Ca_junc - 2 * I_ncx_junc
        I_Ca_tot_sl = -2 * I_ncx_sl + I_pca_sl + I_cabk_sl + I_Ca_sl
        F_expressions[36] = J_ca_juncsl*(Ca_sl - Ca_j)/Vjunc +\
            J_SRCarel*Vsr/Vjunc - J_CaB_junction -\
            Cmem*I_Ca_tot_junc/(2*Frdy*Vjunc) + J_SRleak*Vmyo/Vjunc
        F_expressions[37] = -J_CaB_sl + J_ca_juncsl*(Ca_j - Ca_sl)/Vsl -\
            Cmem*I_Ca_tot_sl/(2*Frdy*Vsl) + J_ca_slmyo*(Ca_i - Ca_sl)/Vsl
        F_expressions[35] = -J_CaB_cytosol + J_ca_slmyo*(Ca_sl - Ca_i)/Vmyo -\
            J_serca*Vsr/Vmyo

        # Return results
        return dolfin.as_vector(F_expressions)
Example #26
0
phi = TestFunction(V)

rho_0_lotnisko_inne = Expression("exp(-a*pow(x[0]-7, 2) - a*pow(x[1]-4, 2))",
                                 degree=2,
                                 a=0.01)
rho_0_lotnisko = Expression("x[1] < 35 ? 1.0 : 0.0", degree=2, a=0.01)
rho_0 = Expression("exp(-a*pow(x[0]-7, 2) - a*pow(x[1]-4, 2))", degree=2, a=1)

F = u * v * dx + sigma * sigma * dot(grad(u), grad(v)) * dx
a, L = lhs(F), rhs(F)

u = Function(V)
velocity = Function(V)
solve(a == L, u, bc_lotnisko)

phi = uf.ln(u)

dx0 = project(phi.dx(0))
dx1 = project(phi.dx(1))
unnormed_grad_phi = project(grad(phi))
module = sqrt(dx0 * dx0 + dx1 * dx1)
normed_phi = unnormed_grad_phi / module

plot(normed_phi)
plt.show()

rhoold = interpolate(rho_0_lotnisko, V)

plot(rhoold)
plt.show()
Example #27
0
    def _I(self, v, s, time):
        """
        Original gotran transmembrane current dV/dt
        """
        time = time if time else Constant(0.0)

        # Assign states
        V_m = v
        assert (len(s) == 38)
        h, j, m, x_kr, x_ks, x_to_f, x_to_s, y_to_f, y_to_s, d, f, f_Ca_Bj,\
            f_Ca_Bsl, Ry_Ri, Ry_Ro, Ry_Rr, Na_Bj, Na_Bsl, CaM, Myo_c, Myo_m,\
            SRB, Tn_CHc, Tn_CHm, Tn_CL, SLH_j, SLH_sl, SLL_j, SLL_sl, Ca_sr,\
            Csqn_b, Na_i, Na_j, Na_sl, K_i, Ca_i, Ca_j, Ca_sl = s

        # Assign parameters
        Fjunc = self._parameters["Fjunc"]
        Fjunc_CaL = self._parameters["Fjunc_CaL"]
        GNa = self._parameters["GNa"]
        GNaB = self._parameters["GNaB"]
        IbarNaK = self._parameters["IbarNaK"]
        KmKo = self._parameters["KmKo"]
        KmNaip = self._parameters["KmNaip"]
        gkp = self._parameters["gkp"]
        pNaK = self._parameters["pNaK"]
        epi = self._parameters["epi"]
        GClB = self._parameters["GClB"]
        GClCa = self._parameters["GClCa"]
        KdClCa = self._parameters["KdClCa"]
        Q10CaL = self._parameters["Q10CaL"]
        pCa = self._parameters["pCa"]
        pK = self._parameters["pK"]
        pNa = self._parameters["pNa"]
        IbarNCX = self._parameters["IbarNCX"]
        Kdact = self._parameters["Kdact"]
        KmCai = self._parameters["KmCai"]
        KmCao = self._parameters["KmCao"]
        KmNai = self._parameters["KmNai"]
        KmNao = self._parameters["KmNao"]
        Q10NCX = self._parameters["Q10NCX"]
        ksat = self._parameters["ksat"]
        nu = self._parameters["nu"]
        IbarSLCaP = self._parameters["IbarSLCaP"]
        KmPCa = self._parameters["KmPCa"]
        Q10SLCaP = self._parameters["Q10SLCaP"]
        GCaB = self._parameters["GCaB"]
        Nao = self._parameters["Nao"]
        Ko = self._parameters["Ko"]
        Cao = self._parameters["Cao"]
        Cli = self._parameters["Cli"]
        Clo = self._parameters["Clo"]
        Frdy = self._parameters["Frdy"]
        R = self._parameters["R"]
        Temp = self._parameters["Temp"]
        g_K1_factor = self._parameters["g_K1_factor"]
        g_CaL_factor = self._parameters["g_CaL_factor"]
        g_Kr_factor = self._parameters["g_Kr_factor"]
        g_Ks_factor = self._parameters["g_Ks_factor"]
        g_to_factor = self._parameters["g_to_factor"]
        SR_Ca_release_ks_factor = self._parameters["SR_Ca_release_ks_factor"]

        # Init return args
        current = [ufl.zero()] * 1

        # Expressions for the Geometry component
        Fsl = 1 - Fjunc
        Fsl_CaL = 1 - Fjunc_CaL

        # Expressions for the Reversal potentials component
        FoRT = Frdy / (R * Temp)
        ena_junc = ufl.ln(Nao / Na_j) / FoRT
        ena_sl = ufl.ln(Nao / Na_sl) / FoRT
        ek = ufl.ln(Ko / K_i) / FoRT
        eca_junc = ufl.ln(Cao / Ca_j) / (2 * FoRT)
        eca_sl = ufl.ln(Cao / Ca_sl) / (2 * FoRT)
        ecl = ufl.ln(Cli / Clo) / FoRT
        Qpow = -31 + Temp / 10

        # Expressions for the I_Na component
        I_Na_junc = Fjunc * GNa * (m * m * m) * (-ena_junc + V_m) * h * j
        I_Na_sl = GNa * (m * m * m) * (-ena_sl + V_m) * Fsl * h * j

        # Expressions for the I_NaBK component
        I_nabk_junc = Fjunc * GNaB * (-ena_junc + V_m)
        I_nabk_sl = GNaB * (-ena_sl + V_m) * Fsl

        # Expressions for the I_NaK component
        sigma = -1 / 7 + ufl.exp(0.0148588410104 * Nao) / 7
        fnak = 1.0/(1 + 0.1245*ufl.exp(-0.1*FoRT*V_m) +\
            0.0365*ufl.exp(-FoRT*V_m)*sigma)
        I_nak_junc = Fjunc*IbarNaK*Ko*fnak/((1 + ufl.elem_pow(KmNaip,\
            4)/ufl.elem_pow(Na_j, 4))*(KmKo + Ko))
        I_nak_sl = IbarNaK*Ko*Fsl*fnak/((1 + ufl.elem_pow(KmNaip,\
            4)/ufl.elem_pow(Na_sl, 4))*(KmKo + Ko))
        I_nak = I_nak_junc + I_nak_sl

        # Expressions for the I_Kr component
        gkr = g_Kr_factor * 0.0150616019019 * ufl.sqrt(Ko)
        rkr = 1.0 / (1 + ufl.exp(37 / 12 + V_m / 24))
        I_kr = (-ek + V_m) * gkr * rkr * x_kr

        # Expressions for the I_Kp component
        kp_kp = 1.0 / (1 + 1786.47556538 * ufl.exp(-0.167224080268 * V_m))
        I_kp_junc = Fjunc * gkp * (-ek + V_m) * kp_kp
        I_kp_sl = gkp * (-ek + V_m) * Fsl * kp_kp
        I_kp = I_kp_sl + I_kp_junc

        # Expressions for the I_Ks component
        eks = ufl.ln((Nao * pNaK + Ko) / (pNaK * Na_i + K_i)) / FoRT
        gks_junc = g_Ks_factor * 0.0035
        gks_sl = g_Ks_factor * 0.0035
        I_ks_junc = Fjunc * gks_junc * (x_ks * x_ks) * (-eks + V_m)
        I_ks_sl = gks_sl * (x_ks * x_ks) * (-eks + V_m) * Fsl
        I_ks = I_ks_sl + I_ks_junc

        # Expressions for the I_to component
        GtoSlow = ufl.conditional(ufl.eq(epi, 1), 0.0156, 0.037596)
        GtoFast = ufl.conditional(ufl.eq(epi, 1), 0.1144, 0.001404)
        I_tos = (-ek + V_m) * GtoSlow * x_to_s * y_to_s
        I_tof = (-ek + V_m) * g_to_factor * GtoFast * x_to_f * y_to_f
        I_to = I_tos + I_tof

        # Expressions for the I_Ki component
        aki = 1.02 / (1 +
                      7.35454251046e-07 * ufl.exp(0.2385 * V_m - 0.2385 * ek))
        bki = (0.762624006506*ufl.exp(0.08032*V_m - 0.08032*ek) +\
            1.15340563519e-16*ufl.exp(0.06175*V_m - 0.06175*ek))/(1 +\
            0.0867722941577*ufl.exp(-0.5143*V_m + 0.5143*ek))
        kiss = aki / (aki + bki)
        I_ki = g_K1_factor * 0.150616019019 * ufl.sqrt(Ko) * (-ek + V_m) * kiss

        # Expressions for the I_ClCa component
        I_ClCa_junc = Fjunc * GClCa * (-ecl + V_m) / (1 + KdClCa / Ca_j)
        I_ClCa_sl = GClCa * (-ecl + V_m) * Fsl / (1 + KdClCa / Ca_sl)
        I_ClCa = I_ClCa_sl + I_ClCa_junc
        I_Clbk = GClB * (-ecl + V_m)

        # Expressions for the I_Ca component
        fcaCaMSL = 0
        fcaCaj = 0
        ibarca_j = 4*Frdy*pCa*(-0.341*Cao +\
            0.341*Ca_j*ufl.exp(2*FoRT*V_m))*FoRT*V_m/(-1 +\
            ufl.exp(2*FoRT*V_m))
        ibarca_sl = 4*Frdy*pCa*(-0.341*Cao +\
            0.341*Ca_sl*ufl.exp(2*FoRT*V_m))*FoRT*V_m/(-1 +\
            ufl.exp(2*FoRT*V_m))
        ibark = Frdy*pK*(-0.75*Ko + 0.75*K_i*ufl.exp(FoRT*V_m))*FoRT*V_m/(-1 +\
            ufl.exp(FoRT*V_m))
        ibarna_j = Frdy*pNa*(-0.75*Nao +\
            0.75*Na_j*ufl.exp(FoRT*V_m))*FoRT*V_m/(-1 + ufl.exp(FoRT*V_m))
        ibarna_sl = Frdy*pNa*(0.75*Na_sl*ufl.exp(FoRT*V_m) -\
            0.75*Nao)*FoRT*V_m/(-1 + ufl.exp(FoRT*V_m))
        I_Ca_junc = g_CaL_factor*0.45*Fjunc_CaL*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bj +\
            fcaCaj)*d*f*ibarca_j
        I_Ca_sl = g_CaL_factor*0.45*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bsl +\
            fcaCaMSL)*Fsl_CaL*d*f*ibarca_sl
        I_CaK = g_CaL_factor*0.45*ufl.elem_pow(Q10CaL, Qpow)*(Fjunc_CaL*(1 - f_Ca_Bj +\
            fcaCaj) + (1 - f_Ca_Bsl + fcaCaMSL)*Fsl_CaL)*d*f*ibark
        I_CaNa_junc = g_CaL_factor*0.45*Fjunc_CaL*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bj\
            + fcaCaj)*d*f*ibarna_j
        I_CaNa_sl = g_CaL_factor*0.45*ufl.elem_pow(Q10CaL, Qpow)*(1 - f_Ca_Bsl +\
            fcaCaMSL)*Fsl_CaL*d*f*ibarna_sl

        # Expressions for the I_NCX component
        Ka_junc = 1.0 / (1 + (Kdact * Kdact) / (Ca_j * Ca_j))
        Ka_sl = 1.0 / (1 + (Kdact * Kdact) / (Ca_sl * Ca_sl))
        s1_junc = Cao * (Na_j * Na_j * Na_j) * ufl.exp(nu * FoRT * V_m)
        s1_sl = Cao * (Na_sl * Na_sl * Na_sl) * ufl.exp(nu * FoRT * V_m)
        s2_junc = (Nao * Nao * Nao) * Ca_j * ufl.exp((-1 + nu) * FoRT * V_m)
        s3_junc = KmCao*(Na_j*Na_j*Na_j) + (Nao*Nao*Nao)*Ca_j +\
            Cao*(Na_j*Na_j*Na_j) + KmCai*(Nao*Nao*Nao)*(1 +\
            (Na_j*Na_j*Na_j)/(KmNai*KmNai*KmNai)) + (KmNao*KmNao*KmNao)*(1 +\
            Ca_j/KmCai)*Ca_j
        s2_sl = (Nao * Nao * Nao) * Ca_sl * ufl.exp((-1 + nu) * FoRT * V_m)
        s3_sl = KmCai*(Nao*Nao*Nao)*(1 +\
            (Na_sl*Na_sl*Na_sl)/(KmNai*KmNai*KmNai)) + (Nao*Nao*Nao)*Ca_sl +\
            (KmNao*KmNao*KmNao)*(1 + Ca_sl/KmCai)*Ca_sl +\
            Cao*(Na_sl*Na_sl*Na_sl) + KmCao*(Na_sl*Na_sl*Na_sl)
        I_ncx_junc = Fjunc*IbarNCX*ufl.elem_pow(Q10NCX, Qpow)*(-s2_junc +\
            s1_junc)*Ka_junc/((1 + ksat*ufl.exp((-1 + nu)*FoRT*V_m))*s3_junc)
        I_ncx_sl = IbarNCX*ufl.elem_pow(Q10NCX, Qpow)*(-s2_sl +\
            s1_sl)*Fsl*Ka_sl/((1 + ksat*ufl.exp((-1 + nu)*FoRT*V_m))*s3_sl)

        # Expressions for the I_PCa component
        I_pca_junc = Fjunc*IbarSLCaP*ufl.elem_pow(Q10SLCaP,\
            Qpow)*ufl.elem_pow(Ca_j, 1.6)/(ufl.elem_pow(Ca_j, 1.6) +\
            ufl.elem_pow(KmPCa, 1.6))
        I_pca_sl = IbarSLCaP*ufl.elem_pow(Q10SLCaP, Qpow)*ufl.elem_pow(Ca_sl,\
            1.6)*Fsl/(ufl.elem_pow(Ca_sl, 1.6) + ufl.elem_pow(KmPCa, 1.6))

        # Expressions for the I_CaBK component
        I_cabk_junc = Fjunc * GCaB * (-eca_junc + V_m)
        I_cabk_sl = GCaB * (-eca_sl + V_m) * Fsl

        # Expressions for the Na Concentrations component
        I_Na_tot_junc = 3*I_nak_junc + 3*I_ncx_junc + I_CaNa_junc + I_Na_junc\
            + I_nabk_junc
        I_Na_tot_sl = I_Na_sl + I_nabk_sl + 3 * I_nak_sl + I_CaNa_sl + 3 * I_ncx_sl

        # Expressions for the K Concentration component
        I_K_tot = -2 * I_nak + I_ks + I_CaK + I_kr + I_kp + I_ki + I_to

        # Expressions for the Ca Concentrations component
        I_Ca_tot_junc = I_pca_junc + I_cabk_junc + I_Ca_junc - 2 * I_ncx_junc
        I_Ca_tot_sl = -2 * I_ncx_sl + I_pca_sl + I_cabk_sl + I_Ca_sl

        # Expressions for the Membrane potential component
        i_Stim = 0
        I_Na_tot = I_Na_tot_junc + I_Na_tot_sl
        I_Cl_tot = I_Clbk + I_ClCa
        I_Ca_tot = I_Ca_tot_junc + I_Ca_tot_sl
        I_tot = I_Na_tot + I_K_tot + I_Cl_tot + I_Ca_tot
        current[0] = -I_tot - i_Stim

        # Return results
        return current[0]
Example #28
0
def test_neohooke():
    mesh = dolfinx.mesh.create_unit_cube(MPI.COMM_WORLD, 7, 7, 7)
    V = dolfinx.fem.VectorFunctionSpace(mesh, ("P", 1))
    P = dolfinx.fem.FunctionSpace(mesh, ("P", 1))

    L = dolfinx.fem.FunctionSpace(mesh, ("DG", 0))

    u = dolfinx.fem.Function(V, name="u")
    v = ufl.TestFunction(V)

    p = dolfinx.fem.Function(P, name="p")
    q = ufl.TestFunction(P)

    lmbda0 = dolfinx.fem.Function(L)

    d = mesh.topology.dim
    Id = ufl.Identity(d)
    F = Id + ufl.grad(u)
    C = F.T * F
    J = ufl.det(F)

    E_, nu_ = 10.0, 0.3
    mu, lmbda = E_ / (2 * (1 + nu_)), E_ * nu_ / ((1 + nu_) * (1 - 2 * nu_))
    psi = (mu / 2) * (ufl.tr(C) -
                      3) - mu * ufl.ln(J) + lmbda / 2 * ufl.ln(J)**2 + (p -
                                                                        1.0)**2
    pi = psi * ufl.dx

    F0 = ufl.derivative(pi, u, v)
    F1 = ufl.derivative(pi, p, q)

    # Number of eigenvalues to find
    nev = 8

    opts = PETSc.Options("neohooke")
    opts["eps_smallest_magnitude"] = True
    opts["eps_nev"] = nev
    opts["eps_ncv"] = 50 * nev
    opts["eps_conv_abs"] = True

    # opts["eps_non_hermitian"] = True
    opts["eps_tol"] = 1.0e-14
    opts["eps_max_it"] = 1000
    opts["eps_error_relative"] = "ascii::ascii_info_detail"
    opts["eps_monitor"] = "ascii"

    slepcp = dolfiny.slepcblockproblem.SLEPcBlockProblem([F0, F1], [u, p],
                                                         lmbda0,
                                                         prefix="neohooke")
    slepcp.solve()

    # mat = dolfiny.la.petsc_to_scipy(slepcp.eps.getOperators()[0])
    # eigvals, eigvecs = linalg.eigsh(mat, which="SM", k=nev)

    with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "eigvec.xdmf", "w") as ofile:
        ofile.write_mesh(mesh)
        for i in range(nev):
            eigval, ur, ui = slepcp.getEigenpair(i)

            # Expect first 6 eignevalues 0, i.e. rigid body modes
            if i < 6:
                assert np.isclose(eigval, 0.0)

            for func in ur:
                name = func.name
                func.name = f"{name}_eigvec_{i}_real"
                ofile.write_function(func)

                func.name = name
Example #29
0
def assemble_test(cell_batch_size: int):
    mesh = dolfin.UnitCubeMesh(MPI.comm_world, 40, 40, 40)

    def isochoric(F):
        C = F.T*F

        I_1 = tr(C)
        I4_f = dot(e_f, C*e_f)
        I4_s = dot(e_s, C*e_s)
        I8_fs = dot(e_f, C*e_s)

        def cutoff(x):
            return 1.0/(1.0 + ufl.exp(-(x - 1.0)*30.0))

        def scaled_exp(a0, a1, argument):
            return a0/(2.0*a1)*(ufl.exp(b*argument) - 1)

        E_1 = scaled_exp(a, b, I_1 - 3.)

        E_f = cutoff(I4_f)*scaled_exp(a_f, b_f, (I4_f - 1.)**2)
        E_s = cutoff(I4_s)*scaled_exp(a_s, b_s, (I4_s - 1.)**2)
        E_3 = scaled_exp(a_fs, b_fs, I8_fs**2)

        E = E_1 + E_f + E_s + E_3
        return E

    cell = mesh.ufl_cell()

    lamda = dolfin.Constant(0.48, cell)
    a = dolfin.Constant(1.0, cell)
    b = dolfin.Constant(1.0, cell)
    a_s = dolfin.Constant(1.0, cell)
    b_s = dolfin.Constant(1.0, cell)
    a_f = dolfin.Constant(1.0, cell)
    b_f = dolfin.Constant(1.0, cell)
    a_fs = dolfin.Constant(1.0, cell)
    b_fs = dolfin.Constant(1.0, cell)

    # For more fun, make these general vector fields rather than
    # constants:
    e_s = dolfin.Constant([0.0, 1.0, 0.0], cell)
    e_f = dolfin.Constant([1.0, 0.0, 0.0], cell)

    V = dolfin.FunctionSpace(mesh, ufl.VectorElement("CG", cell, 1))
    u = dolfin.Function(V)
    du = dolfin.function.argument.TrialFunction(V)
    v = dolfin.function.argument.TestFunction(V)

    # Misc elasticity related tensors and other quantities
    F = grad(u) + ufl.Identity(3)
    F = ufl.variable(F)
    J = det(F)
    Fbar = J**(-1.0/3.0)*F

    # Define energy
    E_volumetric = lamda*0.5*ln(J)**2
    psi = isochoric(Fbar) + E_volumetric

    # Find first Piola-Kircchoff tensor
    P = ufl.diff(psi, F)

    # Define the variational formulation
    F = inner(P, grad(v))*dx

    # Take the derivative
    J = ufl.derivative(F, u, du)

    a, L = J, F

    if cell_batch_size > 1:
        cxx_flags = "-O2 -ftree-vectorize -funroll-loops -march=native -mtune=native"
    else:
        cxx_flags = "-O2"

    assembler = dolfin.fem.assembling.Assembler([[a]], [L], [],
                                                form_compiler_parameters={"cell_batch_size": cell_batch_size,
                                                                          "enable_cross_cell_gcc_ext": True,
                                                                          "cpp_optimize_flags": cxx_flags})

    t = -time.time()
    A, b = assembler.assemble(
        mat_type=dolfin.cpp.fem.Assembler.BlockType.monolithic)
    t += time.time()

    return A, b, t
Example #30
0
chi = 0.2
po = Gdry / lamo
muo = Rg*T*(math.log(Omega*cod/(1+Omega*cod))+1/(1+Omega*cod)+ chi/pow((1+Omega*cod),2)) \
+Omega*po
mus = muo
mutop = -10000

# Kinematics
I = Identity(3)
F = I + grad(u)
J = det(F)

# Constitutive equations

S = (Gdry / lamo) * F - p * cofac(F)
mu = Rg*T*(ufl.ln(Omega*Jo*c/(1+Omega*Jo*c)) + 1./(1.+Omega*Jo*c)+ \
         chi/pow((1.+Omega*Jo*c),2))+Omega*p

h = g(c) * grad(c) + (-c * D / (Rg * T)) * Omega * grad(p)

dmudp = Omega
dmu = mu - mus

# Boundary conditions


def left_boundary(x, on_boundary):  # x = 0
    return on_boundary and abs(x[0]) < DOLFIN_EPS


def back_boundary(x, on_boundary):  # y = 0
Example #31
0
    def F(self, v, s, time=None):
        """
        Right hand side for ODE system
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert (len(s) == 17)
        m, h, j, d, f1, f2, fCa, Xr1, Xr2, Xs, Xf, q, r, Nai, g, Cai, Ca_SR = s

        # Assign parameters
        TTX_10uM = self._parameters["TTX_10uM"]
        TTX_30uM = self._parameters["TTX_30uM"]
        TTX_3uM = self._parameters["TTX_3uM"]
        nifed_100nM = self._parameters["nifed_100nM"]
        nifed_10nM = self._parameters["nifed_10nM"]
        nifed_30nM = self._parameters["nifed_30nM"]
        nifed_3nM = self._parameters["nifed_3nM"]
        g_Na = self._parameters["g_Na"]
        g_CaL = self._parameters["g_CaL"]
        tau_fCa = self._parameters["tau_fCa"]
        L0 = self._parameters["L0"]
        Q = self._parameters["Q"]
        g_b_Na = self._parameters["g_b_Na"]
        g_b_Ca = self._parameters["g_b_Ca"]
        Km_K = self._parameters["Km_K"]
        Km_Na = self._parameters["Km_Na"]
        PNaK = self._parameters["PNaK"]
        KmCa = self._parameters["KmCa"]
        KmNai = self._parameters["KmNai"]
        Ksat = self._parameters["Ksat"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        kNaCa = self._parameters["kNaCa"]
        KPCa = self._parameters["KPCa"]
        g_PCa = self._parameters["g_PCa"]
        Cao = self._parameters["Cao"]
        Cm = self._parameters["Cm"]
        F = self._parameters["F"]
        Ko = self._parameters["Ko"]
        Nao = self._parameters["Nao"]
        R = self._parameters["R"]
        T = self._parameters["T"]
        V_SR = self._parameters["V_SR"]
        Vc = self._parameters["Vc"]
        Buf_C = self._parameters["Buf_C"]
        Buf_SR = self._parameters["Buf_SR"]
        Kbuf_C = self._parameters["Kbuf_C"]
        Kbuf_SR = self._parameters["Kbuf_SR"]
        Kup = self._parameters["Kup"]
        V_leak = self._parameters["V_leak"]
        VmaxUp = self._parameters["VmaxUp"]
        a_rel = self._parameters["a_rel"]
        b_rel = self._parameters["b_rel"]
        c_rel = self._parameters["c_rel"]
        tau_g = self._parameters["tau_g"]

        # Init return args
        F_expressions = [ufl.zero()] * 17

        # Expressions for the Electric potentials component
        E_Na = R * T * ufl.ln(Nao / Nai) / F
        E_Ca = 0.5 * R * T * ufl.ln(Cao / Cai) / F

        # Expressions for the i_Na component
        TTX_coeff = ufl.conditional(ufl.eq(TTX_3uM, 1), 0.18,\
            ufl.conditional(ufl.eq(TTX_10uM, 1), 0.06,\
            ufl.conditional(ufl.eq(TTX_30uM, 1), 0.02, 1)))
        i_Na = g_Na * (m * m * m) * (-E_Na + V) * TTX_coeff * h * j

        # Expressions for the m gate component
        m_inf = 1.0*ufl.elem_pow(1 +\
            0.00308976260789*ufl.exp(-169.491525424*V), -0.333333333333)
        alpha_m = 1.0 / (1 + 6.14421235333e-06 * ufl.exp(-200.0 * V))
        beta_m = 0.1/(1 + 1096.63315843*ufl.exp(200.0*V)) + 0.1/(1 +\
            0.778800783071*ufl.exp(5.0*V))
        tau_m = 0.001 * alpha_m * beta_m
        F_expressions[0] = (-m + m_inf) / tau_m

        # Expressions for the h gate component
        h_inf = 1.0 / ufl.sqrt(1 + 311490.091283 * ufl.exp(175.438596491 * V))
        alpha_h = ufl.conditional(ufl.lt(V, -0.04),\
            4.43126792958e-07*ufl.exp(-147.058823529*V), 0)
        beta_h = ufl.conditional(ufl.lt(V, -0.04), 310000.0*ufl.exp(348.5*V)\
            + 2.7*ufl.exp(79.0*V), 0.77/(0.13 +\
            0.0497581410839*ufl.exp(-90.0900900901*V)))
        tau_h = ufl.conditional(ufl.lt(V, -0.04), 1.5/(1000*alpha_h +\
            1000*beta_h), 0.002542)
        F_expressions[1] = (-h + h_inf) / tau_h

        # Expressions for the j gate component
        j_inf = 1.0 / ufl.sqrt(1 + 311490.091283 * ufl.exp(175.438596491 * V))
        alpha_j = ufl.conditional(ufl.lt(V, -0.04), (37.78 +\
            1000*V)*(-25428*ufl.exp(244.4*V) -\
            6.948e-06*ufl.exp(-43.91*V))/(1 +\
            50262745826.0*ufl.exp(311.0*V)), 0)
        beta_j = ufl.conditional(ufl.lt(V, -0.04),\
            0.02424*ufl.exp(-10.52*V)/(1 +\
            0.0039608683399*ufl.exp(-137.8*V)), 0.6*ufl.exp(57.0*V)/(1 +\
            0.0407622039784*ufl.exp(-100.0*V)))
        tau_j = 7.0 / (1000 * alpha_j + 1000 * beta_j)
        F_expressions[2] = (-j + j_inf) / tau_j

        # Expressions for the i_CaL component
        nifed_coeff = ufl.conditional(ufl.eq(nifed_3nM, 1), 0.93,\
            ufl.conditional(ufl.eq(nifed_10nM, 1), 0.79,\
            ufl.conditional(ufl.eq(nifed_30nM, 1), 0.56,\
            ufl.conditional(ufl.eq(nifed_100nM, 1), 0.28, 1))))
        i_CaL = 4*g_CaL*(F*F)*(-0.341*Cao +\
            Cai*ufl.exp(2*F*V/(R*T)))*V*d*f1*f2*fCa*nifed_coeff/(R*T*(-1 +\
            ufl.exp(2*F*V/(R*T))))

        # Expressions for the d gate component
        d_infinity = 1.0 / (1 + 0.272531793034 * ufl.exp(-1000 * V / 7))
        alpha_d = 0.25 + 1.4 / (1 + ufl.exp(-35 / 13 - 1000 * V / 13))
        beta_d = 1.4 / (1 + ufl.exp(1 + 200 * V))
        gamma_d = 1.0 / (1 + ufl.exp(5 / 2 - 50 * V))
        tau_d = 0.001 * gamma_d + 0.001 * alpha_d * beta_d
        F_expressions[3] = (-d + d_infinity) / tau_d

        # Expressions for the F1 gate component
        f1_inf = 1.0 / (1 + ufl.exp(26 / 3 + 1000 * V / 3))
        constf1 = ufl.conditional(ufl.gt(-f1 + f1_inf, 0), 0.92835 +\
            1433*Cai, 1)
        tau_f1 = 0.001*(20 + 200.0/(1 + ufl.exp(13/10 - 100*V)) + 180.0/(1 +\
            ufl.exp(3 + 100*V)) +\
            1102.5*ufl.exp(-0.00444444444444*ufl.elem_pow(27 + 1000*V,\
            4)))*constf1
        F_expressions[4] = (-f1 + f1_inf) / tau_f1

        # Expressions for the F2 gate component
        f2_inf = 0.33 + 0.67 / (1 + ufl.exp(35 / 4 + 250 * V))
        constf2 = 1
        tau_f2 = 0.001*constf2*(600*ufl.exp(-((25 + 1000*V)*(25 +\
            1000*V))/170) + 16.0/(1 + ufl.exp(3 + 100*V)) + 31.0/(1 +\
            ufl.exp(5/2 - 100*V)))
        F_expressions[5] = (-f2 + f2_inf) / tau_f2

        # Expressions for the FCa gate component
        alpha_fCa = 1.0 / (1 + 5.95374180765e+25 * ufl.elem_pow(Cai, 8))
        beta_fCa = 0.1 / (1 + 0.000123409804087 * ufl.exp(10000.0 * Cai))
        gamma_fCa = 0.3 / (1 + 0.391605626677 * ufl.exp(1250.0 * Cai))
        fCa_inf = 0.760109455762*alpha_fCa + 0.760109455762*beta_fCa +\
            0.760109455762*gamma_fCa
        constfCa = ufl.conditional(ufl.And(ufl.gt(V, -0.06), ufl.gt(fCa_inf,\
            fCa)), 0, 1)
        F_expressions[6] = (-fCa + fCa_inf) * constfCa / tau_fCa

        # Expressions for the Xr1 gate component
        V_half = -19.0 - 1000*R*T*ufl.ln(ufl.elem_pow(1 + 0.384615384615*Cao,\
            4)/(L0*ufl.elem_pow(1 + 1.72413793103*Cao, 4)))/(F*Q)
        Xr1_inf = 1.0 / (1 +
                         ufl.exp(0.204081632653 * V_half - 204.081632653 * V))
        alpha_Xr1 = 450.0 / (1 + ufl.exp(-9 / 2 - 100 * V))
        beta_Xr1 = 6.0 / (1 + 13.5813245226 * ufl.exp(86.9565217391 * V))
        tau_Xr1 = 0.001 * alpha_Xr1 * beta_Xr1
        F_expressions[7] = (-Xr1 + Xr1_inf) / tau_Xr1

        # Expressions for the Xr2 gate component
        Xr2_infinity = 1.0 / (1 + ufl.exp(44 / 25 + 20 * V))
        alpha_Xr2 = 3.0 / (1 + ufl.exp(-3 - 50 * V))
        beta_Xr2 = 1.12 / (1 + ufl.exp(-3 + 50 * V))
        tau_Xr2 = 0.001 * alpha_Xr2 * beta_Xr2
        F_expressions[8] = (-Xr2 + Xr2_infinity) / tau_Xr2

        # Expressions for the Xs gate component
        Xs_infinity = 1.0 / (1 + ufl.exp(-5 / 4 - 125 * V / 2))
        alpha_Xs = 1100.0 / ufl.sqrt(1 + ufl.exp(-5 / 3 - 500 * V / 3))
        beta_Xs = 1.0 / (1 + ufl.exp(-3 + 50 * V))
        tau_Xs = alpha_Xs * beta_Xs / 1000
        F_expressions[9] = (-Xs + Xs_infinity) / tau_Xs

        # Expressions for the Xf gate component
        Xf_infinity = 1.0 / (1 + 5780495.71031 * ufl.exp(200 * V))
        tau_Xf = 1.9 / (1 + ufl.exp(3 / 2 + 100 * V))
        F_expressions[10] = (-Xf + Xf_infinity) / tau_Xf

        # Expressions for the i_b Na component
        i_b_Na = g_b_Na * (-E_Na + V)

        # Expressions for the i_b Ca component
        i_b_Ca = g_b_Ca * (-E_Ca + V)

        # Expressions for the i_NaK component
        i_NaK = Ko*PNaK*Nai/((Km_K + Ko)*(Km_Na + Nai)*(1 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the i_NaCa component
        i_NaCa = kNaCa*(Cao*(Nai*Nai*Nai)*ufl.exp(F*gamma*V/(R*T)) -\
            alpha*(Nao*Nao*Nao)*Cai*ufl.exp(F*(-1 + gamma)*V/(R*T)))/((1 +\
            Ksat*ufl.exp(F*(-1 + gamma)*V/(R*T)))*(Cao +\
            KmCa)*((KmNai*KmNai*KmNai) + (Nao*Nao*Nao)))

        # Expressions for the i_PCa component
        i_PCa = g_PCa * Cai / (KPCa + Cai)

        # Expressions for the q gate component
        q_inf = 1.0 / (1 + ufl.exp(53 / 13 + 1000 * V / 13))
        tau_q = 0.00606 + 0.039102/(0.0168716780457*ufl.exp(-80.0*V) +\
            6.42137321286*ufl.exp(100.0*V))
        F_expressions[11] = (-q + q_inf) / tau_q

        # Expressions for the r gate component
        r_inf = 1.0 / (1 + 3.28489055021 * ufl.exp(-53.3333333333 * V))
        tau_r = 0.00275352 + 0.01440516/(16.3010892258*ufl.exp(90.0*V) +\
            0.0211152735604*ufl.exp(-120.0*V))
        F_expressions[12] = (-r + r_inf) / tau_r

        # Expressions for the Sodium dynamics component
        F_expressions[13] = -1e+18*Cm*(3*i_NaCa + 3*i_NaK + i_Na +\
            i_b_Na)/(F*Vc)

        # Expressions for the Calcium dynamics component
        i_rel = 0.0411*(c_rel + a_rel*(Ca_SR*Ca_SR)/((b_rel*b_rel) +\
            (Ca_SR*Ca_SR)))*d*g
        i_up = VmaxUp / (1 + (Kup * Kup) / (Cai * Cai))
        i_leak = V_leak * (-Cai + Ca_SR)
        g_inf = ufl.conditional(ufl.le(Cai, 0.00035), 1.0/(1 +\
            5.43991024148e+20*ufl.elem_pow(Cai, 6)), 1.0/(1 +\
            1.9720198874e+55*ufl.elem_pow(Cai, 16)))
        const2 = ufl.conditional(ufl.And(ufl.gt(V, -0.06), ufl.gt(g_inf, g)),\
            0, 1)
        F_expressions[14] = (-g + g_inf) * const2 / tau_g
        Cai_bufc = 1.0 / (1 + Buf_C * Kbuf_C / ((Kbuf_C + Cai) *
                                                (Kbuf_C + Cai)))
        Ca_SR_bufSR = 1.0/(1 + Buf_SR*Kbuf_SR/((Kbuf_SR + Ca_SR)*(Kbuf_SR +\
            Ca_SR)))
        F_expressions[15] = (-i_up - 5e+17*Cm*(-2*i_NaCa + i_CaL + i_PCa +\
            i_b_Ca)/(F*Vc) + i_leak + i_rel)*Cai_bufc
        F_expressions[16] = Vc * (-i_leak - i_rel + i_up) * Ca_SR_bufSR / V_SR

        # Return results
        return dolfin.as_vector(F_expressions)
Example #32
0
def rhs(states, time, parameters, dy=None):
    """
    Compute right hand side
    """
    # Imports
    import ufl
    import dolfin

    # Assign states
    assert(isinstance(states, dolfin.Function))
    assert(states.function_space().depth() == 1)
    assert(states.function_space().num_sub_spaces() == 17)
    Xr1, Xr2, Xs, m, h, j, d, f, fCa, s, r, Ca_SR, Ca_i, g, Na_i, V, K_i =\
        dolfin.split(states)

    # Assign parameters
    assert(isinstance(parameters, (dolfin.Function, dolfin.Constant)))
    if isinstance(parameters, dolfin.Function):
        assert(parameters.function_space().depth() == 1)
        assert(parameters.function_space().num_sub_spaces() == 45)
    else:
        assert(parameters.value_size() == 45)
    P_kna, g_K1, g_Kr, g_Ks, g_Na, g_bna, g_CaL, g_bca, g_to, K_mNa, K_mk,\
        P_NaK, K_NaCa, K_sat, Km_Ca, Km_Nai, alpha, gamma, K_pCa, g_pCa,\
        g_pK, Buf_c, Buf_sr, Ca_o, K_buf_c, K_buf_sr, K_up, V_leak, V_sr,\
        Vmax_up, a_rel, b_rel, c_rel, tau_g, Na_o, Cm, F, R, T, V_c,\
        stim_amplitude, stim_duration, stim_period, stim_start, K_o =\
        dolfin.split(parameters)

    # Reversal potentials
    E_Na = R*T*ufl.ln(Na_o/Na_i)/F
    E_K = R*T*ufl.ln(K_o/K_i)/F
    E_Ks = R*T*ufl.ln((Na_o*P_kna + K_o)/(Na_i*P_kna + K_i))/F
    E_Ca = 0.5*R*T*ufl.ln(Ca_o/Ca_i)/F

    # Inward rectifier potassium current
    alpha_K1 = 0.1/(1.0 + 6.14421235332821e-6*ufl.exp(0.06*V - 0.06*E_K))
    beta_K1 = (3.06060402008027*ufl.exp(0.0002*V - 0.0002*E_K) +\
        0.367879441171442*ufl.exp(0.1*V - 0.1*E_K))/(1.0 + ufl.exp(0.5*E_K -\
        0.5*V))
    xK1_inf = alpha_K1/(alpha_K1 + beta_K1)
    i_K1 = 0.430331482911935*ufl.sqrt(K_o)*(-E_K + V)*g_K1*xK1_inf

    # Rapid time dependent potassium current
    i_Kr = 0.430331482911935*ufl.sqrt(K_o)*(-E_K + V)*Xr1*Xr2*g_Kr

    # Rapid time dependent potassium current xr1 gate
    xr1_inf = 1.0/(1.0 + 0.0243728440732796*ufl.exp(-0.142857142857143*V))
    alpha_xr1 = 450.0/(1.0 + ufl.exp(-9/2 - V/10.0))
    beta_xr1 = 6.0/(1.0 + 13.5813245225782*ufl.exp(0.0869565217391304*V))
    tau_xr1 = alpha_xr1*beta_xr1

    # Rapid time dependent potassium current xr2 gate
    xr2_inf = 1.0/(1.0 + 39.1212839981532*ufl.exp(0.0416666666666667*V))
    alpha_xr2 = 3.0/(1.0 + 0.0497870683678639*ufl.exp(-0.05*V))
    beta_xr2 = 1.12/(1.0 + 0.0497870683678639*ufl.exp(0.05*V))
    tau_xr2 = alpha_xr2*beta_xr2

    # Slow time dependent potassium current
    i_Ks = (Xs*Xs)*(V - E_Ks)*g_Ks

    # Slow time dependent potassium current xs gate
    xs_inf = 1.0/(1.0 + 0.69967253737513*ufl.exp(-0.0714285714285714*V))
    alpha_xs = 1100.0/ufl.sqrt(1.0 +\
        0.188875602837562*ufl.exp(-0.166666666666667*V))
    beta_xs = 1.0/(1.0 + 0.0497870683678639*ufl.exp(0.05*V))
    tau_xs = alpha_xs*beta_xs

    # Fast sodium current
    i_Na = (m*m*m)*(-E_Na + V)*g_Na*h*j

    # Fast sodium current m gate
    m_inf = 1.0/((1.0 +\
        0.00184221158116513*ufl.exp(-0.110741971207087*V))*(1.0 +\
        0.00184221158116513*ufl.exp(-0.110741971207087*V)))
    alpha_m = 1.0/(1.0 + ufl.exp(-12.0 - V/5.0))
    beta_m = 0.1/(1.0 + 0.778800783071405*ufl.exp(0.005*V)) + 0.1/(1.0 +\
        ufl.exp(7.0 + V/5.0))
    tau_m = alpha_m*beta_m

    # Fast sodium current h gate
    h_inf = 1.0/((1.0 + 15212.5932856544*ufl.exp(0.134589502018843*V))*(1.0 +\
        15212.5932856544*ufl.exp(0.134589502018843*V)))
    alpha_h = 4.43126792958051e-7*ufl.exp(-0.147058823529412*V)/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V))
    beta_h = (310000.0*ufl.exp(0.3485*V) + 2.7*ufl.exp(0.079*V))/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V)) + 0.77*(1.0 - 1.0/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V)))/(0.13 +\
        0.0497581410839387*ufl.exp(-0.0900900900900901*V))
    tau_h = 1.0/(alpha_h + beta_h)

    # Fast sodium current j gate
    j_inf = 1.0/((1.0 + 15212.5932856544*ufl.exp(0.134589502018843*V))*(1.0 +\
        15212.5932856544*ufl.exp(0.134589502018843*V)))
    alpha_j = (37.78 + V)*(-6.948e-6*ufl.exp(-0.04391*V) -\
        25428.0*ufl.exp(0.2444*V))/((1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V))*(1.0 +\
        50262745825.954*ufl.exp(0.311*V)))
    beta_j = 0.6*(1.0 - 1.0/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V)))*ufl.exp(0.057*V)/(1.0 +\
        0.0407622039783662*ufl.exp(-0.1*V)) +\
        0.02424*ufl.exp(-0.01052*V)/((1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V))*(1.0 +\
        0.00396086833990426*ufl.exp(-0.1378*V)))
    tau_j = 1.0/(alpha_j + beta_j)

    # Sodium background current
    i_b_Na = (-E_Na + V)*g_bna

    # L type ca current
    i_CaL = 4.0*(F*F)*(-0.341*Ca_o +\
        Ca_i*ufl.exp(2.0*F*V/(R*T)))*V*d*f*fCa*g_CaL/((-1.0 +\
        ufl.exp(2.0*F*V/(R*T)))*R*T)

    # L type ca current d gate
    d_inf = 1.0/(1.0 + 0.513417119032592*ufl.exp(-0.133333333333333*V))
    alpha_d = 0.25 + 1.4/(1.0 +\
        0.0677244716592409*ufl.exp(-0.0769230769230769*V))
    beta_d = 1.4/(1.0 + ufl.exp(1.0 + V/5.0))
    gamma_d = 1.0/(1.0 + 12.1824939607035*ufl.exp(-0.05*V))
    tau_d = gamma_d + alpha_d*beta_d

    # L type ca current f gate
    f_inf = 1.0/(1.0 + 17.4117080633276*ufl.exp(0.142857142857143*V))
    tau_f = 80.0 + 165.0/(1.0 + ufl.exp(5/2 - V/10.0)) +\
        1125.0*ufl.exp(-0.00416666666666667*((27.0 + V)*(27.0 + V)))

    # L type ca current fca gate
    alpha_fCa = 1.0/(1.0 + 8.03402376701711e+27*ufl.elem_pow(Ca_i, 8.0))
    beta_fCa = 0.1/(1.0 + 0.00673794699908547*ufl.exp(10000.0*Ca_i))
    gama_fCa = 0.2/(1.0 + 0.391605626676799*ufl.exp(1250.0*Ca_i))
    fCa_inf = 0.157534246575342 + 0.684931506849315*gama_fCa +\
        0.684931506849315*beta_fCa + 0.684931506849315*alpha_fCa
    tau_fCa = 2.0
    d_fCa = (-fCa + fCa_inf)/tau_fCa

    # Calcium background current
    i_b_Ca = (V - E_Ca)*g_bca

    # Transient outward current
    i_to = (-E_K + V)*g_to*r*s

    # Transient outward current s gate
    s_inf = 1.0/(1.0 + ufl.exp(4.0 + V/5.0))
    tau_s = 3.0 + 85.0*ufl.exp(-0.003125*((45.0 + V)*(45.0 + V))) + 5.0/(1.0 +\
        ufl.exp(-4.0 + V/5.0))

    # Transient outward current r gate
    r_inf = 1.0/(1.0 + 28.0316248945261*ufl.exp(-0.166666666666667*V))
    tau_r = 0.8 + 9.5*ufl.exp(-0.000555555555555556*((40.0 + V)*(40.0 + V)))

    # Sodium potassium pump current
    i_NaK = K_o*Na_i*P_NaK/((K_mk + K_o)*(Na_i + K_mNa)*(1.0 +\
        0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

    # Sodium calcium exchanger current
    i_NaCa = (-(Na_o*Na_o*Na_o)*Ca_i*alpha*ufl.exp((-1.0 + gamma)*F*V/(R*T))\
        + (Na_i*Na_i*Na_i)*Ca_o*ufl.exp(F*V*gamma/(R*T)))*K_NaCa/((1.0 +\
        K_sat*ufl.exp((-1.0 + gamma)*F*V/(R*T)))*((Na_o*Na_o*Na_o) +\
        (Km_Nai*Km_Nai*Km_Nai))*(Km_Ca + Ca_o))

    # Calcium pump current
    i_p_Ca = Ca_i*g_pCa/(K_pCa + Ca_i)

    # Potassium pump current
    i_p_K = (-E_K + V)*g_pK/(1.0 +\
        65.4052157419383*ufl.exp(-0.167224080267559*V))

    # Calcium dynamics
    i_rel = ((Ca_SR*Ca_SR)*a_rel/((Ca_SR*Ca_SR) + (b_rel*b_rel)) + c_rel)*d*g
    i_up = Vmax_up/(1.0 + (K_up*K_up)/(Ca_i*Ca_i))
    i_leak = (-Ca_i + Ca_SR)*V_leak
    g_inf = (1.0 - 1.0/(1.0 + 0.0301973834223185*ufl.exp(10000.0*Ca_i)))/(1.0 +\
        1.97201988740492e+55*ufl.elem_pow(Ca_i, 16.0)) + 1.0/((1.0 +\
        0.0301973834223185*ufl.exp(10000.0*Ca_i))*(1.0 +\
        5.43991024148102e+20*ufl.elem_pow(Ca_i, 6.0)))
    d_g = (-g + g_inf)/tau_g
    Ca_i_bufc = 1.0/(1.0 + Buf_c*K_buf_c/((K_buf_c + Ca_i)*(K_buf_c + Ca_i)))
    Ca_sr_bufsr = 1.0/(1.0 + Buf_sr*K_buf_sr/((K_buf_sr + Ca_SR)*(K_buf_sr +\
        Ca_SR)))

    # Sodium dynamics

    # Membrane
    i_Stim = -(1.0 - 1.0/(1.0 + ufl.exp(-5.0*stim_start +\
        5.0*time)))*stim_amplitude/(1.0 + ufl.exp(-5.0*stim_start + 5.0*time\
        - 5.0*stim_duration))

    # Potassium dynamics

    # The ODE system: 17 states

    # Init test function
    _v = dolfin.TestFunction(states.function_space())

    # Derivative for state Xr1
    dy = ((-Xr1 + xr1_inf)/tau_xr1)*_v[0]

    # Derivative for state Xr2
    dy += ((-Xr2 + xr2_inf)/tau_xr2)*_v[1]

    # Derivative for state Xs
    dy += ((-Xs + xs_inf)/tau_xs)*_v[2]

    # Derivative for state m
    dy += ((-m + m_inf)/tau_m)*_v[3]

    # Derivative for state h
    dy += ((-h + h_inf)/tau_h)*_v[4]

    # Derivative for state j
    dy += ((j_inf - j)/tau_j)*_v[5]

    # Derivative for state d
    dy += ((d_inf - d)/tau_d)*_v[6]

    # Derivative for state f
    dy += ((-f + f_inf)/tau_f)*_v[7]

    # Derivative for state fCa
    dy += ((1.0 - 1.0/((1.0 + ufl.exp(60.0 + V))*(1.0 + ufl.exp(-10.0*fCa +\
        10.0*fCa_inf))))*d_fCa)*_v[8]

    # Derivative for state s
    dy += ((-s + s_inf)/tau_s)*_v[9]

    # Derivative for state r
    dy += ((-r + r_inf)/tau_r)*_v[10]

    # Derivative for state Ca_SR
    dy += ((-i_leak + i_up - i_rel)*Ca_sr_bufsr*V_c/V_sr)*_v[11]

    # Derivative for state Ca_i
    dy += ((-i_up - (i_CaL + i_p_Ca + i_b_Ca - 2.0*i_NaCa)*Cm/(2.0*F*V_c) +\
        i_leak + i_rel)*Ca_i_bufc)*_v[12]

    # Derivative for state g
    dy += ((1.0 - 1.0/((1.0 + ufl.exp(60.0 + V))*(1.0 + ufl.exp(-10.0*g +\
        10.0*g_inf))))*d_g)*_v[13]

    # Derivative for state Na_i
    dy += ((-3.0*i_NaK - 3.0*i_NaCa - i_Na - i_b_Na)*Cm/(F*V_c))*_v[14]

    # Derivative for state V
    dy += (-i_Ks - i_to - i_Kr - i_p_K - i_NaK - i_NaCa - i_Na - i_p_Ca -\
        i_b_Na - i_CaL - i_Stim - i_K1 - i_b_Ca)*_v[15]

    # Derivative for state K_i
    dy += ((-i_Ks - i_to - i_Kr - i_p_K - i_Stim - i_K1 +\
        2.0*i_NaK)*Cm/(F*V_c))*_v[16]

    # Return dy
    return dy
Example #33
0
    def _I(self, v, s, time):
        """
        Original gotran transmembrane current dV/dt
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert (len(s) == 17)
        m, h, j, d, f1, f2, fCa, Xr1, Xr2, Xs, Xf, q, r, Nai, g, Cai, Ca_SR = s

        # Assign parameters
        Chromanol_iKs30 = self._parameters["Chromanol_iKs30"]
        Chromanol_iKs50 = self._parameters["Chromanol_iKs50"]
        Chromanol_iKs70 = self._parameters["Chromanol_iKs70"]
        Chromanol_iKs90 = self._parameters["Chromanol_iKs90"]
        E4031_100nM = self._parameters["E4031_100nM"]
        E4031_30nM = self._parameters["E4031_30nM"]
        TTX_10uM = self._parameters["TTX_10uM"]
        TTX_30uM = self._parameters["TTX_30uM"]
        TTX_3uM = self._parameters["TTX_3uM"]
        nifed_100nM = self._parameters["nifed_100nM"]
        nifed_10nM = self._parameters["nifed_10nM"]
        nifed_30nM = self._parameters["nifed_30nM"]
        nifed_3nM = self._parameters["nifed_3nM"]
        PkNa = self._parameters["PkNa"]
        g_Na = self._parameters["g_Na"]
        g_CaL = self._parameters["g_CaL"]
        g_Kr = self._parameters["g_Kr"]
        g_Ks = self._parameters["g_Ks"]
        g_K1 = self._parameters["g_K1"]
        E_f = self._parameters["E_f"]
        g_f = self._parameters["g_f"]
        g_b_Na = self._parameters["g_b_Na"]
        g_b_Ca = self._parameters["g_b_Ca"]
        Km_K = self._parameters["Km_K"]
        Km_Na = self._parameters["Km_Na"]
        PNaK = self._parameters["PNaK"]
        KmCa = self._parameters["KmCa"]
        KmNai = self._parameters["KmNai"]
        Ksat = self._parameters["Ksat"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        kNaCa = self._parameters["kNaCa"]
        KPCa = self._parameters["KPCa"]
        g_PCa = self._parameters["g_PCa"]
        g_to = self._parameters["g_to"]
        Cao = self._parameters["Cao"]
        F = self._parameters["F"]
        Ki = self._parameters["Ki"]
        Ko = self._parameters["Ko"]
        Nao = self._parameters["Nao"]
        R = self._parameters["R"]
        T = self._parameters["T"]

        # Init return args
        current = [ufl.zero()] * 1

        # Expressions for the Electric potentials component
        E_Na = R * T * ufl.ln(Nao / Nai) / F
        E_K = R * T * ufl.ln(Ko / Ki) / F
        E_Ks = R * T * ufl.ln((Ko + Nao * PkNa) / (Ki + PkNa * Nai)) / F
        E_Ca = 0.5 * R * T * ufl.ln(Cao / Cai) / F

        # Expressions for the i_Na component
        TTX_coeff = ufl.conditional(ufl.eq(TTX_3uM, 1), 0.18,\
            ufl.conditional(ufl.eq(TTX_10uM, 1), 0.06,\
            ufl.conditional(ufl.eq(TTX_30uM, 1), 0.02, 1)))
        i_Na = g_Na * (m * m * m) * (-E_Na + V) * TTX_coeff * h * j

        # Expressions for the i_CaL component
        nifed_coeff = ufl.conditional(ufl.eq(nifed_3nM, 1), 0.93,\
            ufl.conditional(ufl.eq(nifed_10nM, 1), 0.79,\
            ufl.conditional(ufl.eq(nifed_30nM, 1), 0.56,\
            ufl.conditional(ufl.eq(nifed_100nM, 1), 0.28, 1))))
        i_CaL = 4*g_CaL*(F*F)*(-0.341*Cao +\
            Cai*ufl.exp(2*F*V/(R*T)))*V*d*f1*f2*fCa*nifed_coeff/(R*T*(-1 +\
            ufl.exp(2*F*V/(R*T))))

        # Expressions for the i_Kr component
        E4031_coeff = ufl.conditional(ufl.eq(E4031_30nM, 1), 0.77,\
            ufl.conditional(ufl.eq(E4031_100nM, 1), 0.5, 1))
        i_Kr = 0.430331482912 * g_Kr * ufl.sqrt(Ko) * (
            -E_K + V) * E4031_coeff * Xr1 * Xr2

        # Expressions for the i_Ks component
        Chromanol_coeff = ufl.conditional(ufl.eq(Chromanol_iKs30, 1), 0.7,\
            ufl.conditional(ufl.eq(Chromanol_iKs50, 1), 0.5,\
            ufl.conditional(ufl.eq(Chromanol_iKs70, 1), 0.3,\
            ufl.conditional(ufl.eq(Chromanol_iKs90, 1), 0.1, 1))))
        i_Ks = g_Ks*(Xs*Xs)*(1 + 0.6/(1 +\
            6.48182102606e-07*ufl.elem_pow(1.0/Cai, 1.4)))*(-E_Ks +\
            V)*Chromanol_coeff

        # Expressions for the i_K1 component
        alpha_K1 = 3.91 / (
            1 + 2.44592857399e-52 * ufl.exp(594.2 * V - 594.2 * E_K))
        beta_K1 = (0.00277806676906*ufl.exp(588.6*V - 588.6*E_K) -\
            1.5394838221*ufl.exp(0.2*V - 0.2*E_K))/(1 + ufl.exp(454.7*V -\
            454.7*E_K))
        XK1_inf = alpha_K1 / (alpha_K1 + beta_K1)
        i_K1 = 0.430331482912 * g_K1 * ufl.sqrt(Ko) * (-E_K + V) * XK1_inf

        # Expressions for the i_f component
        i_f = g_f * (-E_f + V) * Xf

        # Expressions for the i_b Na component
        i_b_Na = g_b_Na * (-E_Na + V)

        # Expressions for the i_b Ca component
        i_b_Ca = g_b_Ca * (-E_Ca + V)

        # Expressions for the i_NaK component
        i_NaK = Ko*PNaK*Nai/((Km_K + Ko)*(Km_Na + Nai)*(1 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the i_NaCa component
        i_NaCa = kNaCa*(Cao*(Nai*Nai*Nai)*ufl.exp(F*gamma*V/(R*T)) -\
            alpha*(Nao*Nao*Nao)*Cai*ufl.exp(F*(-1 + gamma)*V/(R*T)))/((1 +\
            Ksat*ufl.exp(F*(-1 + gamma)*V/(R*T)))*(Cao +\
            KmCa)*((KmNai*KmNai*KmNai) + (Nao*Nao*Nao)))

        # Expressions for the i_PCa component
        i_PCa = g_PCa * Cai / (KPCa + Cai)

        # Expressions for the i_to component
        i_to = g_to * (-E_K + V) * q * r

        # Expressions for the Membrane component
        current[0] = -i_CaL - i_K1 - i_Kr - i_Ks - i_Na - i_NaCa - i_NaK -\
            i_PCa - i_b_Ca - i_b_Na - i_f - i_to

        # Return results
        return current[0]
Example #34
0
    def _I(self, v, s, time):
        """
        Original gotran transmembrane current dV/dt
        """
        time = time if time else Constant(0.0)

        # Assign states
        V = v
        assert (len(s) == 18)
        Xr1, Xr2, Xs, m, h, j, d, f, f2, fCass, s, r, Ca_SR, Ca_i, Ca_ss,\
            R_prime, Na_i, K_i = s

        # Assign parameters
        P_kna = self._parameters["P_kna"]
        g_K1 = self._parameters["g_K1"]
        g_Kr = self._parameters["g_Kr"]
        g_Ks = self._parameters["g_Ks"]
        g_Na = self._parameters["g_Na"]
        g_bna = self._parameters["g_bna"]
        g_CaL = self._parameters["g_CaL"]
        g_bca = self._parameters["g_bca"]
        g_to = self._parameters["g_to"]
        K_mNa = self._parameters["K_mNa"]
        K_mk = self._parameters["K_mk"]
        P_NaK = self._parameters["P_NaK"]
        K_NaCa = self._parameters["K_NaCa"]
        K_sat = self._parameters["K_sat"]
        Km_Ca = self._parameters["Km_Ca"]
        Km_Nai = self._parameters["Km_Nai"]
        alpha = self._parameters["alpha"]
        gamma = self._parameters["gamma"]
        K_pCa = self._parameters["K_pCa"]
        g_pCa = self._parameters["g_pCa"]
        g_pK = self._parameters["g_pK"]
        Ca_o = self._parameters["Ca_o"]
        Na_o = self._parameters["Na_o"]
        F = self._parameters["F"]
        R = self._parameters["R"]
        T = self._parameters["T"]
        K_o = self._parameters["K_o"]

        # Init return args
        current = [ufl.zero()] * 1

        # Expressions for the Reversal potentials component
        E_Na = R * T * ufl.ln(Na_o / Na_i) / F
        E_K = R * T * ufl.ln(K_o / K_i) / F
        E_Ks = R * T * ufl.ln((Na_o * P_kna + K_o) / (K_i + P_kna * Na_i)) / F
        E_Ca = 0.5 * R * T * ufl.ln(Ca_o / Ca_i) / F

        # Expressions for the Inward rectifier potassium current component
        alpha_K1 = 0.1 / (1 +
                          6.14421235333e-06 * ufl.exp(-0.06 * E_K + 0.06 * V))
        beta_K1 = (3.06060402008*ufl.exp(0.0002*V - 0.0002*E_K) +\
            0.367879441171*ufl.exp(0.1*V - 0.1*E_K))/(1 + ufl.exp(0.5*E_K -\
            0.5*V))
        xK1_inf = alpha_K1 / (alpha_K1 + beta_K1)
        i_K1 = 0.430331482912 * g_K1 * ufl.sqrt(K_o) * (-E_K + V) * xK1_inf

        # Expressions for the Rapid time dependent potassium current component
        i_Kr = 0.430331482912 * g_Kr * ufl.sqrt(K_o) * (-E_K + V) * Xr1 * Xr2

        # Expressions for the Slow time dependent potassium current component
        i_Ks = g_Ks * (Xs * Xs) * (-E_Ks + V)

        # Expressions for the Fast sodium current component
        i_Na = g_Na * (m * m * m) * (-E_Na + V) * h * j

        # Expressions for the Sodium background current component
        i_b_Na = g_bna * (-E_Na + V)

        # Expressions for the L_type Ca current component
        i_CaL = 4*g_CaL*(F*F)*(-15 + V)*(0.25*Ca_ss*ufl.exp(F*(-30 +\
            2*V)/(R*T)) - Ca_o)*d*f*f2*fCass/(R*T*(-1 + ufl.exp(F*(-30 +\
            2*V)/(R*T))))

        # Expressions for the Calcium background current component
        i_b_Ca = g_bca * (-E_Ca + V)

        # Expressions for the Transient outward current component
        i_to = g_to * (-E_K + V) * r * s

        # Expressions for the Sodium potassium pump current component
        i_NaK = K_o*P_NaK*Na_i/((K_mNa + Na_i)*(K_mk + K_o)*(1 +\
            0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

        # Expressions for the Sodium calcium exchanger current component
        i_NaCa = K_NaCa*(-alpha*(Na_o*Na_o*Na_o)*Ca_i*ufl.exp(F*(-1 +\
            gamma)*V/(R*T)) +\
            Ca_o*(Na_i*Na_i*Na_i)*ufl.exp(F*gamma*V/(R*T)))/((1 +\
            K_sat*ufl.exp(F*(-1 + gamma)*V/(R*T)))*(Km_Ca +\
            Ca_o)*((Na_o*Na_o*Na_o) + (Km_Nai*Km_Nai*Km_Nai)))

        # Expressions for the Calcium pump current component
        i_p_Ca = g_pCa * Ca_i / (Ca_i + K_pCa)

        # Expressions for the Potassium pump current component
        i_p_K = g_pK * (-E_K +
                        V) / (1 + 65.4052157419 * ufl.exp(-0.167224080268 * V))

        # Expressions for the Membrane component
        i_Stim = 0
        current[0] = -i_CaL - i_Ks - i_NaCa - i_b_Na - i_Stim - i_Kr - i_p_Ca\
            - i_to - i_b_Ca - i_Na - i_p_K - i_NaK - i_K1

        # Return results
        return current[0]
def rhs(states, time, parameters, dy=None):
    """
    Compute right hand side
    """
    # Imports
    import ufl
    import dolfin

    # Assign states
    assert(isinstance(states, dolfin.Function))
    assert(states.function_space().depth() == 1)
    assert(states.function_space().num_sub_spaces() == 17)
    Xr1, Xr2, Xs, m, h, j, d, f, fCa, s, r, Ca_SR, Ca_i, g, Na_i, V, K_i =\
        dolfin.split(states)

    # Assign parameters
    assert(isinstance(parameters, (dolfin.Function, dolfin.Constant)))
    if isinstance(parameters, dolfin.Function):
        assert(parameters.function_space().depth() == 1)
        assert(parameters.function_space().num_sub_spaces() == 45)
    else:
        assert(parameters.value_size() == 45)
    P_kna, g_K1, g_Kr, g_Ks, g_Na, g_bna, g_CaL, g_bca, g_to, K_mNa, K_mk,\
        P_NaK, K_NaCa, K_sat, Km_Ca, Km_Nai, alpha, gamma, K_pCa, g_pCa,\
        g_pK, Buf_c, Buf_sr, Ca_o, K_buf_c, K_buf_sr, K_up, V_leak, V_sr,\
        Vmax_up, a_rel, b_rel, c_rel, tau_g, Na_o, Cm, F, R, T, V_c,\
        stim_amplitude, stim_duration, stim_period, stim_start, K_o =\
        dolfin.split(parameters)

    # Reversal potentials
    E_Na = R*T*ufl.ln(Na_o/Na_i)/F
    E_K = R*T*ufl.ln(K_o/K_i)/F
    E_Ks = R*T*ufl.ln((Na_o*P_kna + K_o)/(Na_i*P_kna + K_i))/F
    E_Ca = 0.5*R*T*ufl.ln(Ca_o/Ca_i)/F

    # Inward rectifier potassium current
    alpha_K1 = 0.1/(1.0 + 6.14421235332821e-6*ufl.exp(0.06*V - 0.06*E_K))
    beta_K1 = (3.06060402008027*ufl.exp(0.0002*V - 0.0002*E_K) +\
        0.367879441171442*ufl.exp(0.1*V - 0.1*E_K))/(1.0 + ufl.exp(0.5*E_K -\
        0.5*V))
    xK1_inf = alpha_K1/(alpha_K1 + beta_K1)
    i_K1 = 0.430331482911935*ufl.sqrt(K_o)*(-E_K + V)*g_K1*xK1_inf

    # Rapid time dependent potassium current
    i_Kr = 0.430331482911935*ufl.sqrt(K_o)*(-E_K + V)*Xr1*Xr2*g_Kr

    # Rapid time dependent potassium current xr1 gate
    xr1_inf = 1.0/(1.0 + 0.0243728440732796*ufl.exp(-0.142857142857143*V))
    alpha_xr1 = 450.0/(1.0 + ufl.exp(-9/2 - V/10.0))
    beta_xr1 = 6.0/(1.0 + 13.5813245225782*ufl.exp(0.0869565217391304*V))
    tau_xr1 = alpha_xr1*beta_xr1

    # Rapid time dependent potassium current xr2 gate
    xr2_inf = 1.0/(1.0 + 39.1212839981532*ufl.exp(0.0416666666666667*V))
    alpha_xr2 = 3.0/(1.0 + 0.0497870683678639*ufl.exp(-0.05*V))
    beta_xr2 = 1.12/(1.0 + 0.0497870683678639*ufl.exp(0.05*V))
    tau_xr2 = alpha_xr2*beta_xr2

    # Slow time dependent potassium current
    i_Ks = (Xs*Xs)*(V - E_Ks)*g_Ks

    # Slow time dependent potassium current xs gate
    xs_inf = 1.0/(1.0 + 0.69967253737513*ufl.exp(-0.0714285714285714*V))
    alpha_xs = 1100.0/ufl.sqrt(1.0 +\
        0.188875602837562*ufl.exp(-0.166666666666667*V))
    beta_xs = 1.0/(1.0 + 0.0497870683678639*ufl.exp(0.05*V))
    tau_xs = alpha_xs*beta_xs

    # Fast sodium current
    i_Na = (m*m*m)*(-E_Na + V)*g_Na*h*j

    # Fast sodium current m gate
    m_inf = 1.0/((1.0 +\
        0.00184221158116513*ufl.exp(-0.110741971207087*V))*(1.0 +\
        0.00184221158116513*ufl.exp(-0.110741971207087*V)))
    alpha_m = 1.0/(1.0 + ufl.exp(-12.0 - V/5.0))
    beta_m = 0.1/(1.0 + 0.778800783071405*ufl.exp(0.005*V)) + 0.1/(1.0 +\
        ufl.exp(7.0 + V/5.0))
    tau_m = alpha_m*beta_m

    # Fast sodium current h gate
    h_inf = 1.0/((1.0 + 15212.5932856544*ufl.exp(0.134589502018843*V))*(1.0 +\
        15212.5932856544*ufl.exp(0.134589502018843*V)))
    alpha_h = 4.43126792958051e-7*ufl.exp(-0.147058823529412*V)/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V))
    beta_h = (310000.0*ufl.exp(0.3485*V) + 2.7*ufl.exp(0.079*V))/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V)) + 0.77*(1.0 - 1.0/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V)))/(0.13 +\
        0.0497581410839387*ufl.exp(-0.0900900900900901*V))
    tau_h = 1.0/(alpha_h + beta_h)

    # Fast sodium current j gate
    j_inf = 1.0/((1.0 + 15212.5932856544*ufl.exp(0.134589502018843*V))*(1.0 +\
        15212.5932856544*ufl.exp(0.134589502018843*V)))
    alpha_j = (37.78 + V)*(-6.948e-6*ufl.exp(-0.04391*V) -\
        25428.0*ufl.exp(0.2444*V))/((1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V))*(1.0 +\
        50262745825.954*ufl.exp(0.311*V)))
    beta_j = 0.6*(1.0 - 1.0/(1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V)))*ufl.exp(0.057*V)/(1.0 +\
        0.0407622039783662*ufl.exp(-0.1*V)) +\
        0.02424*ufl.exp(-0.01052*V)/((1.0 +\
        2.3538526683702e+17*ufl.exp(1.0*V))*(1.0 +\
        0.00396086833990426*ufl.exp(-0.1378*V)))
    tau_j = 1.0/(alpha_j + beta_j)

    # Sodium background current
    i_b_Na = (-E_Na + V)*g_bna

    # L type ca current
    i_CaL = 4.0*(F*F)*(-0.341*Ca_o +\
        Ca_i*ufl.exp(2.0*F*V/(R*T)))*V*d*f*fCa*g_CaL/((-1.0 +\
        ufl.exp(2.0*F*V/(R*T)))*R*T)

    # L type ca current d gate
    d_inf = 1.0/(1.0 + 0.513417119032592*ufl.exp(-0.133333333333333*V))
    alpha_d = 0.25 + 1.4/(1.0 +\
        0.0677244716592409*ufl.exp(-0.0769230769230769*V))
    beta_d = 1.4/(1.0 + ufl.exp(1.0 + V/5.0))
    gamma_d = 1.0/(1.0 + 12.1824939607035*ufl.exp(-0.05*V))
    tau_d = gamma_d + alpha_d*beta_d

    # L type ca current f gate
    f_inf = 1.0/(1.0 + 17.4117080633276*ufl.exp(0.142857142857143*V))
    tau_f = 80.0 + 165.0/(1.0 + ufl.exp(5/2 - V/10.0)) +\
        1125.0*ufl.exp(-0.00416666666666667*((27.0 + V)*(27.0 + V)))

    # L type ca current fca gate
    alpha_fCa = 1.0/(1.0 + 8.03402376701711e+27*ufl.elem_pow(Ca_i, 8.0))
    beta_fCa = 0.1/(1.0 + 0.00673794699908547*ufl.exp(10000.0*Ca_i))
    gama_fCa = 0.2/(1.0 + 0.391605626676799*ufl.exp(1250.0*Ca_i))
    fCa_inf = 0.157534246575342 + 0.684931506849315*gama_fCa +\
        0.684931506849315*beta_fCa + 0.684931506849315*alpha_fCa
    tau_fCa = 2.0
    d_fCa = (-fCa + fCa_inf)/tau_fCa

    # Calcium background current
    i_b_Ca = (V - E_Ca)*g_bca

    # Transient outward current
    i_to = (-E_K + V)*g_to*r*s

    # Transient outward current s gate
    s_inf = 1.0/(1.0 + ufl.exp(4.0 + V/5.0))
    tau_s = 3.0 + 85.0*ufl.exp(-0.003125*((45.0 + V)*(45.0 + V))) + 5.0/(1.0 +\
        ufl.exp(-4.0 + V/5.0))

    # Transient outward current r gate
    r_inf = 1.0/(1.0 + 28.0316248945261*ufl.exp(-0.166666666666667*V))
    tau_r = 0.8 + 9.5*ufl.exp(-0.000555555555555556*((40.0 + V)*(40.0 + V)))

    # Sodium potassium pump current
    i_NaK = K_o*Na_i*P_NaK/((K_mk + K_o)*(Na_i + K_mNa)*(1.0 +\
        0.0353*ufl.exp(-F*V/(R*T)) + 0.1245*ufl.exp(-0.1*F*V/(R*T))))

    # Sodium calcium exchanger current
    i_NaCa = (-(Na_o*Na_o*Na_o)*Ca_i*alpha*ufl.exp((-1.0 + gamma)*F*V/(R*T))\
        + (Na_i*Na_i*Na_i)*Ca_o*ufl.exp(F*V*gamma/(R*T)))*K_NaCa/((1.0 +\
        K_sat*ufl.exp((-1.0 + gamma)*F*V/(R*T)))*((Na_o*Na_o*Na_o) +\
        (Km_Nai*Km_Nai*Km_Nai))*(Km_Ca + Ca_o))

    # Calcium pump current
    i_p_Ca = Ca_i*g_pCa/(K_pCa + Ca_i)

    # Potassium pump current
    i_p_K = (-E_K + V)*g_pK/(1.0 +\
        65.4052157419383*ufl.exp(-0.167224080267559*V))

    # Calcium dynamics
    i_rel = ((Ca_SR*Ca_SR)*a_rel/((Ca_SR*Ca_SR) + (b_rel*b_rel)) + c_rel)*d*g
    i_up = Vmax_up/(1.0 + (K_up*K_up)/(Ca_i*Ca_i))
    i_leak = (-Ca_i + Ca_SR)*V_leak
    g_inf = (1.0 - 1.0/(1.0 + 0.0301973834223185*ufl.exp(10000.0*Ca_i)))/(1.0 +\
        1.97201988740492e+55*ufl.elem_pow(Ca_i, 16.0)) + 1.0/((1.0 +\
        0.0301973834223185*ufl.exp(10000.0*Ca_i))*(1.0 +\
        5.43991024148102e+20*ufl.elem_pow(Ca_i, 6.0)))
    d_g = (-g + g_inf)/tau_g
    Ca_i_bufc = 1.0/(1.0 + Buf_c*K_buf_c/((K_buf_c + Ca_i)*(K_buf_c + Ca_i)))
    Ca_sr_bufsr = 1.0/(1.0 + Buf_sr*K_buf_sr/((K_buf_sr + Ca_SR)*(K_buf_sr +\
        Ca_SR)))

    # Sodium dynamics

    # Membrane
    i_Stim = -(1.0 - 1.0/(1.0 + ufl.exp(-5.0*stim_start +\
        5.0*time)))*stim_amplitude/(1.0 + ufl.exp(-5.0*stim_start + 5.0*time\
        - 5.0*stim_duration))

    # Potassium dynamics

    # The ODE system: 17 states

    # Init test function
    _v = dolfin.TestFunction(states.function_space())

    # Derivative for state Xr1
    dy = ((-Xr1 + xr1_inf)/tau_xr1)*_v[0]

    # Derivative for state Xr2
    dy += ((-Xr2 + xr2_inf)/tau_xr2)*_v[1]

    # Derivative for state Xs
    dy += ((-Xs + xs_inf)/tau_xs)*_v[2]

    # Derivative for state m
    dy += ((-m + m_inf)/tau_m)*_v[3]

    # Derivative for state h
    dy += ((-h + h_inf)/tau_h)*_v[4]

    # Derivative for state j
    dy += ((j_inf - j)/tau_j)*_v[5]

    # Derivative for state d
    dy += ((d_inf - d)/tau_d)*_v[6]

    # Derivative for state f
    dy += ((-f + f_inf)/tau_f)*_v[7]

    # Derivative for state fCa
    dy += ((1.0 - 1.0/((1.0 + ufl.exp(60.0 + V))*(1.0 + ufl.exp(-10.0*fCa +\
        10.0*fCa_inf))))*d_fCa)*_v[8]

    # Derivative for state s
    dy += ((-s + s_inf)/tau_s)*_v[9]

    # Derivative for state r
    dy += ((-r + r_inf)/tau_r)*_v[10]

    # Derivative for state Ca_SR
    dy += ((-i_leak + i_up - i_rel)*Ca_sr_bufsr*V_c/V_sr)*_v[11]

    # Derivative for state Ca_i
    dy += ((-i_up - (i_CaL + i_p_Ca + i_b_Ca - 2.0*i_NaCa)*Cm/(2.0*F*V_c) +\
        i_leak + i_rel)*Ca_i_bufc)*_v[12]

    # Derivative for state g
    dy += ((1.0 - 1.0/((1.0 + ufl.exp(60.0 + V))*(1.0 + ufl.exp(-10.0*g +\
        10.0*g_inf))))*d_g)*_v[13]

    # Derivative for state Na_i
    dy += ((-3.0*i_NaK - 3.0*i_NaCa - i_Na - i_b_Na)*Cm/(F*V_c))*_v[14]

    # Derivative for state V
    dy += (-i_Ks - i_to - i_Kr - i_p_K - i_NaK - i_NaCa - i_Na - i_p_Ca -\
        i_b_Na - i_CaL - i_Stim - i_K1 - i_b_Ca)*_v[15]

    # Derivative for state K_i
    dy += ((-i_Ks - i_to - i_Kr - i_p_K - i_Stim - i_K1 +\
        2.0*i_NaK)*Cm/(F*V_c))*_v[16]

    # Return dy
    return dy