from DWSIM.Thermodynamics import *
import math
from System import Array
import clr
clr.AddReference('DWSIM.MathOps.DotNumerics')
from DotNumerics.ODE import *
#Initializing values in input streams (Only one input stream for batch reactor)
feed1=[0] #the array containing the properties of feed stream
P1=[0] #array for Pressure of feed stream
massflow1=[0] #array for total mass flow of feed stream
molfrac1=[0] #array for molar fraction of feed stream
molflow1=[0] #array for total molar flow of feed stream
enthalpy1=[0] #array for the total enthalpy of feed stream
T1=[0] #array for the temperature of feed stream
#Extracting input from streams
feed1[0] = ims1
P1 = feed1[0].GetProp("pressure", "Overall", None, "", "")
massflow1 = feed1[0].GetProp("totalFlow" ,"Overall", None, "", "mass")
molflow1 = feed1[0].GetProp("totalFlow" ,"Overall", None, "", "mole")
volflow1=feed1[0].GetProp("totalflow","Overall",None,"","volume")
enthalpy1 = feed1[0].GetProp("enthalpy" ,"Overall", None, "Mixture", "mass")
molfrac1 = feed1[0].GetProp("fraction", "Overall", None, "", "mole")
T1 = feed1[0].GetProp("temperature", "Overall", None, "", "")
massfrac1 = feed1[0].GetProp("fraction", "Overall", None, "", "mass")

#Defining constants
 
z= viscosity #viscosity of inlet mixture
R= revmin  #revolutions per minute
a= rout    #outside radius of stator
b= rin     #inside radius of stator
c= clearance #rotor clearance width
theta= anglethe #angle between milling faces
s0= dsp #density of suspended phase in colloid
s= dcp #desnity of continuous phase in colloid 
l= slantl #slant height
pi= math.pi

#Defining Volume Flow rate
OverProp = feed1[0].GetPhase('Overall').Properties #The array which stores phase property of input stream
Q=massflow1[0]/OverProp.density #Volumetric flow of feed stream


#Defining power consumption
P=[0]

P[0]= (2* pi**3 * z *(R/60)**2* (a**4 - b**4))/(c* math.cos(theta))

# Defining pressure drop
p=[0]

p[0]= (s/20)*((2*pi*R/60)**2)*( 4*c*math.sin(theta)*(a-b) + 3*(a**2- b**2)) - (6*z*Q*math.log(a/b)/(pi*c**3*math.cos(theta)))

P2=[0]
P2[0]= P1[0]-p[0]

#Average particle diameter

r= (a+b)/2 # mean diameter of mill

d2= math.sqrt(( (Q *z/(s0-s)) * (18/(r*(2*pi*R/60)**2)) * (1/(2*math.sin(theta)*pi*l*(a+b)))))

#Defining settling velocity

Vs= [0]
Vs[0]= (d2**2/18*z) * (s0-s)*r*((2*pi*R/60)**2)

#Defining settling length
L=[0]
L[0]= c/(2*math.sin(theta))

#Defining volume milling zone
Vm= pi*l*(a+b)*c

#Settling time
ts=[0]
ts[0]= Vm/Q

#Temperature Rise in colloid mill
deltaT=[0]
deltaT[0]= 414*0.44* P[0]/(Q*746)

T2=[0]
T2[0]=T1[0]+deltaT[0]


Flowsheet.WriteMessage("The power consumption of colloid mill is" + str(P) + "W")

Flowsheet.WriteMessage("The particle diameter is" + str(d2) + "m")

Flowsheet.WriteMessage("The settling length is" + str(L) + "m")

Flowsheet.WriteMessage("The volume milling zone is" + str(Vm) + "m3")

Flowsheet.WriteMessage("The settling time is" + str(ts)+ "s")

out=[0]
out[0]=oms1
out[0].Clear()
out[0].ClearAllProps()
out[0].SetProp("totalFlow" ,"Overall", None, "", "mass",massflow1)
out[0].SetProp("totalflow", "Overall",None, "","mole",molflow1)
out[0].SetProp("fraction","Overall",None,"","mole",molfrac1)
out[0].SetProp("pressure", "Overall", None, "", "",P2)  
out[0].SetProp("temperature", "Overall", None, "", "",T2) 
