Skip to content

Commit

Permalink
Merge pull request #22 from sigma-epsilon/17-customizable-cell-interp…
Browse files Browse the repository at this point in the history
…olators

17 customizable cell interpolators
  • Loading branch information
BALOGHBence committed Aug 27, 2023
2 parents 1962ef8 + fd0241d commit 08ab196
Show file tree
Hide file tree
Showing 18 changed files with 453 additions and 310 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ If you are a developer and want to install the library in development mode, the
>>> pip install "-e .[test, dev]"
```

## Versioning

The project follows the rules of [semantic versioning](https://semver.org/).

## How to contribute?

Contributions are currently expected in any the following ways:
Expand Down
4 changes: 2 additions & 2 deletions docs/source/examples/shape_functions_1d.ipynb

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@
}
],
"source": [
"from sigmaepsilon.mesh.cells import LagrangianCellInterpolator\n",
"from sigmaepsilon.mesh.cells import LagrangianCellApproximator\n",
"\n",
"interpolator = H8.interpolator()\n",
"isinstance(interpolator, LagrangianCellInterpolator)"
"approximator = H8.approximator()\n",
"isinstance(approximator, LagrangianCellApproximator)"
]
},
{
Expand Down Expand Up @@ -83,14 +83,14 @@
"source_values = [1, 2, 3, 4, 5, 6, 7, 8]\n",
"target_coordinates = master_coordinates\n",
"\n",
"interpolator(source=source_coordinates, values=source_values, target=target_coordinates)"
"approximator(source=source_coordinates, values=source_values, target=target_coordinates)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It is possible to pass the source coordinates to the factory function. This is useful if we plan to reuse the interpolator with the same source points and can save a little time. In this case only the source values and the target points need to be provided."
"It is possible to pass the source coordinates to the factory function. This is useful if we plan to reuse the approximator with the same source points and can save a little time. In this case only the source values and the target points need to be provided."
]
},
{
Expand All @@ -114,15 +114,15 @@
"source_values = [1, 2, 3, 4, 5, 6, 7, 8]\n",
"target_coordinates = master_coordinates\n",
"\n",
"interpolator = H8.interpolator(source_coordinates)\n",
"interpolator(values=source_values, target=target_coordinates)"
"approximator = H8.approximator(source_coordinates)\n",
"approximator(values=source_values, target=target_coordinates)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As noted in the documentation, if the number of source coorindates does not match the number of nodes (and hence the number of shape functions) of the master element of the class, the interpolation is gonna be under or overdetermined and the operation involves the calculation of a generalized inverse. This means, that for instance feeding a 4-noded quadrilateral with 9 source points and data values is more information than what the class is normally able to precisely handle and the resulting interpolator will represent a fitting function. In that case, if you want a precise interpolation, you would want to use a 9-node quadrilateral, or accept the loss of information."
"As noted in the documentation, if the number of source coorindates does not match the number of nodes (and hence the number of shape functions) of the master element of the class, the approximation is gonna be under or overdetermined and the operation involves the calculation of a generalized inverse. This means, that for instance feeding a 4-noded quadrilateral with 9 source points and data values is more information than what the class is normally able to precisely handle and the resulting approximator will represent a fitting function. In that case, if you want a precise approximation, you would want to use a 9-node quadrilateral, or accept the loss of information."
]
},
{
Expand Down Expand Up @@ -150,8 +150,8 @@
"source_values = [i + 1 for i in range(9)]\n",
"target_coordinates = master_coordinates\n",
"\n",
"interpolator = Q4.interpolator()\n",
"interpolator(source=source_coordinates, values=source_values, target=target_coordinates)"
"approximator = Q4.approximator()\n",
"approximator(source=source_coordinates, values=source_values, target=target_coordinates)"
]
},
{
Expand All @@ -178,8 +178,8 @@
}
],
"source": [
"interpolator = Q9.interpolator()\n",
"interpolator(source=source_coordinates, values=source_values, target=target_coordinates)"
"approximator = Q9.approximator()\n",
"approximator(source=source_coordinates, values=source_values, target=target_coordinates)"
]
},
{
Expand Down Expand Up @@ -213,8 +213,8 @@
"source_values = [i + 1 for i in range(3)]\n",
"target_coordinates = master_coordinates\n",
"\n",
"interpolator = L3.interpolator()\n",
"interpolator(source=source_coordinates, values=source_values, target=target_coordinates)"
"approximator = L3.approximator()\n",
"approximator(source=source_coordinates, values=source_values, target=target_coordinates)"
]
},
{
Expand All @@ -228,7 +228,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Set up an interpolator:"
"Set up an approximator:"
]
},
{
Expand All @@ -239,7 +239,7 @@
"source": [
"import numpy as np\n",
"\n",
"interpolator = H8.interpolator()\n",
"approximator = H8.approximator()\n",
"master_coordinates = H8.master_coordinates()\n",
"\n",
"source_coordinates = master_coordinates / 2\n",
Expand Down Expand Up @@ -271,7 +271,7 @@
],
"source": [
"source_values = np.random.rand(10, 2, 8)\n",
"interpolator(\n",
"approximator(\n",
" source=source_coordinates, \n",
" values=source_values, \n",
" target=target_coordinates[:3]\n",
Expand Down Expand Up @@ -303,7 +303,7 @@
],
"source": [
"source_values = np.random.rand(8, 2, 10)\n",
"interpolator(\n",
"approximator(\n",
" source=source_coordinates, \n",
" values=source_values, \n",
" target=target_coordinates[:3],\n",
Expand All @@ -317,6 +317,50 @@
"source": [
"The workaround here is to use `numpy.ascontiguousarray` and reordering the input data."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Custom cells"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Getting an approximator for a custom cell goes the same way, after the cell have been properly set up."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([-0.5, 1.5, 3.5, 5.5])"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from sigmaepsilon.mesh.cells.base import PolyCell1d\n",
"\n",
"Custom1dCell: PolyCell1d = PolyCell1d.generate_class(NNODE=4)\n",
"\n",
"master_coordinates = Custom1dCell.master_coordinates()\n",
"source_coordinates = master_coordinates / 2\n",
"source_values = [i + 1 for i in range(Custom1dCell.NNODE)]\n",
"target_coordinates = master_coordinates\n",
"\n",
"approximator = Custom1dCell.approximator()\n",
"approximator(source=source_coordinates, values=source_values, target=target_coordinates)"
]
}
],
"metadata": {
Expand Down
3 changes: 1 addition & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "sigmaepsilon.mesh"
version = "1.0.0"
version = "1.1.0"
description = "A Python package to build, manipulate and analyze polygonal meshes."
classifiers=[
"Development Status :: 5 - Production/Stable",
Expand Down Expand Up @@ -43,7 +43,6 @@ where = ["src"]
dependencies = { file = ["requirements.txt"] }
optional-dependencies.test = { file = ["requirements-test.txt"] }
optional-dependencies.dev = { file = ["requirements-dev.txt"] }
optional-dependencies.devops = { file = ["requirements-devops.txt"] }
optional-dependencies.docs = { file = ["docs/requirements.txt"] }

[project.urls]
Expand Down
16 changes: 6 additions & 10 deletions src/sigmaepsilon/mesh/celldata.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Union
from typing import Union, Iterable
from copy import copy, deepcopy
from functools import partial

Expand Down Expand Up @@ -51,11 +51,7 @@ class CellData(CellDataBase):
For every key and value pair where the value is a numpy array
with a matching shape (has entries for all cells), the key
is considered as a field and the value is added to the database.
See Also
--------
:class:`awkward.Array`
:class:`awkward.Record`
"""

_attr_map_ = {
Expand Down Expand Up @@ -290,7 +286,7 @@ def __getattr__(self, attr):
name = self.__class__.__name__
raise AttributeError(f"'{name}' object has no attribute called {attr}")

def set_nodal_distribution_factors(self, factors: ndarray, key: str = None):
def set_nodal_distribution_factors(self, factors: ndarray, key: str = None) -> None:
"""
Sets nodal distribution factors.
Expand Down Expand Up @@ -350,7 +346,7 @@ def pull(
return d

@property
def fields(self):
def fields(self) -> Iterable[str]:
"""Returns the fields in the database object."""
return self._wrapped.fields

Expand Down Expand Up @@ -400,7 +396,7 @@ def frames(self, value: ndarray):
self._wrapped[self._dbkey_frames_] = frames

@property
def t(self):
def t(self) -> ndarray:
"""Returns the thicknesses of the cells."""
return self._wrapped[self._dbkey_thickness_].to_numpy()

Expand All @@ -412,7 +408,7 @@ def t(self, value: Union[float, int, ndarray]):
self._wrapped[self._dbkey_thickness_] = value

@property
def A(self):
def A(self) -> ndarray:
"""Returns the thicknesses of the cells."""
return self._wrapped[self._dbkey_areas_].to_numpy()

Expand Down
4 changes: 2 additions & 2 deletions src/sigmaepsilon/mesh/cells/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from .w6 import W6 as Wedge
from .w18 import W18

from .base.interpolator import LagrangianCellInterpolator
from .base.approximator import LagrangianCellApproximator

__all__ = [
"L2",
Expand All @@ -41,5 +41,5 @@
"W6",
"Wedge",
"W18",
"LagrangianCellInterpolator"
"LagrangianCellApproximator"
]
9 changes: 9 additions & 0 deletions src/sigmaepsilon/mesh/cells/base/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from .cell1d import PolyCell1d
from .cell2d import PolyCell2d
from .cell3d import PolyCell3d

__all__ = [
"PolyCell1d",
"PolyCell2d",
"PolyCell3d",
]
Loading

0 comments on commit 08ab196

Please sign in to comment.