From 11f1e7c2460f7d64e99d6a3698486141a2bf4546 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 09:15:28 +0400 Subject: [PATCH 1/8] remove Requires and add a Makie extension --- Project.toml | 10 +- docs/src/literate/beso.jl | 1 - docs/src/literate/csimp.jl | 1 - docs/src/literate/geso.jl | 1 - docs/src/literate/global_stress.jl | 1 - docs/src/literate/local_stress.jl | 1 - docs/src/literate/simp.jl | 1 - .../TopOptMakieExt/TopOptMakieExt.jl | 204 +++++++++++++++++- src/CheqFilters/CheqFilters.jl | 2 +- src/FEA/FEA.jl | 2 +- src/Functions/Functions.jl | 2 +- src/TopOpt.jl | 7 +- src/TopOptProblems/IO/IO.jl | 2 + .../{Visualization => IO}/mesh_types.jl | 4 +- src/TopOptProblems/TopOptProblems.jl | 5 +- .../Visualization/Visualization.jl | 15 -- .../TrussTopOptProblems.jl | 2 - .../TrussVisualization/TrussVisualization.jl | 12 -- .../TrussVisualization/makie.jl | 197 ----------------- src/Utilities/Utilities.jl | 2 +- test/Functions/test_truss_stress_fns.jl | 1 - test/examples/local_stress.jl | 1 - .../truss_compliance_2d1.jl | 1 - test/examples/neural.jl | 1 - test/truss_topopt_problems/test_buckling.jl | 1 - .../test_buckling_optimize.jl | 2 - test/truss_topopt_problems/test_fea.jl | 1 - test/truss_topopt_problems/test_problem.jl | 1 - .../demos/continuum/cont_compliance1.jl | 1 - .../demos/continuum/cont_compliance2.jl | 1 - .../demos/continuum/cont_compliance3_3D.ipynb | 3 +- test/wcsmo14/demos/continuum/cont_stress.jl | 1 - .../demos/truss/truss_compliance_2d1.jl | 1 - .../demos/truss/truss_compliance_2d2.jl | 1 - .../demos/truss/truss_compliance_3d1.jl | 1 - .../demos/truss/truss_compliance_3d2.jl | 1 - .../wcsmo14/jl_benchmarks/compare_neo99_2D.jl | 1 - test/wcsmo14/jl_benchmarks/compare_polytop.jl | 1 - test/wcsmo14/jl_benchmarks/compare_top3d.jl | 1 - .../wcsmo14/jl_benchmarks/compare_top3d125.jl | 1 - .../jl_benchmarks/compare_topopt_py.jl | 1 - 41 files changed, 220 insertions(+), 276 deletions(-) rename src/TopOptProblems/Visualization/makie.jl => ext/TopOptMakieExt/TopOptMakieExt.jl (68%) rename src/TopOptProblems/{Visualization => IO}/mesh_types.jl (95%) delete mode 100644 src/TopOptProblems/Visualization/Visualization.jl delete mode 100644 src/TrussTopOptProblems/TrussVisualization/TrussVisualization.jl delete mode 100644 src/TrussTopOptProblems/TrussVisualization/makie.jl diff --git a/Project.toml b/Project.toml index 0b5ce987..4067d147 100644 --- a/Project.toml +++ b/Project.toml @@ -31,7 +31,6 @@ Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" Preconditioners = "af69fa37-3177-5a40-98ee-561f696e4fcd" Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c" Reexport = "189a3867-3050-52da-a836-e630ba90ab69" -Requires = "ae029012-a4dd-5104-9daa-d747884805df" Revise = "295af30f-e4ad-537b-8983-00126c2a3abe" Setfield = "efcf1570-3423-57d1-acb7-fd33fddbac46" SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" @@ -70,7 +69,6 @@ NonconvexSemidefinite = "0.1.7" Parameters = "0.12" Preconditioners = "0.3, 0.4, 0.5, 0.6" Reexport = "1" -Requires = "1.1" Revise = "3" Setfield = "0.7, 0.8, 1" StaticArrays = "1" @@ -81,4 +79,10 @@ TimerOutputs = "0.5" VTKDataTypes = "0.1" WriteVTK = "1.9" Zygote = "0.6" -julia = "1.5, 1.6" +julia = "1.9" + +[weakdeps] +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" + +[extensions] +TopOptMakieExt = "Makie" diff --git a/docs/src/literate/beso.jl b/docs/src/literate/beso.jl index c5684d6e..3185817f 100644 --- a/docs/src/literate/beso.jl +++ b/docs/src/literate/beso.jl @@ -36,7 +36,6 @@ result = beso(x0) # Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first # ```julia # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize # fig = visualize(problem; topology = result.topology) # Makie.display(fig) # ``` diff --git a/docs/src/literate/csimp.jl b/docs/src/literate/csimp.jl index aa9cd2fc..e6639fae 100644 --- a/docs/src/literate/csimp.jl +++ b/docs/src/literate/csimp.jl @@ -77,7 +77,6 @@ end # Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first # ```julia # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize # fig = visualize(problem; topology = x, default_exagg_scale = 0.07, # scale_range = 10.0, vector_linewidth = 3, vector_arrowsize = 0.5, # ) diff --git a/docs/src/literate/geso.jl b/docs/src/literate/geso.jl index 7502ebe0..4041cf3a 100644 --- a/docs/src/literate/geso.jl +++ b/docs/src/literate/geso.jl @@ -36,7 +36,6 @@ result = geso(x0) # Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first # ```julia # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize # fig = visualize(problem; topology = result.topology) # Makie.display(fig) # ``` diff --git a/docs/src/literate/global_stress.jl b/docs/src/literate/global_stress.jl index 58fd8ddd..a1896cbe 100644 --- a/docs/src/literate/global_stress.jl +++ b/docs/src/literate/global_stress.jl @@ -71,7 +71,6 @@ r = optimize(model, alg, x0; options) # Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first # ```julia # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize # fig = visualize(problem; topology = r.minimizer) # Makie.display(fig) # ``` diff --git a/docs/src/literate/local_stress.jl b/docs/src/literate/local_stress.jl index fe249c6c..293a58de 100644 --- a/docs/src/literate/local_stress.jl +++ b/docs/src/literate/local_stress.jl @@ -50,7 +50,6 @@ maximum(stress(filter(PseudoDensities(x)))) # Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first # ```julia # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize # fig = visualize( # problem; topology = r.minimizer, default_exagg_scale = 0.07, # scale_range = 10.0, vector_linewidth = 3, vector_arrowsize = 0.5, diff --git a/docs/src/literate/simp.jl b/docs/src/literate/simp.jl index 5151ffcd..00c9fbca 100644 --- a/docs/src/literate/simp.jl +++ b/docs/src/literate/simp.jl @@ -60,7 +60,6 @@ r = optimize(model, alg, x0; options) # Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first # ```julia # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize # fig = visualize( # problem; topology = r.minimizer, # default_exagg_scale = 0.07, scale_range = 10.0, diff --git a/src/TopOptProblems/Visualization/makie.jl b/ext/TopOptMakieExt/TopOptMakieExt.jl similarity index 68% rename from src/TopOptProblems/Visualization/makie.jl rename to ext/TopOptMakieExt/TopOptMakieExt.jl index 36ee85dc..9477412d 100644 --- a/src/TopOptProblems/Visualization/makie.jl +++ b/ext/TopOptMakieExt/TopOptMakieExt.jl @@ -1,12 +1,15 @@ +module TopOptMakieExt + using LinearAlgebra: norm -# using Makie -using .GLMakie: Makie -using .Makie: lift, cam3d!, Point3f0, Vec3f0, Figure, Auto, RGBAf -using .Makie: DataAspect, Axis, LScene, SliderGrid -using .Makie.GeometryBasics +using Makie: Makie, lift, cam3d!, Point3f0, Vec3f0, Figure, Auto, RGBAf +using Makie: DataAspect, Axis, LScene, SliderGrid, linesegments!, Point2f0 +using Makie.GeometryBasics using ColorSchemes using GeometryBasics: GLTriangleFace -using ..TopOptProblems: getcloaddict, boundingbox +import TopOpt +using TopOpt.TopOptProblems: getcloaddict, boundingbox, getdim, StiffnessTopOptProblem +using TopOpt.TrussTopOptProblems: TrussProblem +using Ferrite ################################ # Credit to Simon Danisch for the conversion code below @@ -171,7 +174,7 @@ So we recommend using `GLMakie` backend until you are satisfied, and switch back - `Makie.Figure` handle """ -function visualize( +function TopOpt.visualize( problem::StiffnessTopOptProblem{dim,T}; u=undef, topology=undef, @@ -379,3 +382,190 @@ function visualize( return fig end + +function TopOpt.visualize( + problem::TrussProblem{xdim,T}; + u=undef, + topology=undef, + undeformed_mesh_color=RGBAf(0, 0, 0, 1.0), + cell_colors=undef, + draw_legend=false, + colormap=ColorSchemes.Spectral_10, + deformed_mesh_color=RGBAf(0, 1, 1, 0.4), + display_supports=true, + vector_arrowsize=0.3, + vector_linewidth=1.0, + default_support_scale=1e-2, + default_load_scale=1e-2, + scale_range=1.0, + default_exagg_scale=1.0, + exagg_range=10.0, + default_element_linewidth_scale=6.0, + element_linewidth_range=10.0, + kw..., +) where {xdim,T} + ndim = getdim(problem) + ncells = Ferrite.getncells(problem) + nnodes = Ferrite.getnnodes(problem) + given_u = u !== undef + topology = topology == undef ? ones(T, ncells) : topology + + fig = Figure() + if ndim == 2 + ax1 = Axis(fig[1, 1]) + # tightlimits!(ax1) + # ax1.aspect = AxisAspect(1) + ax1.aspect = DataAspect() + else + ax1 = LScene(fig[1, 1]; scenekw=(camera=cam3d!, raw=false)) #, height=750) + end + + # * linewidth scaling / support / load appearance / deformatione exaggeration control + linewidth_lsgrid = SliderGrid( + fig[2, 1], + ( + label="element linewidth", + range=0.0:0.01:element_linewidth_range, + format="{:.2f}", + startvalue=default_element_linewidth_scale, + ); + width=Auto(), + ) + if display_supports + condition_lsgrid = SliderGrid( + fig[3, 1], + ( + label="support scale", + range=0.0:0.01:scale_range, + format="{:.2f}", + startvalue=default_support_scale, + ), + ( + label="load scale", + range=0.0:0.01:scale_range, + format="{:.2f}", + startvalue=default_load_scale, + ); + width=Auto(), + ) + end + if given_u + deform_lsgrid = SliderGrid( + fig[4, 1], + ( + label="deformation exaggeration", + range=0.0:0.01:exagg_range, + format="{:.2f}", + startvalue=default_exagg_scale, + ); + width=Auto(), + ) + end + + # * undeformed truss elements + nodes = problem.truss_grid.grid.nodes + PtT = ndim == 2 ? Point2f0 : Point3f0 + edges_pts = [ + PtT(nodes[cell.nodes[1]].x) => PtT(nodes[cell.nodes[2]].x) for + cell in problem.truss_grid.grid.cells + ] + + # * linewidth and color per cell + scaled_cell_colors = similar(topology) + scaled_cell_colors .= 0.0 + if cell_colors !== undef + @assert length(cell_colors) == length(topology) "$(length(cell_colors)) , $(length(topology))" + val_range = maximum(cell_colors) - minimum(cell_colors) + scaled_cell_colors = (cell_colors .- minimum(cell_colors)) / val_range + end + if cell_colors !== undef && draw_legend + _create_colorbar(fig[1, 2], colormap, cell_colors) + end + + # linewidth: 2Xncells vector, 2i ~ 2i-1 represents a line's two endpoints' width + undeformed_mesh_colors = Vector{RGBAf}(undef, 2 * length(topology)) + topology_linewidth = similar(topology, 2 * length(topology)) + for i in eachindex(topology) + ccolor = undeformed_mesh_color + if cell_colors !== undef + ccolor = ColorSchemes.get(colormap, scaled_cell_colors[i]) + end + topology_linewidth[(2 * i - 1):(2 * i)] .= topology[i] + undeformed_mesh_colors[(2 * i - 1):(2 * i)] .= ccolor + end + element_linewidth = lift( + s -> topology_linewidth .* s, linewidth_lsgrid.sliders[1].value + ) + linesegments!(ax1, edges_pts; linewidth=element_linewidth, color=undeformed_mesh_colors) + + # # * deformed truss elements + if given_u + node_dofs = problem.metadata.node_dofs + @assert length(u) == ndim * nnodes + exagg_edge_pts = lift( + s -> [ + PtT(nodes[cell.nodes[1]].x) + PtT(u[node_dofs[:, cell.nodes[1]]] * s) => + PtT(nodes[cell.nodes[2]].x) + PtT(u[node_dofs[:, cell.nodes[2]]] * s) for + cell in problem.truss_grid.grid.cells + ], + deform_lsgrid.sliders[1].value, + ) + linesegments!( + ax1, exagg_edge_pts; linewidth=element_linewidth, color=deformed_mesh_color + ) + end + + if display_supports + # * load vectors + loaded_nodes = [PtT(nodes[node_id].x) for node_id in keys(problem.force)] + scaled_load_dirs = lift( + s -> [PtT(force / norm(force) * s) for force in values(problem.force)], + condition_lsgrid.sliders[2].value, + ) + Makie.arrows!( + ax1, + loaded_nodes, + scaled_load_dirs; + arrowcolor=:purple, + arrowsize=vector_arrowsize, + linecolor=:purple, + linewidth=vector_linewidth, + ) + Makie.scatter!(ax1, loaded_nodes) #, markersize = lift(s -> s * 3, lsgrid.sliders[1].value)) + + # * fixties vectors + ch = problem.ch + for (dbc_id, dbc) in enumerate(ch.dbcs) + support_vectors = [] + node_ids = dbc.faces + if 1 in dbc.components + push!(support_vectors, [1.0, 0.0, 0.0]) + end + if 2 in dbc.components + push!(support_vectors, [0.0, 1.0, 0.0]) + end + if 3 in dbc.components + push!(support_vectors, [0.0, 0.0, 1.0]) + end + fixed_nodes = PtT.(nodes[node_ind].x for node_ind in node_ids) + for v in support_vectors + Makie.arrows!( + fixed_nodes, + lift( + s -> [PtT(s .* v) for nid in node_ids], + condition_lsgrid.sliders[1].value, + ); + arrowcolor=:orange, + arrowsize=vector_arrowsize, + linecolor=:orange, + linewidth=vector_linewidth, + ) + end + Makie.scatter!(ax1, fixed_nodes) #, markersize = lift(s -> s * 3, lsgrid.sliders[1].value)) + end + end # end if display_supports + + return fig +end + +end diff --git a/src/CheqFilters/CheqFilters.jl b/src/CheqFilters/CheqFilters.jl index 4b9fd464..bacdd2cc 100644 --- a/src/CheqFilters/CheqFilters.jl +++ b/src/CheqFilters/CheqFilters.jl @@ -1,6 +1,6 @@ module CheqFilters -using ..Utilities, Ferrite, Requires +using ..Utilities, Ferrite using ..FEA, Statistics using ..TopOpt: TopOpt, ElementFEAInfo, PseudoDensities import ..TopOpt: PENALTY_BEFORE_INTERPOLATION diff --git a/src/FEA/FEA.jl b/src/FEA/FEA.jl index 0c001d4e..36544f9c 100644 --- a/src/FEA/FEA.jl +++ b/src/FEA/FEA.jl @@ -1,7 +1,7 @@ module FEA using ..TopOpt: TopOpt, PENALTY_BEFORE_INTERPOLATION -using ..TopOptProblems, ..Utilities, Requires +using ..TopOptProblems, ..Utilities using Ferrite, Setfield, TimerOutputs, Preconditioners using IterativeSolvers, StaticArrays, SparseArrays using LinearAlgebra diff --git a/src/Functions/Functions.jl b/src/Functions/Functions.jl index 38a706e0..6e27b817 100644 --- a/src/Functions/Functions.jl +++ b/src/Functions/Functions.jl @@ -4,7 +4,7 @@ using ..TopOpt: dim, TopOpt, PENALTY_BEFORE_INTERPOLATION, PseudoDensities using ..TopOptProblems, ..TrussTopOptProblems using ..TopOptProblems: initialize_K, getdh using ..FEA, ..CheqFilters -using ..Utilities, ForwardDiff, LinearAlgebra, Requires +using ..Utilities, ForwardDiff, LinearAlgebra using ..TrussTopOptProblems: getA, compute_local_axes using Parameters: @unpack diff --git a/src/TopOpt.jl b/src/TopOpt.jl index 9ce76f22..74805feb 100644 --- a/src/TopOpt.jl +++ b/src/TopOpt.jl @@ -1,7 +1,7 @@ module TopOpt const PENALTY_BEFORE_INTERPOLATION = true -using Requires, Reexport, ChainRulesCore +using Reexport, ChainRulesCore @reexport using Nonconvex, NonconvexMMA, NonconvexSemidefinite, NonconvexPercival @@ -50,6 +50,8 @@ export PseudoDensities include(joinpath("Utilities", "Utilities.jl")) using .Utilities +function visualize end + # Topopology optimization problem definitions include(joinpath("TopOptProblems", "TopOptProblems.jl")) @@ -121,5 +123,6 @@ export TopOpt, MMA02, HeavisideProjection, SigmoidProjection, - ProjectedPenalty + ProjectedPenalty, + visualize end diff --git a/src/TopOptProblems/IO/IO.jl b/src/TopOptProblems/IO/IO.jl index b852c636..db4ef66b 100644 --- a/src/TopOptProblems/IO/IO.jl +++ b/src/TopOptProblems/IO/IO.jl @@ -2,6 +2,8 @@ module InputOutput export InpStiffness, save_mesh +include("mesh_types.jl") + include(joinpath("INP", "INP.jl")) using .INP diff --git a/src/TopOptProblems/Visualization/mesh_types.jl b/src/TopOptProblems/IO/mesh_types.jl similarity index 95% rename from src/TopOptProblems/Visualization/mesh_types.jl rename to src/TopOptProblems/IO/mesh_types.jl index 6b5d4923..a320f7fe 100644 --- a/src/TopOptProblems/Visualization/mesh_types.jl +++ b/src/TopOptProblems/IO/mesh_types.jl @@ -1,6 +1,8 @@ using GeometryBasics: GeometryBasics using GeometryTypes: GeometryTypes -using ..TopOptProblems: getdh +using ..TopOptProblems: + getdh, AbstractTopOptProblem, StiffnessTopOptProblem, QuadraticHexahedron +using Ferrite, VTKDataTypes """ map Ferrite cell type to VTKDataTypes cell type diff --git a/src/TopOptProblems/TopOptProblems.jl b/src/TopOptProblems/TopOptProblems.jl index 6fc7ee7b..e8bbea4f 100644 --- a/src/TopOptProblems/TopOptProblems.jl +++ b/src/TopOptProblems/TopOptProblems.jl @@ -1,7 +1,7 @@ module TopOptProblems using Ferrite, StaticArrays, LinearAlgebra -using SparseArrays, Setfield, Requires +using SparseArrays, Setfield using ..TopOpt.Utilities using ..TopOpt: PENALTY_BEFORE_INTERPOLATION using ..Utilities: @forward_property @@ -27,9 +27,6 @@ include("buckling.jl") include(joinpath("IO", "IO.jl")) using .InputOutput -include("Visualization/Visualization.jl") -using .Visualization - export RayProblem, PointLoadCantilever, HalfMBB, diff --git a/src/TopOptProblems/Visualization/Visualization.jl b/src/TopOptProblems/Visualization/Visualization.jl deleted file mode 100644 index 5a873911..00000000 --- a/src/TopOptProblems/Visualization/Visualization.jl +++ /dev/null @@ -1,15 +0,0 @@ -module Visualization - -using ..TopOptProblems: AbstractTopOptProblem, StiffnessTopOptProblem, QuadraticHexahedron -using Ferrite, VTKDataTypes, Requires - -include("mesh_types.jl") - -function __init__() - @require GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" @eval begin - include("makie.jl") - export visualize - end -end - -end diff --git a/src/TrussTopOptProblems/TrussTopOptProblems.jl b/src/TrussTopOptProblems/TrussTopOptProblems.jl index 72d99448..8066ce8b 100644 --- a/src/TrussTopOptProblems/TrussTopOptProblems.jl +++ b/src/TrussTopOptProblems/TrussTopOptProblems.jl @@ -26,8 +26,6 @@ include("matrices_and_vectors.jl") include("elementinfo.jl") include(joinpath("TrussIO", "TrussIO.jl")) using .TrussIO -include(joinpath("TrussVisualization", "TrussVisualization.jl")) -using .TrussVisualization export TrussGrid, TrussProblem, TrussFEACrossSec, TrussFEAMaterial export PointLoadCantileverTruss diff --git a/src/TrussTopOptProblems/TrussVisualization/TrussVisualization.jl b/src/TrussTopOptProblems/TrussVisualization/TrussVisualization.jl deleted file mode 100644 index 80ad4d88..00000000 --- a/src/TrussTopOptProblems/TrussVisualization/TrussVisualization.jl +++ /dev/null @@ -1,12 +0,0 @@ -module TrussVisualization - -using Requires - -function __init__() - @require GLMakie = "e9467ef8-e4e7-5192-8a1a-b1aee30e663a" @eval begin - include("makie.jl") - export visualize - end -end - -end diff --git a/src/TrussTopOptProblems/TrussVisualization/makie.jl b/src/TrussTopOptProblems/TrussVisualization/makie.jl deleted file mode 100644 index ee321b94..00000000 --- a/src/TrussTopOptProblems/TrussVisualization/makie.jl +++ /dev/null @@ -1,197 +0,0 @@ -using Ferrite -using LinearAlgebra: norm - -# using Makie: Makie -using .GLMakie: Makie -using .Makie: lift, cam3d!, Point3f0, Vec3f0, Figure, Auto, linesegments!, Point2f0, RGBAf -using .Makie: DataAspect, Axis, LScene, SliderGrid -using ColorSchemes - -using ...TopOpt.TopOptProblems: getdim -using ..TrussTopOptProblems: TrussProblem -using ..TopOpt.TopOptProblems.Visualization: _create_colorbar - -function visualize( - problem::TrussProblem{xdim,T}; - u=undef, - topology=undef, - undeformed_mesh_color=RGBAf(0, 0, 0, 1.0), - cell_colors=undef, - draw_legend=false, - colormap=ColorSchemes.Spectral_10, - deformed_mesh_color=RGBAf(0, 1, 1, 0.4), - display_supports=true, - vector_arrowsize=0.3, - vector_linewidth=1.0, - default_support_scale=1e-2, - default_load_scale=1e-2, - scale_range=1.0, - default_exagg_scale=1.0, - exagg_range=10.0, - default_element_linewidth_scale=6.0, - element_linewidth_range=10.0, - kw..., -) where {xdim,T} - ndim = getdim(problem) - ncells = Ferrite.getncells(problem) - nnodes = Ferrite.getnnodes(problem) - given_u = u !== undef - topology = topology == undef ? ones(T, ncells) : topology - - fig = Figure() - if ndim == 2 - ax1 = Axis(fig[1, 1]) - # tightlimits!(ax1) - # ax1.aspect = AxisAspect(1) - ax1.aspect = DataAspect() - else - ax1 = LScene(fig[1, 1]; scenekw=(camera=cam3d!, raw=false)) #, height=750) - end - - # * linewidth scaling / support / load appearance / deformatione exaggeration control - linewidth_lsgrid = SliderGrid( - fig[2, 1], - ( - label="element linewidth", - range=0.0:0.01:element_linewidth_range, - format="{:.2f}", - startvalue=default_element_linewidth_scale, - ); - width=Auto(), - ) - if display_supports - condition_lsgrid = SliderGrid( - fig[3, 1], - ( - label="support scale", - range=0.0:0.01:scale_range, - format="{:.2f}", - startvalue=default_support_scale, - ), - ( - label="load scale", - range=0.0:0.01:scale_range, - format="{:.2f}", - startvalue=default_load_scale, - ); - width=Auto(), - ) - end - if given_u - deform_lsgrid = SliderGrid( - fig[4, 1], - ( - label="deformation exaggeration", - range=0.0:0.01:exagg_range, - format="{:.2f}", - startvalue=default_exagg_scale, - ); - width=Auto(), - ) - end - - # * undeformed truss elements - nodes = problem.truss_grid.grid.nodes - PtT = ndim == 2 ? Point2f0 : Point3f0 - edges_pts = [ - PtT(nodes[cell.nodes[1]].x) => PtT(nodes[cell.nodes[2]].x) for - cell in problem.truss_grid.grid.cells - ] - - # * linewidth and color per cell - scaled_cell_colors = similar(topology) - scaled_cell_colors .= 0.0 - if cell_colors !== undef - @assert length(cell_colors) == length(topology) "$(length(cell_colors)) , $(length(topology))" - val_range = maximum(cell_colors) - minimum(cell_colors) - scaled_cell_colors = (cell_colors .- minimum(cell_colors)) / val_range - end - if cell_colors !== undef && draw_legend - _create_colorbar(fig[1, 2], colormap, cell_colors) - end - - # linewidth: 2Xncells vector, 2i ~ 2i-1 represents a line's two endpoints' width - undeformed_mesh_colors = Vector{RGBAf}(undef, 2 * length(topology)) - topology_linewidth = similar(topology, 2 * length(topology)) - for i in eachindex(topology) - ccolor = undeformed_mesh_color - if cell_colors !== undef - ccolor = ColorSchemes.get(colormap, scaled_cell_colors[i]) - end - topology_linewidth[(2 * i - 1):(2 * i)] .= topology[i] - undeformed_mesh_colors[(2 * i - 1):(2 * i)] .= ccolor - end - element_linewidth = lift( - s -> topology_linewidth .* s, linewidth_lsgrid.sliders[1].value - ) - linesegments!(ax1, edges_pts; linewidth=element_linewidth, color=undeformed_mesh_colors) - - # # * deformed truss elements - if given_u - node_dofs = problem.metadata.node_dofs - @assert length(u) == ndim * nnodes - exagg_edge_pts = lift( - s -> [ - PtT(nodes[cell.nodes[1]].x) + PtT(u[node_dofs[:, cell.nodes[1]]] * s) => - PtT(nodes[cell.nodes[2]].x) + PtT(u[node_dofs[:, cell.nodes[2]]] * s) for - cell in problem.truss_grid.grid.cells - ], - deform_lsgrid.sliders[1].value, - ) - linesegments!( - ax1, exagg_edge_pts; linewidth=element_linewidth, color=deformed_mesh_color - ) - end - - if display_supports - # * load vectors - loaded_nodes = [PtT(nodes[node_id].x) for node_id in keys(problem.force)] - scaled_load_dirs = lift( - s -> [PtT(force / norm(force) * s) for force in values(problem.force)], - condition_lsgrid.sliders[2].value, - ) - Makie.arrows!( - ax1, - loaded_nodes, - scaled_load_dirs; - arrowcolor=:purple, - arrowsize=vector_arrowsize, - linecolor=:purple, - linewidth=vector_linewidth, - ) - Makie.scatter!(ax1, loaded_nodes) #, markersize = lift(s -> s * 3, lsgrid.sliders[1].value)) - - # * fixties vectors - ch = problem.ch - for (dbc_id, dbc) in enumerate(ch.dbcs) - support_vectors = [] - node_ids = dbc.faces - if 1 in dbc.components - push!(support_vectors, [1.0, 0.0, 0.0]) - end - if 2 in dbc.components - push!(support_vectors, [0.0, 1.0, 0.0]) - end - if 3 in dbc.components - push!(support_vectors, [0.0, 0.0, 1.0]) - end - fixed_nodes = PtT.(nodes[node_ind].x for node_ind in node_ids) - for v in support_vectors - Makie.arrows!( - fixed_nodes, - lift( - s -> [PtT(s .* v) for nid in node_ids], - condition_lsgrid.sliders[1].value, - ); - arrowcolor=:orange, - arrowsize=vector_arrowsize, - linecolor=:orange, - linewidth=vector_linewidth, - ) - end - Makie.scatter!(ax1, fixed_nodes) #, markersize = lift(s -> s * 3, lsgrid.sliders[1].value)) - end - end # end if display_supports - - return fig -end diff --git a/src/Utilities/Utilities.jl b/src/Utilities/Utilities.jl index 274162b3..67b6dd8a 100644 --- a/src/Utilities/Utilities.jl +++ b/src/Utilities/Utilities.jl @@ -1,6 +1,6 @@ module Utilities -using ForwardDiff, Ferrite, IterativeSolvers, Requires, StaticArrays, LinearAlgebra +using ForwardDiff, Ferrite, IterativeSolvers, StaticArrays, LinearAlgebra export AbstractPenalty, PowerPenalty, diff --git a/test/Functions/test_truss_stress_fns.jl b/test/Functions/test_truss_stress_fns.jl index ae8b7d29..6b3e9015 100644 --- a/test/Functions/test_truss_stress_fns.jl +++ b/test/Functions/test_truss_stress_fns.jl @@ -29,7 +29,6 @@ Input JSON file name "testfile2.json" using TopOpt #using Makie, GLMakie -#using TopOpt.TrussTopOptProblems.TrussVisualization: visualize #using ColorSchemes @testset "TrussStress" begin diff --git a/test/examples/local_stress.jl b/test/examples/local_stress.jl index e125af98..31fb8bae 100644 --- a/test/examples/local_stress.jl +++ b/test/examples/local_stress.jl @@ -2,7 +2,6 @@ using TopOpt, LinearAlgebra, StatsFuns, Test using StatsFuns: logsumexp # using Makie, CairoMakie -# using TopOpt.TopOptProblems.Visualization: visualize E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio diff --git a/test/examples/mixed_integer_truss/truss_compliance_2d1.jl b/test/examples/mixed_integer_truss/truss_compliance_2d1.jl index fa1737cb..35c74de8 100644 --- a/test/examples/mixed_integer_truss/truss_compliance_2d1.jl +++ b/test/examples/mixed_integer_truss/truss_compliance_2d1.jl @@ -1,6 +1,5 @@ using TopOpt, LinearAlgebra, StatsFuns # using Makie, GLMakie -# using TopOpt.TrussTopOptProblems.TrussVisualization: visualize Nonconvex.@load Juniper diff --git a/test/examples/neural.jl b/test/examples/neural.jl index d09b9a8a..5321785f 100644 --- a/test/examples/neural.jl +++ b/test/examples/neural.jl @@ -128,6 +128,5 @@ tf(res2.minimizer) using Makie, CairoMakie topology = filter(tf(res2.minimizer)) -using TopOpt.TopOptProblems.Visualization: visualize visualize(problem; topology) =# diff --git a/test/truss_topopt_problems/test_buckling.jl b/test/truss_topopt_problems/test_buckling.jl index 5db81232..9713eb49 100644 --- a/test/truss_topopt_problems/test_buckling.jl +++ b/test/truss_topopt_problems/test_buckling.jl @@ -135,6 +135,5 @@ gm_ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); # # lines!(tip_displacement[:,1], load_parameters.*P_val, color = :blue, linewidth = 3) # # fig -# # using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # # fig = visualize(problem; u=u1) # end # end test set diff --git a/test/truss_topopt_problems/test_buckling_optimize.jl b/test/truss_topopt_problems/test_buckling_optimize.jl index fcb58b32..5f6b6987 100644 --- a/test/truss_topopt_problems/test_buckling_optimize.jl +++ b/test/truss_topopt_problems/test_buckling_optimize.jl @@ -79,7 +79,6 @@ gm_ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); # @test smallest_pos_eigval >= 1.0 # # using Makie -# # using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # # fig = visualize(problem; topology=r.minimizer) # end @@ -184,7 +183,6 @@ gm_ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); # using Makie # import GLMakie - # using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # fig = visualize(problem; topology=x0) # Makie.display(fig) diff --git a/test/truss_topopt_problems/test_fea.jl b/test/truss_topopt_problems/test_fea.jl index bfb57935..82d34dc9 100644 --- a/test/truss_topopt_problems/test_fea.jl +++ b/test/truss_topopt_problems/test_fea.jl @@ -9,7 +9,6 @@ using TopOpt.TopOptProblems: using TopOpt.TrussTopOptProblems: getA, default_quad_order, compute_local_axes # if get(ENV, "CI", nothing) != "true" # import Makie -# using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # end include("utils.jl") diff --git a/test/truss_topopt_problems/test_problem.jl b/test/truss_topopt_problems/test_problem.jl index 4ff67528..1c668153 100644 --- a/test/truss_topopt_problems/test_problem.jl +++ b/test/truss_topopt_problems/test_problem.jl @@ -5,7 +5,6 @@ using TopOpt.TrussTopOptProblems: load_truss_json, load_truss_geo using Base.Iterators # if get(ENV, "CI", nothing) != "true" # import Makie -# @eval using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # end ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); diff --git a/test/wcsmo14/demos/continuum/cont_compliance1.jl b/test/wcsmo14/demos/continuum/cont_compliance1.jl index 52f60347..b3b6c7a6 100644 --- a/test/wcsmo14/demos/continuum/cont_compliance1.jl +++ b/test/wcsmo14/demos/continuum/cont_compliance1.jl @@ -2,7 +2,6 @@ module ContComplianceDemo1 using TopOpt, LinearAlgebra, StatsFuns # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio diff --git a/test/wcsmo14/demos/continuum/cont_compliance2.jl b/test/wcsmo14/demos/continuum/cont_compliance2.jl index 5c4bc7ce..97fb7733 100644 --- a/test/wcsmo14/demos/continuum/cont_compliance2.jl +++ b/test/wcsmo14/demos/continuum/cont_compliance2.jl @@ -2,7 +2,6 @@ module ContComplianceDemo2 using TopOpt, LinearAlgebra, StatsFuns # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio diff --git a/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb b/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb index 805f8a92..29a31b07 100644 --- a/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb +++ b/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb @@ -7,8 +7,7 @@ "metadata": {}, "outputs": [], "source": [ - "using Makie, GLMakie, TopOpt, LinearAlgebra, StatsFuns\n", - "using TopOpt.TopOptProblems.Visualization: visualize" + "using Makie, GLMakie, TopOpt, LinearAlgebra, StatsFuns", ] }, { diff --git a/test/wcsmo14/demos/continuum/cont_stress.jl b/test/wcsmo14/demos/continuum/cont_stress.jl index 79e199a2..44486dfb 100644 --- a/test/wcsmo14/demos/continuum/cont_stress.jl +++ b/test/wcsmo14/demos/continuum/cont_stress.jl @@ -2,7 +2,6 @@ module ContStressDemo using TopOpt, LinearAlgebra, StatsFuns # using Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio diff --git a/test/wcsmo14/demos/truss/truss_compliance_2d1.jl b/test/wcsmo14/demos/truss/truss_compliance_2d1.jl index 8886f6dd..eeaf1a26 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_2d1.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_2d1.jl @@ -2,7 +2,6 @@ module TrussComplianceDemo2D1 using TopOpt, LinearAlgebra, StatsFuns #using Makie, GLMakie -#using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # 2D ndim = 2 diff --git a/test/wcsmo14/demos/truss/truss_compliance_2d2.jl b/test/wcsmo14/demos/truss/truss_compliance_2d2.jl index 50a5b63c..04fc0033 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_2d2.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_2d2.jl @@ -2,7 +2,6 @@ module TrussComplianceDemo2D2 using TopOpt, LinearAlgebra, StatsFuns #using Makie, GLMakie -#using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # 2D ndim = 2 diff --git a/test/wcsmo14/demos/truss/truss_compliance_3d1.jl b/test/wcsmo14/demos/truss/truss_compliance_3d1.jl index 66edeca5..4b1a7837 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_3d1.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_3d1.jl @@ -2,7 +2,6 @@ module TrussComplianceDemo3D1 using TopOpt, LinearAlgebra, StatsFuns # using Makie, GLMakie -# using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # 3D ndim = 3 diff --git a/test/wcsmo14/demos/truss/truss_compliance_3d2.jl b/test/wcsmo14/demos/truss/truss_compliance_3d2.jl index 583d14fe..c8437fc4 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_3d2.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_3d2.jl @@ -2,7 +2,6 @@ module TrussComplianceDemo3D2 using TopOpt, LinearAlgebra, StatsFuns #using Makie, GLMakie -#using TopOpt.TrussTopOptProblems.TrussVisualization: visualize # 3D ndim = 3 diff --git a/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl b/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl index 64cd094b..bba770ad 100644 --- a/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl +++ b/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl @@ -1,6 +1,5 @@ using TopOpt # import Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize using TimerOutputs diff --git a/test/wcsmo14/jl_benchmarks/compare_polytop.jl b/test/wcsmo14/jl_benchmarks/compare_polytop.jl index 84a3a04d..b83d59e5 100644 --- a/test/wcsmo14/jl_benchmarks/compare_polytop.jl +++ b/test/wcsmo14/jl_benchmarks/compare_polytop.jl @@ -1,6 +1,5 @@ using TopOpt # import Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize using TimerOutputs diff --git a/test/wcsmo14/jl_benchmarks/compare_top3d.jl b/test/wcsmo14/jl_benchmarks/compare_top3d.jl index d4e248f3..811dc0d3 100644 --- a/test/wcsmo14/jl_benchmarks/compare_top3d.jl +++ b/test/wcsmo14/jl_benchmarks/compare_top3d.jl @@ -1,7 +1,6 @@ using TopOpt Nonconvex.@load Ipopt # import Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize using Suppressor diff --git a/test/wcsmo14/jl_benchmarks/compare_top3d125.jl b/test/wcsmo14/jl_benchmarks/compare_top3d125.jl index a57f0a71..6ae8d22d 100644 --- a/test/wcsmo14/jl_benchmarks/compare_top3d125.jl +++ b/test/wcsmo14/jl_benchmarks/compare_top3d125.jl @@ -1,6 +1,5 @@ using TopOpt # import Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize using Suppressor using FromFile diff --git a/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl b/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl index 23d05531..ff5804e0 100644 --- a/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl +++ b/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl @@ -1,6 +1,5 @@ using TopOpt # import Makie, GLMakie -# using TopOpt.TopOptProblems.Visualization: visualize using TimerOutputs using Suppressor From beb319da625a94cfbe3c53f26b174d03cb51d46f Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 10:16:08 +0400 Subject: [PATCH 2/8] test the plotting code in scripts using CairoMakie as a backend --- docs/src/literate/beso.jl | 12 +++---- docs/src/literate/csimp.jl | 21 +++++++----- docs/src/literate/geso.jl | 12 +++---- docs/src/literate/global_stress.jl | 12 +++---- docs/src/literate/local_stress.jl | 22 ++++++++----- docs/src/literate/simp.jl | 31 ++++++++--------- ext/TopOptMakieExt/TopOptMakieExt.jl | 2 +- test/Functions/test_truss_stress_fns.jl | 30 +++++++++-------- test/Project.toml | 2 ++ test/examples/local_stress.jl | 19 +++++++---- .../truss_compliance_2d1.jl | 11 +++---- test/examples/neural.jl | 6 ++-- .../test_buckling_optimize.jl | 16 ++++----- test/truss_topopt_problems/test_fea.jl | 13 +++----- test/truss_topopt_problems/test_problem.jl | 33 +++++++++---------- .../demos/continuum/cont_compliance1.jl | 11 ++++--- .../demos/continuum/cont_compliance2.jl | 11 ++++--- .../demos/continuum/cont_compliance3_3D.ipynb | 3 +- test/wcsmo14/demos/continuum/cont_stress.jl | 11 ++++--- .../demos/truss/truss_compliance_2d1.jl | 11 +++---- .../demos/truss/truss_compliance_2d2.jl | 11 +++---- .../demos/truss/truss_compliance_3d1.jl | 13 ++++---- .../demos/truss/truss_compliance_3d2.jl | 11 +++---- .../wcsmo14/jl_benchmarks/compare_neo99_2D.jl | 21 ++++++++---- test/wcsmo14/jl_benchmarks/compare_polytop.jl | 21 ++++++++---- test/wcsmo14/jl_benchmarks/compare_top3d.jl | 22 +++++++++---- .../wcsmo14/jl_benchmarks/compare_top3d125.jl | 25 +++++++++----- .../jl_benchmarks/compare_topopt_py.jl | 20 +++++++---- 28 files changed, 250 insertions(+), 183 deletions(-) diff --git a/docs/src/literate/beso.jl b/docs/src/literate/beso.jl index 3185817f..3420004a 100644 --- a/docs/src/literate/beso.jl +++ b/docs/src/literate/beso.jl @@ -33,12 +33,12 @@ x0 = ones(length(solver.vars)) result = beso(x0) # ### (Optional) Visualize the result using Makie.jl -# Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first -# ```julia -# using Makie, GLMakie -# fig = visualize(problem; topology = result.topology) -# Makie.display(fig) -# ``` +# Need to run `using Pkg; Pkg.add("Makie")` first and either `Pkg.add("CairoMakie")` or `Pkg.add("GLMakie")` +using Makie +using CairoMakie +# alternatively, `using GLMakie` +fig = visualize(problem; topology=result.topology) +Makie.display(fig) #md # ## [Plain Program](@id beso-plain-program) #md # diff --git a/docs/src/literate/csimp.jl b/docs/src/literate/csimp.jl index e6639fae..95024235 100644 --- a/docs/src/literate/csimp.jl +++ b/docs/src/literate/csimp.jl @@ -74,14 +74,19 @@ end @show constr(x) # ### (Optional) Visualize the result using Makie.jl -# Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first -# ```julia -# using Makie, GLMakie -# fig = visualize(problem; topology = x, default_exagg_scale = 0.07, -# scale_range = 10.0, vector_linewidth = 3, vector_arrowsize = 0.5, -# ) -# Makie.display(fig) -# ``` +# Need to run `using Pkg; Pkg.add("Makie")` first and either `Pkg.add("CairoMakie")` or `Pkg.add("GLMakie")` +using Makie +using CairoMakie +# alternatively, `using GLMakie` +fig = visualize( + problem; + topology=x, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.5, +) +Makie.display(fig) #md # ## [Plain Program](@id csimp-plain-program) #md # diff --git a/docs/src/literate/geso.jl b/docs/src/literate/geso.jl index 4041cf3a..69e12a84 100644 --- a/docs/src/literate/geso.jl +++ b/docs/src/literate/geso.jl @@ -33,12 +33,12 @@ x0 = ones(length(solver.vars)) result = geso(x0) # ### (Optional) Visualize the result using Makie.jl -# Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first -# ```julia -# using Makie, GLMakie -# fig = visualize(problem; topology = result.topology) -# Makie.display(fig) -# ``` +# Need to run `using Pkg; Pkg.add("Makie")` first and either `Pkg.add("CairoMakie")` or `Pkg.add("GLMakie")` +using Makie +using CairoMakie +# alternatively, `using GLMakie` +fig = visualize(problem; topology=result.topology) +Makie.display(fig) #md # ## [Plain Program](@id geso-plain-program) #md # diff --git a/docs/src/literate/global_stress.jl b/docs/src/literate/global_stress.jl index a1896cbe..8d0d67f8 100644 --- a/docs/src/literate/global_stress.jl +++ b/docs/src/literate/global_stress.jl @@ -68,12 +68,12 @@ r = optimize(model, alg, x0; options) @show constr(r.minimizer) # ### (Optional) Visualize the result using Makie.jl -# Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first -# ```julia -# using Makie, GLMakie -# fig = visualize(problem; topology = r.minimizer) -# Makie.display(fig) -# ``` +# Need to run `using Pkg; Pkg.add("Makie")` first and either `Pkg.add("CairoMakie")` or `Pkg.add("GLMakie")` +using Makie +using CairoMakie +# alternatively, `using GLMakie` +fig = visualize(problem; topology=r.minimizer) +Makie.display(fig) #md # ## [Plain Program](@id global-stress-plain-program) #md # diff --git a/docs/src/literate/local_stress.jl b/docs/src/literate/local_stress.jl index 293a58de..aaf72232 100644 --- a/docs/src/literate/local_stress.jl +++ b/docs/src/literate/local_stress.jl @@ -47,15 +47,19 @@ maximum(stress(filter(PseudoDensities(x0)))) maximum(stress(filter(PseudoDensities(x)))) # ### (Optional) Visualize the result using Makie.jl -# Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first -# ```julia -# using Makie, GLMakie -# fig = visualize( -# problem; topology = r.minimizer, default_exagg_scale = 0.07, -# scale_range = 10.0, vector_linewidth = 3, vector_arrowsize = 0.5, -# ) -# Makie.display(fig) -# ``` +# Need to run `using Pkg; Pkg.add("Makie")` first and either `Pkg.add("CairoMakie")` or `Pkg.add("GLMakie")` +using Makie +using CairoMakie +# alternatively, `using GLMakie` +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.5, +) +Makie.display(fig) #md # ## [Plain Program](@id local-stress-plain-program) #md # diff --git a/docs/src/literate/simp.jl b/docs/src/literate/simp.jl index 00c9fbca..adb17a9f 100644 --- a/docs/src/literate/simp.jl +++ b/docs/src/literate/simp.jl @@ -57,24 +57,25 @@ r = optimize(model, alg, x0; options) @show obj(r.minimizer) # ### (Optional) Visualize the result using Makie.jl -# Need to run `using Pkg; Pkg.add(["Makie", "GLMakie"])` first -# ```julia -# using Makie, GLMakie -# fig = visualize( -# problem; topology = r.minimizer, -# default_exagg_scale = 0.07, scale_range = 10.0, -# vector_linewidth = 3, vector_arrowsize = 0.5, -# ) -# Makie.display(fig) -# ``` +# Need to run `using Pkg; Pkg.add("Makie")` first and either `Pkg.add("CairoMakie")` or `Pkg.add("GLMakie")` +using Makie +using CairoMakie +# alternatively, `using GLMakie` +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.5, +) +Makie.display(fig) # or convert it to a Mesh # Need to run `using Pkg; Pkg.add(GeometryBasics)` first -# ```julia -# import Makie, GeometryBasics -# result_mesh = GeometryBasics.Mesh(problem, r.minimizer); -# Makie.mesh(result_mesh) -# ``` +using Makie, GeometryBasics +result_mesh = GeometryBasics.Mesh(problem, r.minimizer); +Makie.mesh(result_mesh) #md # ## [Plain Program](@id simp-plain-program) #md # diff --git a/ext/TopOptMakieExt/TopOptMakieExt.jl b/ext/TopOptMakieExt/TopOptMakieExt.jl index 9477412d..d75e2042 100644 --- a/ext/TopOptMakieExt/TopOptMakieExt.jl +++ b/ext/TopOptMakieExt/TopOptMakieExt.jl @@ -6,7 +6,7 @@ using Makie: DataAspect, Axis, LScene, SliderGrid, linesegments!, Point2f0 using Makie.GeometryBasics using ColorSchemes using GeometryBasics: GLTriangleFace -import TopOpt +using TopOpt: TopOpt using TopOpt.TopOptProblems: getcloaddict, boundingbox, getdim, StiffnessTopOptProblem using TopOpt.TrussTopOptProblems: TrussProblem using Ferrite diff --git a/test/Functions/test_truss_stress_fns.jl b/test/Functions/test_truss_stress_fns.jl index 6b3e9015..a8bbcd69 100644 --- a/test/Functions/test_truss_stress_fns.jl +++ b/test/Functions/test_truss_stress_fns.jl @@ -28,8 +28,10 @@ Input JSON file name "testfile2.json" # 3 -> -70.7106 [50sqrt(2) , compression] using TopOpt -#using Makie, GLMakie -#using ColorSchemes +using Makie +using CairoMakie +# using GLMakie +using ColorSchemes @testset "TrussStress" begin # Hand calculated result. @@ -69,15 +71,17 @@ using TopOpt @assert abs(σ[3] - result_stress[3]) < 1e-12 #visualization - #color_per_cell = abs.(σ.*x) - # fig1 = visualize( - # problem, u = fill(0.1, nnodes*ndim), topology=x, - # default_exagg_scale=0.0 - # ,default_element_linewidth_scale = 5.0 - # ,default_load_scale = 0.5 - # ,default_support_scale = 0.1 - # ,cell_colors = color_per_cell - # ,colormap = ColorSchemes.Spectral_10 - # ) - # Makie.display(fig1) + color_per_cell = abs.(σ .* x) + fig1 = visualize( + problem; + u=fill(0.1, nnodes * ndim), + topology=x, + default_exagg_scale=0.0, + default_element_linewidth_scale=5.0, + default_load_scale=0.5, + default_support_scale=0.1, + cell_colors=color_per_cell, + colormap=ColorSchemes.Spectral_10, + ) + Makie.display(fig1) end diff --git a/test/Project.toml b/test/Project.toml index b0131194..8f47f803 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -1,5 +1,6 @@ [deps] Arpack = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97" +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" Ferrite = "c061ca5d-56c9-439f-9c0e-210fe06d3992" FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" @@ -8,6 +9,7 @@ FromFile = "ff7dd447-1dcb-4ce3-b8ac-22a812192de7" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" Juniper = "2ddba703-00a4-53a7-87a5-e8b9971dde84" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" Nonconvex = "01bcebdf-4d21-426d-b5c4-6132c1619978" NonconvexIpopt = "bf347577-a06d-49ad-a669-8c0e005493b8" Pkg = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f" diff --git a/test/examples/local_stress.jl b/test/examples/local_stress.jl index 31fb8bae..af6b298d 100644 --- a/test/examples/local_stress.jl +++ b/test/examples/local_stress.jl @@ -1,7 +1,9 @@ # using Revise using TopOpt, LinearAlgebra, StatsFuns, Test using StatsFuns: logsumexp -# using Makie, CairoMakie +using Makie +using CairoMakie +# using GLMakie E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio @@ -67,9 +69,12 @@ end s = stress(filter(PseudoDensities(x))) @test (maximum(s) - threshold) / threshold < 0.01 -# f = visualize( -# problem; topology = filter(PseudoDensities(x)), default_exagg_scale = 0.07, -# scale_range = 10.0, vector_linewidth = 3, vector_arrowsize = 0.5, -# ) -# savefig("result.png", _f) -# end +f = visualize( + problem; + topology=filter(PseudoDensities(x)), + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.5, +) +savefig("result.png", _f) diff --git a/test/examples/mixed_integer_truss/truss_compliance_2d1.jl b/test/examples/mixed_integer_truss/truss_compliance_2d1.jl index 35c74de8..be02be3d 100644 --- a/test/examples/mixed_integer_truss/truss_compliance_2d1.jl +++ b/test/examples/mixed_integer_truss/truss_compliance_2d1.jl @@ -1,5 +1,7 @@ using TopOpt, LinearAlgebra, StatsFuns -# using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie Nonconvex.@load Juniper @@ -41,8 +43,5 @@ TopOpt.setpenalty!(solver, p) @show obj(r.minimizer) @show constr(r.minimizer) -# fig = visualize( -# problem; solver.u, topology = r.minimizer, -# default_exagg_scale=0.0 -# ) -# Makie.display(fig) +fig = visualize(problem; solver.u, topology=r.minimizer, default_exagg_scale=0.0) +Makie.display(fig) diff --git a/test/examples/neural.jl b/test/examples/neural.jl index 5321785f..726b42c3 100644 --- a/test/examples/neural.jl +++ b/test/examples/neural.jl @@ -124,9 +124,9 @@ Zygote.gradient(constr, p0) tf(res2.minimizer) -#= -using Makie, CairoMakie +using Makie +using CairoMakie +# using GLMakie topology = filter(tf(res2.minimizer)) visualize(problem; topology) -=# diff --git a/test/truss_topopt_problems/test_buckling_optimize.jl b/test/truss_topopt_problems/test_buckling_optimize.jl index 5f6b6987..a914bd1a 100644 --- a/test/truss_topopt_problems/test_buckling_optimize.jl +++ b/test/truss_topopt_problems/test_buckling_optimize.jl @@ -6,6 +6,10 @@ using NonconvexIpopt using TopOpt using Arpack +using Makie +using CairoMakie +# using GLMakie + fea_ins_dir = joinpath(@__DIR__, "instances", "fea_examples"); gm_ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); @@ -78,7 +82,6 @@ gm_ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); # smallest_pos_eigval = 1/sparse_eigvals[1] # @test smallest_pos_eigval >= 1.0 -# # using Makie # # fig = visualize(problem; topology=r.minimizer) # end @@ -181,12 +184,9 @@ gm_ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); @test 0 < minimum(ev2) < 0.02 @test maximum(ev2) ≈ 3250 rtol = 0.001 - # using Makie - # import GLMakie - - # fig = visualize(problem; topology=x0) - # Makie.display(fig) + fig = visualize(problem; topology=x0) + Makie.display(fig) - # fig = visualize(problem; topology=r2.minimizer) - # Makie.display(fig) + fig = visualize(problem; topology=r2.minimizer) + Makie.display(fig) end diff --git a/test/truss_topopt_problems/test_fea.jl b/test/truss_topopt_problems/test_fea.jl index 82d34dc9..7c14129b 100644 --- a/test/truss_topopt_problems/test_fea.jl +++ b/test/truss_topopt_problems/test_fea.jl @@ -7,9 +7,9 @@ using TopOpt using TopOpt.TopOptProblems: boundingbox, nnodespercell, getgeomorder, getmetadata, getdh, getE, getdim using TopOpt.TrussTopOptProblems: getA, default_quad_order, compute_local_axes -# if get(ENV, "CI", nothing) != "true" -# import Makie -# end +using Makie +using CairoMakie +# using GLMakie include("utils.jl") @@ -73,11 +73,8 @@ ins_dir = joinpath(@__DIR__, "instances", "fea_examples"); solver = FEASolver(Direct, problem) solver() - # if get(ENV, "CI", nothing) != "true" - # ## TODO plot analysis result with - # fig = visualize(problem; u=solver.u) - # Makie.display(fig) - # end + fig = visualize(problem; u=solver.u) + Makie.display(fig) # we use kN for force and m for length # thus, pressure/modulus is in kN/m diff --git a/test/truss_topopt_problems/test_problem.jl b/test/truss_topopt_problems/test_problem.jl index 1c668153..dc626531 100644 --- a/test/truss_topopt_problems/test_problem.jl +++ b/test/truss_topopt_problems/test_problem.jl @@ -3,9 +3,9 @@ using TopOpt using TopOpt.TopOptProblems: getE using TopOpt.TrussTopOptProblems: load_truss_json, load_truss_geo using Base.Iterators -# if get(ENV, "CI", nothing) != "true" -# import Makie -# end +using Makie +using CairoMakie +# using GLMakie ins_dir = joinpath(@__DIR__, "instances", "ground_meshes"); @@ -78,15 +78,17 @@ end ) println("$(result.convstate)") - # if get(ENV, "CI", nothing) != "true" - # solver() - # fig = visualize( - # problem, u=solver.u, topology = result.topology, vector_arrowsize = 0.1, - # vector_linewidth=0.8, default_exagg_scale=ndim == 3 ? 1.0 : 0.01, - # exagg_range = ndim == 3 ? 10.0 : 0.1, - # ) - # Makie.display(fig) - # end + solver() + fig = visualize( + problem; + u=solver.u, + topology=result.topology, + vector_arrowsize=0.1, + vector_linewidth=0.8, + default_exagg_scale=ndim == 3 ? 1.0 : 0.01, + exagg_range=ndim == 3 ? 10.0 : 0.1, + ) + Makie.display(fig) end # end testset @testset "PointLoadCantileverTruss" for dim in [2, 3] @@ -121,9 +123,6 @@ end # end testset TopOpt.setpenalty!(solver, penalty.p) result = Nonconvex.optimize(m, MMA87(), x0; options=options) - # if get(ENV, "CI", nothing) != "true" - # fig = visualize( - # problem; topology = result.minimizer) - # Makie.display(fig) - # end + fig = visualize(problem; topology=result.minimizer) + Makie.display(fig) end # end testset diff --git a/test/wcsmo14/demos/continuum/cont_compliance1.jl b/test/wcsmo14/demos/continuum/cont_compliance1.jl index b3b6c7a6..65bcdb78 100644 --- a/test/wcsmo14/demos/continuum/cont_compliance1.jl +++ b/test/wcsmo14/demos/continuum/cont_compliance1.jl @@ -1,7 +1,9 @@ module ContComplianceDemo1 using TopOpt, LinearAlgebra, StatsFuns -# using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio @@ -40,8 +42,9 @@ TopOpt.setpenalty!(solver, p) @show obj(r.minimizer) @show constr(r.minimizer) topology = cheqfilter(PseudoDensities(r.minimizer)).x -# fig = visualize(problem; solver.u, -# topology = topology, default_exagg_scale=0.0, scale_range=10.0) -# Makie.display(fig) +fig = visualize( + problem; solver.u, topology=topology, default_exagg_scale=0.0, scale_range=10.0 +) +Makie.display(fig) end diff --git a/test/wcsmo14/demos/continuum/cont_compliance2.jl b/test/wcsmo14/demos/continuum/cont_compliance2.jl index 97fb7733..4558f8e8 100644 --- a/test/wcsmo14/demos/continuum/cont_compliance2.jl +++ b/test/wcsmo14/demos/continuum/cont_compliance2.jl @@ -1,7 +1,9 @@ module ContComplianceDemo2 using TopOpt, LinearAlgebra, StatsFuns -# using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio @@ -45,8 +47,9 @@ TopOpt.setpenalty!(solver, p) @show constr(r.minimizer) @show maximum(stress(cheqfilter(PseudoDensities(r.minimizer)))) topology = cheqfilter(PseudoDensities(r.minimizer)).x -# fig = visualize(problem; solver.u, -# topology = topology, default_exagg_scale=0.0, scale_range=10.0) -# Makie.display(fig) +fig = visualize( + problem; solver.u, topology=topology, default_exagg_scale=0.0, scale_range=10.0 +) +Makie.display(fig) end diff --git a/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb b/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb index 29a31b07..54ee4887 100644 --- a/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb +++ b/test/wcsmo14/demos/continuum/cont_compliance3_3D.ipynb @@ -7,7 +7,8 @@ "metadata": {}, "outputs": [], "source": [ - "using Makie, GLMakie, TopOpt, LinearAlgebra, StatsFuns", + "using Makie, CairoMakie, TopOpt, LinearAlgebra, StatsFuns"\n, + "# using GLMakie" ] }, { diff --git a/test/wcsmo14/demos/continuum/cont_stress.jl b/test/wcsmo14/demos/continuum/cont_stress.jl index 44486dfb..4b22bd99 100644 --- a/test/wcsmo14/demos/continuum/cont_stress.jl +++ b/test/wcsmo14/demos/continuum/cont_stress.jl @@ -1,7 +1,9 @@ module ContStressDemo using TopOpt, LinearAlgebra, StatsFuns -# using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie E = 1.0 # Young’s modulus v = 0.3 # Poisson’s ratio @@ -52,8 +54,9 @@ TopOpt.setpenalty!(solver, p) @show constr2(r.minimizer) @show maximum(stress(cheqfilter(PseudoDensities(r.minimizer)))) topology = cheqfilter(PseudoDensities(r.minimizer)).x -# fig = visualize(problem; solver.u, -# topology = topology, default_exagg_scale=0.0, scale_range=10.0) -# Makie.display(fig) +fig = visualize( + problem; solver.u, topology=topology, default_exagg_scale=0.0, scale_range=10.0 +) +Makie.display(fig) end diff --git a/test/wcsmo14/demos/truss/truss_compliance_2d1.jl b/test/wcsmo14/demos/truss/truss_compliance_2d1.jl index eeaf1a26..d376cab6 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_2d1.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_2d1.jl @@ -1,7 +1,9 @@ module TrussComplianceDemo2D1 using TopOpt, LinearAlgebra, StatsFuns -#using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie # 2D ndim = 2 @@ -46,10 +48,7 @@ TopOpt.setpenalty!(solver, p) @show obj(r.minimizer) @show constr(r.minimizer) -#fig = visualize( -# problem; solver.u, topology = r.minimizer, -# default_exagg_scale=0.0 -#) -#Makie.display(fig) +fig = visualize(problem; solver.u, topology=r.minimizer, default_exagg_scale=0.0) +Makie.display(fig) end diff --git a/test/wcsmo14/demos/truss/truss_compliance_2d2.jl b/test/wcsmo14/demos/truss/truss_compliance_2d2.jl index 04fc0033..807eff23 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_2d2.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_2d2.jl @@ -1,7 +1,9 @@ module TrussComplianceDemo2D2 using TopOpt, LinearAlgebra, StatsFuns -#using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie # 2D ndim = 2 @@ -43,10 +45,7 @@ TopOpt.setpenalty!(solver, p) @show obj(r.minimizer) @show constr(r.minimizer) -#fig = visualize( -# problem; solver.u, topology = r.minimizer, -# default_exagg_scale=0.0 -#) -#Makie.display(fig) +fig = visualize(problem; solver.u, topology=r.minimizer, default_exagg_scale=0.0) +Makie.display(fig) end diff --git a/test/wcsmo14/demos/truss/truss_compliance_3d1.jl b/test/wcsmo14/demos/truss/truss_compliance_3d1.jl index 4b1a7837..f2201852 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_3d1.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_3d1.jl @@ -1,7 +1,9 @@ module TrussComplianceDemo3D1 using TopOpt, LinearAlgebra, StatsFuns -# using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie # 3D ndim = 3 @@ -44,10 +46,9 @@ TopOpt.setpenalty!(solver, p) @show r.minimizer @show obj(r.minimizer) @show constr(r.minimizer) -# fig = visualize( -# problem; u=solver.u, topology = r.minimizer, -# default_exagg_scale=0.0, exagg_range=100.0, -# ) -# Makie.display(fig) +fig = visualize( + problem; u=solver.u, topology=r.minimizer, default_exagg_scale=0.0, exagg_range=100.0 +) +Makie.display(fig) end diff --git a/test/wcsmo14/demos/truss/truss_compliance_3d2.jl b/test/wcsmo14/demos/truss/truss_compliance_3d2.jl index c8437fc4..b71cf4d6 100644 --- a/test/wcsmo14/demos/truss/truss_compliance_3d2.jl +++ b/test/wcsmo14/demos/truss/truss_compliance_3d2.jl @@ -1,7 +1,9 @@ module TrussComplianceDemo3D2 using TopOpt, LinearAlgebra, StatsFuns -#using Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie # 3D ndim = 3 @@ -43,10 +45,7 @@ TopOpt.setpenalty!(solver, p) @show obj(r.minimizer) @show constr(r.minimizer) -#fig = visualize( -# problem; solver.u, topology = r.minimizer, -# default_exagg_scale=0.0 -#) -#Makie.display(fig) +fig = visualize(problem; solver.u, topology=r.minimizer, default_exagg_scale=0.0) +Makie.display(fig) end diff --git a/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl b/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl index bba770ad..779bd0a1 100644 --- a/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl +++ b/test/wcsmo14/jl_benchmarks/compare_neo99_2D.jl @@ -1,5 +1,7 @@ using TopOpt -# import Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie using TimerOutputs @@ -62,8 +64,15 @@ show(to) @show obj(r.minimizer) @show constr(r.minimizer) -# # # Visualize the result using Makie.jl -# fig = visualize(problem; topology=r.minimizer, -# default_exagg_scale=0.07, scale_range=10.0, vector_linewidth=3, vector_arrowsize=0.005, -# default_support_scale=0.01, default_load_scale=0.01) -# Makie.display(fig) +# Visualize the result using Makie.jl +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.005, + default_support_scale=0.01, + default_load_scale=0.01, +) +Makie.display(fig) diff --git a/test/wcsmo14/jl_benchmarks/compare_polytop.jl b/test/wcsmo14/jl_benchmarks/compare_polytop.jl index b83d59e5..99a79da7 100644 --- a/test/wcsmo14/jl_benchmarks/compare_polytop.jl +++ b/test/wcsmo14/jl_benchmarks/compare_polytop.jl @@ -1,5 +1,7 @@ using TopOpt -# import Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie using TimerOutputs @@ -63,8 +65,15 @@ show(to) @show obj(r.minimizer) -# # # Visualize the result using Makie.jl -# fig = visualize(problem; topology=r.minimizer, -# default_exagg_scale=0.07, scale_range=10.0, vector_linewidth=3, vector_arrowsize=0.005, -# default_support_scale=0.01, default_load_scale=0.01) -# Makie.display(fig) +# Visualize the result using Makie.jl +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.005, + default_support_scale=0.01, + default_load_scale=0.01, +) +Makie.display(fig) diff --git a/test/wcsmo14/jl_benchmarks/compare_top3d.jl b/test/wcsmo14/jl_benchmarks/compare_top3d.jl index 811dc0d3..8199b6d5 100644 --- a/test/wcsmo14/jl_benchmarks/compare_top3d.jl +++ b/test/wcsmo14/jl_benchmarks/compare_top3d.jl @@ -1,6 +1,8 @@ using TopOpt Nonconvex.@load Ipopt -# import Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie using Suppressor @@ -76,9 +78,15 @@ open("jl-top3d.matlab_$(nels).txt", "w") do io write(io, output) end -# # # Visualize the result using Makie.jl -# fig = visualize(problem; topology=r.minimizer, -# default_exagg_scale=0.07, scale_range=10.0, vector_linewidth=3, vector_arrowsize=0.5) -# Makie.display(fig) - -# Makie.save("jl-top3d.matlab__$(nels).png", fig) +# Visualize the result using Makie.jl +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.5, +) +Makie.display(fig) + +Makie.save("jl-top3d.matlab__$(nels).png", fig) diff --git a/test/wcsmo14/jl_benchmarks/compare_top3d125.jl b/test/wcsmo14/jl_benchmarks/compare_top3d125.jl index 6ae8d22d..fcf69868 100644 --- a/test/wcsmo14/jl_benchmarks/compare_top3d125.jl +++ b/test/wcsmo14/jl_benchmarks/compare_top3d125.jl @@ -1,5 +1,7 @@ using TopOpt -# import Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie using Suppressor using FromFile @@ -77,10 +79,17 @@ open("jl-top3D125.matlab_$(nels).txt", "w") do io write(io, output) end -# # # Visualize the result using Makie.jl -# fig = visualize(problem; topology=r.minimizer, -# default_exagg_scale=0.07, scale_range=10.0, vector_linewidth=3, vector_arrowsize=0.005, -# default_support_scale=0.01, default_load_scale=0.01) -# Makie.display(fig) - -# Makie.save("jl-top3D125.matlab__$(nels).png", fig) +# Visualize the result using Makie.jl +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.005, + default_support_scale=0.01, + default_load_scale=0.01, +) +Makie.display(fig) + +Makie.save("jl-top3D125.matlab__$(nels).png", fig) diff --git a/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl b/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl index ff5804e0..1f43abd2 100644 --- a/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl +++ b/test/wcsmo14/jl_benchmarks/compare_topopt_py.jl @@ -1,5 +1,7 @@ using TopOpt -# import Makie, GLMakie +using Makie +using CairoMakie +# using GLMakie using TimerOutputs using Suppressor @@ -72,9 +74,15 @@ open("jl-topopt.py_$(nels).txt", "w") do io write(io, output) end -# # # Visualize the result using Makie.jl -# fig = visualize(problem; topology=r.minimizer, -# default_exagg_scale=0.07, scale_range=10.0, vector_linewidth=3, vector_arrowsize=0.5) -# Makie.display(fig) +# Visualize the result using Makie.jl +fig = visualize( + problem; + topology=r.minimizer, + default_exagg_scale=0.07, + scale_range=10.0, + vector_linewidth=3, + vector_arrowsize=0.5, +) +Makie.display(fig) -# Makie.save("jl-topopt.py_$(nels).png", fig) +Makie.save("jl-topopt.py_$(nels).png", fig) From 93144033ef907ff57967f081108f7176f69b5cf0 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 10:34:56 +0400 Subject: [PATCH 3/8] fix deps --- docs/Project.toml | 2 ++ test/Project.toml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/Project.toml b/docs/Project.toml index 8a65511c..6aec2824 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -1,10 +1,12 @@ [deps] +CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" +Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a" Nonconvex = "01bcebdf-4d21-426d-b5c4-6132c1619978" NonconvexTOBS = "6c0b5230-d4c9-466e-bfd4-b31e6272ab65" StatsFuns = "4c63d2b9-4356-54db-8cca-17b64c39e42c" diff --git a/test/Project.toml b/test/Project.toml index 8f47f803..36744bad 100644 --- a/test/Project.toml +++ b/test/Project.toml @@ -2,10 +2,12 @@ Arpack = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97" CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4" +ColorSchemes = "35d6a980-a343-548e-a6ea-1d62b119f2f4" Ferrite = "c061ca5d-56c9-439f-9c0e-210fe06d3992" FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210" FromFile = "ff7dd447-1dcb-4ce3-b8ac-22a812192de7" +GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326" Ipopt = "b6b21f68-93f8-5de0-b562-5493be1d77c9" Juniper = "2ddba703-00a4-53a7-87a5-e8b9971dde84" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" From 12f2ab1423d87a4e29c75c95d3d65ce7cf55c8ed Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 10:47:17 +0400 Subject: [PATCH 4/8] result.topology -> result.minimizer --- test/truss_topopt_problems/test_problem.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/truss_topopt_problems/test_problem.jl b/test/truss_topopt_problems/test_problem.jl index dc626531..defc0b33 100644 --- a/test/truss_topopt_problems/test_problem.jl +++ b/test/truss_topopt_problems/test_problem.jl @@ -82,7 +82,7 @@ end fig = visualize( problem; u=solver.u, - topology=result.topology, + topology=result.minimizer, vector_arrowsize=0.1, vector_linewidth=0.8, default_exagg_scale=ndim == 3 ? 1.0 : 0.01, From 6b6d844fbe11b06e0cfc0ed5f6d446f2b6099371 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 11:35:58 +0400 Subject: [PATCH 5/8] savefig -> save --- test/examples/local_stress.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/examples/local_stress.jl b/test/examples/local_stress.jl index af6b298d..bc94781b 100644 --- a/test/examples/local_stress.jl +++ b/test/examples/local_stress.jl @@ -77,4 +77,4 @@ f = visualize( vector_linewidth=3, vector_arrowsize=0.5, ) -savefig("result.png", _f) +save("result.png", _f) From 6b39668f045ffbf3ce92b05159ae6cdb04f4b521 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 12:36:58 +0400 Subject: [PATCH 6/8] fix typo --- test/examples/local_stress.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/examples/local_stress.jl b/test/examples/local_stress.jl index bc94781b..93569a0d 100644 --- a/test/examples/local_stress.jl +++ b/test/examples/local_stress.jl @@ -69,7 +69,7 @@ end s = stress(filter(PseudoDensities(x))) @test (maximum(s) - threshold) / threshold < 0.01 -f = visualize( +fig = visualize( problem; topology=filter(PseudoDensities(x)), default_exagg_scale=0.07, @@ -77,4 +77,4 @@ f = visualize( vector_linewidth=3, vector_arrowsize=0.5, ) -save("result.png", _f) +save("result.png", fig) From 659276602c08e89215b925ee3b4b527476ebcd9c Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 13:33:41 +0400 Subject: [PATCH 7/8] add GeometryBasics to docs Project.toml --- docs/Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/docs/Project.toml b/docs/Project.toml index 6aec2824..359a9471 100644 --- a/docs/Project.toml +++ b/docs/Project.toml @@ -3,6 +3,7 @@ CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0" Cbc = "9961bab8-2fa3-5c5a-9d89-47fab24efd76" Documenter = "e30172f5-a6a5-5a46-863b-614d45cd2de4" DocumenterCitations = "daee34ce-89f3-4625-b898-19384cb65244" +GeometryBasics = "5c1252a2-5f33-56bf-86c9-59e7332b4326" JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6" LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e" Literate = "98b081ad-f1c9-55d3-8b20-4c87d4299306" From cad39c5c5700d36885fe2c2224d243e8e091c044 Mon Sep 17 00:00:00 2001 From: Mohamed Tarek Date: Mon, 7 Oct 2024 16:09:10 +0400 Subject: [PATCH 8/8] throw a more informative error in the default visualize method --- src/TopOpt.jl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/TopOpt.jl b/src/TopOpt.jl index 74805feb..c747e407 100644 --- a/src/TopOpt.jl +++ b/src/TopOpt.jl @@ -50,7 +50,11 @@ export PseudoDensities include(joinpath("Utilities", "Utilities.jl")) using .Utilities -function visualize end +function visualize(arg::T; kwargs...) where {T} + return error( + "`visualize` is not defined for input type `$T`. This may be because the input to the function is incorrect or because you forgot to load `Makie` in your code. You can load `Makie` with `using Makie`. To see the available methods of `visualize` and their documentation, you can run `? visualize` in the Julia REPL.", + ) +end # Topopology optimization problem definitions include(joinpath("TopOptProblems", "TopOptProblems.jl"))