How to impose a pressure distribution from a data file as a boundary condition (loading)?

Consider the following:

from dolfin import *
import numpy as np
mesh = UnitSquareMesh(1, 1)

V = FunctionSpace(mesh, "CG", 1)


pressure_data = np.array([0.1, 0.3, 0.4, 0.8])
pressure_coordinates = np.array([[1., 1.], [0, 0], [0., 1.], [1., 0]])
ind2 = np.lexsort((pressure_coordinates[:, 1], pressure_coordinates[:, 0]))


p = Function(V)
dof_coords = V.tabulate_dof_coordinates()
ind = np.lexsort((dof_coords[:, 1], dof_coords[:, 0]))

for (in_index, p_index) in zip(ind2, ind):
    p.vector()[p_index] = pressure_data[in_index]

with XDMFFile("p.xdmf") as xdmf:
    xdmf.write(p)
2 Likes