Skip to content

Commit 3dff013

Browse files
Update .sub_T when temperature is time dependent (#1128)
* added test that catches the bug * comment/noet * fix
2 parents 35b86fe + 10f4794 commit 3dff013

2 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/festim/hydrogen_transport_problem.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,6 +1239,12 @@ def initialise(self):
12391239
def define_temperature(self):
12401240
super().define_temperature()
12411241

1242+
# NOTE this won't be needed anymore when https://github.com/FEniCS/dolfinx/pull/4140
1243+
# is released
1244+
1245+
# because dolfinx.fem.Expressions cannot work with submeshes
1246+
# (ie. mixing parent and submesh),
1247+
# we need to create "sub" temperature functions for each subdomain
12421248
# pass temperature function to each subdomain
12431249
if isinstance(self.temperature_fenics, fem.Function):
12441250
for subdomain in self.volume_subdomains:
@@ -1866,6 +1872,19 @@ def post_processing(self):
18661872
export.data.append(c)
18671873
export.t.append(float(self.t))
18681874

1875+
def update_time_dependent_values(self):
1876+
super().update_time_dependent_values()
1877+
1878+
# update sub_T if temperature is given as a function
1879+
if self.temperature_time_dependent:
1880+
if isinstance(self.temperature_fenics, fem.Function):
1881+
for subdomain in self.volume_subdomains:
1882+
temp = self.temperature_fenics
1883+
sub_T = subdomain.sub_T
1884+
from festim.helpers import nmm_interpolate
1885+
1886+
nmm_interpolate(f_out=sub_T, f_in=temp)
1887+
18691888
def iterate(self):
18701889
"""Iterates the model for a given time step."""
18711890
if self.show_progress_bar:

test/system_tests/test_misc.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from mpi4py import MPI
2+
13
import dolfinx
24
import numpy as np
35
import pytest
@@ -352,3 +354,38 @@ def test_timesteps():
352354

353355
expected_timesteps = np.linspace(0, 10, num=10, endpoint=False)
354356
assert np.allclose(my_model.timesteps, expected_timesteps)
357+
358+
359+
def test_sub_temperature_as_function_mixed_domain_not_updated():
360+
361+
my_model = F.HydrogenTransportProblemDiscontinuous()
362+
363+
n = 8
364+
mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, n, n)
365+
my_model.mesh = F.Mesh(mesh)
366+
367+
surface_1 = F.SurfaceSubdomain(id=1)
368+
369+
material_1 = F.Material(D_0=1, E_D=0, name="material_1")
370+
volume_1 = F.VolumeSubdomain(id=2, material=material_1)
371+
372+
my_model.subdomains = [surface_1, volume_1]
373+
374+
H = F.Species(name="H", subdomains=my_model.volume_subdomains)
375+
my_model.species = [H]
376+
377+
my_model.boundary_conditions = [
378+
F.FixedConcentrationBC(species=H, subdomain=surface_1, value=lambda T: 2 * T)
379+
]
380+
381+
my_model.temperature = lambda t, x: 1 + x[0] + x[1] + t
382+
383+
my_model.settings = F.Settings(final_time=1, atol=1e-9, rtol=1e-9, stepsize=0.1)
384+
385+
avg_surf = F.AverageSurface(field=H, surface=surface_1)
386+
my_model.exports = [avg_surf]
387+
388+
my_model.initialise()
389+
my_model.run()
390+
391+
assert not np.allclose(avg_surf.data, 4.0)

0 commit comments

Comments
 (0)