Skip to content

Commit

Permalink
Pre-commit clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
alicebarthel committed Mar 7, 2023
1 parent cb4b227 commit a3baa9c
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 44 deletions.
12 changes: 7 additions & 5 deletions compass/ocean/tests/mitgcm_baroclinic_gyre/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from compass.ocean.tests.mitgcm_baroclinic_gyre.gyre_test_case import (
GyreTestCase,
)
from compass.testgroup import TestGroup
from compass.ocean.tests.mitgcm_baroclinic_gyre.gyre_test_case import GyreTestCase


class MitgcmBaroclinicGyre(TestGroup):
Expand All @@ -15,8 +17,8 @@ def __init__(self, mpas_core):

for resolution in ['80km']:
self.add_test_case(
GyreTestCase(test_group=self, resolution=resolution, long=False))
GyreTestCase(test_group=self, resolution=resolution,
long=False))
self.add_test_case(
GyreTestCase(test_group=self, resolution=resolution, long=True))


GyreTestCase(test_group=self, resolution=resolution,
long=True))
3 changes: 1 addition & 2 deletions compass/ocean/tests/mitgcm_baroclinic_gyre/cull_mesh.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import numpy as np
import xarray as xr
import numpy as np

from mpas_tools.io import write_netcdf
from mpas_tools.mesh.conversion import convert, cull

Expand Down
20 changes: 11 additions & 9 deletions compass/ocean/tests/mitgcm_baroclinic_gyre/forward.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from compass.model import run_model
from compass.step import Step
from compass.model import partition, run_model


