Skip to content

Commit fc5f3c6

Browse files
committed
add calculate_grid_convergence_index
1 parent 050ce85 commit fc5f3c6

1 file changed

Lines changed: 32 additions & 1 deletion

File tree

mhkit/river/io/d3d.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ def index_to_seconds(data: netCDF4.Dataset, time_index: int) -> Union[int, float
7272
"""
7373
return _convert_time(data, time_index=time_index)
7474

75+
7576
# pylint: disable=possibly-used-before-assignment
7677
def seconds_to_index(data: netCDF4.Dataset, seconds_run: Union[int, float]) -> int:
7778
"""
@@ -95,6 +96,7 @@ def seconds_to_index(data: netCDF4.Dataset, seconds_run: Union[int, float]) -> i
9596
"""
9697
return _convert_time(data, seconds_run=seconds_run)
9798

99+
98100
# pylint: disable=possibly-used-before-assignment
99101
def _convert_time(
100102
data: netCDF4.Dataset or xr.Dataset,
@@ -161,10 +163,11 @@ def _convert_time(
161163

162164
return converted_value
163165

164-
166+
# pylint: disable=unused-variable
165167
# pylint: disable=too-many-locals
166168
# pylint: disable=too-many-branches
167169
# pylint: disable=too-many-statements
170+
# pylint: disable=used-before-assignment
168171
# pylint: disable=possibly-used-before-assignment
169172
def get_layer_data(
170173
data: Union[netCDF4.Dataset, xr.Dataset],
@@ -685,6 +688,7 @@ def variable_interpolation(
685688

686689
return transformed_data
687690

691+
688692
# pylint: disable=possibly-used-before-assignment
689693
def get_all_data_points(
690694
data: (netCDF4.Dataset, xr.Dataset),
@@ -1045,3 +1049,30 @@ def list_variables(data: Union[netCDF4.Dataset, xr.Dataset, xr.DataArray]) -> Li
10451049
"data must be a NetCDF4 Dataset, xarray Dataset, or "
10461050
f"xarray DataArray. Got: {type(data)}"
10471051
)
1052+
1053+
def calculate_grid_convergence_index(fine_grid, coarse_grid, refinement_ratio,factor_of_safety=1.25, order=2):
1054+
"""
1055+
Calculate the Grid Convergence Index (GCI) between two grid sizes. https://www.grc.nasa.gov/WWW/wind/valid/tutorial/spatconv.html
1056+
1057+
Parameters
1058+
----------
1059+
fine_grid: numpy.ndarray
1060+
Results from the finer grid.
1061+
coarse_grid: numpy.ndarray
1062+
Results from the coarser grid.
1063+
refinement_ratio: float
1064+
Refinement ratio between the grids.
1065+
order: int
1066+
Order of accuracy (default is 2).
1067+
1068+
Returns
1069+
-------
1070+
gci: float
1071+
Grid Convergence Index (GCI).
1072+
"""
1073+
# Calculate the approximate relative error
1074+
error = np.abs((fine_grid - coarse_grid) / fine_grid)
1075+
1076+
# Calculate the GCI
1077+
gci = (factor_of_safety * error) / (refinement_ratio**order - 1)
1078+
return gci

0 commit comments

Comments
 (0)