11# utilities for autograd derivative passing
2- from __future__ import annotations
2+
3+ from typing import Optional
34
45import numpy as np
5- import pydantic .v1 as pd
66import xarray as xr
7+ from pydantic import Field
78
9+ from ...compat import Self
810from ...constants import LARGE_NUMBER
911from ..base import Tidy3dBaseModel
1012from ..data .data_array import ScalarFieldDataArray , SpatialDataArray
@@ -43,32 +45,27 @@ class DerivativeSurfaceMesh(Tidy3dBaseModel):
4345
4446 """
4547
46- centers : ArrayLike = pd .Field (
47- ...,
48+ centers : ArrayLike = Field (
4849 title = "Centers" ,
4950 description = "(N, 3) array storing the centers of each surface element." ,
5051 )
5152
52- areas : ArrayLike = pd .Field (
53- ...,
53+ areas : ArrayLike = Field (
5454 title = "Area Elements" ,
5555 description = "(N,) array storing the first perpendicular vectors of each surface element." ,
5656 )
5757
58- normals : ArrayLike = pd .Field (
59- ...,
58+ normals : ArrayLike = Field (
6059 title = "Normals" ,
6160 description = "(N, 3) array storing the normal vectors of each surface element." ,
6261 )
6362
64- perps1 : ArrayLike = pd .Field (
65- ...,
63+ perps1 : ArrayLike = Field (
6664 title = "Perpendiculars 1" ,
6765 description = "(N, 3) array storing the first perpendicular vectors of each surface element." ,
6866 )
6967
70- perps2 : ArrayLike = pd .Field (
71- ...,
68+ perps2 : ArrayLike = Field (
7269 title = "Perpendiculars 1" ,
7370 description = "(N, 3) array storing the first perpendicular vectors of each surface element." ,
7471 )
@@ -77,122 +74,110 @@ class DerivativeSurfaceMesh(Tidy3dBaseModel):
7774class DerivativeInfo (Tidy3dBaseModel ):
7875 """Stores derivative information passed to the ``.compute_derivatives`` methods."""
7976
80- paths : list [PathType ] = pd .Field (
81- ...,
77+ paths : list [PathType ] = Field (
8278 title = "Paths to Traced Fields" ,
8379 description = "List of paths to the traced fields that need derivatives calculated." ,
8480 )
8581
86- E_der_map : FieldData = pd .Field (
87- ...,
82+ E_der_map : FieldData = Field (
8883 title = "Electric Field Gradient Map" ,
8984 description = 'Dataset where the field components ``("Ex", "Ey", "Ez")`` store the '
9085 "multiplication of the forward and adjoint electric fields. The tangential components "
9186 "of this dataset is used when computing adjoint gradients for shifting boundaries. "
9287 "All components are used when computing volume-based gradients." ,
9388 )
9489
95- D_der_map : FieldData = pd .Field (
96- ...,
90+ D_der_map : FieldData = Field (
9791 title = "Displacement Field Gradient Map" ,
9892 description = 'Dataset where the field components ``("Ex", "Ey", "Ez")`` store the '
9993 "multiplication of the forward and adjoint displacement fields. The normal component "
10094 "of this dataset is used when computing adjoint gradients for shifting boundaries." ,
10195 )
10296
103- E_fwd : FieldData = pd .Field (
104- ...,
97+ E_fwd : FieldData = Field (
10598 title = "Forward Electric Fields" ,
10699 description = 'Dataset where the field components ``("Ex", "Ey", "Ez")`` represent the '
107100 "forward electric fields used for computing gradients for a given structure." ,
108101 )
109102
110- E_adj : FieldData = pd .Field (
111- ...,
103+ E_adj : FieldData = Field (
112104 title = "Adjoint Electric Fields" ,
113105 description = 'Dataset where the field components ``("Ex", "Ey", "Ez")`` represent the '
114106 "adjoint electric fields used for computing gradients for a given structure." ,
115107 )
116108
117- D_fwd : FieldData = pd .Field (
118- ...,
109+ D_fwd : FieldData = Field (
119110 title = "Forward Displacement Fields" ,
120111 description = 'Dataset where the field components ``("Ex", "Ey", "Ez")`` represent the '
121112 "forward displacement fields used for computing gradients for a given structure." ,
122113 )
123114
124- D_adj : FieldData = pd .Field (
125- ...,
115+ D_adj : FieldData = Field (
126116 title = "Adjoint Displacement Fields" ,
127117 description = 'Dataset where the field components ``("Ex", "Ey", "Ez")`` represent the '
128118 "adjoint displacement fields used for computing gradients for a given structure." ,
129119 )
130120
131- eps_data : PermittivityData = pd .Field (
132- ...,
121+ eps_data : PermittivityData = Field (
133122 title = "Permittivity Dataset" ,
134123 description = "Dataset of relative permittivity values along all three dimensions. "
135124 "Used for automatically computing permittivity inside or outside of a simple geometry." ,
136125 )
137126
138- eps_in : tidycomplex = pd . Field (
127+ eps_in : tidycomplex = Field (
139128 title = "Permittivity Inside" ,
140129 description = "Permittivity inside of the ``Structure``. "
141130 "Typically computed from ``Structure.medium.eps_model``."
142131 "Used when it can not be computed from ``eps_data`` or when ``eps_approx==True``." ,
143132 )
144133
145- eps_out : tidycomplex = pd .Field (
146- ...,
134+ eps_out : tidycomplex = Field (
147135 title = "Permittivity Outside" ,
148136 description = "Permittivity outside of the ``Structure``. "
149137 "Typically computed from ``Simulation.medium.eps_model``."
150138 "Used when it can not be computed from ``eps_data`` or when ``eps_approx==True``." ,
151139 )
152140
153- eps_background : tidycomplex = pd . Field (
141+ eps_background : tidycomplex = Field (
154142 None ,
155143 title = "Permittivity in Background" ,
156144 description = "Permittivity outside of the ``Structure`` as manually specified by. "
157145 "``Structure.background_medium``. " ,
158146 )
159147
160- bounds : Bound = pd .Field (
161- ...,
148+ bounds : Bound = Field (
162149 title = "Geometry Bounds" ,
163150 description = "Bounds corresponding to the structure, used in ``Medium`` calculations." ,
164151 )
165152
166- bounds_intersect : Bound = pd .Field (
167- ...,
153+ bounds_intersect : Bound = Field (
168154 title = "Geometry and Simulation Intersections Bounds" ,
169155 description = "Bounds corresponding to the minimum intersection between the "
170156 "structure and the simulation it is contained in." ,
171157 )
172158
173- frequency : float = pd .Field (
174- ...,
159+ frequency : float = Field (
175160 title = "Frequency of adjoint simulation" ,
176161 description = "Frequency at which the adjoint gradient is computed." ,
177162 )
178163
179- eps_no_structure : SpatialDataArray = pd . Field (
164+ eps_no_structure : Optional [ SpatialDataArray ] = Field (
180165 None ,
181166 title = "Permittivity Without Structure" ,
182167 description = "The permittivity of the original simulation without the structure that is "
183168 "being differentiated with respect to. Used to approximate permittivity outside of the "
184169 "structure for shape optimization." ,
185170 )
186171
187- eps_inf_structure : SpatialDataArray = pd . Field (
172+ eps_inf_structure : Optional [ SpatialDataArray ] = Field (
188173 None ,
189174 title = "Permittivity With Infinite Structure" ,
190175 description = "The permittivity of the original simulation where the structure being "
191176 " differentiated with respect to is inifinitely large. Used to approximate permittivity "
192177 "inside of the structure for shape optimization." ,
193178 )
194179
195- eps_approx : bool = pd . Field (
180+ eps_approx : bool = Field (
196181 False ,
197182 title = "Use Permittivity Approximation" ,
198183 description = "If ``True``, approximates outside permittivity using ``Simulation.medium``"
@@ -201,7 +186,7 @@ class DerivativeInfo(Tidy3dBaseModel):
201186 "evaluate the inside and outside relative permittivity for each geometry." ,
202187 )
203188
204- def updated_paths (self , paths : list [PathType ]) -> DerivativeInfo :
189+ def updated_paths (self , paths : list [PathType ]) -> Self :
205190 """Update this ``DerivativeInfo`` with new set of paths."""
206191 return self .updated_copy (paths = paths )
207192
0 commit comments