Templating
Use templating when a project or a project support file
must contain values that change between solves. A template contains JCM text
and optional Python blocks. jcmsuite renders the template before the solver
or JCMgeo uses the generated file.
For solve input shapes, see Solving Projects. For geometry templates, see Geo.
Choose A Template File
Use the template extension that matches the generated JCM file:
Template |
Generated file |
Use |
|---|---|---|
|
|
|
|
|
Project entrypoint |
For example, layout.jcmt renders to layout.jcm. A project template named
project.jcmpt renders to project.jcmp.
Substitute Project Keys
Use a placeholder in a template. A placeholder starts with %( and ends with a
format letter, such as e. The name inside the placeholder must match a project
key passed with keys=:
Radius = %(radius)e
Pass the values to solve with keys=:
import jcmsuite
result = jcmsuite.solve(
"project.jcmp",
keys={"radius": 0.33},
)
The solve call renders the selected project support
files before it runs the project.
The source template remains unchanged and is available for the next solve.
The renderer supports these replacement formats:
Value type |
Placeholder |
Format Code |
Output format |
|---|---|---|---|
Integer |
|
|
Decimal integer |
Floating-point |
|
|
Floating-point value |
Complex |
|
|
JCMsuite complex value |
String |
|
|
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 And Matrices
Numeric project keys can be Python lists, tuples, or NumPy arrays. A list is a
sequence in square brackets, such as [0.0, 1.0, 0.0]. A one-dimensional value
renders as a JCM vector. A two-dimensional value renders as a JCM matrix. For
an integer placeholder, every array element must be an integer.
GlobalPosition = %(position)e
RelPermittivity = %(permittivity)e
keys = {
"position": [0.0, 1.0, 0.0],
"permittivity": [[1.0, 0.0], [0.0, 1.0]],
}
Use Nested Project Keys
Project keys can also contain nested values. A dictionary groups named values in
braces, such as {"material": {"permittivity": 2.3104}}. Use a colon-separated
path in the placeholder to select a nested project key. Each path component
selects one named value, from left to right. Every component before the final
component must select another dictionary:
RelPermittivity = %(material:permittivity)e
keys = {
"material": {
"permittivity": 2.3104,
},
}
Use the complete path in the placeholder for a value nested more than one
level. The following placeholder selects
keys["material"]["substrate"]["permittivity"]:
RelPermittivity = %(material:substrate:permittivity)e
keys = {
"material": {
"substrate": {
"permittivity": 2.3104,
},
},
}
Inside a Python block, use square brackets and the value name instead of a colon path:
<?
permittivity = keys["material"]["substrate"]["permittivity"]
?>
If a path component is missing, or if an intermediate value is not a dictionary,
the renderer raises TemplateMappingError.
Use complete project keys for every value that changes the rendered input. This keeps a resultbag entry associated with the inputs that produced its result.
For a runnable example, see Keyword Substitution.
Render During A Solve
solve accepts a .jcmp project or a .jcmpt project template.
For a .jcmp project, solve renders the selected .jcmt project
support files when you pass project keys or a JCMT pattern. For example,
solve("project.jcmp", keys={"radius": 0.33}) renders layout.jcmt as
layout.jcm before the solver uses the project.
For a .jcmpt project, solve renders the project template as .jcmp.
It also renders companion templates.
Use jcmt_pattern= to select suffixed project support files. For example,
jcmt_pattern="fine" selects layout.fine.jcmt or layout.fine.jcm before
the unsuffixed layout.jcmt or layout.jcm.
Use temporary=True when the rendered files are temporary solve inputs. Use
working_dir= when you must keep the rendered files in a selected directory.
Render A Template Directly
Use jcmt2jcm when you need a rendered file without
running a solve:
from jcmsuite.templates import jcmt2jcm
rendered_layout = jcmt2jcm(
"layout.jcmt",
keys={"radius": 0.33},
outputfile="rendered_layout.jcm",
)
The jcmt2jcm function returns the absolute path of the
rendered file. Pass keys=None
or omit keys when the template does not use external values. Pass
outputfile= to select the generated file path. If you omit outputfile=,
jcmt2jcm removes the final t from .jcmt or .jcmpt.
The function accepts only .jcmt and .jcmpt input files. It raises
InvalidConfigurationError when the
input file does not exist or has an unsupported extension. It raises
TemplateMappingError when a required
value is missing from project keys.
Use Python Blocks
Use <? ... ?> or <?Python ... ?> blocks for calculations and repeated JCM
text. The template receives project keys through keys. A Python block can add
project keys for later substitutions. numpy is available in each Python
block. Text outside a Python block remains JCM input text:
<?Python
wave_number = 2 * 3.14159 / keys["wavelength"]
keys["wave_number"] = wave_number
?>
K = [%(wave_number)e 0 0]
Use Python blocks to calculate values such as refractive-index-dependent permittivities, wave vectors, and polarization vectors. Keep direct project key substitutions for values that do not require a calculation.
Repeat JCM Text With Loops
Use a Python for loop when the template must generate repeated JCM blocks.
Close the Python block before the JCM text that the loop repeats. Open another
Python block at the same indentation level to continue or close the loop.
<?
for rod_index in keys["rod_indices"]:
keys["rod_index"] = rod_index
?>
Circle {
Name = "Rod %(rod_index)d"
}
<?
?>
Warning
Close every Python loop in a later <? ... ?> block. Indent the first ?>
tag at the same level as the statements in the active loop body. In the
example, the first ?> has four spaces before it. The final ?> closes the
for loop.
Nested loops can generate two-dimensional arrays. Project keys can control the number of loop iterations, so one template can generate multiple geometry sizes.
Render Geometry Templates
Use geo with the same project keys to inspect a rendered
layout.jcmt before you run an electromagnetic solve:
import jcmsuite
jcmsuite.geo(
project_dir="examples/mie2d",
keys={"radius": 0.5},
show=True,
)
The call renders the selected layout template, creates grid.jcm, and requests
JCMview to open the grid. The call does not run the electromagnetic solve.
JCMview requires a graphical environment.
Next Steps
Use Solving Projects to choose the results that
solvereturns.Use Geo to render a geometry template before mesh generation.
Use Keyword Substitution for a complete template project example.
Use Python Code Snippets for template calculations and Loops for generated geometry.