OpenDrift / opendrift

Open source framework for ocean trajectory modelling
https://opendrift.github.io
GNU General Public License v2.0
249 stars 120 forks source link

add mortality #816

Closed CaroMedel closed 2 years ago

CaroMedel commented 2 years ago

Hi all, I am new using opendrift and Python. I am trying to add mortality to an IBM; but I only want them to die every certain time steps (not every time step). I have tried this:

def constant_mortality(self):
mort = 0.1 mort_rate = mort self.time_step.total_seconds()/86400 self.elements.otr += mort_rate if (np.all((self.elements.otr % mort) == 0) == True): m = len(self.elements.lat) mort lista1 = list(range(1, len(self.elements.lat))) positions = random.sample(lista1, m) self.elements.status[positions] = 1 self.deactivate_elements(self.elements.status == 1, reason='dead')

I don't get an error, but it does not kill any of them because np.all((self.elements.otr % mort) == 0 is always False. Any other idea to add mortality when a condition is satisfied? Any help is appreciated

knutfrode commented 2 years ago

Hi,

The time step number is given by self.steps. And if you want to deactivate elements only every 6 time steps, you can do something like (pseudo code, not tested):

def constant_mortality(self):
    mort = 0.1
    mort_rate = mort * self.time_step.total_seconds()/86400
    self.elements.otr += mort_rate
    if self.steps % 6 == 0:
        self.deactivate_elements(self.otr > some_condition, reason='dead')

Note that you should not edit self.elements.status directly: this is 0 for active elements, and is automatically given an integer value after deactivation, corresponding to the reason/string (here 'died')

CaroMedel commented 2 years ago

Thanks