def test_default_basic_single_cell_solver(self, cell_model, theta): "Test basic single cell solver." time = Constant(0.0) model = cell_model Model = cell_model.__class__ if Model == supported_cell_models[3] and theta > 0: pytest.xfail("failing configuration (but should work)") model.stimulus = Expression("1000*t", t=time, degree=1) info_green("\nTesting %s" % model) vec_solve = self._run_solve(model, time, theta) if Model == supported_cell_models[3] and theta == 0: pytest.xfail("failing configuration (but should work)") if Model in self.references and theta in self.references[Model]: ind, ref_value = self.references[Model][theta] print("vec_solve", vec_solve.get_local()) print("ind", ind, "ref", ref_value) assert_almost_equal(vec_solve[ind], ref_value, 1e-10) else: info_red("Missing references for %r, %r" % (Model, theta))
def _setup_solver(self, Model, Scheme, time=0.0, stim=None, params=None): ''' Generate a new solver object with the given start time, stimulus and parameters. ''' # Create model instance if isinstance(Model, str): Model = eval(Model) model = Model(params=params) # Initialize time and stimulus (note t=time construction!) if stim is None: stim = Expression("1000*t", t=time, degree=1) # Initialize solver mesh = UnitIntervalMesh(5) params = CardiacODESolver.default_parameters() params["scheme"] = Scheme solver = CardiacODESolver(mesh, time, model, I_s=stim, params=params) # Create scheme info_green("\nTesting %s with %s scheme" % (model, Scheme)) # Start with native initial conditions (vs_, vs) = solver.solution_fields() vs_.assign(model.initial_conditions()) vs.assign(vs_) return solver
def test_ReplayOfSplittingSolver_IsExact(self, Solver, solver_type, tol): """Test that basic and optimised splitting solvers yield very comparative results when configured identically.""" self.tlm_adj_setup(Solver, solver_type) info_green("Replaying") success = replay_dolfin(tol=tol, stop=True) assert success
def test_AdjointModelOfSplittingSolver_PassesTaylorTest(self, Solver, solver_type): """Test that basic and optimised splitting solvers yield very comparative results when configured identically.""" J, Jhat, m, Jics = self.tlm_adj_setup(Solver, solver_type) # Check adjoint model correctness info_green("Compute gradient with adjoint linear model") dJdics = compute_gradient(J, m, forget=False) assert (dJdics is not None), "Gradient is None (#fail)." conv_rate = taylor_test(Jhat, m, Jics, dJdics, seed=1.) # Check that minimal convergence rate is greater than some given number assert_greater(conv_rate, 1.9)
def _setup_solver(self, Model, Scheme, mesh, time, stim=None, params=None): # Create model instance model = Model(params=params) # Initialize time and stimulus (note t=time construction!) if stim is None: stim = Expression("1000*t", t=time, degree=1) # Initialize solver params = CardiacODESolver.default_parameters() params["scheme"] = Scheme solver = CardiacODESolver(mesh, time, model, I_s=stim, params=params) # Create scheme info_green("\nTesting %s with %s scheme" % (model, Scheme)) # Start with native initial conditions (vs_, vs) = solver.solution_fields() vs_.assign(model.initial_conditions()) vs.assign(vs_) return solver
bc = DirichletBC(V, 0.0, "on_boundary") solve(F == 0, u, bc) # The functional of interest is the normed difference between desired # and simulated temperature profile x = SpatialCoordinate(mesh) u_desired = exp(-1 / (1 - x[0] * x[0]) - 1 / (1 - x[1] * x[1])) J = assemble(0.5 * inner(u - u_desired, u - u_desired) * dx) # Run the optimisation rf = ReducedFunctional(J, Control(m)) ub = 0.5 lb = interpolate(Constant(-1), V) # Test 2 different ways of imposing bounds m_opt = minimize(rf, method="L-BFGS-B", tol=2e-08, bounds=(lb, ub), options={ "disp": True, "maxiter": 5 }) assert min(m_opt.vector().get_local()) > lb((0, 0)) - 0.05 info_red("Skipping bound check in L-BFGS-B test") # Skipping this test for now until I have figured out what is going wrong #assert max(m_opt.vector().array()) < ub + 0.05 assert abs(rf(m_opt)) < 1e-3 info_green("Test passed")