Skip to content

Conversation

@benegee
Copy link
Collaborator

@benegee benegee commented Feb 29, 2024

This is to demonstrate how an external function could be called to evaluate source terms.

Issues:

  • Path to shared object is currently hard-coded.
  • Global buffer variable in libelixir to prevent subsequent allocations in source_term_callback

Some rough timing results:

Timing libtrixi
────────────────────────────────────────────────────────────────────────────────────────────────────
Trixi.jl simulation finished.  Final time: 2.0  Time steps: 229 (accepted), 229 (total)
────────────────────────────────────────────────────────────────────────────────────────────────────


*** Trixi controller ***   Finalize Trixi simulation
 ────────────────────────────────────────────────────────────────────────────────────
              Trixi.jl                      Time                    Allocations
                                   ───────────────────────   ────────────────────────
         Tot / % measured:              2.39s /  19.5%            129MiB /  14.2%

 Section                   ncalls     time    %tot     avg     alloc    %tot      avg
 ────────────────────────────────────────────────────────────────────────────────────
 I/O                            5    200ms   43.0%  40.1ms   10.6MiB   57.4%  2.11MiB
   save solution                4    124ms   26.6%  31.0ms   10.3MiB   56.1%  2.59MiB
   ~I/O~                        5   76.3ms   16.4%  15.3ms    230KiB    1.2%  46.1KiB
   get element variables        4   1.52μs    0.0%   380ns     0.00B    0.0%    0.00B
   save mesh                    4    501ns    0.0%   125ns     0.00B    0.0%    0.00B
   get node variables           4   88.0ns    0.0%  22.0ns     0.00B    0.0%    0.00B
 rhs!                       1.15k    174ms   37.3%   152μs   5.14KiB    0.0%    4.59B
   source terms             1.15k   80.7ms   17.3%  70.4μs     0.00B    0.0%    0.00B
   volume integral          1.15k   45.9ms    9.8%  40.0μs     0.00B    0.0%    0.00B
   interface flux           1.15k   29.7ms    6.4%  25.9μs     0.00B    0.0%    0.00B
   surface integral         1.15k   10.8ms    2.3%  9.44μs     0.00B    0.0%    0.00B
   Jacobian                 1.15k   4.16ms    0.9%  3.63μs     0.00B    0.0%    0.00B
   reset ∂u/∂t              1.15k   2.20ms    0.5%  1.92μs     0.00B    0.0%    0.00B
   ~rhs!~                   1.15k    523μs    0.1%   457ns   5.14KiB    0.0%    4.59B
   boundary flux            1.15k   21.4μs    0.0%  18.7ns     0.00B    0.0%    0.00B
 analyze solution               4   87.2ms   18.7%  21.8ms   7.85MiB   42.6%  1.96MiB
 calculate dt                 230   4.44ms    1.0%  19.3μs     0.00B    0.0%    0.00B
 ────────────────────────────────────────────────────────────────────────────────────
Timing Trixi.jl
────────────────────────────────────────────────────────────────────────────────────
              Trixi.jl                      Time                    Allocations
                                   ───────────────────────   ────────────────────────
         Tot / % measured:              164ms /  94.0%           2.00MiB /  90.6%

 Section                   ncalls     time    %tot     avg     alloc    %tot      avg
 ────────────────────────────────────────────────────────────────────────────────────
 rhs!                       1.15k    144ms   93.2%   126μs   5.14KiB    0.3%    4.59B
   source terms             1.15k   59.7ms   38.7%  52.1μs     0.00B    0.0%    0.00B
   volume integral          1.15k   41.5ms   26.9%  36.2μs     0.00B    0.0%    0.00B
   interface flux           1.15k   26.6ms   17.3%  23.2μs     0.00B    0.0%    0.00B
   surface integral         1.15k   9.68ms    6.3%  8.44μs     0.00B    0.0%    0.00B
   Jacobian                 1.15k   3.82ms    2.5%  3.34μs     0.00B    0.0%    0.00B
   reset ∂u/∂t              1.15k   1.99ms    1.3%  1.73μs     0.00B    0.0%    0.00B
   ~rhs!~                   1.15k    498μs    0.3%   434ns   5.14KiB    0.3%    4.59B
   boundary flux            1.15k   28.8μs    0.0%  25.2ns     0.00B    0.0%    0.00B
 I/O                            5   4.82ms    3.1%   964μs   1.56MiB   85.9%   319KiB
   save solution                4   4.31ms    2.8%  1.08ms   1.55MiB   85.4%   397KiB
   ~I/O~                        5    507μs    0.3%   101μs   8.83KiB    0.5%  1.77KiB
   get element variables        4    765ns    0.0%   191ns     0.00B    0.0%    0.00B
   save mesh                    4    488ns    0.0%   122ns     0.00B    0.0%    0.00B
   get node variables           4    342ns    0.0%  85.5ns     0.00B    0.0%    0.00B
 calculate dt                 230   4.04ms    2.6%  17.6μs     0.00B    0.0%    0.00B
 analyze solution               4   1.59ms    1.0%   396μs    258KiB   13.9%  64.5KiB
 ────────────────────────────────────────────────────────────────────────────────────

Confer #173

@codecov
Copy link

codecov bot commented Feb 29, 2024

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.19%. Comparing base (48ef1c6) to head (1ad3ff6).

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #172      +/-   ##
==========================================
+ Coverage   96.15%   96.19%   +0.04%     
==========================================
  Files          25       26       +1     
  Lines        1273     1289      +16     
  Branches       74       74              
==========================================
+ Hits         1224     1240      +16     
  Misses         45       45              
  Partials        4        4              
Flag Coverage Δ
unittests 96.19% <100.00%> (+0.04%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ 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.

@benegee benegee marked this pull request as ready for review October 8, 2025 19:55
@benegee
Copy link
Collaborator Author

benegee commented Oct 9, 2025

This has not been adopted by an external application but I still like the demo. Could we include it as well?

@benegee benegee requested a review from sloede October 9, 2025 10:13
Comment on lines +8 to +9
void source_term_wave(const double * u, const double * x, const double t,
const double gamma, double * sourceterm) {
Copy link
Member

Choose a reason for hiding this comment

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

Should we pass in the length of u, x, sourceterm?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

And then check on C side before accessing the fields?

Comment on lines +20 to +24
function (callback::SourceTermsCallback)(u, x, t, equations::CompressibleEulerEquations2D)
@unpack source_term_fptr, buffer = callback
@ccall $source_term_fptr(u::Ptr{Cdouble}, x::Ptr{Cdouble}, t::Cdouble,
equations.gamma::Cdouble, buffer::Ptr{Cdouble})::Cvoid
return SVector(buffer[])
Copy link
Member

Choose a reason for hiding this comment

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

Would be nice to have some documentation. Are we calling this per point? It might be better for the user if they could call it over all points. The call-overhead is small, but present.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Both true!

Trixi.jl's approach is to evaluate per point. If you would rather like to evaluate at all points and then access the values via indices you have to redefine Trixi.jl's calc_sources!. We did this here

function Trixi.calc_sources!(du, u, t, source_terms::SourceTerm,

but it's not very elegant.

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.

4 participants