CAM's shallow-convection calculation, compute_uwshcu_inv, was replaced by a new model and run for a full month, then compared with the original Fortran and with the MLP model now in use, for accuracy and speed. The new model needs a layer of limits on its outputs to get through the month; that layer is shown below as well.
Median deviation of all output fields from the original Fortran over the month. Variable by variable, it is closer than the MLP on 18–19 of the 19 main variables.
The new model's run time for the month as a multiple of the original Fortran's (GPU / CPU). The MLP model: 1.10× / 0.98×.
Without limits, the run stops here on a NaN. With the output limits, both runs complete the month.
With the model plugged in directly, the run stops at about step 100. Traced back, it starts from a negative number that is almost zero:
cufrc is about −5×10⁻¹⁰, just below 0. The original Fortran never gives a negative cloud fraction.cloud_fraction.F90 uses it directly, and the total cloud fraction CLD turns negative.mo_photo.F90 computes clouds1.5; a negative number raised to the power 1.5 gives NaN.The first attempt replaced every NaN with 0. The run no longer failed, but from step 3 on, shallow convection was in effect switched off: the results looked normal but were wrong. So the limit layer now sets only the padding rows, which carry no data, to 0; a NaN in a real column is left as it is, so that the model stops.
The limit layer puts back, around the model's outputs, the bounds the original Fortran already guarantees. The items tagged "original guarantee" matter most:
| Limit | Outputs affected | Why |
|---|---|---|
| Non-negative quantities stay ≥ 0original guarantee | umf, flxprc1, flxsnow1, qrten, qsten, precip, snow, evapc, cufrc, qcu, qlu, qiu, cbmf, qc, rliq | In a month of samples the original Fortran never makes these 15 negative; they are clipped to ≥ 0 |
| Cloud fraction at most 1original guarantee | cufrc ≤ 1 | Cloud fraction is a proportion |
| Cloud-top / cloud-base levels roundedoriginal guarantee | cnt, cnb → rounded, limited to 1–30 | Level indices are integers; the model has 30 levels |
| No-convection flag | cush ≥ −1 | The original Fortran gives −1 when there is no convection |
| Water not taken below zero | qvten, qlten, qiten | Within one step, vapor, liquid water and ice cannot be reduced below 0 |
| Snow at most precipitation | snow ≤ precip; flxsnow1 ≤ flxprc1 at every level | Snow is part of precipitation |
| Isotopes derived from water ratios | 608 water-isotope outputs | The model's isotope outputs exceed 10¹⁹ and cannot be used. They are derived instead from the limited water tendencies, at each isotope's ratio to water in the column |
| Padding rows output 0 | Rows beyond each chunk's actual columns | These rows have no pressure data and the model outputs NaN on them; they are never used |
The gap comes from the computation in each call. One call computes one chunk of at most 16 columns (pcols = 16), and each rank makes 2 calls per step: over the month's 1488 steps that is 2,976 calls per rank and 1,523,712 across the 512 ranks. A rank's total shallow-convection time is its number of calls times the milliseconds per call. The 512 ranks run at the same time, so the month's run time follows each rank's 2,976 calls; the 1,523,712 calls of all 512 ranks times the milliseconds per call is the computation summed over the ranks, not wall-clock time. The original Fortran takes about 8.2 ms per call (CPU node). The new model takes 92.6 ms per call on CPU, 11 times the original Fortran and 22 times the MLP (4.3 ms); over the month the model alone costs each rank 276 seconds, where the original Fortran's shallow convection needs only about 24 seconds. The GPU brings the new model down to 39.7 ms per call, so it is clearly faster on GPU than on CPU. The MLP takes only half the original Fortran's time per call, so on CPU its month is slightly faster than the original Fortran.
These per-call figures agree with the month run times: on CPU the new model's shallow convection costs about 252 seconds more than the original Fortran, and the month is in fact 262 seconds slower; the MLP costs about 12 seconds less, and the month is 10 seconds faster. On GPU, both models' month times exceed this estimate by about 40–46 seconds, a fixed cost of running on the GPU (startup, warm-up and so on) that does not depend on the computation per call. The original Fortran's per-call times come from 50-step shadow runs (the model and the original Fortran both run on the same call and are timed separately; CPU-node job 7541265, GPU-node job 7546109), averaged over 51,200 calls (512 ranks × 50 steps × 2); the month runs time only the model. In the 2 initialization steps each rank makes another 4 calls, computed by the original Fortran and not included in the counts above.
| Month run time | Model calls in the month | Shallow convection per call (ms) | Shallow convection per rank over the month (s) | |||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Original Fortran (s) | Model run (s) | Multiple | Per rank | 512 ranks total | Original Fortran | Model | Model ÷ original | Original Fortran | Model | |
| New model · CPU | 440.1 | 702.6 | 1.60 | 2,976 | 1,523,712 | 8.2 | 92.6 | 11.34× | 24 | 276.1 |
| New model · GPU | 462.7 | 609.4 | 1.32 | 2,976 | 1,523,712 | 6.1 | 39.7 | 6.54× | 18 | 118.6 |
| MLP model · CPU | 442.4 | 432.0 | 0.98 | 2,976 | 1,523,712 | 8.2 | 4.3 | 0.52× | 24 | 12.7 |
| MLP model · GPU | 467.1 | 513.2 | 1.10 | 2,976 | 1,523,712 | 6.1 | 4.3 | 0.72× | 18 | 24.7 |
For each variable at each output time: the root-mean-square difference between the model run and the original Fortran, divided by the standard deviation of the original Fortran's field itself. 0 means identical; 1 means the difference is as large as the field's own variability. Output is written every 100 steps (about 2 days), 14 times over the month. When freeCAM replaces nothing, all 19 variables are 0.
By the month median, the new model is closer to the original Fortran than the MLP on 18 (CPU) and 19 (GPU) of the 19 variables. Deviations grow with time, and by the end of the month the gap narrows: for clouds, precipitation and meridional wind both models are near or above 1, essentially uncorrelated with the original Fortran by then, and the new model is smaller on 17 (CPU) and 12 (GPU) of the variables. The same new model run twice, on CPU and on GPU, also differs by 0.00–0.09 at month end; smaller differences than that are treated as noise.
| CPU | GPU | |||||||
|---|---|---|---|---|---|---|---|---|
| Month median | Month end | Month median | Month end | |||||
| New model | MLP | New model | MLP | New model | MLP | New model | MLP | |
| Atmospheric state | ||||||||
| T Temperature | 0.05 | 0.07 | 0.10 | 0.12 | 0.05 | 0.07 | 0.10 | 0.11 |
| Q Specific humidity | 0.13 | 0.15 | 0.25 | 0.24 | 0.13 | 0.15 | 0.22 | 0.22 |
| U Zonal wind | 0.27 | 0.30 | 0.48 | 0.54 | 0.26 | 0.28 | 0.49 | 0.52 |
| V Meridional wind | 0.67 | 0.81 | 1.13 | 1.20 | 0.64 | 0.71 | 1.10 | 1.14 |
| PS Surface pressure | 0.07 | 0.10 | 0.13 | 0.14 | 0.07 | 0.08 | 0.14 | 0.14 |
| TS Surface temperature | 0.12 | 0.19 | 0.23 | 0.27 | 0.13 | 0.18 | 0.23 | 0.23 |
| TMQ Column water vapor | 0.24 | 0.27 | 0.47 | 0.45 | 0.23 | 0.26 | 0.44 | 0.42 |
| Precipitation | ||||||||
| PRECT Total precipitation | 0.84 | 0.83 | 1.06 | 1.11 | 0.82 | 0.83 | 1.05 | 1.07 |
| PRECC Convective precipitation | 0.67 | 0.70 | 0.99 | 1.07 | 0.68 | 0.68 | 0.99 | 1.01 |
| PRECL Large-scale precipitation | 1.04 | 1.08 | 1.11 | 1.15 | 1.03 | 1.11 | 1.09 | 1.11 |
| Clouds | ||||||||
| CLDLOW Low cloud cover | 0.69 | 0.89 | 0.88 | 0.96 | 0.67 | 0.89 | 0.82 | 0.96 |
| CLDMED Mid cloud cover | 0.83 | 0.93 | 1.10 | 1.14 | 0.81 | 0.93 | 1.06 | 1.09 |
| CLDHGH High cloud cover | 0.88 | 0.98 | 1.24 | 1.28 | 0.83 | 0.91 | 1.23 | 1.19 |
| CLDTOT Total cloud cover | 0.78 | 0.92 | 1.08 | 1.09 | 0.74 | 0.91 | 1.04 | 1.05 |
| TGCLDLWP Cloud liquid water path | 0.95 | 1.05 | 1.15 | 1.29 | 0.93 | 1.09 | 1.21 | 1.27 |
| Radiation and surface fluxes | ||||||||
| FLNT TOA net longwave | 0.59 | 0.61 | 0.84 | 0.87 | 0.57 | 0.58 | 0.83 | 0.81 |
| FSNT TOA net shortwave | 0.27 | 0.29 | 0.39 | 0.39 | 0.27 | 0.30 | 0.37 | 0.39 |
| LHFLX Latent heat flux | 0.45 | 0.52 | 0.70 | 0.70 | 0.43 | 0.51 | 0.71 | 0.69 |
| SHFLX Sensible heat flux | 0.54 | 0.72 | 0.77 | 0.86 | 0.50 | 0.60 | 0.86 | 0.83 |
Bold cells are those where the new model is smaller than the MLP.
The 19 above are the main variables. Counting every time-varying field in the output files, with one deviation value per field, the distribution of those values is shown below.
The new model's median of 0.41 is about 20% below the MLP's 0.50–0.54; its maximum, 1.6 against the MLP's 2.9–3.3, differs more clearly. Results on CPU and GPU are almost the same.
| Median | 90th percentile | Maximum | Water-isotope BIG ERROR warnings | Fields with NaN/Inf | Job ID | |
|---|---|---|---|---|---|---|
| New model · CPU | 0.41 | 1.06 | 1.63 | 1 | 0 | 7614456 |
| New model · GPU | 0.41 | 1.04 | 1.60 | 0 | 0 | 7614457 |
| MLP model · CPU | 0.54 | 1.25 | 2.86 | 147 | 0 | 7580687 |
| MLP model · GPU | 0.50 | 1.21 | 3.34 | 223 | 0 | 7591565 |
BIG ERROR is a warning in the model log: the tendencies of the tracer water tagged as H2O should equal those of ordinary water, and the warning is printed when their relative difference exceeds 10⁻¹⁰. Over the month the new model triggered it 1 time (CPU) and 0 times (GPU); the MLP model 147 and 223 times. The new model's isotopes and tracer water are derived proportionally from the limited water tendencies, so they match ordinary water essentially by construction. The 1 warning on CPU came at about step 294, on one rank, with a relative difference of 8.9×10⁻⁸. The difference between 1 and 0 is chance and does not mean the GPU is better: CPU and GPU compute in different orders, so after a few days the weather differs, and the two runs are like two samples of the same month; for the MLP it is the other way round, with more on the GPU. None of the four runs produced NaN or infinity.
Written with Claude Code. It wraps the original model compute_uwshcu_inv_trigger_gated_r5_packed.pt, passes its outputs through the limits of Figure 1, and saves the result as a new TorchScript file, uwshcu_trigger_gated_r5_v4.pt, which both runs in Figures 2 and 3 use. Click a note on the right to highlight the matching lines of code.
"""Version 4 of the wrapper around a packed compute_uwshcu_inv model: physical limits on its outputs. wrap_v4.py <model.pt> <out.pt>The model's 582 bulk outputs pass through, then: * outputs the original kernel never makes negative (in the month capture) are floored at 0: umf, flxprc1, flxsnow1, qrten, qsten, precip, snow, evapc, cufrc, qcu, qlu, qiu, cbmf, qc, rliq; * cufrc <= 1; cush >= -1 (the original's no-convection value); cnt and cnb rounded into [1, 30]; * the water tendencies may not take vapour, liquid or ice below zero within the step; * snow <= precip, and the snow flux <= the precipitation flux at every interface; * the water-isotope slots are derived from those (limited) water tendencies at the column's ratios; * padding rows (the hook's rows past the chunk's columns: no surface pressure) answer 0.A NaN in a real column is left in place, so that CAM stops on it rather than running on zeros."""import sysimport torch SL = {"cush": (0, 1), "umf": (1, 32), "slflx": (32, 63), "qtflx": (63, 94), "flxprc1": (94, 125), "flxsnow1": (125, 156), "qvten": (156, 186), "qlten": (186, 216), "qiten": (216, 246), "sten": (246, 276), "uten": (276, 306), "vten": (306, 336), "qrten": (336, 366), "qsten": (366, 396), "precip": (396, 397), "snow": (397, 398), "evapc": (398, 428), "cufrc": (428, 458), "qcu": (458, 488), "qlu": (488, 518), "qiu": (518, 548), "cbmf": (548, 549), "qc": (549, 579), "rliq": (579, 580), "cnt": (580, 581), "cnb": (581, 582)}NON_NEGATIVE = ["umf", "flxprc1", "flxsnow1", "qrten", "qsten", "precip", "snow", "evapc", "cufrc", "qcu", "qlu", "qiu", "cbmf", "qc", "rliq"] class Limited(torch.nn.Module): def __init__(self, inner): super().__init__() self.inner = inner mask = torch.zeros(582, dtype=torch.bool) for n in NON_NEGATIVE: a, b = SL[n]; mask[a:b] = True self.register_buffer("nonneg", mask) self.register_buffer("iv", torch.tensor([9, 16, 23, 30])); self.register_buffer("il", torch.tensor([10, 17, 24, 31])) self.register_buffer("ii", torch.tensor([11, 18, 25, 32])) def forward(self, dt, ps0_inv, zs0_inv, p0_inv, z0_inv, dp0_inv, u0_inv, v0_inv, qv0_inv, ql0_inv, qi0_inv, t0_inv, s0_inv, tr0_inv, tke_inv, cldfrct_inv, concldfrct_inv, pblh, cush, dpdry0_inv): # type: (Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor, Tensor) -> Tensor n = t0_inv.shape[0] # the hook's padding rows carry no pressure profile; the model answers NaN on them. A real # column's interface pressures reach the surface's (1e5 Pa); index 0 of the _inv arrays is the top real = (torch.amax(ps0_inv, 1) > 1000.0) & torch.all(torch.isfinite(ps0_inv), 1) full = self.inner(dt, ps0_inv, zs0_inv, p0_inv, z0_inv, dp0_inv, u0_inv, v0_inv, qv0_inv, ql0_inv, qi0_inv, t0_inv, s0_inv, tr0_inv, tke_inv, cldfrct_inv, concldfrct_inv, pblh, cush, dpdry0_inv) y = full[:, 0:582].to(torch.float64) y = torch.where(self.nonneg.unsqueeze(0), torch.clamp_min(y, 0.0), y) rdt = 0.999999 / dt.reshape(1, 1) qv = torch.maximum(y[:, 156:186], -torch.clamp_min(qv0_inv - 1.0e-12, 0.0) * rdt) ql = torch.maximum(y[:, 186:216], -torch.clamp_min(ql0_inv, 0.0) * rdt) qi = torch.maximum(y[:, 216:246], -torch.clamp_min(qi0_inv, 0.0) * rdt) snow = torch.minimum(y[:, 397:398], y[:, 396:397]) flxsnow = torch.minimum(y[:, 125:156], y[:, 94:125]) y = torch.cat([torch.clamp_min(y[:, 0:1], -1.0), y[:, 1:125], flxsnow, qv, ql, qi, y[:, 246:397], snow, y[:, 398:428], torch.clamp_max(y[:, 428:458], 1.0), y[:, 458:580], torch.clamp(torch.round(y[:, 580:582]), 1.0, 30.0)], dim=1) # water isotopes at the column's ratios, from the limited water tendencies qv0 = qv0_inv.unsqueeze(2); ql0 = ql0_inv.unsqueeze(2); qi0 = qi0_inv.unsqueeze(2) trv = torch.index_select(tr0_inv, 2, self.iv); trl = torch.index_select(tr0_inv, 2, self.il); tri = torch.index_select(tr0_inv, 2, self.ii) rv = torch.clamp(torch.where(qv0 > 1.0e-12, trv / torch.clamp_min(qv0, 1.0e-30), torch.zeros_like(qv0)), 0.0, 4.0) rl = torch.clamp(torch.where(ql0 > 1.0e-12, trl / torch.clamp_min(ql0, 1.0e-30), rv), 0.0, 4.0) ri = torch.clamp(torch.where(qi0 > 1.0e-12, tri / torch.clamp_min(qi0, 1.0e-30), rv), 0.0, 4.0) rdt3 = rdt.unsqueeze(2) tv = torch.maximum(qv.unsqueeze(2) * rv, -torch.clamp_min(trv, 0.0) * rdt3) tl = torch.maximum(ql.unsqueeze(2) * rl, -torch.clamp_min(trl, 0.0) * rdt3) ti = torch.maximum(qi.unsqueeze(2) * ri, -torch.clamp_min(tri, 0.0) * rdt3) trten = torch.stack([tv, tl, ti], dim=3).reshape(n, 360) lf = torch.clamp((t0_inv - 248.0) / 20.0, 0.0, 1.0).unsqueeze(2) qc = y[:, 549:579].unsqueeze(2) wtqc = torch.stack([qc * lf * rv, qc * (1.0 - lf) * rv], dim=3).reshape(n, 240) w = (qv0_inv * dp0_inv).unsqueeze(2) rp = (rv * w).sum(1) / torch.clamp_min(w.sum(1), 1.0e-30) out = torch.cat([y, trten, wtqc, y[:, 396:397] * rp, y[:, 397:398] * rp], dim=1) return torch.where(real.unsqueeze(1), out, torch.zeros_like(out)) inner = torch.jit.load(sys.argv[1], map_location="cpu").eval()torch.jit.script(Limited(inner).eval()).save(sys.argv[2])print("saved", sys.argv[2])