понедельник, 22 октября 2012 г.

JavaScript support for Thermodroid and Thermomixer

Thermodroid macro language description


Now you can use JavaScript to write macros in Thermodroid and Thermomixer.
You can access to the fluid operations from the methods of the internal object tdI. You shouldn't always write all methods throw accessing to that object. The parser automatically write two with statements: 
with (tdI) { with (Math) { //automatic text. shouldn't write this
  //your code here
  //...
}} //automatic text. you don't see this. shouldn't write this

All the methods and operations with the fluids are the same like in the internal macro language.

Fluid operations

to initialize the new fluid

tdI.newfluid(#,fluid)
# - number of the fluid in the internal array [0..9]
fluid - the fluid name. for example - methane, ethane, nitrogen and so on.

to calculate the fluid at pressure P [Pa] and temperature T [K]
tdI.ptflash(#,P,T)
# - number of the fluid

to calculate the fluid at pressure P [Pa] and vapour fraction V
tdI.pvflash(#,P,V)
# - number of the fluid

to calculate the fluid at temperature T [K] and vapour fraction V
tdI.tvflash(#,T,V)
# - number of the fluid

to calculate the fluid at pressure P [Pa] and molar enthalpy H [kJ/kmole]
tdI.phflash(#P,H)
# - number of the fluid

to get fluid properies use functions
tdI.massenthalpy(#) - get the mass enthalpy value of calculated fluid [kJ/kg]
tdI.molarenthalpy(#) - get the molar enthalpy [kJ/kmole]
tdI.massentropy(#) - get the mass entropy [kJ/kg/K]
tdI.molarentropy(#) - get the molar entropy [kJ/kmole/K]
tdI.massdensity(#) - get the mass density [kg/m3]
tdI.molardensity(#) - get the molar density [kmole/m3]
tdI.massheatofvap(#) - get the heat of evaporation in mass basis [kJ/kg]
tdI.molarheatofvap(#) - get the heat of evaporation in molar basis [kJ/kmole]
tdI.molarweight(#) - get the molar weight [kg/kmole]
tdI.T(#) - get the temperature [K]
tdI.P(#) - get the pressure [Pa]
tdI.V(#) - get the vapour fraction
tdI.viscosity(#)- get the viscosity [cP]
tdI.thermalconduct(#)- get the thermal conductivity [W/m/K]
tdI.Cp(#)- get the thermal capacity [kJ/kg/K]


To receive results to Thermodroid/Thermomixer you should call method

tdI.Var(String aname, Number avalue)
aname - the name of the variable that holds the result;
avalue - the value of the result

To receive results from Thermodroid/Thermomixer you should call method

tdI.getVar(String aname)
aname - the name of the variable you want to access.


Example #1
Calculate the simple exchanger. First fluid - H2O, pressure 1 bar, inlet temperature 50 C, outlet temperature 10 C. Second fluid - methane, pressure 3 bar, inlet state - saturated liquid, outlet state - saturated valve, mass flow 0.2 kg/s. Need to calculate the mass flow for water.
JavaScript Internal Macro language
var p1=100000;
var p2=300000;
var t1=273.15+50;
var t2=273.15+10;
var gm=0.2;
newfluid(0,'h2o');
ptflash(0, p1,t1);
var hw1=massenthalpy(0);
ptflash(0, p1,t2);
var hw2=massenthalpy(0);
var qw=hw1-hw2;
newfluid(1,'methane');
pvflash(1,p2,0);
var hm1=massenthalpy(1);
pvflash(1, p2,1);
var hm2=massenthalpy(1);
var qm=hm2-hm1;
Var('qm',qm);
Var('gm',qm*gm/qw);

p1=100000
p2=300000
t1=273.15+50
t2=273.15+10
gm=0.2
newfluid(0,h2o)
fluid0.ptflash(p1,t1)
hw1=fluid0.massenthalpy()
fluid0.ptflash(p1,t2)
hw2=fluid0.massenthalpy()
qw=hw1-hw2
newfluid(1,methane)
fluid1.pvflash(p2,0)
hm1=fluid1.massenthalpy()
fluid1.pvflash(p2,1)
hm2=fluid1.massenthalpy()
qm=hm2-hm1
gw=qm*gm/qw
Example #2
Calculate the average isobaric thermal capacity for nitrogen at 1 MPa in the temperature interval of [200..300] K with step of 1 K.
JavaScript Internal Macro language
var p=1000000;
var t1=200;
var t2=300;
var tstep = 1;
var t = t1;
var cpav = 0;
newfluid(0,'nitrogen');
while (t <= t2) {
 ptflash(0, p, t);
 cpav += Cp(0);
 t += tstep;
}
cpav /= (t2 - t1)/tstep;

Var('cpav',cpav);

p=1000000
t1=200
t2=300
tstep=1 
t=t1
cpav=0 
newfluid(0,nitrogen)
%%while1
fluid0.ptflash(p, t)
cpav=cpav+fluid0.cp()
t=t+tstep
%if t <= t2 while1 next 
cpav=cpav/((t2 - t1)/tstep)