-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add IndexType #139
Merged
Merged
Add IndexType #139
Changes from 8 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
cfaf202
Add IndexType
dpgrote 3962f02
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 762ef2d
Add more BoxArray features
dpgrote c46639e
Merge branch 'development' into add_IndexType
dpgrote 2754346
Add checking of index values
dpgrote 6f88f79
Bug fix for flip
dpgrote a861ce7
Add tests/test_indextype.py
dpgrote 1e8b9d1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] b23e023
Clean up in test_indextype.py
dpgrote 9b98548
Remove unneeded const
dpgrote 6a7996a
More clean up
dpgrote File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* Copyright 2021-2022 The AMReX Community | ||
* | ||
* Authors: David Grote | ||
* License: BSD-3-Clause-LBNL | ||
*/ | ||
#include <AMReX_Config.H> | ||
#include <AMReX_Dim3.H> | ||
#include <AMReX_IntVect.H> | ||
#include <AMReX_IndexType.H> | ||
|
||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/numpy.h> | ||
|
||
#include <array> | ||
#include <sstream> | ||
#include <string> | ||
|
||
namespace py = pybind11; | ||
using namespace amrex; | ||
|
||
namespace { | ||
const int check_index(const int i) | ||
{ | ||
const int ii = (i >= 0) ? i : AMREX_SPACEDIM + i; | ||
if ((ii < 0) || (ii >= AMREX_SPACEDIM)) | ||
throw py::index_error( "IndexType index " + std::to_string(i) + " out of bounds"); | ||
return ii; | ||
} | ||
} | ||
|
||
void init_IndexType(py::module &m) { | ||
py::class_< IndexType > index_type(m, "IndexType"); | ||
index_type.def("__repr__", | ||
[](py::object& obj) { | ||
py::str py_name = obj.attr("__class__").attr("__name__"); | ||
const std::string name = py_name; | ||
const auto iv = obj.cast<IndexType>(); | ||
std::stringstream s; | ||
s << iv; | ||
return "<amrex." + name + " " + s.str() + ">"; | ||
} | ||
) | ||
.def("__str", | ||
[](const IndexType& iv) { | ||
std::stringstream s; | ||
s << iv; | ||
return s.str(); | ||
}) | ||
|
||
.def(py::init<>()) | ||
.def(py::init<IndexType>()) | ||
#if (AMREX_SPACEDIM > 1) | ||
.def(py::init<AMREX_D_DECL(IndexType::CellIndex, IndexType::CellIndex, IndexType::CellIndex)>()) | ||
#endif | ||
|
||
.def("__getitem__", | ||
[](const IndexType& v, const int i) { | ||
const int ii = check_index(i); | ||
return v[ii]; | ||
}) | ||
|
||
.def("__len__", [](IndexType const &) { return AMREX_SPACEDIM; }) | ||
.def("__eq__", | ||
py::overload_cast<const IndexType&>(&IndexType::operator==, py::const_)) | ||
.def("__ne__", | ||
py::overload_cast<const IndexType&>(&IndexType::operator!=, py::const_)) | ||
.def("__lt__", &IndexType::operator<) | ||
|
||
.def("set", [](IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
v.set(ii); | ||
}) | ||
.def("unset", [](IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
v.unset(ii); | ||
}) | ||
.def("test", [](const IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
return v.test(ii); | ||
}) | ||
.def("setall", &IndexType::setall) | ||
.def("clear", &IndexType::clear) | ||
.def("any", &IndexType::any) | ||
.def("ok", &IndexType::ok) | ||
.def("flip", [](IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
v.flip(ii); | ||
}) | ||
|
||
.def("cell_centered", py::overload_cast<>(&IndexType::cellCentered, py::const_)) | ||
.def("cell_centered", [](const IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
return v.cellCentered(ii); | ||
}) | ||
.def("node_centered", py::overload_cast<>(&IndexType::nodeCentered, py::const_)) | ||
.def("node_centered", [](const IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
return v.nodeCentered(ii); | ||
}) | ||
|
||
.def("set_type", [](IndexType& v, int i, IndexType::CellIndex t) { | ||
const int ii = check_index(i); | ||
v.setType(ii, t); | ||
}) | ||
.def("ix_type", py::overload_cast<>(&IndexType::ixType, py::const_)) | ||
.def("ix_type", [](const IndexType& v, int i) { | ||
const int ii = check_index(i); | ||
return v.ixType(ii); | ||
}) | ||
.def("to_IntVect", &IndexType::toIntVect) | ||
|
||
.def_static("cell_type", &IndexType::TheCellType) | ||
.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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
import numpy as np | ||
Check notice Code scanning / CodeQL Unused import Note test
Import of 'np' is not used.
|
||
import pytest | ||
|
||
import amrex.space3d as amr | ||
|
||
|
||
@pytest.mark.skipif(amr.Config.spacedim != 1, reason="Requires AMREX_SPACEDIM = 1") | ||
def test_indextype_1d(): | ||
obj = amr.IndexType(amr.IndexType.CellIndex.NODE) | ||
assert obj.node_centered() | ||
assert not obj.cell_centered() | ||
with pytest.raises(IndexError): | ||
obj[-2] | ||
|
||
with pytest.raises(IndexError): | ||
obj[1] | ||
|
||
|
||
|
||
@pytest.mark.skipif(amr.Config.spacedim != 2, reason="Requires AMREX_SPACEDIM = 2") | ||
def test_indextype_2d(): | ||
obj = amr.IndexType(amr.IndexType.CellIndex.NODE, amr.IndexType.CellIndex.CELL) | ||
assert obj.node_centered(0) | ||
assert obj.cell_centered(1) | ||
assert obj.node_centered(-2) | ||
assert obj.cell_centered(-1) | ||
|
||
with pytest.raises(IndexError): | ||
obj[-3] | ||
|
||
with pytest.raises(IndexError): | ||
obj[2] | ||
|
||
|
||
|
||
@pytest.mark.skipif(amr.Config.spacedim != 3, reason="Requires AMREX_SPACEDIM = 3") | ||
def test_indextype_3d(): | ||
obj = amr.IndexType( | ||
amr.IndexType.CellIndex.NODE, | ||
amr.IndexType.CellIndex.CELL, | ||
amr.IndexType.CellIndex.NODE, | ||
) | ||
|
||
# Check indexing | ||
assert obj.node_centered(0) | ||
assert obj.cell_centered(1) | ||
assert obj.node_centered(2) | ||
assert obj.node_centered(-3) | ||
assert obj.cell_centered(-2) | ||
assert obj.node_centered(-1) | ||
with pytest.raises(IndexError): | ||
obj[-4] | ||
|
||
with pytest.raises(IndexError): | ||
obj[3] | ||
|
||
|
||
# Check methods | ||
obj.set(1) | ||
assert obj.node_centered() | ||
obj.unset(1) | ||
assert not obj.node_centered() | ||
|
||
|
||
def test_indextype_static(): | ||
cell = amr.IndexType.cell_type() | ||
for i in range(amr.Config.spacedim): | ||
assert not cell[i] | ||
|
||
node = amr.IndexType.node_type() | ||
for i in range(amr.Config.spacedim): | ||
assert node[i] | ||
|
||
assert cell == amr.IndexType.cell_type() | ||
assert node == amr.IndexType.node_type() | ||
assert cell < node | ||
|
||
|
||
def test_indextype_conversions(): | ||
node = amr.IndexType.node_type() | ||
assert node.ix_type() == amr.IntVect(1) | ||
assert node.to_IntVect() == amr.IntVect(1) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / CodeQL
Constant return type Warning