Skip to content

Commit acdeea9

Browse files
author
Wenjie Yao
committed
removing intermediate recording files
1 parent a5d8652 commit acdeea9

1 file changed

Lines changed: 12 additions & 32 deletions

File tree

src/TopOptEMFocus.jl

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@
7878
# \end{aligned}
7979
# ```
8080
#
81-
# We choose a filter radius $r_f=R_f/(2\sqrt{3})$ where $R_f=5$nm, in order to match a published result (using a slightly different filtering scheme) for comparison [6].
81+
# We choose a filter radius $r_f=R_f/(2\sqrt{3})$ where $R_f=10$ nm, in order to match a published result (using a slightly different filtering scheme) for comparison [6].
8282
#
8383
# Next, we apply a smoothed threshold projection to the intermediate variable $p_f$ to obtain a "binarized" density parameter $p_t$ that tends towards values of $0$ or $1$ almost everywhere [6] as the steepness $\beta$ of the thresholding is increased:
8484
# ```math
@@ -133,7 +133,7 @@ model = GmshDiscreteModel("../models/RecCirGeometry.msh")
133133

134134
# ## FE spaces for the magnetic field
135135
#
136-
# We use the first-order Lagrange finite-element basis functions. The Dirichlet edges are labeled as `DirichletEdges` in the mesh file. Since our problem involves complex numbers (because of the PML), we need to specify the `vector_type` as `Vector{ComplexF64}`.
136+
# We use the first-order Lagrange finite-element basis functions. The Dirichlet edges are labeled as `DirichletEdges` in the mesh file. Since our problem involves complex numbers (because of the PML and the complex metal refractive index), we need to specify the `vector_type` as `Vector{ComplexF64}`.
137137
#
138138

139139
order = 1
@@ -440,54 +440,36 @@ g0 = gf_p(p0, grad; r, β, η, phys_params, fem_params)
440440
g1 = gf_p(p0+δp, []; r, β, η, phys_params, fem_params)
441441
g1-g0, grad'*δp
442442

443-
# ## Optimization with NLop
443+
# ## Optimization with NLopt
444444
#
445445
# Now we use NLopt.jl package to implement the MMA algorithm for optimization. Note that we start with $\beta=8$ and then gradually increase it to $\beta=32$ in consistant with Ref. [6].
446446
#
447447

448-
using NLopt, DelimitedFiles
448+
using NLopt
449449

450-
function gf_p_optimize(p_init, r, β, η, TOL = 1e-4, MAX_ITER = 500; phys_params, fem_params)
450+
function gf_p_optimize(p_init; r, β, η, TOL = 1e-4, MAX_ITER = 500, phys_params, fem_params)
451451
##################### Optimize #################
452452
opt = Opt(:LD_MMA, fem_params.np)
453453
opt.lower_bounds = 0
454454
opt.upper_bounds = 1
455455
opt.ftol_rel = TOL
456456
opt.maxeval = MAX_ITER
457457
opt.max_objective = (p0, grad) -> gf_p(p0, grad; r, β, η, phys_params, fem_params)
458-
if (length(p_init)==0)
459-
p_initial_guess = readdlm("p_opt_value.txt", Float64)
460-
p_initial_guess = p_initial_guess[:]
461-
else
462-
p_initial_guess = p_init[:]
463-
end
464458

465-
(g_opt, p_opt, ret) = optimize(opt, p_initial_guess)
459+
(g_opt, p_opt, ret) = optimize(opt, p_init)
466460
@show numevals = opt.numevals # the number of function evaluations
467-
468461
return g_opt, p_opt
469462
end
470463

471-
p_init = fill(0.4, fem_params.np) # Initial guess
464+
p_opt = fill(0.4, fem_params.np) # Initial guess
472465
β_list = [8.0, 16.0, 32.0]
473466

474467
g_opt = 0
468+
TOL = 1e-8
469+
MAX_ITER = 100
475470
for bi = 1 : 3
476471
β = β_list[bi]
477-
if bi == 1
478-
g_opt, p_opt = gf_p_optimize(p_init, r, β, η, 1e-8, 100; phys_params, fem_params)
479-
480-
else
481-
g_opt, p_opt = gf_p_optimize([], r, β, η, 1e-8, 100; phys_params, fem_params)
482-
end
483-
open("p_opt_value.txt", "w") do iop
484-
for i = 1 : length(p_opt)
485-
write(iop, "$(p_opt[i]) \n")
486-
end
487-
end
488-
open("g_opt_value.txt", "a") do io
489-
write(io, "$g_opt \n")
490-
end
472+
g_opt, p_opt = gf_p_optimize(p_opt; r, β, η, TOL, MAX_ITER, phys_params, fem_params)
491473
end
492474
@show g_opt
493475

@@ -497,9 +479,7 @@ end
497479
#
498480

499481
using GLMakie, GridapMakie
500-
501-
p_max = readdlm("p_opt_value.txt", Float64)
502-
p0 = p_max[:]
482+
p0 = p_opt
503483

504484
pf_vec = pf_p0(p0; r, fem_params)
505485
pfh = FEFunction(fem_params.Pf, pf_vec)
@@ -524,7 +504,7 @@ save("shape.png", fig)
524504
# For the electric field, recall that $\vert E\vert^2\sim\vert \frac{1}{\epsilon}\nabla H\vert^2$, the factor 2 below comes from the amplitude compared to the incident plane wave. We can see that the optimized shapes are very similiar to the optimized shape in Ref. [6], proving our results.
525505
#
526506

527-
maxe = 30 # Maximum electric field
507+
maxe = 30 # Maximum electric field magnitude compared to the incident plane wave
528508
e1=abs2(phys_params.n_air^2)
529509
e2=abs2(phys_params.n_metal^2)
530510

0 commit comments

Comments
 (0)