Beispiel #1
0
def passive(params):

    logger.info(Text.blue("\nTest Passive Optimization"))

    logger.info(pformat(params.to_dict()))

    params["phase"] = "passive_inflation"
    measurements, solver_parameters, p_lv, paramvec \
        = setup_simulation(params, patient)

    solver_parameters["relax_adjoint_solver"] = False

    rd, paramvec = run_passive_optimization_step(params, patient,
                                                 solver_parameters,
                                                 measurements, p_lv, paramvec)

    # Dump html visualization of the forward and adjoint system
    adj_html("passive_forward.html", "forward")
    adj_html("passive_adjoint.html", "adjoint")

    # Replay the forward run, i.e make sure that the recording is correct.
    logger.info("Replay dolfin")
    assert replay_dolfin(tol=1e-12)

    # Test that the gradient is correct
    logger.info("Taylor test")
    my_taylor_test(rd, paramvec)

    paramvec_arr = gather_broadcast(paramvec.vector().array())
    rd(paramvec_arr)
    rd.for_res["initial_control"] = rd.initial_paramvec,
    rd.for_res["optimal_control"] = rd.paramvec
    store_results(params, rd, {})
Beispiel #2
0
def active(params):

    logger.info(Text.blue("\nTest Passive Optimization"))
    logger.info(pformat(params.to_dict()))

    params["phase"] = "passive_inflation"
    params["optimize_matparams"] = False
    # patient.passive_filling_duration = 1
    run_passive_optimization(params, patient)
    adj_reset()

    logger.info(Text.blue("\nTest Active Optimization"))
    print(params["sim_file"])
    params["phase"] = "active_contraction"
    params["active_contraction_iteration_number"] = 0
    measurements, solver_parameters, p_lv, gamma \
        = setup_simulation(params, patient)

    solver_parameters["relax_adjoint_solver"] = False
    dolfin.parameters["adjoint"]["test_derivative"] = True
    logger.info("Replay dolfin1")
    replay_dolfin(tol=1e-12)

    rd, gamma = run_active_optimization_step(params, patient,
                                             solver_parameters, measurements,
                                             p_lv, gamma)

    # Dump html visualization of the forward and adjoint system
    adj_html("active_forward.html", "forward")
    adj_html("active_adjoint.html", "adjoint")

    # Replay the forward run, i.e make sure that the recording is correct.
    logger.info("Replay dolfin")
    assert replay_dolfin(tol=1e-12)

    # Test that the gradient is correct
    logger.info("Taylor test")
    my_taylor_test(rd, gamma)

    paramvec_arr = gather_broadcast(gamma.vector().array())
    rd(paramvec_arr)
    rd.for_res["initial_control"] = rd.initial_paramvec,
    rd.for_res["optimal_control"] = rd.paramvec
    store_results(params, rd, {})
Beispiel #3
0
def test_iterate_gamma(problem):

    target_gamma = 0.1
    gamma = problem.material.activation

    if has_dolfin_adjoint:
        dolfin.parameters["adjoint"]["stop_annotating"] = False
        dolfin_adjoint.adj_reset()

    iterate(problem, gamma, target_gamma)

    assert all(gamma.vector().get_local() == target_gamma)
    assert dolfin.norm(problem.state.vector()) > 0

    if has_dolfin_adjoint:
        assert dolfin_adjoint.replay_dolfin(tol=1e-12)
Beispiel #4
0
def test_iterate_pressure(problem):

    target_pressure = 1.0
    plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv']
    pressure = plv[0]

    if has_dolfin_adjoint:
        dolfin.parameters["adjoint"]["stop_annotating"] = False
        dolfin_adjoint.adj_reset()

    iterate(problem, pressure, target_pressure)

    if has_dolfin_adjoint:
        # Check the recording
        assert dolfin_adjoint.replay_dolfin(tol=1e-12)

    # Check that the pressure is correct
    assert float(plv[0]) == target_pressure
    # Check that the state is nonzero
    assert dolfin.norm(problem.state.vector()) > 0
