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

Add force_injection! #199

Merged
merged 5 commits into from
Jan 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 30 additions & 23 deletions .github/workflows/Format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,26 @@ on:

permissions:
contents: read

# needed for julia-actions/cache to delete old caches
actions: write

# needed for googleapis/code-suggester
pull-requests: write

jobs:
runic:
runs-on: ubuntu-latest
if: github.event.pull_request.draft == false
steps:
- uses: actions/checkout@v4
- name: Check out repository
uses: actions/checkout@v4
with:
ref: ${{github.event.pull_request.head.ref}}
repository: ${{github.event.pull_request.head.repo.full_name}}
fetch-depth: 0

- name: Add upstream remote
run: |
git remote add upstream https://github.com/${{ github.repository }}
git fetch upstream

- name: Setup Julia
uses: julia-actions/setup-julia@v2
with:
Expand All @@ -42,27 +44,27 @@ jobs:
- name: Run Runic
id: runic
run: |
set +e
MERGE_BASE=$(git merge-base upstream/${{ github.base_ref }} HEAD) || exit 1
DIFF=$(git runic --diff $MERGE_BASE)
EXIT_CODE=$?
echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
echo "diff<<EOF" >> $GITHUB_OUTPUT
echo "$DIFF" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
# if Runic failed, bail out
[ $EXIT_CODE -eq 2 ] && exit 1 || exit 0
set +e
MERGE_BASE=$(git merge-base upstream/${{ github.base_ref }} HEAD) || exit 1
DIFF=$(git runic --diff $MERGE_BASE)
EXIT_CODE=$?

echo "exit_code=$EXIT_CODE" >> $GITHUB_OUTPUT
echo "diff<<EOF" >> $GITHUB_OUTPUT
echo "$DIFF" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT

# if Runic failed, bail out
[ $EXIT_CODE -eq 2 ] && exit 1 || exit 0

- name: Find comment
uses: peter-evans/find-comment@v3
id: find-comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: 'github-actions[bot]'
body-includes: '<!-- runic-format-summary -->'

- name: Comment formatting suggestions
if: steps.runic.outputs.exit_code == 1
uses: peter-evans/create-or-update-comment@v4
Expand All @@ -81,10 +83,10 @@ jobs:
```diff
${{ steps.runic.outputs.diff }}
```
</details>
edit-mode: replace

</details>
edit-mode: replace

- name: Update stale comment
if: steps.runic.outputs.exit_code == 0 && steps.find-comment.outputs.comment-id
uses: peter-evans/create-or-update-comment@v4
Expand All @@ -96,3 +98,8 @@ jobs:

Your PR no longer requires formatting changes. Thank you for your contribution!
edit-mode: replace

# XXX: if Github ever supports allow-failure (actions/runner#2347)
#- name: Propagate exit code
# run: |
# exit ${{ steps.runic.outputs.exit_code }}
14 changes: 14 additions & 0 deletions ext/JustPICAMDGPUExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,13 @@ module _2D
return inject_particles!(particles, args, grid)
end

function JustPIC._2D.force_injection!(particles::Particles{AMDGPUBackend}, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {N}
force_injection!(particles, p_new, fields, values)
return nothing
end

JustPIC._2D.force_injection!(particles::Particles{AMDGPUBackend}, p_new) = force_injection!(particles, p_new, (), ())

function JustPIC._2D.inject_particles_phase!(
particles::Particles{AMDGPUBackend}, particles_phases, args, fields, grid::NTuple{N}
) where {N}
Expand Down Expand Up @@ -674,6 +681,13 @@ module _3D
return inject_particles!(particles, args, grid)
end

function JustPIC._3D.force_injection!(particles::Particles{AMDGPUBackend}, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {N}
force_injection!(particles, p_new, fields, values)
return nothing
end

JustPIC._3D.force_injection!(particles::Particles{AMDGPUBackend}, p_new) = force_injection!(particles, p_new, (), ())

function JustPIC._3D.inject_particles_phase!(
particles::Particles{AMDGPUBackend}, particles_phases, args, fields, grid::NTuple{N}
) where {N}
Expand Down
14 changes: 14 additions & 0 deletions ext/JustPICCUDAExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,13 @@ module _2D
return inject_particles!(particles, args, grid)
end

function JustPIC._2D.force_injection!(particles::Particles{CUDABackend}, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {N}
force_injection!(particles, p_new, fields, values)
return nothing
end

JustPIC._2D.force_injection!(particles::Particles{CUDABackend}, p_new) = force_injection!(particles, p_new, (), ())

function JustPIC._2D.inject_particles_phase!(
particles::Particles{CUDABackend}, particles_phases, args, fields, grid::NTuple{N}
) where {N}
Expand Down Expand Up @@ -644,6 +651,13 @@ module _3D
return nothing
end

function JustPIC._3D.force_injection!(particles::Particles{CUDABackend}, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {N}
force_injection!(particles, p_new, fields, values)
return nothing
end

JustPIC._3D.force_injection!(particles::Particles{CUDABackend}, p_new) = force_injection!(particles, p_new, (), ())

function JustPIC._3D.move_particles!(
particles::Particles{CUDABackend}, grid::NTuple{N}, args
) where {N}
Expand Down
58 changes: 58 additions & 0 deletions src/Particles/forced_injection.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@

"""
force_injection!(particles::Particles{Backend}, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {Backend, N}

Forcefully injects new particles into the `particles` object. This function modifies the `particles` object in place.

# Arguments
- `particles::Particles{Backend}`: The particles object to be modified.
- `p_new`: The new particles to be injected.
- `fields::NTuple{N, Any}`: A tuple containing the fields to be updated.
- `values::NTuple{N, Any}`: A tuple containing the values corresponding to the fields.

# Returns
- Nothing. This function modifies the `particles` object in place.
"""
function force_injection!(particles::Particles{Backend}, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {Backend, N}
(; coords, index) = particles;
ni = size(index)
@parallel (@idx ni) force_injection!(coords, index, p_new, fields, values)
return nothing
end

"""
force_injection!(particles::Particles{Backend}, p_new) where {Backend}

Forces the injection of new particles into the existing `particles` collection. This function modifies the `particles` in place by adding the new particles specified in `p_new`.

# Arguments
- `particles::Particles{Backend}`: The existing collection of particles to which new particles will be added. The type of backend is specified by the `Backend` parameter.
- `p_new`: The new particles to be injected into the existing collection.
"""
force_injection!(particles::Particles{Backend}, p_new) where {Backend} = force_injection!(particles, p_new, (), ())


@parallel_indices (I...) function force_injection!(coords::NTuple{2}, index, p_new, fields::NTuple{N, Any}, values::NTuple{N, Any}) where {N}

# check whether there are new particles to inject in the ij-th cell
if !isnan(p_new[I..., begin])
c = 0 # helper counter
# iterate over particles in the cell
for ip in cellaxes(index)
c += 1
c > cellnum(index) && continue
doskip(index, ip, I...) || continue
pᵢ = p_new[I..., c]
@index coords[1][ip, I...] = pᵢ[1]
@index coords[2][ip, I...] = pᵢ[2]
@index index[ip, I...] = true

# force fields to have a given value
for (value, field) in zip(values, fields)
@index field[ip, I...] = value
end
end
end

return nothing
end
3 changes: 3 additions & 0 deletions src/common.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ export advection!, advection_LinP!, advection_MQS!
include("Particles/injection.jl")
export check_injection, inject_particles!, inject_particles_phase!, clean_particles!

include("Particles/forced_injection.jl")
export force_injection!

## MARKER CHAIN RELATED FILES

include("MarkerChain/init.jl")
Expand Down
Loading