Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

suggestive constexpr tuple validNbrParticles #972

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

PhilipDeegan
Copy link
Member

getting the tuple/set of possible refined particle count per dim/interp for @UCaromel mostly

Copy link

coderabbitai bot commented Mar 6, 2025

📝 Walkthrough

Walkthrough

This pull request expands the testing framework and enhances utility functionality. A new subdirectory is added in the CMake configuration so that tests in the meta utilities module are built. In the meta utilities header, several functions are introduced for working with simulator configurations and a minor adjustment in const qualifier ordering is made. The types header is updated to accept extra arguments in the template function for_N and refines a pointer’s const annotation in get_env. Additionally, a dedicated test project (with new CMakeLists.txt and unit test source) is added to validate the new meta utilities functionality.

Changes

File(s) Changes
res/cmake/test.cmake Added a line: add_subdirectory(tests/core/utilities/meta) to include the new testing module.
src/core/utilities/meta/meta_utilities.hpp Updated the dummy struct’s const qualifier and added new functions: n_possibleSimulators(), two overloads of validNbrParticlesFor(), and simulatorTupleIndex(). Also simplified type deduction in makeAtRuntime().
src/core/utilities/types.hpp Changed pointer type in get_env from const char* to char const* and updated for_N template functions to accept variadic arguments, passing them through to the callable.
tests/core/utilities/meta/CMakeLists.txt
tests/core/utilities/meta/test_meta_utilities.cpp
Introduced a new test project (test-meta-utilities) that includes configuration for Google Test, creates an executable target, and adds unit tests (e.g., testvalidNbrParticlesFor) for the meta utilities module.

Sequence Diagram(s)

sequenceDiagram
    participant TR as Test Runner
    participant FT as MetaUtilitiesTest
    participant MU as Meta Utilities (validNbrParticlesFor)

    TR->>FT: Start test "testvalidNbrParticlesFor"
    FT->>MU: Invoke validNbrParticlesFor with 2D/2 interpolation parameters
    MU-->>FT: Return computed number of valid particles
    FT->>TR: Assert returned value equals expected (5)
Loading
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@@ -0,0 +1,30 @@
#include <string>
Copy link
Member Author

Choose a reason for hiding this comment

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

torm (copy paste)

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (4)
tests/core/utilities/meta/test_meta_utilities.cpp (2)

11-13: Consider enhancing the test fixture.

The test fixture MetaUtilitiesTest is declared but doesn't contain any setup or teardown code. Either add common setup/teardown operations or use TEST directly without a fixture if no shared context is needed.


15-23: Test implementation looks good, consider adding more test cases.

The test correctly validates the validNbrParticlesFor function for a 2D space with 2 interpolation points. However, it would be valuable to add more test cases with different dimensions and interpolation points to ensure comprehensive coverage.

TEST(MetaUtilitiesTest, testvalidNbrParticlesFor)
{
    std::size_t constexpr static dim    = 2;
    std::size_t constexpr static interp = 2;

    auto constexpr validNbrParticlesTuple = validNbrParticlesFor<dim, interp>();
    auto constexpr n_validNbrParticles    = std::tuple_size_v<decltype(validNbrParticlesTuple)>;
    EXPECT_EQ(n_validNbrParticles, 5);
+
+    // Test cases for different dimensions and interpolation points
+    auto constexpr validNbrParticlesTuple1D = validNbrParticlesFor<1, interp>();
+    auto constexpr n_validNbrParticles1D = std::tuple_size_v<decltype(validNbrParticlesTuple1D)>;
+    EXPECT_GT(n_validNbrParticles1D, 0);
+
+    auto constexpr validNbrParticlesTuple3D = validNbrParticlesFor<3, interp>();
+    auto constexpr n_validNbrParticles3D = std::tuple_size_v<decltype(validNbrParticlesTuple3D)>;
+    EXPECT_GT(n_validNbrParticles3D, 0);
}
src/core/utilities/meta/meta_utilities.hpp (2)

91-96: Consider returning std::array instead of tuple for easier iteration

The function correctly extracts NbRefinedParts from the simulator tuple. However, returning std::array instead of tuple would allow easier iteration over the valid particle numbers at runtime.

template<typename Dimension, typename InterpOrder, typename... NbRefinedParts>
auto static constexpr validNbrParticlesFor(
    std::tuple<Dimension, InterpOrder, NbRefinedParts...> const&)
{
-    return std::tuple<NbRefinedParts...>{};
+    return std::array<size_t, sizeof...(NbRefinedParts)>{NbRefinedParts::value...};
}

117-122: Add static_assert to validate template parameters

The function provides a convenient way to retrieve valid particle numbers, but it should validate the dim and interp parameters at compile time to provide better error messages.

template<std::size_t dim, std::size_t interp>
auto static constexpr validNbrParticlesFor()
{
+    static_assert(dim >= 1 && dim <= 3, "Dimension must be between 1 and 3");
+    static_assert(interp >= 1 && interp <= 3, "Interpolation order must be between 1 and 3");
    return validNbrParticlesFor(
        std::get<simulatorTupleIndex<dim, interp>()>(possibleSimulators()));
}
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between fdc768d and 4dac8d1.

