Bind a Complex Project Key
This tutorial connects a complex Torch project key to the solver derivatives for the real
and imaginary input components. It compares the PyTorch packed gradients with the ordinary
SolveResult.
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/complex_derivative/run.py
import torch
import jcmsuite
from jcmsuite.torch import ComplexDerivative
permittivity = torch.tensor(
1.967 + 4.443j,
dtype=torch.complex128,
requires_grad=True,
)
results = jcmsuite.torch.solve(
"project.jcmp",
keys={
"h": 55.0,
"width": 31.0,
"swa": 88.0,
"radius": 8.0,
"permittivity": permittivity,
},
key_derivative_map={
"permittivity": ComplexDerivative(
real="permittivity_real",
imaginary="permittivity_imag",
),
},
)
field = results.postprocesses[0].to_dict()["ElectricFieldStrength"][0][0][0]
real_torch_permittivity_grad = torch.autograd.grad(
field.real,
permittivity,
retain_graph=True,
)[0]
imag_torch_permittivity_grad = torch.autograd.grad(
field.imag,
permittivity,
retain_graph=True,
)[0]
# Compare against the solver results
ordinary_result = results.untorch()
ordinary_data = ordinary_result.postprocesses[0].to_dict()
ordinary_permittivity_real_grad = ordinary_data["d_permittivity_real"][
"ElectricFieldStrength"
][0][0][0]
ordinary_permittivity_imag_grad = ordinary_data["d_permittivity_imag"][
"ElectricFieldStrength"
][0][0][0]
# PyTorch represents the gradient of a real scalar with respect to a complex input as
# d/d(real) + 1j * d/d(imaginary). Please see the documentation for an explanation of
# this.
expected_real_permittivity_grad = (
ordinary_permittivity_real_grad.real + 1j * ordinary_permittivity_imag_grad.real
)
expected_imag_permittivity_grad = (
ordinary_permittivity_real_grad.imag + 1j * ordinary_permittivity_imag_grad.imag
)
print((real_torch_permittivity_grad - expected_real_permittivity_grad).abs())
print((imag_torch_permittivity_grad - expected_imag_permittivity_grad).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
DerivativeParameter {
Name = "permittivity_real"
TreeEntry {
Path = "Material_3/RelPermittivity"
Scaling = (1, 0)
}
}
DerivativeParameter {
Name = "permittivity_imag"
TreeEntry {
Path = "Material_3/RelPermittivity"
Scaling = (0, 1)
}
}
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]
}
}
}
}