-
-
Notifications
You must be signed in to change notification settings - Fork 346
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
Some Matlab functions to set the state of PureFluids are broken #299
Comments
|
Thanks Ray! The code in |
Looking at
which is consistent with how all of the other setState methods work as well. |
Is there a reason why a vector is used there instead of two separate arguments? The To me, two arguments makes much more sense and I'd prefer to switch the Matlab interface to use two arguments. It would be a breaking change, is the only thing... |
Can you configure to accept both and print out a warning that it will soon be deprecated? Steven
|
Actually, I think I figured out that it does have to be a vector, due to the way the |
I think it should remain the way it is. Changing it would make it inconsistent with the other Matlab functions like Also, I think the prefered method of setting the state in these cases anyway is set(w, 'P', pressure, 'Vapor', vapor_fracton);
set(w, T', temperature, 'Vapor', vapor_fracton); The |
For what it's worth, I did a bit of digging and this bug seems to date back at least to 27c66a8, so this bug's been around for 11 years. |
Fix the setState_satLiquid and setState_satVapor functions so that they call the main set method instead of the underlying C++ layer. Resolves Cantera#299.
And all it took was a little undergraduate project to find it! With a bit more looking, I think I found the particular commit where this happened: d298e3b#diff-d99fd7840078d17ee086780c97c92389L32 You can see that the |
The history of this bug pretty clearly highlights the main problem with the "magic number" approach to dispatching functions in the Matlab interface. Assuming that at some point we get around to rewriting the Matlab interface to use the "new" style classes, it would be good to see if there's a way to avoid using this approach. |
@speth It seems like the reason this approach was chosen was because it is not possible to have multiple functions defined in the same mex C/C++ file that can be called from the Matlab command line. For example, Another approach might be to write a mex file wrapper around the various classes that Cantera uses. There is some discussion of this alternative here, here, and here. |
Fix the setState_satLiquid and setState_satVapor functions so that they call the main set method instead of the underlying C++ layer. Resolves #299.
Right, I understand why the choice was made to implement it as it is, given the limitations of the mex interface. But the fact that there's no useful way to define meaningful names to integer constants in Matlab gives rise to this unmaintainable parallelism between the constants in the The idea of just loading the library directly, which would eliminate the single-entry-point limitation imposed by the mex interface, has crossed my mind before and I did a bit of experimentation. One issue which immediately arises is that you can only use C (not C++) functions, so the purported C interface defined in |
@speth Instead of using magic integers, the examples I linked to use string comparisons in the switch statements, so that you can be mildly more descriptive in the selection of the job to complete. I think that's likely to be the best case for the back end in a mex-type interface, although the front end could certainly be improved by using more modern Matlab OOP techniques. I'm leery of trying to rewrite the C interface and also worried about the (small?) burden it might put on the end user to have to load the library directly. The code generator sounds like a good idea too. FWIW, I happen to agree that there are better alternatives to Matlab (Python being the most relevant case for us). However, universities still teach Matlab in their intro CSE course for non-majors, so I think its worth improving the Matlab interface to try and ease the use of Cantera, especially in the classroom. Of course, without any funding for the improvement, its difficult to devote more than an hour here or there... |
The problem is that the string comparison approach is O(n) in the number of possible function calls while the gigantic switch statement is always just O(1). I think the performance hit of 100 string comparisons just to get e.g. the temperature of a mixture would start to add up. I figure that you could add the check to make sure that the library was loaded in the constructor call for each class so the user didn't have to do it manually. One other advantage of using a library that doesn't touch the mex interface is that you don't need the Matlab headers in order to compile the interface, so you can build it on a computer without Matlab installed. I know many universities still teach Matlab to their engineering majors, and I happen to think it's a mistake. Obviously we should strive to have the Matlab toolbox be bug-free, but I think it's perfectly reasonable to have it be less full-featured than the Python interface as a way to encourage users to use Python instead. |
Matlab R2014a, Cantera 2.2.0, Windows 7 (but probably not OS-specific). The three title functions either throw errors or don't work as expected.
The text was updated successfully, but these errors were encountered: