Geometry#

class planestress.pre.geometry.Geometry(polygons: Polygon | MultiPolygon, materials: Material | list[Material] = Material(name='default', elastic_modulus=1.0, poissons_ratio=0.0, thickness=1.0, density=1.0, color='w'), embedded_geometry: list[Point | Facet] | None = None, tol: int = 12)[source]#

Bases: object

Class describing a geometric region.

Methods

align_center

Aligns the centroid of the geometry to another Geometry, point or origin.

align_to

Aligns the geometry to another Geometry object or point.

calculate_area

Calculates the area of the geometry.

calculate_centroid

Calculates the centroid of the geometry.

calculate_extents

Calculate geometry extents.

compile_geometry

Creates points, facets and holes from shapely geometry.

compile_polygon

Creates points, facets and holes given a Polygon.

create_facet_list

Creates a closed list of facets from a list of points.

create_mesh

Creates and stores a triangular mesh of the geometry.

embed_line

Embeds a point into the mesh.

embed_point

Embeds a point into the mesh.

filter_non_polygons

Filters shapely geometries to return only polygons.

mirror_geometry

Mirrors the geometry about a point on either the x or y axis.

plot_geometry

Plots the geometry.

plot_mesh

Plots the finite element mesh.

rotate_geometry

Rotates the geometry by an angle about a rot_point.

shift_geometry

Shifts the geometry by (x, y).

__init__(polygons: Polygon | MultiPolygon, materials: Material | list[Material] = Material(name='default', elastic_modulus=1.0, poissons_ratio=0.0, thickness=1.0, density=1.0, color='w'), embedded_geometry: list[Point | Facet] | None = None, tol: int = 12) None[source]#

Inits the Geometry class.

Note

Length of materials must equal the number of polygons, i.e. len(polygons.geoms).

Parameters:
  • polygons (Polygon | MultiPolygon) – A shapely.Polygon or shapely.MultiPolygon describing the geometry. A MultiPolygon comprises of a list of Polygon objects, that can describe a geometry with multiple distinct regions.

  • materials (Material | list[Material]) – A list of Material objects describing the material properties of each polygon within the geometry. If a single planestress.pre.Material is supplied, this material is applied to all regions. Defaults to DEFAULT_MATERIAL, i.e. a material with unit properties and a Poisson’s ratio of zero.

  • embedded_geometry (list[Point | Facet] | None) – List of Point or Facet objects to embed into the mesh. Can also be added by using the embed_point() or embed_line() methods. Defaults to None.

  • tol (int) – The points in the geometry get rounded to tol digits. Defaults to 12.

Raises:

ValueError – If the number of materials does not equal the number of polygons.

Example

TODO.

compile_geometry() None[source]#

Creates points, facets and holes from shapely geometry.

compile_polygon(polygon: Polygon, poly_idx: int, loop_idx: int) tuple[list[Point], list[Facet], list[Point], int][source]#

Creates points, facets and holes given a Polygon.

Parameters:
  • polygon (Polygon) – A Polygon object.

  • poly_idx (int) – Polygon index.

  • loop_idx (int) – Loop index.

Returns:

A list of points, facets, and holes, and the current loop index (points, facets, holes, loop_idx).

Return type:

tuple[list[Point], list[Facet], list[Point], int]

create_facet_list(pt_list: list[Point], loop_idx: int) tuple[list[Facet], CurveLoop][source]#

Creates a closed list of facets from a list of points.

Parameters:
  • pt_list (list[Point]) – List of points.

  • loop_idx (int) – Loop index.

Returns:

Closed list of facets and curve loop.

Return type:

tuple[list[Facet], CurveLoop]

embed_point(x: float, y: float, mesh_size: float | None = None) None[source]#

Embeds a point into the mesh.

Parameters:
  • x (float) – x location of the embedded point.

  • y (float) – y location of the embedded point.

  • mesh_size (float | None) – Optional mesh size at the embedded point. If not provided, takes the mesh size of the polygon the point is embedded into. Defaults to None.

Raises:

ValueError – If the point does not lie within any polygons.

Warning

Ensure that embedded points lie within the meshed region and are not located within any holes in the mesh.

Further, due to floating point precision errors, it is recommended that embedded points are also not placed on the edges of polygons.

Note, not all the above conditions are checked.

embed_line(point1: tuple[float, float], point2: tuple[float, float], mesh_size: float | None = None) None[source]#

Embeds a point into the mesh.

Parameters:
  • point1 (tuple[float, float]) – Point location (x, y) of the start of the embedded line.

  • point2 (tuple[float, float]) – Point location (x, y) of the end of the embedded line.

  • mesh_size (float | None) – Optional mesh size along the embedded line. If not provided takes the mesh size of the polygon the line is embedded into. Defaults to None.

Raises:
  • ValueError – If one of the points does not lie within any polygons.

  • ValueError – If the points lie within different polygons.

Warning

Ensure that embedded lines lie within the meshed region and are not located within any holes in the mesh.

