Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
paulxshen committed Sep 1, 2024
1 parent 85b3503 commit e0e4a71
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 139 deletions.
6 changes: 3 additions & 3 deletions lumi/Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is machine-generated - editing it directly is not advised

julia_version = "1.10.1"
julia_version = "1.10.5"
manifest_format = "2.0"
project_hash = "e6029301621baa2fecc22ebba39262d01d08f0c8"

Expand Down Expand Up @@ -359,7 +359,7 @@ weakdeps = ["Dates", "LinearAlgebra"]
[[deps.CompilerSupportLibraries_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "e66e0078-7015-5450-92f7-15fbd957f2ae"
version = "1.1.0+0"
version = "1.1.1+0"

[[deps.CompositionsBase]]
git-tree-sha1 = "802bb88cd69dfd1509f6670416bd4434015693ad"
Expand Down Expand Up @@ -2296,7 +2296,7 @@ version = "0.15.2+0"
[[deps.libblastrampoline_jll]]
deps = ["Artifacts", "Libdl"]
uuid = "8e850b90-86db-534c-a0d3-1478176c7d93"
version = "5.8.0+1"
version = "5.11.0+0"

[[deps.libfdk_aac_jll]]
deps = ["Artifacts", "JLLWrappers", "Libdl"]
Expand Down
2 changes: 1 addition & 1 deletion lumi/pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "luminescent"
version = "0.1.58"
version = "0.1.59"
description = "Lumi FDTD by Luminescent AI is a GPU and automatic differentiation (AD) compatible FDTD package for simulation and inverse design in semiconductor photonics. Create layout in gdsfactory. Then use the Lumi Python plugin for calculating sparams or perform inverse design on Lumi's Julia backend."
authors = ["Paul Shen"]
license = "MIT"
Expand Down
8 changes: 4 additions & 4 deletions lumi/src/luminescent/gplugins/luminescent/run.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ sol = gfrun(path)
# # Pkg.resolve()

# using Pkg
# pkg"dev C:\Users\pxshe\OneDrive\Desktop\Porcupine.jl"
# pkg"dev C:\Users\pxshe\OneDrive\Desktop\ArrayPadding.jl"
# pkg"dev C:\Users\pxshe\OneDrive\Desktop\Jello.jl"
# pkg"dev C:\Users\pxshe\OneDrive\Desktop\Luminescent.jl"
# pkg"add https://github.com/paulxshen/Luminescent.jl,Porcupine,Jello,ArrayPadding;up"

# using Pkg
# pkg"dev C:\Users\pxshe\OneDrive\Desktop\Porcupine.jl;dev C:\Users\pxshe\OneDrive\Desktop\ArrayPadding.jl; dev C:\Users\pxshe\OneDrive\Desktop\Jello.jl;dev C:\Users\pxshe\OneDrive\Desktop\Luminescent.jl;up"
24 changes: 12 additions & 12 deletions lumi/src/straight_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@

c = gf.components.straight(.5)

sol = lumi.write_sparams(
# c, wavelengths=[1.55], keys=["2,1"],
# bbox_layer=[LAYER.WAFER, LAYER.SLAB90],
c, wavelengths=[1.55], keys=["o2@1,o1@1"],
bbox_layer=LAYER.WAFER,
dx=0.1, approx_2D=True, gpu="CUDA",)
# dx=0.1, approx_2D=False, gpu="CUDA",) # dtype="16", dev=True,)
lumi.show_solution()
pp.pprint(sol)
raise ValueError("stop here")
# sol = lumi.write_sparams(
# # c, wavelengths=[1.55], keys=["2,1"],
# # bbox_layer=[LAYER.WAFER, LAYER.SLAB90],
# c, wavelengths=[1.55], keys=["o2@1,o1@1"],
# bbox_layer=LAYER.WAFER,
# dx=0.1, approx_2D=True, gpu="CUDA",)
# # dx=0.1, approx_2D=False, gpu="CUDA",) # dtype="16", dev=True,)
# lumi.show_solution()
# pp.pprint(sol)
# raise ValueError("stop here")

sleep(1)
for (approx_2D, gpu, dtype, wavelengths) in itertools.product(
[True, False],
[None, "CUDA"],
# ["f32"]
["f32", "f16"],
["f32"],
# ["f32", "f16"],
[[1.55], ]):
lumi.write_sparams(c, wavelengths=wavelengths, keys=["2,1"], dx=0.1,
bbox_layer=LAYER.WAFER,
Expand Down
56 changes: 25 additions & 31 deletions lumi/src/tiny_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,29 @@
}}}
# c.show()

prob = lumi.inverse_design_problem(
c, targets,
# bbox_layer=LAYER.WAFER,
# lmin=0.2, dx=0.1, iters=2, eta=10., approx_2D=False, compression=True) # gpu="CUDA", dev=True)
lmin=0.2, dx=0.1, iters=2, approx_2D=True, compression=False) # gpu="CUDA", dev=True)
# lmin=0.2, dx=0.1, iters=2, eta=10., approx_2D=True, gpu="CUDA", dev=True)
sol = lumi.solve(prob, )
# sol = lumi.finetune(2)
sol = lumi.load_solution()
c = lumi.apply_design(c, sol)
c.show()
# raise ValueError("stop here")

# for (approx_2D, gpu, dtype, ) in itertools.product(
# [True,],
# [None, "CUDA"],
# # [None, ],
# ["f32"],
# # ["f32", "f16"],
# ):
# prob = lumi.inverse_design_problem(
# c, targets=targets,
# bbox_layer=LAYER.WAFER,
# lmin=0.2, dx=0.1, iters=2, eta=1., approx_2D=approx_2D, gpu=gpu, dtype=dtype, run=False)
# sol = lumi.solve(prob, run=False)
# sleep(1)
# path="precompile_execution")
# lmin=0.2, dx=0.1, iters=2, eta=10., approx_2D=True, gpu="CUDA")
# prob = lumi.inverse_design_problem(
# c, targets,
# # bbox_layer=LAYER.WAFER,
# # lmin=0.2, dx=0.1, iters=2, eta=10., approx_2D=False, compression=True) # gpu="CUDA", dev=True)
# lmin=0.2, dx=0.1, iters=2, approx_2D=True, compression=False) # gpu="CUDA", dev=True)
# # lmin=0.2, dx=0.1, iters=2, eta=10., approx_2D=True, gpu="CUDA", dev=True)
# sol = lumi.solve(prob, )
# # sol = lumi.finetune(2)
# sol = lumi.load_solution()
# c = lumi.apply_design(c, sol)
# c.show()
# # raise ValueError("stop here")

# lumi.show_solution()
# print("post optim tparams:")
# pprint(sol["tparams"])
for (approx_2D, gpu, dtype, ) in itertools.product(
[True,],
# [None, "CUDA"],
[None, ],
["f32"],
# ["f32", "f16"],
):
prob = lumi.inverse_design_problem(
c, targets,
bbox_layer=LAYER.WAFER,
lmin=0.2, dx=0.1, iters=2, approx_2D=approx_2D, gpu=gpu, dtype=dtype, run=False)
sol = lumi.solve(prob, run=False)
sleep(1)
60 changes: 0 additions & 60 deletions src/geometry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,51 +27,6 @@ function apply_subpixel_averaging(sas, gs)
sas = ignore_derivatives() do
sas
end
# if length(sas) == 6
# return (;
# ϵxx=_apply_subpixel_averaging(sas.ϵxx, gs.ϵ),
# ϵyy=_apply_subpixel_averaging(sas.ϵyy, gs.ϵ),
# μzz=_apply_subpixel_averaging(sas.μzz, gs.μ),
# σxx=_apply_subpixel_averaging(sas.σxx, gs.σ),
# σyy=_apply_subpixel_averaging(sas.σyy, gs.σ),
# mzz=_apply_subpixel_averaging(sas.mzz, gs.m),)
# end
# (; ϵxx=_apply_subpixel_averaging(sas.ϵxx, gs.ϵ),
# ϵyy=_apply_subpixel_averaging(sas.ϵyy, gs.ϵ),
# ϵzz=_apply_subpixel_averaging(sas.ϵzz, gs.ϵ),
# μxx=_apply_subpixel_averaging(sas.μxx, gs.μ),
# μyy=_apply_subpixel_averaging(sas.μyy, gs.μ),
# μzz=_apply_subpixel_averaging(sas.μzz, gs.μ),
# σxx=_apply_subpixel_averaging(sas.σxx, gs.σ),
# σyy=_apply_subpixel_averaging(sas.σyy, gs.σ),
# σzz=_apply_subpixel_averaging(sas.σzz, gs.σ), mxx=_apply_subpixel_averaging(sas.mxx, gs.m),
# myy=_apply_subpixel_averaging(sas.myy, gs.m),
# mzz=_apply_subpixel_averaging(sas.mzz, gs.m),)

