Skip to content

Commit 5fb83bc

Browse files
refactoring
1 parent 80f58be commit 5fb83bc

1 file changed

Lines changed: 25 additions & 15 deletions

File tree

src/festim/exports/vtx.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -357,10 +357,31 @@ def expression(T, **kwargs):
357357

358358
return k * ufl.product(_reactant_names) - p * ufl.product(_product_names)
359359

360-
# generate the __signature__ of the expression function so that it has the
361-
# correct arguments for the user-provided expression.
362-
# This is needed as set_dolfinx_expression() checks
363-
# the arguments of expression and it would otherwise look for kwargs
360+
self.override_signature(expression, reactant_names, product_names)
361+
362+
super().__init__(
363+
filename=filename,
364+
expression=expression,
365+
species_dependent_value={
366+
spe.name: spe for spe in reaction.reactant + reaction.product
367+
},
368+
times=times,
369+
subdomain=subdomain,
370+
checkpoint=checkpoint,
371+
)
372+
373+
def override_signature(
374+
self, expression: Callable, reactant_names: list[str], product_names: list[str]
375+
):
376+
"""
377+
Override the signature of the expression function. This is needed to ensure that
378+
the expression has the correct arguments for set_dolfinx_expression().
379+
380+
Args:
381+
expression: The user-provided expression for the reaction rate. The arguments
382+
of the expression must be T (temperature) and the names of the reactants
383+
and products.
384+
"""
364385
sig_params = [inspect.Parameter("T", inspect.Parameter.POSITIONAL_OR_KEYWORD)]
365386
# Use dict.fromkeys to preserve order and remove duplicates
366387
for name in dict.fromkeys(reactant_names + product_names):
@@ -380,14 +401,3 @@ def expression(T, **kwargs):
380401
f"{inspect.signature(expression).parameters.keys()} but should have arguments "
381402
f"T and {reactant_names + product_names}."
382403
)
383-
384-
super().__init__(
385-
filename=filename,
386-
expression=expression,
387-
species_dependent_value={
388-
spe.name: spe for spe in reaction.reactant + reaction.product
389-
},
390-
times=times,
391-
subdomain=subdomain,
392-
checkpoint=checkpoint,
393-
)

0 commit comments

Comments
 (0)