LESARD: Meshing modules



Three modules are taking care of already-defined meshes. They respectively define the product-type used for aggregating a mesh information, the handler allowing to load a mesh, extract its properties and convert its format, and lastly an extension that allows the extraction of its corresponding dual properties.

Note

To create meshes whose format is compatible with those modules (and therefore this code), please use the program GeneralMeshing available on request. Upon complains, an interface with the gmsh2 format may be considered and further developped.





Mesh Structure


Aim of the module

Module providing the class MeshStructure furnishing a type defining a mesh compatible with the custom pyMsh or Msh formats. A typical import to have a direct access to the class reads

from  MeshStructure import  MeshStructure

Attributes description

class 3_LESARD.SourceCode.Meshing.MeshStructure.MeshStructure

Class purpose

Class that provides a condensed information about a mesh and details all the information that can be retrieved from a .pyMsh or .Msh format.


Note

The structure does not contain any method, it is here only for defining a product-type handler for a mesh and providing a class allowing pickles of binary meshes.

Contained fields

Mesh definition, filled upon mesh load

MeshResolution (integer) – Fenics-like resolution index

MeshName (string) – Mesh name, corresponding to the geometry

MeshOrder (integer) – Order of the variational discretisation space

MeshType (integer list) – List of all the number of edges that the elements contained in the mesh may have

MeshDofsType (string) – Mshr codename of the variational space set up on the mesh

Convexity (integer) – Indicates if the elements should be convex, not convex, or can be both of them

BdConformity (integer) – Indicates if the mesh should be boundary conformal, up to adding a thin layer of triangles

Mesh information, filled upon mesh load

NbMeshPoints (integer) – Number of mesh vertices of the pysical mesh

NbInnerElements (integer) – Number of 2D elements

NbBoundaryElements (integer) – Number of 1D boundary element, either on the domain’s outer boundary of on the domain’s boundary forming holes

NbDofs (integer) – Total number of Dofs

NbBoundaryDofs (integer) – Number of Dofs on the domain’s boundary

Mesh spatial information, filled upon mesh load

CartesianPoints (2D numpy array) – Nx2 (x,y) coordinates of the physical vertices

DofsXY (2D numpy array) – Nx2 (x,y) coordinates of the Dofs

InnerElements (2-level nested integer list) – List of all the physical vertices forming the inner elements (InnerElements[element][vertex local index] -> vertex global index)

Mesh neighboring information, filled upon mesh load

Neighborhoods (2-level nested integer list) – List of the adjascent element given an element and an edge index [element][edge]-> neighboring element index

BoundaryElements (2-level nested integer list) – List of the 1D boundary elements of the domain [element] -> [list of vertices forming this boundary element]

BoundaryVertices (integer list) – List of all physical vertices that lie on the domain’s boundary (warning: the list index has no meaning here)

BoundaryElementsTag (integer list) – Tag of the boundary elements (=numbering/labelling of boundary segments)

BoundaryVerticesTag (integer list) – Tag of the boundary vertices (labelling of boundary vertices, warning: the list index should be mapped to the index of self.BoundaryVertices, it does not correspond to the vertex)

BoundaryVerticesMap (dict) – Dictionary that maps directly the vertices number to the corresponding tag

Variational information, filled upon mesh load

ElementsBoundaryWiseDofs = [] # 3-level nested list giving the Dofs that are lying on each boundary of each inner element: [element][boundary] -> [dof list]

ElementsBoundaryDofs (2-level nested integer list) – Dofs lying on the boundary of an element [element] -> [dof list]

ElementsInnerDofs (2-level nested integer list) – Dofs living strictly within the element [element] -> [dof list]

ElementsDofs (2-level nested integer list) – Dofs living within the element [element] -> [dof list]

BoundaryDofs (integer list) – List of all the domain’s boundary Dofs (warning: the list index is meaningless here)

BoundaryDofsTag (integer list) – Corresponding labelling of the boundary (warning: the list index has to be mapped with the index of self.BoundaryDofs, it does not relate to the Dof number directly)

BoundaryDofsMap (dict) – Dictionary creatng a direct access to the tag given a boundary Dof index

Mesh properties, usually blank at mesh load/export and filled later on upon properties extraction

hmax (float) – Max element diameter

InnerElementsVolume (float list) – List containing the volume of the elements [element]-> diameter

InnerElementsDiameter (float list) – List containing the max spacing beetween two phisical vertices of an element [element]->Diameter

ElementsBoundaryWiseNormals (3-level nested float list) – List containing the boundary normals of an element [element][boundary]->[nx, ny]

