Mesh#

See Mesh in MIKE IO Documentation

import numpy as np
import matplotlib.pyplot as plt
import mikeio

A simple mesh#

Let’s consider a simple mesh consisting of 2 triangular elements.

Note

Example data can be found in the mini_book/data folder in this zip-file.

fn = "data/two_elements.mesh"
with open(fn, "r") as f:
    print(f.read())
100079 1000 4  UTM-31
1 0.0 0.0 -10.0 1 
2 3.0 0.0 -10.0 2 
3 3.0 3.0 -10.0 2 
4 0.0 3.0 -10.0 1 
2 3 21
1 1 2 4
2 2 3 4 
msh = mikeio.open(fn)
msh
<Mesh>
number of nodes: 4
number of elements: 2
projection: UTM-31
msh.plot(show_mesh=True);
_images/0b777b944067e9870c419fee19073910ad3baaf7c4ba7bfc056b8de67681ead1.png
msh.node_coordinates
array([[  0.,   0., -10.],
       [  3.,   0., -10.],
       [  3.,   3., -10.],
       [  0.,   3., -10.]])
msh.element_table
[array([0, 1, 3], dtype=int32), array([1, 2, 3], dtype=int32)]
msh.element_coordinates
array([[  1.,   1., -10.],
       [  2.,   2., -10.]])
msh.geometry.get_element_area()
array([4.5, 4.5])

Let’s plot the node and element coordinates:

xn, yn = msh.node_coordinates[:,0], msh.node_coordinates[:,1]
xe, ye = msh.element_coordinates[:,0], msh.element_coordinates[:,1]

ax = msh.plot(show_mesh=True)
ax.plot(xn, yn, 'ro', markersize=10)
ax.plot(xe, ye, 'bx', markersize=10)
[<matplotlib.lines.Line2D at 0x7fb2a666d450>]
_images/6c8d0ef2e10f3a7d68410352646ca40f2df38558b27c0ca2b358a822e8f000a6.png

Boundary polygons#

It can sometimes be convenient to have mesh boundary as a polygon (or multiple in case of more complex meshes).

bxy = msh.geometry.boundary_polygons.exteriors[0].xy
plt.plot(bxy[:,0], bxy[:,1])
plt.axis("equal");
_images/7c727d51b4adaae21ec85875cc7b045f88796437dd37611fa0423879b340b98a.png

Inside domain?#

MIKE IO has a method for determining if a point (or a list of points) is inside the domain:

  • contains()

pt_1 = [2.0, 1.2]
msh.geometry.contains(pt_1)[0]
np.True_
# or multiple points at the same time
pt_2 = [4.0, 1.2]
pts = np.array([pt_1, pt_2])
msh.geometry.contains(pts)
array([ True, False])
plt.plot(bxy[:,0], bxy[:,1], label='boundary')
plt.plot(xe[0], ye[0], 'b*', markersize=10, label="center, elem 0")
plt.plot(xe[1], ye[1], 'c*', markersize=10, label="center, elem 1")
plt.plot(*pt_1, 'go', markersize=10, label="pt_1")
plt.plot(*pt_2, 'rs', markersize=10, label="pt_2")
plt.axis("equal")
plt.legend(loc="upper right");
_images/ada4cf7024f134c7690adfd6fc65505b4a35e5c79574b953f6ef251b7224910b.png

Find element containing point#

MIKE IO has a method for obtaining the index of the element containing a point:

  • find_index()

g = msh.geometry
g.find_index(coords=pt_1)[0]
np.int64(1)

MIKE IO also has a method for obtaining a list of the n closest element centers:

  • find_nearest_elements()

g.find_nearest_elements(pt_1)
1
g.find_nearest_elements(pt_1, return_distances=True)
(1, 0.8)
g.find_nearest_elements(pt_1, n_nearest=2)
array([1, 0])
# for multiple points
g.find_nearest_elements(pts, return_distances=True)
(array([1, 1]), array([0.8       , 2.15406592]))

A larger mesh#

A dfsu file also has a flexible mesh geometry attribute.

fn = "data/FakeLake.dfsu"
dfs = mikeio.open(fn)
g = dfs.geometry
g.plot();
_images/b1440bc710ab0be73650b333eee1fec782a9ff2f7b9bf96d2c483b6ab3a541bd.png
g.max_nodes_per_element
4
g.plot.boundary_nodes();
_images/9871cff7ecb7af0b1b289307d6b2f984ac3d681b0bfce5457df586647f710984.png
bnd = g.boundary_polygons
ext0 = bnd.exteriors[0]
plt.plot(ext0.xy[:,0], ext0.xy[:,1], label='exterior 0')
int0 = bnd.interiors[0]
plt.plot(int0.xy[:,0], int0.xy[:,1], label='interior 0')
plt.legend();
_images/c88da1546f96bd228c1877af82991a43ea3380892b8f91f20cf9010a3c73f2d1.png

Change depth#

# zn == nodes, not elements!
nc = g.node_coordinates
nc[:,2] = np.clip(nc[:,2], -15, 0) # clip depth to interval [-15,0]

Element coordinates are cached, delete to force recalculation

del g.element_coordinates
g.plot();
_images/50b0c2bfeab4c5ff5b5ff96e5d71c7d0559cc39dece3d83b7d1f674155f2feee.png
g.to_mesh('Fake_lake_clip15.mesh')   # save to a new file

See the MIKE IO Mesh Example notebook for more Mesh operations (including shapely operations).