Skip to content

preparing ground for electromag diagnostics for mhd #999

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

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

Conversation

UCaromel
Copy link
Contributor

Changed ElectromagDiagnosticWriter to not rely on the vecfield names as keys to work with strings from python. Also implemented separate getters for E and B, which were previously given in a single vector for iteration purposes.

Copy link

coderabbitai bot commented Apr 15, 2025

📝 Walkthrough

Walkthrough

The changes refactor how electromagnetic diagnostic fields are handled by replacing generic iteration over a collection of fields with explicit handling of the "EM_B" and "EM_E" fields. A new helper method is introduced to check if a diagnostic is active for a given field. The model view interface is updated by removing a method that returns all electromagnetic fields and adding separate accessors for the B and E fields. All relevant logic in the diagnostic writer is updated to use these explicit accessors and conditional checks.

Changes

File(s) Change Summary
src/diagnostic/detail/types/electromag.hpp Refactored to replace iteration over electromagnetic fields with explicit handling of "EM_B" and "EM_E". Introduced a private helper method isActiveDiag to check diagnostic activation. Updated methods (createFiles, getDataSetInfo, initDataSets, write) to use explicit field access and conditional logic.
src/diagnostic/diagnostic_model_view.hpp Removed getElectromagFields() method returning a vector of electromagnetic field pointers. Added getB() and getE() methods returning references to the respective fields. Updated public interface accordingly.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant ElectromagDiagnosticWriter
    participant ModelView
    participant VecField_B
    participant VecField_E

    User->>ElectromagDiagnosticWriter: Request diagnostic operation
    ElectromagDiagnosticWriter->>ElectromagDiagnosticWriter: isActiveDiag("EM_B")
    alt if "EM_B" active
        ElectromagDiagnosticWriter->>ModelView: getB()
        ModelView-->>ElectromagDiagnosticWriter: VecField_B
        ElectromagDiagnosticWriter->>VecField_B: Process B field
    end
    ElectromagDiagnosticWriter->>ElectromagDiagnosticWriter: isActiveDiag("EM_E")
    alt if "EM_E" active
        ElectromagDiagnosticWriter->>ModelView: getE()
        ModelView-->>ElectromagDiagnosticWriter: VecField_E
        ElectromagDiagnosticWriter->>VecField_E: Process E field
    end
Loading

Tip

⚡💬 Agentic Chat (Pro Plan, General Availability)
  • We're introducing multi-step agentic chat in review comments and issue comments, within and outside of PR's. This feature enhances review and issue discussions with the CodeRabbit agentic chat by enabling advanced interactions, including the ability to create pull requests directly from comments and add commits to existing pull requests.
✨ 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.

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 (3)
src/diagnostic/detail/types/electromag.hpp (3)

52-56: Simplify isActiveDiag implementation

The isActiveDiag method is a good refactoring that extracts a common check, but the parameter naming could be improved for clarity.

Consider this improved implementation:

-    auto isActiveDiag(DiagnosticProperties const& diagnostic, std::string const& tree,
-                      std::string var)
+    auto isActiveDiag(DiagnosticProperties const& diagnostic, std::string const& prefix,
+                      std::string const& fieldName)
     {
-        return diagnostic.quantity == tree + var;
+        return diagnostic.quantity == prefix + fieldName;
     };

This clarifies the purpose of each parameter and makes the code more self-documenting.


61-65: Simplify createFiles method

The current implementation creates a local variable tree that's only used once. This could be simplified further.

 void ElectromagDiagnosticWriter<H5Writer>::createFiles(DiagnosticProperties& diagnostic)
 {
-    std::string tree = "/";
-    checkCreateFileFor_(diagnostic, fileData_, tree, "EM_B", "EM_E");
+    checkCreateFileFor_(diagnostic, fileData_, "/", "EM_B", "EM_E");
 }

168-180: Refine the use of tree variable

The tree variable is created but only used within the isActiveDiag calls. This pattern is repeated in several methods and could be simplified.

-    std::string tree = "/";
     std::string path = h5Writer.patchPath() + "/";

-    if (isActiveDiag(diagnostic, tree, "EM_B"))
+    if (isActiveDiag(diagnostic, "/", "EM_B"))
     {
         auto& B = h5Writer.modelView().getB();
         h5Writer.writeTensorFieldAsDataset(h5file, path + "EM_B", B);
     }
