Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
paulxshen committed Dec 6, 2024
1 parent 4ea1c91 commit 3305804
Show file tree
Hide file tree
Showing 20 changed files with 1,492 additions and 370 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ docs/build/
*.*.pyc
*.pyo
_*.py
/src/sim
/src/pic

# /src/sim
# /src/pic

Manifest.*
_.png
Expand Down
619 changes: 320 additions & 299 deletions Luminescent_AI_docs.ipynb

Large diffs are not rendered by default.

38 changes: 26 additions & 12 deletions lumi/src/bend.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,36 @@
import luminescent as lumi
import gdsfactory as gf
import numpy as np

N = 3
N = 3 # 3D or 2D
keys = ["2,1"] # same as keys=["o2@0,o1@0"]
nres = 40

radius = 1.5
wavelengths = 1.55
name = f"bend_R{radius}"

# radius = 2
# wavelengths = 1.1
# name = f"bend_R{radius}_{wavelengths}"
# wavelengths = lumi.wavelength_range(center=1.35, bandwidth=.4, length=3)
# name = f"bend_R{radius}_multi"

# N = 3 # 2D or 3D
# radius = 5
# wavelengths = 1.55
# name = f"bend_R{radius}"

radius = 2
wavelengths = lumi.wavelength_range(center=1.35, bandwidth=.4, length=3)
name = f"bend_R{radius}_multi"
# N = 3
# radius = 5
# wavelengths = np.linspace(1.5, 1.6, 5) # number or list or array
# name = f"bend_R{radius}_multi"

N = 2
radius = 5
wavelengths = 1.55
name = f"bend_R{radius}"
wavelengths = np.linspace(1.5, 1.6, 5) # number or list or array
nres = 20
name = f"bend_R{radius}_multi_{N}D"

c = gf.components.bend_circular(radius=radius, allow_min_radius_violation=True)
c.plot()
lumi.write_sparams(c, name=name, wavelengths=wavelengths, keys=["2,1"], # same as keys=["o2@0,o1@0"]
dx=0.05, N=N,
run=False)
lumi.write_sparams(c, name=name, wavelengths=wavelengths,
nres=nres, keys=keys, N=N, run=False)
# sol = lumi.load_solution(name=name)
6 changes: 3 additions & 3 deletions lumi/src/luminescent/gplugins/luminescent/inverse_design.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def gcell_problem(c, targets, iters,
wavelengths=wavelengths,
study="inverse_design",
keys=keys,
N=N, ** kwargs)
N=N, approx=N == 2, ** kwargs)

prob["restart"] = restart
prob["weights"] = {**{
Expand Down Expand Up @@ -111,10 +111,10 @@ def _bbox(b):
"bbox": _bbox(p.bbox()),
"symmetries": s,
"init": init,
"lvoid": lvoid,
"lsolid": lsolid,
} for p, s in zip(list(polys.values())[0], symmetries)
]
prob["lvoid"] = lvoid
prob["lsolid"] = lsolid
prob["stoploss"] = stoploss
prob["design_config"] = dict()
l = get_layers(layer_stack, fill_layer)[0]
Expand Down
42 changes: 20 additions & 22 deletions lumi/src/luminescent/gplugins/luminescent/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
from gdsfactory.generic_tech import LAYER_STACK, LAYER


def setup(c, study, dx, margin,
def setup(c, study, nres, wl,
bbox_layer=LAYER.WAFER,
zmargin2=None, zlims=None, core_layer=LAYER.WG,
port_source_offset="auto", source_margin="auto",
port_source_offset="auto", port_margin="auto",
runs=[], sources=[],
layer_stack=LAYER_STACK, materials=MATERIALS,
exclude_layers=[
DESIGN_LAYER, GUESS], N=3, Courant=None,
gpu=None, dtype=np.float32,
plot=False, framerate=0,
magic="", wd=os.path.join(os.getcwd(), "luminescent_runs"), name=None, **kwargs):
magic="", wd=os.path.join(os.getcwd(), "luminescent_runs"), name=None,
approx=False,
**kwargs):
prob = dict()
dx0 = dx
# dx *= 2
ratio = 6
dy = dx = wl/nres
dl = dx/ratio
# dl = .01
# ratio = int(dx/dl)
dy = dx
dz = 1 * dx

