Skip to content

docs: adding a PSD example #4115

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

Merged
merged 12 commits into from
Jul 22, 2025
Merged

docs: adding a PSD example #4115

merged 12 commits into from
Jul 22, 2025

Conversation

germa89
Copy link
Collaborator

@germa89 germa89 commented Jul 21, 2025

Description

As the title. Add a PSD analysis to the docs.

Issue linked

Close #3865

Checklist

Summary by Sourcery

Add support for response power spectral density retrieval in the gRPC interface, include corresponding tests and fixtures, and provide a new PSD example script based on VM203.

New Features:

  • Introduce rpsd wrapper in mapdl_grpc.py to return response PSD data as numpy arrays

Documentation:

  • Add a comprehensive PSD random vibration example (VM203) in the examples directory

Tests:

  • Add test_rpsd to validate the rpsd output and shape
  • Add psd_analysis fixture to initialize a PSD analysis using vm203 data

@Copilot Copilot AI review requested due to automatic review settings July 21, 2025 18:25
@germa89 germa89 requested a review from a team as a code owner July 21, 2025 18:25
@germa89 germa89 self-assigned this Jul 21, 2025
@ansys-reviewer-bot
Copy link
Contributor

Thanks for opening a Pull Request. If you want to perform a review write a comment saying:

@ansys-reviewer-bot review

Copy link
Contributor

sourcery-ai bot commented Jul 21, 2025

Reviewer's Guide

This PR implements a new rpsd wrapper in the gRPC client to return response power spectral density results as numpy arrays, adds unit tests and a fixture for PSD analysis, and contributes a complete PSD example script (VM203) for the documentation.

Class diagram for the new rpsd method in MapdlGrpc

classDiagram
    class MapdlBase {
        +rpsd(ir, ia, ib, itype, datum, name, signif, **kwargs)
    }
    class MapdlGrpc {
        +rpsd(ir, ia, ib, itype, datum, name, signif, **kwargs) NDArray[np.float64]
    }
    MapdlGrpc --|> MapdlBase
Loading

File-Level Changes

Change Details Files
Introduce rpsd wrapper method in the gRPC client
  • Add rpsd method decorated with wraps(MapdlBase.rpsd)
  • Call super().rpsd with all parameters
  • Return PSD result via self.vget("_temp", ir)
src/ansys/mapdl/core/mapdl_grpc.py
Add unit test and fixture for rpsd command
  • Implement test_rpsd to verify ndarray output and shape
  • Create psd_analysis fixture loading VM203 input via vmfiles
tests/test_mapdl.py
tests/conftest.py
Contribute PSD example script for VM203 plate analysis
  • Add full example demonstrating random vibration PSD workflow
  • Include model setup, solving, PSD result retrieval, and plotting
examples/00-mapdl-examples/psd-vm203.py

Assessment against linked issues

Issue Objective Addressed Explanation
#3865 Provide a documented example in the PyMAPDL documentation demonstrating how to perform a response PSD analysis and plot the results when applying a force PSD as input.
#3865 Ensure that the example covers the process of extracting and plotting the response PSD (e.g., velocity or displacement) for a specific node, including any necessary post-processing steps.

Possibly linked issues

  • #4752: The PR adds a comprehensive PSD analysis example to the docs, including how to plot response PSD, directly addressing the issue.

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds a Power Spectral Density (PSD) analysis example to the documentation, demonstrating random vibration analysis using PyMAPDL with ANSYS Verification Manual problem VM203.

  • Adds a comprehensive PSD analysis example with modal analysis, spectrum analysis, and post-processing
  • Implements wrapper function for the RPSD command to return numpy arrays
  • Includes test coverage for the new RPSD functionality

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
examples/00-mapdl-examples/psd-vm203.py New comprehensive example demonstrating PSD analysis workflow
src/ansys/mapdl/core/mapdl_grpc.py Adds rpsd method wrapper to return numpy arrays
tests/conftest.py Adds psd_analysis fixture using VM203 file
tests/test_mapdl.py Adds test for rpsd command functionality
Comments suppressed due to low confidence (1)

tests/conftest.py:1338

  • The fixture runs VM203 input but doesn't ensure the analysis completes successfully or that the required data for PSD analysis is available. Consider adding validation or error handling to ensure the fixture provides a valid state for PSD testing.
    mapdl.input(vmfiles["vm203"])

