Skip to content

Checkpointing#952

Merged
RemDelaporteMathurin merged 18 commits into
festim-dev:fenicsxfrom
RemDelaporteMathurin:checkpoint
Mar 4, 2025
Merged

Checkpointing#952
RemDelaporteMathurin merged 18 commits into
festim-dev:fenicsxfrom
RemDelaporteMathurin:checkpoint

Conversation

@RemDelaporteMathurin
Copy link
Copy Markdown
Collaborator

@RemDelaporteMathurin RemDelaporteMathurin commented Feb 26, 2025

Proposed changes

Contributes to #946 for the mono-material case. Checkpointing (ie. reading a function from a file) is implemented by leveraging adios4dolfinx (thanks @jorgensd for this!).

As it doesn't support submeshes for now, this only works for the non-mixed domain approach in the HydrogenTransportProblem class

Usage:

import festim as F

import dolfinx
import adios4dolfinx
import mpi4py.MPI as MPI

# build initial condition
mesh = dolfinx.mesh.create_unit_square(
    MPI.COMM_WORLD, nx=10, ny=10, cell_type=dolfinx.cpp.mesh.CellType.quadrilateral
)
el = "P"
degree = 1
V = dolfinx.fem.functionspace(mesh, (el, degree))


def f(x):
    return x[1] ** 2 + 2 * x[0] ** 2


u_ref = dolfinx.fem.Function(V)
u_ref.interpolate(f)
filename = "initial_condition.bp"

adios4dolfinx.write_mesh(filename, mesh)
adios4dolfinx.write_function(filename, u_ref, name="my_function", time=0.2)

# create problem
my_problem = F.HydrogenTransportProblem()
H = F.Species("H")
my_problem.species = [H]
my_problem.mesh = F.Mesh(mesh)
my_problem.initial_conditions = [
    F.InitialCondition(
        value=F.read_function_from_file(
            filename=filename, name="my_function", timestamp=0.2
        ),
        species=H,
    )
]
mat = F.Material(D_0=1, E_D=0.1, name="dummy_mat")
my_problem.subdomains = [F.VolumeSubdomain(id=0, material=mat)]

my_problem.temperature = 300

my_problem.exports = [
    F.VTXSpeciesExport(
        filename="H.bp",
        field=H,
        subdomain=my_problem.subdomains[0],
        checkpoint=False,
    ),
    F.VTXSpeciesExport(
        filename="H_for_checkpoint.bp",
        field=H,
        subdomain=my_problem.subdomains[0],
        checkpoint=True,
    ),
]

my_problem.settings = F.Settings(atol=1e-10, rtol=1e-10, final_time=100)
my_problem.settings.stepsize = F.Stepsize(1)
my_problem.initialise()
my_problem.run()

Types of changes

What types of changes does your code introduce to FESTIM?

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Code refactoring
  • Documentation Update (if none of the other choices apply)
  • New tests

Checklist

  • Black formatted
  • Unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works
  • I have added necessary documentation (if appropriate)

@RemDelaporteMathurin RemDelaporteMathurin added the fenicsx Issue that is related to the fenicsx support label Feb 26, 2025
@codecov
Copy link
Copy Markdown

codecov Bot commented Feb 26, 2025

Codecov Report

Attention: Patch coverage is 92.30769% with 3 lines in your changes missing coverage. Please review.

Project coverage is 96.08%. Comparing base (d061987) to head (67ae917).
Report is 248 commits behind head on fenicsx.

Files with missing lines Patch % Lines
src/festim/hydrogen_transport_problem.py 90.00% 2 Missing ⚠️
src/festim/initial_condition.py 92.85% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           fenicsx     #952      +/-   ##
===========================================
- Coverage    96.14%   96.08%   -0.07%     
===========================================
  Files           46       46              
  Lines         2567     2604      +37     
===========================================
+ Hits          2468     2502      +34     
- Misses          99      102       +3     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@RemDelaporteMathurin
Copy link
Copy Markdown
Collaborator Author

@jhdark just updated the branch to pass with nightly, ready for review

Copy link
Copy Markdown
Collaborator

@jhdark jhdark left a comment

Choose a reason for hiding this comment

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

Looks like a great start, could do with some more docs.

But also is it worth making a base class InitialValueFromFile, then adding another class for reading a temperature field from a file? Doesn't look like it would be too difficult to add, but would obviously increase the size of the PR.

Plus, we could consider either some checks to make sure the mesh in the file is the same as the one defined in the problem, or could maybe add some functionality to interpolate between meshes?

Comment thread src/festim/hydrogen_transport_problem.py Outdated
Comment thread src/festim/initial_condition.py Outdated
Comment thread src/festim/initial_condition.py Outdated
Comment thread test/system_tests/test_io.py Outdated
Copy link
Copy Markdown
Collaborator

@jhdark jhdark left a comment

Choose a reason for hiding this comment

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

InitialCondition should support a fem.Function as value and then we can just bundle this in a one-liner. comm could also be an argument for full flexibility

import festim as F


def read_function_from_file(filename, name, timestamp, family="P", order=1):
    import dolfinx.fem as fem
    import mpi4py.MPI as MPI
    import adios4dolfinx

    mesh_in = adios4dolfinx.read_mesh(filename, MPI.COMM_WORLD)
    V_in = fem.functionspace(mesh_in, (family, order))
    u_in = fem.Function(V_in)
    adios4dolfinx.read_function(
        filename=filename,
        u=u_in,
        name=name,
        time=timestamp,
    )
    return u_in


my_model = F.HydrogenTransportProblem()

H = F.Species("H")
my_model.species = [H]

my_model.initial_conditions = [
    F.InitialCondition(
        value=read_function_from_file("filename.bp", "name in file", 100), species=H
    )
]

Copy link
Copy Markdown
Collaborator Author

@RemDelaporteMathurin RemDelaporteMathurin left a comment

Choose a reason for hiding this comment

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

@jhdark I modified the PR according to our discussion and added some more documentation

Comment thread src/festim/hydrogen_transport_problem.py Outdated
Comment thread test/system_tests/test_io.py Outdated
Comment thread test/system_tests/test_io.py Outdated
Comment thread src/festim/initial_condition.py Outdated
Comment thread src/festim/initial_condition.py Outdated
Comment thread src/festim/hydrogen_transport_problem.py Outdated
@RemDelaporteMathurin RemDelaporteMathurin added the enhancement New feature or request label Mar 3, 2025
Comment thread src/festim/__init__.py Outdated
Comment thread src/festim/initial_condition.py
Comment thread src/festim/initial_condition.py Outdated
Comment thread test/system_tests/test_io.py Outdated
Comment thread test/system_tests/test_io.py Outdated
RemDelaporteMathurin and others added 5 commits March 4, 2025 09:34
Co-authored-by: James Dark <65899899+jhdark@users.noreply.github.com>
Co-authored-by: James Dark <65899899+jhdark@users.noreply.github.com>
@RemDelaporteMathurin RemDelaporteMathurin merged commit 939540b into festim-dev:fenicsx Mar 4, 2025
@RemDelaporteMathurin RemDelaporteMathurin deleted the checkpoint branch March 4, 2025 17:38
This was referenced Mar 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request fenicsx Issue that is related to the fenicsx support

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants