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
isNone
, 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 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
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:
- Returns:
the mesh generator itself
- Return type:
- add_cube(x, y, z, dx, dy, dz)[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:
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 notNone
, this parameter will be ignored ifa
isNone
, it will be used to generate the randoma
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:
- 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:
- 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 notNone
, this parameter will be ignored ifmu
isNone
, 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 (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:
- 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:
- 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 notNone
, this parameter will be ignored ifa
isNone
, it will be used to generate the randoma
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: