dtamayo / reboundx

A library for adding additional forces to the REBOUND N-body integration package
GNU General Public License v3.0
80 stars 60 forks source link

Getting warning/error with additional forces #126

Closed Bruntonian closed 2 months ago

Bruntonian commented 3 months ago

I had to reinstall my reboundx (via pip install -e .) after editing an equation. Prior to this edit, everything was running fine and I am certain that my edit did not alter anything significant.

However, since I did that, I'm now getting this warning and the simulation is not running:

Warning (from warnings module): File "/Users/ian/reboundian/rebound/rebound/simulation.py", line 254 warnings.warn(msg[1:], RuntimeWarning) RuntimeWarning: REBOUNDx overwrites sim->additional_forces, sim->pre_timestep_modifications and sim->post_timestep_modifications whenever forces or operators that use them get added. If you want to use REBOUNDx together with your own custom functions that use these callbacks, you should add them through REBOUNDx. See https://github.com/dtamayo/reboundx/blob/master/ipython_examples/Custom_Effects.ipynb for a tutorial.

Warning (from warnings module): File "/Users/ian/reboundian/rebound/rebound/simulation.py", line 254 warnings.warn(msg[1:], RuntimeWarning) RuntimeWarning: REBOUNDx Warning: additional_forces was set and is being overwritten by REBOUNDx. To incorporate both, you can add your own custom effects through REBOUNDx. See https://github.com/dtamayo/reboundx/blob/master/ipython_examples/Custom_Effects.ipynb for a tutorial.

Here is the relevant part of the code:

import reboundx
import numpy as np
import matplotlib.pyplot as plt

# Constants
customdist = 5e11 # [cm] 

Nmoons = 2
Msol = 2E33
M_1 = 10E26/Msol # [g]
R_1 = 2E8/customdist # [cm]
M_2 = 3E21/Msol
R_2 = 1E7/customdist

# Set parameters
dens1 = 400
Sigma_0 = dens1 * (customdist**2)/(Msol)

h_r = 0.05
beta = 0
surfexp = 3/2

trunc = 5E-2
ideWidth = 1E-5

# Set integration time
tmax = 4000

# Set initial starting locations
b2start = 0.85

Nout = 100000
times = np.linspace(0., tmax, Nout)

# Initialize sim
sim = rebound.Simulation()
sim.units = ('yr', 'customdist', 'Msol')
sim.add(m=1)

# Adding Particles with varying initial starting locations
sim.add(m=M_2, r=R_2, a=b2start, e=1E-5)#, inc=1E-5)  
sim.add(m=M_1, r=R_1, a=1., e=1E-6)#, inc=1E-6)  

sim.move_to_com()
ps = sim.particles

# Extras for Type I Migration
rebx = reboundx.Extras(sim)
mig = rebx.load_force("type_I_migration")
rebx.add_force(mig)

etc. 

The thing was working fine just last week, any advice would be greatly appreciated. Can offer more info if needed.

Python 3.12.2 macOS 14.4.1

Update/Edit: I also tried completely uninstalling, then reinstalling without adding in any of my custom effects, and I am still getting the same error when I attempt to add Type I Migration.

Thank you, -Ian

dtamayo commented 3 months ago

Hi Ian, sorry this fell through the cracks. Are you at DDA? If so feel free to find me anytime. I think I might be able to help faster if we could try things out together on your laptop.

Bruntonian commented 3 months ago

No worries at all, that sounds great, I'll try to find you today.

tamidodo commented 2 months ago

Hello,

I am also running into this situation. I was running my code just fine on my local environment and then when I created a new environment and installed the packages on that computer, my simulations stop running when I try to add radiation forces but run just fine if I take that part out. Same error message, it complains that I "need to attach reboundx.Extras instance to simulation before setting params" even though I definitely have. How do you resolve this?

Thanks, Tammy

dtamayo commented 2 months ago

Hi Tammy,

Sorry, we sorted this out in person and forgot to close the issue, but it would be good to have a solution here. Could you try making a new environment and doing

pip install reboundx --no-binary :all: --no-cache-dir

tamidodo commented 2 months ago

I tried doing that, still getting the same error. My environment is Python 3.9.7, rebound version 4.3.2, reboundx version 4.1.0. This is to match my local environment which is successfully running simulations. The code for adding the extra forces is below and the error I'm getting is "AttributeError: Need to attach reboundx.Extras instance to simulation before setting params." which makes no sense because I clearly have...

# Add radiation forces
#print("adding radiation forces")
rebx = reboundx.Extras(sim)
rf = rebx.load_force("radiation_forces")
rebx.add_force(rf)
rf.params["c"] = rbxConstants.C
Q_pr = 1  # valid for meteoroid size >> wavelength of radiation
# meteoroid radius should be in m, density should be in kg/m^3
ps["Meteoroid"].params["beta"] = rebx.rad_calc_beta(
    6.6743e-11, 3.0e8, msun, lum_sol, r, rho, Q_pr
)
dtamayo commented 2 months ago

Hm, I just ran the command above with python 3.9.7 and I can't reproduce the error on my macbook pro. What commands did you run? Did you make a new environment? You didn't seem to install the latest versions of rebound and reboundx. Do you need those specific ones?