-    if (isActiveDiag(diagnostic, tree, "EM_E"))
+    if (isActiveDiag(diagnostic, "/", "EM_E"))
     {
         auto& E = h5Writer.modelView().getE();
         h5Writer.writeTensorFieldAsDataset(h5file, path + "EM_E", E);
     }

Alternatively, if this prefix is likely to change or be used in multiple places, consider defining it as a class constant.

📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 5e2ada6 and b16f851.

📒 Files selected for processing (2)
  • src/diagnostic/detail/types/electromag.hpp (5 hunks)
  • src/diagnostic/diagnostic_model_view.hpp (1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
`**/*.hpp`: Review the C++ code, point out issues relative to principles of clean code, expressiveness, and performance.

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

  • src/diagnostic/diagnostic_model_view.hpp
  • src/diagnostic/detail/types/electromag.hpp
🧬 Code Graph Analysis (1)
src/diagnostic/diagnostic_model_view.hpp (2)
src/core/data/ions/ion_population/ion_population.hpp (2)
  • VecField (86-86)
  • VecField (87-87)
src/core/data/electrons/electrons.hpp (5)
  • VecField (91-102)
  • VecField (274-274)
  • VecField (276-276)
  • VecField (350-350)
  • VecField (355-355)
⏰ Context from checks skipped due to timeout of 90000ms (7)
  • GitHub Check: build (ubuntu-latest, gcc)
  • GitHub Check: build (ubuntu-latest, clang)
  • GitHub Check: build (macos-latest)
  • GitHub Check: build (macos-14)
  • GitHub Check: build (macos-13)
  • GitHub Check: Analyze (cpp)
  • GitHub Check: Analyze (python)
🔇 Additional comments (2)
src/diagnostic/detail/types/electromag.hpp (2)

93-102: LGTM! Good refactoring of field access

The refactoring to check for specific diagnostic field types and retrieve them individually through dedicated accessors makes the code more explicit and easier to understand.


143-154: LGTM! Consistent pattern for field initialization

The code consistently applies the same pattern of checking if a diagnostic is active for a specific field and then initializing it. This makes the control flow more explicit and easier to follow.

Comment on lines +49 to +51
NO_DISCARD VecField& getB() const { return model_.state.electromag.B; }

NO_DISCARD VecField& getE() const { return model_.state.electromag.E; }
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix const-correctness issues in getB() and getE() methods

The current implementation of both methods violates const-correctness principles by returning non-const references from const methods. This can lead to unexpected behavior where supposedly immutable objects can be modified.

Apply this diff to fix the const-correctness:

-    NO_DISCARD VecField& getB() const { return model_.state.electromag.B; }
+    NO_DISCARD VecField const& getB() const { return model_.state.electromag.B; }
+    NO_DISCARD VecField& getB() { return model_.state.electromag.B; }

-    NO_DISCARD VecField& getE() const { return model_.state.electromag.E; }
+    NO_DISCARD VecField const& getE() const { return model_.state.electromag.E; }
+    NO_DISCARD VecField& getE() { return model_.state.electromag.E; }

This follows the pattern seen elsewhere in the codebase for accessor methods, providing both const and non-const versions that return const and non-const references respectively.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
NO_DISCARD VecField& getB() const { return model_.state.electromag.B; }
NO_DISCARD VecField& getE() const { return model_.state.electromag.E; }
NO_DISCARD VecField const& getB() const { return model_.state.electromag.B; }
NO_DISCARD VecField& getB() { return model_.state.electromag.B; }
NO_DISCARD VecField const& getE() const { return model_.state.electromag.E; }
NO_DISCARD VecField& getE() { return model_.state.electromag.E; }


if (isActiveDiag(diagnostic, tree, "EM_B"))
{
auto& B = h5Writer.modelView().getB();

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable B is not used.
auto& name = vecField->name();
if (diagnostic.quantity == "/" + name)
initVF(path, attr, name, null);
auto& E = h5Writer.modelView().getE();

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable E is not used.
};


template<typename H5Writer>
void ElectromagDiagnosticWriter<H5Writer>::createFiles(DiagnosticProperties& diagnostic)
{
for (auto* vecField : this->h5Writer_.modelView().getElectromagFields())
checkCreateFileFor_(diagnostic, fileData_, "/", vecField->name());
std::string tree = "/";
Copy link
Member

Choose a reason for hiding this comment

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

const

h5Writer.writeTensorFieldAsDataset(*fileData_.at(diagnostic.quantity),
h5Writer.patchPath() + "/" + vecField->name(),
*vecField);
std::string tree = "/";
Copy link
Member

Choose a reason for hiding this comment

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

consts

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