# Name in Function class does not write attribute into xdmf

**URL:** <https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361>\
**Category:** General\
**Tags:** visualization\
**Created:** [April 21, 2024, 8:35pm UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361 "2024-04-21T20:35:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mingming\_Zhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mingming_zhang/32/6535_2.png) [@Mingming\_Zhang](https://fenicsproject.discourse.group/u/Mingming_Zhang)\
**Post date:** [April 21, 2024, 8:35pm UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/1 "2024-04-21T20:35:10Z")

</div>

Hi! I’m trying to give a specific name to a field, but the method (or parameter) `name` provided by `Function` class just ignore the name string I provided, which results in some random name like f\_20, f\_117 etc. The MWE is as following:

```auto
from fenics import *
import os

mesh = RectangleMesh(Point(0, 0), Point(1.0, 1.0), 10, 10)

def initialize_temperature(Q):
      
      T0 = Expression('0.5*(1.0-x[1]*x[1])+0.01*cos(pi*x[0]/length)*sin(pi*x[1]/height)', length = 1, height = 1, degree=1)
      T0 = interpolate(T0, Q)
      
      return T0, 0.0
  
V = VectorElement('Lagrange', mesh.ufl_cell(), 2)
Q = FiniteElement('Lagrange', mesh.ufl_cell(), 1)
W = FunctionSpace(mesh, MixedElement([V, Q]))

QT = FunctionSpace(mesh, "DG", 1)
T_n = Function(QT, name = "Temperature")
T_n, t = initialize_temperature(QT)

dirname = "result"
outputname = "convection"
xdmfpath = os.path.abspath(dirname)+"/"+outputname+".xdmf"
xdmf_field = XDMFFile(xdmfpath)
hdf5path = os.path.abspath(dirname)+"/"+outputname
field_series = TimeSeries(hdf5path)

#Here is probably the error
xdmf_field.write(T_n, t)
field_series.store(T_n.vector(), t)
xdmf_field.close()

```

short notes on this snippet, `T_n` is a Function object with name “Temperature” I’d like to visualize in Paraview. If I debug this code, type `T_n.name()` in terminal, it gives “Temperature”, which is right. But the command `xdmf_field.write(T_n, t)` just ignores `T_n`’s name and writes some arbitrary string in convection.xdmf, e.g. f\_20. Paraview will just show that arbitrary string for that field.

I also found a method `name` and `rename` in class` XDMFFile`, but I haven’t figured out how to use it. Any suggestion? Thanks!

---

<div class="post-metadata">

**Author:** ![chenyongxin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chenyongxin/32/6501_2.png) [@chenyongxin](https://fenicsproject.discourse.group/u/chenyongxin)\
**Post date:** [April 22, 2024, 4:04am UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/2 "2024-04-22T04:04:59Z")

</div>

> [@Mingming\_Zhang](#):
>
> ```auto
> T_n = Function(QT, name = "Temperature")
> T_n, t = initialize_temperature(QT)
> 
> ```

The issue comes in the second line where you actually create a new `T_n` to overwrite the older one but without naming.

To fix it, conder to change your function `initialize_temperature` to

```auto
def initialize_temperature(Q):
      
      T0 = Expression('0.5*(1.0-x[1]*x[1])+0.01*cos(pi*x[0]/length)*sin(pi*x[1]/height)', length = 1, height = 1, degree=1)
      T0 = interpolate(T0, Q)
      T0.rename("Temperature", "")

      return T0, 0.0

```

---

<div class="post-metadata">

**Author:** ![Mingming\_Zhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mingming_zhang/32/6535_2.png) [@Mingming\_Zhang](https://fenicsproject.discourse.group/u/Mingming_Zhang)\
**Post date:** [April 22, 2024, 4:41am UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/3 "2024-04-22T04:41:21Z")

</div>

Gee! This is kinda tricky, it works now, thanks! But when you say ‘create a new `T_n` to overwrite the older one’, do you mean these two `T_n` are completely different, from a class-object point of view? If so, the first `T_n` is not required and can be omitted.

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 22, 2024, 4:48am UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/4 "2024-04-22T04:48:17Z")

</div>

Yes, Since the first T\_n is never accessed before the next one is created, you can remove it.

---

<div class="post-metadata">

**Author:** ![Mingming\_Zhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mingming_zhang/32/6535_2.png) [@Mingming\_Zhang](https://fenicsproject.discourse.group/u/Mingming_Zhang)\
**Post date:** [April 22, 2024, 5:13am UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/5 "2024-04-22T05:13:24Z")

</div>

This is quite dangerous I have to say. In my full-size code, the first `T_n` is created to store solution, a Function object. Then temperature field is initialized and pushed into the second `T_n`, an interpolated Expression object. If I want to use the first `T_n` to save numerical solution and do some manipulation, what to do? Besides why can’t we push initialized Expression object into the first numerical solution `T_n` ? This is more intuitive and consistent, thanks!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [April 22, 2024, 5:33am UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/6 "2024-04-22T05:33:04Z")

</div>

> [@Mingming\_Zhang](#):
>
> This is quite dangerous I have to say.

This is standard Python behavior.  
When you use = you redirect the variable, discarding the old variable if you do not have a reference to it somewhere Else.

You would have to use `dolfin.Function.assign` or `dolfin.Function.interpolate` to store data in an already initialized variable.

---

<div class="post-metadata">

**Author:** ![chenyongxin](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/chenyongxin/32/6501_2.png) [@chenyongxin](https://fenicsproject.discourse.group/u/chenyongxin)\
**Post date:** [April 22, 2024, 5:33am UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/7 "2024-04-22T05:33:37Z")

</div>

Consider the following:

```auto
T_n = Function(QT, name = "Temperature") # initialized

T_n_new, t = initialize_temperature(QT)
T_n.assign(T_n_new)

```

---

<div class="post-metadata">

**Author:** ![Mingming\_Zhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mingming_zhang/32/6535_2.png) [@Mingming\_Zhang](https://fenicsproject.discourse.group/u/Mingming_Zhang)\
**Post date:** [April 22, 2024, 1:57pm UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/8 "2024-04-22T13:57:43Z")

</div>

Thanks for the clarification!

---

<div class="post-metadata">

**Author:** ![Mingming\_Zhang](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/mingming_zhang/32/6535_2.png) [@Mingming\_Zhang](https://fenicsproject.discourse.group/u/Mingming_Zhang)\
**Post date:** [April 22, 2024, 1:58pm UTC](https://fenicsproject.discourse.group/t/name-in-function-class-does-not-write-attribute-into-xdmf/14361/9 "2024-04-22T13:58:47Z")

</div>

Thanks! Really helpful!
