cmutils module¶
Pre-/Post-Processing of contMech Data¶
Miscellaneous functions for input, output, manipulation and plotting of contMech config files. Very powerful in combinations with numpy’s and scipy’s standard manipulation and statistics tools.
Examples¶
Generate, view, export, import and rotate a contMech config file:
x,y = cm.XY((64,64), Lx=2., Ly=2.)
z = x**2 - y**2
cm.plotImg(z)
cm.dumpConfig(z, 'konfig.dat')
z2 = cm.rot90(cm.readConfig('konfig.dat'))
cm.plotImg(z2)
API documentation¶
- cmutils.bilin_resample(array, new_shape)[source]¶
resample / change array resolution through bilinear interpolation
- Parameters:
array (np.ndarray) – array of shape (nx, ny) to be resampled into shape new_shape
new_shape (iterable containing two int values) – new target resolution (nx_new, ny_new)
- Returns:
result (np.ndarray) – resampled version of array
- cmutils.corners(array, N=None)[source]¶
meeting point of array’s corners
constructs the 2 N x 2 N area around the point where the four corners meet if np.ndarray array is periodically repeated in the plane.
- Returns:
np.ndarray
- cmutils.createBorder(array, nxNew, nyNew)[source]¶
create border around surface
generates a (nxNew, nyNew) numpy.ndarray that contains the (nx, ny) numpy.ndarray array. nxNew must be larger than nx and nyNew larger than ny.
- Returns:
the new numpy.ndarray
- cmutils.dumpConfig(arrays, filepath='konfig0py.real', Lx=None, Ly=None)[source]¶
dump arrays as contMech konfig
writes list arrays of numpy.ndarrays to a file in the typical format that can be plotted in gnuplot or imported into a contMech simulation.
- cmutils.dumpImg(array, filename)[source]¶
dump array as image text file
writes 2D (nx, ny) numpy.ndarray array to a text file with ny values per line and nx lines.
- cmutils.dumpLines(array, lines, filename='', dim=0)[source]¶
Dump line scan(s)
exports individual lines along dimension dim from 2D numpy.ndarray array into filename. lines can be an int or a list/tuple of ints with desired line indices to be exported.
- cmutils.flipMulti(arrays, axis=0)[source]¶
flips displacements and pressures by rotation around a lateral axis
assumes that arrays is a tuple containing uz, pz(, uy, py, ux, px) in this order. Returns the tuple manipulated as if the simulation was rotated by 180° around the x-axis (axis = 0) or y-axis (axis = 1).
- Parameters:
arrays (tuple/list of np.ndarrays) – assuming z,y,x ordering, displacement and pressure
axis (int (0,1)) – axis, around which the system is rotated. 0: x-axis, 1: y-axis.
- Returns:
arrays as represented in the flipped coordinate system
- cmutils.plotImg(array, clim='auto', title=False, aspect='true', cmap='turbo', alpha=1.0, axis=None, pad=0.01, cba=0.85, rot=True, figsize=(3.3, 3), **kwargs)[source]¶
plot array as an image
plots 2D numpy.ndarray array as a colored image, similar to gnuplot’s “splot … with pm3d”. clim must be of form (v_min,v_max), the value range to which colors are mapped.
- Returns:
matplotlib.axes.Axes object containing the plot
- cmutils.plotLines(array, lines, axis=None, dim=0, ls='-', figsize=None, **kwargs)[source]¶
plot line scan(s)
plots line(s) along dimension dim from numpy.ndarray array. lines can be an int or a list/tuple of ints with desired line indices. axis, the matplotlib.axes.Axes object on which to plot, can be specified. as well as typical kwargs for plots.
- Returns:
matplotlib.axes.Axes object containing the plot
- cmutils.plotPSD(array, axis=None, **kwargs)[source]¶
plot PSD of array
plots the PSD of numpy.ndarray array. axis, the matplotlib.axes.Axes object on which to plot, can be specified.
- Returns:
matplotlib.axes.Axes object containing the plot
- cmutils.plotSurf(array, kind='color', title=False, clim='auto', axis=None, figsize=None, stride=4, cmap='turbo', lw=0.5, lc='gray', aa=False, alpha=1, label=None, **kwargs)[source]¶
plot 3D view of surface
plots 2D numpy.ndarray array, using the data as z values, in a 3D view.
for kind = “color”, the color map cmap can be specified, as well as the color range clim in the form (v_min,v_max). stride specifies the discretization increment, lower being higher quality.
for kind = “wire”, the linewidth lw and linecolor lc can be specified.
- Returns:
matplotlib.axes.Axes object containing the plot
- cmutils.psd(array, output='')[source]¶
compute Power Spectral Density (PSD) of an array
calculates the PSD of numpy.ndarray array with auto quasi-log q space. writes the PSD to file output if given.
- Returns:
numpy.ndarray with 2 columns containing q and C(q) values.
- cmutils.readConfig(filepath, usecols=2)[source]¶
read contMech konfig file
reads column usecols from file filepath, and converts it to an (nx,ny) numpy.ndarray, assuming the first line in the file is of the form “#nx ny”
- Returns:
numpy.ndarray
- cmutils.readImg(filepath)[source]¶
read image file (without lateral info)
reads file filepath with ny numbers per line and nx lines into a (nx, ny) np.ndarray.
- Returns:
np.ndarray
- cmutils.readMulti(filepath, usecols=None)[source]¶
read contMech konfig file
reads column usecols from file filepath, and converts it to an (nx,ny) numpy.ndarray, assuming the first line in the file is of the form “#nx ny”
- Returns:
numpy.ndarray
- cmutils.reduce(array, resol)[source]¶
resample / change array resolution
resamples the (nx,ny) numpy.ndarray array to the new resolution resol. resol can be of form (nx_new, ny_new) or a single int, which is translated to resol = (round(nx/resol), round(ny/resol)).
- Returns:
resampled numpy.ndarray
- cmutils.resample(array, resol)[source]¶
resample / change array resolution
- Parameters:
array (np.ndarray with shape (nx, ny)) – array to be resampled
resol (float or 2-tuple/list of int) – either the new target resolution (nx_new, ny_new) or a single float representing the new resolution relative to the original according to (nx_new, ny_new) = (round(resol * nx), round(resol * ny)).
- Returns:
result (np.ndarray) – resampled version of array
- cmutils.smoothPBC(array, overlapX=None, overlapY=None)[source]¶
make non-periodic image smooth at periodic boundaries
- Parameters:
array (np.ndarray) – array whose edges are manipulated
overlapX (int or None) – all points within overlapX from the edges of array along axis 0 are gradually approach the value of the opposite edge.
overlapY (int or None) – all points within overlapY from the edges of array along axis 1 are gradually approach the value of the opposite edge.
- Returns:
np.ndarray