From 6fea4410e365260999eeadb47a000e553bfc0c4a Mon Sep 17 00:00:00 2001 From: Luca Bertagna Date: Wed, 8 Nov 2023 15:29:34 -0700 Subject: [PATCH] EAMxx: fix setting of FillValue based on fp precision --- components/eamxx/src/share/io/scorpio_output.cpp | 12 +++++++++--- .../eamxx/src/share/io/scream_output_manager.cpp | 12 ++++++++---- components/eamxx/src/share/io/tests/io_basic.cpp | 5 +++++ 3 files changed, 22 insertions(+), 7 deletions(-) diff --git a/components/eamxx/src/share/io/scorpio_output.cpp b/components/eamxx/src/share/io/scorpio_output.cpp index 9dc10c9a65ef..1ee62ce2c84a 100644 --- a/components/eamxx/src/share/io/scorpio_output.cpp +++ b/components/eamxx/src/share/io/scorpio_output.cpp @@ -896,9 +896,14 @@ register_variables(const std::string& filename, const std::string& fp_precision, const scorpio::FileMode mode) { - using namespace scorpio; using namespace ShortFieldTagsNames; + using strvec_t = std::vector; + + EKAT_REQUIRE_MSG (ekat::contains(strvec_t{"float","single","double","real"},fp_precision), + "Error! Invalid/unsupported value for fp_precision.\n" + " - input value: " + fp_precision + "\n" + " - supported values: float, single, double, real\n"); // Helper lambdas auto set_decomp_tag = [&](const FieldLayout& layout) { @@ -963,8 +968,9 @@ register_variables(const std::string& filename, if (mode != FileMode::Append ) { // Add FillValue as an attribute of each variable // FillValue is a protected metadata, do not add it if it already existed - if (fp_precision == "real") { - Real fill_value = m_fill_value; + if (fp_precision=="double" or + (fp_precision=="real" and std::is_same::value)) { + double fill_value = m_fill_value; set_variable_metadata(filename, name, "_FillValue",fill_value); } else { float fill_value = m_fill_value; diff --git a/components/eamxx/src/share/io/scream_output_manager.cpp b/components/eamxx/src/share/io/scream_output_manager.cpp index 92903106a77c..379c1ef96a20 100644 --- a/components/eamxx/src/share/io/scream_output_manager.cpp +++ b/components/eamxx/src/share/io/scream_output_manager.cpp @@ -566,10 +566,11 @@ void OutputManager:: set_params (const ekat::ParameterList& params, const std::map>& field_mgrs) { + using vos_t = std::vector; + m_params = params; - if (m_is_model_restart_output) { - using vos_t = std::vector; + if (m_is_model_restart_output) { // We build some restart parameters internally auto avg_type = m_params.get("Averaging Type","INSTANT"); m_avg_type = str2avg(avg_type); @@ -614,8 +615,11 @@ set_params (const ekat::ParameterList& params, // Allow user to ask for higher precision for normal model output, // but default to single to save on storage const auto& prec = m_params.get("Floating Point Precision", "single"); - EKAT_REQUIRE_MSG (prec=="single" || prec=="double" || prec=="real", - "Error! Invalid floating point precision '" + prec + "'.\n"); + vos_t valid_prec = {"single", "float", "double", "real"}; + EKAT_REQUIRE_MSG (ekat::contains(valid_prec,prec), + "Error! Invalid/unsupported value for 'Floating Point Precision'.\n" + " - input value: " + prec + "\n" + " - supported values: float, single, double, real\n"); } } /*===============================================================================================*/ diff --git a/components/eamxx/src/share/io/tests/io_basic.cpp b/components/eamxx/src/share/io/tests/io_basic.cpp index 8bfed2136522..4b7ee47bc5f5 100644 --- a/components/eamxx/src/share/io/tests/io_basic.cpp +++ b/components/eamxx/src/share/io/tests/io_basic.cpp @@ -157,6 +157,11 @@ void write (const std::string& avg_type, const std::string& freq_units, // Create Output manager OutputManager om; + + // Attempt to use invalid fp precision string + om_pl.set("Floating Point Precision",std::string("triple")); + REQUIRE_THROWS (om.setup(comm,om_pl,fm,gm,t0,t0,false)); + om_pl.set("Floating Point Precision",std::string("single")); om.setup(comm,om_pl,fm,gm,t0,t0,false); // Time loop: ensure we always hit 3 output steps