Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #7

Merged
merged 3 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .vscode/settings.json

This file was deleted.

2 changes: 1 addition & 1 deletion docs/.documenter-siteinfo.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"documenter":{"julia_version":"1.10.0","generation_timestamp":"2024-02-14T12:24:44","documenter_version":"1.2.1"}}
{"documenter":{"julia_version":"1.10.0","generation_timestamp":"2024-02-15T11:52:47","documenter_version":"1.2.1"}}
Binary file modified docs/assets/3d_quarter_wavelength_antenna_nres_16.mp4
Binary file not shown.
Binary file modified docs/assets/3d_scattering_nres_16.mp4
Binary file not shown.
27 changes: 20 additions & 7 deletions docs/index.html

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion docs/search_index.js

Large diffs are not rendered by default.

Binary file not shown.
20 changes: 12 additions & 8 deletions examples/3d_dipole_antenna/[simulation]_3d_dipole_antenna.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ dx = 1.0f0 / nres # pixel resolution in [wavelengths]
l = 2
sz0 = nres .* (l, l, l)

boundaries = [PEC(-3)] # unspecified boundaries default to PML
n = [0, 0, 1]
monitors = [
Monitor([l / 2, l / 2, 0.1f0], [1, 1, 0], n),
Monitor([l / 2, l / 2, 0.5f0], [1, 1, 0], n),
]
boundaries = [PEC(-3)] # ground plane on -z, unspecified boundaries default to PML
n =
monitors = [
# Monitor([.5+l / 2, 0, 1], [0,1, 1,], [1,0,0]),
Monitor([l / 2, l / 2, 1], [1, 1, 0], [0, 0, 1]),
]
sources = [
Source(t -> cos(F(2π) * t), [l / 2, l / 2, 0.125f0], [0, 0, 0.25f0]; Jz=1),
]
Expand All @@ -35,8 +35,12 @@ p = apply(geometry_splits; ϵ, μ, σ, σm)

# run simulation
t = 0:dt:T
u = similar([u0], length(t))
@showtime u = accumulate!((u, t) -> step!(u, p, t, dx, dt, field_padding, source_instances), u, t, init=deepcopy(u0))
u = [[similar.(a) for a = u0] for t = t]
u[1] = u0
@showtime reduce(
(u, (u1, t)) -> step!(u1, u, p, t, dx, dt, field_padding, source_instances),
zip(u[2:end], t[1:end-1]),
init=u0)
y = hcat([power.((m,), u) for m = monitor_instances]...)

Ez = map(u) do u
Expand Down
8 changes: 6 additions & 2 deletions examples/3d_periodic_scattering/3d_periodic_scattering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,12 @@ p = apply(geometry_splits; ϵ, μ, σ, σm)

# run simulation
t = 0:dt:T
u = similar([u0], length(t))
@showtime u = accumulate!((u, t) -> step!(u, p, t, dx, dt, field_padding, source_instances), u, t, init=deepcopy(u0))
u = [[similar.(a) for a = u0] for t = t]
u[1] = u0
@showtime reduce(
(u, (u1, t)) -> step!(u1, u, p, t, dx, dt, field_padding, source_instances),
zip(u[2:end], t[1:end-1]),
init=u0)
y = hcat([power.((m,), u) for m = monitor_instances]...)

