# Error in setting Boundary Conditions for DFG benchmark 2D-1

I am trying to convert my legacy FEniCS code to FEniCSx. I’m working on the DFG benchmark 2D-1 in FeatFlow (DFG benchmark 2D-1 (RE20, laminar) - Featflow). I am getting several errors while setting up the boundary conditions. I am using the same mesh from the tutorial of the DFG 2D-3 benchmark.

The working legacy FEniCS code is:

``````# Define boundaries
inflow   = 'near(x[0], 0)'
outflow  = 'near(x[0], 2.2)'
walls    = 'near(x[1], 0) || near(x[1], 0.41)'
cylinder = 'on_boundary && x[0]>0.1 && x[0]<0.3 && x[1]>0.1 && x[1]<0.3'

# Define inflow profile
inflow_profile = ('4.0*0.3*x[1]*(0.41 - x[1]) / pow(0.41, 2)', '0')

# Define boundary conditions
bcu_inflow = DirichletBC(W.sub(0), Expression(inflow_profile, degree=2), inflow)
bcu_walls = DirichletBC(W.sub(0), Constant((0, 0)), walls)
bcu_cylinder = DirichletBC(W.sub(0), Constant((0, 0)), cylinder)
bcp_outflow = DirichletBC(W.sub(1), Constant(0), outflow)
bc = [bcu_inflow, bcu_walls, bcu_cylinder, bcp_outflow]
``````

The FEniCSx code I tried is:

``````# Reading Mesh
mesh, cell_tags, facet_tags = read_from_msh("mesh2D.msh", MPI.COMM_WORLD, 0, gdim=2)
fdim = mesh.topology.dim - 1

# Material parameters
nu = fem.Constant(mesh, PETSc.ScalarType(0.001))  # Kinematic viscosity

P2 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2)
P1 = ufl.FiniteElement("Lagrange", mesh.ufl_cell(), 1)
V, Q = fem.FunctionSpace(mesh, P2), fem.FunctionSpace(mesh, P1)

# Create the function space
TH = P2 * P1
W = fem.FunctionSpace(mesh, TH)
W0, _ = W.sub(0).collapse()

def velocity_expression(x):
values = np.zeros((mesh.geometry.dim, x.shape[1]),dtype=PETSc.ScalarType)
values[0] = 4.0*0.3*x[1]*(0.41 - x[1]) / pow(0.41, 2)
return values

# Inlet
u_inlet = fem.Function(V)
u_inlet.interpolate(velocity_expression)
bcu_inflow = fem.dirichletbc(u_inlet, fem.locate_dofs_topological((W.sub(0), V), fdim, facet_tags.find(2)), W.sub(0))
# Walls
u_nonslip = np.array((0,) * mesh.geometry.dim, dtype=PETSc.ScalarType)
bcu_walls = fem.dirichletbc(u_nonslip, fem.locate_dofs_topological((W.sub(0), V), fdim, facet_tags.find(4)), W.sub(0))
# Obstacle
bcu_obstacle = fem.dirichletbc(u_nonslip, fem.locate_dofs_topological((W.sub(0), V), fdim, facet_tags.find(5)), W.sub(0))
# Outlet
bcp_outlet = fem.dirichletbc(PETSc.ScalarType(0), fem.locate_dofs_topological((W.sub(1), Q), fdim, facet_tags.find(3)), W.sub(1))

bc = [bcu_inflow, bcu_obstacle, bcu_walls,bcp_outlet]
``````

This produces a considerable error statement:

``````Traceback (most recent call last):
File "/Users/varunkumar/opt/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/bcs.py", line 125, in __init__
super().__init__(_value, dofs, V)  # type: ignore
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. dolfinx.cpp.fem.DirichletBC_float64(g: numpy.ndarray[numpy.float64], dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: numpy.ndarray[numpy.int32])
4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: List[numpy.ndarray[numpy.int32][2]], V: dolfinx::fem::FunctionSpace)

