@@ -11,6 +11,8 @@ class battery_2condensators(ptype):
1111 project
1212 Attributes:
1313 A: system matrix, representing the 3 ODEs
14+ t_switch: time point of the switch
15+ SV, SC1, SC2: states of switching (important for switch estimator)
1416 """
1517
1618 def __init__ (self , problem_params , dtype_u = mesh , dtype_f = imex_mesh ):
@@ -41,12 +43,10 @@ def __init__(self, problem_params, dtype_u=mesh, dtype_f=imex_mesh):
4143 )
4244
4345 self .A = np .zeros ((3 , 3 ))
46+ self .t_switch = None
4447 self .SV = 0
4548 self .SC1 = 1
4649 self .SC2 = 0
47- self .t_switch = None
48- self .count_switches = 0
49- self .diff = 0.0
5050
5151 def eval_f (self , u , t ):
5252 """
@@ -61,32 +61,25 @@ def eval_f(self, u, t):
6161 f = self .dtype_f (self .init , val = 0.0 )
6262 f .impl [:] = self .A .dot (u )
6363
64- if u [ 2 ] > self .params . V_ref [ 1 ] :
65- if self .t_switch is not None :
64+ if self .t_switch is not None :
65+ if self .SV == 0 and self . SC1 == 0 and self . SC2 == 1 :
6666 if t >= self .t_switch :
67- if self .count_switches == 1 :
68- # switch to C2
69- f .expl [0 ] = 0
70- elif self .count_switches == 2 :
71- # switch to Vs
72- f .expl [0 ] = self .params .Vs / self .params .L
67+ f .expl [0 ] = 0
7368 else :
74- if self .count_switches == 1 :
75- # C1 supplies energy
76- f .expl [0 ] = 0
77- elif self .count_switches == 2 :
78- # C2 supplies energy
79- f .expl [0 ] = 0
80- else :
81- if u [1 ] > self .params .V_ref [0 ]:
82- # C1 supplies energy
8369 f .expl [0 ] = 0
70+ elif self .SV == 1 and self .SC1 == 0 and self .SC2 == 0 :
71+ if t >= self .t_switch :
72+ f .expl [0 ] = self .params .Vs / self .params .L
8473 else :
85- # switch to C2
8674 f .expl [0 ] = 0
75+
8776 else :
88- # switch to Vs
89- f .expl [0 ] = self .params .Vs / self .params .L
77+ if u [1 ] > self .params .V_ref [0 ] and u [2 ] > self .params .V_ref [1 ]:
78+ f .expl [0 ] = 0
79+ elif u [1 ] <= self .params .V_ref [0 ] and u [2 ] > self .params .V_ref [1 ]:
80+ f .expl [0 ] = 0
81+ elif u [1 ] <= self .params .V_ref [0 ] and u [2 ] <= self .params .V_ref [1 ]:
82+ f .expl [0 ] = self .params .Vs / self .params .L
9083
9184 return f
9285
@@ -103,36 +96,25 @@ def solve_system(self, rhs, factor, u0, t):
10396 """
10497 self .A = np .zeros ((3 , 3 ))
10598
106- if rhs [ 2 ] > self .params . V_ref [ 1 ] :
107- if self .t_switch is not None :
99+ if self .t_switch is not None :
100+ if self .SV == 0 and self . SC1 == 0 and self . SC2 == 1 :
108101 if t >= self .t_switch :
109- if self .count_switches == 1 :
110- # switch to C2
111- self .A [2 , 2 ] = - 1 / (self .params .C2 * self .params .R )
112- elif self .count_switches == 2 :
113- # switch to Vs
114- self .A [0 , 0 ] = - (self .params .Rs + self .params .R ) / self .params .L
115-
102+ self .A [2 , 2 ] = - 1 / (self .params .C2 * self .params .R )
116103 else :
117- if self .count_switches == 1 :
118- # C1 supplies energy
119- self .A [1 , 1 ] = - 1 / (self .params .C1 * self .params .R )
120- elif self .count_switches == 2 :
121- # C2 supplies energy
122- self .A [2 , 2 ] = - 1 / (self .params .C2 * self .params .R )
123-
124- else :
125- if rhs [1 ] > self .params .V_ref [0 ]:
126- # C1 supplies energy
127104 self .A [1 , 1 ] = - 1 / (self .params .C1 * self .params .R )
128-
105+ elif self .SV == 1 and self .SC1 == 0 and self .SC2 == 0 :
106+ if t >= self .t_switch :
107+ self .A [0 , 0 ] = - (self .params .Rs + self .params .R ) / self .params .L
129108 else :
130- # switch to C2
131109 self .A [2 , 2 ] = - 1 / (self .params .C2 * self .params .R )
132110
133111 else :
134- # switch to Vs
135- self .A [0 , 0 ] = - (self .params .Rs + self .params .R ) / self .params .L
112+ if rhs [1 ] > self .params .V_ref [0 ] and rhs [2 ] > self .params .V_ref [1 ]:
113+ self .A [1 , 1 ] = - 1 / (self .params .C1 * self .params .R )
114+ elif rhs [1 ] <= self .params .V_ref [0 ] and rhs [2 ] > self .params .V_ref [1 ]:
115+ self .A [2 , 2 ] = - 1 / (self .params .C2 * self .params .R )
116+ elif rhs [1 ] <= self .params .V_ref [0 ] and rhs [2 ] <= self .params .V_ref [1 ]:
117+ self .A [0 , 0 ] = - (self .params .Rs + self .params .R ) / self .params .L
136118
137119 me = self .dtype_u (self .init )
138120 me [:] = np .linalg .solve (np .eye (self .params .nvars ) - factor * self .A , rhs )
@@ -189,9 +171,12 @@ def get_switching_info(self, u, t):
189171
190172 return switch_detected , m_guess , vC_switch
191173
192- def set_counter (self ):
174+ def flip_switches (self ):
193175 """
194- Counts the number of switches found.
176+ Flips the switches of the circuit to its new state
195177 """
196178
197- self .count_switches += 1
179+ if self .SV == 0 and self .SC1 == 1 and self .SC2 == 0 :
180+ self .SV , self .SC1 , self .SC2 = 0 , 0 , 1
181+ elif self .SV == 0 and self .SC1 == 0 and self .SC2 == 1 :
182+ self .SV , self .SC1 , self .SC2 = 1 , 0 , 0
0 commit comments