torch_fem.mesh

Mesh

class Mesh(mesh)[source]

Bases: Module

Parameters:

mesh (meshio.Mesh()) – a meshio mesh object

points

2D tensor of shape \([|\mathcal V|, D]\), where \(|\mathcal V|\) is the number of points and \(D\) is the dimension of the space the coordinates of the points

Type:

torch.Tensor

cells

Each key is a torch_fem.shape.element_type(), and for each element_type, there is a corresponding 2D tensor of shape \([|\mathcal V, B]\), where \(B\) is the number of basis functions the cells of the mesh

Type:

BufferDict[str, torch.Tensor]

point_data

Each key is a torch_fem.shape.element_type(), the point data

Type:

BufferDict[str, torch.Tensor], optional

cell_data

Each key is a torch_fem.shape.element_type(), the cell data

Type:

BufferDict[str, BufferDict[int, torch.Tensor]], optional

field_data

Each key is a torch_fem.shape.element_type(), the field data

Type:

BufferDict[str, torch.Tensor], optional

cell_sets

Each key is a torch_fem.shape.element_type(), the cell sets

Type:

dict, optional

dim2eletyp

Each key is a dimension, and the value is a list of element types of the dimension

Type:

Dict[int, List[str]]

default_eletyp

the default element type

Type:

str

default_element_type

the default element type

Type:

str

register_point_data(key, value)[source]

Add key-value pair to point_data buffer, since the point_data is a torch_fem.nn.BufferDict, you are recommended to use this method instead of __setitem__

Parameters:
  • key (str) – the key of the value

  • value (torch.Tensor) – 1D tensor of shape \([|\mathcal V|,...]\), where \(\mathcal V\) is the number of nodes/vertices/points, the value to be registered

Returns:

self will be returned

Return type:

torch_fem.mesh.Mesh

to_meshio()[source]
Returns:

the meshio mesh object

Return type:

meshio.Mesh

save(file_name: str, file_format: str = None)[source]
Parameters:
  • file_name (str) – the name of the file

  • file_format (str) – the format of the file, e.g., ‘msh’, ‘vtk’, ‘obj’ default is the file extension

Returns:

self will be returned

Return type:

torch_fem.mesh.Mesh

to_file(file_name: str, file_format: str = None)[source]
Parameters:
  • file_name (str) – the name of the file

  • file_format (str) – the format of the file, e.g., ‘msh’, ‘vtk’, ‘obj’ default is the file extension

Returns:

self will be returned

Return type:

torch_fem.mesh.Mesh

node_adjacency(element_type=None)[source]

get the node adjacency matrix, inside each element, the nodes are considered fully connected

Parameters:

element_type (str or Iterable[str] or None) – the type of the elements if None is the default_element_type default : None

Returns:

the adjacency matrix of nodes \([|\mathcal V|,|\mathcal V|]\), where \(|\mathcal V|\) is the number of nodes

Return type:

SparseMatrix

element_adjacency(element_type=None)[source]

get the element adjacency matrix, the element are considered connected only if they share a boundary/facet

Parameters:

element_type (str or Iterable[str] or None) – the type of the elements, should be of same dimension if None is the default_element_type default : None

Returns:

the adjacency matrix of elements \([|\mathcal C|,|\mathcal C|]\), where \(|\mathcal C|\) is the number of elements

Return type:

SparseMatrix

elements(element_type=None)[source]
Parameters:

element_type (str or Iterable[str] or None) – the type of the elements if None is the default_eletyp will be used default : None

Returns:

if element_type is str, return the corresponding elements connections of shape \([|\mathcal C|, B]\), where \(|\mathcal C|\) is the number of elements and \(B\) is the number of basis functions if element_typs is Iterable[str], return the mapping of corresponding elements connections of shape \([|\mathcal C|, B]\), where \(|\mathcal C|\) is the number of elements and \(B\) is the number of basis functions if element_type is None, the element_type will be the default_element_type and do as above

Return type:

torch.Tensor or Dict[str, torch.Tensor]

clone()[source]

The gradient will vanish if you use torch.Tensor.clone to clone the mesh, so we provide this method to clone the mesh :returns: the cloned mesh :rtype: torch_fem.mesh.Mesh

plot(values=None, save_path=None, backend='matplotlib', dt=None, show_mesh=False)[source]
Parameters:
  • values (None or Dict[str, torch.Tensor] or Dict[str, List[torch.Tensor]]) – the values to plot, if None, only plot the mesh if Dict[str, torch.Tensor], a static subplots will be plotted, the key is the name of the subplot, the value is of shape \([|\mathcal V|]\), where \(|\mathcal V|\) is the number of points if Dict[str, List[torch.Tensor]], a mp4/gif will be plotted, the key is the name of the subplot, each item in the list is of shape \([|\mathcal V|]\), where \(|\mathcal V|\) is the number of points default: None

  • save_path (str or None) – the path to save the plot, if None, it will not be saved if the values is passed in as Dict[str, List[torch.Tensor]], the save_path must endswith ‘.mp4’ or ‘.gif’ default: None

  • backend (str) – the backend of the plot, must be one of [‘matplotlib’, ‘pyvista’] default: ‘matplotlib’

  • dt (float or None) – the time interval between each frame, only used when values is passed in as Dict[str, List[torch.Tensor]] default: None

  • show_mesh (bool) – whether to show the mesh, when values is passed in as Dict[str, List[torch.Tensor]] or Dict[str, torch.Tensor] default: False

property n_point
Returns:

the number of nodes/vertices/points \(|\mathcal V|\)

Return type:

int

property boundary_mask
Returns:

