Skip to content

Commit c10d4d2

Browse files
authored
adapt time utilities from imod-python (#244)
Begin to implement the time side of #226 and also to address #243 with a Time subclass of flopy3's ModelTime with extra capabilities. Adapt some time-related utilities from imod-python supporting the ability to generate a TDIS from timestamps - https://github.com/Deltares/imod-python/blob/87bbed86a6e36b1b65644625aabc6c3d5b6fda63/imod/util/time.py#L98 - https://github.com/Deltares/imod-python/blob/87bbed86a6e36b1b65644625aabc6c3d5b6fda63/imod/mf6/simulation.py#L177 This can be the foundation for a similar method to create a TDIS from simulation packages later on.
1 parent 83426ba commit c10d4d2

File tree

15 files changed

+357
-38
lines changed

15 files changed

+357
-38
lines changed

docs/examples/quickstart.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@
22

33
import matplotlib.pyplot as plt
44
import numpy as np
5-
from flopy.discretization.modeltime import ModelTime
65
from flopy.discretization.structuredgrid import StructuredGrid
76

87
from flopy4.mf6.gwf import Chd, Gwf, Ic, Npf, Oc
98
from flopy4.mf6.ims import Ims
109
from flopy4.mf6.simulation import Simulation
10+
from flopy4.mf6.utils.time import Time
1111

1212
name = "quickstart"
1313
workspace = Path(__file__).parent / name
14-
time = ModelTime(perlen=[1.0], nstp=[1])
14+
time = Time(perlen=[1.0], nstp=[1])
1515
grid = StructuredGrid(
1616
nlay=1,
1717
nrow=10,

flopy4/mf6/component.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
from flopy4.mf6.constants import MF6
1313
from flopy4.mf6.spec import field, fields_dict, to_field
14-
from flopy4.mf6.utils.grid_utils import update_maxbound
14+
from flopy4.mf6.utils.grid import update_maxbound
1515
from flopy4.uio import IO, Loader, Writer
1616

1717
COMPONENTS = {}

flopy4/mf6/gwf/chd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from flopy4.mf6.converter import structure_array
1111
from flopy4.mf6.package import Package
1212
from flopy4.mf6.spec import array, field, path
13-
from flopy4.mf6.utils.grid_utils import update_maxbound
13+
from flopy4.mf6.utils.grid import update_maxbound
1414
from flopy4.utils import to_path
1515

1616

flopy4/mf6/gwf/drn.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from flopy4.mf6.converter import structure_array
1111
from flopy4.mf6.package import Package
1212
from flopy4.mf6.spec import array, field, path
13-
from flopy4.mf6.utils.grid_utils import update_maxbound
13+
from flopy4.mf6.utils.grid import update_maxbound
1414
from flopy4.utils import to_path
1515

1616

flopy4/mf6/gwf/rch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from flopy4.mf6.converter import structure_array
1111
from flopy4.mf6.package import Package
1212
from flopy4.mf6.spec import array, field, path
13-
from flopy4.mf6.utils.grid_utils import update_maxbound
13+
from flopy4.mf6.utils.grid import update_maxbound
1414
from flopy4.utils import to_path
1515

1616

flopy4/mf6/gwf/wel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from flopy4.mf6.converter import structure_array
1111
from flopy4.mf6.package import Package
1212
from flopy4.mf6.spec import array, field, path
13-
from flopy4.mf6.utils.grid_utils import update_maxbound
13+
from flopy4.mf6.utils.grid import update_maxbound
1414
from flopy4.utils import to_path
1515

1616

flopy4/mf6/simulation.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
from os import PathLike
22
from warnings import warn
33

4-
from flopy.discretization.modeltime import ModelTime
54
from modflow_devtools.misc import cd, run_cmd
65
from xattree import xattree
76

@@ -11,14 +10,15 @@
1110
from flopy4.mf6.solution import Solution
1211
from flopy4.mf6.spec import field
1312
from flopy4.mf6.tdis import Tdis
13+
from flopy4.mf6.utils.time import Time
1414

1515

1616
def convert_time(value):
17-
if isinstance(value, ModelTime):
17+
if isinstance(value, Time):
1818
return Tdis.from_time(value)
1919
if isinstance(value, Tdis):
2020
return value
21-
raise TypeError(f"Expected ModelTime or Tdis, got {type(value)}")
21+
raise TypeError(f"Expected Time or Tdis, got {type(value)}")
2222

2323

2424
@xattree
@@ -41,8 +41,8 @@ def __attrs_post_init__(self):
4141
self.filename = "mfsim.nam"
4242

4343
@property
44-
def time(self) -> ModelTime:
45-
"""Return the simulation time discretization."""
44+
def time(self) -> Time:
45+
"""Return a `Time` object describing the simulation's time discretization."""
4646
return self.tdis.to_time()
4747

4848
def run(self, exe: str | PathLike = "mf6", verbose: bool = False) -> None:

flopy4/mf6/tdis.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33

44
import numpy as np
55
from attrs import Converter, define
6-
from flopy.discretization.modeltime import ModelTime
7-
from numpy.typing import NDArray
6+
from numpy.typing import ArrayLike, NDArray
87
from xattree import ROOT, xattree
98

109
from flopy4.mf6.converter import structure_array
1110
from flopy4.mf6.package import Package
1211
from flopy4.mf6.spec import array, dim, field
12+
from flopy4.mf6.utils.time import Time
1313

1414

1515
@xattree
@@ -42,9 +42,9 @@ class PeriodData:
4242
converter=Converter(structure_array, takes_self=True, takes_field=True),
4343
)
4444

45-
def to_time(self) -> ModelTime:
46-
"""Convert to a `ModelTime` object."""
47-
return ModelTime(
45+
def to_time(self) -> Time:
46+
"""Convert to a `Time` object."""
47+
return Time(
4848
nper=self.nper,
4949
time_units=self.time_units,
5050
start_date_time=self.start_date_time,
@@ -54,8 +54,8 @@ def to_time(self) -> ModelTime:
5454
)
5555

5656
@classmethod
57-
def from_time(cls, time: ModelTime) -> "Tdis":
58-
"""Create a time discretization from a `ModelTime`."""
57+
def from_time(cls, time: Time) -> "Tdis":
58+
"""Create a time discretization from a `Time` object."""
5959
return cls(
6060
nper=time.nper,
6161
time_units=None if time.time_units in [None, "unknown"] else time.time_units,
@@ -64,3 +64,33 @@ def from_time(cls, time: ModelTime) -> "Tdis":
6464
nstp=time.nstp,
6565
tsmult=time.tsmult,
6666
)
67+
68+
@classmethod
69+
def from_timestamps(
70+
cls,
71+
timestamps: ArrayLike,
72+
nstp: Optional[ArrayLike] = None,
73+
tsmult: Optional[ArrayLike] = None,
74+
) -> "Tdis":
75+
"""
76+
Create a time discretization from timestamps.
77+
78+
Parameters
79+
----------
80+
timestamps : sequence of datetime-likes
81+
Stress period start times
82+
nstp : int or sequence of int, optional
83+
Number of timesteps per stress period. If scalar, applied to all periods.
84+
If None, defaults to 1 for all periods.
85+
tsmult : float or sequence of float, optional
86+
Timestep multiplier per stress period. If scalar, applied to all periods.
87+
If None, defaults to 1.0 for all periods.
88+
89+
Returns
90+
-------
91+
Tdis
92+
Time discretization object
93+
"""
94+
95+
time = Time.from_timestamps(timestamps, nstp=nstp, tsmult=tsmult)
96+
return cls.from_time(time)

flopy4/mf6/utils/cbc_reader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from flopy.discretization import StructuredGrid
1414

1515
from flopy4.adapters import StructuredGridWrapper
16-
from flopy4.mf6.utils.grid_utils import get_coords
16+
from flopy4.mf6.utils.grid import get_coords
1717

1818

1919
@define
File renamed without changes.

0 commit comments

Comments
 (0)