-
Notifications
You must be signed in to change notification settings - Fork 105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add velocity functions for different equations #2086
base: main
Are you sure you want to change the base?
Conversation
Review checklistThis checklist is meant to assist creators of PRs (to let them know what reviewers will typically look for) and reviewers (to guide them in a structured review process). Items do not need to be checked explicitly for a PR to be eligible for merging. Purpose and scope
Code quality
Documentation
Testing
Performance
Verification
Created with ❤️ by the Trixi.jl community. |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #2086 +/- ##
==========================================
+ Coverage 96.34% 96.35% +0.01%
==========================================
Files 470 470
Lines 37501 37588 +87
==========================================
+ Hits 36129 36216 +87
Misses 1372 1372
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! Could you please
export velocity
in the main file- write a docstring describing
velocity
in general - maybe inequations.jl
since it is a rather general interface? - add some unit tests checking whether the correct values are returned in https://github.com/trixi-framework/Trixi.jl/blob/main/test/test_unit.jl?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot! I just have some minor comments.
Two of the GLM MHD 2D tests are failing: https://github.com/trixi-framework/Trixi.jl/actions/runs/11035734678/job/30652521365?pr=2086#step:7:1405 |
The |
@inline function velocity(u, equations::CompressibleEulerEquations2D) | ||
rho = u[1] | ||
v1 = u[2] / rho | ||
v2 = u[3] / rho | ||
return SVector(v1, v2) | ||
end | ||
|
||
@inline function velocity(u, orientation::Int, equations::CompressibleEulerEquations2D) | ||
rho = u[1] | ||
v = u[orientation + 1] / rho | ||
return v | ||
end | ||
|
||
@inline function velocity(u, normal_direction::AbstractVector, | ||
equations::CompressibleEulerEquations2D) | ||
rho = u[1] | ||
v1 = u[2] / rho | ||
v2 = u[3] / rho | ||
v = v1 * normal_direction[1] + v2 * normal_direction[2] | ||
return v | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we need three versions of this functionality? Wouldn't it be sufficient to use the one that only dispatches on the equation set and returns the SVector
of Cartesian fluxes. Then computing a particular normal direction, using orientation or a vector, could be done within the function that calls velocity
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the observation!
I updated the PR to have a general function for the velocity in normal_direction
.
However, having different versions for the orientation
is slightly cheaper, as the general velocity
function will compute velocity in all directions even though we need only one. Thus, there is a minor readability-performance trade-off and I would like your thoughts on it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see a reason for this. Checking a simple example, the compiler seems to be smart enough to optimize away the case of constant orientation
s but not with variable ones, e.g.,
julia> using StaticArrays
julia> @inline function velocity(u)
rho = u[1]
v1 = u[2] / rho
v2 = u[3] / rho
return SVector(v1, v2)
end
velocity (generic function with 1 method)
julia> @inline function velocity(u, orientation)
rho = u[1]
v = u[1 + orientation] / rho
return v
end
velocity (generic function with 2 methods)
julia> foo1(u) = velocity(u)[1]
foo1 (generic function with 1 method)
julia> foo2(u) = velocity(u, 1)
foo2 (generic function with 1 method)
julia> u = SVector(1.0, 0.1, -0.2, 1.5)
4-element SVector{4, Float64} with indices SOneTo(4):
1.0
0.1
-0.2
1.5
julia> @code_llvm debuginfo=:none foo1(u)
define double @julia_foo1_188([1 x [4 x double]]* nocapture noundef nonnull readonly align 8 dereferenceable(32) %0) #0 {
top:
%1 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 0
%2 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 1
%unbox = load double, double* %2, align 8
%unbox1 = load double, double* %1, align 8
%3 = fdiv double %unbox, %unbox1
ret double %3
}
julia> @code_llvm debuginfo=:none foo2(u)
define double @julia_foo2_201([1 x [4 x double]]* nocapture noundef nonnull readonly align 8 dereferenceable(32) %0) #0 {
top:
%1 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 0
%2 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 1
%unbox = load double, double* %2, align 8
%unbox1 = load double, double* %1, align 8
%3 = fdiv double %unbox, %unbox1
ret double %3
}
julia> bar1(u, i) = velocity(u)[i]
bar1 (generic function with 1 method)
julia> bar2(u, i) = velocity(u, i)
bar2 (generic function with 1 method)
julia> @code_llvm debuginfo=:none bar1(u, 1)
define double @julia_bar1_215([1 x [4 x double]]* nocapture noundef nonnull readonly align 8 dereferenceable(32) %0, i64 signext %1) #0 {
top:
%newstruct = alloca <2 x double>, align 16
%2 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 0
%3 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 1
%unbox1 = load double, double* %2, align 8
%4 = bitcast double* %3 to <2 x double>*
%5 = load <2 x double>, <2 x double>* %4, align 8
%6 = insertelement <2 x double> poison, double %unbox1, i64 0
%7 = shufflevector <2 x double> %6, <2 x double> poison, <2 x i32> zeroinitializer
%8 = fdiv <2 x double> %5, %7
store <2 x double> %8, <2 x double>* %newstruct, align 16
%9 = add i64 %1, -1
%boundscheck = icmp ult i64 %9, 2
br i1 %boundscheck, label %pass, label %fail
fail: ; preds = %top
%10 = bitcast <2 x double>* %newstruct to i8*
call void @ijl_bounds_error_unboxed_int(i8* %10, {}* nonnull inttoptr (i64 4803813760 to {}*), i64 %1)
unreachable
pass: ; preds = %top
%11 = getelementptr inbounds <2 x double>, <2 x double>* %newstruct, i64 0, i64 %9
%unbox4 = load double, double* %11, align 8
ret double %unbox4
}
julia> @code_llvm debuginfo=:none bar2(u, 1)
define double @julia_bar2_217([1 x [4 x double]]* nocapture noundef nonnull readonly align 8 dereferenceable(32) %0, i64 signext %1) #0 {
top:
%boundscheck = icmp ult i64 %1, 4
br i1 %boundscheck, label %pass, label %fail
fail: ; preds = %top
%2 = add i64 %1, 1
%3 = bitcast [1 x [4 x double]]* %0 to i8*
call void @ijl_bounds_error_unboxed_int(i8* nonnull %3, {}* nonnull inttoptr (i64 4766129648 to {}*), i64 %2)
unreachable
pass: ; preds = %top
%4 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 0
%5 = getelementptr inbounds [1 x [4 x double]], [1 x [4 x double]]* %0, i64 0, i64 0, i64 %1
%unbox = load double, double* %5, align 8
%unbox1 = load double, double* %4, align 8
%6 = fdiv double %unbox, %unbox1
ret double %6
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What do you think, @andrewwinters5000?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you fine with this, @andrewwinters5000?
Trixi.jl
has functions to computedensity
andpressure
for all equations, but most equations do not have those forvelocity
. This PR adds those functions toCompressibleEuler
,MultiComponentEuler
,PolytropicEuler
,IdealMHD
,ShallowWater
equations.