From 1bda968732eb657c58339b0a9baf87c95b55be43 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Tue, 20 Aug 2024 15:13:10 -0400 Subject: [PATCH 01/14] Added conditional to MetricTerms init to allow for metric term generation for surface level (no eta_file specified) --- ndsl/grid/generation.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index 275db563..c58a6838 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -297,12 +297,13 @@ def __init__( self._dy_center = None self._area = None self._area_c = None - ( - self._ks, - self._ptop, - self._ak, - self._bk, - ) = self._set_hybrid_pressure_coefficients(eta_file, ak, bk) + if eta_file != "None": + ( + self._ks, + self._ptop, + self._ak, + self._bk, + ) = self._set_hybrid_pressure_coefficients(eta_file, ak, bk) self._ec1 = None self._ec2 = None self._ew1 = None From b18880b666ed00e9231749a9fdaacee4488a4291 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Thu, 22 Aug 2024 10:29:49 -0400 Subject: [PATCH 02/14] Changes to support testing in pace for eta file specification --- ndsl/grid/eta.py | 4 +--- ndsl/grid/generation.py | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/ndsl/grid/eta.py b/ndsl/grid/eta.py index 90db8c4a..1015312d 100644 --- a/ndsl/grid/eta.py +++ b/ndsl/grid/eta.py @@ -30,10 +30,8 @@ class HybridPressureCoefficients: def _load_ak_bk_from_file(eta_file: str) -> Tuple[np.ndarray, np.ndarray]: - if eta_file == "None": - raise ValueError("eta file not specified") if not os.path.isfile(eta_file): - raise ValueError("file " + eta_file + " does not exist") + raise ValueError("eta file does not exist") # read file into ak, bk arrays data = xr.open_dataset(eta_file) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index c58a6838..a42edfdf 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -304,6 +304,27 @@ def __init__( self._ak, self._bk, ) = self._set_hybrid_pressure_coefficients(eta_file, ak, bk) + else: + ks = self.quantity_factory.zeros( + [], + "", + dtype=Float, + ) + ptop = self.quantity_factory.zeros( + [], + "Pa", + dtype=Float, + ) + ak = self.quantity_factory.zeros( + [Z_INTERFACE_DIM], + "Pa", + dtype=Float, + ) + bk = self.quantity_factory.zeros( + [Z_INTERFACE_DIM], + "", + dtype=Float, + ) self._ec1 = None self._ec2 = None self._ew1 = None From ba28f33c97559fed6056a2ac03bacd253e0cfc82 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Tue, 27 Aug 2024 14:24:54 -0400 Subject: [PATCH 03/14] Moved test_eta.py from pace/tests/main/grid into NDSL/tests/grid, added eta_file generator script for testing --- ndsl/grid/eta.py | 2 +- tests/grid/generate_eta_files.py | 399 +++++++++++++++++++++++++++++++ tests/grid/test_eta.py | 216 +++++++++++++++++ 3 files changed, 616 insertions(+), 1 deletion(-) create mode 100755 tests/grid/generate_eta_files.py create mode 100755 tests/grid/test_eta.py diff --git a/ndsl/grid/eta.py b/ndsl/grid/eta.py index 1015312d..b14da5a2 100644 --- a/ndsl/grid/eta.py +++ b/ndsl/grid/eta.py @@ -31,7 +31,7 @@ class HybridPressureCoefficients: def _load_ak_bk_from_file(eta_file: str) -> Tuple[np.ndarray, np.ndarray]: if not os.path.isfile(eta_file): - raise ValueError("eta file does not exist") + raise ValueError("eta file " + eta_file + " does not exist") # read file into ak, bk arrays data = xr.open_dataset(eta_file) diff --git a/tests/grid/generate_eta_files.py b/tests/grid/generate_eta_files.py new file mode 100755 index 00000000..1fb4d5ee --- /dev/null +++ b/tests/grid/generate_eta_files.py @@ -0,0 +1,399 @@ +import numpy as np +import xarray as xr + + +""" +This notebook uses the python xarray module +to create an eta_file containing ak and bk coefficients +for km=79 and km=91. The coefficients are written out to +eta79.nc and eta91.nc netcdf files respectively + +To run this script: `python3 ./generate_eta_files.py` +""" + +# km = 79 +ak = xr.DataArray( + dims=["km1"], + attrs=dict(units="Pa", _FillValue=False), + data=np.array( + [ + 3.000000e02, + 6.467159e02, + 1.045222e03, + 1.469188e03, + 1.897829e03, + 2.325385e03, + 2.754396e03, + 3.191294e03, + 3.648332e03, + 4.135675e03, + 4.668282e03, + 5.247940e03, + 5.876271e03, + 6.554716e03, + 7.284521e03, + 8.066738e03, + 8.902188e03, + 9.791482e03, + 1.073499e04, + 1.162625e04, + 1.237212e04, + 1.299041e04, + 1.349629e04, + 1.390277e04, + 1.422098e04, + 1.446058e04, + 1.462993e04, + 1.473633e04, + 1.478617e04, + 1.478511e04, + 1.473812e04, + 1.464966e04, + 1.452370e04, + 1.436382e04, + 1.417324e04, + 1.395491e04, + 1.371148e04, + 1.344540e04, + 1.315890e04, + 1.285407e04, + 1.253280e04, + 1.219685e04, + 1.184788e04, + 1.148739e04, + 1.111682e04, + 1.073748e04, + 1.035062e04, + 9.957395e03, + 9.558875e03, + 9.156069e03, + 8.749922e03, + 8.341315e03, + 7.931065e03, + 7.519942e03, + 7.108648e03, + 6.698281e03, + 6.290007e03, + 5.884984e03, + 5.484372e03, + 5.089319e03, + 4.700960e03, + 4.320421e03, + 3.948807e03, + 3.587201e03, + 3.236666e03, + 2.898237e03, + 2.572912e03, + 2.261667e03, + 1.965424e03, + 1.685079e03, + 1.421479e03, + 1.175419e03, + 9.476516e02, + 7.388688e02, + 5.497130e02, + 3.807626e02, + 2.325417e02, + 1.054810e02, + -8.381903e-04, + 0.000000e00, + ] + ), +) +bk = xr.DataArray( + dims=["km1"], + attrs=dict(units="None", _FillValue=False), + data=np.array( + [ + 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.00106595, + 0.00412866, + 0.00900663, + 0.01554263, + 0.02359921, + 0.03305481, + 0.0438012, + 0.05574095, + 0.06878554, + 0.08285347, + 0.09786981, + 0.1137643, + 0.130471, + 0.1479275, + 0.1660746, + 0.1848558, + 0.2042166, + 0.2241053, + 0.2444716, + 0.2652672, + 0.286445, + 0.3079604, + 0.3297701, + 0.351832, + 0.3741062, + 0.3965532, + 0.4191364, + 0.4418194, + 0.4645682, + 0.48735, + 0.5101338, + 0.5328897, + 0.5555894, + 0.5782067, + 0.6007158, + 0.6230936, + 0.6452944, + 0.6672683, + 0.6889648, + 0.7103333, + 0.7313231, + 0.7518838, + 0.7719651, + 0.7915173, + 0.8104913, + 0.828839, + 0.846513, + 0.8634676, + 0.8796583, + 0.8950421, + 0.9095779, + 0.9232264, + 0.9359506, + 0.9477157, + 0.9584892, + 0.9682413, + 0.9769447, + 0.9845753, + 0.9911126, + 0.9965372, + 1.0, + ] + ), +) +coefficients = xr.Dataset(data_vars={"ak": ak, "bk": bk}) +coefficients.to_netcdf("eta79.nc") + + +# km = 91 +ak = xr.DataArray( + dims=["km1"], + attrs=dict(units="Pa", _FillValue=False), + data=np.array( + [ + 1.00000000e00, + 1.75000000e00, + 2.75000000e00, + 4.09999990e00, + 5.98951054e00, + 8.62932968e00, + 1.22572632e01, + 1.71510906e01, + 2.36545467e01, + 3.21627693e01, + 4.31310921e01, + 5.71100426e01, + 7.46595764e01, + 9.64470978e01, + 1.23169769e02, + 1.55601318e02, + 1.94594009e02, + 2.41047531e02, + 2.95873840e02, + 3.60046967e02, + 4.34604828e02, + 5.20628723e02, + 6.19154846e02, + 7.31296021e02, + 8.58240906e02, + 1.00106561e03, + 1.16092859e03, + 1.33903992e03, + 1.53650012e03, + 1.75448938e03, + 1.99417834e03, + 2.25667407e03, + 2.54317139e03, + 2.85476392e03, + 3.19258569e03, + 3.55775366e03, + 3.95135107e03, + 4.37428662e03, + 4.82711084e03, + 5.31022168e03, + 5.82387793e03, + 6.36904248e03, + 6.94875244e03, + 7.56691992e03, + 8.22634277e03, + 8.93120996e03, + 9.68446191e03, + 1.04822725e04, + 1.13182793e04, + 1.21840771e04, + 1.30655674e04, + 1.39532207e04, + 1.48307285e04, + 1.56872617e04, + 1.65080645e04, + 1.72810996e04, + 1.79942988e04, + 1.86363223e04, + 1.91961797e04, + 1.96640723e04, + 2.00301914e04, + 2.02853691e04, + 2.04215254e04, + 2.04300684e04, + 2.03028730e04, + 2.00323711e04, + 1.96110664e04, + 1.90313848e04, + 1.82866426e04, + 1.73777930e04, + 1.63224639e04, + 1.51444033e04, + 1.38725674e04, + 1.25404785e04, + 1.11834170e04, + 9.83532715e03, + 8.52630664e03, + 7.28224512e03, + 6.12326074e03, + 5.06350684e03, + 4.11124902e03, + 3.27000122e03, + 2.53922729e03, + 1.91530762e03, + 1.39244995e03, + 9.63134766e02, + 6.20599365e02, + 3.57989502e02, + 1.69421387e02, + 5.10314941e01, + 2.48413086e00, + 0.00000000e00, + ] + ), +) +bk = xr.DataArray( + dims=["km1"], + attrs=dict(units="None", _FillValue=False), + data=np.array( + [ + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 0.00000000e00, + 3.50123992e-06, + 2.81484008e-05, + 9.38666999e-05, + 2.28561999e-04, + 5.12343016e-04, + 1.04712998e-03, + 1.95625005e-03, + 3.42317997e-03, + 5.58632007e-03, + 8.65428988e-03, + 1.27844000e-02, + 1.81719996e-02, + 2.49934997e-02, + 3.34198996e-02, + 4.36249003e-02, + 5.57769015e-02, + 7.00351968e-02, + 8.65636021e-02, + 1.05520003e-01, + 1.27051994e-01, + 1.51319996e-01, + 1.78477004e-01, + 2.08675995e-01, + 2.42069006e-01, + 2.78813988e-01, + 3.19043010e-01, + 3.62558991e-01, + 4.08596009e-01, + 4.56384987e-01, + 5.05111992e-01, + 5.53902984e-01, + 6.01903021e-01, + 6.48333013e-01, + 6.92534983e-01, + 7.33981013e-01, + 7.72292018e-01, + 8.07236016e-01, + 8.38724971e-01, + 8.66774976e-01, + 8.91497016e-01, + 9.13065016e-01, + 9.31702971e-01, + 9.47658002e-01, + 9.61175978e-01, + 9.72495019e-01, + 9.81844008e-01, + 9.89410996e-01, + 9.95342016e-01, + 1.00000000e00, + ] + ), +) +coefficients = xr.Dataset(data_vars={"ak": ak, "bk": bk}) +coefficients.to_netcdf("eta91.nc") + +# km = diff --git a/tests/grid/test_eta.py b/tests/grid/test_eta.py new file mode 100755 index 00000000..44c92dee --- /dev/null +++ b/tests/grid/test_eta.py @@ -0,0 +1,216 @@ +#!/usr/bin/env python3 + +import os + +import numpy as np +import pytest +import xarray as xr + +from ndsl import ( + NullComm, + CubedSphereCommunicator, + CubedSpherePartitioner, + QuantityFactory, + TilePartitioner, + SubtileGridSizer, +) +from ndsl.grid import MetricTerms + + +""" +This test checks to ensure that ak and bk +values are read-in and stored properly. +In addition, this test checks to ensure that +the function set_hybrid_pressure_coefficients +fail as expected if the computed eta values +vary non-mononitically and if the eta_file +is not provided. +""" + + +def set_answers(eta_file): + + """ + Read in the expected values of ak and bk + arrays from the input eta NetCDF files. + """ + + data = xr.open_dataset(eta_file) + return data["ak"].values, data["bk"].values + + +def write_non_mono_eta_file(in_eta_file, out_eta_file): + """ + Reads in file eta79.nc and alters randomly chosen ak/bk values + This tests the expected failure of set_eta_hybrid_coefficients + for coefficients that lead to non-monotonically increasing + eta values + """ + + data = xr.open_dataset(in_eta_file) + data["ak"].values[10] = data["ak"].values[0] + data["bk"].values[20] = 0.0 + + data.to_netcdf(out_eta_file) + + +@pytest.mark.parametrize("km", [79, 91]) +def test_set_hybrid_pressure_coefficients_correct(km): + + """This test checks to see that the ak and bk arrays + are read-in correctly and are stored as + expected. Both values of km=79 and km=91 are + tested and both tests are expected to pass + with the stored ak and bk values agreeing with the + values read-in directly from the NetCDF file. + """ + + dirname = os.path.dirname(os.path.abspath(__file__)) + eta_file = os.path.join(dirname, f"eta{km}.nc") + + backend = "numpy" + + layout = (1,1) + + nz = km + ny = 48 + nx = 48 + nhalo = 3 + + partitioner = CubedSpherePartitioner(TilePartitioner(layout)) + + communicator = CubedSphereCommunicator(NullComm(rank=0,total_ranks=6), partitioner) + + sizer = SubtileGridSizer.from_tile_params( + nx_tile=nx, + ny_tile=ny, + nz=nz, + n_halo=nhalo, + extra_dim_lengths={}, + layout=layout, + tile_partitioner=partitioner.tile, + tile_rank=communicator.tile.rank, + ) + + quantity_factory = QuantityFactory.from_backend(sizer=sizer, backend=backend) + + metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) + + ak_results = metric_terms.ak.data + bk_results = metric_terms.bk.data + ak_answers, bk_answers = set_answers(f"eta{km}.nc") + + if ak_answers.size != ak_results.size: + raise ValueError("Unexpected size of bk") + if bk_answers.size != bk_results.size: + raise ValueError("Unexpected size of ak") + + if not np.array_equal(ak_answers, ak_results): + raise ValueError("Unexpected value of ak") + if not np.array_equal(bk_answers, bk_results): + raise ValueError("Unexpected value of bk") + + + +def test_set_hybrid_pressure_coefficients_nofile(): + + """This test checks to see that the program + fails when (1) the eta_file is not specified in the yaml + configuration file; and (2), the computed eta values + increase non-monotonically. For the latter test, the eta_file + is specified in test_config_not_mono.yaml file and + the ak and bk values in the eta_file have been changed nonsensically + to result in erronenous eta values. + """ + + eta_file = "NULL" + + backend = "numpy" + + layout = (1,1) + + nz = 79 + ny = 48 + nx = 48 + nhalo = 3 + + partitioner = CubedSpherePartitioner(TilePartitioner(layout)) + + communicator = CubedSphereCommunicator(NullComm(rank=0,total_ranks=6), partitioner) + + sizer = SubtileGridSizer.from_tile_params( + nx_tile=nx, + ny_tile=ny, + nz=nz, + n_halo=nhalo, + extra_dim_lengths={}, + layout=layout, + tile_partitioner=partitioner.tile, + tile_rank=communicator.tile.rank, + ) + + quantity_factory = QuantityFactory.from_backend(sizer=sizer, backend=backend) + + try: + metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) + except Exception as error: + if str(error) == "eta file NULL does not exist": + pytest.xfail("testing eta file not specified") + else: + pytest.fail(f"ERROR {error}") + + +def test_set_hybrid_pressure_coefficients_not_mono(): + + """This test checks to see that the program + fails when (1) the eta_file is not specified in the yaml + configuration file; and (2), the computed eta values + increase non-monotonically. For the latter test, the eta_file + is specified in test_config_not_mono.yaml file and + the ak and bk values in the eta_file have been changed nonsensically + to result in erronenous eta values. + """ + + dirname = os.path.dirname(os.path.abspath(__file__)) + in_eta_file = os.path.join(dirname, "eta79.nc") + out_eta_file = "eta_not_mono_79.nc" + write_non_mono_eta_file(in_eta_file, out_eta_file) + eta_file = out_eta_file + + backend = "numpy" + + layout = (1,1) + + nz = 79 + ny = 48 + nx = 48 + nhalo = 3 + + partitioner = CubedSpherePartitioner(TilePartitioner(layout)) + + communicator = CubedSphereCommunicator(NullComm(rank=0,total_ranks=6), partitioner) + + sizer = SubtileGridSizer.from_tile_params( + nx_tile=nx, + ny_tile=ny, + nz=nz, + n_halo=nhalo, + extra_dim_lengths={}, + layout=layout, + tile_partitioner=partitioner.tile, + tile_rank=communicator.tile.rank, + ) + + quantity_factory = QuantityFactory.from_backend(sizer=sizer, backend=backend) + + try: + metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) + except Exception as error: + if os.path.isfile(out_eta_file): + os.remove(out_eta_file) + if str(error) == "ETA values are not monotonically increasing": + pytest.xfail("testing eta values are not monotomincally increasing") + else: + pytest.fail( + "ERROR in testing etav values not are not monotonically increasing" + ) From 9cafe2e93cdcd5ee0e58557b4d6ee736e2cf3502 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Tue, 27 Aug 2024 14:33:36 -0400 Subject: [PATCH 04/14] Amending unit_tests.yaml to reflect addition of tests/grid/test_eta.py --- .github/workflows/unit_tests.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml index d3b127dc..e780c190 100644 --- a/.github/workflows/unit_tests.yaml +++ b/.github/workflows/unit_tests.yaml @@ -22,6 +22,10 @@ jobs: - name: Install Python packages run: pip3 install .[test] + - name: prepare input eta files + run: | + python tests/grid/generate_eta_files.py + - name: Run serial-cpu tests run: coverage run --rcfile=setup.cfg -m pytest -x tests From 9cb8f1407d5ebfe1c39d2913c12599ceec184eae Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Tue, 27 Aug 2024 14:53:59 -0400 Subject: [PATCH 05/14] Amending file path for test_eta.py --- .github/workflows/unit_tests.yaml | 1 + tests/grid/test_eta.py | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml index e780c190..c4cbb425 100644 --- a/.github/workflows/unit_tests.yaml +++ b/.github/workflows/unit_tests.yaml @@ -24,6 +24,7 @@ jobs: - name: prepare input eta files run: | + cd ${GITHUB_WORKSPACE}/ndsl python tests/grid/generate_eta_files.py - name: Run serial-cpu tests diff --git a/tests/grid/test_eta.py b/tests/grid/test_eta.py index 44c92dee..2835ab99 100755 --- a/tests/grid/test_eta.py +++ b/tests/grid/test_eta.py @@ -65,8 +65,8 @@ def test_set_hybrid_pressure_coefficients_correct(km): values read-in directly from the NetCDF file. """ - dirname = os.path.dirname(os.path.abspath(__file__)) - eta_file = os.path.join(dirname, f"eta{km}.nc") + working_dir = str(os.getcwd()) + eta_file = f"{working_dir}/eta{km}.nc" backend = "numpy" @@ -155,7 +155,7 @@ def test_set_hybrid_pressure_coefficients_nofile(): metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) except Exception as error: if str(error) == "eta file NULL does not exist": - pytest.xfail("testing eta file not specified") + pytest.xfail("testing eta file not correctly specified") else: pytest.fail(f"ERROR {error}") @@ -171,8 +171,8 @@ def test_set_hybrid_pressure_coefficients_not_mono(): to result in erronenous eta values. """ - dirname = os.path.dirname(os.path.abspath(__file__)) - in_eta_file = os.path.join(dirname, "eta79.nc") + working_dir = str(os.getcwd()) + in_eta_file = f"{working_dir}/eta79.nc" out_eta_file = "eta_not_mono_79.nc" write_non_mono_eta_file(in_eta_file, out_eta_file) eta_file = out_eta_file From a0c4992aab98dea6719d7deb060767bc4ef978df Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Tue, 27 Aug 2024 14:57:53 -0400 Subject: [PATCH 06/14] Another attempt to get the workflow to generate the test eta_files --- .github/workflows/unit_tests.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/unit_tests.yaml b/.github/workflows/unit_tests.yaml index c4cbb425..e780c190 100644 --- a/.github/workflows/unit_tests.yaml +++ b/.github/workflows/unit_tests.yaml @@ -24,7 +24,6 @@ jobs: - name: prepare input eta files run: | - cd ${GITHUB_WORKSPACE}/ndsl python tests/grid/generate_eta_files.py - name: Run serial-cpu tests From 10942b2381403a6c54def53e76e7bac1c06e8eef Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Tue, 27 Aug 2024 14:59:14 -0400 Subject: [PATCH 07/14] Linting --- tests/grid/test_eta.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/tests/grid/test_eta.py b/tests/grid/test_eta.py index 2835ab99..1e0cd099 100755 --- a/tests/grid/test_eta.py +++ b/tests/grid/test_eta.py @@ -7,12 +7,12 @@ import xarray as xr from ndsl import ( - NullComm, CubedSphereCommunicator, CubedSpherePartitioner, + NullComm, QuantityFactory, - TilePartitioner, SubtileGridSizer, + TilePartitioner, ) from ndsl.grid import MetricTerms @@ -70,7 +70,7 @@ def test_set_hybrid_pressure_coefficients_correct(km): backend = "numpy" - layout = (1,1) + layout = (1, 1) nz = km ny = 48 @@ -79,7 +79,7 @@ def test_set_hybrid_pressure_coefficients_correct(km): partitioner = CubedSpherePartitioner(TilePartitioner(layout)) - communicator = CubedSphereCommunicator(NullComm(rank=0,total_ranks=6), partitioner) + communicator = CubedSphereCommunicator(NullComm(rank=0, total_ranks=6), partitioner) sizer = SubtileGridSizer.from_tile_params( nx_tile=nx, @@ -94,7 +94,9 @@ def test_set_hybrid_pressure_coefficients_correct(km): quantity_factory = QuantityFactory.from_backend(sizer=sizer, backend=backend) - metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) + metric_terms = MetricTerms( + quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file + ) ak_results = metric_terms.ak.data bk_results = metric_terms.bk.data @@ -111,7 +113,6 @@ def test_set_hybrid_pressure_coefficients_correct(km): raise ValueError("Unexpected value of bk") - def test_set_hybrid_pressure_coefficients_nofile(): """This test checks to see that the program @@ -127,7 +128,7 @@ def test_set_hybrid_pressure_coefficients_nofile(): backend = "numpy" - layout = (1,1) + layout = (1, 1) nz = 79 ny = 48 @@ -136,7 +137,7 @@ def test_set_hybrid_pressure_coefficients_nofile(): partitioner = CubedSpherePartitioner(TilePartitioner(layout)) - communicator = CubedSphereCommunicator(NullComm(rank=0,total_ranks=6), partitioner) + communicator = CubedSphereCommunicator(NullComm(rank=0, total_ranks=6), partitioner) sizer = SubtileGridSizer.from_tile_params( nx_tile=nx, @@ -152,7 +153,11 @@ def test_set_hybrid_pressure_coefficients_nofile(): quantity_factory = QuantityFactory.from_backend(sizer=sizer, backend=backend) try: - metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) + metric_terms = MetricTerms( + quantity_factory=quantity_factory, + communicator=communicator, + eta_file=eta_file, + ) except Exception as error: if str(error) == "eta file NULL does not exist": pytest.xfail("testing eta file not correctly specified") @@ -179,7 +184,7 @@ def test_set_hybrid_pressure_coefficients_not_mono(): backend = "numpy" - layout = (1,1) + layout = (1, 1) nz = 79 ny = 48 @@ -188,7 +193,7 @@ def test_set_hybrid_pressure_coefficients_not_mono(): partitioner = CubedSpherePartitioner(TilePartitioner(layout)) - communicator = CubedSphereCommunicator(NullComm(rank=0,total_ranks=6), partitioner) + communicator = CubedSphereCommunicator(NullComm(rank=0, total_ranks=6), partitioner) sizer = SubtileGridSizer.from_tile_params( nx_tile=nx, @@ -204,7 +209,11 @@ def test_set_hybrid_pressure_coefficients_not_mono(): quantity_factory = QuantityFactory.from_backend(sizer=sizer, backend=backend) try: - metric_terms = MetricTerms(quantity_factory=quantity_factory, communicator=communicator, eta_file=eta_file) + metric_terms = MetricTerms( + quantity_factory=quantity_factory, + communicator=communicator, + eta_file=eta_file, + ) except Exception as error: if os.path.isfile(out_eta_file): os.remove(out_eta_file) From 712a385c08e35c3034cc48495c10504a25431c27 Mon Sep 17 00:00:00 2001 From: Frank Malatino <142349306+fmalatino@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:49:02 -0400 Subject: [PATCH 08/14] Use of f-string in ValueError statement of _load_ak_bk_from_file Co-authored-by: Oliver Elbert --- ndsl/grid/eta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndsl/grid/eta.py b/ndsl/grid/eta.py index b14da5a2..35ac510d 100644 --- a/ndsl/grid/eta.py +++ b/ndsl/grid/eta.py @@ -31,7 +31,7 @@ class HybridPressureCoefficients: def _load_ak_bk_from_file(eta_file: str) -> Tuple[np.ndarray, np.ndarray]: if not os.path.isfile(eta_file): - raise ValueError("eta file " + eta_file + " does not exist") + raise ValueError(f"eta file {eta_file} does not exist") # read file into ak, bk arrays data = xr.open_dataset(eta_file) From 37872f07f9a42e60de6226f4cfea9e4534c7df85 Mon Sep 17 00:00:00 2001 From: Frank Malatino <142349306+fmalatino@users.noreply.github.com> Date: Thu, 29 Aug 2024 15:49:51 -0400 Subject: [PATCH 09/14] Amending pytest.fail statement in test_eta.py Co-authored-by: Oliver Elbert --- tests/grid/test_eta.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/grid/test_eta.py b/tests/grid/test_eta.py index 1e0cd099..a4808fdf 100755 --- a/tests/grid/test_eta.py +++ b/tests/grid/test_eta.py @@ -221,5 +221,5 @@ def test_set_hybrid_pressure_coefficients_not_mono(): pytest.xfail("testing eta values are not monotomincally increasing") else: pytest.fail( - "ERROR in testing etav values not are not monotonically increasing" + "ERROR in testing eta values not are not monotonically increasing" ) From c76a4869c05d829d70a1ca2b81ed09c94d7b7487 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Fri, 30 Aug 2024 10:35:28 -0400 Subject: [PATCH 10/14] Amended generation.py to define hybrid pressure variables appropriately --- ndsl/grid/generation.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index a42edfdf..1ba36e9c 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -305,22 +305,22 @@ def __init__( self._bk, ) = self._set_hybrid_pressure_coefficients(eta_file, ak, bk) else: - ks = self.quantity_factory.zeros( + self._ks = self.quantity_factory.zeros( [], "", dtype=Float, ) - ptop = self.quantity_factory.zeros( + self._ptop = self.quantity_factory.zeros( [], "Pa", dtype=Float, ) - ak = self.quantity_factory.zeros( + self._ak = self.quantity_factory.zeros( [Z_INTERFACE_DIM], "Pa", dtype=Float, ) - bk = self.quantity_factory.zeros( + self._bk = self.quantity_factory.zeros( [Z_INTERFACE_DIM], "", dtype=Float, From 1082b622281f24d34e12a8ca875ad28b884b0dac Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Wed, 4 Sep 2024 21:23:42 -0400 Subject: [PATCH 11/14] Amended test_eta.py docstrings to reflect updated testing methods and eta_file variable in generation.py to use an empty string in conditional statement for hybrid pressure generation --- ndsl/grid/generation.py | 4 ++-- tests/grid/test_eta.py | 26 ++++++++++++-------------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index 1ba36e9c..5c39c346 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -237,7 +237,7 @@ def __init__( dy_const: float = 1000.0, deglat: float = 15.0, extdgrid: bool = False, - eta_file: str = "None", + eta_file: str = "", ak: Optional[np.ndarray] = None, bk: Optional[np.ndarray] = None, ): @@ -297,7 +297,7 @@ def __init__( self._dy_center = None self._area = None self._area_c = None - if eta_file != "None": + if eta_file is not "": ( self._ks, self._ptop, diff --git a/tests/grid/test_eta.py b/tests/grid/test_eta.py index a4808fdf..713e30b6 100755 --- a/tests/grid/test_eta.py +++ b/tests/grid/test_eta.py @@ -115,13 +115,10 @@ def test_set_hybrid_pressure_coefficients_correct(km): def test_set_hybrid_pressure_coefficients_nofile(): - """This test checks to see that the program - fails when (1) the eta_file is not specified in the yaml - configuration file; and (2), the computed eta values - increase non-monotonically. For the latter test, the eta_file - is specified in test_config_not_mono.yaml file and - the ak and bk values in the eta_file have been changed nonsensically - to result in erronenous eta values. + """ + This test checks to see that the program + fails when the eta_file is not specified + in the yaml configuration file. """ eta_file = "NULL" @@ -167,13 +164,14 @@ def test_set_hybrid_pressure_coefficients_nofile(): def test_set_hybrid_pressure_coefficients_not_mono(): - """This test checks to see that the program - fails when (1) the eta_file is not specified in the yaml - configuration file; and (2), the computed eta values - increase non-monotonically. For the latter test, the eta_file - is specified in test_config_not_mono.yaml file and - the ak and bk values in the eta_file have been changed nonsensically - to result in erronenous eta values. + """ + This test checks to see that the program + fails when the computed eta values increase + non-monotonically. For the latter test, the + eta_file is specified in test_config_not_mono.yaml + file and the ak and bk values in the eta_file + have been changed nonsensically to result in + erronenous eta values. """ working_dir = str(os.getcwd()) From 36496917793743ad71f8219cdb8fce60b07d3194 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Fri, 6 Sep 2024 11:02:06 -0400 Subject: [PATCH 12/14] Linting --- ndsl/grid/generation.py | 2 +- tests/grid/test_eta.py | 12 ++++++------ 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index 5c39c346..caf91031 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -297,7 +297,7 @@ def __init__( self._dy_center = None self._area = None self._area_c = None - if eta_file is not "": + if eta_file != "": ( self._ks, self._ptop, diff --git a/tests/grid/test_eta.py b/tests/grid/test_eta.py index 713e30b6..ab0539f8 100755 --- a/tests/grid/test_eta.py +++ b/tests/grid/test_eta.py @@ -117,7 +117,7 @@ def test_set_hybrid_pressure_coefficients_nofile(): """ This test checks to see that the program - fails when the eta_file is not specified + fails when the eta_file is not specified in the yaml configuration file. """ @@ -166,11 +166,11 @@ def test_set_hybrid_pressure_coefficients_not_mono(): """ This test checks to see that the program - fails when the computed eta values increase - non-monotonically. For the latter test, the - eta_file is specified in test_config_not_mono.yaml - file and the ak and bk values in the eta_file - have been changed nonsensically to result in + fails when the computed eta values increase + non-monotonically. For the latter test, the + eta_file is specified in test_config_not_mono.yaml + file and the ak and bk values in the eta_file + have been changed nonsensically to result in erronenous eta values. """ From 1318b56927143905cbb8d48f88f84141bfde4d31 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Thu, 12 Sep 2024 13:10:01 -0400 Subject: [PATCH 13/14] Amended generation.py to set default eta_file to None --- ndsl/grid/generation.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index caf91031..8a20be65 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -237,7 +237,7 @@ def __init__( dy_const: float = 1000.0, deglat: float = 15.0, extdgrid: bool = False, - eta_file: str = "", + eta_file: str = None, ak: Optional[np.ndarray] = None, bk: Optional[np.ndarray] = None, ): @@ -297,7 +297,7 @@ def __init__( self._dy_center = None self._area = None self._area_c = None - if eta_file != "": + if eta_file is not None: ( self._ks, self._ptop, From a1010dd603fe32c11bc3d6c0379cbf32ea59e118 Mon Sep 17 00:00:00 2001 From: Frank Malatino Date: Thu, 12 Sep 2024 14:06:58 -0400 Subject: [PATCH 14/14] Changed type hinting of eta_file in generation.py to reflect optional aspect --- ndsl/grid/generation.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndsl/grid/generation.py b/ndsl/grid/generation.py index 8a20be65..75437272 100644 --- a/ndsl/grid/generation.py +++ b/ndsl/grid/generation.py @@ -237,7 +237,7 @@ def __init__( dy_const: float = 1000.0, deglat: float = 15.0, extdgrid: bool = False, - eta_file: str = None, + eta_file: Optional[str] = None, ak: Optional[np.ndarray] = None, bk: Optional[np.ndarray] = None, ):