A very specific and interesting request!
The Vinokur stretching algorithm is a popular method for generating non-uniform, boundary-layer refined meshes, particularly useful for computational fluid dynamics (CFD) and heat transfer simulations. I'll provide you with a Python implementation of the Vinokur stretching algorithm for 2D mesh generation.
What is the Vinokur stretching algorithm?
The Vinokur algorithm is a 1D stretching function that maps a uniform computational space (xi) to a non-uniform physical space (x) with controlled spacing and stretching. The algorithm ensures:
- Smoothness: gradual variation of cell sizes.
- Orthogonality: cells are nearly orthogonal to boundaries.
- Boundary layer refinement: tightly packed cells near boundaries.
The 2D mesh generation will involve tensor-product extension of the 1D Vinokur algorithm.
Python code: Vinokur Stretching Algorithm for 2D Mesh Generation
import numpy as np
import matplotlib.pyplot as plt
def vinokur_1d(N, L, beta, ds1=1e-6, ds2=None):
"""
1D Vinokur stretching function.
Parameters:
- N (int): Number of intervals (cells = N - 1)
- L (float): Length of the domain
- beta (float): Stretching parameter (~1: uniform, >1: boundary-refined)
- ds1 (float, optional): Initial spacing (default=1e-6)
- ds2 (float, optional): End spacing (default=None, mirror ds1)
Returns:
- xi (numpy array): Uniform computational space (N points)
- x (numpy array): Non-uniform physical space (N points)
"""
if ds2 is None:
ds2 = ds1 # symmetric stretching
A = np.sqrt(beta) / (1 + (beta - 1) * (ds1 / L))
B = 1 / (1 + (beta - 1) * (ds1 / L))
delta = (1 - A) / (1 + A)
xi = np.linspace(0, 1, N)
u = 0.5 * (1 + np.tanh(delta * (xi - 0.5)) / np.tanh(delta / 2))
s = u / (A + (1 - A) * u)
x = L * s # scale to physical length
# Adjust first and last spacings
x[1] = ds1
x[-1] = L - ds2
# Re-scale to ensure exact length (due to tanh approx.)
x = x / x[-1] * L
return xi, x
def generate_2d_mesh(Nx, Ny, Lx, Ly, betax, betay, ds1x=1e-6, ds1y=1e-6):
"""
2D mesh generation using tensor-product extension of Vinokur.
Parameters:
- Nx, Ny (int): Number of intervals in x, y directions
- Lx, Ly (float): Lengths of the domain in x, y directions
- betax, betay (float): Stretching parameters in x, y directions
- ds1x, ds1y (float, optional): Initial spacings in x, y (default=1e-6)
Returns:
- X, Y (2D numpy arrays): Mesh coordinates (Ny x Nx)
"""
_, x = vinokur_1d(Nx, Lx, betax, ds1x)
_, y = vinokur_1d(Ny, Ly, betay, ds1y)
X, Y = np.meshgrid(x, y) # tensor-product extension
return X, Y
# Example usage
Nx, Ny = 101, 81
Lx, Ly = 10.0, 5.0
betax, betay = 5.0, 3.0 # more refinement near x=0, y=0
X, Y = generate_2d_mesh(Nx, Ny, Lx, Ly, betax, betay)
# Visualize the mesh
plt.figure(figsize=(8, 6))
plt.plot(X, Y, 'b-', alpha=0.5)
plt.plot(X.T, Y.T, 'b-', alpha=0.5)
plt.title("2D Mesh with Vinokur Stretching")
plt.xlabel("X"); plt.ylabel("Y")
plt.axis('equal') # aspect ratio 1:1
plt.show()
Here's what's happening:
- The
vinokur_1d function implements the 1D Vinokur stretching algorithm. It takes the number of intervals N, domain length L, stretching parameter beta, and initial/end spacings ds1 and ds2.
- The
generate_2d_mesh function uses tensor-product extension to create a 2D mesh. It calls vinokur_1d for both x and y directions and combines the results using np.meshgrid.
- In the example usage, we generate a 2D mesh with
Nx=101, Ny=81, Lx=10, Ly=5, and stretching parameters betax=5, betay=3. The resulting mesh has more refinement near x=0 and y=0.
- Finally, we visualize the mesh using Matplotlib.
Tips and Variations
- Adjust
betax and betay to control the amount of stretching in each direction. Higher values lead to more aggressive boundary layer refinement.
- Change
ds1x and ds1y to set different initial spacings (e.g., for asymmetric stretching).
- Experiment with different
Nx and Ny values to balance mesh resolution and computational cost.
- For CFD applications, consider clustering cells near walls (e.g.,
betax=10 near x=0) and using a coarser mesh in the freestream region.
- To improve orthogonality, you can apply elliptic mesh smoothing or variational mesh optimization techniques (not implemented here).
Now, go forth and mesh the world!