Skip to content

Commit

Permalink
added test for iterate
Browse files Browse the repository at this point in the history
  • Loading branch information
RemDelaporteMathurin committed Oct 31, 2023
1 parent 03d4d08 commit a9fa55b
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions test/test_h_transport_problem.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import festim as F
import tqdm.autonotebook
import mpi4py.MPI as MPI
import dolfinx
import ufl
import numpy as np

# TODO test all the methods in the class


def test_iterate():
"""Test that the iterate method updates the solution and time correctly"""
# BUILD
my_model = F.HydrogenTransportProblem()

my_model.settings = F.Settings(atol=1e-6, rtol=1e-6, final_time=10)

my_model.progress = tqdm.autonotebook.tqdm(
desc="Solving H transport problem",
total=my_model.settings.final_time,
unit_scale=True,
)

my_model.boundary_conditions = []

mesh = dolfinx.mesh.create_unit_square(MPI.COMM_WORLD, 10, 10)

V = dolfinx.fem.FunctionSpace(mesh, ("CG", 1))
my_model.u = dolfinx.fem.Function(V)
my_model.u_n = dolfinx.fem.Function(V)
my_model.dt = dolfinx.fem.Constant(mesh, 2.0)
v = ufl.TestFunction(V)

source_value = 2.0
form = (
my_model.u - my_model.u_n
) / my_model.dt * v * ufl.dx - source_value * v * ufl.dx

problem = dolfinx.fem.petsc.NonlinearProblem(form, my_model.u, bcs=[])
my_model.solver = dolfinx.nls.petsc.NewtonSolver(MPI.COMM_WORLD, problem)

my_model.t = dolfinx.fem.Constant(mesh, 0.0)

for i in range(10):
# RUN
my_model.iterate(skip_post_processing=True)

# TEST

# check that t evolves
expected_t_value = (i + 1) * float(my_model.dt)
assert np.isclose(float(my_model.t), expected_t_value)

# check that u and u_n are updated
expected_u_value = (i + 1) * float(my_model.dt) * source_value
assert np.all(np.isclose(my_model.u.x.array, expected_u_value))

assert np.all(np.isclose(my_model.u_n.x.array, expected_u_value))

0 comments on commit a9fa55b

Please sign in to comment.