I’ve been trying to solve a level set equation that depends on velocity, however I can’t figure out what’s wrong with my variational formulation. The weak form of the equation is:

Hi. Its hard to tell without info about ifo about formulation of phi, w and u. But if phi and w are test and trial of scalar field and u is a vector field then grad(w)*u is rather ambigues term. If so you might want to use some inner product there.

a = dot(phi, v)*dx
L = dt*dot(phi_n , dot(grad(v) , u))*dx + dot(phi_n , v)*dx

However, now I’m having trouble solving the system generated. The scheme looks like this:

u = Expression(('1.0', '0.0'), degree=1)
# Define time parameters
T = 1.0 # Total simulation time
dt = 0.01 # Time step size
t = 0.0 # Initial time
# Define the advection equation
phi = TrialFunction(V)
phi_n = interpolate(initial_condition, V) # Initial condition
v = TestFunction(V)
a = dot(phi, v)*dx
L = dt*dot(phi_n , dot(grad(v) , u))*dx + dot(phi_n , v)*dx
# Create a function to store the solution
phi = Function(V)
i = 0
# Time-stepping loop
while t < T:
solve(a == L, phi)
name = "img5/output" + i * "a" + ".jpg"
a = plot(phi_n, dt, title="Level Set Equation")
plt.colorbar(a)
plt.savefig(name)
plt.clf()
t += dt
phi_n.assign(phi) # Update the solution for the next time step

Throws the following error:
*** Error: Unable to successfully call PETSc function ‘MatSetValuesLocal’.

Please create a reproducible example, i.e.
A complete code (with imports) that uses a built-in mesh, so that it can be executed by others and get the same error message.