diff --git a/examples/plotting.ipynb b/examples/plotting.ipynb index 26e2180..53ad34a 100644 --- a/examples/plotting.ipynb +++ b/examples/plotting.ipynb @@ -1,163 +1,163 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Plotting examples using GGDUtils\n", - " \n", - " For running this notebook, you need to install package IJulia in your home environment (that is messy, but that is the only way I know right now). So in your terminal:\n", - " ```\n", - " % julia\n", - " julia > ]\n", - " (@v1.9 pkg) pkg> add IJulia\n", - " ```\n", - "\n", - " After this, Julia kernel would appear in your jupyter notebooks as an option. This also works for julia notebooks directly opened on VSCode. Select the Julia kernel to run this notebook." - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "using Pkg\n", - "Pkg.activate(\"./\")\n", - "Pkg.add(url=\"git@github.com:ProjectTorreyPines/IMASDD.jl.git\")\n", - "Pkg.develop(path=\"../\")\n", - "Pkg.add(\"Plots\")\n", - "Pkg.add(\"LaTeXStrings\")" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "using IMASDD\n", - "using GGDUtils\n", - "using Plots\n", - "using LaTeXStrings" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "dd = IMASDD.json2imas(\"../samples/time_dep_edge_profiles_last_step_only.json\")\n", - "grid_ggd = dd.edge_profiles.grid_ggd[1]\n", - "space = grid_ggd.space[1]" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Plotting grid and subsets" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Choose backend\n", - "gr() # Fast and can save pdf\n", - "# plotlyjs() # Use for interactive plot, can only save png\n", - "\n", - "plot(space) # Simply plot the grid described in space, all common arguments to plot can be given here\n", - "\n", - "# You can overlay any subset by giving a second argument\n", - "# Labels \n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 6), markercolor=:chocolate1)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 7), linecolor=:red, linewidth=2)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 8), linecolor=:darkred, linewidth=2)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 9), linecolor=:limegreen, linewidth=2)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 10), linecolor=:darkgreen, linewidth=2)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 11), linecolor=:cyan, linewidth=2)\n", - "# plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 12), linecolor=:teal, linewidth=1)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 13), linecolor=:royalblue1, linewidth=2)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 14), linecolor=:navyblue, linewidth=2)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 15), linecolor=:fuchsia, linewidth=2, linestyle=:dash)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 16), linecolor=:purple4, linewidth=2, linestyle=:dash)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 101), markershape=:rect, markercolor=:royalblue1)\n", - "# plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 102), markershape=:rect, markercolor=:maroon)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 103), markershape=:diamond, markercolor=:fuchsia)\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 104), markershape=:diamond, markercolor=:purple4)\n", - "\n", - "# Legend is supressed unless asked for specifically\n", - "plot!(legend=true)\n", - "# Default labels are subset.identifier.name but can be changed by providing a label argument\n", - "\n", - "# It is recommended to add atleast 10 pt margin to the left to accomodate the y-axis label\n", - "plot!(left_margin=10Plots.pt)\n" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Plotting 2D quantities as heatmaps" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Choose backend\n", - "gr() # Fast and can save pdf\n", - "# plotlyjs() # Use for interactive plot, can only save png\n", - "\n", - "n_e = GGDUtils.get_prop_with_grid_subset_index(dd.edge_profiles.ggd[1].electrons.density, 5)\n", - "plot(dd.edge_profiles.grid_ggd, n_e, colorbar_title=\"Electrons density / \" * L\"m^{-3}\",\n", - " left_margin=10Plots.pt)" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "### You can overlap any grid on top of a quantity" - ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# Choose backend\n", - "gr() # Fast and can save pdf\n", - "# plotlyjs() # Use for interactive plot, can only save png\n", - "\n", - "plot(dd.edge_profiles.grid_ggd, n_e) # Note default label in colorbar\n", - "plot!(space, GGDUtils.get_grid_subset_with_index(grid_ggd, 16), linecolor=:red,\n", - " linewidth=2, linestyle=:solid, label=\"Separatix\", legend=true,\n", - " left_margin=10Plots.pt)" - ] - } - ], - "metadata": { - "kernelspec": { - "display_name": "Julia 1.9.4", - "language": "julia", - "name": "julia-1.9" - }, - "language_info": { - "file_extension": ".jl", - "mimetype": "application/julia", - "name": "julia", - "version": "1.9.4" - }, - "orig_nbformat": 4 - }, - "nbformat": 4, - "nbformat_minor": 2 -} + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Plotting examples using GGDUtils\n", + " \n", + " For running this notebook, you need to install package IJulia in your home environment (that is messy, but that is the only way I know right now). So in your terminal:\n", + " ```\n", + " % julia\n", + " julia > ]\n", + " (@v1.9 pkg) pkg> add IJulia\n", + " ```\n", + "\n", + " After this, Julia kernel would appear in your jupyter notebooks as an option. This also works for julia notebooks directly opened on VSCode. Select the Julia kernel to run this notebook." + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "using Pkg\n", + "Pkg.activate(\"./\")\n", + "Pkg.add(url=\"git@github.com:ProjectTorreyPines/IMASDD.jl.git\")\n", + "Pkg.develop(path=\"../\")\n", + "Pkg.add(\"Plots\")\n", + "Pkg.add(\"LaTeXStrings\")" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "using IMASDD\n", + "using GGDUtils\n", + "using Plots\n", + "using LaTeXStrings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "dd = IMASDD.json2imas(\"../samples/time_dep_edge_profiles_last_step_only.json\")\n", + "grid_ggd = dd.edge_profiles.grid_ggd[1]\n", + "space = grid_ggd.space[1]" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plotting grid and subsets" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Choose backend\n", + "gr() # Fast and can save pdf\n", + "# plotlyjs() # Use for interactive plot, can only save png\n", + "\n", + "plot(space) # Simply plot the grid described in space, all common arguments to plot can be given here\n", + "\n", + "# You can overlay any subset by giving a second argument\n", + "# Labels \n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, \"x_points\"), markercolor=:chocolate1)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, \"core_cut\"), linecolor=:red, linewidth=2)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 8), linecolor=:darkred, linewidth=2)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 9), linecolor=:limegreen, linewidth=2)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 10), linecolor=:darkgreen, linewidth=2)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 11), linecolor=:cyan, linewidth=2)\n", + "# plot!(space, GGDUtils.get_grid_subset(grid_ggd, 12), linecolor=:teal, linewidth=1)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 13), linecolor=:royalblue1, linewidth=2)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 14), linecolor=:navyblue, linewidth=2)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 15), linecolor=:fuchsia, linewidth=2, linestyle=:dash)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 16), linecolor=:purple4, linewidth=2, linestyle=:dash)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 101), markershape=:rect, markercolor=:royalblue1)\n", + "# plot!(space, GGDUtils.get_grid_subset(grid_ggd, 102), markershape=:rect, markercolor=:maroon)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 103), markershape=:diamond, markercolor=:fuchsia)\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 104), markershape=:diamond, markercolor=:purple4)\n", + "\n", + "# Legend is supressed unless asked for specifically\n", + "plot!(legend=true)\n", + "# Default labels are subset.identifier.name but can be changed by providing a label argument\n", + "\n", + "# It is recommended to add atleast 10 pt margin to the left to accomodate the y-axis label\n", + "plot!(left_margin=10Plots.pt)\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Plotting 2D quantities as heatmaps" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Choose backend\n", + "gr() # Fast and can save pdf\n", + "# plotlyjs() # Use for interactive plot, can only save png\n", + "\n", + "n_e = GGDUtils.get_prop_with_grid_subset_index(dd.edge_profiles.ggd[1].electrons.density, 5)\n", + "plot(dd.edge_profiles.grid_ggd, n_e, colorbar_title=\"Electrons density / \" * L\"m^{-3}\",\n", + " left_margin=10Plots.pt)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "### You can overlap any grid on top of a quantity" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Choose backend\n", + "gr() # Fast and can save pdf\n", + "# plotlyjs() # Use for interactive plot, can only save png\n", + "\n", + "plot(dd.edge_profiles.grid_ggd, n_e) # Note default label in colorbar\n", + "plot!(space, GGDUtils.get_grid_subset(grid_ggd, 16), linecolor=:red,\n", + " linewidth=2, linestyle=:solid, label=\"Separatix\", legend=true,\n", + " left_margin=10Plots.pt)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Julia 1.9.4", + "language": "julia", + "name": "julia-1.10" + }, + "language_info": { + "file_extension": ".jl", + "mimetype": "application/julia", + "name": "julia", + "version": "1.9.4" + }, + "orig_nbformat": 4 + }, + "nbformat": 4, + "nbformat_minor": 2 +} \ No newline at end of file diff --git a/src/interpolations.jl b/src/interpolations.jl index 88e943f..985062b 100644 --- a/src/interpolations.jl +++ b/src/interpolations.jl @@ -234,7 +234,7 @@ function interp( grid_ggd::IMASDD.edge_profiles__grid_ggd, value_field::Symbol=:values, ) - subset = get_grid_subset_with_index(grid_ggd, prop.grid_subset_index) + subset = get_grid_subset(grid_ggd, prop.grid_subset_index) space = grid_ggd.space[subset.element[1].object[1].space] return interp(getfield(prop, value_field), space, subset) end @@ -281,13 +281,13 @@ function interp( value_field::Symbol=:values, ) where {T <: edge_profiles__prop_on_subset} prop = get_prop_with_grid_subset_index(prop_arr, grid_subset_index) - subset = get_grid_subset_with_index(grid_ggd, grid_subset_index) + subset = get_grid_subset(grid_ggd, grid_subset_index) space = grid_ggd.space[subset.element[1].object[1].space] return interp(getfield(prop, value_field), space, subset) end function get_TPS_mats(grid_ggd::IMASDD.edge_profiles__grid_ggd, grid_subset_index::Int) - subset = get_grid_subset_with_index(grid_ggd, grid_subset_index) + subset = get_grid_subset(grid_ggd, grid_subset_index) space = grid_ggd.space[subset.element[1].object[1].space] return get_TPS_mats(space, subset) end diff --git a/src/recipes.jl b/src/recipes.jl index 7e1fcb1..7c41df2 100644 --- a/src/recipes.jl +++ b/src/recipes.jl @@ -103,7 +103,7 @@ end grid_ggd::IMASDD.edge_profiles__grid_ggd, prop::IMASDD.IDSvectorElement, ) - subset = get_grid_subset_with_index(grid_ggd, prop.grid_subset_index) + subset = get_grid_subset(grid_ggd, prop.grid_subset_index) space = grid_ggd.space[subset.element[1].object[1].space] nodes = space.objects_per_dimension[1].object cells = space.objects_per_dimension[3].object diff --git a/src/subset_tools.jl b/src/subset_tools.jl index b09ee58..aefd8f5 100644 --- a/src/subset_tools.jl +++ b/src/subset_tools.jl @@ -79,7 +79,7 @@ function get_subset_space(space::IMASDD.edge_profiles__grid_ggd___space, end """ - get_grid_subset_with_index( + get_grid_subset( grid_ggd::IMASDD.edge_profiles__grid_ggd, grid_subset_index::Int, @@ -87,7 +87,7 @@ end Returns the grid_subset in a grid_ggd with the matching grid_subset_index """ -function get_grid_subset_with_index( +function get_grid_subset( grid_ggd::IMASDD.edge_profiles__grid_ggd, grid_subset_index::Int, ) @@ -96,11 +96,30 @@ function get_grid_subset_with_index( return subset end end - # BCL 12/8: Creates type instability, but maybe okay since it's a "simple" Union - # subset::Union{Int64, IMASDD.edge_profiles__grid_ggd___grid_subset} - # - # Better would be to immediately throw an error or return nothing - return 0 # Indicates failure + + return error("Subset ", grid_subset_index, " not found.") +end + +""" + get_grid_subset( + grid_ggd::IMASDD.edge_profiles__grid_ggd, + grid_subset_name::String, + +) + +Returns the grid_subset in a grid_ggd with the matching grid_subset_name +""" +function get_grid_subset( + grid_ggd::IMASDD.edge_profiles__grid_ggd, + grid_subset_name::String, +) + for subset ∈ grid_ggd.grid_subset + if subset.identifier.name == grid_subset_name + return subset + end + end + + return error("Subset ", grid_subset_name, " not found.") end """ diff --git a/test/runtests.jl b/test/runtests.jl index fcff4e5..7e87b1d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,4 +1,4 @@ -import GGDUtils: interp, get_kdtree, project_prop_on_subset!, get_grid_subset_with_index +import GGDUtils: interp, get_kdtree, project_prop_on_subset!, get_grid_subset import OMAS as IMASDD import Statistics: mean using Test @@ -66,7 +66,7 @@ if args["interp"] @test abs.((grid_val .- searched_val) ./ grid_val) < allowed_rtol # test interp(prop_arr, space, subset) - subset = get_grid_subset_with_index(grid_ggd, 5) + subset = get_grid_subset(grid_ggd, 5) print("interp(prop_arr, space, subset) time: ") @time get_n_e = interp(ids.edge_profiles.ggd[1].electrons.density, space, subset) @@ -128,9 +128,9 @@ if args["projection"] space = ids.edge_profiles.grid_ggd[1].space[1] prop = ids.edge_profiles.ggd[1].electrons.density # All cells - from_subset = get_grid_subset_with_index(ids.edge_profiles.grid_ggd[1], 5) + from_subset = get_grid_subset(ids.edge_profiles.grid_ggd[1], 5) # separatix - to_subset = get_grid_subset_with_index(ids.edge_profiles.grid_ggd[1], 16) + to_subset = get_grid_subset(ids.edge_profiles.grid_ggd[1], 16) print("project_prop_on_subset!(prop, from_subset, to_subset, space) time: ") @time separatix_centers, values_at_separatix = project_prop_on_subset!(prop, from_subset, to_subset, space) @@ -140,7 +140,7 @@ if args["projection"] # end subset_core = - get_grid_subset_with_index(ids.edge_profiles.grid_ggd[1], 22) + get_grid_subset(ids.edge_profiles.grid_ggd[1], 22) print("project_prop_on_subset!(prop, from_subset, subset_core) time: ") @time core_element_inds, values_at_core = project_prop_on_subset!(prop, from_subset, subset_core) @@ -161,9 +161,9 @@ if args["in"] @testset "test ∈" begin grid_ggd = ids.edge_profiles.grid_ggd[1] space = grid_ggd.space[1] - subset_corebnd = get_grid_subset_with_index(grid_ggd, 15) - subset_sol = get_grid_subset_with_index(grid_ggd, 23) - subset_odr = get_grid_subset_with_index(grid_ggd, 24) + subset_corebnd = get_grid_subset(grid_ggd, 15) + subset_sol = get_grid_subset(grid_ggd, 23) + subset_odr = get_grid_subset(grid_ggd, 24) @test (6.0, 0.0) ∈ (subset_corebnd, space) @test (5.0, -2.5) ∉ (subset_corebnd, space)