Model
Model(**kwargs)Abstract model class to calculate a component’s S matrix.
Models must be derived from this class and implement the method
start.
Additionally, for model parameterization to work correctly, the
__init__ method must call super().__init__ with all parameters
as keyword arguments (see example below). By default, models are updated
by calling their __init__ method with the new keyword values. If a
different behavior is required, the __init__ method must replace
parametric_kwargs with the proper values for the user-defined
update function and keywords. The update function must update the model
in place and also update parametric_kwargs when called.
Finally, the derived class must be registered with
register_model_class before it can be used in a phf file.
Important
The following attributes and methods defined in the base class
should never be redefined in the derived one: s_matrix,
setup_time_stepper, update, time_stepper,
parametric_function, parametric_kwargs, and
random_variables.
Example
>>> class CustomModel(pf.Model):... def __init__(self, *, coeff):... super().__init__(coeff=coeff)... self.coeff = coeff...... def start(self, component, frequencies, **kwargs):... # Do any type of model calculation... s_param = numpy.exp(self.coeff * frequencies)... s = {("port_in@mode_in", "port_out@mode_out"): s_param}... return pf.SMatrix(s)......>>> pf.register_model_class(CustomModel)>>> model = CustomModel(coeff=5e-15j)Methods
Section titled “Methods”| Method | Description |
|---|---|
autograd_smatrix | Compute an autograd-compatible S matrix for traced parameters. |
estimate_cost | Estimate the cost for S matrix computation. |
s_matrix | Compute the S matrix for a component using this model. |
setup_time_stepper | Obtain a time stepper for a component using this model. |
start | Start the S matrix computation for a component using this model. |
transform | Apply a transformation to this model. |
update | Update this model. |
autograd_smatrix
Section titled “autograd_smatrix”autograd_smatrix( self, *, component, traced_params, frequencies, component_for_params, param_names, show_progress=True, model_kwargs=None,)Compute an autograd-compatible S matrix for traced parameters.
Important
This is an abstract method that must be implemented by derived classes that support autograd-based S-matrix computation.
Args
- component (Component): Nominal component associated with this autograd evaluation.
- traced_params (ndarray): Traced parameter vector used by autograd.
- frequencies (Sequence[float]): Frequency values at which to calculate the scattering parameters (in Hz).
- component_for_params (Callable[[ndarray], Component]): Callback that returns the component for a given traced parameter vector.
- param_names (Sequence[str]): Flattened traced parameter names aligned with
traced_params. - show_progress (bool): Progress display hint from
Component.s_matrix. - model_kwargs (dict[str, Any] | None): Model-specific keyword arguments forwarded from
Component.s_matrix.
Returns
AutogradSMatrix: Autograd-compatible S matrix.
estimate_cost
Section titled “estimate_cost”estimate_cost(self, *args, **kwargs)Estimate the cost for S matrix computation.
All arguments are passed to the model’s start method with the
addition of the keyword argument cost_estimation=True.
Returns
float: Cost estimation.
s_matrix
Section titled “s_matrix”s_matrix(self, component, frequencies, show_progress=True, model_kwargs={})Compute the S matrix for a component using this model.
Args
- component (Component): Component to perform the calculation.
- frequencies (Sequence[float]): Frequency values at which to calculate the scattering parameters (in Hz).
- show_progress (bool): If
True, show computation progress. - model_kwargs (dict[str, object]): Keyword arguments passed to
Model.start. Accepted arguments depend on the model type.
Returns
SMatrix: Computed S matrix.
Example
Compute only the responses from mode 0 of P0 with Tidy3D to
reduce the number and cost of simulations:
>>> model = pf.Tidy3DModel()>>> s_matrix = model.s_matrix( # doctest: +SKIP... component,... frequencies,... model_kwargs={"inputs": ("P0@0",)},... )A port name such as ("P0",) selects all modes of that port. All
outputs for the selected inputs are returned; responses from
unselected inputs are not computed. Use Model.estimate_cost
with the same inputs passed directly to estimate the reduced run.
setup_time_stepper
Section titled “setup_time_stepper”setup_time_stepper( self, component, time_step, carrier_frequency=0, show_progress=True, time_stepper_kwargs={},)Obtain a time stepper for a component using this model.
Args
- component (Component): Component for the time stepper.
- time_step (float): The interval between time steps (in seconds).
- carrier_frequency (float): The carrier frequency used to construct the time stepper. The carrier should be omitted from the input signals, as it is handled automatically by the time stepper.
- show_progress (bool): If
True, show computation progress. - time_stepper_kwargs (dict[str, object]): Keyword arguments passed to
TimeStepper.setup. Each time stepper type can accept a different set of arguments. Check the documentation for the each time stepper’ssetup_statemethod for more information.
Returns
TimeStepper: Initialized time stepper.
start(self, component, frequencies, **kwargs)Start the S matrix computation for a component using this model.
Important
This is an abstract method that must be implemented by derived classes.
If the S matrix calculation can be performed synchronously, this method should return it directly.
Otherwise, it must return an object with attributes status and
s_matrix that will be pooled to check the calculation progress. The
status attribute must return a dictionary with 'progress' (a
number from 0 to 100 indicating the calculation progress) and
'message' (one of 'running', 'success', or 'error',
indicating the current calculation status). The 's_matrix' attribute
should return the computed SMatrix once the calculation is
successful (or None otherwise).
Important
Once the start method returns, there are no guarantees that the
components, technologies or any other objects used as arguments will
remain constant. That means no references to the original arguments
should be kept after the method returns if the S matrix computation
will continue latter, because they might change externally before
the computation finishes. If needed, copies of the required objects
must be used.
Args
- component (Component): Component to perform the calculation.
- frequencies (Sequence[float]): Frequency values at which to calculate the scattering parameters (in Hz).
- **kwargs (object): Keyword arguments forwarded from the
Component.s_matrixcall plus others generated by calling models (such asCircuitModel).
Returns
SMatrix | object: Object with status and s_matrix.
transform
Section titled “transform”transform( self, translation=(0, 0), rotation=0, scaling=1, x_reflection=False)Apply a transformation to this model.
The base implementation is a no-op and returns self. Derived classes
can override this method to transform model-specific geometry settings.
Args
- translation (Sequence[float] | complex): Translation vector.
- rotation (float): Rotation angle in degrees.
- scaling (float): Magnification factor.
- x_reflection (bool): Mirror across the horizontal axis.
Returns
Model: This object with transformations applied.
update
Section titled “update”update(self, *args, **kwargs)Update this model.
All contents of the model are updated with the contents from the updated version. object contents or modifications introduced after the model creation (outside of the original parametric function) are not propagated.
The model’s time stepper does not participate in the update, unless
the __init__ method of the derived model class replaces it via
set_time_stepper.
Args
- *args (object): Positional arguments to the parametric model.
- **kwargs (object): Keyword arguments to the parametric model.
Returns
Model: This model.
Note
The original keyword arguments used to generate the model are stored
and updated with the passed kwargs. This updated version is used
in the function call and will be stored for future updates.
Positional arguments are not stored.
Attributes
Section titled “Attributes”| Attribute | Description |
|---|---|
parametric_function | str: Function used to update the model. |
parametric_kwargs | dict[str, object]: Keyword arguments used to update the model. |
properties | Properties: Object properties. |
random_variables | list[RandomVariable]: Random variables associated to the model’s parameters. |
time_stepper | TimeStepper | None: Time stepper associated with this model. |
parametric_function
Section titled “parametric_function”str: Function used to update the model.
parametric_kwargs
Section titled “parametric_kwargs”dict[str, object]: Keyword arguments used to update the model.
properties
Section titled “properties”Properties: Object properties.
random_variables
Section titled “random_variables”list[RandomVariable]: Random variables associated to the model’s parameters.
time_stepper
Section titled “time_stepper”TimeStepper | None: Time stepper associated with this model.