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

WI ensuring runnable code #50

Merged
merged 17 commits into from
Feb 24, 2022

Conversation

torbjornstoro
Copy link
Collaborator

@torbjornstoro torbjornstoro commented Feb 16, 2022

Description

Summary of changes

  • Made sure the code is runable, and produces a spectrum
  • Added averaging along line-detector, instead of using sngle point on detector line

Resolved Issues

@gomezzz gomezzz self-requested a review February 17, 2022 09:20
Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See specific comments :)

nidn/fdtd_int/FDTD_compute_spectrum.py Outdated Show resolved Hide resolved
nidn/fdtd_int/FDTD_compute_spectrum.py Outdated Show resolved Hide resolved
nidn/fdtd_int/FDTD_compute_spectrum.py Outdated Show resolved Hide resolved
nidn/fdtd_int/FDTD_compute_spectrum.py Outdated Show resolved Hide resolved
nidn/fdtd_int/FDTD_compute_spectrum.py Outdated Show resolved Hide resolved
nidn/fdtd_int/init_fdtd.py Outdated Show resolved Hide resolved
nidn/fdtd_int/init_fdtd.py Outdated Show resolved Hide resolved
nidn/fdtd_int/init_fdtd.py Outdated Show resolved Hide resolved
nidn/fdtd_int/init_fdtd.py Outdated Show resolved Hide resolved
nidn/fdtd_int/FDTD_compute_spectrum.py Outdated Show resolved Hide resolved
…ion for compute spectrum, with a parameter solver in cfg that decides whether to use rcwa or fdtd
Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see comments, note you have merge conflict atm ;)

nidn/fdtd/constants.py Outdated Show resolved Hide resolved
nidn/fdtd/constants.py Outdated Show resolved Hide resolved
nidn/fdtd/constants.py Outdated Show resolved Hide resolved
nidn/fdtd_integration/constants.py Outdated Show resolved Hide resolved
Comment on lines 54 to 73
grid = _add_object(
grid,
int(
cfg.FDTD_pml_thickness * scaling
+ cfg.FDTD_free_space_distance * scaling
+ i * scaling * cfg.FDTD_per_layer_thickness
scaling
* (
cfg.FDTD_pml_thickness
+ cfg.FDTD_free_space_distance
+ i * cfg.FDTD_per_layer_thickness
)
),
int(
cfg.FDTD_pml_thickness * scaling
+ cfg.FDTD_free_space_distance * scaling
+ (i + 1) * scaling * cfg.FDTD_per_layer_thickness
scaling
* (
cfg.FDTD_pml_thickness
+ cfg.FDTD_free_space_distance
+ (i + 1) * cfg.FDTD_per_layer_thickness
)
),
permittivity[i],
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is super hard to read and understand, please untangle a bit and explain the positions

# We want to place the detector at (...). Therefore it has to be (...)
transmission_detector_x = scaling
                    * (
                        cfg.FDTD_pml_thickness
                        + cfg.FDTD_free_space_distance
                        + i * cfg.FDTD_per_layer_thickness
                    )
(...)
grid = _add_object(grid,transmission_detector_x ,reflection_detector_x )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally same for

 grid, t_detector, r_detector = _add_detectors(
        grid,
        int(
            scaling
            * (
                cfg.FDTD_reflection_detector_x
                + cfg.N_layers * cfg.FDTD_per_layer_thickness
            )
        ),
        int(cfg.FDTD_reflection_detector_x * scaling),
    )

above

nidn/fdtd_integration/init_fdtd.py Outdated Show resolved Hide resolved
Array, Array: Reflection spectrumand transmission spectrum
"""
if cfg.solver == "FDTD":
return compute_spectrum_fdtd(eps_grid[0, 0, :, 0].real, cfg)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This can't be right. Materials can have complex permittivity? Also, the permittivity depends on frequency so you cannot throw away the last dimension. Further, can there be patterns on the grid in FDTD? If not then you always need to set Nx = 1, Ny = 1 for FDTD and this should be asserted here instead of just throwing away the other dimensions.

Ideally, do the following here:

  1. Pass full eps_grid
  2. Use appropriate eps for appropriate wl (not throwing away last dim)
  3. Check that eps_grid first two dimension have len 1 and raise an exception (unless it supports patterned layers?). For ( you can just use assert eps_grid.shape[0] == 1 and assert eps_grid.shape[1] == run_cfg.Ny ) .

As a general note: Never write code that fails silently. If you give it a patterned layer with the wrong frequencies this code will still run and you won't notice. This is a recipe for horrible bugs :)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(e.g. see also how the other compute spectrum handles this

logger.debug("Testing input for NaNs")
)

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small addendum,I think you can have patterned objects? https://github.com/flaport/fdtd#adding-an-object-to-the-grid

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also think I can, but maybe leave this out for now as I think it will take some time to implement in a smooth way?

nidn/utils/resources/default_config.toml Outdated Show resolved Hide resolved
@gomezzz
Copy link
Collaborator

gomezzz commented Feb 21, 2022

One more comment, currently I cannot import the module
image

I think you need to change a few more imports here and there

Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

some small comments, very minor changes

@@ -325,28 +326,28 @@ def update_H(self):
det.detect_H()

def reset(self):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, seeing as you haven't run black on the entire thing yet (it seems) maybe run in CLI once black . to format all the new code automatically :)

nidn/fdtd_integration/compute_spectrum_fdtd.py Outdated Show resolved Hide resolved
@@ -28,13 +26,16 @@ def compute_spectrum_fdtd(permittivity, cfg: DotMap):

# For each wavelength, calculate transmission and reflection coefficents

for w in tqdm(physical_wavelengths):
for i in tqdm(range(len(physical_wavelengths))):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this progressbar will always show. I don't think we want that when training a neural net. You can make it optional by having a variable like this

disable_progress_bar = logger._core.min_level >= 20
for i in tqdm(range(len(physical_wavelengths)),disable=disable_progress_bar ):
   (...)

then it will only show on DEBUG or TRACE (it's a bit of a hack but loguru doesn't seem to easily over the loglevel :S

Comment on lines 91 to 93
grid[0:pml_thickness, :, :] = fdtd.PML(
name="pml_xlow"
) # Add PML boundary to the left side of the grid
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
grid[0:pml_thickness, :, :] = fdtd.PML(
name="pml_xlow"
) # Add PML boundary to the left side of the grid
# Add PML boundary to the left side of the grid
grid[0:pml_thickness, :, :] = fdtd.PML(name="pml_xlow")

If you move the comment above the line it won't do the weird line breaks I think. (Also below)

UNIT_MAGNITUDE = 10 ** (-6)

MU_0: float = 4e-7 * PI
""" vacuum permeability """
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please put the comment in the line above not below :)

…bsolute to relative to the grid, added some logging and some formatting
Copy link
Collaborator

@gomezzz gomezzz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

two very minimal changes, feel to merge afterwards

@@ -1,5 +1,6 @@
from dotmap import DotMap
import fdtd
from scipy.fft import set_backend
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you use this import?

M_E = 9.1093837015 * 10 ** (-31)
# Free space permittivity
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Free space permittivity
# Vacuum permittivity

This is vacuum I think, right? (free space could also be interpreted as air maybe?)

@torbjornstoro torbjornstoro merged commit 2ffb3e9 into waveregime-internship Feb 24, 2022
@torbjornstoro torbjornstoro deleted the WI_ensuring_runnable_code branch February 24, 2022 09:52
@gomezzz gomezzz mentioned this pull request Feb 24, 2022
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants