Skip to content

Commit 1ae4c44

Browse files
removed the need for override_solution
1 parent c6d16d7 commit 1ae4c44

3 files changed

Lines changed: 43 additions & 30 deletions

File tree

src/festim/hydrogen_transport_problem.py

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1467,9 +1467,6 @@ def create_subdomain_formulation(self, subdomain: _subdomain.VolumeSubdomain):
14671467
if reaction.volume != subdomain:
14681468
continue
14691469

1470-
# TODO remove
1471-
# temporarily overide the solution to the one of the subdomain
1472-
self.override_solution_attributes(reaction)
14731470
# reactant
14741471
for reactant in reaction.reactant:
14751472
if isinstance(reactant, _species.Species):
@@ -1592,27 +1589,6 @@ def create_formulation(self):
15921589
},
15931590
)
15941591

1595-
def override_solution_attributes(self, reaction: _reaction.Reaction):
1596-
"""
1597-
Reaction.reaction_term() relies on the .solution attribute of the species
1598-
however, in the discontinuous class, this attribute doesn't really make sense
1599-
since there is one solution per subdomain.
1600-
Therefore we temporarily override the .solution attribute based on the reactants,
1601-
products, and `others` if there are implicit species
1602-
"""
1603-
list_of_species_to_override = reaction.reactant + reaction.product
1604-
1605-
# check if we have implicit species:
1606-
for reactant in reaction.reactant:
1607-
if isinstance(reactant, _species.ImplicitSpecies):
1608-
for other_spe in reactant.others:
1609-
if other_spe not in list_of_species_to_override:
1610-
list_of_species_to_override.append(other_spe)
1611-
1612-
for species in list_of_species_to_override:
1613-
if isinstance(species, _species.Species):
1614-
species.solution = species.subdomain_to_solution[reaction.volume]
1615-
16161592
def create_solver(self):
16171593
if Version(dolfinx.__version__) == Version("0.9.0"):
16181594
self.solver = BlockedNewtonSolver(

src/festim/reaction.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,24 @@ def reaction_term(
134134
The reaction term to be used in a formulation.
135135
"""
136136

137+
# make sure products is a list
138+
products = self.product if isinstance(self.product, list) else [self.product]
139+
140+
# detect if mixed_domain
141+
mixed_domain = any(
142+
isinstance(reactant, _Species) and reactant.subdomain_to_solution != {}
143+
for reactant in self.reactant
144+
) or any(
145+
isinstance(product, _Species) and product.subdomain_to_solution != {}
146+
for product in products
147+
)
148+
149+
def get_concentration(species):
150+
if mixed_domain:
151+
return species.concentration_submesh(self.volume)
152+
else:
153+
return species.concentration
154+
137155
if self.product == []:
138156
if self.p_0 is not None:
139157
raise ValueError(
@@ -155,8 +173,6 @@ def reaction_term(
155173
"E_p cannot be None when reaction products are present."
156174
)
157175

158-
products = self.product if isinstance(self.product, list) else [self.product]
159-
160176
# reaction rates
161177
k = self.k_0 * exp(-self.E_k / (_k_B * temperature))
162178

@@ -173,18 +189,22 @@ def reaction_term(
173189
assert len(reactant_concentrations) == len(reactants)
174190
for i, reactant in enumerate(reactants):
175191
if reactant_concentrations[i] is None:
176-
reactant_concentrations[i] = reactant.concentration
192+
reactant_concentrations[i] = get_concentration(reactant)
177193
else:
178-
reactant_concentrations = [reactant.concentration for reactant in reactants]
194+
reactant_concentrations = [
195+
get_concentration(reactant) for reactant in reactants
196+
]
179197

180198
# if product_concentrations is provided, use these concentrations
181199
if product_concentrations is not None:
182200
assert len(product_concentrations) == len(products)
183201
for i, product in enumerate(products):
184202
if product_concentrations[i] is None:
185-
product_concentrations[i] = product.concentration
203+
product_concentrations[i] = get_concentration(product)
186204
else:
187-
product_concentrations = [product.concentration for product in products]
205+
product_concentrations = [
206+
get_concentration(product) for product in products
207+
]
188208

189209
# multiply all concentrations to be used in the term
190210
product_of_reactants = reactant_concentrations[0]

src/festim/species.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ def __str__(self) -> str:
111111
def concentration(self):
112112
return self.solution
113113

114+
def concentration_submesh(self, subdomain: _VolumeSubdomain):
115+
assert subdomain in self.subdomains, (
116+
f"Species {self.name} has no solution on subdomain {subdomain}."
117+
)
118+
return self.subdomain_to_solution[subdomain]
119+
114120
@property
115121
def legacy(self) -> bool:
116122
"""
@@ -174,6 +180,17 @@ def concentration(self):
174180
)
175181
return self.value_fenics - sum([other.solution for other in self.others])
176182

183+
def concentration_submesh(self, subdomain: _VolumeSubdomain):
184+
if len(self.others) > 0:
185+
for other in self.others:
186+
assert other.subdomain_to_solution[subdomain], (
187+
f"Cannot compute concentration of {self.name} because {other.name}"
188+
+ f" has no solution on subdomain {subdomain}."
189+
)
190+
return self.value_fenics - sum(
191+
[other.subdomain_to_solution[subdomain] for other in self.others]
192+
)
193+
177194
def create_value_fenics(self, mesh, t: fem.Constant):
178195
"""Creates the value of the density as a fenics object and sets it to
179196
self.value_fenics.

0 commit comments

Comments
 (0)