Skip to content

Refactor StoichManager for sparse Jacobian Calculations#1081

Closed
ischoegl wants to merge 37 commits intoCantera:mainfrom
ischoegl:sparse-stoich-coeffs
Closed

Refactor StoichManager for sparse Jacobian Calculations#1081
ischoegl wants to merge 37 commits intoCantera:mainfrom
ischoegl:sparse-stoich-coeffs

Conversation

@ischoegl
Copy link
Copy Markdown
Member

@ischoegl ischoegl commented Aug 1, 2021

Changes proposed in this pull request

  • Simplify StoichManager
  • Leverage <Eigen/Sparse> to handle sparse multiplications
  • Implement calculation of sparse Jacobian terms

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

See #Cantera/enhancements#111 and #Cantera/enhancements#100

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

While calculations are handled by <Eigen/Sparse> internally, results are exposed to the Python API as:

>>> gas.net_rop_species_derivatives # similar for forward/reverse
>>> gas.net_rop_temperature_derivatives # similar for forward/reverse
>>> gas.net_production_rate_species_derivatives # similar for creation/destruction
>>> gas.net_production_rate_temperature_derivatives # similar for creation/destruction

All derivatives are checked against numerical implementations in unit tests. At the moment, third-body terms are not considered. (Edit: now done.) There may be some overlap with #1010, where Jacobians are evaluated for entire ReactorNets (whereas this PR focuses on GasKinetics).

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

@ischoegl ischoegl marked this pull request as draft August 1, 2021 00:38
@codecov
Copy link
Copy Markdown

codecov bot commented Aug 1, 2021

Codecov Report

Merging #1081 (e1c9286) into main (e77148b) will increase coverage by 0.03%.
The diff coverage is 76.41%.

❗ Current head e1c9286 differs from pull request most recent head 53b764e. Consider uploading reports for the commit 53b764e to get more accurate results
Impacted file tree graph

@@            Coverage Diff             @@
##             main    #1081      +/-   ##
==========================================
+ Coverage   73.45%   73.48%   +0.03%     
==========================================
  Files         364      362       -2     
  Lines       47611    48009     +398     
==========================================
+ Hits        34972    35280     +308     
- Misses      12639    12729      +90     
Impacted Files Coverage Δ
include/cantera/base/global.h 93.33% <ø> (ø)
include/cantera/kinetics/Kinetics.h 51.35% <0.00%> (-4.54%) ⬇️
src/clib/ct.cpp 8.29% <0.00%> (ø)
src/numerics/DenseMatrix.cpp 62.93% <ø> (ø)
src/numerics/polyfit.cpp 94.44% <ø> (ø)
include/cantera/kinetics/ReactionRate.h 65.43% <21.42%> (+4.09%) ⬆️
src/kinetics/GasKinetics.cpp 79.62% <64.86%> (-12.99%) ⬇️
src/kinetics/Kinetics.cpp 79.75% <73.68%> (-4.18%) ⬇️
include/cantera/cython/wrappers.h 81.81% <77.77%> (-1.79%) ⬇️
include/cantera/kinetics/StoichManager.h 96.73% <94.69%> (-1.91%) ⬇️
... and 27 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e77148b...53b764e. Read the comment docs.

@bryanwweber
Copy link
Copy Markdown
Member

I think to fix the failures on the Conda builds, you need to include config.h before you check whether CT_USE_SYSTEM_EIGEN is true. At least, that's what's done in eigen_dense.h, via ct_defs.h.

@ischoegl
Copy link
Copy Markdown
Member Author

ischoegl commented Aug 2, 2021

I think to fix the failures on the Conda builds, you need to include config.h before you check whether CT_USE_SYSTEM_EIGEN is true. At least, that's what's done in eigen_dense.h, via ct_defs.h.

👍 - Ah - I was wondering about that. Thanks! I ended up moving the include of StoichManager.h to the *.cpp files, which appears to have solved the issue by a different route. Main reason was that it didn't need to be included everywhere ...

@ischoegl ischoegl force-pushed the sparse-stoich-coeffs branch from ae147d6 to 318e5b3 Compare August 2, 2021 13:02
@bryanwweber
Copy link
Copy Markdown
Member

bryanwweber commented Aug 2, 2021

I ended up moving the include of StoichManager.h to the *.cpp files, which appears to have solved the issue by a different route.

👍 Nonetheless, I still think it's a good idea to make sure that config.h is included before checking CT_USE_SYSTEM_EIGEN in StoichManager.h, because it may get moved around again at a later date, or used in a different file, etc. and would lead to hard-to-debug errors.

@ischoegl ischoegl force-pushed the sparse-stoich-coeffs branch 3 times, most recently from 9dc30cb to 6f79569 Compare August 2, 2021 15:41
@ischoegl
Copy link
Copy Markdown
Member Author

ischoegl commented Aug 2, 2021

Nonetheless, I still think it's a good idea to make sure that config.h is included before checking CT_USE_SYSTEM_EIGEN in StoichManager.h, because it may get moved around again at a later date, or used in a different file, etc. and would lead to hard-to-debug errors.

Fair enough. I changed eigen_dense.h to include sparse (and renamed to eigen_defs.h), and included that in SolutionManager.h. I believe that way everything is taken care of.

@ischoegl ischoegl force-pushed the sparse-stoich-coeffs branch 12 times, most recently from da06abc to 8b5a95d Compare August 4, 2021 20:43
@ischoegl ischoegl changed the title Sparse stoich coeffs Refactor StoichManager for sparse Jacobian Calculations Aug 4, 2021
@ischoegl

This comment has been minimized.

@ischoegl ischoegl force-pushed the sparse-stoich-coeffs branch 5 times, most recently from 7d0aac7 to 9e0a6b9 Compare August 5, 2021 16:08
While the method is part of the initialization, it is the final step
required after all reactions are added to the Kinetics object.
While changes worked and can potentially create easier-to-follow code
there is a performance penalty, as additional copy operations are needed.
Create flag to specify whether third body derivatives are included
Uses wrapper.h to simplify passing of data
* Move functions to header file
* Make function names more consistent
* Pass raw pointers to add flexibility
Enable 'exact' temperature derivatives for forward and net rates of
progress, with the caveat that the derivative of the equilibrium constant
is evaluated numerically.
Make naming more consistent and improve code performance and readability.
All derivatives are taken with respect to temperature and species mole
fractions. In order to clarify (and shorten method names), Jacobian routines
are renamed as follows:

* Kinetics::*RopTemperatureDerivatives  -> Kinetics::*RatesOfProgress_ddT
* Kinetics::*RopSpeciesDerivatives      -> Kinetics::*RatesOfProgress_ddC
* Kinetics::*RateTemperatureDerivatives -> Kinetics::*Rates_ddT
* Kinetics::*RateSpeciesDerivatives     -> Kinetics::*Rates_ddC

Essential jacobians methods are consolidated within GasKinetics
@ischoegl
Copy link
Copy Markdown
Member Author

ischoegl commented Aug 25, 2021

As this PR experienced substantial 'feature drift', I am closing this, and continuing with #1089 (which also avoids a meandering commit history).

@ischoegl ischoegl closed this Aug 25, 2021
@ischoegl ischoegl deleted the sparse-stoich-coeffs branch August 25, 2021 17:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants