Level Set equation on FeniCs variational formulation

Dear FeniCs communtiy.

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:

<\phi , w> = <phi_n , w> + dt * <phi_n , grad(w) * u>

However, when I try to implement it, was I think it should be, I get dimension problems. Can anyone check if the variational form is correctly typed?

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

This is the error I get:

  UFLException: Invalid ranks 1 and 1 in product.

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.

I did, and it worked well, Thanks.

This made it work.

  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.