Message Boards Message Boards

0
|
1427 Views
|
2 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Can ThermodynamicData be sped up?

Posted 8 months ago

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}
POSTED BY: Otto Linsuain
2 Replies
Posted 8 months ago

Thanks a lot for the response Jason, this may come in handy. As it often the case, I am not expecting to have a list of numbers ready to pass to the function, it is more like I need to call it from inside a solution loop, but this is very good to know anyway. I have always been puzzled by the functions that evaluate faster is you pass them a list than if you map them to the same list. Thanks, O. Linsuain.

POSTED BY: Otto Linsuain

ThermodynamicData relies on the Wolfram Knowledgebase, so it doesn't surprise me that the compiled and uncompiled functions have similar timing.

You can get a big efficiency boost by making one listed call to ThermodynamicData instead of mapping it over a list., see this example.

In[12]:= pressures = RandomReal[{13.0, 16.0}, 100];
AbsoluteTiming[res1 = Map[Viscosity[#, 400.0] &, pressures];]

Out[13]= {17.839, Null}

In[14]:= AbsoluteTiming[res2 = Viscosity[pressures, 400.0];]

Out[14]= {0.792283, Null}

In[15]:= res1 == res2

Out[15]= True

By experimenting it looks like you can pass in a list for pressure and temperature both.

POSTED BY: Jason Biggs
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract