-
Notifications
You must be signed in to change notification settings - Fork 6
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 add tests #54
WI add tests #54
Changes from all commits
8af9c09
6aab3c0
4f86c7e
1fa05ce
3b4cc39
92875b6
c6b659a
53e851a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
from dotmap import DotMap | ||
import fdtd | ||
|
||
from ..fdtd import ( | ||
AbsorbingObject, | ||
set_backend, | ||
Grid, | ||
PML, | ||
PeriodicBoundary, | ||
LineSource, | ||
LineDetector, | ||
PointSource, | ||
) | ||
from ..utils.global_constants import EPS_0, SPEED_OF_LIGHT, UNIT_MAGNITUDE | ||
from .constants import FDTD_GRID_SCALE | ||
|
||
|
@@ -17,13 +26,13 @@ def init_fdtd(cfg: DotMap, include_object, wavelength, permittivity): | |
Returns: | ||
fdtd:Grid: Grid with all the added object, ready to be run | ||
""" | ||
fdtd.set_backend("torch") | ||
set_backend("torch") | ||
scaling = UNIT_MAGNITUDE / (cfg.physical_wavelength_range[0] * FDTD_GRID_SCALE) | ||
x_grid_size = int( | ||
scaling * (cfg.FDTD_grid[0] + cfg.N_layers * cfg.PER_LAYER_THICKNESS[0]) | ||
) | ||
y_grid_size = int(cfg.FDTD_grid[1] * scaling) | ||
grid = fdtd.Grid( | ||
grid = Grid( | ||
(x_grid_size, y_grid_size, 1), | ||
grid_spacing=cfg.physical_wavelength_range[0] * FDTD_GRID_SCALE, | ||
permittivity=1.0, | ||
|
@@ -38,16 +47,20 @@ def init_fdtd(cfg: DotMap, include_object, wavelength, permittivity): | |
cfg.FDTD_reflection_detector_x | ||
+ cfg.N_layers * cfg.PER_LAYER_THICKNESS[0] | ||
) | ||
+ cfg.FDTD_min_gridpoints_between_detectors | ||
), | ||
int(cfg.FDTD_reflection_detector_x * scaling), | ||
) | ||
assert cfg.FDTD_pulse_type in ["pulse", "continuous"] | ||
use_pulse = cfg.FDTD_pulse_type == "pulse" | ||
|
||
grid = _add_source( | ||
grid, | ||
int(cfg.FDTD_source[0] * scaling), | ||
int(cfg.FDTD_source[1] * scaling), | ||
int(cfg.FDTD_source_position[0] * scaling), | ||
int(cfg.FDTD_source_position[1] * scaling), | ||
wavelength / SPEED_OF_LIGHT, | ||
cfg.FDTD_use_pulsesource, | ||
cfg.FDTD_use_pointsource, | ||
use_pulse, | ||
cfg.FDTD_source_type, | ||
) | ||
if include_object: | ||
for i in range(cfg.N_layers): | ||
|
@@ -88,17 +101,17 @@ def _add_boundaries(grid, pml_thickness): | |
fdtd.Grid: The grid with the added boundaries | ||
""" | ||
# Add PML boundary to the left side of the grid | ||
grid[0:pml_thickness, :, :] = fdtd.PML(name="pml_xlow") | ||
grid[0:pml_thickness, :, :] = PML(name="pml_xlow") | ||
# Add PML boundary to the right side of the grid | ||
grid[-pml_thickness:, :, :] = fdtd.PML(name="pml_xhigh") | ||
grid[-pml_thickness:, :, :] = PML(name="pml_xhigh") | ||
# Add periodic boundaries at both sides in y-direction | ||
grid[:, 0, :] = fdtd.PeriodicBoundary(name="ybounds") | ||
grid[:, 0, :] = PeriodicBoundary(name="ybounds") | ||
# Add periodic boundaries on both sides in z-direction. Only applicable for 3D grids | ||
# grid[:, :, 0] = fdtd.PeriodicBoundary(name="zbounds") | ||
return grid | ||
|
||
|
||
def _add_source(grid, source_x, source_y, period, use_pulse_source, use_point_source): | ||
def _add_source(grid, source_x, source_y, period, use_pulse_source, source_type): | ||
"""Add a specified source to the fdtd grid | ||
|
||
Args: | ||
|
@@ -113,17 +126,18 @@ def _add_source(grid, source_x, source_y, period, use_pulse_source, use_point_so | |
fdtd.Grid: The grid with the added source | ||
""" | ||
|
||
if use_point_source: | ||
grid[source_x, source_y, 0] = fdtd.PointSource( | ||
if source_type == "point": | ||
grid[source_x, source_y, 0] = PointSource( | ||
period=period, | ||
name="linesource", | ||
name="pointsource", | ||
pulse=use_pulse_source, | ||
cycle=1, | ||
hanning_dt=1e-15, | ||
) | ||
elif source_type == "line": | ||
grid[source_x, :, 0] = LineSource(period=period, name="linesource") | ||
else: | ||
grid[source_x, :, 0] = fdtd.LineSource(period=period, name="linesource") | ||
|
||
raise ValueError(f'FDTD_source_type must be either "line" or "point"') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. potentially as above with assert may be easier There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. but this is also fine |
||
return grid | ||
|
||
|
||
|
@@ -139,8 +153,8 @@ def _add_detectors(grid, transmission_detector_x, reflection_detector_x): | |
fdtd.Grid: The grid with the added detectors | ||
""" | ||
|
||
transmission_detector = fdtd.LineDetector(name="t_detector") | ||
reflection_detector = fdtd.LineDetector(name="r_detector") | ||
transmission_detector = LineDetector(name="t_detector") | ||
reflection_detector = LineDetector(name="r_detector") | ||
grid[transmission_detector_x, :, 0] = transmission_detector | ||
grid[reflection_detector_x, :, 0] = reflection_detector | ||
return grid, transmission_detector, reflection_detector | ||
|
@@ -158,9 +172,9 @@ def _add_object(grid, object_start_x, object_end_x, permittivity, frequency): | |
Returns: | ||
fdtd.Grid: The grid with the added object | ||
""" | ||
# Not sure whether the conductivity should be relative or absolute, i.e. if it should be multiplied with EPS_0. | ||
# Not sure whether the conductivity should be relative or absolute, i.e. if it should be multiplied with EPS_0. Multiplied with 2pi to get w(angular frequency)? | ||
# Since the permittivity is set to 1 for the free space grid, I'll leave it at an relative value for now. Also, permittivity for object is relative. | ||
grid[object_start_x:object_end_x, :, :] = fdtd.AbsorbingObject( | ||
grid[object_start_x:object_end_x, :, :] = AbsorbingObject( | ||
permittivity=permittivity.real, | ||
conductivity=permittivity.imag * frequency, | ||
) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import numpy as np | ||
from ..fdtd_integration.calculate_transmission_reflection_coefficients import ( | ||
calculate_transmission_reflection_coefficients, | ||
) | ||
from ..utils.load_default_cfg import load_default_cfg | ||
|
||
|
||
def test_calculate_coefficient(): | ||
cfg = load_default_cfg() | ||
time_points = [0.002 * np.pi * i for i in range(1000)] | ||
signal_a = 2 * np.sin(time_points) | ||
signal_b = np.sin(time_points) | ||
signal_array = [signal_a, signal_b] | ||
( | ||
transmission_coefficient_ms, | ||
reflection_coefficient_ms, | ||
) = calculate_transmission_reflection_coefficients( | ||
signal_array, signal_array, "mean square", cfg | ||
) | ||
# TODO: Add test for fft, when the method is complete | ||
assert transmission_coefficient_ms - 0.25 == 0 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for completeness also check reflection_coefficient. Would it make sense to check here also if they are all >= 0 and <= 1.0? ( We ought to have some way to deal with this in the code as well btw 🤔 But maybe can happen when you look into the FFT? (Does that have an issue btw? Then could add a todo there for checking that spectrum values are between 0 and 1 and throwing at least a warning if they aren't.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you think this should be done in the test, or in the compute_spectrum_fdtd function? (The check that all values are betweenn 0 and 1) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would still assert it here that they are all between 0 and 1. Doesn't hurt and serves as a sanity check if the check inside the codebase is accidentally circumvented |
||
assert reflection_coefficient_ms - 0.25 == 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
test_calculate_coefficient() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if it was not a torch tensor before then you already have lost the gradients somewhere along the way :) You can leave it for now, just pointing out