tensormesh.dataset¶
mesh¶
- class MeshGen(element_type=None, dimension: int = 2, order: int = 1, chara_length: float = 0.1, cache_path: str = './tmp.msh', verbose: bool = False)[source]¶
Bases:
object- Parameters:
element_type (str, optional) – If
element_typeisNone, then it will generate a mix mesh. Otherwise, the order and element will be determined byelement_type.dimension (int, optional) – The dimension of the mesh, e.g., \(2\) or \(3\).
order (int, optional) – The order of the element, e.g., \(1\), \(2\).
chara_length (float, optional) – The characteristic length of the mesh. The smaller the value, the more dense the mesh. Default is \(0.1\).
Examples
generate rectangle mesh with triangle elements:
from tensormesh import MeshGen generator = MeshGen(element_type="tri") # triangle mesh for 2d generator.addRectangle(0,0,1,1) # add a rectangle mesh = generator.gen().plot() # generate and visualize the mesh
generate mixed mesh with left triangle and right rectangle
from tensormesh import MeshGen mesh_gen = MeshGen(element_type=None, chara_length=0.1, order=2) mesh_gen.add_rectangle(0,0,0.5,1, element="tri") mesh_gen.add_rectangle(0.5,0,0.5,1, element="quad") mesh_gen.remove_circle(0.5,0.5,0.1) mesh_gen.gen().plot()
- __init__(element_type=None, dimension: int = 2, order: int = 1, chara_length: float = 0.1, cache_path: str = './tmp.msh', verbose: bool = False)[source]¶
- add_rectangle(left, bottom, width, height, element='tri')[source]¶
add a rectangle to the geometry
- Parameters:
- Returns:
the mesh generator itself
- Return type:
- add_cube(x, y, z, dx, dy, dz, element: str = 'tet')[source]¶
add a cube to the geometry, only works for 3d
- Parameters:
- Returns:
the mesh generator itself
- Return type:
- remove_cube(x, y, z, dx, dy, dz)[source]¶
remove the cube from the geometry, only works for 3d
- Parameters:
- Returns:
the mesh generator itself
- Return type:
- add_sphere(x, y, z, r)[source]¶
add a sphere to the geometry, only works for 3d
not supported for hex element because of algorithm issue
Batch mesh generators¶
Module-level helpers that produce a fresh Mesh
each call — used by the dataset-generation scripts under
examples/dataset/. The Mesh class also exposes
classmethods with the same names (e.g. gen_rectangle())
for one-off use.
- gen_rectangle(chara_length=0.1, order=1, element_type='quad', left=0.0, right=1.0, bottom=0.0, top=1.0, visualize=False, cache_path=None)[source]¶
- Parameters:
chara_length (float) – the characteristic length of the mesh
order (int) – the order of the mesh
element_type (str) – the type of the element, e.g., ‘quad’, ‘tri’
left (float) – the left boundary of the rectangle
right (float) – the right boundary of the rectangle
bottom (float) – the bottom boundary of the rectangle
top (float) – the top boundary of the rectangle
visualize (bool) – whether to visualize the mesh
cache_path (str) – the path to store the mesh
- Return type:
- gen_hollow_rectangle(chara_length=0.1, order=1, element_type='quad', outer_left=0.0, outer_right=1.0, outer_bottom=0.0, outer_top=1.0, inner_left=0.25, inner_right=0.75, inner_bottom=0.25, inner_top=0.75, visualize=False, cache_path=None)[source]¶
- Parameters:
chara_length (float) – the characteristic length of the mesh
order (int) – the order of the mesh
element_type (str) – the type of the element, e.g., ‘quad’, ‘tri’
outer_left (float) – the left boundary of the rectangle
outer_right (float) – the right boundary of the rectangle
outer_bottom (float) – the bottom boundary of the rectangle
outer_top (float) – the outer_top boundary of the rectangle
inner_left (float) – the inner left boundary of the rectangle
inner_right (float) – the inner right boundary of the rectangle
inner_bottom (float) – the inner bottom boundary of the rectangle
inner_top (float) – the inner top boundary of the rectangle
visualize (bool) – whether to visualize the mesh
cache_path (str) – the path to store the mesh
- gen_circle(chara_length=0.1, order=1, element_type='tri', cx=0.0, cy=0.0, r=1.0, visualize=False, cache_path=None, verbose=False)[source]¶
- Parameters:
chara_length (float) – The characteristic length of the mesh
order (int) – The order of the elements
element_type (str) – The type of the elements. Must be one of “quad” or “tri”
cx (float) – The x-coordinate of the center of the circle
cy (float) – The y-coordinate of the center of the circle
r (float) – The radius of the circle
visualize (bool) – Whether to visualize the mesh
cache_path (str) – The path to save the mesh
verbose (bool) – Whether to print detailed information
- Return type:
None
- gen_hollow_circle(chara_length=0.1, order=1, element_type='quad', cx=0.0, cy=0.0, r_inner=1.0, r_outer=2.0, visualize=False, cache_path=None, verbose=False)[source]¶
- Parameters:
chara_length (float) – The characteristic length of the mesh
order (int) – The order of the elements
element_type (str) – The type of the elements. Must be one of “quad” or “tri”
cx (float) – The x-coordinate of the center of the circle
cy (float) – The y-coordinate of the center of the circle
r_inner (float) – The inner radius of the circle
r_outer (float) – The outer radius of the circle
visualize (bool) – Whether to visualize the mesh
cache_path (str) – The path to save the mesh
verbose (bool) – Whether to print detailed information
- Return type:
- gen_L(chara_length=0.1, order=1, element_type='quad', left=0.0, right=1.0, bottom=0.0, top=1.0, top_inner=0.5, right_inner=0.5, visualize=False, cache_path=None)[source]¶
- Parameters:
chara_length (float) – the characteristic length of the mesh
order (int) – the order of the mesh
element_type (str) – the type of the element, e.g., ‘quad’, ‘tri’
left (float) – the left boundary of the Lshape
right (float) – the right boundary of the Lshape
bottom (float) – the bottom boundary of the Lshape
top (float) – the top boundary of the Lshape
top_inner (float) – the top inner boundary of the Lshape
right_inner – the right inner boundary of the Lshape
visualize (bool) – whether to visualize the mesh
cache_path (str) – the path to store the mesh
- Return type:
- gen_cube(chara_length=0.1, order=1, left=0.0, right=1.0, bottom=0.0, top=1.0, front=0.0, back=1.0, visualize=False, cache_path=None)[source]¶
- Parameters:
chara_length (float, optional) – the characteristic length of the mesh, default:
0.1order (int, optional) – the order of the basis function, default:
1left (float, optional) – the left boundary of the cube, default:
0.0right (float, optional) – the right boundary of the cube, default:
1.0bottom (float, optional) – the bottom boundary of the cube, default:
0.0top (float, optional) – the top boundary of the cube, default:
1.0front (float, optional) – the front boundary of the cube, default:
0.0back (float, optional) – the back boundary of the cube, default:
1.0visualize (bool, optional) – whether to visualize the mesh, default:
Falsecache_path (str, optional) – the path to save the mesh, if
None, it will be decided bygen_cube(), default:None
- Returns:
the mesh object
- Return type:
- gen_hollow_cube(chara_length=0.1, order=1, outer_left=0.0, outer_right=1.0, outer_bottom=0.0, outer_top=1.0, outer_front=0.0, outer_back=1.0, inner_left=0.25, inner_right=0.75, inner_bottom=0.25, inner_top=0.75, inner_front=0.25, inner_back=0.75, visualize=False, cache_path='.gmsh_cache/tmp.msh')[source]¶
- Parameters:
chara_length (float, optional) – the characteristic length of the mesh, default:
0.1order (int, optional) – the order of the basis function, default:
1outer_left (float, optional) – the left boundary of the outer cube, default:
0.0outer_right (float, optional) – the right boundary of the outer cube, default:
1.0outer_bottom (float, optional) – the bottom boundary of the outer cube, default:
0.0outer_top (float, optional) – the top boundary of the outer cube, default:
1.0outer_front (float, optional) – the front boundary of the outer cube, default:
0.0outer_back (float, optional) – the back boundary of the outer cube, default:
1.0inner_left (float, optional) – the left boundary of the inner cube, default:
0.25inner_right (float, optional) – the right boundary of the inner cube, default:
0.75inner_bottom (float, optional) – the bottom boundary of the inner cube, default:
0.25inner_top (float, optional) – the top boundary of the inner cube, default:
0.75inner_front (float, optional) – the front boundary of the inner cube, default:
0.25inner_back (float, optional) – the back boundary of the inner cube, default:
0.75visualize (bool, optional) – whether to visualize the mesh, default:
Falsecache_path (str, optional) – the path to save the mesh, if
None, it will be decided bygen_hollow_cube(), default:None
- Returns:
the mesh object
- Return type:
- gen_sphere(chara_length=0.1, order=1, cx=0.0, cy=0.0, cz=0.0, r=1.0, visualize=False, cache_path=None)[source]¶
- Parameters:
chara_length (float) – the characteristic length of the mesh
order (int) – the order of the mesh
cx (float) – the x cooridnate of the center of the sphere
cy (float) – the y cooridnate of the center of the sphere
cz (float) – the z coorindate of the center of the sphere
r (float) – the radius of the sphere
visualize (bool) – whether to visualize the mesh
cache_path (str) – the path to store the mesh
- Return type:
None
- gen_hollow_sphere(chara_length=0.1, order=1, cx=0.0, cy=0.0, cz=0.0, r_inner=1.0, r_outer=2.0, visualize=False, cache_path=None)[source]¶
- Parameters:
chara_length (float) – the characteristic length of the mesh
order (int) – the order of the mesh
cx (float) – the x cooridnate of the center of the sphere
cy (float) – the y cooridnate of the center of the sphere
cz (float) – the z coorindate of the center of the sphere
r_inner (float) – the inner radius of the sphere
r_outer (float) – the outer radius of the sphere
- Return type:
None
equation¶
- class PoissonMultiFrequency(a=None, K=2, r=-0.5)[source]¶
Bases:
objectMulti-frequency wave equation, with \(0\) boundary condition
\[-\Delta u = f \quad (x, y)\]where \((x_1,x_2)\in [0,1]^2\), with the boundary condition \(u(t, \pm 1, \pm 1) = 0\)
- Parameters:
a (Tensor , optional) – 3D tensor of shape \([N, K, K]\) or 2D tensor of shape \([K, K]\), where \(N\) is the number of samples, \(K\) is the dimension of the frequencies the coefficient of the wave equation, if
None, it will be randomly generated by \(\mu\sim Unif([-1,1]^{K\times K})\)K (int, optional) – the dimension of the frequencies, if
ais notNone, this parameter will be ignored ifaisNone, it will be used to generate the randomac (float, optional) – the poisson speed, default is \(1.0\)
r (float, optional) – the coefficient of the poisson equation, default is \(0.5\)
- source_term(points, domain='rectangle')[source]¶
Generate the poisson source function at each point in the domain
\[f=\frac{\pi}{K^2} \sum_{i,j=1}^{K} a_{ij} \cdot (i^2 + j^2)^{r} sin(\pi ix) sin(\pi jy)\]- Parameters:
- Returns:
u0 – 1D tensor of shape \([|\mathcal V|]\) or 2D Tensor \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices
- Return type:
- solution(points)[source]¶
Generate the poisson solution function at each point in the domain
\[u(x, y) = \frac{1}{\pi\cdot K^2} \sum_{i,j=1}^{K} a_{ij} \cdot (i^2 + j^2)^{r-1} sin(\pi ix) sin(\pi jy)\]- Parameters:
points (Tensor) – 2D tensor of shape \([|\mathcal V|, 2]\), where \(|\mathcal V|\) is the number of vertices all the points must be in \([0,1]^2\)
- Returns:
u – 1D tenor of shape \([|\\mathcal V|]\) or \([N, |\\mathcal V|]\), where \(N\) is the number of samples, \(|\\mathcal V|\) is the number of vertices
- Return type:
- class HeatMultiFrequency(mu=None, d=2)[source]¶
Bases:
objectMulti-frequency heat equation, with \(0\) boundary condition
\[\frac{\partial u }{\partial t} = \Delta u\]where \(t \in [0,T],\quad(x_1,x_2)\in [-1,1]^2\), with the boundary condition \(u(t, \pm 1, \pm 1) = 0\)
- Parameters:
mu (Tensor , optional) – 2D tensor of shape \([N, d]\) or 1D tensor of shape \([d]\), where \(N\) is the number of samples, \(d\) is the dimension of the frequencies the coefficient of the heat equation, if
None, it will be randomly generated by \(\mu\sim Unif([-1,1]^d)\)d (int, optional) – the dimension of the frequencies, if
muis notNone, this parameter will be ignored ifmuisNone, it will be used to generate the randommu
- initial_condition(points)[source]¶
Generate the heat source function at each point in the domain
\[u(0,x_1,x_2,\mu) = -\frac{1}{d}\sum_{m=1}^d \mu_m sin(\pi m x_1)sin(\pi m x_2)/\sqrt m\]- Parameters:
points (Tensor) – 2D tensor of shape \([|\mathcal V|, 2]\), where \(|\mathcal V|\) is the number of vertices all the points must be in \([-1,1]^2\)
- Returns:
1D tensor of shape \([|\mathcal V|]\) or \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices
- Return type:
- solution(points, t)[source]¶
Generate the poisson solution function at each point in the domain
\[u(t,x_1,x_2,\mu) = -\frac{1}{d}\sum_{m=1}^d \frac{\mu_m}{\sqrt{m}} e^{-2m^2\pi^2t} sin(\pi m x_1)sin(\pi mx_2)\]- Parameters:
- Returns:
ut – 1D tensor of shape \([|\mathcal V|]\) or \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices
- Return type:
- class WaveMultiFrequency(a=None, K=2, c=1.0, r=0.5)[source]¶
Bases:
objectMulti-frequency wave equation, with \(0\) boundary condition
\[u_{tt} = c^2 \Delta u\]where \(t \in [0,T],\quad(x_1,x_2)\in [0,1]^2\), with the boundary condition \(u(t, \pm 1, \pm 1) = 0\)
- Parameters:
a (Tensor , optional) – 3D tensor of shape \([N, K, K]\) or 2D tensor of shape \([K, K]\), where \(N\) is the number of samples, \(K\) is the dimension of the frequencies the coefficient of the wave equation, if
None, it will be randomly generated by \(\mu\sim Unif([-1,1]^{K\times K})\)K (int, optional) – the dimension of the frequencies, if
ais notNone, this parameter will be ignored ifaisNone, it will be used to generate the randomac (float, optional) – the wave speed, default is \(1.0\)
r (float, optional) – the coefficient of the wave equation, default is \(0.5\)
- initial_condition(points)[source]¶
Generate the wave initial function at each point in the domain
\[u(0, x, y, a) = \frac{\pi}{K^2} \sum_{i,j=1}^{K} a_{ij} \cdot (i^2 + j^2)^{-r} sin(\pi ix) sin(\pi jy)\]- Parameters:
points (Tensor) – 2D tensor of shape \([|\mathcal V|, 2]\), where \(|\mathcal V|\) is the number of vertices all the points must be in \([0,1]^2\)
- Returns:
u0 (torch.Tensor) – 1D tensor of shape \([|\mathcal V|]\) \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices
v0 (torch.Tensor) – 1D tensor of shape \([|\mathcal V|]\) \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices
- solution(points, t=0.1)[source]¶
Generate the wave solution function at each point in the domain
\[u(t, x, y, a) = \frac{\pi}{K^2} \sum_{i,j=1}^{K} a_{ij} \cdot (i^2 + j^2)^{-r} sin(\pi ix) sin(\pi jy) cos(c\pi t \sqrt{i^2 + j^2})\]- Parameters:
- Returns:
ut – 1D tenor of shape \([|\mathcal V|]\) or \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices
- Return type: