-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcomplete_simulation_example.py
More file actions
538 lines (465 loc) · 17.2 KB
/
Copy pathcomplete_simulation_example.py
File metadata and controls
538 lines (465 loc) · 17.2 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
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
#!/usr/bin/env python3
"""
PyOpenMagnetics - Complete Design, Simulation, and Visualization Example
This example demonstrates:
1. Manual magnetic component design (without adviser)
2. Core and winding loss calculations
3. Inductance verification
4. SVG visualization of all components
5. Magnetic and electric field plotting
Usage:
python complete_simulation_example.py
"""
import PyOpenMagnetics
import json
import os
def create_manual_design():
"""
Create a manual transformer design for a flyback converter.
This bypasses the adviser and creates the design directly.
"""
print("=" * 70)
print("CREATING MANUAL TRANSFORMER DESIGN")
print("=" * 70)
# Design specifications
specs = {
"L_m": 1000e-6, # 1 mH magnetizing inductance
"turns_ratio": 10.0, # 10:1
"f_sw": 100000, # 100 kHz
"V_in": 200, # Input voltage
"I_pri": 0.5, # Primary current
}
# Step 1: Select core
print("\n[1] Selecting core...")
shape = PyOpenMagnetics.find_core_shape_by_name("E 42/21/15")
material = PyOpenMagnetics.find_core_material_by_name("3C95")
print(f" Core: E 42/21/15 with 3C95 material")
# Step 2: Calculate turns and gap
print("\n[2] Calculating turns and gap...")
N_pri = 40 # Primary turns
N_sec = int(N_pri / specs["turns_ratio"]) # Secondary turns
# Calculate gap for target inductance
# L = N^2 * AL, and AL ≈ μ0 * Ae / gap
Ae = 181e-6 # m² (E 42/21/15 effective area)
mu_0 = 4 * 3.14159 * 1e-7
AL_target = specs["L_m"] / (N_pri ** 2)
gap_length = mu_0 * Ae / AL_target
print(f" Primary turns: {N_pri}")
print(f" Secondary turns: {N_sec}")
print(f" Air gap: {gap_length*1000:.3f} mm")
# Step 3: Create core specification
core_data = {
"functionalDescription": {
"type": "two-piece set",
"shape": shape,
"material": material,
"gapping": [{"type": "subtractive", "length": gap_length}],
"numberStacks": 1
}
}
# Calculate complete core data
core = PyOpenMagnetics.calculate_core_data(core_data, False)
# Get effective parameters
if "processedDescription" in core:
eff_params = core["processedDescription"].get("effectiveParameters", {})
Ae_actual = eff_params.get("effectiveArea", Ae)
le = eff_params.get("effectiveLength", 0)
print(f" Effective area: {Ae_actual*1e6:.1f} mm²")
print(f" Effective length: {le*1000:.1f} mm")
# Step 4: Select wires
print("\n[3] Selecting wires...")
try:
primary_wire = PyOpenMagnetics.find_wire_by_name("Round 0.40 - Grade 1")
secondary_wire = PyOpenMagnetics.find_wire_by_name("Round 0.80 - Grade 1")
print(f" Primary: Round 0.40mm")
print(f" Secondary: Round 0.80mm")
except:
# Fallback if wire names don't match
primary_wire = {"type": "round", "name": "Round 0.40mm"}
secondary_wire = {"type": "round", "name": "Round 0.80mm"}
# Step 5: Create coil
print("\n[4] Creating coil...")
coil_data = {
"functionalDescription": [
{
"name": "Primary",
"numberTurns": N_pri,
"numberParallels": 1,
"wire": primary_wire,
"isolationSide": "primary"
},
{
"name": "Secondary",
"numberTurns": N_sec,
"numberParallels": 1,
"wire": secondary_wire,
"isolationSide": "secondary"
}
]
}
# Create magnetic
magnetic = {
"core": core,
"coil": coil_data
}
print("✓ Manual design created successfully")
return magnetic, specs
def simulate_losses(magnetic, specs):
"""
Calculate core and winding losses.
Args:
magnetic: Magnetic design dict
specs: Design specifications
Returns:
dict: Simulation results
"""
print("\n" + "=" * 70)
print("SIMULATING LOSSES")
print("=" * 70)
# Create operating point
operating_point = {
"name": "Nominal",
"conditions": {"ambientTemperature": 40},
"excitationsPerWinding": [
{
"name": "Primary",
"frequency": specs["f_sw"],
"current": {
"processed": {
"label": "Triangular",
"dutyCycle": 0.45,
"offset": specs["I_pri"],
"peakToPeak": specs["I_pri"] * 0.4
}
}
}
]
}
# Create inputs for loss calculation
inputs = {
"designRequirements": {
"magnetizingInductance": {"nominal": specs["L_m"]},
"turnsRatios": [{"nominal": specs["turns_ratio"]}]
},
"operatingPoints": [operating_point]
}
# Process inputs
processed_result = PyOpenMagnetics.process_inputs(inputs)
if isinstance(processed_result, dict) and "data" in processed_result:
processed_inputs = processed_result["data"]
else:
processed_inputs = processed_result
# Calculate core losses
print("\n[1] Calculating core losses...")
models = {
"coreLosses": "IGSE",
"reluctance": "ZHANG"
}
try:
core_losses = PyOpenMagnetics.calculate_core_losses(
magnetic["core"],
magnetic["coil"],
processed_inputs,
models
)
print(f" Core losses: {core_losses.get('coreLosses', 0):.3f} W")
print(f" Peak flux density: {core_losses.get('magneticFluxDensityPeak', 0)*1000:.1f} mT")
print(f" Temperature rise: {core_losses.get('maximumCoreTemperatureRise', 0):.1f} K")
except Exception as e:
print(f" ✗ Core loss calculation failed: {e}")
core_losses = {}
# Calculate winding losses
print("\n[2] Calculating winding losses...")
try:
winding_losses = PyOpenMagnetics.calculate_winding_losses(
magnetic,
operating_point,
temperature=80
)
print(f" Total winding losses: {winding_losses.get('windingLosses', 0):.3f} W")
if "windingLossesPerWinding" in winding_losses:
for i, loss in enumerate(winding_losses["windingLossesPerWinding"]):
print(f" Winding {i+1} losses: {loss:.3f} W")
except Exception as e:
print(f" ✗ Winding loss calculation failed: {e}")
winding_losses = {}
# Calculate total losses
total_core = core_losses.get("coreLosses", 0)
total_winding = winding_losses.get("windingLosses", 0)
total_losses = total_core + total_winding
print(f"\n[3] Total losses: {total_losses:.3f} W")
print(f" Core: {total_core:.3f} W ({total_core/total_losses*100:.1f}%)")
print(f" Winding: {total_winding:.3f} W ({total_winding/total_losses*100:.1f}%)")
return {
"core_losses": core_losses,
"winding_losses": winding_losses,
"total_losses": total_losses
}
def verify_inductance(magnetic, specs):
"""
Verify the actual inductance of the design.
Args:
magnetic: Magnetic design dict
specs: Design specifications
"""
print("\n" + "=" * 70)
print("VERIFYING INDUCTANCE")
print("=" * 70)
# Create a simple operating point for inductance calculation
operating_point = {
"name": "Inductance Check",
"conditions": {"ambientTemperature": 25},
"excitationsPerWinding": [
{
"name": "Primary",
"frequency": specs["f_sw"],
"current": {
"processed": {
"label": "DC",
"dutyCycle": 0.5,
"offset": 0.1,
"peakToPeak": 0.01
}
}
}
]
}
models = {"reluctance": "ZHANG"}
try:
L_actual = PyOpenMagnetics.calculate_inductance_from_number_turns_and_gapping(
magnetic["core"],
magnetic["coil"],
operating_point,
models
)
L_target = specs["L_m"]
error = abs(L_actual - L_target) / L_target * 100
print(f"\n Target inductance: {L_target*1e6:.1f} µH")
print(f" Actual inductance: {L_actual*1e6:.1f} µH")
print(f" Error: {error:.1f}%")
if error < 10:
print(" ✓ Inductance within acceptable range")
else:
print(" ⚠ Inductance deviation is high - adjust gap")
except Exception as e:
print(f" ✗ Inductance calculation failed: {e}")
def visualize_design(magnetic, output_dir="output"):
"""
Generate comprehensive visualizations of the design.
Args:
magnetic: Magnetic design dict
output_dir: Directory to save SVG files
"""
print("\n" + "=" * 70)
print("GENERATING VISUALIZATIONS")
print("=" * 70)
# Create output directory
os.makedirs(output_dir, exist_ok=True)
# 1. Plot core
print("\n[1] Plotting core...")
try:
result = PyOpenMagnetics.plot_core(magnetic["core"], use_colors=True)
if result.get('success'):
filename = os.path.join(output_dir, "01_core.svg")
with open(filename, 'w') as f:
f.write(result['svg'])
print(f" ✓ Saved: {filename}")
else:
print(f" ✗ Failed: {result.get('error', 'Unknown error')}")
except Exception as e:
print(f" ✗ Error: {e}")
# 2. Plot wire (primary)
print("\n[2] Plotting primary wire...")
try:
wire = magnetic["coil"]["functionalDescription"][0]["wire"]
if isinstance(wire, dict):
result = PyOpenMagnetics.plot_wire(wire)
if result.get('success'):
filename = os.path.join(output_dir, "02_wire_primary.svg")
with open(filename, 'w') as f:
f.write(result['svg'])
print(f" ✓ Saved: {filename}")
except Exception as e:
print(f" ✗ Error: {e}")
# 3. Plot wire (secondary)
print("\n[3] Plotting secondary wire...")
try:
wire = magnetic["coil"]["functionalDescription"][1]["wire"]
if isinstance(wire, dict):
result = PyOpenMagnetics.plot_wire(wire)
if result.get('success'):
filename = os.path.join(output_dir, "03_wire_secondary.svg")
with open(filename, 'w') as f:
f.write(result['svg'])
print(f" ✓ Saved: {filename}")
except Exception as e:
print(f" ✗ Error: {e}")
# 4. Plot complete magnetic
print("\n[4] Plotting complete magnetic...")
try:
result = PyOpenMagnetics.plot_magnetic(magnetic)
if result.get('success'):
filename = os.path.join(output_dir, "04_magnetic_complete.svg")
with open(filename, 'w') as f:
f.write(result['svg'])
print(f" ✓ Saved: {filename}")
else:
print(f" ✗ Failed: {result.get('error', 'Unknown error')}")
except Exception as e:
print(f" ✗ Error: {e}")
# 5. Plot magnetic field (if we have operating point data)
print("\n[5] Plotting magnetic field...")
try:
# Create operating point for field plot
operating_point = {
"name": "Field Plot",
"conditions": {"ambientTemperature": 25},
"excitationsPerWinding": [
{
"name": "Primary",
"frequency": 100000,
"current": {
"processed": {
"label": "Triangular",
"dutyCycle": 0.45,
"offset": 0.5,
"peakToPeak": 0.2
}
}
},
{
"name": "Secondary",
"frequency": 100000,
"current": {
"processed": {
"label": "Triangular",
"dutyCycle": 0.45,
"offset": 5.0,
"peakToPeak": 2.0
}
}
}
]
}
result = PyOpenMagnetics.plot_magnetic_field(magnetic, operating_point)
if result.get('success'):
filename = os.path.join(output_dir, "05_magnetic_field.svg")
with open(filename, 'w') as f:
f.write(result['svg'])
print(f" ✓ Saved: {filename}")
else:
print(f" ✗ Failed: {result.get('error', 'Unknown error')}")
except Exception as e:
print(f" ✗ Error: {e}")
# 6. Plot electric field
print("\n[6] Plotting electric field...")
try:
operating_point_with_voltage = {
"name": "Electric Field Plot",
"conditions": {"ambientTemperature": 25},
"excitationsPerWinding": [
{
"name": "Primary",
"frequency": 100000,
"current": {
"processed": {
"label": "Triangular",
"dutyCycle": 0.45,
"offset": 0.5,
"peakToPeak": 0.2
}
},
"voltage": {
"processed": {
"label": "Rectangular",
"dutyCycle": 0.45,
"offset": 100,
"peakToPeak": 200
}
}
},
{
"name": "Secondary",
"frequency": 100000,
"current": {
"processed": {
"label": "Triangular",
"dutyCycle": 0.45,
"offset": 5.0,
"peakToPeak": 2.0
}
},
"voltage": {
"processed": {
"label": "Rectangular",
"dutyCycle": 0.45,
"offset": 10,
"peakToPeak": 20
}
}
}
]
}
result = PyOpenMagnetics.plot_electric_field(magnetic, operating_point_with_voltage)
if result.get('success'):
filename = os.path.join(output_dir, "06_electric_field.svg")
with open(filename, 'w') as f:
f.write(result['svg'])
print(f" ✓ Saved: {filename}")
else:
print(f" ✗ Failed: {result.get('error', 'Unknown error')}")
except Exception as e:
print(f" ✗ Error: {e}")
print(f"\n✓ All visualizations saved to: {output_dir}/")
def export_spice_model(magnetic, output_dir="output"):
"""
Export the magnetic component as a SPICE subcircuit.
Args:
magnetic: Magnetic design dict
output_dir: Directory to save SPICE file
"""
print("\n" + "=" * 70)
print("EXPORTING SPICE MODEL")
print("=" * 70)
try:
subcircuit = PyOpenMagnetics.export_magnetic_as_subcircuit(magnetic)
filename = os.path.join(output_dir, "transformer.spice")
with open(filename, 'w') as f:
f.write(subcircuit)
print(f"\n✓ SPICE subcircuit exported to: {filename}")
print(f" Subcircuit length: {len(subcircuit)} characters")
except Exception as e:
print(f"\n✗ SPICE export failed: {e}")
def main():
"""Main execution function."""
print("\n" + "=" * 70)
print(" PYOPENMAGNETICS - COMPLETE SIMULATION EXAMPLE")
print(" Design, Simulate, Visualize, Export")
print("=" * 70)
# Step 1: Create design
magnetic, specs = create_manual_design()
# Step 2: Simulate losses
results = simulate_losses(magnetic, specs)
# Step 3: Verify inductance
verify_inductance(magnetic, specs)
# Step 4: Generate visualizations
visualize_design(magnetic)
# Step 5: Export SPICE model
export_spice_model(magnetic)
print("\n" + "=" * 70)
print("✓ COMPLETE")
print("=" * 70)
print("\nGenerated files:")
print(" - output/01_core.svg")
print(" - output/02_wire_primary.svg")
print(" - output/03_wire_secondary.svg")
print(" - output/04_magnetic_complete.svg")
print(" - output/05_magnetic_field.svg")
print(" - output/06_electric_field.svg")
print(" - output/transformer.spice")
print("\nNext steps:")
print(" 1. Review SVG visualizations in output/ directory")
print(" 2. Import SPICE model into your simulator")
print(" 3. Build prototype and verify measurements")
if __name__ == "__main__":
main()