-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introducing @register_dynamic decorator to solve dynamic-reuse issue
- Loading branch information
Showing
13 changed files
with
88 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,3 @@ | ||
""" stuff not intended to be imported from user code """ | ||
|
||
from .register_dynamic import register_dynamic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
""" decorator for dynamics classes | ||
ensuring that their instances can be re-used with multiple builders """ | ||
|
||
from copy import deepcopy | ||
|
||
|
||
def _instantiate(self, *, builder): | ||
copy = deepcopy(self) | ||
copy.register(builder=builder) | ||
return copy | ||
|
||
|
||
def register_dynamic(): | ||
def decorator(cls): | ||
if hasattr(cls, "instantiate"): | ||
assert cls.instantiate is _instantiate | ||
else: | ||
setattr(cls, "instantiate", _instantiate) | ||
return cls | ||
|
||
return decorator |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
""" checks if @register_product makes dynamics instances reusable """ | ||
|
||
import numpy as np | ||
|
||
from PySDM import Builder | ||
from PySDM.backends import CPU | ||
from PySDM.environments import Box | ||
from PySDM.dynamics.impl import register_dynamic | ||
|
||
|
||
def test_impl_register_dynamic(): | ||
# arrange | ||
@register_dynamic() | ||
class Dynamic: | ||
def __init__(self): | ||
self.particulator = None | ||
|
||
def register(self, *, builder: Builder): | ||
self.particulator = builder.particulator | ||
|
||
dynamic = Dynamic() | ||
kwargs = {"n_sd": 0, "backend": CPU(), "environment": Box(dt=0, dv=0)} | ||
builders = [Builder(**kwargs), Builder(**kwargs)] | ||
|
||
# act | ||
for builder in builders: | ||
builder.add_dynamic(dynamic) | ||
builder.build( | ||
attributes={"multiplicity": np.empty(0), "water mass": np.empty(0)} | ||
) | ||
|
||
# assert | ||
assert dynamic.particulator is None | ||
assert builders[0].particulator is not builders[1].particulator | ||
for builder in builders: | ||
assert ( | ||
builder.particulator.dynamics["Dynamic"].particulator | ||
is builder.particulator | ||
) |