Skip to content

Commit

Permalink
Fixes to Unbound C++ Types
Browse files Browse the repository at this point in the history
  • Loading branch information
ax3l committed Sep 1, 2023
1 parent dd06010 commit 4515577
Show file tree
Hide file tree
Showing 18 changed files with 276 additions and 187 deletions.
7 changes: 4 additions & 3 deletions .github/update_stub.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
# Authors: Axel Huebl
# License: BSD-3-Clause-LBNL
#
set -eu -o pipefail

# we are in the source directory, .github/
this_dir=$(cd $(dirname $0) && pwd)

pybind11-stubgen --ignore-all-errors -o ${this_dir}/../src/amrex/ amrex.space1d
pybind11-stubgen --ignore-all-errors -o ${this_dir}/../src/amrex/ amrex.space2d
pybind11-stubgen --ignore-all-errors -o ${this_dir}/../src/amrex/ amrex.space3d
pybind11-stubgen --exit-code -o ${this_dir}/../src/amrex/ amrex.space1d
pybind11-stubgen --exit-code -o ${this_dir}/../src/amrex/ amrex.space2d
pybind11-stubgen --exit-code -o ${this_dir}/../src/amrex/ amrex.space3d
2 changes: 1 addition & 1 deletion .github/workflows/stubs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ jobs:
- name: Run pre-commit cleanup
run: |
git add .
pre-commit run -a
pre-commit run -a || true
git add .
- name: Update Install
Expand Down
1 change: 1 addition & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ repos:
args: [--allow-multiple-documents]
- id: check-added-large-files
args: ['--maxkb=40']
exclude: ^.*\.pyi$
- id: requirements-txt-fixer
# - id: fix-encoding-pragma
# exclude: ^noxfile.py$
Expand Down
51 changes: 37 additions & 14 deletions src/Base/Array4.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ void make_Array4(py::module &m, std::string typestr)
{
using namespace amrex;

constexpr bool is_not_const = std::is_same_v<std::remove_cv_t<T>, T>;

// dispatch simpler via: py::format_descriptor<T>::format() naming
auto const array_name = std::string("Array4_").append(typestr);
py::class_< Array4<T> >(m, array_name.c_str())
py::class_< Array4<T> > py_array4(m, array_name.c_str());
py_array4
.def("__repr__",
[typestr](Array4<T> const & a4) {
std::stringstream s;
Expand Down Expand Up @@ -193,15 +196,7 @@ void make_Array4(py::module &m, std::string typestr)
.def("contains", &Array4<T>::contains)
//.def("__contains__", &Array4<T>::contains)

// setter & getter
.def("__setitem__", [](Array4<T> & a4, IntVect const & v, T const value){ a4(v) = value; })
.def("__setitem__", [](Array4<T> & a4, std::array<int, 4> const key, T const value){
a4(key[0], key[1], key[2], key[3]) = value;
})
.def("__setitem__", [](Array4<T> & a4, std::array<int, 3> const key, T const value){
a4(key[0], key[1], key[2]) = value;
})

// getter
.def("__getitem__", [](Array4<T> & a4, IntVect const & v){ return a4(v); })
.def("__getitem__", [](Array4<T> & a4, std::array<int, 4> const key){
return a4(key[0], key[1], key[2], key[3]);
Expand All @@ -211,11 +206,25 @@ void make_Array4(py::module &m, std::string typestr)
})
;

// setter
if constexpr (is_not_const)
{
py_array4
.def("__setitem__", [](Array4<T> & a4, IntVect const & v, T const value){ a4(v) = value; })
.def("__setitem__", [](Array4<T> & a4, std::array<int, 4> const key, T const value){
a4(key[0], key[1], key[2], key[3]) = value;
})
.def("__setitem__", [](Array4<T> & a4, std::array<int, 3> const key, T const value){
a4(key[0], key[1], key[2]) = value;
})
;
}

// free standing C++ functions:
m.def("lbound", &lbound< Array4<T> >);
m.def("ubound", &ubound< Array4<T> >);
m.def("length", &length< Array4<T> >);
m.def("makePolymorphic", &makePolymorphic< Array4<T> >);
m.def("lbound", &lbound< T >);
m.def("ubound", &ubound< T >);
m.def("length", &length< T >);
//m.def("makePolymorphic", &makePolymorphic< T >);
}

void init_Array4(py::module &m) {
Expand All @@ -233,6 +242,20 @@ void init_Array4(py::module &m) {
make_Array4< unsigned long >(m, "ulong");
make_Array4< unsigned long long >(m, "ulonglong");

make_Array4< float const >(m, "float_const");
make_Array4< double const >(m, "double_const");
make_Array4< long double const >(m, "longdouble_const");

make_Array4< short const >(m, "short_const");
make_Array4< int const >(m, "int_const");
make_Array4< long const >(m, "long_const");
make_Array4< long long const >(m, "longlong_const");

make_Array4< unsigned short const >(m, "ushort_const");
make_Array4< unsigned int const >(m, "uint_const");
make_Array4< unsigned long const >(m, "ulong_const");
make_Array4< unsigned long long const >(m, "ulonglong_const");

// std::complex< float|double|long double> ?

/*
Expand Down
4 changes: 4 additions & 0 deletions src/Base/BoxArray.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
#include "pyAMReX.H"

#include "Base/Vector.H"

#include <AMReX_BoxArray.H>
#include <AMReX_IntVect.H>

Expand Down Expand Up @@ -281,4 +283,6 @@ void init_BoxArray(py::module &m) {
*/

;

make_Vector<BoxArray> (m, "BoxArray");
}
19 changes: 13 additions & 6 deletions src/Base/CoordSys.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
/* Copyright 2021-2023 The AMReX Community
*
* Authors: Axel Huebl, Ryan Sandberg
* License: BSD-3-Clause-LBNL
*/
#include "pyAMReX.H"

#include <AMReX_CoordSys.H>
Expand All @@ -8,6 +13,14 @@ void init_CoordSys(py::module& m)
using namespace amrex;

py::class_<CoordSys> coord_sys(m, "CoordSys");

py::enum_<CoordSys::CoordType>(coord_sys, "CoordType")
.value("undef", CoordSys::CoordType::undef)
.value("cartesian", CoordSys::CoordType::cartesian)
.value("RZ", CoordSys::CoordType::RZ)
.value("SPHERICAL", CoordSys::CoordType::SPHERICAL)
.export_values();

coord_sys.def("__repr__",
[](const CoordSys&) {
return "<amrex.CoordSys>";
Expand All @@ -27,10 +40,4 @@ void init_CoordSys(py::module& m)
// ...
;

py::enum_<CoordSys::CoordType>(coord_sys, "CoordType")
.value("undef", CoordSys::CoordType::undef)
.value("cartesian", CoordSys::CoordType::cartesian)
.value("RZ", CoordSys::CoordType::RZ)
.value("SPHERICAL", CoordSys::CoordType::SPHERICAL)
.export_values();
}
4 changes: 4 additions & 0 deletions src/Base/DistributionMapping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
#include "pyAMReX.H"

#include "Base/Vector.H"

#include <AMReX_BoxArray.H>
#include <AMReX_DistributionMapping.H>
#include <AMReX_Vector.H>
Expand Down Expand Up @@ -73,4 +75,6 @@ void init_DistributionMapping(py::module &m) {
return dm[index];
})
;

make_Vector<DistributionMapping> (m, "DistributionMapping");
}
4 changes: 3 additions & 1 deletion src/Base/FArrayBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,15 @@ void init_FArrayBox(py::module &m) {
.def(py::init< Arena* >())
.def(py::init< Box const &, int, Arena* >())
.def(py::init< Box const &, int, bool, bool, Arena* >())
.def(py::init< FArrayBox const &, MakeType, int, int >())
//.def(py::init< FArrayBox const &, MakeType, int, int >())
.def(py::init< Box const &, int, Real const* >())
.def(py::init< Box const &, int, Real* >())
.def(py::init< Array4<Real> const& >())
.def(py::init< Array4<Real> const&, IndexType >())
.def(py::init< Array4<Real const> const& >())
.def(py::init< Array4<Real const> const&, IndexType >())

/*
.def("read_from",
py::overload_cast<std::istream&>(&FArrayBox::readFrom),
py::arg("is")
Expand All @@ -51,5 +52,6 @@ void init_FArrayBox(py::module &m) {
py::overload_cast<std::ostream&, int, int>(&FArrayBox::writeOn, py::const_),
py::arg("of"), py::arg("comp"), py::arg("num_comp")
)
*/
;
}
4 changes: 4 additions & 0 deletions src/Base/Geometry.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "pyAMReX.H"

#include "Base/Vector.H"

#include <AMReX_Geometry.H>
#include <AMReX_CoordSys.H>
#include <AMReX_MultiFab.H>
Expand Down Expand Up @@ -205,4 +207,6 @@ void init_Geometry(py::module& m)
// .def("computeRoundoffDomain")
;


make_Vector<Geometry> (m, "Geometry");
}
14 changes: 7 additions & 7 deletions src/Base/IndexType.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Copyright 2021-2022 The AMReX Community
*
* Authors: David Grote
* Authors: David Grote, Axel Huebl
* License: BSD-3-Clause-LBNL
*/
#include "pyAMReX.H"
Expand Down Expand Up @@ -28,6 +28,12 @@ void init_IndexType(py::module &m) {
using namespace amrex;

py::class_< IndexType > index_type(m, "IndexType");

py::enum_<IndexType::CellIndex>(index_type, "CellIndex")
.value("CELL", IndexType::CellIndex::CELL)
.value("NODE", IndexType::CellIndex::NODE)
.export_values();

index_type.def("__repr__",
[](py::object& obj) {
py::str py_name = obj.attr("__class__").attr("__name__");
Expand Down Expand Up @@ -111,10 +117,4 @@ void init_IndexType(py::module &m) {
.def_static("node_type", &IndexType::TheNodeType)

;

py::enum_<IndexType::CellIndex>(index_type, "CellIndex")
.value("CELL", IndexType::CellIndex::CELL)
.value("NODE", IndexType::CellIndex::NODE)
.export_values();

}
4 changes: 4 additions & 0 deletions src/Base/IntVect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
*/
#include "pyAMReX.H"

#include "Base/Vector.H"

#include <AMReX_Dim3.H>
#include <AMReX_IntVect.H>

Expand Down Expand Up @@ -153,4 +155,6 @@ void init_IntVect(py::module &m)
py::overload_cast<const IntVect&, int>(&coarsen));
m.def("refine",
py::overload_cast<const Dim3&, const IntVect&>(&refine));

make_Vector<IntVect> (m, "IntVect");
}
Loading

0 comments on commit 4515577

Please sign in to comment.