Skip to content

Commit

Permalink
Merge pull request #17 from hz-b/dev/feature/update-to-madng-1.0
Browse files Browse the repository at this point in the history
Update to mad-ng v 1.0.0
  • Loading branch information
PierreSchnizer authored Dec 14, 2024
2 parents b6c427a + aa42c59 commit 09cb9d7
Show file tree
Hide file tree
Showing 37 changed files with 479 additions and 166 deletions.
66 changes: 0 additions & 66 deletions .github/workflows/build_and_test_ubuntu_latest.yml

This file was deleted.

39 changes: 39 additions & 0 deletions .github/workflows/composite-action/python-build/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: python-package

runs:
using: "composite"
steps:
- name: checkout gtpsa code
uses: actions/checkout@v4

- name: Downloads artefact
uses: actions/download-artifact@v4
with:
name: gtpsa-release-deb
- name: setup environment
shell: bash
run: |
sudo apt update
sudo apt install python3-dev python3-venv libarmadillo-dev ./gtpsa*.deb
- name: setup python, numpy v1.0
shell: bash
run: |
python3 -m venv venv-numpy-v1.0
. venv-numpy-v1.0/bin/activate
python3 -m pip install --upgrade pip wheel build pytest 'numpy<2.0' pandas
gtpsa_PREFIX=/usr/local python3 -m pip install -v ./python/
python3 -c "import gtpsa"
python3 -m pytest -v -v -v --log-cli-level=DEBUG ./python/tests
deactivate
- name: setup python, numpy v2.0
shell: bash
run: |
python3 -m venv venv-numpy-v2.0
. venv-numpy-v2.0/bin/activate
python3 -m pip install --upgrade pip wheel build pytest 'numpy>=2.0' pandas
gtpsa_PREFIX=/usr/local python3 -m pip install -v ./python/
python3 -c "import gtpsa"
python3 -m pytest -v --log-cli-level=DEBUG ./python/tests/
deactivate
49 changes: 49 additions & 0 deletions .github/workflows/composite-action/ubuntu-deb-package/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: ubuntu-deb-package
description : "builds gtpsa c++ package"

runs:
using: "composite"
steps:
- name: checkout gtpsa-cpp code
uses: actions/checkout@v4
with:
submodules: True

- name: install apt dependencies
shell: bash
run: |
sudo apt update
# gfortran ... currently for mad-ng gtpsa library
sudo apt install g++ gfortran cmake make libarmadillo-dev
# deliberatly only installing a subset of boost
sudo apt install libboost-chrono-dev libboost-filesystem-dev libboost-program-options-dev libboost-test-dev
- name: Configure CMake
shell: bash
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}}

- name: Build
shell: bash
# Build your program with the given configuration
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

- name: Test
shell: bash
working-directory: ${{github.workspace}}/build
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest -C ${{env.BUILD_TYPE}}

