from shapely.geometry import Polygon
import holoviews as hv
import pandas as pd
import numpy as np
import geoviews as gv
import xarray as xr
import cartopy.crs as ccrs
import hvplot.pandas
import hvplot.xarray
import math
import xmsgridtrace
import xmsgrid
import gridtrace_tools
hv.extension("bokeh")
First, we will create a simple unstructured grid.
points = [
(0, 0, 0), (1, 0, 0), (2, 0, 0),
(0, 1, 0), (1, 1, 0), (2, 1, 0),
(0, 2, 0), (1, 2, 0), (2, 2, 0)
]
cells = [
xmsgrid.ugrid.UGrid.QUAD, 4, 0, 1, 4, 3,
xmsgrid.ugrid.UGrid.QUAD, 4, 1, 2, 5, 4,
xmsgrid.ugrid.UGrid.QUAD, 4, 3, 4, 7, 6,
xmsgrid.ugrid.UGrid.QUAD, 4, 4, 5, 8, 7,
]
ug = xmsgrid.ugrid.UGrid(points, cells)
%opts Polygons [width=700 height=700]
ug_polygons, ug_lines, ug_points = gridtrace_tools.create_hv_objects_from_ugrid(ug)
ug_polygons = ug_polygons.redim.range(x=(-0.5, 2.5), y=(-0.5, 2.5))
ug_polygons * ug_lines * ug_points
tracer = xmsgridtrace.gridtrace.GridTrace(
ugrid=ug,
vector_multiplier=1,
max_tracing_time=-1,
max_tracing_distance=-1,
min_delta_time=0.01,
max_change_distance=-1,
max_change_velocity=-1,
max_change_direction_in_radians=(math.pi/4)
)
This next step will define the vectors on the ugrid. Notice that a "time" is associated with each set of vectors.
scalars = [
[
(0, 2, 0), (-0.2, 0, 0), (-2, 0, 0),
(0, 0.2, 0), (0, 0, 0), (0, -0.2, 0),
(2, 0, 0), (0.2, 0, 0), (0, -2, 0),
],
[
(0, 4, 0), (-0.4, 0, 0), (-4, 0, 0),
(0, 0.4, 0), (0, 0, 0), (0, -0.4, 0),
(4, 0, 0), (0.4, 0, 0), (0, -4, 0),
],
]
activity = [True for _ in range(0, len(scalars[0]))]
# Time 0.0
tracer.add_grid_scalars_at_time(
scalars=scalars[0],
scalar_loc="points",
cell_activity=activity,
activity_loc="points",
time=0.0
)
# Time 20.0
tracer.add_grid_scalars_at_time(
scalars=scalars[1],
scalar_loc="points",
cell_activity=activity,
activity_loc="points",
time=20.0
)
# draw the vectors
%opts Polygons [width=700 height=700]
vf_scalars = gridtrace_tools.hv_vector_field_from_2_timesteps(
scalars[0], points
)
ug_polygons * ug_points * ug_lines * vf_scalars
traces, times = tracer.trace_point((0.5, 0.5, 0), 0.0)
print(traces)
print(times)
hv_traces = hv.Points(traces).options(color='red', size=4)
ug_polygons * ug_points * ug_lines * vf_scalars * hv_traces