-
Notifications
You must be signed in to change notification settings - Fork 12
/
bmi.py
713 lines (583 loc) · 18.5 KB
/
bmi.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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
from abc import ABC, abstractmethod
from typing import Tuple
import numpy as np
class Bmi(ABC):
@abstractmethod
def initialize(self, config_file: str) -> None:
"""Perform startup tasks for the model.
Perform all tasks that take place before entering the model's time
loop, including opening files and initializing the model state. Model
inputs are read from a text-based configuration file, specified by
`filename`.
Parameters
----------
config_file : str, optional
The path to the model configuration file.
Notes
-----
Models should be refactored, if necessary, to use a
configuration file. CSDMS does not impose any constraint on
how configuration files are formatted, although YAML is
recommended. A template of a model's configuration file
with placeholder values is used by the BMI.
"""
...
@abstractmethod
def update(self) -> None:
"""Advance model state by one time step.
Perform all tasks that take place within one pass through the model's
time loop. This typically includes incrementing all of the model's
state variables. If the model's state variables don't change in time,
then they can be computed by the :func:`initialize` method and this
method can return with no action.
"""
...
@abstractmethod
def update_until(self, time: float) -> None:
"""Advance model state until the given time.
Parameters
----------
time : float
A model time later than the current model time.
"""
...
@abstractmethod
def finalize(self) -> None:
"""Perform tear-down tasks for the model.
Perform all tasks that take place after exiting the model's time
loop. This typically includes deallocating memory, closing files and
printing reports.
"""
...
@abstractmethod
def get_component_name(self) -> str:
"""Name of the component.
Returns
-------
str
The name of the component.
"""
...
@abstractmethod
def get_input_item_count(self) -> int:
"""Count of a model's input variables.
Returns
-------
int
The number of input variables.
"""
...
@abstractmethod
def get_output_item_count(self) -> int:
"""Count of a model's output variables.
Returns
-------
int
The number of output variables.
"""
...
@abstractmethod
def get_input_var_names(self) -> Tuple[str]:
"""List of a model's input variables.
Input variable names must be CSDMS Standard Names, also known
as *long variable names*.
Returns
-------
list of str
The input variables for the model.
Notes
-----
Standard Names enable the CSDMS framework to determine whether
an input variable in one model is equivalent to, or compatible
with, an output variable in another model. This allows the
framework to automatically connect components.
Standard Names do not have to be used within the model.
"""
...
@abstractmethod
def get_output_var_names(self) -> Tuple[str]:
"""List of a model's output variables.
Output variable names must be CSDMS Standard Names, also known
as *long variable names*.
Returns
-------
list of str
The output variables for the model.
"""
...
@abstractmethod
def get_var_grid(self, name: str) -> int:
"""Get grid identifier for the given variable.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
int
The grid identifier.
"""
...
@abstractmethod
def get_var_type(self, name: str) -> str:
"""Get data type of the given variable.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
str
The Python variable type; e.g., ``str``, ``int``, ``float``.
"""
...
@abstractmethod
def get_var_units(self, name: str) -> str:
"""Get units of the given variable.
Standard unit names, in lower case, should be used, such as
``meters`` or ``seconds``. Standard abbreviations, like ``m`` for
meters, are also supported. For variables with compound units,
each unit name is separated by a single space, with exponents
other than 1 placed immediately after the name, as in ``m s-1``
for velocity, ``W m-2`` for an energy flux, or ``km2`` for an
area.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
str
The variable units.
Notes
-----
CSDMS uses the `UDUNITS`_ standard from Unidata.
.. _UDUNITS: http://www.unidata.ucar.edu/software/udunits
"""
...
@abstractmethod
def get_var_itemsize(self, name: str) -> int:
"""Get memory use for each array element in bytes.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
int
Item size in bytes.
"""
...
@abstractmethod
def get_var_nbytes(self, name: str) -> int:
"""Get size, in bytes, of the given variable.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
int
The size of the variable, counted in bytes.
"""
...
@abstractmethod
def get_var_location(self, name: str) -> str:
"""Get the grid element type that the a given variable is defined on.
The grid topology can be composed of *nodes*, *edges*, and *faces*.
*node*
A point that has a coordinate pair or triplet: the most
basic element of the topology.
*edge*
A line or curve bounded by two *nodes*.
*face*
A plane or surface enclosed by a set of edges. In a 2D
horizontal application one may consider the word “polygon”,
but in the hierarchy of elements the word “face” is most common.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
str
The grid location on which the variable is defined. Must be one of
`"node"`, `"edge"`, or `"face"`.
Notes
-----
CSDMS uses the `ugrid conventions`_ to define unstructured grids.
.. _ugrid conventions: http://ugrid-conventions.github.io/ugrid-conventions
"""
...
@abstractmethod
def get_current_time(self) -> float:
"""Current time of the model.
Returns
-------
float
The current model time.
"""
...
@abstractmethod
def get_start_time(self) -> float:
"""Start time of the model.
Model times should be of type float.
Returns
-------
float
The model start time.
"""
...
@abstractmethod
def get_end_time(self) -> float:
"""End time of the model.
Returns
-------
float
The maximum model time.
"""
...
@abstractmethod
def get_time_units(self) -> str:
"""Time units of the model.
Returns
-------
str
The model time unit; e.g., `days` or `s`.
Notes
-----
CSDMS uses the UDUNITS standard from Unidata.
"""
...
@abstractmethod
def get_time_step(self) -> float:
"""Current time step of the model.
The model time step should be of type float.
Returns
-------
float
The time step used in model.
"""
...
@abstractmethod
def get_value(self, name: str, dest: np.ndarray) -> np.ndarray:
"""Get a copy of values of the given variable.
This is a getter for the model, used to access the model's
current state. It returns a *copy* of a model variable, with
the return type, size and rank dependent on the variable.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
dest : ndarray
A numpy array into which to place the values.
Returns
-------
ndarray
The same numpy array that was passed as an input buffer.
"""
...
@abstractmethod
def get_value_ptr(self, name: str) -> np.ndarray:
"""Get a reference to values of the given variable.
This is a getter for the model, used to access the model's
current state. It returns a reference to a model variable,
with the return type, size and rank dependent on the variable.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
Returns
-------
array_like
A reference to a model variable.
"""
...
@abstractmethod
def get_value_at_indices(
self, name: str, dest: np.ndarray, inds: np.ndarray
) -> np.ndarray:
"""Get values at particular indices.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
dest : ndarray
A numpy array into which to place the values.
inds : array_like
The indices into the variable array.
Returns
-------
array_like
Value of the model variable at the given location.
"""
...
@abstractmethod
def set_value(self, name: str, src: np.ndarray) -> None:
"""Specify a new value for a model variable.
This is the setter for the model, used to change the model's
current state. It accepts, through *src*, a new value for a
model variable, with the type, size and rank of *src*
dependent on the variable.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
src : array_like
The new value for the specified variable.
"""
...
@abstractmethod
def set_value_at_indices(
self, name: str, inds: np.ndarray, src: np.ndarray
) -> None:
"""Specify a new value for a model variable at particular indices.
Parameters
----------
name : str
An input or output variable name, a CSDMS Standard Name.
inds : array_like
The indices into the variable array.
src : array_like
The new value for the specified variable.
"""
...
# Grid information
@abstractmethod
def get_grid_rank(self, grid: int) -> int:
"""Get number of dimensions of the computational grid.
Parameters
----------
grid : int
A grid identifier.
Returns
-------
int
Rank of the grid.
"""
...
@abstractmethod
def get_grid_size(self, grid: int) -> int:
"""Get the total number of elements in the computational grid.
Parameters
----------
grid : int
A grid identifier.
Returns
-------
int
Size of the grid.
"""
...
@abstractmethod
def get_grid_type(self, grid: int) -> str:
"""Get the grid type as a string.
Parameters
----------
grid : int
A grid identifier.
Returns
-------
str
Type of grid as a string.
"""
...
# Uniform rectilinear
@abstractmethod
def get_grid_shape(self, grid: int, shape: np.ndarray) -> np.ndarray:
"""Get dimensions of the computational grid.
Parameters
----------
grid : int
A grid identifier.
shape : ndarray of int, shape *(ndim,)*
A numpy array into which to place the shape of the grid.
Returns
-------
ndarray of int
The input numpy array that holds the grid's shape.
"""
...
@abstractmethod
def get_grid_spacing(self, grid: int, spacing: np.ndarray) -> np.ndarray:
"""Get distance between nodes of the computational grid.
Parameters
----------
grid : int
A grid identifier.
spacing : ndarray of float, shape *(ndim,)*
A numpy array to hold the spacing between grid rows and columns.
Returns
-------
ndarray of float
The input numpy array that holds the grid's spacing.
"""
...
@abstractmethod
def get_grid_origin(self, grid: int, origin: np.ndarray) -> np.ndarray:
"""Get coordinates for the lower-left corner of the computational grid.
Parameters
----------
grid : int
A grid identifier.
origin : ndarray of float, shape *(ndim,)*
A numpy array to hold the coordinates of the lower-left corner of
the grid.
Returns
-------
ndarray of float
The input numpy array that holds the coordinates of the grid's
lower-left corner.
"""
...
# Non-uniform rectilinear, curvilinear
@abstractmethod
def get_grid_x(self, grid: int, x: np.ndarray) -> np.ndarray:
"""Get coordinates of grid nodes in the x direction.
Parameters
----------
grid : int
A grid identifier.
x : ndarray of float, shape *(nrows,)*
A numpy array to hold the x-coordinates of the grid node columns.
Returns
-------
ndarray of float
The input numpy array that holds the grid's column x-coordinates.
"""
...
@abstractmethod
def get_grid_y(self, grid: int, y: np.ndarray) -> np.ndarray:
"""Get coordinates of grid nodes in the y direction.
Parameters
----------
grid : int
A grid identifier.
y : ndarray of float, shape *(ncols,)*
A numpy array to hold the y-coordinates of the grid node rows.
Returns
-------
ndarray of float
The input numpy array that holds the grid's row y-coordinates.
"""
...
@abstractmethod
def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
"""Get coordinates of grid nodes in the z direction.
Parameters
----------
grid : int
A grid identifier.
z : ndarray of float, shape *(nlayers,)*
A numpy array to hold the z-coordinates of the grid nodes layers.
Returns
-------
ndarray of float
The input numpy array that holds the grid's layer z-coordinates.
"""
...
@abstractmethod
def get_grid_node_count(self, grid: int) -> int:
"""Get the number of nodes in the grid.
Parameters
----------
grid : int
A grid identifier.
Returns
-------
int
The total number of grid nodes.
"""
...
@abstractmethod
def get_grid_edge_count(self, grid: int) -> int:
"""Get the number of edges in the grid.
Parameters
----------
grid : int
A grid identifier.
Returns
-------
int
The total number of grid edges.
"""
...
@abstractmethod
def get_grid_face_count(self, grid: int) -> int:
"""Get the number of faces in the grid.
Parameters
----------
grid : int
A grid identifier.
Returns
-------
int
The total number of grid faces.
"""
...
@abstractmethod
def get_grid_edge_nodes(self, grid: int, edge_nodes: np.ndarray) -> np.ndarray:
"""Get the edge-node connectivity.
Parameters
----------
grid : int
A grid identifier.
edge_nodes : ndarray of int, shape *(2 x nnodes,)*
A numpy array to place the edge-node connectivity. For each edge,
connectivity is given as node at edge tail, followed by node at
edge head.
Returns
-------
ndarray of int
The input numpy array that holds the edge-node connectivity.
"""
...
@abstractmethod
def get_grid_face_edges(self, grid: int, face_edges: np.ndarray) -> np.ndarray:
"""Get the face-edge connectivity.
Parameters
----------
grid : int
A grid identifier.
face_edges : ndarray of int
A numpy array to place the face-edge connectivity.
Returns
-------
ndarray of int
The input numpy array that holds the face-edge connectivity.
"""
...
@abstractmethod
def get_grid_face_nodes(self, grid: int, face_nodes: np.ndarray) -> np.ndarray:
"""Get the face-node connectivity.
Parameters
----------
grid : int
A grid identifier.
face_nodes : ndarray of int
A numpy array to place the face-node connectivity. For each face,
the nodes (listed in a counter-clockwise direction) that form the
boundary of the face.
Returns
-------
ndarray of int
The input numpy array that holds the face-node connectivity.
"""
...
@abstractmethod
def get_grid_nodes_per_face(
self, grid: int, nodes_per_face: np.ndarray
) -> np.ndarray:
"""Get the number of nodes for each face.
Parameters
----------
grid : int
A grid identifier.
nodes_per_face : ndarray of int, shape *(nfaces,)*
A numpy array to place the number of nodes per face.
Returns
-------
ndarray of int
The input numpy array that holds the number of nodes per face.
"""
...