Visualizing a Three-Dimensional Function

I’ve isolated the question to your actual query: How to use numpy.meshgrid with a 1D DOLFINx problem:


import matplotlib.pyplot as plt

import numpy as np

import ufl
from dolfinx.fem import Function, FunctionSpace
from dolfinx import mesh

from mpi4py import MPI


dt = 0.1  # time step


msh = mesh.create_interval(MPI.COMM_WORLD, 3, points=(0, 1))
P1 = ufl.FiniteElement("Lagrange", msh.ufl_cell(), 1)
ME = FunctionSpace(msh, P1 * P1)
u = Function(ME)

t = 0.0
T = 40 * dt

V0, dofs = ME.sub(0).collapse()
coords = V0.tabulate_dof_coordinates()[:, 0]
sort_coords = np.argsort(coords)

values = []
times = []
while (t < T):
    t += dt

    u.sub(0).interpolate(lambda x: np.cos(t)*x[0] + np.cos(x[0]))
    times.append(t)
    values.append(u.x.array[dofs][sort_coords])

values = np.asarray(values)

X, Y = np.meshgrid(coords[sort_coords], times)
Z = np.asarray(values)

fig = plt.figure()
ax = plt.axes(projection='3d')
ax.contour3D(X, Y, Z, 50, cmap='binary')
ax.set_xlabel('x')
ax.set_ylabel('t')
ax.set_zlabel('c')
plt.savefig("test.png")

Which illustrates your problem with a mixed function space, where there is a time dependent update on the first component of the mixed space.

test

2 Likes