-
Notifications
You must be signed in to change notification settings - Fork 49
Seeding dynamic and associated initialisation logic (super-droplet injection during simulation) #1367
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
Merged
Merged
Seeding dynamic and associated initialisation logic (super-droplet injection during simulation) #1367
Changes from 47 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
646286c
first try at adding a Seeding dynamic
slayoo 6c22296
moving contents of test_seeding into a Jupyter notebook, descretisati…
slayoo 560d2b9
addressing pylint hints
slayoo 8f10d80
import reorder
slayoo 3ce815f
add seeding to examples_tests conftest.py
slayoo 3a2a870
fix import warning
slayoo 04cf0ee
import reorder
slayoo eb09a71
compatibility fix with PySDM v2.71
slayoo 29bca2a
callable suped_droplet_injection_rate instead of time_window + sd_cou…
slayoo 4ec6978
seeding/no seeding plot
slayoo f0eea06
addressing pylint hints
slayoo 3f66e1e
add unit test for seeding
04ec98f
move seeding test to smoke tests
d3318e6
refactor the seeding parcel-based smoke test into a box-based not-so-…
slayoo 1f05941
pylint hints
slayoo a2bd158
plot=False
slayoo bdca44b
asserts on sd_num values
slayoo bd9677e
improve test coverage
slayoo 47616e1
avoid division by zero in test
slayoo dd042a8
fix output logic
slayoo bb4de82
disable multi-threading for a parcel env
slayoo 61d4df0
add rain water mixing ratio product and tst for its values in a smoke…
slayoo e427886
precommit
slayoo c17f338
added seeded particle sampling logic (i.e. user performs sampling, dy…
slayoo 9f42bc3
precommit
slayoo 569a3fa
use discretise_multiplicities in seeding setup; pylint hints
slayoo 0642e96
TODO label, pylint-disable comment
slayoo 5e5b775
add notebook and test for notebook of parcel simulation without colli…
eadebc5
pylint
5c4c52f
pylint notebook
9520ab1
add TODO note about shuffle
1a9238e
add blank new unit test for particle injection logic
fc4f49e
add flag for enable_collisions in seeding example and remove duplicat…
859ce39
unit test for number_of_super_particles_to_inject and add ValueError …
274a70e
add test for seeded particle index and fix injection logic to iterate…
d19057f
move backend tests to backends/. add test for seeding dynamic includi…
0dff888
pylint fixes. disable unused-argument for the MockParticulator seedin…
e2bd0a4
add edge case of all nan to discretise_multiplicities and add test to…
a1b988d
replace pytest.mark.xfail with pytest.raises to check for exception t…
slayoo 53fbae4
remove comments which just repeat function names
slayoo b799972
adding test for attr update marks
slayoo 773408b
adding test for no-seeds-available error condition
slayoo d20c69a
precommit run
slayoo 67f603d
addressing pylint comments
slayoo 0d58b70
make precommit ignor long lambda line
slayoo 37ea0a0
silence pylint
slayoo d305e95
two more pylint disable comments
slayoo 794243c
try moving seeding to multi_process_a - there are timeouts on windows…
slayoo 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,68 @@ | ||
| """ CPU implementation of backend methods for particle injections """ | ||
|
|
||
| from functools import cached_property | ||
|
|
||
| import numba | ||
|
|
||
| from PySDM.backends.impl_common.backend_methods import BackendMethods | ||
|
|
||
|
|
||
| class SeedingMethods(BackendMethods): # pylint: disable=too-few-public-methods | ||
| @cached_property | ||
| def _seeding(self): | ||
| @numba.njit(**{**self.default_jit_flags, "parallel": False}) | ||
| def body( # pylint: disable=too-many-arguments | ||
| idx, | ||
| multiplicity, | ||
| extensive_attributes, | ||
| seeded_particle_index, | ||
| seeded_particle_multiplicity, | ||
| seeded_particle_extensive_attributes, | ||
| number_of_super_particles_to_inject: int, | ||
| ): | ||
| number_of_super_particles_already_injected = 0 | ||
| # TODO #1387 start enumerating from the end of valid particle set | ||
| for i, mult in enumerate(multiplicity): | ||
| if ( | ||
| number_of_super_particles_to_inject | ||
| == number_of_super_particles_already_injected | ||
| ): | ||
| break | ||
| if mult == 0: | ||
| idx[i] = -1 | ||
| s = seeded_particle_index[ | ||
| number_of_super_particles_already_injected | ||
| ] | ||
| number_of_super_particles_already_injected += 1 | ||
| multiplicity[i] = seeded_particle_multiplicity[s] | ||
| for a in range(len(extensive_attributes)): | ||
| extensive_attributes[a, i] = ( | ||
| seeded_particle_extensive_attributes[a, s] | ||
| ) | ||
| assert ( | ||
| number_of_super_particles_to_inject | ||
| == number_of_super_particles_already_injected | ||
| ) | ||
|
|
||
| return body | ||
|
|
||
| def seeding( | ||
| self, | ||
| *, | ||
| idx, | ||
| multiplicity, | ||
| extensive_attributes, | ||
| seeded_particle_index, | ||
| seeded_particle_multiplicity, | ||
| seeded_particle_extensive_attributes, | ||
| number_of_super_particles_to_inject: int, | ||
| ): | ||
| self._seeding( | ||
| idx=idx.data, | ||
| multiplicity=multiplicity.data, | ||
| extensive_attributes=extensive_attributes.data, | ||
| seeded_particle_index=seeded_particle_index.data, | ||
| seeded_particle_multiplicity=seeded_particle_multiplicity.data, | ||
| seeded_particle_extensive_attributes=seeded_particle_extensive_attributes.data, | ||
| number_of_super_particles_to_inject=number_of_super_particles_to_inject, | ||
| ) | ||
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
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,94 @@ | ||
| """ particle injection handling, requires initalising a simulation with | ||
| enough particles flagged with NaN multiplicity (translated to zeros | ||
| at multiplicity discretisation """ | ||
|
|
||
| from collections.abc import Sized | ||
|
|
||
| import numpy as np | ||
|
|
||
| from PySDM.dynamics.impl import register_dynamic | ||
| from PySDM.initialisation import discretise_multiplicities | ||
|
|
||
|
|
||
| @register_dynamic() | ||
| class Seeding: | ||
| def __init__( | ||
| self, | ||
| *, | ||
| super_droplet_injection_rate: callable, | ||
| seeded_particle_extensive_attributes: dict, | ||
| seeded_particle_multiplicity: Sized, | ||
| ): | ||
| for attr in seeded_particle_extensive_attributes.values(): | ||
| assert len(seeded_particle_multiplicity) == len(attr) | ||
| self.particulator = None | ||
| self.super_droplet_injection_rate = super_droplet_injection_rate | ||
| self.seeded_particle_extensive_attributes = seeded_particle_extensive_attributes | ||
| self.seeded_particle_multiplicity = seeded_particle_multiplicity | ||
| self.rnd = None | ||
| self.u01 = None | ||
| self.index = None | ||
|
|
||
| def register(self, builder): | ||
| self.particulator = builder.particulator | ||
|
|
||
| def post_register_setup_when_attributes_are_known(self): | ||
| if tuple(self.particulator.attributes.get_extensive_attribute_keys()) != tuple( | ||
| self.seeded_particle_extensive_attributes.keys() | ||
| ): | ||
| raise ValueError( | ||
| f"extensive attributes ({self.seeded_particle_extensive_attributes.keys()})" | ||
| " do not match those used in particulator" | ||
| f" ({self.particulator.attributes.get_extensive_attribute_keys()})" | ||
| ) | ||
|
|
||
| self.index = self.particulator.Index.identity_index( | ||
| len(self.seeded_particle_multiplicity) | ||
| ) | ||
| if len(self.seeded_particle_multiplicity) > 1: | ||
| self.rnd = self.particulator.Random( | ||
| len(self.seeded_particle_multiplicity), self.particulator.formulae.seed | ||
| ) | ||
| self.u01 = self.particulator.Storage.empty( | ||
| len(self.seeded_particle_multiplicity), dtype=float | ||
| ) | ||
| self.seeded_particle_multiplicity = ( | ||
| self.particulator.IndexedStorage.from_ndarray( | ||
| self.index, | ||
| discretise_multiplicities( | ||
| np.asarray(self.seeded_particle_multiplicity) | ||
| ), | ||
| ) | ||
| ) | ||
| self.seeded_particle_extensive_attributes = ( | ||
| self.particulator.IndexedStorage.from_ndarray( | ||
| self.index, | ||
| np.asarray(list(self.seeded_particle_extensive_attributes.values())), | ||
| ) | ||
| ) | ||
|
|
||
| def __call__(self): | ||
| if self.particulator.n_steps == 0: | ||
| self.post_register_setup_when_attributes_are_known() | ||
|
|
||
| time = self.particulator.n_steps * self.particulator.dt | ||
| number_of_super_particles_to_inject = self.super_droplet_injection_rate(time) | ||
|
|
||
| if number_of_super_particles_to_inject > 0: | ||
| assert number_of_super_particles_to_inject <= len( | ||
| self.seeded_particle_multiplicity | ||
| ) | ||
|
|
||
| if self.rnd is not None: | ||
| self.u01.urand(self.rnd) | ||
| # TODO #1387 make shuffle smarter | ||
| # e.g. don't need to shuffle if only one type of seed particle | ||
| # or if the number of super particles to inject | ||
| # is equal to the number of possible seeds | ||
| self.index.shuffle(self.u01) | ||
| self.particulator.seeding( | ||
| seeded_particle_index=self.index, | ||
| number_of_super_particles_to_inject=number_of_super_particles_to_inject, | ||
| seeded_particle_multiplicity=self.seeded_particle_multiplicity, | ||
| seeded_particle_extensive_attributes=self.seeded_particle_extensive_attributes, | ||
| ) |
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
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,2 @@ | ||
| from .settings import Settings | ||
| from .simulation import Simulation |
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.