[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
In this final example we will illustrate how we can simplify the definition of non-trivial structures by using Python functions, rather than explicitly specifying the refractive index profile.
The following code shows how we automatically generate a staircase approximation of a parabolic refractive index profile.
#!/usr/bin/env python #################################################################### # # Parabolic refractive index profile. # #################################################################### from camfr import * set_lambda(1) set_N(20) set_polarisation(TE) w = 5.0 # width of waveguide set_upper_PML(-0.1) set_lower_PML(-0.1) # Define parabolic refractive index profile. def index(x): max_n = 3.5 # max refractive index a = 0.1 # slope n = max_n - a * pow(w / 2.0 - x, 2) if n < 1: return 1 else: return n # Construct a staircase approximation. expr = Expression() materials = [] steps = 10 for i in range(steps): x = i * w / steps m = Material(index(x + 0.5 * w / steps)) materials.append(m) d = w / steps expr.add(m(d)) slab = Slab(expr) # Compare continuous and staircase profile. outfile = file("tutorial7.out",'w') steps2 = 100 for i in range(steps2): x = i * w / steps2 print >> outfile, x, index(x), slab.n(Coord(x, 0, 0)).real outfile.close() |
The lines after def index(x):
define a function which takes as argument
the x
coordinate and returns the refractive index at this position in
a certain parabolic refractive index profile. This code also illustrates the
use of conditionals, and stresses once again the importance of indentation in
Python.
The next block of code creates the expression expr
, which will describe
the staircase approximation of the index profile. The width of the waveguide
will be divided in steeps
piecewise constant parts.
First, an empty expression is created: expr = Expression()
. This
expression is gradually filled in a loop while i
runs from 0 to
steps-1
.
A material m
is created with the refractive index from the middle of
the current step of the index profile.
Finally, a term is added to the expression which describes the current step,
consisting of material m
with thickness d
:
expr.add(m(d))
.
At the end of the loop, this expression is used to define a slab, which can
be used in subsequent calculations : slab = Slab(expr)
.
Very important to notice is that we keep hold of the materials m
we
create in the loop, by first defining an empty list materials = []
,
to which we add each material: materials.append(m)
.
The reason for this lies in Python's garbage collection. Without the list
materials
to hold on to the materials, Python will see m
as
just a temporary variable, which it will deallocate and garbage collect after
the loop has finished. Since we still need these materials in the subsequent
calculations, we have to tell Python not to free them by storing them in a
list at the top level in the source code.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] |
This document was generated by root on August, 27 2008 using texi2html 1.76.