def plotVel(fieldGens,vinf,BCList,tim): """Plots velocity vector plot Designed to be used with tInt test case 4. Change for different cases as required""" plt.figure() [plt.plot(numpy.array(eachBC.points).transpose()[0],numpy.array(eachBC.points).transpose()[1],linewidth=3.0) for eachBC in BCList] plt.title('Test Case for RK-2 Viscous Flow Around a Cylinder \n Velocity Plot: Time=%f'%tim) X,Y = numpy.meshgrid(numpy.arange(0.0,5.0,0.2),numpy.arange(0.0,2.5,0.1)) plt.xlim([-0.1,5.0]) plt.ylim([-0.1,2.5]) pos=numpy.array([X.flatten(),Y.flatten()]).transpose() # Apply no penetration boundary condition in order to find velocity field [eachBC.findVcp(fieldGens,vinf) for eachBC in BCList] [eachBC.applyNPBC(fieldGens) for eachBC in BCList] Vel = definations.velField(pos,fieldGens,vinf) [eachBC.closeNPBC(fieldGens) for eachBC in BCList] # If any point is inside the boundary give it a zero velocity for j in range(len(pos)): for eachBC in BCList: if eachBC.inBoundary(pos[j]): Vel[j]=numpy.zeros(2) u=Vel.transpose()[0].reshape(X.shape) v=Vel.transpose()[1].reshape(X.shape) plt.quiver(X,Y,u,v) plt.savefig('Velocity_%f.png'%tim) return()
def testCylBC1(Np=50): """Test cylinder-constant velocity-no penetration boundary condition""" vinf = numpy.array([10.0, 0.0]) fieldGens = [] toMod = [] #Create cylinder points rad = 1.0 [points, cpl, normals] = cylBCPoints(rad, Np) #Add boundary condnitions BC = wallNPNSBC(points, cpl, normals) BC.findVcp(fieldGens, vinf) BC.applyNPBC(fieldGens) #Define mesh X, Y = numpy.meshgrid(numpy.arange(-3., 3., 10. / Np), numpy.arange(-3., 3., 10. / Np)) u = numpy.copy(X) v = numpy.copy(X) #Create position array pos = [] for i in range(len(X.flatten())): pos.append([X.flatten()[i], Y.flatten()[i]]) pos = numpy.array(pos) #Calculate field at these positions F = dfn.velField(pos, fieldGens, vinf) # Arrange field values in u and v in order to plot them for i in range(len(X.flatten())): u.ravel()[i] = F[i][0] v.ravel()[i] = F[i][1] if (X.flatten()[i]**2 + Y.flatten()[i]**2 - rad**2) <= 0.2: u.ravel()[i] = 0.0 v.ravel()[i] = 0.0 BC.closeNPBC(fieldGens) plt.figure() plt.quiver(X, Y, u, v) plt.title('Vector field due to boundary conditions') plt.plot( numpy.array(points).transpose()[0], numpy.array(points).transpose()[1], 'o') plt.show()
def testCylBC1(Np=50): """Test cylinder-constant velocity-no penetration boundary condition""" vinf=numpy.array([10.0,0.0]) fieldGens=[] toMod=[] #Create cylinder points rad=1.0 [points,cpl,normals]=cylBCPoints(rad,Np) #Add boundary condnitions BC=wallNPNSBC(points,cpl,normals) BC.findVcp(fieldGens,vinf) BC.applyNPBC(fieldGens) #Define mesh X,Y = numpy.meshgrid(numpy.arange(-3.,3.,10./Np),numpy.arange(-3.,3.,10./Np)) u=numpy.copy(X) v=numpy.copy(X) #Create position array pos=[] for i in range(len(X.flatten())): pos.append([X.flatten()[i],Y.flatten()[i]]) pos=numpy.array(pos) #Calculate field at these positions F=dfn.velField(pos,fieldGens,vinf) # Arrange field values in u and v in order to plot them for i in range(len(X.flatten())): u.ravel()[i]=F[i][0] v.ravel()[i]=F[i][1] if (X.flatten()[i]**2+Y.flatten()[i]**2-rad**2)<=0.2: u.ravel()[i]=0.0 v.ravel()[i]=0.0 BC.closeNPBC(fieldGens) plt.figure() plt.quiver(X,Y,u,v) plt.title('Vector field due to boundary conditions') plt.plot(numpy.array(points).transpose()[0],numpy.array(points).transpose()[1],'o') plt.show()
def checkTreeCoeffs(): """Check whether tree coefficients are properly generated or not. Generates random fieldGens. Find velocities using FMM and regular method, and compares accuracies at different positions""" # Generate random fieldGens and put them in random lists fieldGens = randomFieldGens() # Create random 20 positions; where velocity field will be evaluated pos = numpy.zeros([20, 2]) for i in range(20): pos[i][0] = random.random() pos[i][1] = random.random() # Find Velocity with regular method fieldReg = dfn.velField(pos, fieldGens, vinf=0.0) # Find Velocity with FMM fieldFMM = velFieldFMM(pos, fieldGens, vinf=0.0) # Find and print errors between the two for i in range(20): err = numpy.norm(fieldReg - fieldFMM) print err if err > 1e-06: print "Error is greater than 1e-06"
def checkTreeCoeffs(): """Check whether tree coefficients are properly generated or not. Generates random fieldGens. Find velocities using FMM and regular method, and compares accuracies at different positions""" # Generate random fieldGens and put them in random lists fieldGens = randomFieldGens() # Create random 20 positions; where velocity field will be evaluated pos = numpy.zeros([20,2]) for i in range(20): pos[i][0] = random.random() pos[i][1] = random.random() # Find Velocity with regular method fieldReg = dfn.velField(pos,fieldGens,vinf = 0.0) # Find Velocity with FMM fieldFMM = velFieldFMM(pos,fieldGens,vinf = 0.0) # Find and print errors between the two for i in range(20): err = numpy.norm(fieldReg - fieldFMM) print err if err > 1e-06: print "Error is greater than 1e-06"
def checkFMMTime(): """Compare the time taken to calculate velocity fields on each other with varying number of fieldGens""" Np = [100, 300, 600, 1000, 2000] tReg = [] tFMM = [] for i in Np: # Generate random fieldGens and put them in random lists fieldGens = randomFieldGens(i) # Calculate pos matrix N = 0 pos = [] for eachList in fieldGens: N = N + eachList.nPoints pos = pos + eachList.posit() pos = numpy.array(pos) # Calculate time for regular velocity calculation initT = time.time() fieldReg = dfn.velField(pos, fieldGens, vinf=0.) dt = time.time() - initT tReg.append(dt) # Calculate time fot FMM velocity calculation initT = time.time() fieldFMM = velFieldFMM(pos, fieldGens, vinf=0.) dt = time.time() - initT tFMM.append(dt) # Plot trends plt.figure() plt.plot(Np, tReg, label='Regular') plt.plot(Np, tFMM, label='FMM') plt.legend(loc='upper center') plt.show()
def checkFMMTime(): """Compare the time taken to calculate velocity fields on each other with varying number of fieldGens""" Np = [100,300,600,1000,2000] tReg = [] tFMM = [] for i in Np: # Generate random fieldGens and put them in random lists fieldGens = randomFieldGens(i) # Calculate pos matrix N = 0 pos = [] for eachList in fieldGens: N=N+eachList.nPoints pos=pos+eachList.posit() pos=numpy.array(pos) # Calculate time for regular velocity calculation initT = time.time() fieldReg = dfn.velField(pos,fieldGens,vinf = 0.) dt = time.time() - initT tReg.append(dt) # Calculate time fot FMM velocity calculation initT = time.time() fieldFMM = velFieldFMM(pos,fieldGens,vinf = 0.) dt = time.time() - initT tFMM.append(dt) # Plot trends plt.figure() plt.plot(Np,tReg,label = 'Regular') plt.plot(Np,tFMM,label = 'FMM') plt.legend(loc = 'upper center') plt.show()
def findVcps(self, fieldGens, vinf=0.0): """For slip boundary conditions Vcp is to be find slightly above the control point that is done by using this function. """ self.vcps = dfn.velField(self.cps, fieldGens, vinf)
def findVcp(self, fieldGens=[], vinf=0.0): """find V at control points. This step is to be executed at starting of each step""" self.vcp = dfn.velField(self.cp, fieldGens, vinf)
def advectRK2(dt, toMod=[dfn.vortexList(), dfn.traceList()], fieldGens=[dfn.vortexList(), dfn.linVortList()], vinf=0.0, BCList=[]): """Modify "toMod(List format)" objects according to RK 2 advection based on fieldGens(List format) for advection""" #Calculate total number of toMod Points and append their positions N = 0 pos = [] for eachList in toMod: N = N + eachList.nPoints pos = pos + eachList.posit() pos = numpy.array(pos) oldPos = pos.copy() # Satisfy no penetration [eachBC.findVcp(fieldGens, vinf) for eachBC in BCList] [eachBC.applyNPBC(fieldGens) for eachBC in BCList] #Calculate no slip velocities [eachBC.findVcps(fieldGens, vinf) for eachBC in BCList] # Calculate field at all positions field = dfn.velField(pos, fieldGens, vinf) # Close NP BC [eachBC.closeNPBC(fieldGens) for eachBC in BCList] #Take first step of RK 2 and modify positions n = 0 for eachList in toMod: for eachPoint in eachList.allV: newPos = dfn.eulerInt(pos[n], field[n], dt / 2.0) eachPoint.modifyPos( newPos ) # Note that this step will also modify appropriate positions in fieldGens, because of pointers n = n + 1 #Calculate total number of toMod Points and append their positions newN = 0 pos = [] for eachList in toMod: newN = newN + eachList.nPoints pos = pos + eachList.posit() pos = numpy.array(pos) if newN != N: print "number of points cannot change in an advection step" # Satisfy no penetration [eachBC.findVcp(fieldGens, vinf) for eachBC in BCList] [eachBC.applyNPBC(fieldGens) for eachBC in BCList] #Calculate field with new positions field = dfn.velField(pos, fieldGens, vinf) # Close NP BC [eachBC.closeNPBC(fieldGens) for eachBC in BCList] # Take second step of RK 2 and modify final positions n = 0 for eachList in toMod: for eachPoint in eachList.allV: newPos = dfn.eulerInt(oldPos[n], field[n], dt) dPos = newPos - oldPos[n] #Reflect the new position if it is in boundary for eachBC in BCList: if (eachBC.inBoundary(newPos)): newPos = eachBC.reflect(oldPos[n], dPos) eachPoint.modifyPos(newPos) n = n + 1
def advectRK2(dt,toMod=[dfn.vortexList(),dfn.traceList()],fieldGens=[dfn.vortexList(),dfn.linVortList()],vinf=0.0,BCList=[]): """Modify "toMod(List format)" objects according to RK 2 advection based on fieldGens(List format) for advection""" #Calculate total number of toMod Points and append their positions N=0 pos=[] for eachList in toMod: N=N+eachList.nPoints pos=pos+eachList.posit() pos=numpy.array(pos) oldPos=pos.copy() # Satisfy no penetration [eachBC.findVcp(fieldGens,vinf) for eachBC in BCList] [eachBC.applyNPBC(fieldGens) for eachBC in BCList] #Calculate no slip velocities [eachBC.findVcps(fieldGens,vinf) for eachBC in BCList] # Calculate field at all positions field=dfn.velField(pos,fieldGens,vinf) # Close NP BC [eachBC.closeNPBC(fieldGens) for eachBC in BCList] #Take first step of RK 2 and modify positions n=0 for eachList in toMod: for eachPoint in eachList.allV: newPos=dfn.eulerInt(pos[n],field[n],dt/2.0) eachPoint.modifyPos(newPos) # Note that this step will also modify appropriate positions in fieldGens, because of pointers n=n+1 #Calculate total number of toMod Points and append their positions newN=0 pos=[] for eachList in toMod: newN=newN+eachList.nPoints pos=pos+eachList.posit() pos=numpy.array(pos) if newN!=N: print "number of points cannot change in an advection step" # Satisfy no penetration [eachBC.findVcp(fieldGens,vinf) for eachBC in BCList] [eachBC.applyNPBC(fieldGens) for eachBC in BCList] #Calculate field with new positions field=dfn.velField(pos,fieldGens,vinf) # Close NP BC [eachBC.closeNPBC(fieldGens) for eachBC in BCList] # Take second step of RK 2 and modify final positions n=0 for eachList in toMod: for eachPoint in eachList.allV: newPos=dfn.eulerInt(oldPos[n],field[n],dt) dPos=newPos-oldPos[n] #Reflect the new position if it is in boundary for eachBC in BCList: if (eachBC.inBoundary(newPos)): newPos=eachBC.reflect(oldPos[n],dPos) eachPoint.modifyPos(newPos) n=n+1
def findVcps(self,fieldGens,vinf=0.0): """For slip boundary conditions Vcp is to be find slightly above the control point that is done by using this function. """ self.vcps=dfn.velField(self.cps,fieldGens,vinf)
def findVcp(self,fieldGens=[],vinf=0.0): """find V at control points. This step is to be executed at starting of each step""" self.vcp=dfn.velField(self.cp,fieldGens,vinf)