Get FEM, save plot as PNG?

93 views Asked by At

I am using the python bindings for getfem, to that effect I wrote this script, following their tutorial:

import getfem as gf
import numpy as np
import math


center = [0.0, 0.0]
dir = [0.0, 1.0]
radius = 1.0
angle = 0.2 * math.pi

mo = gf.MesherObject("cone", center, dir, radius, angle)

h = 0.1
K = 2
mesh = gf.Mesh("generate", mo, h, K)

outer_faces = mesh.outer_faces()
OUTER_BOUND = 1
mesh.set_region(OUTER_BOUND, outer_faces)

sl = gf.Slice(("none",), mesh, 1)

mfu = gf.MeshFem(mesh, 1)
elements_degree = 2
mfu.set_classical_fem(elements_degree)

mim = gf.MeshIm(mesh, pow(elements_degree, 2))

md = gf.Model("real")
md.add_fem_variable("u", mfu)

md.add_Laplacian_brick(mim, "u")
F = 1.0
md.add_fem_data("F", mfu)

md.set_variable("F", np.repeat(F, mfu.nbdof()))
md.add_source_term_brick(mim, "u", "F")

md.add_Dirichlet_condition_with_multipliers(mim, "u", elements_degree - 1, OUTER_BOUND)
md.solve()

U = md.variable("u")
sl.export_to_vtk("u.vtk", "ascii", mfu, U, "U")

This exports a vtk file. Somewhere, I found a way to display it on a Jupyter notebook:

import pyvista as pv
from pyvirtualdisplay import Display

display = Display(visible=0, size=(1280, 1024))
display.start()
p = pv.Plotter()
m = pv.read("u.vtk")
contours = m.contour()
p.add_mesh(m, show_edges=False)
p.add_mesh(contours, color="black", line_width=1)
p.add_mesh(m.contour(8).extract_largest(), opacity=0.1)
pts = m.points
p.show(window_size=[384, 384], cpos="xy")

display.stop()

enter image description here

It looks awfully compressed for some reason. I am trying to save it as a PNG instead.

Does anyone know how to convert the vtk into a png? Paraview is depercated in modern systems for all intents and purposes so that's out the gate.

1

There are 1 answers

0
Alex Kaszynski On

You can generate a screenshot without displaying it with:

import pyvista as pv
from pyvirtualdisplay import Display

display = Display(visible=0, size=(1280, 1024))
display.start()
p = pv.Plotter(off_screen=True)
m = pv.read("u.vtk")
contours = m.contour()
p.add_mesh(m, show_edges=False)
p.add_mesh(contours, color="black", line_width=1)
p.add_mesh(m.contour(8).extract_largest(), opacity=0.1)
pts = m.points
p.show(cpos="xy", screenshot='screenshot.png')

display.stop()