Skip to content

Commit

Permalink
Adding to Mesh: nodaltyings, node2dof, and periodic overload of elem2…
Browse files Browse the repository at this point in the history
…node
  • Loading branch information
tdegeus committed Feb 14, 2022
1 parent d31b566 commit 23bc93f
Show file tree
Hide file tree
Showing 6 changed files with 283 additions and 19 deletions.
14 changes: 7 additions & 7 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
repos:
- repo: https://github.com/psf/black
rev: 21.9b0
rev: 22.1.0
hooks:
- id: black
args: [--safe, --quiet, --line-length=100]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.0.1
rev: v4.1.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -20,13 +20,13 @@ repos:
rev: v1.1
hooks:
- id: autoflake
args: [--in-place, --remove-all-unused-imports, --remove-unused-variable]
args: [--in-place, --remove-unused-variable, --remove-all-unused-imports]
- repo: https://github.com/asottile/reorder_python_imports
rev: v2.6.0
rev: v2.7.1
hooks:
- id: reorder-python-imports
- repo: https://github.com/asottile/pyupgrade
rev: v2.29.0
rev: v2.31.0
hooks:
- id: pyupgrade
args: [--py36-plus]
Expand All @@ -36,11 +36,11 @@ repos:
- id: flake8
args: [--max-line-length=100]
- repo: https://github.com/asottile/setup-cfg-fmt
rev: v1.18.0
rev: v1.20.0
hooks:
- id: setup-cfg-fmt
- repo: https://github.com/pocc/pre-commit-hooks
rev: v1.3.4
rev: v1.3.5
hooks:
- id: clang-format
args: [-i]
33 changes: 30 additions & 3 deletions include/GooseFEM/Mesh.h
Original file line number Diff line number Diff line change
Expand Up @@ -1404,6 +1404,15 @@ For example for 3 nodes in 2 dimensions the output is
*/
inline xt::xtensor<size_t, 2> dofs(size_t nnode, size_t ndim);

/**
List nodal tyings based on DOF-numbers per node.
\param dofs DOFs per node [nnode, ndim].
\return Nodes to which the nodes is connected (sorted) [nnode, ...]
*/
template <class D>
inline std::vector<std::vector<size_t>> nodaltyings(const D& dofs);

/**
Number of elements connected to each node.
Expand All @@ -1413,16 +1422,34 @@ Number of elements connected to each node.
template <class E>
inline xt::xtensor<size_t, 1> coordination(const E& conn);

/**
Nodes connected to each DOF.
\param dofs DOFs per node [nnode, ndim].
\param sorted If ``true`` the list of nodes for each DOF is sorted.
\return Nodes per DOF [ndof, ...].
*/
template <class D>
inline std::vector<std::vector<size_t>> node2dof(const D& dofs, bool sorted = true);

/**
Elements connected to each node.
\param conn Connectivity.
\param sorted If ``true`` the output is sorted.
\return Elements per node.
\param conn Connectivity [nelem, nne].
\param sorted If ``true`` the list of elements for each node is sorted.
\return Elements per node [nnode, ...].
*/
template <class E>
inline std::vector<std::vector<size_t>> elem2node(const E& conn, bool sorted = true);

/**
\copydoc elem2node(const E&, bool)
\param dofs DOFs per node, allowing accounting for periodicity [nnode, ndim].
*/
template <class E, class D>
inline std::vector<std::vector<size_t>> elem2node(const E& conn, const D& dofs, bool sorted = true);

