-
Notifications
You must be signed in to change notification settings - Fork 2
/
CircuitBoard.h
600 lines (577 loc) · 19.9 KB
/
CircuitBoard.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//
// Created by ryanz on 11/9/2022.
//
#ifndef PHYSICSFORMULA_CIRCUITBOARD_H
#define PHYSICSFORMULA_CIRCUITBOARD_H
#include <iostream>
#include <utility>
#include <vector>
#include "ResistorNode.h"
#include "CapacitorNode.h"
namespace CB {
// use graph theory to create a circuit board
struct EMF {
double voltage;
double current;
double power;
};
class Edge {
EMF emf;
};
class Junction {
vector<Junction*> inputs;
vector<Junction*> outputs;
};
}
#endif //PHYSICSFORMULA_CIRCUITBOARD_H
//
//Circuits::voltageAcrossBattery(.020, 125.0, 12.0);
//CB::CapacitorNode c1(1.0, 1.0);
//CB::CapacitorNode c2(1.0, 1.0);
//CB::Resistor r1(1.0, 1.0);
//CB::Resistor r2(1.0, 1.0);
//CB::Inductor i1(1.0, 1.0);
//CB::Inductor i2(1.0, 1.0);
//CB::ResistorNode rn1({r1, r2}, 's');
//CB::InductorNode ln1({i1, i2}, 's');
//CB::VoltageSource vs12(12.0, 1.0);
//CB::CircuitNode cn1({c1, c2}, 's');
//
//
//// abstract base class Component to be used as a base class for all components
//class Component {
//public:
// // pure virtual function
// virtual void print() const = 0;
//
// char type{};
//};
//
//
//class Capacitor : public Component {
//public:
// // capacitance in Farads
// double capacitance{};
// // working voltage in Volts
// double voltage{};
// // tolerance in percent
// double tolerance{};
// // leakage current in Amps
// double leakageCurrent{};
// // working temperature in Celsius
// double temperature{};
// // the polarization of the capacitor
// char polarization{};
//
// // default constructor
// Capacitor() : capacitance(0.0), voltage(0.0), tolerance(0.0),
// leakageCurrent(0.0), temperature(0.0), polarization('p') {
// type = 'c';
// }
// // constructor with parameters
// explicit Capacitor(double c=0.0, double v=0.0, double t=0.0, double l=0.0,
// double temp=0.0, char p='p')
// : capacitance(c), voltage(v), tolerance(t), leakageCurrent(l),
// temperature(temp), polarization(p) {
// type = 'c';
// }
// // copy constructor
// Capacitor(const Capacitor& c)
// : capacitance(c.capacitance), voltage(c.voltage), tolerance(c.tolerance),
// leakageCurrent(c.leakageCurrent), temperature(c.temperature),
// polarization(c.polarization) {
// type = 'c';
// }
//
// // print function
// void print() const override {
// std::cout << "Capacitance: " << capacitance << " Farads" << std::endl;
// std::cout << "Voltage: " << voltage << " Volts" << std::endl;
// std::cout << "Tolerance: " << tolerance << " percent" << std::endl;
// std::cout << "Leakage Current: " << leakageCurrent << " Amps" << std::endl;
// std::cout << "Temperature: " << temperature << " Celsius" << std::endl;
// std::cout << "Polarization: " << polarization << std::endl;
// }
//
// // destructor
// ~Capacitor() = default;
//};
//
//class CapacitorNode : public Component {
//public:
// std::vector<Capacitor> capacitances;
// char type; // 'p' for parallel, 's' for series
// double equivalentCapacitance;
//
// // default constructor
// CapacitorNode() : capacitances(), type('p'), equivalentCapacitance(0.0) {}
// // constructor with parameters
// CapacitorNode(std::vector<Capacitor> c, char t) {
// capacitances = std::move(c);
// type = t;
// equivalentCapacitance = calculateEquivalentCapacitance();
// }
// double calculateEquivalentCapacitance() {
// double sum = 0.0;
// if (type == 'p') {
// for (const Capacitor& c : capacitances) {
// sum += c.capacitance;
// }
// } else if (type == 's') {
// for (const Capacitor& c : capacitances) {
// sum += 1.0 / c.capacitance;
// }
// sum = 1.0 / sum;
// }
// return sum;
// }
//
// // print function to print each capacitor in the node and the equivalent capacitance
// void print() const override {
// std::cout << "CapacitorNode Node: " << std::endl;
// for (Capacitor c : capacitances) {
// c.print();
// }
// std::cout << "Equivalent Capacitance: " << equivalentCapacitance << " Farads" << std::endl;
// }
//};
//
//class Resistor : public Component {
//public:
// // resistance in Ohms
// double resistance{};
// // accuracy
// double accuracy{};
// // stability
// double stability{};
// // reliability
// double reliability{};
// // tolerance
// double tolerance{};
// // voltage coefficient
// double voltageCoefficient{};
// // noise
// double noise{};
// // temperature rating
// double temperatureRating{};
// // thermal resistance
// double thermalResistance{};
//
// // default constructor
// Resistor() : resistance(0.0), accuracy(0.0), stability(0.0), reliability(0.0),
// tolerance(0.0), voltageCoefficient(0.0), noise(0.0), temperatureRating(0.0),
// thermalResistance(0.0) {
// type = 'r';
// }
// // constructor with parameters
// explicit Resistor(double r=0.0, double a=0.0, double s=0.0, double re=0.0,
// double t=0.0, double v=0.0, double n=0.0, double temp=0.0,
// double th=0.0)
// : resistance(r), accuracy(a), stability(s), reliability(re), tolerance(t),
// voltageCoefficient(v), noise(n), temperatureRating(temp), thermalResistance(th) {
// type = 'r';
// }
// // copy constructor
// Resistor(const Resistor& r)
// : resistance(r.resistance), accuracy(r.accuracy), stability(r.stability),
// reliability(r.reliability), tolerance(r.tolerance), voltageCoefficient(r.voltageCoefficient),
// noise(r.noise), temperatureRating(r.temperatureRating), thermalResistance(r.thermalResistance) {
// type = 'r';
// }
//
// // print function
// void print() const override {
// std::cout << "Resistance: " << resistance << " Ohms" << std::endl;
// std::cout << "Accuracy: " << accuracy << std::endl;
// std::cout << "Stability: " << stability << std::endl;
// std::cout << "Reliability: " << reliability << std::endl;
// std::cout << "Tolerance: " << tolerance << std::endl;
// std::cout << "Voltage Coefficient: " << voltageCoefficient << std::endl;
// std::cout << "Noise: " << noise << std::endl;
// std::cout << "Temperature Rating: " << temperatureRating << std::endl;
// std::cout << "Thermal Resistance: " << thermalResistance << std::endl;
// }
//
// // destructor
// ~Resistor() = default;
//};
//
//class ResistorNode : public Component {
//public:
// std::vector<Resistor> resistors;
// char type; // 'p' for parallel, 's' for series
// double equivalentResistance;
//
// ResistorNode() : resistors(), type('p'), equivalentResistance(0.0) {}
// ResistorNode(std::vector<Resistor> r, char t) {
// resistors = r;
// type = t;
// equivalentResistance = calculateEquivalentResistance();
// }
// double calculateEquivalentResistance() {
// double sum = 0.0;
// if (type == 'p') {
// for (const Resistor& resistor : resistors) {
// sum += 1.0 / resistor.resistance;
// }
// sum = 1.0 / sum;
// } else if (type == 's') {
// for (const Resistor& resistor : resistors) {
// sum += resistor.resistance;
// }
//
// }
// return sum;
// }
//
// void print() const override {
// std::cout << "Resistor Node: " << std::endl;
// for (Resistor r : resistors) {
// r.print();
// }
// std::cout << "Equivalent Resistance: " << equivalentResistance << " Ohms" << std::endl;
// }
//};
//
//class Inductor : public Component {
//public:
// // inductance in Henrys
// double inductance{};
// // tolerance in percent
// double tolerance{};
// // working temperature in Celsius
// double temperature{};
//
// // default constructor
// Inductor() : inductance(0.0), tolerance(0.0), temperature(0.0) {
// type = 'i';
// }
// // constructor with parameters
// explicit Inductor(double i=0.0, double t=0.0, double temp=0.0)
// : inductance(i), tolerance(t), temperature(temp) {
// type = 'i';
// }
// // copy constructor
// Inductor(const Inductor& i)
// : inductance(i.inductance), tolerance(i.tolerance), temperature(i.temperature) {
// type = 'i';
// }
//
// // print function
// void print() const override {
// std::cout << "Inductance: " << inductance << " Henrys" << std::endl;
// std::cout << "Tolerance: " << tolerance << std::endl;
// std::cout << "Temperature: " << temperature << std::endl;
// }
//
// // destructor
// ~Inductor() = default;
//};
//
//class InductorNode : public Component {
//public:
// std::vector<Inductor> inductances;
// char type; // 'p' for parallel, 's' for series
// double equivalentInductance;
//
// InductorNode() : inductances(), type('p'), equivalentInductance(0.0) {}
// InductorNode(std::vector<Inductor> i, char t) {
// inductances = i;
// type = t;
// equivalentInductance = calculateEquivalentInductance();
// }
// double calculateEquivalentInductance() {
// double sum = 0.0;
// if (type == 'p') {
// for (const Inductor& inductor : inductances) {
// sum += inductor.inductance;
// }
// } else if (type == 's') {
// for (const Inductor& inductor : inductances) {
// sum += 1.0 / inductor.inductance;
// }
// sum = 1.0 / sum;
// }
// return sum;
// }
//
// void print() const override {
// std::cout << "Inductor Node: " << std::endl;
// for (Inductor i : inductances) {
// i.print();
// }
// }
//};
//
//class VoltageSource : public Component {
//public:
// // voltage in Volts
// double voltage{};
// // tolerance in percent
// double tolerance{};
// // working temperature in Celsius
// double temperature{};
//
// // default constructor
// VoltageSource() : voltage(0.0), tolerance(0.0), temperature(0.0) {
// type = 'v';
// }
// // constructor with parameters
// explicit VoltageSource(double v=0.0, double t=0.0, double temp=0.0)
// : voltage(v), tolerance(t), temperature(temp) {
// type = 'v';
// }
// // copy constructor
// VoltageSource(const VoltageSource& v)
// : voltage(v.voltage), tolerance(v.tolerance), temperature(v.temperature) {
// type = 'v';
// }
//
// // print function
// void print() const override {
// std::cout << "Voltage: " << voltage << " Volts" << std::endl;
// std::cout << "Tolerance: " << tolerance << std::endl;
// std::cout << "Temperature: " << temperature << std::endl;
// }
//};
//
//class CircuitNode {
//public:
// std::vector<Component> components;
// std::vector<CircuitNode> children;
// double voltage;
// double current;
// double resistance;
// double inductance;
// double capacitance;
// double impedance;
// double admittance;
// double power;
// double energy;
//
// CircuitNode() : components(), children(), voltage(0.0),
// current(0.0), resistance(0.0), inductance(0.0), capacitance(0.0),
// impedance(0.0), admittance(0.0), power(0.0), energy(0.0) {}
//
// CircuitNode(std::vector<Component> c, std::vector<CircuitNode> ch) {
// components = std::move(c);
// children = std::move(ch);
// voltage = calculateVoltage();
// current = calculateCurrent();
// resistance = calculateResistance();
// inductance = calculateInductance();
// capacitance = calculateCapacitance();
// impedance = calculateImpedance();
// admittance = calculateAdmittance();
// power = calculatePower();
// energy = calculateEnergy();
// }
//
//// void addComponent(Component c) {
//// components.push_back(c);
//// }
//
//
// double calculateVoltage() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'v') {
// sum += dynamic_cast<VoltageSource&>(const_cast<Component&>(component)).voltage;
// }
// }
// return sum;
// }
//
// double calculateCurrent() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'r') {
// sum += dynamic_cast<Resistor&>(const_cast<Component&>(component)).resistance;
// }
// }
// return voltage / sum;
// }
//
// double calculateResistance() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'r') {
// sum += dynamic_cast<Resistor&>(const_cast<Component&>(component)).resistance;
// }
// }
// return sum;
// }
//
// double calculateInductance() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'i') {
// sum += dynamic_cast<Inductor&>(const_cast<Component&>(component)).inductance;
// }
// }
// return sum;
// }
//
// double calculateCapacitance() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'c') {
// sum += dynamic_cast<Capacitor&>(const_cast<Component&>(component)).capacitance;
// }
// }
// return sum;
// }
//
// double calculateImpedance() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'r') {
// sum += dynamic_cast<Resistor&>(const_cast<Component&>(component)).resistance;
// }
// }
// return sum;
// }
//
// double calculateAdmittance() {
// double sum = 0.0;
// for (const Component& component : components) {
// if (component.type == 'c') {
// sum += 1.0 / dynamic_cast<Capacitor&>(const_cast<Component&>(component)).capacitance;
// }
// }
// return sum;
// }
//
// double calculatePower() const {
// return voltage * current;
// }
//
// double calculateEnergy() const {
// return 0.5 * voltage * current;
// }
//
//
//
// void print() const {
// std::cout << "Circuit Node: " << std::endl;
// // print components which hve the print implemeted in the derived classes
// for (const Component& component : components) {
// component.print();
// }
// // print children
// for (const CircuitNode& child : children) {
// child.print();
// }
// std::cout << "Voltage: " << voltage << std::endl;
// std::cout << "Current: " << current << std::endl;
// std::cout << "Resistance: " << resistance << std::endl;
// std::cout << "Inductance: " << inductance << std::endl;
// std::cout << "Capacitance: " << capacitance << std::endl;
// std::cout << "Impedance: " << impedance << std::endl;
// std::cout << "Admittance: " << admittance << std::endl;
// std::cout << "Power: " << power << std::endl;
// std::cout << "Energy: " << energy << std::endl;
//
// }
////
//// // conect two components in series
//// void connectSeries(Component& c1, Component& c2) {
//// std::vector<Component> components;
//// components.push_back(c1);
//// components.push_back(c2);
//// CircuitNode node(components, std::vector<CircuitNode>());
//// children.push_back(node);
//// }
////
//// // connect two components in parallel
//// void connectParallel(Component& c1, Component& c2) {
//// // create a new circuit node
//// CircuitNode node;
//// // add the components to the node
//// node.components.push_back(*c1);
//// node.components.push_back(*c2);
//// // add the node to the children
//// children.push_back(node);
//// }
//
//};
//
//
//class CircuitBoard {
//public:
// std::vector<CircuitNode*> nodes;
// CircuitBoard() : nodes() {}
// explicit CircuitBoard(std::vector<CircuitNode*> n) : nodes(std::move(n)) {}
//
// // kirchoff's current law using nodal analysis
// void kirchoffCurrentLaw() {
// // for each node
// for (CircuitNode* node : nodes) {
// // for each component in the node
// for (Component& component : node->components) {
// // if the component is a resistor
// if (component.type == 'r') {
// // add the resistance to the node's resistance
// node->resistance += dynamic_cast<Resistor&>(component).resistance;
// }
// // if the component is a capacitor
// if (component.type == 'c') {
// // add the capacitance to the node's capacitance
// node->capacitance += dynamic_cast<Capacitor&>(component).capacitance;
// }
// // if the component is an inductor
// if (component.type == 'i') {
// // add the inductance to the node's inductance
// node->inductance += dynamic_cast<Inductor&>(component).inductance;
// }
// // if the component is a voltage source
// if (component.type == 'v') {
// // add the voltage to the node's voltage
// node->voltage += dynamic_cast<VoltageSource&>(component).voltage;
// }
// }
// // calculate the node's impedance
// node->impedance = node->resistance + (node->inductance * node->capacitance);
// // calculate the node's admittance
// node->admittance = 1.0 / node->impedance;
// // calculate the node's current
// node->current = node->voltage / node->impedance;
// // calculate the node's power
// node->power = node->voltage * node->current;
// // calculate the node's energy
// node->energy = node->power * 1.0 / 2.0;
// }
// }
//
//// void kirchoffCurrentLaw() {
//// // for each node
//// for (CircuitNode* node : nodes) {
//// // for each component in the node
//// for (Component* component : node->components) {
//// // if the component is a resistor
//// if (dynamic_cast<Resistor*>(component)) {
//// // add the resistance to the node's resistance
//// node->resistance += dynamic_cast<Resistor*>(component)->resistance;
//// }
//// // if the component is a capacitor
//// if (dynamic_cast<CapacitorNode*>(component)) {
//// // add the capacitance to the node's capacitance
//// node->capacitance += dynamic_cast<CapacitorNode*>(component)->capacitance;
//// }
//// // if the component is an inductor
//// if (dynamic_cast<Inductor*>(component)) {
//// // add the inductance to the node's inductance
//// node->inductance += dynamic_cast<Inductor*>(component)->inductance;
//// }
//// }
//// }
//// }
//
// void print() {
// std::cout << "Circuit Board: " << std::endl;
// for (CircuitNode* n : nodes) {
// n->print();
// }
// }
//
//};