-
Notifications
You must be signed in to change notification settings - Fork 66
chore: Move nonlinear models to separate file (FXC-3925) #2947
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 files reviewed, 1 comment
28931ca to
51ea669
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 files reviewed, 1 comment
51ea669 to
7cba2aa
Compare
dbochkov-flexcompute
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great to see the 8k medium.py file finally get chopped into pieces. I think the new logic for setting wavelength into auto grid is a more robust behavior, I guess just need to skip it if there are no sources to begin with to prevent unintentional errors. Also, could you add a note in the subsection's docstring that auto grid will be assigned a wavelength if not yet provided because automatic removing of sources
Thanks. I actually realized I don't need to hardcode the wavelength in the final simulation subsection -- I just need to hardcode it for intermediate steps where the sources are not included to avoid validation. So maybe I'll leave the behavior as it was previously with regards to grid... |
7cba2aa to
33379ad
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Additional Comments (1)
-
tidy3d/components/medium.py, line 105-106 (link)logic: Duplicate constant definitions - these constants are now defined in both
medium.pyandnonlinear.py, but are only used innonlinear.py. Remove frommedium.py.
5 files reviewed, 1 comment
c899d32 to
7226fb9
Compare
|
OK, I was actually pretty confused about this. I changed it as follows, hopefully it makes sense now:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
5 files reviewed, no comments
Diff CoverageDiff: origin/develop...HEAD, staged and unstaged changes
Summary
tidy3d/components/nonlinear.pyLines 29-47 29 """Check that the model is compatible with the medium."""
30 from .medium import AbstractCustomMedium, DispersiveMedium, Medium
31
32 if isinstance(medium, AbstractCustomMedium):
! 33 raise ValidationError(
34 f"'NonlinearModel' of class '{type(self).__name__}' is not currently supported "
35 f"for medium class '{type(medium).__name__}'."
36 )
37 if medium.is_time_modulated:
! 38 raise ValidationError(
39 f"'NonlinearModel' of class '{type(self).__name__}' is not currently supported "
40 f"for time-modulated medium class '{type(medium).__name__}'."
41 )
42 if not isinstance(medium, (Medium, DispersiveMedium)):
! 43 raise ValidationError(
44 f"'NonlinearModel' of class '{type(self).__name__}' is not currently supported "
45 f"for medium class '{type(medium).__name__}'."
46 )Lines 53-61 53
54 @property
55 def complex_fields(self) -> bool:
56 """Whether the model uses complex fields."""
! 57 return False
58
59 @property
60 def aux_fields(self) -> list[str]:
61 """List of available aux fields in this model."""Lines 357-365 357 @pd.validator("models", always=True)
358 def _no_duplicate_models(cls, val):
359 """Ensure each type of model appears at most once."""
360 if val is None:
! 361 return val
362 models = [model.__class__ for model in val]
363 models_unique = set(models)
364 if len(models) != len(models_unique):
365 raise ValidationError(Lines 390-398 390 @pd.validator("models", always=True)
391 def _consistent_models(cls, val):
392 """Ensure that parameters shared between models are consistent."""
393 if val is None:
! 394 return val
395 n0 = None
396 for model in val:
397 if isinstance(model, (KerrNonlinearity, TwoPhotonAbsorption)):
398 if model.n0 is not None: |
@dbochkov-flexcompute only 7k more lines to go 😂 |
yaugenst-flex
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @caseyflex, this is great, only have a couple of minor nits.
edbf4b1 to
0a2424b
Compare
0a2424b to
6f9cbbd
Compare
This PR:
medium.pya bituse_complex_fieldswas removedSimulation.subsectiontreatment of sources in the presence of nonlinear materialsGreptile Overview
Updated On: 2025-10-31 17:39:54 UTC
Greptile Summary
Successfully refactored nonlinear models into a separate
nonlinear.pyfile to improve code organization and reduce the size ofmedium.py.Key changes:
tidy3d/components/nonlinear.pywith all nonlinear model classes (NonlinearModel,NonlinearSusceptibility,TwoPhotonAbsorption,KerrNonlinearity,NonlinearSpec)_get_n0,_get_freq0,_hardcode_medium_freqs) that were previously used for frequency hardcodingSimulation.subsectionto preserve sources instead of hardcoding frequency info into nonlinear materialsThe refactoring is clean and maintains backward compatibility for public APIs.
Confidence Score: 5/5
Important Files Changed
File Analysis
Sequence Diagram
sequenceDiagram participant User participant Simulation participant Medium participant NonlinearSpec participant NonlinearModel Note over Medium,NonlinearModel: Before: All in medium.py Note over Medium,NonlinearModel: After: Separated into nonlinear.py User->>Medium: Create medium with nonlinear_spec Medium->>NonlinearSpec: Import from nonlinear module NonlinearSpec->>NonlinearModel: Contains models list User->>Simulation: Call subsection() Note over Simulation: Removed frequency hardcoding Simulation->>Simulation: Preserve sources in subsection Simulation-->>User: Return subsection with sources