Coverage for /opt/hostedtoolcache/Python/3.11.10/x64/lib/python3.11/site-packages/pysagas/sensitivity/cart3d/utilities.py: 9%

69 statements  

« prev     ^ index     » next       coverage.py v7.6.4, created at 2024-10-30 04:27 +0000

1import os 

2import sys 

3import traceback 

4import pandas as pd 

5from typing import Tuple, List 

6 

7 

8def process_components_file( 

9 a_inf: float, 

10 rho_inf: float, 

11 filepath: str = "Components.i.plt", 

12 write_data: bool = True, 

13 verbosity: int = 1, 

14) -> Tuple[pd.DataFrame, pd.DataFrame]: 

15 """A ParaView script to process Components.i.plt to extract 

16 points and cells with data attached. 

17 

18 Parameters 

19 ---------- 

20 a_inf : float 

21 The freestream speed of sound (m/s). 

22 

23 rho_inf : float 

24 The freestream density (kg/m^3). 

25 

26 filepath : str, optional 

27 The filepath to the Components.i.plt file to be processed. 

28 The default is Components.i.plt. 

29 

30 write_data : bool, optional 

31 Write the flow data to CSV files. The default is True. 

32 

33 verbosity : int, optional 

34 The verbosity of the code. The defualt is 1. 

35 

36 Returns 

37 ------- 

38 points : pd.DataFrame 

39 A DataFrame of the point data. 

40 cells : pd.DataFrame 

41 A DataFrame of the cell data. 

42 """ 

43 # Check file exists 

44 if not os.path.exists(filepath): 

45 raise FileNotFoundError(f"File '{filepath}' does not exist.") 

46 

47 # Define root directory 

48 root_dir = os.path.dirname(filepath) 

49 

50 try: 

51 # import the simple module from the paraview 

52 import vtk 

53 from paraview.simple import ( 

54 VisItTecplotBinaryReader, 

55 _DisableFirstRenderCameraReset, 

56 GetActiveViewOrCreate, 

57 Show, 

58 ProgrammableFilter, 

59 Hide, 

60 PointDatatoCellData, 

61 ExportView, 

62 ) 

63 except ModuleNotFoundError: 

64 # Cannot find paraview python package 

65 print( 

66 "Cannot find ParaView Python package. If ParaView is already " 

67 + "installed, please append the bin/ directory to the Python path. " 

68 + "If it is not installed, please do so. If you are using " 

69 + "an Anaconda environment, you can install using " 

70 + "'conda install -c conda-forge paraview'.\n\n" 

71 ) 

72 tb = traceback.format_exc() 

73 print(tb) 

74 sys.exit() 

75 

76 # disable automatic camera reset on 'Show' 

77 _DisableFirstRenderCameraReset() 

78 

79 # create a new 'VisItTecplotBinaryReader' 

80 if verbosity > 0: 

81 print(f"Loading {filepath}") 

82 componentsiplt = VisItTecplotBinaryReader(FileName=[filepath]) 

83 componentsiplt.MeshStatus = ["Surface"] 

84 componentsiplt.PointArrayStatus = [] 

85 

86 # Properties modified on componentsiplt 

87 componentsiplt.PointArrayStatus = [ 

88 "Cp", 

89 "Pressure", 

90 "Rho", 

91 "U", 

92 "V", 

93 "W", 

94 "x", 

95 "y", 

96 "z", 

97 ] 

98 

99 # get active view 

100 spreadSheetView1 = GetActiveViewOrCreate("SpreadSheetView") 

101 # uncomment following to set a specific view size 

102 # spreadSheetView1.ViewSize = [400, 400] 

103 

104 # show data in view 

105 componentsipltDisplay = Show(componentsiplt, spreadSheetView1) 

106 

107 # update the view to ensure updated data information 

108 spreadSheetView1.Update() 

109 

110 # create a new 'Programmable Filter' 

111 programmableFilter1 = ProgrammableFilter(Input=componentsiplt) 

112 programmableFilter1.Script = "" 

113 programmableFilter1.RequestInformationScript = "" 

114 programmableFilter1.RequestUpdateExtentScript = "" 

115 programmableFilter1.PythonPath = "" 

116 

117 # Properties modified on programmableFilter1 

118 if verbosity > 0: 

119 print(" Dimensionalising attributes.") 

