Impose Velocity Profile on Inlet Face using Array

Is the velocity profile going to vary both in space and time (is it constant per time step?). If it is constant per time step, I do not see your issue. If it is varying in time, is the data points ordered from lowest y-value to highest y-value, and is the number of data points in the numpy array one to one with the number of dofs in the function space?

If so, you could do the following:

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

V = VectorFunctionSpace(mesh, "CG", 2)
u_inlet = Function(V)

# As we have a four cells along each side,
# we have 9 degrees of freedom in a "CG-2" space along the left side
values_x = np.array([0.1,0.3,0.5,0.8,1.1,0.8,0.7,0.3,0.2])
values_y = np.zeros(9)
dof_coords = V.tabulate_dof_coordinates()

# Identify coordinates where x[0]=0
dofs_on_left_side = np.flatnonzero(np.isclose(dof_coords[:,0], 0))
left_dofs_coords = dof_coords[dofs_on_left_side]
#Sort these coord by increasing y-coordinate
sorted_indices = np.argsort(left_dofs_coords[:,1])

# From the sorted dofs, decide which is the x and y component of the space
x_dofs = V.sub(0).dofmap().dofs()
y_dofs = V.sub(1).dofmap().dofs()
x_c, y_c = 0, 0
for index in sorted_indices:
    dof = dofs_on_left_side[index]
    if dof in x_dofs:
        u_inlet.vector().vec().setValueLocal(dof, values_x[x_c])
        x_c +=1
    elif dof in y_dofs:
        u_inlet.vector().vec().setValueLocal(dof, values_y[y_c])
        y_c +=1
XDMFFile("u_inlet.xdmf").write_checkpoint(u_inlet, "u", 0)

And repeat this insertion into u_inlet once per time step (which you can have assigned to a boundary condition).

2 Likes