Check Torch Export Limits
This tutorial shows that Torch-aware postprocess records reject
to_xarray() and
to_df().
Use untorch() to access ordinary result exports.
Experimental feature
The jcmsuite.torch integration is experimental. Its API and behavior can
change in future releases. Do not depend on its current interface for
production workflows without testing each package update.
Run it from demos/documentation_tutorials:
python derivatives/torch/export_limits/run.py
"""Check the explicit export boundary for Torch-bound results."""
import torch
import jcmsuite
from jcmsuite.torch import RealDerivative
width = torch.tensor(31.0, dtype=torch.float64, requires_grad=True)
results = jcmsuite.torch.solve(
"project.jcmp",
keys={
"h": 55.0,
"width": width,
"swa": 88.0,
"radius": 8.0,
"permittivity": 1.967 + 4.443j,
},
temporary=True,
key_derivative_map={"width": RealDerivative("width_parameter")},
)
try:
results.postprocesses[0].to_xarray()
except jcmsuite.exceptions.UnsupportedExportError:
print("xarray export is explicitly unsupported")
Project Files
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
DerivativeParameter {
Name = "permittivity"
TreeEntry = "Material_3/RelPermittivity"
}
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
}
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"
}
}
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]
}
}
}
}