Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove ᶜspecific from the cache #3549

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open

Remove ᶜspecific from the cache #3549

wants to merge 7 commits into from

Conversation

ph-kev
Copy link
Member

@ph-kev ph-kev commented Jan 27, 2025

This PR removes ᶜspecific from the cache.

@ph-kev ph-kev force-pushed the kp/specific2 branch 2 times, most recently from c5db5ed to 878d0de Compare January 28, 2025 06:05
Comment on lines 112 to 113
q_tot_int_level =
Fields.field_values(Fields.level(p.scratch.ᶜtemp_scalar_3, 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's a bigger question here: why do we need an extra cache variable for this at all? We already have ᶜts, which contains this variable (q_tot)

Copy link
Member Author

@ph-kev ph-kev Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this part of the code, it look like q_tot only exists if the moisture model is EquilMoistModel or NonEquilMoistModel. For this function specifically, it looks like there is no config that call this function which uses a DryModel. Should I change it to just use p.precomputed.ᶜts.q_tot instead of using a scratch variable?

function thermo_vars(moisture_model, specific, K, Φ)
energy_var = (; e_int = specific.e_tot - K - Φ)
moisture_var = if moisture_model isa DryModel
(;)
elseif moisture_model isa EquilMoistModel
(; specific.q_tot)
elseif moisture_model isa NonEquilMoistModel
q_pt_args = (specific.q_tot, specific.q_liq, specific.q_ice)
(; q_pt = TD.PhasePartition(q_pt_args...))
end
return (; energy_var..., moisture_var...)
end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.precomputed.ᶜts.q_tot doesn't always exist (e.g., in the case of the dry model). I think we should instead use TD.total_specific_humidity(thermo_params, ts), which will point to the correct result for all simulations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if I am calling that function right, but when I tried to use it, I am getting this error:

infil> TD.total_specific_humidity(thermo_params, p.precomputed.ᶜts)
ERROR: MethodError: no method matching total_specific_humidity(::Thermodynamics.Parameters.ThermodynamicsParameters{…}, ::ClimaCore.Fields.Field{…})
The function `total_specific_humidity` exists, but no method is defined for this combination of argument types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'd need to broadcast for it to work on the whole field: TD.total_specific_humidity.(thermo_params, p.precomputed.ᶜts).. But my reservation is about whether we actually need a cached variable for that field to begin with. Let's schedule some time to chat about this.

@ph-kev
Copy link
Member Author

ph-kev commented Jan 28, 2025

Also, the reproducibility tests are failing. Should I expect this?

For this buildkite, it still fail the reproducibility tests despite the commit looking fine to me.

@@ -336,13 +336,15 @@ NVTX.@annotate function set_diagnostic_edmf_precomputed_quantities_do_integral!(
Fields.field_values(Fields.level(Fields.coordinate_field(Y.f).z, half))

# integral
@. p.scratch.ᶜtemp_scalar_3 = Y.c.ρq_tot / Y.c.ρ
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the way, you can improve readability by defining

q_tot = p.scratch.ᶜtemp_scalar_3

so that expressions below will have q_tot instead of p.scratch.ᶜtemp_scalar_3

@charleskawczynski
Copy link
Member

Also, the reproducibility tests are failing. Should I expect this?

For this buildkite, it still fail the reproducibility tests despite the commit looking fine to me.

No, I think the repro tests should be passing. This is supposed to be a refactoring change, correct?

if so, I think you do need to make sure that you’ve rebased on the latest main.

@ph-kev ph-kev force-pushed the kp/specific2 branch 2 times, most recently from 97507a6 to 91d8c81 Compare January 29, 2025 01:31
@charleskawczynski
Copy link
Member

Actually, looking at the code changes, it's possible that we would expect to see changes due to machine precision error-- computing variables on the fly multiple times could result in changes.

@ph-kev
Copy link
Member Author

ph-kev commented Jan 29, 2025

Actually, looking at the code changes, it's possible that we would expect to see changes due to machine precision error-- computing variables on the fly multiple times could result in changes.

I found the reason why the reproducibility tests are failing. I changed this line to this and it passed now.

@. ᶜ∇²specific_energy = wdivₕ(gradₕ((Y.c.ρe_tot / Y.c.ρ) + (ᶜp / Y.c.ρ)))

The buildkite for reference: https://buildkite.com/clima/climaatmos-ci/builds/22504

@ph-kev ph-kev force-pushed the kp/specific2 branch 4 times, most recently from 83b565b to 9147021 Compare January 29, 2025 21:14
This is done by pattern matching in `propertynames(tendency_field)` for names of
the form `ρ[quantity]` or `ρa[quantity]`.
"""
@generated function matching_ρ(Y, tendency_field)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we somehow eliminate this function and write things more explicitly?

Of not, let’s write a unit test for this function.

Comment on lines 112 to 113
q_tot_int_level =
Fields.field_values(Fields.level(p.scratch.ᶜtemp_scalar_3, 1))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

p.precomputed.ᶜts.q_tot doesn't always exist (e.g., in the case of the dry model). I think we should instead use TD.total_specific_humidity(thermo_params, ts), which will point to the correct result for all simulations.

@ph-kev ph-kev force-pushed the kp/specific2 branch 4 times, most recently from 4b18c1d to 8fb7679 Compare February 3, 2025 18:26
Comment on lines +396 to +397
@. p.scratch.ᶜtemp_scalar_3 = Y.c.ρq_tot / Y.c.ρ
q_tot = p.scratch.ᶜtemp_scalar_3
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bc_q_tot = @lazy @. Y.c.ρq_tot / Y.c.ρ

Operators.column_reduce!(
(nt1, nt2) ->
abs(nt1.q_tot - q_tot_isoline) < abs(nt2.q_tot - q_tot_isoline) ?
nt1 : nt2,
isoline_z_ρ_q,
Base.broadcasted(NT ∘ tuple, ᶜz, Y.c.ρ, ᶜspecific.q_tot),
Base.broadcasted(NT ∘ tuple, ᶜz, Y.c.ρ, q_tot),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Base.broadcasted(NT tuple, ᶜz, Y.c.ρ, q_tot),
Base.broadcasted(NT tuple, ᶜz, Y.c.ρ, bc_q_tot),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants