def solve_optimize(conf, options): opts = conf.options trunk = io.get_trunk(conf.filename_mesh) data = {} dpb = Problem.from_conf(conf, init_equations=False) equations = getattr(conf, '_'.join(('equations_direct', opts.problem))) dpb.set_equations(equations) dpb.name = 'direct' dpb.time_update(None) apb = dpb.copy('adjoint') 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) ls_conf = dpb.get_solver_conf(opts.ls) dnls_conf = dpb.get_solver_conf(opts.nls_direct) anls_conf = dpb.get_solver_conf(opts.nls_adjoint) opt_conf = dpb.get_solver_conf(opts.optimizer) dpb.init_solvers(ls_conf=ls_conf, nls_conf=dnls_conf) apb.init_solvers(ls_conf=ls_conf, nls_conf=anls_conf) shape_opt = so.ShapeOptimFlowCase.from_conf(conf, dpb, apb) design0 = shape_opt.dsg_vars.val shape_opt.cache = Struct(design=design0 + 100, state=None, i_mesh=-1) opt_status = IndexedStruct() optimizer = Solver.any_from_conf(opt_conf, obj_fun=so.obj_fun, obj_fun_grad=so.obj_fun_grad, status=opt_status, obj_args=(shape_opt, opts)) ## # State problem solution for the initial design. vec_dp0 = so.solve_problem_for_design(dpb, design0, shape_opt, opts) dpb.save_state(trunk + '_direct_initial.vtk', vec_dp0) ## # Optimize. des = optimizer(design0) print opt_status ## # Save final state (for "optimal" design). dpb.domain.mesh.write(trunk + '_opt.mesh', io='auto') dpb.save_state(trunk + '_direct_current.vtk', shape_opt.cache.state) print des
def solve_generic_direct(conf, options): opts = conf.options dpb = Problem.from_conf(conf, init_equations=False) equations = getattr(conf, '_'.join(('equations_direct', opts.problem))) dpb.set_equations(equations) dpb.time_update(None) nls_conf = dpb.get_solver_conf(opts.nls_direct) state_dp = dpb.solve(nls_conf=nls_conf) return dpb, state_dp, {}
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 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 __init__( self, dims, center_location, cell_sizes=np.array([2, 2]), prob_file="C:\\Users\\wbald\\sfepythings\\blocks\\fem\\prob_desc_2d.py", put_mesh="C:\\Users\\wbald\\sfepythings"): """ dims: array, dimensions of rectangle [x,y] center_location: array, centre of rectangle [x,y] cell sizes: array, x and y side length of FEM rectangular elements stretch: default distance by which to displace the upper y axis edge. prob_file: problem description file put_mesh: where to save the mesh file """ assert (dims.shape[0] == 2) assert (cell_sizes.shape[0] == 2) # assume linear elasticity. Fix strain of rectangle to 0.001 and query # at different strains by scaling linearly self.dims = dims self.prob_file = prob_file self.FEM_model_strain = 0.001 self.elongation = self.FEM_model_strain * self.dims[1] nums = np.divide(dims, cell_sizes) nums = np.around(nums).astype(int) + np.array([1, 1]) blockmesh = gen_block_mesh(dims, nums, center_location) blockmesh.write(put_mesh + '\\mesh.vtk') conf = ProblemConf.from_file(prob_file) # 'region_ylower__1' holds the edge to be fixed # 'region_yupper__2' holds the edge to be displaced conf.regions['region_ylower__1'].select = 'vertices in (y < ' + str( 0.01) + ')' conf.regions['region_yupper__2'].select = 'vertices in (y > ' + str( dims[1] - 0.01) + ')' conf.ebcs['ebc_Displaced__1'].dofs['u.1'] = self.elongation self.prob = Problem.from_conf(conf) # for reshaping sfepy output into xyz displacements self.reshape_tuple = ((self.prob.fields['displacement'].n_nod, self.prob.fields['displacement'].n_components))
def solve_navier_stokes(conf, options): opts = conf.options dpb = Problem.from_conf(conf, init_equations=False) equations = getattr(conf, '_'.join(('equations_direct', opts.problem))) dpb.set_equations(equations) ls_conf = dpb.get_solver_conf(opts.ls) nls_conf = dpb.get_solver_conf(opts.nls_direct) method = opts.direct_method if method == 'stationary': data = {} dpb.time_update(None) state_dp = dpb.solve(nls_conf=nls_conf) elif method == 'transient': ls = Solver.any_from_conf(ls_conf) ts_conf = dpb.get_solver_conf(opts.ts_direct) data = {'ts': Struct(dt=ts_conf.dt)} # Plug in mass term. mequations = {} for key, eq in equations.iteritems(): if 'dw_div_grad' in eq: eq = '+'.join((ts_conf.mass_term, eq)).replace('++', '+') mequations[key] = eq if ts_conf.stokes_init: state_dp0 = solve_stokes(dpb, conf.equations_direct_stokes, nls_conf) dpb.set_equations(mequations) else: dpb.set_equations(mequations) state_dp0 = dpb.create_state() dpb.time_update(None) state_dp0.apply_ebc() from sfepy.base.log import Log log = Log.from_conf(Struct(is_plot=True), ([r'$||u||$'], [r'$||p||$'])) output('Navier-Stokes...') ev = BasicEvaluator(dpb, ts=Struct(dt=ts_conf.dt)) nls = Solver.any_from_conf(nls_conf, evaluator=ev, lin_solver=ls) n_step = ts_conf.n_step step = 0 while 1: for ii in xrange(n_step): output(step) vec_u = state_dp0('w') vec_p = state_dp0('r') log(nm.linalg.norm(vec_u), nm.linalg.norm(vec_p)) dpb.variables.set_data_from_state('w_0', state_dp0(), 'w') vec_dp = nls(state_dp0()) step += 1 state_dp = state_dp0.copy() state_dp.set_reduced(vec_dp) state_dp0 = state_dp if ts_conf.interactive: try: n_step = int(raw_input('continue: ')) if n_step <= 0: break except: break vec_u = state_dp('w') vec_p = state_dp('r') log(nm.linalg.norm(vec_u), nm.linalg.norm(vec_p), finished=True) else: raise 'unknown Navier-Stokes solution method (%s)!' % method return dpb, state_dp, data
def solve_navier_stokes(conf, options): opts = conf.options dpb = Problem.from_conf(conf, init_equations=False) equations = getattr(conf, '_'.join(('equations_direct', opts.problem))) dpb.set_equations(equations) ls_conf = dpb.get_solver_conf( opts.ls ) nls_conf = dpb.get_solver_conf(opts.nls_direct) method = opts.direct_method if method == 'stationary': data = {} dpb.time_update(None) state_dp = dpb.solve(nls_conf=nls_conf) elif method == 'transient': ls = Solver.any_from_conf( ls_conf ) ts_conf = dpb.get_solver_conf( opts.ts_direct ) data = {'ts' : Struct( dt = ts_conf.dt )} # Plug in mass term. mequations = {} for key, eq in equations.iteritems(): if 'dw_div_grad' in eq: eq = '+'.join( (ts_conf.mass_term, eq) ).replace( '++', '+') mequations[key] = eq if ts_conf.stokes_init: state_dp0 = solve_stokes( dpb, conf.equations_direct_stokes, nls_conf ) dpb.set_equations( mequations ) else: dpb.set_equations( mequations ) state_dp0 = dpb.create_state() dpb.time_update( None ) state_dp0.apply_ebc() from sfepy.base.log import Log log = Log.from_conf( Struct( is_plot = True ), ([r'$||u||$'], [r'$||p||$']) ) output( 'Navier-Stokes...' ) ev = BasicEvaluator( dpb, ts = Struct( dt = ts_conf.dt ) ) nls = Solver.any_from_conf( nls_conf, evaluator = ev, lin_solver = ls ) n_step = ts_conf.n_step step = 0 while 1: for ii in xrange( n_step ): output( step ) vec_u = state_dp0('w') vec_p = state_dp0('r') log( nm.linalg.norm( vec_u ), nm.linalg.norm( vec_p ) ) dpb.variables.set_data_from_state('w_0', state_dp0(), 'w') vec_dp = nls( state_dp0() ) step += 1 state_dp = state_dp0.copy() state_dp.set_reduced(vec_dp) state_dp0 = state_dp if ts_conf.interactive: try: n_step = int( raw_input( 'continue: ' ) ) if n_step <= 0: break except: break vec_u = state_dp('w') vec_p = state_dp('r') log( nm.linalg.norm( vec_u ), nm.linalg.norm( vec_p ), finished = True ) else: raise 'unknown Navier-Stokes solution method (%s)!' % method return dpb, state_dp, data
def solve_optimize( conf, options ): opts = conf.options trunk = io.get_trunk( conf.filename_mesh ) data = {} dpb = Problem.from_conf(conf, init_equations=False) equations = getattr( conf, '_'.join( ('equations_direct', opts.problem) ) ) dpb.set_equations( equations ) dpb.name = 'direct' dpb.time_update(None) apb = dpb.copy('adjoint') 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) ls_conf = dpb.get_solver_conf(opts.ls) dnls_conf = dpb.get_solver_conf(opts.nls_direct) anls_conf = dpb.get_solver_conf(opts.nls_adjoint) opt_conf = dpb.get_solver_conf(opts.optimizer) dpb.init_solvers(ls_conf=ls_conf, nls_conf=dnls_conf) apb.init_solvers(ls_conf=ls_conf, nls_conf=anls_conf) shape_opt = so.ShapeOptimFlowCase.from_conf(conf, dpb, apb) design0 = shape_opt.dsg_vars.val shape_opt.cache = Struct(design=design0 + 100, state=None, i_mesh=-1) opt_status = IndexedStruct() optimizer = Solver.any_from_conf(opt_conf, obj_fun=so.obj_fun, obj_fun_grad=so.obj_fun_grad, status=opt_status, obj_args=(shape_opt, opts)) ## # State problem solution for the initial design. vec_dp0 = so.solve_problem_for_design(dpb, design0, shape_opt, opts) dpb.save_state( trunk + '_direct_initial.vtk', vec_dp0 ) ## # Optimize. des = optimizer( design0 ) print opt_status ## # Save final state (for "optimal" design). dpb.domain.mesh.write( trunk + '_opt.mesh', io = 'auto' ) dpb.save_state(trunk + '_direct_current.vtk', shape_opt.cache.state) print des
def load_and_plot_fun(folder, filename, exact=None): """ Parameters ---------- folder : str folder where to look for files filename : str used in {name}.i.vtk, i = 0,1, ... tns - 1 number of time steps exact : callable exact solution at the last frame """ in_file = head(glob(pjoin(folder, "*.vtk"))) coors, data = load_state_1D_vtk(in_file) approx_order = data.shape[0] - 1 dmesh = Mesh.from_file(in_file) domain = FEDomain("", dmesh) omega = domain.create_region('Omega', 'all') field = DGField('f', nm.float64, 'scalar', omega, approx_order=approx_order) # Sufficient quadrature order for the analytical expression. idiff = Integral('idiff', 20) u = FieldVariable("u", "unknown", field) eqs = Equations( [Equation('balance', SurfaceTerm("s()", "u", idiff, omega, u=u))]) pb = Problem("err_est", equations=eqs) u.set_data(field.ravel_sol(data.swapaxes(0, 1))) num_qp = pb.evaluate('ev_volume_integrate.idiff.Omega(u)', u=u, integrals=Integrals([idiff]), mode='qp') aux = Material('aux', function=sol_fun) ana_qp = pb.evaluate('ev_volume_integrate_mat.idiff.Omega(aux.u, u)', aux=aux, u=u, integrals=Integrals([idiff]), mode='qp') qps = pb.fields["f"].mapping.get_physical_qps(idiff.get_qp("1_2")[0]) fqps = qps.flatten() plt.figure("Reconstructed solution") plt.gca().set_ylim(-.5, 3.) ww_approx, xx = reconstruct_legendre_dofs(coors, None, data) ww_exact = exact(xx) XN = xx[-1] X1 = xx[0] plt.plot([X1, XN], [2, 2], 'grey', alpha=.6) plt.plot([X1, XN], [0, 0], 'grey', alpha=.6) plt.plot(fqps, ana_qp.flatten(), label="$p_{exact}(1, x)$") plt.plot(fqps, num_qp.flatten(), label="$p_h(1, x)$") plt.legend() plt.show()