# Reading in vector associated with element (or node)

**URL:** <https://fenicsproject.discourse.group/t/reading-in-vector-associated-with-element-or-node/2848>\
**Category:** mesh\
**Created:** [April 9, 2020, 2:42pm UTC](https://fenicsproject.discourse.group/t/reading-in-vector-associated-with-element-or-node/2848 "2020-04-09T14:42:08Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marchirschvogel](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/marchirschvogel/32/1138_2.png) [@marchirschvogel](https://fenicsproject.discourse.group/u/marchirschvogel)\
**Post date:** [April 9, 2020, 2:42pm UTC](https://fenicsproject.discourse.group/t/reading-in-vector-associated-with-element-or-node/2848/1 "2020-04-09T14:42:09Z")

</div>

Dear community,

I’m trying to read a vector which is associated to an element (or could be to a node, too). This is for an anisotropic solid material which has a fiber direction at every cell, which is predefined and should be handed in to Fenics to be read at the element level.

So, the common way I thought this might work is to set a vector attribute in the xdmf file. An abbreviation of my file looks like this:

> \<Xdmf Version=“3.0”\>  
> \<Domain\>  
> \<Grid Name=“Grid”\>  
> \<Geometry GeometryType=“XYZ”\>  
> \<DataItem DataType=“Float” Dimensions=“1199 3” Format=“XML” Precision=“8”\>  
> 31.1172078030027 -37.35353365752 23.3608991129423  
> 49.1337819098811 -1.2034305749188e-14 18.5333726269385  
> …  
> 19.51531616548419 -43.562521374451734 -15.792062807031005  
> \</DataItem\>  
> \</Geometry\>  
> \<Topology NodesPerElement=“4” NumberOfElements=“3612” TopologyType=“tetrahedron”\>  
> \<DataItem DataType=“Int” Dimensions=“3612 4” Format=“XML” Precision=“4”\>  
> 780 893 115 324  
> 766 328 949 334  
> …  
> 214 117 215 1188  
> \</DataItem\>  
> \</Topology\>  
> \<Attribute AttributeType=“Vector” Center=“Cell” Name=“fibers”\>  
> \<DataItem DataType=“Double” Dimensions=“3612” Format=“XML” Precision=“4”\>  
> 0.2324 -0.983765 0.7863  
> -0.1243 -0.283468 0.35235  
> …  
> 0.1243 0.231324 -0.237467  
> \</DataItem\>  
> \</Attribute\>  
> \</Grid\>  
> \</Domain\>

Then, I thought reading the vectors worked as for the scalar attributes for boundary conditions (domain ids):

> mvc\_fib = MeshValueCollection(“double”, mesh, 3)  
> with XDMFFile(‘file.xdmf’) as infile:  
> infile.read(mvc\_fib, “fibers”)
> 
> mf\_fib = cpp.mesh.MeshFunctionDouble(mesh, mvc\_fib)

However, it turns out that the mesh value collection only collects scalar values and orders all my vector components in a 1-d array.

So, even if the xdmf format supports attributes that are vector-, tensor- or in general matrix-valued (as I figured out from the xdmf documentation), I’m not able to get this info properly into Fenics.

It would be great if anyone had an idea on how to get a fiber field to the element level and then to have a element-wise 3x1 vector f0 which can be used in a material law.  
Basically, I have the fiber vectors in a textfile as a list with each row corresponding to an element (or, alternatively, to a node). Maybe there is another option instead of the xdmf attributes?

Thanks!

Best wishes,  
Marc

---

<div class="post-metadata">

**Author:** ![marchirschvogel](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/marchirschvogel/32/1138_2.png) [@marchirschvogel](https://fenicsproject.discourse.group/u/marchirschvogel)\
**Post date:** [April 13, 2020, 11:48am UTC](https://fenicsproject.discourse.group/t/reading-in-vector-associated-with-element-or-node/2848/2 "2020-04-13T11:48:15Z")

</div>

Meanwhile, I figured out how to proceed. Indeed, one has to define mesh functions for each vector component separately, similar to this example:  
[https://fenicsproject.org/docs/dolfin/2018.1.0/python/demos/tensor-weighted-poisson/documentation.html](https://fenicsproject.org/docs/dolfin/2018.1.0/python/demos/tensor-weighted-poisson/documentation.html)

Bw,  
Marc
