Skip to content

Add non-Newtonian (modified Cross) viscosity model for the fluid - #115

Open
simonecap00 wants to merge 9 commits into
masterfrom
Non-Newtonian-implementation
Open

simonecap00 wants to merge 9 commits into
masterfrom
Non-Newtonian-implementation

Conversation

@simonecap00

@simonecap00 simonecap00 commented Oct 23, 2026 •

Copy link
Copy Markdown

Summary

This PR adds a non-Newtonian fluid model (modified Cross model) to turtleFSI. The default behaviour is unchanged: the model is only active when nonnewtonian=True is set in the problem file, globally or per fluid region.

Changes

  • turtleFSI/modules/nonnewtonian.py (new):
    • computes the shear rate in the ALE frame and evaluates the modified Cross model;
    • get_fluid_rheology reads the rheology of each fluid region: modified Cross with its own parameters, or Newtonian;
    • EffectiveViscosity: a single DG1 field mu_eff shared by all fluid regions. mu_eff.update() updates it explicitly with a second-order extrapolation in time (3/2·μⁿ⁻¹ − 1/2·μⁿ⁻², first order at the start and after a restart), projecting region by region and clipping to each region's [mu_inf, mu_0]. Newtonian regions keep mu_f.
  • turtleFSI/modules/fluid.py: new arguments nonnewtonian (default False) and cross_params. Non-Newtonian regions use mu_eff in the fluid stress term, Newtonian regions use the constant mu_f as before, so the Newtonian path is unchanged. mu_eff is returned so it can be updated in pre_solve (it is None for a Newtonian fluid).
  • turtleFSI/problems/example_aneurysm_nonnewtonian.py (new): example problem file showing how a simulation script uses the model, including how the effective viscosity is updated at each time step and stored to viscosity.xdmf for visualization (only during the second cardiac cycle, with a separate file after a restart).
  • tests/test_nonnewtonian.py (new): two fluid regions (one non-Newtonian, one Newtonian) and a solid; checks the initial values, the first/second-order update and the clipping.

Usage

In set_problem_parameters:

nonnewtonian=True,
cross_params={"mu_0": 0.056, "mu_inf": 0.00345, "lmbda": 3.736, "n": 2.406, "a": 0.254},

With several fluid regions, nonnewtonian and cross_params can also be set per region in fluid_properties (the global values are used for the regions that do not set them), e.g.:

fluid_properties=[
    {"dx_f_id": 1, "rho_f": 1.0E3, "mu_f": 3.45E-3, "nonnewtonian": True,
     "cross_params": {"mu_0": 0.056, "mu_inf": 0.00345, "lmbda": 3.736, "n": 2.406, "a": 0.254}},
    {"dx_f_id": 1001, "rho_f": 1.0E3, "mu_f": 3.5E-2, "nonnewtonian": False},
],

In pre_solve, update the viscosity once per time step:

if mu_eff is not None:
    mu_eff.update()

In post_solve, store the viscosity (in the example, only during the second cardiac cycle):

if "mu_file" not in namespace:
    mu_file = XDMFFile(MPI.comm_world, str(Path(visualization_folder) / "viscosity.xdmf"))
    mu_file.parameters["flush_output"] = True
    mu_file.parameters["rewrite_function_mesh"] = False
    namespace["mu_file"] = mu_file

namespace["mu_file"].write(mu_eff, t)

Return dict(mu_file=namespace["mu_file"]) from post_solve so the file stays open across time steps.

Tests

  • tests/test_nonnewtonian.py and the existing tests/test_turtleFSI.py pass on Saga (FEniCS 2019).
  • CI currently fails in the conda setup step (HTTP 404 when downloading Mambaforge), before any test runs.

the nonnewtonian.py is the implementation of the modified cross model through a second order lagged version. fluid.py has the addition of viscosity that could be not constant if the nonnewtonian is selected, while the problem file has an example of how to store and update the viscosity during the simulation, with a flag to select the option of the rheology behavior.
Existing problem files that do not define nonnewtonian keep the previous (Newtonian) behaviour.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Multi-region viscosity updates, output scheduling, and restart-safe viscosity storage have unresolved correctness issues.

