From beef3b49253266bbfddadb88ba7a942e9cbab7a2 Mon Sep 17 00:00:00 2001 From: Jorgen Dokken Date: Tue, 19 Nov 2024 10:13:02 +0000 Subject: [PATCH 1/6] Use newest newton solver --- src/festim/hydrogen_transport_problem.py | 22 +++++++++++++++------- src/festim/settings.py | 7 ++++++- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/festim/hydrogen_transport_problem.py b/src/festim/hydrogen_transport_problem.py index 1bba9c7b6..74328c3dd 100644 --- a/src/festim/hydrogen_transport_problem.py +++ b/src/festim/hydrogen_transport_problem.py @@ -8,7 +8,7 @@ import tqdm.autonotebook import ufl from dolfinx import fem -from scifem import NewtonSolver +from scifem import BlockedNewtonSolver import festim.boundary_conditions import festim.problem @@ -1209,18 +1209,26 @@ def mixed_term(u, v, n): self.forms = dolfinx.fem.form( [subdomain.F for subdomain in self.volume_subdomains], entity_maps=entity_maps, + jit_options = {"cffi_extra_compile_args": ["-O3", "-march=native"], + "cffi_libraries": ["m"]} ) - self.J = dolfinx.fem.form(J, entity_maps=entity_maps) + self.J = dolfinx.fem.form(J, entity_maps=entity_maps, + jit_options = {"cffi_extra_compile_args": ["-O3", "-march=native"], + "cffi_libraries": ["m"]} + ) def create_solver(self): - self.solver = NewtonSolver( + self.solver = BlockedNewtonSolver( self.forms, - self.J, [subdomain.u for subdomain in self.volume_subdomains], + J = self.J, bcs=self.bc_forms, - max_iterations=self.settings.max_iterations, petsc_options=self.petsc_options, ) + self.solver.max_iterations=self.settings.max_iterations, + self.solver.convergence_criterion = self.settings.convergence_criterion + self.solver.atol = self.settings.atol + self.solver.rtol = self.settings.rtol def create_flux_values_fenics(self): """For each particle flux create the ``value_fenics`` attribute""" @@ -1279,8 +1287,8 @@ def iterate(self): self.update_time_dependent_values() - # solve main problem - self.solver.solve(self.settings.atol, self.settings.rtol) + # Solve main problem + self.solver.solve() # post processing self.post_processing() diff --git a/src/festim/settings.py b/src/festim/settings.py index 1cd3bdd56..1eaeba530 100644 --- a/src/festim/settings.py +++ b/src/festim/settings.py @@ -1,5 +1,5 @@ import festim as F - +from typing import Literal class Settings: """Settings for a festim simulation. @@ -14,6 +14,7 @@ class Settings: Defaults to None stepsize (festim.Stepsize, optional): stepsize for a transient simulation. Defaults to None + convergence_criterion: resiudal or incremental (for Newton solver) Attributes: atol (float): Absolute tolerance for the solver. @@ -23,6 +24,8 @@ class Settings: final_time (float): Final time for a transient simulation. stepsize (festim.Stepsize): stepsize for a transient simulation. + convergence_criterion: resiudal or incremental (for Newton solver) + """ def __init__( @@ -33,6 +36,7 @@ def __init__( transient=True, final_time=None, stepsize=None, + convergence_criterion:Literal["residual", "incremental"] = "residual" ) -> None: self.atol = atol self.rtol = rtol @@ -40,6 +44,7 @@ def __init__( self.transient = transient self.final_time = final_time self.stepsize = stepsize + self.convergence_criterion = convergence_criterion @property def stepsize(self): From 5bf396e053fc08ad43703a36801d346bf1706a3d Mon Sep 17 00:00:00 2001 From: Jorgen Dokken Date: Tue, 19 Nov 2024 10:13:29 +0000 Subject: [PATCH 2/6] Bump scifem release --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 64f5d7d16..d40b71b2e 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,7 +13,7 @@ description = "Finite element simulations of hydrogen transport" readme = "README/md" requires-python = ">=3.9" license = { file = "LICENSE" } -dependencies = ["tqdm", "scifem>=0.2.8"] +dependencies = ["tqdm", "scifem>=0.2.13"] classifiers = [ "Natural Language :: English", "Topic :: Scientific/Engineering", From e7c7876898695c0bde8302505e493d3c010983dc Mon Sep 17 00:00:00 2001 From: Jorgen Dokken Date: Tue, 19 Nov 2024 10:26:06 +0000 Subject: [PATCH 3/6] Remove rtol from steady state --- src/festim/hydrogen_transport_problem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/festim/hydrogen_transport_problem.py b/src/festim/hydrogen_transport_problem.py index 74328c3dd..137fb79c8 100644 --- a/src/festim/hydrogen_transport_problem.py +++ b/src/festim/hydrogen_transport_problem.py @@ -1316,7 +1316,7 @@ def run(self): self.progress_bar.refresh() # refresh progress bar to show 100% else: # Solve steady-state - self.solver.solve(self.settings.rtol) + self.solver.solve() self.post_processing() def __del__(self): From 472eaa52d843478d140e1f744ad910017302ccd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20S=2E=20Dokken?= Date: Tue, 19 Nov 2024 11:29:46 +0100 Subject: [PATCH 4/6] ruff formatting --- src/festim/hydrogen_transport_problem.py | 22 ++++++++++++++-------- src/festim/settings.py | 6 ++++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/festim/hydrogen_transport_problem.py b/src/festim/hydrogen_transport_problem.py index 137fb79c8..f348d7d17 100644 --- a/src/festim/hydrogen_transport_problem.py +++ b/src/festim/hydrogen_transport_problem.py @@ -1209,23 +1209,29 @@ def mixed_term(u, v, n): self.forms = dolfinx.fem.form( [subdomain.F for subdomain in self.volume_subdomains], entity_maps=entity_maps, - jit_options = {"cffi_extra_compile_args": ["-O3", "-march=native"], - "cffi_libraries": ["m"]} + jit_options={ + "cffi_extra_compile_args": ["-O3", "-march=native"], + "cffi_libraries": ["m"], + }, + ) + self.J = dolfinx.fem.form( + J, + entity_maps=entity_maps, + jit_options={ + "cffi_extra_compile_args": ["-O3", "-march=native"], + "cffi_libraries": ["m"], + }, ) - self.J = dolfinx.fem.form(J, entity_maps=entity_maps, - jit_options = {"cffi_extra_compile_args": ["-O3", "-march=native"], - "cffi_libraries": ["m"]} - ) def create_solver(self): self.solver = BlockedNewtonSolver( self.forms, [subdomain.u for subdomain in self.volume_subdomains], - J = self.J, + J=self.J, bcs=self.bc_forms, petsc_options=self.petsc_options, ) - self.solver.max_iterations=self.settings.max_iterations, + self.solver.max_iterations = (self.settings.max_iterations,) self.solver.convergence_criterion = self.settings.convergence_criterion self.solver.atol = self.settings.atol self.solver.rtol = self.settings.rtol diff --git a/src/festim/settings.py b/src/festim/settings.py index 1eaeba530..d8d447bbc 100644 --- a/src/festim/settings.py +++ b/src/festim/settings.py @@ -1,6 +1,8 @@ -import festim as F from typing import Literal +import festim as F + + class Settings: """Settings for a festim simulation. @@ -36,7 +38,7 @@ def __init__( transient=True, final_time=None, stepsize=None, - convergence_criterion:Literal["residual", "incremental"] = "residual" + convergence_criterion: Literal["residual", "incremental"] = "residual", ) -> None: self.atol = atol self.rtol = rtol From 54063ba06f43a3cd9e7198226977348e1ab2c1c8 Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Tue, 19 Nov 2024 08:34:53 -0500 Subject: [PATCH 5/6] fixed atol=None --- test/system_tests/test_multi_material.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/system_tests/test_multi_material.py b/test/system_tests/test_multi_material.py index 7d2720d09..07ac562a8 100644 --- a/test/system_tests/test_multi_material.py +++ b/test/system_tests/test_multi_material.py @@ -142,7 +142,7 @@ def c_exact_bot_np(x): my_model.temperature = 500.0 # lambda x: 300 + 10 * x[1] + 100 * x[0] - my_model.settings = F.Settings(atol=None, rtol=1e-5, transient=False) + my_model.settings = F.Settings(atol=1e-5, rtol=1e-5, transient=False) my_model.exports = [ F.VTXSpeciesExport(f"u_{subdomain.id}.bp", field=H, subdomain=subdomain) for subdomain in my_model.volume_subdomains @@ -216,7 +216,7 @@ def test_1_material_discontinuous_version(): my_model.temperature = lambda x: 300 + 100 * x[0] - my_model.settings = F.Settings(atol=None, rtol=1e-5, transient=False) + my_model.settings = F.Settings(atol=1e-5, rtol=1e-5, transient=False) my_model.exports = [ F.VTXSpeciesExport( @@ -384,7 +384,7 @@ def test_2_mats_particle_flux_bc(): my_model.temperature = lambda x: 300 + 10 * x[1] + 100 * x[0] - my_model.settings = F.Settings(atol=None, rtol=1e-5, transient=False) + my_model.settings = F.Settings(atol=1e-5, rtol=1e-5, transient=False) my_model.exports = [ F.VTXSpeciesExport(f"u_{subdomain.id}.bp", field=H, subdomain=subdomain) From 482d920cf7e13b3c2486fed8a09bf7f7c5db6091 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B8rgen=20Schartum=20Dokken?= Date: Tue, 19 Nov 2024 14:47:32 +0100 Subject: [PATCH 6/6] Apply suggestions from code review --- src/festim/hydrogen_transport_problem.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/festim/hydrogen_transport_problem.py b/src/festim/hydrogen_transport_problem.py index f348d7d17..04520a0f7 100644 --- a/src/festim/hydrogen_transport_problem.py +++ b/src/festim/hydrogen_transport_problem.py @@ -1231,7 +1231,7 @@ def create_solver(self): bcs=self.bc_forms, petsc_options=self.petsc_options, ) - self.solver.max_iterations = (self.settings.max_iterations,) + self.solver.max_iterations = self.settings.max_iterations self.solver.convergence_criterion = self.settings.convergence_criterion self.solver.atol = self.settings.atol self.solver.rtol = self.settings.rtol