Keyword Substitution

Use project keys to parameterize the fixed project from Project Setup. The rod radius changes between solves. The other project inputs remain unchanged. Later pages use the same mechanism for parameter scans and more complex template calculations.

Create A Template

Rename layout.jcm to layout.jcmt to create a template project support file. Replace the fixed rod radius with a printf-style placeholder:

    # Scatterer (rod)
    Circle {
      Name = "Rod"
      DomainId = 2
      Priority = 1
      Radius = %(radius)e
      RefineAll = 4
    }

The name inside the placeholder is the project key name. The e and f format codes accept numeric values. When solve receives project keys, JCMsuite renders layout.jcmt as layout.jcm. JCMsuite then runs the project. The source template remains unchanged for the next solve.

The template renderer supports these replacement formats:

Value type

Placeholder

Format Code

Output format

Integer

%(count)d or %(count)i

d or i

Decimal integer

Floating-point

%(radius)e or %(radius)f

e or f

Floating-point value

Complex

%(permittivity)g

g

JCMsuite complex value (real, imaginary)

String

%(name)s

s

String value

The g format code formats complex values. For a complex scalar, it renders the real and imaginary parts as a JCMsuite complex pair. For example, 1.967 + 4.443j renders as (1.967, 4.443). Numeric vectors and matrices can also contain complex values. Each complex element uses the same pair representation.

Numeric placeholders can include an optional one- or two-digit precision modifier before the format code. The modifier controls the precision used by the e, f, and g formats:

Wavelength = %(wavelength)6e
Radius = %(radius)3f
Permittivity = %(permittivity)8g

Without a modifier, the renderer uses its default numeric precision. The modifier is a precision value, not a project key name or a field width.

Use Arrays

Numeric project keys accept Python lists, tuples, and NumPy arrays. A one-dimensional value renders as a JCM vector. A two-dimensional value renders as a JCM matrix. For integer placeholders, every element must be an integer.

For example, use these template placeholders:

GlobalPosition = %(position)e
RelPermittivity = %(permittivity)e

Then provide a vector and a matrix in the project keys:

keys = {
    "position": [0.0, 1.0, 0.0],
    "permittivity": [[1.0, 0.0], [0.0, 1.0]],
}

Use Nested Project Keys

Project keys can contain nested values. Use a colon-separated path in the placeholder to select a nested project key. See Templating for path resolution rules and multi-level examples.

RelPermittivity = %(material:permittivity)e

Provide the nested project keys in keys:

keys = {
    "material": {
        "permittivity": 2.3104,
    },
}

See Templating for the complete template workflow.

Run The Project

from matplotlib import pyplot as plt

import jcmsuite

# set geometry parameter
keys = {"radius": 0.3}

# run the project
results = jcmsuite.solve("mie2D.jcmp", keys=keys)

# Access the results of the first postprocess and export it to a dictionary
scattering_postprocess = results.postprocesses[0]
scattering_export = scattering_postprocess.to_dict()
cross_section = scattering_export["ElectromagneticFieldEnergyFlux"][0][0].real

print(f"scattering cross section: {cross_section}")

# Access the field export of the second postprocess
field_export = results.postprocesses[1].fields

# Select the first field intensity and export it to an xarray Dataset
xarray_export = field_export.intensity.select(0).to_xarray()

xx = xarray_export["X"]
yy = xarray_export["Y"]
intensity = xarray_export["field"]

plt.pcolor(xx, yy, intensity, shading="auto")
plt.show()

The project keys supply the rod radius as r = 0.3\,\mathrm{\mu m}. solve renders the layout template with the radius value and returns a SolveResult. The script prints the exterior energy flux. The script also displays the intensity of the exported electric field.

Inspect The Geometry

Use geo with the same project keys to inspect the rendered mesh before you run the electromagnetic solve:

import jcmsuite

keys = {"radius": 0.3}

# Create the mesh and view it
results = jcmsuite.geo(".", keys=keys, show=True)

The call renders layout.jcmt, generates grid.jcm, and opens the grid in JCMview.

Project Files

run.py
from matplotlib import pyplot as plt

import jcmsuite

# set geometry parameter
keys = {"radius": 0.3}

# run the project
results = jcmsuite.solve("mie2D.jcmp", keys=keys)

# Access the results of the first postprocess and export it to a dictionary
scattering_postprocess = results.postprocesses[0]
scattering_export = scattering_postprocess.to_dict()
cross_section = scattering_export["ElectromagneticFieldEnergyFlux"][0][0].real

print(f"scattering cross section: {cross_section}")

# Access the field export of the second postprocess
field_export = results.postprocesses[1].fields

# Select the first field intensity and export it to an xarray Dataset
xarray_export = field_export.intensity.select(0).to_xarray()

xx = xarray_export["X"]
yy = xarray_export["Y"]
intensity = xarray_export["field"]

plt.pcolor(xx, yy, intensity, shading="auto")
plt.show()
run_geo.py
import jcmsuite

keys = {"radius": 0.3}

# Create the mesh and view it
results = jcmsuite.geo(".", keys=keys, show=True)
layout.jcmt
Layout2D {
  UnitOfLength = 1e-6

  MeshOptions {
    MaximumSideLength = 0.1
    CurvilinearDegree = 2
  }

  Objects {
    # Computational domain
    Parallelogram {
      Name = "ComputationalDomain"
      DomainId = 1
      Priority = ComputationalDomain
      Width = 4
      Height = 4

      Boundary {
        Class = Transparent
      }
    }

    # Scatterer (rod)
    Circle {
      Name = "Rod"
      DomainId = 2
      Priority = 1
      Radius = %(radius)e
      RefineAll = 4
    }
  }
}
materials.jcm
# material properties for the surroundings (air)
Material {
  DomainId = 1
  RelPermittivity = 1.0
  RelPermeability = 1.0
}

# material properties the glass rod
Material {
  DomainId = 2
  RelPermittivity = 2.3104 # 1.52^2
  RelPermeability = 1.0
}
mie2D.jcmp
Project {
  InfoLevel = 3
  Electromagnetics {
    TimeHarmonic {
      Scattering {
        FieldComponents = Electric
        Accuracy {
          FiniteElementDegree = 3
          Refinement {
            MaxNumberSteps = 0
            PreRefinements = 0
          }
        }
      }
    }
  }
}
# computes the energy flux of the scattered field into the exterior domain
PostProcess {
  FluxIntegration {
    FieldBagPath = "./mie2D_results/fieldbag.jcm"
    OutputFileName = "./mie2D_results/energyflux_scattered.jcm"
    OutputQuantity = ElectromagneticFieldEnergyFlux
    InterfaceType = ExteriorDomain
  }
}

# exports computed field on cartesian grid
PostProcess {
  ExportFields {
    FieldBagPath = "./mie2D_results/fieldbag.jcm"
    OutputFileName = "./mie2D_results/fieldbag_cartesian.jcm"
    Cartesian {
      NGridPointsX = 200
      NGridPointsY = 200
    }
  }
}
sources.jcm
SourceBag {
  Source {
    ElectricFieldStrength {
      PlaneWave {
        K = [1.14239732857811e7 0.0 0.0]
        Amplitude = [0 0 1]
      }
    }
  }
}

Next Step

Continue to Parameter Scan to solve the project for many radius values.