Skip to content

Commit

Permalink
Update mapping routines
Browse files Browse the repository at this point in the history
  • Loading branch information
bennibolm committed Oct 14, 2024
1 parent 8d7c2e1 commit 9c10471
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 50 deletions.
38 changes: 27 additions & 11 deletions examples/t8code_2d_fv/elixir_advection_basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,19 @@ coordinates_max = (8.0, 8.0) # maximum coordinates (max(x), max(y))
mapping_coordinates = Trixi.coordinates2mapping(coordinates_min, coordinates_max)

# Option 2: faces
f1(s) = SVector(-1.0, s - 1.0)
f2(s) = SVector(1.0, s + 1.0)
f3(s) = SVector(s, -1.0 + sin(0.5 * pi * s))
f4(s) = SVector(s, 1.0 + sin(0.5 * pi * s))
# waving flag
# f1(s) = SVector(-1.0, s - 1.0)
# f2(s) = SVector(1.0, s + 1.0)
# f3(s) = SVector(s, -1.0 + sin(0.5 * pi * s))
# f4(s) = SVector(s, 1.0 + sin(0.5 * pi * s))

# [0,8]^2
f1(s) = SVector(0.0, 4*(s+1))
f2(s) = SVector(8.0, 4*(s+1))
f3(s) = SVector(4*(s+1), 0.0)
f4(s) = SVector(4*(s+1), 8.0)
faces = (f1, f2, f3, f4)
Trixi.validate_faces(faces)
mapping_faces = Trixi.transfinite_mapping(faces)

# Option 3: classic mapping
Expand All @@ -41,9 +49,13 @@ function mapping(xi, eta)
end

# Note and TODO:
# Normally, this should be put somewhere else. For now, that doesn't properly.
# See note in `src/auxiliary/t8code.jl`
function f(cmesh, gtreeid, ref_coords, num_coords, out_coords, tree_data, user_data)
# Normally, this should be put somewhere else. For now, that doesn't work properly.
# For instance, in `src/auxiliary/t8code.jl`
# Problem: Even when define this routine somewhere else (then by using a closure) and called
# directly within this elixir (e.g. mapping = trixi_t8_mapping_c(mapping)), we get the SegFault error.
# - Is the function called with the correct parameters? Memory location correct? It seems so, yes.
# - Life time issue for the GC tracked Julia object used in C? **Yes, see gc deactivation in elixir.**
function trixi_t8_mapping(cmesh, gtreeid, ref_coords, num_coords, out_coords, tree_data, user_data)
ltreeid = t8_cmesh_get_local_id(cmesh, gtreeid)
eclass = t8_cmesh_get_tree_class(cmesh, ltreeid)
T8code.t8_geom_compute_linear_geometry(eclass, tree_data,
Expand All @@ -65,18 +77,20 @@ function f(cmesh, gtreeid, ref_coords, num_coords, out_coords, tree_data, user_d
return nothing
end

function f_c()
@cfunction($f, Cvoid,
function trixi_t8_mapping_c()
@cfunction($trixi_t8_mapping, Cvoid,
(t8_cmesh_t, t8_gloidx_t, Ptr{Cdouble}, Csize_t,
Ptr{Cdouble}, Ptr{Cvoid}, Ptr{Cvoid}))
end

trees_per_dimension = (2, 2)

# Disabling the gc for almost the entire elixir seems to work in order to fix the SegFault errors with trixi_t8_mapping_c and mapping_coordinates
# GC.enable(false)

eclass = T8_ECLASS_QUAD
mesh = T8codeMesh(trees_per_dimension, eclass,
# mapping = Trixi.trixi_t8_mapping_c(mapping),
mapping = f_c(),
mapping = trixi_t8_mapping_c(),
# Plan is to use either
# coordinates_max = coordinates_max, coordinates_min = coordinates_min,
# or at least
Expand Down Expand Up @@ -110,3 +124,5 @@ sol = solve(ode, CarpenterKennedy2N54(williamson_condition = false),
dt = 1.0, # solve needs some value here but it will be overwritten by the stepsize_callback
save_everystep = false, callback = callbacks);
summary_callback()

# GC.enable(true)
39 changes: 0 additions & 39 deletions src/auxiliary/t8code.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,42 +190,3 @@ function trixi_t8_adapt!(mesh, indicators)

return differences
end

# Note and TODO:
# This routine seems to work with a "standard" mapping, but not with `coordinates2mapping`.
# Now, even when called directly within the elixir.
# - Is the function called with the correct parameters? Memory location correct?
# - Life time issue for the GC tracked Julia object used in C?
# function trixi_t8_mapping_c(mapping)
# function f(cmesh, gtreeid, ref_coords, num_coords, out_coords, tree_data, user_data)
# @T8_ASSERT(cmesh isa Ptr{t8_cmesh})
# @T8_ASSERT(gtreeid isa t8_gloidx_t)
# @T8_ASSERT(ref_coords isa Ptr{Cdouble})
# @T8_ASSERT(num_coords isa Csize_t)
# @T8_ASSERT(out_coords isa Ptr{Cdouble})
# @T8_ASSERT(tree_data isa Ptr{Cvoid})
# @T8_ASSERT(user_data isa Ptr{Cvoid})

# ltreeid = t8_cmesh_get_local_id(cmesh, gtreeid)
# eclass = t8_cmesh_get_tree_class(cmesh, ltreeid)
# T8code.t8_geom_compute_linear_geometry(eclass, tree_data,
# ref_coords, num_coords, out_coords)

# for i in 1:num_coords
# offset_3d = 3 * (i - 1) + 1

# xi = unsafe_load(out_coords, offset_3d)
# eta = unsafe_load(out_coords, offset_3d + 1)
# xy = mapping(xi, eta)

# unsafe_store!(out_coords, xy[1], offset_3d)
# unsafe_store!(out_coords, xy[2], offset_3d + 1)
# end

# return nothing
# end

# return @cfunction($f, Cvoid,
# (t8_cmesh_t, t8_gloidx_t, Ptr{Cdouble}, Csize_t, Ptr{Cdouble},
# Ptr{Cvoid}, Ptr{Cvoid}))
# end

0 comments on commit 9c10471

Please sign in to comment.