120 programmableFilter1.Script = f""" 

121 # Paraview script to normalise Cart3D variables. 

122 R_gas = 287.058 

123 gamma = 1.4 

124 

125 # Redefine the Cart3D variables - normalised values are *_tilde 

126 output.PointData.append(inputs[0].PointData["U"], "U_tilde") 

127 output.PointData.append(inputs[0].PointData["V"], "V_tilde") 

128 output.PointData.append(inputs[0].PointData["W"], "W_tilde") 

129 output.PointData.append(inputs[0].PointData["Pressure"], "p_tilde") 

130 output.PointData.append(inputs[0].PointData["Rho"], "rho_tilde") 

131 

132 # Define the dimensional flow properties 

133 output.PointData.append({a_inf} * inputs[0].PointData["U"], "U") 

134 output.PointData.append({a_inf} * inputs[0].PointData["V"], "V") 

135 output.PointData.append({a_inf} * inputs[0].PointData["W"], "W") 

136 

137 output.PointData.append({a_inf} * {a_inf} * {rho_inf} * output.PointData["p_tilde"], "p") 

138 output.PointData.append({rho_inf} * output.PointData["rho_tilde"], "rho") 

139 output.PointData.append({a_inf}*{a_inf}*{rho_inf}*output.PointData["p_tilde"] / ({rho_inf}*output.PointData["rho_tilde"]*R_gas), "T") 

140 output.PointData.append((abs(gamma * R_gas * output.PointData["T"]))**0.5, "a") 

141 output.PointData.append((output.PointData["U"]**2 + output.PointData["V"]**2 + output.PointData["W"]**2)**0.5, "V_mag") 

142 output.PointData.append(output.PointData["V_mag"]/output.PointData["a"], "M")""" 

143 programmableFilter1.RequestInformationScript = "" 

144 programmableFilter1.RequestUpdateExtentScript = "" 

145 programmableFilter1.PythonPath = "" 

146 

147 # show data in view 

148 programmableFilter1Display = Show(programmableFilter1, spreadSheetView1) 

149 

150 # hide data in view 

151 Hide(componentsiplt, spreadSheetView1) 

152 

153 # update the view to ensure updated data information 

154 spreadSheetView1.Update() 

155 

156 # create a new 'Point Data to Cell Data' 

157 pointDatatoCellData1 = PointDatatoCellData(Input=programmableFilter1) 

158 

159 # show data in view 

160 pointDatatoCellData1Display = Show(pointDatatoCellData1, spreadSheetView1) 

161 

162 # hide data in view 

163 Hide(programmableFilter1, spreadSheetView1) 

164 

165 # update the view to ensure updated data information 

166 spreadSheetView1.Update() 

167 

168 # Properties modified on pointDatatoCellData1 

169 pointDatatoCellData1.PassPointData = 1 

170 

171 # update the view to ensure updated data information 

172 spreadSheetView1.Update() 

173 

174 # Properties modified on spreadSheetView1 

175 spreadSheetView1.GenerateCellConnectivity = 1 

176 

177 # update the view to ensure updated data information 

178 spreadSheetView1.Update() 

179 

180 # Properties modified on spreadSheetView1 

181 spreadSheetView1.FieldAssociation = "Point Data" 

182 

183 # update the view to ensure updated data information 

184 spreadSheetView1.Update() 

185 

186 # export view 

187 if verbosity > 0: 

188 print(" Saving point data.") 

189 points_filename = os.path.join(root_dir, "points.csv") 

190 ExportView(points_filename, view=spreadSheetView1) 

191 

192 # Properties modified on spreadSheetView1 

193 spreadSheetView1.FieldAssociation = "Cell Data" 

194 

195 # update the view to ensure updated data information 

196 spreadSheetView1.Update() 

197 

198 # export view 

199 if verbosity > 0: 

200 print(" Saving cell data.") 

201 cells_filename = os.path.join(root_dir, "cells.csv") 

202 ExportView(cells_filename, view=spreadSheetView1) 

203 

204 if verbosity > 0: 

205 print("Complete.") 

206 

207 # Read the files to Pandas DataFrames 

208 points = pd.read_csv(points_filename) 

209 cells = pd.read_csv(cells_filename) 

210 

211 if not write_data: 

212 # Delete the files 

213 os.remove(points_filename) 

214 os.remove(cells_filename) 

215 

216 return points, cells