Repository: pybamm-team/PyBaMM

Issue:
Rename `set_events`
As discussed in the PR #4254, `set_event` should be renamed because of misleading.

              This adds a side effect to what would be expected to be a simple setter. At minimum it should be changed to `append_events()` or `add_events()`. Was there an actual bug that this was fixing or just not sure if it would cause issues?

_Originally posted by @kratman in https://github.com/pybamm-team/PyBaMM/pull/4254#discussion_r1672332716_
            

Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/pybamm-team/PyBaMM/pull/4421?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 99.43%. Comparing base [(`b073fc7`)](https://app.codecov.io/gh/pybamm-team/PyBaMM/commit/b073fc7152694c8cfa35fc77a7741d1b2c7e80dd?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team) to head [(`b227416`)](https://app.codecov.io/gh/pybamm-team/PyBaMM/commit/b227416146626547ddb52838f9e07ae07244c1da?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team).

<details><summary>Additional details and impacted files</summary>


```diff
@@             Coverage Diff             @@
##           develop    #4421      +/-   ##
===========================================
+ Coverage    99.41%   99.43%   +0.01%     
===========================================
  Files          292      292              
  Lines        22212    22212              
===========================================
+ Hits         22083    22086       +3     
+ Misses         129      126       -3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/pybamm-team/PyBaMM/pull/4421?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=pybamm-team).

medha-14: > It looks like you might have missed `reaction_driven_porosity.py` when doing these changes. Can you double check that one?

Sorry I missed it. 
kratman: Thank you, just update the changelog and it should be good to go. Put this one under "breaking changes" in the unreleased section since it is renaming a function.
agriyakhetarpal: @all-contributors please add @medha-14 for code
allcontributors[bot]: @agriyakhetarpal 

I've put up [a pull request](https://github.com/pybamm-team/PyBaMM/pull/4423) to add @medha-14! :tada:

Patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 5203229bd..7764a3b24 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -3,6 +3,9 @@
 ## Optimizations
 - Removed the `start_step_offset` setting and disabled minimum `dt` warnings for drive cycles with the (`IDAKLUSolver`). ([#4416](https://github.com/pybamm-team/PyBaMM/pull/4416))
 
+## Breaking changes
+- Renamed `set_events` function to `add_events_from` to better reflect its purpose. ([#4421](https://github.com/pybamm-team/PyBaMM/pull/4421))
+
 # [v24.9.0](https://github.com/pybamm-team/PyBaMM/tree/v24.9.0) - 2024-09-03
 
 ## Features
diff --git a/src/pybamm/models/base_model.py b/src/pybamm/models/base_model.py
index 0d4638e17..989465e37 100644
--- a/src/pybamm/models/base_model.py
+++ b/src/pybamm/models/base_model.py
@@ -757,7 +757,7 @@ class BaseModel:
                 f"Setting initial conditions for {submodel_name} submodel ({self.name})"
             )
             submodel.set_initial_conditions(self.variables)
-            submodel.set_events(self.variables)
+            submodel.add_events_from(self.variables)
             pybamm.logger.verbose(f"Updating {submodel_name} submodel ({self.name})")
             self.update(submodel)
             self.check_no_repeated_keys()
diff --git a/src/pybamm/models/full_battery_models/base_battery_model.py b/src/pybamm/models/full_battery_models/base_battery_model.py
index ccda594b1..e0ade7b42 100644
--- a/src/pybamm/models/full_battery_models/base_battery_model.py
+++ b/src/pybamm/models/full_battery_models/base_battery_model.py
@@ -1033,7 +1033,7 @@ class BaseBatteryModel(pybamm.BaseModel):
                 f"Setting initial conditions for {submodel_name} submodel ({self.name})"
             )
             submodel.set_initial_conditions(self.variables)
-            submodel.set_events(self.variables)
+            submodel.add_events_from(self.variables)
             pybamm.logger.verbose(f"Updating {submodel_name} submodel ({self.name})")
             self.update(submodel)
             self.check_no_repeated_keys()
diff --git a/src/pybamm/models/submodels/base_submodel.py b/src/pybamm/models/submodels/base_submodel.py
index e120691ed..d5e313e15 100644
--- a/src/pybamm/models/submodels/base_submodel.py
+++ b/src/pybamm/models/submodels/base_submodel.py
@@ -221,7 +221,7 @@ class BaseSubModel(pybamm.BaseModel):
         """
         pass
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         """
         A method to set events related to the state of submodel variable. Note: this
         method modifies the state of self.events. Unless overwritten by a submodel, the
diff --git a/src/pybamm/models/submodels/electrolyte_diffusion/constant_concentration.py b/src/pybamm/models/submodels/electrolyte_diffusion/constant_concentration.py
index eee441446..006619e8b 100644
--- a/src/pybamm/models/submodels/electrolyte_diffusion/constant_concentration.py
+++ b/src/pybamm/models/submodels/electrolyte_diffusion/constant_concentration.py
@@ -76,6 +76,6 @@ class ConstantConcentration(BaseElectrolyteDiffusion):
             }
         }
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         # No event since the concentration is constant
         pass
diff --git a/src/pybamm/models/submodels/equivalent_circuit_elements/diffusion_element.py b/src/pybamm/models/submodels/equivalent_circuit_elements/diffusion_element.py
index c7f1b4bcd..bf9a18387 100644
--- a/src/pybamm/models/submodels/equivalent_circuit_elements/diffusion_element.py
+++ b/src/pybamm/models/submodels/equivalent_circuit_elements/diffusion_element.py
@@ -102,7 +102,7 @@ class DiffusionElement(pybamm.BaseSubModel):
         z = variables["Distributed SoC"]
         self.initial_conditions = {z: self.param.initial_soc}
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         z_surf = variables["Surface SoC"]
         self.events += [
             pybamm.Event("Minimum surface SoC", z_surf),
diff --git a/src/pybamm/models/submodels/equivalent_circuit_elements/ocv_element.py b/src/pybamm/models/submodels/equivalent_circuit_elements/ocv_element.py
index 9d1adf3d5..901b63cf7 100644
--- a/src/pybamm/models/submodels/equivalent_circuit_elements/ocv_element.py
+++ b/src/pybamm/models/submodels/equivalent_circuit_elements/ocv_element.py
@@ -54,7 +54,7 @@ class OCVElement(pybamm.BaseSubModel):
         soc = variables["SoC"]
         self.initial_conditions = {soc: self.param.initial_soc}
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         soc = variables["SoC"]
         self.events += [
             pybamm.Event("Minimum SoC", soc),
diff --git a/src/pybamm/models/submodels/equivalent_circuit_elements/voltage_model.py b/src/pybamm/models/submodels/equivalent_circuit_elements/voltage_model.py
index 380902fca..89f8904f3 100644
--- a/src/pybamm/models/submodels/equivalent_circuit_elements/voltage_model.py
+++ b/src/pybamm/models/submodels/equivalent_circuit_elements/voltage_model.py
@@ -54,7 +54,7 @@ class VoltageModel(pybamm.BaseSubModel):
 
         return variables
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         voltage = variables["Voltage [V]"]
 
         # Add voltage events
diff --git a/src/pybamm/models/submodels/interface/interface_utilisation/current_driven_utilisation.py b/src/pybamm/models/submodels/interface/interface_utilisation/current_driven_utilisation.py
index bbd9af4fb..e78520488 100644
--- a/src/pybamm/models/submodels/interface/interface_utilisation/current_driven_utilisation.py
+++ b/src/pybamm/models/submodels/interface/interface_utilisation/current_driven_utilisation.py
@@ -89,7 +89,7 @@ class CurrentDriven(BaseModel):
 
         self.initial_conditions = {u: u_init}
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         domain, Domain = self.domain_Domain
 
         if self.reaction_loc == "full electrode":
diff --git a/src/pybamm/models/submodels/particle_mechanics/crack_propagation.py b/src/pybamm/models/submodels/particle_mechanics/crack_propagation.py
index 3bb9ecb7e..b51f1d1eb 100644
--- a/src/pybamm/models/submodels/particle_mechanics/crack_propagation.py
+++ b/src/pybamm/models/submodels/particle_mechanics/crack_propagation.py
@@ -102,7 +102,7 @@ class CrackPropagation(BaseMechanics):
             l_cr_0 = pybamm.PrimaryBroadcast(l_cr_0, f"{domain} electrode")
         self.initial_conditions = {l_cr: l_cr_0}
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         domain, Domain = self.domain_Domain
 
         if self.x_average is True:
diff --git a/src/pybamm/models/submodels/porosity/constant_porosity.py b/src/pybamm/models/submodels/porosity/constant_porosity.py
index 0b9f3c0da..6d6b93d76 100644
--- a/src/pybamm/models/submodels/porosity/constant_porosity.py
+++ b/src/pybamm/models/submodels/porosity/constant_porosity.py
@@ -27,6 +27,6 @@ class Constant(BaseModel):
 
         return variables
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         # No events since porosity is constant
         pass
diff --git a/src/pybamm/models/submodels/porosity/reaction_driven_porosity.py b/src/pybamm/models/submodels/porosity/reaction_driven_porosity.py
index fc69d0f1f..989c90cd9 100644
--- a/src/pybamm/models/submodels/porosity/reaction_driven_porosity.py
+++ b/src/pybamm/models/submodels/porosity/reaction_driven_porosity.py
@@ -64,7 +64,7 @@ class ReactionDriven(BaseModel):
 
         return variables
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         eps_p = variables["Positive electrode porosity"]
         self.events.append(
             pybamm.Event(
diff --git a/src/pybamm/models/submodels/porosity/reaction_driven_porosity_ode.py b/src/pybamm/models/submodels/porosity/reaction_driven_porosity_ode.py
index 476845f05..f82a73ae6 100644
--- a/src/pybamm/models/submodels/porosity/reaction_driven_porosity_ode.py
+++ b/src/pybamm/models/submodels/porosity/reaction_driven_porosity_ode.py
@@ -94,7 +94,7 @@ class ReactionDrivenODE(BaseModel):
             eps = variables["Porosity"]
             self.initial_conditions = {eps: self.param.epsilon_init}
 
-    def set_events(self, variables):
+    def add_events_from(self, variables):
         for domain in self.options.whole_cell_domains:
             if domain == "separator":
                 continue


Test patch:
diff --git a/tests/unit/test_models/test_base_model.py b/tests/unit/test_models/test_base_model.py
index 4d5f71201..d50cd66d2 100644
--- a/tests/unit/test_models/test_base_model.py
+++ b/tests/unit/test_models/test_base_model.py
@@ -231,7 +231,7 @@ class TestBaseModel(unittest.TestCase):
                 u = variables["u"]
                 self.initial_conditions = {u: c}
 
-            def set_events(self, variables):
+            def add_events_from(self, variables):
                 e = pybamm.InputParameter("e")
                 u = variables["u"]
                 self.events = [pybamm.Event("u=e", u - e)]
@@ -1178,7 +1178,7 @@ class TestBaseModel(unittest.TestCase):
                 v = variables["v"]
                 self.initial_conditions = {u: 0, v: 0}
 
-            def set_events(self, variables):
+            def add_events_from(self, variables):
                 u = variables["u"]
                 self.events.append(
                     pybamm.Event(