Skip to content

Commit

Permalink
Merge pull request #309 from TomTranter/node_voltages
Browse files Browse the repository at this point in the history
Node voltages
  • Loading branch information
TomTranter authored Jun 24, 2024
2 parents b332223 + 7ef8589 commit da6de7e
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
4 changes: 3 additions & 1 deletion liionpack/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,9 @@ def plot_cells(output, color="dark"):

# Get time and results for battery cells
time = output["Time [s]"]
cell_vars = [k for k in output.keys() if len(output[k].shape) > 1]
cell_vars = [
k for k in output.keys() if len(output[k].shape) > 1 and k != "Node voltage [V]"
]

context = lp_context(color)
cmap = lp_cmap(context)
Expand Down
2 changes: 2 additions & 0 deletions liionpack/solver_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ def solve(
nproc=1,
output_variables=None,
manager="casadi",
node_termination_func=None,
):
"""
Solves a pack simulation
Expand Down Expand Up @@ -434,5 +435,6 @@ def solve(
nproc=nproc,
initial_soc=initial_soc,
setup_only=False,
node_termination_func=node_termination_func,
)
return output
13 changes: 12 additions & 1 deletion liionpack/solvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,12 @@ def solve(
output_variables,
initial_soc,
nproc,
node_termination_func=None,
setup_only=False,
):
self.netlist = netlist
self.sim_func = sim_func

self.node_termination_func = node_termination_func
self.parameter_values = parameter_values
self.check_current_function()
# Get netlist indices for resistors, voltage sources, current sources
Expand Down Expand Up @@ -195,10 +196,14 @@ def solve(
self.shm_i_app = np.zeros([self.Nsteps, self.Nspm], dtype=np.float32)
self.shm_Ri = np.zeros([self.Nsteps, self.Nspm], dtype=np.float32)
self.output = np.zeros([self.Nvar, self.Nsteps, self.Nspm], dtype=np.float32)
self.node_voltages = np.zeros([self.Nsteps, len(V_node)], dtype=np.float32)

# Initialize currents in battery models
self.shm_i_app[0, :] = I_batt * -1

# Initialize the node voltages
self.node_voltages[0, :] = V_node

# Step forward in time
self.V_terminal = np.zeros(self.Nsteps, dtype=np.float32)
self.I_terminal = np.zeros(self.Nsteps, dtype=np.float32)
Expand Down Expand Up @@ -263,6 +268,7 @@ def step_output(self):
self.all_output["Pack current [A]"] = self.I_terminal[:report_steps]
self.all_output["Pack terminal voltage [V]"] = self.V_terminal[:report_steps]
self.all_output["Cell current [A]"] = self.shm_i_app[:report_steps, :]
self.all_output["Node voltage [V]"] = self.node_voltages[:report_steps, :]
self.all_output["Cell internal resistance [Ohm]"] = self.shm_Ri[
:report_steps, :
]
Expand Down Expand Up @@ -306,6 +312,7 @@ def _step(self, step, protocol, termination, updated_inputs, skip_vcheck):
I_app = I_batt[:] * -1
self.shm_i_app[self.global_step, :] = I_app
self.shm_i_app[self.global_step + 1, :] = I_app
self.node_voltages[self.global_step, :] = V_node
self.inputs_dict = lp.build_inputs_dict(I_app, self.inputs, updated_inputs)
# 06 Check if voltage limits are reached and terminate
if np.any(temp_v < self.v_cut_lower):
Expand All @@ -318,6 +325,10 @@ def _step(self, step, protocol, termination, updated_inputs, skip_vcheck):
if np.any(v_thresh < 0) and np.any(v_thresh > 0):
# some have crossed the stopping condition
vlims_ok = False
if self.node_termination_func is not None:
if self.node_termination_func(V_node):
lp.logger.warning("Node voltage limit reached")
vlims_ok = False
if skip_vcheck:
vlims_ok = True
# 07 Step the electrochemical system
Expand Down

0 comments on commit da6de7e

Please sign in to comment.