Skip to content

Commit f5038ec

Browse files
abulenokslayoo
andauthored
Replace uses of __setitem__ with fill for Storages. closes #1022 (#1014)
Co-authored-by: Sylwester Arabas <[email protected]>
1 parent 10421af commit f5038ec

File tree

8 files changed

+29
-11
lines changed

8 files changed

+29
-11
lines changed

PySDM/backends/impl_numba/storage.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,9 @@ def to_ndarray(self):
187187

188188
def upload(self, data):
189189
np.copyto(self.data, data, casting="safe")
190+
191+
def fill(self, other):
192+
if isinstance(other, Storage):
193+
self.data[:] = other.data
194+
else:
195+
self.data[:] = other

PySDM/backends/impl_thrust_rtc/storage.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,4 +497,17 @@ def divide_if_not_zero(self, divisor):
497497
Impl.divide_if_not_zero(self, divisor)
498498
return self
499499

500+
def fill(self, value):
501+
if isinstance(value, Storage):
502+
trtc.Copy(value.data, self.data)
503+
else:
504+
if isinstance(value, int):
505+
dvalue = trtc.DVInt64(value)
506+
elif isinstance(value, float):
507+
dvalue = BACKEND._get_floating_point(value)
508+
else:
509+
raise TypeError("Only Storage, int and float are supported.")
510+
trtc.Fill(self.data, dvalue)
511+
return self
512+
500513
return Storage

PySDM/dynamics/collisions/breakup_efficiencies/constEb.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ def register(self, builder):
1212
self.particulator = builder.particulator
1313

1414
def __call__(self, output, is_first_in_pair):
15-
output[:] = self.Eb
15+
output.fill(self.Eb)

PySDM/dynamics/collisions/breakup_fragmentations/always_n.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def __init__(self, n, vmin=0.0, nfmax=None):
1111
self.nfmax = nfmax
1212

1313
def __call__(self, nf, frag_size, u01, is_first_in_pair):
14-
nf[:] = self.N
14+
nf.fill(self.N)
1515
frag_size.sum(self.particulator.attributes["volume"], is_first_in_pair)
1616
frag_size /= self.N
1717

PySDM/dynamics/collisions/breakup_fragmentations/straub2010.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __call__(self, nf, frag_size, u01, is_first_in_pair):
4747

4848
# compute the dimensionless numbers and CW=CKE * We
4949
self.arrays["tmp"].sum(self.particulator.attributes["volume"], is_first_in_pair)
50-
self.arrays["Sc"][:] = self.arrays["tmp"][:] # TODO #976
50+
self.arrays["Sc"].fill(self.arrays["tmp"])
5151
self.arrays["Sc"] **= 2 / 3
5252
self.arrays["Sc"] *= (
5353
self.const.PI * self.const.sgm_w * (6 / self.const.PI) ** (2 / 3)
@@ -64,12 +64,12 @@ def __call__(self, nf, frag_size, u01, is_first_in_pair):
6464
self.arrays["CKE"] *= self.arrays["tmp2"]
6565
self.arrays["CKE"] *= self.const.rho_w
6666

67-
self.arrays["We"][:] = self.arrays["CKE"][:] # TODO #976
67+
self.arrays["We"].fill(self.arrays["CKE"])
6868
self.arrays["We"].divide_if_not_zero(self.arrays["Sc"])
6969

70-
self.arrays["CW"][:] = self.arrays["We"][:] # TODO #976
70+
self.arrays["CW"].fill(self.arrays["We"])
7171
self.arrays["CW"] *= self.arrays["CKE"]
72-
self.arrays["CW"] /= si.joule * 1e-6 # convert to µJ
72+
self.arrays["CW"] /= si.uJ
7373

7474
self.arrays["gam"].max(self.particulator.attributes["radius"], is_first_in_pair)
7575
self.arrays["tmp"].min(self.particulator.attributes["radius"], is_first_in_pair)

PySDM/dynamics/collisions/coalescence_efficiencies/constEc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ def register(self, builder):
1010
self.particulator = builder.particulator
1111

1212
def __call__(self, output, is_first_in_pair):
13-
output[:] = self.Ec
13+
output.fill(self.Ec)

PySDM/dynamics/collisions/coalescence_efficiencies/straub2010.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ def register(self, builder):
2525

2626
def __call__(self, output, is_first_in_pair):
2727
self.arrays["tmp"].sum(self.particulator.attributes["volume"], is_first_in_pair)
28-
self.arrays["Sc"][:] = self.arrays["tmp"][:]
28+
self.arrays["Sc"].fill(self.arrays["tmp"])
2929
self.arrays["tmp"] *= 2
3030

3131
self.arrays["tmp2"].distance(
@@ -47,4 +47,4 @@ def __call__(self, output, is_first_in_pair):
4747
self.arrays["We"].divide_if_not_zero(self.arrays["Sc"])
4848
self.arrays["We"] *= -1.15
4949

50-
output[:] = np.exp(self.arrays["We"])
50+
output[:] = np.exp(self.arrays["We"]) # TODO #976

PySDM/dynamics/collisions/collision_kernels/constantK.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,7 @@ def __init__(self, a):
99
self.particulator = None
1010

1111
def __call__(self, output, is_first_in_pair):
12-
output *= 0
13-
output += self.a
12+
output.fill(self.a)
1413

1514
def register(self, builder):
1615
self.particulator = builder.particulator

0 commit comments

Comments
 (0)