You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Add non-Newtonian (modified Cross) viscosity model for the fluid - #115
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.
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.:
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.
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.
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.
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.
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.
Fix off-by-one stride check for visualization output
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.
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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=Trueis set in the problem file, globally or per fluid region.Changes
turtleFSI/modules/nonnewtonian.py(new):get_fluid_rheologyreads the rheology of each fluid region: modified Cross with its own parameters, or Newtonian;EffectiveViscosity: a single DG1 fieldmu_effshared 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 keepmu_f.turtleFSI/modules/fluid.py: new argumentsnonnewtonian(defaultFalse) andcross_params. Non-Newtonian regions usemu_effin the fluid stress term, Newtonian regions use the constantmu_fas before, so the Newtonian path is unchanged.mu_effis returned so it can be updated inpre_solve(it isNonefor 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 toviscosity.xdmffor 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:With several fluid regions,
nonnewtonianandcross_paramscan also be set per region influid_properties(the global values are used for the regions that do not set them), e.g.:In
pre_solve, update the viscosity once per time step:In
post_solve, store the viscosity (in the example, only during the second cardiac cycle):Return
dict(mu_file=namespace["mu_file"])frompost_solveso the file stays open across time steps.Tests
tests/test_nonnewtonian.pyand the existingtests/test_turtleFSI.pypass on Saga (FEniCS 2019).