Advertisement
nicogodet

Untitled

Jun 29th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.84 KB | None | 0 0
  1. #Import Libraries
  2.  
  3. from yade import pack,plot
  4. import math
  5. import random as rand
  6. import numpy as np
  7. from scipy.stats import gaussian_kde
  8.  
  9. # Time step
  10. O.dt=1e-6
  11.  
  12. # Skeleton creation
  13. title='FILETAIEAIE'
  14. folder=os.path.dirname(sys.argv[0])+"Results/"+str(title)+'/'
  15. if os.path.exists(folder)==False:
  16.     os.mkdir(folder)
  17.     os.mkdir(folder+'VTK/')
  18.  
  19. ##############################
  20. ## PARAMETERS OF SIMULATION ##
  21. ##############################
  22. ###Configuration : inclined channel
  23. slope = math.radians(31)    #Slope of the channe, in radian
  24. length = 90 #Length of the channe, in m
  25. width = 2   #Width of the channel, in m
  26. height = 2  #Height of the channel, in m
  27. gravityVector = Vector3(9.81*sin(slope),0.0,-9.81*cos(slope))   #Gravity vector to consider a channel inclined with slope angle 'slope'
  28.  
  29. ### Materials
  30. # Channel
  31. chanFrictAng =  70.
  32.  
  33. #Fluid
  34. fluidDensity =  1200.   #Density of the fluid, in kg/m3, used for the computation of the dragforce
  35. m_viscosity =   2   #Test avec 0.022
  36.  
  37. # Debris
  38. diameterPart =  0.1 #Diameter of the particles, in m
  39. m_roughness =   0.01    #Fraction on the radius
  40. sphDensity =    2500
  41. sphYoung =  1e7
  42. sphPoisson =    0.2
  43. sphFrictAng =   35.
  44.  
  45. O.materials.append(
  46.         FrictMat(
  47.             density=sphDensity,
  48.             frictionAngle=math.radians(sphFrictAng),
  49.             poisson=sphPoisson,
  50.             young=sphYoung,
  51.             label='Frict'
  52.         ))
  53.  
  54. O.materials.append(
  55.                 FrictMat(
  56.                         density=2500,
  57.                         frictionAngle=math.radians(chanFrictAng),
  58.                         poisson=0.2,
  59.                         young=1e7,
  60.                         label='FrictBase'
  61.                 ))
  62.  
  63. O.materials.append(
  64.                 FrictMat(
  65.                         density=2500,
  66.                         frictionAngle=math.radians(0.),
  67.                         poisson=0.2,
  68.                         young=1e7,
  69.                         label='FrictWall'
  70.                 ))
  71.  
  72. O.materials.append(
  73.                 FrictMat(
  74.                         density=2500,
  75.                         frictionAngle=math.radians(20.),
  76.                         poisson=0.2,
  77.                         young=1e7,
  78.                         label='FrictRigid'
  79.                 ))
  80.  
  81. ########################
  82. ## FRAMEWORK CREATION ##
  83. ########################
  84.  
  85. # Channel creation
  86. boxColor = (104/255., 111/255., 140/255.)
  87. chanBack = O.bodies.append(aabbWalls([(0,0,0),(0,width,height)],thickness=0.0,material='FrictWall',oversizeFactor=1.0,color=boxColor))
  88. chanS1 = O.bodies.append(aabbWalls([(0,0,0),(length,0,height)],thickness=0.0,material='FrictWall',oversizeFactor=1.0,color=boxColor))
  89. chanB = O.bodies.append(aabbWalls([(0,0,0),(length,width,0)],thickness=0.0,material='FrictBase',oversizeFactor=1.0,color=boxColor))
  90. chanS2 = O.bodies.append(aabbWalls([(0,width,0),(length,width,height)],thickness=0.0,material='FrictWall',oversizeFactor=1.0,color=boxColor))
  91. chanBack = O.bodies.append(aabbWalls([(length,0,0),(length,width,height)],thickness=0.0,material='FrictWall',oversizeFactor=1.0,color=boxColor))
  92. # Deposit box
  93. DepBox = O.bodies.append(aabbWalls([(0.,0.,-0.1),(0.1*length,width,2*height)],thickness=0.0,material='FrictWall',oversizeFactor=1.0,color=boxColor))
  94.  
  95. # Rigid wall
  96. rigWall = O.bodies.append(
  97.         aabbWalls(
  98.         [(0.9*length,0,0),(0.9*length+0.1,width,height/2)],
  99.         thickness=0.,
  100.         material='FrictRigid',
  101.         oversizeFactor=1.0,
  102.         color=boxColor))
  103.  
  104.        
  105.        
  106. # Creation of sphere pack
  107. partCloud = pack.SpherePack()
  108. partNumber = 10000
  109. psdSizes = [0.08,0.16,0.32,0.64]#[0.00125,0.0025,0.005,0.01,0.02,0.04,0.08,0.16,0.32] #couper au centimetre
  110. psdCumm = [0,0.53,0.86,1.  ]#[  0.01,  0.05, 0.17, 0.26,0.34,0.43,0.53,0.86,1.  ]
  111. #partCloud.makeCloud(minCorner=(0.,0.,0.),maxCorner=(length/10.,width,2*height), psdSizes=psdSizes, psdCumm=psdCumm, distributeMass=True,seed=23)
  112. partCloud.makeCloud(minCorner=(length/10./3.*2.,0.,0.),maxCorner=(length/10.,width,height), rMean=diameterPart/2.0)
  113. spIds = partCloud.toSimulation(material='Frict',color=[205./255,175./255,149./255])
  114. allSp = spIds
  115. #####################
  116. ## SIMULATION LOOP ##
  117. #####################
  118.  
  119. # Definition of simulation's variable
  120. Factor = 1.5    #Factor ratio for the distance detection of distant interactions
  121. theta_method = 0.55 #1 : Euler Backward, 0.5 : trapezoidal rule, 0 : not used, 0.55 : suggested optimum
  122. resolution = 1      #Change normal component resolution method, 0: Iterative exact resolution (theta method, linear contact), 1: Newton-Rafson dimentionless resolution (theta method, linear contact), 2: Newton-Rafson with nonlinear surface deflection (Hertzian-like contact)
  123. NewtonRafsonTol = 1e-6  #Precision of the resolution of Newton-Rafson's method
  124. vel_front = Vector3(0.,0.,0.)
  125.  
  126. # Engine loop
  127. O.engines=[
  128.     ForceResetter(),
  129.     InsertionSortCollider([
  130.         Bo1_Sphere_Aabb(aabbEnlargeFactor=Factor, label="aabb"),
  131.         Bo1_Box_Aabb(),
  132.         Bo1_Wall_Aabb()],
  133.         verletDist=-0.1,
  134.         allowBiggerThanPeriod=False),
  135.  
  136.     InteractionLoop(
  137.     [
  138.         Ig2_Sphere_Sphere_ScGeom6D(interactionDetectionFactor=Factor,label="Ig2"),
  139.         Ig2_Box_Sphere_ScGeom6D()
  140.     ],
  141.         [
  142.         Ip2_FrictMat_FrictMat_LubricationPhys(eta=m_viscosity,eps=m_roughness,label="Phys_Lub"),
  143.     ],
  144.         [
  145.         Law2_ScGeom_ImplicitLubricationPhys(
  146.                         activateNormalLubrication=True,
  147.                         activateTangencialLubrication=True,
  148.                         activateTwistLubrication=True,
  149.                         activateRollLubrication=True,
  150.                         theta=theta_method,
  151.                         resolution=resolution,
  152.                         warnedOnce=True,
  153.                         maxSubSteps=20,
  154.                         NewtonRafsonTol=NewtonRafsonTol,
  155.                         debug=False)
  156.     ]),
  157.  
  158.     #DragEngine(Rho=1200,Cd=0.47,V=vel_front, label='Drag', dead=True),
  159.     NewtonIntegrator(gravity=gravityVector, damping = 0.4, label='newton'),
  160.  
  161.     # Launching Python functions inside the loop
  162.     PyRunner(command='gravityDeposit()',iterPeriod=500,label='gravDep',dead=False),
  163.     PyRunner(command='Fx, Fy, Fz=Force_wall()',iterPeriod=1000,label='FWall',dead=True),
  164.     PyRunner(command='deadZone()',iterPeriod=1000,label='deadZone_engine',dead=True),
  165.     PyRunner(command='max_pos_x,height_front, vel_front=front()',iterPeriod=1000,label='dista',dead=True),
  166.     PyRunner(command='vel_profil()',iterPeriod=1000,label='velProfil',dead=True),
  167.     PyRunner(command='shearLub_tot,shearCon_tot=bedshear_total()',iterPeriod=1000,label='bedshear_tot_engine',dead=True),
  168.     PyRunner(command='shearLub1,shearCon1=bedshear_at_x(0.15*length)',iterPeriod=1000,label='bedshear_1_engine',dead=True),
  169.     PyRunner(command='shearLub2,shearCon2=bedshear_at_x(30)',iterPeriod=1000,label='bedshear_2_engine',dead=True),
  170.     PyRunner(command='shearLub3,shearCon3=bedshear_at_x(50)',iterPeriod=1000,label='bedshear_3_engine',dead=True),
  171.     PyRunner(command='shearLub4,shearCon4=bedshear_at_x(75)',iterPeriod=1000,label='bedshear_4_engine',dead=True),
  172.     VTKRecorder(fileName=folder+'/VTK/vtk-',virtPeriod=0.1),
  173. #   PyRunner(command='Epot,Ekin,Etot=Energy()',iterPeriod=500),
  174. #   PyRunner(command='addPlotData()',iterPeriod=1000), #ne pas oublier de commenter
  175.     ]
  176.  
  177. # Caracteristics of the drag engine
  178. #Drag.ids=spIds
  179. #Drag.Rho=fluidDensity
  180. #Drag.Cd=0.47
  181.  
  182. #########################
  183. ## FUNCTION DEFINITION ##
  184. #########################
  185.  
  186. def gravityDeposit():
  187.     if O.iter>5000 and kineticEnergy()<10 and unbalancedForce()<1:
  188.         #O.pause()
  189.         for i in DepBox:
  190.             O.bodies.erase(i)
  191.         gravDep.dead=True
  192.         #Drag.dead=False
  193.         dista.dead=False
  194.         velProfil.dead=False
  195.         bedshear_tot_engine.dead=False
  196.         bedshear_1_engine.dead=False
  197.         bedshear_2_engine.dead=False
  198.         bedshear_3_engine.dead=False
  199.         bedshear_4_engine.dead=False
  200.         newton.damping=0.
  201.         FWall.dead=False
  202.         Phys_Lub.eta=m_viscosity
  203. #       O.save("deposit-mono5cm.yade")
  204.         print "Gravity deposit finished & gate opened"
  205.  
  206. def addPack(minX,minY,minZ,maxX,maxY,maxZ,num):
  207.     global spIds
  208.     PackAdd = pack.SpherePack()
  209.     PackAdd.makeCloud(minCorner=(minX,minY,minZ),maxCorner=(maxX,maxY,maxZ),rMean=diameterPart/2.,num=num)
  210.     spIdsAdd=PackAdd.toSimulation(material=mat,color=[205./255,175./255,149./255])
  211.     spIds=spIds+spIdsAdd
  212.     #Drag.ids=spIds
  213.  
  214. xs=np.linspace(0,length,int(length)*10)
  215. zs=np.linspace(0,height,int(height)*100)
  216. max_pos_x, height_front, vel_front, max_vel_front=0.,0.,Vector3(0.,0.,0.),0.
  217. def front():
  218.     global max_vel_front
  219.     fvel=open(folder+"front.txt","a")
  220.     fvel3D=open(folder+"front3D.txt","a")
  221. #   for i in spIds:
  222. #       O.bodies[i].shape.color=[205./255,175./255,149./255]
  223.     list_x,list_z=[],[]
  224.     for i in spIds:
  225.         list_x.append(O.bodies[i].state.pos[0])
  226.         list_z.append(O.bodies[i].state.pos[2])
  227.     density_x = gaussian_kde(list_x)
  228.     density_x.covariance_factor = lambda : .05
  229.     density_x._compute_covariance()
  230.     density_z = gaussian_kde(list_z)
  231.     density_z.covariance_factor = lambda : .05
  232.     density_z._compute_covariance()
  233.    
  234.     max_density_x=max(density_x(xs))
  235.     max_pos_x=xs[max([i for i,x in enumerate(density_x(xs)) if x>0.5*max_density_x])]
  236.    
  237.     height=max([x for x in zs if 0.95>density_z.integrate_box_1d(0,x)])+diameterPart/2.
  238.  
  239.     vel_front,a=Vector3(0.,0.,0.),0
  240.     for i in spIds:
  241.         if abs(O.bodies[i].state.pos[0] - max_pos_x) <=0.5:
  242. #           O.bodies[i].shape.color=[0./255,0./255,255./255]
  243.             vel_front+=O.bodies[i].state.vel
  244.             a+=1
  245.     if a!=0:
  246.         vel_front=vel_front/float(a)
  247.         #Drag.V=vel_front
  248.  
  249.     max_vel_front=max(max_vel_front,vel_front[0])
  250.    
  251.     fvel.write(str(O.time)+";"+str(max_pos_x)+";"+str(height)+";"+str(vel_front[0])+"\n")
  252.     fvel3D.write(str(O.time)+";"+str(vel_front[0])+";"+str(vel_front[1])+";"+str(vel_front[2])+"\n")
  253.     fvel.close()
  254.     fvel3D.close()
  255.    
  256.     return max_pos_x,height, vel_front
  257.  
  258. def distance(a,b):
  259.     return sqrt((a.state.pos[0]-b.state.pos[0])**2+(a.state.pos[1]-b.state.pos[1])**2+(a.state.pos[2]-b.state.pos[2])**2)
  260.  
  261. deadZoneIds=[]
  262. def deadZone():
  263.     global spIds
  264.     for i in spIds:
  265.         if (O.bodies[i].state.vel[0]/float(max_vel_front)) < 0.05:
  266.             spIds.remove(i)
  267.             deadZoneIds.append(i)
  268.     if spIds==[]:
  269.         dista.dead=True
  270.         velProfil.dead=True
  271.     #Drag.ids=spIds
  272.  
  273. def vel_profil():
  274.     velocity_profil=open(folder+"velocity_profil.txt","a") #ecriture dans un fichier velocity_profil.txt
  275.     velocity_profil.write(str(O.time)+";"+str(max_pos_x)+";")
  276.     l=[]
  277.     for i in spIds:
  278.         if max_pos_x-1.2 <= O.bodies[i].state.pos[0] <= max_pos_x-0.8:
  279.             l.append(i)
  280.    
  281.     list_vel_x,list_vel_y,list_vel_z="0","0","0"
  282.     for h in range(int(height_front/diameterPart)):
  283.         vel_x,vel_y,vel_z,a=0.,0.,0.,0
  284.         for i in l:
  285.             if h*diameterPart <= O.bodies[i].state.pos[2] <= (h+1)*diameterPart:
  286.                 vel_x+=O.bodies[i].state.vel[0]
  287.                 vel_y+=O.bodies[i].state.vel[1]
  288.                 vel_z+=O.bodies[i].state.vel[2]
  289.                 a+=1
  290.         if a!=0:
  291.             list_vel_x+=":"+str(vel_x/a)
  292.             list_vel_y+=":"+str(vel_y/a)
  293.             list_vel_z+=":"+str(vel_z/a)   
  294.     velocity_profil.write(str(height_front)+";"+str(list_vel_x)+";"+str(list_vel_y)+";"+str(list_vel_z)+"\n")
  295.     velocity_profil.close()
  296.  
  297. FtotX,FtotY,FtotZ,tF=[],[],[],[]
  298. def Force_wall():
  299.     f=open(folder+"Force_wall.txt","a")
  300.     ### Calculate each component of the total NORMAL force between impacting particles and the wall:
  301.     ### Diviser le mur en multiple tranche
  302.  
  303.     Fx=0
  304.     Fy=0
  305.     Fz=0
  306.     for i in O.interactions:
  307.         if i.id1 == rigWall[0]:
  308.             deadZone_engine.dead=False
  309.             Fx+= i.phys.normalForce[0]
  310.             Fy+= i.phys.normalForce[1]
  311.             Fz+= i.phys.normalForce[2]
  312.     FtotX.append(Fx)
  313.     FtotY.append(Fy)
  314.     FtotZ.append(Fz)
  315.     tF.append(O.time)
  316.     f.write(str(O.time)+";"+str(Fx)+";"+str(Fy)+";"+str(Fz)+"\n")
  317.     f.close()
  318.     return Fx, Fy, Fz
  319.  
  320. shearLub_tot,shearCon_tot=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  321. def bedshear_total():
  322.     shearLub,shearCon=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  323.     fbedshear=open(folder+"bedshear.txt","a")
  324.     for i in O.interactions:
  325.         if i.id1 in chanB:
  326.             shearLub+=i.phys.shearLubricationForce
  327.             shearCon+=i.phys.shearContactForce
  328.     fbedshear.write(str(O.time)+";"+str(shearLub[0])+";"+str(shearLub[1])+";"+str(shearLub[2])+";"+str(shearCon[0])+";"+str(shearCon[1])+";"+str(shearCon[2])+"\n")
  329.     fbedshear.close()
  330.     return shearLub,shearCon
  331.  
  332. shearLub1,shearCon1=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  333. shearLub2,shearCon2=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  334. shearLub3,shearCon3=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  335. shearLub4,shearCon4=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  336. shearLub5,shearCon5=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  337. def bedshear_at_x(x):
  338.     fbedshear=open(folder+"bedshear_at_"+str(x)+".txt","a")
  339.     shearLub,shearCon=Vector3(0.,0.,0.),Vector3(0.,0.,0.)
  340.     for i in O.interactions:
  341.         if (i.id1 in chanB) and (x-0.5 <= O.bodies[i.id2].state.pos[0] <= x+0.5):
  342.             shearLub+=i.phys.shearLubricationForce
  343.             shearCon+=i.phys.shearContactForce
  344.     fbedshear.write(str(O.time)+";"+str(shearLub[0])+";"+str(shearLub[1])+";"+str(shearLub[2])+";"+str(shearCon[0])+";"+str(shearCon[1])+";"+str(shearCon[2])+"\n")
  345.     fbedshear.close()
  346.     return shearLub,shearCon
  347.  
  348. Epot,Ekin,Etot=0.,0.,0.
  349. def Energy():
  350.     Epot,Ekin,Etot=0.,0.,0.
  351.     for i in spIds:
  352.         p=Vector3(length-O.bodies[i].state.pos[0],O.bodies[i].state.pos[1],O.bodies[i].state.pos[2])
  353.         Epot+=O.bodies[i].state.mass*Vector3.dot(gravityVector,p)
  354.         Ekin+=0.5*O.bodies[i].state.mass*O.bodies[i].state.vel.norm()**2
  355.     return Epot,Ekin,Epot+Ekin
  356.    
  357. def DragForce(rho, Cd):
  358.     for i in spIds:
  359.         relVel = -(Vector3(vel_front,0.,0.)-O.bodies[i].state.vel)
  360.         drag = -0.5 * relVel.squaredNorm() * relVel.normalized() * rho * Cd
  361.         O.forces.addF(i,drag)
  362.            
  363.    
  364.  
  365. def addPlotData():
  366. #       plot.addData(i=O.iter, unbalanced=unbalancedForce(),t=O.time,front_vel=front_vel,tt=O.time,shearLub_tot=shearLub_tot[0],shearCon_tot=shearCon_tot[0],ttt=O.time,shearLub1=shearLub1[0],shearCon1=shearCon1[0],tttt=O.time,Epot=Epot,Ekin=Ekin,Etot=Etot, **O.energy)    
  367.     plot.addData(i=O.iter, unbalanced=unbalancedForce(),t=O.time,vel_front=vel_front[0],tt=O.time,shearLub_tot=shearLub_tot[0],shearCon_tot=shearCon_tot[0],ttt=O.time,shearLub1=shearLub1[0],shearCon1=shearCon1[0],tttt=O.time,Epot=Epot,Ekin=Ekin,Etot=Etot, **O.energy)
  368.    
  369.  
  370.  
  371. O.trackEnergy=True
  372.  
  373. #plot.plots={'i':('unbalanced'),'tttt':('Epot',None,('Etot','g')),'tt':'vel_front'}
  374. #plot.labels={'vel_front':'vel_front '+title}
  375. #plot.plots={'t':'Epot','tt':'Ekin','ttt':'Etot'}
  376. #plot.plot(subPlots=False)
  377.  
  378. f=open(folder+"param.txt","w")
  379. f.write("###############################"+
  380.     "\n## Parameters of the channel ##"+
  381.     "\n###############################"+
  382.     "\nlength = "+str(length)+" m"+
  383.     "\nwidth = "+str(width)+" m"+
  384.     "\nheight = "+str(height)+" m"+
  385.     "\nslope = "+str(math.degrees(slope))+" degrees"+
  386.         "\nchanFricAng = "+str(chanFrictAng)+" degrees"+
  387.     "\n\n###############################"+
  388.     "\n## Parameters of Lubrication ##"+
  389.     "\n###############################"+
  390.     "\nviscosity = "+str(m_viscosity)+" Pa.s"
  391.     "\nroughness = "+str(m_roughness)+" % of diameterPart"
  392.     "\n\n################################"+
  393.     "\n## Parameters of the material ##"+
  394.     "\n################################"+
  395.     "\n\nType of material = Frict"+
  396.     "\ndensity = "+str(sphDensity)+" kg/m^3"+
  397.     "\ndiameter = "+str(diameterPart)+" m"+
  398.     "\nyoung = "+str(sphYoung)+" Pa"+
  399.     "\npoisson = "+str(sphPoisson)+
  400.     "\nsphFricAng = "+str(sphFrictAng)+" degrees")
  401. f.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement