Skip to content

Feature/cpp parity ops#315

Draft
Jiya-Rathi wants to merge 4 commits intomainfrom
feature/CPP_Parity_Ops
Draft

Feature/cpp parity ops#315
Jiya-Rathi wants to merge 4 commits intomainfrom
feature/CPP_Parity_Ops

Conversation

@Jiya-Rathi
Copy link
Copy Markdown
Collaborator

What type of PR is this? (check all applicable)

  • Refactor
  • Feature
  • Bug Fix
  • Optimization
  • Example
  • Documentation

Description

Currently the mimetic operators in MATLAB/Octave work for periodic and non-periodic boundary conditions. The C++ implementation needs to include functionality to support the periodic cases. The new MATLAB/Octave interface returns mimetic operators depending on whether or not the operator contain a periodic boundary condition type a0 U + b0 dU/dn = g. However, the current C++ gradient constructors do not check for these boundary conditions, nor has an implementation for periodic boundaries. We propose creating new C++ constructors that match the grad implementations in MATLAB/Octave.

Related Issues & Documents

QA Instructions, Screenshots, Recordings

Run test2.cpp to test this code.

Added/updated tests?

  • Yes
  • No, and this is why: please replace this line with details on why tests
    have not been included
  • I need help with writing tests

Read Contributing Guide and Code of Conduct

[optional] Are there any post deployment tasks we need to perform?

[optional] What gif best describes this PR or how it makes you feel?

Add BC-aware constructor overloads accepting Robin coefficient vectors dc (a0) and nc (b0). An axis is periodic when all dc/nc entries are zero, otherwise the existing non-periodic operator is used. Mixed periodicity supported in 2D/3D via per-axis checks. Adds two private static helpers: periodicGrad1D and isPeriodic. All original constructors unchanged.
Replace file(GLOB) with explicit source lists in src/cpp, tests/cpp, and examples/cpp CMakeLists.txt to avoid stale builds when files are added or renamed.

Extend gradient.cpp with periodic boundary condition support:
- Implement isPeriodic helper returning int instead of bool for efficiency
- Remove Armadillo dependency in isPeriodic and zero-fill logic
- Update dc and nc parameter types accordingly

Update tests/cpp/test2.cpp to cover the periodic gradient case.
Add BC-aware constructor overloads accepting Robin coefficient vectors (dc/nc) for 1D, 2D, and 3D. Implement periodicGrad1D as a circulant sparse matrix via finite-difference stencil, with isPeriodic helper to detect periodic axes. Support mixed periodicity in 2D/3D through per-axis checking and Kronecker product assembly. Preserve backward compatibility by leaving original constructors untouched.
Copy link
Copy Markdown
Collaborator

@jbrzensk jbrzensk left a comment

Choose a reason for hiding this comment

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

Hi ya'll, thanks for this.

I tried it and it works appropriately. Most of the notes are on formatting. The clang-formatter flags almost every line, for spacing issues. It doesn't reject the PR but does generate a lot of errors that need to be addressed.

// A
at(0, 0) = -8.0 / 3.0;
at(0, 1) = 3.0;
at(0, 1) = 3.0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

All the spaces and non-spaces are flagged by the linter.
Click lint-new-code, run super linter

at(m - 1, m - 2) = 3.0 / 40.0;
at(m - 1, m - 3) = -1.0 / 168.0;
// Middle
at(0, 0) = -352.0/105.0; at(0, 1) = 35.0/8.0;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Same here. One line for each command, spaces between operators, no spaces after ;

Comment on lines +313 to +314
// first and last rows removed, leaving only the s interior rows.
static sp_mat trimmedIdentity(u32 s) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Will this be used anywhere else? Maybe it should be in the utils.h?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I agree, let me look into this. Thanks

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

the trimmedIdentity function is currently used only for trimming in the gradient.cpp file, a similar operation is perform in the divergence.cpp but column-wise. The function was added to simply reduce code duplication.
A closer look, there are other sections of the divergence.cpp file that can also call this functions (with different arguments) and avoid duplications. For instance,
lines 242-245, and lines 268-273 of divergence.cpp.
@jbrzensk do you think that this would be useful to users as utility? or should it be kept internally?

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Its fine internal here as a static if nothing else could use it.

@Tony-Drummond
Copy link
Copy Markdown
Collaborator

FYI, this work is being incrementally committed to allow for other team members to provide feedback as the work progresses.

Thus, this Draft PR will have intermediate steps before becoming a PR. Other team members can incrementally review the C++ implementations of the three operators; gradient, divergence and laplacian in the feature/CPP_Parity_Ops branch. Here's a recap of the whole process:

  1. First, @Jiya-Rathi created this draft PR for the branch feature/CPP_Parity_Ops to host all C++ updates to the mimetic operators,
  2. to allow incremental reviews of the C++ updates, @Jiya-Rathi has created a second branch feature/CPP_Parity_Ops_New, where she will continue to add new operator functionality, while the branch feature/CPP_Parity_Ops is in review. After reaching agreement on the content of feature/CPP_Parity_Ops, @Jiya-Rathi will merge the content of feature/CPP_Parity_Ops_New* to feature/CPP_Parity_Ops, one operator a time.
  3. After completing the review of the C++ updates for the three mimetic operators, this Draft-PR will be converted to a PR for the final merge of feature/CPP_Parity_Ops to main.

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.

Bring C++ to parity with MATLAB/Octave: gradient, divergence, laplacian

3 participants