|
| 1 | +""" particle injection handling, requires initalising a simulation with |
| 2 | +enough particles flagged with NaN multiplicity (translated to zeros |
| 3 | +at multiplicity discretisation """ |
| 4 | + |
| 5 | +from collections.abc import Sized |
| 6 | + |
| 7 | +import numpy as np |
| 8 | + |
| 9 | +from PySDM.dynamics.impl import register_dynamic |
| 10 | +from PySDM.initialisation import discretise_multiplicities |
| 11 | + |
| 12 | + |
| 13 | +@register_dynamic() |
| 14 | +class Seeding: |
| 15 | + def __init__( |
| 16 | + self, |
| 17 | + *, |
| 18 | + super_droplet_injection_rate: callable, |
| 19 | + seeded_particle_extensive_attributes: dict, |
| 20 | + seeded_particle_multiplicity: Sized, |
| 21 | + ): |
| 22 | + for attr in seeded_particle_extensive_attributes.values(): |
| 23 | + assert len(seeded_particle_multiplicity) == len(attr) |
| 24 | + self.particulator = None |
| 25 | + self.super_droplet_injection_rate = super_droplet_injection_rate |
| 26 | + self.seeded_particle_extensive_attributes = seeded_particle_extensive_attributes |
| 27 | + self.seeded_particle_multiplicity = seeded_particle_multiplicity |
| 28 | + self.rnd = None |
| 29 | + self.u01 = None |
| 30 | + self.index = None |
| 31 | + |
| 32 | + def register(self, builder): |
| 33 | + self.particulator = builder.particulator |
| 34 | + |
| 35 | + def post_register_setup_when_attributes_are_known(self): |
| 36 | + if tuple(self.particulator.attributes.get_extensive_attribute_keys()) != tuple( |
| 37 | + self.seeded_particle_extensive_attributes.keys() |
| 38 | + ): |
| 39 | + raise ValueError( |
| 40 | + f"extensive attributes ({self.seeded_particle_extensive_attributes.keys()})" |
| 41 | + " do not match those used in particulator" |
| 42 | + f" ({self.particulator.attributes.get_extensive_attribute_keys()})" |
| 43 | + ) |
| 44 | + |
| 45 | + self.index = self.particulator.Index.identity_index( |
| 46 | + len(self.seeded_particle_multiplicity) |
| 47 | + ) |
| 48 | + if len(self.seeded_particle_multiplicity) > 1: |
| 49 | + self.rnd = self.particulator.Random( |
| 50 | + len(self.seeded_particle_multiplicity), self.particulator.formulae.seed |
| 51 | + ) |
| 52 | + self.u01 = self.particulator.Storage.empty( |
| 53 | + len(self.seeded_particle_multiplicity), dtype=float |
| 54 | + ) |
| 55 | + self.seeded_particle_multiplicity = ( |
| 56 | + self.particulator.IndexedStorage.from_ndarray( |
| 57 | + self.index, |
| 58 | + discretise_multiplicities( |
| 59 | + np.asarray(self.seeded_particle_multiplicity) |
| 60 | + ), |
| 61 | + ) |
| 62 | + ) |
| 63 | + self.seeded_particle_extensive_attributes = ( |
| 64 | + self.particulator.IndexedStorage.from_ndarray( |
| 65 | + self.index, |
| 66 | + np.asarray(list(self.seeded_particle_extensive_attributes.values())), |
| 67 | + ) |
| 68 | + ) |
| 69 | + |
| 70 | + def __call__(self): |
| 71 | + if self.particulator.n_steps == 0: |
| 72 | + self.post_register_setup_when_attributes_are_known() |
| 73 | + |
| 74 | + time = self.particulator.n_steps * self.particulator.dt |
| 75 | + number_of_super_particles_to_inject = self.super_droplet_injection_rate(time) |
| 76 | + |
| 77 | + if number_of_super_particles_to_inject > 0: |
| 78 | + assert number_of_super_particles_to_inject <= len( |
| 79 | + self.seeded_particle_multiplicity |
| 80 | + ) |
| 81 | + |
| 82 | + if self.rnd is not None: |
| 83 | + self.u01.urand(self.rnd) |
| 84 | + # TODO #1387 make shuffle smarter |
| 85 | + # e.g. don't need to shuffle if only one type of seed particle |
| 86 | + # or if the number of super particles to inject |
| 87 | + # is equal to the number of possible seeds |
| 88 | + self.index.shuffle(self.u01) |
| 89 | + self.particulator.seeding( |
| 90 | + seeded_particle_index=self.index, |
| 91 | + number_of_super_particles_to_inject=number_of_super_particles_to_inject, |
| 92 | + seeded_particle_multiplicity=self.seeded_particle_multiplicity, |
| 93 | + seeded_particle_extensive_attributes=self.seeded_particle_extensive_attributes, |
| 94 | + ) |
0 commit comments