# gks = ignore_derivatives() do
# getindex.(keys(sas), 1)
# end

# namedtuple([k => begin
# # sa = sas[k]
# reduce(enumerate(eachcol(sa.v)), init=a) do a, (i, (l, r))
# d = ndims(a)
# select = i .== (1:d) |> Tuple
# if l == -1
# a = pad(a, :replicate, select, 0)
# end
# if r == 1
# a = pad(a, :replicate, 0, select)
# end
# if l == -1 == r
# a = (2selectdim(a, i, 2:(size(a, i))) - diff(a, dims=i)) / 2
# elseif l == 1 == r
# a = (2selectdim(a, i, 1:(size(a, i)-1)) + diff(a, dims=i)) / 2
# end
# a
# end
# end for ((k, sa), a) = zip(pairs(sas), getindex.((gs,), gks))])
# end for k = keys(sas)])
namedtuple([k => _apply_subpixel_averaging(sas[k], values(gs)[findfirst(keys(gs)) do gk
startswith(string(k), string(gk))
end]) for k = keys(sas)])
Expand All @@ -87,22 +42,7 @@ function _apply_geometry_padding(p::AbstractVector{<:OutPad}, a)
end

function apply_geometry_padding(gps, gs)
# (;
# ϵ=_apply_geometry_padding(gps.ϵ, gs.ϵ),
# μ=_apply_geometry_padding(gps.μ, gs.μ),
# σ=_apply_geometry_padding(gps.σ, gs.σ),
# m=_apply_geometry_padding(gps.m, gs.m),)
# global _gps = gps
# global _gs = gs
# dict([(k, apply(gps[k], gs[k])) for k = Porcupine.keys(gs)])
namedtuple([k => _apply_geometry_padding(gps[k], gs[k]) for k = keys(gs)])
# dict([k => _apply_geometry_padding(gps[k], gs[k]) for k = keys(gs)])
# namedtuple(r)
# v = Porcupine.values(gs)
# u = ignore_derivatives() do
# getindex.((gps,), keys(gs))
# end
# namedtuple([k => apply(u, v) for (k, u, v) = zip(Porcupine.keys(gs), u, v)])
end

function _apply_field_padding(p::AbstractVector{<:InPad}, a::AbstractArray; nonzero_only=false)
Expand Down
30 changes: 11 additions & 19 deletions src/gf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Optimisers.maywrite(::CUDA.CUSPARSE.CuSparseMatrixCSC{Float32,Int32}) = true
Optimisers.maywrite(::CUDA.CUSPARSE.CuSparseMatrixCSC{Float16,Int32}) = true
Optimisers.maywrite(::SparseArrays.SparseMatrixCSC{Float32,Int32}) = true
Optimisers.maywrite(::SparseArrays.SparseMatrixCSC{Float16,Int32}) = true
# using Porcupine: keys, values, first

function julia_main()::Cint
if !isempty(ARGS)
Expand All @@ -14,7 +13,6 @@ function julia_main()::Cint
end
return 0
end
# add Dates, DataStructures, JSON, Images, BSON,Flux,CUDA
function lastrun(s=nothing, path=joinpath(pwd(), "lumi_runs"))
l = filter(isdir, readdir(path, join=true))
sort!(l)
Expand All @@ -32,21 +30,17 @@ function lastrun(s=nothing, path=joinpath(pwd(), "lumi_runs"))
end
end