if not name:
Expand All @@ -43,13 +41,12 @@ def setup(c, study, dx, margin,
path = os.path.join(wd, name)
prob["path"] = path
prob["name"] = name

prob["wl"] = wl
prob = {**prob, **kwargs}
prob["dx"] = dx
prob["dy"] = dy
prob["dz"] = dz
prob["dl"] = dl
prob["dx0"] = dx0
prob["ratio"] = ratio
prob["dtype"] = str(dtype)
prob["timestamp"] = datetime.datetime.now().isoformat(
Expand All @@ -72,15 +69,15 @@ def setup(c, study, dx, margin,
hcore = d.thickness
zcore = d.zmin

zmargin1 = 2.5*hcore
zmargin1 = 3*hcore
hmode = hcore+2*zmargin1
hmode = trim(hmode, 2*dz)
zmargin1 = (hmode-hcore)/2

zmode = zcore-zmargin1
zcenter = zcore+hcore/2

zmargin2 = 2.5*hcore
zmargin2 = 2*hcore
zmargin2 = trim(zmargin2, 2*dz)
h = hcore+2*(zmargin1+zmargin2)
zmin = zcore-zmargin2-zmargin1
Expand All @@ -102,14 +99,16 @@ def setup(c, study, dx, margin,
port_width = max([p.width/1e3 for p in c.ports])
ps = portsides(c)
xmargin = ymargin = 2*port_width
source_port_margin = 6*port_width
source_port_margin = port_width if approx else 6*port_width
port_margin = 2*dx
margins = []
for p in ps:
if set(p).intersection(source_ports):
margins.append(source_port_margin)
# elif p:
# margins.append(border_margin)
margins.append(source_port_margin+port_margin)
elif set(p).intersection(nonsource_ports):
margins.append(port_margin)
else:
assert not p
margins.append(xmargin)
l0, w0 = c.bbox_np()[1]-c.bbox_np()[0]

Expand Down Expand Up @@ -177,8 +176,11 @@ def setup(c, study, dx, margin,

for run in runs:
for port in run["sources"]:
run["sources"][port]["center"] = (
np.array(c0.ports[port].center)/1e3).tolist()
s = c0.ports[port]
a = pi/180*s.orientation
center = np.array(s.center)/1e3 - \
np.array([cos(a), sin(a)])*port_margin
run["sources"][port]["center"] = center.tolist()

# a = min([min([materials[d.material]["epsilon"] for d in get_layers(
# layer_stack, l)]) for l in bbox_layer])
Expand Down Expand Up @@ -228,10 +230,6 @@ def setup(c, study, dx, margin,
wavelengths.append(wl)
wavelengths = sorted(set(wavelengths))

if source_margin == "auto":
source_margin = 2*dx
prob["source_margin"] = source_margin

bbox = c.bbox_np()

prob["mode_solutions"] = mode_solutions
Expand Down
14 changes: 5 additions & 9 deletions lumi/src/luminescent/gplugins/luminescent/sparams.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@
from gdsfactory.generic_tech import LAYER_STACK, LAYER


def sparams_problem(c: gf.Component,
def sparams_problem(c: gf.Component, nres,
wavelengths,
margin=None, # zmargin=None,zlims=None,
dx=.05,
entries=[],
center_wavelengths=None, keys=[],
entries=[], keys=[],
N=3, layer_stack=LAYER_STACK,
study="sparams",
**kwargs):
Expand All @@ -33,7 +30,7 @@ def sparams_problem(c: gf.Component,
wavelengths = [wavelengths]
else:
wavelengths = wavelengths
wavelengths, T = adjust_wavelengths(wavelengths)
wavelengths, wl, T = adjust_wavelengths(wavelengths)
for w in wavelengths:
for k in keys:
entries.append([w, *unpack_sparam_key(k)])
Expand Down Expand Up @@ -79,15 +76,14 @@ def sparams_problem(c: gf.Component,
"normal": normal_from_orientation(c.ports[o].orientation),
"center": (np.array(c.ports[o].center)/1e3).tolist(),
"width": (np.array(c.ports[o].width)/1e3).tolist(),
# "endpoints": extend(c.ports[o].endpoints, margin),
"wavelength_mode_numbers": {w: list(range(imow[i][mi][o]+1)) for w in wavelengths},
} for o in imow[i][mi]}}
d["sources"] = SortedDict(d["sources"])
d["monitors"] = SortedDict(d["monitors"])
runs.append(d)

prob = setup(c, study=study, dx=dx,
runs=runs, margin=margin,
prob = setup(c, study=study, nres=nres, wl=wl,
runs=runs,
layer_stack=layer_stack, N=N, **kwargs)
prob["wavelengths"] = wavelengths
prob["Ttrans"] = None
Expand Down
4 changes: 2 additions & 2 deletions lumi/src/luminescent/gplugins/luminescent/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ def wavelength_range(center, bandwidth, length=3):

def adjust_wavelengths(wavelengths):
if len(wavelengths) == 1:
return wavelengths, 1
return wavelengths, wavelengths[0], 1
wavelengths = sorted(set(wavelengths), reverse=True)
wl = wavelengths[round((len(wavelengths)-1)/2)]
freqs = [wl/w for w in wavelengths]
Expand All @@ -202,4 +202,4 @@ def adjust_wavelengths(wavelengths):
wavelengths = [wl/f for f in reversed(freqs)]
print(
f"wavelengths has been adjusted to facilitate simulation:\n{wavelengths}")
return wavelengths, nresfreq
return wavelengths, wl, nresfreq
12 changes: 6 additions & 6 deletions lumi/src/mode_converter_inverse_design_test.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# recommended RAM: >16G
from pprint import pprint
import luminescent as lumi

name = "mode_converter" # can be any string
c = lumi.gcells.mimo(west=1, east=1, l=5.0, w=2.0,
c = lumi.gcells.mimo(west=1, east=1, l=5.0, w=2.4,
wwg=.5, taper=.05)
targets = {"tparams": {1.55: {"o2@1,o1@0": 1.0}}}

prob = lumi.gcell_problem(
c, targets, name=name,
lvoid=0.2, lsolid=0.1, dx=0.1,
N=2, iters=100, stoploss=.1)
lumi.solve(prob)
sol = lumi.load_solution(name)
N=2, nres=15,
lvoid=0.15, lsolid=.15,
iters=100, stoploss=.05, )
# lumi.solve(prob)
lumi.solve(prob, run=False)
2 changes: 2 additions & 0 deletions lumi/src/movie.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import luminescent as lumi
lumi.finetune(0, name="mode_converter", framerate=30, run=False)
11 changes: 6 additions & 5 deletions lumi/src/splitter.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
from pprint import pprint
import luminescent as lumi

name = "1x2_splitter"
c = lumi.gcells.mimo(west=1, east=2, l=4.5, w=2.4, wwg=.5, taper=.05, )
name = "splitter"
c = lumi.gcells.mimo(west=1, east=2, l=4.0, w=2.0, wwg=.5, taper=.05, )
targets = {
"tparams": {1.55: {"2,1": 0.5}},
}

prob = lumi.gcell_problem(
c, targets, name=name,
N=2, symmetries=[1], lvoid=0.15, lsolid=.15, dx=0.1,
eta=.2, iters=50, stoploss=.05, )
sol = lumi.solve(prob)
N=2, nres=15, symmetries=[1],
lvoid=0.15, lsolid=.15,
iters=50, stoploss=.05, )
lumi.solve(prob, run=False)
# name = "1x4_splitter"
# c = lumi.gcells.mimo(west=1, east=4, l=6.0, w=6.0, wwg=.5, taper=.05, )
# targets = {
Expand Down
3 changes: 2 additions & 1 deletion lumi/src/straight.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@
name="straight",
# core_layer=LAYER.WG, bbox_layer=LAYER.WAFER, # defaults
# layer_stack=LAYER_STACK, materials=MATERIALS, # defaults
dx=0.05, N=3,) # run=False) # , gpu="CUDA",) # or gpu=None
dx=0.1, N=2, run=False)
# dx=0.05, N=3,) # run=False) # , gpu="CUDA",) # or gpu=None
# sol = lumi.lumi_solution()
2 changes: 1 addition & 1 deletion lumi/src/tiny_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@
c, targets, name=name,
lvoid=0.2, dx=0.1, iters=2,
N=2, gpu=None,)
sol = lumi.solve(prob)
sol = lumi.solve(prob, run=False)
4 changes: 2 additions & 2 deletions src/core/monitors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ function MonitorInstance(m::Monitor, g, ϵ, temp, mode_solutions=nothing)
dx = deltas[1][1]
@unpack λmodenums, λmodes = specs
if !isnothing(λmodenums)
start = int((center3 + lb3) / dl)
stop = int((center3 + ub3) / dl)
start = round((center3 + lb3) / dl)
stop = round((center3 + ub3) / dl)
start, stop = min.(start, stop), max.(start, stop)

sel = abs.(stop - start) .>= 0.001
Expand Down
20 changes: 17 additions & 3 deletions src/core/sources.jl
Original file line number Diff line number Diff line change
Expand Up @@ -131,8 +131,8 @@ function SourceInstance(s::Source, g, ϵ, temp, mode_solutions=nothing)
dx = deltas[1][1]
@unpack λmodenums, λmodes = specs
if !isnothing(λmodenums)
start = int((center3 + lb3) / dl)
stop = int((center3 + ub3) / dl)
start = round((center3 + lb3) / dl)
stop = round((center3 + ub3) / dl)
start, stop = min.(start, stop), max.(start, stop)

sel = abs.(stop - start) .>= 0.001
Expand Down Expand Up @@ -172,7 +172,21 @@ function SourceInstance(s::Source, g, ϵ, temp, mode_solutions=nothing)

o = NamedTuple([k => F.(start - fl[:, 1] + 1) for (k, fl) = pairs(field_lims)])
λmodes = fmap(F, λmodes)
sigmodes = reduce(vcat, [collect(zip(fill(λ, length(modes)), modes)) for (λ, modes) = (pairs(λmodes))])

λmodes = sort(λmodes, by=kv -> kv[1])
λs = keys(λmodes)
modess = values(λmodes)
iss = cluster(λs)
sigmodes = map(iss) do is
f = t -> sum(getindex.((λs,), is)) do λ
cispi(2t / λ) |> C
end
_modess = getindex.((modess,), is)
modes = _modess[round(length(_modess) / 2 + 0.1)]
f, modes
end

sigmodes = reduce(vcat, [collect(zip(fill(f, length(modes)), modes)) for (f, modes) = sigmodes])
sigmodes = [
begin
_f = if isa(sig, Number)
Expand Down
16 changes: 16 additions & 0 deletions src/core/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -81,3 +81,19 @@ function _make_field_deltas(d, N, field_boundvals, field_sizes, i, isdiff=false)
reshape(d, Tuple(1 - sel + field_sizes[k][i] * sel))
end for (k, v) = pairs(field_boundvals)])
end
function cluster(v)
groups = []
group = []
for (i, x) = enumerate(v)
if isempty(group) || x / v[group[1]] < 1.1
push!(group, i)
else
push!(groups, group)
group = [i]
end
end
if !isempty(group)
push!(groups, group)
end
groups
end
Loading

0 comments on commit 3305804

Please sign in to comment.