Skip to content

Complete Unit Test Suite for the Matlab Toolbox - Pt.2#1881

Merged
ischoegl merged 42 commits intoCantera:mainfrom
ssun30:matlab_tests
Jul 7, 2025
Merged

Complete Unit Test Suite for the Matlab Toolbox - Pt.2#1881
ischoegl merged 42 commits intoCantera:mainfrom
ssun30:matlab_tests

Conversation

@ssun30
Copy link
Copy Markdown
Contributor

@ssun30 ssun30 commented May 6, 2025

Changes proposed in this pull request

Added unit tests for the remaining classes and methods in the Matlab toolbox.

Fixed bugs that prevent these tests from passing.

If applicable, fill in the issue number this pull request is fixing

Updates to Cantera/enhancements#177

If applicable, provide an example illustrating new features this pull request is introducing

Added tests for rates of progress, rate constants, species rates, and thermodynamic values.

Checklist

  • The pull request includes a clear description of this code change
  • Commit messages have short titles and reference relevant issues
  • Build passes (scons build & scons test) and unit tests address code coverage
  • Style & formatting of contributed code follows contributing guidelines
  • The pull request is ready for review

@ssun30 ssun30 marked this pull request as ready for review May 6, 2025 21:17
@codecov
Copy link
Copy Markdown

codecov bot commented May 6, 2025

Codecov Report

Attention: Patch coverage is 70.73171% with 12 lines in your changes missing coverage. Please review.

Project coverage is 74.87%. Comparing base (284a989) to head (3e6ccd9).
Report is 43 commits behind head on main.

Files with missing lines Patch % Lines
interfaces/matlab_experimental/Base/Interface.m 27.27% 8 Missing ⚠️
...nterfaces/matlab_experimental/Reactor/ReactorNet.m 85.00% 3 Missing ⚠️
interfaces/matlab_experimental/Base/Water.m 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1881      +/-   ##
==========================================
+ Coverage   74.28%   74.87%   +0.58%     
==========================================
  Files         448      448              
  Lines       55744    55734      -10     
  Branches     9190     9192       +2     
==========================================
+ Hits        41411    41730     +319     
+ Misses      11232    10901     -331     
- Partials     3101     3103       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ssun30
Copy link
Copy Markdown
Contributor Author

ssun30 commented May 7, 2025

@ischoegl The second part of the unit test suite is ready for review. This will include tests for 0D classes. That leaves 1D classes to be the only part remaining, and these will come in a later PR as this one is also approaching 3000 lines.

All CIs will pass but there is one issue that throws warnings about already deleted objects:

*******************************************************************************
  CanteraError thrown by Cabinet::at:
  Object with index 5 has been deleted.
  *******************************************************************************
  
  
  Error in Solution/delete (line 98)
              ctFunc('soln_del', s.solnID);

This happens when an Interface class object is deleted in MATLAB, which calls the Solution class destructor, since Interface now inherits from Solution. Here's how the Interface constructor works:

First, it calls the soln_newInterface method in clib to create a new Interface object in SolutionCabinet.
ID = ctFunc('soln_newInterface', src, name, na, adj);

It then inherits methods and properties from the Solution class.
s@Solution(ID);

These steps should address this comment from #1665:

Both Interface and Solution are stored in the same SolutionCabinet, so the solnID should reference the correct object. You should not require interfaceID. I'm not sure how an object that should be correctly loaded in C++ ends up scrambled in clib? If there's an issue, it needs to be fixed in clib rather than having a workaround being created in MATLAB. Fwiw, Interface inherits from Solution in C++ and the same should be true in MATLAB (I believe there are technical reasons why it's implemented differently in Cython).

But for some reason, when the destructor is called, Cantera somehow thinks the object is already deleted, even though it isn't. Prior to invoking the destructor, all the methods of the Interface object could be called without problems. Any idea why this could happen?

Copy link
Copy Markdown
Member

@bryanwweber bryanwweber left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @ssun30! I have a few suggestions for improvements here.

Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @ssun30. I only have minor comments.

@ischoegl
Copy link
Copy Markdown
Member

@ssun30 … the changes in commit 258ce81 make me believe that #1898 may have been a MATLAB implementation issue after all. Could you verify?

@ssun30
Copy link
Copy Markdown
Contributor Author

ssun30 commented Jun 11, 2025

@ssun30 … the changes in commit 258ce81 make me believe that #1898 may have been a MATLAB implementation issue after all. Could you verify?

That's correct. The soln_del method was called twice by the destructors of both Interface and Solution.

@ssun30 ssun30 force-pushed the matlab_tests branch 2 times, most recently from 256a46a to 0c97c78 Compare June 18, 2025 15:54
@ssun30 ssun30 requested a review from bryanwweber June 18, 2025 18:19
Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ssun30 ... some minor comments.

As in my previous feedback, I'd prefer if the MATLAB toolbox could adhere to the same approach as the other APIs and introduce minimal extra code: ideally, it should act as a minimal wrapper for CLib that just re-establishes the underlying C++ class structure.

One thing I noticed recently is that the bulk of the exception handling in MATLAB is re-implemented rather than relying on exceptions thrown by the C++ core - this results in a lot of redundant code.

Copy link
Copy Markdown
Member

@speth speth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your work on this, @ssun30. I'm really glad to see the expanded testing of the Matlab toolbox, and I think the approach taken here of translating some of the Python tests is generally a good one.

One thing to consider, to some extent here, but especially looking forward to tests of the 1D solver, is that the Python test suite includes many tests which are focused on making sure that the underlying algorithms are working correctly, which isn't really necessary to repeat in the Matlab test suite. Here, it's sufficient to just ensure that all of the classes / methods behave in expected ways, but not necessarily to test whether the solvers produce expected results across different ranges of inputs (though it is important to test that inputs that result in errors propagate those errors back in the expected way).

@ssun30 ssun30 force-pushed the matlab_tests branch 2 times, most recently from 5fba737 to c9ec1c4 Compare June 26, 2025 06:10
Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for removing the unnecessary checks for the destructors. One thing I wanted to point out is that you may want to check whether constructors actually work (currently, they could fail silently, potentially wreaking havoc later on). Those checks should be added for all CLib constructor calls.

Edit: ok to be deferred.

@ischoegl ischoegl dismissed their stale review June 27, 2025 13:13

Exception are beyond the scope of this PR.

@ssun30 ssun30 requested review from ischoegl and speth June 30, 2025 20:15
Copy link
Copy Markdown
Member

@ischoegl ischoegl left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All good from my side (my request for changes is removed as it goes beyond the scope of this PR).

Copy link
Copy Markdown
Member

@speth speth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the updates, @ssun30. This looks good to me.

@ischoegl ischoegl merged commit 0e55a49 into Cantera:main Jul 7, 2025
47 of 49 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants