def get_pressure_profile(CFD_folder='./openfoam/', pressure_var='p', axis_val=1): PPObj = ppl.OpenFOAM_PostProc(CFD_folder) pObj = PPObj.plotlist[pressure_var] h, p = pObj.profile(axis=axis_val, startrec=pObj.maxrec, endrec=pObj.maxrec) p = np.squeeze(p) return h, p
import numpy as np import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import sys ppdir = '/home/asufian/Programs/coupled_LAMMPS_OpenFOAM/pyDataView' sys.path.append(ppdir) import postproclib as ppl #Get Post Proc Object fdir = './openfoam/' PPObj = ppl.OpenFOAM_PostProc(fdir) #Get plotting object pObj = PPObj.plotlist['p'] UbObj = PPObj.plotlist['Ub'] #Get profile h, p = pObj.profile(axis=1, startrec=pObj.maxrec, endrec=pObj.maxrec) h, Ub = UbObj.profile(axis=1, startrec=UbObj.maxrec, endrec=UbObj.maxrec) Ub = Ub[:, 1] #Sim box sizes nx = pObj.Raw.ncx ny = pObj.Raw.ncy nz = pObj.Raw.ncz xL = pObj.Raw.xL yL = pObj.Raw.yL zL = pObj.Raw.zL
os.system('./run.sh') #Add pyDataView to system path sys.path.insert(0, "./pyDataView/") try: import postproclib as ppl except ImportError: cmd = "git clone https://github.com/edwardsmith999/pyDataView.git ./pyDataView" downloadout = sp.check_output(cmd, shell=True) print(downloadout) sys.path.insert(0, "./pyDataView") import postproclib as ppl #Get post-processing object OPEN_FOAM_CASE = './openfoam/' PPObj = ppl.OpenFOAM_PostProc(OPEN_FOAM_CASE) #Extract the pressure field object pObj = PPObj.plotlist['p'] #Extract grid configuration and physical size in y-direction (gravity direction) nx = pObj.Raw.ncx ny = pObj.Raw.ncy nz = pObj.Raw.ncz ylo = 0 yhi = pObj.Raw.yL #Extract pressure field for the final timestep p = pObj.read(startrec = pObj.maxrec, endrec = pObj.maxrec) p = np.reshape(p, [nx, ny, nz], order='F') pMean = np.mean(np.mean(p, 2), 0)
def check_OpenFOAM_vs_Analytical(fdir, plotstuff=False): OpenFOAMfdir = fdir + "/cfd_data/openfoam/" print("Openfoam dir = ", OpenFOAMfdir) OpenFOAMuObj = ppl.OpenFOAM_vField(OpenFOAMfdir, parallel_run=True) dt = float(OpenFOAMuObj.Raw.delta_t) tplot = float( OpenFOAMuObj.Raw.header.headerDict['controlDict']['writeInterval']) endtime = float( OpenFOAMuObj.Raw.header.headerDict['controlDict']['endTime']) enditer = int(endtime / dt) OpenFOAMwriteinterval = int(tplot / dt) # Parameters of the cpu topology (cartesian grid) xyzL = [OpenFOAMuObj.Raw.xL, OpenFOAMuObj.Raw.yL, OpenFOAMuObj.Raw.zL] dx = OpenFOAMuObj.Raw.dx dy = OpenFOAMuObj.Raw.dy dz = OpenFOAMuObj.Raw.dz ncx = OpenFOAMuObj.Raw.ncx ncy = OpenFOAMuObj.Raw.ncy ncz = OpenFOAMuObj.Raw.ncz #Analytical solution scriptdir = fdir + "/md_data/python_dummy/" with open(scriptdir + "test_vs_couette_analytical.py", 'r') as f: for l in f: if "U =" in l: d = l.split("=") U = float(d[1].replace("\n", "").replace(" ", "")) nu = OpenFOAMuObj.Raw.nu[0] Re = (xyzL[1] + dy / 2.) / nu CAObj = CA(Re=Re, U=U, Lmin=0., Lmax=xyzL[1], npoints=2 * ncy + 1) if plotstuff: import matplotlib.pyplot as plt fig, ax = plt.subplots(1, 1) n = 0 for time in range(enditer): #Plot data if time % OpenFOAMwriteinterval == 0: rec = int(time / float(OpenFOAMwriteinterval)) try: OpenFOAMpp = ppl.OpenFOAM_PostProc(OpenFOAMfdir) OpenFOAMuObj = OpenFOAMpp.plotlist[ 'Ub'] #ppl.OpenFOAM_vField(OpenFOAMfdir, parallel_run=True) y, u = OpenFOAMuObj.profile(1, startrec=rec, endrec=rec) y_anal, u_anal = CAObj.get_vprofile(time * dt, flip=False) error = (u_anal[1:-1:2] - u[:, 0]) / u_anal[1:-1:2] if plotstuff: l, = ax.plot(u[:, 0], y, 'ro-', label="OpenFOAM domain from file") ax.plot(u_anal[1:-1:2], y_anal[1:-1:2], 'k.-', label="Analytical Solution") #ax.plot(10.*(u[:,0]-u_anal[-2:0:-2]),y,'y--') ax.set_xlim([-0.1, U * 1.1]) ax.set_xlabel("$u$", fontsize=24) ax.set_ylabel("$y$", fontsize=24) plt.legend(loc=1) plt.pause(0.001) plt.savefig('out{:05}.png'.format(n)) n += 1 ax.cla() l2 = np.sqrt(np.sum(error[1:]**2)) if not np.isnan(l2): print("Time = ", time, "Error= ", l2) test_error(l2, time) except AssertionError as e: print("AssertionError ", e) raise except: # ppl.field.OutsideRecRange: print("Error result missing", time, OpenFOAMuObj.maxrec, rec) raise