Skip to content

Commit

Permalink
Change name to output_name in diagnostics
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Sep 11, 2023
1 parent 0d4ced6 commit 78a98f8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
6 changes: 4 additions & 2 deletions docs/src/diagnostics.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ Second, you can specify the diagnostics you want to output directly in the
```
diagnostics:
- short_name: air_density
name: a_name
output_name: a_name
period: 3hours
- reduction_time: average
short_name: air_density
period: 12hours
```
This adds two diagnostics (both for `air_density`). The `period` keyword
identifies the period over which to compute the reduction and how often to save
to disk. `name` is optional, and if provided, it identifies the name of the
to disk. `output_name` is optional, and if provided, it identifies the name of the
output file.

### From a script
Expand Down Expand Up @@ -101,6 +101,8 @@ More specifically, a `ScheduledDiagnostic` contains the following pieces of data
`pre_output_hook!` is called with two arguments: the value accumulated during
the reduction, and the number of times the diagnostic was computed from the
last time it was output.
- `output_name`: A descriptive name that can be used by the `output_writer`. If
not provided, a default one is generated.

To implement operations like the arithmetic average, the `reduction_time_func`
has to be chosen as `+`, and a `pre_output_hook!` that renormalize `acc` by the
Expand Down
45 changes: 23 additions & 22 deletions src/diagnostics/diagnostics.jl
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ struct ScheduledDiagnosticIterations{T1, T2, OW, F1, F2, PO}
reduction_space_func::F2
compute_every::T2
pre_output_hook!::PO
name::String
output_name::String

