Skip to content

Commit 3e22a26

Browse files
committed
Objects can now be removed from the grid (#4).
The following methods are tested to work: ``` grid.objects.pop(idx) del grid.objects[idx] del grid.object_name ```
1 parent 077a802 commit 3e22a26

File tree

5 files changed

+66
-20
lines changed

5 files changed

+66
-20
lines changed

fdtd/boundaries.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
## Boundary Conditions [base class]
20-
class _Boundary:
20+
class Boundary:
2121
""" an FDTD Boundary [base class] """
2222

2323
def __init__(self, name: str = None):
@@ -128,7 +128,7 @@ def _handle_slice(s):
128128

129129

130130
## Periodic Boundaries
131-
class PeriodicBoundary(_Boundary):
131+
class PeriodicBoundary(Boundary):
132132
""" An FDTD Periodic Boundary
133133
134134
Note:
@@ -211,7 +211,7 @@ def update_H(self):
211211
## Perfectly Matched Layer (PML)
212212

213213

214-
class PML(_Boundary):
214+
class PML(Boundary):
215215
""" A perfectly matched layer (PML)
216216
217217
a PML is an impedence-matched area at the boundary of the grid for which

fdtd/detectors.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
from .grid import Grid
1616
from .backend import backend as bd
1717

18+
## Base detector class:
19+
class Detector:
20+
""" an FDTD Detector [base class] """
21+
22+
1823
## Detector
1924
class LineDetector:
2025
""" A detector along a line in the FDTD grid """

fdtd/grid.py

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,20 @@ def curl_H(H: Tensorlike) -> Tensorlike:
7171
return curl
7272

7373

74+
class GridList(list):
75+
""" Special list that removes item from grid when deleted from list """
76+
77+
def __init__(self, grid):
78+
super().__init__([])
79+
self.grid = grid
80+
81+
def __delitem__(self, i):
82+
item = self[i]
83+
super().__delitem__(i)
84+
if hasattr(item, "name") and hasattr(self.grid, item.name):
85+
delattr(self.grid, item.name)
86+
87+
7488
## FDTD Grid Class
7589
class Grid:
7690
""" The FDTD Grid
@@ -149,16 +163,16 @@ def __init__(
149163
self.time_steps_passed = 0
150164

151165
# dictionary containing the sources:
152-
self.sources = []
166+
self.sources = GridList(self)
153167

154168
# dictionary containing the boundaries
155-
self.boundaries = []
169+
self.boundaries = GridList(self)
156170

157171
# dictionary containing the detectors
158-
self.detectors = []
172+
self.detectors = GridList(self)
159173

160174
# dictionary containing the objects in the grid
161-
self.objects = []
175+
self.objects = GridList(self)
162176

163177
def _handle_distance(self, distance: Number) -> int:
164178
""" transform a distance to an integer number of gridpoints """
@@ -361,6 +375,28 @@ def __setitem__(self, key, attr):
361375
z=self._handle_single_key(z),
362376
)
363377

378+
def __delattr__(self, name):
379+
attr = getattr(self, name)
380+
super().__delattr__(name)
381+
if not isinstance(attr, (Object, Source, Detector, Boundary)):
382+
return
383+
try:
384+
del self.objects[self.objects.index(attr)]
385+
except ValueError:
386+
pass
387+
try:
388+
del self.sources[self.sources.index(attr)]
389+
except ValueError:
390+
pass
391+
try:
392+
del self.detectors[self.detectors.index(attr)]
393+
except ValueError:
394+
pass
395+
try:
396+
del self.boundaries[self.boundaries.index(attr)]
397+
except ValueError:
398+
pass
399+
364400
def __repr__(self):
365401
return (
366402
f"{self.__class__.__name__}(shape=({self.Nx},{self.Ny},{self.Nz}), "
@@ -390,3 +426,11 @@ def __str__(self):
390426
for obj in self.objects:
391427
s += str(obj)
392428
return s
429+
430+
431+
## Imports (placed here to prevent circular imports)
432+
# relative
433+
from .objects import Object
434+
from .sources import Source
435+
from .detectors import Detector
436+
from .boundaries import Boundary

fdtd/objects.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
## Object
2424
class Object:
25-
""" An object to place in the grid """
25+
""" An object to place in the grid [base class] """
2626

2727
def __init__(self, permittivity: Tensorlike, name: str = None):
2828
""" Create an object """
@@ -63,24 +63,17 @@ def _register_grid(
6363
self._permittivity = self._permittivity[:, :, :, None]
6464
self.inverse_permittivity = (
6565
bd.ones((self.Nx, self.Ny, self.Nz, 3)) / self._permittivity
66+
- self.grid.inverse_permittivity[self.x, self.y, self.z]
6667
)
6768

6869
# set the permittivity values of the object at its border to be equal
6970
# to the grid permittivity. This way, the object is made symmetric.
7071
if self.Nx > 1:
71-
self.inverse_permittivity[-1, :, :, 0] = self.grid.inverse_permittivity[
72-
-1, self.y, self.z, 0
73-
]
72+
self.inverse_permittivity[-1, :, :, 0] = 0
7473
if self.Ny > 1:
75-
self.inverse_permittivity[:, -1, :, 1] = self.grid.inverse_permittivity[
76-
self.x, -1, self.z, 1
77-
]
74+
self.inverse_permittivity[:, -1, :, 1] = 0
7875
if self.Nz > 1:
79-
self.inverse_permittivity[:, :, -1, 2] = self.grid.inverse_permittivity[
80-
self.x, self.y, -1, 2
81-
]
82-
83-
self.grid.inverse_permittivity[self.x, self.y, self.z] = 0
76+
self.inverse_permittivity[:, :, -1, 2] = 0
8477

8578
def _handle_slice(self, s: ListOrSlice, max_index: int = None) -> slice:
8679
if isinstance(s, list):

fdtd/sources.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,13 @@
1717
from .grid import Grid
1818
from .backend import backend as bd
1919

20+
## Base source class:
21+
class Source:
22+
""" an FDTD Source [base class] """
23+
2024

2125
## LineSource class
22-
class LineSource:
26+
class LineSource(Source):
2327
""" A source along a line in the FDTD grid """
2428

2529
def __init__(

0 commit comments

Comments
 (0)