# Error with UFL type inside a loop

Hello!
I have a code that works well but when I use a loop it does not work and I do not know which is the error because I have done all steps out of the code and it works.

from dolfin import *
import matplotlib.pyplot as plt
import math
import numpy as np

mesh =UnitCubeMesh(4, 4, 4)
# Defining the function spaces
V_c = FunctionSpace(mesh, 'P', 1)

# Velocity field
vel_x= Expression(('vmag*cos(theta)*ux','vmag*sin(theta)*ux','ux'), domain=mesh, degree=1, theta=pi/2, vmag=2, ux=5)
vel_y= Expression(('vmag*cos(theta)*uy','vmag*sin(theta)*uy','uy'), domain=mesh, degree=1, theta=pi/2, vmag=2, uy=2)
for theta in np.linspace(0, pi/2, num=10):
for vmag in np.linspace(0.5, 9, num=18):
u=vmag*(cos(theta)*vel_x+vmag*sin(theta)*vel_y)
#STABILIZATION TERMS
norm=sqrt(dot(u,u))


--------------------------------------------------------------------------
UFLValueError                             Traceback (most recent call last)
<ipython-input-15-ac6ef75c3701> in <module>
3         u=vmag*(cos(theta)*vel_x+vmag*sin(theta)*vel_y)
4         #STABILIZATION TERMS
----> 5         norm=sqrt(dot(u,u))

~/anaconda3/envs/nfenics/lib/python3.8/site-packages/ufl/operators.py in dot(a, b)
181 def dot(a, b):
182     "UFL operator: Take the dot product of *a* and *b*. The complex conjugate of the second argument is taken."
--> 183     a = as_ufl(a)
184     b = as_ufl(b)
185     if a.ufl_shape == () and b.ufl_shape == ():

~/anaconda3/envs/nfenics/lib/python3.8/site-packages/ufl/constantvalue.py in as_ufl(expression)
469         return IntValue(expression)
470     else:
--> 471         raise UFLValueError("Invalid type conversion: %s can not be converted"
472                             " to any UFL type." % str(expression))

UFLValueError: Invalid type conversion: [Product(FloatValue(0.5), Indexed(Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3), 22), VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3)), 37), MultiIndex((FixedIndex(0),))))
Product(FloatValue(0.5), Indexed(Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3), 22), VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3)), 37), MultiIndex((FixedIndex(1),))))
Product(FloatValue(0.5), Indexed(Coefficient(FunctionSpace(Mesh(VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3), 22), VectorElement(FiniteElement('Lagrange', tetrahedron, 1), dim=3)), 37), MultiIndex((FixedIndex(2),))))] can not be converted to any UFL type.


I guess that the error is related to use np at the moment to perform the loop

Please reduce your code to a minimal example that can be reproduced by anyone. This implies repmoving reading in input.
I would suggest you try to remove all code that isn’t needed to reproduce the error. If you can remove a variable, and the error is still present, please reduce the example.

I hope that it will be more clear now, do you any suggestions why it does not work ?

Wrap the numpy.float64 as float:

       u=float(vmag)*(cos(theta)*vel_x+float(vmag)*sin(theta)*vel_y)


It works thank you so much for your help.