Skip to content

Commit

Permalink
Add Simulation.num_computational_grid_points property
Browse files Browse the repository at this point in the history
  • Loading branch information
momchil-flex committed Aug 21, 2024
1 parent 526f04f commit 53ef995
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Added value_and_grad function to the autograd plugin, importable via `from tidy3d.plugins.autograd import value_and_grad`. Supports differentiating functions with auxiliary data (`value_and_grad(f, has_aux=True)`).

- `Simulation.num_computational_grid_points` property to examine the number of grid cells that compose the computational domain corresponding to the simulation. This can differ from `Simulation.num_cells` based on boundary conditions and symmetries.

### Fixed
- `DataArray` interpolation failure due to incorrect ordering of coordinates when interpolating with autograd tracers.
- Error in `CustomSourceTime` when evaluating at a list of times entirely outside of the range of the envelope definition times.
Expand Down
21 changes: 21 additions & 0 deletions tests/test_components/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,27 @@ def test_sim_init():
sim.epsilon(m)


def test_num_cells():
"""Test num_cells and num_computational_grid_points."""

sim = td.Simulation(
size=(1, 1, 1),
run_time=1e-12,
grid_spec=td.GridSpec.uniform(dl=0.1),
sources=[
td.PointDipole(
center=(0, 0, 0),
polarization="Ex",
source_time=td.GaussianPulse(freq0=2e14, fwidth=1e14),
)
],
)
assert sim.num_computational_grid_points > sim.num_cells # due to extra pixels at boundaries

sim = sim.updated_copy(symmetry=(1, 0, 0))
assert sim.num_computational_grid_points < sim.num_cells # due to symmetry


def test_monitors_data_size():
"""make sure a simulation can be initialized"""

Expand Down
32 changes: 31 additions & 1 deletion tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4239,7 +4239,7 @@ def grid(self) -> Grid:

@cached_property
def num_cells(self) -> int:
"""Number of cells in the simulation.
"""Number of cells in the simulation grid.
Returns
-------
Expand All @@ -4249,6 +4249,36 @@ def num_cells(self) -> int:

return np.prod(self.grid.num_cells, dtype=np.int64)

@property
def _num_computational_grid_points_dim(self):
"""Number of cells in the computational domain for this simulation along each dimension."""
num_cells = self.grid.num_cells
num_cells_comp_domain = []
# symmetry overrides other boundaries so should be checked first
for sym, npts, boundary in zip(self.symmetry, num_cells, self.boundary_spec.to_list):
if sym != 0:
num_cells_comp_domain.append(npts // 2 + 2)
elif isinstance(boundary[0], Periodic):
num_cells_comp_domain.append(npts)
else:
num_cells_comp_domain.append(npts + 2)
return num_cells_comp_domain

@property
def num_computational_grid_points(self):
"""Number of cells in the computational domain for this simulation. This is usually
different from ``num_cells`` due to the boundary conditions. Specifically, all boundary
conditions apart from ``Periodic`` require an extra pixel at the end of the simulation
domain. On the other hand, if a symmetry is present along a given dimension, only half of
the grid cells along that dimension will be in the computational domain.
Returns
-------
int
Number of yee cells in the computational domain corresponding to the simulation.
"""
return np.prod(self._num_computational_grid_points_dim, dtype=np.int64)

def get_refractive_indices(self, freq: float) -> list[float]:
"""List of refractive indices in the simulation at a given frequency."""

Expand Down

0 comments on commit 53ef995

Please sign in to comment.