示例#1
0
    def test_compute_gradient(self):
        "Test that we can compute the gradient for some given functional"
        set_dolfin_parameters()
        model = Model()

        params = BasicSingleCellSolver.default_parameters()
        params["theta"] = theta
        time = Constant(0.0)
        solver = BasicSingleCellSolver(model, time, params=params)

        # Get initial conditions (Projection of expressions
        # don't get annotated, which is fine, because there is
        # no need.)
        ics = project(model.initial_conditions(), solver.VS)

        # Run forward model
        info_green("Running forward %s with theta %g" % (model, theta))
        self._run(solver, model, ics)

        # Define functional
        (vs_, vs) = solver.solution_fields()
        J = Functional(inner(vs, vs) * dx * dt[FINISH_TIME])

        # Compute gradient with respect to vs_. Highly unclear
        # why with respect to ics and vs fail.
        info_green("Computing gradient")
        dJdics = compute_gradient(J, Control(vs_))
        assert (dJdics is not None), "Gradient is None (#fail)."
        print(dJdics.vector().array())
    def tlm_adj_setup_initial_conditions(self, cell_model, Scheme):
        mesh = UnitIntervalMesh(3)
        Model = cell_model.__class__

        # Initiate solver, with model and Scheme
        params = Model.default_parameters()
        model = Model(params=params)

        solver = self._setup_solver(model, Scheme, mesh)
        ics = project(model.initial_conditions(), solver.VS).copy(deepcopy=True, name="ics")

        info_green("Running forward %s with %s (setup)" % (model, Scheme))
        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(ics):
            self._run(solver, ics)
            (vs_, vs) = solver.solution_fields()
            return assemble(form(vs))

        # Stop annotating
        parameters["adjoint"]["stop_annotating"] = True

        m = Control(vs)
        return J, Jhat, m, Jics
示例#3
0
    def test_compute_adjoint(self):
        "Test that we can compute the adjoint for some given functional"
        set_dolfin_parameters()
        model = Model()

        params = BasicSingleCellSolver.default_parameters()
        params["theta"] = theta
        time = Constant(0.0)
        solver = BasicSingleCellSolver(model, time, params=params)

        # Get initial conditions (Projection of expressions
        # don't get annotated, which is fine, because there is
        # no need.)
        ics = project(model.initial_conditions(), solver.VS)

        # Run forward model
        info_green("Running forward %s with theta %g" % (model, theta))
        self._run(solver, model, ics)

        (vs_, vs) = solver.solution_fields()

        # Define functional and compute gradient etc
        J = Functional(inner(vs_, vs_) * dx * dt[FINISH_TIME])

        # Compute adjoint
        info_green("Computing adjoint")
        z = compute_adjoint(J)

        # Check that no vs_ adjoint is None (== 0.0!)
        for (value, var) in z:
            if var.name == "vs_":
                msg = "Adjoint solution for vs_ is None (#fail)."
                assert (value is not None), msg
示例#4
0
    def tlm_adj_setup(self, Solver, solver_type):
        """ Common code for test_tlm and test_adjoint. """
        wrap = generate_solver(Solver, solver_type)
        vs_, vs = wrap.run_forward_model()

        # Define functional
        form = lambda w: inner(w, w) * dx
        J = Functional(form(vs) * dt[FINISH_TIME])
        if Solver == SplittingSolver:
            m = Control(vs)
        else:
            m = Control(vs_)

        # Compute value of functional with current ics
        Jics = assemble(form(vs))

        # Define reduced functional
        def Jhat(ics):
            wrap = generate_solver(Solver,
                                   solver_type,
                                   ics=ics,
                                   enable_adjoint=False)
            vs_, vs = wrap.run_forward_model()

            return assemble(form(vs))

        # Stop annotating
        parameters["adjoint"]["stop_annotating"] = True

        return J, Jhat, m, Jics
    def test_taylor_remainder(self):
        "Run Taylor remainder tests for selection of models and solvers."
        set_dolfin_parameters()
        model = Model()

        params = BasicSingleCellSolver.default_parameters()
        params["theta"] = theta
        time = Constant(0.0)
        solver = BasicSingleCellSolver(model, time, params=params)

        # Get initial conditions (Projection of expressions
        # don't get annotated, which is fine, because there is
        # no need.)
        ics = project(model.initial_conditions(), solver.VS)

        # Run forward model
        info_green("Running forward %s with theta %g" % (model, theta))
        self._run(solver, model, 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))

        # Compute gradient with respect to vs_ (ics?)
        dJdics = compute_gradient(J, Control(vs_),
                                  forget=False)

        # Stop annotating
        parameters["adjoint"]["stop_annotating"] = True

        # Set-up runner
        def Jhat(ics):
            self._run(solver, model, ics)
            (vs_, vs) = solver.solution_fields()
            return assemble(form(vs))

        # Run taylor test
        if isinstance(model, Tentusscher_2004_mcell):
            seed=1.e-5
        else:
            seed=None

        conv_rate = taylor_test(Jhat, Control(vs_),
                                Jics, dJdics, seed=seed)

        # Check that minimal rate is greater than some given number
        assert_greater(conv_rate, 1.8)
    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
    def tlm_adj_setup(self, Solver, solver_type):
        """ Common code for test_tlm and test_adjoint. """
        self._setup_solver(Solver, solver_type)
        self._solve()
        (vs_, vs) = self.solver.solution_fields()

        # Define functional
        form = lambda w: inner(w, w) * dx
        J = Functional(form(vs) * dt[FINISH_TIME])
        m = Control(vs_)

        # Compute value of functional with current ics
        Jics = assemble(form(vs))

        # Define reduced functional
        def Jhat(ics):
            self._setup_solver(Solver, solver_type, enable_adjoint=False)
            vs = self._solve(ics)
            return assemble(form(vs))

        # Stop annotating
        parameters["adjoint"]["stop_annotating"] = True

        return J, Jhat, m, Jics