|
| 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 |
0 commit comments