Skip to content

Commit

Permalink
Enhance math.linsteps(..., values=0.0) (#917)
Browse files Browse the repository at this point in the history
* Enhance `math.linsteps(..., values=0.0)`

* `BoundaryDict().plot()` pass missing `size` arg

* Use `BoundaryDict` in examples
  • Loading branch information
adtzlr authored Dec 8, 2024
1 parent ada2a2f commit be2f25c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 28 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ All notable changes to this project will be documented in this file. The format
- Autodetect the stress-type in `SolidBody.plot(name)` from `name`.
- Enhance the `hello_world(axisymmetric=False, planestrain=False, curve=False, xdmf=False, container=False)` function with new arguments to customize the generated template script.
- Enhance `Boundary` with added support for multiaxial prescribed values.
- Enhance `math.linsteps(..., values=0)` with default values except for the column `axis` if `axis` is not None.

### Fixed
- Fix `Boundary(..., mode="and")` by ignoring any undefined axis.
Expand Down
4 changes: 3 additions & 1 deletion examples/ex01_beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
dimensional vector-valued displacement field is initiated on the region.
"""

# sphinx_gallery_thumbnail_number = -1
import felupe as fem

cube = fem.Cube(a=(0, 0, 0), b=(2000, 100, 100), n=(101, 6, 6))
Expand All @@ -29,7 +30,8 @@

# %%
# A fixed boundary condition is applied on the left end of the beam.
boundaries = {"fixed": fem.dof.Boundary(displacement, fx=0)}
boundaries = fem.BoundaryDict(fixed=fem.dof.Boundary(displacement, fx=0))
boundaries.plot().show()

# %%
# The material behaviour is defined through a built-in isotropic linear-elastic material
Expand Down
3 changes: 1 addition & 2 deletions examples/ex02_plate-with-hole.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,6 @@
mesh = fem.mesh.concatenate([face, face.mirror(normal=[-1, 1, 0]), rect])
mesh = mesh.sweep(decimals=5)

mesh.plot().show()

# %%
# A numeric quad-region created on the mesh in combination with a vector-valued
# displacement field represents the plate. The Boundary conditions for the symmetry
Expand All @@ -58,6 +56,7 @@
field = fem.FieldContainer([displacement])

boundaries = fem.dof.symmetry(displacement)
boundaries.plot().show()

# %%
# The material behaviour is defined through a built-in isotropic linear-elastic material
Expand Down
4 changes: 3 additions & 1 deletion examples/ex03_plasticity.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
numeric region.
"""

# sphinx_gallery_thumbnail_number = -1
import numpy as np

import felupe as fem
Expand All @@ -34,7 +35,8 @@

# %%
# A fixed boundary condition is applied at :math:`x=0`.
boundaries = {"fixed": fem.dof.Boundary(displacement, fx=0)}
boundaries = fem.BoundaryDict(fixed=fem.dof.Boundary(displacement, fx=0))
boundaries.plot().show()

# %%
# The material behaviour is defined through a built-in isotropic linear-elastic plastic
Expand Down
11 changes: 5 additions & 6 deletions examples/ex05_rubber-metal-bushing.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@
mesh = fem.mesh.stack(meshes.meshes)
x, y, z = mesh.points.T

mesh.plot().show()

# %%
# A global region as well as sub-regions for all materials are generated. The same
# applies to the fields, the material formulations as well as the solid bodies.
Expand All @@ -78,10 +76,11 @@
# %%
# The boundary conditions are created on the global displacement field. Masks are
# created for both the innermost and the outermost metal sheet faces.
boundaries = {
"inner": fem.dof.Boundary(field[0], mask=np.isclose(np.sqrt(y**2 + z**2), 25)),
"outer": fem.dof.Boundary(field[0], mask=np.isclose(np.sqrt(y**2 + z**2), 65)),
}
boundaries = fem.BoundaryDict(
inner=fem.dof.Boundary(field[0], mask=np.isclose(np.sqrt(y**2 + z**2), 25)),
outer=fem.dof.Boundary(field[0], mask=np.isclose(np.sqrt(y**2 + z**2), 65)),
)
boundaries.plot(show_lines=False).show()

# prescribed values for the innermost radial mesh points
table = fem.math.linsteps([0, 1], num=3)
Expand Down
5 changes: 3 additions & 2 deletions examples/ex07_engine-mount.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
# sub-meshes with shared points-array and a global mesh
meshes = fem.MeshContainer([metal, rubber, air], merge=True)
mesh = fem.mesh.concatenate(meshes).sweep()
meshes.plot(colors=["grey", "black", "white"]).show()

# %%
# A global region as well as sub-regions for all materials are generated. The same
Expand All @@ -65,11 +64,13 @@
inner = np.logical_and(only_cells_metal, radius <= 45)
outer = np.logical_and(only_cells_metal, radius > 45)

boundaries = dict(
boundaries = fem.BoundaryDict(
fixed=fem.Boundary(field[0], mask=outer),
u_x=fem.Boundary(field[0], mask=inner, skip=(0, 1)),
u_y=fem.Boundary(field[0], mask=inner, skip=(1, 0)),
)
plotter = meshes.plot(colors=["grey", "black", "white"])
boundaries.plot(plotter=plotter, scale=0.02).show()

# %%
# The material behaviour of the rubberlike solid is defined through a built-in hyperelastic
Expand Down
10 changes: 5 additions & 5 deletions examples/ex08_shear.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@
region = fem.RegionQuad(mesh)
field = fem.FieldsMixed(region, n=3, planestrain=True)

boundaries = {
"fixed": fem.Boundary(field[0], fy=mesh.y.min()),
"control": fem.Boundary(field[0], fy=mesh.y.max(), skip=(0, 1)),
}
boundaries = fem.BoundaryDict(
fixed=fem.Boundary(field[0], fy=mesh.y.min()),
control=fem.Boundary(field[0], fy=mesh.y.max(), skip=(0, 1)),
)

dof0, dof1 = fem.dof.partition(field, boundaries)

Expand Down Expand Up @@ -97,7 +97,7 @@
centerpoint=mesh.npoints - 1,
)

plotter = mesh.plot()
plotter = boundaries.plot()
plotter = mpc.plot(plotter=plotter)
plotter.show()

Expand Down
1 change: 1 addition & 0 deletions src/felupe/dof/_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def plot(
label=label,
color=color,
plotter=plotter,
size=size,
show_points=show_points,
show_lines=show_lines,
**kwargs,
Expand Down
25 changes: 14 additions & 11 deletions src/felupe/math/_math.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
import numpy as np


def linsteps(points, num=10, endpoint=True, axis=None, axes=None):
def linsteps(points, num=10, endpoint=True, axis=None, axes=None, values=0.0):
"""Return a sequence from batches of evenly spaced samples between pairs of
milestone points.
Expand All @@ -40,6 +40,9 @@ def linsteps(points, num=10, endpoint=True, axis=None, axes=None):
axes : int or None, optional
Number of output columns if ``axis`` is not None. Requires ``axes > axis`` and
will be set to ``axes = axis + 1`` by default (None).
values : array_like, optional
Default values if ``axis`` is not None (not used for column ``axis``). Default
is 0.0.
Returns
-------
Expand All @@ -49,7 +52,7 @@ def linsteps(points, num=10, endpoint=True, axis=None, axes=None):
Examples
--------
>>> import felupe as fem
>>>
>>> fem.math.linsteps([0, 0.5, 1.5, 3.5], num=2)
array([0. , 0.25, 0.5 , 1. , 1.5 , 2.5 , 3.5 ])
Expand All @@ -58,14 +61,14 @@ def linsteps(points, num=10, endpoint=True, axis=None, axes=None):
>>> fem.math.linsteps([0, 0.5, 1.5, 3.5], num=2, endpoint=False)
array([0. , 0.25, 0.5 , 1. , 1.5 , 2.5 ])
>>> fem.math.linsteps([0, 0.5, 1.5, 3.5], num=2, axis=1)
array([[0. , 0. ],
[0. , 0.25],
[0. , 0.5 ],
[0. , 1. ],
[0. , 1.5 ],
[0. , 2.5 ],
[0. , 3.5 ]])
>>> fem.math.linsteps([0, 0.5, 1.5, 3.5], num=2, axis=1, values=[-1, 0])
array([[-1. , 0. ],
[-1. , 0.25],
[-1. , 0.5 ],
[-1. , 1. ],
[-1. , 1.5 ],
[-1. , 2.5 ],
[-1. , 3.5 ]])
Output with four columns:
Expand Down Expand Up @@ -120,7 +123,7 @@ def linsteps(points, num=10, endpoint=True, axis=None, axes=None):
axes = axis + 1

steps_1d = steps
steps = np.zeros((len(steps_1d), axes))
steps = np.ones((len(steps_1d), axes)) * np.atleast_2d(values)
steps[:, axis] = steps_1d

return steps

0 comments on commit be2f25c

Please sign in to comment.