-
Notifications
You must be signed in to change notification settings - Fork 25
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Export average surface temperature #949
base: fenicsx
Are you sure you want to change the base?
Export average surface temperature #949
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @kaelyndunnell thanks for this, this is very much needed!
However I think it should be implemented differently:
- It doesn't make sense to ask users to provide
temperature_field
as an argument ofSurfaceTemperature
since they don't have access to the fenics object (fem.Constant
orfem.Temperature
) before callinginitialise
and since exports are defined before callinginitialise
. Instead, I believe the class should not have this argument at all, andtemperature_field
is set as an attribute only insideHTransportProblem
for export in self.exports:
if isinstance(export, SurfaceTemperature):
export.temperature_field = self.temperature_fenics
SurfaceTemperature
shares a lot of functionality withSurfaceQuantity
: it should inherit from it- I'm thinking by looking at this we should revisit the design of these quantities @JonathanGSDUFOUR , I'll open an issue to describe my idea but here's the rough pitch
class SurfaceQuantity:
def __init__(self, surface, filename):
self.surface = surface
self.filename = filename
self.field = None # this is no longer a user argument but only an attribute
...
class AverageSurfaceConcentration(SurfaceQuantity):
def __init__(self, species, surface, filename):
self.species = species
super.__init__(surface, filename)
Inside HTransportProblem
def initialise_exports(self):
for export in self.exports:
if not hasattr(export, "species"): # appropriate fenics functions are already stored in F.Species objects
export.field = self.temperature_fenics
elif isinstance(export, exports.SurfaceTemperature): | ||
export.compute(self.ds) # compute surface temp | ||
|
||
export.t.append(float(self.t)) # update export time | ||
|
||
# if filename given write export data to file | ||
if export.filename is not None: | ||
export.write(t=float(self.t)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These lines are not tested. That's because by design, it's impossible to use SurfaceTemperature
in combination with HTransportProblem
Co-authored-by: Rémi Delaporte-Mathurin <[email protected]>
@@ -415,6 +418,9 @@ def initialise_exports(self): | |||
export.D = D | |||
export.D_expr = D_expr | |||
|
|||
if isinstance(export, exports.AverageSurfaceTemperature): | |||
export.temperature_field = self.temperature_fenics |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be tested!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
See comment below about integration test
def test_write_overwrite(tmp_path): | ||
"""Test that the write method overwrites the file if it already exists""" | ||
filename = os.path.join(tmp_path, "my_export.csv") | ||
my_model = F.HydrogenTransportProblem( | ||
mesh=F.Mesh1D(np.linspace(0, 6.0, 10000)), temperature=500 | ||
) | ||
my_model.define_temperature() | ||
|
||
my_export = F.AverageSurfaceTemperature( | ||
filename=filename, | ||
surface=F.SurfaceSubdomain1D(id=35, x=1), | ||
) | ||
my_export.value = 2.0 | ||
my_export.write(0) | ||
my_export.write(1) | ||
|
||
my_export2 = F.AverageSurfaceTemperature( | ||
filename=filename, | ||
surface=F.SurfaceSubdomain1D(id=1, x=1), | ||
) | ||
my_export2.value = 3.0 | ||
my_export2.write(1) | ||
my_export2.write(2) | ||
my_export2.write(3) | ||
|
||
data = np.genfromtxt(filename, delimiter=",", names=True) | ||
file_length = data.size | ||
expected_length = 3 | ||
|
||
assert file_length == expected_length | ||
|
||
|
||
def test_filename_setter_raises_TypeError(): | ||
"""Test that a TypeError is raised when the filename is not a string""" | ||
|
||
with pytest.raises(TypeError, match="filename must be of type str"): | ||
my_model = F.HydrogenTransportProblem( | ||
mesh=F.Mesh1D(np.linspace(0, 6.0, 10000)), temperature=500 | ||
) | ||
my_model.define_temperature() | ||
|
||
F.AverageSurfaceTemperature( | ||
filename=1, | ||
surface=F.SurfaceSubdomain1D(id=1, x=1), | ||
) | ||
|
||
|
||
def test_filename_setter_raises_ValueError(tmp_path): | ||
"""Test that a ValueError is raised when the filename does not end with .csv or .txt""" | ||
|
||
with pytest.raises(ValueError): | ||
my_model = F.HydrogenTransportProblem( | ||
mesh=F.Mesh1D(np.linspace(0, 6.0, 10000)), temperature=500 | ||
) | ||
my_model.define_temperature() | ||
|
||
F.AverageSurfaceTemperature( | ||
filename=os.path.join(tmp_path, "my_export.xdmf"), | ||
surface=F.SurfaceSubdomain1D(id=1, x=1), | ||
) | ||
|
||
|
||
@pytest.mark.parametrize("value", ["my_export.csv", "my_export.txt"]) | ||
def test_writer(tmp_path, value): | ||
"""Test that the writes values at each timestep to either a csv or txt file""" | ||
my_model = F.HydrogenTransportProblem( | ||
mesh=F.Mesh1D(np.linspace(0, 6.0, 10000)), temperature=500 | ||
) | ||
my_model.define_temperature() | ||
|
||
my_export = F.AverageSurfaceTemperature( | ||
filename=os.path.join(tmp_path, f"{value}"), | ||
surface=F.SurfaceSubdomain1D(id=1, x=0), | ||
) | ||
my_export.value = 2.0 | ||
|
||
for i in range(10): | ||
my_export.write(i) | ||
file_length = len(np.genfromtxt(my_export.filename, delimiter=",")) | ||
|
||
expected_length = i + 2 | ||
|
||
assert file_length == expected_length | ||
|
||
|
||
def test_surface_setter_raises_TypeError(): | ||
"""Test that a TypeError is raised when the surface is not a | ||
F.SurfaceSubdomain""" | ||
|
||
with pytest.raises( | ||
TypeError, match="surface should be an int or F.SurfaceSubdomain" | ||
): | ||
my_model = F.HydrogenTransportProblem( | ||
mesh=F.Mesh1D(np.linspace(0, 6.0, 10000)), temperature=500 | ||
) | ||
my_model.define_temperature() | ||
F.AverageSurfaceTemperature( | ||
surface="1", | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These tests aren't need as it tests the functionality in the parent class SurfaceQuantity
my_export = F.AverageSurfaceTemperature(surface=dummy_surface) | ||
my_export.temperature_field = my_model.temperature_fenics | ||
my_export.compute(ds) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good, but we also need to test the full integration with HydrogenTransportProblem
ie. just do
my_model.exports = [....]
my_model.initialise()
my_model.run()
# test value of export after run
Proposed changes
Adds export for average surface temperature.
Types of changes
What types of changes does your code introduce to FESTIM?
Checklist
Further comments
If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...