-
Notifications
You must be signed in to change notification settings - Fork 67
fix(mode): fixed bug where mode solver was not respecting location of PEC #3030
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
base: develop
Are you sure you want to change the base?
fix(mode): fixed bug where mode solver was not respecting location of PEC #3030
Conversation
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.
3 files reviewed, 1 comment
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/mode/mode_solver.pyLines 1666-1674 1666 ) -> tuple[list[float], list[dict[str, ArrayComplex4D]], list[EpsSpecType]]:
1667 """Call the mode solver at all requested frequencies."""
1668 if tidy3d_extras["use_local_subpixel"]:
1669 subpixel_ms = tidy3d_extras["mod"].SubpixelModeSolver.from_mode_solver(self)
! 1670 return subpixel_ms._solve_all_freqs(
1671 coords=coords,
1672 symmetry=symmetry,
1673 )tidy3d/components/mode/solver.pyLines 163-175 163 eps_cross = cls._truncate_medium_data(eps_cross, cell_min, cell_max)
164
165 # Truncate mu_cross if provided
166 if mu_cross is not None:
! 167 mu_cross = cls._truncate_medium_data(mu_cross, cell_min, cell_max)
168
169 # Truncate split_curl_scaling if provided
170 if split_curl_scaling is not None:
! 171 split_curl_scaling = tuple(
172 s[cell_min[0] : cell_max[0], cell_min[1] : cell_max[1]]
173 for s in split_curl_scaling
174 )Lines 1166-1178 1166
1167 if isinstance(mat_data, Numpy):
1168 # Tensorial format: shape (9, Nx, Ny)
1169 return mat_data[:, x_slice, y_slice]
! 1170 if len(mat_data) == 9:
1171 # Nine separate 2D arrays
! 1172 return tuple(arr[x_slice, y_slice] for arr in mat_data)
1173
! 1174 raise ValueError("Wrong input to mode solver permittivity/permeability truncation!")
1175
1176 @staticmethod
1177 def format_medium_data(mat_data):
1178 """ |
f179f91 to
6bb0d8f
Compare
|
@greptileai another round |
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.
4 files reviewed, 1 comment
d1ada2b to
3df4c17
Compare
I think it's quite common to have mode plane smaller than the simulation domain, and it'll be very useful to fix that too? Regarding closest grid boundary, i guess it should work in most cases, as it's expected to have the mode plane boundary to lie at the metal surface, and metal surface is snapped to grid. |
It's not really an issue though if the mode plane is smaller than the simulation domain. All that will happen is an extra cell will be added that is outside the monitor bounds. But since in the simulation there is no PEC boundary at the monitor bounds anyways, it is an accurate representation. A structure just outside/touching the monitor bounds will still be captured by the mode solver, and is the current workaround for this bug. |
caseyflex
left a comment
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.
I think the warnings make sense if the boundary won’t be used.
the only unclear case is PML, which you don’t earn for just because it would be annoying. I guess that can be a separate discussion about unifying PML treatment
3df4c17 to
4384ea8
Compare
c6e2d73 to
a436028
Compare
… PEC boundary conditions from the Simulation
a436028 to
5a716d5
Compare
Greptile Overview
Greptile Summary
This PR fixes a bug where the mode solver grid would extend beyond simulation domain boundaries, causing incorrect PEC boundary condition application. The fix implements grid truncation at simulation boundaries and zero-pads fields outside the domain.
Key changes:
_sim_boundary_positionsmethod to detect PEC boundaries and determine where to truncate the computational gridcompute_modesthat clips coordinates, permittivity, permeability, and basis fields to simulation boundsThe implementation correctly handles the core issue, but there's a minor inconsistency in the boundary intersection detection logic that could cause edge cases.
Confidence Score: 4/5
np.isclosevsfp_epsthreshold) that could cause edge case failures. The core truncation and zero-padding logic appears soundtidy3d/components/mode/mode_solver.pyfor the boundary intersection logic inconsistencyImportant Files Changed
File Analysis
_sim_boundary_positionsmethod to detect PEC boundaries and determine truncation positions. Passes boundary positions to solver for grid truncation. Minor inconsistency in boundary checking logic_truncate_medium_datahelper methodSequence Diagram
sequenceDiagram participant MS as ModeSolver participant MSC as ModeSolver._solve_single_freq participant SBP as ModeSolver._sim_boundary_positions participant CM as compute_modes (solver.py) participant TMD as _truncate_medium_data MS->>MS: User calls .data property MS->>MSC: _solve_single_freq(freq, coords, symmetry) MSC->>SBP: Get PEC boundary positions SBP->>SBP: Check boundary types (PEC, PMC, etc.) SBP->>SBP: Determine if solver grid intersects boundaries SBP-->>MSC: Return (pos_min, pos_max) tuples MSC->>CM: compute_modes(eps_cross, coords, ..., sim_pec_pos_min, sim_pec_pos_max) CM->>CM: Calculate truncation indices from PEC positions CM->>CM: Check if truncation needed (trim_min != [0,0] or trim_max != [Nx,Ny]) alt Truncation needed CM->>CM: Truncate coords arrays CM->>TMD: Truncate eps_cross TMD-->>CM: Truncated eps CM->>TMD: Truncate mu_cross (if present) TMD-->>CM: Truncated mu CM->>CM: Truncate split_curl_scaling (if present) CM->>CM: Truncate solver_basis_fields (if present) end CM->>CM: Solve eigenvalue problem on truncated grid CM->>CM: Reshape fields to (2, 3, Nx_trunc, Ny_trunc, 1, num_modes) alt Truncation was applied CM->>CM: Zero-pad fields back to original size Note over CM: np.pad with trim_min/trim_max offsets end CM-->>MSC: Return (n_complex, fields, eps_spec) MSC-->>MS: Return mode data with correct boundary conditions