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

Correct Formula for Potential Energy #2227

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

DanielDoehring
Copy link
Contributor

The formula for the (gravitational) potential energy from the Euler-Gravity paper is wrong:

Screenshot from 2025-01-09 18-31-12

and should contain a factor of 0.5 according to the book "Galactic Dynamics"

Screenshot from 2025-01-10 09-10-27

The reason why this was not spotted is that the analytical solution in the reproducibility repository is only read from file, not computed from the formula for the analytical solution (see e.g. FLASH user guide, equation (34.35))

$$E_\text{pot} = -\frac{\pi G (\rho_0 \delta_0 L)^2}{2 k^2} (1 + \cos(2 \omega t) )$$

Using this code

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.ticker import ScalarFormatter

df_Jeans = pd.read_csv('./analysis.dat', delim_whitespace=True, skiprows=1, header=None)

RWTH_Blue_RGB   = [(0, 84/256, 159/256)]

fig, ax = plt.subplots()

# .dat File Structure:
#timestep time dt e_total e_kinetic e_internal e_potential 

# Parameters as found in elixir
rho0 = 1.5e7
p0 = 1.5e7
gamma = 5/3
c0 = np.sqrt(gamma * p0 / rho0)
G = 6.674e-8
delta0 = 1e-3
L = 1.0 # Domain length

k = 4 * np.pi

omega_squared = (c0 * k)**2 - 4 * np.pi * G * rho0
omega = np.sqrt(omega_squared)

# Skip first entry (wrong result for naive initialization which does not fulfill the Poisson equation)
time = df_Jeans[1][1:-1]

E_potential_analytical = - np.pi * G / 2 * (rho0 * delta0 * L / k)**2 * (1 + np.cos(2 * omega * time))

#plt.plot(time, df_Jeans[6][1:-1], color = RWTH_Blue_RGB[0], label = "Numerical Approximation")
plt.scatter(time, df_Jeans[6][1:-1], color = RWTH_Blue_RGB[0], label = "Numerical Approximation")

plt.plot(time, E_potential_analytical, color = 'k', label = "Analytical Solution")

plt.title("Potential Energy")

formatter = ScalarFormatter(useMathText=True)
formatter.set_scientific(True)
ax.xaxis.set_major_formatter(formatter)
ax.yaxis.set_major_formatter(formatter)

plt.legend(loc = 'lower left')

ax.grid(axis ='y', which='both', alpha=0.1, linewidth = 1.5, color ='black')
ax.grid(axis ='x', which='both', alpha=0.1, linewidth = 1.5, color ='black')
ax.set_axisbelow(True)  # Hide grid 


### Crop plot window ###
ax.set_xlim([3, 5])

ax.set_xlabel(r'$t$')

plt.tight_layout()  # Title, labels, ... to screen

plt.show()

one obtains the correct plot:

Screenshot from 2025-01-10 11-55-08

@DanielDoehring DanielDoehring added the bug Something isn't working label Jan 10, 2025
Copy link
Contributor

Review checklist

This checklist is meant to assist creators of PRs (to let them know what reviewers will typically look for) and reviewers (to guide them in a structured review process). Items do not need to be checked explicitly for a PR to be eligible for merging.

Purpose and scope

  • The PR has a single goal that is clear from the PR title and/or description.
  • All code changes represent a single set of modifications that logically belong together.
  • No more than 500 lines of code are changed or there is no obvious way to split the PR into multiple PRs.

Code quality

  • The code can be understood easily.
  • Newly introduced names for variables etc. are self-descriptive and consistent with existing naming conventions.
  • There are no redundancies that can be removed by simple modularization/refactoring.
  • There are no leftover debug statements or commented code sections.
  • The code adheres to our conventions and style guide, and to the Julia guidelines.

Documentation

  • New functions and types are documented with a docstring or top-level comment.
  • Relevant publications are referenced in docstrings (see example for formatting).
  • Inline comments are used to document longer or unusual code sections.
  • Comments describe intent ("why?") and not just functionality ("what?").
  • If the PR introduces a significant change or new feature, it is documented in NEWS.md with its PR number.

Testing

  • The PR passes all tests.
  • New or modified lines of code are covered by tests.
  • New or modified tests run in less then 10 seconds.

Performance

  • There are no type instabilities or memory allocations in performance-critical parts.
  • If the PR intent is to improve performance, before/after time measurements are posted in the PR.

Verification

  • The correctness of the code was verified using appropriate tests.
  • If new equations/methods are added, a convergence test has been run and the results
    are posted in the PR.

Created with ❤️ by the Trixi.jl community.

Copy link
Member

@andrewwinters5000 andrewwinters5000 left a comment

Choose a reason for hiding this comment

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

Thanks for catching this!

…ty_jeans_instability.jl

Co-authored-by: Hendrik Ranocha <[email protected]>
Copy link

codecov bot commented Jan 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 83.95%. Comparing base (8592c0e) to head (a52a659).

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2227      +/-   ##
==========================================
+ Coverage   77.62%   83.95%   +6.34%     
==========================================
  Files         486      486              
  Lines       39107    39118      +11     
==========================================
+ Hits        30353    32840    +2487     
+ Misses       8754     6278    -2476     
Flag Coverage Δ
unittests 83.95% <100.00%> (+6.34%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants