I tried defining a function that will return the viscosity of water as a function of pressure and temperature. I also tried creating a compiled version. Both seem too slow. Can anybody suggest a way of speeding these up? See code below.
Thanks,
O. Linsuain
In[8]:= (* **** Standard version **** *)
Viscosity[press_, temp_] :=
QuantityMagnitude[
ThermodynamicData["Water",
"Viscosity", {"Pressure" -> Quantity[press, "Megapascals"],
"Temperature" -> Quantity[temp, "Kelvins"]}]]
(* **** Compiled vesion **** *)
ViscosityCompiled =
Compile[{press, temp},
QuantityMagnitude[
ThermodynamicData["Water",
"Viscosity", {"Pressure" -> Quantity[press, "Megapascals"],
"Temperature" -> Quantity[temp, "Kelvins"]}]]]
Out[9]= CompiledFunction[{11, 12., 5468}, {
Blank[Real],
Blank[Real]}, {{3, 0, 0}, {3, 0, 1}, {3, 0, 2}}, {}, {0, 0, 3, 0, 0}, \
{{46,
Function[{press, temp},
QuantityMagnitude[
ThermodynamicData[
"Water", "Viscosity", {
"Pressure" -> Quantity[press, "Megapascals"],
"Temperature" -> Quantity[temp, "Kelvins"]}]]], 3, 0, 0, 3, 0,
1, 3, 0, 2}, {1}},
Function[{press, temp},
QuantityMagnitude[
ThermodynamicData[
"Water", "Viscosity", {
"Pressure" -> Quantity[press, "Megapascals"],
"Temperature" -> Quantity[temp, "Kelvins"]}]]], Evaluate]
This works fine:
In[10]:= Viscosity[15.0, 400.0]
ViscosityCompiled[15.0, 400.0]
Out[10]= 0.000222455
Out[11]= 0.000222455
But this is too slow with any of the two versions (notice it is only 100 numbers). Interestingly, the first time took about 58 seconds each.
In[12]:= AbsoluteTiming[
Map[Viscosity[#, 400.0] &, RandomReal[{13.0, 16.0}, 100]];]
AbsoluteTiming[
Map[ViscosityCompiled[#, 400.0] &, RandomReal[{13.0, 16.0}, 100]];]
Out[12]= {29.9476, Null}
Out[13]= {29.7495, Null}