Apply boundary after reading data from txt file

I need to apply boundary condition after reading data from .txt file. Any clue, how can I apply?

from dolfin import *

import matplotlib.pyplot as plt
from dolfin import *

mesh = UniSquareMesh(10,10)
V = VectorFunctionSpace(mesh, "Lagrange", 1)


left =  CompiledSubDomain("near(x[1], side) && on_boundary", side = 0.0)
right = CompiledSubDomain("near(x[1], side) && on_boundary", side = 1.0)


# read data from txt to apply BC on right (tt)

bcl = DirichletBC(V, Constant((0,0)), left)
bcr = DirichletBC(V.sub(1), tt, right, method = 'pointwise')

See for instance: Impose Velocity Profile on Inlet Face using Array - #6 by dokken

1 Like

Hi, I have tried applying the same logic to my problem but getting some error while assigning to ux in the loop (AttributeError: ‘function’ object has no attribute ‘setValueLocal’): Please find MWC

from dolfin import *

import numpy as np


#fname = 'exdata.txt';
#values_x = np.loadtxt(fname)
values_x = np.array([0.0 0.1 0.0])

mesh = UnitSquareMesh(2,2)
V = FunctionSpace(mesh, "CG", 1)
ux = Function(V)
dof_coords = V.tabulate_dof_coordinates()

dofs_on_left_side = np.flatnonzero(np.isclose(dof_coords[:,0], 0))
left_dofs_coords = dof_coords[dofs_on_left_side]

sorted_indices = np.argsort(left_dofs_coords[:,1])
#print (sorted_indices)

x_dofs = V.dofmap().dofs()
#print (x_dofs)

x_c, y_c = 0, 0
for index in sorted_indices:
    dof = dofs_on_left_side[index]
    ux.vector.setValueLocal(dof, values_x[x_c])
    x_c +=1

In the post above, they do not call:

but

    ux.vector().vec().setValueLocal(dof, values_x[x_c])

I am able to fix this by using this:

for index in sorted_indices:
    dof = dofs_on_left_side[index]
    ux.vector()[dof]=values_x[x_c]
    x_c +=1