Filling up a MeshFunction with a numpy array

Asked by Luis Linares

Hello,

I have read about the pending implementation of reading MeshFunction XML files for parallel computations. As a workaround for this, I have tried the following:

mesh = Mesh( "mesh.xml" )
domains = MeshFunction('uint', mesh, "mesh_domains.xml")
indices = domains.array()
"""
.
pickle indices in serial code, unpickle in parallel code
.
"""
par_domains = MeshFunctuion('uint', mesh)
par_domains.set_values( unpickled_indices_array )

but the call to set_values returns:

TypeError: in method 'MeshFunctionUInt_set_values', argument 2 of type 'std::vector< unsigned int > const &'

I have the latest fenics version of the Ubuntu repositories. I have read elsewhere in the net and the call to set_values is supposed to work with a numpy array of the proper datatype (as is mine). Do I have any other choice??

Question information

Language:
English Edit question
Status:
Solved
For:
DOLFIN Edit question
Assignee:
No assignee Edit question
Solved by:
Johan Hake
Solved:
Last query:
Last reply:
Revision history for this message
Best Johan Hake (johan-hake) said :
#1

Hello!

Reading MeshFunctions in paralell should just work from at least the 1.0
version. It might be that you have stored the MeshFunction in an old
format. Then you just re store it in a serial run and you should be fine.

   File("mesh_domains.xml") << MeshFunction('uint', mesh,
"mesh_domains.xml")

On 06/06/2012 03:05 AM, Luis Linares wrote:
> New question #199520 on DOLFIN:
> https://answers.launchpad.net/dolfin/+question/199520
>
> Hello,
>
> I have read about the pending implementation of reading MeshFunction XML files for parallel computations. As a workaround for this, I have tried the following:
>
> mesh = Mesh( "mesh.xml" )
> domains = MeshFunction('uint', mesh, "mesh_domains.xml")
> indices = domains.array()
> """
> .
> pickle indices in serial code, unpickle in parallel code
> .
> """
> par_domains = MeshFunctuion('uint', mesh)
> par_domains.set_values( unpickled_indices_array )
>
> but the call to set_values returns:
>
> TypeError: in method 'MeshFunctionUInt_set_values', argument 2 of type 'std::vector< unsigned int> const&'
>
> I have the latest fenics version of the Ubuntu repositories. I have read elsewhere in the net and the call to set_values is supposed to work with a numpy array of the proper datatype (as is mine). Do I have any other choice??

from dolfin import *
mesh = UnitCube(2,2,2)
mf = CellFunction("uint", mesh)
mf.set_values(np.ones(mf.size(), dtype=uintc))

works just fine here. It might be that you need the development version
for this to work. However you should be able to assign directly to the
numpy array repr of the MeshFunction:

mf.array()[:] = np.ones(mf.size())

This works because the array() return a view of the underlying data
values, so any changes on that array will alter the MeshFunction.

Johan

Revision history for this message
Luis Linares (luis-linares) said :
#2

Hi Johan,

Thanks for the tips! Both procedures worked as you stated. I installed the dev version of dolfin and It work fine. It was actually easy to simply remove dolfin (Ubuntu won't remove all the rest of the FeniCS packages, except for 'fenics' itself which is just a dummy) and build and checkinstall!

Cheers,

Luis

Revision history for this message
Luis Linares (luis-linares) said :
#3

Thanks Johan Hake, that solved my question.