-
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
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## fenicsx #949 +/- ##
===========================================
- Coverage 96.02% 95.89% -0.13%
===========================================
Files 45 46 +1
Lines 2439 2511 +72
===========================================
+ Hits 2342 2408 +66
- Misses 97 103 +6 ☔ View full report in Codecov by Sentry. |
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
import ufl | ||
import festim as F | ||
|
||
class SurfaceTemperature: |
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.
I would be explicit an name it AverageSurfaceTemperature
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 inherit from SurfaceQuantity
class SurfaceTemperature: | |
class SurfaceTemperature(F.SurfaceQuantity): |
@property | ||
def surface(self): | ||
return self._surface | ||
|
||
@surface.setter | ||
def surface(self, value): | ||
if not isinstance(value, (int, F.SurfaceSubdomain)) or isinstance(value, bool): | ||
raise TypeError("surface should be an int or F.SurfaceSubdomain") | ||
self._surface = value |
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 is duplicated from SurfaceQuantity
. Can be removed when this class inherits from it
@property | ||
def filename(self): | ||
return self._filename | ||
|
||
@filename.setter | ||
def filename(self, value): | ||
if value is None: | ||
self._filename = None | ||
elif not isinstance(value, str): | ||
raise TypeError("filename must be of type str") | ||
elif not value.endswith(".csv") and not value.endswith(".txt"): | ||
raise ValueError("filename must end with .csv or .txt") | ||
self._filename = value |
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.
Can be removed if inherits from SurfaceQuantity
def write(self, t): | ||
"""Writes the time and temperature value to the file. | ||
|
||
Args: | ||
t (float): current time value | ||
""" | ||
if self.filename is not None: | ||
if self._first_time_export: | ||
header = ["t(s)", f"{self.title}"] | ||
with open(self.filename, mode="w+", newline="") as file: | ||
writer = csv.writer(file) | ||
writer.writerow(header) | ||
self._first_time_export = False | ||
with open(self.filename, mode="a", newline="") as file: | ||
writer = csv.writer(file) | ||
writer.writerow([t, self.value]) |
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.
Can be removed if inherits from SurfaceQuantity
@@ -786,6 +793,8 @@ def update_time_dependent_values(self): | |||
if source.temperature_dependent: | |||
source.update(t=t) | |||
|
|||
surface_temp_processed = False |
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.
What is this? the indentation is odd, is this a leftover?
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
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...