Due to floating point precision errors, it is recommended that embedded lines do not touch the edges of polygons.

Embedded lines should not cross any other embedded lines or geometry, i.e. should not cross from one polygon to another.

Note, not all the above conditions are checked.

calculate_area() float[source]#

Calculates the area of the geometry.

Returns:

Geometry area

Return type:

float

calculate_centroid() tuple[float, float][source]#

Calculates the centroid of the geometry.

Returns:

Geometry centroid

Return type:

tuple[float, float]

calculate_extents() tuple[float, float, float, float][source]#

Calculate geometry extents.

Calculates the minimum and maximum x and y values amongst the list of points, i.e. the points that describe the bounding box of the Geometry instance.

Returns:

Minimum and maximum x and y values (x_min, x_max, y_min, y_max)

Return type:

tuple[float, float, float, float]

align_to(other: Geometry | tuple[float, float], on: str, inner: bool = False) Geometry[source]#

Aligns the geometry to another Geometry object or point.

Returns a new Geometry object, representing self translated so that is aligned on one of the outer or inner bounding box edges of other.

Parameters:
  • other (Geometry | tuple[float, float]) – A Geometry or point (x, y) to align to.

  • on (str) – Which side of other to align to, either “left”, “right”, “bottom”, or “top”.

  • inner (bool) – If True, aligns to the inner bounding box edge of other. Defaults to False.

Returns:

New Geometry object aligned to other.

Return type:

Geometry

Example

TODO.

align_center(align_to: Geometry | tuple[float, float] | None = None) Geometry[source]#

Aligns the centroid of the geometry to another Geometry, point or origin.

Returns a new Geometry object, translated such that its centroid is aligned to the centroid of another Geometry, a point, or the origin.

Parameters:

align_to (Geometry | tuple[float, float] | None) – Location to align the centroid to, either another Geometry object, a point (x, y) or None. Defaults to None (i.e. align to the origin).

Raises:

ValueError – If align_to is not a valid input.

Returns:

New Geometry object aligned to align_to.

Return type:

Geometry

Example

TODO.

shift_geometry(x: float = 0.0, y: float = 0.0) Geometry[source]#

Shifts the geometry by (x, y).

Parameters:
  • x (float) – Distance to shift along the x axis. Defaults to 0.0.

  • y (float) – Distance to shift along the y axis. Defaults to 0.0.

Returns:

New Geometry object shifted by (x, y).

Return type:

Geometry

Example

TODO.

rotate_geometry(angle: float, rot_point: tuple[float, float] | str = 'center', use_radians: bool = False) Geometry[source]#

Rotates the geometry by an angle about a rot_point.

Parameters:
  • angle (float) – Angle by which to rotate the geometry. A positive angle leads to a counter-clockwise rotation.

  • rot_point (tuple[float, float] | str) – Point (x, y) about which to rotate the geometry. May also be "center" (rotates about center of bounding box) or “centroid” (rotates about centroid). Defaults to "center".

  • use_radians (bool) – If True, angle is in radians, if False angle is in degrees. Defaults to False.

Returns:

New Geometry object rotated by angle about rot_point.

Return type:

Geometry

Example

TODO.

mirror_geometry(axis: str = 'x', mirror_point: tuple[float, float] | str = 'center') Geometry[source]#

Mirrors the geometry about a point on either the x or y axis.

Parameters:
  • axis (str) – Mirror axis, may be "x" or "y". Defaults to "x".

  • mirror_point (tuple[float, float] | str) – Point (x, y) about which to mirror the geometry. May also be "center" (mirrors about center of bounding box) or “centroid” (mirrors about centroid). Defaults to "center".

Raises:

ValueError – If axis is not "x" or "y".

Returns:

New Geometry object mirrored about mirror_point on axis.

Return type:

Geometry

Example

TODO.

__or__(other: Geometry) Geometry[source]#

Performs a union operation using the | operator.

Note

The material of the first geometry is applied to the entire region of the “unioned” geometry. Keeps the smallest value of tol between the two geometries.

Parameters:

other (Geometry) – Geometry object to union with.

Raises:

ValueError – If shapely is unable to perform the union.

Returns:

New Geometry object unioned with other.

Return type:

Geometry

Example

TODO.

__sub__(other: Geometry) Geometry[source]#

Performs a difference operation using the - operator.

Warning

If self or other contains multiple regions, these regions may be combined into one region after the difference operation. It is recommended to first perform difference operations on Polygon objects, and later combine into into shapely.MultiPolygon objects, see the example below. Check the assignment of materials after a difference operation.

Parameters:

other (Geometry) – Geometry object to difference with.

Raises:

ValueError – If shapely is unable to perform the difference.

Returns:

New Geometry object differenced with other.

Return type:

Geometry

Example

TODO. Use brackets to show order of operations important!

__add__(other: Geometry) Geometry[source]#

Performs an addition operation using the + operator.

Note

The smallest value of tol is applied to both geometries.

