Skip to content

Comments

Optimize build_cartesian_ncc_matrix#331

Open
tjc0726 wants to merge 1 commit intoDedalusProject:masterfrom
tjc0726:master
Open

Optimize build_cartesian_ncc_matrix#331
tjc0726 wants to merge 1 commit intoDedalusProject:masterfrom
tjc0726:master

Conversation

@tjc0726
Copy link

@tjc0726 tjc0726 commented Feb 24, 2026

This PR optimizes Product.build_cartesian_ncc_matrix() in dedalus/core/arithmetic.py to achieve speedup for problems with spatially varying coefficients (NCC).

The key insight is to batch-accumulate sparse matrices instead of sequential addition, reducing sorting/deduplication overhead from O(n²) to O(n log n).

In my case

Benchmark: 2D Navier-Stokes with NCC, N=64

  • Original: 297.63s per Newton iteration
  • Optimized: 27.23s per Newton iteration
  • Speedup: 10.93x

Numerical Verification
All fields match to machine precision (relative error < 1e-14):

  • u: 3.789e-14
  • p: 2.028e-12
  • force_x: 6.609e-16

here's my example using monkry patch, one can delete the patch to check the difference

example_ncc_flow_optimized.py

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.

1 participant