Domain (2-level nested integer list) – List that contains the ordered vertices of the [outer boundary, inner boundary1 (if any), inner boundary2 (if any), …..]

Variational properties filled upon extraction of variational properties

Dofs2Vertex (integer list) – Correspondance table from global Dof numbering to the corresponding Vertex global index [dof]-> vertexId or None

Vertex2Dofs (2-level nested integer list) – Correspondance table from global Vertex numbering to the corresponding Dofs global indices (in case of DG, mapping to several Dofs) [vertex]->[dofs located at thid vertex]

TabulatedVertex (integer list) – Gives locally the correspondance table from Vertex to Dof

VertexLocation (2-level nested integer list) – Give locally the position of the vertices in the list of Dofs [element]->[indices where the dofs are actually located at vertices location]

TabulatedDofs (integer list) – Gives locally the correspondance table from Dof to Vertex

Dof2VertexLoc (3-level nested integer list) – Same as Dof2Vertex, but locally to an element

Vertex2DofLoc (3-level nested integer list) – Same as Vertex2Dofs, but locally to an element


Note

Further fields can be added later to a MeshStructure class’s instance, but a such modified instance should not be saved through pickles with the wish of being later reloaded as such.





Primal Mesh


Aim of the module

Module providing the class MeshHandling gathering the tools to handle a mesh given in a custom MeshStructure format. For having a direct access to the class, import the module by

from Meshing_PrimalMesh import *

Attributes description

class 3_LESARD.SourceCode.Meshing.Meshing_PrimalMesh.MeshHandling(*args)

Class purpose

Class Furnishing the basic tools to handle a mesh that is in a MeshStructure format. The initialisation of the class without any argument (allowing to use LoadMesh and ReadTxtMesh from optional arguments), with a mesh itself in a MeshStructure format or from 4 argument that point to the mesh file to load.


Creating a mesh instance

One can create a void instance, generating an instance with all the empty fields given in the MeshStructure class.

Msh = MeshHandling()

Once such a void instance has been creates, the only possible use of this class is to load a mesh to populate the empty fields.

Msh.ReadTxtMesh(MeshName = "Name", MeshOrder = 3, MeshResolution = 10, MeshDofsType = "DG")
Msh.LoadMesh   (MeshName = "Name", MeshOrder = 3, MeshResolution = 10, MeshDofsType = "DG")

Rather than loading the mesh afterwards, one can load it upon the instance creation. When a single argument is given, it should be a MeshStructure type variable

Msh = MeshHandling(mesh)
# The mesh is automatically copied in the HandlingStructure.

When four arguments are given, a corresponding mesh file should exist in the Pathes.MeshPath folder, either in a binary pyMsh or in raw text file Msh format.

# The mesh contained in the file "MeshFolder/Name_o2_DG_res_10.pyMsh" will be loaded.
# If it does not exist, it tries to load "MeshFolder/Name_o2_DG_res_10.msh" and creates the
# corresponding binary file automatically.
Name       = "Name"
Order      = 2
DofsTypes  = "DG"
Resolution = 10
Msh = MeshHandling(Name, Order, DofsTypes, Resolution)

Note

Once a mesh is loaded, its properties (element’s normals, width, etc) are automatically extracted


Using the mesh within the structure

Complete the mesh properties (cells volume, diameter, normals, automatically called when the instance is created upon argument(s))

Msh.ExtractMeshProperties()

Export the mesh in a Binary pickle format associated to the MeshStructure class (in general, the mesh properties are not exported, this routine is generally used after a fresh load of a text-file containing the raw mesh information to generale a binary one, before any property extraction)

Msh.ExportBinaryMesh()

Export the mesh in a custom .Msh text format

Msh.ExportTextMesh()

Export the mesh in a Gmsh-2.16 format (only for tringles up to order 5, quads up to order 2 and Lagrange elements):

Msh.ExportGmshFormatMesh()

Plot the mesh (save it in a png file, not showing it):

Msh.PlotMesh()

To plot the numbering of the local element (shows it, no save it):

# Select the element you want to information for
i = 50
# Plot the numbering details in this element
Msh.PlotMeshElementDetails(i)

To extract the mesh itself in a variable without carrying the full MeshHandling structure, consider only the field Mesh:

MeshItself = Msh.Mesh
# Or directly
MeshItself = MeshHandling(MeshName,  MeshOrder, MeshType, MeshResolution).Mesh

Note

Each element of the mesh stored in a .pyMsh or .Msh format should be given by an ordered list of vertices, but the orientation is not impacting. However, if some elements are given counter-clockwise, the export to gmsh2-2 will lead to unwished results, at least in the gmsh display.


