-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathjplot.jl
94 lines (84 loc) · 3.07 KB
/
jplot.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#this file contains function related to plotting the mesh objects.
#we will use matplotlib.pyplot to achieve this
#
using FEniCS
import PyPlot: triplot, plot_trisurf, tripcolor, tricontourf
@pyimport matplotlib.tri as tri
@pyimport fenics
#creates a Triangulation for the Mesh type
function mesh2triangle(object::Mesh)
xy = coordinates(object)
return tri.Triangulation(xy[:, 1], xy[:, 2], cells(object))
end
#creates a Triangulation for the feMesh type
function mesh2triangle(space::feMesh)
xy = space.nodes
#needed to convert the element numbering back to a zero based index
cells = space.elements .- 1
return tri.Triangulation(xy[:, 1], xy[:, 2], cells)
end
function plot(object::Mesh; kws...)
geom = geometry(object)
gdim = geom[:dim]()
topol = topology(object)
tdim = topol[:dim]()
#plot 2D shapes
if gdim == 2 && tdim == 2
xy = coordinates(object)
triangles = mesh2triangle(object)
triplot(triangles; kws...)
elseif gdim == 3 && tdim == 3
bmesh = BoundaryMesh(object, "exterior", false)
plot(bmesh; kws...)
elseif gdim == 3 && tdim == 2
xy = coordinates(object)
plot_trisurf(xy[:, 1], xy[:, 2], xy[:, 3], triangles = cells(object); kws...)
end
end
#Only very basic function plotting currently exists, need to dispatch based on kwargs for full plotting.
function plot(object::Union{Expression, FeFunction}; kws...)
f_space = object.pyobject[:function_space]()
mesh = f_space[:mesh]()
geometry = mesh[:geometry]()
topology = mesh[:topology]()
gdim = geometry[:dim]()
tdim = topology[:dim]()
fvec = vector(object)
if fvec[:size]() == mesh[:num_cells]()
C = fvec[:array]()
if gdim == 2 && tdim == 2
tripcolor(mesh2triangle(mesh), C; kws...)
end
elseif gdim == 3 && tdim == 2
xy = coordinates(object)
plot_trisurf(mesh2triangle(mesh), xy[:, 2], C; kws...)
elseif object.pyobject[:value_rank]() == 0
C = object.pyobject[:compute_vertex_values](mesh)
tricontourf(mesh2triangle(Mesh(mesh)), C, 40; kws...)
end
end
function surf_plot(object::Union{Expression, FeFunction}; kws...)
f_space = object.pyobject[:function_space]()
mesh = f_space[:mesh]()
geometry = mesh[:geometry]()
topology = mesh[:topology]()
gdim = geometry[:dim]()
tdim = topology[:dim]()
fvec = vector(object)
if object.pyobject[:value_rank]() == 0
C = object.pyobject[:compute_vertex_values](mesh)
xy = coordinates(Mesh(mesh))
PyPlot.surf(xy[:, 1], xy[:, 2], C)
else
error("surf_plot is not available for these dimensions")
end
end
#plotting for solution computed via jinterface.jl, directly with the Mesh
function plot(mesh::Mesh, solution::AbstractArray, levels::Int = 40; kws...)
tricontourf(mesh2triangle(mesh), solution, levels; kws...)
end
#plotting for solution computed via jinterface.jl
function plot(space::feMesh, solution::AbstractArray, levels::Int = 40; kws...)
tricontourf(mesh2triangle(space), solution, levels; kws...)
end
export plot, surf_plot