📒 Files selected for processing (5)
  • res/cmake/test.cmake (1 hunks)
  • src/core/utilities/meta/meta_utilities.hpp (5 hunks)
  • src/core/utilities/types.hpp (3 hunks)
  • tests/core/utilities/meta/CMakeLists.txt (1 hunks)
  • tests/core/utilities/meta/test_meta_utilities.cpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.hpp`: Review the C++ code, point out issues relative t...

**/*.hpp: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

  • src/core/utilities/meta/meta_utilities.hpp
  • src/core/utilities/types.hpp
⏰ Context from checks skipped due to timeout of 90000ms (5)
  • GitHub Check: build (macos-latest)
  • GitHub Check: build (macos-14)
  • GitHub Check: Analyze (cpp)
  • GitHub Check: build (ubuntu-latest)
  • GitHub Check: build (macos-13)
🔇 Additional comments (13)
res/cmake/test.cmake (1)

24-24: Addition of meta utilities tests looks good.

The new test directory for meta utilities is properly added to the CMake configuration, following the same pattern as the other test directories.

tests/core/utilities/meta/test_meta_utilities.cpp (2)

1-10: Appropriate includes and namespace usage.

The includes are properly organized - standard library headers first, followed by project-specific headers, and then testing framework headers. The using directive for the PHARE::core namespace makes the code more readable.


25-30: Main function implementation is correct.

The main function correctly initializes Google Test and runs all tests.

tests/core/utilities/meta/CMakeLists.txt (1)

1-18: CMake configuration looks good.

The CMake configuration properly sets up the project, adds the executable with the correct source file, includes Google Test, and links against the required libraries. The test is configured correctly with add_no_mpi_phare_test.

src/core/utilities/types.hpp (6)

229-229: Consistent const ordering in pointer type.

Changing from const char* to char const* follows a more consistent pattern for const qualification in the codebase, with the const qualifier placed to the right of the type. Both are semantically identical, but this change improves consistency.


427-429: Enhanced flexibility with variadic template parameters.

The function template has been improved to accept additional arguments that will be forwarded to the callable function. This change enhances the flexibility and reusability of the for_N function.


437-440: Updated return type calculation to account for variadic arguments.

The return type calculation has been correctly updated to include the variadic arguments in the function invocation. This ensures proper type deduction when the function is called with additional arguments.


446-452: Forward additional arguments in tuple creation.

The implementation correctly passes the additional arguments to the callable function in all three return modes (make_tuple, make_array, forward_tuple). This ensures consistent behavior across all return types.


458-458: Forward arguments in non-returning function call.

The implementation correctly passes the additional arguments to the callable function when it doesn't return a value. This ensures consistent behavior between returning and non-returning callables.


461-464: Updated second overload for consistency.

The second overload of for_N has been properly updated to match the first overload's signature, accepting and forwarding variadic template parameters. This ensures both overloads provide the same functionality with different callable types.

src/core/utilities/meta/meta_utilities.hpp (3)

21-21: Good use of conventional const placement

Changing from static const type value = 0; to static type const value = 0; follows the C++ convention of placing const after the type for constants, making the code more idiomatic.


86-89: LGTM!

This well-crafted utility function leverages std::tuple_size_v to determine the number of possible simulators at compile time. The constexpr specifier ensures compile-time evaluation.


132-133: Good use of type alias template

Using std::tuple_element_t<0, SimuType> instead of typename std::tuple_element<0, SimuType>::type improves code readability while maintaining identical functionality.

@PHAREHUB PHAREHUB deleted a comment from coderabbitai bot Mar 6, 2025
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn>
constexpr auto for_N(Fn& fn)
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn, typename... Args>
constexpr auto for_N(Fn& fn, Args&&... args)

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable args is never read.
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn>
constexpr auto for_N(Fn& fn)
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn, typename... Args>
constexpr auto for_N(Fn& fn, Args&&... args)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn>
constexpr auto for_N(Fn&& fn)
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn, typename... Args>
constexpr auto for_N(Fn&& fn, Args&&... args)

Check notice

Code scanning / CodeQL

Unused static variable Note

Static variable args is never read.
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn>
constexpr auto for_N(Fn&& fn)
template<std::uint16_t N, auto M = for_N_R_mode::make_tuple, typename Fn, typename... Args>
constexpr auto for_N(Fn&& fn, Args&&... args)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable args is not used.

TEST(MetaUtilitiesTest, testvalidNbrParticlesFor)
{
std::size_t constexpr static dim = 2;

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable dim is not used.
TEST(MetaUtilitiesTest, testvalidNbrParticlesFor)
{
std::size_t constexpr static dim = 2;
std::size_t constexpr static interp = 2;

Check notice

Code scanning / CodeQL

Unused local variable Note test

Variable interp is not used.
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