1D tensor of shape \([|\mathcal V|]\), where \(|\mathcal V|\) is the number of points the mask of the boundary points, "is_boundary" key or "boundary_mask" key is required in point_data

Return type:

torch.Tensor

property default_element_type
Returns:

the default element type, if the mesh is composed of mixed elements, it will return List[str], otherwise it will return str

Return type:

str or List[str]

property dtype
Returns:

the data type of the points, e.g., torch.float32, torch.float64

Return type:

torch.dtype

property device
Returns:

the device of the points, e.g., torch.device(“cpu”), torch.device(“cuda:0”)

Return type:

torch.device

classmethod from_meshio(mesh)[source]
Parameters:

mesh (meshio.Mesh) – a meshio mesh object

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

classmethod read(file_name: str, file_format: str = None)[source]
Parameters:
  • file_name (str) – the name of the file

  • file_format (str) – the format of the file, e.g., ‘msh’, ‘vtk’, ‘obj’ default is the file extension

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

classmethod from_file(file_name: str, file_format: str = None)[source]
Parameters:
  • file_name (str) – the name of the file

  • file_format (str) – the format of the file, e.g., ‘msh’, ‘vtk’, ‘obj’ default is the file extension

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static gen_rectangle(chara_length=0.1, order=1, element_type='tri', left=0.0, right=1.0, bottom=0.0, top=1.0, visualize=False, cache_path=None)[source]
Parameters:
  • chara_length (float, optional) – the characteristic length of the mesh, default: 0.1

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

  • element_type (str, optional) – the type of the element, default: "tri"

  • left (float, optional) – the left boundary of the rectangle, default: 0.0

  • right (float, optional) – the right boundary of the rectangle, default: 1.0

  • bottom (float, optional) – the bottom boundary of the rectangle, default: 0.0

  • top (float, optional) – the top boundary of the rectangle, 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 torch_fem.dataset.mesh.gen_rectangle(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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, optional) – the characteristic length of the mesh, default: 0.1

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

  • element_type (str, optional) – the type of the element, default: "quad"

  • outer_left (float, optional) – the left boundary of the outer rectangle, default: 0.0

  • outer_right (float, optional) – the right boundary of the outer rectangle, default: 1.0

  • outer_bottom (float, optional) – the bottom boundary of the outer rectangle, default: 0.0

  • outer_top (float, optional) – the top boundary of the outer rectangle, default: 1.0

  • inner_left (float, optional) – the left boundary of the inner rectangle, default: 0.25

  • inner_right (float, optional) – the right boundary of the inner rectangle, default: 0.75

  • inner_bottom (float, optional) – the bottom boundary of the inner rectangle, default: 0.25

  • inner_top (float, optional) – the top boundary of the inner rectangle, 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 torch_fem.dataset.mesh.gen_hollow_rectangle(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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)[source]
Parameters:
  • chara_length (float, optional) – the characteristic length of the mesh, default: 0.1

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

  • element_type (str, optional) – the type of the element, default: "tri"

  • cx (float, optional) – the x coordinate of the center of the circle, default: 0.0

  • cy (float, optional) – the y coordinate of the center of the circle, default: 0.0

  • r (float, optional) – the radius of the circle, 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 torch_fem.dataset.mesh.gen_circle(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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)[source]
Parameters:
  • chara_length (float, optional) – the characteristic length of the mesh, default: 0.1

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

  • element_type (str, optional) – the type of the element, default: "quad"

  • cx (float, optional) – the x coordinate of the center of the circle, default: 0.0

  • cy (float, optional) – the y coordinate of the center of the circle, default: 0.0

  • r_inner (float, optional) – the inner radius of the circle, default: 1.0

  • r_outer (float, optional) – the outer radius of the circle, default: 2.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 torch_fem.dataset.mesh.gen_hollow_circle(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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, optional) – the characteristic length of the mesh, default: 0.1

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

  • element_type (str, optional) – the type of the element, default: "quad"

  • left (float, optional) – the left boundary of the rectangle, default: 0.0

  • right (float, optional) – the right boundary of the rectangle, default: 1.0

  • bottom (float, optional) – the bottom boundary of the rectangle, default: 0.0

  • top (float, optional) – the top boundary of the rectangle, default: 1.0

  • top_inner (float, optional) – the top inner boundary of the rectangle, default: 0.5

  • right_inner (float, optional) – the right inner boundary of the rectangle, default: 0.5

  • 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 torch_fem.dataset.mesh.gen_L(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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.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 torch_fem.dataset.mesh.gen_cube(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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.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 torch_fem.dataset.mesh.gen_hollow_cube(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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, optional) – the characteristic length of the mesh, default: 0.1

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

  • cx (float, optional) – the x coordinate of the center of the sphere, default: 0.0

  • cy (float, optional) – the y coordinate of the center of the sphere, default: 0.0

  • cz (float, optional) – the z coordinate of the center of the sphere, default: 0.0

  • r (float, optional) – the radius of the sphere, 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 torch_fem.dataset.mesh.gen_sphere(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh

static 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, optional) – the characteristic length of the mesh, default: 0.1

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

  • cx (float, optional) – the x coordinate of the center of the sphere, default: 0.0

  • cy (float, optional) – the y coordinate of the center of the sphere, default: 0.0

  • cz (float, optional) – the z coordinate of the center of the sphere, default: 0.0

  • r_inner (float, optional) – the inner radius of the sphere, default: 1.0

  • r_outer (float, optional) – the outer radius of the sphere, default: 2.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 torch_fem.dataset.mesh.gen_hollow_sphere(), default: None

Returns:

the mesh object

Return type:

torch_fem.mesh.Mesh