tilupy 0.1.5__py3-none-any.whl → 1.0.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of tilupy might be problematic. Click here for more details.

@@ -10,21 +10,45 @@ import os
10
10
  import numpy as np
11
11
  import tilupy.read
12
12
 
13
+ from tilupy import notations
14
+
13
15
  # Dictionnary with results names lookup table, to match code output names
14
- LOOKUP_NAMES = dict(h='rho', hvert='rho',
15
- u='unorm', ux='u', uy='ut',
16
- hu='momentum', hu2int='ek',
17
- vol='vol')
16
+ LOOKUP_NAMES = dict(
17
+ h="rho",
18
+ hvert="rho",
19
+ ux="u",
20
+ uy="ut",
21
+ u="unorm",
22
+ hu="momentum",
23
+ ek="ek",
24
+ vol="vol",
25
+ ep="ep",
26
+ etot="etot",
27
+ )
18
28
 
19
29
  # Classify results
20
- STATES_OUTPUT = ['h', 'ux', 'uy', 'hvert']
21
- forces = ['facc', 'fcurv', 'ffric', 'fgrav', 'fpression']
30
+ STATES_OUTPUT = ["h", "ux", "uy", "hvert"]
31
+
32
+ tmp = ["facc", "fcurv", "ffric", "fgrav", "finert", "fpression"]
33
+
22
34
  FORCES_OUTPUT = []
23
- for axis in ['x', 'y']:
24
- for f in forces:
25
- FORCES_OUTPUT.append(f + axis)
26
-
27
- INTEGRATED_OUTPUT = ['ek', 'ep', 'etot']
35
+ for force in tmp:
36
+ FORCES_OUTPUT.append(force + "x")
37
+ FORCES_OUTPUT.append(force + "y")
38
+
39
+ FORCES_OUTPUT += [
40
+ "shearx",
41
+ "sheary",
42
+ "shearz",
43
+ "normalx",
44
+ "normaly",
45
+ "normalz",
46
+ "pbottom",
47
+ "pcorrdt",
48
+ "pcorrdiv",
49
+ ]
50
+
51
+ INTEGRATED_OUTPUT = ["ek", "ep", "etot"]
28
52
 
29
53
 
30
54
  def read_params(file):
@@ -46,61 +70,63 @@ def read_params(file):
46
70
  params = None
47
71
  else:
48
72
  params = dict()
49
- with open(file, 'r') as f:
73
+ with open(file, "r") as f:
50
74
  for line in f:
51
- (key, val) = line.split(' ')
75
+ (key, val) = line.split(" ")
52
76
  try:
53
77
  params[key] = float(val)
54
78
  except ValueError:
55
79
  params[key] = val.rstrip()
56
- params['nx'] = int(params['nx'])
57
- params['ny'] = int(params['ny'])
80
+ params["nx"] = int(params["nx"])
81
+ params["ny"] = int(params["ny"])
58
82
 
59
83
  return params
60
84
 
85
+
61
86
  def get_axes(**varargs):
62
87
  """Set x and y axes."""
63
- if 'x0' in varargs :
64
- x0 = varargs['x0']
88
+ if "x0" in varargs:
89
+ x0 = varargs["x0"]
65
90
  else:
66
91
  x0 = 0
67
- if 'y0' in varargs:
68
- y0 = varargs['y0']
92
+ if "y0" in varargs:
93
+ y0 = varargs["y0"]
69
94
  else:
70
95
  y0 = 0
71
96
 
72
- nx = varargs['nx']
73
- ny = varargs['ny']
74
- dx = varargs['per']/nx
75
- dy = varargs['pery']/ny
76
- x = dx*np.arange(nx)+dx/2
77
- y = dy*np.arange(ny)+dy/2
97
+ nx = varargs["nx"]
98
+ ny = varargs["ny"]
99
+ dx = varargs["per"] / nx
100
+ dy = varargs["pery"] / ny
101
+ x = dx * np.arange(nx) + dx / 2
102
+ y = dy * np.arange(ny) + dy / 2
78
103
 
79
104
  try:
80
- coord_pos = varargs['coord_pos']
105
+ coord_pos = varargs["coord_pos"]
81
106
  except KeyError:
