Skip to content

refactor: vectorize explicit stencils, extract shared latent heat in 1D solvers#73

Merged
djsilva99 merged 2 commits intodjsilva99:developfrom
formeo:refactor-1d-solvers
Mar 31, 2026
Merged

refactor: vectorize explicit stencils, extract shared latent heat in 1D solvers#73
djsilva99 merged 2 commits intodjsilva99:developfrom
formeo:refactor-1d-solvers

Conversation

@formeo
Copy link
Copy Markdown

@formeo formeo commented Mar 24, 2026

Related to #45

What changed

New file: _latent_heat.py
Extracts the latent heat phase-transition logic that was duplicated in all
4 solvers into a single apply_latent_heat() function.

Explicit solvers (explicit_general.py, explicit_k.py)

  • Replaced copy.deepcopy + per-element Python loops with numpy vectorized
    FDM stencil computation
  • Interior points computed in one array operation instead of a for-loop
  • Boundary conditions applied to the numpy array directly

Implicit solvers (implicit_general.py, implicit_k.py)

  • Replaced the inlined latent heat block with apply_latent_heat() call
  • Minor cleanup: use n = obj.num_points local variable, remove unused
    copy import

What didn't change

  • Function signatures and return format (y, lheat)
  • FDM stencil formulas (same equations, just vectorized)
  • Latent heat logic (exact same branching, moved to shared function)
  • Implicit solvers still use np.linalg.solve with a tridiagonal matrix
    (no scipy.linalg.solve_banded — that could be a separate PR)

Testing

All 25 existing tests pass. Test runtime dropped from ~118s to ~72s on
the same machine (the effect will be larger on bigger grids since the
vectorization benefit scales with num_points).

Stats

4 files changed, 110 insertions(+), 213 deletions(-)
1 new file: heatrapy/dimension_1/solvers/_latent_heat.py (52 lines)

- Extract duplicated latent heat block into _latent_heat.py
- Vectorize explicit_general and explicit_k FDM stencils with numpy
- Remove copy.deepcopy from explicit solvers
- All 25 tests pass, ~1.6x speedup on test suite

Closes djsilva99#45
@codecov
Copy link
Copy Markdown

codecov bot commented Mar 26, 2026

Codecov Report

❌ Patch coverage is 86.51685% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 72.79%. Comparing base (8097499) to head (2e183e0).
⚠️ Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
heatrapy/dimension_1/solvers/_latent_heat.py 60.00% 10 Missing ⚠️
heatrapy/dimension_1/solvers/explicit_general.py 95.23% 1 Missing ⚠️
heatrapy/dimension_1/solvers/explicit_k.py 95.23% 1 Missing ⚠️
@@             Coverage Diff             @@
##           develop      #73      +/-   ##
===========================================
+ Coverage    70.66%   72.79%   +2.13%     
===========================================
  Files           21       22       +1     
  Lines         2335     2272      -63     
===========================================
+ Hits          1650     1654       +4     
+ Misses         685      618      -67     
Files with missing lines Coverage Δ
heatrapy/dimension_1/solvers/implicit_general.py 96.15% <100.00%> (+34.92%) ⬆️
heatrapy/dimension_1/solvers/implicit_k.py 96.00% <100.00%> (+35.58%) ⬆️
heatrapy/dimension_1/solvers/explicit_general.py 96.00% <95.23%> (+37.30%) ⬆️
heatrapy/dimension_1/solvers/explicit_k.py 96.00% <95.23%> (+37.30%) ⬆️
heatrapy/dimension_1/solvers/_latent_heat.py 60.00% <60.00%> (ø)

... and 2 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@djsilva99
Copy link
Copy Markdown
Owner

@formeo thanks a lot for the contribution — the implementation looks solid and the code quality is great overall 👍

I have a few small requests before merging:

  • Could you update the PR title to "refactor: vectorize explicit stencils, extract shared latent heat in 1D solvers" to follow Conventional Commits? This helps ensure it’s properly reflected when generating new releases.
  • Please remove the "Closes 45" from the PR description. The correct use of typing is part of that issue, so it shouldn’t be closed by this PR alone.
  • It would be great to add a test covering a phase transition scenario. For example, you could create a simple integration test in test_single_1d.py using water as the material, with an initial temperature below 0ºC and a fixed temperature boundary condition above 0ºC. This would also increase the overall coverage that must be addressed.

Thanks again — really nice work!

@formeo formeo changed the title Refactor 1D solvers: vectorize explicit stencils, extract shared latent heat refactor: vectorize explicit stencils, extract shared latent heat in 1D solvers Mar 31, 2026
@formeo
Copy link
Copy Markdown
Author

formeo commented Mar 31, 2026

Thanks Daniel! All three addressed:

  1. ✅ PR title updated to follow Conventional Commits
  2. ✅ Removed "Closes Refactor 1D solvers #45" from description
  3. ✅ Added phase transition test using water (263K → 300K boundary,
    verifies temperature is held at 273K by latent heat absorption)

The new test runs with explicit_k(x) and checks that:

  • the point nearest the hot boundary is held at 273K (int = 272)
    instead of rising freely toward 300K
  • an interior point further from the boundary stays below the
    transition temperature (int = 266)

This covers the latent heat heating path in _latent_heat.py.

@djsilva99 djsilva99 changed the base branch from master to develop March 31, 2026 16:58
@djsilva99 djsilva99 merged commit 71522f1 into djsilva99:develop Mar 31, 2026
3 checks passed
@formeo formeo mentioned this pull request Apr 1, 2026
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.

2 participants