Invoked with: array([0., 0.]), [array([    0,     1,     2,     3,     8,     9,    36,    37,    40,
41,    50,    51,    54,    55,    60,    61,    95,    96,
101,   102,   109,   110,   113,   114,   169,   170,   175,
176,   183,   184,   187,   188,   253,   254,   257,   258,
267,   268,   271,   272,   330,   331,   334,   335,   344,
345,   348,   349,   407,   408,   413,   414,   421,   422,
425,   426,   484,   485,   490,   491,   498,   499,   502,
503,   561,   562,   565,   566,   575,   576,   579,   580,
638,   639,   642,   643,   652,   653,   656,   657,   715,
716,   721,   722,   729,   730,   733,   734,   792,   793,
798,   799,   806,   807,   810,   811,   869,   870,   873,
874,   883,   884,   887,   888,   946,   947,   950,   951,
960,   961,   964,   965,  1023,  1024,  1029,  1030,  1037,
1038,  1041,  1042,  1100,  1101,  1106,  1107,  1114,  1115,
1118,  1119,  1177,  1178,  1181,  1182,  1191,  1192,  1195,
1196,  1254,  1255,  1258,  1259,  1268,  1269,  1272,  1273,
1331,  1332,  1337,  1338,  1345,  1346,  1349,  1350,  1408,
1409,  1414,  1415,  1422,  1423,  1426,  1427,  1485,  1486,
1489,  1490,  1499,  1500,  1503,  1504,  1562,  1563,  1566,
1567,  1576,  1577,  1580,  1581,  1639,  1640,  1645,  1646,
1653,  1654,  1657,  1658,  1716,  1717,  1722,  1723,  1730,
1731,  1734,  1735,  1793,  1794,  1797,  1798,  1807,  1808,
1811,  1812,  1870,  1871,  1874,  1875,  1884,  1885,  1888,
1889,  1947,  1948,  1953,  1954,  1961,  1962,  1965,  1966,
2024,  2025,  2030,  2031,  2038,  2039,  2042,  2043,  2152,
2153,  2156,  2157,  2166,  2167,  2170,  2171,  2265,  2266,
2269,  2270,  2279,  2280,  2283,  2284,  2378,  2379,  2384,
2385,  2392,  2393,  2396,  2397,  2491,  2492,  2497,  2498,
2505,  2506,  2509,  2510,  2604,  2605,  2608,  2609,  2618,
2619,  2622,  2623,  2717,  2718,  2721,  2722,  2731,  2732,
2735,  2736,  2881,  2882,  2887,  2888,  2895,  2896,  2899,
2900,  3030,  3031,  3036,  3037,  3044,  3045,  3048,  3049,
3235,  3236,  3239,  3240,  3249,  3250,  3253,  3254,  3420,
3421,  3424,  3425,  3434,  3435,  3438,  3439,  3656,  3657,
3662,  3663,  3670,  3671,  3674,  3675,  3877,  3878,  3883,
3884,  3891,  3892,  3895,  3896,  4280,  4281,  4284,  4285,
4294,  4295,  4298,  4299,  4618,  4619,  4622,  4623,  4632,
4633,  4636,  4637,  4965,  4966,  4971,  4972,  4979,  4980,
4983,  4984,  5312,  5313,  5318,  5319,  5326,  5327,  5330,
5331,  5673,  5674,  5677,  5678,  5687,  5688,  5691,  5692,
6015,  6016,  6019,  6020,  6029,  6030,  6033,  6034,  6367,
6368,  6373,  6374,  6381,  6382,  6385,  6386,  6751,  6752,
6757,  6758,  6765,  6766,  6769,  6770,  7125,  7126,  7129,
7130,  7139,  7140,  7143,  7144,  7499,  7500,  7503,  7504,
7513,  7514,  7517,  7518,  7873,  7874,  7879,  7880,  7887,
7888,  7891,  7892,  8242,  8243,  8248,  8249,  8256,  8257,
8260,  8261,  8631,  8632,  8635,  8636,  8645,  8646,  8649,
8650,  8986,  8987,  8990,  8991,  9000,  9001,  9004,  9005,
9328,  9329,  9334,  9335,  9342,  9343,  9346,  9347,  9638,
9639,  9644,  9645,  9652,  9653,  9656,  9657,  9949,  9950,
9953,  9954,  9963,  9964,  9967,  9968, 10279, 10280, 10283,
10284, 10293, 10294, 10297, 10298, 10576, 10577, 10582, 10583,
10590, 10591, 10594, 10595, 10869, 10870, 10875, 10876, 10883,
10884, 10887, 10888, 11144, 11145, 11148, 11149, 11158, 11159,
11162, 11163, 11429, 11430, 11433, 11434, 11443, 11444, 11447,
11448, 11699, 11700, 11705, 11706, 11713, 11714, 11717, 11718,
11975, 11976, 11981, 11982, 11989, 11990, 11993, 11994, 12264,
12265, 12268, 12269, 12278, 12279, 12282, 12283, 12567, 12568,
12571, 12572, 12581, 12582, 12585, 12586, 12865, 12866, 12871,
12872, 12879, 12880, 12883, 12884, 13163, 13164, 13169, 13170,
13177, 13178, 13181, 13182, 13456, 13457, 13460, 13461, 13470,
13471, 13474, 13475, 13773, 13774, 13777, 13778, 13787, 13788,
13791, 13792, 14080, 14081, 14086, 14087, 14094, 14095, 14098,
14099, 14423, 14424, 14429, 14430, 14437, 14438, 14441, 14442,
14794, 14795, 14798, 14799], dtype=int32), array([    0,     1,     2,     3,     8,     9,    30,    31,    34,
35,    42,    43,    46,    47,    52,    53,    80,    81,
86,    87,    92,    93,    96,    97,   144,   145,   150,
151,   156,   157,   160,   161,   218,   219,   222,   223,
230,   231,   234,   235,   286,   287,   290,   291,   298,
299,   302,   303,   354,   355,   360,   361,   366,   367,
370,   371,   422,   423,   428,   429,   434,   435,   438,
439,   490,   491,   494,   495,   502,   503,   506,   507,
558,   559,   562,   563,   570,   571,   574,   575,   626,
627,   632,   633,   638,   639,   642,   643,   694,   695,
700,   701,   706,   707,   710,   711,   762,   763,   766,
767,   774,   775,   778,   779,   830,   831,   834,   835,
842,   843,   846,   847,   898,   899,   904,   905,   910,
911,   914,   915,   966,   967,   972,   973,   978,   979,
982,   983,  1034,  1035,  1038,  1039,  1046,  1047,  1050,
1051,  1102,  1103,  1106,  1107,  1114,  1115,  1118,  1119,
1170,  1171,  1176,  1177,  1182,  1183,  1186,  1187,  1238,
1239,  1244,  1245,  1250,  1251,  1254,  1255,  1306,  1307,
1310,  1311,  1318,  1319,  1322,  1323,  1374,  1375,  1378,
1379,  1386,  1387,  1390,  1391,  1442,  1443,  1448,  1449,
1454,  1455,  1458,  1459,  1510,  1511,  1516,  1517,  1522,
1523,  1526,  1527,  1578,  1579,  1582,  1583,  1590,  1591,
1594,  1595,  1646,  1647,  1650,  1651,  1658,  1659,  1662,
1663,  1714,  1715,  1720,  1721,  1726,  1727,  1730,  1731,
1782,  1783,  1788,  1789,  1794,  1795,  1798,  1799,  1894,
1895,  1898,  1899,  1906,  1907,  1910,  1911,  1994,  1995,
1998,  1999,  2006,  2007,  2010,  2011,  2094,  2095,  2100,
2101,  2106,  2107,  2110,  2111,  2194,  2195,  2200,  2201,
2206,  2207,  2210,  2211,  2294,  2295,  2298,  2299,  2306,
2307,  2310,  2311,  2394,  2395,  2398,  2399,  2406,  2407,
2410,  2411,  2538,  2539,  2544,  2545,  2550,  2551,  2554,
2555,  2670,  2671,  2676,  2677,  2682,  2683,  2686,  2687,
2850,  2851,  2854,  2855,  2862,  2863,  2866,  2867,  3014,
3015,  3018,  3019,  3026,  3027,  3030,  3031,  3222,  3223,
3228,  3229,  3234,  3235,  3238,  3239,  3418,  3419,  3424,
3425,  3430,  3431,  3434,  3435,  3770,  3771,  3774,  3775,
3782,  3783,  3786,  3787,  4070,  4071,  4074,  4075,  4082,
4083,  4086,  4087,  4378,  4379,  4384,  4385,  4390,  4391,
4394,  4395,  4686,  4687,  4692,  4693,  4698,  4699,  4702,
4703,  5006,  5007,  5010,  5011,  5018,  5019,  5022,  5023,
5310,  5311,  5314,  5315,  5322,  5323,  5326,  5327,  5622,
5623,  5628,  5629,  5634,  5635,  5638,  5639,  5962,  5963,
5968,  5969,  5974,  5975,  5978,  5979,  6294,  6295,  6298,
6299,  6306,  6307,  6310,  6311,  6626,  6627,  6630,  6631,
6638,  6639,  6642,  6643,  6958,  6959,  6964,  6965,  6970,
6971,  6974,  6975,  7286,  7287,  7292,  7293,  7298,  7299,
7302,  7303,  7630,  7631,  7634,  7635,  7642,  7643,  7646,
7647,  7946,  7947,  7950,  7951,  7958,  7959,  7962,  7963,
8250,  8251,  8256,  8257,  8262,  8263,  8266,  8267,  8526,
8527,  8532,  8533,  8538,  8539,  8542,  8543,  8802,  8803,
8806,  8807,  8814,  8815,  8818,  8819,  9094,  9095,  9098,
9099,  9106,  9107,  9110,  9111,  9358,  9359,  9364,  9365,
9370,  9371,  9374,  9375,  9618,  9619,  9624,  9625,  9630,
9631,  9634,  9635,  9862,  9863,  9866,  9867,  9874,  9875,
9878,  9879, 10114, 10115, 10118, 10119, 10126, 10127, 10130,
10131, 10354, 10355, 10360, 10361, 10366, 10367, 10370, 10371,
10598, 10599, 10604, 10605, 10610, 10611, 10614, 10615, 10854,
10855, 10858, 10859, 10866, 10867, 10870, 10871, 11122, 11123,
11126, 11127, 11134, 11135, 11138, 11139, 11386, 11387, 11392,
11393, 11398, 11399, 11402, 11403, 11650, 11651, 11656, 11657,
11662, 11663, 11666, 11667, 11910, 11911, 11914, 11915, 11922,
11923, 11926, 11927, 12190, 12191, 12194, 12195, 12202, 12203,
12206, 12207, 12462, 12463, 12468, 12469, 12474, 12475, 12478,
12479, 12764, 12765, 12770, 12771, 12776, 12777, 12780, 12781,
13092, 13093, 13096, 13097], dtype=int32)], FunctionSpace(Mesh(VectorElement(Basix element (P, quadrilateral, 2, equispaced, unset, False), 2), 0), VectorElement(FiniteElement('Q', quadrilateral, 2), dim=2))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
bcu_walls = fem.dirichletbc(u_nonslip, fem.locate_dofs_topological((W.sub(0), V), fdim, facet_tags.find(4)), W.sub(0))
File "/Users/varunkumar/opt/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/bcs.py", line 183, in dirichletbc
return formcls(value, dofs, V)
File "/Users/varunkumar/opt/anaconda3/envs/fenicsx/lib/python3.10/site-packages/dolfinx/fem/bcs.py", line 127, in __init__
super().__init__(_value, dofs, V._cpp_object)  # type: ignore
TypeError: __init__(): incompatible constructor arguments. The following argument types are supported:
1. dolfinx.cpp.fem.DirichletBC_float64(g: numpy.ndarray[numpy.float64], dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
2. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Constant<double>, dofs: numpy.ndarray[numpy.int32], V: dolfinx::fem::FunctionSpace)
3. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: numpy.ndarray[numpy.int32])
4. dolfinx.cpp.fem.DirichletBC_float64(g: dolfinx::fem::Function<double>, dofs: List[numpy.ndarray[numpy.int32][2]], V: dolfinx::fem::FunctionSpace)

