Skip to content

Commit

Permalink
addtional argument for convert value
Browse files Browse the repository at this point in the history
  • Loading branch information
jhdark committed Jan 27, 2025
1 parent ac60724 commit 76ab530
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 60 deletions.
39 changes: 13 additions & 26 deletions src/festim/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,6 @@
import ufl


# def as_fenics_constant(
# value: float | int | fem.Constant, mesh: dolfinx.mesh.Mesh
# ) -> fem.Constant:
# """Converts a value to a dolfinx.Constant

# Args:
# value: the value to convert
# mesh: the mesh of the domiain

# Returns:
# The converted value

# Raises:
# TypeError: if the value is not a float, an int or a dolfinx.Constant
# """
# if isinstance(value, (float, int)):
# return fem.Constant(mesh, dolfinx.default_scalar_type(value))
# elif isinstance(value, fem.Constant):
# return value
# else:
# raise TypeError(
# f"Value must be a float, an int or a dolfinx.Constant, not {type(value)}"
# )


def as_fenics_constant(
value: float | int | fem.Constant, mesh: dolfinx.mesh.Mesh
) -> fem.Constant:
Expand Down Expand Up @@ -211,7 +186,12 @@ def temperature_dependent(self) -> bool:
return False

def convert_input_value(
self, mesh=None, function_space=None, t=None, temperature=None
self,
mesh=None,
function_space=None,
t=None,
temperature=None,
up_to_mapping=False,
):
"""Converts a user given value to a relevent fenics object depending
on the type of the value provided
Expand All @@ -221,6 +201,8 @@ def convert_input_value(
function_space (dolfinx.fem.function.FunctionSpace): the function space of the fenics object
t (fem.Constant): the time
temperature (fem.Function, fem.Constant or ufl.core.expr.Expr): the temperature
up_to_mapping (bool): if True, the value is only mapped to a function if the input is callable,
not interpolated or converted to a function
"""
if isinstance(self.input_value, fem.Constant):
self.fenics_object = self.input_value
Expand Down Expand Up @@ -248,6 +230,11 @@ def convert_input_value(
value=self.input_value(t=float(t)), mesh=mesh
)

elif up_to_mapping:
self.fenics_object = as_mapped_function(
value=self.input_value, mesh=mesh, t=t, temperature=temperature
)

elif self.temperature_dependent:
self.fenics_interpolation_expression, self.fenics_object = (
as_fenics_interp_expr_and_function(
Expand Down
47 changes: 13 additions & 34 deletions src/festim/hydrogen_transport_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -493,23 +493,13 @@ def define_boundary_conditions(self):
# if name of species is given then replace with species object
bc.species = _species.find_species_from_name(bc.species, self.species)
if isinstance(bc, boundary_conditions.ParticleFluxBC):
if isinstance(
bc.value.input_value,
(int, float, fem.Constant, fem.Function, ufl.core.expr.Expr),
):
bc.value.convert_input_value(
mesh=self.mesh.mesh,
t=self.t,
temperature=self.temperature.fenics_object,
)

else:
bc.value.fenics_object = festim.as_mapped_function(
value=bc.value.input_value,
mesh=self.mesh.mesh,
temperature=self.temperature.fenics_object,
t=self.t,
)
bc.value.convert_input_value(
mesh=self.mesh.mesh,
t=self.t,
temperature=self.temperature.fenics_object,
up_to_mapping=True,
)

super().define_boundary_conditions()

Expand Down Expand Up @@ -571,34 +561,23 @@ def create_source_values_fenics(self):
"""For each source create the value_fenics"""
for source in self.sources:
# create value_fenics for all F.ParticleSource objects
if isinstance(source, festim.source.ParticleSource):
if isinstance(
source.value.input_value,
(int, float, fem.Constant, fem.Function, ufl.core.expr.Expr),
):
source.value.convert_input_value(
mesh=self.mesh.mesh,
t=self.t,
temperature=self.temperature.fenics_object,
)
elif callable(source.value.input_value):
source.value.fenics_object = festim.as_mapped_function(
value=source.value.input_value,
mesh=self.mesh.mesh,
t=self.t,
temperature=self.temperature.fenics_object,
)
source.value.convert_input_value(
mesh=self.mesh.mesh,
t=self.t,
temperature=self.temperature.fenics_object,
up_to_mapping=True,
)

def create_flux_values_fenics(self):
"""For each particle flux create the value_fenics"""
for bc in self.boundary_conditions:
# create value_fenics for all F.ParticleFluxBC objects
if isinstance(bc, boundary_conditions.ParticleFluxBC):

bc.value.convert_input_value(
mesh=self.mesh.mesh,
temperature=self.temperature.fenics_object,
t=self.t,
up_to_mapping=True,
)

def create_initial_conditions(self):
Expand Down

0 comments on commit 76ab530

Please sign in to comment.