tamidodo commented 2 months ago

I did make a new environment. The problem I have is when I install the latest versions for rebound/reboundx, I don't hit the "need to attach reboundx.Extras" error but it doesn't run my simulation (gets stuck on the first sim.integrate(time) run, doesn't throw error but gets held up forever) unless I remove the lines where I've added the radiation forces, in which case it runs just fine. The versions I mentioned earlier are the ones I have in the environment locally where everything works fine so I've been trying to duplicate that environment on this new computer.

The commands I ran are below:

conda create -n tamayo-test python==3.9.7 conda activate tamayo-test (tamayo-test)$ pip install rebound==4.3.2 Collecting rebound==4.3.2 Downloading rebound-4.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (10 kB) Downloading rebound-4.3.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (744 kB) Installing collected packages: rebound Successfully installed rebound-4.3.2 (tamayo-test)$ pip install reboundx --no-binary :all: --no-cache-dir Collecting reboundx Downloading reboundx-4.1.0.tar.gz (91 kB) Installing build dependencies ... done Getting requirements to build wheel ... done Preparing metadata (pyproject.toml) ... done Requirement already satisfied: rebound>=4.0.0 in /home/tdo47/.conda/envs/tamayo-test/lib/python3.9/site-packages (from reboundx) (4.3.2) Building wheels for collected packages: reboundx Building wheel for reboundx (pyproject.toml) ... done Created wheel for reboundx: filename=reboundx-4.1.0-cp39-cp39-linux_x86_64.whl size=79758 sha256=a44d0dcdec27bac26eb71264917b81297796d9d2b52c15ed78b2151e4d6603f5 Stored in directory: /tmp/pip-ephem-wheel-cache-_yxqjj8e/wheels/89/ea/22/1f8131ed9b9d9615287b303fb5a32e1bae2162630fbfa3d180 Successfully built reboundx Installing collected packages: reboundx Successfully installed reboundx-4.1.0 (tamayo-test)$ python test_sim.py Searching NASA Horizons for 'Sun'... Found: Sun (10) Searching NASA Horizons for 'Mercury'... Found: Mercury Barycenter (199) (chosen from query 'Mercury') Searching NASA Horizons for 'Venus'... Found: Venus Barycenter (299) (chosen from query 'Venus') Searching NASA Horizons for 'Earth'... Found: Earth-Moon Barycenter (3) (chosen from query 'Earth') Searching NASA Horizons for 'Luna'... Found: Moon (301) (chosen from query 'Luna') Searching NASA Horizons for 'Mars'... Found: Mars Barycenter (4) (chosen from query 'Mars') Searching NASA Horizons for 'Jupiter'... Found: Jupiter Barycenter (5) (chosen from query 'Jupiter') Searching NASA Horizons for 'Saturn'... Found: Saturn Barycenter (6) (chosen from query 'Saturn') Searching NASA Horizons for 'Uranus'... Found: Uranus Barycenter (7) (chosen from query 'Uranus') Searching NASA Horizons for 'Neptune'... Found: Neptune Barycenter (8) (chosen from query 'Neptune') Searching NASA Horizons for 'C/2014 Q2'... Found: Lovejoy (C/2014 Q2) /home/tdo47/.conda/envs/tamayo-test/lib/python3.9/site-packages/rebound/horizons.py:167: RuntimeWarning: Warning: Mass cannot be retrieved from NASA HORIZONS. Set to 0. warnings.warn("Warning: Mass cannot be retrieved from NASA HORIZONS. Set to 0.", RuntimeWarning) /home/tdo47/.conda/envs/tamayo-test/lib/python3.9/site-packages/rebound/simulation.py:254: RuntimeWarning: REBOUNDx overwrites sim->additional_forces, sim->pre_timestep_modifications and sim->post_timestep_modifications whenever forces or operators that use them get added. If you want to use REBOUNDx together with your own custom functions that use these callbacks, you should add them through REBOUNDx. See https://github.com/dtamayo/reboundx/blob/master/ipython_examples/Custom_Effects.ipynb for a tutorial. warnings.warn(msg[1:], RuntimeWarning) Traceback (most recent call last): File "/home/tdo47/msc-uwo/test_sim.py", line 13, in ps[3].params["J2"] = 1.0826157e-3 File "/home/tdo47/.conda/envs/tamayo-test/lib/python3.9/site-packages/reboundx/init.py", line 40, in params params = Params(self) File "/home/tdo47/.conda/envs/tamayo-test/lib/python3.9/site-packages/reboundx/params.py", line 25, in init raise AttributeError("Need to attach reboundx.Extras instance to simulation before setting params.") AttributeError: Need to attach reboundx.Extras instance to simulation before setting params.

And here is the code I used for the test simulation: test_sim.txt

dtamayo commented 2 months ago

Thanks for all the testing @tamidodo . I think the issue was compatibility issues with versions of REBOUND in the installation wheels. I think REBOUNDx 4.2.0 should resolve your issues. Could you let me know?

tamidodo commented 2 months ago

Brilliant, that worked perfectly. Thank you for your time on this!