# Constraint nodes value

**URL:** <https://fenicsproject.discourse.group/t/constraint-nodes-value/5576>\
**Category:** mesh\
**Created:** [April 14, 2021, 2:10am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576 "2021-04-14T02:10:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [April 14, 2021, 2:10am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/1 "2021-04-14T02:10:16Z")

</div>

Hi,

I am a beginner at Fenics. Is there any way to set up a constraint condition for the nodes? I am testing with a basic heat conduction problem. and created a simple bifurcation mesh:

editor.add\_vertex(0, np.array([0.0,0.0,0.0]))  
editor.add\_vertex(1, np.array([0.0279,0,0]))  
editor.add\_vertex(2, np.array([0.0279,0.0,0]))  
editor.add\_vertex(3, np.array([0.043032,0.015132,0]))  
editor.add\_vertex(4, np.array([0.0279,0.0,0]))  
editor.add\_vertex(5, np.array([0.043032,-0.015132,0]))

editor.add\_cell(0, np.array([0,1], dtype=np.uintp))  
editor.add\_cell(1, np.array([2,3], dtype=np.uintp))  
editor.add\_cell(2, np.array([4,5], dtype=np.uintp))

Is that possible to set a constrain that node1=node2+node4?

Many Thanks!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 14, 2021, 6:23am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/2 "2021-04-14T06:23:07Z")

</div>

That is a multi point constraint and has been implemented as an add on to dolfin-x. See:

> **[jorgensd/dolfinx\_mpc](https://github.com/jorgensd/dolfinx_mpc)**
>
> Extension for dolfinx to handle multi-point constraints.

And

> **[dokken.pdf](https://fenics2021.com/slides/dokken.pdf)**
>
> 1608.83 KB

Note that you are constraining degrees of freedom (located at vertices). In dolfin and dolfinx the mesh and function space is decoupled. This means that you can have an arbitrary order function space on a first order geometry

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [April 14, 2021, 5:34pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/4 "2021-04-14T17:34:04Z")

</div>

Thank you very much  
I will take a look

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 20, 2021, 11:19pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/5 "2021-05-20T23:19:42Z")

</div>

Hi,  
I am facing an issue when I try to run your demo code: demo\_contact\_3D.py,  
which error showing as:  
HDF5-DIAG: Error detected in HDF5 (1.10.4) MPI-process 0:  
#000: …/…/…/src/H5F.c line 444 in H5Fcreate(): unable to create file  
major: File accessibilty  
minor: Unable to open file  
#001: …/…/…/src/H5Fint.c line 1364 in H5F\_\_create(): unable to open file  
major: File accessibilty  
minor: Unable to open file  
#002: …/…/…/src/H5Fint.c line 1557 in H5F\_open(): unable to open file: time = Thu May 20 22:50:27 2021  
, name = ‘results/demo\_contact\_3D.h5’, tent\_flags = 13  
major: File accessibilty  
minor: Unable to open file  
#003: …/…/…/src/H5FD.c line 734 in H5FD\_open(): open failed  
major: Virtual File Layer  
minor: Unable to initialize object  
#004: …/…/…/src/H5FDsec2.c line 346 in H5FD\_sec2\_open(): unable to open file: name = ‘results/demo\_contact\_3D.h5’, errno = 2, error message = ‘No such file or directory’, flags = 13, o\_flags = 242  
major: File accessibilty  
minor: Unable to open file  
Traceback (most recent call last):  
File “demo\_contact\_3D.py”, line 242, in   
outfile = dolfinx.io.XDMFFile(comm, “results/demo\_contact\_3D.xdmf”, “w”)  
RuntimeError: Failed to create HDF5 file.  
I assume this is a core issue, but not sure how to fix it, can you give me some advice?  
Many thanks

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 21, 2021, 4:57am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/6 "2021-05-21T04:57:08Z")

</div>

Simply create the directory results in the folder containing the demo

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 21, 2021, 5:17pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/7 "2021-05-21T17:17:42Z")

</div>

Thank you so much, I resolve the issue and the code looks good.  
Btw, it that still able to use the MeshEditor() function in dolfinx?

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 22, 2021, 12:29am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/8 "2021-05-22T00:29:13Z")

