Skip to content

Commit 167fbd0

Browse files
Merge pull request #527 from JulienDoerner/numpy2
Numpy2 implementation and updates on testing
2 parents 09ac0ac + 4fe3402 commit 167fbd0

File tree

13 files changed

+331
-135
lines changed

13 files changed

+331
-135
lines changed

.github/workflows/testing_OSX.yml

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ jobs:
1313
cc: "clang"
1414
fc: "gfortran-14"
1515
swig_builtin: "On" #uses swig 4.0.2
16-
py: "/usr/bin/python3"
16+
py: "/usr/bin/python3" # python 3.14
1717
steps:
1818
- name: Checkout repository
1919
uses: actions/checkout@v4
2020
- name: Preinstall
2121
run: |
22-
brew install hdf5 fftw cfitsio muparser libomp swig python3
23-
pip install numpy==1.26.4 cmake
22+
brew install hdf5 fftw cfitsio muparser libomp swig
23+
python3 -m venv venv
24+
source venv/bin/activate
25+
python -m pip install --upgrade pip
26+
pip install numpy cmake
27+
pip show numpy # cross-check numpy version for Python_NumPy_INCLUDE_DIR
2428
- name: Set up the build
2529
env:
2630
CXX: ${{ matrix.config.cxx }}
@@ -30,7 +34,17 @@ jobs:
3034
run: |
3135
mkdir build
3236
cd build
33-
cmake .. -DENABLE_PYTHON=True -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS="none"
37+
38+
cmake .. \
39+
-DCMAKE_INSTALL_PREFIX=$GITHUB_WORKSPACE/venv \
40+
-DENABLE_PYTHON=True \
41+
-DPython_EXECUTABLE=$GITHUB_WORKSPACE/venv/bin/python \
42+
-DCMAKE_CXX_STANDARD=17 \
43+
-DCMAKE_CXX_FLAGS="-std=c++17 -stdlib=libc++" \
44+
-DENABLE_TESTING=On \
45+
-DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} \
46+
-DPython_INSTALL_PACKAGE_DIR=$GITHUB_WORKSPACE/venv/lib/python3.14/site-packages
47+
3448
- name: Build CRPropa
3549
run: |
3650
cd build

.github/workflows/testing_ubuntu20.yml

Lines changed: 0 additions & 47 deletions
This file was deleted.

.github/workflows/testing_ubuntu22.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ jobs:
2121
- name: Preinstall
2222
run: |
2323
sudo apt-get update
24-
sudo apt-get install libmuparser-dev python3 python3-numpy python3-setuptools libhdf5-serial-dev libomp5 libomp-dev libfftw3-dev libcfitsio-dev lcov
24+
sudo apt-get install libmuparser-dev python3 python3-setuptools libhdf5-serial-dev libomp5 libomp-dev libfftw3-dev libcfitsio-dev lcov
25+
pip install --upgrade pip
26+
pip install numpy
27+
pip show numpy
2528
- name: Set up the build
2629
env:
2730
CXX: ${{ matrix.config.cxx }}
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
name: crpropa-testing_ubuntu24
2+
on: [push, pull_request]
3+
4+
jobs:
5+
x64:
6+
runs-on: ${{ matrix.config.os }}
7+
strategy:
8+
fail-fast: false
9+
matrix:
10+
config:
11+
- name: "ubuntu-24"
12+
os: ubuntu-24.04
13+
cxx: "g++-14"
14+
cc: "gcc-14"
15+
fc: "gfortran-14"
16+
swig_builtin: "On"
17+
py: "/usr/bin/python3"
18+
steps:
19+
- name: Checkout repository
20+
uses: actions/checkout@v4
21+
- name: Preinstall
22+
run: |
23+
sudo apt-get update
24+
sudo apt-get install libmuparser-dev python-dev-is-python3 libhdf5-serial-dev libomp5 libomp-dev libfftw3-dev libcfitsio-dev lcov
25+
pip install numpy
26+
- name: Set up the build
27+
env:
28+
CXX: ${{ matrix.config.cxx }}
29+
CC: ${{ matrix.config.cc }}
30+
FC: ${{ matrix.config.fc }}
31+
run: |
32+
mkdir build
33+
cd build
34+
cmake .. -DENABLE_PYTHON=True -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS="none"
35+
- name: Build CRPropa
36+
run: |
37+
cd build
38+
make
39+
- name: Run tests
40+
run: |
41+
cd build
42+
make test
43+
- name: Archive test results
44+
if: always()
45+
uses: actions/upload-artifact@v4
46+
with:
47+
name: "test-report_${{matrix.config.os}}"
48+
path: build/Testing/Temporary/LastTest.log
49+
arm:
50+
runs-on: ${{ matrix.config.os }}
51+
strategy:
52+
fail-fast: false
53+
matrix:
54+
config:
55+
- name: "ubuntu-24"
56+
os: ubuntu-24.04-arm
57+
cxx: "g++-14"
58+
cc: "gcc-14"
59+
fc: "gfortran-14"
60+
swig_builtin: "On"
61+
py: "/usr/bin/python3"
62+
steps:
63+
- name: Checkout repository
64+
uses: actions/checkout@v4
65+
- name: Preinstall
66+
run: |
67+
sudo apt-get update
68+
sudo apt-get install libmuparser-dev python-dev-is-python3 libhdf5-serial-dev libomp5 libomp-dev libfftw3-dev libcfitsio-dev lcov
69+
pip install numpy
70+
pip show numpy
71+
- name: Set up the build
72+
env:
73+
CXX: ${{ matrix.config.cxx }}
74+
CC: ${{ matrix.config.cc }}
75+
FC: ${{ matrix.config.fc }}
76+
run: |
77+
mkdir build
78+
cd build
79+
cmake .. -DENABLE_PYTHON=True -DENABLE_TESTING=On -DENABLE_SWIG_BUILTIN=${{ matrix.config.swig_builtin }} -DSIMD_EXTENSIONS="none"
80+
- name: Build CRPropa
81+
run: |
82+
cd build
83+
make
84+
- name: Run tests
85+
run: |
86+
cd build
87+
make test
88+
- name: Archive test results
89+
if: always()
90+
uses: actions/upload-artifact@v4
91+
with:
92+
name: "test-report_${{matrix.config.os}}"
93+
path: build/Testing/Temporary/LastTest.log

