emopt.grid

Python interface for grid smoothing code.

Grid smoothing refers to the process of mapping a continuously-defined shapes with sharp boundaries to a rectangular grid in a continuous manner. This process results in a rectangular grid whose material distribution matches the defined shape except at the boundaries where the effective material value is between the material value within the bound shape and that of the surrounding medium, hence making the boundaries appear as if they have been ‘smoothed’ out.

Ensuring that this mapping from ‘real’ space to the discretized rectangular grid is continuous (i.e. small changes in the underlying boundaries produce small changes in the material distribution of the grid) is very important to sensitivity analysis. Gradients computed using an adjoint method, in particular, will be inaccurate if changes to the material distribution occur in discrete jumps which are too large.

Grid smoothing can be accomplished in a variety of ways. The implementation here is very general for sets of shapes which do not require changes in topology (creation and elimination of holes, etc). It relies on representing boundaries using polygons and then computing the smoothed grid by applying a series of boolean subtraction operations (in c++).

class emopt.grid.Circle(x0, y0, r)

Bases: emopt.grid.MaterialPrimitive

Define a circle primitive.

Notes

This is not fully implemented.

Todo

Actually fully implement this.

get_r()
get_x0()
get_y0()
set_material(mat)
set_position(x0, y0)
set_radius(r)
class emopt.grid.ConstantMaterial2D(value)

Bases: emopt.grid.Material2D

A uniform constant material.

Parameters:value (complex) – The constant material value.
material_value

The constant material value

Type:complex
material_value
class emopt.grid.ConstantMaterial3D(value)

Bases: emopt.grid.Material3D

A uniform constant 3D material.

Parameters:value (complex) – The constant material value.
material_value

The constant material value

Type:complex
material_value
class emopt.grid.GridMaterial2D(M, N, grid)

Bases: emopt.grid.Material2D

Define a simple rectangular-grid-based Material distribution.

This is the simplest form of Material object which defines a material distribution as a simple rectangular grid. On its own, there is no grid smoothing performed by a GridMaterial, however it can be used to implement custom grid smoothing subroutines.

Parameters:
  • M (int) – Number of rows in the grid.
  • N (int) – Number of columns in the grid.
  • grid (numpy.ndarray) – The complex (dtype=np.complex128) material distribution
M

The number of rows in the grid.

Type:int
N

The number of cols in the grid.

Type:int
grid

The complex material distribution.

Type:numpy.ndarray
M
N
grid
class emopt.grid.GridMaterial3D(X, Y, Z, Nx, Ny, Nz, grid)

Bases: future.types.newobject.newobject

get_value(k, j, i)
class emopt.grid.Material2D

Bases: future.types.newobject.newobject

Define a general interface for 2D Material distributions.

Notes

Currently material distributions only support 2D systems.

get_value(self, x, y)

Get the value of the material distribution at (x,y)

get_values(self, m1, m2, n1, n2, arr=None)

Get the values of the material distribution within a set of array indicesa set of array indices.

get_values_on(self, domain)

Get the values of the material distribution within a domain.

get_value(x, y)

Get the value of the material distribution at (x,y).

Parameters:
  • x (int or float) – The (fractional) x index.
  • y (int or float) – The (fractional) y index.
Returns:

The complex material value at the desired location.

Return type:

complex128

get_values(k1, k2, j1, j2, sx=0.0, sy=0.0, arr=None)

Get the values of the material distribution within a set of array indicesa set of array indices.

Parameters:
  • k1 (int) – The lower integer bound on x of the desired region
  • k2 (int) – The upper integer bound on x of the desired region
  • j1 (int) – The lower integer bound on y of the desired region
  • j2 (int) – The upper integer bound on y of the desired region
  • arr (numpy.ndarray (optional)) – The array with dimension (m2-m1)x(n2-n1) with type np.complex128 which will store the retrieved material distribution. If None, a new array will be created. (optional = None)
Returns:

The retrieved complex material distribution.

Return type:

numpy.ndarray

get_values_in(domain, sx=0, sy=0, squeeze=False, arr=None)

Get the values of the material distribution within a domain.

Parameters:domain (emopt.misc.DomainCoordinates) – The domain in which the material distribution is retrieved.
Returns:The retrieved material distribution which lies in the domain.
Return type:numpy.ndarray
class emopt.grid.Material3D

