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

Momchil/nonuniform #314

Merged
merged 21 commits into from
Apr 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
6dafec6
First try at reorganizing Grid
momchil-flex Mar 29, 2022
91c41b1
Parsing structures part of the nonuniform mesh generation working and…
momchil-flex Mar 31, 2022
fc18121
kwargs in simulation.plot_grid to pass to LineCollection
momchil-flex Apr 2, 2022
e46c38c
Updating structure parsing for the case with symmetry
momchil-flex Apr 4, 2022
39229f4
Changing uniform grid size to exactly span the simulation domain and …
momchil-flex Apr 6, 2022
289cd53
Nonuniform mesh generation given bounding edges and maximal step size
weiliangjin2021 Apr 10, 2022
01e9aa1
Unified mesh API for uniform, customized, and automatic mesh generation
weiliangjin2021 Apr 16, 2022
6ec8c15
Bringing back grid_size with a deprecated warning; making all tests a…
momchil-flex Apr 16, 2022
0d45d79
Making some static and some private methods regular methods in the me…
momchil-flex Apr 16, 2022
1b5e773
Moving wavelength to MeshSpec instead of AutoMeshSpec
momchil-flex Apr 17, 2022
6e953fd
Removing grid_size from most tests; Renaming UniformMeshSpec -> Unifo…
momchil-flex Apr 17, 2022
ca957bf
Reorganizing grid.py into grid/ module; taking the auto mesh function…
momchil-flex Apr 17, 2022
7c28c9b
Validating auto mesh wavelength and uniform mesh grid size; changing …
momchil-flex Apr 17, 2022
2a328be
Adding .auto and .uniform classmethods to MeshSpec; removing all grid…
momchil-flex Apr 17, 2022
d66c5c2
Fixing IO issue, bumping version, updating changelog
momchil-flex Apr 17, 2022
d186276
Replacing 'mesh' with 'grid' everywhere for uniformity with Simulatio…
momchil-flex Apr 18, 2022
ec78cb4
Erroring if grid_size supplied; various small fixes
momchil-flex Apr 19, 2022
6724f4a
Mesher class to hold all the auto grid generating functions
momchil-flex Apr 19, 2022
e61f5c0
Another batch of small changes
momchil-flex Apr 19, 2022
79a5c7d
Adding override_structures to GridSpec
momchil-flex Apr 21, 2022
ad52efd
Update shapely requirements
momchil-flex Apr 22, 2022
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
2 changes: 1 addition & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[MASTER]
extension-pkg-allow-list=pydantic
good-names=ax, im, Lx, Ly, Lz, x0, y0, z0, x, y, z, u, v, w, f, t, y1, y2, x1, x2, x3, x4, y3, y4, xs, ys, zs, Ax, Ay, Az, Nx, Ny, Nz, N, dl, rr, E, H, xx, yy, zz, dx, dy, Jx, Jy, Jz, Ex, Ey, Ez, Mx, My, Mz, Hx, Hy, Hz, dz, e, fp, dt, a, c, kx, ky, kz, k0, Jx_k, Jy_k, My_k, Mx_k, N_theta, N_phi, L_theta, L_phi, ux, uy, uz, nk, i, j, k, J, M, _N_th, _N_ph, _L_th, _L_ph, r, Et_array, Ep_array, Er_array, Ht_array, Hp_array, Hr_array, Et, Ep, Er, Ht, Hp, Hr, Ex_data, Ey_data, Ez_data, Hx_data, Hy_data, Hz_data, Ar, Atheta, Aphi, mu
good-names=ax, im, Lx, Ly, Lz, x0, y0, z0, x, y, z, u, v, w, f, t, y1, y2, x1, x2, x3, x4, y3, y4, xs, ys, zs, Ax, Ay, Az, Nx, Ny, Nz, N, dl, rr, E, H, xx, yy, zz, dx, dy, Jx, Jy, Jz, Ex, Ey, Ez, Mx, My, Mz, Hx, Hy, Hz, dz, e, fp, dt, a, c, kx, ky, kz, k0, Jx_k, Jy_k, My_k, Mx_k, N_theta, N_phi, L_theta, L_phi, ux, uy, uz, nk, i, j, k, J, M, _N_th, _N_ph, _L_th, _L_ph, r, Et_array, Ep_array, Er_array, Ht_array, Hp_array, Hr_array, Et, Ep, Er, Ht, Hp, Hr, Ex_data, Ey_data, Ez_data, Hx_data, Hy_data, Hz_data, Ar, Atheta, Aphi, mu, n, k

[BASIC]

Expand Down
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added

- New `grid_spec` Field in `Simulation` that allows more flexibility in defining the mesh.
- `GridSpec1d` class defining how the meshing along each dimension should be done, with sublcasses `UniformGrid` and `CustomGrid` that cover the functionality
previously offered by supplying a float or a list of floats to `Simulation.grid_size`. New functionality offered by `AutoGrid` subclass, with the
mesh automatically generated based on the minimum required steps per wavelength.

### Changed
- `Simulation.grid_spec` uses the default `GridSpec`, which has `AutoGrid(min_steps_per_wvl=10)` in each direction. To initialize a `Simulation` then it is no
longer needed to provide grid information, if sources are added to the simulation. Otherwise an error will be raised to provide a wavelength for the auto mesh.

### Removed
- `Simulation.grid_size` is now deprecated in favor of `Simulation.grid_spec`.

## [1.2.2] - 2022-4-16

Expand Down
2 changes: 1 addition & 1 deletion requirements/core.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ h5py>=3.0.0
rich
nlopt
matplotlib
shapely>=1.8.1
shapely<=1.8.0,>=1.7.0
descartes
pydantic>=1.9.0
PyYAML
Expand Down
2 changes: 2 additions & 0 deletions test_local.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
set -e
momchil-flex marked this conversation as resolved.
Show resolved Hide resolved

black .
python lint.py
Expand All @@ -8,5 +9,6 @@ pytest -ra tests/test_IO.py
pytest -ra tests/test_material_library.py
pytest -ra tests/test_plugins.py
pytest -ra tests/test_sidewall.py
pytest -ra tests/test_meshgenerate.py

pytest --doctest-modules tidy3d/components --ignore=tidy3d/components/base.py
1 change: 0 additions & 1 deletion tests/test_IO.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ def test_simulation_preserve_types():

