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

EAMxx: extract diagnostic creation logic outside of output class #6796

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions components/eamxx/src/diagnostics/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
set(DIAGNOSTIC_SRCS
aerocom_cld.cpp
aodvis.cpp
atm_backtend.cpp
atm_density.cpp
dry_static_energy.cpp
exner.cpp
field_at_height.cpp
field_at_level.cpp
field_at_pressure_level.cpp
longwave_cloud_forcing.cpp
number_path.cpp
potential_temperature.cpp
precip_surf_mass_flux.cpp
relative_humidity.cpp
Expand All @@ -17,15 +21,11 @@ set(DIAGNOSTIC_SRCS
virtual_temperature.cpp
water_path.cpp
wind_speed.cpp
aodvis.cpp
number_path.cpp
aerocom_cld.cpp
atm_backtend.cpp
)

add_library(diagnostics ${DIAGNOSTIC_SRCS})
target_link_libraries(diagnostics PUBLIC scream_share)

if (NOT SCREAM_LIB_ONLY)
if (NOT SCREAM_LIB_ONLY AND NOT SCREAM_ONLY_GENERATE_BASELINES)
add_subdirectory(tests)
endif()
5 changes: 2 additions & 3 deletions components/eamxx/src/diagnostics/aerocom_cld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ AeroComCld::AeroComCld(const ekat::Comm &comm,
"to be 'Bot' or 'Top' in its input parameters.\n");
}

std::string AeroComCld::name() const { return "AeroComCld" + m_topbot; }

void AeroComCld::set_grids(
const std::shared_ptr<const GridsManager> grids_manager) {
using namespace ekat::units;
Expand Down Expand Up @@ -76,7 +74,8 @@ void AeroComCld::set_grids(
m_dz.allocate_view();

// Construct and allocate the output field
FieldIdentifier fid(name(), vector1d_layout, nondim, grid_name);

FieldIdentifier fid("AeroComCld"+m_topbot, vector1d_layout, nondim, grid_name);
m_diagnostic_output = Field(fid);
m_diagnostic_output.allocate_view();

Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/aerocom_cld.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AeroComCld : public AtmosphereDiagnostic {
AeroComCld(const ekat::Comm &comm, const ekat::ParameterList &params);

// The name of the diagnostic
std::string name() const override;
std::string name() const override { return "AeroComCld"; }

// Set the grid
void set_grids(
Expand Down
23 changes: 9 additions & 14 deletions components/eamxx/src/diagnostics/field_at_height.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,16 @@ FieldAtHeight (const ekat::Comm& comm, const ekat::ParameterList& params)
" - surface reference: " + surf_ref + "\n"
" - valid options: sealevel, surface\n");
m_z_name = (surf_ref == "sealevel") ? "z" : "height";
const auto& location = m_params.get<std::string>("vertical_location");
auto chars_start = location.find_first_not_of("0123456789.");
EKAT_REQUIRE_MSG (chars_start!=0 && chars_start!=std::string::npos,
"Error! Invalid string for height value for FieldAtHeight.\n"
" - input string : " + location + "\n"
" - expected format: Nm, with N integer\n");
const auto z_str = location.substr(0,chars_start);
m_z = std::stod(z_str);

const auto units = location.substr(chars_start);

const auto units = m_params.get<std::string>("height_units");
EKAT_REQUIRE_MSG (units=="m",
"Error! Invalid string for height value for FieldAtHeight.\n"
" - input string : " + location + "\n"
" - expected format: Nm, with N integer\n");
m_diag_name = m_field_name + "_at_" + m_params.get<std::string>("vertical_location") + "_above_" + surf_ref;
"Error! Invalid units for FieldAtHeight.\n"
" - input units: " + units + "\n"
" - valid units: m\n");

auto z_val = m_params.get<std::string>("height_value");
m_z = std::stod(z_val);
m_diag_name = m_field_name + "_at_" + z_val + units + "_above_" + surf_ref;
}

void FieldAtHeight::
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/field_at_height.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class FieldAtHeight : public AtmosphereDiagnostic
FieldAtHeight (const ekat::Comm& comm, const ekat::ParameterList& params);

// The name of the diagnostic
std::string name () const { return m_diag_name; }
std::string name () const { return "FieldAtHeight"; }

// Set the grid
void set_grids (const std::shared_ptr<const GridsManager> grids_manager);
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/field_at_level.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class FieldAtLevel : public AtmosphereDiagnostic
FieldAtLevel (const ekat::Comm& comm, const ekat::ParameterList& params);

// The name of the diagnostic
std::string name () const { return m_diag_name; }
std::string name () const { return "FieldAtLevel"; }

// Set the grid
void set_grids (const std::shared_ptr<const GridsManager> grids_manager);
Expand Down
32 changes: 13 additions & 19 deletions components/eamxx/src/diagnostics/field_at_pressure_level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,22 @@ FieldAtPressureLevel (const ekat::Comm& comm, const ekat::ParameterList& params)
{
m_field_name = m_params.get<std::string>("field_name");

// Figure out the pressure value
const auto& location = m_params.get<std::string>("vertical_location");
auto chars_start = location.find_first_not_of("0123456789.");
EKAT_REQUIRE_MSG (chars_start!=0 && chars_start!=std::string::npos,
"Error! Invalid string for pressure value for FieldAtPressureLevel.\n"
" - input string : " + location + "\n"
" - expected format: Nxyz, with N integer, and xyz='mb', 'hPa', or 'Pa'\n");
const auto press_str = location.substr(0,chars_start);
m_pressure_level = std::stod(press_str);

const auto units = location.substr(chars_start);
const auto units = m_params.get<std::string>("pressure_units");
EKAT_REQUIRE_MSG (units=="mb" or units=="hPa" or units=="Pa",
"Error! Invalid string for pressure value for FieldAtPressureLevel.\n"
" - input string : " + location + "\n"
" - expected format: Nxyz, with N integer, and xyz='mb', 'hPa', or 'Pa'\n");
"Error! Invalid units for FieldAtPressureLevel.\n"
" - input units: " + units + "\n"
" - valid units: 'mb', 'hPa', 'Pa'\n");

// Figure out the pressure value, and convert to Pa if needed
auto p_value = m_params.get<std::string>("pressure_value");

// Convert pressure level to Pa, the units of pressure in the simulation
if (units=="mb" || units=="hPa") {
m_pressure_level *= 100;
m_pressure_level = std::stod(p_value)*100;
} else {
m_pressure_level = std::stod(p_value);
}

m_mask_val = m_params.get<double>("mask_value",Real(constants::DefaultFillValue<float>::value));

m_diag_name = m_field_name + "_at_" + location;
m_diag_name = m_field_name + "_at_" + p_value + units;
}

