def obj_fun(self, state_dp): """ Objective function evaluation for given direct problem state. """ var_data = state_dp.get_parts() var_data = remap_dict(var_data, self.var_map) self.of_equations.set_data(var_data, ignore_unknown=True) val = eval_equations(self.of_equations, self.of_variables) return nm.squeeze( val )
def solve_adjoint(conf, options, dpb, vec_dp, data): """ Solve the adjoint (linear) problem. """ opts = conf.options if dpb: apb = dpb.copy('adjoint') else: apb = ProblemDefinition.from_conf(conf) equations = getattr(conf, '_'.join(('equations_adjoint', opts.problem, opts.objective_function))) apb.set_equations(equations) apb.time_update(None) apb.ebcs.zero_dofs() apb.update_equations(None, ebcs=apb.ebcs) var_data = dpb.equations.get_state_parts(vec_dp) var_data = remap_dict(var_data, opts.var_map) nls_conf = apb.get_solver_conf(opts.nls_adjoint) vec_ap = apb.solve(nls_conf=nls_conf, var_data=var_data) trunk = io.get_trunk(conf.filename_mesh) apb.save_state(trunk + '_adjoint.vtk', vec_ap) shape_opt = so.ShapeOptimFlowCase.from_conf(conf, dpb, apb) ## print shape_opt ## pause() if options.test is not None: ## # Test shape sensitivity. if shape_opt.test_terms_if_test: so.test_terms([options.test], opts.term_delta, shape_opt, var_data, vec_ap) shape_opt.check_sensitivity([options.test], opts.delta, var_data, vec_ap) ## # Compute objective function. val = shape_opt.obj_fun(vec_dp) print 'actual obj_fun:', val ## pause() ## # Compute shape sensitivity. vec_sa = shape_opt.sensitivity(var_data, vec_ap) print 'actual sensitivity:', vec_sa
def solve_adjoint(conf, options, dpb, state_dp, data): """ Solve the adjoint (linear) problem. """ opts = conf.options if dpb: apb = dpb.copy('adjoint') else: apb = Problem.from_conf(conf, init_equations=False) equations = getattr( conf, '_'.join( ('equations_adjoint', opts.problem, opts.objective_function))) apb.set_equations(equations) apb.time_update(None) apb.ebcs.zero_dofs() apb.update_equations(None, ebcs=apb.ebcs) var_data = state_dp.get_parts() var_data = remap_dict(var_data, opts.var_map) nls_conf = apb.get_solver_conf(opts.nls_adjoint) state_ap = apb.solve(nls_conf=nls_conf, var_data=var_data) trunk = io.get_trunk(conf.filename_mesh) apb.save_state(trunk + '_adjoint.vtk', state_ap) shape_opt = so.ShapeOptimFlowCase.from_conf(conf, dpb, apb) if options.test is not None: ## # Test shape sensitivity. if shape_opt.test_terms_if_test: so.test_terms([options.test], opts.term_delta, shape_opt, var_data, state_ap) shape_opt.check_sensitivity([options.test], opts.delta, var_data, state_ap) ## # Compute objective function. val = shape_opt.obj_fun(state_dp) print 'actual obj_fun:', val ## # Compute shape sensitivity. vec_sa = shape_opt.sensitivity(var_data, state_ap) print 'actual sensitivity:', vec_sa
def obj_fun_grad(design, shape_opt, opts): """ The objective function gradient evaluation. """ state_dp = solve_problem_for_design(shape_opt.dpb, design, shape_opt, opts) var_data = state_dp.get_parts() var_data = remap_dict(var_data, shape_opt.var_map) state_ap = solve_problem_for_design(shape_opt.apb, design, shape_opt, opts, var_data=var_data, use_cache=False, is_mesh_update=False) vec_sa = shape_opt.sensitivity(var_data, state_ap) return vec_sa