Skip to content

Commit

Permalink
Replace complex fields implementation in certain nonlinearities
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyflex committed Feb 18, 2025
1 parent 00dd7c2 commit 35edc0b
Show file tree
Hide file tree
Showing 5 changed files with 229 additions and 108 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Renamed `SkinDepthFitterParam` --> `SurfaceImpedanceFitterParam` used in `LossyMetalMedium`. `plot` in `LossyMetalMedium` now plots complex-valued surface impedance.
- Changed plot_3d iframe url to tidy3d production environment.
- `num_freqs` is now set to 3 by default for the `PlaneWave`, `GaussianBeam`, and `AnalyticGaussianBeam` sources, which makes the injection more accurate in broadband cases.
- Nonlinear models `KerrNonlinearity` and `TwoPhotonAbsorption` now default to using the physical real fields instead of complex fields.

### Fixed
- Make gauge selection for non-converged modes more robust.
Expand Down
86 changes: 64 additions & 22 deletions tests/test_components/test_medium.py
Original file line number Diff line number Diff line change
Expand Up @@ -591,6 +591,18 @@ def test_nonlinear_medium():
)
)

assert med._nonlinear_num_iters == 20
assert td.Medium()._nonlinear_num_iters == 0
assert td.Medium(nonlinear_spec=td.NonlinearSusceptibility(chi3=1.5))._nonlinear_num_iters == 1
assert (
td.Medium(
nonlinear_spec=td.NonlinearSusceptibility(chi3=1.5, numiters=2)
)._nonlinear_num_iters
== 2
)
assert td.Medium()._nonlinear_models == []
assert td.Medium(nonlinear_spec=td.NonlinearSpec())._nonlinear_models == []

# warn about deprecated api
with AssertLogLevel("WARNING"):
med = td.Medium(nonlinear_spec=td.NonlinearSusceptibility(chi3=1.5))
Expand Down Expand Up @@ -634,11 +646,15 @@ def test_nonlinear_medium():
# active materials
with pytest.raises(ValidationError):
med = td.Medium(
nonlinear_spec=td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=-1, n0=1)])
nonlinear_spec=td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=-1, n0=1, freq0=1)])
)

with pytest.raises(ValidationError):
med = td.Medium(nonlinear_spec=td.NonlinearSpec(models=[td.KerrNonlinearity(n2=-1j, n0=1)]))
med = td.Medium(
nonlinear_spec=td.NonlinearSpec(
models=[td.KerrNonlinearity(n2=-1j, n0=1, use_complex_fields=True)]
)
)

# automatic detection of n0 and freq0
n0 = 2
Expand All @@ -662,26 +678,6 @@ def test_nonlinear_medium():
assert n0 == nonlinear_spec.models[0]._get_n0(n0=None, medium=medium, freqs=[freq0])
assert freq0 == nonlinear_spec.models[0]._get_freq0(freq0=None, freqs=[freq0])

# two photon absorption is phenomenological
with AssertLogLevel("WARNING", contains_str="phenomenological"):
med = td.Medium(
nonlinear_spec=td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=-1, n0=1)]),
allow_gain=True,
)
sim.updated_copy(medium=med, path="structures/0")

# complex parameters
with AssertLogLevel("WARNING", contains_str="preferred"):
med = td.Medium(
nonlinear_spec=td.NonlinearSpec(
models=[
td.KerrNonlinearity(n2=-1 + 1j, n0=1),
],
num_iters=20,
)
)
sim.updated_copy(medium=med, path="structures/0")

# subsection with nonlinear materials needs to hardcode source info
sim2 = sim.updated_copy(center=(-4, -4, -4), path="sources/0")
sim2 = sim2.updated_copy(
Expand All @@ -696,13 +692,27 @@ def test_nonlinear_medium():
source2 = source.updated_copy(source_time=source_time2)
with pytest.raises(SetupError):
sim.updated_copy(sources=[source, source2])
with pytest.raises(SetupError):
sim.updated_copy(sources=[])

# but if we provided it, it's ok
nonlinear_spec = td.NonlinearSpec(models=[td.KerrNonlinearity(n2=1, n0=1)])
structure = structure.updated_copy(medium=medium.updated_copy(nonlinear_spec=nonlinear_spec))
sim = sim.updated_copy(structures=[structure])
assert 1 == nonlinear_spec.models[0]._get_n0(n0=1, medium=medium, freqs=[1, 2])

nonlinear_spec = td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=1, n0=1)])
structure = structure.updated_copy(medium=medium.updated_copy(nonlinear_spec=nonlinear_spec))
sim = sim.updated_copy(structures=[structure])
with pytest.raises(SetupError):
sim = sim.updated_copy(structures=[structure], sources=[source, source2])
with pytest.raises(SetupError):
sim = sim.updated_copy(structures=[structure], sources=[])
nonlinear_spec = td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=1, n0=1, freq0=1)])
structure = structure.updated_copy(medium=medium.updated_copy(nonlinear_spec=nonlinear_spec))
sim = sim.updated_copy(structures=[structure])
assert 1 == nonlinear_spec.models[0]._get_freq0(freq0=1, freqs=[1, 2])

# active materials with automatic detection of n0
nonlinear_spec_active = td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=-1)])
medium_active = medium.updated_copy(nonlinear_spec=nonlinear_spec_active)
Expand All @@ -727,6 +737,38 @@ def test_nonlinear_medium():
with pytest.raises(ValidationError):
td.Medium2D(ss=modulated, tt=modulated)

# some parameters must be real now, unless we use old implementation
_ = td.TwoPhotonAbsorption(beta=1j, use_complex_fields=True)
with pytest.raises(pydantic.ValidationError):
_ = td.TwoPhotonAbsorption(beta=1j)
_ = td.KerrNonlinearity(n2=1j, use_complex_fields=True)
with pytest.raises(pydantic.ValidationError):
_ = td.KerrNonlinearity(n2=1j)

# consistent complex fields
_ = td.NonlinearSpec(
models=[
td.TwoPhotonAbsorption(beta=1, use_complex_fields=True),
td.KerrNonlinearity(n2=1, use_complex_fields=True),
]
)
with pytest.raises(pydantic.ValidationError):
_ = td.NonlinearSpec(
models=[
td.TwoPhotonAbsorption(beta=1, use_complex_fields=True),
td.KerrNonlinearity(n2=1, use_complex_fields=False),
]
)

# warn if using old implementation
with AssertLogLevel("WARNING", contains_str="use_complex_fields"):
med = td.Medium(
nonlinear_spec=td.NonlinearSpec(
models=[td.KerrNonlinearity(n2=1, use_complex_fields=True)]
)
)
_ = sim.updated_copy(medium=med, path="structures/0")


def test_custom_medium():
Nx, Ny, Nz, Nf = 4, 3, 1, 1
Expand Down
2 changes: 1 addition & 1 deletion tests/test_components/test_simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -3073,7 +3073,7 @@ def test_fixed_angle_sim():
permittivity=3,
nonlinear_spec=td.NonlinearSpec(
models=[
td.KerrNonlinearity(n2=-1 + 1j, n0=1),
td.KerrNonlinearity(n2=1, n0=1),
],
num_iters=20,
),
Expand Down
Loading

0 comments on commit 35edc0b

Please sign in to comment.