void FieldAtPressureLevel::
Expand Down Expand Up @@ -91,6 +83,8 @@ initialize_impl (const RunType /*run_type*/)
// Add a field representing the mask as extra data to the diagnostic field.
auto nondim = ekat::units::Units::nondimensional();
const auto& gname = fid.get_grid_name();
m_mask_val = m_params.get<double>("mask_value",Real(constants::DefaultFillValue<float>::value));


std::string mask_name = name() + " mask";
FieldLayout mask_layout( {COL}, {num_cols});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class FieldAtPressureLevel : public AtmosphereDiagnostic
FieldAtPressureLevel (const ekat::Comm& comm, const ekat::ParameterList& params);

// The name of the diagnostic
std::string name () const { return m_diag_name; }
std::string name () const { return "FieldAtPressureLevel"; }

// Set the grid
void set_grids (const std::shared_ptr<const GridsManager> grids_manager);
Expand Down
4 changes: 1 addition & 3 deletions components/eamxx/src/diagnostics/number_path.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ NumberPathDiagnostic::NumberPathDiagnostic(const ekat::Comm &comm,
}
}

std::string NumberPathDiagnostic::name() const { return m_kind + "NumberPath"; }

void NumberPathDiagnostic::set_grids(
const std::shared_ptr<const GridsManager> grids_manager) {
using namespace ekat::units;
Expand All @@ -55,7 +53,7 @@ void NumberPathDiagnostic::set_grids(
add_field<Required>(m_nname, scalar3d, 1 / kg, grid_name);

// Construct and allocate the diagnostic field
FieldIdentifier fid(name(), scalar2d, kg/(kg*m2), grid_name);
FieldIdentifier fid(m_kind + "NumberPath", scalar2d, kg/(kg*m2), grid_name);
m_diagnostic_output = Field(fid);
m_diagnostic_output.allocate_view();
}
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/number_path.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class NumberPathDiagnostic : public AtmosphereDiagnostic {
const ekat::ParameterList &params);

// The name of the diagnostic
std::string name() const;
std::string name() const override { return "NumberPath"; }

// Set the grid
void set_grids(const std::shared_ptr<const GridsManager> grids_manager);
Expand Down
7 changes: 1 addition & 6 deletions components/eamxx/src/diagnostics/potential_temperature.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ PotentialTemperatureDiagnostic::PotentialTemperatureDiagnostic (const ekat::Comm
}
}

std::string PotentialTemperatureDiagnostic::name() const
{
return m_ptype;
}

