Skip to content

Replace examples with those from the second edition of the AMUSE book#1227

Open
rieder wants to merge 28 commits intoamusecode:mainfrom
rieder:examples_book_v2
Open

Replace examples with those from the second edition of the AMUSE book#1227
rieder wants to merge 28 commits intoamusecode:mainfrom
rieder:examples_book_v2

Conversation

@rieder
Copy link
Member

@rieder rieder commented Mar 10, 2026

Existing examples are outdated, in some cases not working and often duplicated by others.
This replaces all examples from the "textbook", "syllabus" and "simple" directories with the complete set from the textbook (second edition).

@rieder
Copy link
Member Author

rieder commented Mar 10, 2026

To be done:

  • Some tests may use some of the old examples, so these need to be updated
  • Add installable package for the examples (they should be installable as "amuse-examples" via pip, at the least - living in the amuse.examples namespace, as this is how they are used in the book).
  • a file "AMUSE_updates.pdf" needs to be added, as a file containing any errata for the book.
  • missing / later conceived examples may be added later

@rieder
Copy link
Member Author

rieder commented Mar 10, 2026

If we do want to keep (some of) the old examples around, they should be in some directory that makes it clear that these are archived, and not updated. If they are still useful, they should be moved to the new structure.

@LourensVeen
Copy link
Member

I don't see how the tests would depend on the examples, but I'll go and do a local build and run them all to check.

I think pip packages, the PDF, and more examples should be separate Issues, so if it doesn't break the tests then I'll merge this, even if it isn't perfect I doubt it'll make things worse.

Copy link
Member

@LourensVeen LourensVeen left a comment

Choose a reason for hiding this comment

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

I've pushed a commit to move the examples to src/amuse_examples and add a forward in src/amuse/examples, and make the whole thing an installable package like the others.

Looking at it again, I think it's probably better to put the actual examples into a subdirectory src/amuse_examples/amuse_examples, so that there's nothing else in the directory. Then we can symlink that to /examples, and if anyone plays with it on the command line then they're less likely to mess up the package definitions.

I've run the tests and this didn't break anything, but then I ran the examples themselves and found many problems, which I've added comments for.

Copy link
Member

Choose a reason for hiding this comment

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

Crashes with

  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/2017arXiv170307029H_Fig3.py", line 48, in <module>
    from distinct_colours import get_distinct
ModuleNotFoundError: No module named 'distinct_colours'

Copy link
Member Author

Choose a reason for hiding this comment

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

that is a remnant, should have been removed from the example.

Copy link
Member

Choose a reason for hiding this comment

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

Ah, I actually found the file and put it in as part of my quick fixes. So that should come back out then.

Copy link
Member

Choose a reason for hiding this comment

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

Won't run using amuse.examples.run:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
    from amuse.examples import run ; run('supernova_grid')
                                     ~~~^^^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse_examples/__init__.py", line 55, in run
    example.main()
    ~~~~~~~~~~~~^^
TypeError: main() missing 7 required positional arguments: 'stellar_mass', 'stellar_radius', 'core_mass', 'core_radius', 't_end', 'dt', and 'resolution'

Copy link
Member

Choose a reason for hiding this comment

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

If I fix that, it crashes because we're trying to index grid.rho with something that isn't an int:

  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/examples/supernova_grid.py", line 17, in plot_grid
    rho = grid.rho[:,:,halfway].value_in(units.g/units.cm**3)
          ~~~~~~~~^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/units/quantities.py", line 666, in __getitem__
    return new_quantity(self._number[index], self.unit)
                        ~~~~~~~~~~~~^^^^^^^
IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices

Lines 16 and 17 look suspicious to me, because there's an ellipsis in the first place on line 16, then halfway is used in the last place on line 17. But I don't know what this does.

Copy link
Member

Choose a reason for hiding this comment

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

Fails because prepare_figure is missing.

@@ -0,0 +1,55 @@
from amuse.units import units
from amuse.community.seba import Seba
from seculartriple_tps import Seculartriple
Copy link
Member

Choose a reason for hiding this comment

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

seculartriple_tps is missing.

Copy link
Member Author

Choose a reason for hiding this comment

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

