BELFEM 0.9.0
Berkeley Lab Finite Element Framework
Loading...
Searching...
No Matches
cl_ElectricalCircuit.hpp
Go to the documentation of this file.
1/*
2 * BELFEM -- The Berkeley Lab Finite Element Framework
3 * Copyright (c) 2026, The Regents of the University of California,
4 * through Lawrence Berkeley National Laboratory (subject to receipt of any required
5 * approvals from the U.S. Dept. of Energy). All rights reserved.
6 *
7 * Developers: Christian Messe, Gregory Giard
8 *
9 * See the top-level LICENSE file for the complete license and disclaimer.
10 */
11
12#ifndef CL_ELECTRICALCIRCUIT_HPP
13#define CL_ELECTRICALCIRCUIT_HPP
14
15#include "typedefs.hpp"
16#include "cl_ElectricNode.hpp"
17#include "cl_TwoTerminals.hpp"
19#include "cl_Resistor.hpp"
20#include "cl_SourceFunction.hpp"
21#include "cl_Cell.hpp"
22#include "cl_SpMatrix.hpp"
23#include "cl_Vector.hpp"
24#include "cl_Solver.hpp"
25#include "cl_Circuit.hpp"
26
27namespace belfem
28{
29 namespace electronics
30 {
31//-----------------------------------------------------------------------------
39 {
40
42 Cell < ElectricNode * > mNodes ;
43
45 Cell < Component * > mComponents ;
46
48 Cell < Component * > mComponentsUnknown ;
49
51 Cell < FEMTwoTerminals* > mTerminalPairs ;
52
54 Cell < graph::Vertex* > mVertices ;
55
57 uint mNumberOfNodes;
58
60 uint mNumberOfComponents = 0;
61
63 uint mNumberOfUnknownCurrents = 0;
64
66 SpMatrix * mJ = nullptr ;
67
69 SpMatrix * mMNA = nullptr ;
70
72 Vector< real > mRHS;
73
74 real mRHSnorm ;
75
78
80 Vector< real > mPrevX ;
81
83 Solver mSolver { SolverType::SUPERLU } ;
84
86 Cell< string > mOutputCurrents ;
87
89 Vector< id_t > mOutputVoltages ;
90
92 string mOutputFile = "" ;
93
95 real mDeltaTime ;
96
98 real mTime = 0.0 ;
99
101 real mOmega = 1.0;
102
103//-----------------------------------------------------------------------------
104 public:
105//-----------------------------------------------------------------------------
106
107 ElectricalCircuit( const uint aNumberOfNodes) ;
108
109 ~ElectricalCircuit() override ;
110
111 // NON-COPYABLE, NON-MOVABLE. This class owns every entry of
112 // mNodes and mComponents plus the two matrices mJ and mMNA,
113 // and deletes all of them in its destructor, so the implicit
114 // copy would be a shallow pointer copy and the second
115 // destructor a double free. Copy assignment is already
116 // implicitly deleted through the by-value mSolver member,
117 // whose own assignment is deleted -- these declarations state
118 // the invariant instead of leaving it to that chain
123
124//-----------------------------------------------------------------------------
125
126 real
127 get_voltage_on_node( const index_t aIndex) const ;
128
129//-----------------------------------------------------------------------------
130
131 real
132 get_current_on_component( const index_t aIndex) const ;
133
134//-----------------------------------------------------------------------------
135
136 Component *
137 component( const index_t aIndex) const ;
138
139//-----------------------------------------------------------------------------
140
141 Cell < FEMTwoTerminals * > &
142 terminal_pairs( ) ;
143
144//-----------------------------------------------------------------------------
145
146 uint
147 number_of_nodes() const ;
148
149//-----------------------------------------------------------------------------
150
151 uint
152 number_of_components() const ;
153
154//-----------------------------------------------------------------------------
155
156 void
157 set_timestep( const real aDeltaTime ) override ;
158
159//-----------------------------------------------------------------------------
160
161 void
162 set_file( const string aFile ) ;
163
164//-----------------------------------------------------------------------------
165
166 void
167 set_output_currents( const Cell < string > aCurrents ) ;
168
169//-----------------------------------------------------------------------------
170
171 void
172 set_output_voltages( const Vector < id_t > aVoltages ) ;
173
174//-----------------------------------------------------------------------------
175
176
177 void
178 set_omega( const real aOmega ) override ;
179
180//-----------------------------------------------------------------------------
181
182 void
183 shift() override ;
184
185//-----------------------------------------------------------------------------
186
187 void
188 shift_back() override ;
189
190//-----------------------------------------------------------------------------
191
192
193 void
194 create_resistor( const real aValue, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "") ;
195
196//-----------------------------------------------------------------------------
197
198 void
199 create_inductor( const real aValue, const uint aOrder, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "") ;
200
201//-----------------------------------------------------------------------------
202
203 void
204 create_capacitor( const real aValue, const uint aOrder, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "") ;
205
206//-----------------------------------------------------------------------------
207
208 void
209 create_voltage_source( SourceFunction * aFunction, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "" ) ;
210
211//-----------------------------------------------------------------------------
212
213 void
214 create_current_source( SourceFunction * aFunction, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "" ) ;
215
216//-----------------------------------------------------------------------------
217
218 void
219 create_switch( const bool aIsClosed , const real aTimeSwitch, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "" ) ;
220
221//-----------------------------------------------------------------------------
222
223 void
224 create_diode( const real aIs, const real aVt, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "" ) ;
225
226//-----------------------------------------------------------------------------
227
228 void
229 create_superconductor( const real aIc, const real aN, const real aEc, const real aLength, const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "" ) ;
230
231//-----------------------------------------------------------------------------
232
233 void
234 create_terminal_pair( const index_t aNIndex1, const index_t aNIndex2, const string aLabel = "" ) ;
235
236//-----------------------------------------------------------------------------
237
238 void
240
241//-----------------------------------------------------------------------------
242
249 uint
251
252//-----------------------------------------------------------------------------
253
259 uint
260 degree_of_graph_vertex( const index_t aIndex ) const ;
261
262//-----------------------------------------------------------------------------
263
264 void
265 compute_jacobian_and_rhs() override ;
266
267//-----------------------------------------------------------------------------
268
269 void
270 compute_MNA_matrix() override ;
271
272//-----------------------------------------------------------------------------
273
274 void
275 solve() override ;
276
277//-----------------------------------------------------------------------------
278
279 real
280 residual() const override ;
281
282//-----------------------------------------------------------------------------
283
284 void
285 save_timestep() override ;
286
287//-----------------------------------------------------------------------------
288
289 void
291
292//-----------------------------------------------------------------------------
293
294 real
295 current( const index_t aIndex ) const override ;
296
297//-----------------------------------------------------------------------------
298
299 real
300 voltage( const index_t aIndex ) const override ;
301
302 void
303 set_current_and_voltage( const index_t aIndex, const real aI, const real aV ) override ;
304
305//-----------------------------------------------------------------------------
306
307 void
308 save_state( hid_t aFile ) override ;
309
310 void
311 load_state( hid_t aFile ) override ;
312
313 private:
314
315 void
316 update_components();
317
318//-----------------------------------------------------------------------------
319 };
320
321 inline real
323 {
324 return mTerminalPairs( aIndex )->get_current() ;
325 }
326
327//-----------------------------------------------------------------------------
328
329 inline real
331 {
332 return mTerminalPairs( aIndex )->get_node_plus()->get_voltage()-
333 mTerminalPairs( aIndex )->get_node_minus()->get_voltage() ;
334 }
335
336//-----------------------------------------------------------------------------
337
338 inline void
340 {
341 mTerminalPairs( aIndex )->set_IV( aI, aV ) ;
342 }
343 }
344}
345
346#endif //CL_ELECTRICALCIRCUIT_HPP
Circuit()=default
Unified interface to the sparse direct solvers.
Definition cl_Solver.hpp:34
Definition cl_SourceFunction.hpp:80
Sparse matrix in CSR or CSC format.
Definition cl_SpMatrix.hpp:52
Base class for every circuit component.
Definition cl_Component.hpp:34
void create_capacitor(const real aValue, const uint aOrder, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:318
void create_resistor(const real aValue, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:298
real current(const index_t aIndex) const override
Definition cl_ElectricalCircuit.hpp:322
void shift() override
Definition cl_ElectricalCircuit.cpp:195
real voltage(const index_t aIndex) const override
Definition cl_ElectricalCircuit.hpp:330
uint number_of_components() const
Definition cl_ElectricalCircuit.cpp:120
void create_diode(const real aIs, const real aVt, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:376
void set_output_voltages(const Vector< id_t > aVoltages)
Definition cl_ElectricalCircuit.cpp:178
real get_current_on_component(const index_t aIndex) const
Definition cl_ElectricalCircuit.cpp:88
void load_state(hid_t aFile) override
Definition cl_ElectricalCircuit.cpp:1085
void save_timestep() override
Definition cl_ElectricalCircuit.cpp:1006
void set_current_and_voltage(const index_t aIndex, const real aI, const real aV) override
Definition cl_ElectricalCircuit.hpp:339
ElectricalCircuit(const ElectricalCircuit &)=delete
void create_switch(const bool aIsClosed, const real aTimeSwitch, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:357
void create_terminal_pair(const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:396
void init_output_file()
Definition cl_ElectricalCircuit.cpp:1040
void compute_adjacency()
Definition cl_ElectricalCircuit.cpp:417
void compute_MNA_matrix() override
Definition cl_ElectricalCircuit.cpp:610
void solve() override
Definition cl_ElectricalCircuit.cpp:948
ElectricalCircuit(ElectricalCircuit &&)=delete
void save_state(hid_t aFile) override
Definition cl_ElectricalCircuit.cpp:1061
uint number_of_nodes() const
Definition cl_ElectricalCircuit.cpp:112
uint degree_of_graph_vertex(const index_t aIndex) const
adjacency-list length of one vertex of that graph, i.e.
Definition cl_ElectricalCircuit.cpp:136
uint number_of_graph_vertices() const
number of vertices in the adjacency graph handed to the SpMatrix: the non-ground nodes,...
Definition cl_ElectricalCircuit.cpp:128
void create_inductor(const real aValue, const uint aOrder, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:308
void create_superconductor(const real aIc, const real aN, const real aEc, const real aLength, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:386
void set_output_currents(const Cell< string > aCurrents)
Definition cl_ElectricalCircuit.cpp:170
real residual() const override
Definition cl_ElectricalCircuit.cpp:998
void set_omega(const real aOmega) override
Definition cl_ElectricalCircuit.cpp:186
ElectricalCircuit & operator=(const ElectricalCircuit &)=delete
void set_file(const string aFile)
Definition cl_ElectricalCircuit.cpp:162
ElectricalCircuit & operator=(ElectricalCircuit &&)=delete
real get_voltage_on_node(const index_t aIndex) const
Definition cl_ElectricalCircuit.cpp:80
void set_timestep(const real aDeltaTime) override
Definition cl_ElectricalCircuit.cpp:144
Component * component(const index_t aIndex) const
Definition cl_ElectricalCircuit.cpp:96
void compute_jacobian_and_rhs() override
Definition cl_ElectricalCircuit.cpp:749
Cell< FEMTwoTerminals * > & terminal_pairs()
Definition cl_ElectricalCircuit.cpp:104
void create_voltage_source(SourceFunction *aFunction, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:328
ElectricalCircuit(const uint aNumberOfNodes)
Definition cl_ElectricalCircuit.cpp:32
void shift_back() override
Definition cl_ElectricalCircuit.cpp:227
void create_current_source(SourceFunction *aFunction, const index_t aNIndex1, const index_t aNIndex2, const string aLabel="")
Definition cl_ElectricalCircuit.cpp:347
Definition cl_Capacitor.cpp:18
USER GUIDES:
Definition cl_Capacitor.cpp:16
int hid_t
Definition hdf5_types.hpp:20
unsigned int uint
Definition typedefs.hpp:30
@ SUPERLU
Definition en_SolverEnums.hpp:25
uint32_t index_t
Definition typedefs.hpp:52
double real
Definition typedefs.hpp:36