Pass fenics form to a python function


I want to pass a relationship based on the solution of the problem to a python function as a float in every time step. Here a sample of my solution loop:

while (time <= T):

    # solve the system and obtain the solution (u,trz)
	nIter, rNorm = NewtonRaphson(Res,Jac,bcs_,uknown,uknown0,rank)
    # split the unkown
	u,trz = uknown.split(True)
    # Define a tensor based on the solution of the u
    F = as_tensor([[u,0.,0.],[0.,u,0.],[0.,0.,1.]])
    # Define a scalar quantity
    FF = inner(F,F)
    # pass this scalar to a python function and take the output "out" which is a float
    out = reaction(FF)

	# Move to next time step

	time  += dt

I want to pass the quantity FF, as a float, to a python function reaction() to solve a system of ODEs with solve_ivp of scipy. How can I do it ?

Thanks you

You Need to assemble FF over an integration domain. If you want FF to be a scalar over every cell, you would have to project FF into a suitable function space (FunctionSpace(mesh, "DG", 0)) and access the underlying array of the output function,

Q = FunctionSpace(mesh, "DG", 0)
q = project(FF, Q)

or alternatively, if you want FF to be a single scalar:

q = assemble(FF*dx)

Thank you @dokken !!