feat: lazy absorber BC w/socket set fix

main
Sofus Albert Høgsbro Rose 2024-05-27 18:19:57 +02:00
parent a3551c68b7
commit 44a0ea95d3
Signed by: so-rose
GPG Key ID: AD901CB0F3701434
6 changed files with 158 additions and 37 deletions

View File

@ -369,15 +369,6 @@ class FuncFlow:
return data | {info.output: self.realize(params, symbol_values=symbol_values)} return data | {info.output: self.realize(params, symbol_values=symbol_values)}
# return {
# dim: (
# dim_idx
# if info.has_idx_cont(dim) or info.has_idx_labels(dim)
# else ??
# )
# for dim, dim_idx in self.dims
# } | {info.output: output_data}
#################### ####################
# - Composition Operations # - Composition Operations
#################### ####################

View File

@ -251,7 +251,7 @@ class ParamsFlow:
func_args=self.func_args + other.func_args, func_args=self.func_args + other.func_args,
func_kwargs=self.func_kwargs | other.func_kwargs, func_kwargs=self.func_kwargs | other.func_kwargs,
symbols=self.symbols | other.symbols, symbols=self.symbols | other.symbols,
is_differentiable=self.is_differentiable & other.is_differentiable, is_differentiable=self.is_differentiable and other.is_differentiable,
) )
def compose_within( def compose_within(

View File

@ -453,10 +453,17 @@ class MaxwellSimNode(bpy.types.Node, bl_instance.BLInstance):
created_sockets[socket_name] = socket_def created_sockets[socket_name] = socket_def
# Initialize Just-Created BL Sockets # Initialize Just-Created BL Sockets
for socket_name, socket_def in created_sockets.items(): for bl_socket_name, socket_def in created_sockets.items():
socket_def.preinit(all_bl_sockets[socket_name]) socket_def.preinit(all_bl_sockets[bl_socket_name])
socket_def.init(all_bl_sockets[socket_name]) socket_def.init(all_bl_sockets[bl_socket_name])
socket_def.postinit(all_bl_sockets[socket_name]) socket_def.postinit(all_bl_sockets[bl_socket_name])
# Invalidate Cached NoFlows
self._compute_input.invalidate(
input_socket_name=bl_socket_name,
kind=...,
unit_system=...,
)
def _sync_sockets(self) -> None: def _sync_sockets(self) -> None:
"""Synchronize the node's sockets with the active sockets. """Synchronize the node's sockets with the active sockets.

View File

@ -110,10 +110,11 @@ class AdiabAbsorbBoundCondNode(base.MaxwellSimNode):
col.label(text='2ε₀/Δt') col.label(text='2ε₀/Δt')
#################### ####################
# - Output # - FlowKind.Value
#################### ####################
@events.computes_output_socket( @events.computes_output_socket(
'BC', 'BC',
# Loaded
props={'active_socket_set'}, props={'active_socket_set'},
input_sockets={ input_sockets={
'Layers', 'Layers',
@ -124,33 +125,154 @@ class AdiabAbsorbBoundCondNode(base.MaxwellSimNode):
'σ Order': True, 'σ Order': True,
'σ Range': True, 'σ Range': True,
}, },
output_sockets={'BC'},
output_socket_kinds={'BC': ct.FlowKind.Params},
) )
def compute_adiab_absorber_bound_cond(self, props, input_sockets) -> td.Absorber: def compute_bc_value(self, props, input_sockets, output_sockets) -> td.Absorber:
r"""Computes the adiabatic absorber boundary condition based on the active socket set. r"""Computes the adiabatic absorber boundary condition based on the active socket set.
- **Simple**: Use `tidy3d`'s default parameters for defining the absorber parameters (apart from number of layers). - **Simple**: Use `tidy3d`'s default parameters for defining the absorber parameters (apart from number of layers).
- **Full**: Use the user-defined $\sigma$ parameters, specifically polynomial order and sim-relative min/max conductivity values. - **Full**: Use the user-defined $\sigma$ parameters, specifically polynomial order and sim-relative min/max conductivity values.
""" """
log.debug( output_params = output_sockets['BC']
'%s: Computing "%s" Adiabatic Absorber Boundary Condition (Input Sockets = %s)', layers = input_sockets['Layers']
self.sim_node_name,
props['active_socket_set'],
input_sockets,
)
has_output_params = not ct.FlowSignal.check(output_params)
has_layers = not ct.FlowSignal.check(layers)
active_socket_set = props['active_socket_set']
if has_layers and has_output_params and not output_params.symbols:
# Simple PML # Simple PML
if props['active_socket_set'] == 'Simple': if active_socket_set == 'Simple':
return td.Absorber(num_layers=input_sockets['Layers']) return td.Absorber(num_layers=layers)
# Full PML # Full PML
sig_order = input_sockets['σ Order']
sig_range = input_sockets['σ Range']
has_sig_order = not ct.FlowSignal.check(sig_order)
has_sig_range = not ct.FlowSignal.check(sig_range)
if has_sig_order and has_sig_range:
return td.Absorber( return td.Absorber(
num_layers=input_sockets['Layers'], num_layers=layers,
parameters=td.AbsorberParams( parameters=td.AbsorberParams(
sigma_order=input_sockets['σ Order'], sigma_order=sig_order,
sigma_min=input_sockets['σ Range'][0], sigma_min=sig_range[0],
sigma_max=input_sockets['σ Range'][1], sigma_max=sig_range[1],
), ),
) )
return ct.FlowSignal.FlowPending
####################
# - FlowKind.Func
####################
@events.computes_output_socket(
'BC',
kind=ct.FlowKind.Func,
# Loaded
props={'active_socket_set'},
input_sockets={
'Layers',
'σ Order',
'σ Range',
},
input_socket_kinds={
'Layers': ct.FlowKind.Func,
'σ Order': ct.FlowKind.Func,
'σ Range': ct.FlowKind.Func,
},
input_sockets_optional={
'σ Order': True,
'σ Range': True,
},
output_sockets={'BC'},
output_socket_kinds={'BC': ct.FlowKind.Params},
)
def compute_bc_func(self, props, input_sockets, output_sockets) -> td.Absorber:
r"""Computes the adiabatic absorber boundary condition based on the active socket set.
- **Simple**: Use `tidy3d`'s default parameters for defining the absorber parameters (apart from number of layers).
- **Full**: Use the user-defined $\sigma$ parameters, specifically polynomial order and sim-relative min/max conductivity values.
"""
layers = input_sockets['Layers']
has_layers = not ct.FlowSignal.check(layers)
active_socket_set = props['active_socket_set']
if has_layers:
# Simple PML
if active_socket_set == 'Simple':
return layers.compose_within(
enclosing_func=lambda _layers: td.Absorber(num_layers=_layers),
supports_jax=False,
)
# Full PML
sig_order = input_sockets['σ Order']
sig_range = input_sockets['σ Range']
has_sig_order = not ct.FlowSignal.check(sig_order)
has_sig_range = not ct.FlowSignal.check(sig_range)
if has_sig_order and has_sig_range:
return (layers | sig_order | sig_range).compose_within(
enclosing_func=lambda els: td.Absorber(
num_layers=els[0][0],
parameters=td.AbsorberParams(
sigma_order=els[0][1],
sigma_min=els[1][0],
sigma_max=els[1][1],
),
),
supports_jax=False,
)
return ct.FlowSignal.FlowPending
####################
# - FlowKind.Params
####################
@events.computes_output_socket(
'BC',
kind=ct.FlowKind.Params,
# Loaded
props={'active_socket_set'},
input_sockets={
'Layers',
'σ Order',
'σ Range',
},
input_socket_kinds={
'Layers': ct.FlowKind.Params,
'σ Order': ct.FlowKind.Params,
'σ Range': ct.FlowKind.Params,
},
input_sockets_optional={
'σ Order': True,
'σ Range': True,
},
)
def compute_params(self, props, input_sockets) -> td.Box:
layers = input_sockets['Layers']
has_layers = not ct.FlowSignal.check(layers)
active_socket_set = props['active_socket_set']
if has_layers:
# Simple PML
if active_socket_set == 'Simple':
return layers
# Full PML
sig_order = input_sockets['σ Order']
sig_range = input_sockets['σ Range']
has_sig_order = not ct.FlowSignal.check(sig_order)
has_sig_range = not ct.FlowSignal.check(sig_range)
if has_sig_order and has_sig_range:
return layers | sig_order | sig_range
return ct.FlowSignal.FlowPending
#################### ####################

View File

@ -93,10 +93,11 @@ class BoundCondsNode(base.MaxwellSimNode):
} }
#################### ####################
# - Output Socket Computation # - FlowKind.Value
#################### ####################
@events.computes_output_socket( @events.computes_output_socket(
'BCs', 'BCs',
kind=ct.FlowKind.Value,
input_sockets={'X', 'Y', 'Z', '+X', '-X', '+Y', '-Y', '+Z', '-Z'}, input_sockets={'X', 'Y', 'Z', '+X', '-X', '+Y', '-Y', '+Z', '-Z'},
input_sockets_optional={ input_sockets_optional={
'X': True, 'X': True,

View File

@ -205,8 +205,8 @@ class BoxStructureNode(base.MaxwellSimNode):
if has_center and has_size and has_medium: if has_center and has_size and has_medium:
if props['differentiable'] == ( if props['differentiable'] == (
center.is_differentiable center.is_differentiable
& size.is_differentiable and size.is_differentiable
& medium.is_differentiable and medium.is_differentiable
): ):
return center | size | medium return center | size | medium
return ct.FlowSignal.FlowPending return ct.FlowSignal.FlowPending