-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathreader.py
More file actions
102 lines (90 loc) · 3.94 KB
/
Copy pathreader.py
File metadata and controls
102 lines (90 loc) · 3.94 KB
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
# Input json file reading in programs format
import json as js
from model.impedances import Resistor
from model.impedances import Inductor
from model.impedances import Capacitor
from model.source import Voltage
from model.source import Current
from model.node import Node
Rth = 1.E-30
class Reader:
# the json file must be valid, the validator does this process
def __init__(self, json):
# the parameter json is the json file path
try:
with open(json, 'r') as f:
self.json = js.load(f)
self.data = {
'stop': self.json['parameters']['stop'],
'dT': self.json['parameters']['dT'],
'frequency': self.json['parameters']['frequency']
} # consolidated dictionary with converted objects
except Exception as error:
print(error)
self.json = {}
def readNodes(self):
# reads the components in node
nodes = self.json['nodes']
self.data['nodes'] = []
for node in nodes:
self.data['nodes'].append(Node.json(node, nodes[node]))
print('Nodes processed: ')
for node in self.data['nodes']:
node.log()
def readCircuits(self):
# reads the components in circuits
circuits = self.json['circuits']
self.data['circuits'] = []
for circuit in circuits:
if 'resistance' in circuits[circuit]:
self.data['circuits'].append(Resistor.json(circuit, circuits[circuit]))
if 'inductance' in circuits[circuit]:
self.data['circuits'].append(Inductor.json(circuit, circuits[circuit]))
if 'capacitance' in circuits[circuit]:
self.data['circuits'].append(Capacitor.json(circuit, circuits[circuit]))
print('Circuits processed: ')
for circuit in self.data['circuits']:
circuit.log()
def readSources(self):
# reads the components in sources
frequency = self.json['parameters']['frequency']
sources = self.json['sources']
self.data['sources'] = []
for source in sources:
if 'voltage' in sources[source]:
self.data['sources'].append(Voltage.json(source, sources[source], frequency))
if 'current' in sources[source]:
self.data['sources'].append(Current.json(source, sources[source], frequency))
print('Sources processed: ')
for source in self.data['sources']:
source.log()
# preparing sources for simulation, converting all voltage sources into current sources
def convertSources(self):
for i in range(len(self.data['sources'])):
source = self.data['sources'][i]
if str(type(source).__name__) == 'Voltage':
source = Current(source.name, source.id, source.f_id, source.t_id, source.voltage / Rth, source.angle, source.frequency)
thevenin = Resistor('fictional thevenin', 'th <> ' + source.id, source.f_id, source.t_id, Rth)
self.data['circuits'].append(thevenin)
if source.f_id != '0':
for node in self.data['nodes']:
if node.id == source.f_id:
node.circuits.append(thevenin.id)
if source.t_id != '0':
for node in self.data['nodes']:
if node.id == source.t_id:
node.circuits.append(thevenin.id)
self.data['sources'][i] = source
# print(source.current)
print('Sources converted!')
# print(self.data)
# presenting systems consolidated data
def log(self):
print('\nSystem data: ')
print('Frequency: ' + str(self.data['frequency']))
for node in self.data['nodes']:
node.log()
for circuit in self.data['circuits']:
circuit.log()
for source in self.data['sources']:
source.log()