# Loops Use loops in template Python blocks to create repeated JCM input text. This page creates a staggered hexagonal array of glass rods. The {term}`project keys` control the rod radius, lattice spacing, and rod counts. Loops reduce repeated JCM input text when a project contains many similar objects. One `for` loop can generate one JCM block for each rod. Nested loops can generate two-dimensional arrays. Project keys can control the number of loop iterations, so one template can create multiple geometry sizes. ## Define The Geometry The lattice uses these grid vectors: $$ \mathbf{g}_1 = a \begin{pmatrix} 0 \\ 1 \end{pmatrix}, \qquad \mathbf{g}_2 = a \begin{pmatrix} \sin(60^\circ) \\ \cos(60^\circ) \end{pmatrix}, $$ where $a$ is the lattice spacing. The template uses the grid vectors to place each rod and to calculate the width and height of the computational domain. ```{figure} ../_static/getting_started/loops/phc_geometry.png :alt: Staggered hexagonal array of glass rods. Staggered hexagonal array of glass rods. ``` The script defines a rod radius of $r = 0.3\,\mathrm{\mu m}$, a lattice spacing of $a = 1.0\,\mathrm{\mu m}$, and rod counts of $[3, 3]$: ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/run.py :language: python :lines: 6-15 ``` The first and third columns contain three rods. The staggered second column contains two rods. The template therefore creates eight rods. ## Continue Loops Across Template Blocks Start a Python loop in a `` before the JCM text that the loop must repeat. Open another ` 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. ``` The complete `layout.jcmt` template follows. The first Python block calculates the grid vectors and the computational-domain dimensions. The static `Layout2D` text defines the domain and its transparent boundary. The second Python block starts the nested loops that generate the rod geometry: ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/layout.jcmt :language: jcmt ``` The opening Python block sets `grid_vector_1`, `grid_vector_2`, `max_x`, and `max_y`. The block also sets the `computational_domain_X` and `computational_domain_Y` project keys that define the `Parallelogram` size. The nested loops calculate each rod centre. Each loop iteration sets the `center` and `rod_index` project keys. The indented `?>` tag transfers control to the JCM `Circle` block, which uses those project keys. The final Python block closes both loops. ## Run The Project ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/run.py :language: python ``` {func}`solve ` renders the template {term}`project support files ` and returns a {class}`SolveResult `. The script exports the first postprocess data as a dictionary and prints the exterior energy flux. The script selects the first exported field from the second postprocess and displays its intensity. ```{figure} ../_static/getting_started/loops/phc_intensity.png :alt: Field-intensity plot around the rod array. Field intensity around the rod array. ``` The script requires JCMsuite, an available license, and `matplotlib`. ## Inspect The Geometry Run `run_geo.py` before a solve to inspect the rendered rod array. The script uses the radius, lattice spacing, and rod counts from its project keys. JCMgeo creates `grid.jcm` and JCMview opens the grid. The script does not run the electromagnetic solve. ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/run_geo.py :language: python ``` JCMview requires a graphical environment. ## Project Files
run.py ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/run.py :language: python ```
run_geo.py ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/run_geo.py :language: python ```
layout.jcmt ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/layout.jcmt :language: jcmt ```
materials.jcmt ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/materials.jcmt :language: jcmt ```
mie2D.jcmp ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/mie2D.jcmp :language: jcmt ```
sources.jcmt ```{literalinclude} ../../demos/documentation_tutorials/getting_started/loops/sources.jcmt :language: jcmt ```
## Next Step Continue to [Parallel Parameter Scan](parallel_parameter_scan.md) to run the radius scan with a local daemon resource.