function write_sparams(runs, run_probs, g, path, origin, dx,
function write_sparams(runs, run_probs, geometry, path, origin, dx,
designs=nothing, design_config=nothing, models=nothing;
img=nothing, autodiff=false, compression=false, verbose=false, perturb=nothing, with=false, ls=nothing, kw...)
F = run_probs[1].F
geometry = make_geometry(models, origin, dx, g, designs, design_config; F, perturb)
geometry = make_geometry(models, origin, dx, geometry, designs, design_config; F, perturb)

i = 1


global sols = [
sols = [
begin
# prob[:geometry] = geometry
# prob = merge(prob, (; geometry,))
prob[:geometry] = geometry
#@debug typeof(prob.u0.E.Ex), typeof(prob.geometry.ϵ)
sol = solve(prob, geometry; autodiff, ls, compression, verbose)
sol = solve(prob; autodiff, ls, compression, verbose)

ignore() do
if !isnothing(img)
Expand All @@ -62,7 +56,7 @@ function write_sparams(runs, run_probs, g, path, origin, dx,
end
end
sol
end for prob in run_probs
end for (i, prob) in enumerate(run_probs)
# end for (i, prob) in enumerate(run_probs)
]
# return sols[1].forward_mode_powers[1][1][1]
Expand Down Expand Up @@ -190,7 +184,6 @@ end
# global virgin, stop, best, best0, sparams0
function gfrun(path; kw...)
println("setting up simulation...")
# do something based on ARGS?
PROB_PATH = joinpath(path, "prob.bson")
SOL_PATH = joinpath(path, "sol.json")

Expand All @@ -199,9 +192,7 @@ function gfrun(path; kw...)
tol = 1e-3
dosave = false
verbose = false
#=
We load design layout which includes a 2d device of device waveguide geometry as well as variables with locations of ports, sources, design regions and material properties.
=#

@load PROB_PATH name dtype margin zmargin source_margin Courant port_source_offset portsides runs ports dx components study mode_solutions eps_2D eps_3D mode_height zmin thickness zcore gpu_backend d magic
F = Float32
if contains(dtype, "16")
Expand Down Expand Up @@ -438,7 +429,6 @@ function gfrun(path; kw...)
verbose,)
end for (i, (run, sources, monitors)) in enumerate(zip(runs, runs_sources, runs_monitors))
]
const g0 = run_probs[1].geometry

if !isempty(gpu_backend)
println("using $gpu_backend backend.")
Expand All @@ -456,6 +446,7 @@ function gfrun(path; kw...)
else
println("using CPU backend.")
end
g0 = run_probs[1].geometry

virgin = true
# error()
Expand Down Expand Up @@ -504,7 +495,7 @@ function gfrun(path; kw...)

for i = 1:iters
# for i = 1:20
global virgin, stop, best, best0, sparams0
# global virgin, stop, best, best0, sparams0
global img = if virgin
virgin = false
"before.png"
Expand All @@ -519,6 +510,7 @@ function gfrun(path; kw...)
F, img, autodiff, compression, ls)
l = 0
for k = keys(targets)
print("losses ")
y = targets[k]
err = -
if :phasediff == k
Expand Down Expand Up @@ -551,7 +543,7 @@ function gfrun(path; kw...)
print("$(k): $_l ")
l += _l
end
println("\n$i loss $l\n")
println("\n($i) weighted total loss $l\n")
l
end
elseif "phase_shifter" == preset.name
Expand Down
7 changes: 1 addition & 6 deletions src/main.jl
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
using Humanize: digitsep
using UnPack, LinearAlgebra, Statistics, Random, Jello, Functors, DataStructures, GPUArraysCore
# using Meshes: Sphere
using ArrayPadding: left, right
using Zygote
using CairoMakie
# Random.seed!(1)ImageIO,ImageTransformations,

using Porcupine: keys, values, fmap
using Porcupine
using ArrayPadding
# include("../../Porcupine.jl/src/main.jl")
# include("../../ArrayPadding.jl/src/main.jl")
# include("../../Jello.jl/src/main.jl")
include("constants.jl")
include("utils.jl")
include("modes.jl")
Expand All @@ -33,7 +28,7 @@ using Dates, DataStructures, JSON, Images, BSON, Flux, CUDA, GPUArraysCore
using Flux: mae, Adam
using Zygote: withgradient, Buffer, ignore_derivatives
using BSON: @save, @load, load
include("gf.jl")
# using AbbreviatedStackTraces

# include("gf.jl")
# include("main.jl")
5 changes: 2 additions & 3 deletions src/solve.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ function f2(((u, mf), p, (dx, dt, field_padding, source_instances, autodiff), (T
((u, mf), p, (dx, dt, field_padding, source_instances, autodiff), (T, monitor_instances))
end

function solve(prob, geometry; autodiff=false, compression=false, ls=nothing, verbose=false, kwargs...)
function solve(prob, ; autodiff=false, compression=false, ls=nothing, verbose=false, kwargs...)
# global _prob = prob
@unpack dx, dt, u0, field_padding, geometry_padding, subpixel_averaging, source_instances, monitor_instances, transient_duration, F, polarization, steady_state_duration, d, n = prob
@unpack dx, dt, u0, geometry, field_padding, geometry_padding, subpixel_averaging, source_instances, monitor_instances, transient_duration, F, polarization, steady_state_duration, d, n = prob

p = apply_geometry_padding(geometry_padding, geometry)
# global asdffsd = p
p = apply_subpixel_averaging(subpixel_averaging, p)
# return p.ϵxx |> sum

Expand Down

0 comments on commit e0e4a71

Please sign in to comment.