torch_fem.dataset

Mesh

class MeshGen(element_type=None, dimension=2, order=1, chara_length=0.1, cache_path='./tmp.msh')[source]

Bases: object

Parameters:
  • 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\).

Examples

  1. generate rectangle mesh with triangle elements:

    from torch_fem 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 torch_fem 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()
    
add_rectangle(left, bottom, width, height, element='tri')[source]

add a rectangle to the geometry

Parameters:
  • 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”

Returns:

the mesh generator itself

Return type:

MeshGen

remove_rectangle(left, bottom, width, height)[source]

remove the rectangle from the geometry

Parameters:
  • 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

Returns:

the mesh generator itself

Return type:

MeshGen

add_circle(cx, cy, r, element='tri')[source]

add a circle to the geometry

Parameters:
  • 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”

Returns:

the mesh generator itself

Return type:

MeshGen

remove_circle(cx, cy, r)[source]

remove the cirlce from the geometry

Parameters:
  • cx (float) – the x coordinate of the center

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

  • r (float) – the radius of the circle

Returns:

the mesh generator itself

Return type:

MeshGen

add_cube(x, y, z, dx, dy, dz)[source]

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

Parameters:
  • 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

Returns:

the mesh generator itself

Return type:

MeshGen

remove_cube(x, y, z, dx, dy, dz)[source]

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

Parameters:
  • 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

Returns:

the mesh generator itself

Return type:

MeshGen

add_sphere(x, y, z, r)[source]

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

Parameters:
  • 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

Returns:

the mesh generator itself

Return type:

MeshGen

remove_sphere(x, y, z, r)[source]

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

Parameters:
  • 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

Returns:

the mesh generator itself

Return type:

MeshGen

gen(show=False)[source]

generate the mesh from the geometry

Parameters:

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

Returns:

the generated mesh

Return type:

torch_fem.mesh.Mesh

Equation

class PoissonMultiFrequency(a=None, K=2, c=1.0, r=0.5)[source]

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

Parameters:
  • a (torch.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\)

initial_condition(points)[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:

points (torch.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 – 1D tensor of shape \([|\mathcal V|]\) \([N, |\mathcal V|]\), where \(N\) is the number of samples, \(|\mathcal V|\) is the number of vertices

Return type:

torch.Tensor

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 (torch.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:

torch.Tensor

class HeatMultiFrequency(mu=None, d=2)[source]

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

Parameters:
  • mu (torch.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

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 (torch.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:

torch.Tensor

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

  • t (float) – the time

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:

torch.Tensor

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

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

Parameters:
  • a (torch.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\)

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 (torch.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:
  • points (torch.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\)

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:

torch.Tensor