This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

MeshValueCollection Problem

0 votes

Hey! I'm trying to read mesh values through the following lines:

mvc_Dxy = MeshValueCollection("size_t", mesh, "Dxy_center.xml")
meshf_Dxy = MeshFunction("size_t", mesh, mvc_Dxy)
values_Dxy = meshf_Dxy.array()

but it gives me the wrong values. In fact I need to read either positive and negative values.. I know that size_t is not the right type but double or uint fail too (uint doesn't exist anymore)..
Here few lines from Dxy_center.xml

    <value cell_index="0" local_entity="0" value="-9" />
    <value cell_index="1" local_entity="0" value="-55" />
    <value cell_index="2" local_entity="0" value="-207" />

For example if I print values_Dxy[1], I get 4294967241!

Can anyone help?

Thanks
Cristina

asked Sep 25, 2014 by MCri FEniCS User (1,120 points)

1 Answer

+1 vote
 
Best answer

Hi, the problem is that you have defined the collection and mesh function to hold size_t (unsigned integers) but your values are negative (the second one is -55). Try instead

mvc_Dxy = MeshValueCollection("int", mesh, "Dxy_center.xml")
meshf_Dxy = MeshFunction("int", mesh, mvc_Dxy)
values_Dxy = meshf_Dxy.array()
answered Sep 25, 2014 by MiroK FEniCS Expert (80,920 points)
selected Sep 29, 2014 by MCri

It gives me this:

mvc_Dxy = MeshValueCollection("int", mesh, "Dxy_center.xml")
File "/usr/lib/python2.7/dist-packages/dolfin/cpp/mesh.py", line 7861, in new
return MeshValueCollectionInt(args)
File "/usr/lib/python2.7/dist-packages/dolfin/cpp/mesh.py", line 7109, in init
_mesh.MeshValueCollectionInt_swiginit(self,_mesh.new_MeshValueCollectionInt(
args))
RuntimeError:

And I give both positive and negative numbers:

    <value cell_index="3" local_entity="0" value="-26" />
    <value cell_index="4" local_entity="0" value="-197" />
    <value cell_index="5" local_entity="0" value="55" />
    <value cell_index="6" local_entity="0" value="-49" />

If you check the header in the xml files what is the type attribute?

Do you mean this:

<?xml version="1.0"?>
<dolfin xmlns:dolfin="http://www.fenicsproject.org"> 
 <mesh_value_collection type="uint" dim="3" size="276607">

(Sorry for the delay !)

Yes, exactly that. Try making it

<mesh_value_collection type="int" dim="3" size="276607">

Just can I ask you an other question?
I read the values with these few lines:

mvc_Dyz = MeshValueCollection("int", mesh, "Dyz_center.xml")
meshf_Dyz = MeshFunction("int", mesh, mvc_Dyz)
values_Dyz = meshf_Dyz.array()

If I have to "copy" them on another MeshFunction, what should I use?
For example if I do:

t12=MeshFunction("double",mesh,3)
while (i<n):
    t12[i] = values_Dyz[i] / trace[i]

where trace[i] is a positive number, or just t12[i] = values_Dyz[i].. it gives me (AGAIN) wrong values! If I use "int" too

Yes, double is the right specialization of the template. What do you mean wrong? Could this be interger-division problem? I mean, is there a difference between

values_Dyz[i] / trace[i]

and

values_Dyz[i] /float(trace[i])

This is what I do: I read Dxx,Dyy and Dzz values, which are only positive numbers and they work with size_t. The trace is the sum of these values:

mvc_Dxx = MeshValueCollection("size_t", mesh, "Dxx_center.xml")
meshf_Dxx = MeshFunction("size_t", mesh, mvc_Dxx)
values_Dxx = meshf_Dxx.array()

mvc_Dyy = MeshValueCollection("size_t", mesh, "Dyy_center.xml")
meshf_Dyy = MeshFunction("size_t", mesh, mvc_Dyy)
values_Dyy = meshf_Dyy.array()

mvc_Dzz = MeshValueCollection("size_t", mesh, "Dzz_center.xml")
meshf_Dzz = MeshFunction("size_t", mesh, mvc_Dzz)
values_Dzz = meshf_Dzz.array()`
t12=MeshFunction("double",mesh,3)

trace = MeshFunction("size_t",mesh,3)

Then I do :

while (i<n):
    trace[i] = (values_Dxx[i] + values_Dyy[i] + values_Dzz[i]) 
    t12[i] = values_Dxy[i]/trace[i]

It gives me
t12[i] = values_Dxy[i]/trace[i]
File "/usr/lib/python2.7/dist-packages/dolfin/cpp/mesh.py", line 6144, in setitem
self._setitem(index, value)
TypeError: in method 'MeshFunctionDouble__setitem', argument 3 of type 'double'

Anyway, if I only do: t12[i]= Dxy[i] it gives me wrong numbers, such as
1836908392
32739
205851392
instead of -9; -5

The type of values_Dxy[i]/trace[i] must match the type of t12 in order for assignment
to work. You get the error because type of rhs is not double. Try

t12[i] = float(values_Dxy[i]/trace[i])
...