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.MaterialPrimitiveDefine 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.Material2DA uniform constant material.
Parameters: value (complex) – The constant material value. -
material_value
-
-
class
emopt.grid.ConstantMaterial3D(value)¶ Bases:
emopt.grid.Material3DA uniform constant 3D material.
Parameters: value (complex) – The constant material value. -
material_value
-
-
class
emopt.grid.GridMaterial2D(M, N, grid)¶ Bases:
emopt.grid.Material2DDefine a simple rectangular-grid-based Material distribution.
This is the simplest form of
Materialobject which defines a material distribution as a simple rectangular grid. On its own, there is no grid smoothing performed by aGridMaterial, however it can be used to implement custom grid smoothing subroutines.Parameters: -
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.newobjectDefine 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: 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.newobjectDefine 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: 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.newobjectDefine 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: Returns: True if the (x,y) point is contained within the primitive, false otherwise.
Return type:
-
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: 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.MaterialPrimitiveDefine a rectangular material primitive.
A rectangle is defined by its center position and its width and height.
Parameters: -
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:
-
set_width(width) (Deprecated) Set the width of the rectangle.
-
width
-
x0
-
y0
-
-
class
emopt.grid.StructuredMaterial2D(w, h, dx, dy)¶ Bases:
emopt.grid.Material2DCreate 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: -
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.Material3DCreate 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.
-
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)¶