New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support of HelmholzMedia by OpenModelica #34
Comments
Library report, Library coverage, last 7 runs
|
We'll look into this ASAP, given the growing interest in sCO2 cycles your library can be indeed very useful. |
cannot happen, but what was the input?
do not use RSS, instead keep iterating as long as residual OR Newton step are larger than tolerance
I have found the error associated with the log(delta - g[i,9]). This is assocaiated with the code f_r
(delta - g[i,9])^g[i,4] gets calculated as exp(g[i,4] *log(delta - g[i,9])) howver it is always a square so the negative argument it not an issue it needs to be replaced with:
i.e. remove g[i,4] and g[i,5] and replace with 2's. Note in other parts, i.e derivatives) this is already done g[i,4] and g[i,5] are not used. The is also an error with the residualNonAnalytical terms which calculate a (-ve)^1.3333. I haven't found that yet but it can be fixed by setting all the NonAnalytical coefficients to zero. This is OK as residualNonAnalytical is only needed very close to the critical point. |
Excellent! @branch171, are you able to provide a PR to fix it? |
what is a PR? I can show you which line to change. |
The code to change is in function f_r I have commented out the old line and below is the replacement: algorithm
f_residual :=
sum(p[i,1]*tau^p[i,2]*delta^p[i,3] for i in 1:nPoly)
+ sum(b[i,1]*tau^b[i,2]*delta^b[i,3]*exp(-delta^b[i,4]) for i in 1:nBwr)
//+ sum(g[i,1]*tau^g[i,2]*delta^g[i,3]*exp(g[i,6]*(delta - g[i,9])^g[i,4] + g[i,7]*(tau - g[i,8])^g[i,5]) for i in 1:nGauss)
+ sum(g[i,1]*tau^g[i,2]*delta^g[i,3]*exp(g[i,6]*(delta - g[i,9])^2 + g[i,7]*(tau - g[i,8])^2) for i in 1:nGauss)
+ sum(a[i,1]*Disb[i]*delta*Psi[i] for i in 1:nNonAna); To fix the nNonAna (-ve)^1.3333 error in CarbonDioxide: set the residualNonAnalytical all to zero and comment out the previous values. residualNonAnalytical=fill(0.0, 0, 12))
//residualNonAnalytical=[
// -0.666422765408E+00, 0.000, 1.00, 2, 2, 0.875, 0.300, 0.70, 10.0, 275., 0.3, 3.5;
// 0.726086323499E+00, 0.000, 1.00, 2, 2, 0.925, 0.300, 0.70, 10.0, 275., 0.3, 3.5;
// 0.550686686128E-01, 0.000, 1.00, 2, 2, 0.875, 0.300, 0.70, 12.5, 275., 1.0, 3.]) |
Thanks for the input! I am not 100% sure what would be the best solution here. RefProp used the coefficients g4 and g5 instead of hardcoded 2 because they see the possibility for other vaues. Before changing this, we should probably check no fluid (in RefProp) ever uses this. Maybe OpenModelica can also do some additional transformations/evaluations with constants!? Also, the values for density and also delta could be limited to a certain range. For the non-analytical terms, this change would have to be moved into some if-else probably. These terms are only used for water and carbondioxide, where the critical region can be the operating region. |
Just as a point the code for derivatives does NOT use g4 and g5 and has hardcoded 2 for these already. So if we use g4 and g5 then the fix needs to propagate out to these as well. It would seem strange to change from hardcode 2 as by definition a Gaussian is exp(-x^2). Yes I agree the analytical term should have if-else. Its not really important until you are very close to the critical point. I will look at the Pull Request and help on this. |
I am on vacation until end of July, and will have a look at the code when I am back.
|
@thorade, I'm not sure what you mean here. I'm on holiday in August, but I should be online from Aug 15, we can continue the discussion then. |
According to the analysis by branch171: g[i,9]^g[i,4] // does NOT work
g[i,9]^2 // works Even if we have Of course this could also be fixed on library side, by using hardcoded Another thing that might be worth trying is to just split up the matrix, and use Integers where possible. Before doing this, I would like to check whether it even solves the issue, and then check with RefProp or CoolProp which exponents can be restricted to Integers instead of Reals. For checking in OM, just use constant Integer g4 = 2;
constant Integer g5 = 2;
algorithm
f_residual :=
sum(p[i,1]*tau^p[i,2]*delta^p[i,3] for i in 1:nPoly)
+ sum(b[i,1]*tau^b[i,2]*delta^b[i,3]*exp(-delta^b[i,4]) for i in 1:nBwr)
//+ sum(g[i,1]*tau^g[i,2]*delta^g[i,3]*exp(g[i,6]*(delta - g[i,9])^g[i,4] + g[i,7]*(tau - g[i,8])^g[i,5]) for i in 1:nGauss)
+ sum(g[i,1]*tau^g[i,2]*delta^g[i,3]*exp(g[i,6]*(delta - g[i,9])^g4 + g[i,7]*(tau - g[i,8])^g5) for i in 1:nGauss)
+ sum(a[i,1]*Disb[i]*delta*Psi[i] for i in 1:nNonAna); |
The issue here is how to deal with x^y. Where x^y the y can be real it maps to exp(yln(x)) for a c compiler. This is a problem when x is negative as ln(-ve) is undefined. However, x^2 can be interpreted as xx and this of cause works for any x even -ve. Within the helmholtz function the Gaussian can apply to -ve and +ve x as it a function of distance of delta and tau normally either side of the critical point. You could fix this with exp(yln(abs(x))). I would be surprised if this was ever anything other than y = 2. According to R Span's book Multiparameter Equations of State the Guassian terms are always exp(-a(delta- b)^2 - c(tau-d)^2) with 2 fixed. Its not a parameter. See page 151. If y was fractional it would give complex numbers and if odd it would give unsymmetrical values that changed sign this would be bad in the eos. Also if evaluated at the critical point exp(y*ln(0)) is undefined for any y. |
The definition of
As I understand, the definition of
I guess this relies on the fact that the IEEE double precision standard allows to identify Real numbers that happen to be integer (e.g. 2.0) and hence handle them appropriately. From this point of view, the transformation made by the OpenModelica backend is illegitimate, because it assumes As a workaround while OMC is fixed, maybe writing |
Using |
That sounds good.
Me too. Never mind. |
If I read the RefProp Fortran source code and the .fld files correctly, some fluids like R125 use exponents that are different from 2. The book by Span was published in 2000. |
is the RefProp source code on GIT. I'll take a look and see what is happening. |
RefProp source code is not on github or anywhere else online, but if you install RefProp, you should have received a copy of the code. I looked at lines 147-181 of the file |
is there a chance you could attach the file or just those lines. I don't have RefProp. |
Thank you @kabdelhak !! |
No problem! I also had a short look at 4. (Examples.MediaTestModels.CarbondioxideTestModel error message during initialization: functions.c:2865: Invalid root: (-0.996066)^(1.33333). Maybe this error message can be enhanced to give more information?) Unfortunately it would be hard to give a better error message here, although i totally agree it should. We would need a new way of reporting and writing these errors during simulation since they rely entirely on temporary variables. But i was right, it is related. The function in question is once again
Here we have the exponent First of all the minimum value for delta is only set to 0 in this model and not to 1, so the base
Here we can see: I hope this helps! If the model still fails after you have updated these values i can have a look on why the index shift does not work, but as far as i can see it is just an error message fault and we actually use the correct values for computation. |
I am double checking now if I typed the equation for non-analytcial terms correctly. And on these two notebooks: |
My analysis for issue 4. was a little off here, it wasn't a modeling error, but a simplification error during compliation!
|
Thanks again Karim! One more note for users interested in CO2: There are two equations of state available, the very accurate one, and a shorter equation of state without analytical terms. |
This ticket has grown quite long, and some very nice improvements have been done. |
@thorade, we've made good progress with the support of your library in OpenModelica. @perost did an excellent job with the new frontend, so we passed from two models only running with the old front end, to all except one of the 42 runnable models of the library getting successfully compiled into simulation code, see report. Unfortunately, about a quarter of them still fail during simulation because of numerical issues.
I haven't had the time so far to look into that, so I'm not sure whether this is because of some shortcomings of the OpenModelica backend/runtime. Could you have a quick look and report here?
The text was updated successfully, but these errors were encountered: