Skip to content

Commit

Permalink
Quick bug fix: subset_do does not need space kwarg
Browse files Browse the repository at this point in the history
space keyword argument was being used in subset_do when use_nodes
was true but this requried unnecessary creation of empty dummy input
when use_nodes was not true.

With _parent fields in IMASdd ids, we can actually get the space object
corresponding to a grid subset. So new functions have been added to
get the parent objects:

get_grid_ggd: gets parent grid_ggd from space or grid_subset
get_space: gets corresponding space of a grid_subset from the parent.

New tests for testing subset_tools have been added as well.

Since the previous 3.0.0 has a bug because of this, minor version
has been rolled up.
  • Loading branch information
anchal-physics committed Feb 26, 2025
1 parent d88fbe9 commit 5d134b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "IMASggd"
uuid = "b7b5e640-9b39-4803-84eb-376048795def"
authors = ["Anchal Gupta <[email protected]>"]
version = "3.0.0"
version = "3.1.0"

[deps]
ArgParse = "c7e460c6-2fb9-53a9-8c5b-16f535851c63"
Expand Down
2 changes: 2 additions & 0 deletions docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ get_subset_space
get_grid_subset
get_subset_boundary_inds
get_subset_boundary
get_grid_ggd
get_space
subset_do
get_subset_centers
project_prop_on_subset!
Expand Down
33 changes: 26 additions & 7 deletions src/subset_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ export get_subset_space
export get_grid_subset
export get_subset_boundary_inds
export get_subset_boundary
export get_grid_ggd
export get_space
export subset_do
export get_subset_centers
export project_prop_on_subset!
Expand Down Expand Up @@ -162,26 +164,43 @@ function get_subset_boundary(
return ret_subset
end

"""
get_grid_ggd(subset::Union{all__grid_subset, all__space})::all__grid_ggd
Get the parent grid_ggd of a `grid_subset` or `space` object.
"""
function get_grid_ggd(subset::Union{all__grid_subset, all__space})::all__grid_ggd
return getfield(getfield(subset, :_parent).value, :_parent).value
end

"""
get_space_from_subset(subset::all__grid_subset)::all__space
Get the corresponding space in parent grid_ggd for a grid_subset object.
"""
function get_space(subset::all__grid_subset)::all__space
grid_ggd = get_grid_ggd(subset)
return grid_ggd.space[subset.element[1].object[1].space]

Check warning on line 183 in src/subset_tools.jl

View check run for this annotation

Codecov / codecov/patch

src/subset_tools.jl#L181-L183

Added lines #L181 - L183 were not covered by tests
end

"""
subset_do(
set_operator,
itrs::Vararg{all__grid_subset};
space::all__space,
use_nodes=false,
)::all__grid_subset
Function to perform any set operation (intersect, union, setdiff etc.) on
subset.element to generate a list of elements to go to subset object. If use_nodes is
true, the set operation will be applied on the set of nodes from subset.element, space
argument is required for this.
true, the set operation will be applied on the set of nodes from subset.element.
"""
function subset_do(
set_operator,
itrs::Vararg{all__grid_subset};
space::all__space,
use_nodes=false,
)::all__grid_subset
if use_nodes
space = get_space(itrs[1])

Check warning on line 203 in src/subset_tools.jl

View check run for this annotation

Codecov / codecov/patch

src/subset_tools.jl#L203

Added line #L203 was not covered by tests
ele_inds = set_operator(
[
union([
Expand All @@ -195,10 +214,10 @@ function subset_do(
ele_inds = set_operator(
[[ele.object[1].index for ele subset.element] for subset itrs]...,
)
dim = itrs[1][1].object[1].dimension
dim = itrs[1].element[1].object[1].dimension
end
ret_subset = typeof(itrs[1][1])()
space_number = itrs[1][1].object[1].space
ret_subset = typeof(itrs[1])()
space_number = itrs[1].element[1].object[1].space
add_subset_element!(ret_subset, space_number, dim, ele_inds)
return ret_subset
end
Expand Down
38 changes: 37 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import IMASggd: interp, get_kdtree, project_prop_on_subset!, get_grid_subset
import IMASggd:
interp, get_kdtree, project_prop_on_subset!, get_grid_subset, get_grid_ggd,
get_subset_boundary, subset_do
using IMASdd: IMASdd
import Statistics: mean
using Test
Expand All @@ -22,6 +24,9 @@ function parse_commandline()
["--interpeqt"],
Dict(:help => "Test interpolation of equilibrium time slice",
:action => :store_true),
["--subset_tools"],
Dict(:help => "Test subset tools",
:action => :store_true),
)
args = ArgParse.parse_args(s)
if !any(values(args)) # If no flags are set, run all tests
Expand Down Expand Up @@ -157,6 +162,37 @@ if args["projection"]
end
end

if args["subset_tools"]
@testset "test subset_tools" begin
grid_ggd = ids.edge_profiles.grid_ggd[1]
space = grid_ggd.space[1]

@test grid_ggd == get_grid_ggd(space)

subset_core = get_grid_subset(grid_ggd, 22)
subset_sol = get_grid_subset(grid_ggd, 23)
subset_odr = get_grid_subset(grid_ggd, 24)
subset_idr = get_grid_subset(grid_ggd, 25)

subset_corebnd = get_grid_subset(grid_ggd, 15)
subset_separatrix = get_grid_subset(grid_ggd, 16)
subset_pfrcut = get_grid_subset(grid_ggd, 8)

core_bdry = get_subset_boundary(space, subset_core)
sol_bdry = get_subset_boundary(space, subset_sol)
idr_bdry = get_subset_boundary(space, subset_idr)
odr_bdry = get_subset_boundary(space, subset_odr)

@test subset_pfrcut.element ==
subset_do(intersect, idr_bdry, odr_bdry).element
@test subset_corebnd.element ==
subset_do(setdiff, core_bdry, sol_bdry).element
@test subset_separatrix.element ==
subset_do(intersect, sol_bdry,
subset_do(union, core_bdry, odr_bdry, idr_bdry)).element
end
end

if args["in"]
@testset "test ∈" begin
grid_ggd = ids.edge_profiles.grid_ggd[1]
Expand Down

0 comments on commit 5d134b7

Please sign in to comment.