that is part of the TRES package (https://github.com/silviatoonen/tres), maybe fail with an error message if this is not installed

Copy link
Member

Choose a reason for hiding this comment

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

From a quick look, TRES needs to be updated anyway to work with the new AMUSE build system. Won't work as-is. One for my to-do list I guess, if I have the resources.

@github-project-automation github-project-automation bot moved this from Backlog to Open PRs in Development Board Mar 12, 2026
@LourensVeen LourensVeen self-assigned this Mar 13, 2026
@LourensVeen LourensVeen added this to the Release 2026.3.0 milestone Mar 13, 2026
@LourensVeen
Copy link
Member

Moved the examples to src/amuse/examples because that's the only way to make python -m amuse.examples.<name> work. (Well, I can think of something else, but that's so ugly it's borderline evil.)

Here's a list of example names that occur in the book, but seem to be missing from this PR:

plot_nbody_performance
brutus
run_nbody_precision
plot_stellar_structure
stellar.merger_stellar_density_profile
stellar_isochrone
stellar.massloss_response
plot_stellar_massloss_response
plot_M67withBSS_tracks
plot_solar_comparison
plot_hydro_disk_with_bump
hydro_give_or_take
plot_accretion_from_windy_star
data.fig_star_in_sph_N10000.M1.0MSun.npy
supernova_irradiation
evolve_triple_with_wind
plot_triple_with_wind
gravity_collision
supernova_IIp_Lightcurve
plot_supernova_IIp_and_disk_temperature
gravity_kepler_disks
compare_disk_size_Trapezium
basic_multiples
imbhs_with_friction
gravity_potential
gravity_gravity
plot_sun_and_earth_and_moon
no_bridge_ph4_integrator
three_body_bridge
three_body_bridge_hierachical
three_body_bridge_n_order
solar_cluster_in_galaxy_potential
integrate_sun_in_galaxy_potential
solar_cluster_in_semilive_galaxy
plot_solar_cluster_in_live_galaxy
Sun_and_m67_in_the_galaxy
plot_rlof_triples
xi_tau_rlof
plot_xi_tau_orbital_evolution
make_planets_oligarch
planetary_system
plot_planetary_system
venice_minimal
plot_projected_disk_density_from_top
plot_projected_disk_temp_from_top
plot_sstar_orbits
initialize_sstars
plot_plummer_stars_and_planets
christmas_card_2010
stellar.multiple_stellar_threaded
test_jeans_instability
particles_and_gas_in_cluster
hr_diagram_cluster
gas_in_cluster
pythagorean
kepler_orbiters_planets_around_sun
evolve_orbit_in_potential
evolve_binary
particles_and_multiples
basic_multiples_si
molecular_cloud_chemistry
plot_nbody_performance
mercury_example
h2region
linear_wave
linear_wave_boundaries
solar_cluster_in_live_galaxy
clump_finding
ppd_minimal

rieder and others added 2 commits March 16, 2026 13:41
This file was deleted from the examples on purpose, the correct fix would be to remove the references to this. The replacement is using the matplotlib style for the book.
Copy link
Member Author

Choose a reason for hiding this comment

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

need to remove these references instead.

rieder added 3 commits March 16, 2026 13:48
The use of this is replaced with a dedicated matplotlib stylesheet, need
to update examples to use this instead.


def main(**kwargs):
calculate_core_temperature_density(**kwargs)
Copy link
Member

Choose a reason for hiding this comment

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

I'm getting

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/examples/calculate_core_temperature_density.py", line 137, in <module>
    main(**arguments.__dict__)
    ~~~~^^^^^^^^^^^^^^^^^^^^^^
  File "/home/lourens/.miniconda3/envs/amuse-pr-1227/lib/python3.14/site-packages/amuse/examples/calculate_core_temperature_density.py", line 132, in main
    calculate_core_temperature_density(**kwargs)
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^
TypeError: calculate_core_temperature_density() got an unexpected keyword argument 'metalicity'

@LourensVeen
Copy link
Member

I've added back some more missing examples from main. Remaining missing ones that are mentioned in the book but that I can't find:

hr_diagram_cluster   (8.5.3 on page 8-28)
initialize_sstars    (10.4.1 on page 10-25)
integrate_sun_in_galaxy_potential    (9.4.1.2 on page 9-32)
plot_plummer_stars_and_planets    (fig. A.1 on page A-10)
plot_rlof_triples    (fig. 9.13 on 9-38)
plot_solar_cluster_in_live_galaxy    (fig. 9.11 on page 9-35)
plot_sun_and_earth_and_moon    (fig. 9.7 on page 9-23)
plot_xi_tau_orbital_evolution    (fig. 9.14 on page 9-39)
ppd_minimal    (fig. B.3 on page B-15)
run_nbody_precision    (fig. 4.13 on page 4-46)
three_body_bridge_n_order    (table 9.1 on page 9-21)
venice_minimal    (fig. 10.2 on page 10-11)
xi_tau_rlof    (fig. 9.14 on page 9-39)

and then there's data.fig_star_in_sph_N10000.M1.0MSun.npy which is also missing, which is used by plot_single_star_to_sph.py. That single_star_to_sph.py script currently has its write command commented out, so it doesn't produce output, and anyway it uses a different file name.

@rieder
Copy link
Member Author

rieder commented Mar 17, 2026

Last missing files (and if found, where they are found):
stellar.merger_stellar_density_profile --> merger_stellar_density_profile.py (wrong name)
data.fig_star_in_sph_N10000.M1.0MSun.npy --> data/fig_star_in_sph_N10000M1.0MSun.npy (missing dot)
compare_disk_size_Trapezium --> compare_disk_size_trapezium.py (capitalization is off)
plot_sun_and_earth_and_moon --> ?
three_body_bridge_hierachical --> three_body_bridge_hierarchical.py (spelling error)
three_body_bridge_n_order --> ?
integrate_sun_in_galaxy_potential --> ?
plot_solar_cluster_in_live_galaxy --> ?
Sun_and_m67_in_the_galaxy --> ?
xi_tau_rlof --> ?
plot_xi_tau_orbital_evolution --> ?
venice_minimal --> ?
initialize_sstars --> ?
stellar.multiple_stellar_threaded --> multiple_stellar_threaded (wrong name)
ppd_minimal --> ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Open PRs

Development

Successfully merging this pull request may close these issues.

2 participants