- name: make tar package
shell: bash
# need to be root to install ?
run: |
(cd ${{github.workspace}}/build && make package)
- name: Upload artifact for python build
uses: actions/upload-artifact@v4
with:
name: gtpsa-release-deb
path: ${{github.workspace}}/build/*.deb
43 changes: 43 additions & 0 deletions .github/workflows/orchestration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: build orchestration

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Release
# cmake is used to install file ... defaults to /usr/local
LD_LIBRARY_PATH: /usr/local/lib

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
ubuntu-deb-package:
# The CMake configure and build commands are platform agnostic and should work equally well on Windows or Mac.
# You can convert this to a matrix build if you need cross-platform coverage.
# See: https://docs.github.com/en/free-pro-team@latest/actions/learn-github-actions/managing-complex-workflows#using-a-build-matrix

runs-on: ubuntu-latest

steps:
- name: checkout gtpsa-cpp code
uses: actions/checkout@v4
with:
submodules: True

- name: ubuntu-deb-package
uses: ./.github/workflows/composite-action/ubuntu-deb-package

python-package:
needs : ubuntu-deb-package
runs-on: ubuntu-latest

steps:
- name: checkout gtpsa-cpp code
uses: actions/checkout@v4
with:
submodules: False

- name: python-build
uses: ./.github/workflows/composite-action/python-build
24 changes: 24 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,27 @@ IF(USE_pybind11)
endif()
add_subdirectory(examples)
add_subdirectory(tests)

# generate postinst file in ${CMAKE_BINARY_DIR} from template #
CONFIGURE_FILE("${CMAKE_SOURCE_DIR}/contrib/postinst.in" "postinst" @ONLY IMMEDIATE)

# generate a DEB when cpack is run
SET(CPACK_GENERATOR "DEB")
SET(CPACK_PACKAGE_NAME ${CMAKE_PROJECT_NAME})
SET(CPACK_SET_DESTDIR TRUE)
SET(CPACK_DEBIAN_PACKAGE_MAINTAINER "[email protected]")
SET(CPACK_PACKAGE_VERSION_MAJOR "0")
SET(CPACK_PACKAGE_VERSION_MINOR "1")
SET(CPACK_PACKAGE_VERSION_PATCH "0")
include(GNUInstallDirs)
# SET(CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_SOURCE_DIR}/docs/CPack.Description.txt")
SET(CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README.rst")
SET(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/LICENSE")
# SET(CPACK_DEBIAN_PACKAGE_DEPENDS "libarmadillo-dev")
set (CPACK_DEBIAN_PACKAGE_SHLIBDEPS ON)
SET(CPACK_PACKAGE_VENDOR "HZB")
# make postinst run after install #
SET(CPACK_DEBIAN_PACKAGE_CONTROL_EXTRA "${CMAKE_BINARY_DIR}/postinst;")


include(CPack)
8 changes: 8 additions & 0 deletions Changelog
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
14. December 2024 <[email protected]>
* updated upstream to mad-ng 1.0.0
* some methods removed: no direct upstream correspondance
* method setVariable changed: index needs to be at least 1,
order of tpsa object needs to be at least 1
**NB** default now to one
python checks before calling if user does not set flag to not check,
c++ side deliberatyl not checking it: avoid code duplication
2 changes: 2 additions & 0 deletions Todo.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* set_variable: find out index of iv, consistency between C++ and python interface
* make mad_tpsa library ensure raise c++ exception
2 changes: 2 additions & 0 deletions contrib/postinst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env sh
/usr/sbin/ldconfig
3 changes: 2 additions & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
add_subdirectory(c)
#: add it back again as soon as the examples are fixed upstream
# add_subdirectory(c)
add_subdirectory(c++)
4 changes: 2 additions & 2 deletions examples/c++/gtpsa_ex0.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ int main(int argc, char *argv[])
auto a_desc = std::make_shared<gtpsa::desc>(1, 0);
std::cout << "desc " << a_desc << std::endl;

auto t1 = gtpsa::tpsa(a_desc, mad_tpsa_default);
auto t2 = gtpsa::tpsa(t1, mad_tpsa_default);
auto t1 = gtpsa::tpsa(a_desc, mad_tpsa_dflt);
auto t2 = gtpsa::tpsa(t1, mad_tpsa_dflt);

std::vector<num_t> nums = {M_PI/6e0};

Expand Down
4 changes: 2 additions & 2 deletions examples/c++/gtpsa_ex1.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ int main(int argc, char *argv[])
auto a_desc = std::make_shared<gtpsa::desc>(6, 4);
std::cout << "desc " << a_desc << std::endl;

auto t1 = gtpsa::tpsa(a_desc, mad_tpsa_default);
auto t2 = gtpsa::tpsa(t1, mad_tpsa_default);
auto t1 = gtpsa::tpsa(a_desc, mad_tpsa_dflt);
auto t2 = gtpsa::tpsa(t1, mad_tpsa_dflt);

std::vector<num_t> nums = {M_PI/6, 1,1,1,1,1,1};

Expand Down
10 changes: 5 additions & 5 deletions examples/c++/space_state.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ int main(int argc, char * argv[])
auto desc = std::make_shared<gtpsa::desc>(1,0);
std::cerr << "desc " << *desc << std::endl;

auto tr = gtpsa::tpsa(desc, mad_tpsa_default);
auto tr = gtpsa::tpsa(desc, mad_tpsa_dflt);

auto st1 = gtpsa::ss_vect<gtpsa::tpsa>(tr, mad_tpsa_default);
auto st2 = gtpsa::ss_vect<gtpsa::tpsa>(tr, mad_tpsa_default);
auto st1 = gtpsa::ss_vect<gtpsa::tpsa>(tr, mad_tpsa_dflt);
auto st2 = gtpsa::ss_vect<gtpsa::tpsa>(tr, mad_tpsa_dflt);
st1 += st2;

double tmp;
auto st4 = gtpsa::ss_vect<double>(tmp, mad_tpsa_default);
auto st5 = gtpsa::ss_vect<double>(tmp, mad_tpsa_default);
auto st4 = gtpsa::ss_vect<double>(tmp, mad_tpsa_dflt);
auto st5 = gtpsa::ss_vect<double>(tmp, mad_tpsa_dflt);

return 0;
}
2 changes: 1 addition & 1 deletion external_repositories/mad-ng
27 changes: 27 additions & 0 deletions python/pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[build-system]
requires = ["setuptools", "setuptools-scm", "pybind11>=2.9"]
build-backend = "setuptools.build_meta"

[project]
name = "gtpsa"
version = "0.1.0"
authors = [
{name = "Pierre Schnizer", email = "[email protected]"},
]
description = "wraps gtpsa as provided by madng"
# readme = "../README.rst"
requires-python = ">=3.8"
license = {text = "GPL"}
classifiers = [
"Framework :: Django",
"Programming Language :: Python :: 3",
"Development Status :: 2 - Pre-Alpha",
"Intended Audience :: Science/Research",
"License :: OSI Approved :: GNU General Public License (GPL)",
"Programming Language :: Python :: 3",
"Topic :: Scientific/Engineering :: Physics"
]

dependencies = [
"numpy",
]
2 changes: 1 addition & 1 deletion python/src/desc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Args:\n\
.def_property_readonly("maximum_length", &gtpsa::mad::desc::maxLen )
// rename this mehtod
// .def("ordLen" , &gtpsa::mad::desc::ordLen )
.def("truncate" , &gtpsa::mad::desc::trunc )
//.def("truncate" , &gtpsa::mad::desc::trunc )
//.def("index" , &gtpsa::mad::desc::nxtbyvar )
.def("nextByVariable" , &gtpsa::mad::desc::nxtbyvar )
.def("nextByOrder" , &gtpsa::mad::desc::nxtbyord )
Expand Down
27 changes: 18 additions & 9 deletions python/src/gtpsa.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,31 +26,40 @@ Use clone to create a copy of the content too. \n";
template<class Cls, typename T>
static void set_variable(Cls& inst, const T& v, idx_t i, const T& s, const bool check_first)
{
auto nv = inst.getDescription()->getNv();
const auto desc = inst.getDescription();
const auto nv = desc->getNv();

if(check_first) {
if(i <= 0){
std::stringstream strm;
strm << "index of variable must be at least 1, but was " << i;
strm << "py:: index of variable must be at least 1, but was " << i;
throw std::runtime_error(strm.str());
}
if(i > nv){
std::stringstream strm;
strm << "index of derivative must not exceed number of variables (" << nv << ") but was " << i;
strm << "py:: index of derivative must not exceed number of variables (" << nv << ") but was " << i;
throw std::runtime_error(strm.str());
}
if (!(inst.order() >= 1)){
throw std::runtime_error("inst.>order() needs to be >= 1");
}
if (!(0 < i && i <= nv)){
throw std::runtime_error("gtpas needs to be >= 1");
}
}
inst.setVariable(v, i, s);
}

template<class Cls, typename T>
static void set_knob(Cls& inst, const T& v, idx_t i, const T& s, const bool check_first)
{
auto desc = inst.getDescription();
auto nv = desc->getNv();
auto info = desc->getInfo();
auto np = info.getNumberOfParameters();
auto total_number = info.getTotalNumber();
const auto desc = inst.getDescription();
const auto nv = desc->getNv();
const auto info = desc->getInfo();
const auto np = info.getNumberOfParameters();
const auto total_number = info.getTotalNumber();

std::cerr << "inst order " << inst.order() << std::endl;
if(check_first) {
if(i < nv){
std::stringstream strm;
Expand Down Expand Up @@ -252,7 +261,7 @@ struct AddMethods
set_variable(inst, v, i, s, check_first);
},
"set the variable to value and gradient at index of variable to 1. v:= scale * this->v + value",
py::arg("value"), py::arg("index_of_variable") = 0, py::arg("scale") = 0, py::arg("check_first") = true
py::arg("value"), py::arg("index_of_variable") = 1, py::arg("scale") = 0, py::arg("check_first") = true
)
.def("deriv", [](const Cls& inst, int iv){
using namespace gtpsa::python;
Expand Down
Loading

0 comments on commit 09cb9d7

Please sign in to comment.