Skip to content

Commit

Permalink
Generalize file reading infra to take given files
Browse files Browse the repository at this point in the history
  • Loading branch information
Sbozzolo committed Dec 16, 2024
1 parent df1a3ad commit fe9bea7
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 52 deletions.
23 changes: 18 additions & 5 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,29 @@ Main
-------
### Features

### DYAMOND-summer initial conditions
### Read initial conditions from NetCDF files

Added functionality to allow initial conditions to be overwritten by
interpolated datasets. Currently supports equilibrium moisture models
for DYAMOND runs, assuming a simulation start date of 1 August 2016.
Added functionality to allow initial conditions to be overwritten by
interpolated NetCDF datasets.

To use this feature from the YAML interface, just pass the path of the file.
We expect the file to contain the following variables:
- `p`, for pressure,
- `t`, for temperature,
- `q`, for humidity,
- `u, v, w`, for velocity,
- `cswc, crwc` for snow and rain water content (for 1 moment microphysics).

For example, to use the DYAMONDSummer initial condition, set
```
initial_condition: "artifact\"DYAMONDSummer\"/DYAMOND_SUMMER_ICS_p98deg.nc"
```
in your configuration file.

### Write diagnostics to text files

Added functionality to write diagnostics in DictWriter to text files.
This is useful for outputing scalar diagnostics, such as total mass of
This is useful for outputting scalar diagnostics, such as total mass of
the atmosphere. PR [3476](https://github.com/CliMA/ClimaAtmos.jl/pull/3476)

v0.28.0
Expand Down
2 changes: 1 addition & 1 deletion config/default_configs/default_config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ surface_temperature:
help: "Prescribed surface temperature functional form ['ZonallySymmetric' (default), 'ZonallyAsymmetric', 'RCEMIPII']"
value: "ZonallySymmetric"
initial_condition:
help: "Initial condition [`DryBaroclinicWave`, `MoistBaroclinicWave`, `DecayingProfile`, `IsothermalProfile`, `Bomex`, `DryDensityCurrentProfile`, `AgnesiHProfile`, `ScharProfile`, `RisingThermalBubbleProfile`, `ISDAC`, `DYAMONDSummer`]"
help: "Initial condition [`DryBaroclinicWave`, `MoistBaroclinicWave`, `DecayingProfile`, `IsothermalProfile`, `Bomex`, `DryDensityCurrentProfile`, `AgnesiHProfile`, `ScharProfile`, `RisingThermalBubbleProfile`, `ISDAC`], or a file path for a NetCDF file (read documentation about requirements)."
value: "DecayingProfile"
perturb_initstate:
help: "Add a perturbation to the initial condition [`false`, `true` (default)]"
Expand Down
2 changes: 1 addition & 1 deletion config/gpu_configs/gpu_aquaplanet_dyamond_summer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ toml: [toml/longrun_aquaplanet.toml]
prescribe_ozone: true
aerosol_radiation: true
prescribed_aerosols: ["CB1", "CB2", "DST01", "OC1", "OC2", "SO4", "SSLT01"]
initial_condition: "DYAMONDSummer"
initial_condition: "artifact\"DYAMONDSummer\"/DYAMOND_SUMMER_ICS_p98deg.nc"
topography: "Earth"
diagnostics:
- short_name: [ts, ta, thetaa, ha, pfull, rhoa, ua, va, wa, hur, hus, cl, clw, cli, hussfc, evspsbl, pr, rv]
Expand Down
48 changes: 31 additions & 17 deletions src/initial_conditions/initial_conditions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,16 @@ function (initial_condition::DecayingProfile)(params)
end

"""
DYAMONDSummer()
MoistFromFile(file_path)
This function assigns a default initial condition, populating the `LocalState`
with `NaN`, prior to applying the `overwrite_initial_conditions` method, which
interpolates values from NetCDF data onto the `ExtrudedFiniteDifferenceSpace`.
This function assigns an empty initial condition for , populating the `LocalState` with
`NaN`, and later overwriting it with the content of the given file
"""
struct DYAMONDSummer <: InitialCondition end
struct MoistFromFile <: InitialCondition
file_path::String
end

function (initial_condition::DYAMONDSummer)(params)
function (initial_condition::MoistFromFile)(params)
function local_state(local_geometry)
FT = eltype(params)
grav = CAP.grav(params)
Expand Down Expand Up @@ -379,28 +380,41 @@ end

"""
overwrite_initial_conditions!(initial_condition, args...)
Do-nothing fallback method for the operation overwriting initial conditions
(this functionality required in instances where we interpolate initial conditions from NetCDF files).
Future work may revisit this design choice.
"""
overwrite_initial_conditions!(initial_condition::InitialCondition, args...) =
(return nothing)
function overwrite_initial_conditions!(
initial_condition::InitialCondition,
args...,
)
return nothing
end

"""
overwrite_initial_conditions!(initial_condition, Y, thermo_params, config)
Given a prognostic state `Y`, an `initial condition` (specifically, where
initial values are assigned from interpolations of existing datasets), a `thermo_state`, this function overwrites the default initial condition
and populates prognostic variables with interpolated values using the `SpaceVaryingInputs`
tool. To mitigate issues related to unbalanced states following the interpolation operation,
we recompute vertical pressure levels assuming hydrostatic balance, given the surface pressure
overwrite_initial_conditions!(initial_condition::MoistFromFile, Y, thermo_params, config)
Given a prognostic state `Y`, an `initial condition` (specifically, where initial values are
assigned from interpolations of existing datasets), a `thermo_state`, this function
overwrites the default initial condition and populates prognostic variables with
interpolated values using the `SpaceVaryingInputs` tool. To mitigate issues related to
unbalanced states following the interpolation operation, we recompute vertical pressure
levels assuming hydrostatic balance, given the surface pressure.
We expect the file to contain the following variables:
- `p`, for pressure,
- `t`, for temperature,
- `q`, for humidity,
- `u, v, w`, for velocity,
- `cswc, crwc` for snow and rain water content (for 1 moment microphysics).
"""
function overwrite_initial_conditions!(
initial_condition::DYAMONDSummer,
initial_conditions::MoistFromFile,
Y,
thermo_params,
)
# Get file from AtmosArtifacts
file_path = AA.dyamond_summer_artifact_path(; context = config.comms_ctx)
file_path = isfile(file_path) || error("$(file_path) is not a file")
center_space = Fields.axes(Y.c)
face_space = Fields.axes(Y.f)
# Using surface pressure, air temperature and specific humidity
Expand Down
26 changes: 13 additions & 13 deletions src/solver/type_getters.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,9 +294,10 @@ function get_initial_condition(parsed_args)
"RisingThermalBubbleProfile",
"ScharProfile",
"PrecipitatingColumn",
"DYAMONDSummer",
]
return getproperty(ICs, Symbol(parsed_args["initial_condition"]))()
elseif isfile(parsed_args["initial_condition"])
return ICs.MoistFromFile(parsed_args["initial_condition"])
elseif parsed_args["initial_condition"] == "GCM"
@assert parsed_args["prognostic_tke"] == true
return ICs.GCMDriven(
Expand Down Expand Up @@ -663,22 +664,21 @@ function get_simulation(config::AtmosConfig)
t_start = Spaces.undertype(axes(Y.c))(0)
end
@info "Allocating Y: $s"

# In instances where we wish to interpolate existing datasets, e.g.
# NetCDF files containing spatially varying thermodynamic properties,
# this call to `overwrite_initial_conditions` accesses rewrite the
# variable in `Y` (specific to `initial_condition`) with those computed
# using the `SpaceVaryingInputs` tool.
CA.InitialConditions.overwrite_initial_conditions!(
initial_condition,
Y,
params.thermodynamics_params,
)
end

tracers = get_tracers(config.parsed_args)

# In instances where we wish to interpolate existing datasets,
# e.g. NetCDF files containing spatially varying thermodynamic properties,
# this call to `overwrite_initial_conditions`
# accesses the appropriate artifact for a given `initial_condition`
# and then updates some predetermined state `Y` (specific to `initial_condition`)
# with those computed using the `SpaceVaryingInputs` tool.
CA.InitialConditions.overwrite_initial_conditions!(
initial_condition,
Y,
params.thermodynamics_params,
)

s = @timed_str begin
p = build_cache(
Y,
Expand Down
15 changes: 0 additions & 15 deletions src/utils/AtmosArtifacts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,4 @@ function earth_orography_file_path(; context = nothing)
)
end

"""
dyamond_summer_artifact_path(; context = nothing)
Construct the file path for the 0.98deg DYAMOND initial conditions:
the artifact contains initial conditions for DYAMOND simulations
beginning on 1 August 2016.
"""
function dyamond_summer_artifact_path(; context = nothing)
filename = "DYAMOND_SUMMER_ICS_p98deg.nc"
return joinpath(
@clima_artifact("DYAMOND_SUMMER_ICS_p98deg", context),
filename,
)
end

end

0 comments on commit fe9bea7

Please sign in to comment.