Cantera / cantera

Chemical kinetics, thermodynamics, and transport tool suite
https://cantera.org
Other
624 stars 350 forks source link

Add stack trace option to CanteraError #1730

Closed speth closed 4 months ago

speth commented 4 months ago

Changes proposed in this pull request

Add a static method CanteraError::setStackTraceDepth that can be used to add the top N C++ stack frames to the message of a CanteraError. This can provide additional context on where certain errors are being thrown from.

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

From Python, instantiate a Solution object where the YAML file specifies a negative temperature for the initial state:

import cantera as ct
ct.CanteraError.set_stack_trace_depth(10)
ct.Solution('broken.yaml')

Now, the Python traceback is followed by the CanteraError message and the top of the C++ stack:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/python/cantera/solutionbase.pyx", line 37, in cantera.solutionbase._SolutionBase.__cinit__
    self._cinit(infile=infile, name=name, adjacent=adjacent, origin=origin,
  File "build/python/cantera/solutionbase.pyx", line 75, in cantera.solutionbase._SolutionBase._cinit
    self._init_yaml(infile, name, adjacent, yaml, transport)
  File "build/python/cantera/solutionbase.pyx", line 160, in cantera.solutionbase._SolutionBase._init_yaml
    soln = newSolution(
cantera._utils.CanteraError: 
*******************************************************************************
CanteraError thrown by Phase::setTemperature:
temperature must be positive. T = -300
-------------------------------------------------------------------------------
 0# Cantera::CanteraError::CanteraError(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 1# Cantera::CanteraError::CanteraError<double>(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, double const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 2# Cantera::Phase::setTemperature(double) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 3# Cantera::ThermoPhase::setState_TP(double, double) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 4# Cantera::ThermoPhase::setState(Cantera::AnyMap const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 5# Cantera::setupPhase(Cantera::ThermoPhase&, Cantera::AnyMap const&, Cantera::AnyMap const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 6# Cantera::newThermo(Cantera::AnyMap const&, Cantera::AnyMap const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 7# Cantera::newSolution(Cantera::AnyMap const&, Cantera::AnyMap const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::vector<std::__1::shared_ptr<Cantera::Solution>, std::__1::allocator<std::__1::shared_ptr<Cantera::Solution>>> const&, std::__1::map<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>, std::__1::shared_ptr<Cantera::Solution>, std::__1::less<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>>>, std::__1::allocator<std::__1::pair<std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const, std::__1::shared_ptr<Cantera::Solution>>>> const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 8# Cantera::newSolution(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char>> const&, std::__1::vector<std::__1::shared_ptr<Cantera::Solution>, std::__1::allocator<std::__1::shared_ptr<Cantera::Solution>>> const&) in /Users/speth/src/cantera/build/lib/libcantera_shared.3.1.0.dylib
 9# __pyx_pw_7cantera_12solutionbase_13_SolutionBase_9_init_yaml(_object*, _object* const*, long, _object*) in /Users/speth/src/cantera/build/python/cantera/_cantera.cpython-311-darwin.so
*******************************************************************************

Checklist

codecov[bot] commented 4 months ago

Codecov Report

All modified and coverable lines are covered by tests :white_check_mark:

Project coverage is 72.80%. Comparing base (b2c0af5) to head (eaae5d1).

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #1730 +/- ## ======================================= Coverage 72.80% 72.80% ======================================= Files 381 381 Lines 54022 54033 +11 Branches 9210 9213 +3 ======================================= + Hits 39330 39341 +11 Misses 11707 11707 Partials 2985 2985 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.