thresh_1 = 0.2


def thresh(f, thresh):
    smooth_f = 0.05
    f_thresh = 0.5 * (fenics.tanh((f - thresh) / smooth_f) + 1)
    return f_thresh


m_target, w_target = fenics.split(u_target)
m, w = fenics.split(u)

J = fenics.Functional(
    fenics.inner(m - m_target, m - m_target) *
    sim.subdomains.dx +  # displacements
    fenics.inner(
        thresh(w, thresh_1) -
        thresh(w_target, thresh_1),  # thresholded concentration
        thresh(w, thresh_1) - thresh(w_target, thresh_1)) * sim.subdomains.dx)

controls = [fenics.ConstantControl(param) for param in params_init]


def eval_cb(j, a):
    params = [param.values() for param in a]
    print(j, *params)


reduced_functional = fenics.ReducedFunctional(J,
                                              controls,
                                              eval_cb_post=eval_cb)
Пример #2
0
w_target = sim.functionspace.project_over_space(w_target_tmp,
                                                subspace_id=1,
                                                annotate=False)
# this level of noise still results in good approximation of original parameters
# conc_target_noise = add_noise(w_target, 0.01)
# disp_target_noise = add_noise(m_target, 0.005)
# conc_target_noise = add_noise(w_target, 0.02)
# disp_target_noise = add_noise(m_target, 0.01)
conc_target_noise = add_noise(w_target, 0.1)
disp_target_noise = add_noise(m_target, 0.05)

disp, conc = fenics.split(u)
alpha = 0.5
J = fenics.Functional(
    fenics.inner(conc - conc_target_noise, conc - conc_target_noise) *
    sim.subdomains.dx +
    fenics.inner(disp - disp_target_noise, disp - disp_target_noise) *
    sim.subdomains.dx + alpha * fenics.inner(u, u) * sim.subdomains.dx)

# 2) Noise
m_target_tmp, w_target_tmp = fenics.split(u_target)
m_target = sim.functionspace.project_over_space(m_target_tmp,
                                                subspace_id=0,
                                                annotate=False)
w_target = sim.functionspace.project_over_space(w_target_tmp,
                                                subspace_id=1,
                                                annotate=False)
# this level of noise still results in good approximation of original parameters
# conc_target_noise = add_noise(w_target, 0.01)
# disp_target_noise = add_noise(m_target, 0.005)
# conc_target_noise = add_noise(w_target, 0.02)
# ==============================================================================
# OPTIMISATION
# ==============================================================================
output_path = os.path.join(
    test_config.output_path,
    'test_case_simulation_tumor_growth_2D_subdomains_adjoint', 'adjoint')
fu.ensure_dir_exists(output_path)

D = fenics.Constant(0.1)
rho = fenics.Constant(0.01)
coupling = fenics.Constant(0.1)

u = sim.run_for_adjoint([D, rho, coupling], output_dir=output_path)

J = fenics.Functional(
    fenics.inner(u - u_target, u - u_target) * sim.subdomains.dx)
controls = [
    fenics.ConstantControl(D),
    fenics.ConstantControl(rho),
    fenics.ConstantControl(coupling)
]


def eval_cb(j, a):
    D, rho, coupling = a
    print(j, D.values(), rho.values(), coupling.values())


reduced_functional = fenics.ReducedFunctional(J,
                                              controls,
                                              eval_cb_post=eval_cb)
#                      dpi=300)
#
#
# plott.show_img_seg_f(function=sim.functionspace.project_over_space(conc_sim - conc_sim_target, subspace_id=1), show=True,
#                      path=os.path.join(output_path, 'conc_diff.png'),
#                      dpi=300)
# plott.show_img_seg_f(function=sim.functionspace.project_over_space(disp_sim - disp_sim_target, subspace_id=0), show=True,
#                      path=os.path.join(output_path, 'disp_diff.png'),
#                      dpi=300)

# J = fenics.Functional(   fenics.inner(conc_opt - conc_sim_target, conc_opt - conc_sim_target) * sim.subdomains.dx
#                        + fenics.inner(disp_opt - disp_sim_target, disp_opt - disp_sim_target) * sim.subdomains.dx)

J = fenics.Functional(
    fenics.inner(conc_opt - conc_sim_reloaded, conc_opt - conc_sim_reloaded) *
    sim.subdomains.dx +
    fenics.inner(disp_opt - disp_sim_reloaded, disp_opt - disp_sim_reloaded) *
    sim.subdomains.dx)

controls = [
    fenics.ConstantControl(D),
    fenics.ConstantControl(rho),
    fenics.ConstantControl(c)
]

import pandas as pd

opt_param_progress_post = []
opt_dj_progress_post = []