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
Using IncompressibleNavierStokes it is possible to write the momentum equations without the pressure by explicitly solving the discrete Poisson equation and obtaining:
<!– 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.
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: 202-element Vector{Float64}:
0.0
0.001692801247740749
0.002474639967564903
0.003731106127119373
0.004794554038028024
0.006023202707194937
0.007225926495334916
0.008496357953074934
0.009780738453350616
0.011101743076348276
⋮
0.896690579004794
0.9106602125448481
0.9248472834187952
0.939260325372123
0.9539091535453473
0.9688042961469006
0.9839569635705989
0.9993782537267281
1.0
u: 202-element Vector{Array{Float64, 3}}:
[-0.6321966880505496 -0.6512335688598714 … -0.6321966880505496 -0.6512335688598714; -0.37994893743320535 -0.49006030475628515 … -0.37994893743320535 -0.49006030475628515; … ; -0.6321966880505496 -0.6512335688598714 … -0.6321966880505496 -0.6512335688598714; -0.37994893743320535 -0.49006030475628515 … -0.37994893743320535 -0.49006030475628515;;; -1.3675675254262603 -1.5085495041376273 … -1.3675675254262603 -1.5085495041376273; -1.274543633705214 -1.4357168978087953 … -1.274543633705214 -1.4357168978087953; … ; -1.3675675254262603 -1.5085495041376273 … -1.3675675254262603 -1.5085495041376273; -1.274543633705214 -1.4357168978087953 … -1.274543633705214 -1.4357168978087953]
[-0.615989242176627 -0.6294425507664585 … -0.615989242176627 -0.6294425507664585; -0.38529142621869733 -0.5005383510896478 … -0.3863412614304039 -0.4892676883825711; … ; -0.615989242176627 -0.6540807892927175 … -0.6285096022667264 -0.6294425507664585; -0.38529142621869733 -0.4892676883825711 … -0.38529142621869733 -0.4892676883825711;;; -1.4025223652171745 -1.5278709990741257 … -1.4025223652171745 -1.5278709990741257; -1.290266720914735 -1.4044927230660782 … -1.250950284863013 -1.4356964523911666; … ; -1.4025223652171745 -1.4845862672319123 … -1.3510880507292928 -1.5278709990741257; -1.290266720914735 -1.4356964523911666 … -1.290266720914735 -1.4356964523911666]
[-0.6091784083432579 -0.6202403866591866 … -0.6091784083432579 -0.6202403866591866; -0.38789720207868245 -0.5057610285680655 … -0.3896707688200793 -0.48908478122423876; … ; -0.6091784083432579 -0.6560715220550645 … -0.6273390668227643 -0.6202403866591866; -0.38789720207868245 -0.48908478122423876 … -0.38789720207868245 -0.48908478122423876;;; -1.4174664947961377 -1.5358044666839787 … -1.4174664947961377 -1.5358044666839787; -1.2966790724029973 -1.3900611869045432 … -1.239750693417548 -1.435188414971457; … ; -1.4174664947961377 -1.4730069260087526 … -1.3428676388943168 -1.5358044666839787; -1.2966790724029973 -1.435188414971457 … -1.2966790724029973 -1.435188414971457]
[-0.5991147760410891 -0.6065071058700311 … -0.5991147760410891 -0.6065071058700311; -0.3922386007779064 -0.5146078928230745 … -0.39551106176788653 -0.4889574743751939; … ; -0.5991147760410891 -0.6601176098929176 … -0.6261805322110798 -0.6065071058700311; -0.3922386007779064 -0.4889574743751939 … -0.3922386007779064 -0.4889574743751939;;; -1.439950004933424 -1.5473618843655672 … -1.439950004933424 -1.5473618843655672; -1.3059351956813035 -1.366934870898871 … -1.221425153829031 -1.4338189638180465; … ; -1.439950004933424 -1.4538059182243805 … -1.3288952152282558 -1.5473618843655672; -1.3059351956813035 -1.4338189638180465 … -1.3059351956813035 -1.4338189638180465]
[-0.5914290726181167 -0.595823031342561 … -0.5914290726181167 -0.595823031342561; -0.39603460681039354 -0.5224866201166225 … -0.40091375786207845 -0.4889481887386094; … ; -0.5914290726181167 -0.6643123062163154 … -0.6259031535739007 -0.595823031342561; -0.39603460681039354 -0.4889481887386094 … -0.39603460681039354 -0.4889481887386094;;; -1.4575550561266768 -1.556091651327139 … -1.4575550561266768 -1.556091651327139; -1.312820827415277 -1.347484107330612 … -1.2056584212309218 -1.4322086039556465; … ; -1.4575550561266768 -1.4370545857388999 … -1.3163774560243513 -1.556091651327139; -1.312820827415277 -1.4322086039556465 … -1.312820827415277 -1.4322086039556465]
[-0.5834668508200221 -0.5844435565447441 … -0.5834668508200221 -0.5844435565447441; -0.4005211612941346 -0.5319724427554288 … -0.4076599376972919 -0.48896826982215236; … ; -0.5834668508200221 -0.669970604776179 … -0.6263862401340259 -0.5844435565447441; -0.4005211612941346 -0.48896826982215236 … -0.4005211612941346 -0.48896826982215236;;; -1.476343834883942 -1.5651020647102194 … -1.476343834883942 -1.5651020647102194; -1.3197748291221296 -1.3252233361192294 … -1.1872251740984818 -1.4299336719913547; … ; -1.476343834883942 -1.4172238428025108 … -1.3011865209428635 -1.5651020647102194; -1.3197748291221296 -1.4299336719913547 … -1.3197748291221296 -1.4299336719913547]
[-0.5765825098350621 -0.574188381841243 … -0.5765825098350621 -0.574188381841243; -0.40497604260511927 -0.541589275949042 … -0.41476152320690807 -0.48893690517249616; … ; -0.5765825098350621 -0.6762768288428371 … -0.627685298339179 -0.574188381841243; -0.40497604260511927 -0.48893690517249616 … -0.40497604260511927 -0.48893690517249616;;; -1.4932089592081441 -1.5729325909900425 … -1.4932089592081441 -1.5729325909900425; -1.325625607771624 -1.3037176957731602 … -1.1690301428793677 -1.4273726449347413; … ; -1.4932089592081441 -1.3974118683579273 … -1.2856278358184112 -1.5729325909900425; -1.325625607771624 -1.4273726449347413 … -1.325625607771624 -1.4273726449347413]
[-0.5702295279567582 -0.5641675008275777 … -0.5702295279567582 -0.5641675008275777; -0.40969916522386524 -0.5520259701045026 … -0.4227632459159418 -0.48875617565780577; … ; -0.5702295279567582 -0.6836698148465817 … -0.6299258948539276 -0.5641675008275777; -0.40969916522386524 -0.48875617565780577 … -0.40969916522386524 -0.48875617565780577;;; -1.5094836736970147 -1.5802817987933817 … -1.5094836736970147 -1.5802817987933817; -1.3308716101621123 -1.2813743931197243 … -1.1497305483776474 -1.4244097869024421; … ; -1.5094836736970147 -1.3761627504250544 … -1.2685366647907161 -1.5802817987933817; -1.3308716101621123 -1.4244097869024421 … -1.3308716101621123 -1.4244097869024421]
[-0.5646944724798486 -0.5547344349866447 … -0.5646944724798486 -0.5547344349866447; -0.4144368958497468 -0.5627857996881348 … -0.43133387048123134 -0.4883241023349607; … ; -0.5646944724798486 -0.6918048121591845 … -0.6330673854455559 -0.5547344349866447; -0.4144368958497468 -0.4883241023349607 … -0.4144368958497468 -0.4883241023349607;;; -1.5244410425077182 -1.586893353637798 … -1.5244410425077182 -1.586893353637798; -1.3352965490843047 -1.2592360652554764 … -1.130217052784428 -1.4212432878227261; … ; -1.5244410425077182 -1.3544549787855318 … -1.2506640820010098 -1.586893353637798; -1.3352965490843047 -1.4212432878227261 … -1.3352965490843047 -1.4212432878227261]
[-0.559845688427598 -0.5456017735129262 … -0.559845688427598 -0.5456017735129262; -0.41920910351229035 -0.573983202206529 … -0.4406038962185149 -0.4875199169628934; … ; -0.559845688427598 -0.7007496827005273 … -0.6371742201012305 -0.5456017735129262; -0.41920910351229035 -0.4875199169628934 … -0.41920910351229035 -0.4875199169628934;;; -1.5383805358645553 -1.5929844885159208 … -1.5383805358645553 -1.5929844885159208; -1.3390274702234028 -1.2369939725821115 … -1.110227492088114 -1.4179024353994985; … ; -1.5383805358645553 -1.332006334874074 … -1.2317611802152988 -1.5929844885159208; -1.3390274702234028 -1.4179024353994985 … -1.3390274702234028 -1.4179024353994985]
⋮
[2.565918154766374 3.441505852665819 … 2.565918154766374 3.441505852665819; 2.9281602121344372 -0.317447725091473 … -0.26771378918624145 3.5021959649493954; … ; 2.565918154766374 -0.3400143859559852 … -0.29389402741103865 3.441505852665819; 2.9281602121344372 3.5021959649493954 … 2.9281602121344372 3.5021959649493954;;; -0.3934501808541801 -0.9784809529911632 … -0.3934501808541801 -0.9784809529911632; 0.522430816525508 -0.12251433543026012 … -0.09994767456579247 -0.04654038027694736; … ; -0.3934501808541801 -0.1142222758449014 … -0.09412061550108997 -0.9784809529911632; 0.522430816525508 -0.04654038027694736 … 0.522430816525508 -0.04654038027694736]
[2.5682618672427426 3.4417822594466294 … 2.5682618672427426 3.4417822594466294; 2.9293834248999597 -0.32227276316756703 … -0.2712992493206232 3.501927264693766; … ; 2.5682618672427426 -0.3442032693691848 … -0.29643454610047665 3.4417822594466294; 2.9293834248999597 3.501927264693766 … 2.9293834248999597 3.501927264693766;;; -0.39764495035263736 -0.984356243473541 … -0.39764495035263736 -0.984356243473541; 0.5183241250460967 -0.1203549137141033 … -0.09842440751253004 -0.05242967927477126; … ; -0.39764495035263736 -0.11238152121442033 … -0.0931043674386702 -0.984356243473541; 0.5183241250460967 -0.05242967927477126 … 0.5183241250460967 -0.05242967927477126]
[2.5703691901376664 3.4422154213512592 … 2.5703691901376664 3.4422154213512592; 2.9306180973338494 -0.32722484796318085 … -0.2753060693251677 3.5020693807961747; … ; 2.5703691901376664 -0.3487265906973923 … -0.2995927123601947 3.4422154213512592; 2.9306180973338494 3.5020693807961747 … 2.9306180973338494 3.5020693807961747;;; -0.40170630613692926 -0.9902920741988546 … -0.40170630613692926 -0.9902920741988546; 0.5142446086561638 -0.11843204043834268 … -0.09693029770417563 -0.058496895945553254; … ; -0.40170630613692926 -0.11056035094056377 … -0.09190110294647955 -0.9902920741988546; 0.5142446086561638 -0.058496895945553254 … 0.5142446086561638 -0.058496895945553254]
[2.5723957199758227 3.4429533952390807 … 2.5723957199758227 3.4429533952390807; 2.931996089309628 -0.3321969547102543 … -0.2796173410496009 3.5027335496664214; … ; 2.5723957199758227 -0.35346222535890404 … -0.30324865227802256 3.4429533952390807; 2.931996089309628 3.5027335496664214 … 2.931996089309628 3.5027335496664214;;; -0.40575812422630164 -0.9964306384188538 … -0.40575812422630164 -0.9964306384188538; 0.5100877343463873 -0.11684227969603334 … -0.09557700904742798 -0.06485593107187278; … ; -0.40575812422630164 -0.10889325645338975 … -0.09065161927597871 -0.9964306384188538; 0.5100877343463873 -0.06485593107187278 … 0.5100877343463873 -0.06485593107187278]
[2.574484126924228 3.444122741899035 … 2.574484126924228 3.444122741899035; 2.9336328802050233 -0.3370972656027623 … -0.2841242164584563 3.5040085186409837; … ; 2.574484126924228 -0.3582994048902968 … -0.30728510995148767 3.444122741899035; 2.9336328802050233 3.5040085186409837 … 2.9336328802050233 3.5040085186409837;;; -0.409915820341621 -1.0028985686383791 … -0.409915820341621 -1.0028985686383791; 0.505759285910997 -0.11565796292026813 … -0.09445582363277792 -0.07160434249377896; … ; -0.409915820341621 -0.10749015319756974 … -0.089478019971781 -1.0028985686383791; 0.505759285910997 -0.07160434249377896 … 0.505759285910997 -0.07160434249377896]
[2.576761857047728 3.4458280403060138 … 2.576761857047728 3.4458280403060138; 2.9356267998170327 -0.3418502171137529 … -0.288728335182168 3.505961501159463; … ; 2.576761857047728 -0.363141068569843 … -0.31159104561143136 3.4458280403060138; 2.9356267998170327 3.505961501159463 … 2.9356267998170327 3.505961501159463;;; -0.41428432231918655 -1.0098055590919133 … -0.41428432231918655 -1.0098055590919133; 0.5011763660075679 -0.1149290236528325 … -0.09363817219678644 -0.07882313225946846; … ; -0.41428432231918655 -0.10643672883649419 … -0.08848265430684454 -1.0098055590919133; 0.5011763660075679 -0.07882313225946846 … 0.5011763660075679 -0.07882313225946846]
[2.579339815455126 3.4481523105380063 … 2.579339815455126 3.4481523105380063; 2.938058954186307 -0.34639692073906403 … -0.2933436560386324 3.508639694496433; … ; 2.579339815455126 -0.36790544300093164 … -0.3160645664287038 3.4481523105380063; 2.938058954186307 3.508639694496433 … 2.938058954186307 3.508639694496433;;; -0.4189569094643705 -1.0172440789932333 … -0.4189569094643705 -1.0172440789932333; 0.49626769827686484 -0.11468531249602101 … -0.09317679023419771 -0.08657738741576942; … ; -0.4189569094643705 -0.10579568156653296 … -0.08774802632032623 -1.0172440789932333; 0.49626769827686484 -0.08657738741576942 … 0.49626769827686484 -0.08657738741576942]
[2.5823116529340124 3.4511579931468397 … 2.5823116529340124 3.4511579931468397; 2.940993519171097 -0.35069465589118987 … -0.29789738503920815 3.5120720520758075; … ; 2.5823116529340124 -0.3725265376855062 … -0.32061488147602907 3.4511579931468397; 2.940993519171097 3.5120720520758075 … 2.940993519171097 3.5120720520758075;;; -0.4240144116310053 -1.025289395494268 … -0.4240144116310053 -1.025289395494268; 0.4909737427008371 -0.11493924766001556 … -0.09310736586574357 -0.09491698925799726; … ; -0.4240144116310053 -0.10560867019153909 … -0.08733752377528295 -1.025289395494268; 0.4909737427008371 -0.09491698925799726 … 0.4909737427008371 -0.09491698925799726]
[2.5824396821941398 3.451292883857175 … 2.5824396821941398 3.451292883857175; 2.9411219079880193 -0.3508607235144149 … -0.29807712666620906 3.512225055005529; … ; 2.5824396821941398 -0.37270739213583354 … -0.320797091895139 3.451292883857175; 2.9411219079880193 3.512225055005529 … 2.9411219079880193 3.512225055005529;;; -0.4242254376067874 -1.0256242588414892 … -0.4242254376067874 -1.0256242588414892; 0.4907535517955694 -0.11495973962757433 … -0.0931130710062 -0.09526282996814939; … ; -0.4242254376067874 -0.10561101278180364 … -0.08732855850482839 -1.0256242588414892; 0.4907535517955694 -0.09526282996814939 … 0.4907535517955694 -0.09526282996814939]
Alternatively, it is also possible to use an in-place formulation
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
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.
IncompressibleNavierStokes.right_hand_side! Method
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
.