Skip to content

Using IncompressibleNavierStokes in SciML

The SciML organization is a collection of tools for solving equations and modeling systems. It has a coherent development principle, unified APIs over large collections of equation solvers, pervasive differentiability and sensitivity analysis, and features many of the highest performance and parallel implementations one can find.

In particular, DifferentialEquations.jl contains tools to solve differential equations defined as dudt=f(u,t) that include a large collection of solvers, sensitivity analysis, and more.

Using IncompressibleNavierStokes it is possible to write the momentum equations without the pressure by explicitly solving the discrete Poisson equation and obtaining:

duhdt=(IGL1WM)(F(uh)yG)GL1WdyMdt=f(uh).

<!– There is an issue with linking to other MD pages: –> <!– https://github.com/LuxDL/DocumenterVitepress.jl/issues/172 –> The derivation and the drawbacks of this approach are discussed in the documentation.

This projected right-hand side can be used in the SciML solvers to solve the Navier-Stokes equations. The following example shows how to use the SciML solvers to solve the ODEs obtained from the Navier-Stokes equations.

OrdinaryDiffEqTsit5

We here use the explicit solver Tsit5 from OrdinaryDiffEqTsit5 directly to skip loading all the toolchains for implicit solvers, which takes a while to install on GitHub.

julia
using OrdinaryDiffEqTsit5
using IncompressibleNavierStokes
ax = range(0, 1, 101)
setup = Setup(; x = (ax, ax), Re = 500.0)
psolver = default_psolver(setup)
f = create_right_hand_side(setup, psolver)
u0 = random_field(setup)
tspan = (0.0, 1.0)     # time span where to solve.
problem = ODEProblem(f, u0, tspan) #SciMLBase.ODEProblem
sol = solve(problem, Tsit5(), reltol = 1e-8, abstol = 1e-8) # sol: SciMLBase.ODESolution
retcode: Success
Interpolation: specialized 4th order "free" interpolation
t: 244-element Vector{Float64}:
 0.0
 0.0015489518055344191
 0.0022612670146322554
 0.0034024297202108343
 0.004365539023874
 0.0054721634857962075
 0.0065506196454278645
 0.0076830177127558946
 0.008821385092485779
 0.00998267243396393

 0.911011427434683
 0.9216819459345801
 0.9325348935030995
 0.9435740668998773
 0.9548027140410242
 0.9662234854343992
 0.9778386003853389
 0.9896501207021785
 1.0
u: 244-element Vector{Array{Float64, 3}}:
 [0.46169314766812314 -0.08064432552538069 … 0.46169314766812314 -0.08064432552538069; 0.5293283260936434 -0.10845395994898649 … 0.5293283260936434 -0.10845395994898649; … ; 0.46169314766812314 -0.08064432552538069 … 0.46169314766812314 -0.08064432552538069; 0.5293283260936434 -0.10845395994898649 … 0.5293283260936434 -0.10845395994898649;;; -1.8105521305127463 -1.7532271407497877 … -1.8105521305127463 -1.7532271407497877; -1.434364753304517 -1.4065551188809118 … -1.434364753304517 -1.4065551188809118; … ; -1.8105521305127463 -1.7532271407497877 … -1.8105521305127463 -1.7532271407497877; -1.434364753304517 -1.4065551188809118 … -1.434364753304517 -1.4065551188809118]
 [0.3282830191673751 -0.2144039709867286 … 0.3282830191673751 -0.2144039709867286; 0.4100847668040876 -0.15966725867747625 … 0.4712239907749937 -0.22789163383504774; … ; 0.3282830191673751 -0.13033224127648757 … 0.40474515767150016 -0.2144039709867286; 0.4100847668040876 -0.22789163383504774 … 0.4100847668040876 -0.22789163383504774;;; -1.8163634523855714 -1.694702499384956 … -1.8163634523855714 -1.694702499384956; -1.4451817581059347 -1.4102722016196096 … -1.4396072190205964 -1.3639223764211252; … ; -1.8163634523855714 -1.7508422928151262 … -1.8183985045063493 -1.694702499384956; -1.4451817581059347 -1.3639223764211252 … -1.4451817581059347 -1.3639223764211252]
 [0.2675451959527584 -0.2749792312854956 … 0.2675451959527584 -0.2749792312854956; 0.35557847922830627 -0.18231430490766262 … 0.4448352103329765 -0.2818888625692179; … ; 0.2675451959527584 -0.1524171533858616 … 0.3791126866030678 -0.2749792312854956; 0.35557847922830627 -0.2818888625692179 … 0.35557847922830627 -0.2818888625692179;;; -1.817239253120923 -1.6667535391143027 … -1.817239253120923 -1.6667535391143027; -1.448967623955368 -1.4119480167765641 … -1.4418451682983628 -1.3436516807727603; … ; -1.817239253120923 -1.74919337968125 … -1.8211113847132427 -1.6667535391143027; -1.448967623955368 -1.3436516807727603 … -1.448967623955368 -1.3436516807727603]
 [0.17110500260543215 -0.3707027854870693 … 0.17110500260543215 -0.3707027854870693; 0.26880579279873873 -0.21733489891274876 … 0.40308999869738404 -0.3670796542274148; … ; 0.17110500260543215 -0.18675937092937658 … 0.3387824365954729 -0.3707027854870693; 0.26880579279873873 -0.3670796542274148 … 0.26880579279873873 -0.3670796542274148;;; -1.816350298137928 -1.6208341462910167 … -1.816350298137928 -1.6208341462910167; -1.4534830998583672 -1.4146255676026875 … -1.4452010955860568 -1.310433241010784; … ; -1.816350298137928 -1.7459452191318516 … -1.82433427443327 -1.6208341462910167; -1.4534830998583672 -1.310433241010784 … -1.4534830998583672 -1.310433241010784]
 [0.09058421513077537 -0.450148467565211 … 0.09058421513077537 -0.450148467565211; 0.1961828237036056 -0.24562925436657962 … 0.368435700204154 -0.4376455464805785; … ; 0.09058421513077537 -0.2147139649742275 … 0.30544945630403675 -0.450148467565211; 0.1961828237036056 -0.4376455464805785 … 0.1961828237036056 -0.4376455464805785;;; -1.8134780249050504 -1.5811563780360782 … -1.8134780249050504 -1.5811563780360782; -1.455834398450393 -1.4169050749698728 … -1.447820364362221 -1.281786093994439; … ; -1.8134780249050504 -1.7427182227959104 … -1.8260332522346012 -1.5811563780360782; -1.455834398450393 -1.281786093994439 … -1.455834398450393 -1.281786093994439]
 [-0.0008947402026591306 -0.5398124429095649 … -0.0008947402026591306 -0.5398124429095649; 0.11353086710464526 -0.27665032575537846 … 0.32935232592421904 -0.5171324552583227; … ; -0.0008947402026591306 -0.24562034800220325 … 0.26794818677623405 -0.5398124429095649; 0.11353086710464526 -0.5171324552583227 … 0.11353086710464526 -0.5171324552583227;;; -1.8078957269795544 -1.5347439819836062 … -1.8078957269795544 -1.5347439819836062; -1.4569351783899147 -1.4195757753773748 … -1.4506057531305456 -1.2483035805294662; … ; -1.8078957269795544 -1.7385834605077377 … -1.8269154696486545 -1.5347439819836062; -1.4569351783899147 -1.2483035805294662 … -1.4569351783899147 -1.2483035805294662]
 [-0.08891832326174624 -0.6254201420257633 … -0.08891832326174624 -0.6254201420257633; 0.033895403004111785 -0.3052932890016494 … 0.2920967903729316 -0.5928742786558814; … ; -0.08891832326174624 -0.2744340569127644 … 0.23222933479141494 -0.6254201420257633; 0.033895403004111785 -0.5928742786558814 … 0.033895403004111785 -0.5928742786558814;;; -1.8002441297607026 -1.4888846229201114 … -1.8002441297607026 -1.4888846229201114; -1.4564249689883926 -1.4222536349714099 … -1.4531128670602897 -1.2152074103490902; … ; -1.8002441297607026 -1.7342338905984849 … -1.8267704548924384 -1.4888846229201114; -1.4564249689883926 -1.2152074103490902 … -1.4564249689883926 -1.2152074103490902]
 [-0.18008104097629238 -0.7133041504250635 … -0.18008104097629238 -0.7133041504250635; -0.04865116821948992 -0.3336286558545213 … 0.25394339883583134 -0.6704934097161468; … ; -0.18008104097629238 -0.30323296909186254 … 0.19561671552054632 -0.7133041504250635; -0.04865116821948992 -0.6704934097161468 … -0.04865116821948992 -0.6704934097161468;;; -1.7900304562776073 -1.440297183149706 … -1.7900304562776073 -1.440297183149706; -1.454292324691374 -1.4251600972598026 … -1.4555557840224551 -1.1800789249566304; … ; -1.7900304562776073 -1.7294529585526133 … -1.8256673404450514 -1.440297183149706; -1.454292324691374 -1.1800789249566304 … -1.454292324691374 -1.1800789249566304]
 [-0.27034523460640547 -0.7994565314605779 … -0.27034523460640547 -0.7994565314605779; -0.13042517210590227 -0.36027425566010096 … 0.2166646160783299 -0.7464751662376765; … ; -0.27034523460640547 -0.3306070890693693 … 0.15975563344573146 -0.7994565314605779; -0.13042517210590227 -0.7464751662376765 … -0.13042517210590227 -0.7464751662376765;;; -1.7777040235172514 -1.3912331976989645 … -1.7777040235172514 -1.3912331976989645; -1.4505982061502405 -1.4281828968904644 … -1.457850063481189 -1.1444824806981582; … ; -1.7777040235172514 -1.7245470607519882 … -1.8237044551872725 -1.3912331976989645; -1.4505982061502405 -1.1444824806981582 … -1.4505982061502405 -1.1444824806981582]
 [-0.3609169881769161 -0.884950426732755 … -0.3609169881769161 -0.884950426732755; -0.2124974356589172 -0.38552673331199255 … 0.17982471600330116 -0.8218096516395101; … ; -0.3609169881769161 -0.35682778453302594 … 0.12418284055156541 -0.884950426732755; -0.2124974356589172 -0.8218096516395101 … -0.2124974356589172 -0.8218096516395101;;; -1.76321485415419 -1.3411748446400775 … -1.76321485415419 -1.3411748446400775; -1.4453390998624254 -1.4313603850650187 … -1.4600593338439791 -1.107969706901715; … ; -1.76321485415419 -1.719553458721572 … -1.8209579016069923 -1.3411748446400775; -1.4453390998624254 -1.107969706901715 … -1.4453390998624254 -1.107969706901715]

 [-3.021774271481763 -3.3745161091219256 … -3.021774271481763 -3.3745161091219256; -2.1717823971029753 -0.06911375168850398 … -0.03729719676743799 -2.5953933894970587; … ; -3.021774271481763 -0.06513855879698924 … -0.030694521189482675 -3.3745161091219256; -2.1717823971029753 -2.5953933894970587 … -2.1717823971029753 -2.5953933894970587;;; 3.5976891112516687 5.109444565381124 … 3.5976891112516687 5.109444565381124; 3.2965567701398464 0.33329939306832057 … 0.3293242001768154 4.894991371882385; … ; 3.5976891112516687 0.3198744741800295 … 0.312158741255979 5.109444565381124; 3.2965567701398464 4.894991371882385 … 3.2965567701398464 4.894991371882385]
 [-3.0101527359562095 -3.3622035989842285 … -3.0101527359562095 -3.3622035989842285; -2.160169780375288 -0.0675082797698226 … -0.03554885857158378 -2.5832988568466932; … ; -3.0101527359562095 -0.0642138354673027 … -0.029642317009844012 -3.3622035989842285; -2.160169780375288 -2.5832988568466932 … -2.160169780375288 -2.5832988568466932;;; 3.5936191428143385 5.106558171958505 … 3.5936191428143385 5.106558171958505; 3.293403329625358 0.33240981780767614 … 0.3291153735051658 4.892854733320253; … ; 3.5936191428143385 0.31883442659510647 … 0.3118518421235826 5.106558171958505; 3.293403329625358 4.892854733320253 … 3.293403329625358 4.892854733320253]
 [-2.9982586313142328 -3.3496152654285924 … -2.9982586313142328 -3.3496152654285924; -2.1482839090718353 -0.06578350621445042 … -0.03369391193245914 -2.5709246661980916; … ; -2.9982586313142328 -0.06315364614709801 … -0.028467465635460723 -3.3496152654285924; -2.1482839090718353 -2.5709246661980916 … -2.1482839090718353 -2.5709246661980916;;; 3.589778206431419 5.103949306404896 … 3.589778206431419 5.103949306404896; 3.2905163765301926 0.33171271843900096 … 0.32908285837165835 4.891030448680357; … ; 3.589778206431419 0.3179345982439004 … 0.31167423467015953 5.103949306404896; 3.2905163765301926 4.891030448680357 … 3.2905163765301926 4.891030448680357]
 [-2.9860925688161286 -3.336747306926582 … -2.9860925688161286 -3.336747306926582; -2.1361269695782346 -0.06393840499929661 … -0.0317361838789289 -2.558266360905398; … ; -2.9860925688161286 -0.06195572792940919 … -0.02717128124536334 -3.336747306926582; -2.1361269695782346 -2.558266360905398 … -2.1361269695782346 -2.558266360905398;;; 3.5861703508015776 5.101623455950773 … 3.5861703508015776 5.101623455950773; 3.287896605266971 0.3312053628207044 … 0.3292226857508264 4.88952114859092; … ; 3.5861703508015776 0.3171765482224647 … 0.3116261398590228 5.101623455950773; 3.287896605266971 4.88952114859092 … 3.287896605266971 4.88952114859092]
 [-2.9736575632088416 -3.323598321447489 … -2.9736575632088416 -3.323598321447489; -2.123704009646847 -0.06197325377747221 … -0.029680780388786774 -2.545322463449908; … ; -2.9736575632088416 -0.06061871985036534 … -0.025756069659294584 -3.323598321447489; -2.123704009646847 -2.545322463449908 … -2.123704009646847 -2.545322463449908;;; 3.582798637014144 5.09958520395747 … 3.582798637014144 5.09958520395747; 3.285543378966926 0.3308844541138923 … 0.3295299201867946 4.888328454666916; … ; 3.582798637014144 0.31656139688121043 … 0.31170707018643273 5.09958520395747; 3.285543378966926 4.888328454666916 … 3.285543378966926 4.888328454666916]
 [-2.9609591704630915 -3.3101695652098675 … -2.9609591704630915 -3.3101695652098675; -2.1110230320220014 -0.05988988457435196 … -0.027534153989879646 -2.532094763562723; … ; -2.9609591704630915 -0.05914240505566947 … -0.024225253184464366 -3.3101695652098675; -2.1110230320220014 -2.532094763562723 … -2.1110230320220014 -2.532094763562723;;; 3.579664881789351 5.097837948954461 … 3.579664881789351 5.097837948954461; 3.2834544189190376 0.33074597911588816 … 0.32999849959721456 4.887452628411164; … ; 3.579664881789351 0.31608973052149475 … 0.3119157195919538 5.097837948954461; 3.2834544189190376 4.887452628411164 … 3.2834544189190376 4.887452628411164]
 [-2.948005336241489 -3.2964649186904267 … -2.948005336241489 -3.2964649186904267; -2.09809477700221 -0.05769184162051031 … -0.025304074754287198 -2.5185882875944205; … ; -2.948005336241489 -0.057527902903249384 … -0.022583433048592852 -3.2964649186904267; -2.09809477700221 -2.5185882875944205 … -2.09809477700221 -2.5185882875944205;;; 3.5767693514735495 5.096383582384046 … 3.5767693514735495 5.096383582384046; 3.2816254777659397 0.3307850258184525 … 0.33062108710120053 4.886892192835735; … ; 3.5767693514735495 0.31576147012888145 … 0.31224984821452567 5.096383582384046; 3.2816254777659397 4.886892192835735 … 3.2816254777659397 4.886892192835735]
 [-2.9348060632040345 -3.282490667633773 … -2.9348060632040345 -3.282490667633773; -2.0849323051011868 -0.05538444182920574 … -0.022999515811374813 -2.50481105168504; … ; -2.9348060632040345 -0.055777798834579666 … -0.02083638727011616 -3.282490667633773; -2.0849323051011868 -2.50481105168504 … -2.0849323051011868 -2.50481105168504;;; 3.5741104772095182 5.0952221848248405 … 3.5741104772095182 5.0952221848248405; 3.2800500622918998 0.33099559917281773 … 0.33138895617820086 4.886643584999929; … ; 3.5741104772095182 0.31557572767814385 … 0.312706178230541 5.0952221848248405; 3.2800500622918998 4.886643584999929 … 3.2800500622918998 4.886643584999929]
 [-2.9232300690898985 -3.270224128859518 … -2.9232300690898985 -3.270224128859518; -2.0733995472649194 -0.05331122851906984 … -0.020959051797968958 -2.4927152760281577; … ; -2.9232300690898985 -0.05416205091667352 … -0.019249829819723986 -3.270224128859518; -2.0733995472649194 -2.4927152760281577 … -2.0733995472649194 -2.4927152760281577;;; 3.5720039752721626 5.094454141197276 … 3.5720039752721626 5.094454141197276; 3.2788878111266127 0.33130957875761946 … 0.3321604011552327 4.886675183071758; … ; 3.5720039752721626 0.3155286148371269 … 0.31319472662810893 5.094454141197276; 3.2788878111266127 4.886675183071758 … 3.2788878111266127 4.886675183071758]

