JUK1
Capacitor.hpp
Go to the documentation of this file.
1 #ifndef _CAPACITOR_HPP_INC_
2 #define _CAPACITOR_HPP_INC_
5 
6 
10 template<typename T>
11 struct Capacitor : public Component<T> {
12  public:
13  T value = 0;
14 
15  size_t n1 = 0;
16  size_t n2 = 0;
17  T lastCurrent = 0;
18 
19  bool trapezoidalRule = true;
20  void addDynamicStampTo(Stamp<T> & stamp, const Matrix<T> & solutionMatrix,
21  const size_t currentSolutionIndex, T timestep) const {
22  size_t n1p = n1 - 1;
23  size_t n2p = n2 - 1;
24 
25  T u0 = 0;
26  if (n1) {
27  u0 = solutionMatrix(n1p, currentSolutionIndex - 1);
28  }
29 
30  if (n2) {
31  u0 -= solutionMatrix(n2p, currentSolutionIndex - 1);
32  }
33 
34 
35  T G_eq = 0;
36  T I_eq = 0;
37 
38  if (trapezoidalRule) {
39  G_eq = 2 * value / timestep;
40  I_eq = lastCurrent + G_eq * u0;
41  } else {
42  G_eq = value / timestep;
43  I_eq = value * u0 / timestep;
44  }
45 
46  if (n1) {
47  stamp.G(n1p, n1p) += G_eq;
48  stamp.s(n1p, 0) += I_eq;
49  }
50 
51  if (n2) {
52  stamp.G(n2p, n2p) += G_eq;
53  stamp.s(n2p, 0) += -I_eq;
54  }
55 
56  if (n1 && n2) {
57  stamp.G(n1p, n2p) += -G_eq;
58  stamp.G(n2p, n1p) += -G_eq;
59  }
60  }
61 
62  void updateStoredState(const Matrix<T> & solutionMatrix,
63  const size_t currentSolutionIndex, T timestep,
64  size_t sizeG_A) {
65  if (trapezoidalRule) {
66  size_t n1p = n1 - 1;
67  size_t n2p = n2 - 1;
68  T u0 = 0;
69  T u1 = 0;
70  if (n1) {
71  u0 = solutionMatrix(n1p, currentSolutionIndex - 1);
72  u1 = solutionMatrix(n1p, currentSolutionIndex);
73  }
74 
75  if (n2) {
76  u0 -= solutionMatrix(n2p, currentSolutionIndex - 1);
77  u1 -= solutionMatrix(n2p, currentSolutionIndex);
78  }
79 
80  T G_eq = 2 * value / timestep;
81  lastCurrent = G_eq * u1 - (lastCurrent + G_eq * u0);
82  }
83  }
84 
85  void addDCAnalysisStampTo(Stamp<T> & stamp, const Matrix<T> & solutionVector,
86  size_t numCurrents) const {
87  // open circuit. Maybe add large connection to ref if unstable
88  if (n1 > 0) {
89  stamp.G(n1 - 1, n1 - 1) += 1e-9;
90  }
91  if (n2 > 0) {
92  stamp.G(n2 - 1, n2 - 1) += 1e-9;
93  }
94  }
95 
96  static void
97  addToElements(const std::string & line, CircuitElements<T> & elements,
98  size_t & numNodes, size_t & numCurrents, size_t & numDCCurrents) {
99  // std::regex capacitorRegex( R"(^C(.*?)\s(\d+?)\s(\d+?)\s(.+?)\s?$)" );
100  std::regex capacitorRegex = generateRegex("C", "n n w");
101  Capacitor<T> capacitor;
102  std::smatch matches;
103 
104  std::regex_match(line, matches, capacitorRegex);
105 
106  capacitor.designator = "C";
107  capacitor.designator += matches.str(1);
108 
109  capacitor.n1 = std::stoi(matches.str(2));
110  capacitor.n2 = std::stoi(matches.str(3));
111  capacitor.trapezoidalRule = true;
112 
113  numNodes = std::max(numNodes, std::stoull(matches.str(2)));
114  numNodes = std::max(numNodes, std::stoull(matches.str(3)));
115 
116  if constexpr (std::is_same_v<T, double> || std::is_same_v<T, float>) {
117  capacitor.value = std::stod(matches.str(4));
118  } else {
119  static_assert("Unsupported Type");
120  }
121 
122  elements.dynamicElements.emplace_back(
123  std::make_shared<Capacitor<T> >(capacitor));
124 
125  elements.nodeComponentMap.insert(
126  {{capacitor.n1, elements.dynamicElements.back()},
127  {capacitor.n2, elements.dynamicElements.back()}});
128  }
129 };
130 
131 
132 #endif
std::regex generateRegex(std::string indentifier, std::string simplifiedMatching, bool startAnchor=true, bool endAnchor=true)
a helper function to aid in the construction of regexes for parsing netlist files
common weighting for all matrix elements
Definition: QPpassive.m:47
An ideal capacitor model.
Definition: Capacitor.hpp:11
size_t n2
Definition: Capacitor.hpp:16
void updateStoredState(const Matrix< T > &solutionMatrix, const size_t currentSolutionIndex, T timestep, size_t sizeG_A)
Updates any stored state based on the current solution index.
Definition: Capacitor.hpp:62
size_t n1
Definition: Capacitor.hpp:15
void addDynamicStampTo(Stamp< T > &stamp, const Matrix< T > &solutionMatrix, const size_t currentSolutionIndex, T timestep) const
Adds this component's dynamic stamp to the target stamp.
Definition: Capacitor.hpp:20
void addDCAnalysisStampTo(Stamp< T > &stamp, const Matrix< T > &solutionVector, size_t numCurrents) const
adds this component's DC stamp to the target stamp.
Definition: Capacitor.hpp:85
bool trapezoidalRule
Definition: Capacitor.hpp:19
static void addToElements(const std::string &line, CircuitElements< T > &elements, size_t &numNodes, size_t &numCurrents, size_t &numDCCurrents)
Definition: Capacitor.hpp:97
a glorified container for the different types of components.
A template base class to define the fundamental things a component should define.
Definition: Component.hpp:108
std::string designator
The designator as in the netlist for e.g.
Definition: Component.hpp:110
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
Matrix< T > s
Definition: Component.hpp:28
Matrix< T > G
Definition: Component.hpp:27