4 open issues
What changed in this PR

Adds an opt-in modified Cross viscosity model to turtleFSI’s fluid formulation, preserving the default Newtonian behavior.

Changes:

  • Computes ALE shear rate and updates effective viscosity using explicit extrapolation and clipping.
  • Uses a DG1 viscosity field in fluid stress terms when enabled.
  • Adds an aneurysm example demonstrating viscosity updates and selective visualization output.
File Description
turtleFSI/​problems/​example_aneurysm_nonnewtonian.py Demonstrates model configuration, timestep updates, and viscosity output.
turtleFSI/​modules/​nonnewtonian.py Implements shear-rate calculation, the modified Cross model, and viscosity updates.
turtleFSI/​modules/​fluid.py Adds optional variable viscosity to fluid stress terms.

Review effort: Balanced

Comment thread turtleFSI/modules/fluid.py Outdated
Comment on lines +28 to +31
if nonnewtonian:
V_mu = FunctionSpace(mesh, "DG", 1)
mu_eff = Function(V_mu)
mu_eff.interpolate(Constant(fluid_properties[fluid_region]["mu_f"]))

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

@simonecap00 Can you have a look at this suggestion?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Hi Kei, thanks for flagging this! Fixed in c417a29.

I went for a single DG1 viscosity field shared by all the fluid regions, where each region picks its own rheology: either modified Cross (with its own cross_params) or plain Newtonian (mu_f). You can still set nonnewtonian / cross_params globally as before, or per region in fluid_properties.

The update (projection region by region + clipping to each region's [mu_inf, mu_0], first order at start/restart and then second-order extrapolation) now lives in nonnewtonian.py, so in pre_solve you just call mu_eff.update(). The Newtonian path in fluid.py is the same as on master.

I also added tests/test_nonnewtonian.py (two fluid regions + a solid): the new tests and the existing test_turtleFSI.py all pass on Saga. And I applied the other three Copilot suggestions and cleaned up the example a bit.

Btw, CI is failing at the conda setup step (404 when downloading Mambaforge) before any test runs, so it's not related to this PR.

Let me know what you think!

simonecap00 and others added 5 commits November 4, 2026 09:51
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
Remove debug prints, unused variable and imports, document the modified Cross parameters and fix typos in comments. No change in behaviour.
Each fluid region is non-Newtonian (modified Cross, own cross_params) or Newtonian (mu_f), set globally or per region in fluid_properties. The update and per-region clipping live in nonnewtonian.py; pre_solve only calls mu_eff.update(). The Newtonian path is unchanged. Adds tests/test_nonnewtonian.py.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🔵 Needs a closer look

A save-stride mismatch suppresses visualization output with --save-step 10, while Newtonian runs incorrectly enter viscosity update/output handling with a float instead of None.

1 open issue
2 previously missed issues

In code that hasn't changed since last review

Medium severity Return None for disabled non-Newtonian viscosity

turtleFSI/​modules/​fluid.py:64

With nonnewtonian=False, mu_eff is the last region's scalar mu_f, not None. Consequently, the documented if mu_eff is not None guard enters the update/output path even for a Newtonian simulation and attempts to operate on a float. Return None when the model is disabled, without changing the coefficients used in the stress forms.

Medium severity Fix off-by-one stride check for visualization output

turtleFSI/​problems/​example_aneurysm_nonnewtonian.py:126

With --save-step 10, the solver calls this wrapper only when counter % 10 == 0 (monolithic.py:194–195), but the example's stride of 10 accepts only counters ending in 9 because this predicate uses counter + 1. No displacement, velocity, or pressure visualization is then written, while viscosity output continues. Align the stride check with the solver's zero-based counter.

3 resolved since last review

Review effort: Balanced

@simonecap00

Copy link
Copy Markdown
Author

The PR didn't pick up c417a29 (it was still on a98d860), so Copilot reviewed the old code: the per-region viscosity and mu_eff = None for Newtonian fluids were already fixed there. The save-stride off-by-one is fixed in 8ca625a.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants