Inspect Second-Order Solver Derivatives

Use one ordinary solve to select first, diagonal second, and mixed derivatives. This example does not use Torch.

Each tutorial folder contains its own project and support files. You can copy the folder and run it separately. The scripts use "project.jcmp"; jcmsuite resolves this path relative to the script, not the shell’s current directory.

Run The Example

You need an installed jcmsuite package, NumPy, pandas, and a configured JCMsuite runtime with a solver license. From the repository root, run:

python demos/documentation_tutorials/derivatives/setup_and_export/second_order/run.py

The script prints Fourier derivatives, Fourier quantity names, and a scattering table. The substrate permittivity is 2.0 + 0.2j, and the line permittivity is 1.4. The numbers illustrate the workflow; they are not accuracy assertions.

"""Inspect first, diagonal second, and mixed solver derivatives without Torch."""

import jcmsuite

result = jcmsuite.solve(
    "project.jcmp",
    keys={"permittivity": 2.0 + 0.2j, "line_permittivity": 1.4},
    temporary=True,
)

# Numeric records use solver parameter names, including both complex coordinates.
fourier = result.postprocesses[0]
first = fourier.derivatives["permittivity_real"]
diagonal = first.derivatives["permittivity_real"]
fourier_pair = first.derivatives["permittivity_imag"]
print("First derivative:", first.to_dict())
print("Diagonal second derivative:", diagonal.to_dict())
print("Fourier mixed derivative quantities:", list(fourier_pair.to_dict()))
scattering = result.postprocesses[1]
scattering_pair = scattering.derivatives["permittivity_real"].derivatives[
    "permittivity_imag"
]
print("Scattering mixed derivative:\n", scattering_pair.to_df().head())

What To Notice

  • Order = 2 requests second-order solver data.

  • .derivatives[first].derivatives[second] selects a numeric postprocess parameter pair. Ordinary views use the solver parameter names declared in materials.jcmt. Missing pairs are not replaced by zeros.

The geometry is fixed. The derivative directions change the real and imaginary parts of the substrate permittivity and the line permittivity. The small mesh and export grid keep the demonstration quick; they are not a convergence study.

To compute second derivatives of a Torch objective, continue with the Torch numeric postprocess tutorial.

Project Files

project.jcmp
Project {
  Derivatives {
    Order = 2
  }
  InfoLevel = 1
  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"
  }
}
materials.jcmt
DerivativeParameter {
  Name = "line_permittivity"
  TreeEntry = "Material_2/RelPermittivity"
}
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
}
Material {
  Name = "Material_2"
  DomainId = 2
  RelPermittivity = %(line_permittivity)g
  RelPermeability = 1
}
Material {
  Name = "Material_3"
  DomainId = 3
  RelPermittivity = %(permittivity)g
  RelPermeability = 1
}
layout.jcm
Layout2D {
  Name = "Line"
  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 = 55
      Width = 31
      InnerAngles = [88 88]
      Port = South
      Alignment {
        Parent {
          Domain = "Substrate"
          Port = North
        }
        Orientation = AntiParallel
      }
      CornerRounding {
        Point = [3 4]
        Radius = 8
        NPoints = 5
      }
    }
  }
}
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]
      }
    }
  }
}