Beispiel #1
0
    def test_solve(self):
        "Test that solver runs."
        self.setUp()

        # Create solver and solve
        solver = BidomainSolver(self.mesh,
                                self.time,
                                self.M_i,
                                self.M_e,
                                I_s=self.stimulus,
                                I_a=self.applied_current)
        solutions = solver.solve((self.t0, self.t0 + 2 * self.dt), self.dt)
        for (interval, fields) in solutions:
            (v_, vur) = fields
Beispiel #2
0
def main(N, dt, T, theta):

    # Create data
    mesh = UnitSquareMesh(N, N)
    time = Constant(0.0)
    ac_str = "cos(t)*cos(pi*x[0])*cos(pi*x[1]) + pow(pi, 2)*cos(pi*x[0])*cos(pi*x[1])*sin(t)"
    stimulus = Expression(ac_str, t=time, degree=5)
    M_i = 1.
    M_e = 1.0

    # Set-up solver
    params = BidomainSolver.default_parameters()
    params["theta"] = theta
    params["linear_solver_type"] = "direct"
    params["use_avg_u_constraint"] = True
    params["enable_adjoint"] = False
    solver = BidomainSolver(mesh, time, M_i, M_e, I_s=stimulus, params=params)

    # Define exact solution (Note: v is returned at end of time
    # interval(s), u is computed at somewhere in the time interval
    # depending on theta)
    v_exact = Expression("cos(pi*x[0])*cos(pi*x[1])*sin(t)", t=T, degree=3)
    u_exact = Expression("-cos(pi*x[0])*cos(pi*x[1])*sin(t)/2.0",
                         t=T - (1. - theta) * dt,
                         degree=3)

    # Define initial condition(s)
    (v_, vu) = solver.solution_fields()

    # Solve
    solutions = solver.solve((0, T), dt)
    for (interval, fields) in solutions:
        continue

    # Compute errors
    (v, u) = vu.split(deepcopy=True)[0:2]

    v_error = errornorm(v_exact, v, "L2", degree_rise=2)
    u_error = errornorm(u_exact, u, "L2", degree_rise=2)
    return [v_error, u_error, mesh.hmin(), dt, T]
Beispiel #3
0
    def test_compare_direct_iterative(self):
        "Test that direct and iterative solution give comparable results."
        self.setUp()

        # Create solver and solve
        params = BidomainSolver.default_parameters()
        params["linear_solver_type"] = "direct"
        params["use_avg_u_constraint"] = True
        solver = BidomainSolver(self.mesh,
                                self.time,
                                self.M_i,
                                self.M_e,
                                I_s=self.stimulus,
                                I_a=self.applied_current,
                                params=params)
        solutions = solver.solve((self.t0, self.t0 + 3 * self.dt), self.dt)
        for (interval, fields) in solutions:
            (v_, vur) = fields
            (v, u, r) = vur.split(deepcopy=True)
            a = v.vector().norm("l2")

        # Create solver and solve using iterative means
        params = BidomainSolver.default_parameters()
        params["petsc_krylov_solver"]["monitor_convergence"] = True
        solver = BidomainSolver(self.mesh,
                                self.time,
                                self.M_i,
                                self.M_e,
                                I_s=self.stimulus,
                                I_a=self.applied_current,
                                params=params)
        solutions = solver.solve((self.t0, self.t0 + 3 * self.dt), self.dt)
        for (interval, fields) in solutions:
            (v_, vu) = fields
            (v, u) = vu.split(deepcopy=True)
            b = v.vector().norm("l2")

        print "lu gives ", a
        print "krylov gives ", b
        assert_almost_equal(a, b, 1e-4)
Beispiel #4
0
    def test_compare_with_basic_solve(self):
        """Test that solver with direct linear algebra gives same
        results as basic bidomain solver."""
        self.setUp()

        # Create solver and solve
        params = BidomainSolver.default_parameters()
        params["linear_solver_type"] = "direct"
        params["use_avg_u_constraint"] = True
        solver = BidomainSolver(self.mesh,
                                self.time,
                                self.M_i,
                                self.M_e,
                                I_s=self.stimulus,
                                I_a=self.applied_current,
                                params=params)
        solutions = solver.solve((self.t0, self.t0 + 2 * self.dt), self.dt)
        for (interval, fields) in solutions:
            (v_, vur) = fields
        bidomain_result = vur.vector().norm("l2")

        # Create other solver and solve
        solver = BasicBidomainSolver(self.mesh,
                                     self.time,
                                     self.M_i,
                                     self.M_e,
                                     I_s=self.stimulus,
                                     I_a=self.applied_current)
        solutions = solver.solve((self.t0, self.t0 + 2 * self.dt), self.dt)
        for (interval, fields) in solutions:
            (v_, vur) = fields
        basic_bidomain_result = vur.vector().norm("l2")

        print bidomain_result
        print basic_bidomain_result
        assert_almost_equal(bidomain_result, basic_bidomain_result, 1e-13)