Skip to content
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

Viscous force and memory allocation #113

Merged
merged 15 commits into from
Jun 21, 2024

Conversation

marinlauber
Copy link
Member

This implements a simple viscous force routine. Following the structure of the routine for the pressure force, which needs to include the time of the simulation to make sure we are measuring at the correct time, see #110;

Also, it is allocating like crazy, same as for the pressure force routine.

This is a work in progress.

@marinlauber
Copy link
Member Author

This fixes #110 by providing a higher-level interface function for the force routines. It also adds a moment computation routine. I will add some proper tests soon.

@marinlauber marinlauber marked this pull request as ready for review June 11, 2024 14:37
@marinlauber
Copy link
Member Author

This is ready for review @weymouth @b-fg. I have added some examples I had lying around that show some of the capabilities added recently, but they didn't have an example.

I also removed the type argument in loc and related functions.

src/util.jl Outdated
@@ -121,8 +121,8 @@ using StaticArrays
Location in space of the cell at CartesianIndex `I` at face `i`.
Using `i=0` returns the cell center s.t. `loc = I`.
"""
@inline loc(i,I::CartesianIndex{N},T=Float64) where N = SVector{N,T}(I.I .- 1.5 .- 0.5 .* δ(i,I).I)
@inline loc(Ii::CartesianIndex,T=Float64) = loc(last(Ii),Base.front(Ii),T)
@inline loc(i,I::CartesianIndex{N}) where N = SVector{N}(I.I .- 3//2 .- 1//2 .* δ(i,I).I)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked this works on GPU? It was not working for me on the AD branch.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I have not

Copy link
Member

@b-fg b-fg Jun 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it probably won't. We are currently using 1.5f0... because otherwise it doesn't work in GPUs.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will revert to the type conversion of the SVector for now. We can sort this out later.

src/util.jl Outdated Show resolved Hide resolved
src/Metrics.jl Outdated Show resolved Hide resolved
src/Metrics.jl Outdated Show resolved Hide resolved
@b-fg
Copy link
Member

b-fg commented Jun 17, 2024

There are currently no tests for ∮τnds nor ∇²u.

@marinlauber
Copy link
Member Author

I can add a test for that!

@b-fg
Copy link
Member

b-fg commented Jun 21, 2024

All tests passing locally on CPU and GPU.

@marinlauber the modifications I did to your previous changes (up to bb99599) make all the metrics work for GPU now, since that implementation was only working for CPU. The allocations are very good as well (there are just a few more, but the routines are also a bit faster):

# Tests using SIMD execution (single-thread mode) and Array backend:

# Previous (bb99599)
# Pressure force: 0.040567 seconds (4 CPU allocations: 176 bytes)
# Viscous force: 0.343564 seconds (4 CPU allocations: 176 bytes)

# Current changes (d01f68c)
# Pressure force: 0.035714 seconds (39 CPU allocations: 1.562 KiB)
# Viscous force: 0.310422 seconds (38 CPU allocations: 1.516 KiB)

The only remaining thing to discuss is the examples added. Only the TwoD_cylinderVIV.jl had to be changed to comply with this PR, and the other examples do not actually even use any force/moment metric. I suggest we remove those changes from this PR and create a new one just for examples. Then this PR should be ready to merge.

@b-fg b-fg requested a review from weymouth June 21, 2024 14:22
@b-fg
Copy link
Member

b-fg commented Jun 21, 2024

@weymouth can you please give it a look before we merge to master?

…examples.

These examples do not use force metrics so they should be submitted in a different PR.
@marinlauber
Copy link
Member Author

Are we ok with discontinuing the ∮nds function?

Copy link
Collaborator

@weymouth weymouth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, except for that one default, which I can fix.

src/AutoBody.jl Outdated
@@ -112,6 +112,11 @@ function measure(a::Bodies,x,t)
sdf, map, _ = sdf_map_d(a.bodies,a.ops,x,t)
measure(sdf,map,x,t)
end
# measures the distance, normal, and velocity accuratly for sdf≤1
function measure_fast(body::AutoBody,x,t)
abs(body.sdf(x,t))>1 && return zero(eltype(x)),zero(x),zero(x)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why doesn't this return d, zero(x), zero(x)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's an error, should return d.

@b-fg
Copy link
Member

b-fg commented Jun 21, 2024

Are we ok with discontinuing the ∮nds function?

Yeap, we will just keep the high-level functions since it was difficult to do multiple dispatch of ∮nds applied to 2D/3D scalar/vector fields.

@b-fg
Copy link
Member

b-fg commented Jun 21, 2024

Looks good, except for that one default, which I can fix.

Which one?

@b-fg
Copy link
Member

b-fg commented Jun 21, 2024

The promote_type works but makes it a tiny bit slower even if there are less allocations:

# Tests using SIMD execution (single-thread mode) and Array backend:

# Previous changes (d01f68c)
# Pressure force: 0.035714 seconds (39 CPU allocations: 1.562 KiB)
# Viscous force: 0.310422 seconds (38 CPU allocations: 1.516 KiB)

# Current changes (0f70067)
# Pressure force: 0.142078 seconds (19 CPU allocations: 800 bytes)
# Viscous force: 0.402344 seconds (19 CPU allocations: 768 bytes)

The price to pay for having these functions AD-compatible is fine though (plus with KA in CPU or GPU the difference is very small!) :)

@b-fg b-fg merged commit 36dc237 into WaterLily-jl:master Jun 21, 2024
21 checks passed
weymouth referenced this pull request Jul 22, 2024
Make Simulation mutable and loc(T=Float32)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants