def _set_boundary(self, mesh): bd_u = Ofpp.parse_boundary_field(self.path_dir + self.path_bd_u) bd_p = Ofpp.parse_boundary_field(self.path_dir + self.path_bd_p) # mesh = Ofpp.FoamMesh(self.path_dir) def make_bd_tuple(bd_key): source_dic = mesh.boundary[bd_key] if b'value' in bd_u[bd_key]: u_val = bd_u[bd_key][b'value'] else: u_val = None if b'value' in bd_p[bd_key]: p_val = bd_p[bd_key][b'value'] else: p_val = None bd_tuple = self._bd_tuple(bd_key.decode(), source_dic.type.decode(), source_dic.id, source_dic.start, source_dic.num, u_val, p_val) return bd_tuple bd = [make_bd_tuple(key) for key in mesh.boundary.keys()] owner = np.array(mesh.owner) self.boundary = bd self.bd_nums = [bd[i].num for i in range(len(bd))] self.bd_faces = [ np.arange(bd[i].i_start, bd[i].i_start + bd[i].num) for i in range(len(bd)) ] self.bd_cells = [owner[self.bd_faces[i]] for i in range(len(bd))]
def update_from_file(self, path_u=None, path_p=None, path_rho=None): if path_u is None: path_u = self.path_u if path_p is None: path_p = self.path_p if path_rho is None: path_rho = self.path_rho u_data = Ofpp.parse_internal_field(self.path_dir + path_u) p_data = Ofpp.parse_internal_field(self.path_dir + path_p) rho_data = Ofpp.parse_internal_field(self.path_dir + path_rho) # Calculate conservative variables u = u_data[:, 0] v = u_data[:, 1] w = u_data[:, 2] ru = rho_data * u rv = rho_data * v rw = rho_data * w e = p_data / (1.4 - 1.0) + 0.5 * rho_data * (u * u + v * v + w * w) self.n_cell = u_data.shape[0] # self.data = np.hstack((rho_data[:, np.newaxis], u_data, p_data[:, np.newaxis])) self.data = np.vstack((rho_data, ru, rv, rw, e)).T
def getfeatures(path): import Ofpp dir_xyz = '0' meshx_test = Ofpp.parse_internal_field(path+dir_xyz+'/ccx') meshy_test = Ofpp.parse_internal_field(path+dir_xyz+'/ccy') meshz_test = Ofpp.parse_internal_field(path+dir_xyz+'/ccz') meshx_test = meshx_test + 0.02 + 0.018 mesh_test = np.vstack([meshx_test.T,meshy_test.T,meshz_test.T]).T return mesh_test
def _init_mesh(self): mesh = Ofpp.FoamMesh(self.path_dir) mesh.read_cell_centres(self.path_dir + self.path_centres) mesh.read_cell_volumes(self.path_dir + self.path_vols) self.nodes = mesh.points self.face_nodes = mesh.faces self.cell_faces = mesh.cell_faces self.n_node = len(self.nodes) # self.n_face = mesh.num_inner_face self.n_face = mesh.num_face self.n_bdface = mesh.num_face - mesh.num_inner_face self.n_cell = len(self.cell_faces) self.owner = mesh.owner self.neighbour = mesh.neighbour self.centers = mesh.cell_centres self.volumes = mesh.cell_volumes self._set_boundary(mesh) self._calc_face_centers() self._calc_face_vec() self._calc_vec_lr()
def update_from_file(self, path_u=None, path_p=None, path_rho=None): if path_u is None: path_u = self.path_u if path_p is None: path_p = self.path_p if path_rho is None: path_rho = self.path_rho u_data = Ofpp.parse_internal_field(self.path_dir + path_u) p_data = Ofpp.parse_internal_field(self.path_dir + path_p) rho_data = Ofpp.parse_internal_field(self.path_dir + path_rho) # Calculate temperature t_data = 1.4 * p_data / rho_data self.n_cell = u_data.shape[0] # self.data = np.hstack((rho_data[:, np.newaxis], u_data, p_data[:, np.newaxis])) self.data = np.hstack( (rho_data[:, np.newaxis], u_data, t_data[:, np.newaxis]))
def getfeatures(path): import os import Ofpp dir_xyz = '0' meshx_test = Ofpp.parse_internal_field(path + dir_xyz + '/ccx') meshy_test = Ofpp.parse_internal_field(path + dir_xyz + '/ccy') meshz_test = Ofpp.parse_internal_field(path + dir_xyz + '/ccz') meshx_test = meshx_test + 0.02 V = Ofpp.parse_internal_field(path + dir_xyz + '/V') mesh_test = np.vstack([meshx_test.T, meshy_test.T, meshz_test.T]).T folders = next(os.walk(path))[1] new_folders = [folder for folder in folders if folder.isdigit()] num_folders = [int(folder) for folder in new_folders] dir_result = str(max(num_folders)) U = Ofpp.parse_internal_field(path + dir_result + '/U') gradU = Ofpp.parse_internal_field(path + dir_result + '/gradU') dwall = Ofpp.parse_internal_field(path + dir_result + '/dwall') if os.path.isfile(path + dir_result + '/k'): k = Ofpp.parse_internal_field(path + dir_result + '/k') else: k = np.copy(meshx_test) k[:] = 0 feature = np.vstack([U.T, gradU.T, dwall.T, k.T, V.T]).T return feature, mesh_test
current_path = join(thisTime_path, column) this_time_dir = os.listdir(thisTime_path) # # print(this_time_dir) # if 'PV' in this_time_dir: # PV_FLAG = True # else: # PV_FLAG = False if 'f_Bilger' in this_time_dir: f_Bilger_FLAG = True else: f_Bilger_FLAG = False if os.path.exists(current_path): this_df[column] = Ofpp.parse_internal_field( current_path) # column is also the field name here else: #raise FileNotFoundError print('%s Not Found!' % column) this_df[column] = 0 # f_Bilger computation Y = this_df[species_lu19].values f_Bilger = compute_fBilger(Y) # print('f_Bilger: ',f_Bilger) this_df['f_Bilger'] = f_Bilger print(' ') print(this_df.head()) print(' ')
import torch.optim as optim import pdb from torch.utils.data import DataLoader import time from scipy.interpolate import interp1d import tikzplotlib sys.path.insert(0, '../source') from dataset import VaryGeoDataset from pyMesh import hcubeMesh, visualize2D, plotBC, plotMesh,setAxisLabel,\ np2cuda,to4DTensor from model import USCNN from readOF import convertOFMeshToImage,convertOFMeshToImage_StructuredMesh from sklearn.metrics import mean_squared_error as calMSE import Ofpp h=0.01 OFBCCoord=Ofpp.parse_boundary_field('TemplateCase/30/C') OFLOWC=OFBCCoord[b'low'][b'value'] OFUPC=OFBCCoord[b'up'][b'value'] OFLEFTC=OFBCCoord[b'left'][b'value'] OFRIGHTC=OFBCCoord[b'right'][b'value'] leftX=OFLEFTC[:,0];leftY=OFLEFTC[:,1] lowX=OFLOWC[:,0];lowY=OFLOWC[:,1] rightX=OFRIGHTC[:,0];rightY=OFRIGHTC[:,1] upX=OFUPC[:,0];upY=OFUPC[:,1] ny=len(leftX);nx=len(lowX) myMesh=hcubeMesh(leftX,leftY,rightX,rightY, lowX,lowY,upX,upY,h,True,True, tolMesh=1e-10,tolJoint=1) batchSize=1 NvarInput=2 NvarOutput=1
# u_np = VN.vtk_to_numpy(data.GetCellData().GetArray('U')) # # # # myArr = vtk.vtkPassArrays() # myArr.SetInputDataObject(reader.GetInputDataObject(0, 0)) # p2c = vtk.vtkCellDataToPointData() # p2c.SetInputConnection(reader.GetOutputPort()) # p2c.Update() import Ofpp as of import numpy as np import matplotlib.pyplot as plt # From low to high x, then low to high y, lastly, low to high z U = of.parse_internal_field('./ABL_N_H/Field/20000.9038025/U') ccx = of.parse_internal_field('./ABL_N_H/Field/20000.9038025/ccx') ccy = of.parse_internal_field('./ABL_N_H/Field/20000.9038025/ccy') ccz = of.parse_internal_field('./ABL_N_H/Field/20000.9038025/ccz') cc = np.vstack((ccx, ccy, ccz)) sliceOrigin = (0, 0, 90) sliceNormal = (0, 0, 1) idx = np.argwhere((ccz > 94) & (ccz < 96)) Uground = U[0:90000]
def open_file(self, filename): self.mesh = Ofpp.FoamMesh(self.filename)
help="The path of all merlin runs") args = parser.parse_args() DATA_DIR = args.data_dir X = args.merlin_paths dir_names = [DATA_DIR + "/" + Xi + "/cavity" for Xi in X] num_of_timesteps = 10 U = [] enstrophy = [] for i, dir_name in enumerate(dir_names): for name in glob.glob(dir_name + "/[0-9]*/U"): if name[-4:] == "/0/U": continue U.append(Ofpp.parse_internal_field(name)) for name in glob.glob(dir_name + "/[0-9]*/enstrophy"): if name[-12:] == "/0/enstrophy": continue enstrophy.append(Ofpp.parse_internal_field(name)) resolution = np.array(enstrophy).shape[-1] U = np.array(U).reshape(len(dir_names), num_of_timesteps, resolution, 3) enstrophy = np.array(enstrophy).reshape(len(dir_names), num_of_timesteps, resolution) / float(resolution) np.savez("data.npz", U, enstrophy)
from unittest import TestCase import unittest import numpy as np import Ofpp from smithers.io.openfoamhandler import OpenFoamHandler openfoam_mesh_path = 'tests/test_datasets/openfoam_mesh' notime_openfoam_mesh_path = 'tests/test_datasets/notime_openfoam_mesh' handler = OpenFoamHandler() mesh = handler.read(openfoam_mesh_path) truth_mesh = Ofpp.FoamMesh(openfoam_mesh_path) class TestOpenFoamHandler(TestCase): def test_read(self): assert type(mesh) == dict assert 'points' in mesh['0'] assert 'faces' in mesh['0'] assert 'boundary' in mesh['0'] assert 'cells' in mesh['0'] def test_read_boundary_names(self): assert set(mesh['0']['boundary'].keys()) == set([b'inlet', b'outlet', b'bottom', b'top', b'obstacle', b'frontAndBack']) def test_read_points(self): np.testing.assert_almost_equal(mesh['0']['points'], truth_mesh.points) def test_read_faces(self): np.testing.assert_almost_equal(mesh['0']['faces'], truth_mesh.faces)
dfdeta=torch.cat((dfdeta_low[:,:,0:2,:],dfdeta_internal,dfdeta_up[:,:,-2:,:]),2) dfdx=Jinv*(dfdxi*dydeta-dfdeta*dydxi) return dfdx def dfdy(f,dxdxi,dxdeta,Jinv): dfdxi_internal=(-f[:,:,:,4:]+8*f[:,:,:,3:-1]-8*f[:,:,:,1:-3]+f[:,:,:,0:-4])/12/h dfdxi_left=(-11*f[:,:,:,0:-3]+18*f[:,:,:,1:-2]-9*f[:,:,:,2:-1]+2*f[:,:,:,3:])/6/h dfdxi_right=(11*f[:,:,:,3:]-18*f[:,:,:,2:-1]+9*f[:,:,:,1:-2]-2*f[:,:,:,0:-3])/6/h dfdxi=torch.cat((dfdxi_left[:,:,:,0:2],dfdxi_internal,dfdxi_right[:,:,:,-2:]),3) dfdeta_internal=(-f[:,:,4:,:]+8*f[:,:,3:-1,:]-8*f[:,:,1:-3,:]+f[:,:,0:-4,:])/12/h dfdeta_low=(-11*f[:,:,0:-3,:]+18*f[:,:,1:-2,:]-9*f[:,:,2:-1,:]+2*f[:,:,3:,:])/6/h dfdeta_up=(11*f[:,:,3:,:]-18*f[:,:,2:-1,:]+9*f[:,:,1:-2,:]-2*f[:,:,0:-3,:])/6/h dfdeta=torch.cat((dfdeta_low[:,:,0:2,:],dfdeta_internal,dfdeta_up[:,:,-2:,:]),2) dfdy=Jinv*(dfdeta*dxdxi-dfdxi*dxdeta) return dfdy os.system('mkdir test') OFBCCoord=Ofpp.parse_boundary_field('TemplateCase_4side/1/C') OFLOWC=OFBCCoord[b'low'][b'value'] OFUPC=OFBCCoord[b'up'][b'value'] OFLEFTC=OFBCCoord[b'left'][b'value'] OFRIGHTC=OFBCCoord[b'right'][b'value'] leftX=OFLEFTC[:,0];leftY=OFLEFTC[:,1] lowX=OFLOWC[:,0];lowY=OFLOWC[:,1] rightX=OFRIGHTC[:,0];rightY=OFRIGHTC[:,1] upX=OFUPC[:,0];upY=OFUPC[:,1] ny=len(leftX);nx=len(lowX) myMesh=hcubeMesh(leftX,leftY,rightX,rightY, lowX,lowY,upX,upY,h,True,True, tolMesh=1e-10,tolJoint=0.2) OFPic=convertOFMeshToImage_StructuredMesh(nx,ny,'TemplateCase_4side/1/C', ['TemplateCase_4side/1/f'], [0,1,0,1],0.0,False)
import pdb from torch.utils.data import DataLoader import time from scipy.interpolate import interp1d import tikzplotlib sys.path.insert(0, '../source') from dataset import VaryGeoDataset from pyMesh import hcubeMesh, visualize2D, plotBC, plotMesh,setAxisLabel,\ np2cuda,to4DTensor from model import USCNN, USCNNSepPhi, USCNNSep, DDBasic from readOF import convertOFMeshToImage, convertOFMeshToImage_StructuredMesh from sklearn.metrics import mean_squared_error as calMSE import Ofpp h = 0.01 OFBCCoord = Ofpp.parse_boundary_field('TemplateCase_simpleVessel/3200/C') OFLOWC = OFBCCoord[b'low'][b'value'] OFUPC = OFBCCoord[b'up'][b'value'] OFLEFTC = OFBCCoord[b'left'][b'value'] OFRIGHTC = OFBCCoord[b'rifht'][b'value'] leftX = OFLEFTC[:, 0] leftY = OFLEFTC[:, 1] lowX = OFLOWC[:, 0] lowY = OFLOWC[:, 1] rightX = OFRIGHTC[:, 0] rightY = OFRIGHTC[:, 1] upX = OFUPC[:, 0] upY = OFUPC[:, 1] ny = len(leftX) nx = len(lowX)