def Jhat(val): cell_params[param_name].assign(val) ics = Function(project(model.initial_conditions(), solver.VS), name="ics") self._run(solver, ics) (vs_, vs) = solver.solution_fields() return assemble(form(vs))
def main(N, dt, T, theta): if dolfin_adjoint: adj_reset() # Create cardiac model mesh = UnitSquareMesh(N, N) time = Constant(0.0) cell_model = NoCellModel() ac_str = "cos(t)*cos(2*pi*x[0])*cos(2*pi*x[1]) + 4*pow(pi, 2)*cos(2*pi*x[0])*cos(2*pi*x[1])*sin(t)" stimulus = Expression(ac_str, t=time, degree=3) heart = CardiacModel(mesh, time, 1.0, 1.0, cell_model, stimulus=stimulus) # Set-up solver ps = BasicSplittingSolver.default_parameters() ps["theta"] = theta ps["BasicBidomainSolver"]["linear_variational_solver"][ "linear_solver"] = "direct" solver = BasicSplittingSolver(heart, params=ps) # 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(2*pi*x[0])*cos(2*pi*x[1])*sin(t)", t=T, degree=3) u_exact = Expression("-cos(2*pi*x[0])*cos(2*pi*x[1])*sin(t)/2.0", t=T - (1 - theta) * dt, degree=3) # Define initial condition(s) vs0 = Function(solver.VS) (vs_, vs, vur) = solver.solution_fields() vs_.assign(vs0) # Solve solutions = solver.solve((0, T), dt) for (timestep, (vs_, vs, vur)) in solutions: continue # Compute errors (v, s) = vs.split(deepcopy=True) v_error = errornorm(v_exact, v, "L2", degree_rise=2) (v, u, r) = vur.split(deepcopy=True) u_error = errornorm(u_exact, u, "L2", degree_rise=2) return (v_error, u_error, mesh.hmin(), dt, T)
def tlm_adj_setup_cellmodel_parameters(self, cell_model, Scheme): mesh = UnitIntervalMesh(3) Model = cell_model.__class__ # Initiate solver, with model and Scheme cell_params = Model.default_parameters() param_name = cellmodel_parameters_seeds[Model][0] cell_params[param_name] = Constant(cell_params[param_name], name=param_name) model = Model(params=cell_params) solver = self._setup_solver(model, Scheme, mesh) info_green("Running forward %s with %s (setup)" % (model, Scheme)) ics = Function(project(model.initial_conditions(), solver.VS), name="ics") self._run(solver, ics) # Define functional (vs_, vs) = solver.solution_fields() form = lambda w: inner(w, w) * dx J = Functional(form(vs) * dt[FINISH_TIME]) # Compute value of functional with current ics Jics = assemble(form(vs)) # Set-up runner def Jhat(val): cell_params[param_name].assign(val) ics = Function(project(model.initial_conditions(), solver.VS), name="ics") self._run(solver, ics) (vs_, vs) = solver.solution_fields() return assemble(form(vs)) # Stop annotating solver.parameters["enable_adjoint"] = False m = ConstantControl(cell_params[param_name]) return J, Jhat, m, Jics