Skip to content
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

Average surface and volume derived quantities in Cylindrical and Spherical #820

Merged
merged 31 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fb31540
first effort
jhdark Jul 25, 2024
5fb6032
add new classes
jhdark Jul 25, 2024
cd7c930
remove comments and testing
jhdark Jul 26, 2024
e82da3a
fix
jhdark Jul 26, 2024
740e8e6
surface not volume
jhdark Jul 26, 2024
b0ce4fb
fix documentation
jhdark Jul 26, 2024
a12c519
fix azimuth range spherical and doc strings
jhdark Jul 26, 2024
307b95b
no more than pi in range
jhdark Jul 26, 2024
a83849e
ranges cancel out surface
jhdark Jul 26, 2024
4bc4a6a
remove angles
jhdark Jul 26, 2024
861fb61
Merge branch 'main' into cyl_and_sph_avg_quantities
jhdark Aug 5, 2024
bb1a023
update doc strings
jhdark Aug 5, 2024
d4fe1c2
remove print and quit
jhdark Aug 5, 2024
d904cb3
added tests
jhdark Aug 5, 2024
b96300d
rename tests
jhdark Aug 5, 2024
41b90f1
change export name
jhdark Aug 5, 2024
5944b68
test average volume
jhdark Aug 5, 2024
e747d52
dont need haevy mesh
jhdark Aug 5, 2024
1c22343
Merge branch 'main' into cyl_and_sph_avg_quantities
jhdark Jan 10, 2025
636a322
show units now default
jhdark Jan 10, 2025
87c4312
simplify avg sph surf class
jhdark Jan 10, 2025
d4b6979
fix tests
jhdark Jan 10, 2025
152a9c5
remove unused imports
jhdark Jan 10, 2025
1e5f411
Update festim/exports/derived_quantities/average_surface.py
jhdark Jan 10, 2025
296d744
test avg surface on a rhombus
jhdark Jan 10, 2025
58bd814
add allowed meshes property
jhdark Jan 14, 2025
e5f0a6a
accept spherical meshes for evg surface
jhdark Jan 14, 2025
ce2f148
remove uneccesary test
jhdark Jan 14, 2025
47f853e
test allowed meshes
jhdark Jan 14, 2025
301cba0
additional tests for avg volume
jhdark Jan 14, 2025
10fc3b0
simplify tests
jhdark Jan 14, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions festim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@
)
from .exports.derived_quantities.hydrogen_flux import HydrogenFlux
from .exports.derived_quantities.thermal_flux import ThermalFlux
from .exports.derived_quantities.average_volume import AverageVolume
from .exports.derived_quantities.average_volume import (
AverageVolume,
AverageVolumeCylindrical,
AverageVolumeSpherical,
)
from .exports.derived_quantities.maximum_volume import MaximumVolume
from .exports.derived_quantities.minimum_volume import MinimumVolume
from .exports.derived_quantities.minimum_surface import MinimumSurface
from .exports.derived_quantities.maximum_surface import MaximumSurface
from .exports.derived_quantities.total_surface import TotalSurface
from .exports.derived_quantities.total_volume import TotalVolume
from .exports.derived_quantities.average_surface import AverageSurface
from .exports.derived_quantities.average_surface import (
AverageSurface,
AverageSurfaceCylindrical,
AverageSurfaceSpherical,
)
from .exports.derived_quantities.point_value import PointValue

from .exports.derived_quantities.derived_quantities import DerivedQuantities
Expand Down
89 changes: 89 additions & 0 deletions festim/exports/derived_quantities/average_surface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from festim import SurfaceQuantity
import fenics as f
import numpy as np


class AverageSurface(SurfaceQuantity):
Expand Down Expand Up @@ -43,3 +44,91 @@
return f.assemble(self.function * self.ds(self.surface)) / f.assemble(
1 * self.ds(self.surface)
)


class AverageSurfaceCylindrical(AverageSurface):
RemDelaporteMathurin marked this conversation as resolved.
Show resolved Hide resolved
"""
Computes the average value of a field on a given surface
int(f ds) / int (1 * ds)
ds is the surface measure in cylindrical coordinates.
ds = r dr dtheta

Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
surface (int): the surface id
title (str): the title of the derived quantity
show_units (bool): show the units in the title in the derived quantities
file
function (dolfin.function.function.Function): the solution function of
RemDelaporteMathurin marked this conversation as resolved.
Show resolved Hide resolved
the field

Notes:
Units are in H/m3 for hydrogen concentration and K for temperature
"""

def __init__(self, field, surface) -> None:
super().__init__(field=field, surface=surface)
self.r = None

Check warning on line 71 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L70-L71

Added lines #L70 - L71 were not covered by tests

def compute(self):

if self.r is None:
mesh = (

Check warning on line 76 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L75-L76

Added lines #L75 - L76 were not covered by tests
self.function.function_space().mesh()
) # get the mesh from the function
rthetaz = f.SpatialCoordinate(mesh) # get the coordinates from the mesh
self.r = rthetaz[0] # only care about r here

Check warning on line 80 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L79-L80

Added lines #L79 - L80 were not covered by tests

# dS_z = r dr dtheta , assuming axisymmetry dS_z = theta r dr
# dS_r = r dz dtheta , assuming axisymmetry dS_r = theta r dz
# in both cases the expression with self.dx is the same

avg_surf = f.assemble(

Check warning on line 86 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L86

Added line #L86 was not covered by tests
self.function * self.r * self.ds(self.surface)
) / f.assemble(1 * self.r * self.ds(self.surface))