Parameters:

other (Geometry) – Geometry object to add to.

Returns:

New Geometry object added with other.

Return type:

Geometry

Example

TODO.

__and__(other: Geometry) Geometry[source]#

Performs an intersection operation using the & operator.

Note

The material of the first geometry is applied to the entire region of the “intersected” geometry. Keeps the smallest value of tol between the two geometries.

Parameters:

other (Geometry) – Geometry object to intersection with.

Raises:

ValueError – If shapely is unable to perform the intersection.

Returns:

New Geometry object intersected with other.

Return type:

Geometry

Example

TODO.

static filter_non_polygons(input_geom: GeometryCollection | LineString | Point | Polygon | MultiPolygon) Polygon | MultiPolygon[source]#

Filters shapely geometries to return only polygons.

Returns a Polygon or a MultiPolygon representing any such Polygon or MultiPolygon that may exist in the input_geom. If input_geom is a LineString or Point, an empty Polygon is returned.

Parameters:

input_geom (GeometryCollection | LineString | Point | Polygon | MultiPolygon) – Shapely geometry to filter

Returns:

Filtered polygon

Return type:

Polygon | MultiPolygon

create_mesh(mesh_sizes: float | list[float] = 0.0, quad_mesh: bool = False, mesh_order: int = 1, serendipity: bool = False, mesh_algorithm: int = 6, subdivision_algorithm: int = 0, fields: list[Field] | None = None) None[source]#

Creates and stores a triangular mesh of the geometry.

Parameters:
  • mesh_sizes (float | list[float]) – A list of the characteristic mesh lengths for each polygon in the Geometry object. If a list of length 1 or a float i passed, then this one size will be applied to all polygons. A value of 0 removes the area constraint. Defaults to 0.0.

  • quad_mesh (bool) – If set to True, recombines the triangular mesh to create quadrilaterals. Defaults to False.

  • mesh_order (int) – Order of the mesh, 1 - linear or 2 - quadratic. Defaults to 1.

  • serendipity (bool) – If set to True, creates serendipity elements for quadrilateral meshes, i.e. creates "Quad8" elements instead of "Quad9" elements. Defaults to False.

  • mesh_algorithm (int) – Gmsh meshing algorithm, see below for more details. Defaults to 6.

  • subdivision_algorithm (int) – Gmsh subdivision algorithm, see below for more details. Defaults to 0.

  • fields (list[Field] | None) – A list of Field objects, describing mesh refinement fields.

Raises:

ValueError – If the length of mesh_sizes does not equal the number of polygons, or is not a float/list of length 1.

mesh_algorithm

TODO - information about meshing algorithm.

subdivision_algorithm

TODO - information about subdivision algorithm.

plot_geometry(analysis_case: AnalysisCase | None = None, **kwargs: Any) matplotlib.axes.Axes[source]#

Plots the geometry.

Optionally also renders the boundary conditions of an analysis case if provided.

Parameters:
  • analysis_case (AnalysisCase | None) – Plots the boundary conditions within an analysis case if provided. Defaults to None.

  • kwargs (Any) – See below.

Keyword Arguments:
  • title (str) – Plot title. Defaults to "Geometry".

  • tags (list[str]) – A list of tags to plot, can contain any of the following: "points", "facets". Defaults to [].

  • legend (bool) – If set to True, plots the legend. Defaults to True.

  • bc_text (bool) – If set to True, plots the values of the boundary conditions. Defaults to False.

  • bc_fmt (str) – Boundary condition text formatting string. Defaults to ".3e".

  • arrow_length_scale (float) – Arrow length scaling factor. Defaults to 0.1.

  • arrow_width_scale (float) – Arrow width scaling factor. Defaults to 0.003.

  • support_scale (float) – Support scaling factor. Defaults to 0.03.

  • num_supports (int) – Number of line supports to plot internally. Defaults to 1.

  • kwargs (dict[str, Any]) – Other keyword arguments are passed to plotting_context().

Returns:

Matplotlib axes object.

Return type:

matplotlib.axes.Axes

Example

TODO.

plot_mesh(**kwargs: Any) matplotlib.axes.Axes[source]#

Plots the finite element mesh.

Parameters:

kwargs (Any) – See below.

Keyword Arguments:
  • title (str) – Plot title. Defaults to "Finite Element Mesh".

  • materials (bool) – If set to True shades the elements with the specified material colors. Defaults to True.

  • node_indexes (bool) – If set to True, plots the indexes of each node. Defaults to False.

  • element_indexes (bool) – If set to True, plots the indexes of each element. Defaults to False.

  • alpha (float) – Transparency of the mesh outlines, \(0 \leq \alpha \leq 1\). Defaults to 0.5.

  • kwargs (dict[str, Any]) – Other keyword arguments are passed to plotting_context().

Raises:

RuntimeError – If a mesh has not yet been generated.

Returns:

Matplotlib axes object.

Return type:

matplotlib.axes.Axes

Example

TODO.