JUK1
SinusoidalVoltageSource.hpp
Go to the documentation of this file.
1 #ifndef _SINUSOIDALVOLTAGESOURCE_INC_
2 #define _SINUSOIDALVOLTAGESOURCE_INC_
5 #include <math.h>
6 
10 template<typename T>
11 struct SinusoidalVoltageSource : public Component<T> {
12  size_t n1 = 0;
13  size_t n2 = 0;
14  size_t currentIndex = 0;
15 
16  T V = 1;
17  T phase = 0;
18  T frequency = 1;
19  T offset = 0;
20  bool degrees = true;
21 
22  void addDynamicStampTo(Stamp<T> & stamp, const Matrix<T> & solutionMatrix,
23  const size_t currentSolutionIndex, T timestep) const {
24  size_t n1p = n1 - 1;
25  size_t n2p = n2 - 1;
26  size_t currentIndexp = currentIndex - 1;
27 
28  if (n1) {
29  stamp.G(n1p, stamp.sizeG_A + currentIndexp) += 1;
30  stamp.G(stamp.sizeG_A + currentIndexp, n1p) += 1;
31  }
32 
33  if (n2) {
34  stamp.G(n2p, stamp.sizeG_A + currentIndexp) += -1;
35  stamp.G(stamp.sizeG_A + currentIndexp, n2p) += -1;
36  }
37 
38  if (degrees) {
39  stamp.s(stamp.sizeG_A + currentIndexp,
40  0) += offset + V * std::sin(2 * std::numbers::pi * frequency *
41  currentSolutionIndex * timestep +
42  std::numbers::pi * phase / 180);
43  } else {
44  stamp.s(stamp.sizeG_A + currentIndexp,
45  0) += offset + V * std::sin(2 * std::numbers::pi * frequency *
46  currentSolutionIndex * timestep +
47  phase);
48  }
49  }
50 
51  void updateStoredState(const Matrix<T> & solutionMatrix,
52  const size_t currentSolutionIndex, T timestep,
53  size_t sizeG_A) {
54  }
55 
56  void addDCAnalysisStampTo(Stamp<T> & stamp, const Matrix<T> & solutionVector,
57  size_t numCurrents) const {
58  addDynamicStampTo(stamp, solutionVector, 0, 0);
59  }
60 
61  static void
62  addToElements(const std::string & line, CircuitElements<T> & elements,
63  size_t & numNodes, size_t & numCurrents, size_t & numDCCurrents) {
64  // n1 n2 amp freq off phase
65  std::regex sinusoidalVoltageSourceRegex = generateRegex("VS",
66  "n n ? w w w w");
67 
68  SinusoidalVoltageSource<T> voltageSource;
69  std::smatch matches;
70 
71  std::regex_match(line, matches, sinusoidalVoltageSourceRegex);
72 
73  voltageSource.designator = "VS";
74  voltageSource.designator += matches.str(1);
75 
76  voltageSource.n1 = std::stoi(matches.str(2));
77  voltageSource.n2 = std::stoi(matches.str(3));
78 
79  numNodes = std::max(numNodes, std::stoull(matches.str(2)));
80  numNodes = std::max(numNodes, std::stoull(matches.str(3)));
81 
82  if constexpr (std::is_same_v<T, double> || std::is_same_v<T, float>) {
83  voltageSource.V = std::stod(matches.str(4));
84  } else {
85  static_assert("Unsupported Type");
86  }
87 
88  if (matches.size() > 4 && matches.str(5) != "") {
89  voltageSource.frequency = std::stod(matches.str(5));
90  }
91 
92  if (matches.size() > 5 && matches.str(6) != "") {
93  voltageSource.offset = std::stod(matches.str(6));
94  }
95 
96  if (matches.size() > 6 && matches.str(7) != "") {
97  voltageSource.phase = std::stod(matches.str(7));
98  }
99 
100  voltageSource.currentIndex = ++numCurrents;
101 
102  elements.dynamicElements.emplace_back(
103  std::make_shared<SinusoidalVoltageSource<T> >(voltageSource));
104  elements.nodeComponentMap.insert(
105  {{voltageSource.n1, elements.dynamicElements.back()},
106  {voltageSource.n2, elements.dynamicElements.back()}});
107  }
108 };
109 
110 #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
pi
Definition: Freq.m:24
common weighting for all matrix elements
Definition: QPpassive.m:47
ang(col sin()
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 sinusoidal voltage source model.
static void addToElements(const std::string &line, CircuitElements< T > &elements, size_t &numNodes, size_t &numCurrents, size_t &numDCCurrents)
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.
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.
void addDCAnalysisStampTo(Stamp< T > &stamp, const Matrix< T > &solutionVector, size_t numCurrents) const
adds this component's DC stamp to the target stamp.
A helper struct to store the preallocated stamps for MNA.
Definition: Component.hpp:23
Matrix< T > s
Definition: Component.hpp:28
size_t sizeG_A
Definition: Component.hpp:24
Matrix< T > G
Definition: Component.hpp:27