Community Tip - If community subscription notifications are filling up your inbox you can set up a daily digest and get all your notifications in a single email. X
Greetings
I have a rather strange question, with topic that some people might consider insignificant and unimportant, but this particular function that Im making will probably be used numerous times in a single worksheet. For that reason Im trying to optimize almost every function in my file as much as possible. Ofcourse I dont expect a detailed answer with testing results etc, but rather your thoughts on this topic.
In this case, I need to determine in which region of IAPWS-IF97 (formulation for thermodynamic properties of water) the point lies on a pressure-temperature plot.P-T Diagram, Validity range of IF-97
So, here is the question itself: wich model of (p,T) function would be faster and easier to calculate (e.g. on a low-spec hardware), the one with separated IF`s, or the one where IF`s combined with usage of multiple "and (˄)" and "or (˅)"?
And will the result be different in Mathcad Prime?
Model 1
Model 2
Functions and constants used (for clarification, in case you are interested)
Reg5ValRangeErr = "Error: Point is Out of the Validity Range of Region 5 (611.213 Pa ≤ p ≤ 50 MPa, 1073.15 K < T ≤ 2273.15 K)"
Turn both calculations into functions of T, p and perform timing tests (use the time(0) function) by calling each function multiple times using a series of predefined values of T, p or random created values.
What I notice is that your three functions p.s(T), p.B23(T) and T.B23(100MPa) are called multiple times and it may be beneficial if you calculate the values only once and assign the result variables which then are used in the comparisons. Especially T.B23(100MPa) is a constant value independent from T and so could be precalculated in front of the function in the worksheet.
Are you sure about the "AND" here? Shouldn't it rather be an "OR" ??
I am not sure if it would speed up the evaluation but you could omit the lower limit in all your comparisons. Just use if T<b instead of if a<T<b.
But you will have to add an additional "if T<To then return errormessage" at the front which may eat up the gain of speed by not having to check the lower bounds, You'll have to experiment with this.
If you set up a worksheet in Mathcad to time both functions you could post it here and we could convert it and test it in Prime.
BTW, I guess you hardly will see the result "4" because of numerical inaccuracies due to tiny internal round-off errors.
So either omit that case completely and only distinct between area 1 or 2 or, If its important to check if point is near(!!) the line you may demand only approximate equality rather than exact equality. This means that instead of
you would use
It does not matter if you use <= or just < and you may adjust the 'level of equality' to your needs - maybe 10-3 Pa is too low.
Hi Werner,
Sorry for delayed reply, had stuff to do.
@Werner_E wrote:perform timing tests (use the time(0) function) by calling each function multiple times using a series of predefined values of T, p
Thank you for pointing out onto this time() function, didnt know about the existence of such.
I used it to test out my functions, file attached.
@Werner_E wrote:functions p.s(T), p.B23(T) and T.B23(100MPa) are called multiple times and it may be beneficial if you calculate the values only once and assign the result variables which then are used in the comparisons. Especially T.B23(100MPa) is a constant value independent from T and so could be precalculated in front of the function in the worksheet.
Indeed, assigning function's value as a variable instead of calling it multiple times is beneficial, I didnt use it on my screenshots just because I wanted to show the IF's structure only.
@Werner_E wrote:Are you sure about the "AND" here? Shouldn't it rather be an "OR" ??
Yes, my bad on this one, didnt notice it.
@Werner_E wrote:I am not sure if it would speed up the evaluation but you could omit the lower limit in all your comparisons. Just use if T<b instead of if a<T<b.
But you will have to add an additional "if T<To then return errormessage" at the front which may eat up the gain of speed by not having to check the lower bounds, You'll have to experiment with this.
Yes, agree with you, so I cloned the 1st model with the applied modification and also tested it out.
In the end, the first model (untouched) is labeled α, its modified version - β, and the original second model is γ.
@Werner_E wrote:BTW, I guess you hardly will see the result "4" because of numerical inaccuracies due to tiny internal round-off errors.
So either omit that case completely and only distinct between area 1 or 2 or, If its important to check if point is near(!!) the line you may demand only approximate equality rather than exact equality. This means that instead of
you would use
It does not matter if you use <= or just < and you may adjust the 'level of equality' to your needs - maybe 10-3 Pa is too low.
Indeed, on general occasions, it has better to be exactly as you described. But in my situation, user would specifically get the saturated pressure value from temperature (or backwards) and input it in the function, and not "point finger into the sky and guess it right".
Here are the results of your timing tests on my (slow) machine.
I also added a test using slightly less than 1 Million evenly distributed in the whole area visible in your plot. Time shown is the total calc time, not divided by the number of points checked.
I would say that even though the third approach is slightly faster (in this run, pressing Ctrl-F9 you might see another 'winner') they all are equally well.
I converted the sheet to Prime 11.0.0.0 and recalculated it. Of course multithreading was turned off as otherwise timing would not work.
Here are the disappointing results running on the very same machine:
Just for fun I added the points differently colored to your plot (its no surprise that none of them is in 'region' 4). So there is a visual check for plausibility of the region test function(s).
I attach my modified MC15 sheet and also for whatever it may be worth the converted P11 sheet. The latter is quite a mess and the plots fail. I made no attempts to clean up or fix.
Hi,
Have you looked at the CoolProp wrapper for Prime.
https://coolprop.org/ for documentation
It complies with IAPWS-IF97
It looks like you are reinventing the wheel. Web site to download two files
https://sourceforge.net/projects/coolprop/files/CoolProp/7.2.0/MathcadPrime/
CoolProp is compiled code so will run far faster than Primes interpreted programming.
The web site has two files, a Prime worksheet with examples, and a DLL file
Just put the DLL in C:\Program Files\PTC\Mathcad Prime 11.0.0.0\Custom Functions and hey presto you can use the functions.
After you put the DLL in the folder restart Prime to register it
Worksheet from the web has examples
They are fast
Cheers
Terry
Hi,
Looked into it a bit deeper and you need to specify IF97 like this
Cheers
Terry
Yes, Im aware of CoolProp libriary, but as you said - it is for Prime version, and for Legacy Mathcad you have to build .dll by your own (there is an instruction how to do it, but still it is too complex for me).
And coolprop have too much functionality, and because of that it has quite an amount of inputs also. All in all, it excesses my needs, and I almost finished IAPWS-IF97 (and same implies for IAPWS-95) integration in Mathcad, so there is no necessity in coolprop.
Perhaps a dumb question: why not try using the converter to convert your stuff to Prime and then using coolprop? Original Mathcad was great, but it is now a dead end.
For me, the UI of the old one is simplier and more convenient to use and orient. Also I alreaddy used to the key-combinations (shortcuts) that doesnt work the same in Prime (now it is about the muscle memory etc).
Plus, it might be my delusional perception, but from my experience I got impression that Prime works slower than OG (not in terms of calculations, but in terms of working with interface and its functionality)
And to be fair, I was planning to post it here (as separate topic) after I finish polishing everithing. You already can see how it performs on this diagrams (pictures from november 2025, but here are only boundaries)
And I've also ran the computer-program verification tests on values of different properties (the same inputs as in the IAPWS formulations, so anyone can easily compare and check).
I also plan to make a doc file with descriptions on functions and instructions how to use them.
Hi
To save you the trouble here is three Coolprop 6.4.1 files for Mathcad 15.
a DLL you can save to C:\Program Files (x86)\Mathcad\Mathcad 15\userefi
A Mathcad 15 worksheet with examples of the functions
A descriptive xml file
No building the DLL is necessary as it has been done for you. The enclosed file works out of the box if you put it in the "userefi" directory.
You can use it to check your work.
Great graphs in your last post
Cheers
Terry