Initialisation

The initialisation loads the mesh within the structure and extracts its properties (normals, elements’ width etc). Two configurations of arguments are allowed; either furnishing a mesh of a Mesh structure type or all the attributes allowing to load a mesh from an external mesh file.


Parameters

    args (list) – Variable length list of arguments, their type corresponding to their number as follows.

Option 1:
  •   args (MeshStructure) – A mesh of MeshStructure class

Option 2:
  •   args (string) – A file name storing a mesh, either finishing in “.pyMsh” (binary) or “.Msh” (text file).

Option 3:
  •   args[0] (string) – Mesh name

  •   args[1] (integer) – Mesh order

  •   args[2] (string) – types of degrees of freedom in the mshr fenics format

  •   args[3] (integer) – Mesh resolution in the mshr fenics format or equivalent integer represenation



Methods

LoadMesh(**kwargs)

Reads the Mesh as a numpy structure from filename. All the arguments are keyword arguments and are optional. If the keyword “FileName” is given, the mesh will be loaded from the given file. If another keyword is given, it will overrite the parameter already registered in the structure (during the init phase), convenient for reloading a new mesh when changing only one parameter.

Parameters
  • "FileName" (string) – the mesh file to load the mesh from in a text format

  • "MeshName" (string) – the name of the mesh to be loaded

  • "MeshOrder" (integer) – the order of the mesh to be loaded

  • "MeshResolution" (integer) – the resolution of the mesh to be loaded

  • "MeshDofsType" (string) – the type of degrees of freedom in a fenics nomenclature

Returns

Populates or overwrites the fields of the instance.

Return type

None

ReadTxtMesh(**kwargs)

Reads the Mesh as a textfile from filename. All the arguments are keyword arguments and are optional. If a keyword is given, it will overrite the parameter already registered in the structure (during the init phase), convenient for reloading a new mesh when changing only one parameter

Parameters
  • "MeshName" (string) – the name of the mesh to be loaded

  • "MeshOrder" (integer) – the order of the mesh to be loaded

  • "MeshResolution" (integer) – the resolution of the mesh to be loaded

  • "MeshDofsType" (string) – the type of degrees of freedom in a fenics nomenclature

Returns

Populates or overwrites the fields of the instance.

Return type

None

ExtractMeshProperties()

Extracting the basic properties of each inner element

Warning

The boundaryElements list should not be empty

Parameters

None – A freshly loaded mesh in the mesh structure

Returns

    None – Edits the mesh which is already given within the structure, filling the fields

InnerElementsVolume – List of the element’s volumes

InnerElementsDiameter – List of the element’s volumes

ElementsBoundaryWiseNormals – Nested list of the element’s normal to faces

ElementsDofs – List of the degrees of freedom of each element (numbering different from their vertices, even in P1/B1)

ExtractVariationalProperties()

Extracting the variational properties of the mesh.

Warning

The boundaryElements list should not be empty

Parameters

None – A freshly loaded mesh in the mesh structure

Returns

    None – Edits the mesh which is already given within the structure by filling the fields

Dof2Vertex –  correspondance table from global Dof numbering to the corresponding Vertex global index

Vertex2Dofs – correspondance table from global Vertex numbering to the corresponding Dofs global indices (in case of DG, mapping to several Dofs)

TabulatedVertex – Gives locally the correspondance table from Vertex to Dof

VertexLocation – Give locally the position of the vertices in the list of Dofs

TabulatedDofs – Gives locally the correspondance table from Dof to Vertex

Dof2VertexLoc – Same as Dof2Vertex, but locally to an element

Vertex2DofLoc – Same as Vertex2Dofs, but locally to an element

ExportTextMesh()

Export the obtained mesh file

Parameters

None – The mesh structure stored within the instance of mesh

Returns

Exports the mesh files into the same folder as the run code + mesh prefix given in Pathes.MeshPath. The variable self.Meshes is exported in a human readable version in ExportFile+'.Msh'.

Return type

None

ExportBinaryMesh()

Export the obtained the best mesh file

Parameters

None – The mesh structure stored within the instance of mesh

Returns

Exports the mesh files into the same folder as the run code + mesh prefix given in Pathes.MeshPath. The variable self.Meshes is exported in a numpy binary .pyMsh file.

Return type

None

ExportGmshFormatMesh()

Export the mesh in a Gmsh2-2 format. Due to Gmsh2-2 format, the export is only possible for quadrangles up to order 2 and triangles up to order 5. Only accepts Lagrange elements so far.

Parameters

None – The mesh structure stored within the instance of mesh

Returns

Exports the mesh files into the same folder as the run code + mesh prefix given in Pathes.MeshPath. The variable self.Meshes is exported in a gmsh-2.2 version in ExportFile+'.msh'.

Return type

None

PlotMesh()

Plots the mesh given in the structure

Parameters

None – The (filled) mesh loaded and whose properties have been computed within the structure

Returns

Plots the meshes and saves it as png files in Pathes.MeshPath

Return type

None




Dual Mesh


Aim of the module

Module providing the class DualMesh extending MeshHandling. Given a mesh in a MeshStructure format, it computes the properties of the dual mesh (based on the geometry, not the variational space).

For having a direct access to the class, import the module by

from Meshing_DualMesh import *

Attributes description

class 3_LESARD.SourceCode.Meshing.Meshing_DualMesh.DualMesh(*params)

Class purpose

Class that inherits from MeshHandling and that completes the MeshStructure class by the Dual mesh informations. The generation of the properties is automatic on the instance creation and enriches the MeshStructure class by the following fields

DualEdgesWidths (float numpy array) – the length of the segments connecting the edges mid-points with the barycenter, for each edge

DualNormals (2D float numpy array) – the normal of the segments connecting the edges mid-points with the barycenter, for each edge, pointing from the upflow vertex to downflow vertex according to the shape’s orientation

DualCenters (2D float numpy array) – the coordinates of each edge’s center, followed by the ones of the center of mass

DualAreas (2D float numpy array) – the areas corresponding to each subelement attached to each vertex

DualAreasTot (float numpy array) – the areas corresponding to the control cell center attached to each vertex


Note

Watch out not to save the DualMesh instance as a pyMsh binary file. Indeed, the pickle is planned for the MeshStructure class, not its extension.


Creating a mesh instance

The class is inherited from MeshHandling and does not take any further argument when initialising the instance. Please therefore chek the instance creation of MeshHandling for complete details. However, a simple instance creation can be done by

Msh = DualMesh(MeshName,  MeshOrder, MeshType, MeshResolution)

To retrieve the mesh itself without carrying the full DualMesh structure consider only the field Mesh:

MeshItself = DualMesh(MeshName,  MeshOrder, MeshType, MeshResolution).Mesh

Note

Once a mesh is loaded, its properties (element’s normals, width, etc) and dual properties (element’s dual areas, element’s barycenter and edges mid points, etc) are both automatically extracted.


Using the mesh within the structure

The loaded mesh sees its dual information extracted upon initialisation. If nevertheless the properties are to be extracted once more, consider the following command.

Msh.ComputeDualProperties()

All the methods from the PrimalMesh are available, however they do not consider the DualMesh extra fields. By example, the PlotMesh method will not show the dual informations.

Msh.PlotMesh()

Warning

Pay attention not to use the method ExportBinaryMesh as loading a binary exported DualMesh as a primal mesh later may fail. If you created a DualMesh instance from a mesh stored in text file, a corresponding binary file will be created when loading first the primal mesh solely.


Initialisation

The initialisation is identical to the one of the class MeshHandling. It loads the mesh within the structure and extracts its primal and dual properties. Two configurations of arguments are allowed; either furnishing a mesh of a Mesh structure type or all the attributes allowing to load a mesh from an external mesh file.


Parameters

    args (list) – Variable length list of arguments, their type corresponding to their number as follows.

Option 1:
  •   args (MeshStructure) – A mesh of MeshStructure class

Option2:
  •   args[0] (string) – Mesh name

  •   args[1] (integer) – Mesh order

  •   args[2] (string) – types of degrees of freedom in the mshr fenics format

  •   args[3] (integer) – Mesh resolution in the mshr fenics format or equivalent integer represenation



Methods

ComputeDualProperties()

Computing the essential properties of the associated dual mesh. .. warning: Not valid for non-convex elements so far

Parameters

None – A freshly loaded mesh in the mesh structure

Returns

    None – Edits the mesh which is already given within the structure, adding the fields

DualEdgesWidths (float numpy array) – the length of the segments connecting the edges mid-points with the barycenter, for each edge

DualNormals (2D float numpy array) – the normal of the segments connecting the edges mid-points with the barycenter, for each edge, pointing from the upflow vertex to downflow vertex according to the shape’s orientation

DualCenters (2D float numpy array) – the coordinates of each edge’s center, followed by the ones of the center of mass

DualAreas (2D float numpy array) – the areas corresponding to each subelement attached to each vertex

DualAreasTot (float numpy array) – the areas corresponding to the control cell center attached to each vertex