// =========================================================================================
void PotentialTemperatureDiagnostic::set_grids(const std::shared_ptr<const GridsManager> grids_manager)
{
Expand All @@ -51,7 +46,7 @@ void PotentialTemperatureDiagnostic::set_grids(const std::shared_ptr<const Grids
add_field<Required>("qc", scalar3d_layout_mid, kg/kg, grid_name, ps);

// Construct and allocate the diagnostic field
FieldIdentifier fid (name(), scalar3d_layout_mid, K, grid_name);
FieldIdentifier fid (m_ptype, scalar3d_layout_mid, K, grid_name);
m_diagnostic_output = Field(fid);
auto& C_ap = m_diagnostic_output.get_header().get_alloc_properties();
C_ap.request_allocation(ps);
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/potential_temperature.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PotentialTemperatureDiagnostic : public AtmosphereDiagnostic
PotentialTemperatureDiagnostic (const ekat::Comm& comm, const ekat::ParameterList& params);

// The name of the diagnostic
std::string name () const;
std::string name () const override { return "PotentialTemperature"; }

// Set the grid
void set_grids (const std::shared_ptr<const GridsManager> grids_manager);
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/precip_surf_mass_flux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ set_grids(const std::shared_ptr<const GridsManager> grids_manager)
}

// Construct and allocate the diagnostic field
FieldIdentifier fid(name(), scalar2d_layout_mid, m/s, grid_name);
FieldIdentifier fid(m_name, scalar2d_layout_mid, m/s, grid_name);
m_diagnostic_output = Field(fid);
m_diagnostic_output.get_header().get_alloc_properties().request_allocation();
m_diagnostic_output.allocate_view();
Expand Down
2 changes: 1 addition & 1 deletion components/eamxx/src/diagnostics/precip_surf_mass_flux.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class PrecipSurfMassFlux : public AtmosphereDiagnostic
PrecipSurfMassFlux (const ekat::Comm& comm, const ekat::ParameterList& params);

// The name of the diagnostic
std::string name () const { return m_name; }
std::string name () const { return "PrecipSurfMassFlux"; }

// Set the grid
void set_grids (const std::shared_ptr<const GridsManager> grids_manager);
Expand Down
9 changes: 2 additions & 7 deletions components/eamxx/src/diagnostics/register_diagnostics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,20 +36,14 @@ inline void register_diagnostics () {
diag_factory.register_product("AtmosphereDensity",&create_atmosphere_diagnostic<AtmDensityDiagnostic>);
diag_factory.register_product("Exner",&create_atmosphere_diagnostic<ExnerDiagnostic>);
diag_factory.register_product("VirtualTemperature",&create_atmosphere_diagnostic<VirtualTemperatureDiagnostic>);
diag_factory.register_product("z_int",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("z_mid",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("geopotential_int",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("geopotential_mid",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("height_int",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("height_mid",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("dz",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("DryStaticEnergy",&create_atmosphere_diagnostic<DryStaticEnergyDiagnostic>);
diag_factory.register_product("SeaLevelPressure",&create_atmosphere_diagnostic<SeaLevelPressureDiagnostic>);
diag_factory.register_product("WaterPath",&create_atmosphere_diagnostic<WaterPathDiagnostic>);
diag_factory.register_product("ShortwaveCloudForcing",&create_atmosphere_diagnostic<ShortwaveCloudForcingDiagnostic>);
diag_factory.register_product("LongwaveCloudForcing",&create_atmosphere_diagnostic<LongwaveCloudForcingDiagnostic>);
diag_factory.register_product("RelativeHumidity",&create_atmosphere_diagnostic<RelativeHumidityDiagnostic>);
diag_factory.register_product("VaporFlux",&create_atmosphere_diagnostic<VaporFluxDiagnostic>);
diag_factory.register_product("VerticalLayer",&create_atmosphere_diagnostic<VerticalLayerDiagnostic>);
diag_factory.register_product("precip_surf_mass_flux",&create_atmosphere_diagnostic<PrecipSurfMassFlux>);
diag_factory.register_product("surface_upward_latent_heat_flux",&create_atmosphere_diagnostic<SurfaceUpwardLatentHeatFlux>);
diag_factory.register_product("wind_speed",&create_atmosphere_diagnostic<WindSpeed>);
Expand All @@ -60,4 +54,5 @@ inline void register_diagnostics () {
}

} // namespace scream

#endif // SCREAM_REGISTER_DIAGNOSTICS_HPP
94 changes: 45 additions & 49 deletions components/eamxx/src/diagnostics/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,77 +1,73 @@
# NOTE: tests inside this if statement won't be built in a baselines-only build
include(ScreamUtils)

function (createDiagTest test_name test_srcs)
CreateUnitTest(${test_name} "${test_srcs}"
LIBS diagnostics physics_share
LABELS diagnostics)
endfunction ()

if (NOT SCREAM_ONLY_GENERATE_BASELINES)
include(ScreamUtils)
# Test extracting a single level of a field
CreateDiagTest(field_at_level "field_at_level_tests.cpp")

# Test extracting a single level of a field
CreateDiagTest(field_at_level "field_at_level_tests.cpp")
# Test interpolating a field onto a single pressure level
CreateDiagTest(field_at_pressure_level "field_at_pressure_level_tests.cpp")

# Test interpolating a field onto a single pressure level
CreateDiagTest(field_at_pressure_level "field_at_pressure_level_tests.cpp")
# Test interpolating a field at a specific height
CreateDiagTest(field_at_height "field_at_height_tests.cpp")
# Test interpolating a field at a specific height
CreateDiagTest(field_at_height "field_at_height_tests.cpp")

# Test potential temperature diagnostic
CreateDiagTest(potential_temperature "potential_temperature_test.cpp")
# Test potential temperature diagnostic
CreateDiagTest(potential_temperature "potential_temperature_test.cpp")

# Test exner diagnostic
CreateDiagTest(exner_function "exner_test.cpp")
# Test exner diagnostic
CreateDiagTest(exner_function "exner_test.cpp")

# Test virtual temperature
CreateDiagTest(virtual_temperature "virtual_temperature_test.cpp")
# Test virtual temperature
CreateDiagTest(virtual_temperature "virtual_temperature_test.cpp")

# Test atmosphere density
CreateDiagTest(atmosphere_density "atm_density_test.cpp")
# Test atmosphere density
CreateDiagTest(atmosphere_density "atm_density_test.cpp")

# Test vertical layer (dz, z_int, z_mid)
CreateDiagTest(vertical_layer "vertical_layer_tests.cpp")
# Test vertical layer (dz, z_int, z_mid)
CreateDiagTest(vertical_layer "vertical_layer_tests.cpp")

# Test dry static energy
CreateDiagTest(dry_static_energy "dry_static_energy_test.cpp")
# Test dry static energy
CreateDiagTest(dry_static_energy "dry_static_energy_test.cpp")

# Test sea level pressure
CreateDiagTest(sea_level_pressure "sea_level_pressure_test.cpp")
# Test sea level pressure
CreateDiagTest(sea_level_pressure "sea_level_pressure_test.cpp")

# Test total water path
CreateDiagTest(water_path "water_path_tests.cpp")
# Test total water path
CreateDiagTest(water_path "water_path_tests.cpp")

# Test shortwave cloud forcing
CreateDiagTest(shortwave_cloud_forcing "shortwave_cloud_forcing_tests.cpp")
# Test shortwave cloud forcing
CreateDiagTest(shortwave_cloud_forcing "shortwave_cloud_forcing_tests.cpp")

# Test longwave cloud forcing
CreateDiagTest(longwave_cloud_forcing "longwave_cloud_forcing_tests.cpp")
# Test longwave cloud forcing
CreateDiagTest(longwave_cloud_forcing "longwave_cloud_forcing_tests.cpp")

# Test Relative Humidity
CreateDiagTest(relative_humidity "relative_humidity_tests.cpp")
# Test Relative Humidity
CreateDiagTest(relative_humidity "relative_humidity_tests.cpp")

# Test Vapor Flux
CreateDiagTest(vapor_flux "vapor_flux_tests.cpp")
# Test Vapor Flux
CreateDiagTest(vapor_flux "vapor_flux_tests.cpp")

# Test precipitation mass surface flux
CreateDiagTest(precip_surf_mass_flux "precip_surf_mass_flux_tests.cpp")
# Test precipitation mass surface flux
CreateDiagTest(precip_surf_mass_flux "precip_surf_mass_flux_tests.cpp")

# Test surface latent heat flux
CreateDiagTest(surface_upward_latent_heat_flux "surf_upward_latent_heat_flux_tests.cpp")
# Test surface latent heat flux
CreateDiagTest(surface_upward_latent_heat_flux "surf_upward_latent_heat_flux_tests.cpp")

# Test wind speed diagnostic
CreateDiagTest(wind_speed "wind_speed_tests.cpp")
# Test wind speed diagnostic
CreateDiagTest(wind_speed "wind_speed_tests.cpp")

# Test AODVIS
CreateDiagTest(aodvis "aodvis_test.cpp")
# Test AODVIS
CreateDiagTest(aodvis "aodvis_test.cpp")

# Test "number" paths
CreateDiagTest(number_paths "number_paths_tests.cpp")
# Test "number" paths
CreateDiagTest(number_paths "number_paths_tests.cpp")

# Test AEROCOM_CLD
CreateDiagTest(aerocom_cld "aerocom_cld_test.cpp")
# Test AEROCOM_CLD
CreateDiagTest(aerocom_cld "aerocom_cld_test.cpp")

# Test atm_tend
CreateDiagTest(atm_backtend "atm_backtend_test.cpp")

endif()
# Test atm_tend
CreateDiagTest(atm_backtend "atm_backtend_test.cpp")
Loading
Loading