-
Notifications
You must be signed in to change notification settings - Fork 328
/
model_component.py
467 lines (387 loc) · 13.6 KB
/
model_component.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
#! /usr/bin/env python
"""Defines the base component class from which Landlab components inherit.
Base component class methods
++++++++++++++++++++++++++++
.. autosummary::
~landlab.core.model_component.Component.name
~landlab.core.model_component.Component.from_path
~landlab.core.model_component.Component.unit_agnostic
~landlab.core.model_component.Component.units
~landlab.core.model_component.Component.definitions
~landlab.core.model_component.Component.input_var_names
~landlab.core.model_component.Component.output_var_names
~landlab.core.model_component.Component.optional_var_names
~landlab.core.model_component.Component.var_type
~landlab.core.model_component.Component.var_units
~landlab.core.model_component.Component.var_definition
~landlab.core.model_component.Component.var_mapping
~landlab.core.model_component.Component.var_loc
~landlab.core.model_component.Component.var_help
~landlab.core.model_component.Component.initialize_output_fields
~landlab.core.model_component.Component.initialize_optional_output_fields
~landlab.core.model_component.Component.shape
~landlab.core.model_component.Component.grid
~landlab.core.model_component.Component.coords
"""
import os
import textwrap
import numpy as np
from .. import registry
from ..field import FieldError
from .model_parameter_loader import load_params
_VAR_HELP_MESSAGE = """
name: {name}
description:
{desc}
units: {units}
unit agnostic: {unit_agnostic}
at: {loc}
intent: {intent}
"""
class classproperty(property):
def __get__(self, cls, owner):
return self.fget.__get__(None, owner)()
class Component:
"""Base component class from which Landlab components inherit."""
_info = {}
_name = None
_cite_as = ""
_unit_agnostic = None
def __new__(cls, *args, **kwds):
registry.add(cls)
return object.__new__(cls)
def __init__(self, grid):
self._grid = grid
self._current_time = None
# ensure that required input fields exist
for name in self._info.keys():
at = self._info[name]["mapping"]
optional = self._info[name]["optional"]
in_true = "in" in self._info[name]["intent"]
if (in_true) and (not optional):
# if required input, verify that it exists.
if name not in self._grid[at]:
raise FieldError(
"{component} is missing required input field: {name} at {at}".format(
component=self._name, name=name, at=at
)
)
# if required input exists, check dtype.
field = self._grid[at][name]
dtype = self._info[name]["dtype"]
if field.dtype != dtype:
raise FieldError(
"{component} required input variable: {name} at {at} has incorrect dtype. dtype must be {dtype} and is {actual}".format(
component=self._name,
name=name,
at=at,
dtype=dtype,
actual=field.dtype,
)
)
# if optional input exists, check dtype
if in_true and optional:
if name in self._grid[at]:
field = self._grid[at][name]
dtype = self._info[name]["dtype"]
if field.dtype != dtype:
raise FieldError(
"{component} optional input variable: {name} at {at} has incorrect dtype. dtype must be {dtype} and is {actual}".format(
component=self._name,
name=name,
at=at,
dtype=dtype,
actual=field.dtype,
)
)
@classmethod
def from_path(cls, grid, path):
"""Create a component from an input file.
Parameters
----------
grid : ModelGrid
A landlab grid.
path : str or file_like
Path to a parameter file, contents of a parameter file, or
a file-like object.
Returns
-------
Component
A newly-created component.
"""
if os.path.isfile(path):
with open(path, "r") as fp:
params = load_params(fp)
else:
params = load_params(path)
return cls(grid, **params)
@classproperty
@classmethod
def cite_as(cls):
"""Citation information for component.
Return required software citation, if any. An empty string indicates
that no citations other than the standard Landlab package citations are
needed for the component.
Citations are provided in BibTeX format.
Returns
-------
cite_as
"""
return cls._cite_as
@property
def current_time(self):
"""Current time.
Some components may keep track of the current time. In this case, the
``current_time`` attribute is incremented. Otherwise it is set to None.
Returns
-------
current_time
"""
return self._current_time
@current_time.setter
def current_time(self, new_time):
if self._current_time is not None:
assert new_time > self._current_time
self._current_time = new_time
@classproperty
@classmethod
def input_var_names(cls):
"""Names of fields that are used by the component.
Returns
-------
tuple of str
Tuple of field names.
"""
input_var_names = [
name
for name in cls._info.keys()
if (not cls._info[name]["optional"]) and ("in" in cls._info[name]["intent"])
]
return tuple(sorted(input_var_names))
@classproperty
@classmethod
def output_var_names(cls):
"""Names of fields that are provided by the component.
Returns
-------
tuple of str
Tuple of field names.
"""
output_var_names = [
name
for name in cls._info.keys()
if (not cls._info[name]["optional"])
and ("out" in cls._info[name]["intent"])
]
return tuple(sorted(output_var_names))
@classproperty
@classmethod
def optional_var_names(cls):
"""Names of fields that are optionally provided by the component, if
any.
Returns
-------
tuple of str
Tuple of field names.
"""
optional_var_names = [
name for name in cls._info.keys() if cls._info[name]["optional"]
]
return tuple(sorted(optional_var_names))
@classmethod
def var_type(cls, name):
"""Returns the dtype of a field (float, int, bool, str...).
Parameters
----------
name : str
A field name.
Returns
-------
dtype
The dtype of the field.
"""
return cls._info[name]["dtype"]
@classproperty
@classmethod
def name(cls):
"""Name of the component.
Returns
-------
str
Component name.
"""
return cls._name
@classproperty
@classmethod
def unit_agnostic(cls):
"""Whether the component is unit agnostic.
If True, then the component is unit agnostic. Under this condition a
user must still provide consistent units across all input arguments,
keyword arguments, and fields. However, when ``unit_agnostic`` is True
the units specified can be interpreted as dimensions.
When False, then the component requires inputs in the specified units.
Returns
-------
bool
"""
return cls._unit_agnostic
@classproperty
@classmethod
def units(cls):
"""Get the units for all field values.
Returns
-------
tuple or str
Units for each field.
"""
return tuple(
sorted([(name, cls._info[name]["units"]) for name in cls._info.keys()])
)
@classmethod
def var_units(cls, name):
"""Get the units of a particular field.
Parameters
----------
name : str
A field name.
Returns
-------
str
Units for the given field.
"""
return cls._info[name]["units"]
@classproperty
@classmethod
def definitions(cls):
"""Get a description of each field.
Returns
-------
tuple of (*name*, *description*)
A description of each field.
"""
return tuple(
sorted([(name, cls._info[name]["doc"]) for name in cls._info.keys()])
)
@classmethod
def var_definition(cls, name):
"""Get a description of a particular field.
Parameters
----------
name : str
A field name.
Returns
-------
tuple of (*name*, *description*)
A description of each field.
"""
return cls._info[name]["doc"]
@classmethod
def var_help(cls, name):
"""Print a help message for a particular field.
Parameters
----------
name : str
A field name.
"""
desc = os.linesep.join(
textwrap.wrap(
cls._info[name]["doc"], initial_indent=" ", subsequent_indent=" "
)
)
units = cls._info[name]["units"]
loc = cls._info[name]["mapping"]
intent = cls._info[name]["intent"]
help = _VAR_HELP_MESSAGE.format(
name=name,
desc=desc,
units=units,
loc=loc,
intent=intent,
unit_agnostic=cls._unit_agnostic,
)
print(help.strip())
@classproperty
@classmethod
def var_mapping(cls):
"""Location where variables are defined.
Returns
-------
tuple of (name, location)
Tuple of variable name and location ('node', 'link', etc.) pairs.
"""
return tuple(
sorted([(name, cls._info[name]["mapping"]) for name in cls._info.keys()])
)
@classmethod
def var_loc(cls, name):
"""Location where a particular variable is defined.
Parameters
----------
name : str
A field name.
Returns
-------
str
The location ('node', 'link', etc.) where a variable is defined.
"""
return cls._info[name]["mapping"]
def initialize_output_fields(self, values_per_element=None):
"""Create fields for a component based on its input and output var
names.
This method will create new fields (without overwrite) for any fields
output by, but not supplied to, the component. New fields are
initialized to zero. Ignores optional fields. New fields are created as
arrays of floats, unless the component specifies the variable type.
Parameters
----------
values_per_element: int (optional)
On occasion, it is necessary to create a field that is of size
(n_grid_elements, values_per_element) instead of the default size
(n_grid_elements,). Use this keyword argument to acomplish this
task.
"""
for name in self._info.keys():
at = self._info[name]["mapping"]
optional = self._info[name]["optional"]
out_true = "out" in self._info[name]["intent"]
if (out_true) and (not optional) and (name not in self._grid[at]):
type_in = self.var_type(name)
num_elements = self._grid.size(at)
if values_per_element is None:
size = num_elements
else:
size = (num_elements, values_per_element)
init_vals = np.zeros(size, dtype=type_in)
units_in = self.var_units(name)
self.grid.add_field(name, init_vals, at=at, units=units_in, copy=False)
def initialize_optional_output_fields(self):
"""Create fields for a component based on its optional field outputs,
if declared in _optional_var_names.
This method will create new fields (without overwrite) for any
fields output by the component as optional. New fields are
initialized to zero. New fields are created as arrays of floats,
unless the component also contains the specifying property
_var_type.
"""
for name in self._info.keys():
at = self._info[name]["mapping"]
optional = self._info[name]["optional"]
out_true = "out" in self._info[name]["intent"]
if (out_true) and (optional) and (name not in self._grid[at]):
type_in = self.var_type(name)
init_vals = self.grid.zeros(at, dtype=type_in)
units_in = self.var_units(name)
self.grid.add_field(name, init_vals, at=at, units=units_in, copy=False)
@property
def shape(self):
"""Return the grid shape attached to the component, if defined."""
return self.grid._shape
@property
def grid(self):
"""Return the grid attached to the component."""
return self._grid
@property
def coords(self):
"""Return the coordinates of nodes on grid attached to the
component."""
return (self.grid.node_x, self.grid.node_y)