Thank you. I’m still a little lost as I’m fairly new to Fenics. I’ve made the updates you suggested but still having some trouble. It might help to see the full code.
# =====================================================================
# Create mesh
# =====================================================================
channel = Rectangle(Point(0, 0), Point(14, 0.6))
domain = channel
mesh = generate_mesh(domain, 64)
# =====================================================================
# Define function spaces
# =====================================================================
V_e = VectorElement("CG", mesh.ufl_cell(), 1)
Q_e = FiniteElement("CG", mesh.ufl_cell(), 1)
W_e = V_e * Q_e
W = FunctionSpace(mesh, W_e)
# =====================================================================
# COLLECT BOUNDARY CONDITIONS
# =====================================================================
inflow = 'near(x[0], 0)'
outflow = 'near(x[0], 14)'
walls = 'near(x[1], 0) || near(x[1], 0.6)'
u_inlet = Function(W.sub(0).collapse())
u_inlet.vector()[:] = 1
# Define boundary conditions
bcu_inflow = DirichletBC(W.sub(0), u_inlet, inflow)
# free to slip walls
bcu_walls = DirichletBC(W.sub(0).sub(1), Constant(0), walls)
#bcp_outflow = DirichletBC(W.sub(1), outflow_profile, outflow)
bcs = [bcu_inflow, bcu_walls]
# define Jacobian
dW = TrialFunction(W)
J = derivative(F,w,dW)
a = lhs(F)
L = rhs(F)
# =====================================================================
problem = NonlinearVariationalProblem(F, w, bcs, J=J)
solver = NonlinearVariationalSolver(problem)
solver.parameters['newton_solver']['maximum_iterations'] = 20
solver.parameters['newton_solver']['relative_tolerance'] = 1.0e-6
# =====================================================================
# create files for storing
# =====================================================================
uFile = File(outputFileV, 'compressed')
pFile = File(outputFileP, 'compressed')
# =====================================================================
t = dt
count = 0
for n in range(num_steps):
# Updating inlet and outlet boundary conditions
# 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 = vel_prof(ai,omega,mu2,rho2,waves,dp_dz,t)
values_y = np.zeros(14)
dof_coords = W.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 = W.sub(0).dofmap().dofs()
y_dofs = W.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
# Solve for velocity and pressure fields
print("Solving.....")
solver.solve()
(u,p) = w.split()
N1 = 20 #save every N time steps
if (count/N1).is_integer():
uFile << u
pFile << p
print("Written Velocity and Pressure Data")
w0.assign(w)
t += dt
I’m still getting the following error.
u_inlet.vector().vec().setValueLocal(dof, values_x[x_c])
File "PETSc/Vec.pyx", line 688, in petsc4py.PETSc.Vec.setValueLocal (src/petsc4py.PETSc.c:98842)
petsc4py.PETSc.Error: error code 63