diff --git a/femmt/functions.py b/femmt/functions.py index 6bc4c2ad..2a1f2ec9 100644 --- a/femmt/functions.py +++ b/femmt/functions.py @@ -1064,25 +1064,42 @@ def visualize_simulation_results(simulation_result_file_path: str, store_figure_ """Visualize the simulation results by a figure.""" with open(simulation_result_file_path, "r") as fd: loaded_results_dict = json.loads(fd.read()) - - inductance = loaded_results_dict["single_sweeps"][0]["winding1"]["flux_over_current"][0] + # core eddy and hysteresis losses loss_core_eddy_current = loaded_results_dict["total_losses"]["eddy_core"] loss_core_hysteresis = loaded_results_dict["total_losses"]["hyst_core_fundamental_freq"] - loss_winding_1 = loaded_results_dict["total_losses"]["winding1"]["total"] + # Inductances and winding losses + windings_inductance = [] + windings_loss = [] + windings_labels = [] + # Dynamically for 3 windings + for i in range(1, 3): + winding_key = f"winding{i}" + if winding_key in loaded_results_dict["single_sweeps"][0]: + windings_inductance.append(loaded_results_dict["single_sweeps"][0][winding_key]["flux_over_current"][0]) + windings_loss.append(loaded_results_dict["total_losses"][winding_key]["total"]) + windings_labels.append(f"Winding {i}") + + # Plotting results + fig, ax = plt.subplots() + bar_width = 0.35 - print(inductance) - print(loss_core_eddy_current) - print(loss_core_hysteresis) - print(loss_winding_1) + # Plot core losses + ax.bar(0, loss_core_hysteresis, width=bar_width, label='Core Hysteresis Loss') + ax.bar(0, loss_core_eddy_current, bottom=loss_core_hysteresis, width=bar_width, label='Core Eddy Current Loss') - bar_width = 0.35 - plt.bar(0, loss_core_hysteresis, width=bar_width) - plt.bar(0, loss_core_eddy_current, bottom=loss_core_hysteresis, width=bar_width) - plt.bar(1, loss_winding_1, width=bar_width) - plt.legend(['Hysteresis loss', 'Eddy current loss', 'Winding loss']) - plt.ylabel('Losses in W') - plt.xticks([0, 1, 2], ['Core', 'Winding 1', 'Winding 2']) - plt.grid() + # Plot winding inductances and losses + for index, (inductance, loss) in enumerate(zip(windings_inductance, windings_loss), start=1): + ax.bar(index, loss, width=bar_width, label=f'{windings_labels[index - 1]} Loss') + print(f"{windings_labels[index - 1]} Inductance: {inductance} H") + print(f"{windings_labels[index - 1]} Loss: {loss} W") + + ax.set_ylabel('Losses in W') + ax.set_title('Loss Distribution in Magnetic Components') + ax.set_xticks(list(range(len(windings_labels) + 1))) + ax.set_xticklabels(['Core'] + windings_labels) + ax.legend() + + plt.grid(True) plt.savefig(store_figure_file_path, bbox_inches="tight") if show_plot: @@ -1090,7 +1107,6 @@ def visualize_simulation_results(simulation_result_file_path: str, store_figure_ return loaded_results_dict - def point_is_in_rect(x, y, rect): """Check if a given x-y point is inside a rectangular field (e.g. inside a conductor).""" # x, y of the point diff --git a/gui/femmt_gui.py b/gui/femmt_gui.py index cffe1dcb..1159bb64 100644 --- a/gui/femmt_gui.py +++ b/gui/femmt_gui.py @@ -3195,14 +3195,31 @@ def md_action_run_simulation(self) -> None: self.md_loss_plot_label.setMask(pixmap.mask()) self.md_loss_plot_label.show() - inductance = loaded_results_dict["single_sweeps"][0]["winding1"]["flux_over_current"][0] + # inductance = loaded_results_dict["single_sweeps"][0]["winding1"]["flux_over_current"][0] loss_core_eddy_current = loaded_results_dict["total_losses"]["eddy_core"] loss_core_hysteresis = loaded_results_dict["total_losses"]["hyst_core_fundamental_freq"] - loss_winding_1 = loaded_results_dict["total_losses"]["winding1"]["total"] + # loss_winding_1 = loaded_results_dict["total_losses"]["winding1"]["total"] self.md_loss_core_hysteresis_label.setText(f"Core Hysteresis loss: {loss_core_hysteresis} W") self.md_loss_core_eddy_current_label.setText(f"Core Eddy Current loss: {loss_core_eddy_current} W") - self.md_loss_winding1_label.setText(f"Winding 1 loss: {loss_winding_1} W") - self.md_inductance_label.setText(f"Primary Inductance: {inductance} H") + # self.md_loss_winding1_label.setText(f"Winding 1 loss: {loss_winding_1} W") + # self.md_inductance_label.setText(f"Primary Inductance: {inductance} H") + inductances = [] + windings_loss = [] + for i in range(1, 3): # for 3 windings + winding_key = f"winding{i}" + if winding_key in loaded_results_dict["single_sweeps"][0]: + inductances.append(loaded_results_dict["single_sweeps"][0][winding_key]["flux_over_current"][0]) + windings_loss.append(loaded_results_dict["total_losses"][winding_key]["total"]) + + # show them just for 2 windings in GUI + if self.md_simulation_type_comboBox.currentText() == self.translation_dict['inductor']: + self.md_loss_winding1_label.setText(f"Winding 1 loss: {windings_loss[0]} W") + self.md_inductance1_label.setText(f"Primary Inductance: {inductances[0]} H") + elif self.md_simulation_type_comboBox.currentText() == self.translation_dict['transformer']: + self.md_loss_winding1_label.setText(f"Winding 1 loss: {windings_loss[0]} W") + self.md_loss_winding2_label.setText(f"Winding 2 loss: {windings_loss[1]} W") + self.md_inductance1_label.setText(f"Primary Inductance: {inductances[0]} H") + self.md_inductance2_label.setText(f"Secondary Inductance: {inductances[1]} H") # log_path = geo.e_m_results_log_path # simulation_results = str(fmt.read_results_log(log_path)) diff --git a/gui/femmt_gui.ui b/gui/femmt_gui.ui index 861c5d60..079a1ac4 100644 --- a/gui/femmt_gui.ui +++ b/gui/femmt_gui.ui @@ -30,7 +30,7 @@ - 1 + 0 @@ -46,7 +46,7 @@ - 0 + 2 @@ -1822,15 +1822,15 @@ Simulation Text Output - - + + - - + + @@ -1843,6 +1843,13 @@ + + + + + + + @@ -1850,8 +1857,8 @@ - - + + @@ -9557,7 +9564,7 @@ 0 0 - 98 + 1072 1024