pymech.meshtools#
This module contains some functions to generate and modify Nek5000 meshes.
Note
Those functions can build the proper connectivity of the elements, storing the elements’ neighbours as "E "
boundary conditions like in the .rea
files. This information seem to not be used at all by Nek5000, and is not contained in the binary .re2
files. In some functions it is possible to discard the connectivity information in order to accelerate the execution. This is particularly relevant in merge
.
extrude#
This function performs a linear extrusion of a 2D mesh in the z direction. The boundary conditions are kept identical on the existing boundaries and can be specified on the new boundaries.
The mandatory inputs are:
mesh
: apymech.core.HexaData
structure representing the 2D mesh to extrude;z
: a list of increasing z coordinates at which the mesh will be extruded.Additionally, the following arguments can be specified:
bc1
andbc2
: the boundary conditions to apply on the faces atz[0]
andz[-1]
. They are specified as a list of strings, one for each field (velocity, temperature, passive scalars). If nothing is specified, this defaults to periodic boundary conditions for all fields.internal_bcs
: boolean specifying whether the internal connectivity should be built ("E "
boundary conditions between connected elements). The default isTrue
.
The output is a pymech.core.HexaData
structure representing the extruded 3D mesh.
Contents of meshtools.py#
- pymech.meshtools.delete_internal_bcs(mesh)[source]#
Deletes the internal boundary conditions ‘E’ in a Nek5000 mesh. Those are present in .rea files but do not need to be valid, and are completely absent from .re2 files. Returns the number of deleted conditions.
- Parameters:
mesh (
pymech.core.HexaData
) – The mesh to modify in-place.
- pymech.meshtools.edge_circle(el, iedge, midpoint)[source]#
Finds the radius of curvature and circle center based on the midsize-node of edge iedge of element el:
- Parameters:
el (
pymech.core.HexaData
) – element of mesh (usually, el=mesh.elem[i])iedge (int) – index of edge
midpoint (float) – list of coordinates of midsize-node (in other words, if the curvature were type ‘m’, the values of el.curv[iedge][:3])
- pymech.meshtools.edge_mid(el, iedge)[source]#
Finds the coordinates of the midsize-node of edge iedge of element el (in other words, if the curvature were type ‘m’, the values of el.curv[iedge][:3]):
- Parameters:
el (
pymech.core.HexaData
) – element of mesh (usually, el=mesh.elem[i])iedge (int) – index of edge
- pymech.meshtools.exponential_refinement_parameter(l0, ltot, n, tol=1e-14)[source]#
In a 1D exponential mesh spacing where n elements of lengths l0, alpha*l0, …, alpha^(n-1)*l0 add up to a total length ltot, return the parameter alpha given l0, ltot, and n.
- pymech.meshtools.extrude(mesh: HexaData, z, bc1=None, bc2=None, internal_bcs=True)[source]#
Extrudes a 2D mesh into a 3D one
- Parameters:
mesh (
pymech.core.HexaData
) – 2D mesh structure to extrudez (float 1d array) – z coordinates at which to extrude the mesh, in increasing order
bc1 (str list) – A list of boundary conditions to use at the first end, one string per field. Defaults to periodic.
bc2 (str list) – A list of boundary conditions to use at the other end, one string per field. Defaults to periodic.
internal_bcs (bool) – if True, build mesh connectivity using internal ‘E’ boundary conditions (note that those are not used by Nek5000 and will not be written to binary .re2 files).
- pymech.meshtools.extrude_mid(mesh, z, bc1, bc2, fun, funpar=0.0, internal_bcs=True)[source]#
Extrudes the mid elments of the 2D mesh into a 3D one. Following the pattern:
_____ _____ _____ _____ |1 /|\ 4| /|\ | |__ / | \ __|__ / | \ __| (fun (with parameter funpar) should change change sign in the mid element) |0 |2 |3 | 5| | | | | (half of the mid elements are also divided in 2 in (x, y)-plane) |__|__|__|__|__|__|__|__| (numbers in the figure indicate the indices (iel + 0; iel + 1; etc))
- Parameters:
mesh (
pymech.core.HexaData
) – 2D mesh structure to extrudez (float) – list of z values of the nodes of the elements of the extruded mesh in the high discretization region (len(z)-1 must be divide by 4)
bc1 (str list) – A list of boundary conditions to use at the first end, one string per field.
bc2 (str list) – A list of boundary conditions to use at the other end, one string per field.
fun (function) – function that define the splitting lines for different discretization meshes
funpar (not defined, depends on the function) – parameter for functions that define the splitting lines for different discretization meshes (default: zero, can be used for when funpar is not needed inside fun)
Suggestion (see function extrude_split to understand how to call extrude_mid) –
- pymech.meshtools.extrude_refine(mesh2D, z, bc1=None, bc2=None, fun=None, funpar=None, imesh_high=0, internal_bcs=True)[source]#
Extrudes a 2D mesh into a 3D one, following the pattern
_____ _____ _____ _____ | | | | | | | | | | | | | | | |_____|_____|_____|_____| | /|\ | /|\ | |__ / | \ __|__ / | \ __| (fun (with parameter funpar) should change change sign in the mid element) | | | | | | | | | (half of the mid elements are also divided in 2 in (x,y)-plane) |__|__|__|__|__|__|__|__| | | | | | | | | | | | | | | | | | | | | | | | | | | | (imesh_high is the index of mesh with higher intended discretization in z) |__|__|__|__|__|__|__|__|
The pattern is similar to “Picture Frame” of an ancient NEKTON manual (https://www.mcs.anl.gov/~fischer/Nek5000/nekmanual.pdf). If the mid elements have curvature, the extrusion might modify it. Do not split in regions where the value of curvature parameters is very important.
- Parameters:
mesh2D (
pymech.core.HexaData
) – 2D mesh structure to extrudez (float array) – list of z values of the most refined zones of the extruded mesh
bc1 (str list) – A list of boundary conditions to use at the first end, one string per field. Defaults to periodic.
bc2 (str list) – A list of boundary conditions to use at the other end, one string per field. Defaults to periodic.
fun (function) – list of functions that define the splitting lines for different discretization meshes (default: empty, in which case the simple extrusion function extrude is called instead)
funpar (list) – list of parameters for functions that define the splitting lines for different discretization meshes (default: None, equivalent to an array of zeroes)
imesh_high (int) – index of fun that defines the mesh with higher discretization. Example: 0, is the most internal mesh; 1 is the second most internal mesh, etc (default: the most internal mesh, imesh_high=0)
- pymech.meshtools.gen_box(nx: int, ny: int, xmin: float, xmax: float, ymin: float, ymax: float, var=[2, 2, 1, 0, 0], bcs_xmin=None, bcs_xmax=None, bcs_ymin=None, bcs_ymax=None, internal_bcs=True)[source]#
generates a rectangular box of nx×ny elements in [xmin, xmax]×[ymin, ymax]. Boundary conditions can optionally be scpecified for each side.
- Parameters:
nx (float) – number of spectral elements in the x direction
ny (float) – number of spectral elements in the y direction
xmin (float) – bounds pof the box in the x and y directions
xmax (float) – bounds pof the box in the x and y directions
ymin (float) – bounds pof the box in the x and y directions
ymax (float) – bounds pof the box in the x and y directions
var (int list) – optional, the list of fields to include in the mesh (velocity, pressure, temperature, passive scalars). Defaults to only velocity and pressure.
bcs_xmin (str lists) – optional, lists of boundary conditions for each of the sides of the box. Each argument must be either None or a list of boundary conditions, one for each field present in the mesh except pressure. If None, it defaults to periodic boundary conditions for all fields.
bcs_xmax (str lists) – optional, lists of boundary conditions for each of the sides of the box. Each argument must be either None or a list of boundary conditions, one for each field present in the mesh except pressure. If None, it defaults to periodic boundary conditions for all fields.
bcs_ymin (str lists) – optional, lists of boundary conditions for each of the sides of the box. Each argument must be either None or a list of boundary conditions, one for each field present in the mesh except pressure. If None, it defaults to periodic boundary conditions for all fields.
bcs_ymax (str lists) – optional, lists of boundary conditions for each of the sides of the box. Each argument must be either None or a list of boundary conditions, one for each field present in the mesh except pressure. If None, it defaults to periodic boundary conditions for all fields.
internal_bcs (bool) – Optional, specifies whether to build the connectivity boundary conditions between internal elements. Default is True.
- pymech.meshtools.gen_circle(r: float, s: float, ns: int, no: int, curvature_fun=None, bl_fun=None, var=[2, 2, 1, 0, 0], bc=['W'], internal_bcs=True)[source]#
Generates a 2D circular mesh with a square at the center surrounded by an O mesh.
- Parameters:
r (float) – radius of the mesh
s (float) – relative length of the diagonal of the centre square mesh to the circle diameter
ns (int) – number of elements in the side of the square and in a quarter of the circumference of the circle
no (int) – number of elements in the O mesh part in the radial direction
internal_bcs (bool) – if True, builds the internal connectivity information of the elements
curvature_fun ([0, 1] -> [0, 1] function or None) – Function defining the evolution of the relative curvature of the edges between the straight side of the square and the circular edge. Defaults to x -> sin(pi/2 x) if None. A constant 1 means concentric circles, a constant zero means only straight edges inside the domain, but ideally you want a continuous function with f(0) = 0 and f(1) = 1.
bl_fun ([0, 1] -> [0, 1] function or None) – Function defining the evolution of the grid location between the edge of the square at 0 and the edge of the circle at 1. Defaults to an exponential grid spacing with a uniform spacing around the square if None.
var (integer list) – Number of geometry, velocity, pressure, temperature and scalar variables to define in the mesh
bc (str list) – boundary conditions to use for each field
internal_bcs – whether to build internal connectivity information
- pymech.meshtools.generate_internal_bcs(mesh, tol=0.001)[source]#
Generates internal boundary conditions ‘E’ in a Nek5000 mesh based on geometric proximity of faces. This will loop over all pairs of faces and connect them if their centres are closer than some specified relative tolerance. This function returns the number of internal connections found.
- Parameters:
mesh (
pymech.core.HexaData
) – The mesh to modify in-placetol (float) – the tolerance within which the centres of two faces are considered the same, relative to the smallest edge of the elements
- pymech.meshtools.keep_elements(mesh: HexaData, elems, external_bc='')[source]#
Reduce the mesh to a subset of its elements
- Parameters:
mesh (
pymech.core.HexaData
) – mesh to modifyelems (int array) – list of element numbers (zero indexed) to keep
external_bc (string) – if an element was connected to another which is removed from the mesh, replace that condition with external_bc
- pymech.meshtools.map2D(mesh: HexaData, transformation, curvature=True, boundary_curvature=True)[source]#
Applies a coordinate transformation to a 2D mesh, returning the transformed mesh. The faces are curved to second-order accuracy by setting the midpoints according to the transformation.
- Parameters:
mesh (
pymech.core.HexaData
) – the mesh to transform, will not be modified.transformation ((float, float) -> (float float) function) – the coordinate transformation to apply. It must be a valid right-handed transformation (the determinant of its Jacobian must be positive on the mesh domain).
curvature (bool) – specifies whether to apply curvature to all faces. The faces that are already curved in the original mesh will always be curved. Curvature can still be applied on the boundaries when this is False if boundary_curvature is set to True. True by default.
boundary_curvature (bool) – specifies whether to apply curvature to boundary faces by setting midpoints. True by default.
- Returns:
mapped_mesh – the transformed mesh
- Return type:
- pymech.meshtools.rotate_2d(mesh, x0, y0, theta)[source]#
Rotate a mesh around an axis aligned with z passing through (x0, y0, 0) by an angle theta.
- Parameters:
mesh (
pymech.core.HexaData
) – the mesh to modify in placex0 (float) – x-coordinate of the center of rotation
y0 (float) – y-coordinate of the center of rotation
theta (rotation angle) –