-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathcomponent.py
More file actions
6763 lines (5881 loc) · 360 KB
/
component.py
File metadata and controls
6763 lines (5881 loc) · 360 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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Define the magnetic component."""
# Python standard libraries
import csv
import fileinput
import os
import gmsh
import json
import warnings
import inspect
import re
import pandas as pd
from datetime import datetime
import time
import logging
import dataclasses
from matplotlib import pyplot as plt
import ast
# Third party libraries
from onelab import onelab
import materialdatabase as mdb
import numpy as np
# Local libraries
import femmt.functions as ff
from femmt.constants import *
from femmt.mesh import Mesh
from femmt.model import VirtualWindingWindow, WindingWindow, Core, Insulation, StrayPath, AirGaps, Conductor
from femmt.enumerations import *
from femmt.data import FileData, MeshData
from femmt.drawing import TwoDaxiSymmetric
from femmt.thermal import thermal_simulation, calculate_heat_flux_round_wire, read_results_log
from femmt.dtos import *
import femmt.functions_reluctance as fr
logger = logging.getLogger(__name__)
class MagneticComponent:
"""
A MagneticComponent is the main object for all simulation purposes in femmt.
- One or more "MagneticComponents" can be created
- Each "MagneticComponent" owns its own instance variable values
"""
# Initialization of all class variables
# Common variables for all instances
onelab_folder_path: str = None
is_onelab_silent: bool = False
def __init__(self, simulation_type: SimulationType = SimulationType.FreqDomain,
component_type: ComponentType = ComponentType.Inductor, working_directory: str = None,
clean_previous_results: bool = True, onelab_verbosity: Verbosity = 1, is_gui: bool = False,
simulation_name: str | None = None, wwr_enabled=True):
# TODO Add a enum? for the verbosity to combine silent and print_output_to_file variables
"""
Initialize the magnetic component.
:param component_type: Available options:
- "inductor"
- "transformer"
- "integrated_transformer" (Transformer with included stray-path)
:type component_type: ComponentType
:param working_directory: Sets the working directory
:type working_directory: string
:param is_gui: Asks at first startup for onelab-path. Distinction between GUI and command line.
Defaults to 'False' in command-line-mode.
:type is_gui: bool
:param simulation_name: name without any effect. Will just be displayed in the result-log file
:type simulation_name: str
"""
# Get caller filepath when no working_directory was set
if working_directory is None:
caller_filename = inspect.stack()[1].filename
working_directory = os.path.join(os.path.dirname(caller_filename), "femmt")
if not os.path.exists(working_directory):
os.mkdir(working_directory)
# Create file paths class in order to handle all paths
self.file_data: FileData = FileData(working_directory)
# Clear result folder structure in case of missing
if clean_previous_results:
self.file_data.clear_previous_simulation_results()
# Variable to set silent mode
self.verbosity = onelab_verbosity
if not gmsh.isInitialized():
gmsh.initialize()
if onelab_verbosity != Verbosity.ToConsole:
gmsh.option.setNumber("General.Terminal", 0)
self.is_onelab_silent = True
if onelab_verbosity == Verbosity.ToFile:
self.is_onelab_silent = True
self.wwr_enabled = wwr_enabled
logger.info(f"\n"
f"Initialized a new Magnetic Component of type {component_type.name}\n"
f"--- --- --- ---")
# To make sure femm is only imported once
self.femm_is_imported = False
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Component Geometry
self.component_type = component_type # "inductor", "transformer", "integrated_transformer" (or "three-phase-transformer")
self.simulation_type = simulation_type # frequency domain # time domain
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Components
self.core: Core = None # Contains all information about the cores
self.air_gaps: AirGaps | None = None # Contains every air gap
self.windings = None
# self.windings: List of the different winding objects which the following structure:
# windings[0]: primary, windings[1]: secondary, windings[2]: tertiary ....
self.insulation: Insulation = None # Contains information about the needed insulations
self.winding_windows = None
# self.winding_windows: Contains a list of every winding_window which was created containing a
# list of virtual_winding_windows
self.stray_path = None # Contains information about the stray_path (only for integrated transformers)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Control Flags
self.plot_fields = "standard" # can be "standard" or False
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Excitation Parameters for freq and time domain
# Empty lists will be set when a winding window is added to the magnetic component
self.imposed_reduced_frequency = None
self.flag_excitation_type = None
self.current = [] # Defined for every conductor
self.current_density = [] # Defined for every conductor
self.voltage = [] # Defined for every conductor
self.charge = []
self.v_core = None
# self.v_ground_core = None
self.v_ground_out_boundary = None
self.capacitance_matrix_nodes = {}
self.time = [] # Defined for time domain simulation
self.average_currents = [] # Defined for average currents for every winding
self.rms_currents = [] # Defined for rms currents for every winding
self.step_time = None
self.time_period = None
self.initial_time = None # Default 0
self.max_time = None # Simulation's duration
self.nb_steps_per_period = None # Number of time steps
self.nb_steps = None
self.frequency = None
self.phase_deg = None # Default is zero, Defined for every conductor
self.red_freq = None # [] * self.n_windings # Defined for every conductor
self.max_reduced_frequency = 3.25
self.delta = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Steinmetz loss material coefficients and current waveform
self.Ipeak: float | None = None
self.ki: float | None = None
self.alpha: float | None = None
self.beta: float | None = None
self.t_rise: float | None = None
self.t_fall: float | None = None
self.f_switch: float | None = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# MeshData to store the mesh size for different points
# Object is added in set_core
padding = 1.5
global_accuracy = 0.5
self.mesh_data = MeshData(global_accuracy, global_accuracy, global_accuracy, global_accuracy, padding, mu_0)
self.mesh = None
self.two_d_axi: TwoDaxiSymmetric = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# -- Used for Litz Validation --
self.sweep_frequencies = None
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 2 and 3 winding transformer inductance matrix
self.L_1_1 = None
self.L_2_2 = None
self.L_3_3 = None
self.M = None
self.M_12 = None
self.M_21 = None
self.M_13 = None
self.M_32 = None
self.M_32 = None
self.M_23 = None
self.Pv = None
# 2 and 3 winding transformer primary concentrated equivalent circuit
self.n_conc = None
self.L_s_conc = None
self.L_h_conc = None
self.L_s1 = None
self.L_s2 = None
self.L_s3 = None
self.L_h = None
self.L_s12 = None
self.L_s13 = None
self.L_s23 = None
self.n_12 = None
self.n_13 = None
# -- FEMM variables --
self.tot_loss_femm = None
self.onelab_setup(is_gui)
self.onelab_client = onelab.client(__file__)
self.simulation_name = simulation_name
def update_mesh_accuracies(self, mesh_accuracy_core: float, mesh_accuracy_window: float,
mesh_accuracy_conductor, mesh_accuracy_air_gaps: float):
"""
Update mesh accuracies for core, windows, conductors and air gaps.
:param mesh_accuracy_core: mesh accuracy of the core
:type mesh_accuracy_core: float
:param mesh_accuracy_window: mesh accuracy of the winding window
:type mesh_accuracy_window: float
:param mesh_accuracy_conductor: mesh accuracy of the conductors
:type mesh_accuracy_conductor: float
:param mesh_accuracy_air_gaps: mesh accuracy of the air gaps
:type mesh_accuracy_air_gaps: float
"""
self.mesh_data.mesh_accuracy_core = mesh_accuracy_core
self.mesh_data.mesh_accuracy_window = mesh_accuracy_window
self.mesh_data.mesh_accuracy_conductor = mesh_accuracy_conductor
self.mesh_data.mesh_accuracy_air_gaps = mesh_accuracy_air_gaps
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Thermal simulation
def thermal_simulation(self, thermal_conductivity_dict: dict, boundary_temperatures_dict: dict,
boundary_flags_dict: dict, case_gap_top: float,
case_gap_right: float, case_gap_bot: float, show_thermal_simulation_results: bool = True,
pre_visualize_geometry: bool = False, color_scheme: dict = ff.colors_femmt_default,
colors_geometry: dict = ff.colors_geometry_femmt_default, flag_insulation: bool = True):
"""
Start the thermal simulation using thermal_simulation.py.
:param thermal_conductivity_dict: Contains the thermal conductivities for every region
:type thermal_conductivity_dict: dict
:param boundary_temperatures_dict: Contains the temperatures at each boundary line
:type boundary_temperatures_dict: dict
:param boundary_flags_dict: Sets the boundary type (Dirichlet or von Neumann) for each boundary line
:type boundary_flags_dict: dict
:param case_gap_top: Size of the top case
:type case_gap_top: float
:param case_gap_right: Size of the right case
:type case_gap_right: float
:param case_gap_bot: Size of the bot case
:type case_gap_bot: float
:param show_thermal_simulation_results: Shows thermal results in gmsh, defaults to True
:type show_thermal_simulation_results: bool, optional
:param pre_visualize_geometry: Shows the thermal model before simulation, defaults to False
:type pre_visualize_geometry: bool, optional
:param color_scheme: Color scheme for visualization, defaults to ff.colors_femmt_default
:type color_scheme: dict, optional
:param colors_geometry: Color geometry for visualization, defaults to ff.colors_geometry_femmt_default
:type colors_geometry: dict, optional
:param flag_insulation: True to simulate the insulation
:type flag_insulation: bool
"""
# Create necessary folders
self.file_data.create_folders(self.file_data.thermal_results_folder_path)
self.mesh.generate_thermal_mesh(case_gap_top, case_gap_right, case_gap_bot, color_scheme, colors_geometry,
pre_visualize_geometry)
# insulation_tag = self.mesh.ps_insulation if flag_insulation and len(self.insulation.core_cond) == 4 else None
if not os.path.exists(self.file_data.e_m_results_log_path):
# Simulation results file not created
raise Exception(
"Cannot run thermal simulation -> Magnetic simulation needs to run first (no results_log.json found")
# Check if the results log path simulation settings fit the current simulation settings
current_settings = MagneticComponent.encode_settings(self)
del current_settings["working_directory"]
del current_settings["date"]
with open(self.file_data.e_m_results_log_path, "r") as fd:
content = json.load(fd)
log_settings = content["simulation_settings"]
del log_settings["working_directory"]
del log_settings["date"]
if current_settings != log_settings:
raise Exception(f"The settings from the log file {self.file_data.e_m_results_log_path} do not match "
f"the current simulation settings. Please re-run the magnetic simulation.")
tags = {
"core_tags": self.mesh.ps_core,
"background_tag": self.mesh.ps_air,
"winding_tags": self.mesh.ps_cond,
"air_gaps_tag": self.mesh.ps_air_gaps if self.air_gaps.number > 0 else None,
"boundary_regions": self.mesh.thermal_boundary_region_tags,
"insulations_tag": self.mesh.ps_insulation if flag_insulation and len(
self.insulation.core_cond) == 4 else None
}
# Core area -> Is needed to estimate the heat flux
# Power density for volumes W/m^3
# core_area = self.calculate_core_volume()
core_area = self.calculate_core_parts_volume()
# Set wire radii
wire_radii = [winding.conductor_radius for winding in self.windings]
thermal_parameters = {
"file_data": self.file_data,
"tags_dict": tags,
"thermal_conductivity_dict": thermal_conductivity_dict,
"boundary_temperatures": boundary_temperatures_dict,
"boundary_flags": boundary_flags_dict,
"boundary_physical_groups": {
"top": self.mesh.thermal_boundary_ps_groups[0],
"top_right": self.mesh.thermal_boundary_ps_groups[1],
"right": self.mesh.thermal_boundary_ps_groups[2],
"bot_right": self.mesh.thermal_boundary_ps_groups[3],
"bot": self.mesh.thermal_boundary_ps_groups[4]
},
"core_area": core_area,
"conductor_radii": wire_radii,
"wire_distances": self.get_wire_distances(),
"case_volume": self.core.r_outer * case_gap_top + self.core.core_h * case_gap_right + self.core.r_outer * case_gap_bot,
"show_thermal_fem_results": show_thermal_simulation_results,
"print_sensor_values": False,
"silent": self.verbosity == Verbosity.Silent, # Add verbosity for thermal simulation
"flag_insulation": flag_insulation
}
thermal_simulation.run_thermal(**thermal_parameters)
logger.info(f"The electromagnetic results are stored here: {self.file_data.e_m_results_log_path}")
logger.info(f"The thermal results are stored here: {self.file_data.results_folder_path}/results_thermal.json")
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Setup
def onelab_setup(self, is_gui: bool):
"""
Set up the onelab filepaths.
Either reads ONELAB parent folder path from config.json or asks the user to provide the ONELAB path it.
Creates a config.json inside the site-packages folder at first run.
:param is_gui: set to True to avoid terminal output question for onelab file path at first run.
Used especially in GUI
:type is_gui: bool
"""
# check if config.json is available and not empty
if os.path.isfile(self.file_data.config_path) and os.stat(self.file_data.config_path).st_size != 0:
onelab_path = ""
with open(self.file_data.config_path, "r") as fd:
loaded_dict = json.loads(fd.read())
onelab_path = loaded_dict['onelab']
if os.path.exists(onelab_path) and os.path.isfile(os.path.join(onelab_path, "onelab.py")):
# Path found
self.file_data.onelab_folder_path = onelab_path
return
# Let the user enter the onelab_path:
# Find out the onelab_path of installed module, or in case of running directly from git,
# find the onelab_path of git repository loop until path is correct
onelab_path_wrong = True
# This is needed because in the gui the input() command cannot be called (it would result in an infinite loop).
# If the config file was not found just return out of the function.
# The config file will be added later by the gui handler.
if is_gui:
return
while onelab_path_wrong:
onelab_path = os.path.normpath(input(
"Enter the path of onelab's parent folder (path to folder which contains getdp, onelab executable files): "))
if os.path.exists(onelab_path):
onelab_path_wrong = False
break
else:
logger.info('onelab not found! Tool searches for onelab.py in the folder. Please re-enter path!')
self.file_data.onelab_folder_path = onelab_path
# Write the path to the config.json
onelab_path_dict = {"onelab": onelab_path}
with open(os.path.join(self.file_data.config_path), 'w', encoding='utf-8') as fd:
json.dump(onelab_path_dict, fd, indent=2, ensure_ascii=False)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Geometry Parts
def high_level_geo_gen(self, frequency: float = None, skin_mesh_factor: float = None):
"""Update the mesh data and creates the model and mesh objects.
:param frequency: Frequency used in the mesh density, defaults to None
:type frequency: float, optional
:param skin_mesh_factor: Used in the mesh density, defaults to None
:type skin_mesh_factor: float, optional
"""
# Default values for global_accuracy and padding
self.mesh_data.update_spatial_data(self.core.core_inner_diameter, self.core.window_w, self.windings)
# Update mesh data
self.mesh_data.update_data(frequency, skin_mesh_factor)
# Create model
self.two_d_axi = TwoDaxiSymmetric(self.core, self.mesh_data, self.air_gaps, self.winding_windows,
self.stray_path,
self.insulation, self.component_type, len(self.windings), self.verbosity)
self.two_d_axi.draw_model()
# Create mesh
self.mesh = Mesh(self.two_d_axi, self.windings, self.winding_windows, self.core.correct_outer_leg,
self.file_data, self.verbosity, None, self.wwr_enabled)
# self.mesh = Mesh(self.two_d_axi, self.windings, self.core.correct_outer_leg, self.file_data, None, ff.silent)
def mesh(self, frequency: float = None, skin_mesh_factor: float = None):
"""Generate model and mesh.
:param frequency: Frequency used in the mesh density, defaults to None
:type frequency: float, optional
:param skin_mesh_factor: Used in the mesh density, defaults to None
:type skin_mesh_factor: float, optional
"""
self.high_level_geo_gen(frequency=frequency, skin_mesh_factor=skin_mesh_factor)
self.mesh.generate_hybrid_mesh() # create the mesh itself with gmsh
self.mesh.generate_electro_magnetic_mesh() # assign the physical entities/domains to the mesh
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Create Model
def set_insulation(self, insulation: Insulation):
"""Add insulation to the model.
:param insulation: insulation object
:type insulation: Insulation
"""
if self.simulation_type == SimulationType.ElectroStatic:
insulation.bobbin_dimensions = True
else:
insulation.bobbin_dimensions = False
if self.simulation_type == SimulationType.ElectroStatic and insulation.bobbin_dimensions is None:
raise Exception("bobbin parameters must be set in electrostatic simulations")
if insulation.cond_cond is None or not insulation.cond_cond:
raise Exception("insulations between the conductors must be set")
if insulation.core_cond is None or not insulation.core_cond:
raise Exception("insulations between the core and the conductors must be set")
self.insulation = insulation
def set_stray_path(self, stray_path: StrayPath):
"""Add the stray path to the model.
:param stray_path: StrayPath object
:type stray_path: StrayPath
"""
self.stray_path = stray_path
def set_air_gaps(self, air_gaps: AirGaps):
"""Add the air_gaps to the model.
:param air_gaps: AirGaps object
:type air_gaps: AirGaps
"""
# Sorting air gaps from lower to upper
air_gaps.midpoints.sort(key=lambda x: x[1])
self.air_gaps = air_gaps
def set_winding_windows(self, winding_windows: list[WindingWindow]):
"""
Add the winding windows to the model.
Creates the windings list, which contains the conductors from the virtual winding windows but sorted
by the winding_number (ascending). Sets empty lists for excitation parameters.
:param winding_windows: list of WindingWindow objects
:type winding_windows: list[WindingWindow]
"""
self.winding_windows = winding_windows
windings = []
for ww in winding_windows:
for vww in ww.virtual_winding_windows:
if not vww.winding_is_set:
raise Exception("Each virtual winding window needs to have a winding")
for winding in vww.windings:
if winding not in windings:
windings.append(winding)
self.windings = sorted(windings, key=lambda x: x.winding_number)
# Print statement was moved here so the silence functionality is not needed in Conductors class.
# TODO Can this be even removed?
for winding in self.windings:
if winding.conductor_type == ConductorType.RoundLitz:
logger.info(f"Updated Litz Configuration: \n"
f" ff: {winding.ff} \n"
f" Number of layers/strands: {winding.n_layers}/{winding.n_strands} \n"
f" Strand radius: {winding.strand_radius} \n"
f" Conductor radius: {winding.conductor_radius}\n"
f"---")
# Set excitation parameter lists
self.current = [None] * len(windings)
self.current_density = [None] * len(windings)
self.voltage = [None] * len(windings)
self.phase_deg = np.zeros(len(windings))
# Correct the turns lists in each vww, so that they have the same length
for ww in winding_windows:
for vww in ww.virtual_winding_windows:
zeros_to_append = (len(self.windings) - len(vww.turns))
if zeros_to_append < 0:
for _ in range(0, -zeros_to_append):
vww.turns.pop()
else:
for _ in range(0, zeros_to_append):
vww.turns.append(0)
def set_core(self, core: Core):
"""Add the core to the model.
:param core: Core object
:type core: Core
"""
self.core = core
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Pre-Processing
def create_model(self, freq: float, skin_mesh_factor: float = 0.5, pre_visualize_geometry: bool = False,
save_png: bool = False, color_scheme: dict = ff.colors_femmt_default,
colors_geometry: dict = ff.colors_geometry_femmt_default, benchmark: bool = False):
"""
Create a model from the abstract geometry description inside onelab including optional mesh generation.
:param freq: Frequency [Hz]
:type freq: float
:param skin_mesh_factor: [default to 0.5]
:type skin_mesh_factor: float
:param pre_visualize_geometry: True for a pre-visualization (e.g. check your geometry) and after this a
simulation runs, False for a direct simulation
:type pre_visualize_geometry: bool
:param save_png: True to save a png-figure, false for no figure
:type save_png: bool
:param color_scheme: color file (definition for red, green, blue, ...)
:type color_scheme: dict
:param colors_geometry: definition for e.g. core is gray, winding is orange, ...
:type colors_geometry: dict
:param benchmark: Benchmark simulation (stop time). Defaults to False.
:type benchmark: bool
"""
if self.core is None:
raise Exception("A core class needs to be added to the magnetic component")
if self.air_gaps is None:
self.air_gaps = AirGaps(None, None)
logger.info("No air gaps are added")
if self.insulation is None:
raise Exception("An insulation class need to be added to the magnetic component")
if self.winding_windows is None:
raise Exception("Winding windows are not set properly. Please check the winding creation")
if benchmark:
start_time = time.time()
self.high_level_geo_gen(frequency=freq, skin_mesh_factor=skin_mesh_factor)
high_level_geo_gen_time = time.time() - start_time
start_time = time.time()
self.mesh.generate_hybrid_mesh(visualize_before=pre_visualize_geometry, save_png=save_png,
color_scheme=color_scheme, colors_geometry=colors_geometry)
generate_hybrid_mesh_time = time.time() - start_time
return high_level_geo_gen_time, generate_hybrid_mesh_time
else:
self.high_level_geo_gen(frequency=freq, skin_mesh_factor=skin_mesh_factor)
self.mesh.generate_hybrid_mesh(visualize_before=pre_visualize_geometry, save_png=save_png,
color_scheme=color_scheme, colors_geometry=colors_geometry)
if self.component_type in [ComponentType.Inductor, ComponentType.Transformer, ComponentType.IntegratedTransformer]:
self.log_coordinates_description()
def get_single_complex_permeability(self):
"""
Read the complex permeability from the material database.
In case of amplitude dependent material definition, the initial permeability is used.
:return: complex
"""
if self.core.permeability_type == PermeabilityType.FromData:
# take datasheet value from database
complex_permeability = mu_0 * mdb.MaterialDatabase(
self.verbosity == Verbosity.Silent).get_material_attribute(material_name=self.core.material,
attribute="initial_permeability")
logger.info(f"{complex_permeability=}")
if self.core.permeability_type == PermeabilityType.FixedLossAngle:
complex_permeability = mu_0 * self.core.mu_r_abs * complex(np.cos(np.deg2rad(self.core.phi_mu_deg)),
np.sin(np.deg2rad(self.core.phi_mu_deg)))
if self.core.permeability_type == PermeabilityType.RealValue:
complex_permeability = mu_0 * self.core.mu_r_abs
return complex_permeability
def check_model_mqs_condition(self) -> None:
"""
Check the model for magneto-quasi-static condition for frequencies != 0.
Is called before a simulation.
Loads the permittivity from the material database (measurement or datasheet) and calculates the
resonance ratio = diameter_to_wavelength_ratio / diameter_to_wavelength_ratio_of_first_resonance
"""
if self.frequency != 0:
if self.core.permittivity["datasource"] == "measurements" or self.core.permittivity[
"datasource"] == "datasheet":
epsilon_r, epsilon_phi_deg = mdb.MaterialDatabase(self.verbosity == Verbosity.Silent).get_permittivity(
temperature=self.core.temperature, frequency=self.frequency,
material_name=self.core.material,
datasource=self.core.permittivity["datasource"],
datatype=self.core.permittivity["datatype"],
measurement_setup=self.core.permittivity["measurement_setup"],
interpolation_type="linear")
complex_permittivity = epsilon_0 * epsilon_r * complex(np.cos(np.deg2rad(epsilon_phi_deg)),
np.sin(np.deg2rad(epsilon_phi_deg)))
logger.info(f"{complex_permittivity=}\n"
f"{epsilon_r=}\n"
f"{epsilon_phi_deg=}")
ff.check_mqs_condition(radius=self.core.core_inner_diameter / 2, frequency=self.frequency,
complex_permeability=self.get_single_complex_permeability(),
complex_permittivity=complex_permittivity, conductivity=self.core.sigma,
relative_margin_to_first_resonance=0.5)
else:
ff.check_mqs_condition(radius=self.core.core_inner_diameter / 2, frequency=self.frequency,
complex_permeability=self.get_single_complex_permeability(),
complex_permittivity=0, conductivity=self.core.sigma,
relative_margin_to_first_resonance=0.5)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Miscellaneous
def calculate_core_cross_sectional_area(self):
"""
Calculate the effective cross-sectional area of the core using the core inner diameter.
:return: Cross-sectional area of the core.
:rtype: float
"""
# Calculate the cross-sectional area using the inner diameter of the core
width = self.core.core_inner_diameter / 2
cross_sectional_area = np.pi * (width ** 2)
return cross_sectional_area
def calculate_core_volume_with_air(self) -> float:
"""Calculate the volume of the core including air.
:return: Volume of the core
:rtype: float
"""
if self.core.core_type == CoreType.Single:
core_height = self.core.window_h + self.core.core_inner_diameter / 2
elif self.core.core_type == CoreType.Stacked:
core_height = self.core.window_h_bot + self.core.window_h_top + self.core.core_inner_diameter * 3 / 4
# TODO: could also be done arbitrarily
core_width = self.core.r_outer
return np.pi * core_width ** 2 * core_height
def calculate_core_volume(self) -> float:
"""Calculate the volume of the core excluding air.
:return: Volume of the core.
:rtype: float
"""
core_height = None
winding_height = None
if self.core.core_type == CoreType.Single:
core_height = self.core.window_h + self.core.core_inner_diameter / 2
winding_height = self.core.window_h
elif self.core.core_type == CoreType.Stacked:
core_height = self.core.window_h_bot + self.core.window_h_top + self.core.core_inner_diameter * 3 / 4
# TODO: could also be done arbitrarily
winding_height = self.core.window_h_bot + self.core.window_h_top # TODO: could also be done arbitrarily
core_width = self.core.r_outer
winding_width = self.core.window_w
air_gap_volume = 0
inner_leg_width = self.core.r_inner - winding_width
for leg_position, _, height in self.air_gaps.midpoints:
if leg_position == AirGapLegPosition.LeftLeg.value:
# left leg
# TODO this is wrong since the air gap is not centered on the y axis
width = core_width - self.core.r_inner
elif leg_position == AirGapLegPosition.CenterLeg.value:
# center leg
width = inner_leg_width
elif leg_position == AirGapLegPosition.RightLeg.value:
# right leg
# TODO this is wrong since the air gap is not centered on the y axis
width = core_width - self.core.r_inner
else:
raise Exception(f"Invalid leg position tag {leg_position} used for an air gap.")
air_gap_volume += np.pi * width ** 2 * height
return np.pi * (core_width ** 2 * core_height - (inner_leg_width + winding_width) ** 2 * winding_height + \
inner_leg_width ** 2 * winding_height) - air_gap_volume
def calculate_core_parts_volume(self) -> list:
"""Calculate the volume of the part core excluding air.
:return: Volume of the core part.
:rtype: list
"""
# Extract heights from the midpoints of air gaps
if self.air_gaps.midpoints:
heights = [point[2] for point in self.air_gaps.midpoints]
core_parts_volumes = []
def get_width(part_number: int):
"""
If there is a stray path, calculate width based on its starting index and part number.
part_number is the core_part_i+2; means that if the start_index is 0, the stray path is in core_part_2
if the start_index is 1, the stray path is in core_part_3 and so on
:param part_number: core_part_i+2
:type part_number: int
"""
if self.stray_path and part_number == self.stray_path.start_index + 2:
return self.stray_path.length
return self.core.core_inner_diameter / 2
if self.air_gaps.midpoints:
# # Sorting air gaps from lower to upper
sorted_midpoints = sorted(self.air_gaps.midpoints, key=lambda x: x[1])
# Finding position of first airgap
bottommost_airgap_position = sorted_midpoints[0][1]
bottommost_airgap_height = sorted_midpoints[0][2]
# Finding position of last airgap
topmost_airgap_position = sorted_midpoints[-1][1]
topmost_airgap_height = sorted_midpoints[-1][2]
# if single core
if self.core.core_type == CoreType.Single:
# if Airgap is existed
if self.air_gaps.midpoints:
# For single core and more than one core_part, volume for every core part is calculated
# core_part_1 is divided into parts cores
# # subpart1: bottom left subpart
subpart1_1_height = bottommost_airgap_position + self.core.window_h / 2 - bottommost_airgap_height / 2
subpart1_1_width = self.core.core_inner_diameter / 2
subpart1_1_volume = np.pi * subpart1_1_width ** 2 * subpart1_1_height
# # subpart2: bottom mid subpart
subpart1_2_height = self.core.core_inner_diameter / 4
subpart1_2_width = self.core.r_outer
subpart1_2_volume = np.pi * subpart1_2_width ** 2 * subpart1_2_height
# subpart3: right subpart
subpart1_3_height = self.core.window_h
subpart1_3_width = self.core.r_outer
subpart1_3_volume = np.pi * subpart1_3_width ** 2 * subpart1_3_height - \
(np.pi * (self.core.window_w + self.core.core_inner_diameter / 2) ** 2 * self.core.window_h)
# subpart4: top mid-subpart
subpart1_4_height = self.core.core_inner_diameter / 4
subpart1_4_width = self.core.r_outer
subpart1_4_volume = np.pi * subpart1_4_width ** 2 * subpart1_4_height
# subpart5: top left subpart
subpart1_5_height = self.core.window_h / 2 - topmost_airgap_position - topmost_airgap_height / 2
subpart1_5_width = self.core.core_inner_diameter / 2
subpart1_5_volume = np.pi * subpart1_5_width ** 2 * subpart1_5_height
# Calculate the volume of core part 1 by summing up subpart volumes
core_part_1_volume = subpart1_1_volume + subpart1_2_volume + subpart1_3_volume + \
subpart1_4_volume + subpart1_5_volume
core_parts_volumes.append(core_part_1_volume)
# Calculate the volumes of the core parts between the air gaps
for i in range(len(sorted_midpoints) - 1):
air_gap_1_position = sorted_midpoints[i][1]
air_gap_1_height = sorted_midpoints[i][2]
air_gap_2_position = sorted_midpoints[i + 1][1]
air_gap_2_height = sorted_midpoints[i + 1][2]
# calculate the height based on airgap positions and heights, and the width
core_part_height = air_gap_2_position - air_gap_2_height / 2 - (air_gap_1_position + air_gap_1_height / 2)
core_part_width = get_width(i + 2)
# calculate the volume
core_part_volume = np.pi * core_part_width ** 2 * core_part_height
core_parts_volumes.append(core_part_volume)
else:
# subpart1: left subpart
subpart1_1_height = self.core.window_h
subpart1_1_width = self.core.core_inner_diameter / 2
subpart1_1_volume = np.pi * subpart1_1_width ** 2 * subpart1_1_height
# subpart2: top subpart
subpart1_2_height = self.core.core_inner_diameter / 4
subpart1_2_width = self.core.r_outer
subpart1_2_volume = np.pi * subpart1_2_width ** 2 * subpart1_2_height
# subpart3: right subpart
subpart1_3_height = self.core.window_h
subpart1_3_width = self.core.r_outer
subpart1_3_volume = np.pi * subpart1_3_width ** 2 * subpart1_3_height - \
(np.pi * (self.core.window_w + self.core.core_inner_diameter / 2) ** 2 * self.core.window_h)
# subpart4: bottom subpart
subpart1_4_height = self.core.core_inner_diameter / 4
subpart1_4_width = self.core.r_outer
subpart1_4_volume = np.pi * subpart1_4_width ** 2 * subpart1_4_height
# Calculate the volume of core part 1 by summing up subpart volumes
core_part_1_volume = subpart1_1_volume + subpart1_2_volume + subpart1_3_volume + subpart1_4_volume
core_parts_volumes.append(core_part_1_volume)
# Return the total core part volume
# return core_parts_volumes
elif self.core.core_type == CoreType.Stacked:
# For stacked core types, the volume is divided into different core * parts, each of which is further
# divided into parts to calculate the total volume of each core part.
# core_part_2 : core part between the bottom air gap and subpart_1 of core_part_1
core_part_1_height = self.core.window_h_bot / 2 - heights[0] / 2
core_part_1_width = self.core.core_inner_diameter / 2
core_part_1_volume = np.pi * core_part_1_width ** 2 * core_part_1_height
core_parts_volumes.append(core_part_1_volume)
# Core Part 1 Calculation
# Core part 1 is calculated as the sum of three different parts
# subpart_1: bottom left subpart
subpart2_1_height = self.core.window_h_bot / 2 - heights[0] / 2
subpart2_1_width = self.core.core_inner_diameter / 2
subpart2_1_volume = np.pi * subpart2_1_width ** 2 * subpart2_1_height
# subpart_2 : bottom mid subpart
subpart2_2_height = self.core.core_inner_diameter / 4
subpart2_2_width = self.core.r_outer
subpart2_2_volume = np.pi * subpart2_2_width ** 2 * subpart2_2_height
# subpart_3: bottom right subpart
subpart2_3_height = self.core.window_h_bot
subpart2_3_width = self.core.r_outer
subpart2_3_volume = np.pi * (subpart2_3_width ** 2 * subpart2_3_height) - \
np.pi * ((self.core.window_w + self.core.core_inner_diameter/2) ** 2 * self.core.window_h_bot)
# Summing up the volumes of the parts to get the total volume of core part 1
core_part_2_volume = subpart2_1_volume + subpart2_2_volume + subpart2_3_volume
core_parts_volumes.append(core_part_2_volume)
# core_part_3 : left mid core part (stacked)
core_part_3_height = self.core.core_inner_diameter / 4
core_part_3_width = self.core.r_inner
core_part_3_volume = np.pi * core_part_3_width ** 2 * core_part_3_height
core_parts_volumes.append(core_part_3_volume)
# core_part_4: right mid core part
core_part_4_height = self.core.core_inner_diameter / 4
core_part_4_width = self.core.r_outer
core_part_4_volume = np.pi * core_part_4_width ** 2 * core_part_4_height - core_part_3_volume
core_parts_volumes.append(core_part_4_volume)
# core_part_5
# core_part_5 is divided into 3 parts
# subpart_1: left top subpart
subpart5_1_height = self.core.window_h_top - heights[1] / 2
subpart5_1_width = self.core.core_inner_diameter / 2
subpart5_1_volume = np.pi * subpart5_1_width ** 2 * subpart5_1_height
# subpart_2: mid top subpart
subpart5_2_height = self.core.core_inner_diameter / 4
subpart5_2_width = self.core.r_outer
subpart5_2_volume = np.pi * subpart5_2_width ** 2 * subpart5_2_height
# subpart 3: top right subpart
subpart5_3_height = self.core.window_h_top
subpart5_3_width = self.core.r_outer
subpart5_3_volume = np.pi * (subpart5_3_width ** 2 * subpart5_3_height) - \
np.pi * ((self.core.window_w + self.core.core_inner_diameter / 2) ** 2 * self.core.window_h_top)
# Summing up the volumes of the parts to get the total volume of core_part_5
core_part_5_volume = subpart5_1_volume + subpart5_2_volume + subpart5_3_volume
core_parts_volumes.append(core_part_5_volume)
# Core Volume Consistency Check
# Sum all the core part volumes
total_parts_volume = sum(core_parts_volumes)
# Calculate the whole core volume
whole_core_volume = self.calculate_core_volume()
# Define a margin of error
margin_of_error = 1e-5
# Check if the volumes are equal within the margin of error
if not (abs(whole_core_volume - total_parts_volume) <= margin_of_error):
error_message = (f"Sum of core parts ({total_parts_volume}) does not equal the whole core "
f"volume ({whole_core_volume}) within the margin of error ({margin_of_error}).")
raise ValueError(error_message)
# Returning the final list of core part volumes
return core_parts_volumes
def calculate_core_weight(self) -> float:
"""
Calculate the weight of the core in kg.
This method is using the core volume from for an ideal rotation-symmetric core and the volumetric mass
density from the material database.
"""
if self.core.material == 'custom':
volumetric_mass_density = 0
warnings.warn("Volumetric mass density not implemented for custom cores. "
"Returns '0' in log-file: Core cost will also result to 0.",
stacklevel=2)
else:
volumetric_mass_density = self.core.material_database.get_material_attribute(
material_name=self.core.material, attribute="volumetric_mass_density")
return self.calculate_core_volume() * volumetric_mass_density
def get_wire_distances(self) -> list[list[float]]:
"""Return the distance (radius) of each conductor to the y-axis.
:return: Wire distances
:rtype: list[list[float]]
"""
# wire_distance = []
# for winding in self.two_d_axi.p_conductor:
# # 5 points are for 1 wire
# num_points = len(winding)
# num_windings = num_points // 5
# winding_list = []
# for i in range(num_windings):
# winding_list.append(winding[i * 5][0])
# wire_distance.append(winding_list)
#
# return wire_distance
wire_distance = []
for _, conductor in enumerate(self.two_d_axi.p_conductor):
num_points = len(conductor)
num_turns = num_points // 5
point_increment = 5
winding_list = []
for i in range(num_turns):
winding_list.append(conductor[i * point_increment][0])
wire_distance.append(winding_list)
return wire_distance
def calculate_wire_lengths(self) -> list[float]:
"""Calculate the wire length of all conductors inside the magnetic component."""
distances = self.get_wire_distances()
lengths = []
for winding in distances:
lengths.append(sum([2 * np.pi * turn for turn in winding]))
return lengths
def calculate_wire_volumes(self) -> list[float]:
"""Calculate the wire volume of the magnetic component."""
wire_volumes = []
wire_lengths = self.calculate_wire_lengths()
for index, winding in enumerate(self.windings):
cross_section_area = 0
if winding.conductor_type == ConductorType.RoundLitz or winding.conductor_type == ConductorType.RoundSolid:
# For round wire it is always the same
cross_section_area = np.pi * winding.conductor_radius ** 2
elif winding.conductor_type == ConductorType.RectangularSolid:
# Since the foil sizes also depends on the winding scheme, conductor_arrangement and wrap_para_type
# the volume calculation is different.
for ww in self.winding_windows:
for vww_index, vww in enumerate(ww.virtual_winding_windows):
winding_type = vww.winding_type
winding_scheme = vww.winding_scheme
wrap_para_type = vww.wrap_para
for vww_winding in vww.windings:
if vww_winding.winding_number == index:
if winding_type == WindingType.Single:
if winding_scheme == WindingScheme.Full:
cross_section_area = self.core.window_h * self.core.window_w
elif winding_scheme == WindingScheme.FoilHorizontal:
cross_section_area = self.core.window_w * winding.thickness
elif winding_scheme == WindingScheme.FoilVertical:
if wrap_para_type == WrapParaType.FixedThickness:
cross_section_area = self.core.window_h * winding.thickness
elif wrap_para_type == WrapParaType.Interpolate: