Skip to content

Commit

Permalink
source reorg
Browse files Browse the repository at this point in the history
  • Loading branch information
dbochkov-flexcompute committed Dec 9, 2024
1 parent 163804e commit c242868
Show file tree
Hide file tree
Showing 27 changed files with 798 additions and 722 deletions.
3 changes: 2 additions & 1 deletion tests/_test_local/_test_data_performance.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
from tidy3d.components.grid.grid_spec import GridSpec
from tidy3d.components.monitor import FieldMonitor
from tidy3d.components.simulation import Simulation
from tidy3d.components.source import GaussianPulse, PointDipole
from tidy3d.components.source.current import PointDipole
from tidy3d.components.source.time import GaussianPulse

sys.path.append("/users/twhughes/Documents/Flexcompute/tidy3d-core")
from tidy3d_backend.utils import Profile
Expand Down
4 changes: 3 additions & 1 deletion tests/test_components/test_boundaries.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
PMCBoundary,
StablePML,
)
from tidy3d.components.source import GaussianPulse, PlaneWave, PointDipole
from tidy3d.components.source.current import PointDipole
from tidy3d.components.source.field import PlaneWave
from tidy3d.components.source.time import GaussianPulse
from tidy3d.exceptions import DataError, SetupError

from ..utils import assert_log_level
Expand Down
4 changes: 2 additions & 2 deletions tests/test_components/test_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pydantic.v1 as pydantic
import pytest
import tidy3d as td
from tidy3d.components.source import CHEB_GRID_WIDTH, DirectionalSource
from tidy3d.components.source.field import CHEB_GRID_WIDTH, DirectionalSource
from tidy3d.exceptions import SetupError

from ..utils import AssertLogLevel, assert_log_level
Expand Down Expand Up @@ -79,7 +79,7 @@ def test_source_times():
# plt.close()

# test we can make cw pulse
from tidy3d.components.source import ContinuousWave
from tidy3d.components.source.time import ContinuousWave

c = ContinuousWave(freq0=1e12, fwidth=0.1e12)
c.amp_time(ts)
Expand Down
3 changes: 2 additions & 1 deletion tests/test_web/test_webapi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
from tidy3d.components.data.sim_data import SimulationData
from tidy3d.components.grid.grid_spec import GridSpec
from tidy3d.components.monitor import FieldMonitor
from tidy3d.components.source import GaussianPulse, PointDipole
from tidy3d.components.source.current import PointDipole
from tidy3d.components.source.time import GaussianPulse
from tidy3d.exceptions import SetupError
from tidy3d.web.api.asynchronous import run_async
from tidy3d.web.api.container import Batch, Job
Expand Down
24 changes: 14 additions & 10 deletions tidy3d/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,29 @@

# simulation
from .components.simulation import Simulation