Invoked with: array([0., 0.]), [array([    0,     1,     2,     3,     8,     9,    36,    37,    40,
41,    50,    51,    54,    55,    60,    61,    95,    96,
101,   102,   109,   110,   113,   114,   169,   170,   175,
176,   183,   184,   187,   188,   253,   254,   257,   258,
267,   268,   271,   272,   330,   331,   334,   335,   344,
345,   348,   349,   407,   408,   413,   414,   421,   422,
425,   426,   484,   485,   490,   491,   498,   499,   502,
503,   561,   562,   565,   566,   575,   576,   579,   580,
638,   639,   642,   643,   652,   653,   656,   657,   715,
716,   721,   722,   729,   730,   733,   734,   792,   793,
798,   799,   806,   807,   810,   811,   869,   870,   873,
874,   883,   884,   887,   888,   946,   947,   950,   951,
960,   961,   964,   965,  1023,  1024,  1029,  1030,  1037,
1038,  1041,  1042,  1100,  1101,  1106,  1107,  1114,  1115,
1118,  1119,  1177,  1178,  1181,  1182,  1191,  1192,  1195,
1196,  1254,  1255,  1258,  1259,  1268,  1269,  1272,  1273,
1331,  1332,  1337,  1338,  1345,  1346,  1349,  1350,  1408,
1409,  1414,  1415,  1422,  1423,  1426,  1427,  1485,  1486,
1489,  1490,  1499,  1500,  1503,  1504,  1562,  1563,  1566,
1567,  1576,  1577,  1580,  1581,  1639,  1640,  1645,  1646,
1653,  1654,  1657,  1658,  1716,  1717,  1722,  1723,  1730,
1731,  1734,  1735,  1793,  1794,  1797,  1798,  1807,  1808,
1811,  1812,  1870,  1871,  1874,  1875,  1884,  1885,  1888,
1889,  1947,  1948,  1953,  1954,  1961,  1962,  1965,  1966,
2024,  2025,  2030,  2031,  2038,  2039,  2042,  2043,  2152,
2153,  2156,  2157,  2166,  2167,  2170,  2171,  2265,  2266,
2269,  2270,  2279,  2280,  2283,  2284,  2378,  2379,  2384,
2385,  2392,  2393,  2396,  2397,  2491,  2492,  2497,  2498,
2505,  2506,  2509,  2510,  2604,  2605,  2608,  2609,  2618,
2619,  2622,  2623,  2717,  2718,  2721,  2722,  2731,  2732,
2735,  2736,  2881,  2882,  2887,  2888,  2895,  2896,  2899,
2900,  3030,  3031,  3036,  3037,  3044,  3045,  3048,  3049,
3235,  3236,  3239,  3240,  3249,  3250,  3253,  3254,  3420,
3421,  3424,  3425,  3434,  3435,  3438,  3439,  3656,  3657,
3662,  3663,  3670,  3671,  3674,  3675,  3877,  3878,  3883,
3884,  3891,  3892,  3895,  3896,  4280,  4281,  4284,  4285,
4294,  4295,  4298,  4299,  4618,  4619,  4622,  4623,  4632,
4633,  4636,  4637,  4965,  4966,  4971,  4972,  4979,  4980,
4983,  4984,  5312,  5313,  5318,  5319,  5326,  5327,  5330,
5331,  5673,  5674,  5677,  5678,  5687,  5688,  5691,  5692,
6015,  6016,  6019,  6020,  6029,  6030,  6033,  6034,  6367,
6368,  6373,  6374,  6381,  6382,  6385,  6386,  6751,  6752,
6757,  6758,  6765,  6766,  6769,  6770,  7125,  7126,  7129,
7130,  7139,  7140,  7143,  7144,  7499,  7500,  7503,  7504,
7513,  7514,  7517,  7518,  7873,  7874,  7879,  7880,  7887,
7888,  7891,  7892,  8242,  8243,  8248,  8249,  8256,  8257,
8260,  8261,  8631,  8632,  8635,  8636,  8645,  8646,  8649,
8650,  8986,  8987,  8990,  8991,  9000,  9001,  9004,  9005,
9328,  9329,  9334,  9335,  9342,  9343,  9346,  9347,  9638,
9639,  9644,  9645,  9652,  9653,  9656,  9657,  9949,  9950,
9953,  9954,  9963,  9964,  9967,  9968, 10279, 10280, 10283,
10284, 10293, 10294, 10297, 10298, 10576, 10577, 10582, 10583,
10590, 10591, 10594, 10595, 10869, 10870, 10875, 10876, 10883,
10884, 10887, 10888, 11144, 11145, 11148, 11149, 11158, 11159,
11162, 11163, 11429, 11430, 11433, 11434, 11443, 11444, 11447,
11448, 11699, 11700, 11705, 11706, 11713, 11714, 11717, 11718,
11975, 11976, 11981, 11982, 11989, 11990, 11993, 11994, 12264,
12265, 12268, 12269, 12278, 12279, 12282, 12283, 12567, 12568,
12571, 12572, 12581, 12582, 12585, 12586, 12865, 12866, 12871,
12872, 12879, 12880, 12883, 12884, 13163, 13164, 13169, 13170,
13177, 13178, 13181, 13182, 13456, 13457, 13460, 13461, 13470,
13471, 13474, 13475, 13773, 13774, 13777, 13778, 13787, 13788,
13791, 13792, 14080, 14081, 14086, 14087, 14094, 14095, 14098,
14099, 14423, 14424, 14429, 14430, 14437, 14438, 14441, 14442,
14794, 14795, 14798, 14799], dtype=int32), array([    0,     1,     2,     3,     8,     9,    30,    31,    34,
35,    42,    43,    46,    47,    52,    53,    80,    81,
86,    87,    92,    93,    96,    97,   144,   145,   150,
151,   156,   157,   160,   161,   218,   219,   222,   223,
230,   231,   234,   235,   286,   287,   290,   291,   298,
299,   302,   303,   354,   355,   360,   361,   366,   367,
370,   371,   422,   423,   428,   429,   434,   435,   438,
439,   490,   491,   494,   495,   502,   503,   506,   507,
558,   559,   562,   563,   570,   571,   574,   575,   626,
627,   632,   633,   638,   639,   642,   643,   694,   695,
700,   701,   706,   707,   710,   711,   762,   763,   766,
767,   774,   775,   778,   779,   830,   831,   834,   835,
842,   843,   846,   847,   898,   899,   904,   905,   910,
911,   914,   915,   966,   967,   972,   973,   978,   979,
982,   983,  1034,  1035,  1038,  1039,  1046,  1047,  1050,
1051,  1102,  1103,  1106,  1107,  1114,  1115,  1118,  1119,
1170,  1171,  1176,  1177,  1182,  1183,  1186,  1187,  1238,
1239,  1244,  1245,  1250,  1251,  1254,  1255,  1306,  1307,
1310,  1311,  1318,  1319,  1322,  1323,  1374,  1375,  1378,
1379,  1386,  1387,  1390,  1391,  1442,  1443,  1448,  1449,
1454,  1455,  1458,  1459,  1510,  1511,  1516,  1517,  1522,
1523,  1526,  1527,  1578,  1579,  1582,  1583,  1590,  1591,
1594,  1595,  1646,  1647,  1650,  1651,  1658,  1659,  1662,
1663,  1714,  1715,  1720,  1721,  1726,  1727,  1730,  1731,
1782,  1783,  1788,  1789,  1794,  1795,  1798,  1799,  1894,
1895,  1898,  1899,  1906,  1907,  1910,  1911,  1994,  1995,
1998,  1999,  2006,  2007,  2010,  2011,  2094,  2095,  2100,
2101,  2106,  2107,  2110,  2111,  2194,  2195,  2200,  2201,
2206,  2207,  2210,  2211,  2294,  2295,  2298,  2299,  2306,
2307,  2310,  2311,  2394,  2395,  2398,  2399,  2406,  2407,
2410,  2411,  2538,  2539,  2544,  2545,  2550,  2551,  2554,
2555,  2670,  2671,  2676,  2677,  2682,  2683,  2686,  2687,
2850,  2851,  2854,  2855,  2862,  2863,  2866,  2867,  3014,
3015,  3018,  3019,  3026,  3027,  3030,  3031,  3222,  3223,
3228,  3229,  3234,  3235,  3238,  3239,  3418,  3419,  3424,
3425,  3430,  3431,  3434,  3435,  3770,  3771,  3774,  3775,
3782,  3783,  3786,  3787,  4070,  4071,  4074,  4075,  4082,
4083,  4086,  4087,  4378,  4379,  4384,  4385,  4390,  4391,
4394,  4395,  4686,  4687,  4692,  4693,  4698,  4699,  4702,
4703,  5006,  5007,  5010,  5011,  5018,  5019,  5022,  5023,
5310,  5311,  5314,  5315,  5322,  5323,  5326,  5327,  5622,
5623,  5628,  5629,  5634,  5635,  5638,  5639,  5962,  5963,
5968,  5969,  5974,  5975,  5978,  5979,  6294,  6295,  6298,
6299,  6306,  6307,  6310,  6311,  6626,  6627,  6630,  6631,
6638,  6639,  6642,  6643,  6958,  6959,  6964,  6965,  6970,
6971,  6974,  6975,  7286,  7287,  7292,  7293,  7298,  7299,
7302,  7303,  7630,  7631,  7634,  7635,  7642,  7643,  7646,
7647,  7946,  7947,  7950,  7951,  7958,  7959,  7962,  7963,
8250,  8251,  8256,  8257,  8262,  8263,  8266,  8267,  8526,
8527,  8532,  8533,  8538,  8539,  8542,  8543,  8802,  8803,
8806,  8807,  8814,  8815,  8818,  8819,  9094,  9095,  9098,
9099,  9106,  9107,  9110,  9111,  9358,  9359,  9364,  9365,
9370,  9371,  9374,  9375,  9618,  9619,  9624,  9625,  9630,
9631,  9634,  9635,  9862,  9863,  9866,  9867,  9874,  9875,
9878,  9879, 10114, 10115, 10118, 10119, 10126, 10127, 10130,
10131, 10354, 10355, 10360, 10361, 10366, 10367, 10370, 10371,
10598, 10599, 10604, 10605, 10610, 10611, 10614, 10615, 10854,
10855, 10858, 10859, 10866, 10867, 10870, 10871, 11122, 11123,
11126, 11127, 11134, 11135, 11138, 11139, 11386, 11387, 11392,
11393, 11398, 11399, 11402, 11403, 11650, 11651, 11656, 11657,
11662, 11663, 11666, 11667, 11910, 11911, 11914, 11915, 11922,
11923, 11926, 11927, 12190, 12191, 12194, 12195, 12202, 12203,
12206, 12207, 12462, 12463, 12468, 12469, 12474, 12475, 12478,
12479, 12764, 12765, 12770, 12771, 12776, 12777, 12780, 12781,
13092, 13093, 13096, 13097], dtype=int32)], <dolfinx.cpp.fem.FunctionSpace object at 0x10ef0d7b0>
``````