CMakeLists.txt

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ list(APPEND CRPROPA_EXTRA_LIBRARIES sophia gfortran)
157157
list(APPEND CRPROPA_EXTRA_INCLUDES libs/sophia)
158158

159159
# Galactic magnetic lenses
160-
option(ENABLE_GALACTICMAGNETICLENS "Galactic Magnetic Lens" ON)
160+
option(ENABLE_GALACTICMAGNETICLENS "Galactic Magnetic Lens" OFF)
161161
option(INSTALL_EIGEN "Install provided EIGEN headers" OFF)
162162
SET(EIGEN_PATH "" CACHE STRING "Use EIGEN from this path instead of the version shipped with CRPropa")
163163
SET(WITH_GALACTIC_LENSES FALSE)
@@ -453,6 +453,8 @@ find_package(Python 3.0 REQUIRED COMPONENTS Interpreter Development NumPy)
453453
if(ENABLE_PYTHON AND Python_FOUND)
454454
find_package(SWIG 3.0 REQUIRED)
455455
include_directories(${Python_INCLUDE_DIRS})
456+
add_definitions(-DCRPROPA_HAVE_PYTHON)
457+
list(APPEND CRPROPA_SWIG_DEFINES -DCRPROPA_HAVE_PYTHON)
456458

457459
# print Python info in detail
458460
message(STATUS "Python: Found!")
@@ -467,23 +469,29 @@ if(ENABLE_PYTHON AND Python_FOUND)
467469
message(STATUS " development libraries: NOT found!")
468470
endif(Python_Development_FOUND)
469471

470-
472+
471473
# use Python_INSTALL_PACKAGE_DIR if provided; otherwise, install in Python_SITELIB
472474
set(Python_INSTALL_PACKAGE_DIR "${Python_SITELIB}" CACHE PATH "folder in which the python package is installed")
473475
message(STATUS " package install directory: ${Python_INSTALL_PACKAGE_DIR}")
474-
475-
476+
476477
# look for NumPy
477478
if(Python_NumPy_FOUND)
478-
set(CMAKE_SWIG_FLAGS -DWITHNUMPY ${CRP})
479-
list(APPEND CRPROPA_SWIG_DEFINES -DWITHNUMPY)
480-
include_directories(${Python_NumPy_INCLUDE_DIRS})
481-
message(STATUS "NumPy: Found!")
482-
message(STATUS " headers: ${Python_NumPy_INCLUDE_DIRS} (version ${Python_NumPy_VERSION})")
479+
set(CMAKE_SWIG_FLAGS -DWITHNUMPY ${CRP})
480+
list(APPEND CRPROPA_SWIG_DEFINES -DWITHNUMPY)
481+
include_directories(${Python_NumPy_INCLUDE_DIRS})
482+
message(STATUS "NumPy: Found!")
483+
message(STATUS " headers: ${Python_NumPy_INCLUDE_DIRS} (version ${Python_NumPy_VERSION})")
483484
elseif(Python_NumPy_FOUND)
484-
message(STATUS "NumPy: NOT found!")
485-
message(STATUS " CRPropa might work just fine with Python, but features like Galactic lenses will not be available.")
485+
message(STATUS "NumPy: NOT found!")
486+
message(STATUS " CRPropa might work just fine with Python, but features like Galactic lenses will not be available.")
486487
endif(Python_NumPy_FOUND)
488+
489+
# deactivate lenses if NumPy version is >= 2.0
490+
if(Python_NumPy_VERSION VERSION_GREATER_EQUAL 2.0 AND WITH_GALACTIC_LENSES)
491+
set(WITH_GALACTIC_LENSES FALSE CACHE BOOL "" FORCE)
492+
set(ENABLE_GALACTICMAGNETICLENS OFF CACHE BOOL "" FORCE)
493+
message(WARNING "NumPy version >= 2.0 detected, disabling Galactic lenses support")
494+
endif()
487495

