Skip to content

Fix: UndefVarError in get_norm_matrix for BlockProjection#42

Open
Ady0333 wants to merge 2 commits intogridap:mainfrom
Ady0333:fix/block-projection-norm-matrix
Open

Fix: UndefVarError in get_norm_matrix for BlockProjection#42
Ady0333 wants to merge 2 commits intogridap:mainfrom
Ady0333:fix/block-projection-norm-matrix

Conversation

@Ady0333
Copy link
Copy Markdown
Contributor

@Ady0333 Ady0333 commented Apr 6, 2026

Fixes #41

Problem

The return_cache function for get_norm_matrix(::BlockProjection) in src/RB/RBSteady/Projections.jl (lines 721-726) references an undefined variable s instead of the actual parameter a::BlockProjection.

Broken code:

function Arrays.return_cache(::typeof(get_norm_matrix), a::BlockProjection)
  i = findfirst(s.touched)                    
  @notimplementedif isnothing(i)
  A = typeof(get_norm_matrix(a[i]))
  norm_matrix = Array{A,ndims(s)}(undef,size(s))  
  return norm_matrix
end

This is a copy-paste error from the adjacent return_cache functions (lines 586-592) where the parameter is correctly named s::BlockSnapshots. When adapting the function for BlockProjection, the parameter was renamed to a, but three references in the body (s.touched, ndims(s), size(s)) were not updated.

Secondary issue: Even with the variable name fixed, the function allocated a plain Array instead of a BlockArray, which would fail on Block(i,j) indexing downstream.

Impact

Who is affected: Anyone running multi-field ROM workflows (Stokes, Navier-Stokes, coupled PDEs) that compute projection errors or assemble norm matrices.

What breaks:

  • Every call to get_norm_matrix on a BlockProjection crashes with UndefVarError: s not defined at line 722
  • projection_error on MultiFieldRBSpace is completely broken
  • Multi-field ROM validation and error estimation workflows cannot run
  • This is a guaranteed crash. The bug triggers on the very first line of the function body

Silent failure? No, this is an unconditional hard crash. The feature is completely non-functional.

How to Reproduce

Confirmed with regression test:

using Gridap, GridapROMs

# Any multi-field ROM setup
bp = BlockProjection(...)  # minimal valid BlockProjection
get_norm_matrix(bp)

# Crashes immediately:
# UndefVarError: `s` not defined
#   at return_cache(::typeof(get_norm_matrix), a::BlockProjection{PODProjection, 1})
#   @ GridapROMs.RBSteady ~/GridapROMs.jl/src/RB/RBSteady/Projections.jl:722

Or via the standard multi-field workflow:

# After building a MultiFieldRBSpace
projection_error(rb_space, x, r)
# → crashes in get_norm_matrix call chain

The crash happens unconditionally, every call to get_norm_matrix on any BlockProjection hits this bug.

Fix

Changed: src/RB/RBSteady/Projections.jl, function Arrays.return_cache(::typeof(get_norm_matrix), a::BlockProjection)

Replaced the broken 4-line body with a corrected 3-line implementation that:

  1. References the correct parameter a (not undefined s)
  2. Computes block sizes from num_fe_dofs(a)
  3. Allocates a properly structured BlockArray instead of plain Array

Fixed code:

function Arrays.return_cache(::typeof(get_norm_matrix), a::BlockProjection)
  block_sizes = num_fe_dofs(a)
  norm_matrix = mortar(map(s -> zeros(s, s), block_sizes))
  return norm_matrix
end

This ensures:

  • All variable references are valid (uses a, not s)
  • Returns a BlockArray with proper block structure for multi-field indexing
  • Zero-initialized (correct semantic for norm matrix cache)

Net change: -1 line, but functionally complete rewrite of the body.

Testing

Added test/RBSteady/block_projection_norm_matrix.jl with regression tests that:

  1. Creates a minimal BlockProjection with multiple blocks
  2. Calls get_norm_matrix(bp) and verifies it doesn't crash with UndefVarError
  3. Validates the returned cache is a BlockArray (not plain Array)
  4. Checks proper block structure and dimensions
  5. Verifies zero-initialization

Test result before fix: UndefVarError: s not defined at line 722
Test result after fix: All assertions pass

After This Fix

  • get_norm_matrix(::BlockProjection) is fully functional
  • Multi-field ROM projection error estimation works correctly
  • projection_error on MultiFieldRBSpace completes successfully
  • Stokes/Navier-Stokes/coupled PDE ROM validation pipelines can run
  • Proper block structure ensures downstream indexing operations work

Ady0333 added 2 commits April 6, 2026 11:36
Replace undefined variable `s` with `a` and use BlockArray for
proper Block indexing in return_cache(get_norm_matrix, ::BlockProjection).

Signed-off-by: Ady0333 <adityashinde1525@gmail.com>
@Ady0333
Copy link
Copy Markdown
Contributor Author

Ady0333 commented Apr 6, 2026

Hello @nichomueller , Please review this PR and let me know if any changes are required....

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.

[BUG]: UndefVarError in return_cache for get_norm_matrix on BlockProjection

1 participant