I can’t figure out what’s wrong with the code. Any help is very much appreciated.

Consider the following (using a random meshtag and mesh as you have not supplied yours:

``````from dolfinx import fem
from dolfinx.mesh import create_unit_square, meshtags
from mpi4py import MPI
import ufl
from petsc4py import PETSc
import numpy as np
mesh = create_unit_square(MPI.COMM_WORLD, 10, 10)
fdim = mesh.topology.dim - 1
mesh.topology.create_entities(mesh.topology.dim-1)
fl = np.arange(mesh.topology.index_map(mesh.topology.dim-1).size_local, dtype=np.int32)
facet_tags = meshtags(mesh, fdim, fl, fl)

# Material parameters
nu = fem.Constant(mesh, PETSc.ScalarType(0.001))  # Kinematic viscosity

P2 = ufl.VectorElement("Lagrange", mesh.ufl_cell(), 2)
P1 = ufl.FiniteElement("Lagrange", mesh.ufl_cell(), 1)
V, Q = fem.FunctionSpace(mesh, P2), fem.FunctionSpace(mesh, P1)

# Create the function space
TH = P2 * P1
W = fem.FunctionSpace(mesh, TH)
W0, _ = W.sub(0).collapse()

def velocity_expression(x):
values = np.zeros((mesh.geometry.dim, x.shape[1]), dtype=PETSc.ScalarType)
values[0] = 4.0*0.3*x[1]*(0.41 - x[1]) / pow(0.41, 2)
return values

# Inlet
u_inlet = fem.Function(V)
u_inlet.interpolate(velocity_expression)
bcu_inflow = fem.dirichletbc(u_inlet, fem.locate_dofs_topological(
(W.sub(0), V), fdim, facet_tags.find(2)), W.sub(0))
# Walls
u_nonslip = fem.Function(V)
u_nonslip.x.set(0.)
bcu_walls = fem.dirichletbc(u_nonslip, fem.locate_dofs_topological(
(W.sub(0), V), fdim, facet_tags.find(4)), W.sub(0))
# Obstacle
bcu_obstacle = fem.dirichletbc(u_nonslip, fem.locate_dofs_topological(
(W.sub(0), V), fdim, facet_tags.find(5)), W.sub(0))
# Outlet
bcp_outlet = fem.dirichletbc(PETSc.ScalarType(0), fem.locate_dofs_topological(
W.sub(1), fdim, facet_tags.find(3)), W.sub(1))

bc = [bcu_inflow, bcu_obstacle, bcu_walls, bcp_outlet]
``````