Log dolfinx.fem.Constant

We need .value to access the value of dolfinx.fem.Constant. However, to log it correctly, we also need to make it a list by .tolist(). Otherwise the returned number is always the current number.

Incorrect log:

test_mesh = dolfinx.mesh.create_rectangle(
    comm=mpi4py.MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(32, 16),
    cell_type=dolfinx.mesh.CellType.triangle,
)
test_num = dolfinx.fem.Constant(test_mesh, petsc4py.PETSc.ScalarType(0.))
test_list = []
for i in range(10):
    test_num.value = i
    test_list.append(test_num.value)
    print(test_list)

Correct log:

test_mesh = dolfinx.mesh.create_rectangle(
    comm=mpi4py.MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(32, 16),
    cell_type=dolfinx.mesh.CellType.triangle,
)
test_num = dolfinx.fem.Constant(test_mesh, petsc4py.PETSc.ScalarType(0.))
test_list = []
for i in range(10):
    test_num.value = i
    test_list.append(test_num.value.tolist())
    print(test_list)

This is expected behavior, as test_num.value is a numpy array with shape () for scalar data.
Therefore, if you look at the id of test_num.value:

import mpi4py
import petsc4py
import dolfinx


test_mesh = dolfinx.mesh.create_rectangle(
    comm=mpi4py.MPI.COMM_WORLD,
    points=((0.0, 0.0), (2.0, 1.0)),
    n=(32, 16),
    cell_type=dolfinx.mesh.CellType.triangle,
)
test_num = dolfinx.fem.Constant(test_mesh, petsc4py.PETSc.ScalarType(0.))
test_list = []
for i in range(10):
    test_num.value = i
    test_list.append(test_num.value)
    print(id(test_num.value))

you will see that it is the same for all time steps, as you are assign the data i to the same array every time.
Using append to a list (with numpy array as input) does not make a deepcopy of the array, it adds a reference to the array, rather than a deepcopy. Instead of using tolist, you could alternatively use copy().

for i in range(10):
    test_num.value = i
    test_list.append(test_num.value.copy())
    print(test_list)