82
- coord_pos = 'bottom_left'
83
-
84
- if coord_pos == 'bottom_left':
85
- x = x+x0
86
- y = y+y0
87
- elif coord_pos == 'upper_right':
88
- x = x+x0-x[-1]
89
- y = y+y0-y[-1]
90
- elif coord_pos == 'upper_left':
91
- x = x+x0
92
- y = y+y0-y[-1]
93
- elif coord_pos == 'lower_right':
94
- x = x+x0-x[-1]
95
- y = y+y0
107
+ coord_pos = "bottom_left"
108
+
109
+ if coord_pos == "bottom_left":
110
+ x = x + x0
111
+ y = y + y0
112
+ elif coord_pos == "upper_right":
113
+ x = x + x0 - x[-1]
114
+ y = y + y0 - y[-1]
115
+ elif coord_pos == "upper_left":
116
+ x = x + x0
117
+ y = y + y0 - y[-1]
118
+ elif coord_pos == "lower_right":
119
+ x = x + x0 - x[-1]
120
+ y = y + y0
96
121
 
97
122
  return x, y
98
123
 
124
+
99
125
  def read_file_bin(file, nx, ny):
100
126
  """Read shaltop .bin result file."""
101
127
  data = np.fromfile(file, dtype=np.float32)
102
- nbim = int(np.size(data)/nx/ny)
103
- data = np.reshape(data, (nx, ny, nbim), order='F')
128
+ nbim = int(np.size(data) / nx / ny)
129
+ data = np.reshape(data, (nx, ny, nbim), order="F")
104
130
  data = np.transpose(np.flip(data, axis=1), (1, 0, 2))
105
131
 
106
132
  return data
@@ -109,7 +135,7 @@ def read_file_bin(file, nx, ny):
109
135
  def read_file_init(file, nx, ny):
110
136
  """Read shaltop initial .d files."""
111
137
  data = np.loadtxt(file)
112
- data = np.reshape(data, (nx, ny), order='F')
138
+ data = np.reshape(data, (nx, ny), order="F")
113
139
  data = np.transpose(np.flip(data, axis=1), (1, 0))
114
140
  return data
115
141
 
@@ -128,173 +154,302 @@ class Results(tilupy.read.Results):
128
154
 
129
155
  """
130
156
  super().__init__()
131
-
157
+
132
158
  if folder_base is None:
133
159
  folder_base = os.getcwd()
134
160
  if file_params is None:
135
- file_params = 'params.txt'
136
-
137
- if '.' not in file_params:
138
- file_params = file_params + '.txt'
139
-
161
+ file_params = "params.txt"
162
+
163
+ if "." not in file_params:
164
+ file_params = file_params + ".txt"
165
+
140
166
  file_params = os.path.join(folder_base, file_params)
141
-
167
+
142
168
  params = read_params(file_params)
143
169
  x, y = get_axes(**params)
144
-
145
- varargs.update(dict(code='shaltop',
146
- htype='normal',
147
- params=params,
148
- x=x, y=y,
149
- nx=len(x), ny=len(y)))
150
-
170
+
171
+ varargs.update(
172
+ dict(
173
+ code="shaltop",
174
+ htype="normal",
175
+ params=params,
176
+ x=x,
177
+ y=y,
178
+ nx=len(x),
179
+ ny=len(y),
180
+ )
181
+ )
182
+
151
183
  for key in varargs:
152
184
  setattr(self, key, varargs[key])
153
185
 
154
186
  self.folder_base = folder_base
155
187
  # Folder where results are stored
156
- if 'folder_output' not in self.params:
157
- self.folder_output = os.path.join(self.folder_base,
158
- 'data2')
188
+ if "folder_output" not in self.params:
189
+ self.folder_output = os.path.join(self.folder_base, "data2")
159
190
  else:
160
- self.folder_output = os.path.join(self.folder_base,
161
- self.params['folder_output'])
191
+ self.folder_output = os.path.join(
192
+ self.folder_base, self.params["folder_output"]
193
+ )
162
194
 
163
195
  # Get time of outputs
164
- self.tim = np.loadtxt(os.path.join(self.folder_output, 'time_im.d'))
196
+ self.tim = np.loadtxt(os.path.join(self.folder_output, "time_im.d"))
197
+ self.tforces = np.loadtxt(
198
+ os.path.join(self.folder_output, "time_forces.d")
199
+ )
165
200
 
166
-
167
-
168
201
  @property
169
202
  def zinit(self):
170
- """ Compute or get cos(slope) of topography """
203
+ """Compute or get cos(slope) of topography"""
171
204
  if self._zinit is None:
172
205
  self.set_zinit()
173
206
  return self._zinit
174
207
 
175
208
  def set_zinit(self, zinit=None):
176
209
  """Set zinit, initial topography."""
177
- path_zinit = os.path.join(self.folder_base, self.folder_output,
178
- 'z.bin')
179
- if not os.path.isfile(path_zinit) and 'file_z_init' in self.params:
180
- path_zinit = os.path.join(self.folder_base,
181
- self.params['file_z_init'])
182
- self._zinit = read_file_init(path_zinit, self.nx, self.ny)
210
+ path_zinit = os.path.join(
211
+ self.folder_base, self.folder_output, "z.bin"
212
+ )
213
+ if not os.path.isfile(path_zinit) and "file_z_init" in self.params:
214
+ path_zinit = os.path.join(
215
+ self.folder_base, self.params["file_z_init"]
216
+ )
217
+ self._zinit = read_file_init(path_zinit, self.nx, self.ny)
183
218
  else:
184
- self._zinit = np.squeeze(read_file_bin(path_zinit, self.nx, self.ny))
185
-
186
- def get_temporal_output(self, name, d=None, t=None, **varargs):
187
- """
188
- Read 2D time dependent simulation results.
219
+ self._zinit = np.squeeze(
220
+ read_file_bin(path_zinit, self.nx, self.ny)
221
+ )
222
+
223
+ def _read_from_file(self, name, operator, axis=None, **kwargs):
224
+ res = None
225
+
226
+ if name in ["u", "momentum", "h"]:
227
+ if operator in ["max"] and axis in [None, "t"]:
228
+ file = os.path.join(
229
+ self.folder_output, LOOKUP_NAMES[name] + operator + ".bin"
230
+ )
231
+ d = np.squeeze(read_file_bin(file, self.nx, self.ny))
232
+ res = tilupy.read.StaticResults2D(
233
+ "_".join([name, operator]), d, x=self.x, y=self.y, z=self.z
234
+ )
235
+
236
+ if (name, operator) == ("hu2", "int"):
237
+ array = np.loadtxt(os.path.join(self.folder_output, "ek.d"))
238
+ d = array[:, 1]
239
+ t = array[:, 0]
240
+ res = tilupy.read.TemporalResults0D(name, d, t)
189
241
 
