AMReX-Astro / AMReX-Astro/Microphysics

use std::expected to handle burn failures?

Open
#1,439 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement integrator reactions
Dominant language
C++
Stars
43
Forks
46
Avg merge
2d 18h
Merged PRs (30d)
15

Description

It would be useful to handle burn/integration failures in a more modern C++ (but exception-free) way.

This could be done by having the burner return an std::expected object (https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0323r3.pdf, monadic functions: https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2505r1.html)

(Public domain implementation here: https://github.com/TartanLlama/expected.)

It works like this:

enum class arithmetic_errc
{
 divide_by_zero, // 9 / 0 == ?
 not_integer_division, // 5 / 2 == 2.5 (which is not an integer)
 integer_divide_overflows, // INT_MIN / -1
};

expected<double, errc> safe_divide(double i, double j)
{
 if (j == 0) return unexpected(arithmetic_errc::divide_by_zero); // (1)
 else return i / j; // (2)
}

int divide2(int i, int j)
{
 auto e = safe_divide(i, j);
 if (!e)
 switch (e.error().value()) {
 case arithmetic_errc::divide_by_zero: return 0;
 case arithmetic_errc::not_integer_division: return i / j; // Ignore.
 case arithmetic_errc::integer_divide_overflows: return INT_MIN;
 // No default! Adding a new enum value causes a compiler warning here,
 // forcing an update of the code.
 }
 return *e;
}

In principle, std::expected objects can be propagated higher in the call stack, so it could be used thoughout Microphysics, not just in returning to the application code.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by locating the burner entry points and the paths that return failures to the application code; the issue names no files or tests. Read how failures currently propagate through Microphysics, then define the affected interfaces and tests needed for std::expected-based, exception-free propagation to be complete.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.