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) m_opt = fenics.minimize(reduced_functional) for var in m_opt: print(var.values()) # ============================================================================== # RESULTS # ============================================================================== # Plot when adjoint computation has finished to avoid recording of function projections # sim.plotting.plot_all(sim_time) # # output_path = os.path.join(test_config.output_path, 'test_case_simulation_tumor_growth_2D_uniform_adjoint') # fu.ensure_dir_exists(output_path)
def create_opt_progress_df(opt_param_list, opt_dj_list, param_name_list): columns_params = ['J', *param_name_list] columns_dJ = ['J', *['dJd%s' % param for param in param_name_list]] # create data frames params_df = pd.DataFrame(opt_param_list) params_df.columns = columns_params dj_df = pd.DataFrame(opt_dj_list) dj_df.columns = columns_dJ # merge opt_df = pd.merge(params_df, dj_df, on='J', how='outer') return opt_df reduced_functional = fenics.ReducedFunctional( J, controls, derivative_cb_post=derivative_cb_post, eval_cb_post=eval_cb_post) dJdu = fenics.compute_gradient(J, controls) bounds = [[0.05, 0.01, 0.05], [0.5, 0.2, 0.05]] m_opt = fenics.minimize(reduced_functional, bounds=bounds, method='SLSQP', options={'disp': True}) opt_df = create_opt_progress_df(opt_param_progress_post, opt_dj_progress_post, ['D', 'rho', 'c']) opt_df.to_excel(os.path.join(output_path, 'optimization.xls')) opt_df.to_pickle(os.path.join(output_path, 'optimization.pkl'))