</div>

No, the interface in dolfinx does not require a mesheditor. We have an interface Where you pass in the mesh topology and geometry. See for instance: [dolfinx/demo\_gmsh.py at main · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/blob/main/python/demo/gmsh/demo_gmsh.py#L95)

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 25, 2021, 7:56pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/9 "2021-05-25T19:56:55Z")

</div>

Thanks, is there a better way that I can update the existing dolfinx image in docker once new things have added from your side. Cause I just realize I do not have nonlinearproblem in fem folder, I have to pull the dolfinx from dockerhub again and run the latest image.  
Many thanks

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 25, 2021, 7:59pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/10 "2021-05-25T19:59:05Z")

</div>

I will soon make a docker image with the 0.1.0version of dolfinx/dolfinx\_mpc.

Please note that you need to create your own custom nonlinear solver if you want to use it with MPC.

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 25, 2021, 7:59pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/11 "2021-05-25T19:59:48Z")

</div>

I see, appreciate that

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 26, 2021, 5:59pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/12 "2021-05-26T17:59:35Z")

</div>

Hi dokken,  
Sorry to bother again  
I use to define Initial condition and Boundary conditions in dolfin as:  
IC:  
V =FunctionSpace(mesh, ‘Lagrange’, 1)  
u\_dd=Expression(‘1.4’,degree=1)  
u\_n = interpolate(u\_dd, V)

BC:  
u0 = abs(round(1 + (Pa\_L-PT)/alpha\_L,2))  
def u0\_boundary(x, on\_boundary):  
tol = 1e-14;  
return abs(x[0] - term\_nodes\_coord[0][0]) \< tol  
bcL = DirichletBC(V, u0, u0\_boundary)

The question is how should I write code in dofinx to achieve the same result?  
Many Thanks

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 26, 2021, 6:05pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/13 "2021-05-26T18:05:33Z")

</div>

Please have a look through the DOLFINx tutorial I have written: [The FEniCS-X tutorial — FEniCS-X tutorial](https://jorgensd.github.io/dolfinx-tutorial/)

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 26, 2021, 6:06pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/14 "2021-05-26T18:06:33Z")

</div>

Thanks!! I was looking for this

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 27, 2021, 9:02pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/15 "2021-05-27T21:02:36Z")

</div>

Hi Dokken,

I am actually working on a 1D problem, I still do not quite understand how to set up a constant value for the boundary condition. seems the Constant() function does not work as before where I use to write:

u0=Constant(5)  
DirichletBC(V, u0, u0\_boundary)

Now is showing me: **init** () missing 1 required positional argument: ‘c’

can you teach about this?  
Thanks for your time

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 27, 2021, 9:39pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/16 "2021-05-27T21:39:23Z")

</div>

Please make a minimal code example that reproduces the error.

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [May 28, 2021, 5:12pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/17 "2021-05-28T17:12:53Z")

</div>

V = dolfinx.FunctionSpace(mesh, (“CG”, 1))  
BC\_test = Constant(mesh, 0)  
BC\_test.value = 5

dofs\_L = dolfinx.fem.locate\_dofs\_geometrical(V, lambda x: np.isclose(x[0], 0))

bc\_L = dolfinx.DirichletBC(BC\_test, dofs\_L)

Error showing:  
bc\_L = dolfinx.DirichletBC(BC\_test, dofs\_L)  
File “/usr/local/dolfinx-real/lib/python3.8/dist-packages/dolfinx/fem/dirichletbc.py”, line 147, in **init**  
raise NotImplementedError  
NotImplementedError

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [May 29, 2021, 6:32pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/18 "2021-05-29T18:32:17Z")

</div>

DOLFINx does currently not support the usage of `Constant` in Dirichlet boundary conditions. However, you can set a constant value as shown in: [Component-wise Dirichlet BC — FEniCSx tutorial](https://jorgensd.github.io/dolfinx-tutorial/chapter3/component_bc.html?highlight=localform#boundary-conditions)  
simply use:

```auto
with uDx.vector.localForm() as uDx_loc:
    uDx_loc.set(5)

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 1, 2021, 9:29am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/19 "2021-06-01T09:29:37Z")

</div>

@sunsystem I have now created a docker image with the 0.1.0 release of dolfinx and dolfinx\_mpc at: [Docker Hub](https://hub.docker.com/repository/docker/dokken92/dolfinx_mpc)  
which can be obtained by running

```bash
docker pull dokken92/dolfinx_mpc:0.1.0

```

---

<div class="post-metadata">

**Author:** ![sunsystem](https://avatars.discourse-cdn.com/v4/letter/s/eb8c5e/32.png) [@sunsystem](https://fenicsproject.discourse.group/u/sunsystem)\
**Post date:** [June 24, 2021, 9:16pm UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/20 "2021-06-24T21:16:15Z")

</div>

Hi, Dokken  
I have a question about the MPC code, where in the function ‘‘create\_general\_constraint(V, slave\_master\_dict)’’ the ‘‘slave\_master\_dict’’ is decribed as:  
If the dof D located at [d0,d1] should be constrained to the dofs  
E and F at [e0,e1] and [f0,f1] as  
D = alpha E + beta F  
the dictionary should be:  
{np.array([d0, d1], dtype=np.float64).tobytes():  
{np.array([e0, e1], dtype=np.float64).tobytes(): alpha,  
np.array([f0, f1], dtype=np.float64).tobytes(): beta}}  
I am a little confuse here, what is location mean？Does it mean the physical location of one point on the mesh (say a 1cm segmanet, point D at [0.15cm, 0cm]), or it means the nodes index (5 nodes in the segment, node D is the 3rd node at [3, 0])?  
And how should I write this part in a 1D mesh (the sample you give is for a 2D mesh, the constrained point is given in 2D form)?

Thanks for your time, and appreciate your help!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [June 25, 2021, 8:42am UTC](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576/21 "2021-06-25T08:42:53Z")

</div>

The location is the location of the degree of freedom you would like to constrain. I.e. given a 1D mesh on the interval [0,2], say I would like the condition u(0) = 2\cdot u(2).  
This would be implemented as follows:

```python
s_m_c = {l2b([0]): {l2b([2]): 2}}

mpc = dolfinx_mpc.MultiPointConstraint(V)
mpc.create_general_constraint(s_m_c)
mpc.finalize()

```

Full working example below:

```python
# Copyright (C) 2021 Jørgen S. Dokken
#
# This file is part of DOLFINX_MPC
#
# SPDX-License-Identifier: LGPL-3.0-or-later

import dolfinx
import dolfinx.io
import dolfinx_mpc
import dolfinx_mpc.utils
import numpy as np
import pytest
import ufl
from mpi4py import MPI
from petsc4py import PETSc

# Create mesh and function space

mesh = dolfinx.IntervalMesh(MPI.COMM_WORLD, 10, [np.array([0]), np.array([2])])
V = dolfinx.FunctionSpace(mesh, ("Lagrange", 1))

# Solve Problem without MPC for reference
u = ufl.TrialFunction(V)
v = ufl.TestFunction(V)
x = ufl.SpatialCoordinate(mesh)
f = 2 * x[0]**2
a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx
rhs = ufl.inner(f, v) * ufl.dx

# Create multipoint constraint

def l2b(li):
    return np.array(li, dtype=np.float64).tobytes()

s_m_c = {l2b([0]): {l2b([2]): 2}}

mpc = dolfinx_mpc.MultiPointConstraint(V)
mpc.create_general_constraint(s_m_c)
mpc.finalize()

problem = dolfinx_mpc.LinearProblem(a, rhs, mpc, bcs=[],
                                    petsc_options={"ksp_type": "preonly", "pc_type": "lu"})
uh = problem.solve()

with dolfinx.io.XDMFFile(MPI.COMM_WORLD, "u1d.xdmf", "w") as xdmf:
    xdmf.write_mesh(mesh)
    xdmf.write_function(uh)

```

[Next page](https://fenicsproject.discourse.group/t/constraint-nodes-value/5576.md?page=2)
