JUK1
Resistor.hpp
Go to the documentation of this file.
1 #ifndef _RESISTOR_HPP_INC_
2 #define _RESISTOR_HPP_INC_
5 
9 template<typename T>
10 struct Resistor : public Component<T> {
11  public:
12  T value = 0;
13 
14  size_t n1 = 0;
15  size_t n2 = 0;
16  size_t currentIndex = 0;
17 
18  bool group1 = true;
19 
20  void addStaticStampTo(Stamp<T> & stamp) const {
21  // the p means prime ( ' ) and is used for the index - 1
22  size_t n1p = n1 - 1;
23  size_t n2p = n2 - 1;
24  size_t currentIndexp = currentIndex - 1;
25 
26  if (group1) {
27  T conductance = 1 / value;
28 
29  if (n1) {
30  stamp.G(n1p, n1p) += conductance;
31  }
32  if (n2) {
33  stamp.G(n2p, n2p) += conductance;
34  }
35  if (n1 && n2) {
36  stamp.G(n1p, n2p) += -conductance;
37  stamp.G(n2p, n1p) += -conductance;
38  }
39  } else {
40  if (n1) {
41  stamp.G(n1p, stamp.sizeG_A + currentIndexp) += 1;
42  stamp.G(stamp.sizeG_A + currentIndexp, n1p) += 1;
43  }
44  if (n2) {
45  stamp.G(n2p, stamp.sizeG_A + currentIndexp) += -1;
46  stamp.G(stamp.sizeG_A + currentIndexp, n2p) += -1;
47  }
48 
49  stamp.G(stamp.sizeG_A + currentIndexp,
50  stamp.sizeG_A + currentIndexp) += -value;
51  }
52  }
53 
54  void addDCAnalysisStampTo(Stamp<T> & stamp, const Matrix<T> & solutionVector,
55  size_t numCurrents) const {
56  addStaticStampTo(stamp);
57  }
58 
59  static void
60  addToElements(const std::string & line, CircuitElements<T> & elements,
61  size_t & numNodes, size_t & numCurrents, size_t & numDCCurrents) {
62  // std::regex resistorRegex(
63  // R"(^R(.*?)\s(\d+?)\s(\d+?)\s(.+?)(?:\s(.))?\s?$)"
64  // );
65  std::regex resistorRegex = generateRegex("R", "n n w ? c");
66  Resistor<T> resistor;
67  std::smatch matches;
68 
69  std::regex_match(line, matches, resistorRegex);
70 
71  resistor.designator = "R";
72  resistor.designator += matches.str(1);
73 
74  resistor.n1 = std::stoi(matches.str(2));
75  resistor.n2 = std::stoi(matches.str(3));
76 
77  numNodes = std::max(numNodes, std::stoull(matches.str(2)));
78  numNodes = std::max(numNodes, std::stoull(matches.str(3)));
79 
80  if constexpr (std::is_same_v<T, double> || std::is_same_v<T, float>) {
81  resistor.value = std::stod(matches.str(4));
82  } else {
83  static_assert("Unsupported Type");
84  }
85 
86  if (matches.size() < 6 && matches.str(5) != "") {
87  resistor.group1 = false;
88  resistor.currentIndex = ++numCurrents;
89  } else {
90  resistor.group1 = true;
91  }
92 
93  elements.staticElements.emplace_back(
94  std::make_shared<Resistor<T> >(resistor));
95 
96  elements.nodeComponentMap.insert(
97  {{resistor.n1, elements.staticElements.back()},
98  {resistor.n2, elements.staticElements.back()}});
99  }
100 };
101 
102 #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
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.
An ideal resistor model.
Definition: Resistor.hpp:10
size_t currentIndex
Definition: Resistor.hpp:16
void addDCAnalysisStampTo(Stamp< T > &stamp, const Matrix< T > &solutionVector, size_t numCurrents) const
adds this component's DC stamp to the target stamp.
Definition: Resistor.hpp:54
bool group1
Definition: Resistor.hpp:18
static void addToElements(const std::string &line, CircuitElements< T > &elements, size_t &numNodes, size_t &numCurrents, size_t &numDCCurrents)
Definition: Resistor.hpp:60
size_t n1
Definition: Resistor.hpp:14
void addStaticStampTo(Stamp< T > &stamp) const
Adds this component's static stamp to the target stamp.
Definition: Resistor.hpp:20
size_t n2
Definition: Resistor.hpp:15
A helper struct to store the preallocated stamps for MNA.
Definition: Component.hpp:23
size_t sizeG_A
Definition: Component.hpp:24
Matrix< T > G
Definition: Component.hpp:27