Diagnostics
Diagnostics compute variables that are only used for output, e.g. accumulated precipitation or the daily maximum temperature. They are calculated on the fly every time step and stored in simulation.variables.output. Nothing in the dynamics or parameterizations reads from variables.output, so diagnostics cannot change the simulation, they only add work to the time step. The primitive equation models hold them in model.diagnostics, a NamedTuple of AbstractDiagnostics.
The diagnostics are responsible for the accumulation (sum, maximum, mean, ...) every time step, the output writer resets them after every output. That way the output interval determines the period over which a diagnostic is taken: precipitation is accumulated since the last output, with daily output Maximum is the daily maximum and with weekly output Mean the weekly mean. Without output, diagnostics are never reset and taken over the whole simulation.
Default diagnostics
The PrimitiveWetModel accumulates large-scale and convective rain and snow as well as the river runoff, all in meters. The parameterizations only compute rates (in m/s, in variables.parameterizations) and the Accumulate diagnostic integrates them over time
using SpeedyWeather
spectral_grid = SpectralGrid(truncation = 32, nlayers = 8)
model = PrimitiveWetModel(spectral_grid)
model.diagnostics(rain_large_scale = Accumulate <: AbstractDiagnostic
├ rate::Symbol = rain_rate_large_scale
├ name::Symbol = rain_large_scale
├ namespace::Symbol =
├ units::String = m
└ desc::String = Large-scale precipitation (rain, accumulated since last outp..., snow_large_scale = Accumulate <: AbstractDiagnostic
├ rate::Symbol = snow_rate_large_scale
├ name::Symbol = snow_large_scale
├ namespace::Symbol =
├ units::String = m
└ desc::String = Large-scale precipitation (snow, accumulated since last outp..., rain_convection = Accumulate <: AbstractDiagnostic
├ rate::Symbol = rain_rate_convection
├ name::Symbol = rain_convection
├ namespace::Symbol =
├ units::String = m
└ desc::String = Convective precipitation (rain, accumulated since last outpu..., snow_convection = Accumulate <: AbstractDiagnostic
├ rate::Symbol = snow_rate_convection
├ name::Symbol = snow_convection
├ namespace::Symbol =
├ units::String = m
└ desc::String = Convective precipitation (snow, accumulated since last outpu..., river_runoff = Accumulate <: AbstractDiagnostic
├ rate::Symbol = river_runoff_rate
├ name::Symbol = river_runoff
├ namespace::Symbol = land
├ units::String = m
└ desc::String = River runoff from soil moisture (accumulated since last outp...)The diagnostics define their own variables in variables.output, so after running a simulation
simulation = initialize!(model)
run!(simulation, period = Day(2))
simulation.variables.output(rain_large_scale = Float32[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], snow_large_scale = Float32[0.0, 9.732626f-7, 1.3758541f-6, 1.5040428f-6, 2.189832f-6, 3.4844338f-6, 6.5411723f-6, 1.0784782f-5, 1.2334924f-5, 1.248371f-5 … 4.5723253f-5, 4.8893795f-5, 4.3232252f-5, 3.6592028f-5, 2.4783514f-5, 1.9948106f-5, 2.4046803f-5, 4.096516f-5, 6.665565f-5, 9.851777f-5], rain_convection = Float32[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], snow_convection = Float32[0.00020886266, 0.00017843206, 0.00018324757, 0.00022073326, 0.00024218365, 0.00022363126, 0.00018055037, 0.00012317412, 8.066779f-5, 6.132158f-5 … 0.0001700172, 0.00011605768, 1.764011f-5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], land = (river_runoff = Float32[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],))The output variables are also written to restart files and restored with StartFromFile so that, e.g., accumulations continue across restarts.
Adding diagnostics
To add a diagnostic, merge it into the default diagnostics, e.g. the maximum and mean surface air temperature with Maximum and Mean. With daily output they are the daily maximum and daily mean
diagnostics = (;
PrecipitationDiagnostics()...,
temperature_max = Maximum(),
temperature_mean = Mean(),
)
output = ArrayOutput(interval = Day(1), groups = (:output,))
model = PrimitiveWetModel(spectral_grid; diagnostics, output)
simulation = initialize!(model)
run!(simulation, period = Day(2), output = true)
model.output.output[end].output.surface_air_temperature_maximum3168-element, 48-ring (XY) OctahedralGaussianField{Float32, 1} as Array on CPU
254.96588f0
255.1967f0
255.34726f0
255.42155f0
255.44388f0
255.43384f0
255.40411f0
255.36641f0
255.33347f0
255.31696f0
⋮
245.46281f0
245.46854f0
245.189f0
244.89607f0
244.70786f0
244.5254f0
244.11528f0
243.28033f0
242.00195f0Pass variable, group, namespace and units to take the maximum or mean of another 2D grid variable. Without PrecipitationDiagnostics() in diagnostics the precipitation is not accumulated, and precipitation output variables like ConvectiveRain are not available.
Custom diagnostics
A diagnostic is a subtype of AbstractDiagnostic which
defines the variables it needs via
variables(diagnostic)returningOutputVariables so that they are allocated invariables.output,optionally initializes these with
initialize!(variables, diagnostic, model),calculates them in
diagnostic!(variables, diagnostic, model), called once per time step after the time step, before the output is written,and resets them in
reset_diagnostic!(variables, diagnostic, model), called by the output writer after every output.
E.g. a diagnostic for the minimum surface air temperature
struct MinimumSurfaceAirTemperature <: SpeedyWeather.AbstractDiagnostic end
SpeedyWeather.variables(::MinimumSurfaceAirTemperature) =
(OutputVariable(:surface_air_temperature_minimum, SpeedyWeather.Grid2D(), units = "K"),)
SpeedyWeather.initialize!(vars, diagnostic::MinimumSurfaceAirTemperature, model) =
SpeedyWeather.reset_diagnostic!(vars, diagnostic, model)
SpeedyWeather.reset_diagnostic!(vars, ::MinimumSurfaceAirTemperature, model) =
fill!(vars.output.surface_air_temperature_minimum, Inf)
function SpeedyWeather.diagnostic!(vars, ::MinimumSurfaceAirTemperature, model)
(; surface_air_temperature_minimum) = vars.output
surface_air_temperature_minimum .= min.(surface_air_temperature_minimum, vars.parameterizations.surface_air_temperature)
return nothing
end
diagnostics = (; PrecipitationDiagnostics()..., temperature_min = MinimumSurfaceAirTemperature())
model = PrimitiveWetModel(spectral_grid; diagnostics)
simulation = initialize!(model)
run!(simulation, period = Day(1))
simulation.variables.output.surface_air_temperature_minimum3168-element, 48-ring (XY) OctahedralGaussianField{Float32, 1} as Array on CPU
252.71793f0
252.86859f0
252.95996f0
252.99721f0
253.00293f0
252.99625f0
252.98749f0
252.98244f0
252.98526f0
252.99704f0
⋮
240.75063f0
240.92036f0
240.75833f0
240.55742f0
240.4254f0
240.25833f0
239.84134f0
239.01225f0
237.76392f0To write diagnostics into the netCDF output, define an output variable whose path points into simulation.variables.output, see Customizing netCDF output.
SpeedyWeather.Accumulate Type
Diagnostic that accumulates a rate [unit/s] in variables.parameterizations over time, i.e. adds Δt * rate every time step into variables.output, e.g. precipitation [m] from the precipitation rate [m/s]. Δt is the model time that passes per time step. Reset to zero by the output writer, so it is the accumulation since the last output. Silently skipped if the rate does not exist (warns on initialization). Fields are
rate::Symbol: [OPTION] Name of the rate [unit/s] invariables.parameterizationsto accumulatename::Symbol: [OPTION] Name of the accumulated variable invariables.outputnamespace::Symbol = Symbol(): [OPTION] Namespace of both rate and accumulated variable, e.g.:landunits::String = "": [OPTION] Units of the accumulated variabledesc::String = "": [OPTION] Description of the accumulated variable
SpeedyWeather.Maximum Type
Diagnostic for the maximum of a 2D grid variable, e.g. the surface air temperature, over the output interval: The output writer resets the maximum (to -Inf) after writing, so with daily output this is the daily maximum. NaNs in the variable propagate into the maximum. Silently skipped if the variable does not exist (warns on initialization). Fields are
variable::Symbol = :surface_air_temperature: [OPTION] Name of the 2D grid variable to take the maximum ofgroup::Symbol = :parameterizations: [OPTION] Variable group ofvariable, e.g.:parameterizationsor:gridnamespace::Symbol = Symbol(): [OPTION] Namespace ofvariableand of the maximum, e.g.:landname::Symbol = Symbol(variable, :_maximum): [OPTION] Name of the maximum invariables.outputunits::String = "K": [OPTION] Units of the maximum
SpeedyWeather.Mean Type
Diagnostic for the time mean of a 2D grid variable, e.g. the surface air temperature, over the output interval: The output writer resets the mean after writing, so with weekly output this is the weekly mean. Updated incrementally so that it can be read (e.g. by output) at any time step. NaN before the first time step after a reset. Every time step has the same weight. Silently skipped if the variable does not exist (warns on initialization). Fields are
variable::Symbol = :surface_air_temperature: [OPTION] Name of the 2D grid variable to take the mean ofgroup::Symbol = :parameterizations: [OPTION] Variable group ofvariable, e.g.:parameterizationsor:gridnamespace::Symbol = Symbol(): [OPTION] Namespace ofvariableand of the mean, e.g.:landname::Symbol = Symbol(variable, :_mean): [OPTION] Name of the mean invariables.outputunits::String = "K": [OPTION] Units of the mean