|
| 1 | +import .DataLayouts as DL |
| 2 | + |
| 3 | +""" |
| 4 | + VerticalMassBorrowingLimiter(f::Fields.Field, q_min) |
| 5 | +
|
| 6 | +A vertical-only mass borrowing limier. |
| 7 | +
|
| 8 | +The mass borrower borrows tracer mass from an adjacent layer. |
| 9 | +It conserves the 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, lim::VerticalMassBorrowingLimiter) |
| 37 | +
|
| 38 | +Apply the vertical mass borrowing |
| 39 | +limiter `lim` to field `q`. |
| 40 | +""" |
| 41 | +apply_limiter!(q::Fields.Field, lim::VerticalMassBorrowingLimiter) = |
| 42 | + apply_limiter!(q, axes(q), lim, ClimaComms.device(axes(q))) |
| 43 | + |
| 44 | +function apply_limiter!( |
| 45 | + q::Fields.Field, |
| 46 | + space::Spaces.FiniteDifferenceSpace, |
| 47 | + lim::VerticalMassBorrowingLimiter, |
| 48 | + device::ClimaComms.AbstractCPUDevice, |
| 49 | +) |
| 50 | + cache = (; bmass = lim.bmass, ic = lim.ic, q_min = lim.q_min) |
| 51 | + columnwise_massborrow_cpu(q, cache) |
| 52 | +end |
| 53 | + |
| 54 | +function apply_limiter!( |
| 55 | + q::Fields.Field, |
| 56 | + space::Spaces.ExtrudedFiniteDifferenceSpace, |
| 57 | + lim::VerticalMassBorrowingLimiter, |
| 58 | + device::ClimaComms.AbstractCPUDevice, |
| 59 | +) |
| 60 | + Fields.bycolumn(axes(q)) do colidx |
| 61 | + cache = |
| 62 | + (; bmass = lim.bmass[colidx], ic = lim.ic[colidx], q_min = lim.q_min) |
| 63 | + columnwise_massborrow_cpu(q[colidx], cache) |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +function columnwise_massborrow_cpu(q::Fields.Field, cache) # column fields |
| 68 | + (; bmass, ic, q_min) = cache |
| 69 | + |
| 70 | + pdel = Fields.field_values(Fields.Δz_field(q)) |
| 71 | + # loop over tracers |
| 72 | + nlevels = Spaces.nlevels(axes(q)) |
| 73 | + @. ic = 0 |
| 74 | + @. bmass = 0 |
| 75 | + q_vals = Fields.field_values(q) |
| 76 | + # top to bottom |
| 77 | + for f in 1:DataLayouts.ncomponents(q_vals) |
| 78 | + for v in 1:nlevels |
| 79 | + CI = CartesianIndex(1, 1, f, v, 1) |
| 80 | + # new mass in the current layer |
| 81 | + pdel_v = DL.getindex_field(pdel, CI) |
| 82 | + nmass = DL.getindex_field(q_vals, CI) + bmass[] / pdel_v |
| 83 | + if nmass > q_min[f] |
| 84 | + # if new mass in the current layer is positive, don't borrow mass any more |
| 85 | + DL.setindex_field!(q_vals, nmass, CI) |
| 86 | + bmass[] = 0 |
| 87 | + else |
| 88 | + # set mass to q_min in the current layer, and save bmass |
| 89 | + bmass[] = (nmass - q_min[f]) * pdel_v |
| 90 | + DL.setindex_field!(q_vals, q_min[f], CI) |
| 91 | + ic[] = ic[] + 1 |
| 92 | + end |
| 93 | + end |
| 94 | + |
| 95 | + # bottom to top |
| 96 | + for v in nlevels:-1:1 |
| 97 | + CI = CartesianIndex(1, 1, f, v, 1) |
| 98 | + # if the surface layer still needs to borrow mass |
| 99 | + if bmass[] < 0 |
| 100 | + pdel_v = DL.getindex_field(pdel, CI) |
| 101 | + # new mass in the current layer |
| 102 | + nmass = DL.getindex_field(q_vals, CI) + bmass[] / pdel_v |
| 103 | + if nmass > q_min[f] |
| 104 | + # if new mass in the current layer is positive, don't borrow mass any more |
| 105 | + DL.setindex_field!(q_vals, nmass, CI) |
| 106 | + bmass[] = 0 |
| 107 | + else |
| 108 | + # if new mass in the current layer is negative, continue to borrow mass |
| 109 | + bmass[] = (nmass - q_min[f]) * pdel_v |
| 110 | + DL.setindex_field!(q_vals, q_min[f], CI) |
| 111 | + end |
| 112 | + end |
| 113 | + end |
| 114 | + end |
| 115 | + |
| 116 | + return nothing |
| 117 | +end |
0 commit comments