-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from JuliaGeodynamics/bk-chmy
Integration with Chmy.jl
- Loading branch information
Showing
14 changed files
with
589 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
```@meta | ||
EditURL = "../../../tutorials/Tutorial_Chmy_MPI.jl" | ||
``` | ||
|
||
# Create an initial model setup for Chmy and run it in parallel | ||
|
||
## Aim | ||
In this tutorial, your will learn how to use [Chmy](https://github.com/PTsolvers/Chmy.jl) to perform a 2D diffusion simulation | ||
on one or multiple CPU's or GPU's. | ||
`Chmy` is a package that allows you to specify grids and fields and create finite difference simulations | ||
|
||
## 1. Load Chmy and required packages | ||
|
||
```julia | ||
using Chmy, Chmy.Architectures, Chmy.Grids, Chmy.Fields, Chmy.BoundaryConditions, Chmy.GridOperators, Chmy.KernelLaunch | ||
using KernelAbstractions | ||
using Printf | ||
using CairoMakie | ||
using GeophysicalModelGenerator | ||
``` | ||
|
||
In case you want to use GPU's, you need to sort out whether you have AMD or NVIDIA GPU's | ||
and load the package accordingly: | ||
using AMDGPU | ||
AMDGPU.allowscalar(false) | ||
using CUDA | ||
CUDA.allowscalar(false) | ||
|
||
To run this in parallel you need to load this: | ||
|
||
```julia | ||
using Chmy.Distributed | ||
using MPI | ||
MPI.Init() | ||
``` | ||
|
||
## 2. Define computational routines | ||
You need to specify compute kernel for the gradients: | ||
|
||
```julia | ||
@kernel inbounds = true function compute_q!(q, C, χ, g::StructuredGrid, O) | ||
I = @index(Global, NTuple) | ||
I = I + O | ||
q.x[I...] = -χ * ∂x(C, g, I...) | ||
q.y[I...] = -χ * ∂y(C, g, I...) | ||
end | ||
``` | ||
|
||
You need to specify compute kernel to update the concentration | ||
|
||
```julia | ||
@kernel inbounds = true function update_C!(C, q, Δt, g::StructuredGrid, O) | ||
I = @index(Global, NTuple) | ||
I = I + O | ||
C[I...] -= Δt * divg(q, g, I...) | ||
end | ||
``` | ||
|
||
And a main function is required: | ||
|
||
```julia | ||
@views function main(backend=CPU(); nxy_l=(126, 126)) | ||
arch = Arch(backend, MPI.COMM_WORLD, (0, 0)) | ||
topo = topology(arch) | ||
me = global_rank(topo) | ||
|
||
# geometry | ||
dims_l = nxy_l | ||
dims_g = dims_l .* dims(topo) | ||
grid = UniformGrid(arch; origin=(-2, -2), extent=(4, 4), dims=dims_g) | ||
launch = Launcher(arch, grid, outer_width=(16, 8)) | ||
|
||
##@info "mpi" me grid | ||
|
||
nx, ny = dims_g | ||
# physics | ||
χ = 1.0 | ||
# numerics | ||
Δt = minimum(spacing(grid))^2 / χ / ndims(grid) / 2.1 | ||
# allocate fields | ||
C = Field(backend, grid, Center()) | ||
P = Field(backend, grid, Center(), Int32) # phases | ||
|
||
q = VectorField(backend, grid) | ||
C_v = (me==0) ? KernelAbstractions.zeros(CPU(), Float64, size(interior(C)) .* dims(topo)) : nothing | ||
|
||
# Use the `GeophysicalModelGenerator` to set the initial conditions. Note that | ||
# you have to call this for a `Phases` and a `Temp` grid, which we call `C` here. | ||
add_box!(P,C,grid, xlim=(-1.0,1.0), zlim=(-1.0,1.0), phase=ConstantPhase(4), T=ConstantTemp(400)) | ||
|
||
# set BC's and updates the halo: | ||
bc!(arch, grid, C => Neumann(); exchange=C) | ||
|
||
# visualisation | ||
fig = Figure(; size=(400, 320)) | ||
ax = Axis(fig[1, 1]; aspect=DataAspect(), xlabel="x", ylabel="y", title="it = 0") | ||
plt = heatmap!(ax, centers(grid)..., interior(C) |> Array; colormap=:turbo) | ||
Colorbar(fig[1, 2], plt) | ||
# action | ||
nt = 100 | ||
for it in 1:nt | ||
(me==0) && @printf("it = %d/%d \n", it, nt) | ||
launch(arch, grid, compute_q! => (q, C, χ, grid)) | ||
launch(arch, grid, update_C! => (C, q, Δt, grid); bc=batch(grid, C => Neumann(); exchange=C)) | ||
end | ||
KernelAbstractions.synchronize(backend) | ||
gather!(arch, C_v, C) | ||
if me == 0 | ||
fig = Figure(; size=(400, 320)) | ||
ax = Axis(fig[1, 1]; aspect=DataAspect(), xlabel="x", ylabel="y", title="it = 0") | ||
plt = heatmap!(ax, C_v; colormap=:turbo) # how to get the global grid for axes? | ||
Colorbar(fig[1, 2], plt) | ||
save("out_gather_$nx.png", fig) | ||
end | ||
return | ||
end | ||
``` | ||
|
||
In the code above, the part that calls `GMG` is: | ||
|
||
```julia | ||
add_box!(P,C,grid, xlim=(-1.0,1.0), zlim=(-1.0,1.0), phase=ConstantPhase(4), T=ConstantTemp(400)) | ||
``` | ||
which works just like any of the other GMG function. | ||
|
||
## 3. Run the simulation on one CPU machine or GPU card: | ||
|
||
Running the code on the CPU is done with this: | ||
|
||
```julia | ||
n = 128 | ||
main(; nxy_l=(n, n) .- 2) | ||
``` | ||
|
||
If you instead want to run this on AMD or NVIDIA GPU's do this: | ||
|
||
```julia | ||
# main(ROCBackend(); nxy_l=(n, n) .- 2) | ||
# main(CUDABackend(); nxy_l=(n, n) .- 2) | ||
``` | ||
|
||
And we need to finalize the simulation with | ||
|
||
```julia | ||
MPI.Finalize() | ||
``` | ||
|
||
## 4. Run the simulation on an MPI-parallel machine | ||
If you want to run this on multiple cores, you will need to setup the [MPI.jl]() package, | ||
such that `mpiexecjl` is created on the command line. | ||
|
||
You can than run it with: | ||
mpiexecjl -n 4 --project=. julia Tutorial_Chmy_MPI.jl | ||
|
||
The full file can be downloaded [here](../../../tutorials/Tutorial_Chmy_MPI.jl) | ||
|
||
--- | ||
|
||
*This page was generated using [Literate.jl](https://github.com/fredrikekre/Literate.jl).* | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
#helper functions to make GMG work with Chmy grids and fields | ||
module Chmy_utils | ||
|
||
using Chmy, Chmy.Grids, Chmy.Fields | ||
|
||
import GeophysicalModelGenerator: create_CartGrid, CartGrid, CartData | ||
import GeophysicalModelGenerator: add_box!, add_sphere!, add_ellipsoid!, add_cylinder! | ||
import GeophysicalModelGenerator: add_layer!, add_polygon!, add_slab!, add_stripes!, add_volcano! | ||
import GeophysicalModelGenerator: above_surface, below_surface | ||
|
||
println("Loading Chmy-GMG tools") | ||
|
||
""" | ||
CartGrid = create_CartGrid(grid::StructuredGrid; ylevel=0.0) | ||
Creates a GMG `CartGrid` data structure from a `Chmy` grid object | ||
""" | ||
function create_CartGrid(grid::StructuredGrid; ylevel=0.0) | ||
|
||
coord1D = Vector.(coords(grid, Vertex())) | ||
coord1D_cen = Vector.(coords(grid, Center())) | ||
N = length.(coord1D) | ||
L = extent(grid, Vertex()) | ||
X₁ = origin(grid, Vertex()) | ||
Δ = spacing(grid) | ||
ConstantΔ = false; | ||
if isa(grid, UniformGrid) | ||
ConstantΔ = true | ||
end | ||
if ndims(grid)==2 | ||
# we need a special treatment of this, as all GMG routines work with 3D coordinates | ||
X₁ = (X₁[1], ylevel, X₁[2]) | ||
L = (L[1], 0.0, L[2]) | ||
Δ = (Δ[1], 0.0, Δ[2]) | ||
N = (N[1],1,N[2]) | ||
coord1D = (coord1D[1], [0.0], coord1D[2]) | ||
coord1D_cen = (coord1D_cen[1], [0.0], coord1D_cen[2]) | ||
end | ||
Xₙ = X₁ .+ L | ||
|
||
|
||
return CartGrid(ConstantΔ,N,Δ,L,X₁,Xₙ,coord1D, coord1D_cen) | ||
end | ||
|
||
# all functions to be ported | ||
function_names = (:add_box!, :add_sphere!, :add_ellipsoid!, :add_cylinder!, :add_layer!, :add_polygon!, :add_slab!, :add_stripes!, :add_volcano!) | ||
|
||
for fn in function_names | ||
|
||
@eval begin | ||
""" | ||
$($fn)( Phase::Field, | ||
Temp::Field, | ||
Grid::StructuredGrid; # required input | ||
kwargs...) | ||
Sets `$($fn)` function for `Chmy` fields and grids. | ||
""" | ||
function $fn( Phase::Field, | ||
Temp::Field, | ||
Grid::StructuredGrid; # required input | ||
kwargs...) | ||
|
||
CartGrid = create_CartGrid(Grid) | ||
|
||
cell = false | ||
if all(location(Phase).==Center()) | ||
cell = true | ||
end | ||
|
||
return ($fn)(Phase, Temp, CartGrid; cell=cell, kwargs...) | ||
end | ||
end | ||
|
||
end | ||
|
||
|
||
# all functions to be ported | ||
function_names = (:above_surface, :below_surface) | ||
|
||
for fn in function_names | ||
|
||
@eval begin | ||
""" | ||
$($fn)( Grid::StructuredGrid, field::Field, DataSurface_Cart::CartData; kwargs...) | ||
Sets `$($fn)` function for `Chmy` grids and the field `field` which can be either on vertices or centers | ||
""" | ||
function $fn( Grid::StructuredGrid, | ||
field::Field, | ||
DataSurface_Cart::CartData; | ||
kwargs...) | ||
|
||
CartGrid = create_CartGrid(Grid) | ||
|
||
cell = false | ||
if all(location(field).==Center()) | ||
cell = true | ||
end | ||
|
||
return ($fn)(CartGrid, DataSurface_Cart; cell=cell, kwargs...) | ||
end | ||
end | ||
|
||
end | ||
|
||
|
||
end # end of module |
Oops, something went wrong.
54377c0
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.
@JuliaRegistrator register
54377c0
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.
Registration pull request created: JuliaRegistries/General/116914
Tip: Release Notes
Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.
To add them here just re-invoke and the PR will be updated.
Tagging
After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.
This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via: