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

Enable snapping_points in grid generation #1719

Merged
merged 1 commit into from
Jun 7, 2024

Conversation

weiliangjin2021
Copy link
Collaborator

@weiliangjin2021 weiliangjin2021 commented May 24, 2024

TODO:

  • Add tests and update CHANGELOG
  • Stress tests and check edge cases

Feature Description

Enforce placing grid points at certain locations. This is helpful in improving simulation accuracy by enforcing grid points at certain geometry singularities. Around the snapping point, the maximal step size is unmodified.

Implementation:

  • Start from intervals returned by parse_structures. If any interval contains a snapping point, split the interval. The maximal grid step size will be the same at the two splitted intervals.
  • We don't split if the insertion results in very small interval. Currently I set the threshold as np.amin(max_dl_list) * 0.5, but it's an open question and worth more investigation.

Example

Here is an example comparing the grids after the insertion of snapping points:

medium = td.Medium(permittivity = 16)

box = td.Structure(
    geometry=td.Box(size=[0.1,]*3, center = (-0.12, 0, 0)), 
    medium=medium,
)

point1 = [0.123, 0.145, 0]
point2 = [-0.148, 0.1, 0]

point_outside = [1, 0.13, 0]

grid_spec=td.GridSpec.auto(
    min_steps_per_wvl=10,
    wavelength=1,
)

grid_spec_snapping = grid_spec.updated_copy(snapping_points=[point1, point2, point_outside])

sim = td.Simulation(
    size=(0.6,) * 3,
    grid_spec=grid_spec,
    run_time=1e-13,
    structures=[box],
    boundary_spec = td.BoundarySpec.all_sides(boundary=td.PECBoundary()),
)
sim_snapping = sim.updated_copy(grid_spec = grid_spec_snapping)

fig, ax = plt.subplots(1, 2, figsize=(8, 5), tight_layout=True)
sim.plot(z=0, ax=ax[0])
sim.plot_grid(z=0, ax=ax[0], colors='red', linewidth=1)

sim_snapping.plot(z=0, ax=ax[1])
sim_snapping.plot_grid(z=0, ax=ax[1], colors='red', linewidth=1)

image

@weiliangjin2021 weiliangjin2021 marked this pull request as draft May 24, 2024 20:37
@weiliangjin2021 weiliangjin2021 marked this pull request as ready for review May 24, 2024 21:51
@weiliangjin2021 weiliangjin2021 added the 2.7 will go into version 2.7.* label May 24, 2024
Copy link
Collaborator

@tylerflex tylerflex left a comment

Choose a reason for hiding this comment

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

Thanks @weiliangjin2021 see my comments.

tests/test_components/test_grid_spec.py Outdated Show resolved Hide resolved
tidy3d/components/grid/grid_spec.py Outdated Show resolved Hide resolved
tidy3d/components/geometry/primitives.py Outdated Show resolved Hide resolved
tidy3d/components/grid/grid_spec.py Outdated Show resolved Hide resolved
Copy link
Contributor

@QimingFlex QimingFlex left a comment

Choose a reason for hiding this comment

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

Thanks, @weiliangjin2021 ! Finally, the snapping points should replace the diagonally placed meshoverride boxes to handle edges. Regarding the open question about the threshold, from my understanding, it is currently determined by the largest allowable step for each structure interval. Adding two more parameters—one for the smallest allowable step and another for the enforced step at the point—might be useful. Perhaps these two parameters could be combined into one.

@weiliangjin2021
Copy link
Collaborator Author

it is currently determined by the largest allowable step for each structure interval.

Not really. Along a direction, the simulation domain is divided into multiple intervals. Each interval has its own maximal allowed step size. The threshold value is half of the minimal of those steps sizes.

Adding two more parameters—one for the smallest allowable step and another for the enforced step at the point

Could you explain a bit more?

@QimingFlex
Copy link
Contributor

I see. I misunderstood it as the smallest allowed step. So currently, the minimum step is given by 'min_steps_per_wvl' and the largest time step is given by these intervals. Is this correct?

@weiliangjin2021
Copy link
Collaborator Author

I see. I misunderstood it as the smallest allowed step. So currently, the minimum step is given by 'min_steps_per_wvl' and the largest time step is given by these intervals. Is this correct?

Is there a typo in your sentence (how does time step enter here)? 'min_steps_per_wvl' determines the maximal allowed step size in each interval.

@QimingFlex
Copy link
Contributor

QimingFlex commented May 29, 2024 via email

@weiliangjin2021 weiliangjin2021 changed the title Enable fixpoints in grid generation Enable snapping_points in grid generation May 29, 2024
@weiliangjin2021
Copy link
Collaborator Author

Sorry it is a typo, ‘space’ step.

Well, then I still don't get your question. I guess it should be clear that the threshold is np.amin(max_dl_list) * 0.5?

@QimingFlex
Copy link
Contributor

Sorry it is a typo, ‘space’ step.

Well, then I still don't get your question. I guess it should be clear that the threshold is np.amin(max_dl_list) * 0.5?

I tested with several examples and understand the threshold. The parameter looks good to me!

@momchil-flex momchil-flex changed the base branch from pre/2.7 to develop June 7, 2024 14:55
Copy link
Collaborator

@momchil-flex momchil-flex left a comment

Choose a reason for hiding this comment

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

Looks good, main comment from usability perspective is that in some cases you might want to set the point along e.g. one of the axes only, but I think it's fine, we don't need to complicate things so much for now.

tidy3d/components/grid/mesher.py Show resolved Hide resolved
@weiliangjin2021 weiliangjin2021 merged commit 0571af5 into develop Jun 7, 2024
16 checks passed
@weiliangjin2021 weiliangjin2021 deleted the weiliang/fixed_point branch June 7, 2024 17:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
2.7 will go into version 2.7.*
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants