Skip to content

Make Func1 API's more consistent#1758

Merged
speth merged 9 commits intoCantera:mainfrom
ischoegl:tweak-func1
Aug 9, 2024
Merged

Make Func1 API's more consistent#1758
speth merged 9 commits intoCantera:mainfrom
ischoegl:tweak-func1

Conversation

@ischoegl
Copy link
Copy Markdown
Member

@ischoegl ischoegl commented Aug 4, 2024

Changes proposed in this pull request

This PR ensures that Python and MATLAB API's are consistent as far as possible (follow-up to #1741).

  • Implement alternative Python constructor that corresponds to MATLAB API
  • Deprecate Func1.cxx_functor in Python
  • Use specialized functions for Func1 operators that implement rules for simplifications (rules had already been part of the code base, but were not used as of Cantera 3.0)
  • Update operators in MATLAB
  • Implement operators in Python
  • Fix some typos and exceptions for Func1::isIdentical
  • Add google tests for operator functions to improve coverage of previously untested code.

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

In MATLAB:

>> out = Func1('sin') / 2  % Func1 operators work as of PR 1741
 
out = 
 
   0.5\sin(x)
 
>> out.type()  % simplification introduced in this PR (used to be 'ratio')

ans =

    'times-constant'

In Python:

In [1]: import cantera as ct

In [2]: a = ct.Func1(lambda x: x**2)

In [3]: b = a * 0  # Func1 operator introduced in this PR

In [4]: b.type
Out[4]: 'constant'

In [5]: b(1.5)
Out[5]: 0.0

In [6]: c = 2 * a

In [7]: c.type
Out[7]: 'times-constant'

In [8]: c(1.5), 2 * 1.5**2
Out[8]: (4.5, 4.5)

In [9]: f5 = ct.Func1("pow", 3.)  # MATLAB-like constructor (used to be 'ct.Func1.cxx_functor')
   ...: f5.type
Out[9]: 'pow'

In [10]: f5(2.0)
Out[10]: 8.0

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

@codecov
Copy link
Copy Markdown

codecov bot commented Aug 4, 2024

Codecov Report

Attention: Patch coverage is 73.64341% with 34 lines in your changes missing coverage. Please review.

Project coverage is 73.20%. Comparing base (3196d5e) to head (7e0ebe1).
Report is 3 commits behind head on main.

Files Patch % Lines
src/numerics/Func1.cpp 45.83% 9 Missing and 17 partials ⚠️
src/clib/ctfunc.cpp 60.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1758      +/-   ##
==========================================
+ Coverage   73.12%   73.20%   +0.07%     
==========================================
  Files         381      381              
  Lines       54174    54289     +115     
  Branches     9223     9242      +19     
==========================================
+ Hits        39615    39740     +125     
+ Misses      11598    11577      -21     
- Partials     2961     2972      +11     

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

@ischoegl ischoegl changed the title Tweak MATLAB Func1 operators Tweak Func1 operators Aug 4, 2024
@ischoegl ischoegl force-pushed the tweak-func1 branch 3 times, most recently from 932b9da to 561f756 Compare August 4, 2024 22:13
@ischoegl ischoegl marked this pull request as ready for review August 4, 2024 22:42
@ischoegl ischoegl requested a review from a team August 4, 2024 22:42
@ischoegl ischoegl marked this pull request as draft August 5, 2024 01:35
@ischoegl ischoegl removed the request for review from a team August 5, 2024 01:39
@ischoegl ischoegl changed the title Tweak Func1 operators Make Func1 API's more consistent Aug 5, 2024
@ischoegl

This comment was marked as duplicate.

@ischoegl ischoegl marked this pull request as ready for review August 5, 2024 03:08
@ischoegl ischoegl requested a review from a team August 5, 2024 03:08
@ischoegl ischoegl force-pushed the tweak-func1 branch 8 times, most recently from c492eb5 to 23f2771 Compare August 6, 2024 16:21
@ischoegl
Copy link
Copy Markdown
Member Author

ischoegl commented Aug 6, 2024

I ended up adding a section to our custom_reaction.py benchmark that uses C++ functors. Here are results:

% python samples/python/kinetics/custom_reactions.py
Average time of 100 simulation runs for 'gri30.yaml' (CH4)
- New framework (YAML): 18.90 μs/step (T_final=2780.21)
- Two Custom reactions (Python): 19.27 μs/step (T_final=2780.21) ...+1.92%
- Two Custom reactions (C++): 18.93 μs/step (T_final=2780.21) ...+0.15%
- Two Extensible reactions: 19.74 μs/step (T_final=2780.21) ...+4.45%

i.e. unsurprisingly the C++ Func1 performs more or less the same as the original mechanism (on some runs, it was actually faster, so it is within the accuracy of this poor man's benchmark).

While I don't believe that C++ functors are all that important to the Python API, I believe that having an interface equivalent to MATLAB is overall beneficial.

@ischoegl

This comment was marked as outdated.

@ischoegl

This comment was marked as outdated.

ischoegl

This comment was marked as resolved.

@ischoegl ischoegl force-pushed the tweak-func1 branch 2 times, most recently from 727437c to 983e599 Compare August 7, 2024 14:21
@ischoegl
Copy link
Copy Markdown
Member Author

ischoegl commented Aug 7, 2024

Rebased once more and took care of a couple of paper cuts; PR should be ready for a review.

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, @ischoegl, this generally makes sense to me, and I appreciate the comprehensive unit tests. I had a handful of small suggestions.

@ischoegl ischoegl marked this pull request as draft August 9, 2024 13:13
@ischoegl
Copy link
Copy Markdown
Member Author

ischoegl commented Aug 9, 2024

@speth ... I updated the Python docstring as follows:

     [...]
     Note that `Func1` objects also allow for direct access to functor objects
     implemented in C++ based on associated type specifiers::

         >>> f5 = Func1("exp", 3.)  # C++ 'Exp1' functor
         >>> f5.cxx_type
         'Cantera::Exp1'
         >>> f5.write()
         '\\exp(3x)'
         >>> f5(2.)
         403.4287934927351
         >>> f6 = Func1("Arrhenius", [9630.0, 2.0, 2012.878])  # C++ 'Arrhenius1' functor
         >>> f6(1500)
         5662665826.195515

     For implemented C++ functor types, see the Cantera C++ :ct:`Func1` documentation.

     `Func1` objects support operator overloading which facilitates the construction of
     compound functions, where some standard simplifications are implemented::

         >>> f7 = 2 * f5 + 3
         >>> f7.write()
         '2\\exp(3x) + 3'
         >>> f7(2.)
         809.8575869854702
         >>> f8 = f5 * f5
         >>> f8.write()
         '\\exp(6x)'
         >>> f8(2.)
         162754.79141900392

which also illustrates Func1.write (added in 3.0) and a Func1.cxx_type that I added in an update to this PR.

@ischoegl ischoegl marked this pull request as ready for review August 9, 2024 16:20
@ischoegl ischoegl requested a review from speth August 9, 2024 16:20
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, @ischoegl. This looks good to me.

@speth speth merged commit 831ffe9 into Cantera:main Aug 9, 2024
@ischoegl ischoegl deleted the tweak-func1 branch August 9, 2024 21:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants