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

Add L grid to cfg #18

Merged
merged 2 commits into from
Aug 24, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
6 changes: 6 additions & 0 deletions nidn/tests/trcwa_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ def test_single_layer():
run_cfg.N_layers = 1
run_cfg.target_frequencies = [0.5]
run_cfg.N_freq = 1
run_cfg.TRCWA_L_grid = [[1.0, 0.0], [0.0, 1.0]]
run_cfg.TRCWA_NG = 11

# Get eps shape
shape = [
Expand Down Expand Up @@ -55,6 +57,8 @@ def test_uniform_layer():
run_cfg.N_layers = 1
run_cfg.target_frequencies = [0.05, 0.1]
run_cfg.N_freq = 2
run_cfg.TRCWA_L_grid = [[1.0, 0.0], [0.0, 1.0]]
run_cfg.TRCWA_NG = 11

# Get eps shape
shape = [
Expand Down Expand Up @@ -96,6 +100,8 @@ def test_three_layer():
run_cfg.N_layers = 3
run_cfg.target_frequencies = [3.0]
run_cfg.N_freq = 1
run_cfg.TRCWA_L_grid = [[1.0, 0.0], [0.0, 1.0]]
run_cfg.TRCWA_NG = 11

# Get eps shape
shape = [
Expand Down
12 changes: 12 additions & 0 deletions nidn/training/utils/validate_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ def _validate_config(cfg: DotMap):
"use_regularization_loss",
"add_noise",
"noise_scale",
"TRCWA_L_grid",
"TRCWA_NG",
]

for key in required_keys:
Expand All @@ -50,6 +52,7 @@ def _validate_config(cfg: DotMap):
"n_neurons",
"eps_oversampling",
"seed",
"TRCWA_NG",
]
float_keys = [
"L",
Expand Down Expand Up @@ -99,6 +102,7 @@ def _validate_config(cfg: DotMap):
"iterations",
"eps_oversampling",
"noise_scale",
"TRCWA_NG",
]
for key in positive_value_keys:
if not (cfg[key] > 0):
Expand All @@ -111,3 +115,11 @@ def _validate_config(cfg: DotMap):

if cfg.type != "classification" and cfg.type != "regression":
raise ValueError(f"type must be either 'classification' or 'regression'")

if not cfg.TRCWA_L_grid[1][0] < cfg.TRCWA_L_grid[1][1]:
raise ValueError(f"TRCWA_L_grid dim1 must be ordered from low to high")
if not cfg.TRCWA_L_grid[0][0] > cfg.TRCWA_L_grid[0][1]:
raise ValueError(f"TRCWA_L_grid dim0 must be ordered from high to low")

if not all(cfg.TRCWA_L_grid) >= 0:
raise ValueError(f"TRCWA_L_grid must be positive")
2 changes: 1 addition & 1 deletion nidn/trcwa/compute_spectrum.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def compute_spectrum(eps_grid, run_cfg: DotMap):
for idx, freq in enumerate(run_cfg.target_frequencies):

# Create TRCWA Object for this frequency
trcwa = _init_trcwa(eps_grid[:, :, :, idx], freq)
trcwa = _init_trcwa(eps_grid[:, :, :, idx], freq, run_cfg)

# Compute the spectrum
reflectance, transmittance = trcwa.RT_Solve(normalize=1)
Expand Down
6 changes: 0 additions & 6 deletions nidn/trcwa/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,6 @@
# or doi.org/10.1364/OE.21.030812 for a more thorough explanation
TRCWA_Q_ABS = 1e5

# Truncation order (actual number might be smaller)
TRCWA_NG = 11

TRCWA_L1 = [0.1, 0] # everything is set relative to this
TRCWA_L2 = [0, 0.1] # it is easiest to say 1 = 1 micron

# Dielectric constant for top and bottom layer (vacuum layer)
TRCWA_VACUUM_EPS = torch.tensor(1.0)

Expand Down
11 changes: 9 additions & 2 deletions nidn/trcwa/init_trcwa.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
from .trcwa import TRCWA


def _init_trcwa(eps_grid, target_frequency):
def _init_trcwa(eps_grid, target_frequency, run_cfg):
"""Creates a TRCWA object matching the given eps_grid and target_frequency.

Args:
eps_grid (torch.tensor): Grid of epsilon values. Should be [Nx, Ny, N_layers].
target_frequency (float): Target frequency for this simulation run.
run_cfg (DotMap): Run configuration.

Returns:
TRCWA obj: The created object which is ready to compute the spectrum
Expand All @@ -25,7 +26,13 @@ def _init_trcwa(eps_grid, target_frequency):

# Initialize TRCWA object
trcwa = TRCWA(
TRCWA_NG, TRCWA_L1, TRCWA_L2, freqcmp, TRCWA_THETA, TRCWA_PHI, verbose=0,
run_cfg.TRCWA_NG,
run_cfg.TRCWA_L_grid[0],
run_cfg.TRCWA_L_grid[1],
freqcmp,
TRCWA_THETA,
TRCWA_PHI,
verbose=0,
)

# Add vacuum layer at the top
Expand Down
4 changes: 4 additions & 0 deletions nidn/utils/resources/default_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,7 @@ N_layers = 1
# Target frequencies
physical_wavelength_range = [250e-9,10e-6]
N_freq = 20

# TRCWA parameters
TRCWA_L_grid = [[0.1,0.0],[0.0,0.1]] # grid dimension for TRCWA
TRCWA_NG = 11 # Truncation order (actual number might be smaller)