Skip to content

Commit 4bcc36a

Browse files
committed
Moving nonlinear warnings to simulation and consolidating
1 parent 217e269 commit 4bcc36a

File tree

3 files changed

+49
-46
lines changed

3 files changed

+49
-46
lines changed

tests/test_components/test_medium.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -561,17 +561,6 @@ def test_nonlinear_medium(log_capture):
561561
)
562562
)
563563

564-
# complex parameters
565-
with AssertLogLevel(log_capture, "WARNING", contains_str="preferred"):
566-
med = td.Medium(
567-
nonlinear_spec=td.NonlinearSpec(
568-
models=[
569-
td.KerrNonlinearity(n2=-1 + 1j, n0=1),
570-
],
571-
num_iters=20,
572-
)
573-
)
574-
575564
# warn about deprecated api
576565
med = td.Medium(nonlinear_spec=td.NonlinearSusceptibility(chi3=1.5))
577566
assert_log_level(log_capture, "WARNING")
@@ -621,12 +610,6 @@ def test_nonlinear_medium(log_capture):
621610
with pytest.raises(ValidationError):
622611
med = td.Medium(nonlinear_spec=td.NonlinearSpec(models=[td.KerrNonlinearity(n2=-1j, n0=1)]))
623612

624-
with AssertLogLevel(log_capture, "WARNING", contains_str="phenomenological"):
625-
med = td.Medium(
626-
nonlinear_spec=td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=-1, n0=1)]),
627-
allow_gain=True,
628-
)
629-
630613
# automatic detection of n0 and freq0
631614
n0 = 2
632615
freq0 = td.C_0 / 1
@@ -649,6 +632,26 @@ def test_nonlinear_medium(log_capture):
649632
assert n0 == nonlinear_spec.models[0]._get_n0(n0=None, medium=medium, freqs=[freq0])
650633
assert freq0 == nonlinear_spec.models[0]._get_freq0(freq0=None, freqs=[freq0])
651634

