festim-dev / FESTIM

Coupled hydrogen/tritium transport and heat transfer modelling using FEniCS
https://festim.readthedocs.io/en/stable/
Apache License 2.0
78 stars 16 forks source link

[BUG] Cannot compute derived quantity with retention with `show_units=True` #765

Closed RemDelaporteMathurin closed 1 month ago

RemDelaporteMathurin commented 1 month ago

Describe the bug When trying to compute a derived quantity (say TotalVolume) with field="retention" the code crashes with the following error:

This bug occurs on main and was introduced by #736

Traceback (most recent call last):
  File "/home/remidm/FESTIM/MWE2.py", line 40, in <module>
    my_model.run()
  File "/home/remidm/FESTIM/festim/generic_simulation.py", line 360, in run
    self.run_steady()
  File "/home/remidm/FESTIM/festim/generic_simulation.py", line 390, in run_steady
    self.run_post_processing()
  File "/home/remidm/FESTIM/festim/generic_simulation.py", line 442, in run_post_processing
    self.exports.write(self.label_to_function, self.mesh.dx)
  File "/home/remidm/FESTIM/festim/exports/exports.py", line 97, in write
    export.compute(self.t)
  File "/home/remidm/FESTIM/festim/exports/derived_quantities/derived_quantities.py", line 155, in compute
    self.data = [self.make_header()]
                 ^^^^^^^^^^^^^^^^^^
  File "/home/remidm/FESTIM/festim/exports/derived_quantities/derived_quantities.py", line 121, in make_header
    header.append(quantity.title)
                  ^^^^^^^^^^^^^^
  File "/home/remidm/FESTIM/festim/exports/derived_quantities/total_volume.py", line 47, in title
    return quantity_title + f" ({self.export_unit})"
                                 ^^^^^^^^^^^^^^^^
  File "/home/remidm/FESTIM/festim/exports/derived_quantities/total_volume.py", line 36, in export_unit
    dim = self.function.function_space().mesh().topology().dim()
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Sum' object has no attribute 'function_space'

To Reproduce

Run:

import festim as F

my_model = F.Simulation()

import numpy as np

my_model.mesh = F.MeshFromVertices(np.linspace(0, 1, num=200))

my_model.materials = F.Material(id=1, D_0=1, E_D=0)

trap = F.Trap(
    k_0=1,
    E_k=0,
    p_0=1,
    E_p=0,
    density=1.3e-3,
    materials=my_model.materials[0],
)

my_model.traps = [trap]

my_model.T = F.Temperature(500)

my_model.settings = F.Settings(
    absolute_tolerance=1e10, relative_tolerance=1e-09, transient=False
)

my_model.exports = [
    F.DerivedQuantities(
        [
            F.TotalVolume("retention", volume=1),
        ],
        show_units=True,
    )
]

my_model.initialise()
my_model.run()