How to import 3D data into a function space?

Hi, I am a novice, please point out any mistakes if there are any. I am attempting to calculate the potential field distribution within a galaxy by solving the 3D Poisson equation. I have stored discrete mass density data in ‘grid_density’ of size (10, 10, 10), and I wish to interpolate or project this data into the defined function space to compute the Poisson equation. However, I encountered an error, and the following is the code and the error message. I would greatly appreciate it if you could provide some helpful advice to help me.

from fenics import *
mesh = BoxMesh(Point(-1,-1,-1),Point(1,1,1),10, 10, 10)
V = FunctionSpace(mesh, 'P', 1)

density = Function(V)
density = project(grid_density, V) # grid_density.shape = (10,10,10), storing mass density data

*** Error: Unable to set local values of PETSc vector.

*** Reason: Size of values array is not equal to local vector size.

*** Where: This error was encountered inside PETScVector.cpp.

*** Process: 0

*** DOLFIN version: 2019.1.0

*** Git changeset: 12ef077802cc9fad34cf984ec7af80585b44301b

Is grid_density just a numpy ndarray?
If so consider Manipulating images - #2 by bleyerj
Which covers the 2D case, and is easy to extend to 3D

1 Like

yes, it is numpy ndarray. I will have a try. Thank you