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
|
OutputSwitch = 1;
ReadDatFileSwitch = 1;
CommentExp = '(?:[\s]*)(?:\(0[\s]+")([^\\)]*)(?:[\s]*"\)+)'; DimensionExp = '(?:[\s]*)(?:\(2[\s]+)([23])(?:[\s]*\))'; NodeExp = '(?:[\s]*)(?:\((?:30)?10[\s]+\()([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]+)([012][\s]+)([23])(?:[\s]*\))+'; FaceExp = '(?:[\s]*)(?:\((?:30)?13[\s]+\()([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]*)([0-9a-fA-F]*)(?:[\s]*\))+'; CellExp = '(?:[\s]*)(?:\((?:30)?12[\s]+\()([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]+)([0-9a-fA-F]+[\s]+)(\d+[\s]?)([0-7]?)(?:[\s]*\))+'; ZoneExp = '(?:[\s]*)(?:\(39[\s]+\()([0-9]+[\s]+)([a-z\-]+[\s]+)([a-z\_0-9\!\$\%\&\*\/\:\<\=\>\?\~\^\.\+\-]+[\s]+)([0-9]+[\s]*)(?:\))+';
CellElementType = [3,3;4,4;4,4;8,6;5,5;6,5];
TotalNumOfNode = 0; TotalNumOfFace = 0; TotalNumOfCell = 0; NodeZoneInfo = [0 0 0 0 0]; FaceZoneInfo = [0 0 0 0 0]; CellZoneInfo = [0 0 0 0 0]; NodeList = zeros(3,1); FaceListInNode = zeros(4,1); FaceTypeList = zeros(1,1); FaceBCTypeList = zeros(1,1); NeighbourCellList = zeros(2,1); CellListInFace = zeros(6,1); CellListInNode = zeros(8,1); CellListInCell = zeros(6,1); CellTypeList = zeros(1,1); CellElementTypeList = zeros(1,1); ZoneInfoList = {0, '', '', 0}; Is3D = 0; NodeZoneInfoFlag = 0; FaceZoneInfoFlag = 0; CellZoneInfoFlag = 0; ZoneInfoListFlag = 0;
[FileName, PathName] = uigetfile('*.cas', 'Select a fluent case file'); if isequal(FileName,0) disp('User selected Cancel') else CasFileId = fopen(fullfile(PathName, FileName)); while ~feof(CasFileId) Line = fgetl(CasFileId); if length(Line) < 65536 if ~isempty(regexp(Line, CommentExp, 'match')) TokenStr = regexp(Line, CommentExp, 'tokens'); CommentStr = cell2mat(TokenStr{1}); disp(CommentStr); elseif ~isempty(regexp(Line, DimensionExp, 'match')) TokenStr = regexp(Line, DimensionExp, 'tokens'); DimensionNum = str2num(cell2mat(TokenStr{1})); if DimensionNum == 3 Is3D = 1; disp('The grid is 3D.') else Is3D = 0; disp('The grid is not 3D.') end elseif ~isempty(regexp(Line, NodeExp, 'match')) TokenStr = regexp(Line, NodeExp, 'tokens'); ZoneID = hex2dec(TokenStr{1}{1}); if(ZoneID == 0) TotalNumOfNode = hex2dec(TokenStr{1}{3}); fprintf("Total number of nodes is: %d\n", TotalNumOfNode); NodeList = zeros(3,TotalNumOfNode); else CurrentInfo = 1; if NodeZoneInfoFlag == 0 NodeZoneInfoFlag = 1; else NodeZoneInfo = [NodeZoneInfo;[0 0 0 0 0]]; CurrentInfo = size(NodeZoneInfo,1); end NodeZoneInfo(CurrentInfo,1) = ZoneID; NodeZoneInfo(CurrentInfo,2) = hex2dec(TokenStr{1}{2}); NodeZoneInfo(CurrentInfo,3) = hex2dec(TokenStr{1}{3}); NodeZoneInfo(CurrentInfo,4) = hex2dec(TokenStr{1}{4}); NodeZoneInfo(CurrentInfo,5) = hex2dec(TokenStr{1}{5}); fprintf("Start to read node information in section %d from %d to %d...\n", NodeZoneInfo(CurrentInfo,1), NodeZoneInfo(CurrentInfo,2), NodeZoneInfo(CurrentInfo,3)); fseek(CasFileId, 1, 'cof'); NodeList(:, NodeZoneInfo(CurrentInfo,2): NodeZoneInfo(CurrentInfo,3)) = fread(CasFileId, [3 NodeZoneInfo(CurrentInfo,3)-NodeZoneInfo(CurrentInfo,2)+1], 'double'); disp("Read node information done."); end elseif ~isempty(regexp(Line, FaceExp, 'match')) TokenStr = regexp(Line, FaceExp, 'tokens'); ZoneID = hex2dec(TokenStr{1}{1}); if(ZoneID == 0) TotalNumOfFace = hex2dec(TokenStr{1}{3}); fprintf("Total number of faces is: %d\n", TotalNumOfFace); FaceListInNode = zeros(4,TotalNumOfFace); NeighbourCellList = zeros(2,TotalNumOfFace); FaceTypeList = zeros(1,TotalNumOfFace); FaceBCTypeList = zeros(1,TotalNumOfFace); else CurrentInfo = 1; if FaceZoneInfoFlag == 0 FaceZoneInfoFlag = 1; else FaceZoneInfo = [FaceZoneInfo;[0 0 0 0 0]]; CurrentInfo = size(FaceZoneInfo,1); end FaceZoneInfo(CurrentInfo,1) = ZoneID; FaceZoneInfo(CurrentInfo,2) = hex2dec(TokenStr{1}{2}); FaceZoneInfo(CurrentInfo,3) = hex2dec(TokenStr{1}{3}); FaceZoneInfo(CurrentInfo,4) = hex2dec(TokenStr{1}{4}); FaceZoneInfo(CurrentInfo,5) = hex2dec(TokenStr{1}{5}); FaceBCTypeList(FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = FaceZoneInfo(CurrentInfo,4); fprintf("Start to read face information in section %d from %d to %d...\n", FaceZoneInfo(CurrentInfo,1), FaceZoneInfo(CurrentInfo,2), FaceZoneInfo(CurrentInfo,3)); fseek(CasFileId, 1, 'cof'); Offset1 = ftell(CasFileId); tmpLine = fgetl(CasFileId); while ~strcmp(tmpLine, 'End of Binary Section 3013)') tmpLine = fgetl(CasFileId); end Offset = ftell(CasFileId) - Offset1 - size(tmpLine,2)-3; fseek(CasFileId, Offset1, 'bof'); if FaceZoneInfo(CurrentInfo,4) < 1000 if FaceZoneInfo(CurrentInfo,5) == 0 NumBytes = Offset/(FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1)/2; ReadValueType = ''; if NumBytes > 4 && NumBytes < 7 ReadValueType = 'ushort'; elseif NumBytes > 8 && NumBytes < 14 ReadValueType = 'ulong'; else msgbox("Not ushort or ulong? Binary data read error!"); exit; end for j=FaceZoneInfo(CurrentInfo,2):1:FaceZoneInfo(CurrentInfo,3) FaceTypeList(j) = fread(CasFileId, [1 1], ReadValueType); if FaceTypeList(j) >= 2 && FaceTypeList(j) <= 4 FaceListInNode(1:FaceTypeList(j),j) = fread(CasFileId, [FaceTypeList(j) 1], ReadValueType); NeighbourCellList(:,j) = fread(CasFileId, [2 1], ReadValueType); elseif FaceTypeList(j) == 5 msgbox("Polygonal in mixed face zone not coded yet."); exit; else msgbox("Face type not 0, 2, 3, 4, or 5? Binary data read error!"); exit; end end disp("Read face information done."); elseif FaceZoneInfo(CurrentInfo,5) == 5 msgbox("Polygonal in mixed face zone not coded yet."); exit; else CurFaceType = FaceZoneInfo(CurrentInfo,5); NumBytes = round(Offset/(FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1)/(CurFaceType+2)); FaceData = 0; if NumBytes == 2 FaceData = fread(CasFileId, [CurFaceType+2 FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1], 'ushort'); elseif NumBytes == 4 FaceData = fread(CasFileId, [CurFaceType+2 FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1], 'ulong'); else msgbox("Binary data read error!"); exit; end FaceTypeList(FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = CurFaceType; FaceListInNode(1:CurFaceType,FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = FaceData(1:CurFaceType, :); NeighbourCellList(:,FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = FaceData(CurFaceType+1:CurFaceType+2, :); disp("Read face information done."); end else if FaceZoneInfo(CurrentInfo,5) == 0 NumBytes = Offset/(FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1)/3; FaceData = 0; if NumBytes == 2 FaceData = fread(CasFileId, [3 FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1], 'ushort'); elseif NumBytes == 4 FaceData = fread(CasFileId, [3 FaceZoneInfo(CurrentInfo,3)-FaceZoneInfo(CurrentInfo,2)+1], 'ulong'); else msgbox("Intersection of non-conformal grid read not ushort or ulong? Binary data read error!"); exit; end FaceTypeList(FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = 0; FaceListInNode(:,FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = 0; NeighbourCellList(:,FaceZoneInfo(CurrentInfo,2):FaceZoneInfo(CurrentInfo,3)) = FaceData(2:3,:); disp("Read face information done."); elseif FaceZoneInfo(CurrentInfo,5) == 5 msgbox("Polygonal in Intersection of non-conformal grids? should never be here!"); exit; else msgbox("Intersection of non-conformal grids? should never be here!"); exit; end end end elseif ~isempty(regexp(Line, CellExp, 'match')) TokenStr = regexp(Line, CellExp, 'tokens'); ZoneID = hex2dec(TokenStr{1}{1}); if(ZoneID == 0) TotalNumOfCell = hex2dec(TokenStr{1}{3}); fprintf("Total number of cells is: %d\n", TotalNumOfCell); CellListInFace = zeros(6,TotalNumOfCell); CellListInNode = zeros(8,TotalNumOfCell); CellTypeList = zeros(1,TotalNumOfCell); CellElementTypeList = zeros(1,TotalNumOfCell); else CurrentInfo = 1; if CellZoneInfoFlag == 0 CellZoneInfoFlag = 1; else CellZoneInfo = [CellZoneInfo;[0 0 0 0 0]]; CurrentInfo = size(CellZoneInfo,1); end CellZoneInfo(CurrentInfo,1) = ZoneID; CellZoneInfo(CurrentInfo,2) = hex2dec(TokenStr{1}{2}); CellZoneInfo(CurrentInfo,3) = hex2dec(TokenStr{1}{3}); CellZoneInfo(CurrentInfo,4) = hex2dec(TokenStr{1}{4}); CellZoneInfo(CurrentInfo,5) = hex2dec(TokenStr{1}{5}); fprintf("Start to read cell information in section %d from %d to %d...\n", CellZoneInfo(CurrentInfo,1), CellZoneInfo(CurrentInfo,2), CellZoneInfo(CurrentInfo,3)); if CellZoneInfo(CurrentInfo,5) == 0 fseek(CasFileId, 1, 'cof'); Offset1 = ftell(CasFileId); tmpLine = fgetl(CasFileId); while ~strcmp(tmpLine, 'End of Binary Section 3012)') tmpLine = fgetl(CasFileId); end Offset = ftell(CasFileId) - Offset1 - size(tmpLine,2)-3; fseek(CasFileId, Offset1, 'bof'); NumBytes = Offset/(CellZoneInfo(CurrentInfo,3)-CellZoneInfo(CurrentInfo,2)+1); if NumBytes == 2 CellElementTypeList(CellZoneInfo(CurrentInfo,2):CellZoneInfo(CurrentInfo,3)) = fread(CasFileId, [1 CellZoneInfo(CurrentInfo,3)-CellZoneInfo(CurrentInfo,2)+1], 'ushort'); elseif NumBytes == 4 CellElementTypeList(CellZoneInfo(CurrentInfo,2):CellZoneInfo(CurrentInfo,3)) = fread(CasFileId, [1 CellZoneInfo(CurrentInfo,3)-CellZoneInfo(CurrentInfo,2)+1], 'ulong'); else msgbox("Intersection of non-conformal grid read not ushort or ulong? Binary data read error!"); exit; end else CellElementTypeList(CellZoneInfo(CurrentInfo,2):CellZoneInfo(CurrentInfo,3)) = CellZoneInfo(CurrentInfo,5); end CellTypeList(CellZoneInfo(CurrentInfo,2):CellZoneInfo(CurrentInfo,3)) = CellZoneInfo(CurrentInfo,4); end elseif ~isempty(regexp(Line, ZoneExp, 'match')) TokenStr = regexp(Line, ZoneExp, 'tokens'); CurrentInfo = 1; if ZoneInfoListFlag == 0 ZoneInfoListFlag = 1; else ZoneInfoList = [ZoneInfoList;{0,'','',0}]; CurrentInfo = size(ZoneInfoList,1); end ZoneInfoList{CurrentInfo,1} = str2num(TokenStr{1}{1}); ZoneInfoList{CurrentInfo,2} = TokenStr{1}{2}; ZoneInfoList{CurrentInfo,3} = TokenStr{1}{3}; ZoneInfoList{CurrentInfo,4} = str2num(TokenStr{1}{4}); fprintf("Zone information obtained: Zone ID=%d, Zone Type=%s, Zone Name=%s, Domain ID=%d.\n", ZoneInfoList{CurrentInfo,1}, ZoneInfoList{CurrentInfo,2}, ZoneInfoList{CurrentInfo,3}, ZoneInfoList{CurrentInfo,4}); end end end fclose(CasFileId); fprintf("Start to initialize cell geometrical information...\n"); fprintf("Step 1: Initialize cell surrounding faces...\n"); CellSurroundFaceCount = zeros(1,TotalNumOfCell); for i=1:1:TotalNumOfFace if FaceListInNode(1,i) ~= 0 && FaceListInNode(2,i) ~= 0 && FaceListInNode(3,i) ~= 0 && FaceListInNode(4,i) ~= 0 if NeighbourCellList(1,i) > 0 && NeighbourCellList(1,i) <= TotalNumOfCell CellSurroundFaceCount(1,NeighbourCellList(1,i)) = CellSurroundFaceCount(1,NeighbourCellList(1,i)) + 1; CellListInFace(CellSurroundFaceCount(1,NeighbourCellList(1,i)), NeighbourCellList(1,i)) = i; end if NeighbourCellList(2,i) > 0 && NeighbourCellList(2,i) <= TotalNumOfCell CellSurroundFaceCount(1,NeighbourCellList(2,i)) = CellSurroundFaceCount(1,NeighbourCellList(2,i)) + 1; CellListInFace(CellSurroundFaceCount(1,NeighbourCellList(2,i)), NeighbourCellList(2,i)) = i; end end end fprintf("Cell surrounding faces initialization done.\n"); fprintf("Step 2: Initialize cell vertex...\n"); for i=1:1:TotalNumOfCell VertexMat = [FaceListInNode(:,CellListInFace(1,i)), FaceListInNode(:,CellListInFace(2,i)), FaceListInNode(:,CellListInFace(3,i)), FaceListInNode(:,CellListInFace(4,i)), FaceListInNode(:,CellListInFace(5,i)), FaceListInNode(:,CellListInFace(6,i))]; VertexMat = reshape(VertexMat, [1 24]); VertexMat = sort(VertexMat); VertexMat = reshape(VertexMat, [3 8]); CellListInNode(:,i) = VertexMat(1,:)'; end fprintf("Cell vertex initialization done.\n"); fprintf("Step 3: Initialize cell surrounding cells......\n"); CellListInCell = zeros(6,TotalNumOfCell); for i=1:1:TotalNumOfCell for j=1:1:6 if NeighbourCellList(1,CellListInFace(j,i)) == 0 || NeighbourCellList(2,CellListInFace(j,i)) == 0 CellListInCell(j,i) = 0; else if NeighbourCellList(1,CellListInFace(j,i)) == i CellListInCell(j,i) = NeighbourCellList(2,CellListInFace(j,i)); else CellListInCell(j,i) = NeighbourCellList(1,CellListInFace(j,i)); end end end end fprintf("Cell surrounding cells initialization done.\n"); fprintf("Cell geometrical information initialization done.\n"); fprintf("Start to calculate cell center...\n"); CellCenter = zeros(3,TotalNumOfCell); for i=1:1:TotalNumOfCell CellCenter(:,i) = 0.125*(NodeList(:,CellListInNode(1,i)) + NodeList(:,CellListInNode(2,i)) + NodeList(:,CellListInNode(3,i)) + NodeList(:,CellListInNode(4,i)) + NodeList(:,CellListInNode(5,i)) + NodeList(:,CellListInNode(6,i)) + NodeList(:,CellListInNode(7,i)) + NodeList(:,CellListInNode(8,i))); end fprintf("Cell center calculation done.\n"); if OutputSwitch==1 fprintf("Start to output NeighbourCellList and CellListInFace.\n"); save([PathName,'\\NeighbourCellList.mat'],'NeighbourCellList'); save([PathName,'\\CellListInFace.mat'],'CellListInFace'); save([PathName,'\\CellListInCell.mat'],'CellListInCell'); end end
if ReadDatFileSwitch == 1 VelocityInCell = zeros(TotalNumOfCell,3); TemperatureInCell = zeros(TotalNumOfCell,1); DataExp = '(?:[\s]*)(?:\(3300[\s]+\()([0-9]+[\s]+)([0-9]+[\s]+)([0-9]+[\s]+)([0-9]+[\s]+)([0-9]+[\s]+)([0-9]+[\s]+)([0-9]+)(?:[\s]*\))+'; SV_UFlag = 111; SV_VFlag = 112; SV_WFlag = 113; SV_TFlag = 3; MaxZoneNum = 23; TargetZoneFlag = zeros(MaxZoneNum,1); TargetZoneFlag(16) = 1; fprintf("Start to read data file...\n"); DatFileName = [FileName(1:length(FileName)-3),'dat']; if exist(fullfile(PathName, DatFileName),'file') DatFileId = fopen(fullfile(PathName, DatFileName)); while ~feof(DatFileId) Line = fgetl(DatFileId); if length(Line) < 65536 if ~isempty(regexp(Line, DataExp, 'match')) TokenStr = regexp(Line, DataExp, 'tokens'); SubSectionId = str2num(TokenStr{1}{1}); TargetZoneId = str2num(TokenStr{1}{2}); if SubSectionId == SV_UFlag && TargetZoneFlag(TargetZoneId) StartCell = str2num(TokenStr{1}{6}); EndCell = str2num(TokenStr{1}{7}); fprintf("Start to read x velocity information in cell zone %d from %d to %d...\n", TargetZoneId, StartCell, EndCell); fseek(DatFileId, 1, 'cof'); VelocityInCell(StartCell:EndCell,1) = fread(DatFileId, [1 EndCell-StartCell+1], 'double'); fprintf("Reading x velocity information in cell zone %d done\n", TargetZoneId); elseif SubSectionId == SV_VFlag && TargetZoneFlag(TargetZoneId) StartCell = str2num(TokenStr{1}{6}); EndCell = str2num(TokenStr{1}{7}); fprintf("Start to read y velocity information in cell zone %d from %d to %d...\n", TargetZoneId, StartCell, EndCell); fseek(DatFileId, 1, 'cof'); VelocityInCell(StartCell:EndCell,2) = fread(DatFileId, [1 EndCell-StartCell+1], 'double'); fprintf("Reading y velocity information in cell zone %d done\n", TargetZoneId); elseif SubSectionId == SV_WFlag && TargetZoneFlag(TargetZoneId) StartCell = str2num(TokenStr{1}{6}); EndCell = str2num(TokenStr{1}{7}); fprintf("Start to read z velocity information in cell zone %d from %d to %d...\n", TargetZoneId, StartCell, EndCell); fseek(DatFileId, 1, 'cof'); VelocityInCell(StartCell:EndCell,3) = fread(DatFileId, [1 EndCell-StartCell+1], 'double'); fprintf("Reading z velocity information in cell zone %d done\n", TargetZoneId); elseif SubSectionId == SV_TFlag && TargetZoneFlag(TargetZoneId) StartCell = str2num(TokenStr{1}{6}); EndCell = str2num(TokenStr{1}{7}); fprintf("Start to read temperature information in cell zone %d from %d to %d...\n", TargetZoneId, StartCell, EndCell); fseek(DatFileId, 1, 'cof'); TemperatureInCell(StartCell:EndCell,1) = fread(DatFileId, [1 EndCell-StartCell+1], 'double'); fprintf("Reading temperature information in cell zone %d done\n", TargetZoneId); end end end end fclose(DatFileId); else fprintf("Fail to read data, dat file does not exist.\n"); end DataInCell = [VelocityInCell,TemperatureInCell]; save([PathName,'\\DataInCell.mat'],'DataInCell'); end
|