Beispiel #5
0
def test_iterate_gamma_pressure(problem):
    target_pressure = 1.0
    plv = [p.traction for p in problem.bcs.neumann if p.name == 'lv']
    pressure = plv[0]

    target_gamma = 0.1
    gamma = problem.material.activation

    if has_dolfin_adjoint:
        dolfin.parameters["adjoint"]["stop_annotating"] = False
        dolfin_adjoint.adj_reset()

    iterate(problem, (pressure, gamma), (target_pressure, target_gamma))

    assert all(gamma.vector().get_local() == target_gamma)
    assert float(plv[0]) == target_pressure
    assert dolfin.norm(problem.state.vector()) > 0

    if has_dolfin_adjoint:
        assert dolfin_adjoint.replay_dolfin(tol=1e-12)
Beispiel #6
0
def test_iterate_gamma_cg1(continuation):

    problem = make_lv_mechanics_problem('CG_1')
    V = problem.material.activation.function_space()
    target_gamma \
        = dolfin.interpolate(dolfin.Expression('0.1 * x[0]',
                                               degree=1), V)
    gamma = problem.material.activation

    if has_dolfin_adjoint:
        dolfin.parameters["adjoint"]["stop_annotating"] = False
        dolfin_adjoint.adj_reset()

    iterate(problem, gamma, target_gamma, continuation=continuation)

    assert np.all(
        gamma.vector().get_local() - target_gamma.vector().get_local() < 1e-12)
    assert dolfin.norm(problem.state.vector()) > 0

    if has_dolfin_adjoint:
        assert dolfin_adjoint.replay_dolfin(tol=1e-12)
Beispiel #7
0
def test_iterate_gamma_regional():
    problem = make_lv_mechanics_problem('regional')
    target_gamma = problem.material.activation.vector().get_local()
    for i in range(len(target_gamma)):
        target_gamma[i] = 0.1 - i * 0.001
    print(target_gamma)

    gamma = problem.material.activation

    if has_dolfin_adjoint:
        dolfin.parameters["adjoint"]["stop_annotating"] = False
        dolfin_adjoint.adj_reset()

    iterate(problem, gamma, target_gamma)

    print(gamma.vector().get_local())
    assert np.all(gamma.vector().get_local() - target_gamma < 1e-12)
    assert dolfin.norm(problem.state.vector()) > 0

    if has_dolfin_adjoint:
        assert dolfin_adjoint.replay_dolfin(tol=1e-12)
Beispiel #8
0
model = ReducedFunctional(config, scale = -1)
m0 = model.initial_control()

# Build the turbine cache 
config.turbine_cache.update(config)

time_forward = []
time_adjoint = []
for i in range(5):
	print "Running forward model round ", i
	t1 = Timer("Forward model")
	model.run_forward_model_mem.fn(m0) 
	time_forward.append(t1.stop())
	print "Forward model runtime: ", time_forward[-1]

	from dolfin_adjoint import replay_dolfin
	print "Replaying model round ", i
	t11 = Timer("Replay forward model")
	replay_dolfin()
	print "Replay model runtime: ", t11.stop() 

	t2 = Timer("Adjoint model")
	model.run_adjoint_model_mem.fn(m0) 
	time_adjoint.append(t2.stop())
	print "Adjoint model runtime: ", time_adjoint[-1]

print "Smallest runtime for forward model: ", min(time_forward)
print "Smallest runtime for forward + adjoint model: ", min(time_forward) + min(time_adjoint)
print "Ratio: ", 1.0 + min(time_adjoint) / min(time_forward)
Beispiel #9
0
model = ReducedFunctional(config, scale=-1, plot=True)
m0 = model.initial_control()

# Build the turbine cache
config.turbine_cache.update(config)

time_forward = []
time_adjoint = []
for i in range(5):
    print "Running forward model round ", i
    t1 = Timer("Forward model")
    model.run_forward_model_mem.fn(m0)
    time_forward.append(t1.stop())
    print "Forward model runtime: ", time_forward[-1]

    from dolfin_adjoint import replay_dolfin
    print "Replaying model round ", i
    t11 = Timer("Replay forward model")
    replay_dolfin()
    print "Replay model runtime: ", t11.stop()

    t2 = Timer("Adjoint model")
    model.run_adjoint_model_mem.fn(m0)
    time_adjoint.append(t2.stop())
    print "Adjoint model runtime: ", time_adjoint[-1]

print "Smallest runtime for forward model: ", min(time_forward)
print "Smallest runtime for forward + adjoint model: ", min(
    time_forward) + min(time_adjoint)
print "Ratio: ", 1.0 + min(time_adjoint) / min(time_forward)