-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathplot_flyback_pyom.py
More file actions
238 lines (209 loc) · 7.74 KB
/
plot_flyback_pyom.py
File metadata and controls
238 lines (209 loc) · 7.74 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
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
#!/usr/bin/env python3
"""
Flyback Transformer Design Visualization using PyOpenMagnetics
===============================================================
Uses the native SVG plotting functions from PyOpenMagnetics.
"""
import json
import PyOpenMagnetics
def create_flyback_magnetic():
"""Create the flyback transformer magnetic structure for plotting."""
# Core specification: E 25/13/7 with 3C95 material and gap
core = {
"functionalDescription": {
"type": "two-piece set",
"material": "3C95",
"shape": "E 25/13/7",
"gapping": [
{
"type": "subtractive",
"length": 0.00013 # 0.13mm gap
}
],
"numberStacks": 1
}
}
# Coil/winding specification
coil = {
"functionalDescription": [
{
"name": "Primary",
"numberTurns": 45,
"numberParallels": 1,
"isolationSide": "primary",
"wire": "Round 0.35 - Grade 1"
},
{
"name": "Secondary",
"numberTurns": 4,
"numberParallels": 2,
"isolationSide": "secondary",
"wire": "Round 0.6 - Grade 1"
}
]
}
# Operating point for field visualization
operating_point = {
"conditions": {
"ambientTemperature": 25
},
"excitationsPerWinding": [
{
"name": "Primary",
"frequency": 100000,
"current": {
"waveform": {
"data": [0, 0.59, 0, 0],
"time": [0, 4.5e-6, 4.5e-6, 10e-6]
}
},
"voltage": {
"waveform": {
"data": [222, 222, -148, -148],
"time": [0, 4.5e-6, 4.5e-6, 10e-6]
}
}
},
{
"name": "Secondary",
"frequency": 100000,
"current": {
"waveform": {
"data": [0, 0, 7.3, 0],
"time": [0, 4.5e-6, 4.5e-6, 10e-6]
}
},
"voltage": {
"waveform": {
"data": [0, 0, 12, 12],
"time": [0, 4.5e-6, 4.5e-6, 10e-6]
}
}
}
]
}
magnetic = {
"core": core,
"coil": coil
}
return magnetic, operating_point
def save_svg(svg_content, filename):
"""Save SVG content to file."""
# Handle dict response with 'data' key
if isinstance(svg_content, dict):
if 'data' in svg_content:
svg_content = svg_content['data']
elif 'svg' in svg_content:
svg_content = svg_content['svg']
else:
svg_content = json.dumps(svg_content, indent=2)
with open(filename, 'w') as f:
f.write(svg_content if isinstance(svg_content, str) else str(svg_content))
print(f" Saved: {filename}")
def main():
print("=" * 60)
print(" FLYBACK TRANSFORMER VISUALIZATION (PyOpenMagnetics)")
print(" 220V AC → 12V @ 1A (12W)")
print("=" * 60)
print()
magnetic, operating_point = create_flyback_magnetic()
output_dir = "/home/alf/OpenMagnetics/PyMKF/examples"
# Process the core to get full data
print("Processing core data...")
try:
core_processed = PyOpenMagnetics.calculate_core_data(
json.dumps(magnetic["core"]),
True
)
print(f" Core processed successfully")
# Debug: check what we got
if isinstance(core_processed, dict):
print(f" Core keys: {list(core_processed.keys())[:5]}...")
except Exception as e:
print(f" Error processing core: {e}")
core_processed = magnetic["core"]
# 1. Plot Core (3D SVG)
print("\n[1/5] Generating core 3D view...")
try:
svg_core = PyOpenMagnetics.plot_core(json.dumps(core_processed), True)
save_svg(svg_core, f"{output_dir}/flyback_core_3d.svg")
except Exception as e:
print(f" Error: {e}")
# 2. Plot Core 2D cross-section
print("\n[2/5] Generating core 2D cross-section...")
try:
svg_core_2d = PyOpenMagnetics.plot_core_2d(json.dumps(core_processed), 1, None, True)
save_svg(svg_core_2d, f"{output_dir}/flyback_core_2d.svg")
except Exception as e:
print(f" Error: {e}")
# 3. Process and plot coil
print("\n[3/5] Generating coil 2D view...")
try:
# Wind the coil first
inputs = {
"designRequirements": {
"magnetizingInductance": {"nominal": 800e-6},
"turnsRatios": [{"nominal": 12.37}]
},
"operatingPoints": [operating_point]
}
wound_result = PyOpenMagnetics.wind_coil_based_on_number_turns_and_layers(
json.dumps(magnetic["coil"]),
json.dumps(core_processed),
1 # 1 layer
)
if isinstance(wound_result, dict) and 'data' in wound_result:
wound_coil = json.loads(wound_result['data']) if isinstance(wound_result['data'], str) else wound_result['data']
else:
wound_coil = wound_result
svg_coil = PyOpenMagnetics.plot_coil_2d(json.dumps(wound_coil), 1, True, True)
save_svg(svg_coil, f"{output_dir}/flyback_coil_2d.svg")
except Exception as e:
print(f" Error: {e}")
# 4. Plot wire cross-section
print("\n[4/5] Generating wire cross-section...")
try:
# Get wire data
wire = PyOpenMagnetics.find_wire_by_name("Round 0.35 - Grade 1")
if isinstance(wire, dict) and 'data' in wire:
wire_data = wire['data']
else:
wire_data = wire
svg_wire = PyOpenMagnetics.plot_wire(json.dumps(wire_data) if isinstance(wire_data, dict) else wire_data, True)
save_svg(svg_wire, f"{output_dir}/flyback_wire.svg")
except Exception as e:
print(f" Error: {e}")
# 5. Try to create a complete magnetic and plot field
print("\n[5/5] Attempting field visualization...")
try:
# Create full magnetic structure
full_magnetic = {
"core": core_processed if isinstance(core_processed, dict) else magnetic["core"],
"coil": wound_coil if 'wound_coil' in dir() else magnetic["coil"]
}
svg_field = PyOpenMagnetics.plot_field_2d(
json.dumps(full_magnetic),
json.dumps(operating_point),
1,
True
)
save_svg(svg_field, f"{output_dir}/flyback_field_2d.svg")
except Exception as e:
print(f" Error (field plot requires complete winding): {e}")
print()
print("=" * 60)
print("VISUALIZATION COMPLETE")
print("=" * 60)
print("\nSVG files can be opened in any web browser or vector editor.")
print()
# List generated files
import os
svg_files = [f for f in os.listdir(output_dir) if f.startswith('flyback_') and f.endswith('.svg')]
if svg_files:
print("Generated SVG files:")
for f in sorted(svg_files):
filepath = os.path.join(output_dir, f)
size = os.path.getsize(filepath)
print(f" • {f} ({size:,} bytes)")
if __name__ == '__main__':
main()