@@ -3,148 +3,151 @@ module ExtendableFEMBaseUnicodePlotsExt
3
3
using ExtendableGrids: ExtendableGrid, simplexgrid, ON_CELLS, ON_FACES, ON_EDGES, CellNodes, CellGeometries, BFaceNodes, BFaceRegions, Coordinates
4
4
using ExtendableFEMBase: FEVector, FEVectorBlock, FESpace, lazy_interpolate!, num_cells, local_celledgenodes, get_ncomponents, H1P1, IdentityComponent, standard_kernel
5
5
using UnicodePlots: UnicodePlots, BrailleCanvas, Plot, heatmap, lineplot,
6
- lineplot!, lines!
6
+ lineplot!, lines!
7
7
8
8
import ExtendableFEMBase: unicode_gridplot, unicode_scalarplot
9
9
10
10
function unicode_gridplot (
11
- xgrid:: ExtendableGrid ;
12
- title = " gridplot" ,
13
- resolution = (40 , 20 ),
14
- autoscale = true ,
15
- color = (200 , 200 , 200 ),
16
- bface_color = (255 , 0 , 0 ),
17
- CanvasType = BrailleCanvas,
18
- plot_based = ON_CELLS, # or ON_FACES/ON_EDGES
19
- kwargs... ,
20
- )
21
- coords = xgrid[Coordinates]
22
- ex = extrema (view (coords, 1 , :))
23
- ey = extrema (view (coords, 2 , :))
11
+ xgrid:: ExtendableGrid ;
12
+ title = " gridplot" ,
13
+ resolution = (40 , 20 ),
14
+ autoscale = true ,
15
+ color = (200 , 200 , 200 ),
16
+ bface_color = (255 , 0 , 0 ),
17
+ CanvasType = BrailleCanvas,
18
+ plot_based = ON_CELLS, # or ON_FACES/ON_EDGES
19
+ kwargs... ,
20
+ )
21
+ coords = xgrid[Coordinates]
22
+ ex = extrema (view (coords, 1 , :))
23
+ ey = extrema (view (coords, 2 , :))
24
24
25
- if autoscale
26
- wx = ex[2 ] - ex[1 ]
27
- wy = ey[2 ] - ey[1 ]
28
- rescale = wx / wy * (resolution[1 ] / (2 * resolution[2 ]))
29
- if rescale > 1
30
- resolution = (resolution[1 ], Int (ceil (resolution[2 ] / rescale)))
31
- else
32
- resolution = (Int (ceil (resolution[1 ] / rescale)), resolution[2 ])
33
- end
34
- end
25
+ if autoscale
26
+ wx = ex[2 ] - ex[1 ]
27
+ wy = ey[2 ] - ey[1 ]
28
+ rescale = wx / wy * (resolution[1 ] / (2 * resolution[2 ]))
29
+ if rescale > 1
30
+ resolution = (resolution[1 ], Int (ceil (resolution[2 ] / rescale)))
31
+ else
32
+ resolution = (Int (ceil (resolution[1 ] / rescale)), resolution[2 ])
33
+ end
34
+ end
35
35
36
- canvas = CanvasType (resolution[2 ], resolution[1 ], # number of rows and columns (characters)
37
- origin_y = ey[1 ], origin_x = ex[1 ], # position in virtual space
38
- height = ey[2 ] - ey[1 ], width = ex[2 ] - ex[1 ]; blend = false , kwargs... ) # size of the virtual space
36
+ canvas = CanvasType (
37
+ resolution[2 ], resolution[1 ], # number of rows and columns (characters)
38
+ origin_y = ey[1 ], origin_x = ex[1 ], # position in virtual space
39
+ height = ey[2 ] - ey[1 ], width = ex[2 ] - ex[1 ]; blend = false , kwargs...
40
+ ) # size of the virtual space
39
41
40
- # # plot all edges
41
- if plot_based in [ON_FACES, ON_EDGES]
42
- # # plot all edges via FaceNodes
43
- facenodes = xgrid[FaceNodes]
44
- nfaces = size (facenodes, 2 )
45
- for j in 1 : nfaces
46
- lines! (canvas, coords[1 , facenodes[1 , j]], coords[2 , facenodes[1 , j]], coords[1 , facenodes[2 , j]], coords[2 , facenodes[2 , j]]; color = color)
47
- end
48
- elseif plot_based == ON_CELLS
49
- # # plot all edges via CellNodes and local_celledgenodes
50
- cellnodes = xgrid[CellNodes]
51
- cellgeoms = xgrid[CellGeometries]
52
- ncells = num_cells (xgrid)
53
- for j in 1 : ncells
54
- cen = local_celledgenodes (cellgeoms[j])
55
- for k ∈ 1 : size (cen, 2 )
56
- lines! (canvas, coords[1 , cellnodes[cen[1 , k], j]], coords[2 , cellnodes[cen[1 , k], j]], coords[1 , cellnodes[cen[2 , k], j]], coords[2 , cellnodes[cen[2 , k], j]]; color = color)
57
- end
58
- end
59
- end
42
+ # # plot all edges
43
+ if plot_based in [ON_FACES, ON_EDGES]
44
+ # # plot all edges via FaceNodes
45
+ facenodes = xgrid[FaceNodes]
46
+ nfaces = size (facenodes, 2 )
47
+ for j in 1 : nfaces
48
+ lines! (canvas, coords[1 , facenodes[1 , j]], coords[2 , facenodes[1 , j]], coords[1 , facenodes[2 , j]], coords[2 , facenodes[2 , j]]; color = color)
49
+ end
50
+ elseif plot_based == ON_CELLS
51
+ # # plot all edges via CellNodes and local_celledgenodes
52
+ cellnodes = xgrid[CellNodes]
53
+ cellgeoms = xgrid[CellGeometries]
54
+ ncells = num_cells (xgrid)
55
+ for j in 1 : ncells
56
+ cen = local_celledgenodes (cellgeoms[j])
57
+ for k in 1 : size (cen, 2 )
58
+ lines! (canvas, coords[1 , cellnodes[cen[1 , k], j]], coords[2 , cellnodes[cen[1 , k], j]], coords[1 , cellnodes[cen[2 , k], j]], coords[2 , cellnodes[cen[2 , k], j]]; color = color)
59
+ end
60
+ end
61
+ end
60
62
61
- # # plot BFaces again and color BFaceRegions
62
- bfacenodes = xgrid[BFaceNodes]
63
- bfaceregions = xgrid[BFaceRegions]
64
- ebfr = extrema (bfaceregions)
65
- nbfaces = size (bfacenodes, 2 )
66
- for j in 1 : nbfaces
67
- cscale = (bfaceregions[j] - ebfr[1 ]) / (ebfr[2 ] - ebfr[1 ] + 1 )
68
- c = Int .(round .(bface_color .* cscale))
69
- lines! (canvas, coords[1 , bfacenodes[1 , j]], coords[2 , bfacenodes[1 , j]], coords[1 , bfacenodes[2 , j]], coords[2 , bfacenodes[2 , j]]; color = c) # pixel space
70
- end
71
- plot = Plot (canvas; title = title, kwargs... )
72
- return plot
63
+ # # plot BFaces again and color BFaceRegions
64
+ bfacenodes = xgrid[BFaceNodes]
65
+ bfaceregions = xgrid[BFaceRegions]
66
+ ebfr = extrema (bfaceregions)
67
+ nbfaces = size (bfacenodes, 2 )
68
+ for j in 1 : nbfaces
69
+ cscale = (bfaceregions[j] - ebfr[1 ]) / (ebfr[2 ] - ebfr[1 ] + 1 )
70
+ c = Int .(round .(bface_color .* cscale))
71
+ lines! (canvas, coords[1 , bfacenodes[1 , j]], coords[2 , bfacenodes[1 , j]], coords[1 , bfacenodes[2 , j]], coords[2 , bfacenodes[2 , j]]; color = c) # pixel space
72
+ end
73
+ plot = Plot (canvas; title = title, kwargs... )
74
+ return plot
73
75
end
74
76
75
77
76
78
function unicode_scalarplot (
77
- u:: FEVectorBlock ;
78
- components = 1 : get_ncomponents (u),
79
- abs = false ,
80
- nrows = 1 , # only used for 2D plots
81
- resolution = (40 , 40 ),
82
- colormap = :viridis , # only used for 2D plots
83
- title = u. name,
84
- kwargs... )
79
+ u:: FEVectorBlock ;
80
+ components = 1 : get_ncomponents (u),
81
+ abs = false ,
82
+ nrows = 1 , # only used for 2D plots
83
+ resolution = (40 , 40 ),
84
+ colormap = :viridis , # only used for 2D plots
85
+ title = u. name,
86
+ kwargs...
87
+ )
85
88
86
- xgrid = u. FES. xgrid
87
- coords = xgrid[Coordinates]
88
- dim = size (coords, 1 )
89
+ xgrid = u. FES. xgrid
90
+ coords = xgrid[Coordinates]
91
+ dim = size (coords, 1 )
89
92
90
- if dim == 1
91
- ex = extrema (view (coords, 1 , :))
92
- X = LinRange (ex[1 ], ex[2 ], resolution[1 ])
93
- xgrid_plot = simplexgrid (X)
94
- elseif dim == 2
95
- ex = extrema (view (coords, 1 , :))
96
- ey = extrema (view (coords, 2 , :))
93
+ if dim == 1
94
+ ex = extrema (view (coords, 1 , :))
95
+ X = LinRange (ex[1 ], ex[2 ], resolution[1 ])
96
+ xgrid_plot = simplexgrid (X)
97
+ elseif dim == 2
98
+ ex = extrema (view (coords, 1 , :))
99
+ ey = extrema (view (coords, 2 , :))
97
100
98
- X = LinRange (ex[1 ], ex[2 ], resolution[1 ])
99
- Y = LinRange (ey[1 ], ey[2 ], resolution[2 ])
100
- xgrid_plot = simplexgrid (X, Y)
101
- else
102
- @warn " sorry, no unicode_scalarplat available for dimension $dim "
103
- return nothing
104
- end
101
+ X = LinRange (ex[1 ], ex[2 ], resolution[1 ])
102
+ Y = LinRange (ey[1 ], ey[2 ], resolution[2 ])
103
+ xgrid_plot = simplexgrid (X, Y)
104
+ else
105
+ @warn " sorry, no unicode_scalarplat available for dimension $dim "
106
+ return nothing
107
+ end
105
108
106
- I = [FEVector (FESpace {H1P1{1}} (xgrid_plot)) for c in components]
107
- if abs
108
- lazy_interpolate! (I[1 ][1 ], [u], [(1 , Identity)]; postprocess = (result, input, qpinfo) -> (result[1 ] = sqrt (sum (view (input, components) .^ 2 ))), not_in_domain_value = 0 )
109
- else
110
- for c ∈ 1 : length (components)
111
- lazy_interpolate! (I[c][1 ], [u], [(1 , IdentityComponent{components[c]})]; postprocess = standard_kernel, not_in_domain_value = 0 )
112
- end
113
- end
109
+ I = [FEVector (FESpace {H1P1{1}} (xgrid_plot)) for c in components]
110
+ if abs
111
+ lazy_interpolate! (I[1 ][1 ], [u], [(1 , Identity)]; postprocess = (result, input, qpinfo) -> (result[1 ] = sqrt (sum (view (input, components) .^ 2 ))), not_in_domain_value = 0 )
112
+ else
113
+ for c in 1 : length (components)
114
+ lazy_interpolate! (I[c][1 ], [u], [(1 , IdentityComponent{components[c]})]; postprocess = standard_kernel, not_in_domain_value = 0 )
115
+ end
116
+ end
114
117
115
- if dim == 1
116
- plt = nothing
117
- ylim = extrema (I[1 ]. entries)
118
- for c ∈ 2 : length (components)
119
- e = extrema (I[c]. entries)
120
- ylim = (min (ylim[1 ], e[1 ]), max (ylim[2 ], e[2 ]))
121
- end
122
- for c ∈ 1 : length (components)
123
- if c == 1
124
- plt = lineplot (X, view (I[c][1 ]), ylim = ylim, xlabel = " x" , name = title * (length (components) == 1 ? " " : " [$(components[c]) ]" ), height = resolution[2 ], width = resolution[1 ])
125
- else
126
- lineplot! (plt, X, view (I[c][1 ]), name = title * " [$(components[c]) ]" )
127
- end
128
- end
129
- return plt
130
- elseif dim == 2
131
- plts = [
132
- heatmap (
133
- reshape (view (I[c][1 ]), (resolution[1 ], resolution[2 ]))' ,
134
- xlabel = " x" ,
135
- ylabel = " y" ,
136
- xfact = (ex[2 ] - ex[1 ]) / (resolution[1 ] - 1 ),
137
- yfact = (ey[2 ] - ey[1 ]) / (resolution[2 ] - 1 ),
138
- xoffset = ex[1 ],
139
- yoffset = ey[1 ],
140
- title = title * (length (components) == 1 ? " " : " [$(components[c]) ]" ),
141
- colormap = colormap,
142
- ) for c ∈ 1 : length (components)
143
- ]
144
- return length (plts) == 1 ? plts[1 ] : plts
145
- # # the following command looks nicer, but requires the Term-based extension of UnicodePlots
146
- # # return UnicodePlots.gridplot(map(i -> plts[i], 1:length(components)); layout = (nrows, nothing))
147
- end
118
+ if dim == 1
119
+ plt = nothing
120
+ ylim = extrema (I[1 ]. entries)
121
+ for c in 2 : length (components)
122
+ e = extrema (I[c]. entries)
123
+ ylim = (min (ylim[1 ], e[1 ]), max (ylim[2 ], e[2 ]))
124
+ end
125
+ for c in 1 : length (components)
126
+ if c == 1
127
+ plt = lineplot (X, view (I[c][1 ]), ylim = ylim, xlabel = " x" , name = title * (length (components) == 1 ? " " : " [$(components[c]) ]" ), height = resolution[2 ], width = resolution[1 ])
128
+ else
129
+ lineplot! (plt, X, view (I[c][1 ]), name = title * " [$(components[c]) ]" )
130
+ end
131
+ end
132
+ return plt
133
+ elseif dim == 2
134
+ plts = [
135
+ heatmap (
136
+ reshape (view (I[c][1 ]), (resolution[1 ], resolution[2 ]))' ,
137
+ xlabel = " x" ,
138
+ ylabel = " y" ,
139
+ xfact = (ex[2 ] - ex[1 ]) / (resolution[1 ] - 1 ),
140
+ yfact = (ey[2 ] - ey[1 ]) / (resolution[2 ] - 1 ),
141
+ xoffset = ex[1 ],
142
+ yoffset = ey[1 ],
143
+ title = title * (length (components) == 1 ? " " : " [$(components[c]) ]" ),
144
+ colormap = colormap,
145
+ ) for c in 1 : length (components)
146
+ ]
147
+ return length (plts) == 1 ? plts[1 ] : plts
148
+ # # the following command looks nicer, but requires the Term-based extension of UnicodePlots
149
+ # # return UnicodePlots.gridplot(map(i -> plts[i], 1:length(components)); layout = (nrows, nothing))
150
+ end
148
151
end
149
152
150
153
end
0 commit comments