190
- Parameters
191
- ----------
192
- name : str
193
- Name of output.
194
- d : ndarray, optional
195
- Data to be read. If None, will be computed.
196
- The default is None.
197
- t : ndarray, optional
198
- Time of results snapshots (1D array), matching last dimension of d.
199
- If None, will be computed. The default is None.
200
- **varargs : TYPE
201
- DESCRIPTION.
242
+ return res
202
243
 
203
- """
244
+ def _get_output(self, name, **kwargs):
204
245
  # Read thicknesses or velocity components
246
+ d = None
247
+ t = None
248
+ notation = None
249
+
205
250
  if name in STATES_OUTPUT:
206
- file = os.path.join(self.folder_output,
207
- LOOKUP_NAMES[name] + '.bin')
251
+ file = os.path.join(
252
+ self.folder_output, LOOKUP_NAMES[name] + ".bin"
253
+ )
208
254
  d = read_file_bin(file, self.nx, self.ny)
209
- if name == 'hvert':
210
- d = d/self.costh[:, :, np.newaxis]
255
+ if name == "hvert":
256
+ d = d / self.costh[:, :, np.newaxis]
211
257
  t = self.tim
212
-
213
- # Raed integrated kinetic energy
214
- if name in ['hu2int']:
215
- array = np.loadtxt(os.path.join(self.folder_output,
216
- LOOKUP_NAMES[name] + '.d'))
217
- d = array[:, 1]
218
- t = array[:, 0]
219
258
 
220
- # Compute the velocity
221
- if name == 'u':
259
+ if name == "u":
222
260
  d = self.get_u()
223
261
  t = self.tim
224
262
 
225
- if name in ['hu', 'hu2']:
226
- fileh = os.path.join(self.folder_output,
227
- 'rho.bin')
263
+ if name in ["hu", "hu2"]:
264
+ fileh = os.path.join(self.folder_output, "rho.bin")
228
265
  h = read_file_bin(fileh, self.nx, self.ny)
229
266
  u = self.get_u()
230
- if name == 'hu':
231
- d = h*u
232
- elif name == 'hu2':
233
- d = h*u**2
267
+ if name == "hu":
268
+ d = h * u
269
+ elif name == "hu2":
270
+ d = h * u**2
234
271
  t = self.tim
235
-
236
- if 'h_thresh' in varargs and varargs['h_thresh'] is not None and d.ndim==3:
237
- d = tilupy.read.use_thickness_threshold(self, d,
238
- varargs['h_thresh'])
239
-
240
- return tilupy.read.TemporalResults(name, d, t)
241
272
 
242
- def get_static_output(self, name,
243
- d=None, from_file=True, **varargs):
244
- """
245
- Read 2D time dependent simulation results.
273
+ if name in INTEGRATED_OUTPUT:
274
+ array = np.loadtxt(
275
+ os.path.join(self.folder_output, LOOKUP_NAMES[name] + ".d")
276
+ )
277
+ d = array[:, 1]
278
+ if "density" in self.params:
279
+ density = self.params["density"]
280
+ else:
281
+ density = 1
282
+ d = d * density
283
+ t = array[:, 0]
246
284
 
