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

Set default value for MeshFunction created from sparse MeshValueCollection?

0 votes

Hi - is it possible to set a default value for a MeshFunction that is created from a sparse MeshValueCollection?

When I display the MeshFunction, it seems to default to a very large number, I'd like to be able to have it default to zero or something … or be able to scale the values in the display:

mesh = Mesh("testing.xml.gz")
mvc = MeshValueCollection("size_t", mesh, "testing_facets.xml.gz")
mf = MeshFunction("size_t", mesh, mvc)
plot(mf, interactive=True)

Here is an example (sparse) MeshValueCollection:

<?xml version="1.0" encoding="UTF-8"?>
<dolfin xmlns:dolfin="http://fenicsproject.org">
  <mesh_value_collection name="boundary_markers" type="uint" dim="2" size="6">
    <value cell_index="3" local_entity="2" value="3" />
    <value cell_index="9" local_entity="1" value="3" />
    <value cell_index="7" local_entity="1" value="1" />
    <value cell_index="8" local_entity="0" value="2" />
    <value cell_index="0" local_entity="2" value="1" />
    <value cell_index="6" local_entity="0" value="2" />
  </mesh_value_collection>
</dolfin>

Here is the example mesh:

<?xml version="1.0" encoding="UTF-8"?>
<dolfin xmlns:dolfin="http://fenicsproject.org">
  <mesh celltype="tetrahedron" dim="3">
    <cells size="12">
      <tetrahedron index="0" v0="0" v1="1" v2="2" v3="3" />
      <tetrahedron index="1" v0="1" v1="2" v2="4" v3="5" />
      <tetrahedron index="2" v0="2" v1="3" v2="4" v3="6" />
      <tetrahedron index="3" v0="0" v1="1" v2="2" v3="7" />
      <tetrahedron index="4" v0="0" v1="2" v2="3" v3="8" />
      <tetrahedron index="5" v0="2" v1="3" v2="6" v3="8" />
      <tetrahedron index="6" v0="2" v1="5" v2="6" v3="8" />
      <tetrahedron index="7" v0="1" v1="2" v2="3" v3="4" />
      <tetrahedron index="8" v0="2" v1="5" v2="7" v3="8" />
      <tetrahedron index="9" v0="0" v1="2" v2="7" v3="8" />
      <tetrahedron index="10" v0="1" v1="2" v2="5" v3="7" />
      <tetrahedron index="11" v0="2" v1="4" v2="5" v3="6" />
    </cells>
    <vertices size="9">
      <vertex index="0" x="-5.00000000e-01" y="-5.00000000e-01" z="5.00000000e-01" />
      <vertex index="1" x="5.00000000e-01" y="-5.00000000e-01" z="5.00000000e-01" />
      <vertex index="2" x="-4.64727495e-02" y="2.92189643e-02" z="-2.63658106e-02" />
      <vertex index="3" x="-5.00000000e-01" y="5.00000000e-01" z="5.00000000e-01" />
      <vertex index="4" x="5.00000000e-01" y="5.00000000e-01" z="5.00000000e-01" />
      <vertex index="5" x="5.00000000e-01" y="5.00000000e-01" z="-5.00000000e-01" />
      <vertex index="6" x="-5.00000000e-01" y="5.00000000e-01" z="-5.00000000e-01" />
      <vertex index="7" x="5.00000000e-01" y="-5.00000000e-01" z="-5.00000000e-01" />
      <vertex index="8" x="-5.00000000e-01" y="-5.00000000e-01" z="-5.00000000e-01" />
    </vertices>
  </mesh>
</dolfin>
asked Apr 11, 2014 by timm FEniCS User (2,100 points)

1 Answer

+1 vote
 
Best answer

Hi, default value for unset entities of MeshFunction created from MeshValueCollection is
hardcoded as maximum value of given type. You could achieve what you are
after by mimicking what the constructor does

from dolfin import *

mesh = Mesh('testing.xml.gz')
mvc = MeshValueCollection('size_t', mesh, 'testing_facets.xml.gz')
d = mvc.dim()

default_value = 0
mf = MeshFunction('size_t', mesh, d, default_value)
values = mf.array()

D = mesh.topology().dim()
mesh.init(D, d)
connectivity = mesh.topology()(D, d)

mvc_values = mvc.values()
for ci_lei, value in mvc_values.iteritems():
    cell_index, local_entity_index = ci_lei
    entity_index = connectivity(cell_index)[local_entity_index]
    values[entity_index] = value
plot(mf, interactive=True)

Or perhaps you could just not include the large values in the plot

plot(mf, range_min=1., range_max=3., interactive=True)
answered Apr 11, 2014 by MiroK FEniCS Expert (80,920 points)
selected Apr 17, 2014 by timm

Thanks!

The plot range thing pretty much does what I need - but the other bit of code that sets the default value is very interesting. I'm going to have to work through it to see if I can really understand what it is doing ...

...