Bind a Real Project Key
This tutorial connects a real Torch project key to a solver-provided numeric postprocess derivative.
The example gets the ordinary result with results.untorch() and compares the Torch gradient
with the ordinary solver derivative.
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/real_derivative/run.py
import torch
import jcmsuite
from jcmsuite.torch import RealDerivative
height = torch.tensor(
55.0,
dtype=torch.float64,
requires_grad=True,
)
results = jcmsuite.torch.solve(
"project.jcmp",
keys={
"h": height,
"width": 31.0,
"swa": 88.0,
"radius": 8.0,
"permittivity": 1.967 + 4.443j,
},
key_derivative_map={
"h": RealDerivative("height"),
},
)
field = results.postprocesses[0].to_dict()["ElectricFieldStrength"][0][0][0]
real_torch_height_grad = torch.autograd.grad(
field.real,
height,
retain_graph=True,
)[0]
imag_torch_height_grad = torch.autograd.grad(
field.imag,
height,
retain_graph=True,
)[0]
# Compare against the solver results
ordinary_result = results.untorch()
ordinary_data = ordinary_result.postprocesses[0].to_dict()
ordinary_height_grad = ordinary_data["d_height"]["ElectricFieldStrength"][0][0][0]
print("Height gradient comparison:")
print((real_torch_height_grad - ordinary_height_grad.real).abs())
print((imag_torch_height_grad - ordinary_height_grad.imag).abs())
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
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
}
}
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]
}
}
}
}