247
- Parameters
248
- ----------
249
- name : str
250
- Name of output.
251
- d : ndarray, optional
252
- Data to be read. Last dimension is for time.
253
- If None, will be computed.
254
- The default is None.
255
- **varargs : TYPE
256
- DESCRIPTION.
285
+ if name in FORCES_OUTPUT:
286
+ file = os.path.join(self.folder_output, name + ".bin")
287
+ d = read_file_bin(file, self.nx, self.ny)
288
+ t = self.tforces
289
+ notation = notations.Notation(
290
+ name,
291
+ long_name=name,
292
+ unit=notations.Unit(Pa=1, kg=-1, m=3),
293
+ symbol=name,
294
+ )
295
+
296
+ if d is None:
297
+ file = os.path.join(self.folder_output, name)
298
+ if os.path.isfile(file + ".bin"):
299
+ d = read_file_bin(file + ".bin", self.nx, self.ny)
300
+ t = self.tim
301
+ elif os.path.isfile(file + ".d"):
302
+ d = np.loadtxt(file + ".d")
303
+
304
+ if (
305
+ "h_thresh" in kwargs
306
+ and kwargs["h_thresh"] is not None
307
+ and d.ndim == 3
308
+ ):
309
+ d = tilupy.read.use_thickness_threshold(
310
+ self, d, kwargs["h_thresh"]
311
+ )
312
+
313
+ if t is None:
314
+ return tilupy.read.AbstractResults(name, d, notation=notation)
257
315
 
