Could-not-convert-string-to-float-error when defining FunctionSpace

While following the very first example of the fenics tutorial by Langtangen and Logg on how to solve the Poisson equation, I ran into a problem. Writing and executing just the first few lines of the tutorial, i.e.

from fenics import *

# Create mesh and define function space
mesh = UnitSquareMesh(8, 8)
V = FunctionSpace(mesh, 'P', 1)

gives me the following error message:

Calling FFC just-in-time (JIT) compiler, this may take some time.
Traceback (most recent call last):
  File "/home/mathiasklahn/Documents/PythonExamples/FenicsExamples/PoissonEquation/Main.py", line 5, in <module>
    V = FunctionSpace(mesh, 'P', 1)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 33, in __init__
    self._init_convenience(*args, **kwargs)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 100, in _init_convenience
    self._init_from_ufl(mesh, element, constrained_domain=constrained_domain)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 42, in _init_from_ufl
    ufc_element, ufc_dofmap = ffc_jit(element, form_compiler_parameters=None,
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 50, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 100, in ffc_jit
    return ffc.jit(ufl_form, parameters=p)
  File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 217, in jit
    module = jit_build(ufl_object, module_name, parameters)
  File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 130, in jit_build
    module, signature = dijitso.jit(jitable=ufl_object,
  File "/usr/lib/python3/dist-packages/dijitso/jit.py", line 165, in jit
    header, source, dependencies = generate(jitable, name, signature, params["generator"])
  File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 65, in jit_generate
    code_h, code_c, dependent_ufl_objects = compile_object(ufl_object,
  File "/usr/lib/python3/dist-packages/ffc/compiler.py", line 149, in compile_element
    return compile_ufl_objects(elements, "element", object_names,
  File "/usr/lib/python3/dist-packages/ffc/compiler.py", line 190, in compile_ufl_objects
    ir = compute_ir(analysis, prefix, parameters, jit)
  File "/usr/lib/python3/dist-packages/ffc/representation.py", line 167, in compute_ir
    ir_elements = [_compute_element_ir(e, element_numbers, classnames, parameters, jit)
  File "/usr/lib/python3/dist-packages/ffc/representation.py", line 167, in <listcomp>
    ir_elements = [_compute_element_ir(e, element_numbers, classnames, parameters, jit)
  File "/usr/lib/python3/dist-packages/ffc/representation.py", line 200, in _compute_element_ir
    fiat_element = create_element(ufl_element)
  File "/usr/lib/python3/dist-packages/ffc/fiatinterface.py", line 100, in create_element
    element = _create_fiat_element(ufl_element)
  File "/usr/lib/python3/dist-packages/ffc/fiatinterface.py", line 198, in _create_fiat_element
    element = ElementClass(fiat_cell, degree)
  File "/usr/lib/python3/dist-packages/FIAT/lagrange.py", line 43, in __init__
    poly_set = polynomial_set.ONPolynomialSet(ref_el, degree)
  File "/usr/lib/python3/dist-packages/FIAT/polynomial_set.py", line 168, in __init__
    dv = expansion_set.tabulate_derivatives(degree, pts)
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 277, in tabulate_derivatives
    data = _tabulate_dpts(self._tabulate, 2, n, order, numpy.array(pts))
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 74, in _tabulate_dpts
    symbolic_tab = tabulator(n, X)
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 227, in _tabulate
    ref_pts = [sum(self.A[i][j] * pts[j] for j in range(m2)) + self.b[i]
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 227, in <listcomp>
    ref_pts = [sum(self.A[i][j] * pts[j] for j in range(m2)) + self.b[i]
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/sympy/core/decorators.py", line 246, in _func
    other = sympify(other, strict=True)
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/sympy/core/sympify.py", line 292, in sympify
    return converter[superclass](a)
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/sympy/core/numbers.py", line 1087, in __new__
    num = _convert_numpy_types(num)
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/sympy/core/sympify.py", line 84, in _convert_numpy_types
    return Float(a, precision=prec)
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/sympy/core/numbers.py", line 1143, in __new__
    _mpf_ = mlib.from_str(num, precision, rnd)
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/mpmath/libmp/libmpf.py", line 1331, in from_str
    man, exp = str_to_man_exp(x, base=10)
  File "/home/mathiasklahn/.local/lib/python3.10/site-packages/mpmath/libmp/libmpf.py", line 1294, in str_to_man_exp
    float(x)
ValueError: could not convert string to float: 'np.float64(-1.0)'

I am running fenics version 2019.2.0.12.dev0 on Ubuntu version 22.04.3 LTS and installed fenics using the following commands:

sudo apt-get install software-properties-common
sudo add-apt-repository ppa:fenics-packages/fenics
sudo apt-get update
sudo apt-get install fenics

Can anyone explain to me why I get this error and how to fix the problem?

sympy and its dependency mpmath seem to come from $HOME/.local rather than the system prefix /usr/lib. Please uninstall them from your local path (e.g., pip uninstall) and then try again. If the error appears again, at the very least confirm that sympy is loaded from the system path rather than your local one.

1 Like

Thank you very much for your quick reply! I have tried uninstalling sympy and mpmath from my local path, but the error still appears. The error message below confirms that ‘sympy’ and ‘mpmath’ are now loaded from the system path.

Calling FFC just-in-time (JIT) compiler, this may take some time.
Traceback (most recent call last):
  File "/home/mathiasklahn/Documents/PythonExamples/FenicsExamples/PoissonEquation/Main.py", line 5, in <module>
    V = FunctionSpace(mesh, 'P', 1)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 33, in __init__
    self._init_convenience(*args, **kwargs)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 100, in _init_convenience
    self._init_from_ufl(mesh, element, constrained_domain=constrained_domain)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/function/functionspace.py", line 42, in _init_from_ufl
    ufc_element, ufc_dofmap = ffc_jit(element, form_compiler_parameters=None,
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 50, in mpi_jit
    return local_jit(*args, **kwargs)
  File "/usr/lib/petsc/lib/python3/dist-packages/dolfin/jit/jit.py", line 100, in ffc_jit
    return ffc.jit(ufl_form, parameters=p)
  File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 217, in jit
    module = jit_build(ufl_object, module_name, parameters)
  File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 130, in jit_build
    module, signature = dijitso.jit(jitable=ufl_object,
  File "/usr/lib/python3/dist-packages/dijitso/jit.py", line 165, in jit
    header, source, dependencies = generate(jitable, name, signature, params["generator"])
  File "/usr/lib/python3/dist-packages/ffc/jitcompiler.py", line 65, in jit_generate
    code_h, code_c, dependent_ufl_objects = compile_object(ufl_object,
  File "/usr/lib/python3/dist-packages/ffc/compiler.py", line 149, in compile_element
    return compile_ufl_objects(elements, "element", object_names,
  File "/usr/lib/python3/dist-packages/ffc/compiler.py", line 190, in compile_ufl_objects
    ir = compute_ir(analysis, prefix, parameters, jit)
  File "/usr/lib/python3/dist-packages/ffc/representation.py", line 167, in compute_ir
    ir_elements = [_compute_element_ir(e, element_numbers, classnames, parameters, jit)
  File "/usr/lib/python3/dist-packages/ffc/representation.py", line 167, in <listcomp>
    ir_elements = [_compute_element_ir(e, element_numbers, classnames, parameters, jit)
  File "/usr/lib/python3/dist-packages/ffc/representation.py", line 200, in _compute_element_ir
    fiat_element = create_element(ufl_element)
  File "/usr/lib/python3/dist-packages/ffc/fiatinterface.py", line 100, in create_element
    element = _create_fiat_element(ufl_element)
  File "/usr/lib/python3/dist-packages/ffc/fiatinterface.py", line 198, in _create_fiat_element
    element = ElementClass(fiat_cell, degree)
  File "/usr/lib/python3/dist-packages/FIAT/lagrange.py", line 43, in __init__
    poly_set = polynomial_set.ONPolynomialSet(ref_el, degree)
  File "/usr/lib/python3/dist-packages/FIAT/polynomial_set.py", line 168, in __init__
    dv = expansion_set.tabulate_derivatives(degree, pts)
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 277, in tabulate_derivatives
    data = _tabulate_dpts(self._tabulate, 2, n, order, numpy.array(pts))
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 74, in _tabulate_dpts
    symbolic_tab = tabulator(n, X)
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 227, in _tabulate
    ref_pts = [sum(self.A[i][j] * pts[j] for j in range(m2)) + self.b[i]
  File "/usr/lib/python3/dist-packages/FIAT/expansions.py", line 227, in <listcomp>
    ref_pts = [sum(self.A[i][j] * pts[j] for j in range(m2)) + self.b[i]
  File "/usr/lib/python3/dist-packages/sympy/core/decorators.py", line 261, in _func
    other = sympify(other, strict=True)
  File "/usr/lib/python3/dist-packages/sympy/core/sympify.py", line 365, in sympify
    return converter[superclass](a)
  File "/usr/lib/python3/dist-packages/sympy/core/numbers.py", line 1087, in __new__
    num = _convert_numpy_types(num)
  File "/usr/lib/python3/dist-packages/sympy/core/sympify.py", line 86, in _convert_numpy_types
    return Float(a, precision=prec)
  File "/usr/lib/python3/dist-packages/sympy/core/numbers.py", line 1143, in __new__
    _mpf_ = mlib.from_str(num, precision, rnd)
  File "/usr/lib/python3/dist-packages/mpmath/libmp/libmpf.py", line 1331, in from_str
    man, exp = str_to_man_exp(x, base=10)
  File "/usr/lib/python3/dist-packages/mpmath/libmp/libmpf.py", line 1294, in str_to_man_exp
    float(x)
ValueError: could not convert string to float: 'np.float64(-1.0)'

I suspect you still have an installation of numpy 2.0 in your $HOME/.local, that you will need to get rid of. Please double check if that is the case with pip show numpy.

You were correct that I had an installation of numpy in my /home/... path. Uninstalling it, however, did not change the error message. Using pip show numpy I can see that I also have an installation of numpy in /usr/local/lib/... Should this installation be removed too?

Yes, anything that doesn’t come from /usr/lib (the path where apt installs packages) can break the package from ppa.

Alright! I uninstalled it, and now I don’t get the error message anymore. Thank you very much for your help!