JUK1
CircuitElements.hpp
Go to the documentation of this file.
1 #ifndef _CIRCUITELEMENTS_HPP_INC_
2 #define _CIRCUITELEMENTS_HPP_INC_
11 #include "CircuitElements/BJT.hpp"
17 
18 #ifdef WITH_MATLAB
19 #include "MatlabEngine.hpp"
20 #include "MatlabDataArray.hpp"
21 #endif
22 
24 enum class SolutionStage {
25  StaticSolution = 0,
26  DynamicSolution = 1,
28 };
29 
31 enum class ComponentType {
32  Resistor,
33  Capacitor,
34  Inductor,
37  BJT,
38  Diode
39 };
40 
41 
45 template<typename T>
61 
62 #ifdef WITH_MATLAB
63  std::shared_ptr<matlab::engine::MATLABEngine> matlabEngine;
64 #endif
65 
67  std::vector<std::shared_ptr<Component<T> > > staticElements;
69  std::vector<std::shared_ptr<Component<T> > > dynamicElements;
71  std::vector<std::shared_ptr<Component<T> > > nonLinearElements;
72 
74  bool staticStampIsFresh = false;
76  bool dynamicStampIsFresh = false;
78  bool nonLinearStampIsFresh = false;
79 
81  std::multimap<size_t, std::shared_ptr<Component<T> > > nodeComponentMap;
82 
88  CircuitElements(size_t numNodes = 0, size_t numCurrents = 0,
89  size_t numDCCurrents = 0)
90  : staticStamp(numNodes, numCurrents), dynamicStamp(numNodes, numCurrents),
91  nonLinearStamp(numNodes, numCurrents),
92  dcStamp(numNodes, numCurrents + numDCCurrents) {
93  }
94 
100  void
101  setNewStampSize(size_t numNodes, size_t numCurrents, size_t numDCCurrents = 0) {
102  staticStamp = Stamp<T>(numNodes, numCurrents);
103  dynamicStamp = Stamp<T>(numNodes, numCurrents);
104  nonLinearStamp = Stamp<T>(numNodes, numCurrents);
105  dcStamp = Stamp<T>(numNodes, numCurrents + numDCCurrents);
106  staticStampIsFresh = false;
107  dynamicStampIsFresh = false;
108  nonLinearStampIsFresh = false;
109  }
110 
115  staticStamp.clear();
116 
117  for (const auto & component : staticElements) {
118  staticStamp.addStaticStamp(component);
119  }
120 
121  for (const auto & component : dynamicElements) {
122  staticStamp.addStaticStamp(component);
123  }
124 
125  for (const auto & component : nonLinearElements) {
126  staticStamp.addStaticStamp(component);
127  }
128 
129  staticStampIsFresh = true;
130  return staticStamp;
131  }
132 
141  Stamp<T> & generateDynamicStamp(const Matrix<T> & solutionMatrix,
142  const size_t currentSolutionIndex, T timestep) {
143  if (!staticStampIsFresh) {
145  }
147 
148  for (const auto & component : dynamicElements) {
149  dynamicStamp.addDynamicStamp(component, solutionMatrix,
150  currentSolutionIndex, timestep);
151  }
152 
153  for (const auto & component : nonLinearElements) {
154  dynamicStamp.addDynamicStamp(component, solutionMatrix,
155  currentSolutionIndex, timestep);
156  }
157 
158  dynamicStampIsFresh = true;
159  return dynamicStamp;
160  }
161 
170  Stamp<T> &
171  generateNonLinearStamp(const Matrix<T> & solutionMatrix,
172  const size_t currentSolutionIndex, T timestep) {
173  if (!dynamicStampIsFresh) {
174  generateDynamicStamp(solutionMatrix, currentSolutionIndex, timestep);
175  }
177 
178  for (const auto & component : nonLinearElements) {
179  nonLinearStamp.addNonLinearStamp(component, solutionMatrix,
180  currentSolutionIndex, timestep);
181  }
182 
183  nonLinearStampIsFresh = true;
184  return nonLinearStamp;
185  }
186 
195  Stamp<T> &
196  generateCompleteStamp(SolutionStage stage, const Matrix<T> & solutionMatrix,
197  const size_t currentSolutionIndex, T timestep) {
198  switch (stage) {
200  if (!staticStampIsFresh) {
202  }
203  return staticStamp;
204  break;
205 
207  if (!dynamicStampIsFresh) {
208  generateDynamicStamp(solutionMatrix, currentSolutionIndex,
209  timestep)
210  }
211  return dynamicStamp;
212  break;
213 
215  if (!nonLinearStampIsFresh) {
216  generateNonLinearStamp(solutionMatrix, currentSolutionIndex,
217  timestep)
218  }
219  return nonLinearStamp;
220  break;
221  }
222  }
223 
233  Stamp<T> &
234  generateDCStamp(const Matrix<T> & solutionVector, size_t numCurrents) {
235  dcStamp.clear();
236 
237  for (const auto & component : staticElements) {
238  dcStamp.addDCAnalysisStamp(component, solutionVector, numCurrents);
239  }
240 
241  for (const auto & component : dynamicElements) {
242  dcStamp.addDCAnalysisStamp(component, solutionVector, numCurrents);
243  }
244 
245  for (const auto & component : nonLinearElements) {
246  dcStamp.addDCAnalysisStamp(component, solutionVector, numCurrents);
247  }
248 
249  return dcStamp;
250  }
251 
259  void updateTimeStep(const Matrix<T> & solutionMatrix,
260  const size_t currentSolutionIndex, T timestep) {
261  dynamicStampIsFresh = false;
262  nonLinearStampIsFresh = false;
263  for (const auto & component : dynamicElements) {
264  component->updateStoredState(solutionMatrix, currentSolutionIndex,
265  timestep, staticStamp.sizeG_A);
266  }
267  for (const auto & component : nonLinearElements) {
268  component->updateStoredState(solutionMatrix, currentSolutionIndex,
269  timestep, staticStamp.sizeG_A);
270  }
271  }
272 
278  void updateDCStoredState(const Matrix<T> & solutionVector, size_t numCurrents) {
279  for (const auto & component : staticElements) {
280  component->updateDCStoredState(solutionVector, dcStamp.sizeG_A,
281  numCurrents);
282  }
283  for (const auto & component : dynamicElements) {
284  component->updateDCStoredState(solutionVector, dcStamp.sizeG_A,
285  numCurrents);
286  }
287  for (const auto & component : nonLinearElements) {
288  component->updateDCStoredState(solutionVector, dcStamp.sizeG_A,
289  numCurrents);
290  }
291  }
292 };
293 
294 #endif
SolutionStage
An enum to track how far we want to go in a solution.
ComponentType
An enum for component types. Current unused.
a glorified container for the different types of components.
Stamp< T > & generateStaticStamp()
Forces a clear of the static stamp, and generates a new one.
Stamp< T > & generateCompleteStamp(SolutionStage stage, const Matrix< T > &solutionMatrix, const size_t currentSolutionIndex, T timestep)
Generates the complete stamp up to a certain point.
void updateDCStoredState(const Matrix< T > &solutionVector, size_t numCurrents)
Updates the components based on their DC value. Applies to dynamic and non-linear components.
std::vector< std::shared_ptr< Component< T > > > dynamicElements
A container to store the dynamic components.
Stamp< T > dcStamp
Preallocated stamp. Used for caching between loop iterations. DC stamps must be updated on every newt...
std::vector< std::shared_ptr< Component< T > > > staticElements
A container to store the static components.
Stamp< T > staticStamp
Preallocated stamp. Used for caching between loop iterations. Static stamps will only be generated on...
bool staticStampIsFresh
A variable used to track if the cached stamp is current.
Stamp< T > & generateNonLinearStamp(const Matrix< T > &solutionMatrix, const size_t currentSolutionIndex, T timestep)
Obtains the dynamic stamp, then adds dynamic components to it.
std::vector< std::shared_ptr< Component< T > > > nonLinearElements
A container to store the Non-Linear components.
CircuitElements(size_t numNodes=0, size_t numCurrents=0, size_t numDCCurrents=0)
Initialisation.
void setNewStampSize(size_t numNodes, size_t numCurrents, size_t numDCCurrents=0)
Updates the size of all stamps.
std::multimap< size_t, std::shared_ptr< Component< T > > > nodeComponentMap
A map to pair nodes with the components connected to them.
Stamp< T > dynamicStamp
Preallocated stamp. Used for caching between loop iterations. Dynamic stamps need to be updated on ev...
Stamp< T > & generateDynamicStamp(const Matrix< T > &solutionMatrix, const size_t currentSolutionIndex, T timestep)
Obtains the static stamp, then adds dynamic components to it.
bool nonLinearStampIsFresh
A variable used to track if the cached stamp is current.
Stamp< T > nonLinearStamp
Preallocated stamp. Used for caching between loop iterations. Non-Linear stamps must be updated on ev...
Stamp< T > & generateDCStamp(const Matrix< T > &solutionVector, size_t numCurrents)
Generates the complete DC stamp.
void updateTimeStep(const Matrix< T > &solutionMatrix, const size_t currentSolutionIndex, T timestep)
Updates the components at the end of each time step. Applies to dynamic and non-linear components.
bool dynamicStampIsFresh
A variable used to track if the cached stamp is current.
An ebbers moll diode model.
Definition: Diode.hpp:11
A matrix class with support for LU-decomposition, and left division.
A helper struct to store the preallocated stamps for MNA.
Definition: Component.hpp:23