return avg_surf

Check warning on line 90 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L90

Added line #L90 was not covered by tests


class AverageSurfaceSpherical(AverageSurface):
RemDelaporteMathurin marked this conversation as resolved.
Show resolved Hide resolved
"""
Computes the average value of a field in a given volume
int(f ds) / int (1 * ds)
ds is the surface measure in cylindrical coordinates.
ds = r^2 sin(theta) dtheta dphi

Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
surface (int): the surface id
title (str): the title of the derived quantity
show_units (bool): show the units in the title in the derived quantities
file
function (dolfin.function.function.Function): the solution function of
the field

Notes:
Units are in H/m3 for hydrogen concentration and K for temperature
"""

def __init__(self, field, surface) -> None:
super().__init__(field=field, surface=surface)
self.r = None

Check warning on line 115 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L114-L115

Added lines #L114 - L115 were not covered by tests

def compute(self):

if self.r is None:
mesh = (

Check warning on line 120 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L119-L120

Added lines #L119 - L120 were not covered by tests
self.function.function_space().mesh()
) # get the mesh from the function
rthetaz = f.SpatialCoordinate(mesh) # get the coordinates from the mesh
self.r = rthetaz[0] # only care about r here

Check warning on line 124 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L123-L124

Added lines #L123 - L124 were not covered by tests

# dV_z = r dr dtheta , assuming axisymmetry dV_z = theta r dr
# dV_r = r dz dtheta , assuming axisymmetry dV_r = theta r dz
# in both cases the expression with self.dx is the same
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not true for spherical, please adapt


avg_surf = f.assemble(

Check warning on line 130 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L130

Added line #L130 was not covered by tests
self.function * self.r**2 * self.ds(self.surface)
) / f.assemble(1 * self.r**2 * self.ds(self.surface))

return avg_surf

Check warning on line 134 in festim/exports/derived_quantities/average_surface.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_surface.py#L134

Added line #L134 was not covered by tests
79 changes: 79 additions & 0 deletions festim/exports/derived_quantities/average_volume.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from festim import VolumeQuantity
import fenics as f
import numpy as np


class AverageVolume(VolumeQuantity):
Expand Down Expand Up @@ -42,3 +43,81 @@
return f.assemble(self.function * self.dx(self.volume)) / f.assemble(
1 * self.dx(self.volume)
)


class AverageVolumeCylindrical(AverageVolume):
"""
Computes the average value of a field in a given volume
int(f dx) / int (1 * dx)
dx is the volume measure in cylindrical coordinates.
dx = r dr dz dtheta

Note: for particle fluxes J is given in H/s, for heat fluxes J is given in W

Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
volume (int): the volume id
title (str): the title of the derived quantity
show_units (bool): show the units in the title in the derived quantities
file
function (dolfin.function.function.Function): the solution function of
the field
"""

def __init__(self, field, volume) -> None:
super().__init__(field=field, volume=volume)
self.r = None

Check warning on line 69 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L68-L69

Added lines #L68 - L69 were not covered by tests

def compute(self):

if self.r is None:
mesh = (

Check warning on line 74 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L73-L74

Added lines #L73 - L74 were not covered by tests
self.function.function_space().mesh()
) # get the mesh from the function
rthetaz = f.SpatialCoordinate(mesh) # get the coordinates from the mesh
self.r = rthetaz[0] # only care about r here

Check warning on line 78 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L77-L78

Added lines #L77 - L78 were not covered by tests

avg_vol = f.assemble(

Check warning on line 80 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L80

Added line #L80 was not covered by tests
self.function * self.r * self.dx(self.volume)
) / f.assemble(1 * self.r * self.dx(self.volume))

return avg_vol

Check warning on line 84 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L84

Added line #L84 was not covered by tests


class AverageVolumeSpherical(AverageVolume):
"""
Computes the average value of a field in a given volume
int(f dx) / int (1 * dx)
dx is the volume measure in cylindrical coordinates.
dx = rho dtheta dphi

Note: for particle fluxes J is given in H/s, for heat fluxes J is given in W

Args:
field (str, int): the field ("solute", 0, 1, "T", "retention")
volume (int): the volume id
title (str): the title of the derived quantity
show_units (bool): show the units in the title in the derived quantities
file
function (dolfin.function.function.Function): the solution function of
the field
"""

def __init__(self, field, volume) -> None:
super().__init__(field=field, volume=volume)
self.r = None

Check warning on line 108 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L107-L108

Added lines #L107 - L108 were not covered by tests

def compute(self):

if self.r is None:
mesh = (

Check warning on line 113 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L112-L113

Added lines #L112 - L113 were not covered by tests
self.function.function_space().mesh()
) # get the mesh from the function
rthetaphi = f.SpatialCoordinate(mesh) # get the coordinates from the mesh
self.r = rthetaphi[0] # only care about r here

Check warning on line 117 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L116-L117

Added lines #L116 - L117 were not covered by tests

avg_vol = f.assemble(

Check warning on line 119 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L119

Added line #L119 was not covered by tests
self.function * self.r**2 * self.dx(self.volume)
) / f.assemble(1 * self.r**2 * self.dx(self.volume))

return avg_vol

Check warning on line 123 in festim/exports/derived_quantities/average_volume.py

View check run for this annotation

Codecov / codecov/patch

festim/exports/derived_quantities/average_volume.py#L123

Added line #L123 was not covered by tests
Loading