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)[源代码]

基类:object

参数:
  • element_type (str, optional) -- If element_type is None, then it will generate a mix mesh. Otherwise, the order and element will be determined by element_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\).

示例

  1. 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
    
  2. 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)[源代码]
add_rectangle(left, bottom, width, height, element='tri')[源代码]

add a rectangle to the geometry

参数:
  • left (float) -- the left boundary of the rectangle

  • bottom (float) -- the bottom boundary of the rectangle

  • width (float) -- the width of the rectangle

  • height (float) -- the height of the rectangle

  • element (str, optional) -- the type of the element, can be "tri" or "quad" default: "tri"

返回:

the mesh generator itself

返回类型:

MeshGen

remove_rectangle(left, bottom, width, height)[源代码]

remove the rectangle from the geometry

参数:
  • left (float) -- the left boundary of the rectangle

  • bottom (float) -- the bottom boundary of the rectangle

  • width (float) -- the width of the rectangle

  • height (float) -- the height of the rectangle

返回:

the mesh generator itself

返回类型:

MeshGen

add_circle(cx, cy, r, element='tri')[源代码]

add a circle to the geometry

参数:
  • cx (float) -- the x coordinate of the center

  • cy (float) -- the y coordinate of the center

  • r (float) -- the radius of the circle

  • element (str, optional) -- the type of the element, can be "tri" or "quad" default: "tri"

返回:

the mesh generator itself

返回类型:

MeshGen

remove_circle(cx, cy, r)[源代码]

remove the cirlce from the geometry

参数:
  • cx (float) -- the x coordinate of the center

  • cy (float) -- the y coordinate of the center

  • r (float) -- the radius of the circle

返回:

the mesh generator itself

返回类型:

MeshGen

add_cube(x, y, z, dx, dy, dz, element: str = 'tet')[源代码]

add a cube to the geometry, only works for 3d

参数:
  • x (float) -- the x coordinate of the center

  • y (float) -- the y coordinate of the center

  • z (float) -- the z coordinate of the center

  • dx (float) -- the width of the cube

  • dy (float) -- the height of the cube

  • dz (float) -- the depth of the cube

返回:

the mesh generator itself

返回类型:

MeshGen

remove_cube(x, y, z, dx, dy, dz)[源代码]

remove the cube from the geometry, only works for 3d

参数:
  • x (float) -- the x coordinate of the center

  • y (float) -- the y coordinate of the center

  • z (float) -- the z coordinate of the center

  • dx (float) -- the width of the cube

  • dy (float) -- the height of the cube

  • dz (float) -- the depth of the cube

返回:

the mesh generator itself

返回类型:

MeshGen

add_sphere(x, y, z, r)[源代码]

add a sphere to the geometry, only works for 3d

not supported for hex element because of algorithm issue

参数:
  • x (float) -- the x coordinate of the center

  • y (float) -- the y coordinate of the center

  • z (float) -- the z coordinate of the center

  • r (float) -- the radius of the sphere

返回:

the mesh generator itself

返回类型:

MeshGen

remove_sphere(x, y, z, r)[源代码]

remove the sphere from the geometry, only works for 3d

参数:
  • x (float) -- the x coordinate of the center

  • y (float) -- the y coordinate of the center

  • z (float) -- the z coordinate of the center

  • r (float) -- the radius of the sphere

返回:

the mesh generator itself

返回类型:

MeshGen

gen(show=False)[源代码]

generate the mesh from the geometry

参数:

show (bool, optional) -- whether to show the mesh in the gmsh gui default: False

返回:

the generated mesh

返回类型:

Mesh

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)[源代码]
参数:
  • 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

返回类型:

Mesh

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)[源代码]
参数:
  • 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)[源代码]
参数:
  • 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

返回类型:

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)[源代码]
参数:
  • 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

返回类型:

Mesh

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)[源代码]
参数:
  • 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

返回类型:

