|
| 1 | +#= |
| 2 | +
|
| 3 | +From E3SM: |
| 4 | + https://github.com/E3SM-Project/E3SM/blob/2c377c5ec9a5585170524b366ad85074ab1b1a5c/components/eam/src/physics/cam/massborrow.F90 |
| 5 | +Described in Appendix B of |
| 6 | + https://gmd.copernicus.org/articles/11/1971/2018/gmd-11-1971-2018.pdf |
| 7 | +
|
| 8 | +=# |
| 9 | + |
| 10 | +import .DataLayouts as DL |
| 11 | + |
| 12 | +struct VerticalMassBorrowingLimiter{F, T} |
| 13 | + bmass::F |
| 14 | + ic::F |
| 15 | + qmin::T |
| 16 | +end |
| 17 | +function VerticalMassBorrowingLimiter(f::Fields.Field, qmin) |
| 18 | + bmass = similar(Spaces.level(f, 1)) |
| 19 | + ic = similar(Spaces.level(f, 1)) |
| 20 | + return VerticalMassBorrowingLimiter(bmass, ic, qmin) |
| 21 | +end |
| 22 | + |
| 23 | +apply_limiter!( |
| 24 | + ρq::Fields.Field, |
| 25 | + ρ::Fields.Field, |
| 26 | + lim::VerticalMassBorrowingLimiter, |
| 27 | +) = apply_limiter!(ρq, ρ, lim, ClimaComms.device(axes(ρ))) |
| 28 | + |
| 29 | +function apply_limiter!( |
| 30 | + ρq::Fields.Field, |
| 31 | + ρ::Fields.Field, |
| 32 | + lim::VerticalMassBorrowingLimiter, |
| 33 | + device::ClimaComms.AbstractCPUDevice, |
| 34 | +) |
| 35 | + Fields.bycolumn(axes(ρ)) do colidx |
| 36 | + cache = |
| 37 | + (; bmass = lim.bmass[colidx], ic = lim.ic[colidx], qmin = lim.qmin) |
| 38 | + columnwise_massborrow_cpu(ρq[colidx], ρ[colidx], cache) |
| 39 | + end |
| 40 | +end |
| 41 | + |
| 42 | +function columnwise_massborrow_cpu(ρq::Fields.Field, ρ::Fields.Field, cache) # column fields |
| 43 | + (; bmass, ic, qmin) = cache |
| 44 | + |
| 45 | + pdel = Fields.field_values(Fields.Δz_field(ρq)) |
| 46 | + # loop over tracers |
| 47 | + nlevels = Spaces.nlevels(axes(ρq)) |
| 48 | + @. ic = 0 |
| 49 | + @. bmass = 0 |
| 50 | + ρq_vals = Fields.field_values(ρq) |
| 51 | + # top to bottom |
| 52 | + for f in 1:DataLayouts.ncomponents(ρq_vals) |
| 53 | + for v in 1:nlevels |
| 54 | + CI = CartesianIndex(1, 1, f, v, 1) |
| 55 | + # new mass in the current layer |
| 56 | + pdel_v = DL.getindex_field(pdel, CI) |
| 57 | + nmass = DL.getindex_field(ρq_vals, CI) + bmass[] / pdel_v |
| 58 | + if nmass > qmin[f] |
| 59 | + # if new mass in the current layer is positive, don't borrow mass any more |
| 60 | + DL.setindex_field!(ρq_vals, nmass, CI) |
| 61 | + bmass[] = 0 |
| 62 | + else |
| 63 | + # set mass to qmin in the current layer, and save bmass |
| 64 | + bmass[] = (nmass - qmin[f]) * pdel_v |
| 65 | + DL.setindex_field!(ρq_vals, qmin[f], CI) |
| 66 | + ic[] = ic[] + 1 |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + # bottom to top |
| 71 | + for v in nlevels:-1:1 |
| 72 | + CI = CartesianIndex(1, 1, f, v, 1) |
| 73 | + # if the surface layer still needs to borrow mass |
| 74 | + if bmass[] < 0 |
| 75 | + pdel_v = DL.getindex_field(pdel, CI) |
| 76 | + # new mass in the current layer |
| 77 | + nmass = DL.getindex_field(ρq_vals, CI) + bmass[] / pdel_v |
| 78 | + if nmass > qmin[f] |
| 79 | + # if new mass in the current layer is positive, don't borrow mass any more |
| 80 | + DL.setindex_field!(ρq_vals, nmass, CI) |
| 81 | + bmass[] = 0 |
| 82 | + else |
| 83 | + # if new mass in the current layer is negative, continue to borrow mass |
| 84 | + bmass[] = (nmass - qmin[f]) * pdel_v |
| 85 | + DL.setindex_field!(ρq_vals, qmin[f], CI) |
| 86 | + end |
| 87 | + end |
| 88 | + end |
| 89 | + end |
| 90 | + |
| 91 | + return nothing |
| 92 | +end |
0 commit comments