"""
ScheduledDiagnosticIterations(; variable::DiagnosticVariable,
Expand All @@ -209,7 +209,7 @@ struct ScheduledDiagnosticIterations{T1, T2, OW, F1, F2, PO}
reduction_space_func = nothing,
compute_every = isa_reduction ? 1 : output_every,
pre_output_hook! = nothing,
name = descriptive_name(self) )
output_name = descriptive_name(self) )
A `DiagnosticVariable` that has to be computed and output during a simulation with a cadence
Expand Down Expand Up @@ -261,9 +261,9 @@ struct ScheduledDiagnosticIterations{T1, T2, OW, F1, F2, PO}
discarded. An example of `pre_output_hook!` to compute the arithmetic
average is `pre_output_hook!(acc, N) = @. acc = acc / N`.
- `name`: A descriptive name for this particular diagnostic. If none is provided, one
will be generated mixing the short name of the variable, the reduction, and the
period of the reduction.
- `output_name`: A descriptive name for this particular diagnostic. If none is provided,
one will be generated mixing the short name of the variable, the
reduction, and the period of the reduction.
"""
Expand All @@ -275,7 +275,7 @@ struct ScheduledDiagnosticIterations{T1, T2, OW, F1, F2, PO}
reduction_space_func = nothing,
compute_every = isnothing(reduction_time_func) ? output_every : 1,
pre_output_hook! = nothing,
name = get_descriptive_name(
output_name = get_descriptive_name(
variable,
output_every,
reduction_time_func,
Expand All @@ -287,14 +287,14 @@ struct ScheduledDiagnosticIterations{T1, T2, OW, F1, F2, PO}
# We provide an inner constructor to enforce some constraints

output_every % compute_every == 0 || error(
"output_every ($output_every) should be multiple of compute_every ($compute_every) for diagnostic $(name)",
"output_every ($output_every) should be multiple of compute_every ($compute_every) for diagnostic $(output_name)",
)

isa_reduction = !isnothing(reduction_time_func)

# If it is not a reduction, we compute only when we output
if !isa_reduction && compute_every != output_every
@warn "output_every ($output_every) != compute_every ($compute_every) for $(name), changing compute_every to match"
@warn "output_every ($output_every) != compute_every ($compute_every) for $(output_name), changing compute_every to match"
compute_every = output_every
end

Expand All @@ -320,7 +320,7 @@ struct ScheduledDiagnosticIterations{T1, T2, OW, F1, F2, PO}
reduction_space_func,
compute_every,
pre_output_hook!,
name,
output_name,
)
end
end
Expand All @@ -334,7 +334,7 @@ struct ScheduledDiagnosticTime{T1, T2, OW, F1, F2, PO}
reduction_space_func::F2
compute_every::T2
pre_output_hook!::PO
name::String
output_name::String

"""
ScheduledDiagnosticTime(; variable::DiagnosticVariable,
Expand All @@ -344,7 +344,7 @@ struct ScheduledDiagnosticTime{T1, T2, OW, F1, F2, PO}
reduction_space_func = nothing,
compute_every = isa_reduction ? :timestep : output_every,
pre_output_hook! = nothing,
name = descriptive_name(self))
output_name = descriptive_name(self))
A `DiagnosticVariable` that has to be computed and output during a simulation with a
Expand Down Expand Up @@ -400,10 +400,11 @@ struct ScheduledDiagnosticTime{T1, T2, OW, F1, F2, PO}
discarded. An example of `pre_output_hook!` to compute the arithmetic
average is `pre_output_hook!(acc, N) = @. acc = acc / N`.
- `name`: A descriptive name for this particular diagnostic. If none is provided, one
will be generated mixing the short name of the variable, the reduction, and the
period of the reduction.
- `output_name`: A descriptive name for this particular diagnostic. If none is provided,
one will be generated mixing the short name of the variable, the
reduction, and the period of the reduction.
"""

function ScheduledDiagnosticTime(;
variable::DiagnosticVariable,
output_every,
Expand All @@ -413,7 +414,7 @@ struct ScheduledDiagnosticTime{T1, T2, OW, F1, F2, PO}
compute_every = isnothing(reduction_time_func) ? output_every :
:timestep,
pre_output_hook! = nothing,
name = get_descriptive_name(
output_name = get_descriptive_name(
variable,
output_every,
reduction_time_func,
Expand All @@ -428,15 +429,15 @@ struct ScheduledDiagnosticTime{T1, T2, OW, F1, F2, PO}
# the list of diagnostics
if !isa(compute_every, Symbol)
output_every % compute_every == 0 || error(
"output_every ($output_every) should be multiple of compute_every ($compute_every) for diagnostic $(name)",
"output_every ($output_every) should be multiple of compute_every ($compute_every) for diagnostic $(output_name)",
)
end

isa_reduction = !isnothing(reduction_time_func)

# If it is not a reduction, we compute only when we output
if !isa_reduction && compute_every != output_every
@warn "output_every ($output_every) != compute_every ($compute_every) for $(name), changing compute_every to match"
@warn "output_every ($output_every) != compute_every ($compute_every) for $(output_name), changing compute_every to match"
compute_every = output_every
end

Expand All @@ -462,7 +463,7 @@ struct ScheduledDiagnosticTime{T1, T2, OW, F1, F2, PO}
reduction_space_func,
compute_every,
pre_output_hook!,
name,
output_name,
)
end
end
Expand Down Expand Up @@ -490,10 +491,10 @@ function ScheduledDiagnosticIterations(
output_every = sd_time.output_every / Δt

isinteger(output_every) || error(
"output_every ($output_every) should be multiple of the timestep ($Δt) for diagnostic $(sd_time.name)",
"output_every ($output_every) should be multiple of the timestep ($Δt) for diagnostic $(sd_time.output_name)",
)
isinteger(compute_every) || error(
"compute_every ($compute_every) should be multiple of the timestep ($Δt) for diagnostic $(sd_time.name)",
"compute_every ($compute_every) should be multiple of the timestep ($Δt) for diagnostic $(sd_time.output_name)",
)

ScheduledDiagnosticIterations(;
Expand All @@ -504,7 +505,7 @@ function ScheduledDiagnosticIterations(
sd_time.reduction_space_func,
compute_every = convert(Int, compute_every),
sd_time.pre_output_hook!,
sd_time.name,
sd_time.output_name,
)
end

Expand Down Expand Up @@ -536,7 +537,7 @@ function ScheduledDiagnosticTime(
sd_time.reduction_space_func,
compute_every,
sd_time.pre_output_hook!,
sd_time.name,
sd_time.output_name,
)
end

Expand Down
2 changes: 1 addition & 1 deletion src/diagnostics/writers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function HDF5Writer()

output_path = joinpath(
integrator.p.simulation.output_dir,
"$(diagnostic.name)_$time.h5",
"$(diagnostic.output_name)_$time.h5",
)

hdfwriter = InputOutput.HDF5Writer(output_path, integrator.p.comms_ctx)
Expand Down

0 comments on commit 78a98f8

Please sign in to comment.