# How to define manufactured function when knowing exact solution

I have a question about how to define the right hand side f in Dolfinx when I know the exact solution to the PDE.
The PDE I consider is u_t - e*div(grad(u))+dot(b,grad(u))+cu=f , where e and b are two constants, and the exact solution I want to test is u(x,y,t)= exp(2pi*t)*sin*(2pi*x)*sin(2pi*y).
I try to use

class exact_solution():
def __init__(self,t):
self.t=t
def __call__(self,x):
return np.exp(np.sin(2*np.pi*self.t))*np.sin*(2*np.pi*x[0])*np.sin*(2*np.pi*x[1])
u_exact=exact_solution(t)

but it does not work.Should I define u_exact as a Function?
May there has some other ways to do this.So how can I use this exact solution to compute the form of the right hand side f which will be used in the linear form ?And how to define the time as a variable since I want to compute the derivative of u w.r.t time, and how to take derivative w.r.t. time (how to compute u_t)?

You are mixing ufl and numpy here.
Consider: Error control: Computing convergence rates — FEniCSx tutorial
which computes the source term for the manufactured problem of a poisson problem

Thank you very much.Now I use this form of definition

import ufl
import numpy as np

def u_exact(mod,t):
return lambda x :mod.exp(mod.sin(2*mod.pi*float(t)))*mod.sin*(2*mod.pi*x[0])*mod.sin*(2*mod.pi*x[1])

exact_soln_ufl=u_exact(ufl,t)  #used for linear form
exact_soln_np=u_exact(np,t)    #used for interpolation

boundary_fun=fem.Function(V)
boundary_fun.interpolate(exact_soln_np)

It sends error like this

unsupported operand type(s) for *: 'float' and 'numpy.ufunc'

So How can I define a function with time t as a parameter when used in function interpolation.And I also want to ask if I use exact_soln_ufl=u_exact(ufl,t) , can I take derivative w.r.t t as diff(exact_soln_ufl,t)?

You need to create a ufl.variable as done in:

You have typos in this code.
It should be

def u_exact(mod, t):
return lambda x: mod.exp(mod.sin(2*mod.pi*float(t)))*mod.sin(2*mod.pi*x[0])*mod.sin(2*mod.pi*x[1])

For any further questions, please provide a minimal reproducible example, like:

import numpy as np
from dolfinx import fem, mesh
import ufl
from mpi4py import MPI

N = 20
msh = mesh.create_unit_cube(comm=MPI.COMM_WORLD, nx=N, ny=N, nz=N)
V = fem.FunctionSpace(msh, ("Lagrange", 1))

def u_exact(mod, t):
return lambda x: mod.exp(mod.sin(2*mod.pi*float(t)))*mod.sin(2*mod.pi*x[0])*mod.sin(2*mod.pi*x[1])

t = 0.1
exact_soln_ufl = u_exact(ufl, t)  # used for linear form
exact_soln_np = u_exact(np, t)  # used for interpolation
boundary_fun = fem.Function(V)
boundary_fun.interpolate(exact_soln_np)

Thank you for your kind replies!