Keyword Substitution

In this section we parameterize the test problem of the previous section Project Setup. We want to vary the radius of the scatterer (the glass rod). For this, we rename the file layout.jcm to layout.jcmt to indicate that this file is a template .jcm file. We then replace line 25:

22    # Scatterer (rod)
23    Circle {
24      DomainId = 2
25      Radius = %(radius)e
26    } 

We give details on the syntax of this keyword substitution below. First we want to see how to use the parameterization in the driver script run.py:

 1import jcmwave
 2
 3# set geometry parameter
 4keys = {'radius':  0.3}
 5
 6# run the project
 7results = jcmwave.solve('mie2D.jcmp', keys=keys)
 8
 9# get scattering cross section
10scattering_cross_section = results[1]['ElectromagneticFieldEnergyFlux'][0][0]
11print ('\nscattering cross section: %.8g\n' % scattering_cross_section.real )
12
13# plot exported cartesian grid within python
14cfb = results[2]
15amplitude = cfb['field'][0]
16intensity = (amplitude.conj()*amplitude).sum(2).real 
17
18from matplotlib.pyplot import *
19pcolormesh(cfb['X'], cfb['Y'], intensity, shading='gouraud') 
20axis('tight')
21gca().set_aspect('equal')
22gca().xaxis.major.formatter.set_powerlimits((-1, 0))
23gca().yaxis.major.formatter.set_powerlimits((-1, 0))
24show()

In line 4 we declare the actual parameter value. All parameter values (here it is only one) are gathered in the dictionary keys. In line 7, the key-value list containing the project parameter is passed to jcmwave.solve, which runs the project for the given set of parameters.

As in the previous project, you find a driver run_geo.py which only runs the mesh generation:

import jcmwave

# set geometry parameter
keys = {'radius':  0.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 radius parameter and to watch how the geometry is updated.

Syntax

Within a .jcmt file a template parameter is defined with the following type-dependent syntax:

  • %(key)d for integer parameters (also valid for arrays)

  • %(key)e for double parameters (also valid for complex double arrays)

  • %(key)s for string parameters.

key is a user-defined name of the parameter. With the syntax %(key)10e one further may set the floating point precision (here 10 digits) for a numerical parameter.

The actual parameter values must be passed to jcmwave_solve by a Python dictionary. For example, when the .jcmt input files contain the following placeholders,

%(c)e
%(lambda)d
%(k)s
%(source.eVector)e
%(material.permittivity)e

the following definitions are appropriate:

keys = {
   'c': 2.99792458e8,
   'lambda': 550e-9}
keys['k'] = 2*pi/keys['lambda']
keys['source'] = {'eVector': [0, 0, 1j]}
keys['material'] = {'permittivity': [1, 1e-2, 0,
                                     1e-2, 1, 0,
                                     0, 0, 1]}

The next section Parameter Scan shows how the parameterization is used to perform parameter scans.

Updated Input Files

  • layout.jcmt [ASCII]

     1Layout2D {
     2  UnitOfLength = 1e-6
     3  
     4  MeshOptions {
     5    MaximumSideLength = 0.1
     6    CurvilinearDegree = 2
     7  }
     8  Objects {
     9    
    10    # Computational domain
    11    Parallelogram {
    12      DomainId = 1
    13      Width = 4
    14      Height = 4
    15      
    16      # set transparent boundary conditions
    17      Boundary{
    18        Class = Transparent
    19      }
    20    }
    21    
    22    # Scatterer (rod)
    23    Circle {
    24      DomainId = 2
    25      Radius = %(radius)e
    26    } 
    27  }
    28}