Loops

In this section we show how to exploit loops (for, while) within Matlab code snippets to define complicated geometries or project setups in a few number of script lines.

Loops within an embedded Matlab code block (cf. previous section Matlab Code Snippets) may extend over several blocks:

 1<?
 2for centerX = [0 : 1 : 9]
 3  keys.center = [centerX 0.0]
 4?>
 5
 6Circle {
 7  Radius = 0.5
 8  GlobalPosition = %(center)e
 9  ...
10}
11
12<?
13end 
14?>

Here, a for-loop starts in line 2 within the first Matlab block. This loop is closed in line 13 after a .jcm code block (lines 6-10). Line 3 dynamically sets the value for key center, which serves as circle’s center in line 8. This way, we have defined ten circles, equally spaced in x-direction.

To see how this works in practice, we want to compute the light scattering off multiple rods aligned on a hexagonal lattice. Figure “Multiple rod geometry” shows the setup.

_images/phc_geometry.png

Multiple rod geometry

\pvec{g}_1 and \pvec{g}_2 are called grid vectors. For a hexagonal alignment we have

\begin{eqnarray*}
\pvec{g}_1 = a\cdot\left(
\begin{array}{c}
0.0 \\
1.0
\end{array}
\right), \;
\pvec{g}_2 = a\cdot \left(
\begin{array}{c}
\sin(60^\circ) \\
\cos(60^\circ)
\end{array}
\right),
\end{eqnarray*}

with a lattice parameter a, which is passed as a further parameter in the driver script:

 1%% set problem parameter
 2keys.radius = 0.3; 
 3keys.n_air = 1.0;
 4keys.n_glass = 1.52;
 5keys.lambda_0 = 0.550; % in um 
 6keys.polarization = 45; % in degree
 7keys.a = 1.0;
 8keys.n_rods = [3 3];
 9  
10%% run the project
11results = jcmwave_solve('mie2D.jcmp', keys);
12
13%% get scattering cross section
14scattering_cross_section = ...
15  results{2}.ElectromagneticFieldEnergyFlux{1};
16fprintf('\nscattering cross section: %.8g\n', ...
17        real(scattering_cross_section)); 
18
19%% plot exported cartesian grid within matlab:
20cfb = results{3};
21amplitude = cfb.field{1};
22intensity = sum(conj(amplitude).*amplitude, 3);
23pcolor(cfb.X, cfb.Y, intensity); 
24shading interp; view(0, 90); axis equal;

In line 8 we set the number of rods in x- and y- direction. Figure “Intensity” shows the computed intensity of the electric field.

_images/phc_intensity.png

Intensity

Pseudo-color intensity plot as produced by the driver run.m.

For this problem you again find a driver run_geo.m which only runs the mesh generation:

%% set geometry parameters
keys.radius = 0.3;
keys.a = 1.0;
keys.n_rods = [3 3];

%% generate mesh file only
jcmwave_geo('.', keys);

%% open grid.jcm in JCMview
jcmwave_view('grid.jcm');

You can use this script to “play” with the geometry parameters and to watch how the geometry is updated.

In the following we want to discuss the updated layout file:

 1<?
 2  % compute grid vectors for hexagonal grid
 3  gv1 = [0.0 keys.a];  
 4  gv2 = [sind(60) cosd(60)]*keys.a;
 5
 6  % compute computational domain enclosing all scatterer
 7  maxX = (keys.n_rods(1)-1)*gv2(1);
 8  maxY = (keys.n_rods(2)-1)*gv1(2);
 9    
10  keys.computational_domain_X = maxX+2*keys.radius+2;
11  keys.computational_domain_Y = maxY+2*keys.radius+2;     
12?>
13
14
15Layout2D {
16  UnitOfLength = 1e-6
17
18  MeshOptions {
19    MaximumSideLength = 0.1
20    CurvilinearDegree = 2
21  }
22
23  Objects {
24    # Computational domain
25    Parallelogram {
26      DomainId = 1
27      Width = %(computational_domain_X)e
28      Height = %(computational_domain_Y)e
29  
30      # set transparent boundary conditions
31      Boundary{
32        Class = Transparent
33      }
34    }
35  
36<?
37  center_array = [maxX/2 maxY/2];
38  for iX=0 : keys.n_rods(1)-1
39    col_start  = [iX*gv2(1) mod(iX, 2)*gv2(2)];
40    for iY=0 : keys.n_rods(2)-1-mod(iX, 2);
41      keys.center = col_start+iY*gv1-center_array;
42     
43      
44?>
45    # Scatterer (rod)
46    Circle {
47      DomainId = 2
48      Radius = %(radius)e
49      GlobalPosition = %(center)e
50    }
51  
52<?
53    end
54   end
55?>
56  }
57}
58
59
60 

The first Matlab block computes the grid vectors \pvec{g}_1, \pvec{g}_2 (lines 2-4), and adapts the computational domain size to enclose all rods (lines 6-11). There, maxX and maxY are the dimensions of the array of rods in x and y.

The Matlab block from lines 34-42 defines two for loops over the number of rods in x and y. Line 39 sets the center of the current rod, which is used in line 46 in the enclosed .jcm block (center_array is used to shift the center of the array of rods to the origin). The for loops are closed in the last Matlab block (lines 51-54).