# make movie
Expand Down
Binary file not shown.
4 changes: 4 additions & 0 deletions makedocs/docs.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
cd "C:\Users\Paul\Documents\New folder"
xcopy /Y makedocs\build\* docs
xcopy /Y examples\3d_dipole_antenna\*.mp4 docs\assets
xcopy /Y examples\3d_periodic_scattering\*.mp4 docs\assets
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@ p0 = make_geometry(model0, base, μ, σ, σm)
function metrics(model, T=T;)
p = make_geometry(model, base, μ, σ, σm)
# run simulation
# u = reduce((u, t) -> step!(u, p, t, dx, dt, field_padding, source_instances;), 0:dt:T-1, init=deepcopy(u0))
# y = reduce(((u, y), t) -> (
# step!(u, p, t, dx, dt, field_padding, source_instances),
# y + dt * power.(monitor_instances, (u,))
# ),
# T-1+dt:dt:T,
# init=(u, zeros(F, length(monitor_instances))))[2]
u = reduce((u, t) -> step!(u, p, t, dx, dt, field_padding, source_instances;), 0:dt:T-1, init=deepcopy(u0))
y = reduce(((u, y), t) -> (
step!(u, p, t, dx, dt, field_padding, source_instances),
Expand Down
4 changes: 1 addition & 3 deletions makedocs/make.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,4 @@ makedocs(
# for more information.
#=deploydocs(
repo = "<repository url>"
)=#
run(`cmd /C MOVE /Y makedocs/build/\* docs/`)
run(`cmd /C MOVE /Y examples/\*/\*.mp4 docs/assets/`)
)=#
24 changes: 17 additions & 7 deletions makedocs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,20 @@ Differentiable FDTD package for inverse design & topology optimization in photon
### Quarter wavelength antenna
![](assets/3d_quarter_wavelength_antenna_nres_16.mp4)
### Inverse design of compact silicon photonic splitter (coming soon)
<!-- In progress, split ratio isn't correct -->
<!-- ![](assets/pre_training_nres_16.mp4) -->
<!-- ![](assets/post_training_nres_16.mp4) -->

## Quickstart
We do a quick 3d simulation of plane wave scattering on periodic array of dielectric spheres (first gallery movie)
```julia
"""
simulation of plane wave scattering on periodic array of dielectric spheres
"""

using UnPack, LinearAlgebra, GLMakie
using FDTDEngine,FDTDToolkit
# using FDTDEngine,FDTDToolkit
dir = pwd()
include("$(dir)/src/main.jl")
include("$dir/../FDTDToolkit.jl/src/main.jl")


name = "3d_scattering"
T = 8.0f0 # simulation duration in [periods]
Expand Down Expand Up @@ -54,8 +59,12 @@ p = apply(geometry_splits; ϵ, μ, σ, σm)

# run simulation
t = 0:dt:T
u = similar([u0], length(t))
@showtime u = accumulate!((u, t) -> step!(u, p, t, dx, dt, field_padding, source_instances), u, t, init=deepcopy(u0))
u = [[similar.(a) for a = u0] for t = t]
u[1] = u0
@showtime reduce(
(u, (u1, t)) -> step!(u1, u, p, t, dx, dt, field_padding, source_instances),
zip(u[2:end], t[1:end-1]),
init=u0)
y = hcat([power.((m,), u) for m = monitor_instances]...)

# make movie
Expand All @@ -75,6 +84,7 @@ recordsim("$dir/$(name)_nres_$nres.mp4", Ez, y;
axis1=(; title="$name\nEz"),
axis2=(; title="monitor powers"),
)

```
## Installation
Install via
Expand Down Expand Up @@ -112,7 +122,7 @@ power_density

## Physics
```@docs
step3!
step!
```
<!-- step1 -->
<!-- stepTMz -->
Expand Down
22 changes: 11 additions & 11 deletions src/maxwell.jl
Original file line number Diff line number Diff line change
@@ -1,49 +1,49 @@
"""
function step3(u, p, t, field_padding, source_instances)
function step!(u1, u, p, t, field_padding, source_instances)

Updates fields for 3d
Updates fields for 3d. Writes new field into buffer array u1
"""
function step3!(u, p, t, dx, dt, field_padding, source_instances)
function step!(u1, u, p, t, dx, dt, field_padding, source_instances)
∇ = StaggeredDel([dx, dx, dx])
ϵ, μ, σ, σm = p
E, H = u
E1, H1 = u1
J = apply(source_instances, t; Jx=0, Jy=0, Jz=0)

# first update E
Hx, Hy, Hz = H
H = mark(field_padding; Hx, Hy, Hz)
dEdt = (∇ × H - σ * E - J) / ϵ

E_ = bufferfrom.(E)
E_ = bufferfrom.(E1)
for i = 1:3
E_[i][:, :, :] = E[i] + dEdt[i] * dt
end
Ex, Ey, Ez = [copy(E_[1]), copy(E_[2]), copy(E_[3])]
# E .= E + dEdt * dt

Ex, Ey, Ez = E
apply!(field_padding; Ex, Ey, Ez)
H = collect.(H)

# then update H
E = mark(field_padding; Ex, Ey, Ez)
dHdt = -(∇ × E .+ σm * H) / μ

H_ = bufferfrom.(H)
H_ = bufferfrom.(H1)
for i = 1:3
H_[i][:, :, :] = H[i] + dHdt[i] * dt
end
Hx, Hy, Hz = [copy(H_[1]), copy(H_[2]), copy(H_[3])]
# H .= H + dHdt * dt

Hx, Hy, Hz = H
apply!(field_padding; Hx, Hy, Hz)
E = collect(collect.(E))
H = [Hx, Hy, Hz]
# E = collect(collect.(E))
# H = [Hx, Hy, Hz]

[E, H]
# [E, H]
u1
end
step! = step3!
step3! = step!
# Flux.trainable(m::PaddedArray) = (; a=m.a)

"""
Expand Down