Alternatively, it is also possible to use an in-place formulation

julia
f!(du,u,p,t) = right_hand_side!(du, u, Ref([setup, psolver]), t)
u = similar(u0)
du = similar(u0)
p = nothing
t = 0.0
f!(du,u,p,t)

that is usually faster than the out-of-place formulation.

You can look here for more information on how to use the SciML solvers and all the options available.

API

IncompressibleNavierStokes.create_right_hand_side Method
julia
create_right_hand_side(
    setup,
    psolver
) -> IncompressibleNavierStokes.var"#right_hand_side#329"
create_right_hand_side(setup, psolver)

Creates a function that computes the right-hand side of the Navier-Stokes equations for a given setup and pressure solver.

Arguments

  • setup: The simulation setup containing grid and boundary conditions.

  • psolver: The pressure solver to be used.

Returns

A function that computes the right-hand side of the Navier-Stokes equations.

source

IncompressibleNavierStokes.right_hand_side! Method
julia
right_hand_side!(dudt, u, params_ref, t)
right_hand_side!(dudt, u, params_ref, t)

Computes the right-hand side of the Navier-Stokes equations in-place.

Arguments

  • dudt: The array to store the computed right-hand side.

  • u: The current velocity field.

  • params_ref: A reference to the parameters containing the setup and pressure solver.

  • t: The current time.

Returns

Nothing. The result is stored in dudt.

source