/**
Return size of each element edge.
Expand Down
63 changes: 63 additions & 0 deletions include/GooseFEM/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1609,6 +1609,30 @@ inline xt::xtensor<size_t, 2> dofs(size_t nnode, size_t ndim)
return xt::reshape_view(xt::arange<size_t>(nnode * ndim), {nnode, ndim});
}

template <class D>
inline std::vector<std::vector<size_t>> nodaltyings(const D& dofs)
{
size_t nnode = dofs.shape(0);
size_t ndim = dofs.shape(1);
auto nodemap = node2dof(dofs);
std::vector<std::vector<size_t>> ret(nnode);

for (size_t m = 0; m < nnode; ++m) {
auto r = nodemap[dofs(m, 0)];
std::sort(r.begin(), r.end());
ret[m] = r;
#ifdef GOOSEFEM_ENABLE_ASSERT
for (size_t i = 1; i < ndim; ++i) {
auto t = nodemap[dofs(m, i)];
std::sort(t.begin(), t.end());
GOOSEFEM_ASSERT(std::equal(r.begin(), r.end(), t.begin()));
}
#endif
}

return ret;
}

template <class E>
inline xt::xtensor<size_t, 1> coordination(const E& conn)
{
Expand All @@ -1625,6 +1649,12 @@ inline xt::xtensor<size_t, 1> coordination(const E& conn)
return N;
}

template <class D>
inline std::vector<std::vector<size_t>> node2dof(const D& dofs, bool sorted)
{
return elem2node(dofs, sorted);
}

template <class E>
inline std::vector<std::vector<size_t>> elem2node(const E& conn, bool sorted)
{
Expand All @@ -1651,6 +1681,39 @@ inline std::vector<std::vector<size_t>> elem2node(const E& conn, bool sorted)
return ret;
}

template <class E, class D>
inline std::vector<std::vector<size_t>> elem2node(const E& conn, const D& dofs, bool sorted)
{
size_t nnode = dofs.shape(0);
auto ret = elem2node(conn, sorted);
auto nties = nodaltyings(dofs);

for (size_t m = 0; m < nnode; ++m) {
if (nties[m].size() <= 1) {
continue;
}
if (m > nties[m][0]) {
continue;
}
size_t n = ret[m].size();
for (size_t j = 1; j < nties[m].size(); ++j) {
n += ret[nties[m][j]].size();
}
ret[m].reserve(n);
for (size_t j = 1; j < nties[m].size(); ++j) {
ret[m].insert(ret[m].end(), ret[nties[m][j]].begin(), ret[nties[m][j]].end());
}
if (sorted) {
std::sort(ret[m].begin(), ret[m].end());
}
for (size_t j = 1; j < nties[m].size(); ++j) {
ret[nties[m][j]] = ret[m];
}
}

return ret;
}

template <class C, class E>
inline xt::xtensor<double, 2> edgesize(const C& coor, const E& conn, ElementType type)
{
Expand Down
12 changes: 4 additions & 8 deletions include/GooseFEM/version.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Current version.
- Define externally using::
-DGOOSEFEM_VERSION="`python -c "from setuptools_scm import get_version;
print(get_version())"`"
MYVERSION=`python -c "from setuptools_scm import get_version; print(get_version())"`
-DGOOSEFEM_VERSION="$MYVERSION"
From the root of this project. This is what ``setup.py`` does.
Expand All @@ -38,12 +38,8 @@ overwrite the automatic version.
namespace GooseFEM {

/**
Return version string, e.g.::
"0.8.0"
\return std::string
Return version string, e.g. `"0.8.0"`
\return String.
*/
inline std::string version();

Expand Down
25 changes: 24 additions & 1 deletion python/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,12 @@ void init_Mesh(py::module& mod)
py::arg("nnode"),
py::arg("ndim"));

mod.def(
"nodaltyings",
&GooseFEM::Mesh::nodaltyings<xt::pytensor<size_t, 2>>,
"See :cpp:func:`GooseFEM::Mesh::nodaltyings`.",
py::arg("dofs"));

mod.def(
"renumber",
&GooseFEM::Mesh::renumber<xt::pytensor<size_t, 2>>,
Expand All @@ -403,11 +409,28 @@ void init_Mesh(py::module& mod)
"See :cpp:func:`GooseFEM::Mesh::coordination`.",
py::arg("conn"));

mod.def(
"node2dof",
&GooseFEM::Mesh::node2dof<xt::pytensor<size_t, 2>>,
"See :cpp:func:`GooseFEM::Mesh::node2dof`.",
py::arg("dofs"),
py::arg("sorted") = true);

mod.def(
"elem2node",
py::overload_cast<const xt::pytensor<size_t, 2>&, bool>(
&GooseFEM::Mesh::elem2node<xt::pytensor<size_t, 2>>),
"See :cpp:func:`GooseFEM::Mesh::elem2node`.",
py::arg("conn"),
py::arg("sorted") = true);

mod.def(
"elem2node",
&GooseFEM::Mesh::elem2node<xt::pytensor<size_t, 2>>,
py::overload_cast<const xt::pytensor<size_t, 2>&, const xt::pytensor<size_t, 2>&, bool>(
&GooseFEM::Mesh::elem2node<xt::pytensor<size_t, 2>, xt::pytensor<size_t, 2>>),
"See :cpp:func:`GooseFEM::Mesh::elem2node`.",
py::arg("conn"),
py::arg("dofs"),
py::arg("sorted") = true);

mod.def(
Expand Down
Loading

0 comments on commit 23bc93f

Please sign in to comment.