Bases: future.types.newobject.newobject

Define a general interface for 3D Material distributions.

get_value(self, x, y, z)

Get the value of the material distribution at (x,y,z)

get_values(self, k1, k2, j1, j2, i1, i2, arr=None)

Get the values of the material distribution within a set of array indicesa set of array indices.

get_values_on(self, domain)

Get the values of the material distribution within a domain.

get_value(x, y, z)

Get the value of the material distribution at (x,y,z).

Parameters:
  • x (int or float) – The (fractional) x index.
  • y (int or float) – The (fractional) y index.
  • z (int or float) – The (fractional) z index
Returns:

The complex material value at the desired location.

Return type:

complex128

get_values(k1, k2, j1, j2, i1, i2, sx=0, sy=0, sz=0, arr=None, reshape=True)

Get the values of the material distribution within a set of array indicesa set of array indices.

Parameters:
  • k1 (int) – The lower integer bound on x of the desired region
  • k2 (int) – The upper integer bound on x of the desired region
  • j1 (int) – The lower integer bound on y of the desired region
  • j2 (int) – The upper integer bound on y of the desired region
  • i1 (int) – The lower integer bound on y of the desired region
  • i2 (int) – The upper integer bound on y of the desired region
  • arr (numpy.ndarray (optional)) – The array with dimension (m2-m1)x(n2-n1) with type np.complex128 which will store the retrieved material distribution. If None, a new array will be created. (optional = None)
Returns:

The retrieved complex material distribution.

Return type:

numpy.ndarray

get_values_in(domain, sx=0, sy=0, sz=0, arr=None, squeeze=False)

Get the values of the material distribution within a domain.

Parameters:
  • domain (emopt.misc.DomainCoordinates) – The domain in which the material distribution is retrieved.
  • sx (float (optional)) – The partial index shift in the x direction
  • sy (float (optional)) – The partial index shift in the y direction
  • sz (float (optional)) – The partial index shift in the z direction
  • arr (np.ndarray (optional)) – The array in which the retrieved material distribution is stored. If None, a new array is instantiated (default = None)
  • squeeze (bool (optional)) – If True, eliminate length-1 dimensions from the resulting array. This only affects 1D and 2D domains. (default = False)
Returns:

The retrieved material distribution which lies in the domain.

Return type:

numpy.ndarray

class emopt.grid.MaterialPrimitive

Bases: future.types.newobject.newobject

Define a MaterialPrimitive.

A MaterialPrimitive is a material distribution belonging to shapes like rectangles, circules, polygons, etc.

Todo

Pythonify this function using properties.

contains_point(self, x, y)

Check if a material primitive contains the supplied (x,y) coordinate

layer

The layer of the material primitive. Lower means higher priority in terms of visibility.

Type:int
contains_point(x, y)

Check if a material primitive contains the supplied (x,y) coordinate

Parameters:
  • x (float) – The real-space x coordinate
  • y (float) – The real-space y coordinate
Returns:

True if the (x,y) point is contained within the primitive, false otherwise.

Return type:

bool

get_layer()

Get the layer of the primitive.

Returns:The layer.
Return type:int
layer
set_layer(layer)

Set the layer of the primitive.

Parameters:layer (int) – The new layer.
class emopt.grid.Polygon(xs=None, ys=None)

Bases: emopt.grid.MaterialPrimitive

Np
add_point(x, y)
add_points(x, y)
material_value
static populate_lines(xs, ys, ds)

Populate one or more line segments with points.

This is useful when defining polygons that will be manipulated by an optimization. Given one or more line segments, a new set of line segments is created which are filled with points spaced approximately by ds.

For example, let’s say you start with this line segment:

——————————-

defined by xs=[x1, x2] and ys=[y1,y1]. Using a value of ds = (x2-x1)/4 would yield a new set of line segments:

——-——-——-——-*

Notes

This function assumes that the supplied line segments fit together end-to-end!

Parameters:
  • xs (list or numpy.array) – The list of x coordinates of the connected line segments
  • ys (list or numpy.array) – The list of y coordinates of the connected line segments
  • ds (float) – The approximate point spacing in the new set of line segments
Returns:

Arrays containing the new x and y coordinates

