Skip to content
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 MultiPointContact.plot(**kwargs) method #718

Merged
merged 3 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions examples/ex06_rubber-metal-spring.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,13 @@ def log_strain(field, substep=None):
point_data={"Logarithmic Strain (Max. Principal)": log_strain},
tol=1e-1,
)
field.view(
view = field.view(
point_data={"Logarithmic Strain (Max. Principal)": log_strain(field)},
).plot("Logarithmic Strain (Max. Principal)").show()
)
plotter = view.plot("Logarithmic Strain (Max. Principal)")
plotter = top.plot(plotter=plotter, offset=1e-1)
plotter = bottom.plot(plotter=plotter, offset=-1e-1)
plotter.show()

# %%
# The axial-compressive and lateral-shear force-displacement curves are obtained from
Expand Down
41 changes: 41 additions & 0 deletions src/felupe/mechanics/_multipoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,47 @@ def __init__(
self.results = Results(stress=False, elasticity=False)
self.assemble = Assemble(vector=self._vector, matrix=self._matrix)

def plot(
self,
plotter=None,
offset=0,
show_edges=True,
color="black",
opacity=0.5,
**kwargs
):
import pyvista as pv

if plotter is None:
plotter = pv.Plotter()

# get edge lengths of deformed enclosing box
x = self.mesh.points + self.field[0].values
edges = np.diag((x.max(axis=0) - x.min(axis=0))) + x.min(axis=0)

# plot a line or a rectangle for each active contact plane
for ax in self.axes:
# fill the point values of the normal axis with the centerpoint values
points = edges.copy()
points[:, ax] = x[self.centerpoint, ax] + offset

# scale the line or rectangle at the origin
origin = points.mean(axis=0)
points = (points - origin) * 1.05 + origin

# plot a line or a rectangle
if len(points) == 3:
plotter.add_mesh(
pv.Rectangle(points), color=color, opacity=opacity, **kwargs
)
else:
points = np.pad(points, ((0, 0), (0, 3 - points.shape[1])))
plotter.add_mesh(
pv.Line(*points), color=color, opacity=opacity, **kwargs
)

return plotter

def _vector(self, field=None, parallel=False):
"Calculate vector of residuals with RBE2 contributions."

Expand Down
18 changes: 18 additions & 0 deletions tests/test_mpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,11 @@ def pre_mpc_mixed(point, values):
RBE2 = fem.MultiPointConstraint(fields, points=mpc, centerpoint=cpoint)
CONT = fem.MultiPointContact(fields, points=mpc, centerpoint=cpoint)

try:
CONT.plot()
except ModuleNotFoundError:
pass

for f in [None, fields]:
K_RBE2 = RBE2.assemble.matrix(f)
r_RBE2 = RBE2.assemble.vector(f)
Expand Down Expand Up @@ -232,7 +237,20 @@ def test_mpc_isolated():
)


def test_mpc_plot_2d():
mesh = fem.Rectangle(n=3)
field = fem.FieldContainer([fem.FieldPlaneStrain(fem.RegionQuad(mesh), dim=2)])
plane = fem.MultiPointContact(field, [0, 1], -1, skip=(0, 1))

try:
plotter = mesh.plot(off_screen=True)
plane.plot(plotter=plotter, line_width=8)
except ModuleNotFoundError:
pass


if __name__ == "__main__":
test_mpc()
test_mpc_mixed()
test_mpc_isolated()
test_mpc_plot_2d()
Loading