mp.Volume(center=mp.Vector3(0, 1, 0), size=mp.Vector3(x=2)), mode=1) ob_list = [TE0] def J(alpha): return npa.sum(npa.abs(alpha)**2) #---------------------------------------------------------------------- #- Define optimization problem #---------------------------------------------------------------------- opt = mpa.OptimizationProblem(simulation=sim, objective_function=J, objective_arguments=ob_list, basis=bases, fcen=fcen, time=time) #---------------------------------------------------------------------- #- Get gradient #---------------------------------------------------------------------- f0, g_adjoint = opt() #---------------------------------------------------------------------- #- FD run #---------------------------------------------------------------------- db = 1e-3 n = Nx * Ny choose = 20
#---------------------------------------------------------------------- TE0 = mpa.EigenmodeCoefficient(sim,mp.Volume(center=mp.Vector3(0,1,0),size=mp.Vector3(x=2)),mode=1) ob_list = [TE0] def J(alpha): return npa.sum(npa.abs(alpha) ** 2) #---------------------------------------------------------------------- #- Define optimization problem #---------------------------------------------------------------------- opt = mpa.OptimizationProblem( simulation = sim, objective_function = J, objective_arguments = ob_list, basis = [basis], fcen = fcen, df = fwidth, nf = nf, time = time ) #---------------------------------------------------------------------- #- Get gradient #---------------------------------------------------------------------- f0, g_adjoint = opt() #---------------------------------------------------------------------- #- FD run #---------------------------------------------------------------------- db = 1e-3
# objective function and extra objective quantities ------------------- #---------------------------------------------------------------------- splitter = False #fobj_router = '|P1_north/P1_west1|^2' fobj_router = '|P1_east|^2' fobj_splitter = '( |P1_north| - |P1_east| )^2 + ( |P1_east| - |M1_south| )^2' objective_function = fobj_router extra_quantities = ['S_north', 'S_south', 'S_east', 'S_west1', 'S_west2'] #---------------------------------------------------------------------- #---------------------------------------------------------------------- #---------------------------------------------------------------------- opt_prob = mpa.OptimizationProblem(sim, objective_regions=objective_regions, objective_function=objective_function, basis=basis, beta_vector=beta_vector, design_function=design_function) f, _ = opt_prob.get_fdf_funcs() n = basis.dim b0 = 9 * np.ones((n, )) opt_prob.visualize(pmesh=True) '''plt.savefig('design_region.png') plt.show() quit()''' #---------------------------------------------------------------------- # -- Solve adjoint problem #---------------------------------------------------------------------- f_adjoint, g_adjoint = opt_prob(b0) #----------------------------------------------------------------------