488496
if(SWIG_VERSION VERSION_GREATER 4.0)
489497
# Use swig 4 builtin doxygen instead of external program

doc/pages/example_notebooks/targeting/Targeting.ipynb

Lines changed: 31 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -91,43 +91,46 @@
9191
},
9292
{
9393
"cell_type": "code",
94-
"execution_count": 3,
94+
"execution_count": null,
9595
"metadata": {},
9696
"outputs": [],
9797
"source": [
9898
"import healpy as hp\n",
9999
"import matplotlib.pylab as plt\n",
100+
"import crpropa as crp\n",
100101
"\n",
101-
"crdata = np.genfromtxt('TargetedEmission.txt')\n",
102-
"Id = crdata[:,3]\n",
103-
"E = crdata[:,4] * EeV\n",
104-
"px = crdata[:,8]\n",
105-
"py = crdata[:,9]\n",
106-
"pz = crdata[:,10]\n",
107-
"w = crdata[:,29]\n",
108-
"lons = np.arctan2(-1. * py, -1. *px)\n",
109-
"lats = np.pi / 2 - np.arccos( -pz / np.sqrt(px*px + py*py+ pz*pz) )\n",
102+
"# If CRPropa was compiled without the GalacticMagneticLens the ParticleMapsContainer does not exist\n",
103+
"if hasattr(crp, 'ParticleMapsContainer'):\n",
104+
" crdata = np.genfromtxt('TargetedEmission.txt')\n",
105+
" Id = crdata[:,3]\n",
106+
" E = crdata[:,4] * EeV\n",
107+
" px = crdata[:,8]\n",
108+
" py = crdata[:,9]\n",
109+
" pz = crdata[:,10]\n",
110+
" w = crdata[:,29]\n",
111+
" lons = np.arctan2(-1. * py, -1. *px)\n",
112+
" lats = np.pi / 2 - np.arccos( -pz / np.sqrt(px*px + py*py+ pz*pz) )\n",
110113
"\n",
111-
"M = ParticleMapsContainer()\n",
112-
"for i in range(len(E)):\n",
113-
" M.addParticle(int(Id[i]), E[i], lons[i], lats[i], w[i])\n",
114+
" M = ParticleMapsContainer()\n",
115+
" for i in range(len(E)):\n",
116+
" M.addParticle(int(Id[i]), E[i], lons[i], lats[i], w[i])\n",
114117
"\n",
115-
"###################################################################\n",
116-
"# WARNING\n",
117-
"# The calls M.getEnergies()/getParticleIds()/getMap() all segfault.\n",
118-
"################################################################### \n",
118+
" ###################################################################\n",
119+
" # WARNING\n",
120+
" # The calls M.getEnergies()/getParticleIds()/getMap() all segfault.\n",
121+
" ################################################################### \n",
119122
"\n",
120-
"#stack all maps\n",
121-
"#crMap = np.zeros(49152)\n",
122-
"#for pid in M.getParticleIds():\n",
123-
" #energies = M.getEnergies(int(pid))\n",
124-
" #for i, energy in enumerate(energies):\n",
125-
" #continue\n",
126-
" #crMap += M.getMap(int(pid), energy * eV )\n",
127-
"#\n",
128-
"##plot maps using healpy\n",
129-
"#hp.mollview(map=crMap, title='Targeted emission')\n",
130-
"#plt.show()"
123+
" #stack all maps\n",
124+
" #crMap = np.zeros(49152)\n",
125+
" #for pid in M.getParticleIds():\n",
126+
" #energies = M.getEnergies(int(pid))\n",
127+
" #for i, energy in enumerate(energies):\n",
128+
" #continue\n",
129+
" #crMap += M.getMap(int(pid), energy * eV )\n",
130+
" #\n",
131+
" ##plot maps using healpy\n",
132+
" #hp.mollview(map=crMap, title='Targeted emission')\n",
133+
" #plt.show()"
131134
]
132135
},
133136
{

0 commit comments

Comments
 (0)