Skip to content

Commit 07c7486

Browse files
authored
Merge pull request #98 from sintefmath/dev
Coarsening features, lower default report level
2 parents 3708d35 + d54892b commit 07c7486

File tree

12 files changed

+421
-101
lines changed

12 files changed

+421
-101
lines changed

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "Jutul"
22
uuid = "2b460a1a-8a2b-45b2-b125-b5c536396eb9"
33
authors = ["Olav Møyner <[email protected]>"]
4-
version = "0.2.39"
4+
version = "0.2.40"
55

66
[deps]
77
AlgebraicMultigrid = "2169fc97-5a83-5252-b627-83903c6c433c"
@@ -69,7 +69,7 @@ ExprTools = "0.1.8"
6969
ForwardDiff = "0.10.30"
7070
GraphMakie = "0.5.3"
7171
Graphs = "1.8.0"
72-
HYPRE = "1.6.0"
72+
HYPRE = "1.6.0, 1.7"
7373
ILUZero = "0.2.0"
7474
JLD2 = "0.5"
7575
KaHyPar = "0.3.0"

ext/JutulMakieExt/interactive_3d.jl

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,7 @@ function plot_interactive_impl(grid, states;
133133

134134
for s in states
135135
di = s[k]
136-
mv = min(minimum(x -> isnan(x) ? Inf : x, di), mv)
137-
Mv = max(maximum(x -> isnan(x) ? -Inf : x, di), Mv)
136+
mv, Mv = my_minmax(di, mv, Mv)
138137
end
139138
if mv == Mv
140139
Mv = 1.01*mv + 1e-12
@@ -151,8 +150,7 @@ function plot_interactive_impl(grid, states;
151150
Mv = -Inf
152151
for s in states
153152
di = view(s[k], row, :)
154-
mv = min(minimum(x -> isnan(x) ? Inf : x, di), mv)
155-
Mv = max(maximum(x -> isnan(x) ? -Inf : x, di), Mv)
153+
mv, Mv = my_minmax(di, mv, Mv)
156154
end
157155
row_limits["$k"][row] = (mv, Mv)
158156
end
@@ -551,13 +549,17 @@ function plot_interactive_impl(grid, states;
551549

552550
# Actual plotting call
553551
if plot_type == :mesh
554-
tri = primitives.triangulation
555-
scat = Makie.mesh!(ax, pts, tri; color = ys,
556-
colorrange = lims,
557-
backlight = 1,
558-
colormap = cmap,
559-
transparency = transparency,
560-
kwarg...)
552+
# TODO: Not sure if this speeds things up
553+
tri_c = Makie.to_triangles(primitives.triangulation)
554+
pts_c = Makie.to_vertices(pts)
555+
scat = Makie.mesh!(ax, pts_c, tri_c;
556+
color = ys,
557+
colorrange = lims,
558+
backlight = 1,
559+
colormap = cmap,
560+
transparency = transparency,
561+
kwarg...
562+
)
561563
if !isnothing(edge_color)
562564
eplt = Jutul.plot_mesh_edges!(ax, grid; color = edge_color, edge_arg...)
563565
connect!(eplt.visible, edge_toggle.active)
@@ -912,3 +914,14 @@ function commercial_colormap()
912914
end
913915
return cmap
914916
end
917+
918+
function my_minmax(di, mv, Mv)
919+
for v in di
920+
if !isfinite(v)
921+
continue
922+
end
923+
mv = min(mv, v)
924+
Mv = max(Mv, v)
925+
end
926+
return (mv, Mv)
927+
end

src/Jutul.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@ module Jutul
9595
include("meshes/meshes.jl")
9696
include("discretization/discretization.jl")
9797

98+
# Coarsening utilities
99+
include("coarsening.jl")
100+
98101
# Extensions' interfaces
99102
include("ext/extensions.jl")
100103

src/coarsening.jl

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
abstract type AbstractCoarseningFunction end
2+
3+
struct CoarsenByVolumeAverage <: AbstractCoarseningFunction end
4+
5+
function inner_apply_coarsening_function(finevals, fine_indices, op::CoarsenByVolumeAverage, coarse, fine, row, name, entity)
6+
subvols = fine[:volumes][fine_indices]
7+
return sum(finevals.*subvols)/sum(subvols)
8+
end
9+
10+
struct CoarsenByHarmonicAverage <: AbstractCoarseningFunction end
11+
12+
function inner_apply_coarsening_function(finevals, fine_indices, op::CoarsenByHarmonicAverage, coarse, fine, row, name, entity)
13+
invvals = 1.0./finevals
14+
return length(invvals)/sum(invvals)
15+
end
16+
17+
struct CoarsenByArithemticAverage <: AbstractCoarseningFunction end
18+
19+
function inner_apply_coarsening_function(finevals, fine_indices, op::CoarsenByArithemticAverage, coarse, fine, row, name, entity)
20+
return sum(finevals)/length(finevals)
21+
end
22+
23+
struct CoarsenByFirstValue <: AbstractCoarseningFunction end
24+
25+
function inner_apply_coarsening_function(finevals, fine_indices, op::CoarsenByFirstValue, coarse, fine, row, name, entity)
26+
return finevals[1]
27+
end
28+
29+
function apply_coarsening_function!(coarsevals, finevals, op, coarse::DataDomain, fine::DataDomain, name, entity::Union{Cells, Faces})
30+
CG = physical_representation(coarse)
31+
function block_indices(CG, block, ::Cells)
32+
return findall(isequal(block), CG.partition)
33+
end
34+
function block_indices(CG, block, ::Faces)
35+
return CG.coarse_faces_to_fine[block]
36+
end
37+
function block_indices(CG, block, ::BoundaryFaces)
38+
return CG.coarse_boundary_to_fine[block]
39+
end
40+
ncoarse = count_entities(coarse, entity)
41+
if finevals isa AbstractVector
42+
for block in 1:ncoarse
43+
ix = block_indices(CG, block, entity)
44+
coarsevals[block] = inner_apply_coarsening_function(view(finevals, ix), ix, op, coarse, fine, 1, name, entity)
45+
end
46+
else
47+
for block in 1:ncoarse
48+
ix = block_indices(CG, block, entity)
49+
for j in axes(coarsevals, 1)
50+
coarsevals[j, block] = inner_apply_coarsening_function(view(finevals, j, ix), ix, op, coarse, fine, j, name, entity)
51+
end
52+
end
53+
end
54+
return coarsevals
55+
end
56+
57+
function coarsen_data_domain(D::DataDomain, partition;
58+
functions = Dict(),
59+
default = CoarsenByArithemticAverage(),
60+
default_other = CoarsenByFirstValue(),
61+
kwarg...
62+
)
63+
for (k, v) in pairs(kwarg)
64+
functions[k] = v
65+
end
66+
g = physical_representation(D)
67+
cg = CoarseMesh(g, partition)
68+
cD = DataDomain(cg)
69+
for name in keys(D)
70+
if !haskey(cD, name)
71+
val = D[name]
72+
e = Jutul.associated_entity(D, name)
73+
Te = eltype(val)
74+
if !(e in (Cells(), Faces(), BoundaryFaces(), nothing))
75+
# Other entities are not supported yet.
76+
continue
77+
end
78+
if isnothing(e)
79+
# No idea about coarse dims, just copy
80+
coarseval = deepcopy(val)
81+
elseif val isa AbstractVecOrMat
82+
ne = count_entities(cg, e)
83+
if val isa AbstractVector
84+
coarseval = zeros(Te, ne)
85+
else
86+
coarseval = zeros(Te, size(val, 1), ne)
87+
end
88+
if eltype(Te)<:AbstractFloat
89+
f = get(functions, name, default)
90+
else
91+
f = get(functions, name, default_other)
92+
end
93+
coarseval = apply_coarsening_function!(coarseval, val, f, cD, D, name, e)
94+
else
95+
# Don't know what's going on
96+
coarseval = deepcopy(val)
97+
end
98+
cD[name, e] = coarseval
99+
end
100+
end
101+
return cD
102+
end

src/dd/submodels.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ Get subvariable of Jutul variable
148148
"""
149149
function subvariable(var, map)
150150
if hasproperty(var, :regions)
151-
@warn "Default subvariable called for $(typeof(var)) that contains regions property. Potential missing interface specialization."
151+
@warn "Default subvariable called for $(typeof(var)) that contains regions property. Potential missing interface specialization. Map was: $(typeof(map))"
152152
end
153153
return var
154154
end

src/meshes/cart.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ struct CartesianMesh{D, Δ, O, T} <: FiniteVolumeMesh
7070
end
7171
Base.show(io::IO, g::CartesianMesh) = print(io, "CartesianMesh ($(dim(g))D) with $(join(grid_dims_ijk(g), "x"))=$(number_of_cells(g)) cells")
7272

73+
function CartesianMesh(v::Int, d = 1.0; kwarg...)
74+
if d isa Float64
75+
d = (d, )
76+
end
77+
return CartesianMesh((v, ), d; kwarg...)
78+
end
79+
7380
dim(t::CartesianMesh) = length(t.dims)
7481
number_of_cells(t::CartesianMesh) = prod(t.dims)
7582
function number_of_faces(t::CartesianMesh)

src/meshes/coarse.jl

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ function dim(G::CoarseMesh)
9999
return dim(G.parent)
100100
end
101101

102+
function mesh_z_is_depth(G::CoarseMesh)
103+
return mesh_z_is_depth(G.parent)
104+
end
105+
102106
function number_of_cells(G::CoarseMesh)
103107
return length(G.partition_lookup)
104108
end
@@ -188,6 +192,49 @@ function triangulate_mesh(m::CoarseMesh; kwarg...)
188192
Cells = (cell_data) -> cell_data[cell_ix],
189193
Faces = (face_data) -> face_data[face_index],
190194
indices = (Cells = cell_ix, Faces = face_index)
191-
)
195+
)
192196
return (points = points, triangulation = triangulation, mapper = mapper)
193197
end
198+
199+
function plot_primitives(mesh::CoarseMesh, plot_type; kwarg...)
200+
if plot_type == :mesh
201+
out = triangulate_mesh(mesh; kwarg...)
202+
else
203+
out = nothing
204+
end
205+
return out
206+
end
207+
208+
function cell_dims(cg::CoarseMesh, pos)
209+
# TODO: This function is inefficient
210+
index = cell_index(cg, pos)
211+
g = cg.parent
212+
T = eltype(g.node_points)
213+
minv = Inf .+ zero(T)
214+
maxv = -Inf .+ zero(T)
215+
216+
for (face, lr) in enumerate(cg.face_neighbors)
217+
l, r = lr
218+
if l == index || r == index
219+
pt, = compute_centroid_and_measure(cg, Faces(), face)
220+
if any(isnan.(pt))
221+
continue
222+
end
223+
minv = min.(pt, minv)
224+
maxv = max.(pt, maxv)
225+
end
226+
end
227+
for (bface, bcell) in enumerate(cg.boundary_cells)
228+
if bcell == index
229+
pt, = compute_centroid_and_measure(cg, BoundaryFaces(), bface)
230+
if any(isnan.(pt))
231+
continue
232+
end
233+
minv = min.(pt, minv)
234+
maxv = max.(pt, maxv)
235+
end
236+
end
237+
Δ = maxv - minv
238+
@assert all(x -> x > 0, Δ) "Cell dimensions were zero? Computed = $maxv - $minv for cell $index."
239+
return Tuple(Δ)
240+
end

0 commit comments

Comments
 (0)