@github-actions github-actions bot added examples Publishing PyMAPDL examples documentation Documentation related (improving, adding, etc) labels Jul 21, 2025
Copy link
Contributor

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @germa89 - I've reviewed your changes - here's some feedback:

  • The new rpsd wrapper uses vget('_temp', ir) where ir is the input index rather than a variable name—consider clarifying or standardizing the parameter so vget always retrieves the intended result array.
  • The long example script executes on import; wrap the main execution block in an if name == 'main' guard to prevent unintended side effects when loading the module.
  • The psd_analysis fixture only inputs the example file without running the necessary modal/PSD steps, so tests could be flaky—add the commands to complete the PSD setup in the fixture to guarantee a ready state.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- The new rpsd wrapper uses vget('_temp', ir) where ir is the input index rather than a variable name—consider clarifying or standardizing the parameter so vget always retrieves the intended result array.
- The long example script executes on import; wrap the main execution block in an if __name__ == '__main__' guard to prevent unintended side effects when loading the module.
- The psd_analysis fixture only inputs the example file without running the necessary modal/PSD steps, so tests could be flaky—add the commands to complete the PSD setup in the fixture to guarantee a ready state.

## Individual Comments

### Comment 1
<location> `src/ansys/mapdl/core/mapdl_grpc.py:3316` </location>
<code_context>
+            signif=signif,
+            **kwargs,
+        )
+        return self.vget("_temp", ir)
+
     def get_nsol(
</code_context>

<issue_to_address>
Potential mismatch in vget argument for rpsd return value.

Please confirm that 'ir' is the intended argument for vget here, as using the wrong variable index could return incorrect data.
</issue_to_address>

### Comment 2
<location> `examples/00-mapdl-examples/psd-vm203.py:259` </location>
<code_context>
+
+###############################################################################
+# plot the response psd
+freqs = mapdl.vget("FREQS", 1)
+
+# Remove the last two values as they are zero
</code_context>

<issue_to_address>
Potential for off-by-one error when slicing frequency and RPSD arrays.

Verify that removing the last two elements from freqs and rpsduz is always valid, as changes in the data structure could cause array length mismatches or omit important data.
</issue_to_address>

<suggested_fix>
<<<<<<< SEARCH
# Remove the last two values as they are zero
plt.plot(freqs[:-2], rpsduz[:-2], label="RPSD UZ")
=======
# Remove the last two values as they are zero, but ensure array lengths match and are valid
if len(freqs) >= 2 and len(rpsduz) >= 2:
    freqs_trimmed = freqs[:-2]
    rpsduz_trimmed = rpsduz[:-2]
else:
    raise ValueError("Frequency and RPSD arrays must have at least two elements to trim.")

if len(freqs_trimmed) != len(rpsduz_trimmed):
    raise ValueError(
        f"Length mismatch after trimming: freqs ({len(freqs_trimmed)}), rpsduz ({len(rpsduz_trimmed)})"
    )

plt.plot(freqs_trimmed, rpsduz_trimmed, label="RPSD UZ")
>>>>>>> REPLACE

</suggested_fix>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

codecov bot commented Jul 21, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.30%. Comparing base (86fa0c0) to head (4cb8fb9).
Report is 6 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4115   +/-   ##
=======================================
  Coverage   91.30%   91.30%           
=======================================
  Files         189      189           
  Lines       15649    15650    +1     
=======================================
+ Hits        14289    14290    +1     
  Misses       1360     1360           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@github-actions github-actions bot added CI/CD Related with CICD, Github Actions, etc maintenance General maintenance of the repo (libraries, cicd, etc) labels Jul 22, 2025
@germa89
Copy link
Collaborator Author

germa89 commented Jul 22, 2025

@pyansys-ci-bot LGTM.

@germa89 germa89 enabled auto-merge (squash) July 22, 2025 10:45
Copy link
Contributor

@pyansys-ci-bot pyansys-ci-bot left a comment

Choose a reason for hiding this comment

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

✅ Approving this PR because germa89 said so in here 😬

LGTM

@germa89 germa89 merged commit 2168c7f into main Jul 22, 2025
48 checks passed
@germa89 germa89 deleted the docs/adding-PSD-example branch July 22, 2025 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CI/CD Related with CICD, Github Actions, etc documentation Documentation related (improving, adding, etc) examples Publishing PyMAPDL examples maintenance General maintenance of the repo (libraries, cicd, etc)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Re: How to plot a response PSD when applying a force PSD as input?
2 participants