# sources
from .components.source import (
from .components.source.base import Source
from .components.source.current import (
CustomCurrentSource,
PointDipole,
UniformCurrentSource,
)
from .components.source.field import (
TFSF,
AstigmaticGaussianBeam,
ContinuousWave,
CustomCurrentSource,
CustomFieldSource,
CustomSourceTime,
FixedAngleSpec,
FixedInPlaneKSpec,
GaussianBeam,
GaussianPulse,
ModeSource,
PlaneWave,
PointDipole,
Source,
)

# sources
from .components.source.time import (
ContinuousWave,
CustomSourceTime,
GaussianPulse,
SourceTime,
UniformCurrentSource,
)

# structures
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/components/boundary.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..log import log
from .base import Tidy3dBaseModel, cached_property
from .medium import Medium
from .source import TFSF, GaussianBeam, ModeSource, PlaneWave
from .source.field import TFSF, GaussianBeam, ModeSource, PlaneWave
from .types import TYPE_TAG_STR, Axis, Complex

MIN_NUM_PML_LAYERS = 6
Expand Down
12 changes: 8 additions & 4 deletions tidy3d/components/data/monitor_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,18 @@
MonitorType,
PermittivityMonitor,
)
from ..source import (
from ..source.base import Source
from ..source.current import (
CustomCurrentSource,
PointDipole,
)
from ..source.field import (
CustomFieldSource,
GaussianPulse,
ModeSource,
PlaneWave,
PointDipole,
Source,
)
from ..source.time import (
GaussianPulse,
SourceTimeType,
)
from ..types import (
Expand Down
3 changes: 2 additions & 1 deletion tidy3d/components/data/sim_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
from ..file_util import replace_values
from ..monitor import Monitor
from ..simulation import Simulation
from ..source import GaussianPulse, SourceType
from ..source.time import GaussianPulse
from ..source.utils import SourceType
from ..structure import Structure
from ..types import Ax, Axis, ColormapType, FieldVal, PlotScale, annotate_type
from ..viz import add_ax_if_none, equal_aspect
Expand Down
3 changes: 2 additions & 1 deletion tidy3d/components/eme/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
from ..monitor import AbstractModeMonitor, ModeSolverMonitor, Monitor, MonitorType
from ..scene import Scene
from ..simulation import AbstractYeeGridSimulation, Simulation
from ..source import GaussianPulse, PointDipole
from ..source.current import PointDipole
from ..source.time import GaussianPulse
from ..structure import Structure
from ..types import Ax, Axis, FreqArray, Symmetry, annotate_type
from ..validators import MIN_FREQUENCY, validate_freqs_min, validate_freqs_not_empty
Expand Down
2 changes: 1 addition & 1 deletion tidy3d/components/grid/grid_spec.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ...log import log
from ..base import Tidy3dBaseModel
from ..geometry.base import Box
from ..source import SourceType
from ..source.utils import SourceType
from ..structure import Structure, StructureType
from ..types import TYPE_TAG_STR, Axis, Coordinate, Symmetry, annotate_type
from .grid import Coords, Coords1D, Grid
Expand Down
11 changes: 5 additions & 6 deletions tidy3d/components/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,19 @@
)
from .run_time_spec import RunTimeSpec
from .scene import MAX_NUM_MEDIUMS, Scene
from .source import (
from .source.base import Source
from .source.current import CustomCurrentSource
from .source.field import (
TFSF,
AstigmaticGaussianBeam,
ContinuousWave,
CustomCurrentSource,
CustomFieldSource,
CustomSourceTime,
FixedAngleSpec,
GaussianBeam,
ModeSource,
PlaneWave,
Source,
SourceType,
)
from .source.time import ContinuousWave, CustomSourceTime
from .source.utils import SourceType
from .structure import MeshOverrideStructure, Structure
from .subpixel_spec import SubpixelSpec
from .types import TYPE_TAG_STR, Ax, Axis, FreqBound, InterpMethod, Literal, Symmetry, annotate_type
Expand Down
Empty file.
120 changes: 120 additions & 0 deletions tidy3d/components/source/base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
"""Defines an abstract base for electromagnetic sources."""

from __future__ import annotations

from abc import ABC
from typing import Tuple

import pydantic.v1 as pydantic

from ..base import cached_property
from ..base_sim.source import AbstractSource
from ..geometry.base import Box
from ..types import TYPE_TAG_STR, Ax
from ..validators import _assert_min_freq
from ..viz import (
ARROW_ALPHA,
ARROW_COLOR_POLARIZATION,
ARROW_COLOR_SOURCE,
PlotParams,
plot_params_source,
)
from .time import SourceTimeType


class Source(Box, AbstractSource, ABC):
"""Abstract base class for all sources."""

source_time: SourceTimeType = pydantic.Field(
...,
title="Source Time",
description="Specification of the source time-dependence.",
discriminator=TYPE_TAG_STR,
)

@cached_property
def plot_params(self) -> PlotParams:
"""Default parameters for plotting a Source object."""
return plot_params_source

@cached_property
def geometry(self) -> Box:
""":class:`Box` representation of source."""

return Box(center=self.center, size=self.size)

@cached_property
def _injection_axis(self):
"""Injection axis of the source."""
return None

@cached_property
def _dir_vector(self) -> Tuple[float, float, float]:
"""Returns a vector indicating the source direction for arrow plotting, if not None."""
return None

@cached_property
def _pol_vector(self) -> Tuple[float, float, float]:
"""Returns a vector indicating the source polarization for arrow plotting, if not None."""
return None

@pydantic.validator("source_time", always=True)
def _freqs_lower_bound(cls, val):
"""Raise validation error if central frequency is too low."""
_assert_min_freq(val.freq0, msg_start="'source_time.freq0'")
return val

def plot(
self,
x: float = None,
y: float = None,
z: float = None,
ax: Ax = None,
**patch_kwargs,
) -> Ax:
"""Plot this source."""

kwargs_arrow_base = patch_kwargs.pop("arrow_base", None)

# call the `Source.plot()` function first.
ax = Box.plot(self, x=x, y=y, z=z, ax=ax, **patch_kwargs)

kwargs_alpha = patch_kwargs.get("alpha")
arrow_alpha = ARROW_ALPHA if kwargs_alpha is None else kwargs_alpha

# then add the arrow based on the propagation direction
if self._dir_vector is not None:
bend_radius = None
bend_axis = None
if hasattr(self, "mode_spec"):
bend_radius = self.mode_spec.bend_radius
bend_axis = self._bend_axis

ax = self._plot_arrow(
x=x,
y=y,
z=z,
ax=ax,
direction=self._dir_vector,
bend_radius=bend_radius,
bend_axis=bend_axis,
color=ARROW_COLOR_SOURCE,
alpha=arrow_alpha,
both_dirs=False,
arrow_base=kwargs_arrow_base,
)

if self._pol_vector is not None:
ax = self._plot_arrow(
x=x,
y=y,
z=z,
ax=ax,
direction=self._pol_vector,
color=ARROW_COLOR_POLARIZATION,
alpha=arrow_alpha,
both_dirs=False,
arrow_base=kwargs_arrow_base,
)

return ax
Loading

0 comments on commit c242868

Please sign in to comment.