Mesh

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)[源代码]
参数:
  • chara_length (float, optional) -- the characteristic length of the mesh, default: 0.1

  • order (int, optional) -- the order of the basis function, default: 1

  • left (float, optional) -- the left boundary of the cube, default: 0.0

  • right (float, optional) -- the right boundary of the cube, default: 1.0

  • bottom (float, optional) -- the bottom boundary of the cube, default: 0.0

  • top (float, optional) -- the top boundary of the cube, default: 1.0

  • front (float, optional) -- the front boundary of the cube, default: 0.0

  • back (float, optional) -- the back boundary of the cube, default: 1.0

  • visualize (bool, optional) -- whether to visualize the mesh, default: False

  • cache_path (str, optional) -- the path to save the mesh, if None, it will be decided by gen_cube(), default: None

返回:

the mesh object

返回类型:

Mesh

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')[源代码]
参数:
  • chara_length (float, optional) -- the characteristic length of the mesh, default: 0.1

  • order (int, optional) -- the order of the basis function, default: 1

  • outer_left (float, optional) -- the left boundary of the outer cube, default: 0.0

  • outer_right (float, optional) -- the right boundary of the outer cube, default: 1.0

  • outer_bottom (float, optional) -- the bottom boundary of the outer cube, default: 0.0

  • outer_top (float, optional) -- the top boundary of the outer cube, default: 1.0

  • outer_front (float, optional) -- the front boundary of the outer cube, default: 0.0

  • outer_back (float, optional) -- the back boundary of the outer cube, default: 1.0

  • inner_left (float, optional) -- the left boundary of the inner cube, default: 0.25

  • inner_right (float, optional) -- the right boundary of the inner cube, default: 0.75

  • inner_bottom (float, optional) -- the bottom boundary of the inner cube, default: 0.25

  • inner_top (float, optional) -- the top boundary of the inner cube, default: 0.75

  • inner_front (float, optional) -- the front boundary of the inner cube, default: 0.25

  • inner_back (float, optional) -- the back boundary of the inner cube, default: 0.75

  • visualize (bool, optional) -- whether to visualize the mesh, default: False

  • cache_path (str, optional) -- the path to save the mesh, if None, it will be decided by gen_hollow_cube(), default: None

返回:

the mesh object

返回类型:

Mesh

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)[源代码]
参数:
  • 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

返回类型:

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)[源代码]
参数:
  • 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

返回类型:

None

equation

class PoissonMultiFrequency(a=None, K=2, r=-0.5)[源代码]

基类:object

Multi-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\)

参数:
  • 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 a is not None, this parameter will be ignored if a is None, it will be used to generate the random a

  • c (float, optional) -- the poisson speed, default is \(1.0\)

  • r (float, optional) -- the coefficient of the poisson equation, default is \(0.5\)

__init__(a=None, K=2, r=-0.5)[源代码]
source_term(points, domain='rectangle')[源代码]

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)\]
参数:
  • 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\)

  • domain (str, optional) -- Domain shape. Analytical solution only supports "rectangle" (default).

返回:

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

返回类型:

Tensor

solution(points)[源代码]

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)\]
参数:

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\)

返回:

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

返回类型:

Tensor

class HeatMultiFrequency(mu=None, d=2)[源代码]

基类:object

Multi-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\)

参数:
  • 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 mu is not None, this parameter will be ignored if mu is None, it will be used to generate the random mu

__init__(mu=None, d=2)[源代码]
initial_condition(points)[源代码]

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\]
参数:

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\)

返回:

1D tensor of shape \([|\mathcal V|]\) or \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices

返回类型:

Tensor

solution(points, t)[源代码]

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)\]
参数:
  • points (Tensor) -- 2D tensor of shape \([|\mathcal V|, 2]\), where \(|\mathcal V|\) is the number of vertices

  • t (float) -- the time

返回:

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

返回类型:

Tensor

class WaveMultiFrequency(a=None, K=2, c=1.0, r=0.5)[源代码]

基类:object

Multi-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\)

参数:
  • 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 a is not None, this parameter will be ignored if a is None, it will be used to generate the random a

  • c (float, optional) -- the wave speed, default is \(1.0\)

  • r (float, optional) -- the coefficient of the wave equation, default is \(0.5\)

__init__(a=None, K=2, c=1.0, r=0.5)[源代码]
initial_condition(points)[源代码]

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)\]
参数:

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\)

返回:

  • 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)[源代码]

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})\]
参数:
  • 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\)

  • t (float) -- the time, default is \(0.1\)

返回:

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

返回类型:

Tensor