Derivative Export
In a Scattering project, solver derivative data can occur in
postprocesses and some
postprocess headers.
PropagatingMode and ResonanceMode eigenvalues do not contain solver
derivatives.
Tabular postprocess data supports to_dict(), to_xarray(), and to_df().
Declared Derivative Parameters
The layout file declares the parameters width_parameter, height, swa, and radius.
DerivativeParameter {
Name = "width_parameter"
TreeEntry = "Layout2D/Objects/Line/Width"
}
DerivativeParameter {
Name = "height"
TreeEntry = "Layout2D/Objects/Line/Height"
}
DerivativeParameter {
Name = "swa"
TreeEntry = "Layout2D/Objects/Line/InnerAngles(1)"
TreeEntry = "Layout2D/Objects/Line/InnerAngles(2)"
}
DerivativeParameter {
Name = "radius"
TreeEntry = "Layout2D/Objects/Line/CornerRounding/Radius"
}
The derivative names appear in the result data. Project keys and derivative names are separate configuration values.
Computation
The solve call computes the solver result and the derivative
data. The export examples select the tabular postprocess.
results = jcmsuite.solve(
"project.jcmp",
keys={
"h": 55.0,
"width": 31.0,
"swa": 88.0,
"radius": 8.0,
"permittivity": 1.967 + 4.443j,
},
temporary=True,
)
Tabular Exports
Exports To A Dictionary
Call to_dict() on a tabular
postprocess to retain its nested mapping. The dictionary can contain base values
and derivative values at the same level:
# The first postprocess is tabular.
table_pp = results.postprocesses[0]
table_dict_export = table_pp.to_dict()
print(table_dict_export.keys())
# dict_keys(
# [
# "title",
# "header",
# "K",
# "N1",
# "N2",
# "ElectricFieldStrength",
# "d_width_parameter",
# "d_height",
# "d_swa",
# "d_radius",
# ]
# )
print(table_dict_export["ElectricFieldStrength"])
# {
# 0: array(
# [[0.36797123 + 0.32891674j, -0.36595891 - 0.32765183j, 0.00305147 + 0.0019181j]]
# ),
# 1: array(
# [[0.05294917 - 0.07681159j, 0.05771068 - 0.07381861j, 0.16780408 - 0.22841493j]]
# ),
# }
The derivative entries contain values for the declared derivative parameters. Derivative
entries can use flat keys such as d_width_parameter or nested mappings. Dictionary
export preserves the structure from the result data.
print(table_dict_export["d_swa"].keys())
# dict_keys(["K", "ElectricFieldStrength"])
print(table_dict_export["d_swa"])
# {
# "K": array([[0.0, 0.0, 0.0]]),
# "ElectricFieldStrength": {
# 0: array(
# [
# [
# -1.67455947e-03 + 1.79252586e-04j,
# 1.72278951e-03 - 1.43207032e-04j,
# 7.31358144e-05 + 5.46593112e-05j,
# ]
# ]
# ),
# 1: array(
# [
# [
# 0.00039417 - 0.00024147j,
# 0.00050829 - 0.00015619j,
# 0.00136848 - 0.00060302j,
# ]
# ]
# ),
# },
# }
The nested d_swa mapping contains the derivative of each tabular result. Its K value
and ElectricFieldStrength values retain the field-index mapping from the result.
Exports To An xarray DataTree
Note
Xarray exports require the xarray extra. See Installation.
Call to_xarray() to export
tabular data to an xarray DataTree.
# The first postprocess is tabular.
table_pp = results.postprocesses[0]
table_xarray_export = table_pp.to_xarray()
print(table_xarray_export)
# <xarray.DataTree>
# Group: /
# │ Dimensions: (field_index: 2, sample_index: 1, component: 3,
# │ K_dim_0: 3)
# │ Coordinates:
# │ * field_index (field_index) int64 16B 0 1
# ...
# │ K (sample_index, K_dim_0) float64 24B -1.103e+07 ......
# │ Data variables:
# │ ElectricFieldStrength (field_index, sample_index, component) complex128 96B ...
# ...
# │ Attributes:
# │ field_name: ElectricFieldStrength
# ...
# ├── Group: /d_width_parameter
# ...
# ├── Group: /d_height
# ...
# ├── Group: /d_swa
# ...
# └── Group: /d_radius
# ...
The root node contains the base data. Nested derivative mappings become child nodes. The example accesses variables and dimensions at the root and at a derivative child node.
print(table_xarray_export["ElectricFieldStrength"])
# <xarray.DataArray 'ElectricFieldStrength' (field_index: 2, sample_index: 1,
# component: 3)> Size: 96B
# array([[[ 0.36797123+0.32891674j, -0.36595891-0.32765183j,
# 0.00305147+0.0019181j ]],
# [[ 0.05294917-0.07681159j, 0.05771068-0.07381861j,
# 0.16780408-0.22841493j]]])
# Coordinates:
# * field_index (field_index) int64 16B 0 1
# ...
print(table_xarray_export["d_width_parameter"])
# <xarray.DataTree 'd_width_parameter'>
# Group: /d_width_parameter
# Dimensions: (field_index: 2, sample_index: 1, component: 3,
# K_dim_0: 3)
# Coordinates:
# K (sample_index, K_dim_0) float64 24B 0.0 0.0 0.0
# Inherited coordinates:
# ...
# Data variables:
# ElectricFieldStrength (field_index, sample_index, component) complex128 96B ...
# ...
# Attributes:
# field_name: ElectricFieldStrength
# ...
The table values use named dimensions, including field_index, sample_index, and
component. Each derivative is available as a child node such as /d_width_parameter.
Exports To pandas DataFrames
Note
DataFrame exports require the pandas extra. See Installation.
Call to_df() to export tabular
data to a pandas DataFrame. Table payloads use the wide layout by default.
Structured and table-collection payloads use the long layout.
table_df_export = table_pp.to_df()
print(table_df_export)
# row_index ... d_radius.ElectricFieldStrength.1.1 ...
# 0 0 ... -0.000035+0.000017j ...
#
# [1 rows x 38 columns]
print(table_df_export.columns)
# Index(
# [
# "row_index",
# "K",
# "N1",
# "N2",
# "ElectricFieldStrength.0.0",
# "ElectricFieldStrength.0.1",
# "ElectricFieldStrength.0.2",
# "ElectricFieldStrength.1.0",
# "ElectricFieldStrength.1.1",
# "ElectricFieldStrength.1.2",
# "d_width_parameter.K",
# "d_width_parameter.ElectricFieldStrength.0.0",
# "d_width_parameter.ElectricFieldStrength.0.1",
# "d_width_parameter.ElectricFieldStrength.0.2",
# "d_width_parameter.ElectricFieldStrength.1.0",
# "d_width_parameter.ElectricFieldStrength.1.1",
# "d_width_parameter.ElectricFieldStrength.1.2",
# "d_height.K",
# "d_height.ElectricFieldStrength.0.0",
# ...
# "d_swa.K",
# ...
# "d_radius.K",
# ...
# ],
# dtype="str",
# )
Project Files
The export examples use the same input files. Only the Python export script differs.
export_dictionary/run.py
"""Export table derivatives as dictionaries."""
import jcmsuite
results = jcmsuite.solve(
"project.jcmp",
keys={
"h": 55.0,
"width": 31.0,
"swa": 88.0,
"radius": 8.0,
"permittivity": 1.967 + 4.443j,
},
temporary=True,
)
# The first postprocess is tabular.
table_pp = results.postprocesses[0]
table_dict_export = table_pp.to_dict()
print(table_dict_export.keys())
# dict_keys(
# [
# "title",
# "header",
# "K",
# "N1",
# "N2",
# "ElectricFieldStrength",
# "d_width_parameter",
# "d_height",
# "d_swa",
# "d_radius",
# ]
# )
print(table_dict_export["ElectricFieldStrength"])
# {
# 0: array(
# [[0.36797123 + 0.32891674j, -0.36595891 - 0.32765183j, 0.00305147 + 0.0019181j]]
# ),
# 1: array(
# [[0.05294917 - 0.07681159j, 0.05771068 - 0.07381861j, 0.16780408 - 0.22841493j]]
# ),
# }
print(table_dict_export["d_swa"].keys())
# dict_keys(["K", "ElectricFieldStrength"])
print(table_dict_export["d_swa"])
# {
# "K": array([[0.0, 0.0, 0.0]]),
# "ElectricFieldStrength": {
# 0: array(
# [
# [
# -1.67455947e-03 + 1.79252586e-04j,
# 1.72278951e-03 - 1.43207032e-04j,
# 7.31358144e-05 + 5.46593112e-05j,
# ]
# ]
# ),
# 1: array(
# [
# [
# 0.00039417 - 0.00024147j,
# 0.00050829 - 0.00015619j,
# 0.00136848 - 0.00060302j,
# ]
# ]
# ),
# },
# }
export_xarray/run.py
"""Export table derivatives as xarray DataTrees."""
import jcmsuite
results = jcmsuite.solve(
"project.jcmp",
keys={
"h": 55.0,
"width": 31.0,
"swa": 88.0,
"radius": 8.0,
"permittivity": 1.967 + 4.443j,
},
temporary=True,
)
# The first postprocess is tabular.
table_pp = results.postprocesses[0]
table_xarray_export = table_pp.to_xarray()
print(table_xarray_export)
# <xarray.DataTree>
# Group: /
# │ Dimensions: (field_index: 2, sample_index: 1, component: 3,
# │ K_dim_0: 3)
# │ Coordinates:
# │ * field_index (field_index) int64 16B 0 1
# ...
# │ K (sample_index, K_dim_0) float64 24B -1.103e+07 ......
# │ Data variables:
# │ ElectricFieldStrength (field_index, sample_index, component) complex128 96B ...
# ...
# │ Attributes:
# │ field_name: ElectricFieldStrength
# ...
# ├── Group: /d_width_parameter
# ...
# ├── Group: /d_height
# ...
# ├── Group: /d_swa
# ...
# └── Group: /d_radius
# ...
print(table_xarray_export["ElectricFieldStrength"])
# <xarray.DataArray 'ElectricFieldStrength' (field_index: 2, sample_index: 1,
# component: 3)> Size: 96B
# array([[[ 0.36797123+0.32891674j, -0.36595891-0.32765183j,
# 0.00305147+0.0019181j ]],
# [[ 0.05294917-0.07681159j, 0.05771068-0.07381861j,
# 0.16780408-0.22841493j]]])
# Coordinates:
# * field_index (field_index) int64 16B 0 1
# ...
print(table_xarray_export["d_width_parameter"])
# <xarray.DataTree 'd_width_parameter'>
# Group: /d_width_parameter
# Dimensions: (field_index: 2, sample_index: 1, component: 3,
# K_dim_0: 3)
# Coordinates:
# K (sample_index, K_dim_0) float64 24B 0.0 0.0 0.0
# Inherited coordinates:
# ...
# Data variables:
# ElectricFieldStrength (field_index, sample_index, component) complex128 96B ...
# ...
# Attributes:
# field_name: ElectricFieldStrength
# ...
export_dataframe/run.py
import jcmsuite
results = jcmsuite.solve(
"project.jcmp",
keys={
"h": 55.0,
"width": 31.0,
"swa": 88.0,
"radius": 8.0,
"permittivity": 1.967 + 4.443j,
},
temporary=True,
)
# The first postprocess is tabular.
table_pp = results.postprocesses[0]
table_df_export = table_pp.to_df()
print(table_df_export)
# row_index ... d_radius.ElectricFieldStrength.1.1 ...
# 0 0 ... -0.000035+0.000017j ...
#
# [1 rows x 38 columns]
print(table_df_export.columns)
# Index(
# [
# "row_index",
# "K",
# "N1",
# "N2",
# "ElectricFieldStrength.0.0",
# "ElectricFieldStrength.0.1",
# "ElectricFieldStrength.0.2",
# "ElectricFieldStrength.1.0",
# "ElectricFieldStrength.1.1",
# "ElectricFieldStrength.1.2",
# "d_width_parameter.K",
# "d_width_parameter.ElectricFieldStrength.0.0",
# "d_width_parameter.ElectricFieldStrength.0.1",
# "d_width_parameter.ElectricFieldStrength.0.2",
# "d_width_parameter.ElectricFieldStrength.1.0",
# "d_width_parameter.ElectricFieldStrength.1.1",
# "d_width_parameter.ElectricFieldStrength.1.2",
# "d_height.K",
# "d_height.ElectricFieldStrength.0.0",
# ...
# "d_swa.K",
# ...
# "d_radius.K",
# ...
# ],
# dtype="str",
# )
project.jcmp
Project {
Derivatives {
Order = 1
}
InfoLevel = 10
Electromagnetics {
TimeHarmonic {
Scattering {
FieldComponents = Electric
Accuracy {
FiniteElementDegree = 3
Precision = 0.001
}
}
}
}
}
PostProcess {
FourierTransform {
FieldBagPath = "project_results/fieldbag.jcm"
OutputFileName = "project_results/fc.jcm"
NormalDirection = Y
Rotation = X:Z:-Y
Format = JCM
}
}
PostProcess {
ScatteringMatrix {
Format = JCM
InputFileName = "project_results/fc.jcm"
OutputFileName = "project_results/sm.jcm"
}
}
layout.jcmt
DerivativeParameter {
Name = "width_parameter"
TreeEntry = "Layout2D/Objects/Line/Width"
}
DerivativeParameter {
Name = "height"
TreeEntry = "Layout2D/Objects/Line/Height"
}
DerivativeParameter {
Name = "swa"
TreeEntry = "Layout2D/Objects/Line/InnerAngles(1)"
TreeEntry = "Layout2D/Objects/Line/InnerAngles(2)"
}
DerivativeParameter {
Name = "radius"
TreeEntry = "Layout2D/Objects/Line/CornerRounding/Radius"
}
Layout2D {
Name = "Line_with_derivatives"
UnitOfLength = 1e-9
MeshOptions {
MinimumMeshAngle = 25
SnapTolerance = 1e-6
MaximumSideLength = 10
}
Objects {
Parallelogram {
Name = "ComputationalDomain"
DomainId = 1
Priority = ComputationalDomain
Width = 90
Height = 100
Boundary {
Number = [1 3]
Class = TransparentBoundary
}
Boundary {
Number = [2 4]
Class = Periodic
}
}
Parallelogram {
Name = "Substrate"
DomainId = 3
Width = 90
Height = 20
Port = South
Alignment {
Parent {
Domain = "ComputationalDomain"
Port = South
}
Orientation = Parallel
}
}
Trapezoid {
Name = "Line"
DomainId = 2
Priority = 4
Height = %(h)e
Width = %(width)e
InnerAngles = [%(swa)e %(swa)e]
Port = South
Alignment {
Parent {
Domain = "Substrate"
Port = North
}
Orientation = AntiParallel
}
CornerRounding {
Point = [3 4]
Radius = %(radius)e
NPoints = 5
}
}
}
}
materials.jcmt
Material {
Name = "Material_1"
DomainId = 1
RelPermittivity = 1
RelPermeability = 1.0
}
Material {
Name = "Material_2"
DomainId = 2
RelPermittivity = 1.4
RelPermeability = 1.0
}
Material {
Name = "Material_3"
DomainId = 3
RelPermittivity = %(permittivity)g
RelPermeability = 1.0
}
sources.jcm
SourceBag {
Source {
ElectricFieldStrength {
PlaneWave {
3DTo2D = yes
Incidence = FromAbove
SP = [1 0]
Lambda0 = 3.65e-07
ThetaPhi = [65, 45]
}
}
}
}
SourceBag {
Source {
ElectricFieldStrength {
PlaneWave {
3DTo2D = yes
Incidence = FromAbove
SP = [0 1]
Lambda0 = 3.65e-07
ThetaPhi = [65, 45]
}
}
}
}
Continue with Torch Integration to connect numeric postprocess derivatives to PyTorch autograd.