258
- """
259
- if name in tilupy.read.COMPUTED_STATIC_DATA_2D:
260
- state, stat = name.split('_')
261
- if stat in ['final', 'initial']:
262
- hh = self.get_temporal_output(state)
263
- if state == 'hvert':
264
- hh.d = hh.d/self.costh[:, :, np.newaxis]
265
- return hh.get_temporal_stat(stat)
266
- if from_file:
267
- file = os.path.join(self.folder_output,
268
- LOOKUP_NAMES[state] + stat + '.bin')
269
- if os.path.isfile(file):
270
- d = np.squeeze(read_file_bin(file, self.nx, self.ny))
271
- else:
272
- print(file + ' was not found, ' + name + '_' + stat
273
- + ' computed from temporal output.')
274
- from_file = False
275
- if not from_file:
276
- data = self.get_temporal_output(state)
277
- if state == 'hvert':
278
- hh.d = hh.d/self.costh
279
- d = data.get_temporal_stat(stat).d
280
316
  else:
281
- raise(NotImplementedError())
282
-
283
- return tilupy.read.StaticResults(name, d)
317
+ if d.ndim == 3:
318
+ return tilupy.read.TemporalResults2D(
319
+ name, d, t, notation=notation, x=self.x, y=self.y, z=self.z
320
+ )
321
+ if d.ndim == 1:
322
+ return tilupy.read.TemporalResults0D(
323
+ name, d, t, notation=notation
324
+ )
325
+
326
+ return None
327
+
328
+ # def get_temporal_output(self, name, d=None, t=None, **varargs):
329
+ # """
330
+ # Read 2D time dependent simulation results.
331
+
332
+ # Parameters
333
+ # ----------
334
+ # name : str
335
+ # Name of output.
336
+ # d : ndarray, optional
337
+ # Data to be read. If None, will be computed.
338
+ # The default is None.
339
+ # t : ndarray, optional
340
+ # Time of results snapshots (1D array), matching last dimension of d.
341
+ # If None, will be computed. The default is None.
342
+ # **varargs : TYPE
343
+ # DESCRIPTION.
344
+
345
+ # """
346
+ # # Read thicknesses or velocity components
347
+ # if name in STATES_OUTPUT:
348
+ # file = os.path.join(
349
+ # self.folder_output, LOOKUP_NAMES[name] + ".bin"
350
+ # )
351
+ # d = read_file_bin(file, self.nx, self.ny)
352
+ # if name == "hvert":
353
+ # d = d / self.costh[:, :, np.newaxis]
354
+ # t = self.tim
355
+
356
+ # # Raed integrated kinetic energy
357
+ # if name in ["hu2_int"]:
358
+ # array = np.loadtxt(
359
+ # os.path.join(self.folder_output, LOOKUP_NAMES[name] + ".d")
360
+ # )
361
+ # d = array[:, 1]
362
+ # t = array[:, 0]
363
+
364
+ # # Compute the velocity
365
+ # if name == "u":
366
+ # d = self.get_u()
367
+ # t = self.tim
368
+
369
+ # if name in ["hu", "hu2"]:
370
+ # fileh = os.path.join(self.folder_output, "rho.bin")
371
+ # h = read_file_bin(fileh, self.nx, self.ny)
372
+ # u = self.get_u()
373
+ # if name == "hu":
374
+ # d = h * u
375
+ # elif name == "hu2":
376
+ # d = h * u**2
377
+ # t = self.tim
378
+
379
+ # if (
380
+ # "h_thresh" in varargs
381
+ # and varargs["h_thresh"] is not None
382
+ # and d.ndim == 3
383
+ # ):
384
+ # d = tilupy.read.use_thickness_threshold(
385
+ # self, d, varargs["h_thresh"]
386
+ # )
387
+
388
+ # return tilupy.read.TemporalResults2D(name, d, t)
389
+
390
+ # def get_static_output(self, name, d=None, from_file=True, **varargs):
391
+ # """
392
+ # Read 2D time dependent simulation results.
393
+
394
+ # Parameters
395
+ # ----------
396
+ # name : str
397
+ # Name of output.
398
+ # d : ndarray, optional
399
+ # Data to be read. Last dimension is for time.
400
+ # If None, will be computed.
401
+ # The default is None.
402
+ # **varargs : TYPE
403
+ # DESCRIPTION.
404
+
405
+ # """
406
+ # if name in tilupy.read.COMPUTED_STATIC_DATA_2D:
407
+ # state, stat = name.split("_")
408
+ # if stat in ["final", "initial"]:
409
+ # hh = self.get_temporal_output(state)
410
+ # if state == "hvert":
411
+ # hh.d = hh.d / self.costh[:, :, np.newaxis]
412
+ # return hh.get_temporal_stat(stat)
413
+ # if from_file:
414
+ # file = os.path.join(
415
+ # self.folder_output, LOOKUP_NAMES[state] + stat + ".bin"
416
+ # )
417
+ # if os.path.isfile(file):
418
+ # d = np.squeeze(read_file_bin(file, self.nx, self.ny))
419
+ # else:
420
+ # print(
421
+ # file
422
+ # + " was not found, "
423
+ # + name
424
+ # + "_"
425
+ # + stat
426
+ # + " computed from temporal output."
427
+ # )
428
+ # from_file = False
429
+ # if not from_file:
430
+ # data = self.get_temporal_output(state)
431
+ # if state == "hvert":
432
+ # hh.d = hh.d / self.costh
433
+ # d = data.get_temporal_stat(stat).d
434
+ # else:
435
+ # raise (NotImplementedError())
436
+
437
+ # return tilupy.read.StaticResults(name, d)
284
438
 
285
439
  def get_u(self):
286
- """ Compute velocity norm from results """
287
- file = os.path.join(self.folder_output,
288
- 'u' + '.bin')
440
+ """Compute velocity norm from results"""
441
+ file = os.path.join(self.folder_output, "u" + ".bin")
289
442
  u = read_file_bin(file, self.nx, self.ny)
290
- file = os.path.join(self.folder_output,
291
- 'ut' + '.bin')
443
+ file = os.path.join(self.folder_output, "ut" + ".bin")
292
444
  ut = read_file_bin(file, self.nx, self.ny)
293
445
 
294
446
  [Fx, Fy] = np.gradient(self.zinit, self.y, self.x)
295
- u = u*self.costh[:, :, np.newaxis]
296
- ut = ut*self.costh[:, :, np.newaxis]
297
- d = np.sqrt(u**2 + ut**2 + (Fx[:, :, np.newaxis]*u
298
- + Fy[:, :, np.newaxis]*ut)**2)
447
+ u = u * self.costh[:, :, np.newaxis]
448
+ ut = ut * self.costh[:, :, np.newaxis]
449
+ d = np.sqrt(
450
+ u**2
451
+ + ut**2
452
+ + (Fx[:, :, np.newaxis] * u + Fy[:, :, np.newaxis] * ut) ** 2
453
+ )
299
454
 
300
455
  return d