-
Notifications
You must be signed in to change notification settings - Fork 2
/
CapacitorNode.h
145 lines (131 loc) · 4.15 KB
/
CapacitorNode.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//
// Created by ryanz on 11/11/2022.
//
#ifndef PHYSICSFORMULA_CAPACITORNODE_H
#define PHYSICSFORMULA_CAPACITORNODE_H
#include <iostream>
#include <vector>
#include <cmath>
#include <cassert>
template<typename T>
class CapacitorNode {
public:
T VCC{};
std::vector<T> capacitances;
std::vector<T> storedEnergies;
std::vector<T> voltages;
char type{}; // 'p' for parallel, 's' for series
T eC{}; // equivalent capacitance
T Q{}; // total charge
CapacitorNode() {
capacitances = {};
storedEnergies = {};
voltages = {};
VCC = 0.0;
type = 'p';
eC = 0.0;
Q = 0.0;
}
[[nodiscard]] T calculateTotalCharge() const {
return VCC * eC;
}
CapacitorNode(std::vector<T> c, char t) {
capacitances = std::move(c);
type = t;
VCC = 0.0;
storedEnergies = {};
voltages = {};
eC = calculateEquivalentCapacitance();
Q = calculateTotalCharge();
}
CapacitorNode(std::vector<T> c, T v, char t) {
capacitances = std::move(c);
type = t;
VCC = v;
eC = calculateEquivalentCapacitance();
Q = calculateTotalCharge();
voltages = calculateVoltages();
storedEnergies = calculateStoredEnergy();
}
T calculateEquivalentCapacitance() {
T sum = 0.0;
if (type == 'p') {
for (T capacitance : capacitances) {
sum += capacitance;
}
} else if (type == 's') {
for (T capacitance : capacitances) {
sum += 1.0 / capacitance;
}
sum = 1.0 / sum;
}
return sum;
}
std::vector<T> calculateStoredEnergy() {
std::vector<T> energies;
if (type == 'p') {
for (T capacitance : capacitances) {
energies.push_back((capacitance * pow(VCC, 2)) / 2.0);
}
} else if (type == 's') {
for (int i = 0; i < capacitances.size(); i++) {
energies.push_back((capacitances[i] * pow(voltages[i], 2)) / 2.0);
}
}
return energies;
}
std::vector<T> calculateVoltages() {
std::vector<T> _voltages;
if (type == 'p') {
// in parallel node voltages are equal to the voltage of the
// source across each capacitor
for (T capacitance : capacitances) {
_voltages.push_back(VCC);
}
} else if (type == 's') {
for (T capacitance : capacitances) {
_voltages.push_back((Q / capacitance));
}
}
return _voltages;
}
void setVoltage(T v) {
VCC = v;
storedEnergies = calculateStoredEnergy();
voltages = calculateVoltages();
Q = calculateTotalCharge();
}
void setCapacitances(std::vector<T> c) {
// assert that VCC is not 0
assert(VCC != 0.0);
capacitances = std::move(c);
eC = calculateEquivalentCapacitance();
storedEnergies = calculateStoredEnergy();
voltages = calculateVoltages();
Q = calculateTotalCharge();
}
void print() {
std::cout << string(60, '*') << std::endl;
std::cout << string(25, ' ') << "CapacitorNode" << std::endl;
std::cout << "type: " << (type == 'p' ? "parallel" : "series")
<< std::endl;
std::cout << "VCC: " << VCC << " V" << std::endl;
std::cout << std::endl;
std::cout << "Capacitances: " << std::endl;
for (int i = 0; i < capacitances.size(); i++) {
std::cout << "capacitor " << i + 1 << ": "
<< capacitances[i] << " F, voltage: "
<< voltages[i] << " V, stored energy: "
<< storedEnergies[i] << " J" << std::endl;
}
std::cout << std::endl;
std::cout << "Equivalent Capacitance: " << eC << " F" << std::endl;
std::cout << "Total Charge: " << Q << " C" << std::endl;
std::cout << string(60, '*') << std::endl;
}
// destructor
~CapacitorNode() {
std::cout << "CapacitorNode object destroyed" << std::endl;
}
};
#endif //PHYSICSFORMULA_CAPACITORNODE_H