Hello together,
I’m new to Fenics and I haven’t seen by now any post about really complicated source functions as e.g.

numpy code

pytorch code
My ultimate goal is to solve Poisson’s problem with a neural network (in pytorch) as the source function. In order to do so I’ve started with a minimal example to get pytorch/numpy source functions work: As mentioned below I reduced the working example to the following
import ufl
from mpi4py import MPI
import dolfinx
from dolfinx.fem.petsc import LinearProblem
from dolfinx import mesh
from dolfinx.fem import FunctionSpace
from dolfinx import fem
import torch as tr
import numpy as np
def my_f(x):
# pytorch stuff
#array_np = np.array([x[0], x[1]])
#array_tr = tr.tensor(array_np, dtype=tr.double)
#value = tr.sum(array_tr).to_numpy()
# numpy stuff
array_np = np.array([x[0], x[1]], dtype=np.float64)
value = np.linalg.norm(array_np)
return value
domain = mesh.create_rectangle(MPI.COMM_WORLD, [[1.0, 1.0], [1.0, 1.0]], [50, 50])
V = FunctionSpace(domain, ("Lagrange", 1))
# ## Defining the source term
f = fem.Function(V)
f.interpolate(my_f)
The Ouput for this code is
Traceback (most recent call last):
File "/usr/lib/petsc/lib/python3/distpackages/dolfinx/fem/function.py", line 397, in interpolate
_interpolate(u, cells)
File "/usr/lib/python3.10/functools.py", line 889, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/usr/lib/petsc/lib/python3/distpackages/dolfinx/fem/function.py", line 373, in _interpolate
self._cpp_object.interpolate(u, cells, nmm_interpolation_data)
TypeError: interpolate(): incompatible function arguments. The following argument types are supported:
1. (self: dolfinx.cpp.fem.Function_float64, f: numpy.ndarray[numpy.float64], cells: numpy.ndarray[numpy.int32]) > None
2. (self: dolfinx.cpp.fem.Function_float64, u: dolfinx.cpp.fem.Function_float64, cells: numpy.ndarray[numpy.int32], nmm_interpolation_data: Tuple[List[int], List[int], List[float], List[int]]) > None
3. (self: dolfinx.cpp.fem.Function_float64, expr: dolfinx::fem::Expression<double, double>, cells: numpy.ndarray[numpy.int32]) > None
Invoked with: <dolfinx.cpp.fem.Function_float64 object at 0x7f0721adccb0>, <function my_f at 0x7f08008cd750>, array([ 0, 1, 2, ..., 4997, 4998, 4999], dtype=int32), ((), (), (), ())
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "~/08_test_interpolate.py", line 33, in <module>
f.interpolate(my_f)
File "/usr/lib/petsc/lib/python3/distpackages/dolfinx/fem/function.py", line 402, in interpolate
self._cpp_object.interpolate(np.asarray(u(x), dtype=self.dtype), cells)
IndexError: invalid axis: 0 (ndim = 0)
I really don’t have a clue why this shouldn’t be possible. The dolfinx version is:
DOLFINx version: 0.7.3
I appreciate any help, thanks in advance.