Skip to content

Update pSA calculation#194

Closed
lispandfound wants to merge 53 commits intomasterfrom
parallel_psa
Closed

Update pSA calculation#194
lispandfound wants to merge 53 commits intomasterfrom
parallel_psa

Conversation

@lispandfound
Copy link
Contributor

@lispandfound lispandfound commented Dec 4, 2025

This PR introduces a new highly accelerated rust pSA + rotd solver which computes the pSA results in a few minutes of tens of thousands of stations and hundreds of thousands of timesteps on a 10 year desktop. I am hoping this is going to be fast enough to redo the alpine simulations pSA, after the mysterious slowdown that occurred on RCH recently turned out to be related to Python memory and CPU bottlenecks. There are few technical improvements to the quality of the pSA results besides the speed:

  1. pSA calculations are done in 64-bit floating point instead of 32-bit floating point. I'm actually pretty convinced we should port all our IM calculations to 64-bit now but that is a separate discussion.
  2. The Newmark-Beta method equations are now rearranged slightly differently to before. The goal is better pSA solutions by avoiding something called catastrophic cancellation and its propagating effects through the integration of estimated acceleration to estimated displacement.
  3. The Newmark-Beta solver now intelligently swaps to the linear instead of constant acceleration approximation when stability analysis indicates that the linear approximation can be used. The linear approximation offers theoretically better pSA calculation especially at larger timesteps.

Obviously, steps 2 and 3 required really careful work on the newmark beta solver. Numerical solvers are notoriously dangerous guns to play with due to the complex equations that go into them.

Note

High line count is due to more CSV benchmark tests, and some lock files for CI reproducibility.

@lispandfound lispandfound marked this pull request as draft December 4, 2025 03:58
@gemini-code-assist
Copy link

Summary of Changes

Hello @lispandfound, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request delivers a substantial upgrade to the pseudo-spectral acceleration (pSA) calculation pipeline by integrating a new, highly optimized Rust solver. The primary goal is to overcome previous Python memory and CPU bottlenecks, enabling much faster processing of large-scale seismic simulations. Beyond speed, the changes enhance the numerical accuracy and stability of pSA results through the adoption of 64-bit floating-point precision and sophisticated refinements to the Newmark-Beta method, including a new adaptive approach for selecting the most appropriate acceleration approximation.

Highlights

  • Performance Enhancement: Introduced a new, highly accelerated Rust-based pSA + RotD solver, significantly reducing computation time for large datasets (tens of thousands of stations, hundreds of thousands of timesteps).
  • Numerical Precision Upgrade: pSA calculations now utilize 64-bit floating-point arithmetic instead of 32-bit, improving the accuracy of results.
  • Improved Numerical Stability: The Newmark-Beta method equations have been rearranged to prevent 'catastrophic cancellation' and its propagating effects, leading to more robust integration of estimated acceleration to displacement.
  • Adaptive Newmark-Beta Solver: The Newmark-Beta solver now intelligently switches between linear and constant acceleration approximations based on stability analysis, offering theoretically better pSA calculation, especially at larger timesteps.
Ignored Files
  • Ignored by pattern: .github/workflows/** (5)
    • .github/workflows/deptry.yml
    • .github/workflows/lint.yml
    • .github/workflows/pytest.yml
    • .github/workflows/ruff.yml
    • .github/workflows/types.yml
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request is a great initiative to improve performance by migrating the pSA solver to a Rust extension. The new Rust code is well-structured, thoroughly tested, and includes thoughtful improvements to the numerical method. The Python integration is also mostly correct. However, I've identified a couple of critical bugs in the Python glue code that will lead to incorrect calculations. I've also included some suggestions to improve robustness and clarity in both the application and test code.

Comment on lines 377 to 381
data[im].values, abs=5e-4, rel=0.01, nan_ok=True
), (
f"Results for {im} do not match!\n{result}"
) # 5e-6 implies rounding to five decimal places

Choose a reason for hiding this comment

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

medium

The comment on line 381 is inconsistent with the tolerance used in the assertion. The comment mentions 5e-6 for five decimal places, but the pytest.approx call uses abs=5e-4 and rel=0.01. This discrepancy can be misleading for anyone maintaining these tests in the future. Please update the comment to reflect the actual tolerances being used.

        )  # Tolerances are looser due to solver changes

Comment on lines 405 to 409
data[im].values, abs=5e-4, rel=0.01, nan_ok=True
), (
f"Results for {im} do not match!\n{result}"
) # 5e-6 implies rounding to five decimal places

Choose a reason for hiding this comment

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

medium

The comment on line 409 regarding the tolerance (5e-6) does not match the actual values used in the pytest.approx assertion (abs=5e-4, rel=0.01). To avoid confusion, please update the comment to align with the code.

        )  # Tolerances are looser due to solver changes

@lispandfound
Copy link
Contributor Author

/gemini review

@gemini-code-assist
Copy link

Note

The number of changes in this pull request is too large for Gemini Code Assist to generate a review.

Copy link
Contributor

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

Copilot reviewed 27 out of 43 changed files in this pull request and generated 2 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@@ -1,3 +1,5 @@
"""ASCII Waveform reading code."""
Copy link

Copilot AI Jan 8, 2026

Choose a reason for hiding this comment

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

The comment on line 1 appears to be a module docstring. In Python, module-level docstrings should be the first statement in the file (before any imports), not a comment. This should be changed from a comment to a proper docstring using triple quotes.

Copilot uses AI. Check for mistakes.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Huh?

lispandfound and others added 2 commits January 8, 2026 14:56
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@lispandfound
Copy link
Contributor Author

@joelridden I just need fresh benchmark files to update the tests to pass.

@joelridden joelridden closed this Jan 19, 2026
@joelridden joelridden deleted the parallel_psa branch January 19, 2026 01:31
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.

3 participants