class Forward(Step):
"""
A step for performing forward MPAS-Ocean runs as part of
A step for performing forward MPAS-Ocean runs as part of
the MITgcm baoclinic gyre test cases.
Attributes
Expand All @@ -14,7 +14,7 @@ class Forward(Step):
"""
def __init__(self, test_case, resolution, name='forward', subdir=None,
long=False):
long=False):
"""
Create a new test case
Expand Down Expand Up @@ -59,18 +59,20 @@ def __init__(self, test_case, resolution, name='forward', subdir=None,
# make sure output is double precision
self.add_streams_file('compass.ocean.streams', 'streams.output')

self.add_namelist_file('compass.ocean.tests.mitgcm_baroclinic_gyre', 'namelist.forward')
self.add_namelist_file('compass.ocean.tests.mitgcm_baroclinic_gyre',
'namelist.forward')
if long:
output_interval = "0010_00:00:00"
restart_interval = "0010_00:00:00"
else:
output_interval = res_params['run_duration'].replace("'", "")
restart_interval = "0030_00:00:00"
replacements = dict(
output_interval=output_interval, restart_interval=restart_interval)
self.add_streams_file(package='compass.ocean.tests.mitgcm_baroclinic_gyre',
streams='streams.forward',
template_replacements=replacements)
replacements = dict(output_interval=output_interval,
restart_interval=restart_interval)
self.add_streams_file(
package='compass.ocean.tests.mitgcm_baroclinic_gyre',
streams='streams.forward',
template_replacements=replacements)
options = dict()
for option in ['dt', 'btr_dt', 'mom_del4', 'run_duration']:
options[f'config_{option}'] = res_params[option]
Expand Down
31 changes: 15 additions & 16 deletions compass/ocean/tests/mitgcm_baroclinic_gyre/initial_state.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import xarray as xr
import numpy as np

import xarray as xr
from mpas_tools.io import write_netcdf

from compass.ocean.vertical import init_vertical_coord
Expand Down Expand Up @@ -52,9 +51,8 @@ def run(self):

_write_forcing(config, ds.latCell)

def _write_initial_state(config, dsMesh):
section = config['mitgcm_baroclinic_gyre']

def _write_initial_state(config, dsMesh):
ds = dsMesh.copy()

bottom_depth = config.getfloat('vertical_grid', 'bottom_depth')
Expand All @@ -63,11 +61,13 @@ def _write_initial_state(config, dsMesh):

init_vertical_coord(config, ds)

# setting the initial conditions
temperature = (-11. * np.log(0.0414*
(-1.*ds.zMid + 100.3)) + 48.8)
# setting the initial conditions
temperature = (-11. * np.log(0.0414 *
(-1. * ds.zMid + 100.3)) + 48.8)
temperature = temperature.transpose('Time', 'nCells', 'nVertLevels')
print(f'bottom T: {temperature[0, 200, -1]} and surface : {temperature[0, 200, 0]}')
bottT = temperature[0, 200, -1].values
surfT = temperature[0, 200, 0].values
print(f'bottom T: {bottT} and surface : {surfT}')
salinity = 34.0 * xr.ones_like(temperature)

normalVelocity = xr.zeros_like(ds.xEdge)
Expand All @@ -76,17 +76,18 @@ def _write_initial_state(config, dsMesh):
normalVelocity = normalVelocity.expand_dims(dim='Time', axis=0)

ds['temperature'] = temperature
ds['salinity'] = salinity
ds['salinity'] = salinity
ds['normalVelocity'] = normalVelocity

omega = 2. * np.pi / 86164.
omega = 2. * np.pi / 86164.
ds['fCell'] = 2. * omega * np.sin(ds.latCell)
ds['fEdge'] = 2. * omega * np.sin(ds.latEdge)
ds['fVertex'] = 2. * omega * np.sin(ds.latVertex)
ds['fEdge'] = 2. * omega * np.sin(ds.latEdge)
ds['fVertex'] = 2. * omega * np.sin(ds.latVertex)

write_netcdf(ds, 'initial_state.nc')
return ds


def _write_forcing(config, lat):
section = config['mitgcm_baroclinic_gyre']
latMin = section.getfloat('lat_min')
Expand All @@ -97,8 +98,8 @@ def _write_forcing(config, lat):
restoring_temp_piston_vel = section.getfloat('restoring_temp_piston_vel')
lat = np.rad2deg(lat)
# set wind stress
windStressZonal = -tauMax * np.cos(2 * np.pi * (lat - latMin) \
/ (latMax - latMin))
windStressZonal = -tauMax * np.cos(2 * np.pi * (lat - latMin) /
(latMax - latMin))

windStressZonal = windStressZonal.expand_dims(dim='Time', axis=0)

Expand All @@ -118,7 +119,6 @@ def _write_forcing(config, lat):
34.0 * xr.ones_like(temperatureSurfaceRestoringValue)
salinityPistonVelocity = xr.zeros_like(temperaturePistonVelocity)


dsForcing = xr.Dataset()
dsForcing['windStressZonal'] = windStressZonal
dsForcing['windStressMeridional'] = windStressMeridional
Expand All @@ -129,4 +129,3 @@ def _write_forcing(config, lat):
dsForcing['salinitySurfaceRestoringValue'] = salinitySurfaceRestoringValue

write_netcdf(dsForcing, 'forcing.nc')

30 changes: 18 additions & 12 deletions compass/ocean/vertical/grid_1d.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import numpy
from importlib import resources
import json
from netCDF4 import Dataset
from importlib import resources

import numpy
import numpy as np
from netCDF4 import Dataset
from scipy.optimize import root_scalar


Expand Down Expand Up @@ -52,7 +53,7 @@ def generate_1d_grid(config):
grid_type != 'tanh_dz':
bottom_depth = section.getfloat('bottom_depth')
# renormalize to the requested range
interfaces = (bottom_depth/interfaces[-1]) * interfaces
interfaces = (bottom_depth / interfaces[-1]) * interfaces

return interfaces

Expand Down Expand Up @@ -128,7 +129,7 @@ def add_1d_grid(config, ds):

def _generate_uniform(vert_levels):
""" Generate uniform layer interfaces between 0 and 1 """
interfaces = numpy.linspace(0., 1., vert_levels+1)
interfaces = numpy.linspace(0., 1., vert_levels + 1)
return interfaces


Expand All @@ -146,7 +147,8 @@ def _read_json(grid_type):
def _create_linear_dz_grid(num_vert_levels, bottom_depth,
linear_dz_rate):
"""
Creates the linear vertical grid for MPAS-Ocean and writes it to a NetCDF file
Creates the linear vertical grid for MPAS-Ocean and
writes it to a NetCDF file
Parameters
----------
Expand All @@ -166,11 +168,13 @@ def _create_linear_dz_grid(num_vert_levels, bottom_depth,
"""

nz = num_vert_levels
layerThickness = [(bottom_depth / nz) - (np.floor(nz/2) - k) * linear_dz_rate for k in np.arange(0, nz)]
min_layer_thickness = layerThickness[0]
layerThickness = [(bottom_depth / nz) - (np.floor(nz / 2) - k) *
linear_dz_rate for k in np.arange(0, nz)]
min_layer_thickness = layerThickness[0]
max_layer_thickness = layerThickness[-1]
print('Linear dz vertical grid')
print(f'min layer thickness: {min_layer_thickness}; max layer thickness {max_layer_thickness} in m;')
print(f'min layer thickness: {min_layer_thickness}; '
f'max layer thickness {max_layer_thickness} in m;')
interfaces = - np.append([0], np.cumsum(layerThickness))

return interfaces
Expand All @@ -179,7 +183,8 @@ def _create_linear_dz_grid(num_vert_levels, bottom_depth,
def _create_tanh_dz_grid(num_vert_levels, bottom_depth, min_layer_thickness,
max_layer_thickness):
"""
Creates the tanh vertical grid for MPAS-Ocean and writes it to a NetCDF file
Creates the tanh vertical grid for MPAS-Ocean and
writes it to a NetCDF file
Parameters
----------
Expand Down Expand Up @@ -224,7 +229,7 @@ def _create_tanh_dz_grid(num_vert_levels, bottom_depth, min_layer_thickness,

def _tanh_match_bottom(delta, nz, dz1, dz2, bottom_depth):
"""
For tanh layer thickness, compute the difference between the
For tanh layer thickness, compute the difference between the
bottom depth computed with the given
parameters and the target ``bottom_depth``, used in the root finding
algorithm to determine which value of ``delta`` to use.
Expand Down Expand Up @@ -262,7 +267,8 @@ def _tanh_match_bottom(delta, nz, dz1, dz2, bottom_depth):

def _tanh_cumsum_z(delta, nz, dz1, dz2):
"""
Compute tanh layer interface depths and layer thicknesses over ``nz`` layers
Compute tanh layer interface depths and layer thicknesses
over ``nz`` layers
Parameters
----------
Expand Down

0 comments on commit a3baa9c

Please sign in to comment.