sim_all = Simulation(
size=(10.0, 10.0, 10.0),
grid_size=(1, 1, 1),
structures=[
Structure(geometry=Box(size=(1, 1, 1)), medium=Medium()),
Structure(geometry=Sphere(radius=1), medium=PoleResidue(eps_inf=1, poles=[])),
Expand Down
72 changes: 23 additions & 49 deletions tests/test_components.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ def test_sim():

sim = Simulation(
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
run_time=1e-12,
structures=[
Structure(
Expand Down Expand Up @@ -71,7 +70,6 @@ def _test_version():

sim = Simulation(
size=(1, 1, 1),
grid_size=(0.1, 0.1, 0.1),
run_time=1e-12,
)
path = "tests/tmp/simulation.json"
Expand All @@ -94,7 +92,7 @@ def place_box(center_offset):
sim = Simulation(
size=(1, 1, 1),
center=CENTER_SHIFT,
grid_size=(0.1, 0.1, 0.1),
grid_spec=GridSpec(wavelength=1.0),
run_time=1e-12,
structures=[
Structure(geometry=Box(size=(1, 1, 1), center=shifted_center), medium=Medium())
Expand Down Expand Up @@ -123,20 +121,17 @@ def place_box(center_offset):
place_box(tuple(center))


def test_sim_grid_size():

size = (1, 1, 1)
_ = Simulation(size=size, grid_size=(1.0, 1.0, 1.0), run_time=1e-12)
def test_sim_size():


def _test_sim_size():
mesh1d = UniformGrid(dl=1e-5)
grid_spec = GridSpec(grid_x=mesh1d, grid_y=mesh1d, grid_z=mesh1d)

with pytest.raises(SetupError):
s = Simulation(size=(1, 1, 1), grid_size=(1e-5, 1e-5, 1e-5), run_time=1e-12)
s = Simulation(size=(1, 1, 1), grid_spec=grid_spec, run_time=1e-12)
s._validate_size()

with pytest.raises(SetupError):
s = Simulation(size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), run_time=1e-7)
s = Simulation(size=(1, 1, 1), run_time=1e-7)
s._validate_size()


Expand All @@ -145,16 +140,13 @@ def _test_monitor_size():
with pytest.raises(SetupError):
s = Simulation(
size=(1, 1, 1),
grid_size=(1e-3, 1e-3, 1e-3),
grid_spec=GridSpec.uniform(1e-3),
monitors=[
FieldMonitor(
size=(inf, inf, inf), freqs=np.linspace(0, 200e12, 10000001), name="test"
)
FieldMonitor(size=(inf, inf, inf), freqs=np.linspace(0, 200e12, 10001), name="test")
],
run_time=1e-12,
)

s.validate_contents()
s.validate_pre_upload()


@pytest.mark.parametrize("freq, log_level", [(1.5, 30), (2.5, None), (3.5, 30)])
Expand All @@ -172,7 +164,6 @@ def test_monitor_medium_frequency_range(caplog, freq, log_level):
)
sim = Simulation(
size=(1, 1, 1),
grid_size=(0.1, 0.1, 0.1),
structures=[box],
monitors=[mnt],
sources=[src],
Expand All @@ -192,26 +183,23 @@ def test_monitor_simulation_frequency_range(caplog, fwidth, log_level):
polarization="Ex",
)
mnt = FieldMonitor(size=(0, 0, 0), name="freq", freqs=[1.5])
sim = Simulation(
size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), monitors=[mnt], sources=[src], run_time=1e-12
)
sim = Simulation(size=(1, 1, 1), monitors=[mnt], sources=[src], run_time=1e-12)
assert_log_level(caplog, log_level)


@pytest.mark.parametrize("grid_size,log_level", [(0.001, None), (3, 30)])
def test_sim_grid_size(caplog, grid_size, log_level):
def test_large_grid_size(caplog, grid_size, log_level):
# small fwidth should be inside range, large one should throw warning

medium = Medium(permittivity=2, frequency_range=(2e14, 3e14))
box = Structure(geometry=Box(size=(0.1, 0.1, 0.1)), medium=medium)
src = UniformCurrentSource(
src = PointDipole(
source_time=GaussianPulse(freq0=2.5e14, fwidth=1e12),
size=(0, 0, 0),
polarization="Ex",
)
_ = Simulation(
size=(1, 1, 1),
grid_size=(0.01, 0.01, grid_size),
grid_spec=GridSpec.uniform(dl=grid_size),
structures=[box],
sources=[src],
run_time=1e-12,
Expand All @@ -232,7 +220,6 @@ def test_sim_structure_gap(caplog, box_size, log_level):
)
sim = Simulation(
size=(10, 10, 10),
grid_size=(0.1, 0.1, 0.1),
structures=[box],
sources=[src],
pml_layers=[PML(num_layers=5), PML(num_layers=5), PML(num_layers=5)],
Expand Down Expand Up @@ -262,7 +249,6 @@ def test_sim_plane_wave_error():
# with transparent box continue
_ = Simulation(
size=(1, 1, 1),
grid_size=(0.1, 0.1, 0.1),
medium=medium_bg,
structures=[box_transparent],
sources=[src],
Expand All @@ -273,7 +259,6 @@ def test_sim_plane_wave_error():
with pytest.raises(SetupError):
_ = Simulation(
size=(1, 1, 1),
grid_size=(0.1, 0.1, 0.1),
medium=medium_bg,
structures=[box_transparent, box],
sources=[src],
Expand All @@ -293,9 +278,7 @@ def test_sim_structure_extent(caplog, box_size, log_level):
polarization="Ex",
)
box = Structure(geometry=Box(size=box_size), medium=Medium(permittivity=2))
sim = Simulation(
size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=[box], sources=[src], run_time=1e-12
)
sim = Simulation(size=(1, 1, 1), structures=[box], sources=[src], run_time=1e-12)

assert_log_level(caplog, log_level)

Expand All @@ -304,21 +287,18 @@ def test_num_mediums():
"""Make sure we error if too many mediums supplied."""

structures = []
grid_spec = GridSpec.auto(wavelength=1.0)
for i in range(200):
structures.append(
Structure(geometry=Box(size=(1, 1, 1)), medium=Medium(permittivity=i + 1))
)
sim = Simulation(
size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=structures, run_time=1e-12
)
sim = Simulation(size=(1, 1, 1), grid_spec=grid_spec, structures=structures, run_time=1e-12)

with pytest.raises(SetupError):
structures.append(
Structure(geometry=Box(size=(1, 1, 1)), medium=Medium(permittivity=i + 2))
)
sim = Simulation(
size=(1, 1, 1), grid_size=(0.1, 0.1, 0.1), structures=structures, run_time=1e-12
)
sim = Simulation(size=(1, 1, 1), grid_spec=grid_spec, structures=structures, run_time=1e-12)


""" geometry """
Expand Down Expand Up @@ -359,13 +339,11 @@ def test_geometry_sizes():
with pytest.raises(pydantic.ValidationError) as e_info:
a = Box(size=size, center=(0, 0, 0))
with pytest.raises(pydantic.ValidationError) as e_info:
s = Simulation(size=size, grid_size=(1.0, 1.0, 1.0), run_time=1e-12)
with pytest.raises(pydantic.ValidationError) as e_info:
s = Simulation(size=(1, 1, 1), grid_size=size, run_time=1e-12)
s = Simulation(size=size, run_time=1e-12, grid_spec=GridSpec(wavelength=1.0))

# negative grid sizes error?
with pytest.raises(pydantic.ValidationError) as e_info:
s = Simulation(size=(1, 1, 1), grid_size=-1.0, run_time=1e-12)
s = Simulation(size=(1, 1, 1), grid_spec=GridSpec.uniform(dl=-1.0), run_time=1e-12)


def test_pop_axis():
Expand Down Expand Up @@ -577,7 +555,6 @@ def _test_names_default():

sim = Simulation(
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
run_time=1e-12,
structures=[
Structure(
Expand Down Expand Up @@ -633,7 +610,6 @@ def test_names_unique():
with pytest.raises(SetupError) as e:
sim = Simulation(
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
run_time=1e-12,
structures=[
Structure(
Expand All @@ -652,7 +628,6 @@ def test_names_unique():
with pytest.raises(SetupError) as e:
sim = Simulation(
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
run_time=1e-12,
sources=[
UniformCurrentSource(
Expand All @@ -675,7 +650,6 @@ def test_names_unique():
with pytest.raises(SetupError) as e:
sim = Simulation(
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
run_time=1e-12,
monitors=[
FluxMonitor(size=(1, 1, 0), center=(0, -0.5, 0), freqs=[1], name="mon1"),
Expand Down Expand Up @@ -822,7 +796,7 @@ def test_mode_object_syms():
sim = Simulation(
center=(1.0, -1.0, 0.5),
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
grid_spec=GridSpec.auto(wavelength=C_0 / 1.0),
run_time=1e-12,
symmetry=(1, -1, 0),
sources=[ModeSource(size=(2, 2, 0), direction="+", source_time=g)],
Expand All @@ -833,7 +807,7 @@ def test_mode_object_syms():
sim = Simulation(
center=(1.0, -1.0, 0.5),
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
grid_spec=GridSpec.auto(wavelength=C_0 / 1.0),
run_time=1e-12,
symmetry=(1, -1, 0),
monitors=[ModeMonitor(size=(2, 2, 0), name="mnt", freqs=[2], mode_spec=ModeSpec())],
Expand All @@ -843,7 +817,7 @@ def test_mode_object_syms():
sim = Simulation(
center=(1.0, -1.0, 0.5),
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
grid_spec=GridSpec.auto(wavelength=C_0 / 1.0),
run_time=1e-12,
symmetry=(1, -1, 0),
sources=[ModeSource(center=(1, -1, 1), size=(2, 2, 0), direction="+", source_time=g)],
Expand All @@ -853,7 +827,7 @@ def test_mode_object_syms():
sim = Simulation(
center=(1.0, -1.0, 0.5),
size=(2.0, 2.0, 2.0),
grid_size=(0.01, 0.01, 0.01),
grid_spec=GridSpec.auto(wavelength=C_0 / 1.0),
run_time=1e-12,
symmetry=(1, -1, 0),
monitors=[
Expand Down
Loading