# Integration within the variational form

**URL:** <https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499>\
**Category:** variational formulation\
**Created:** [September 4, 2021, 3:52pm UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499 "2021-09-04T15:52:30Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![KaiBlaze](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@KaiBlaze](https://fenicsproject.discourse.group/u/KaiBlaze)\
**Post date:** [September 4, 2021, 3:52pm UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499/1 "2021-09-04T15:52:30Z")

</div>

Hello everyone  
I was using FEniCS to solve a 1D peridynamics problem. The exact problem statement is as follows-  
A rod extends from (alpha = 0) to (beta = 1m) and is subjected to body force (b(x)) of 1N/m and the equation is solved for displacement with BCs as u(0) = 0 and u(1) = 0 and delta is a parameter. The variational form for the same is :  
$$\frac{1}{\delta^2}$$ $$ \int\_{\alpha}^{\beta}v(x) \int\_{x - \delta}^{x + \delta} \frac{u(x) - u(x’)}{|x-x’|} dx’ dx = \int\_{\alpha}^{\beta}v(x) b(x) dx $$  
with v being the weight function  
Within the outside integral of variational form, we have another inner integral for a dummy variable x’.  
How is that to be coded in FEniCS?  
My sample code looks like this:

```auto
from fenics import *
import sympy as sy
alpha,beta,delta = 0,1,0.3
no_elem,degree = 2,1
mesh = IntervalMesh(no_elem, alpha, beta)
def boundary_1(x,on_boundary):
    return on_boundary and near(x[0],0,DOLFIN_EPS)
def boundary_2(x,on_boundary):
    return on_boundary and near(x[0],1,DOLFIN_EPS)
V = FunctionSpace(mesh, "CG", degree)
u = TrialFunction(V)
v = TestFunction(V)
body_force = Constant(1.0)
y1 = sy.Symbol('y1')
i = Expression(' (u(x[0]) - u(y1)) / abs (x[0] - y1) ', degree = 2)
a = v*Expression('(sy.integrate(i,(y1,x[0] - delta, x[0] + delta))/delta**2)', degree = 2)
L = body_force*v*dx
u = Function(V)
bcs = [DirichletBC(V, Constant(0.0), boundary_1), DirichletBC(V, Constant(0.0), boundary_1)]
solve(a == L, u, bcs)

```

The error message I get is -

 ![Capturew](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/2/2f1301d5cf8d9ab76afd82807f5feba2f3927f76.jpeg)

The version I m using is dolfin 2019.2.0 (developed version) in Spyder3 notebook in a Linux Subsystem on Windows  
PS : My apologies for posting the error as an image, but for some reason Spyder won’t allow me to copy and paste the error here or anywhere else. (I had to write the code manually)

Any lead and help is in formulation of the weak form is highly appreciated. Thanks in advance!

---

<div class="post-metadata">

**Author:** ![KaiBlaze](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@KaiBlaze](https://fenicsproject.discourse.group/u/KaiBlaze)\
**Post date:** [September 4, 2021, 3:53pm UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499/2 "2021-09-04T15:53:21Z")

</div>

This is the variational form I’m trying to code in FEniCS  
 ![Capture](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/8/8cd7293f0ea1e569a014c68e9ad3d590ca67173e.jpeg)

---

<div class="post-metadata">

**Author:** ![bleyerj](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bleyerj/32/6020_2.png) [@bleyerj](https://fenicsproject.discourse.group/u/bleyerj)\
**Post date:** [September 4, 2021, 8:40pm UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499/3 "2021-09-04T20:40:08Z")

</div>

Hi,  
I am not sure FEniCS is the best tool to handle such integral-type kernel.

---

<div class="post-metadata">

**Author:** ![KaiBlaze](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@KaiBlaze](https://fenicsproject.discourse.group/u/KaiBlaze)\
**Post date:** [September 5, 2021, 5:47am UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499/4 "2021-09-05T05:47:29Z")

</div>

Thank for replying bleyerj. I see. Could you provide a few lines of code on how to write that weak form in FEniCS so that I could give it a shot. I’m not sure on how to code it.

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [September 5, 2021, 4:22pm UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499/5 "2021-09-05T16:22:48Z")

</div>

See perhaps [here](https://fenicsproject.org/qa/11306/computing-projection-of-correlation-matrix/). The principle is similar. These types of formulations are an absolute pain because the discretisation is dense.

---

<div class="post-metadata">

**Author:** ![KaiBlaze](https://avatars.discourse-cdn.com/v4/letter/k/ecb155/32.png) [@KaiBlaze](https://fenicsproject.discourse.group/u/KaiBlaze)\
**Post date:** [September 8, 2021, 11:44am UTC](https://fenicsproject.discourse.group/t/integration-within-the-variational-form/6499/6 "2021-09-08T11:44:46Z")

</div>

Thanks a lot Nate for replying! I have a few questions for you (some of them may be quite naive) -

1. In the example which you have cited, the expression for the integral - type kernel is already known, whereas in my case, it is a function of u which we have to find out, so how do we proceed there?
2. I’m unable to write the bilinear form i.e. the LHS of the variational form and get the stiffness matrix. If you could help me to code it, that would solve my issue completely.
3. This variational form is a 1D. Would the complexity increase if we were to go to 2D and what are the alternatives for FEniCS that could be used to solve these?
