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 Mesh.imshow() and FieldContainer.imshow() #598

Merged
merged 2 commits into from
Jan 10, 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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@ All notable changes to this project will be documented in this file. The format
### Added
- Add optional output location for `FieldContainer.extract(out=None)`.
- Add `Mesh.update(callback=None)`. This is especially useful if the points array of a mesh is changed and an already existing instance of a region has to be reloaded: `Mesh.update(points=new_points, callback=region.reload)`.
- Add `ax = FieldContainer.imshow()` which acts as a wrapper on top of the `img = FieldContainer.screenshot(filename=None)` method. The image data is passed to a matplotlib figure and the `ax` object is returned.
- Add `ax = Mesh.imshow()` which acts as a wrapper on top of the `img = Mesh.screenshot(filename=None)` method. The image data is passed to a matplotlib figure and the `ax` object is returned.

### Changed
- Pass optional keyword-arguments in `math.dot(**kwargs)` to the underlying einsum-calls.
- Enhance the performance of `math.cdya()` by reducing the number of (intermediate) arrays to be created from 4 to 2.
- Use fixed output locations for the extracted field-gradients and the integrated stiffness matrices in `SolidBody` and `SolidBodyNearlyIncompressible`. This enhances the performance.
- Change default filename in `Mesh.screenshot()` from `filename="field.png"` to `filename="mesh.png"`.

### Removed
- Do not invoke `pyvista.start_xvfb()` on a posix-os. If required, run it manually.
Expand Down
13 changes: 13 additions & 0 deletions src/felupe/field/_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,19 @@ def screenshot(
scale=scale,
)

def imshow(self, *args, **kwargs):
"""Take a screenshot of the first field of the container, show the image data in
a figure and return the ax.
"""

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.imshow(self.screenshot(*args, filename=None, **kwargs))
ax.set_axis_off()

return ax

def __add__(self, newvalues):
fields = deepcopy(self)
if len(newvalues) != len(self.fields):
Expand Down
15 changes: 14 additions & 1 deletion src/felupe/mesh/_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def plot(self, *args, **kwargs):
def screenshot(
self,
*args,
filename="field.png",
filename="mesh.png",
transparent_background=None,
scale=None,
**kwargs,
Expand All @@ -187,6 +187,19 @@ def screenshot(
scale=scale,
)

def imshow(self, *args, **kwargs):
"""Take a screenshot of the mesh, show the image data in a figure and return the
ax.
"""

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.imshow(self.screenshot(*args, filename=None, **kwargs))
ax.set_axis_off()

return ax

@wraps(dual)
def dual(
self,
Expand Down
Binary file added tests/field.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified tests/mesh.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions tests/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ def test_view():
field = fem.FieldContainer([fem.FieldPlaneStrain(region, dim=2)])
plotter = field.plot(off_screen=True)
# img = mesh.screenshot(transparent_background=True)
# ax = mesh.imshow()


if __name__ == "__main__":
Expand Down
1 change: 1 addition & 0 deletions tests/test_mesh.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,7 @@ def test_view():
view = mesh.view()
plotter = mesh.plot(off_screen=True)
# img = mesh.screenshot(transparent_background=True)
# ax = mesh.imshow()


def test_mesh_update():
Expand Down