ladim 2.3.2__tar.gz → 2.3.4__tar.gz
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.
- {ladim-2.3.2 → ladim-2.3.4}/PKG-INFO +1 -1
- {ladim-2.3.2 → ladim-2.3.4}/ladim/__init__.py +1 -1
- {ladim-2.3.2 → ladim-2.3.4}/ladim/gridforce/ROMS.py +2 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/release.py +59 -43
- ladim-2.3.4/ladim/state.py +192 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim.egg-info/PKG-INFO +1 -1
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_release.py +26 -0
- ladim-2.3.2/ladim/state.py +0 -120
- {ladim-2.3.2 → ladim-2.3.4}/LICENSE +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/README.md +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/__main__.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/cli.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/config.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/forcing.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/grid.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/gridforce/__init__.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/gridforce/analytical.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/gridforce/zROMS.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/ibms/__init__.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/ibms/light.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/model.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/output.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/plugins/__init__.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/sample.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/schema.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/solver.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/tracker.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim/utilities.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim.egg-info/SOURCES.txt +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim.egg-info/dependency_links.txt +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim.egg-info/entry_points.txt +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim.egg-info/requires.txt +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/ladim.egg-info/top_level.txt +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/postladim/__init__.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/postladim/cellcount.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/postladim/kde_plot.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/postladim/particlefile.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/postladim/variable.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/pyproject.toml +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/setup.cfg +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_config.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_forcing.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_grid.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_ladim.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_output.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_schema.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_solver.py +0 -0
- {ladim-2.3.2 → ladim-2.3.4}/tests/test_utilities.py +0 -0
|
@@ -327,6 +327,8 @@ class Forcing:
|
|
|
327
327
|
self.U = self.Unew
|
|
328
328
|
self.V = self.Vnew
|
|
329
329
|
self.Unew, self.Vnew = self._read_velocity(t1)
|
|
330
|
+
self.dU = (self.Unew - self.U) / (t1 - t0)
|
|
331
|
+
self.dV = (self.Vnew - self.V) / (t1 - t0)
|
|
330
332
|
for name in self.ibm_forcing:
|
|
331
333
|
self[name] = self[name + "new"]
|
|
332
334
|
self[name + "new"] = self._read_field(name, t1)
|
|
@@ -21,11 +21,15 @@ class Releaser:
|
|
|
21
21
|
|
|
22
22
|
A schedule is a table of release times and release intervals, together
|
|
23
23
|
with initial properties of released particles. Each row represents a
|
|
24
|
-
particle source.
|
|
24
|
+
particle source. It is assumed that the table is sorted by
|
|
25
|
+
`release_start`, and that `release_stop` of each line never exceeds the
|
|
26
|
+
release_start value of the next line.
|
|
27
|
+
|
|
28
|
+
Some columns have special meanings:
|
|
25
29
|
|
|
26
30
|
release_start
|
|
27
|
-
First release time of this particle type, in seconds since the
|
|
28
|
-
epoch (1970-01-01).
|
|
31
|
+
First release time of this particle type, in seconds since the
|
|
32
|
+
posix epoch (1970-01-01).
|
|
29
33
|
|
|
30
34
|
release_stop
|
|
31
35
|
Stop time (not inclusive) for this particle source, in seconds
|
|
@@ -94,6 +98,7 @@ class Releaser:
|
|
|
94
98
|
df = add_start_stop_step_to_release_table(df)
|
|
95
99
|
df = apply_warm_start_file(df, file=warm_start_file)
|
|
96
100
|
df = replace_lonlat_in_release_table(df, lonlat_converter)
|
|
101
|
+
df = df.sort_values('release_time')
|
|
97
102
|
releaser = Releaser(df)
|
|
98
103
|
|
|
99
104
|
return releaser
|
|
@@ -149,9 +154,9 @@ class Releaser:
|
|
|
149
154
|
return
|
|
150
155
|
|
|
151
156
|
# Add new particles
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
state.append(new_particles)
|
|
157
|
+
colnames = df.columns.tolist()
|
|
158
|
+
new_particles = {k: df[k].to_numpy() for k in colnames}
|
|
159
|
+
model.state.append(new_particles)
|
|
155
160
|
|
|
156
161
|
|
|
157
162
|
def load_release_file(stream, names: list, formats: dict) -> pd.DataFrame:
|
|
@@ -249,9 +254,10 @@ def add_start_stop_step_to_release_table(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
249
254
|
step = np.full(start.shape, fill_value=max_step, dtype='int64')
|
|
250
255
|
|
|
251
256
|
# Define stop times for release events
|
|
257
|
+
# Every time there is a new release, all previous continuous releases stop
|
|
252
258
|
unq_start, unq_start_inv = np.unique(start, return_inverse=True)
|
|
253
259
|
unq_stop = np.roll(unq_start, -1)
|
|
254
|
-
if len(unq_stop):
|
|
260
|
+
if len(unq_stop): # The last stop time is set to be "infinitely" large
|
|
255
261
|
unq_stop[-1] = np.iinfo(unq_stop.dtype).max
|
|
256
262
|
stop = unq_stop[unq_start_inv]
|
|
257
263
|
|
|
@@ -264,49 +270,58 @@ def truncate_schedule_period(
|
|
|
264
270
|
t2: int | None = None,
|
|
265
271
|
) -> pd.DataFrame:
|
|
266
272
|
"""
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
release_step
|
|
271
|
-
|
|
272
|
-
the
|
|
273
|
+
Return a release schedule truncated to the requested time interval.
|
|
274
|
+
|
|
275
|
+
The input table is a pandas data frame containing the columns
|
|
276
|
+
``release_start``, ``release_stop`` and ``release_step``. The function
|
|
277
|
+
removes rows whose release interval falls outside ``[t1, t2)`` and clamps
|
|
278
|
+
the surviving row boundaries to that interval, while preserving the
|
|
279
|
+
schedule columns needed for later expansion.
|
|
280
|
+
|
|
281
|
+
:param df: Input release schedule as a pandas data frame.
|
|
282
|
+
:param t1: Optional start time of the interval in POSIX seconds. If
|
|
283
|
+
omitted, the first scheduled release start is used.
|
|
284
|
+
:param t2: Optional stop time of the interval in POSIX seconds. If
|
|
285
|
+
omitted, the last scheduled release stop is used.
|
|
286
|
+
:returns: A truncated copy of the schedule with rows outside the interval
|
|
287
|
+
removed and the relevant schedule bounds clipped to ``t1``/``t2``.
|
|
288
|
+
|
|
289
|
+
.. note::
|
|
290
|
+
The returned frame keeps the original schedule columns and is suitable
|
|
291
|
+
for downstream expansion with ``expand_schedule_range``.
|
|
273
292
|
"""
|
|
274
293
|
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if t2 is not None:
|
|
278
|
-
df = _truncate_stop(df, t2)
|
|
279
|
-
|
|
280
|
-
return df
|
|
281
|
-
|
|
294
|
+
release_start = df['release_start'].to_numpy()
|
|
295
|
+
release_stop = df['release_stop'].to_numpy()
|
|
282
296
|
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
idx = df['release_stop'].values > t1
|
|
286
|
-
df_subset = df.loc[idx].copy(deep=True)
|
|
287
|
-
|
|
288
|
-
start = df_subset['release_start'].values
|
|
289
|
-
step = df_subset['release_step'].values
|
|
297
|
+
start = release_start[0] if t1 is None else t1
|
|
298
|
+
stop = release_stop[-1] if t2 is None else t2
|
|
290
299
|
|
|
291
|
-
#
|
|
292
|
-
|
|
293
|
-
new_start = reset_range_start(start[idx], step[idx], t1)
|
|
294
|
-
df_subset.loc[idx, 'release_start'] = new_start
|
|
300
|
+
# First row whose release_stop is greater than start.
|
|
301
|
+
first = np.searchsorted(release_stop, start, side="right")
|
|
295
302
|
|
|
296
|
-
|
|
303
|
+
# First row whose release_start is greater than or equal to stop.
|
|
304
|
+
last = np.searchsorted(release_start, stop, side="left")
|
|
297
305
|
|
|
306
|
+
if first >= last:
|
|
307
|
+
return df.iloc[0:0].copy()
|
|
298
308
|
|
|
299
|
-
def _truncate_stop(df, t2):
|
|
300
309
|
# Remove irrelevant rows
|
|
301
|
-
|
|
302
|
-
|
|
310
|
+
df = df.iloc[first:last].copy()
|
|
311
|
+
|
|
312
|
+
# Truncate start times if necessary
|
|
313
|
+
new_start = df['release_start'].to_numpy().copy()
|
|
314
|
+
new_step = df['release_step'].to_numpy()
|
|
315
|
+
idx = new_start < start
|
|
316
|
+
new_start[idx] = reset_range_start(new_start[idx], new_step[idx], start)
|
|
303
317
|
|
|
304
318
|
# Truncate stop times if necessary
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
319
|
+
new_stop = np.minimum(df['release_stop'].to_numpy(), stop)
|
|
320
|
+
|
|
321
|
+
df['release_start'] = new_start
|
|
322
|
+
df['release_stop'] = new_stop
|
|
308
323
|
|
|
309
|
-
return
|
|
324
|
+
return df
|
|
310
325
|
|
|
311
326
|
|
|
312
327
|
def expand_schedule_range(df: pd.DataFrame) -> pd.DataFrame:
|
|
@@ -321,9 +336,9 @@ def expand_schedule_range(df: pd.DataFrame) -> pd.DataFrame:
|
|
|
321
336
|
with a single release_time column.
|
|
322
337
|
"""
|
|
323
338
|
|
|
324
|
-
start = df['release_start'].
|
|
325
|
-
stop = df['release_stop'].
|
|
326
|
-
step = df['release_step'].
|
|
339
|
+
start = df['release_start'].to_numpy()
|
|
340
|
+
stop = df['release_stop'].to_numpy()
|
|
341
|
+
step = df['release_step'].to_numpy()
|
|
327
342
|
num = np.maximum(np.ceil((stop - start) / step).astype('int64'), 0)
|
|
328
343
|
seq = [i for n in num for i in range(n)]
|
|
329
344
|
idx = np.repeat(np.arange(len(num)), num)
|
|
@@ -433,7 +448,8 @@ def apply_warm_start_file(
|
|
|
433
448
|
warm_start_values = warm_start_particles[colname].to_numpy()
|
|
434
449
|
if np.issubdtype(warm_start_values.dtype, np.datetime64):
|
|
435
450
|
warm_start_values = warm_start_values.astype('datetime64[s]')
|
|
436
|
-
|
|
451
|
+
target_dtype = np.dtype(str(warm_df[colname].dtype))
|
|
452
|
+
warm_df.loc[:, colname] = warm_start_values.astype(target_dtype)
|
|
437
453
|
warm_df['release_start'] = new_start_time.astype(np.int64)
|
|
438
454
|
warm_df['release_stop'] = np.iinfo(df['release_stop'].dtype).max # type: ignore
|
|
439
455
|
warm_df['release_step'] = 60 * 60 * 24 * 366 * 1_000_000
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class State:
|
|
5
|
+
"""
|
|
6
|
+
The state module contains static and dynamic particle properties
|
|
7
|
+
|
|
8
|
+
The other modules interact with the state module mostly through
|
|
9
|
+
the getitem and setitem methods. For instance, to increase the
|
|
10
|
+
depth of all particles by 1, use state['Z'] += 1
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def __init__(self):
|
|
14
|
+
self._num_released = 0
|
|
15
|
+
self._varnames = set()
|
|
16
|
+
self._data = dict() # type: dict[str, np.ndarray]
|
|
17
|
+
|
|
18
|
+
@property
|
|
19
|
+
def size(self):
|
|
20
|
+
"""
|
|
21
|
+
Current number of particles
|
|
22
|
+
"""
|
|
23
|
+
keys = list(self._data.keys())
|
|
24
|
+
if len(keys) == 0:
|
|
25
|
+
return 0
|
|
26
|
+
return len(self._data[keys[0]])
|
|
27
|
+
|
|
28
|
+
@property
|
|
29
|
+
def released(self):
|
|
30
|
+
"""
|
|
31
|
+
Total number of released particles
|
|
32
|
+
"""
|
|
33
|
+
return self._num_released
|
|
34
|
+
|
|
35
|
+
def append(self, particles: dict):
|
|
36
|
+
"""
|
|
37
|
+
Add new particles
|
|
38
|
+
|
|
39
|
+
Missing variables are assigned a default value of 0.
|
|
40
|
+
|
|
41
|
+
:param particles: A mapping from variable names to values
|
|
42
|
+
"""
|
|
43
|
+
# If there are no new particles, do nothing
|
|
44
|
+
if not particles:
|
|
45
|
+
return
|
|
46
|
+
|
|
47
|
+
# Check that input has correct format
|
|
48
|
+
num_new = next(len(v) for v in particles.values())
|
|
49
|
+
fields = {} # type: dict[str, np.ndarray]
|
|
50
|
+
for k, v in particles.items():
|
|
51
|
+
fields[k] = np.asarray(v)
|
|
52
|
+
if not np.shape(fields[k]) == (num_new, ):
|
|
53
|
+
raise ValueError('Unequal number of array elements in input')
|
|
54
|
+
|
|
55
|
+
_add_standard_variables(fields, first_pid=self._num_released)
|
|
56
|
+
|
|
57
|
+
self._data = _append_fields(self._data, fields)
|
|
58
|
+
self._num_released += num_new
|
|
59
|
+
|
|
60
|
+
def remove(self, particles):
|
|
61
|
+
"""
|
|
62
|
+
Remove particles
|
|
63
|
+
|
|
64
|
+
:param particles: Boolean index of particles to remove
|
|
65
|
+
:return:
|
|
66
|
+
"""
|
|
67
|
+
if not np.any(particles):
|
|
68
|
+
return
|
|
69
|
+
|
|
70
|
+
keep = ~particles
|
|
71
|
+
for k in self._data.keys():
|
|
72
|
+
self._data[k] = self._data[k][keep]
|
|
73
|
+
|
|
74
|
+
def __getitem__(self, item):
|
|
75
|
+
return self._data[item]
|
|
76
|
+
|
|
77
|
+
def __setitem__(self, item, value):
|
|
78
|
+
v = np.asarray(value)
|
|
79
|
+
assert v.shape == (self.size, )
|
|
80
|
+
self._data[item] = v
|
|
81
|
+
|
|
82
|
+
def __len__(self):
|
|
83
|
+
return self.size
|
|
84
|
+
|
|
85
|
+
def __contains__(self, item):
|
|
86
|
+
return item in self._data
|
|
87
|
+
|
|
88
|
+
def __getattr__(self, item):
|
|
89
|
+
if item not in self:
|
|
90
|
+
raise AttributeError(f'Attribute not defined: {item}')
|
|
91
|
+
return self[item]
|
|
92
|
+
|
|
93
|
+
def __setattr__(self, item, value):
|
|
94
|
+
excepted_values = [
|
|
95
|
+
'_data', '_model', '_num_released', '_varnames', 'dt', 'timestep',
|
|
96
|
+
'timestamp', 'released'
|
|
97
|
+
]
|
|
98
|
+
if item in list(self.__dict__.keys()) + excepted_values:
|
|
99
|
+
super().__setattr__(item, value)
|
|
100
|
+
elif item in self._data:
|
|
101
|
+
self._data[item] = value
|
|
102
|
+
else:
|
|
103
|
+
raise AttributeError(f"Attribute not defined: '{item}'")
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _add_standard_variables(fields: dict[str, np.ndarray], first_pid: int):
|
|
107
|
+
"""
|
|
108
|
+
Add the standard particle metadata fields ``pid``, ``alive`` and
|
|
109
|
+
``active`` in-place to a mapping of one-dimensional arrays.
|
|
110
|
+
|
|
111
|
+
The input dictionary maps variable names to NumPy arrays, all of which are
|
|
112
|
+
assumed to have the same length ``num_new``. The helper mutates that
|
|
113
|
+
dictionary by adding the missing standard variables and normalizing the
|
|
114
|
+
``active`` field to a boolean array.
|
|
115
|
+
|
|
116
|
+
:param fields: Mapping from field names to one-dimensional NumPy arrays.
|
|
117
|
+
The arrays store values for the same set of newly released particles.
|
|
118
|
+
:param first_pid: The first particle identifier to assign. The new
|
|
119
|
+
``pid`` field is created as ``np.arange(num_new) + first_pid``.
|
|
120
|
+
:returns: The same dictionary object, updated in-place with the new
|
|
121
|
+
standard variables ``pid``, ``alive`` and ``active``.
|
|
122
|
+
|
|
123
|
+
.. note::
|
|
124
|
+
``alive`` is always created as a boolean array of ones, and ``active``
|
|
125
|
+
is either converted from an existing array to boolean or created as an
|
|
126
|
+
array of ones if the input dictionary does not already provide it.
|
|
127
|
+
"""
|
|
128
|
+
num_new = next(len(v) for v in fields.values())
|
|
129
|
+
|
|
130
|
+
# Add standard variables
|
|
131
|
+
fields['pid'] = np.arange(num_new) + first_pid
|
|
132
|
+
fields['alive'] = np.ones(num_new, dtype=bool)
|
|
133
|
+
if 'active' in fields:
|
|
134
|
+
fields['active'] = np.array(fields['active'], dtype=bool)
|
|
135
|
+
else:
|
|
136
|
+
fields['active'] = np.ones(num_new, dtype=bool)
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _append_fields(
|
|
140
|
+
oldf: dict[str, np.ndarray],
|
|
141
|
+
newf: dict[str, np.ndarray]):
|
|
142
|
+
"""
|
|
143
|
+
Append two field dictionaries by concatenating their per-variable arrays.
|
|
144
|
+
|
|
145
|
+
The input dictionaries map a field name to a one-dimensional NumPy array.
|
|
146
|
+
Each array stores particles for that variable, and the key sets are allowed
|
|
147
|
+
to differ: the output contains the union of keys from ``oldf`` and ``newf``.
|
|
148
|
+
For any variable present only in one input mapping, the missing side is
|
|
149
|
+
padded with a zero-filled array of the appropriate dtype and length before
|
|
150
|
+
the concatenation.
|
|
151
|
+
|
|
152
|
+
:param oldf: Existing field mapping. The keys are variable names, and each
|
|
153
|
+
value is a one-dimensional array of shape ``(n_old,)``.
|
|
154
|
+
:param newf: New field mapping. The keys are variable names, and each
|
|
155
|
+
value is a one-dimensional array of shape ``(n_new,)``.
|
|
156
|
+
:returns: A new dictionary whose keys are the union of ``oldf`` and
|
|
157
|
+
``newf`` keys, and whose arrays have the shape ``(n_old + n_new,)``.
|
|
158
|
+
Fields that only exist in either input are appended as zeros on the
|
|
159
|
+
missing side before concatenation.
|
|
160
|
+
|
|
161
|
+
.. note::
|
|
162
|
+
This helper is used to build the particle state ``State._data`` by
|
|
163
|
+
appending newly released particles to the previously stored particle
|
|
164
|
+
fields.
|
|
165
|
+
"""
|
|
166
|
+
|
|
167
|
+
# Find columns of the new field set
|
|
168
|
+
old_cols = list(oldf)
|
|
169
|
+
extra_cols = [c for c in newf if c not in old_cols]
|
|
170
|
+
cols = old_cols + extra_cols
|
|
171
|
+
|
|
172
|
+
num_old = 0 if len(oldf) == 0 else next(len(v) for v in oldf.values())
|
|
173
|
+
num_new = 0 if len(newf) == 0 else next(len(v) for v in newf.values())
|
|
174
|
+
num_total = num_old + num_new
|
|
175
|
+
|
|
176
|
+
# Concatenate old and new particles
|
|
177
|
+
newdata = {} # type: dict[str, np.ndarray]
|
|
178
|
+
for c in cols:
|
|
179
|
+
dtype = oldf[c].dtype if c in oldf else newf[c].dtype
|
|
180
|
+
newdata_c = np.empty(num_total, dtype=dtype)
|
|
181
|
+
if c in oldf:
|
|
182
|
+
newdata_c[:num_old] = oldf[c]
|
|
183
|
+
else:
|
|
184
|
+
newdata_c[:num_old] = 0
|
|
185
|
+
if c in newf:
|
|
186
|
+
newdata_c[num_old:] = newf[c]
|
|
187
|
+
else:
|
|
188
|
+
newdata_c[num_old:] = 0
|
|
189
|
+
|
|
190
|
+
newdata[c] = newdata_c
|
|
191
|
+
|
|
192
|
+
return newdata
|
|
@@ -116,6 +116,32 @@ class Test_add_start_stop_step_to_release_table:
|
|
|
116
116
|
|
|
117
117
|
|
|
118
118
|
class Test_truncate_schedule:
|
|
119
|
+
def test_works_as_expected(self):
|
|
120
|
+
tab = pd.DataFrame(
|
|
121
|
+
[[0, -1, 0, 1], # One particle with release at -1
|
|
122
|
+
[0, 0, 3, 1], # Three particles with release at 0, 1, 2
|
|
123
|
+
[1, 0, 3, 1],
|
|
124
|
+
[2, 0, 3, 1],
|
|
125
|
+
[3, 4, 9, 2], # One particle with release at 4, 6, 8
|
|
126
|
+
[4, 10, 17, 3], # Two particles with release at 10, 13, 16
|
|
127
|
+
[5, 10, 17, 3],
|
|
128
|
+
[6, 17, 19, 1], # One particle with release at 17, 18
|
|
129
|
+
],
|
|
130
|
+
columns=['prop', 'release_start', 'release_stop', 'release_step']
|
|
131
|
+
)
|
|
132
|
+
|
|
133
|
+
result = release.truncate_schedule_period(tab, 1, 16)
|
|
134
|
+
assert result.to_numpy().tolist() == [
|
|
135
|
+
# First particle is gone - scheduled outside interval
|
|
136
|
+
[0, 1, 3, 1], # Next three particles get truncated start time
|
|
137
|
+
[1, 1, 3, 1],
|
|
138
|
+
[2, 1, 3, 1],
|
|
139
|
+
[3, 4, 9, 2], # Middle particle is untouched
|
|
140
|
+
[4, 10, 16, 3], # Two particles with truncated stop time
|
|
141
|
+
[5, 10, 16, 3],
|
|
142
|
+
# Last particle gone - scheduled outside interval
|
|
143
|
+
]
|
|
144
|
+
|
|
119
145
|
def test_can_truncate(self):
|
|
120
146
|
tab = pd.DataFrame({
|
|
121
147
|
'release_start': [0, 0, 15],
|
ladim-2.3.2/ladim/state.py
DELETED
|
@@ -1,120 +0,0 @@
|
|
|
1
|
-
import numpy as np
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
class State:
|
|
5
|
-
"""
|
|
6
|
-
The state module contains static and dynamic particle properties
|
|
7
|
-
|
|
8
|
-
The other modules interact with the state module mostly through
|
|
9
|
-
the getitem and setitem methods. For instance, to increase the
|
|
10
|
-
depth of all particles by 1, use state['Z'] += 1
|
|
11
|
-
"""
|
|
12
|
-
|
|
13
|
-
def __init__(self):
|
|
14
|
-
self._num_released = 0
|
|
15
|
-
self._varnames = set()
|
|
16
|
-
self._data = dict() # type: dict[str, np.ndarray]
|
|
17
|
-
|
|
18
|
-
@property
|
|
19
|
-
def size(self):
|
|
20
|
-
"""
|
|
21
|
-
Current number of particles
|
|
22
|
-
"""
|
|
23
|
-
keys = list(self._data.keys())
|
|
24
|
-
if len(keys) == 0:
|
|
25
|
-
return 0
|
|
26
|
-
return len(self._data[keys[0]])
|
|
27
|
-
|
|
28
|
-
@property
|
|
29
|
-
def released(self):
|
|
30
|
-
"""
|
|
31
|
-
Total number of released particles
|
|
32
|
-
"""
|
|
33
|
-
return self._num_released
|
|
34
|
-
|
|
35
|
-
def append(self, particles: dict):
|
|
36
|
-
"""
|
|
37
|
-
Add new particles
|
|
38
|
-
|
|
39
|
-
Missing variables are assigned a default value of 0.
|
|
40
|
-
|
|
41
|
-
:param particles: A mapping from variable names to values
|
|
42
|
-
"""
|
|
43
|
-
# If there are no new particles, do nothing
|
|
44
|
-
if not particles:
|
|
45
|
-
return
|
|
46
|
-
|
|
47
|
-
# Check that input has correct format
|
|
48
|
-
num_new = next(len(v) for v in particles.values())
|
|
49
|
-
fields = {} # type: dict[str, np.ndarray]
|
|
50
|
-
for k, v in particles.items():
|
|
51
|
-
fields[k] = np.asarray(v)
|
|
52
|
-
if not np.shape(fields[k]) == (num_new, ):
|
|
53
|
-
raise ValueError('Unequal number of array elements in input')
|
|
54
|
-
|
|
55
|
-
# Add standard variables
|
|
56
|
-
fields['pid'] = np.arange(num_new) + self._num_released
|
|
57
|
-
fields['alive'] = np.ones(num_new, dtype=bool)
|
|
58
|
-
if 'active' in fields:
|
|
59
|
-
fields['active'] = np.array(fields['active'], dtype=bool)
|
|
60
|
-
else:
|
|
61
|
-
fields['active'] = np.ones(num_new, dtype=bool)
|
|
62
|
-
|
|
63
|
-
# Concatenate old and new particles
|
|
64
|
-
newdata = {} # type: dict[str, np.ndarray]
|
|
65
|
-
num_old = self.size
|
|
66
|
-
for k, v_new in fields.items():
|
|
67
|
-
if k in self._data:
|
|
68
|
-
v_old = self._data[k]
|
|
69
|
-
else:
|
|
70
|
-
v_old = np.zeros(num_old, dtype=v_new.dtype)
|
|
71
|
-
|
|
72
|
-
newdata[k] = np.concatenate([v_old, v_new], dtype=v_old.dtype)
|
|
73
|
-
|
|
74
|
-
self._data = newdata
|
|
75
|
-
self._num_released += num_new
|
|
76
|
-
|
|
77
|
-
def remove(self, particles):
|
|
78
|
-
"""
|
|
79
|
-
Remove particles
|
|
80
|
-
|
|
81
|
-
:param particles: Boolean index of particles to remove
|
|
82
|
-
:return:
|
|
83
|
-
"""
|
|
84
|
-
if not np.any(particles):
|
|
85
|
-
return
|
|
86
|
-
|
|
87
|
-
keep = ~particles
|
|
88
|
-
for k in self._data.keys():
|
|
89
|
-
self._data[k] = self._data[k][keep]
|
|
90
|
-
|
|
91
|
-
def __getitem__(self, item):
|
|
92
|
-
return self._data[item]
|
|
93
|
-
|
|
94
|
-
def __setitem__(self, item, value):
|
|
95
|
-
v = np.asarray(value)
|
|
96
|
-
assert v.shape == (self.size, )
|
|
97
|
-
self._data[item] = v
|
|
98
|
-
|
|
99
|
-
def __len__(self):
|
|
100
|
-
return self.size
|
|
101
|
-
|
|
102
|
-
def __contains__(self, item):
|
|
103
|
-
return item in self._data
|
|
104
|
-
|
|
105
|
-
def __getattr__(self, item):
|
|
106
|
-
if item not in self:
|
|
107
|
-
raise AttributeError(f'Attribute not defined: {item}')
|
|
108
|
-
return self[item]
|
|
109
|
-
|
|
110
|
-
def __setattr__(self, item, value):
|
|
111
|
-
excepted_values = [
|
|
112
|
-
'_data', '_model', '_num_released', '_varnames', 'dt', 'timestep',
|
|
113
|
-
'timestamp', 'released'
|
|
114
|
-
]
|
|
115
|
-
if item in list(self.__dict__.keys()) + excepted_values:
|
|
116
|
-
super().__setattr__(item, value)
|
|
117
|
-
elif item in self._data:
|
|
118
|
-
self._data[item] = value
|
|
119
|
-
else:
|
|
120
|
-
raise AttributeError(f"Attribute not defined: '{item}'")
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|