Skip to content

Commit 2796799

Browse files
authored
refactor: miscellaneous (#101)
* drop _ats_active, derive from self.ats * simplify comprehensions, use next(iter(...)) and list(...) * move a get_input_var_names() call inside a conditional where it's needed * use math.prod instead of a manual accumulator loop * simpler logic to exclude interface model and connections
1 parent 6f5ff76 commit 2796799

2 files changed

Lines changed: 13 additions & 47 deletions

File tree

modflowapi/extensions/apimodel.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import math
2+
13
import numpy as np
24

35
from ..util import is_exg
@@ -35,7 +37,7 @@ def package_list(self):
3537
"""
3638
Returns a list of package objects for the model
3739
"""
38-
return [package for _, package in self.package_dict.items()]
40+
return list(self.package_dict.values())
3941

4042
@property
4143
def package_names(self):
@@ -46,7 +48,7 @@ def package_names(self):
4648

4749
@property
4850
def package_types(self):
49-
return list(set([package.pkg_type for package in self.package_list]))
51+
return list({package.pkg_type for package in self.package_list})
5052

5153
def _set_package_names(self):
5254
"""
@@ -260,8 +262,8 @@ def shape(self):
260262
"""
261263
Returns a tuple of the model shape
262264
"""
263-
ivn = self.mf6.get_input_var_names()
264265
if self._shape is None:
266+
ivn = self.mf6.get_input_var_names()
265267
shape_vars = gridshape[self.dis_type]
266268
shape = []
267269
for var in shape_vars:
@@ -280,10 +282,7 @@ def size(self):
280282
Returns the number of nodes in the model
281283
"""
282284
if self._size is None:
283-
size = 1
284-
for dim in self.shape:
285-
size *= dim
286-
self._size = size
285+
self._size = math.prod(self.shape)
287286
return self._size
288287

289288
@property

modflowapi/extensions/apisimulation.py

Lines changed: 7 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,6 @@ def __init__(self, mf6, models, solutions, exchanges, tdis, ats):
3636

3737
self.tdis = tdis
3838
self.ats = ats
39-
self._ats_active = True
40-
if ats is None:
41-
self._ats_active = False
4239

4340
def __getattr__(self, item):
4441
"""
@@ -72,7 +69,7 @@ def ats_active(self):
7269
Returns a boolean to indicate if the ATS package is used in this
7370
simulation.
7471
"""
75-
return self._ats_active
72+
return self.ats is not None
7673

7774
@property
7875
def ats_period(self):
@@ -134,9 +131,7 @@ def sln(self):
134131
"""
135132
if len(self._solutions) > 1:
136133
return list(self._solutions.values())
137-
else:
138-
for sln in self._solutions.values():
139-
return sln
134+
return next(iter(self._solutions.values()))
140135

141136
@property
142137
def model_names(self):
@@ -158,7 +153,7 @@ def models(self):
158153
"""
159154
Returns a list of ApiModel objects associated with the simulation
160155
"""
161-
return [v for _, v in self._models.items()]
156+
return list(self._models.values())
162157

163158
@property
164159
def iteration(self):
@@ -283,25 +278,7 @@ def load(mf6):
283278
id_var_addr = mf6.get_var_address("ID", name)
284279
if name.startswith("SLN"):
285280
continue
286-
elif (
287-
name.startswith("GWFIM")
288-
or name.startswith("GWTIM")
289-
or name.startswith("GWEIM")
290-
or name.startswith("PRTIM")
291-
or name.startswith("CHFIM")
292-
or name.startswith("OLFIM")
293-
or name.startswith("SWFIM")
294-
):
295-
continue
296-
elif (
297-
name.startswith("GWFCON")
298-
or name.startswith("GWTCON")
299-
or name.startswith("GWECON")
300-
or name.startswith("PRTCON")
301-
or name.startswith("CHFCON")
302-
or name.startswith("OLFCON")
303-
or name.startswith("SWFCON")
304-
):
281+
elif name[3:5] in ("IM", "CO"):
305282
continue
306283
if id_var_addr not in variables:
307284
continue
@@ -321,22 +298,14 @@ def load(mf6):
321298
id_var_addr = mf6.get_var_address("ID", name)
322299
if name.lower() in models or name == "TDIS":
323300
continue
324-
if (
325-
name.startswith("GWFIM")
326-
or name.startswith("GWTIM")
327-
or name.startswith("GWEIM")
328-
or name.startswith("PRTIM")
329-
or name.startswith("CHFIM")
330-
or name.startswith("OLFIM")
331-
or name.startswith("SWFIM")
332-
):
301+
if name[3:5] == "IM":
333302
continue
334303
if id_var_addr not in variables:
335304
continue
336305

337306
solution_names.append(t[0])
338307

339-
idp_names = [i for i in mf6.get_value("__INPUT__/SIM/NAM/SLNMNAMES")]
308+
idp_names = list(mf6.get_value("__INPUT__/SIM/NAM/SLNMNAMES"))
340309
solution_types = [
341310
i[:-1].lower() for ix, i in enumerate(mf6.get_value("__INPUT__/SIM/NAM/SLNTYPE")) if idp_names[ix]
342311
]
@@ -373,10 +342,8 @@ def load(mf6):
373342
if is_exg(name) and exchange_name not in exchange_names:
374343
exchange_names.append(exchange_name)
375344

376-
# sim_packages: tdis, gwf-gwf, sln
377345
exchanges = {}
378346
for exchange_name in exchange_names:
379-
exchange = ApiExchange(mf6, exchange_name)
380-
exchanges[exchange_name.lower()] = exchange
347+
exchanges[exchange_name.lower()] = ApiExchange(mf6, exchange_name)
381348

382349
return ApiSimulation(mf6, models, solutions, exchanges, tdis, ats)

0 commit comments

Comments
 (0)