(0.97.5) Enforce mass-conservation inside domains with Radiation OpenBoundaryConditions#4627
(0.97.5) Enforce mass-conservation inside domains with Radiation OpenBoundaryConditions#4627
Conversation
|
The momentum flux is u*u*A but the mass flux is u*A --- we're concerned with mass flux, right? |
Yes, we're concerned with mass. I meant that I'm measuring velocity at the boundary. I removed the confusing statement from the comment. |
|
Also for the record, this does seem to allow for the ConjugateGradientSovler to converge when using On main [ Info: time: 0.000, max|u|: 1.010, CFL: 0.10, Net flux: 1.1067e-03
[ Info: time: NaN, max|u|: NaN, CFL: NaN, Net flux: NaN
[ Info: time: NaN, max|u|: NaN, CFL: NaN, Net flux: NaN
[ Info: time: NaN, max|u|: NaN, CFL: NaN, Net flux: NaN
[ Info: time: NaN, max|u|: NaN, CFL: NaN, Net flux: NaN
[ Info: time = NaN, iteration = 100: NaN found in field u. Stopping simulation.
[ Info: time: NaN, max|u|: NaN, CFL: NaN, Net flux: NaNThis branch: ...
[ Info: time: 48.497, max|u|: 1.000, CFL: 0.10, Net flux: 5.5511e-17
[ Info: time: 48.997, max|u|: 1.000, CFL: 0.10, Net flux: 4.1633e-17
[ Info: time: 49.497, max|u|: 1.000, CFL: 0.10, Net flux: 6.9389e-17
[ Info: time: 49.997, max|u|: 1.000, CFL: 0.10, Net flux: 9.7145e-17 |
|
Should we make this PR not a draft anymore, or is there a reason to keep it as a draft? It seems like something we want to merge ASAP? |
I made it a draft because I spent no time optimizing it, so it's very slow (as you can imagine due to the allocations being created every time-step). So that was my attempt as signaling that it's not ready for a full review. I'm okay with making it a full PR though. |
Are we ready to work on turning this into a proper implementation, or do we want more time to tinker? Happy to help whenever! |
Gave it some thought and I think we're ready. The actual algorithm is pretty straightforward. All we need to figure out is the software design. |
|
I read a bit more about this, and it turns out open boundary conditions often don't conserve mass, and a common way to correct is to scale the boundary values to correct so this seems reasonable. |
Okay, that's good in the sense that it matches my understanding. Could you point to some of the literature you read though? I'm having some trouble finding relevant literature for these kinds of "details". |
|
These modifications seem to slow down the simulation by a lot. The culprit seems to be the average boundary mass flux calculations. Running this simulation, which has only one radiation open boundary (and therefore needs Having 4 radiation open boundaries leads to a slowdown of ~6 times... Not sure what can be done to improve this (I'm only doing this calculation wherever there is a radiation open boundary (i.e. an OpenBoundaryCondition with a matching scheme)) |
…ns.jl into tc/mass-flux-correction
glwagner
left a comment
There was a problem hiding this comment.
I think this needs a good proofread because I found a few details to fix. Please read through. Reduce verbosity if possible, very long lines / variable names can impact the legibility of code
|
Addressed all your comments and gave it a thorough proofread afterwards, as per your suggestion. |
|
#4668 seems to have really sped up things. Here are the benchmarks for a simulation without and then with two open boundaries before #4668 was merged: julia> b_without_open
BenchmarkTools.Trial: 5785 samples with 1 evaluation per sample.
Range (min … max): 662.920 μs … 8.982 ms ┊ GC (min … max): 0.00% … 85.53%
Time (median): 871.306 μs ┊ GC (median): 0.00%
Time (mean ± σ): 861.066 μs ± 559.721 μs ┊ GC (mean ± σ): 4.45% ± 6.25%
▂▄▅▅▃ ▁▄█▆▄▂▁
▁▂▃▄▇█████▇▆▅▄▃▂▂▂▁▁▁▁▁▁▁▁▁▁▁▁▁▂▃▄████████▆▅▄▃▃▃▃▂▃▂▂▂▂▂▂▁▁▁▁ ▃
663 μs Histogram: frequency by time 1.02 ms <
Memory estimate: 731.53 KiB, allocs estimate: 2104.
julia> b_with_open
BenchmarkTools.Trial: 5104 samples with 1 evaluation per sample.
Range (min … max): 754.581 μs … 12.483 ms ┊ GC (min … max): 0.00% … 89.36%
Time (median): 972.037 μs ┊ GC (median): 0.00%
Time (mean ± σ): 975.329 μs ± 620.934 μs ┊ GC (mean ± σ): 4.54% ± 6.48%
▄███▆▄▁ ▂▅▇▇▇▇▇▃
▂▂▃▄▆████████▇▅▅▄▃▃▃▃▂▂▂▂▂▃▄▅▇█████████▆▅▄▄▅▃▃▃▃▃▃▃▃▂▂▃▂▂▂▁▂▂ ▄
755 μs Histogram: frequency by time 1.2 ms <
Memory estimate: 746.67 KiB, allocs estimate: 2382.and here are the results after it was merged: julia> b_without_open
BenchmarkTools.Trial: 6946 samples with 1 evaluation per sample.
Range (min … max): 532.364 μs … 5.758 ms ┊ GC (min … max): 0.00% … 78.44%
Time (median): 626.273 μs ┊ GC (median): 0.00%
Time (mean ± σ): 716.930 μs ± 406.932 μs ┊ GC (mean ± σ): 4.45% ± 6.98%
▁▅██▆▂ ▁▃▂
▁▁▂▂▂▂▃▅███████▆▄▃▃▂▂▂▂▁▁▁▁▁▁▁▂▂▂▂▂▂▃▃▅▇████▇▅▃▂▂▂▂▁▂▁▂▁▁▁▁▁▁ ▃
532 μs Histogram: frequency by time 910 μs <
Memory estimate: 715.31 KiB, allocs estimate: 1776.
julia> b_with_open
BenchmarkTools.Trial: 6801 samples with 1 evaluation per sample.
Range (min … max): 539.551 μs … 8.671 ms ┊ GC (min … max): 0.00% … 88.88%
Time (median): 661.378 μs ┊ GC (median): 0.00%
Time (mean ± σ): 731.322 μs ± 411.323 μs ┊ GC (mean ± σ): 4.48% ± 7.13%
▃██▄▂▁ ▁
▂▂▃▄▇██████▇▅▄▅▄▄▄▅▄▄▃▃▃▃▃▃▄▆███▆▄▄▃▃▃▂▂▃▃▂▃▂▂▂▂▂▂▂▂▁▁▁▁▁▁▁▁▁ ▃
540 μs Histogram: frequency by time 1.04 ms <
Memory estimate: 723.58 KiB, allocs estimate: 1844.The difference is now much smaller and, perhaps more importantly, both are faster 🎉 |
Recently (I think in #4578) we found that mass wasn't being exactly conserved whenever PerturbationAdvection was used as an open boundary condition. This PR (which is a draft for now) explores adding a correction to normal velocities at the boundaries right before
solve_for_pressure!()is called which enforces that no net mass enters or leaves the domain.At the moment I just focused on something that works to get started and the algorithm is not optimized at all (suggestions on how to do that are welcome) but it works by:
A MWE measuring the net mass flux into the domain produces this result without the correction:
and this result with the correction:
Notes:
cc @glwagner @xkykai @ali-ramadhan