Skip to content

Commit

Permalink
Refactor assertions in phase field tests for improved accuracy and re…
Browse files Browse the repository at this point in the history
…adability
  • Loading branch information
CastillonMiguel committed Jan 9, 2025
1 parent 5659076 commit 587bc4d
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 11 deletions.
3 changes: 2 additions & 1 deletion test/Element/Allen_Cahn/test_allen_cahn_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ def right(x): return np.isclose(x[0], a)

# Compare the computed total energy with the theoretical energy
energy_simulation = S.energy_files["total.energy"]["gamma"][0]
assert np.isclose(energy_simulation, energy_theory, atol=1e-3), f"Simulated energy {energy_simulation} does not match theoretical energy {energy_theory}"

np.testing.assert_allclose(energy_simulation, energy_theory, rtol=1e-3, atol=1e-3, err_msg=f"Simulated energy {energy_simulation} does not match theoretical energy {energy_theory}")

# Clean up: remove the generated files
if os.path.exists(Data.results_folder_name):
Expand Down
14 changes: 8 additions & 6 deletions test/Element/Phase_Field/test_phase_field_element.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,16 @@ def left(x):
W_gradphi = 0.5 * tanh_a_div_l - 0.5 * a_div_l * (1.0 - tanh_a_div_l**2)
W = np.tanh(a_div_l)

assert np.isclose(S.energy_files["total.energy"]["gamma_phi"][0], 0.5 * W_phi, atol=1e-6), \
f"Computed phi energy {S.energy_files["total.energy"]["gamma_phi"][0]} does not match theoretical energy {W_phi}"

np.testing.assert_allclose(S.energy_files["total.energy"]["gamma_phi"][0], 0.5 * W_phi, rtol=1e-3, atol=1e-6,
err_msg=f"Computed phi energy {S.energy_files['total.energy']['gamma_phi'][0]} does not match theoretical energy {W_phi}")

assert np.isclose(S.energy_files["total.energy"]["gamma_gradphi"][0], 0.5 * W_gradphi, atol=1e-6), \
f"Computed gradphi energy {S.energy_files["total.energy"]["gamma_gradphi"][0]} does not match theoretical energy {W_gradphi}"
np.testing.assert_allclose(S.energy_files["total.energy"]["gamma_gradphi"][0], 0.5 * W_gradphi, rtol=1e-3, atol=1e-6,
err_msg=f"Computed gradphi energy {S.energy_files['total.energy']['gamma_gradphi'][0]} does not match theoretical energy {W_gradphi}")

np.testing.assert_allclose(S.energy_files["total.energy"]["gamma"][0], 0.5 * W, rtol=1e-3, atol=1e-6,
err_msg=f"Computed energy {S.energy_files['total.energy']['gamma'][0]} does not match theoretical energy {W}")

assert np.isclose(S.energy_files["total.energy"]["gamma"][0], 0.5 * W, atol=1e-6), \
f"Computed energy {S.energy_files["total.energy"]["gamma"][0]} does not match theoretical energy {W}"

# Clean up: Remove generated files
if os.path.exists(Data.results_folder_name):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,9 @@ def update_boundary_conditions(bcs, time):
phi_t = 2 * psi_t / (Data.Gc / Data.l + 2 * psi_t)
sigma_t = displacement * (Data.lambda_ + 2 * Data.mu) * (1 - phi_t)**2

assert np.allclose(S.reaction_files["top.reaction"]["Ry"], sigma_t, atol=1e-8), \
f"Computed reaction force {S.reaction_files["top.reaction"]["Ry"]} does not match theoretical reaction {sigma_t}"
np.testing.assert_allclose(S.reaction_files["top.reaction"]["Ry"], sigma_t, rtol=1e-3, atol=1e-8,
err_msg=f"Computed reaction force {S.reaction_files['top.reaction']['Ry']} does not match theoretical reaction {sigma_t}")

# Clean up: Remove generated files
if os.path.exists(Data.results_folder_name):
import shutil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,8 @@ def update_boundary_conditions(bcs, time):
else:
raise ValueError(f"Invalid value for split_energy: {Data.split_energy}. Expected 'spectral' or 'deviatoric'.")

assert np.allclose(S.reaction_files["top.reaction"]["Ry"], sigma_t, atol=1e-8), \
f"Computed reaction force {S.reaction_files["top.reaction"]["Ry"]} does not match theoretical reaction {sigma_t} for {Data.split_energy}"
np.testing.assert_allclose(S.reaction_files["top.reaction"]["Ry"], sigma_t, rtol=1e-3, atol=1e-8,
err_msg=f"Computed reaction force {S.reaction_files['top.reaction']['Ry']} does not match theoretical reaction {sigma_t} for {Data.split_energy}")

# Clean up: Remove generated files
if os.path.exists(Data.results_folder_name):
Expand Down

0 comments on commit 587bc4d

Please sign in to comment.