-
Notifications
You must be signed in to change notification settings - Fork 4
/
fake_models.py
422 lines (297 loc) · 10.6 KB
/
fake_models.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
from typing import Tuple
import numpy
import numpy as np
from bmipy import Bmi
from grpc4bmi.constants import GRPC_MAX_MESSAGE_LENGTH
class SomeException(Exception):
pass
class FailingModel(Bmi):
def __init__(self, exc):
self.exc = exc
def initialize(self, filename):
raise self.exc
def update(self):
raise self.exc
def update_until(self, time: float) -> None:
raise self.exc
def finalize(self):
raise self.exc
def get_component_name(self):
raise self.exc
def get_input_item_count(self) -> int:
raise self.exc
def get_output_item_count(self) -> int:
raise self.exc
def get_input_var_names(self):
raise self.exc
def get_output_var_names(self):
raise self.exc
def get_start_time(self):
raise self.exc
def get_current_time(self):
raise self.exc
def get_end_time(self):
raise self.exc
def get_time_step(self):
raise self.exc
def get_time_units(self):
raise self.exc
def get_var_type(self, name):
raise self.exc
def get_var_units(self, name):
raise self.exc
def get_var_itemsize(self, name):
raise self.exc
def get_var_nbytes(self, name):
raise self.exc
def get_var_grid(self, name):
raise self.exc
def get_value(self, name, dest):
raise self.exc
def get_value_ptr(self, name):
raise self.exc
def get_value_at_indices(self, name, dest, inds):
raise self.exc
def set_value(self, name, src):
raise self.exc
def set_value_at_indices(self, name, inds, src):
raise self.exc
def get_grid_shape(self, grid, shape):
raise self.exc
def get_grid_x(self, grid, x):
raise self.exc
def get_grid_y(self, grid, y):
raise self.exc
def get_grid_z(self, grid, z):
raise self.exc
def get_grid_spacing(self, grid, spacing):
raise self.exc
def get_grid_origin(self, grid, origin):
raise self.exc
def get_grid_rank(self, grid):
raise self.exc
def get_grid_size(self, grid):
raise self.exc
def get_grid_type(self, grid):
raise self.exc
def get_var_location(self, name: str) -> str:
raise self.exc
def get_grid_node_count(self, grid: int) -> int:
raise self.exc
def get_grid_edge_count(self, grid: int) -> int:
raise self.exc
def get_grid_face_count(self, grid: int) -> int:
raise self.exc
def get_grid_edge_nodes(self, grid: int, edge_nodes: np.ndarray) -> np.ndarray:
raise self.exc
def get_grid_face_nodes(self, grid: int, face_nodes: np.ndarray) -> np.ndarray:
raise self.exc
def get_grid_nodes_per_face(self, grid: int, nodes_per_face: np.ndarray) -> np.ndarray:
raise self.exc
def get_grid_face_edges(self, grid: int, face_edges: np.ndarray) -> np.ndarray:
raise self.exc
class GridModel(FailingModel):
def __init__(self):
super(GridModel, self).__init__(SomeException('not used'))
def initialize(self, filename):
pass
def get_output_var_names(self) -> Tuple[str]:
return 'plate_surface__temperature',
def get_var_grid(self, name):
return 0
class UniRectGridModel(GridModel):
def get_grid_type(self, grid):
return 'uniform_rectilinear'
def get_grid_rank(self, grid):
return 3
def get_grid_size(self, grid):
return 24
def get_grid_shape(self, grid: int, shape: np.ndarray) -> np.ndarray:
numpy.copyto(src=[2, 3, 4], dst=shape)
return shape
def get_grid_origin(self, grid, dest):
numpy.copyto(src=[0.1, 1.1, 2.1], dst=dest)
return dest
def get_grid_spacing(self, grid, dest):
numpy.copyto(src=[0.1, 0.2, 0.3], dst=dest)
return dest
class Rect3DGridModel(GridModel):
def get_grid_type(self, grid):
return 'rectilinear'
def get_grid_size(self, grid):
return 24
def get_grid_rank(self, grid: int) -> int:
return 3
def get_grid_shape(self, grid: int, shape: np.ndarray) -> np.ndarray:
numpy.copyto(src=[2, 3, 4], dst=shape)
return shape
def get_grid_x(self, grid: int, x: np.ndarray) -> np.ndarray:
numpy.copyto(src=[0.1, 0.2, 0.3, 0.4], dst=x)
return x
def get_grid_y(self, grid: int, y: np.ndarray) -> np.ndarray:
numpy.copyto(src=[1.1, 1.2, 1.3], dst=y)
return y
def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
numpy.copyto(src=[2.1, 2.2], dst=z)
return z
class Rect2DGridModel(Rect3DGridModel):
def get_grid_size(self, grid):
return 12 # 4*3
def get_grid_rank(self, grid: int) -> int:
return 2
def get_grid_shape(self, grid: int, shape: np.ndarray) -> np.ndarray:
numpy.copyto(src=[3, 4], dst=shape)
return shape
def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
raise NotImplementedError('Do not know what z is')
class Structured3DQuadrilateralsGridModel(GridModel):
# Grid shape:
# 0
# / \
# / \
# 3 1
# \ /
# \ /
# 2
#
def get_grid_type(self, grid):
return 'structured_quadrilateral'
def get_grid_rank(self, grid: int) -> int:
return 3
def get_grid_size(self, grid):
return 4
def get_grid_shape(self, grid, shape):
numpy.copyto(src=[1, 2, 2], dst=shape)
return shape
def get_grid_x(self, grid, x):
numpy.copyto(src=[1.1, 0.1, 1.1, 2.1], dst=x)
return x
def get_grid_y(self, grid: int, y: np.ndarray) -> np.ndarray:
numpy.copyto(src=[2.2, 1.2, 0.2, 2.2], dst=y)
return y
def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
numpy.copyto(src=[1.1, 2.2, 3.3, 4.4], dst=z)
return z
class Structured2DQuadrilateralsGridModel(GridModel):
# Grid shape:
# 0
# / \
# / \
# 3 1
# \ /
# \ /
# 2
#
def get_grid_type(self, grid):
return 'structured_quadrilateral'
def get_grid_rank(self, grid: int) -> int:
return 2
def get_grid_size(self, grid):
return 4
def get_grid_shape(self, grid, shape):
numpy.copyto(src=[2, 2], dst=shape)
return shape
def get_grid_x(self, grid, x):
numpy.copyto(src=[1.1, 0.1, 1.1, 2.1], dst=x)
return x
def get_grid_y(self, grid: int, y: np.ndarray) -> np.ndarray:
numpy.copyto(src=[2.2, 1.2, 0.2, 2.2], dst=y)
return y
def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
raise NotImplementedError('Do not know what z is')
class UnstructuredGridBmiModel(GridModel):
# Uses grid example at https://bmi.readthedocs.io/en/latest/model_grids.html#unstructured-grids
# Grid shape:
# 3-----\
# / \ \
# 0 \ \---5
# \ 2--/ /
# \ / /
# \ / /
# 1---\ /
# 4
def get_grid_type(self, grid):
return 'unstructured'
def get_grid_shape(self, grid, dest):
raise NotImplementedError('Do not know what shape is')
def get_grid_size(self, grid):
return 6
def get_grid_rank(self, grid: int) -> int:
return 2
def get_grid_node_count(self, grid: int) -> int:
return 6
def get_grid_edge_count(self, grid: int) -> int:
return 8
def get_grid_face_count(self, grid: int) -> int:
return 3
def get_grid_edge_nodes(self, grid: int, edge_nodes: np.ndarray) -> np.ndarray:
numpy.copyto(src=(0, 1, 1, 2, 2, 3, 3, 0, 1, 4, 4, 5, 5, 2, 5, 3), dst=edge_nodes)
return edge_nodes
def get_grid_face_nodes(self, grid: int, face_nodes: np.ndarray) -> np.ndarray:
numpy.copyto(src=(0, 1, 2, 3, 1, 4, 5, 2, 2, 5, 3), dst=face_nodes)
return face_nodes
def get_grid_face_edges(self, grid: int, face_edges: np.ndarray) -> np.ndarray:
numpy.copyto(src=(0, 1, 2, 3, 4, 5, 6, 1, 6, 7, 2), dst=face_edges)
return face_edges
def get_grid_nodes_per_face(self, grid: int, nodes_per_face: np.ndarray) -> np.ndarray:
numpy.copyto(src=(4, 4, 3), dst=nodes_per_face)
return nodes_per_face
def get_grid_x(self, grid: int, x: np.ndarray) -> np.ndarray:
numpy.copyto(src=[0., 1., 2., 1., 3., 4.], dst=x)
return x
def get_grid_y(self, grid: int, y: np.ndarray) -> np.ndarray:
numpy.copyto(src=[3., 1., 2., 4., 0., 3.], dst=y)
return y
def get_grid_z(self, grid: int, z: np.ndarray) -> np.ndarray:
raise NotImplementedError('Do not know what z is')
class DTypeModel(GridModel):
def __init__(self):
super().__init__()
self.dtype = numpy.dtype('float32')
self.value = numpy.array((1.1, 2.2, 3.3), dtype=self.dtype)
def get_var_type(self, name):
return str(self.dtype)
def get_var_itemsize(self, name):
return self.dtype.itemsize
def get_var_nbytes(self, name):
return self.dtype.itemsize * self.value.size
def get_value(self, name, dest):
numpy.copyto(src=self.value, dst=dest)
return dest
def get_value_at_indices(self, name, dest, inds):
numpy.copyto(src=self.value[inds], dst=dest)
return dest
def set_value(self, name, src):
self.value[:] = src
def set_value_at_indices(self, name, inds, src):
self.value[inds] = src
class Float32Model(DTypeModel):
pass
class Int32Model(DTypeModel):
def __init__(self):
super().__init__()
self.dtype = numpy.dtype('int32')
self.value = numpy.array((12, 24, 36), dtype=self.dtype)
class BooleanModel(DTypeModel):
def __init__(self):
super().__init__()
self.dtype = numpy.dtype('bool')
self.value = numpy.array((True, False, True), dtype=self.dtype)
class HugeModel(DTypeModel):
"""Model which has value which does not fit in single message body
Can be run from command line with
..code-block:: bash
run-bmi-server --path $PWD/test --name fake_models.HugeModel --port 55555 --debug
"""
def __init__(self):
super().__init__()
self.dtype = numpy.dtype('float64')
# Create value which is bigger than 4Mb
dimension = (3 * GRPC_MAX_MESSAGE_LENGTH) // self.dtype.itemsize + 1000
self.value = numpy.ones((dimension,), dtype=self.dtype)
class WithItemSizeZeroAndVarTypeFloat32Model(Float32Model):
def get_var_itemsize(self, name):
return 0
class WithItemSizeZeroAndUnknownVarType(WithItemSizeZeroAndVarTypeFloat32Model):
def get_var_type(self, name):
return 'real'