Thermodynamics.jl icon indicating copy to clipboard operation
Thermodynamics.jl copied to clipboard

Define `get_expensive_vars` or something?

Open charleskawczynski opened this issue 1 year ago • 1 comments

There are some ClimaAtmos functions that call several functions, resulting in several expensive calls. We could either cache the expensive variables in the thermo state, or we could define a getter that efficiently computes all of the variables and returns a struct where it's cached. One advantage of this is that it's use is optional.

For example:

    @. ᶜlinear_buoygrad = buoyancy_gradients(
        params,
        moisture_model,
        EnvBuoyGrad(
            BuoyGradMean(),
            TD.air_temperature(thermo_params, ᶜts⁰),                           # t_sat
            TD.vapor_specific_humidity(thermo_params, ᶜts⁰),                   # qv_sat
            ᶜspecific⁰.q_tot,                                                  # qt_sat
            TD.dry_pottemp(thermo_params, ᶜts⁰),                               # θ_sat
            TD.liquid_ice_pottemp(thermo_params, ᶜts⁰),                        # θ_liq_ice_sat
            projected_vector_data(
                C3,
                ᶜgradᵥ(ᶠinterp(TD.virtual_pottemp(thermo_params, ᶜts⁰))),
                ᶜlg,
            ),                                                                 # ∂θv∂z_unsat
            projected_vector_data(C3, ᶜgradᵥ(ᶠinterp(ᶜspecific⁰.q_tot)), ᶜlg), # ∂qt∂z_sat
            projected_vector_data(
                C3,
                ᶜgradᵥ(ᶠinterp(TD.liquid_ice_pottemp(thermo_params, ᶜts⁰))),
                ᶜlg,
            ),                                                                 # ∂θl∂z_sat
            ᶜp,                                                                # p
            ifelse(TD.has_condensate(thermo_params, ᶜts⁰), 1, 0),              # en_cld_frac
            ᶜρ⁰,                                                               # ρ
        ),
    )

is a massive kernel:

Screen Shot 2023-09-01 at 4 09 26 PM

Hyperdiffusion (on the left) is shown for scale. It's called 3 times and each one is 10% of root.

charleskawczynski avatar Sep 01 '23 23:09 charleskawczynski

Local notes:

    @. ᶜlinear_buoygrad = buoyancy_gradients(
        BuoyGradMean(),
        thermo_params,
        moisture_model,
        EnvBuoyGradVars(
            ᶜts,
            pd(ᶜgradᵥ(ᶠts), ᶜlg,),    # ∂θv∂z_unsat
            pd(ᶜgradᵥ(TD.total_specific_humidity(thermo_params, ᶠts)), ᶜlg),                                      # ∂qt∂z_sat
            pd(ᶜgradᵥ(TD.liquid_ice_pottemp(thermo_params, ᶠts)), ᶜlg,), # ∂θl∂z_sat
        ),
    )

    if atmos.moisture_model isa DryModel
		@. ᶠts = TD.PhaseDry_ρe(
		    ᶠinterp(TD.air_temperature(thermo_params, ᶜts)),
		    ᶠinterp(TD.internal_energy(thermo_params, ᶜts)),
	    )
	elseif atmos.moisture_model isa EquilMoistModel
		@. ᶠts = TD.PhaseEquil_ρeq(
		    ᶠinterp(TD.air_temperature(thermo_params, ᶜts)),
		    ᶠinterp(TD.internal_energy(thermo_params, ᶜts)),
		    ᶠinterp(TD.total_specific_humidity(thermo_params, ᶜts)),
		    3,
            eltype(thermo_params)(0.003),
	    )
	else
		@assert atmos.moisture_model isa NonEquilMoistModel
		@. ᶠts = TD.PhaseNonEquil_ρeq(
		    ᶠinterp(TD.air_temperature(thermo_params, ᶜts)),
		    ᶠinterp(TD.internal_energy(thermo_params, ᶜts)),
		    ᶠinterp(TD.PhasePartition(thermo_params, ᶜts)),
	    )
	end

charleskawczynski avatar Nov 15 '23 03:11 charleskawczynski