Need help to write exact solutions and sources

Hello, everyone, i am very new to FEnics. I am trying to understand the tutorial codes of Poisson equation and Heat equation. I have seen that the source function is constant in both the tutorial codes of Poisson equation and Heat equation.
Here, it is


I want to know that if my exact solutions is
uex=sin(pi*x) sin(piy) for Poisson Equation and uex(t) =exp(t) sin(pix) sin(piy) for Heat equation. Then, the source function will be non-constant and it will depends on x, y for poisson and x, y, t for Heat equation. Then, how can i write this sources in the code??. Also, how to write the exact solutions in the code so that i can compute L2 and H1 errors.

Please help. It is very needful for me.

Thanks in advance!.

See for instance

Thanks sir. I got my answer for Poisson equation but i am still getting problem for Heat equation.
If uex(t)=exp(t)sin(pix)sin(piy) is the exact solution of Heat equation then how to write it code and it’s corresponding source which depends on t.

# For exact solution
import numpy as np
class exact_solution():
    def __init__(self, t):
        self.t = t

    def __call__(self, x):
        return np.exp(self.t) * np.sin(np.pi * x[0]) * np.sin(np.pi * x[1])


# For source term 
import numpy as np
from dolfinx import fem

class SourceTerm:
    def __init__(self):
        pass  # no parameters needed

    def __call__(self, x, t):
        return (1 + 2 * np.pi**2) * np.exp(t) * np.sin(np.pi * x[0]) * np.sin(np.pi * x[1])

from dolfinx.fem import Expression

t = 0.0  # current time
source_expr = lambda x: (1 + 2 * np.pi**2) * np.exp(t) * np.sin(np.pi * x[0]) * np.sin(np.pi * x[1])
f = fem.Expression(source_expr, X.ufl_element()) 

Is this above codes are fine sir for my uex(t)=exp(t)sin(pix)sin(piy) ??.

No, this is not how I would do it.
Please note that there exist a large collection of demos that cover such aspects

Most notably, you need to have a mechanism for updating t

1 Like

Okay. Thank you so much sir.