Skip to content

Commit 4f601bf

Browse files
committed
Inheritance for Battery!
1 parent 0ae6235 commit 4f601bf

File tree

1 file changed

+10
-87
lines changed

1 file changed

+10
-87
lines changed

pySDC/implementations/problem_classes/Battery.py

Lines changed: 10 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class battery(ptype):
1010
Example implementing the battery drain model as in the description in the PinTSimE project
1111
Attributes:
1212
A: system matrix, representing the 2 ODEs
13+
t_switch: time point of the switch
14+
SV, SC: states of switching (important for switch estimator)
1315
"""
1416

1517
def __init__(self, problem_params, dtype_u=mesh, dtype_f=imex_mesh):
@@ -40,7 +42,8 @@ def __init__(self, problem_params, dtype_u=mesh, dtype_f=imex_mesh):
4042

4143
self.A = np.zeros((2, 2))
4244
self.t_switch = None
43-
self.count_switches = 0
45+
self.SV = 0
46+
self.SC = 1
4447

4548
def eval_f(self, u, t):
4649
"""
@@ -145,44 +148,20 @@ def get_switching_info(self, u, t):
145148

146149
return switch_detected, m_guess, vC_switch
147150

148-
def set_counter(self):
151+
def flip_switches(self):
149152
"""
150-
Counts the number of switches found.
153+
Flips the switches of the circuit to its new state
151154
"""
152155

153-
self.count_switches += 1
156+
if self.SV == 0 and self.SC == 1:
157+
self.SV, self.SC = 1, 0
154158

155159

156-
class battery_implicit(ptype):
157-
"""
158-
Example implementing the battery drain model as in the description in the PinTSimE project
159-
Attributes:
160-
A: system matrix, representing the 2 ODEs
161-
"""
160+
class battery_implicit(battery):
162161

163162
def __init__(self, problem_params, dtype_u=mesh, dtype_f=mesh):
164-
"""
165-
Initialization routine
166-
Args:
167-
problem_params (dict): custom parameters for the example
168-
dtype_u: mesh data type for solution
169-
dtype_f: mesh data type for RHS
170-
"""
171-
172-
problem_params['nvars'] = 2
173163

174-
# these parameters will be used later, so assert their existence
175-
essential_keys = [
176-
'newton_maxiter',
177-
'newton_tol',
178-
'Vs',
179-
'Rs',
180-
'C',
181-
'R',
182-
'L',
183-
'alpha',
184-
'V_ref',
185-
]
164+
essential_keys = ['newton_maxiter', 'newton_tol']
186165
for key in essential_keys:
187166
if key not in problem_params:
188167
msg = 'need %s to instantiate problem, only got %s' % (key, str(problem_params.keys()))
@@ -196,9 +175,6 @@ def __init__(self, problem_params, dtype_u=mesh, dtype_f=mesh):
196175
params=problem_params,
197176
)
198177

199-
self.A = np.zeros((2, 2))
200-
self.t_switch = None
201-
self.count_switches = 0
202178
self.newton_itercount = 0
203179
self.lin_itercount = 0
204180
self.newton_ncalls = 0
@@ -234,7 +210,6 @@ def eval_f(self, u, t):
234210
non_f[0] = 0
235211

236212
f[:] = self.A.dot(u) + non_f
237-
238213
return f
239214

240215
def solve_system(self, rhs, factor, u0, t):
@@ -306,55 +281,3 @@ def solve_system(self, rhs, factor, u0, t):
306281
me[:] = u[:]
307282

308283
return me
309-
310-
def u_exact(self, t):
311-
"""
312-
Routine to compute the exact solution at time t
313-
Args:
314-
t (float): current time
315-
Returns:
316-
dtype_u: exact solution
317-
"""
318-
assert t == 0, 'ERROR: u_exact only valid for t=0'
319-
320-
me = self.dtype_u(self.init)
321-
322-
me[0] = 0.0 # cL
323-
me[1] = self.params.alpha * self.params.V_ref # vC
324-
325-
return me
326-
327-
def get_switching_info(self, u, t):
328-
"""
329-
Provides information about a discrete event for one subinterval.
330-
Args:
331-
u (dtype_u): current values
332-
t (float): current time
333-
Returns:
334-
switch_detected (bool): Indicates if a switch is found or not
335-
m_guess (np.int): Index of where the discrete event would found
336-
vC_switch (list): Contains function values of switching condition (for interpolation)
337-
"""
338-
339-
switch_detected = False
340-
m_guess = -100
341-
342-
for m in range(len(u)):
343-
if u[m][1] - self.params.V_ref <= 0:
344-
switch_detected = True
345-
m_guess = m - 1
346-
break
347-
348-
vC_switch = []
349-
if switch_detected:
350-
for m in range(1, len(u)):
351-
vC_switch.append(u[m][1] - self.params.V_ref)
352-
353-
return switch_detected, m_guess, vC_switch
354-
355-
def set_counter(self):
356-
"""
357-
Counts the number of switches found.
358-
"""
359-
360-
self.count_switches += 1

0 commit comments

Comments
 (0)