build_mesh: Build unstructured meshes

View source: R/build_mesh.R

build_meshR Documentation

Build unstructured meshes

Description

This function is used to build an unstructured mesh (around nodes or elements) from node coordinates and connections as a SpatialPolygonsDataFrame. This function was designed with the WeStCOMS unstructured mesh in mind.

Usage

build_mesh(
  nodexy,
  trinodes,
  mesh_type = "element",
  proj4string = sp::CRS(as.character("+init=epsg:4326")),
  cl = NULL,
  pass2varlist = NULL
)

Arguments

nodexy

A dataframe containing node IDs and coordinates. The dataframe should have three columns: 'node_id', 'x' and 'y'. See dat_nodexy for an example.

trinodes

A dataframe containing element IDs and the surrounding nodes (i.e. which nodes are linked to which other nodes). The dataframe should have four columns: 'element_id', 'node1', 'node2' and 'node3'. See dat_trinodes for an example

mesh_type

A character specifying the mesh type to be built. There are two options: "node" or "element". mesh_type = "node" builds a mesh based on nodes (i.e. around elements). This is useful for plotting conditions resolved at elements. mesh_type = "element" builds a mesh around nodes based on elements. This is useful for plotting conditions resolved at nodes.

proj4string

A projection string of class CRS-class. The World Geodetic System 84 (WGS84), i.e. proj4string = sp::CRS(as.character("+init=epsg:4326")), is the default.

cl

A cluster object created by makeCluster. This is required if you want to run the algorithm in parallel, which can improve computation time (especially for large meshes). If supplied, the connection to the cluster is stopped within the function.

pass2varlist

A list containing the names of exported objects. This may be required if cl is supplied. This is passed to the varlist argument of clusterExport. Exported objects must be located in the global environment.

Value

An SpatialPolygonsDataFrame (see SpatialPolygonsDataFrame-class). Each polygon has an ID corresponding to the ID of the node or element which is surrounds, as supplied by the nodexy or trinodes dataframe respectively.

Author(s)

Edward Lavender

See Also

SpatialPolygonsDataFrame-class for the output class; dat_nodexy for an example nodexy dataframe; dat_trinodes for an example trinodes dataframe; makeCluster and clusterExport for more information on parallelisation.

Examples


# 1) Build a mesh around elements (based on nodes) on a single processor
# You will receive a warning when you run this:
# ...'In sp::Polygon(coords, hole) : less than 4 coordinates in polygon'.
# This can be safely ignored. This is because each polygon is a prism;
# ... i.e., only comprised of three coordinates.

mesh_around_elements <- build_mesh(nodexy = dat_nodexy,
                                   trinodes = dat_trinodes,
                                   mesh_type = "node",
                                   cl = NULL,
                                   pass2varlist = NULL)


# 2) Build a mesh around nodes (based on elements) on a single processor

mesh_around_nodes <- build_mesh(nodexy = dat_nodexy,
                                trinodes = dat_trinodes,
                                mesh_type = "element",
                                cl = NULL,
                                pass2varlist = NULL)

# 3) Build a mesh around elements (based on nodes) using parallel processing

# Define cluster object:
cl <- parallel::makeCluster(2L)
# Run the build_mesh algorithm in parallel by supplying a cluster:
mesh_around_elements <- build_mesh(nodexy = dat_nodexy,
                                   trinodes = dat_trinodes,
                                   mesh_type = "node",
                                   cl = cl,
                                   pass2varlist = c("dat_nodexy", "dat_trinodes"))
# Note that the connection with the cluster is closed within the function.



edwardlavender/fvcom.tbx documentation built on Nov. 26, 2022, 10:28 p.m.