-
-
Notifications
You must be signed in to change notification settings - Fork 374
Accept floating point representations of valid integer geometry flags in ck2yaml #1396
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
Conversation
Thanks for this @cory-kinney! To be honest, I feel quite ambivalent about this change... on the one hand, this change seems sensible and some simple testing on my end shows that it should be pretty robust. It would certainly improve the user experience. On the other hand, the CHEMKIN spec (as much as can be found publicly) states that this value is an index, which I interpret to mean an integer. I think that's the most reasonable interpretation. In Python, indices to access elements in sequences need to be ints, for example. As CHEMKIN is closed source, we can only go by the published materials that are available when implementing this. Specifically, I feel quite reluctant to base Cantera's behavior on CHEMKIN's current (inconsistent in my opinion) behavior, as they can change behavior without warning or recourse from our side. In this specific case, it seems like they'd be unlikely to cause such a backwards-compatibility-breaking change for a fairly minor inconsistency, but the principle remains... In the end, I think this change is probably worth it. I'll just ask you to fix up the failing test which explicitly checks for this error, and change it to a test that ensures this behavior is robust. The case I'd be specifically worried about is automatic writers that format something that should be 2.0 as just slightly less than 2.0 (likewise for 1.0)... Python's default behavior with
That is, if some software writes out a non-linear molecule with a geometry of 1.999999999999999999, it will be turned into a linear molecule with this change. I'd rather error in that case, than produce incorrect results in Cantera. On the less productive side, I also feel like this is another chance for Cantera to point out where CHEMKIN is either incorrect or (in this case) inconsistent with its own documentation. So I'm also inclined to keep this as an error, if only to point out to users that CHEMKIN should not be the de-facto baseline. But, as I said, that's not super productive from a user standpoint 😄 |
I believe the best way forward would be to issue warnings instead of errors, and convert for the user for cases like the one that is reported. Silently accepting incorrect syntax is not ideal. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cory-kinney ... thanks for the PR! As mentioned, I am not opposed to being more permissive when it comes to CHEMKIN input, but would request some changes to your approach.
- Add warning message when float is automatically converted to integer - Change test case for float transport data to test for floating point rounding errors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @cory-kinney! A few more changes based on the updates
H2 0.999999999999999999 38.000 2.920 0.000 0.790 280.000 | ||
H2O 1.999999999999999999 572.400 2.605 1.844 0.000 4.000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see you adjusted these so that we're testing the new behavior, but I think the tests are still going to fail because you need to adjust the code over here:
cantera/test/python/test_convert.py
Line 340 in 332bb9b
def test_transport_float_geometry(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bryanwweber what changes would need to be made? Wouldn't the existing test case fail since it would raise an InputError
with the phrase "geometry flag" in it since --permissive
won't apply?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also need a test where --permissive is passed and the conversion is successful. There should also be a case testing passing --permissive and the check fails because of the rounding issue.
The existing test is only checking one of the code paths, as you note the one without --permissive
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@cory-kinney ... I think the only remaining task preventing this from being approved/merged is the test @bryanwweber mentioned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ischoegl That's correct, I've just been a bit busy so I haven't gotten around to it! I'll try to do that sometime this next week.
Add missing check in ck2yaml for non-integer equivalent floats
Codecov Report
@@ Coverage Diff @@
## main #1396 +/- ##
==========================================
- Coverage 71.03% 70.72% -0.31%
==========================================
Files 363 378 +15
Lines 51855 55141 +3286
Branches 17362 18164 +802
==========================================
+ Hits 36834 38998 +2164
- Misses 12676 13633 +957
- Partials 2345 2510 +165
📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more |
It would seem that the additional tests implemented originally failed because, for the original value chosen to test handling of floating point arithmetic error, Let me know if there are any other changes that should be made. Should I go ahead and squash my commits and write a proper commit message now? |
I believe it's ready for review now @ischoegl ! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @cory-kinney, I have a few more suggested updates here to make sure that the tests provide appropriate coverage.
By the way, the failing tests here seem to be failing on the main
branch too, so you don't need to worry about that.
interfaces/cython/cantera/ck2yaml.py
Outdated
try: | ||
geometry = float(geometry) | ||
except ValueError: | ||
raise InputError( | ||
"Bad geometry flag '{}' for species '{}'. " | ||
"Flag should be an integer.", geometry, label) from None | ||
if geometry == int(geometry): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like there isn't a test of this case, which could be triggered by putting something that's not a number into the transport field. I'm basing this on the coverage comment here, although I do see what looks like it should be the right test below, test_transport_bad_geometry
.
I'd suggest updating the error messages to be slightly different so that we can actually check for them appropriately and make sure that we have tests that cover all the cases:
- Invalid input (character instead of number)
- Float which rounds to 0, 1, or 2 and is only an error if
permissive=False
, and succeeds otherwise - Integer 0, 1, or 2 (this should already be well covered)
- Integer or float >=3 which fails
test/python/test_convert.py
Outdated
self.convert('h2o2.inp', | ||
transport='h2o2-float-geometry-tran.dat', | ||
output='h2o2_transport_float_geometry', permissive=True) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test should also check that the converted mechanism has the appropriate type for the geometry. Otherwise we're not sure what the value is actually is.
Requested changes have been addressed.
I believe that my own initial review comments have been addressed a while ago. I know that @bryanwweber has things under control here, so I am happy with where this is going. |
@bryanwweber I finally got around to implementing the changes you requested (I haven't had to convert a mechanism with this issue in a while, so it was on the back burner), let me know if the latest commit properly addresses them. |
I'm having some trouble verifying the tests work properly now because of a build error that has started occurring recently for unknown reasons.
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One teeny tiny change 😄 Great work @corykinney I'd approve but it looks like you're having some trouble compiling, plus there's the failing .NET job which I'm not sure about.
3866508
to
5006801
Compare
@bryanwweber I was able to build it just fine on my laptop with Python 3.10, so I can now confirm that the test cases pass. I just had to update the check for the error you fixed, as it was looking for |
@corykinney ... the error you posted looks like it's related to #1382, but apparently went away after the fix you indicated. Given that CI passes (coverage failed for a different reason), this should be ready to merge. I'll leave the approval to @bryanwweber ... 😁 |
@corykinney Sorry, it looks like you committed the |
If `permissive` is given, `float` geometry flags are cast to `int` and are accepted if the values are equal, otherwise, `InputError` is raised. If `permissive` is not given, existing behavior is unchanged. Add tests to check for correct behavior with and without permissive, for float rounding error, and for character inputs. Closes Cantera#1395
Ignore `.idea/` folder associated with PyCharm IDE data
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the work here. In it goes!
@bryanwweber Good catch! For whatever reason I've avoided modifying the |
If you want to ignore files just for yourself, you can add it to |
That's a super helpful tip! And thank you both @bryanwweber @ischoegl for your patience and help in figuring out my first pull request! 🎉 |
Changes proposed in this pull request
This pull request automatically converts floating point representations of valid integer geometry flags to integers by casting to a
float
then to anint
and checking equality between the two results.Closes #1395
Checklist
scons build
&scons test
) and unit tests address code coverage