# Efficiently solving Poisson eqn repeatedly

**URL:** <https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332>\
**Category:** dolfinx\
**Created:** [May 16, 2022, 12:54pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332 "2022-05-16T12:54:21Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wvanharten](https://avatars.discourse-cdn.com/v4/letter/w/5daacb/32.png) [@wvanharten](https://fenicsproject.discourse.group/u/wvanharten)\
**Post date:** [May 16, 2022, 12:54pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/1 "2022-05-16T12:54:21Z")

</div>

Hi all,

For my application, I am solving a Poisson equation repeatedly with a different nonconstant RHS and zero Dirichlet boundary conditions.

If I’m not mistaken, I could simply work out the LU decomposition of the LHS matrix and store it somewhere. Then, I’d apply the decomposition repeatedly to a RHS vector to obtain a solution.

However, I’m not sure how to implement this in dolfinx, below a MWE for what I do now:

```auto
from random import random
import dolfinx
import numpy as np
from dolfinx import FunctionSpace, Constant, fem, Function
from dolfinx.fem import create_vector, Form
from mpi4py import MPI
from ufl import TrialFunction, TestFunction, inner, dx
from ufl.operators import grad

N = 2 ** 8
mesh = dolfinx.UnitSquareMesh(MPI.COMM_SELF, N, N, dolfinx.cpp.mesh.CellType.triangle)
V = FunctionSpace(mesh, ("CG", 1))
u = TrialFunction(V)
v = TestFunction(V)
f = Function(V)
uh = Function(V)

A = inner(grad(u), grad(v)) * dx
F = inner(f, v) * dx
problem = fem.LinearProblem(A, F, u=uh, petsc_options={"ksp_type": "preonly", "pc_type": "lu"})  
problem.solve() # Calculate LU of LHS

# Start solving all cases
for index in range(500):
    rand = random()
    f.interpolate(lambda x: np.sin(rand*(x[0] + x[1]))) # Something nontrivial, in reality it's a function of the 
    # previous solutions 
    RHS = inner(f, v) * dx
    form = Form(RHS)
    print("Create")
    vector = create_vector(form)
    problem._b = vector
    problem._L = form
    print("Solve")
    problem.solve()

```

However, I found this by inspecting the source of dolfinx v0.3.0, and not by following any official resources.

In the KSP documentation ([KSP: Linear System Solvers — PETSc 3.14.1 documentation](https://petsc4py.readthedocs.io/en/stable/manual/ksp/#solving-successive-linear-systems)) I can find a mention of my use case, where it mentions successive calls to `KSPSolve()`.

So basically my question is, is this the appropriate method to perform this?

Best whishes,

- Wouter

---

<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:** [May 16, 2022, 2:07pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/2 "2022-05-16T14:07:05Z")

</div>

You can set this explicitly to be sure: [KSPSetReusePreconditioner](https://petsc.org/release/docs/manualpages/KSP/KSPSetReusePreconditioner.html).

---

<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 16, 2022, 2:21pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/3 "2022-05-16T14:21:40Z")

</div>

There are several things in your code that is not efficient,  
1.

> [@wvanharten](#):
>
> `form = Form(RHS)`

You do not need to recompile the form if `f` is in the same function space. You can simply create your form outside the loop and update f through interpolation.

1. 

> [@wvanharten](#):
>
> ```auto
> vector = create_vector(form)
> problem._b = vector
> problem._L = form
> 
> ```

This should not be required, as the form should be constant in time, and the coefficients in `f` will be automatically updated during the solve stage.

---

<div class="post-metadata">

**Author:** ![wvanharten](https://avatars.discourse-cdn.com/v4/letter/w/5daacb/32.png) [@wvanharten](https://fenicsproject.discourse.group/u/wvanharten)\
**Post date:** [May 16, 2022, 2:46pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/4 "2022-05-16T14:46:34Z")

</div>

Thank you for the tips,

I wrote this MWE a bit more simplified than what I’m actually calculating as I thought this would needlessly complicate the example.

In reality I’m solving the weak formulation

\int\_{D} \nabla t\_i(x)\cdot\nabla v(x) dx = \sum\_{j \< i}\int\_{D} a\_{j-i}(x) \nabla t\_i(x)\cdot\nabla v(x) dx for some decreasing sequencing of functions a\_j(x)

which incorporates the gradient of previously calculated solutions t\_i. Would your suggestions still work in this case? I don’t think I can simply re-interpolate my right hand side I’d think.

Thanks in advace,

- Wouter

---

<div class="post-metadata">

**Author:** ![wvanharten](https://avatars.discourse-cdn.com/v4/letter/w/5daacb/32.png) [@wvanharten](https://fenicsproject.discourse.group/u/wvanharten)\
**Post date:** [May 16, 2022, 2:47pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/5 "2022-05-16T14:47:04Z")

</div>

Thank you for your suggestion, I’ll do this to make sure.

- Wouter

---

<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 16, 2022, 5:39pm UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/6 "2022-05-16T17:39:11Z")

</div>

If the variational form is constant as a mathematical expression, for instance L=inner(f(t\_i),v)\*dx + inner(grad(u(t\_(i-1)),grad(v))\*dx`this can easily be done with the same form by interpolation into a previous solution function`un` representing u(t\_i-1). See for instance:  
[https://jorgensd.github.io/dolfinx-tutorial/chapter2/ns\_code2.html#variational-form](https://jorgensd.github.io/dolfinx-tutorial/chapter2/ns_code2.html#variational-form)

---

<div class="post-metadata">

**Author:** ![wvanharten](https://avatars.discourse-cdn.com/v4/letter/w/5daacb/32.png) [@wvanharten](https://fenicsproject.discourse.group/u/wvanharten)\
**Post date:** [May 18, 2022, 7:40am UTC](https://fenicsproject.discourse.group/t/efficiently-solving-poisson-eqn-repeatedly/8332/7 "2022-05-18T07:40:31Z")

</div>

Many Thanks to both @dokken and @nate, It has helped me more than I would have thought!

- Wouter
