def get_reference_bim(a, t0=0, x_c=0, x0=15, verbose=True): if type(t0) == list: return np.array([r for r in imap(getReferenceBIM, repeat(a), t0, repeat(x_c))]) if verbose: print 'Getting a reference solution for a={} from BIM data'.format(a) numRefDir = os.path.join(os.environ['HOME'], 'work/soliton/fullPotentialSolution') if not(os.path.exists(numRefDir)): sys.exit('Numerical reference directory does not exist: '+numRefDir) x_c = x_c - solitonVelBIM[a]*t0 - x0 N=200 line = (np.ones(N)*x_c, np.linspace(-1, a, N)) u, ext = postprocess.readGphov(os.path.join(numRefDir, str(a), 'u')) v, ext = postprocess.readGphov(os.path.join(numRefDir, str(a), 'v')) grid_x, grid_y = np.mgrid[ext[0]:ext[1]:u.shape[1]*1j, ext[2]:ext[3]:u.shape[0]*1j] u = u.transpose() v = v.transpose() ux_sampled = griddata((grid_x.flatten(), grid_y.flatten()), u.flatten(), line, method='linear', fill_value=0) uy_sampled = griddata((grid_x.flatten(), grid_y.flatten()), v.flatten(), line, method='linear', fill_value=0) return np.array(line).transpose(), np.array([ux_sampled, uy_sampled]).transpose()
def initializeThetis(refDir, path='./', solitonPosition=15): L,H,Nx,Ny = determineMesh() angle,tx,ty = determineRotation() dx = L/Nx dy = H/Ny #Thetis needs values given for nodes,not cell centers; #I have to contruct the volumes around 'nodes', to compute volume fractions for them os.mkdir('objets') interface = np.loadtxt('interface.ini') interface = np.vstack((interface[0],interface)) interface = np.vstack((interface,interface[-1])) interface[0,1] = -20 #to create a closed polygon interface[-1,1] = -20 interface[:,0],interface[:,1] = rotateMesh(interface[:,0],interface[:,1],-angle) interface[:,1] = interface[:,1]+ty N_intf = len(interface) f = open('objets/interface.mxa','w') f.write('{0} {0}\n'.format(N_intf)) np.savetxt(f,interface,fmt='%10.4f') l = np.arange(1,N_intf+1) lista = np.vstack((l[:-1],l[1:])).transpose() np.savetxt(f,lista,fmt='%d') f.write('{0} {1}\n'.format(N_intf,1)) f.close() Xc,Yc = meshNodes(Nx,Ny,0,L,-1,H-1,tx=tx,ty=ty,angle=angle) nodes = np.array([Xc.flatten(),Yc.flatten()]).transpose() for component in ['u','v']: u,extent = readGphov(os.path.join(refDir,component)) Ny_ref,Nx_ref = u.shape X_ref,Y_ref = meshNodes(Nx_ref-1,Ny_ref-1,*extent,tx=-solitonPosition) referencePoints = np.array([X_ref.flatten(),Y_ref.flatten()]).transpose() interp = griddata(referencePoints, u.flatten(), nodes, fill_value=0) np.savetxt(component+'.ini',interp,fmt='%10.6f')
def initializeTruchas(refDir, path='./', solitonPosition=15): L,H,Nx,Ny = determineMesh() angle,tx,ty = determineRotation() X,Y = meshNodes(Nx,Ny,0,L,-1,H-1,tx=tx,ty=ty,angle=angle) f = open('polygons.tmp','w') for i in range(X.shape[0]-1): for j in range(X.shape[1]-1): f.write( "{0:10.6f} {1:10.6f}".format(X[i ,j ],Y[i ,j ])) f.write(" {0:10.6f} {1:10.6f}".format(X[i+1,j ],Y[i+1,j ])) f.write(" {0:10.6f} {1:10.6f}".format(X[i+1,j+1],Y[i+1,j+1])) f.write(" {0:10.6f} {1:10.6f}".format(X[i,j+1 ],Y[i ,j+1])) f.write("\n") f.close() subprocess.call(vofOfManyPolygons + ' interface.ini polygons.tmp vof.ini',shell=True) # VELOCITY Xc,Yc = meshCenters(Nx,Ny,0,L,-1,H-1,tx=tx,ty=ty,angle=angle) cellCenters = np.array([Xc.flatten(),Yc.flatten()]).transpose() for component in ['u','v']: u,extent = readGphov(os.path.join(refDir,component)) Ny_ref,Nx_ref = u.shape X_ref,Y_ref = meshNodes(Nx_ref-1,Ny_ref-1,*extent,tx=-solitonPosition) referencePoints = np.array([X_ref.flatten(),Y_ref.flatten()]).transpose() interp = griddata(referencePoints, u.flatten(), cellCenters, fill_value=0) np.savetxt(component+'.ini',interp,fmt='%10.6f')
def initializeGerris(refDir,solitonPosition=15): L,H,Nx,Ny = determineMesh() dx = L/Nx dy = H/Ny angle,tx,ty = determineRotation() f = open('polygons.tmp','w') X,Y = meshNodes(Nx,Ny,0,L,-1,H-1,tx=tx,ty=ty,angle=angle) #rotated mesh, but also translated to have interface at z=0 eps = 0.000001 #required, so that interpolation points are close, but do not coincide with the actual nodes for i in range(X.shape[0]-1): for j in range(X.shape[1]-1): f.write( "{0:10.6f} {1:10.6f}".format(X[i ,j ],Y[i ,j ])) f.write(" {0:10.6f} {1:10.6f}".format(X[i+1,j ],Y[i+1,j ])) f.write(" {0:10.6f} {1:10.6f}".format(X[i+1,j+1],Y[i+1,j+1])) f.write(" {0:10.6f} {1:10.6f}".format(X[i,j+1 ],Y[i ,j+1])) f.write("\n") f.close() subprocess.call(vofOfManyPolygons + ' interface.ini polygons.tmp t.cgd',shell=True) Xc,Yc = meshCenters(Nx,Ny,0-eps,L+eps,0-eps,H+eps) #actual mesh, interface is at z=1 vof = open('t.cgd').read() f = open('t.cgd','w') f.write('2 y x\n') f.write('{0} {1}\n'.format(Ny,Nx)) np.savetxt(f,Yc[:,0].reshape(1,-1),fmt='%12.6f') #in Gerris sw-level is at z=1; reshape to have it in 1 line np.savetxt(f,Xc[0,:].reshape(1,-1),fmt='%12.6f') #dx/2 needed to point at centers of cells f.write(vof) f.close() #VELOCITY Xc_rot, Yc_rot = meshCenters(Nx,Ny,0,L,-1,H-1,tx=tx,ty=ty,angle=angle) #interface at z=0, for interpolation purposes cellCenters = np.array([Xc_rot.flatten(),Yc_rot.flatten()]).transpose() for component in ['u','v']: u,extent = readGphov(os.path.join(refDir,component)) Ny_ref,Nx_ref = u.shape X_ref,Y_ref = meshNodes(Nx_ref-1,Ny_ref-1,*extent,tx=-solitonPosition) referencePoints = np.array([X_ref.flatten(),Y_ref.flatten()]).transpose() f = open(component+'.cgd','w') f.write('2 y x\n') f.write('{0} {1}\n'.format(Ny,Nx)) interp = griddata(referencePoints, u.flatten(), cellCenters, fill_value=0) np.savetxt(f,Yc[:,0].reshape(1,-1),fmt='%12.6f') #in Gerris sw-level is at z=1; reshape to have it in 1 line np.savetxt(f,Xc[0,:].reshape(1,-1),fmt='%12.6f') #dx/2 needed to point at centers of cells np.savetxt(f,interp,fmt='%.4f') f.close()
def get_reference_bim(a, t0=0, x_c=0, x0=15, verbose=True): if type(t0) == list: return np.array( [r for r in imap(getReferenceBIM, repeat(a), t0, repeat(x_c))]) if verbose: print 'Getting a reference solution for a={} from BIM data'.format(a) numRefDir = os.path.join(os.environ['HOME'], 'work/soliton/fullPotentialSolution') if not (os.path.exists(numRefDir)): sys.exit('Numerical reference directory does not exist: ' + numRefDir) x_c = x_c - solitonVelBIM[a] * t0 - x0 N = 200 line = (np.ones(N) * x_c, np.linspace(-1, a, N)) u, ext = postprocess.readGphov(os.path.join(numRefDir, str(a), 'u')) v, ext = postprocess.readGphov(os.path.join(numRefDir, str(a), 'v')) grid_x, grid_y = np.mgrid[ext[0]:ext[1]:u.shape[1] * 1j, ext[2]:ext[3]:u.shape[0] * 1j] u = u.transpose() v = v.transpose() ux_sampled = griddata((grid_x.flatten(), grid_y.flatten()), u.flatten(), line, method='linear', fill_value=0) uy_sampled = griddata((grid_x.flatten(), grid_y.flatten()), v.flatten(), line, method='linear', fill_value=0) return np.array(line).transpose(), np.array([ux_sampled, uy_sampled]).transpose()
def initializeOpenFOAM(refDir,path='./',solitonPosition=15): points = open(os.path.join(path,'constant/polyMesh/points')).readlines() points = [p.replace('(','').replace(')','') for p in points if p[0] == '('] points = np.loadtxt(points) faces = open(os.path.join(path,'constant/polyMesh/faces')).readlines() #faces = [f[2:].replace(')','') for f in faces if f[:2] == '4('] faces = [f.replace(')','') for f in faces if f[:2] in ['3(','4(']] for i in range(len(faces)): if faces[i][:2] == '3(': faces[i] = faces[i][:-1] + ' ' + faces[i].split()[-1]+'\n' faces[i] = faces[i][2:] faces = np.loadtxt(faces,dtype=np.int) #reading 'owner' - assigning a cell number to each face owner = open(os.path.join(path,'constant/polyMesh/owner')).readlines() owner = [p for p in owner if p[0].isdigit()] owner = np.loadtxt(owner[1:],dtype=np.int) #3D->2D faces = points[faces.flatten()].reshape((-1,12)) #replacing indices with coordinates cellFilter=np.all(faces[:,2::3]==faces[:,2::3].max(),axis=1) #this is needed, because in OpenFOAM mesh is 3D, therefore 2D version must be obtained faces = faces.reshape((-1,3))[:,:2].reshape((-1,8)) #third dimension is not needed faces = faces[cellFilter] owner = owner[cellFilter] cells = np.zeros_like(faces) for iF,iC in enumerate(owner): cells[iC] = faces[iF] angle,tx,ty = determineRotation() x = cells[:,::2] y = cells[:,1::2] x = x - tx y = y - ty cells[:,::2],cells[:,1::2] = rotateMesh(x,y,angle) np.savetxt('polygons.tmp',cells,fmt='%12.6f') subprocess.call(vofOfManyPolygons + ' interface.ini polygons.tmp vof.ini',shell=True) #formatting alpha1 OpenFOAM initial field file org = open(os.path.join(path,'0/alpha1')).readlines() f = open(os.path.join(path,'0/alpha1'),'w') skip=0 for i,l in enumerate(org): if not skip: if 'internalField' in l: if l.split()[1] == 'nonuniform': f.write(l) skip = int(org[i+1]) + 4 #counts for old values, opening and closing brackets and the number of cells and a semicolon else: #replacing the 'internalField uniform 0;' line f.write('internalField nonuniform List<scalar>\n') f.write(str(len(cells)) + '\n(\n') f.write(open('vof.ini').read()) f.write(')\n;\n') else: f.write(l) else: skip = skip-1 f.close() # VELOCITY AND PRESSURE interp = {} cellCenters = np.array([np.average(cells[:,::2],axis=1),np.average(cells[:,1::2],axis=1)]).transpose() for component in ['u','v','p']: u,extent = readGphov(os.path.join(refDir,component),rescale=False) Ny_ref,Nx_ref = u.shape X_ref,Y_ref = meshNodes(Nx_ref-1,Ny_ref-1,*extent,tx=-solitonPosition) referencePoints = np.array([X_ref.flatten(),Y_ref.flatten()]).transpose() interp[component] = griddata(referencePoints, u.flatten(), cellCenters, fill_value=0) interp['p'] = (interp['p'] - cellCenters[:,1]) * g*1000 interp['u'] = interp['u'] * np.sqrt(g) interp['v'] = interp['v'] * np.sqrt(g) org = open(os.path.join(path,'0/U')).readlines() f = open(os.path.join(path,'0/U'),'w') skip=0 for i,l in enumerate(org): if not skip: if 'internalField' in l: if l.split()[1] == 'nonuniform': f.write(l) skip = int(org[i+1]) + 4 #counts for old values, opening and closing brackets and the number of cells and a semicolon else: #replacing the 'internalField uniform 0;' line f.write('internalField nonuniform List<vector>\n') f.write(str(len(cells)) + '\n(\n') np.savetxt(f,np.array([interp['u'],interp['v'],np.zeros(len(cells))]).transpose(),fmt='(%10.6f %10.6f %10.6f)') f.write(')\n;\n') else: f.write(l) else: skip = skip-1 f.close() org = open(os.path.join(path,'0/p_rgh')).readlines() f = open(os.path.join(path,'0/p_rgh'),'w') skip=0 for i,l in enumerate(org): if not skip: if 'internalField' in l: if l.split()[1] == 'nonuniform': f.write(l) skip = int(org[i+1]) + 4 #counts for old values, opening and closing brackets and the number of cells and a semicolon else: #replacing the 'internalField uniform 0;' line f.write('internalField nonuniform List<scalar>\n') f.write(str(len(cells)) + '\n(\n') np.savetxt(f,interp['p'],fmt='%10.6f') f.write(')\n;\n') else: f.write(l) else: skip = skip-1 f.close()