Return type:

numpy.ndarray, numpy.ndarray

set_material(mat)
set_point(index, x, y)
set_points(x, y)
xs
ys
class emopt.grid.Rectangle(x0, y0, xspan, yspan)

Bases: emopt.grid.MaterialPrimitive

Define a rectangular material primitive.

A rectangle is defined by its center position and its width and height.

Parameters:
  • x0 (float) – The x coordinate of the center of the Rectangle
  • y0 (float) – The y coordinate of the center of the Rectangle
  • xspan (float) – The width of the Rectangle
  • yspan (float) – The height of the Rectangle
x0

The x coordinate of the center of the Rectangle

Type:float
y0

The y coordinate of the center of the Rectangle

Type:float
width

The width of the Rectangle

Type:float
height

The height of the Rectangle

Type:float
material_value

The material value of the Rectangle’s interior

Type:complex128
set_material(self, mat)

(Deprecated) Set the material value of the Rectangle’s interior.

set_position(self, x0, y0)

Set the (x,y) position of the center of the Rectangle.

set_width(self, width)

(Deprecated) Set the width of the rectangle.

set_height(self, height)

(Deprecated) Set the height of the rectangle.

height
material_value
set_height(height)

(Deprecated) Set the height of the rectangle.

set_material(mat)

(Deprecated) Set the material value of the Rectangle’s interior.

set_position(x0, y0)

Set the (x,y) position of the center of the Rectangle.

Parameters:
  • x0 (float) – The x coordinate of the center of the Rectangle.
  • y0 (float) – The y coordinate of the center of the Rectangle.
set_width(width)

(Deprecated) Set the width of the rectangle.

width
x0
y0
class emopt.grid.StructuredMaterial2D(w, h, dx, dy)

Bases: emopt.grid.Material2D

Create a 2D material consisting of one or more primitive shapes (rectangles, polyongs, etc)

Notes

When used for defining the material distribution for a simulation, the dimensions supplied will typically match the dimensions of the simulation.

Parameters:
  • w (float) – The width of the underlying grid.
  • h (float) – The height of the underlying grid.
  • dx (float) – The grid spacing of the underlying grid in the x direction.
  • dy (float) – The grid spacing of the underlying grid in the y direction.
primitives

The list of primitives used to define the material distribution.

Type:list
add_primitive(prim)

Add a primitive to the StructuredMaterial.

This could be an emopt.grid.Rectangle, emopt.grid.Polygon, etc–anything that extends emopt.grid.MaterialPrimitive.

Parameters:prim (MaterialPrimitive) – The MaterialPrimitive to add.
add_primitives(prims)

Add multiple primitives from a list.

Parameters:prims (list of MaterialPrimitives) – The list of MaterialPrimitives to add.
primitive
primitives
class emopt.grid.StructuredMaterial3D(X, Y, Z, dx, dy, dz)

Bases: emopt.grid.Material3D

Create a 3D material consisting of one or more primitive shapes (rectangles, polygons, etc) which thickness along z.

Currently StructuredMaterial3D only supports layered slab structures.

Notes

When used for defining the material distribution for a simulation, the dimensions supplied will typically match the dimensions of the simulation.

Parameters:
  • X (float) – The x width of the underlying grid.
  • Y (float) – The y width of the underlying grid.
  • Z (float) – The z width of the underlying grid.
  • dx (float) – The grid spacing of the underlying grid in the x direction.
  • dy (float) – The grid spacing of the underlying grid in the y direction.
  • dz (float) – The grid spacing of the underlying grid in the z direction.
primitives

The list of primitives used to define the material distribution.

Type:list
add_primitive(prim, z1, z2)

Add a primitive to the StructuredMaterial.

This could be an emopt.grid.Rectangle, emopt.grid.Polygon, etc–anything that extends emopt.grid.MaterialPrimitive.

Parameters:
  • prim (MaterialPrimitive) – The MaterialPrimitive to add.
  • z1 (float) – The minimum z-coordinate of the primitive to add.
  • z2 (float) – The maximum z-coordinate of the primitive to add.
primitive
primitives
zmaxs
zmins
emopt.grid.row_wise_A_update(eps, mu, ib, ie, M, N, x1, x2, y1, y2, vdiag)