Advanced Computing Platform for Theoretical Physics

commit大文件会使得服务器变得不稳定,请大家尽量只commit代码,不要commit大的文件。

Commit 03136cbd authored by Chen Xia's avatar Chen Xia
Browse files

Five days of hard work

galpropcc: d9829b55373b3520e10653ab27a90cd49835ce44
parents
Pipeline #1195 failed with stages
in 0 seconds
# =====================================
# C++
# =====================================
# Prerequisites
*.d
# Compiled Object files
*.slo
*.lo
*.o
*.obj
# Precompiled Headers
*.gch
*.pch
# Compiled Dynamic libraries
*.so
*.dylib
*.dll
# Fortran module files
*.mod
*.smod
# Compiled Static libraries
*.lai
*.la
*.a
*.lib
# Executables
*.exe
*.out
*.app
# =====================================
# cmake
# =====================================
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
Makefile
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
_deps
# =====================================
# Python
# =====================================
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
# C extensions
*.so
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST
# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec
# Installer logs
pip-log.txt
pip-delete-this-directory.txt
# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/
# Translations
*.mo
*.pot
# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal
# Flask stuff:
instance/
.webassets-cache
# Scrapy stuff:
.scrapy
# Sphinx documentation
docs/_build/
# PyBuilder
.pybuilder/
target/
# Jupyter Notebook
.ipynb_checkpoints
# IPython
profile_default/
ipython_config.py
# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version
# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock
# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock
# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/
# Celery stuff
celerybeat-schedule
celerybeat.pid
# SageMath parsed files
*.sage.py
# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/
# Spyder project settings
.spyderproject
.spyproject
# Rope project settings
.ropeproject
# mkdocs documentation
/site
# mypy
.mypy_cache/
.dmypy.json
dmypy.json
# Pyre type checker
.pyre/
# pytype static type analyzer
.pytype/
# Cython debug symbols
cython_debug/
# scikit-build
_skbuild/
cmake_minimum_required(VERSION 3.15...3.20)
project(galpropy LANGUAGES CXX)
set(CMAKE_CXX_FLAGS_DEBUG "-g -Wall -Wextra")
set(CMAKE_CXX_FLAGS_RELEASE "-O2 -DNDEBUG")
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release)
endif()
message(">> CMAKE_BUILD_TYPE: ${CMAKE_BUILD_TYPE}")
set(CMAKE_CXX_STANDARD 17)
# set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")
if(SKBUILD)
set(THE_PYTHON ${PYTHON_EXECUTABLE})
else()
find_package(Python COMPONENTS Interpreter Development REQUIRED)
set(THE_PYTHON ${Python_EXECUTABLE})
endif()
# find_package(GALPROPCC REQUIRED)
set(GALPROPCC_INC_DIR "/usr/include/cfitsio")
set(GALPROPCC_LIB "galpropcc")
execute_process(
COMMAND "${THE_PYTHON}" -c
"import pybind11; print(pybind11.get_cmake_dir())"
OUTPUT_VARIABLE _tmp_dir
OUTPUT_STRIP_TRAILING_WHITESPACE COMMAND_ECHO STDOUT)
list(APPEND CMAKE_PREFIX_PATH "${_tmp_dir}")
find_package(OpenMP)
if (OPENMP_FOUND)
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
set (CMAKE_Fortran_FLAGS "${CMAKE_Fortran_FLAGS} ${OpenMP_Fortran_FLAGS}")
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif()
find_package(pybind11 CONFIG REQUIRED)
FILE(GLOB TARGET_SRCS src/*.[hc]pp)
set(CORE_TARGET "_bind_galpropcc")
pybind11_add_module(${CORE_TARGET} MODULE ${TARGET_SRCS})
target_include_directories(${CORE_TARGET} PUBLIC ${GALPROPCC_INC_DIR})
target_link_libraries(${CORE_TARGET} PUBLIC ${GALPROPCC_LIB})
install(TARGETS ${CORE_TARGET} LIBRARY DESTINATION .)
MIT License
Copyright (c) 2021 Chen Xia
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
\ No newline at end of file
include README.md
include LICENSE
include setup.py
include pyproject.toml
include CMakeLists.txt
recursive-include src *.cpp *.hpp
# Galpropy
Python wrapper for [GALPROPCC](https://code.itp.ac.cn/ycding/galpropcc), a
modified [GALPROP](https://galprop.stanford.edu) with redesigned C++ interface.
The seamless experience in Python comes from the powerful
[pybind11](https://github.com/pybind/pybind11).
# Installation
Install GALPROPCC and its dependencies first and then install with pip
```bash
$ pip install .
```
[build-system]
requires = [
"setuptools>=59.0",
"wheel",
"pybind11>=2.8.0",
"scikit-build>=0.12",
"cmake>=3.21",
"ninja"
]
build-backend = "setuptools.build_meta"
from setuptools import find_packages
from skbuild import setup
def readme():
with open("README.md", "r") as f:
return f.read()
meta_data = {
"name": "galpropy",
"version": "0.0.1",
"author": "Chen Xia",
"author_email": "xiachen@itp.ac.cn",
"maintainer": "Chen Xia",
"maintainer_email": "xiachen1996@outlook.com",
"description": "Python wrapper for GALPROPCC",
"long_description": readme(),
"long_description_content_type": "text/markdown",
"keywords": "Cosmic Ray, GALPROPCC",
"url": "https://code.itp.ac.cn/physcxia/galpropy",
"project_urls": {"Source Code":
"https://code.itp.ac.cn/physcxia/galpropy"},
"platforms": "Linux",
"license": "MIT",
"classifiers": [
"Programming Language :: Python :: 3 :: Only",
"Programming Language :: Python :: 3.6",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: C++",
"License :: OSI Approved :: MIT License",
"Operating System :: POSIX :: Linux",
"Topic :: Scientific/Engineering :: Physics",
"Development Status :: 3 - Alpha",
]
}
if __name__ == '__main__':
setup(**meta_data,
packages=find_packages(where="src"),
package_dir={"": "src"},
python_requires=">=3.6",
zip_safe=False,
cmake_install_dir="src/galpropy")
#include "galpropy.hpp"
void bind_Distribution(py::module_ &m)
{
py::class_<Dimensions>(m, "Dimensions").def(py::init<>())
.def("is1", &Dimensions::is1)
.def("is2", &Dimensions::is2)
.def("is3", &Dimensions::is3)
.def("ndim", &Dimensions::ndim)
.def("npos", &Dimensions::npos);
py::bind_vector<std::vector<Distribution>>(m, "VecDistribution");
py::class_<Distribution>(m, "Distribution")
.def(py::init<>())
.def("init", py::overload_cast<size_t, size_t, size_t, size_t>
(&Distribution::init))
.def("init", py::overload_cast<size_t, size_t, size_t>
(&Distribution::init))
.def("init", py::overload_cast<const Dimensions&, size_t>
(&Distribution::init), "dims"_a, "np"_a=0)
.def("max", &Distribution::max)
.def("delete_array", &Distribution::delete_array);
}
#include <galprop/ErrorLogger.h>
#include "galpropy.hpp"
void bind_ErrorLogger(py::module_ &m)
{
py::enum_<utl::ErrorLogger::SeverityLevel>(m, "SeverityLevel")
.value("eDebug", utl::ErrorLogger::SeverityLevel::eDebug)
.value("eInfo", utl::ErrorLogger::SeverityLevel::eInfo)
.value("eWarning", utl::ErrorLogger::SeverityLevel::eWarning)
.value("eError", utl::ErrorLogger::SeverityLevel::eError)
.value("eFatal", utl::ErrorLogger::SeverityLevel::eFatal);
py::enum_<utl::ErrorLogger::VerbosityLevel>(m, "VerbosityLevel")
.value("eDefaultVerbosity", utl::ErrorLogger::VerbosityLevel::eDefaultVerbosity)
.value("eTerse", utl::ErrorLogger::VerbosityLevel::eTerse)
.value("eVerbose", utl::ErrorLogger::VerbosityLevel::eVerbose)
.value("eSilent", utl::ErrorLogger::VerbosityLevel::eSilent);
}
#include <galprop/GCR.h>
#include <galprop/DarkMatter.h>
#include "galpropy.hpp"
class PyPopulator : public GCR::Populator {
public:
using GCR::Populator::Populator;
using ptr_t = std::shared_ptr<Particle>;
using map_t = std::map<std::string, ptr_t>;
map_t populate(const Galprop& g) const override {
PYBIND11_OVERRIDE_PURE(map_t, GCR::Populator, populate, g);
}
};
void bind_GCR(py::module_ &m)
{
using vecptrpop = std::vector<std::shared_ptr<GCR::Populator>>;
py::bind_vector<vecptrpop>(m, "VecPtrPopulator");
py::implicitly_convertible<py::list, vecptrpop>();
py::implicitly_convertible<py::tuple, vecptrpop>();
py::class_<GCR> gcr(m, "GCR");
gcr.def(py::init<>())
.def("populate", &GCR::populate)
.def("__getitem__",
[](const GCR& self, const std::string& name) {
return self[name]; },
"get by name")
.def("__getitem__",
[](const GCR &self, std::size_t i) {
if (i >= self.size()) throw py::index_error("out of range");
return self[i]; },
"get by index")
.def("__len__", [](const GCR& self) { return self.size(); })
.def("__iter__", [](const GCR& self)
{ return py::make_iterator(self.begin(), self.end()); },
py::keep_alive<0, 1>())
.def("push_back", [](GCR& self, std::shared_ptr<Particle> p) {
self.push_back(p); })
.def_readwrite("populators", &GCR::populators,
py::return_value_policy::reference_internal);
py::class_<GCR::Populator, PyPopulator, std::shared_ptr<GCR::Populator>>(
gcr, "Populator")
.def(py::init_alias<>())
.def("populate", &GCR::Populator::populate);
py::class_<PopPrimaryNucV54, GCR::Populator,
std::shared_ptr<PopPrimaryNucV54>>(m, "PopPrimaryNucV54")
.def(py::init<>());
py::class_<PopPrimaryEleV54, GCR::Populator,
std::shared_ptr<PopPrimaryEleV54>>(m, "PopPrimaryEleV54")
.def(py::init<>());
py::class_<PopSecondaryV54, GCR::Populator,
std::shared_ptr<PopSecondaryV54>>(m, "PopSecondaryV54")
.def(py::init<>());
// dark matter TODO
py::class_<PopPrimaryDMSP, GCR::Populator,
std::shared_ptr<PopPrimaryDMSP>>(m, "PopPrimaryDMSP")
.def(py::init<>()).def_readwrite("dm", &PopPrimaryDMSP::dm);
py::class_<PopSecondaryAntinucleus, GCR::Populator,
std::shared_ptr<PopSecondaryAntinucleus>>(
m, "PopSecondaryAntinucleus").def(py::init<>());
}
#include <galprop/Galaxy.h>
#include <galprop/Galdef.h>
#include "galpropy.hpp"
void bind_Galaxy(py::module_ &m)
{
py::class_<Galaxy, std::shared_ptr<Galaxy>>(m, "Galaxy")
.def(py::init<>());
}
#include <galprop/Galdef.h>
#include "galpropy.hpp"
void bind_Galdef(py::module_ &m)
{
py::class_<Galdef>(m, "Galdef").def(py::init<>())
.def_readwrite("z_min", &Galdef::z_min)
.def_readwrite("z_max", &Galdef::z_max)
.def_readwrite("dz", &Galdef::dz)
.def_readwrite("r_min", &Galdef::r_min)
.def_readwrite("r_max", &Galdef::r_max)
.def_readwrite("primary_electrons", &Galdef::primary_electrons)
.def_readwrite("electron_g_0", &Galdef::electron_g_0)
.def_readwrite("electron_g_1", &Galdef::electron_g_1)
.def_readwrite("electron_g_2", &Galdef::electron_g_2)
.def_readwrite("electron_rigid_cutoff", &Galdef::electron_rigid_cutoff)
.def_readwrite("electron_rigid_br0", &Galdef::electron_rigid_br0)
.def_readwrite("electron_rigid_br", &Galdef::electron_rigid_br)
.def_readwrite("electron_rigid_br2", &Galdef::electron_rigid_br2)
.def_readwrite("max_Z", &Galdef::max_Z);
}
#include "galpropy.hpp"
void bind_Galprop(py::module_ &m)
{
py::class_<Galprop, std::unique_ptr<Galprop, py::nodelete>>(m, "Galprop")
.def(py::init<>())
.def("create_gcr", &Galprop::create_gcr)
.def("create_galaxy", &Galprop::create_galaxy)
// .def("cr_luminosity", &Galprop::cr_luminosity)
.def("propagate_particles", &Galprop::propagate_particles)
.def("D_pp", &Galprop::D_pp)
.def("D_xx", &Galprop::D_xx)
.def("nuclei_normalize", &Galprop::nuclei_normalize)
.def("electron_normalize", &Galprop::electrons_normalize)
.def("run_DM", &Galprop::run_DM)
.def("propel", &Galprop::propel)
.def("set_dpp_limit", &Galprop::set_dpp_limit)
.def("load_config", &Galprop::load_config)
.def("run", py::overload_cast<>(&Galprop::run), "run without arg")
.def("run", py::overload_cast<const std::string &>(&Galprop::run),
"run with name")
.def("GetEkin", &Galprop::GetEkin)
.def("GetFlux", &Galprop::GetFlux, "Get the flux by ParticleNumber",
"ZA"_a, "ekin_toa"_a=std::valarray<double>{},
"mod"_a=NoModulation())
.def("GetFluxByName", &Galprop::GetFluxByName, "Get the flux by name",
"name"_a, "ekin_toa"_a=std::valarray<double>{},
"mod"_a=NoModulation())
.def_readwrite("n_species", &Galprop::n_species)
.def_readwrite("galdef", &Galprop::galdef)
.def_readwrite("cfg", &Galprop::cfg)
.def_readwrite("particle_lib", &Galprop::particle_lib)
.def_readwrite("gcr", &Galprop::gcr)
.def_readwrite("galaxy", &Galprop::galaxy)
.def_readwrite("energy_losses", &Galprop::energy_losses)
.def_readwrite("propel_operators", &Galprop::propel_operators)
.def_readwrite("energy_loss_fallback", &Galprop::energy_loss_fallback);
// .def_readwrite("propel_operator_fallback",
// &Galprop::propel_operator_fallback);
}
#include <galprop/Particle.h>
#include "galpropy.hpp"
class PyParticle : public Particle {
public:
using Particle::Particle;
bool depends_on(const Particle& p, Galprop& g) const override {
PYBIND11_OVERRIDE_PURE(bool, Particle, depends_on, p, g);
}
void fill_transport_arrays(Galprop& g) override {
PYBIND11_OVERRIDE(void, Particle, fill_transport_arrays, g);
}
void fill_source_functions(Galprop& g) override {
PYBIND11_OVERRIDE_PURE(void, Particle, fill_source_functions, g);
}
void fill_diffusion_coeffs(Galprop& g) override {
PYBIND11_OVERRIDE(void, Particle, fill_diffusion_coeffs, g);
}
void fill_energy_loss_rate(Galprop& g) override {
PYBIND11_OVERRIDE(void, Particle, fill_energy_loss_rate, g);
}
void fill_destruction_rate(Galprop& g) override {
PYBIND11_OVERRIDE(void, Particle, fill_destruction_rate, g);
}
void propagate(Galprop& g) override {
PYBIND11_OVERRIDE(void, Particle, propagate, g);
}
double abundance(void) const override {
PYBIND11_OVERRIDE(double, Particle, abundance);
}
};
void bind_Particle(py::module_ &m)
{
py::bind_vector<std::vector<std::shared_ptr<Particle>>>(
m, "VecPtrParticle");
py::implicitly_convertible<
py::list, std::vector<std::shared_ptr<Particle>>>();
py::implicitly_convertible<
py::tuple, std::vector<std::shared_ptr<Particle>>>();
py::bind_map<std::map<std::string, std::shared_ptr<Particle>>>(
m, "MapPtrParticle");
py::class_<Particle, PyParticle, std::shared_ptr<Particle>>(m, "Particle")
.def(py::init<std::shared_ptr<const ParticleInfo>,
std::shared_ptr<const Galaxy>,
const std::string &>())
.def("depends_on", &Particle::depends_on)
.def("fill_transport_arrays", &Particle::fill_transport_arrays)
.def("fill_source_functions", &Particle::fill_source_functions)
.def("fill_diffusion_coeffs", &Particle::fill_diffusion_coeffs)
.def("fill_energy_loss_rate", &Particle::fill_energy_loss_rate)
.def("fill_destruction_rate", &Particle::fill_destruction_rate)
.def("local_flux", py::overload_cast<double, double>(
&Particle::local_flux, py::const_),
"2-D local flux", "r"_a=Rsun, "z"_a=0)
.def("local_flux", py::overload_cast<double, double, double>(
&Particle::local_flux, py::const_),
"3-D local flux", "x"_a, "y"_a, "z"_a)
.def_readwrite("name", &Particle::name)
.def_readwrite("K_electron", &Particle::K_electron);
}
#include <galprop/ParticleLibrary.h>
#include "galpropy.hpp"