S4Utils functions references¶
Created on Thu Apr 2 12:05:13 2020
@author: mathieu
Basic Utility functions¶
- S4Utils.S4Utils.totuple(a)¶
Converts lists and array to a tuple form. Usefull since S4 mostly accepts tuples instead of numpy arrays.
- Parameters
a (array-like, list or float) – Array-like parameter to cast to a tuple
- Returns
ta – elements of a cast into a tuple of same ‘shape’.
- Return type
tuple
- Raises
TypeError: – If the type of a cannot be cast into a tuple form, returns a
- S4Utils.S4Utils.UpdateMaterials(S, Mat_list, Eps_list, fi, f0)¶
Updates a Simulation to set all refractive indices at given frequency
- Parameters
S (S4.Simulation) –
Mat_list (list) – list of string containing the name of the materials to update. These are typically the ones passes in the ‘Material’ argument for the S4.Simulation.SetMaterial() function
Eps_list (list) – list of arrays containing the permittivities. each element is a 1D array or a 3x3xlen(f) array
fi (float) – frequency at which the simulation is set (reduced units)
f0 (1Darray) – array of reduced frequencies over which the simulation runs
- Return type
None.
Simulation Functions¶
2D Slicing functions¶
- S4Utils.S4Utils.GetField_xy(S, res, zs)¶
Get E and H fields in an xy slice. Built-in S4 function that requires some care and a tweak, but extra efficient !!
- Parameters
S (S4.Simulation) –
res (tuple) – (resx, resy) tuple of number of points along x and y Note that it corresponds to resy lines and resx columns
zs (float) – z-coordinate at which to slice the field
- Return type
None.
- S4Utils.S4Utils.GetField_xz(S, x, y, z, ys=0.0)¶
Get E and H fields in an xz slice. Wraps around GetField_xy which is hard-coded in the C code and thus much faster. BUT you should check that it matches with GetSlice at least once because the C function GetFieldsOnGrid tends to be touchy.
- Parameters
S (S4.Simulation Object) – Simulation to run
x (1Darray) – 1Darray of x-coordinates
y (1Darray) – 1Darray of y-coordinates
z (1Darray) – 1Darray of z-coordinates
ys (float, optional) – y-coordinate of the slice. Defaults to 0 (center of the unit cell).
- Returns
E_slice (len(z) by len(x) by 3 array) – Array of the electric field vector on the slice
H_slice (len(z) by len(x) by 3 array) – Array of the magnetic field vector on the slice
- S4Utils.S4Utils.GetField_yz(S, x, y, z, xs=0.0)¶
Get E and H fields in an yz slice. Wraps around GetField_xy which is hard-coded in the C code and thus much faster. BUT you should check that it matches with GetSlice at least once because the C function GetFieldsOnGrid tends to be touchy.
- Parameters
S (S4.Simulation Object) – Simulation to run
x (1Darray) – 1Darray of x-coordinates
y (1Darray) – 1Darray of y-coordinates
z (1Darray) – 1Darray of z-coordinates
xs (float, optional) – x-coordinate of the slice. Defaults to 0 (center of the unit cell).
- Returns
E_slice (len(z) by len(y) by 3 array) – Array of the electric field vector on the slice
H_slice (len(z) by len(y) by 3 array) – Array of the magnetic field vector on the slice
- S4Utils.S4Utils.GetSlice(S, ax1, ax2, axs=0, plane='xz', mode='All')¶
Slice field and/or epsilon in an arbitrary slice along 2 axis at a given position on the third. Point-by-point computation version. There is no built-in function for this, so it is much slower. However it is less ambiguous than the GetField methods and can be used to check/debug.
- Parameters
S (S4.Simulation) –
ax1 (1Darray) – first axis to define the slice plane
ax2 (1Darray) – second axis to define the slice plane
axs (float) – coordinate along thirs axis on which to slice
plane (str) – ‘xz’, ‘yz’ or ‘xz’ to choose which slice it is
mode (str) – either ‘Field’, ‘Epsilon’, ‘All’ to choose which quantity to slice
3D Volume functions¶
- S4Utils.S4Utils.GetFieldVolume(S, x, y, z)¶
Get the electric field vector in the whole simulation volume
- Parameters
S (S4.Simulation obect) – Simulation object to run
x (1Darray) – 1Darray of x-coordinates
y (1Darray) – 1Darray of y-coordinates
z (1Darray) – 1Darray of z-coordinates
- Returns
E_vol (len(x) by len(y) by len(z) by 3 array) – Array of electric field in the simulation volume
H_vol (len(x) by len(y) by len(z) by 3 array) – Array of magnetic field in the simulation volume
- S4Utils.S4Utils.GetEpsilonVolume(S, x, y, z)¶
Get the reconstructed permittivity in the whole simulation volume
- Parameters
S (S4.Simulation obect) – Simulation object to run
x (1Darray) – 1Darray of x-coordinates
y (1Darray) – 1Darray of y-coordinates
z (1Darray) – 1Darray of z-coordinates
- Returns
Eps_vol – Array of the permittivity in the simulation volume
- Return type
len(x) by len(y) by len(z) array
S4Utils Plotting Utilities¶
The SlicePlot class¶
- class S4Utils.S4Utils.SlicePlot(a1, a2, Field, cmap=matplotlib.pyplot.cm.bwr, hcoord=None, vcoord=None, sym=True)¶
Helper to plot a slice with linecuts at arbitrary positions
- Parameters
a1 (1Darray) – 1Darray for the horizontal axis of the 2D plot (typically x)
a2 (1Darray) – 1Darray for the vertical axis of the 2D plot (typically y or z)
Field (2Darray) – 2Darray to be plotted (len(a2) x len(a1))
cmap (matplotib.colors.Colormap object) – The colormap to pass to matplotlib.pcolormesh for plotting. Defaults to matplotlib.cm.bwr.
hcoord (float, optional) – Horizontal coordinate for the vertical slice. Defaults to the center of the axe coordinate array given (a1).
vcoord (float, optional) – Vertical coordinate for the horizontal slice. Defaults to the center of the axe coordinate array given (a2).
sym (bool, optional) – Flag to force symmetry in the colormap or leave it automated
- a1¶
1Darray for the horizontal axis of the 2D plot (typically x)
- Type
1Darray
- a2¶
1Darray for the vertical axis of the 2D plot (typically y or z)
- Type
1Darray
- Field¶
2Darray to be plotted (len(a2) x len(a1))
- Type
2Darray
- hcoord¶
Horizontal coordinate for the vertical slice. Defaults to the center of the axe coordinate array given (a1).
- Type
float, optional
- vcoord¶
Vertical coordinate for the horizontal slice. Defaults to the center of the axe coordinate array given (a2).
- Type
float, optional
- sym¶
Flag to force symmetry in the colormap or leave it automated
- Type
bool, optional
- figslice¶
The matplotlib figure in which the plot is drawn
- Type
matplotlib.figure
- fignum¶
The number of the figure as retrieved by matplotlib.figure.number
- Type
int
- ax_2D¶
The axes in which the 2D slice is drawn
- Type
matplotlib.axes
- cax¶
The axes in which the colorbar for ax_2D drawn
- Type
matplotlib.axes
- ax_cbar¶
The matplotlib.colorbar instance
- Type
matplotlib.colorbar
- ax_a1slice¶
The axes in which the slice along a2 at a fixed a1 position is drawn
- Type
matplotlib.axes
- lv¶
The line of the vertical cut, e.g. along a2
- Type
matplotlib.line
- ax_a2slice¶
The axes in which the slice along a1 at a fixed a2 position is drawn
- Type
matplotlib.axes
- lh¶
The line of the horizontal cut, e.g. along a1
- Type
matplotlib.line
- lslice_h¶
The line showing the position of the horizontal slice in ax_2D
- Type
matplotlib.line
- lslice_v¶
The line showing the position of the vertical slice in ax_2D
- Type
matplotlib.line
- GUIButton¶
The button to hide/show the GUI
- Type
matplotlib.widgets.CheckButtons
Notes
For a 3D stack of slices one should move to Plotly instead of matplotlib https://nbviewer.jupyter.org/github/empet/Plotly-plots/blob/master/Plotly-Slice-in-volumetric-data.ipynb
The GUISlicePlot class¶
- class S4Utils.S4Utils.GUISlicePlot(parentfig)¶
Generates an auxiliary figure to interact with a SlicePlot. The GUI side plot allows to move the vertical and horizontal sliders to show slices at different locations.
- Parameters
parentfig (
S4Utils.S4Utils.SlicePlotinstance) – The SlicePlot figure to which the GUI applies.
- parentfig¶
The SlicePlot figure to which the GUI applies.
- Type
S4Utils.S4Utils.SlicePlotinstance
- figGUI¶
The matplotlib.figure in which the GUI is drawn
- Type
matplotlib.figure instance
- ax1¶
The axes for the first slider linked to parentfig.a1
- Type
matplotlib.axes
- ax2¶
The axes for the first slider linked to parentfig.a2
- Type
matplotlib.axes
- Sl_ax1¶
The slider linked to the parentfig.a1 axis
- Type
matplotlib.widgets.Slider
- Sl_ax2¶
The slider linked to the parentfig.a2 axis
- Type
matplotlib.widgets.Slider
- Return type
None.
Todo
Add a frequency input textbox to change the frequency and update plot
3D Plotting Function¶
- S4Utils.S4Utils.Plot3DSlices(axes, DataVol, points, InteractiveSlices=False)¶
3D rendering of 3 slices in orthogonal planes.
- Parameters
axes – x, y and z 1Darray vectors of coordinates on which the data is calculated. Usually passed as (x,y,z) or np.array((x,y,z))
DataVol (len(x) by len(y) by len(z) numpy array) – Volumetric data in numpy array to be plotted Usually obtained using
S4Utils.S4Utils.GetFieldVolume()orS4Utils.S4Utils.GetEpsilonVolume()functions.points (tuple, list or array) – point at which to slice the volume data. Usually (xs, ys, zs), in real space coordinates
InteractiveSlices (bool (opt)) – Boolean flag to activate PyVista’s defaults 3D slicing. Defaults to False.
- Return type
None.
Notes
Follows https://docs.pyvista.org/examples/00-load/create-uniform-grid.html and https://docs.pyvista.org/plotting/widgets.html#pyvista.WidgetHelper.add_mesh_slice_orthogonal