635+
# two photon absorption is phenomenological
636+
with AssertLogLevel(log_capture, "WARNING", contains_str="phenomenological"):
637+
med = td.Medium(
638+
nonlinear_spec=td.NonlinearSpec(models=[td.TwoPhotonAbsorption(beta=-1, n0=1)]),
639+
allow_gain=True,
640+
)
641+
sim.updated_copy(medium=med, path="structures/0")
642+
643+
# complex parameters
644+
with AssertLogLevel(log_capture, "WARNING", contains_str="preferred"):
645+
med = td.Medium(
646+
nonlinear_spec=td.NonlinearSpec(
647+
models=[
648+
td.KerrNonlinearity(n2=-1 + 1j, n0=1),
649+
],
650+
num_iters=20,
651+
)
652+
)
653+
sim.updated_copy(medium=med, path="structures/0")
654+
652655
# subsection with nonlinear materials needs to hardcode source info
653656
sim2 = sim.updated_copy(center=(-4, -4, -4), path="sources/0")
654657
sim2 = sim2.updated_copy(

tidy3d/components/medium.py

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -442,14 +442,6 @@ def _hardcode_medium_freqs(
442442

443443
def _validate_medium(self, medium: AbstractMedium):
444444
"""Check that the model is compatible with the medium."""
445-
log.warning(
446-
"Found a medium with a 'TwoPhotonAbsorption' nonlinearity. "
447-
"This uses a phenomenological model based on complex fields, "
448-
"so care should be taken in interpreting the results. For more "
449-
"information on the model, see the documentation at "
450-
"'https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.TwoPhotonAbsorption.html' or the following reference: "
451-
"'N. Suzuki, \"FDTD Analysis of Two-Photon Absorption and Free-Carrier Absorption in Si High-Index-Contrast Waveguides,\" J. Light. Technol. 25, 9 (2007).'."
452-
)
453445
# if n0 is specified, we can go ahead and validate passivity
454446
if self.n0 is not None:
455447
self._validate_medium_freqs(medium, [])
@@ -459,19 +451,6 @@ def complex_fields(self) -> bool:
459451
"""Whether the model uses complex fields."""
460452
return True
461453

462-
@pd.validator("beta", always=True)
463-
def _warn_for_complex_beta(cls, val):
464-
if val is None:
465-
return val
466-
if np.iscomplex(val):
467-
log.warning(
468-
"Complex values of 'beta' in 'TwoPhotonAbsorption' are deprecated "
469-
"and may be removed in a future version. The implementation with "
470-
"complex 'beta' is as described in the 'TwoPhotonAbsorption' docstring, "
471-
"but the physical interpretation of 'beta' may not be correct if it is complex."
472-
)
473-
return val
474-
475454

476455
class KerrNonlinearity(NonlinearModel):
477456
"""Model for Kerr nonlinearity which gives an intensity-dependent refractive index
@@ -546,13 +525,6 @@ def _hardcode_medium_freqs(
546525

547526
def _validate_medium(self, medium: AbstractMedium):
548527
"""Check that the model is compatible with the medium."""
549-
log.warning(
550-
"Found a medium with a 'KerrNonlinearity'. Usually, "
551-
"'NonlinearSusceptibility' is preferred, as it captures "
552-
"additional physical effects by acting on the underlying real fields. "
553-
"The relation between the parameters is "
554-
"'chi3 = (4/3) * eps_0 * c_0 * n0 * Re(n0) * n2'."
555-
)
556528
# if n0 is specified, we can go ahead and validate passivity
557529
if self.n0 is not None:
558530
self._validate_medium_freqs(medium, [])

tidy3d/components/simulation.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@
4545
AbstractPerturbationMedium,
4646
AnisotropicMedium,
4747
FullyAnisotropicMedium,
48+
KerrNonlinearity,
4849
Medium,
4950
Medium2D,
5051
MediumType,
5152
MediumType3D,
53+
TwoPhotonAbsorption,
5254
)
5355
from .monitor import (
5456
AbstractFieldProjectionMonitor,
@@ -3360,13 +3362,39 @@ def _validate_tfsf_nonuniform_grid(self) -> None:
33603362

33613363
def _validate_nonlinear_specs(self) -> None:
33623364
"""Run :class:`.NonlinearSpec` validators that depend on knowing the central
3363-
frequencies of the sources."""
3365+
frequencies of the sources. Also print some warnings only once per unique medium."""
33643366
freqs = np.array([source.source_time.freq0 for source in self.sources])
33653367
for medium in self.scene.mediums:
33663368
if medium.nonlinear_spec is not None:
33673369
for model in medium._nonlinear_models:
33683370
model._validate_medium_freqs(medium, freqs)
33693371

3372+
if isinstance(model, TwoPhotonAbsorption):
3373+
if np.iscomplex(model.beta):
3374+
log.warning(
3375+
"Complex values of 'beta' in 'TwoPhotonAbsorption' are deprecated "
3376+
"and may be removed in a future version. The implementation with "
3377+
"complex 'beta' is as described in the 'TwoPhotonAbsorption' docstring, "
3378+
"but the physical interpretation of 'beta' may not be correct if it is complex."
3379+
)
3380+
log.warning(
3381+
"Found a medium with a 'TwoPhotonAbsorption' nonlinearity. "
3382+
"This uses a phenomenological model based on complex fields, "
3383+
"so care should be taken in interpreting the results. For more "
3384+
"information on the model, see the documentation at "
3385+
"'https://docs.flexcompute.com/projects/tidy3d/en/latest/api/_autosummary/tidy3d.TwoPhotonAbsorption.html' or the following reference: "
3386+
"'N. Suzuki, \"FDTD Analysis of Two-Photon Absorption and Free-Carrier Absorption in Si High-Index-Contrast Waveguides,\" J. Light. Technol. 25, 9 (2007).'."
3387+
)
3388+
3389+
if isinstance(model, KerrNonlinearity):
3390+
log.warning(
3391+
"Found a medium with a 'KerrNonlinearity'. Usually, "
3392+
"'NonlinearSusceptibility' is preferred, as it captures "
3393+
"additional physical effects by acting on the underlying real fields. "
3394+
"The relation between the parameters is "
3395+
"'chi3 = (4/3) * eps_0 * c_0 * n0 * Re(n0) * n2'."
3396+
)
3397+
33703398
""" Pre submit validation (before web.upload()) """
33713399

33723400
def validate_pre_upload(self, source_required: bool = True) -> None:

0 commit comments

Comments
 (0)