@@ -42,20 +42,6 @@ Arguments:
42
42
"""
43
43
@inline specific (ρχ, ρ) = ρχ / ρ
44
44
45
- # ! format: off
46
- @inline specific_name (ρχ_name:: Symbol ) =
47
- if ρχ_name == :ρe_tot ; return :e_tot
48
- elseif ρχ_name == :ρq_tot ; return :q_tot
49
- elseif ρχ_name == :ρq_liq ; return :q_liq
50
- elseif ρχ_name == :ρq_ice ; return :q_ice
51
- elseif ρχ_name == :ρq_rai ; return :q_rai
52
- elseif ρχ_name == :ρn_liq ; return :n_liq
53
- elseif ρχ_name == :ρn_rai ; return :q_rai
54
- elseif ρχ_name == :ρq_sno ; return :q_sno
55
- else ; error (" Uncaught name: $ρχ_name " )
56
- end
57
- # ! format: on
58
-
59
45
@inline function specific (ρaχ, ρa, ρχ, ρ, turbconv_model)
60
46
# TODO : Replace turbconv_model struct by parameters, and include a_half in
61
47
# parameters, not in config
@@ -216,6 +202,36 @@ value is assumed to be equal to the value of `ρaχ` in `sgs`.
216
202
return :(NamedTuple {$specific_sgs_names} (($ (specific_sgs_values... ),)))
217
203
end
218
204
205
+ """
206
+ matching_subfields(tendency_field, specific_field)
207
+
208
+ Given a field that contains the tendencies of variables of the form `ρχ` or
209
+ `ρaχ` and another field that contains the values of specific variables `χ`,
210
+ returns all tuples `(tendency_field.<ρχ or ρaχ>, specific_field.<χ>, :<χ>)`.
211
+ Variables in `tendency_field` that do not have matching variables in
212
+ `specific_field` are omitted, as are variables in `specific_field` that do not
213
+ have matching variables in `tendency_field`. This function is needed to avoid
214
+ allocations due to failures in type inference, which are triggered when the
215
+ `propertynames` of these fields are manipulated during runtime in order to pick
216
+ out the matching subfields (as of Julia 1.8).
217
+ """
218
+ @generated function matching_subfields (tendency_field, specific_field)
219
+ tendency_names = Base. _nt_names (eltype (tendency_field))
220
+ specific_names = Base. _nt_names (eltype (specific_field))
221
+ prefix = :ρa in tendency_names ? :ρa : :ρ
222
+ relevant_specific_names =
223
+ filter (name -> Symbol (prefix, name) in tendency_names, specific_names)
224
+ subfield_tuples = map (
225
+ name -> :((
226
+ tendency_field.$ (Symbol (prefix, name)),
227
+ specific_field.$ name,
228
+ $ (QuoteNode (name)),
229
+ )),
230
+ relevant_specific_names,
231
+ )
232
+ return :(($ (subfield_tuples... ),))
233
+ end
234
+
219
235
"""
220
236
ρa⁺(gs)
221
237
0 commit comments