forked from open-atmos/PySDM
-
Notifications
You must be signed in to change notification settings - Fork 0
Add Spontaneous Breakup #16
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
jb-mackay
wants to merge
12
commits into
master
Choose a base branch
from
bm/spont_breakup
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
be2b340
Initial aoutline of spontaneous breakup.
jb-mackay 9358d47
Add spontaneous breakup mechanism
jb-mackay e961da7
Add spontaneous breakup functions to physics
jb-mackay e4b0404
Add spont breakup rates to physics.
jb-mackay 0dba554
Create compuute_gamma stub.
jb-mackay b1cce54
Add broken spontaneous breakup test.
jb-mackay 286b50d
Debugging with edejong
jb-mackay 84bb721
EJ: Reformulated spont_break rate as a probability with dt, and rand …
e79b475
Add exponential fragmentation rate, as in Kamra 1991
8d211cd
Added exponential-cubic fragmentation function, as in Kamra 1991. Cha…
afcb183
Minor documentation additions
jb-mackay fe091cb
Add basic docstrings to modules and classes.
jb-mackay File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,143 @@ | ||
| """ | ||
| Single particle spontaneous breakup into uniformly sized fragments. | ||
|
|
||
| Created at 10.05.21 by jb-mackay & edejong | ||
| """ | ||
|
|
||
| import numpy as np | ||
| from PySDM.physics import si | ||
| from PySDM.dynamics.impl.random_generator_optimizer import RandomGeneratorOptimizer | ||
| from PySDM.dynamics.impl.random_generator_optimizer_nopair import RandomGeneratorOptimizerNoPair | ||
| import warnings | ||
|
|
||
| # checks that timestep is within this range | ||
| default_dt_coal_range = (.1 * si.second, 100 * si.second) | ||
|
|
||
| # TODO: define some a rate function | ||
| class SpontaneousBreakup: | ||
|
|
||
| def __init__(self, | ||
| breakup_rate, # rate function | ||
| fragmentation, # frag function | ||
| seed=None, | ||
| croupier=None, | ||
| optimized_random=False, | ||
| substeps: int = 1, | ||
| adaptive: bool = False, | ||
| dt_coal_range=default_dt_coal_range | ||
| ): | ||
| assert substeps == 1 or adaptive is False | ||
|
|
||
| self.core = None # .core is the parent for all attributes, dynamics, etc | ||
| self.enable = True # on/off for the dynamic | ||
|
|
||
| self.breakup_rate = breakup_rate | ||
| self.fragmentation = fragmentation | ||
| self.seed = seed | ||
|
|
||
| assert dt_coal_range[0] > 0 | ||
| self.croupier = croupier | ||
| self.optimized_random = optimized_random | ||
| self.__substeps = substeps | ||
| self.adaptive = adaptive | ||
| self.stats_n_substep = None | ||
| self.stats_dt_min = None | ||
| self.dt_coal_range = tuple(dt_coal_range) | ||
|
|
||
| # temporary storage for outputs (may need add some (and remove)) | ||
| self.n_fragment = None | ||
| self.breakup_rate_temp = None | ||
| self.norm_factor_temp = None # renormalizes the system | ||
| self.prob = None | ||
| self.dt_left = None | ||
|
|
||
| # simulation diagnositcs (TODO) | ||
|
|
||
|
|
||
| # registering the process with the builder (initialization) | ||
| def register(self, builder): | ||
| self.core = builder.core | ||
|
|
||
| # outputs a random number (will need one for IF breakup? and for fragmentation number) | ||
| self.rnd_opt = RandomGeneratorOptimizerNoPair(optimized_random=self.optimized_random, | ||
| dt_min=self.dt_coal_range[0], | ||
| seed=self.seed) #self.core.formulae.seed+1) | ||
| self.rnd_opt_frag = RandomGeneratorOptimizerNoPair(optimized_random=self.optimized_random, | ||
| dt_min=self.dt_coal_range[0], | ||
| seed=self.seed) #self.core.formulae.seed+1) | ||
|
|
||
| self.optimised_random = None | ||
|
|
||
| if self.dt_coal_range[1] > self.core.dt: | ||
| self.dt_coal_range = (self.dt_coal_range[0], self.core.dt) | ||
| assert self.dt_coal_range[0] <= self.dt_coal_range[1] | ||
|
|
||
| # init temporary storage; swap to Storage (see Condensation) | ||
| self.n_fragment = self.core.Storage.empty(self.core.n_sd, dtype=float) | ||
| self.breakup_rate_temp = self.core.Storage.empty(self.core.n_sd, dtype=float) | ||
| self.norm_factor_temp = self.core.Storage.empty(self.core.mesh.n_cell, dtype=float) | ||
| self.prob = self.core.Storage.empty(self.core.n_sd, dtype=float) # does the event happen? | ||
| self.dt_left = self.core.Storage.empty(self.core.mesh.n_cell, dtype=float) | ||
|
|
||
| self.stats_n_substep = self.core.Storage.empty(self.core.mesh.n_cell, dtype=int) | ||
| self.stats_n_substep[:] = 0 if self.adaptive else self.__substeps | ||
| self.stats_dt_min = self.core.Storage.empty(self.core.mesh.n_cell, dtype=float) | ||
| self.stats_dt_min[:] = np.nan | ||
|
|
||
| # registering helper processes | ||
| self.rnd_opt.register(builder) | ||
| self.rnd_opt_frag.register(builder) | ||
| self.breakup_rate.register(builder) | ||
| self.fragmentation.register(builder) | ||
|
|
||
| if self.croupier is None: | ||
| self.croupier = self.core.backend.default_croupier | ||
|
|
||
| # Diagnostics | ||
| # self.collision_rate = self.core.Storage.from_ndarray(np.zeros(self.core.mesh.n_cell, dtype=int)) | ||
| # self.collision_rate_deficit = self.core.Storage.from_ndarray(np.zeros(self.core.mesh.n_cell, dtype=int)) | ||
|
|
||
| def __call__(self): | ||
| if self.enable: | ||
| if not self.adaptive: | ||
| for _ in range(self.__substeps): | ||
| self.step() | ||
| else: | ||
| self.dt_left[:] = self.core.dt | ||
|
|
||
| # work on all particles | ||
| while self.core.particles.get_working_length() != 0: | ||
| self.core.particles.cell_idx.sort_by_key(self.dt_left) | ||
| self.step() # do breakup | ||
|
|
||
| self.core.particles.reset_working_length() | ||
| self.core.particles.reset_cell_idx() | ||
| self.rnd_opt.reset() | ||
| self.rnd_opt_frag.reset() | ||
|
|
||
| def step(self): | ||
| # (1) Make the random numbers for fragmentation | ||
| rand = self.rnd_opt.get_random_arrays() # does event occur | ||
| rand_frag = self.rnd_opt_frag.get_random_arrays() # for # fragments | ||
|
|
||
| # (2) Compute the probability of spontaneous breakup | ||
| self.compute_probability(self.prob) | ||
|
|
||
| # (3) Compute the number of fragments | ||
| self.compute_n_fragment(self.n_fragment, rand_frag) | ||
|
|
||
| # (4) Perform the collisional-breakup step: | ||
| self.core.particles.spontaneous_breakup(gamma=self.prob, u01=rand, n_fragment=self.n_fragment) | ||
| if self.adaptive: | ||
| self.core.particles.cut_working_length(self.core.particles.adaptive_sdm_end(self.dt_left)) | ||
|
|
||
| # (2) Does spont breakup occur? | ||
| def compute_probability(self, prob): | ||
| self.breakup_rate(self.breakup_rate_temp) | ||
| prob[:] = self.breakup_rate_temp | ||
| # breakup_rate * dt = expected number of breakups in time dt | ||
| prob[:] *= self.core.dt | ||
|
|
||
| # (3) Compute n_fragment | ||
| def compute_n_fragment(self, n_fragment, u01): | ||
| self.fragmentation(n_fragment, u01) | ||
This file contains hidden or 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 hidden or 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,5 @@ | ||
| """ | ||
| Single droplet breakup fragmentation functions. | ||
| """ | ||
| from .always_n import SpontaneousAlwaysN | ||
| from .expo_cube import SpontaneousExpoCube |
24 changes: 24 additions & 0 deletions
24
PySDM/physics/spontaneous_breakup_fragmentations/always_n.py
This file contains hidden or 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,24 @@ | ||
| import numpy as np | ||
|
|
||
|
|
||
| class SpontaneousAlwaysN: | ||
| """Breakup into `n` droplets""" | ||
|
|
||
| def __init__(self, n): | ||
| self.core = None | ||
| self.N = n | ||
| self.N_vec = None | ||
| self.zeros = None | ||
|
|
||
|
|
||
| def __call__(self, output, u01): | ||
| output *= self.zeros | ||
| output += self.N_vec | ||
|
|
||
| def register(self, builder): | ||
| self.core = builder.core | ||
| N_vec_tmp = np.tile([self.N], self.core.n_sd) | ||
| zeros_tmp = np.tile([0], self.core.n_sd) | ||
| self.N_vec = self.core.PairwiseStorage.from_ndarray(N_vec_tmp) | ||
| self.zeros = self.core.PairwiseStorage.from_ndarray(zeros_tmp) | ||
|
|
29 changes: 29 additions & 0 deletions
29
PySDM/physics/spontaneous_breakup_fragmentations/expo_cube.py
This file contains hidden or 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,29 @@ | ||
| ''' | ||
| Linear fragmentation function in volume. | ||
|
|
||
| For a fragmentation function of the form: | ||
| Q(D0, D) dD = A * (D0)**3 * exp(-B * D) dD | ||
| where D0 is the parent droplet size, Q(D) dD is the number of drops of size D | ||
| formed from the breakup. Then it can be shown that: | ||
| N_frag = int (Q(D0, D) dD) = A * (D0)**3 / B | ||
| and furthermore for consistency with mass conservation: | ||
| D0**3 = int(D**3 Q(D0, D) dD) => 6*A = B**4 | ||
|
|
||
| Therefore this function takes a single argument C = A/B = 1/6 * B**3, and | ||
| translates to a fragmentation function which is cubic in the parent size D0. | ||
| N_frag = C * (D0)**3 | ||
| ''' | ||
| class SpontaneousExpoCube: | ||
|
|
||
| def __init__(self, C): | ||
| self.core = None | ||
| self.C = C | ||
|
|
||
| def __call__(self, output, u01): | ||
| output[:] = self.core.particles['volume'] | ||
| output *= self.C | ||
|
|
||
| def register(self, builder): | ||
| self.core = builder.core | ||
| builder.request_attribute('volume') | ||
|
|
This file contains hidden or 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,5 @@ | ||
| """ | ||
| Single droplet spontaneous breakup rates. | ||
| """ | ||
| from .constant import Constant | ||
| from .exponential import Expon |
This file contains hidden or 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,12 @@ | ||
| class Constant: | ||
| '''Constant rate of breakup for particles of any size''' | ||
| def __init__(self, a): | ||
| self.a = a | ||
| self.core = None | ||
|
|
||
| def __call__(self, output): | ||
| output *= 0 | ||
| output += self.a | ||
|
|
||
| def register(self, builder): | ||
| self.core = builder.core |
This file contains hidden or 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,16 @@ | ||
| from PySDM.physics import constants as const | ||
|
|
||
| class Expon: | ||
| '''Exponential breakup rate by particle diameter: a * r**b''' | ||
| def __init__(self, a, b): | ||
| self.a = a | ||
| self.b = b | ||
| self.core = None | ||
|
|
||
| def __call__(self, output): | ||
| self.core.backend.exponential(output, self.b, self.core.particles['radius'], const.si.um) | ||
| output *= self.a | ||
|
|
||
| def register(self, builder): | ||
| self.core = builder.core | ||
| builder.request_attribute('radius') |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
currently no equivalent to generating
pairs_rand(e.g. in coalescence and breakup dynamics) - do we want to apply this to every SD? @edejong-caltech