Coverage for fiqus/post_processors/PostProcessHomogenizedConductor.py: 15%

78 statements  

« prev     ^ index     » next       coverage.py v7.4.4, created at 2025-11-29 01:35 +0000

1import os, re, logging 

2import numpy as np 

3import pandas as pd 

4import matplotlib.pyplot as plt 

5 

6logger = logging.getLogger('FiQuS') 

7 

8 

9class PostProcess(): 

10 """Class for python post processing of HomogenizedConductor simulations""" 

11 def __init__(self, fdm, solution_folder_path): 

12 self.fdm = fdm 

13 self.solution_folder_path = solution_folder_path 

14 # try to create ROHF plots 

15 self.plot_density_lines() 

16 self.fluxhyst() 

17 self.power_loss() 

18 

19 def load_standard_txt(self, rel_file_path, skiprows=0, transpose=True): 

20 """This function loads standards txt files within the solution folder to numpy arrays. 

21 

22 :param rel_file_path: relative path with file extension within the solution folder 

23 """ 

24 abs_file_path = os.path.join(self.solution_folder_path, rel_file_path) 

25 data = None 

26 if os.path.isfile(abs_file_path): 

27 data = np.loadtxt(abs_file_path, skiprows=skiprows).T if transpose else np.loadtxt(abs_file_path, skiprows=skiprows) 

28 return data 

29 

30 def fluxhyst(self): 

31 """This function tries to load and display the flux hysteresis (ROHF activated) in the solution data.""" 

32 

33 fluxhyst = self.load_standard_txt('txt_files\\fluxhyst.txt') 

34 #flux_average = self.load_standard_txt('txt_files\\flux_density_avg.txt') 

35 #flux_strand = self.load_standard_txt('txt_files\\flux_density_strand_avg.txt') 

36 

37 voltage_rohf = self.load_standard_txt('txt_files\\voltage_rohf.txt') 

38 It = self.load_standard_txt('txt_files\\It.txt', skiprows=1) 

39 

40 if fluxhyst is not None and It is not None: 

41 plt.figure(figsize=(10,5)) 

42 plt.subplot(121) 

43 plt.title('Flux hysteresis') 

44 plt.plot(It[1], fluxhyst[1]) 

45 #plt.plot(It[1], fluxhyst[1] - mu_0/(4*pi) * It[1]) 

46 plt.xlabel('Transport current (A)') 

47 plt.ylabel('Internal Flux (Tm)') 

48 #plt.legend() 

49 

50 plt.subplot(122) 

51 plt.title('ROHF voltage (unit length)') 

52 if voltage_rohf is not None: plt.plot(voltage_rohf[0], voltage_rohf[1]) 

53 plt.xlabel('Time (s)') 

54 plt.ylabel('Voltage (V)') 

55 #plt.legend() 

56 else: 

57 logger.error("POSTPROC: No flux hysteresis data") 

58 

59 def plot_density_lines(self): 

60 

61 fluxdens_line = self.load_standard_txt('txt_files\\flux_density_line.txt', transpose=False) 

62 currentdens_line = self.load_standard_txt('txt_files\\current_density_line.txt', transpose=False) 

63 It = self.load_standard_txt('txt_files\\It.txt') 

64 

65 idx = 10 

66 

67 if fluxdens_line is not None and It is not None: 

68 N_steps = len(It[0]) 

69 time = fluxdens_line[idx][1] 

70 x_vals = np.zeros(200) 

71 fluxdens = np.zeros(200) 

72 for i in range(0,200): 

73 x_vals[i] = fluxdens_line[idx+i*N_steps][2] 

74 fluxdens[i] = fluxdens_line[idx+i*N_steps][5] 

75 

76 plt.figure() 

77 plt.title('fluxdens at '+str(time) +'s') 

78 plt.plot(x_vals, fluxdens) 

79 plt.xlabel('Position on x-axis') 

80 plt.ylabel('Flux density') 

81 

82 if currentdens_line is not None and It is not None: 

83 N_steps = len(It[0]) 

84 time = currentdens_line[idx][1] 

85 x_vals = np.zeros(200) 

86 currentdens = np.zeros(200) 

87 for i in range(0,200): 

88 x_vals[i] = currentdens_line[idx+i*N_steps][2] 

89 currentdens[i] = currentdens_line[idx+i*N_steps][7] 

90 

91 plt.figure() 

92 plt.title('Current denisty at '+str(time) +'s') 

93 plt.plot(x_vals, currentdens) 

94 plt.xlabel('Position on x-axis') 

95 plt.ylabel('Current density') 

96 

97 def power_loss(self): 

98 """This function tries to load and display the power loss from the solution data. Currently only supports ROHF associated losses. """ 

99 

100 power = self.load_standard_txt('txt_files\\power.txt') 

101 

102 if power is not None and len(power) == 5: 

103 plt.figure() 

104 plt.title('Instantaneous power losses') 

105 plt.plot(power[0], power[2], label=r'$P_{\mathrm{irr}}$') 

106 plt.plot(power[0], power[3], label=r'$P_{\mathrm{eddy}}$') 

107 plt.plot(power[0], power[4], label=r'$P_{\mathrm{PL}}$') 

108 plt.legend() 

109 else: 

110 logger.error("POSTPROC: no/wrong power data") 

111 

112 def show(self): 

113 """Utility funtion which is called in the end of the python post proc to display all figures at the same time""" 

114 plt.show() if len(plt.get_fignums()) > 0 else logger.info('- No postproc figures -')