Export Torch Result Data

This tutorial uses TorchPostprocessRecord.to_dict to export structured result data as a dictionary of Torch tensors. The dictionary omits solver derivative metadata.

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/table_export/run.py
"""Export a structured Torch postprocess without derivative metadata."""

import torch

import jcmsuite
from jcmsuite.torch import RealDerivative

height = torch.tensor(55.0, dtype=torch.float64)
width = torch.tensor(31.0, dtype=torch.float64, requires_grad=True)
sidewall_angle = torch.tensor(88.0, dtype=torch.float64, requires_grad=True)

results = jcmsuite.torch.solve(
    "project.jcmp",
    keys={
        "h": height,
        "width": width,
        "swa": sidewall_angle,
        "radius": 8.0,
        "permittivity": 1.967 + 4.443j,
    },
    temporary=True,
    key_derivative_map={
        "width": RealDerivative("width_parameter"),
        "swa": RealDerivative("swa"),
    },
)

values = results.postprocesses[0].to_dict()
print(values.keys())
assert "header" not in values
assert not any(str(key).startswith("d_") for key in values)
assert torch.is_tensor(values["ElectricFieldStrength"][0])

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]
      }
    }
  }
}