Skip to content

Commit

Permalink
first implementation for 2D with parallelstencil, not working
Browse files Browse the repository at this point in the history
  • Loading branch information
Iddingsite committed Sep 25, 2023
1 parent 859fbc3 commit 84e396d
Show file tree
Hide file tree
Showing 27 changed files with 7,267 additions and 172 deletions.
2 changes: 2 additions & 0 deletions LocalPreferences.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[DiffusionGarnet]
backend = "Threads_Float64_2D"
452 changes: 333 additions & 119 deletions Manifest.toml

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@ version = "1.0.0-DEV"

[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
DiffEqBase = "2b5f629d-d688-5b77-993f-72d75c75574e"
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
Logging = "56ddb016-857b-54e1-b83d-db4d58db5568"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
ParallelStencil = "94395366-693c-11ea-3b26-d9b7aac5d958"
Parameters = "d96e819e-fc66-5662-9728-84c9c7592b0a"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Preferences = "21216c6a-2e73-6563-6e65-726566657250"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
Requires = "ae029012-a4dd-5104-9daa-d747884805df"
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
Symbolics = "0c5d862f-8b57-4792-8d23-62f2024744c7"
TerminalLoggers = "5d786b92-1e48-4d6f-9151-6b4477ca9bed"
Expand Down
924 changes: 924 additions & 0 deletions examples/2D/Contour_HR.txt

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions examples/2D/Contour_LR.txt

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions examples/2D/Xalm.txt

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions examples/2D/Xalm_LR.txt

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions examples/2D/Xgrs.txt

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions examples/2D/Xgrs_LR.txt

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions examples/2D/Xprp.txt

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions examples/2D/Xprp_LR.txt

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions examples/2D/Xsps.txt

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions examples/2D/Xsps_LR.txt

Large diffs are not rendered by default.

924 changes: 924 additions & 0 deletions examples/2D/position_Grt.txt

Large diffs are not rendered by default.

99 changes: 99 additions & 0 deletions examples/2D/position_Grt_LR.txt

Large diffs are not rendered by default.

Binary file added examples/Spherical/Grt_Spherical+1D.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 41 additions & 5 deletions src/DiffusionGarnet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,68 @@ module DiffusionGarnet
using Reexport

@reexport using Plots
@reexport using OrdinaryDiffEq
@reexport using BenchmarkTools
@reexport using Symbolics
@reexport using Parameters
@reexport using Unitful
@reexport using DelimitedFiles
@reexport using LinearAlgebra
@reexport using DiffEqCallbacks
# @reexport using ParallelStencil
@reexport using Logging: global_logger
@reexport using TerminalLoggers: TerminalLogger
@reexport using ParallelStencil

using OrdinaryDiffEq
using Symbolics
using ParallelStencil
using Preferences



# initialise ParallelStencil (Thx AlbertDeMontserrat!)
function set_backend(new_backend::String)
if !(
new_backend
("Threads_Float64_2D", "Threads_Float32_2D", "CUDA_Float64_2D", "CUDA_Float32_2D", "Threads_Float64_3D", "Threads_Float32_3D", "CUDA_Float64_3D", "CUDA_Float32_3D")
)
throw(ArgumentError("Invalid backend: \"$(new_backend)\""))
end

# Set it in our runtime values, as well as saving it to disk
@set_preferences!("backend" => new_backend)
@info("New backend set; restart your Julia session for this change to take effect!")
end

const backend = @load_preference("backend", "Threads_Float64_2D")

export backend, set_backend

function __init__()
# initialise global logger for OrdinaryDiffEq
global_logger(TerminalLogger())

# @require ParallelStencil = "94395366-693c-11ea-3b26-d9b7aac5d958" include("Discretisation/2D/semi_discretisation_2D.jl")
end


let
s = split(backend, "_")
device = s[1]
precision = s[2]
dimension = parse(Int, s[3][1])
@eval begin
@init_parallel_stencil($(Symbol(device)), $(Symbol(precision)), $dimension)
end
end

include("input/initialconditions.jl")
include("callbacks/update_diffusion_coef_TP.jl")
include("Discretisation/1D/semi_discretisation_1D.jl")
include("Discretisation/2D/semi_discretisation_2D.jl")
include("Discretisation/Spherical/semi_discretisation_spherical.jl")
include("simulate/simulate.jl")

export InitialConditions1D, InitialConditions2D, InitialConditions3D, InitialConditionsSpherical
export D_ini!, Domain
export semi_discretisation_diffusion_1D, semi_discretisation_diffusion_spherical
export semi_discretisation_diffusion_1D, semi_discretisation_diffusion_spherical, semi_discretisation_diffusion_2D, Diffusion_coef_2D!, stencil_diffusion_2D!
export simulate
export update_diffusion_coef

Expand Down
164 changes: 164 additions & 0 deletions src/Discretisation/2D/semi_discretisation_2D.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@

import Base.@propagate_inbounds

@parallel_indices (iy, ix) function Diffusion_coef_2D!(DMgMg, DMgFe, DMgMn, DFeMg, DFeFe, DFeMn, DMnMg, DMnFe, DMnMn, CMg, CFe ,CMn, D0, D_charact, grt_position)

@propagate_inbounds @inline sum_D(CMg, CFe, CMn, D0, ix, iy) = D0[1] * CMg[iy, ix] + D0[2] * CFe[iy, ix] + D0[3] * CMn[iy, ix] +
D0[4] * (1 - CMg[iy, ix] - CFe[iy, ix] - CMn[iy, ix])

if ix>1 && ix<size(DMgMg,2) && iy>1 && iy<size(DMgMg,1)
if grt_position[iy,ix] == 1.0
DMgMg[iy,ix] = (D0[1] - D0[1] * CMg[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[1] - D0[end])) / D_charact
DMgFe[iy,ix] = ( - D0[1] * CMg[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[2] - D0[end])) / D_charact
DMgMn[iy,ix] = ( - D0[1] * CMg[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[3] - D0[end])) / D_charact
DFeMg[iy,ix] = ( - D0[2] * CFe[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[1] - D0[end])) / D_charact
DFeFe[iy,ix] = (D0[2] - D0[2] * CFe[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[2] - D0[end])) / D_charact
DFeMn[iy,ix] = ( - D0[2] * CFe[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[3] - D0[end])) / D_charact
DMnMg[iy,ix] = ( - D0[3] * CMn[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[1] - D0[end])) / D_charact
DMnFe[iy,ix] = ( - D0[3] * CMn[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[2] - D0[end])) / D_charact
DMnMn[iy,ix] = (D0[3] - D0[3] * CMn[iy,ix] / sum_D(CMg,CFe,CMn,D0,ix,iy) * (D0[3] - D0[end])) / D_charact
else
DMgMg[iy,ix] = 0.0
DMgFe[iy,ix] = 0.0
DMgMn[iy,ix] = 0.0
DFeMg[iy,ix] = 0.0
DFeFe[iy,ix] = 0.0
DFeMn[iy,ix] = 0.0
DMnMg[iy,ix] = 0.0
DMnFe[iy,ix] = 0.0
DMnMn[iy,ix] = 0.0
end
end

return
end


@parallel_indices (iy, ix) function stencil_diffusion_2D!(dtCMg, dtCFe, dtCMn, CMg, CFe ,CMn, DMgMg, DMgFe, DMgMn, DFeMg, DFeFe, DFeMn, DMnMg, DMnFe, DMnMn, position_Grt, Grt_boundaries, Δxad_, Δyad_)

@propagate_inbounds @inline av_D_x(D, ix, iy) = 0.5 * (D[iy,ix] + D[iy,ix+1])
@propagate_inbounds @inline av_D_y(D, ix, iy) = 0.5 * (D[iy,ix] + D[iy+1,ix])
@propagate_inbounds @inline qx(D, C, ix, iy, Δxad_) = av_D_x(D, ix, iy) * (C[iy,ix+1]-C[iy,ix]) * Δxad_
@propagate_inbounds @inline qy(D, C, ix, iy, Δyad_) = av_D_y(D, ix, iy) * (C[iy+1,ix]-C[iy,ix]) * Δyad_


# iterate inside the arrays
if ix>1 && ix<size(dtCMg,2) && iy>1 && iy<size(dtCMg,1)
if position_Grt[iy,ix] == 1.0
dtCMg[iy,ix] = (qx(DMgMg,CMg,ix,iy,Δxad_) - qx(DMgMg,CMg,ix-1,iy,Δxad_)) * Δxad_ +
(qx(DMgFe,CFe,ix,iy,Δxad_) - qx(DMgFe,CFe,ix-1,iy,Δxad_)) * Δxad_ +
(qx(DMgMn,CMn,ix,iy,Δxad_) - qx(DMgMn,CMn,ix-1,iy,Δxad_)) * Δxad_ +
(qy(DMgMg,CMg,ix,iy,Δyad_) - qy(DMgMg,CMg,ix,iy-1,Δyad_)) * Δyad_ +
(qy(DMgFe,CFe,ix,iy,Δyad_) - qy(DMgFe,CFe,ix,iy-1,Δyad_)) * Δyad_ +
(qy(DMgMn,CMn,ix,iy,Δyad_) - qy(DMgMn,CMn,ix,iy-1,Δyad_)) * Δyad_
dtCFe[iy,ix] = (qx(DFeMg,CMg,ix,iy,Δxad_) - qx(DFeMg,CMg,ix-1,iy,Δxad_)) * Δxad_ +
(qx(DFeFe,CFe,ix,iy,Δxad_) - qx(DFeFe,CFe,ix-1,iy,Δxad_)) * Δxad_ +
(qx(DFeMn,CMn,ix,iy,Δxad_) - qx(DFeMn,CMn,ix-1,iy,Δxad_)) * Δxad_ +
(qy(DFeMg,CMg,ix,iy,Δyad_) - qy(DFeMg,CMg,ix,iy-1,Δyad_)) * Δyad_ +
(qy(DFeFe,CFe,ix,iy,Δyad_) - qy(DFeFe,CFe,ix,iy-1,Δyad_)) * Δyad_ +
(qy(DFeMn,CMn,ix,iy,Δyad_) - qy(DFeMn,CMn,ix,iy-1,Δyad_)) * Δyad_
dtCMn[iy,ix] = (qx(DMnMg,CMg,ix,iy,Δxad_) - qx(DMnMg,CMg,ix-1,iy,Δxad_)) * Δxad_ +
(qx(DMnFe,CFe,ix,iy,Δxad_) - qx(DMnFe,CFe,ix-1,iy,Δxad_)) * Δxad_ +
(qx(DMnMn,CMn,ix,iy,Δxad_) - qx(DMnMn,CMn,ix-1,iy,Δxad_)) * Δxad_ +
(qy(DMnMg,CMg,ix,iy,Δyad_) - qy(DMnMg,CMg,ix,iy-1,Δyad_)) * Δyad_ +
(qy(DMnFe,CFe,ix,iy,Δyad_) - qy(DMnFe,CFe,ix,iy-1,Δyad_)) * Δyad_ +
(qy(DMnMn,CMn,ix,iy,Δyad_) - qy(DMnMn,CMn,ix,iy-1,Δyad_)) * Δyad_
# first order Neumann if inclusions
# left
if position_Grt[iy,ix-1] == 0.0
dtCMg[iy,ix] -= - qx(DMgMg,CMg,ix-1,iy,Δxad_) * Δxad_ -
qx(DMgFe,CFe,ix-1,iy,Δxad_) * Δxad_ -
qx(DMgMn,CMn,ix-1,iy,Δxad_) * Δxad_
dtCFe[iy,ix] -= - qx(DFeMg,CMg,ix-1,iy,Δxad_) * Δxad_ -
qx(DFeFe,CFe,ix-1,iy,Δxad_) * Δxad_ -
qx(DFeMn,CMn,ix-1,iy,Δxad_) * Δxad_
dtCMn[iy,ix] -= - qx(DMnMg,CMg,ix-1,iy,Δxad_) * Δxad_ -
qx(DMnFe,CFe,ix-1,iy,Δxad_) * Δxad_ -
qx(DMnMn,CMn,ix-1,iy,Δxad_) * Δxad_
end
# right
if position_Grt[iy,ix+1] == 0.0
dtCMg[iy,ix] -= qx(DMgMg,CMg,ix,iy,Δxad_) * Δxad_ +
qx(DMgFe,CFe,ix,iy,Δxad_) * Δxad_ +
qx(DMgMn,CMn,ix,iy,Δxad_) * Δxad_
dtCFe[iy,ix] -= qx(DFeMg,CMg,ix,iy,Δxad_) * Δxad_ +
qx(DFeFe,CFe,ix,iy,Δxad_) * Δxad_ +
qx(DFeMn,CMn,ix,iy,Δxad_) * Δxad_
dtCMn[iy,ix] -= qx(DMnMg,CMg,ix,iy,Δxad_) * Δxad_ +
qx(DMnFe,CFe,ix,iy,Δxad_) * Δxad_ +
qx(DMnMn,CMn,ix,iy,Δxad_) * Δxad_
end
# bottom
if position_Grt[iy-1,ix] == 0.0
dtCMg[iy,ix] -= - qy(DMgMg,CMg,ix,iy-1,Δyad_) * Δyad_ -
qy(DMgFe,CFe,ix,iy-1,Δyad_) * Δyad_ -
qy(DMgMn,CMn,ix,iy-1,Δyad_) * Δyad_
dtCFe[iy,ix] -= - qy(DFeMg,CMg,ix,iy-1,Δyad_) * Δyad_ -
qy(DFeFe,CFe,ix,iy-1,Δyad_) * Δyad_ -
qy(DFeMn,CMn,ix,iy-1,Δyad_) * Δyad_
dtCMn[iy,ix] -= - qy(DMnMg,CMg,ix,iy-1,Δyad_) * Δyad_ -
qy(DMnFe,CFe,ix,iy-1,Δyad_) * Δyad_ -
qy(DMnMn,CMn,ix,iy-1,Δyad_) * Δyad_
end
# top
if position_Grt[iy+1,ix] == 0.0
dtCMg[iy,ix] -= qy(DMgMg,CMg,ix,iy,Δyad_) * Δyad_ +
qy(DMgFe,CFe,ix,iy,Δyad_) * Δyad_ +
qy(DMgMn,CMn,ix,iy,Δyad_) * Δyad_
dtCFe[iy,ix] -= qy(DFeMg,CMg,ix,iy,Δyad_) * Δyad_ +
qy(DFeFe,CFe,ix,iy,Δyad_) * Δyad_ +
qy(DFeMn,CMn,ix,iy,Δyad_) * Δyad_
dtCMn[iy,ix] -= qy(DMnMg,CMg,ix,iy,Δyad_) * Δyad_ +
qy(DMnFe,CFe,ix,iy,Δyad_) * Δyad_ +
qy(DMnMn,CMn,ix,iy,Δyad_) * Δyad_
end
# if point is an inclusion
else
dtCMg[iy,ix] = 0.0
dtCFe[iy,ix] = 0.0
dtCMn[iy,ix] = 0.0
end
# if point is on grain boundary
if Grt_boundaries[iy,ix] == 1.0
dtCMg[iy,ix] = 0.0
dtCFe[iy,ix] = 0.0
dtCMn[iy,ix] = 0.0
end
# if point is on a model boundary (can define Neumann here)
else
dtCMg[iy,ix] = 0.0
dtCFe[iy,ix] = 0.0
dtCMn[iy,ix] = 0.0
end

return
end


function semi_discretisation_diffusion_2D(du,u,p,t)

@unpack D, D0, D_charact, Δxad_, Δyad_ = p
@unpack grt_position, grt_boundary = p.IC
DMgMg, DMgFe, DMgMn, DFeMg, DFeFe, DFeMn, DMnMg, DMnFe, DMnMn = D

CMg = @view u[:,:,1]
CFe = @view u[:,:,2]
CMn = @view u[:,:,3]

dtCMg = @view du[:,:,1]
dtCFe = @view du[:,:,2]
dtCMn = @view du[:,:,3]

println(typeof(du))

# update diffusive parameters
@parallel Diffusion_coef_2D!(DMgMg, DMgFe, DMgMn, DFeMg, DFeFe, DFeMn, DMnMg, DMnFe, DMnMn,
CMg, CFe ,CMn, D0, D_charact, grt_position)


# semi-discretization
@parallel stencil_diffusion_2D!(dtCMg, dtCFe, dtCMn, CMg, CFe ,CMn,
DMgMg, DMgFe, DMgMn, DFeMg, DFeFe, DFeMn, DMnMg, DMnFe, DMnMn,
grt_position, grt_boundary, Δxad_, Δyad_)
end

Loading

0 comments on commit 84e396d

Please sign in to comment.