|
| 1 | +import .DataLayouts as DL |
| 2 | + |
| 3 | +""" |
| 4 | + VerticalMassBorrowingLimiter(f::Fields.Field, q_min) |
| 5 | +
|
| 6 | +A vertical-only mass borrowing limiter. |
| 7 | +
|
| 8 | +The mass borrower borrows tracer mass from an adjacent, lower layer. |
| 9 | +It conserves the total tracer mass and can avoid negative tracers. |
| 10 | +
|
| 11 | +At level k, it will first borrow the mass from the layer k+1 (lower level). |
| 12 | +If the mass is not sufficient in layer k+1, it will borrow mass from |
| 13 | +layer k+2. The borrower will proceed this process until the bottom layer. |
| 14 | +If the tracer mass in the bottom layer goes negative, it will repeat the |
| 15 | +process from the bottom to the top. In this way, the borrower works for |
| 16 | +any shape of mass profiles. |
| 17 | +
|
| 18 | +This code was adapted from [E3SM](https://github.com/E3SM-Project/E3SM/blob/2c377c5ec9a5585170524b366ad85074ab1b1a5c/components/eam/src/physics/cam/massborrow.F90) |
| 19 | +
|
| 20 | +References: |
| 21 | + - [zhang2018impact](@cite) |
| 22 | +""" |
| 23 | +struct VerticalMassBorrowingLimiter{F, T} |
| 24 | + bmass::F |
| 25 | + ic::F |
| 26 | + q_min::T |
| 27 | +end |
| 28 | +function VerticalMassBorrowingLimiter(f::Fields.Field, q_min) |
| 29 | + bmass = similar(Spaces.level(f, 1)) |
| 30 | + ic = similar(Spaces.level(f, 1)) |
| 31 | + return VerticalMassBorrowingLimiter(bmass, ic, q_min) |
| 32 | +end |
| 33 | + |
| 34 | + |
| 35 | +""" |
| 36 | + apply_limiter!(q::Fields.Field, ρ::Fields.Field, lim::VerticalMassBorrowingLimiter) |
| 37 | +
|
| 38 | +Apply the vertical mass borrowing |
| 39 | +limiter `lim` to field `q`, given |
| 40 | +density `ρ`. |
| 41 | +""" |
| 42 | +apply_limiter!( |
| 43 | + q::Fields.Field, |
| 44 | + ρ::Fields.Field, |
| 45 | + lim::VerticalMassBorrowingLimiter, |
| 46 | +) = apply_limiter!(q, ρ, axes(q), lim, ClimaComms.device(axes(q))) |
| 47 | + |
| 48 | +function apply_limiter!( |
| 49 | + q::Fields.Field, |
| 50 | + ρ::Fields.Field, |
| 51 | + space::Spaces.FiniteDifferenceSpace, |
| 52 | + lim::VerticalMassBorrowingLimiter, |
| 53 | + device::ClimaComms.AbstractCPUDevice, |
| 54 | +) |
| 55 | + cache = (; bmass = lim.bmass, ic = lim.ic, q_min = lim.q_min) |
| 56 | + columnwise_massborrow_cpu(q, ρ, cache) |
| 57 | +end |
| 58 | + |
| 59 | +function apply_limiter!( |
| 60 | + q::Fields.Field, |
| 61 | + ρ::Fields.Field, |
| 62 | + space::Spaces.ExtrudedFiniteDifferenceSpace, |
| 63 | + lim::VerticalMassBorrowingLimiter, |
| 64 | + device::ClimaComms.AbstractCPUDevice, |
| 65 | +) |
| 66 | + Fields.bycolumn(axes(q)) do colidx |
| 67 | + cache = (; |
| 68 | + bmass = lim.bmass[colidx], |
| 69 | + ic = lim.ic[colidx], |
| 70 | + q_min = lim.q_min, |
| 71 | + ) |
| 72 | + columnwise_massborrow_cpu(q[colidx], ρ[colidx], cache) |
| 73 | + end |
| 74 | +end |
| 75 | + |
| 76 | +# TODO: can we improve the performance? |
| 77 | +# `bycolumn` on the CPU may be better here since we could multithread it. |
| 78 | +function columnwise_massborrow_cpu(q::Fields.Field, ρ::Fields.Field, cache) # column fields |
| 79 | + (; bmass, ic, q_min) = cache |
| 80 | + |
| 81 | + Δz = Fields.Δz_field(q) |
| 82 | + Δz_vals = Fields.field_values(Δz) |
| 83 | + ρ_vals = Fields.field_values(ρ) |
| 84 | + # loop over tracers |
| 85 | + nlevels = Spaces.nlevels(axes(q)) |
| 86 | + @. ic = 0 |
| 87 | + @. bmass = 0 |
| 88 | + q_vals = Fields.field_values(q) |
| 89 | + # top to bottom |
| 90 | + for f in 1:DataLayouts.ncomponents(q_vals) |
| 91 | + for v in 1:nlevels |
| 92 | + CI = CartesianIndex(1, 1, f, v, 1) |
| 93 | + # new mass in the current layer |
| 94 | + ρΔz_v = |
| 95 | + DL.getindex_field(Δz_vals, CI) * DL.getindex_field(ρ_vals, CI) |
| 96 | + nmass = DL.getindex_field(q_vals, CI) + bmass[] / ρΔz_v |
| 97 | + if nmass > q_min[f] |
| 98 | + # if new mass in the current layer is positive, don't borrow mass any more |
| 99 | + DL.setindex_field!(q_vals, nmass, CI) |
| 100 | + bmass[] = 0 |
| 101 | + else |
| 102 | + # set mass to q_min in the current layer, and save bmass |
| 103 | + bmass[] = (nmass - q_min[f]) * ρΔz_v |
| 104 | + DL.setindex_field!(q_vals, q_min[f], CI) |
| 105 | + ic[] = ic[] + 1 |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + # bottom to top |
| 110 | + for v in nlevels:-1:1 |
| 111 | + CI = CartesianIndex(1, 1, f, v, 1) |
| 112 | + # if the surface layer still needs to borrow mass |
| 113 | + if bmass[] < 0 |
| 114 | + ρΔz_v = |
| 115 | + DL.getindex_field(Δz_vals, CI) * |
| 116 | + DL.getindex_field(ρ_vals, CI) |
| 117 | + # new mass in the current layer |
| 118 | + nmass = DL.getindex_field(q_vals, CI) + bmass[] / ρΔz_v |
| 119 | + if nmass > q_min[f] |
| 120 | + # if new mass in the current layer is positive, don't borrow mass any more |
| 121 | + DL.setindex_field!(q_vals, nmass, CI) |
| 122 | + bmass[] = 0 |
| 123 | + else |
| 124 | + # if new mass in the current layer is negative, continue to borrow mass |
| 125 | + bmass[] = (nmass - q_min[f]) * ρΔz_v |
| 126 | + DL.setindex_field!(q_vals, q_min[f], CI) |
| 127 | + end |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + return nothing |
| 133 | +end |
0 commit comments