From 463af70c9317a0f1fe5159c52b804dced1a34784 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Fri, 6 Dec 2024 23:36:17 +0100 Subject: [PATCH 1/5] runic format everything --- docs/make.jl | 96 +- examples/examples1d.jl | 16 +- examples/examples2d.jl | 29 +- examples/examples3d.jl | 12 +- examples/experimental/adjacency.jl | 126 +- examples/experimental/extendablegrid.jl | 162 +-- examples/experimental/plotlooptest.jl | 180 +-- examples/experimental/pluto-meshcat.jl | 34 +- examples/experimental/pluto-plots.jl | 26 +- examples/experimental/pluto-pyplot.jl | 38 +- examples/experimental/pluto-wglmakie.jl | 38 +- examples/experimental/rosetta.jl | 74 +- examples/experimental/test_multiscene.jl | 154 +-- .../experimental/test_multiscene_gridfunc.jl | 14 +- examples/gmsh.jl | 361 +++--- examples/pluto-partitioning.jl | 14 +- examples/vtk_write.jl | 52 +- ext/ExtendableGridsGmshExt.jl | 46 +- ext/ExtendableGridsMetisExt.jl | 102 +- ext/ExtendableGridsTetGenExt.jl | 8 +- ext/ExtendableGridsTriangulateExt.jl | 4 +- src/ExtendableGrids.jl | 3 +- src/adaptive_meshrefinements.jl | 174 ++- src/adjacency.jl | 163 +-- src/arraytools.jl | 238 ++-- src/assemblytypes.jl | 30 +- src/binnedpointlist.jl | 56 +- src/cellfinder.jl | 182 +-- src/commongrids.jl | 266 ++-- src/coordinatesystem.jl | 26 +- src/deprecated.jl | 4 +- src/derived.jl | 829 ++++++------ src/elementgeometry.jl | 24 +- src/extendablegrid.jl | 184 +-- src/io.jl | 125 +- src/l2gtransformations.jl | 320 ++--- src/meshrefinements.jl | 474 +++---- src/more.jl | 305 ++--- src/partitioning.jl | 495 +++---- src/regionedit.jl | 324 ++--- src/seal.jl | 75 +- src/serialadjacency.jl | 35 +- src/shape_specs.jl | 179 +-- src/simplexgrid.jl | 1149 +++++++++-------- src/subgrid.jl | 249 ++-- src/tokenstream.jl | 74 +- src/typehierarchy.jl | 28 +- src/vectorofconstants.jl | 18 +- src/voronoi.jl | 125 +- test/gmsh.jl | 44 +- test/partitioning.jl | 68 +- test/runtests.jl | 224 ++-- test/test_gridstuff.jl | 94 +- test/testbinned.jl | 10 +- test/tritet.jl | 122 +- 55 files changed, 4224 insertions(+), 4078 deletions(-) diff --git a/docs/make.jl b/docs/make.jl index a3fae8f3..3c691fa8 100644 --- a/docs/make.jl +++ b/docs/make.jl @@ -3,57 +3,63 @@ import Gmsh import CairoMakie, Pluto, PlutoStaticHTML CairoMakie.activate!(; type = "png", visible = false) ExampleJuggler.verbose!(true) -ExtendableGridsGmshExt=Base.get_extension(ExtendableGrids, :ExtendableGridsGmshExt) +ExtendableGridsGmshExt = Base.get_extension(ExtendableGrids, :ExtendableGridsGmshExt) function mkdocs() cleanexamples() - exampledir=joinpath(@__DIR__, "..", "examples") + exampledir = joinpath(@__DIR__, "..", "examples") - generated_examples = @docscripts(exampledir, - ["examples1d.jl", "examples2d.jl", "examples3d.jl", "gmsh.jl"], - Plotter=CairoMakie) + generated_examples = @docscripts( + exampledir, + ["examples1d.jl", "examples2d.jl", "examples3d.jl", "gmsh.jl"], + Plotter = CairoMakie + ) - notebooks = ["Partitioning" => "pluto-partitioning.jl"] - notebook_examples = @docplutonotebooks(exampledir, notebooks, iframe=false) + notebooks = ["Partitioning" => "pluto-partitioning.jl"] + notebook_examples = @docplutonotebooks(exampledir, notebooks, iframe = false) size_threshold_ignore = last.(notebook_examples) - generated_examples=vcat(generated_examples, notebook_examples) - - makedocs(; sitename = "ExtendableGrids.jl", - modules = [ExtendableGrids,ExtendableGridsGmshExt], - clean = false, - doctest = true, - authors = "J. Fuhrmann, Ch. Merdon, J. Taraz", - repo = "https://github.com/WIAS-PDELib/ExtendableGrids.jl", - format = Documenter.HTML(; size_threshold_ignore, - assets=String["assets/citations.css"], - mathengine = MathJax3()), - pages = [ - "Home" => "index.md", - "Changes" => "changes.md", - "adjacency.md", - "vectorofconstants.md", - "typehierarchy.md", - "elementgeometry.md", - "shape_specs.md", - "coordinatesystem.md", - "extendablegrid.md", - "subgrid.md", - "more.md", - "voronoi.md", - "assembly.md", - "cellfinder.md", - "arraytools.md", - "gridconstructors.md", - "output.md", - "refinement.md", - "partitioning.md", - "regionedit.md", - "tokenstream.md", - "binnedpointlist.md", - "gmsh.md", - "allindex.md", - "Examples" => generated_examples, - ]) + generated_examples = vcat(generated_examples, notebook_examples) + + return makedocs(; + sitename = "ExtendableGrids.jl", + modules = [ExtendableGrids, ExtendableGridsGmshExt], + clean = false, + doctest = true, + authors = "J. Fuhrmann, Ch. Merdon, J. Taraz", + repo = "https://github.com/WIAS-PDELib/ExtendableGrids.jl", + format = Documenter.HTML(; + size_threshold_ignore, + assets = String["assets/citations.css"], + mathengine = MathJax3() + ), + pages = [ + "Home" => "index.md", + "Changes" => "changes.md", + "adjacency.md", + "vectorofconstants.md", + "typehierarchy.md", + "elementgeometry.md", + "shape_specs.md", + "coordinatesystem.md", + "extendablegrid.md", + "subgrid.md", + "more.md", + "voronoi.md", + "assembly.md", + "cellfinder.md", + "arraytools.md", + "gridconstructors.md", + "output.md", + "refinement.md", + "partitioning.md", + "regionedit.md", + "tokenstream.md", + "binnedpointlist.md", + "gmsh.md", + "allindex.md", + "Examples" => generated_examples, + ] + ) end mkdocs() diff --git a/examples/examples1d.jl b/examples/examples1d.jl index 43215a6d..7c4709a5 100644 --- a/examples/examples1d.jl +++ b/examples/examples1d.jl @@ -6,18 +6,18 @@ using ExtendableGrids # function interval_from_vector() X = collect(0:0.05:1) - grid = simplexgrid(X) + return simplexgrid(X) end # ![](interval_from_vector.png) -# +# # ## Interval with local refinement -# +# function interval_localref() XLeft = geomspace(0.0, 0.5, 0.1, 0.01) XRight = geomspace(0.5, 1.0, 0.01, 0.1) X = glue(XLeft, XRight) - grid = simplexgrid(X) + return simplexgrid(X) end # ![](interval_localref.png) @@ -29,7 +29,7 @@ function interval_multiregion() grid = simplexgrid(X) cellmask!(grid, [0.0], [0.5], 3) bfacemask!(grid, [0.5], [0.5], 4) - grid + return grid end # ![](interval_multiregion.png) # @@ -40,8 +40,8 @@ function interval_subgrid() grid = simplexgrid(X) bfacemask!(grid, [0.5], [0.5], 3) cellmask!(grid, [0.0], [0.25], 2) - cellmask!(grid, [0.20], [0.5], 3) - subgrid(grid, [2, 3]) + cellmask!(grid, [0.2], [0.5], 3) + return subgrid(grid, [2, 3]) end # ![](interval_subgrid.png) # ## CI callbacks for [ExampleJuggler.jl](https://github.com/j-fu/ExampleJuggler.jl) @@ -53,6 +53,7 @@ function runtests() @test numbers_match(interval_localref(), 27, 26, 2) @test numbers_match(interval_multiregion(), 21, 20, 3) @test numbers_match(interval_subgrid(), 51, 50, 2) + nothing end # Plot generation @@ -66,4 +67,5 @@ function generateplots(picdir; Plotter = nothing) Plotter.save(joinpath(picdir, "interval_multiregion.png"), gridplot(interval_multiregion(); Plotter, size, legend)) Plotter.save(joinpath(picdir, "interval_subgrid.png"), gridplot(interval_subgrid(); Plotter, size, legend)) end + nothing end diff --git a/examples/examples2d.jl b/examples/examples2d.jl index 22d17d15..97dcc549 100644 --- a/examples/examples2d.jl +++ b/examples/examples2d.jl @@ -1,31 +1,31 @@ # 2D Grid examples # =============== -# +# using Triangulate, ExtendableGrids, SimplexGridFactory # ## Rectangle function rectangle() X = collect(0:0.05:1) Y = collect(0:0.05:1) - simplexgrid(X, X) + return simplexgrid(X, X) end # ![](rectangle.png) -# +# # ## Rectangle with local refinement -# +# function rectangle_localref() hmin = 0.01 hmax = 0.1 XLeft = geomspace(0.0, 0.5, hmax, hmin) XRight = geomspace(0.5, 1.0, hmin, hmax) X = glue(XLeft, XRight) - simplexgrid(X, X) + return simplexgrid(X, X) end # ![](rectangle_localref.png) -# +# # ## Rectangle with multiple regions -# +# function rectangle_multiregion() X = collect(0:0.05:1) Y = collect(0:0.05:1) @@ -34,22 +34,23 @@ function rectangle_multiregion() bfacemask!(grid, [0.0, 0.0], [0.0, 0.5], 5) bfacemask!(grid, [1.0, 0.0], [1.0, 0.5], 6) bfacemask!(grid, [0.0, 0.5], [1.0, 0.5], 7) +grid end # ![](rectangle_multiregion.png) -# +# # ## Subgrid from rectangle -# +# function rectangle_subgrid() X = collect(0:0.05:1) Y = collect(0:0.05:1) grid = simplexgrid(X, Y) rect!(grid, [0.25, 0.25], [0.75, 0.75]; region = 2, bregion = 5) - subgrid(grid, [1]) + return subgrid(grid, [1]) end # ![](rectangle_subgrid.png) -# +# # ## Rect2d with bregion function # # Here, we use function as bregion parameter - this allows to @@ -62,7 +63,7 @@ function rect2d_bregion_function() rect!(grid, [4, 2], [5, 8]; region = 2, bregion = cur -> cur == 5 ? 0 : 8) - subgrid(grid, [2]) + return subgrid(grid, [2]) end # ![](rect2d_bregion_function.png) @@ -90,7 +91,7 @@ function sorted_subgrid(; maxvolume = 0.01) sg = subgrid(g, [2]; boundary = true, transform = (a, b) -> a[1] = b[2]) f = map((x, y) -> sin(3x) * cos(3y), g) sf = view(f, sg) - g, sg, sf + return g, sg, sf end # ![](sorted_subgrid.png) # ## CI callbacks for [ExampleJuggler.jl](https://github.com/j-fu/ExampleJuggler.jl) @@ -107,6 +108,7 @@ function runtests() @test numbers_match(g, 187, 306, 66) @test numbers_match(sg, 17, 16, 0) @test issorted(view(sg[Coordinates], 1, :)) +nothing end # Plot generation @@ -128,4 +130,5 @@ function generateplots(picdir; Plotter = nothing) fname = joinpath(picdir, "sorted_subgrid.png") Plotter.save(fname, reveal(p)) end + nothing end diff --git a/examples/examples3d.jl b/examples/examples3d.jl index f2c4491f..3e28b2ac 100644 --- a/examples/examples3d.jl +++ b/examples/examples3d.jl @@ -1,6 +1,6 @@ # 3D Grid examples # =============== -# +# using ExtendableGrids # ## Quadrilateral @@ -8,7 +8,7 @@ function quadrilateral(; hx = 0.25, hy = 0.2, hz = 0.1) X = collect(0:hx:1) Y = collect(0:hy:1) Z = collect(0:hz:1) - simplexgrid(X, Y, Z) + return simplexgrid(X, Y, Z) end # ![](quadrilateral.png) @@ -23,7 +23,7 @@ function cross3d() rect!(grid, (0.4, 0, 0.2), (0.6, 1, 0.4); region = 2, bregions = [4, 4, 4, 4, (cur) -> cur == 3 ? 0 : 5, 6]) - subgrid(grid, [2]) + return subgrid(grid, [2]) end # ![](cross3d.png) # ## CI callbacks for [ExampleJuggler.jl](https://github.com/j-fu/ExampleJuggler.jl) @@ -38,7 +38,7 @@ function mask_bedges() bedgemask!(grid, [0.0, 0.0, 1.0], [0.0, 1.0, 1.0], 4) bedgemask!(grid, [0.0, 1.0, 0.0], [0.0, 0.0, 1.0], 5) - true + return true end using Test @@ -47,14 +47,16 @@ function runtests() @test numbers_match(quadrilateral(), 330, 1200, 440) @test mask_bedges() @test numbers_match(cross3d(), 189, 480, 344) +nothing end # Plot generation using GridVisualize function generateplots(picdir; Plotter = nothing) - if isdefined(Plotter, :Makie) + if isdefined(Plotter, :Makie) size = (400, 400) Plotter.save(joinpath(picdir, "quadrilateral.png"), gridplot(quadrilateral(); Plotter, size)) Plotter.save(joinpath(picdir, "cross3d.png"), gridplot(cross3d(); Plotter, size)) end +nothing end diff --git a/examples/experimental/adjacency.jl b/examples/experimental/adjacency.jl index fa4ab371..8de76e25 100644 --- a/examples/experimental/adjacency.jl +++ b/examples/experimental/adjacency.jl @@ -6,21 +6,21 @@ using ExtendableGrids function test_adj_correctness() println("A0: Adjacency transpose from matrix") - A0=[i+3*(j-1) for i=1:3, j=1:3] - - A0t=atranspose(A0) + A0 = [i + 3 * (j - 1) for i in 1:3, j in 1:3] + + A0t = atranspose(A0) println("A0:\n", A0) println("A0t:\n", A0t) - + @test A0t == VariableTargetAdjacency{Int64}([1, 1, 1, 2, 2, 2, 3, 3, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) println("A1: Adjacency transpose from variable target adj") - A1=VariableTargetAdjacency() - append!(A1, (1,2,3)) - append!(A1, (4,5,6,7)) - append!(A1, (8,9)) - A1t=atranspose(A1) + A1 = VariableTargetAdjacency() + append!(A1, (1, 2, 3)) + append!(A1, (4, 5, 6, 7)) + append!(A1, (8, 9)) + A1t = atranspose(A1) println("A1:\n", A1) println("A1t:\n", A1t) @@ -29,15 +29,15 @@ function test_adj_correctness() @test A1t == VariableTargetAdjacency{Int64}([1, 1, 1, 2, 2, 2, 2, 3, 3], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]) println("A2: Adjacency transpose from variable target adj") - A2=VariableTargetAdjacency() - append!(A2, (1,2,3)) - append!(A2, (2,3,4)) - append!(A2, (3,4,5)) - - A2t=atranspose(A2) + A2 = VariableTargetAdjacency() + append!(A2, (1, 2, 3)) + append!(A2, (2, 3, 4)) + append!(A2, (3, 4, 5)) + + A2t = atranspose(A2) println("A2:\n", A2) println("A2t:\n", A2t) - @test A2t == VariableTargetAdjacency{Int64}([1, 1, 2, 1, 2, 3, 2, 3, 3], [1, 2, 4, 7, 9, 10]) + return @test A2t == VariableTargetAdjacency{Int64}([1, 1, 2, 1, 2, 3, 2, 3, 3], [1, 2, 4, 7, 9, 10]) end @@ -47,15 +47,15 @@ end # Function to be called with adjaceny for checking access # performance function sum_adj(a) - sum=0 - for irun=1:100 - for isource::Int64=1:num_sources(a) - for itarget=1:num_targets(a,isource) - sum+=a[itarget,isource] + sum = 0 + for irun in 1:100 + for isource::Int64 in 1:num_sources(a) + for itarget in 1:num_targets(a, isource) + sum += a[itarget, isource] end end end - sum + return sum end @@ -68,19 +68,19 @@ struct TestStructAny end -function test_adj_performance(n=100_000) - m=3 - matrix=[i*j for i=1:m, j=1:n] - +function test_adj_performance(n = 100_000) + m = 3 + matrix = [i * j for i in 1:m, j in 1:n] + ######################################################## # Access matrix sas FixedTargeAdjacency, should be fast print("Direct matrix access:") @time begin - for irun=1:100 - sum=0 - for isource=1:num_sources(matrix) - for itarget=1:num_targets(matrix,isource) - sum+=matrix[itarget,isource] + for irun in 1:100 + sum = 0 + for isource in 1:num_sources(matrix) + for itarget in 1:num_targets(matrix, isource) + sum += matrix[itarget, isource] end end end @@ -92,46 +92,46 @@ function test_adj_performance(n=100_000) sum_adj(matrix) @time sum_adj(matrix) println() - + ######################################################## # Reorganize adjacency as variable target adjacency - vadj=VariableTargetAdjacency(matrix) + vadj = VariableTargetAdjacency(matrix) # Direct access should be slower than matrix access print(" Direct vadj access:") @time begin - for irun=1:100 - sum=0 - for isource=1:num_sources(vadj) - for itarget=1:num_targets(vadj,isource) - sum+=vadj[itarget,isource] + for irun in 1:100 + sum = 0 + for isource in 1:num_sources(vadj) + for itarget in 1:num_targets(vadj, isource) + sum += vadj[itarget, isource] end end end end - - + + # Direct access should be slower than matrix access print(" Call with vadj:") @time sum_adj(vadj) println() - + ############################################ # Assign matrix to adj::Adjacency in struct # As Adjacency is a union, this could lead to performance problems, # but we have "UnionSplitting": https://julialang.org/blog/2018/08/union-splitting/ - tstruct=TestStruct(matrix) - tadj=tstruct.adj + tstruct = TestStruct(matrix) + tadj = tstruct.adj # Here we see a slowdown compared to the matrix case as # there must be some dispatch in the Union print(" Direct tadj access:") @time begin - for irun=1:100 - sum=0 - for isource=1:num_sources(tadj) - for itarget=1:num_targets(tadj,isource) - sum+=tadj[itarget,isource] + for irun in 1:100 + sum = 0 + for isource in 1:num_sources(tadj) + for itarget in 1:num_targets(tadj, isource) + sum += tadj[itarget, isource] end end end @@ -147,18 +147,18 @@ function test_adj_performance(n=100_000) # Assign matrix to adj::Adjacency in struct # As Adjacency is a union, this could lead to performance problems, # but we have "UnionSplitting": https://julialang.org/blog/2018/08/union-splitting/ - tstruct_v=TestStruct(vadj) - tvadj=tstruct.adj + tstruct_v = TestStruct(vadj) + tvadj = tstruct.adj # Here we see a slowdown compared to the matrix case as # there must be some dispatch in the Union print(" Direct tvadj access:") @time begin - for irun=1:100 - sum=0 - for isource=1:num_sources(tvadj) - for itarget=1:num_targets(tvadj,isource) - sum+=tvadj[itarget,isource] + for irun in 1:100 + sum = 0 + for isource in 1:num_sources(tvadj) + for itarget in 1:num_targets(tvadj, isource) + sum += tvadj[itarget, isource] end end end @@ -173,18 +173,18 @@ function test_adj_performance(n=100_000) ############################################ # Assign matrix to adj::Any in struct - tstruct_a=TestStructAny(matrix) - aadj=tstruct_a.adj + tstruct_a = TestStructAny(matrix) + aadj = tstruct_a.adj # Here we have unboxing with every access so this is # really expensive print(" Direct aadj access:") @time begin - for irun=1:100 - sum=0 - for isource=1:num_sources(aadj) - for itarget=1:num_targets(aadj,isource) - sum+=aadj[itarget,isource] + for irun in 1:100 + sum = 0 + for isource in 1:num_sources(aadj) + for itarget in 1:num_targets(aadj, isource) + sum += aadj[itarget, isource] end end end @@ -198,5 +198,5 @@ function test_adj_performance(n=100_000) Summary: if we want to have a flexible structure, we could either use only VariableTargetAdjacency or Union{Matrix,VariableTargetAdjacency} """ - nothing + return nothing end diff --git a/examples/experimental/extendablegrid.jl b/examples/experimental/extendablegrid.jl index dbb345ab..3b552a10 100644 --- a/examples/experimental/extendablegrid.jl +++ b/examples/experimental/extendablegrid.jl @@ -9,116 +9,122 @@ using ExtendableGrids abstract type NodeCells <: AbstractGridAdjacency end # Write an instantiate method which constructs the component if it is not available -ExtendableGrids.instantiate(grid, ::Type{NodeCells})=atranspose(grid[CellNodes]) - -function test_create_circle(;nref=5) - nrad=10*2^nref - grid=generate( - points=reduce(hcat,[ [cos(2*pi*i/nrad), sin(2*pi*i/nrad)] for i=1:nrad]), - bfaces=reduce(hcat,vcat([ [i,i+1] for i=1:nrad-1],[[nrad,1]])), - bfaceregions=ones(nrad), - regionpoints=[0.0 0.0;], - regionnumbers=[1], - regionvolumes=[0.1*2.0^(-2*nref)] +ExtendableGrids.instantiate(grid, ::Type{NodeCells}) = atranspose(grid[CellNodes]) + +function test_create_circle(; nref = 5) + nrad = 10 * 2^nref + return grid = generate( + points = reduce(hcat, [ [cos(2 * pi * i / nrad), sin(2 * pi * i / nrad)] for i in 1:nrad]), + bfaces = reduce(hcat, vcat([ [i, i + 1] for i in 1:(nrad - 1)], [[nrad, 1]])), + bfaceregions = ones(nrad), + regionpoints = [0.0 0.0;], + regionnumbers = [1], + regionvolumes = [0.1 * 2.0^(-2 * nref)] ) end +function test_create_square(; nref = 3) + points = [ + [-2.0 -2]; + [2 -2]; + [2 2]; + [-2 2]; + [-1 -1]; + [1 -1]; + [1 1]; + [-1 1] + ] + + bfaces = [ + [0 1]; + [1 2]; + [2 3]; + [3 0]; + + [4 5]; + [5 6]; + [6 7]; + [7 4] + ] .+ 1 + -function test_create_square(;nref=3) - points=[[-2.0 -2]; - [2 -2]; - [2 2]; - [-2 2]; - [-1 -1]; - [1 -1]; - [1 1]; - [-1 1]] - - bfaces=[[0 1]; - [1 2]; - [2 3]; - [3 0]; - - [4 5]; - [5 6]; - [6 7]; - [7 4]].+1 - - - bfaceregions=[1,2,3,4,5,5,5,5] - - regionpoints=[[-1.5 -1.5]; - [0.0 0.0]]; - regionnumbers=[1,2] - regionvolumes=[1.0*2.0^(-nref),1.0*2.0^(-nref)] - - - - grid=generate(points=points, - bfaces=bfaces, - bfaceregions=bfaceregions, - regionpoints=regionpoints, - regionnumbers=regionnumbers, - regionvolumes=regionvolumes, - flags="pqaAD") + bfaceregions = [1, 2, 3, 4, 5, 5, 5, 5] + + regionpoints = [ + [-1.5 -1.5]; + [0.0 0.0] + ] + regionnumbers = [1, 2] + regionvolumes = [1.0 * 2.0^(-nref), 1.0 * 2.0^(-nref)] + + + return grid = generate( + points = points, + bfaces = bfaces, + bfaceregions = bfaceregions, + regionpoints = regionpoints, + regionnumbers = regionnumbers, + regionvolumes = regionvolumes, + flags = "pqaAD" + ) end function test_lazy(grid) - nodecells=grid[NodeCells] + return nodecells = grid[NodeCells] end -function sum_adj(a,n) - sum=0 - for isource=1:num_sources(a) - for itarget=1:num_targets(a,isource) - sum+=a[itarget,isource] +function sum_adj(a, n) + sum = 0 + for isource in 1:num_sources(a) + for itarget in 1:num_targets(a, isource) + sum += a[itarget, isource] end end - sum + return sum end function test_performance(grid) - cellnodes=grid[CellNodes] - n=num_sources(cellnodes) + cellnodes = grid[CellNodes] + n = num_sources(cellnodes) @time begin - sum=0 - for isource=1:num_sources(cellnodes) - for itarget=1:num_targets(cellnodes,isource) - sum+=cellnodes[itarget,isource] + sum = 0 + for isource in 1:num_sources(cellnodes) + for itarget in 1:num_targets(cellnodes, isource) + sum += cellnodes[itarget, isource] end end end - @time sum_adj(cellnodes,n) - vadj=VariableTargetAdjacency(cellnodes) - @time sum_adj(vadj,n) - matrix=Matrix(cellnodes) - @time sum_adj(matrix,n) - nothing + @time sum_adj(cellnodes, n) + vadj = VariableTargetAdjacency(cellnodes) + @time sum_adj(vadj, n) + matrix = Matrix(cellnodes) + @time sum_adj(matrix, n) + return nothing end function test_performance2(grid) - sum=0 + sum = 0 @time begin - sum=0 - for isource=1:num_sources(grid[CellNodes]) - for itarget=1:num_targets(grid[CellNodes],isource) - sum+=grid[CellNodes][itarget,isource] + sum = 0 + for isource in 1:num_sources(grid[CellNodes]) + for itarget in 1:num_targets(grid[CellNodes], isource) + sum += grid[CellNodes][itarget, isource] end end end @show sum - cellnodes=grid[CellNodes] + cellnodes = grid[CellNodes] @time begin - sum=0 - for isource=1:num_sources(cellnodes) - for itarget=1:num_targets(cellnodes,isource) - sum+=cellnodes[itarget,isource] + sum = 0 + for isource in 1:num_sources(cellnodes) + for itarget in 1:num_targets(cellnodes, isource) + sum += cellnodes[itarget, isource] end end end @show sum - nothing + return nothing end diff --git a/examples/experimental/plotlooptest.jl b/examples/experimental/plotlooptest.jl index a1cb4c8e..0f75ebfa 100644 --- a/examples/experimental/plotlooptest.jl +++ b/examples/experimental/plotlooptest.jl @@ -1,40 +1,40 @@ using ExtendableGrids -import Makie, PyPlot,Plots,VTKView +import Makie, PyPlot, Plots, VTKView -X=collect(0.0:0.05:1) +X = collect(0.0:0.05:1) -grid1d=simplexgrid(X) -grid2d=simplexgrid(X,X) +grid1d = simplexgrid(X) +grid2d = simplexgrid(X, X) -func1d=map(x->sin(5*x),grid1d) -func2d=map((x,y)->sin(5*x)*sin(5*y),grid2d) +func1d = map(x -> sin(5 * x), grid1d) +func2d = map((x, y) -> sin(5 * x) * sin(5 * y), grid2d) function gridloop(Plotter) - ctx=PlotterContext(Plotter) - @time for i=1:20 - X=collect(0.0:0.05:1) - Y=collect(0.0:0.05:1+0.01*i) - grid=simplexgrid(X,Y) - plot!(ctx,grid) + ctx = PlotterContext(Plotter) + return @time for i in 1:20 + X = collect(0.0:0.05:1) + Y = collect(0.0:0.05:(1 + 0.01 * i)) + grid = simplexgrid(X, Y) + plot!(ctx, grid) end end function gridloops() gridloop(PyPlot) gridloop(VTKView) -# gridloop(Plots) - gridloop(Makie) + # gridloop(Plots) + return gridloop(Makie) end function funcloop(Plotter) - ctx=PlotterContext(Plotter) - X=collect(0.0:0.05:1) - grid=simplexgrid(X,X) - @time for i=1:20 - func2d=map((x,y)->sin(0.5*i*x)*sin(0.5*i*y),grid2d) - plot!(ctx,grid,func2d) + ctx = PlotterContext(Plotter) + X = collect(0.0:0.05:1) + grid = simplexgrid(X, X) + return @time for i in 1:20 + func2d = map((x, y) -> sin(0.5 * i * x) * sin(0.5 * i * y), grid2d) + plot!(ctx, grid, func2d) end end @@ -42,137 +42,141 @@ function funcloops() funcloop(PyPlot) funcloop(VTKView) funcloop(Plots) - funcloop(Makie) + return funcloop(Makie) end -twofigloop(Plotter)=twofigloop(plottertype(Plotter), Plotter) +twofigloop(Plotter) = twofigloop(plottertype(Plotter), Plotter) -function twofigloop(::Type{MakieType},Makie) - MakieLayout=Makie.AbstractPlotting.MakieLayout +function twofigloop(::Type{MakieType}, Makie) + MakieLayout = Makie.AbstractPlotting.MakieLayout ## Layouting mit leeren scenen auch nicht ## tricontour mit linien ? - scene, layout = MakieLayout.layoutscene(resolution = (600,300)) - ax1=layout[1, 1] = MakieLayout.LAxis(scene) - ax2=layout[1, 2] = MakieLayout.LAxis(scene) - Makie.lines!(ax1,[0.0,0.0001],[0.0,0.0001],color=:white) - Makie.lines!(ax2,[0.0,0.0001],[0.0,0.0001],color=:white) - ctx1=PlotterContext(Makie,scene=ax1) - ctx2=PlotterContext(Makie,scene=ax2) + scene, layout = MakieLayout.layoutscene(resolution = (600, 300)) + ax1 = layout[1, 1] = MakieLayout.LAxis(scene) + ax2 = layout[1, 2] = MakieLayout.LAxis(scene) + Makie.lines!(ax1, [0.0, 0.0001], [0.0, 0.0001], color = :white) + Makie.lines!(ax2, [0.0, 0.0001], [0.0, 0.0001], color = :white) + ctx1 = PlotterContext(Makie, scene = ax1) + ctx2 = PlotterContext(Makie, scene = ax2) Makie.display(scene) - X=collect(0.0:0.05:1) - grid=simplexgrid(X,X) + X = collect(0.0:0.05:1) + grid = simplexgrid(X, X) - for i=1:200 - func1=map((x,y)->sin(0.1*i*x)*sin(0.1*i*y),grid) - func2=map((x,y)->exp(sin(0.1*i*x)*sin(0.1*i*y)),grid) - plot!(ctx1,grid,func1,show=false) - plot!(ctx2,grid,func2,show=false) + for i in 1:200 + func1 = map((x, y) -> sin(0.1 * i * x) * sin(0.1 * i * y), grid) + func2 = map((x, y) -> exp(sin(0.1 * i * x) * sin(0.1 * i * y)), grid) + plot!(ctx1, grid, func1, show = false) + plot!(ctx2, grid, func2, show = false) Makie.update!(scene) Makie.sleep(1.0e-10) end - scene + return scene end # # Works ok this way # -function twofigloop(::Type{PyPlotType},PyPlot) +function twofigloop(::Type{PyPlotType}, PyPlot) - figure=PyPlot.figure(1,figsize=(6,3)) - ctx1=PlotterContext(PyPlot,figure=figure,clear=false,legend=false,subplot=121) - ctx2=PlotterContext(PyPlot,figure=figure,clear=false,legend=false,subplot=122) + figure = PyPlot.figure(1, figsize = (6, 3)) + ctx1 = PlotterContext(PyPlot, figure = figure, clear = false, legend = false, subplot = 121) + ctx2 = PlotterContext(PyPlot, figure = figure, clear = false, legend = false, subplot = 122) - for i=1:20 - X=collect(0.0:0.05:1-0.01*i) - Y=collect(0.0:0.05:1+0.01*i) - grid=simplexgrid(X,Y) + for i in 1:20 + X = collect(0.0:0.05:(1 - 0.01 * i)) + Y = collect(0.0:0.05:(1 + 0.01 * i)) + grid = simplexgrid(X, Y) PyPlot.clf() - func2d=map((x,y)->sin(0.5*i*x)*sin(0.5*i*y),grid) - plot!(ctx1,grid) - plot!(ctx2,grid,func2d) + func2d = map((x, y) -> sin(0.5 * i * x) * sin(0.5 * i * y), grid) + plot!(ctx1, grid) + plot!(ctx2, grid, func2d) end + return end function twowinloop() # in order to make this work we need to switch from # PyPlot.plot to ax.plot. - ctx1=PlotterContext(PyPlot,fignumber=1,legend=false) - ctx2=PlotterContext(PyPlot,fignumber=2,legend=false) + ctx1 = PlotterContext(PyPlot, fignumber = 1, legend = false) + ctx2 = PlotterContext(PyPlot, fignumber = 2, legend = false) - for i=1:20 - X=collect(0.0:0.05:1-0.01*i) - Y=collect(0.0:0.05:1+0.01*i) - grid=simplexgrid(X,Y) + for i in 1:20 + X = collect(0.0:0.05:(1 - 0.01 * i)) + Y = collect(0.0:0.05:(1 + 0.01 * i)) + grid = simplexgrid(X, Y) PyPlot.clf() - func2d=map((x,y)->sin(0.5*i*x)*sin(0.5*i*y),grid) - plot!(ctx1,grid) - plot!(ctx2,grid,func2d) + func2d = map((x, y) -> sin(0.5 * i * x) * sin(0.5 * i * y), grid) + plot!(ctx1, grid) + plot!(ctx2, grid, func2d) end + return end # # Warnings from vtk when updating grid + functions # -function twofigloop(::Type{VTKViewType},VTKView) +function twofigloop(::Type{VTKViewType}, VTKView) - frame=VTKView.StaticFrame() + frame = VTKView.StaticFrame() VTKView.clear!(frame) - VTKView.size!(frame,600,300) - VTKView.layout!(frame,2,1) - dataset=VTKView.DataSet() - ctx1=PlotterContext(VTKView,frame=frame,clear=false,dataset=dataset,framepos=1, label="func1") - ctx2=PlotterContext(VTKView,frame=frame,clear=false,dataset=dataset,framepos=2, label="func2") - - X=collect(0.0:0.05:1) - grid=simplexgrid(X,X) - for i=1:200 - func1=map((x,y)->sin(0.1*i*x)*sin(0.1*i*y),grid) - func2=map((x,y)->exp(sin(0.1*i*x)*sin(0.1*i*y)),grid) - plot!(ctx1,grid,func1) - plot!(ctx2,grid,func2) + VTKView.size!(frame, 600, 300) + VTKView.layout!(frame, 2, 1) + dataset = VTKView.DataSet() + ctx1 = PlotterContext(VTKView, frame = frame, clear = false, dataset = dataset, framepos = 1, label = "func1") + ctx2 = PlotterContext(VTKView, frame = frame, clear = false, dataset = dataset, framepos = 2, label = "func2") + + X = collect(0.0:0.05:1) + grid = simplexgrid(X, X) + for i in 1:200 + func1 = map((x, y) -> sin(0.1 * i * x) * sin(0.1 * i * y), grid) + func2 = map((x, y) -> exp(sin(0.1 * i * x) * sin(0.1 * i * y)), grid) + plot!(ctx1, grid, func1) + plot!(ctx2, grid, func2) end + return end # # Works well this way, missing tricontourf and trigrid plotting... # -function twofigloop(::Type{PlotsType},Plots) +function twofigloop(::Type{PlotsType}, Plots) - ctx1=PlotterContext(Plots,plot=Plots.plot(),show=false,clear=false) - ctx2=PlotterContext(Plots,plot=Plots.plot(),show=false,clear=false) - p=Plots.plot(ctx1[:plot],ctx2[:plot]) + ctx1 = PlotterContext(Plots, plot = Plots.plot(), show = false, clear = false) + ctx2 = PlotterContext(Plots, plot = Plots.plot(), show = false, clear = false) + p = Plots.plot(ctx1[:plot], ctx2[:plot]) - X=collect(0.0:0.05:1) - Y=collect(0.0:0.05:1) - grid=simplexgrid(X,Y) - for i=1:20 - func1=map((x,y)->sin(0.5*i*x)*sin(0.5*i*y),grid) - func2=map((x,y)->exp(sin(0.5*i*x)*sin(0.5*i*y)),grid) - plot!(ctx1,grid,func1) - plot!(ctx2,grid,func2) + X = collect(0.0:0.05:1) + Y = collect(0.0:0.05:1) + grid = simplexgrid(X, Y) + for i in 1:20 + func1 = map((x, y) -> sin(0.5 * i * x) * sin(0.5 * i * y), grid) + func2 = map((x, y) -> exp(sin(0.5 * i * x) * sin(0.5 * i * y)), grid) + plot!(ctx1, grid, func1) + plot!(ctx2, grid, func2) Plots.gui(p) end + return end function twofigloops() twofigloop(PyPlot) twofigloop(VTKView) twofigloop(Plots) - twofigloop(Makie) + return twofigloop(Makie) end function alltests() gridloops() funcloops() - twofigloops() + return twofigloops() end """ Layout plan: diff --git a/examples/experimental/pluto-meshcat.jl b/examples/experimental/pluto-meshcat.jl index e5d3e366..f123fd68 100644 --- a/examples/experimental/pluto-meshcat.jl +++ b/examples/experimental/pluto-meshcat.jl @@ -6,7 +6,7 @@ using InteractiveUtils # This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). macro bind(def, element) - quote + return quote local el = $(esc(element)) global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing el @@ -14,55 +14,55 @@ macro bind(def, element) end # ╔═╡ 60941eaa-1aea-11eb-1277-97b991548781 -using Revise,PlutoUI,ExtendableGrids +using Revise, PlutoUI, ExtendableGrids # ╔═╡ df8aefc0-5273-11eb-1a67-3552732deae0 using MeshCat # ╔═╡ 08fa72d6-5274-11eb-03bd-ef885fdd216d -P=MeshCat +P = MeshCat # ╔═╡ e5cec5c8-5273-11eb-0e0b-03960befcc71 -X=collect(0:0.5:10) +X = collect(0:0.5:10) # ╔═╡ f088d080-5273-11eb-2085-e5d4734512ff -g1=simplexgrid(X) +g1 = simplexgrid(X) # ╔═╡ ff091d22-5273-11eb-2645-89630b592fa0 -gridplot(g1,Plotter=P,resolution=(500,500)) +gridplot(g1, Plotter = P, resolution = (500, 500)) # ╔═╡ 2dd685fe-5274-11eb-173c-6f2ccaf41aae -gridplot(g1,map(x->sin(x),g1),Plotter=P) +gridplot(g1, map(x -> sin(x), g1), Plotter = P) # ╔═╡ 8d1857ae-5274-11eb-265d-09604d9dba94 -g2=simplexgrid(X,X) +g2 = simplexgrid(X, X) # ╔═╡ 9526886c-5274-11eb-33a6-d95a16547472 -gridplot(g2,Plotter=P) +gridplot(g2, Plotter = P) # ╔═╡ ae81ae16-5274-11eb-36e6-a1dd77e51abb -gridplot(g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),Plotter=P,colormap=:hot) +gridplot(g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), Plotter = P, colormap = :hot) # ╔═╡ 677d67f4-5275-11eb-17d6-1954c7cd0131 -g3=simplexgrid(X,X,X) +g3 = simplexgrid(X, X, X) # ╔═╡ 303339e0-527a-11eb-3d89-c9038f9009fc -gp3=GridPlotContext(Plotter=P) +gp3 = GridPlotContext(Plotter = P) # ╔═╡ aaab7994-5275-11eb-159b-fdd7f6bee8c6 -@bind zplane Slider(0:0.51:10,default=4) +@bind zplane Slider(0:0.51:10, default = 4) # ╔═╡ 6dbcc196-5275-11eb-0bd5-2941757247b3 -gridplot!(gp3[1,1],g3,zplane=zplane,show=true) +gridplot!(gp3[1, 1], g3, zplane = zplane, show = true) # ╔═╡ 8d9d4d32-527a-11eb-1142-772022a0c4c4 -gfp3=GridPlotContext(Plotter=P) +gfp3 = GridPlotContext(Plotter = P) # ╔═╡ d6f7eb72-5275-11eb-2fa5-8b7b03a4743b -@bind flevel Slider(0:0.2:20,default=0.5) +@bind flevel Slider(0:0.2:20, default = 0.5) # ╔═╡ 77e28b4c-5275-11eb-199f-253cae56c928 -gridplot!(gfp3[1,1],g3,map((x,y,z)->(sin(x)*exp(0.1*y)*z),g3),Plotter=P,flevel=flevel,zplane=zplane,colormap=:brg,show=true) +gridplot!(gfp3[1, 1], g3, map((x, y, z) -> (sin(x) * exp(0.1 * y) * z), g3), Plotter = P, flevel = flevel, zplane = zplane, colormap = :brg, show = true) # ╔═╡ Cell order: # ╠═60941eaa-1aea-11eb-1277-97b991548781 diff --git a/examples/experimental/pluto-plots.jl b/examples/experimental/pluto-plots.jl index ca098ab4..8945edeb 100644 --- a/examples/experimental/pluto-plots.jl +++ b/examples/experimental/pluto-plots.jl @@ -5,43 +5,43 @@ using Markdown using InteractiveUtils # ╔═╡ 60941eaa-1aea-11eb-1277-97b991548781 -using Revise,PlutoUI,ExtendableGrids +using Revise, PlutoUI, ExtendableGrids # ╔═╡ df8aefc0-5273-11eb-1a67-3552732deae0 using Plots # ╔═╡ 08fa72d6-5274-11eb-03bd-ef885fdd216d -P=Plots +P = Plots # ╔═╡ e5cec5c8-5273-11eb-0e0b-03960befcc71 -X=collect(0:0.5:10) +X = collect(0:0.5:10) # ╔═╡ f088d080-5273-11eb-2085-e5d4734512ff -g1=simplexgrid(X) +g1 = simplexgrid(X) # ╔═╡ ff091d22-5273-11eb-2645-89630b592fa0 -gridplot(g1,Plotter=P,resolution=(500,100)) +gridplot(g1, Plotter = P, resolution = (500, 100)) # ╔═╡ 2dd685fe-5274-11eb-173c-6f2ccaf41aae -gridplot(g1,map(x->sin(x),g1),Plotter=P) +gridplot(g1, map(x -> sin(x), g1), Plotter = P) # ╔═╡ 8d1857ae-5274-11eb-265d-09604d9dba94 -g2=simplexgrid(X,X) +g2 = simplexgrid(X, X) # ╔═╡ 9526886c-5274-11eb-33a6-d95a16547472 -gridplot(g2,Plotter=P) +gridplot(g2, Plotter = P) # ╔═╡ ae81ae16-5274-11eb-36e6-a1dd77e51abb -gridplot(g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),Plotter=P,colormap=:hot) +gridplot(g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), Plotter = P, colormap = :hot) # ╔═╡ eb380f5a-5274-11eb-2a7a-87c75706a2ae -p=GridPlotContext(Plotter=P,layout=(1,2),fignum=2,resolution=(800,400)) +p = GridPlotContext(Plotter = P, layout = (1, 2), fignum = 2, resolution = (800, 400)) # ╔═╡ 0114cb24-5275-11eb-3013-0f3b170f25de begin - gridplot!(p[1,1],g1,map(x->sin(x),g1),title="1D") - gridplot!(p[1,2],g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),colormap=:hot,title="2D") - reveal(p) + gridplot!(p[1, 1], g1, map(x -> sin(x), g1), title = "1D") + gridplot!(p[1, 2], g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), colormap = :hot, title = "2D") + reveal(p) end # ╔═╡ Cell order: diff --git a/examples/experimental/pluto-pyplot.jl b/examples/experimental/pluto-pyplot.jl index 43042bbb..b43d0518 100644 --- a/examples/experimental/pluto-pyplot.jl +++ b/examples/experimental/pluto-pyplot.jl @@ -6,7 +6,7 @@ using InteractiveUtils # This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). macro bind(def, element) - quote + return quote local el = $(esc(element)) global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing el @@ -14,59 +14,59 @@ macro bind(def, element) end # ╔═╡ 60941eaa-1aea-11eb-1277-97b991548781 -using Revise,PlutoUI,ExtendableGrids +using Revise, PlutoUI, ExtendableGrids # ╔═╡ df8aefc0-5273-11eb-1a67-3552732deae0 using PyPlot # ╔═╡ 08fa72d6-5274-11eb-03bd-ef885fdd216d -P=PyPlot +P = PyPlot # ╔═╡ e5cec5c8-5273-11eb-0e0b-03960befcc71 -X=collect(0:0.5:10) +X = collect(0:0.5:10) # ╔═╡ f088d080-5273-11eb-2085-e5d4734512ff -g1=simplexgrid(X) +g1 = simplexgrid(X) # ╔═╡ ff091d22-5273-11eb-2645-89630b592fa0 -gridplot(g1,Plotter=P,resolution=(500,500)) +gridplot(g1, Plotter = P, resolution = (500, 500)) # ╔═╡ 2dd685fe-5274-11eb-173c-6f2ccaf41aae -gridplot(g1,map(x->sin(x),g1),Plotter=P) +gridplot(g1, map(x -> sin(x), g1), Plotter = P) # ╔═╡ 8d1857ae-5274-11eb-265d-09604d9dba94 -g2=simplexgrid(X,X) +g2 = simplexgrid(X, X) # ╔═╡ 9526886c-5274-11eb-33a6-d95a16547472 -gridplot(g2,Plotter=P) +gridplot(g2, Plotter = P) # ╔═╡ ae81ae16-5274-11eb-36e6-a1dd77e51abb -gridplot(g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),Plotter=P,colormap=:hot) +gridplot(g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), Plotter = P, colormap = :hot) # ╔═╡ eb380f5a-5274-11eb-2a7a-87c75706a2ae -p=GridPlotContext(Plotter=P,layout=(1,2),fignum=2,resolution=(800,400)) +p = GridPlotContext(Plotter = P, layout = (1, 2), fignum = 2, resolution = (800, 400)) # ╔═╡ 0114cb24-5275-11eb-3013-0f3b170f25de begin - gridplot!(p[1,1],g1,map(x->sin(x),g1),title="1D") - gridplot!(p[1,2],g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),colormap=:hot,title="2D") - reveal(p) + gridplot!(p[1, 1], g1, map(x -> sin(x), g1), title = "1D") + gridplot!(p[1, 2], g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), colormap = :hot, title = "2D") + reveal(p) end # ╔═╡ 677d67f4-5275-11eb-17d6-1954c7cd0131 -g3=simplexgrid(X,X,X) +g3 = simplexgrid(X, X, X) # ╔═╡ aaab7994-5275-11eb-159b-fdd7f6bee8c6 -@bind zplane Slider(0:0.51:10,default=4) +@bind zplane Slider(0:0.51:10, default = 4) # ╔═╡ 6dbcc196-5275-11eb-0bd5-2941757247b3 -gridplot(g3,Plotter=P,zplane=zplane) +gridplot(g3, Plotter = P, zplane = zplane) # ╔═╡ d6f7eb72-5275-11eb-2fa5-8b7b03a4743b -@bind flevel Slider(0:0.2:20,default=0.5) +@bind flevel Slider(0:0.2:20, default = 0.5) # ╔═╡ 77e28b4c-5275-11eb-199f-253cae56c928 -gridplot(g3,map((x,y,z)->(sin(x)*exp(0.1*y)*z),g3),Plotter=P,flevel=flevel,zplane=zplane,colormap=:brg) +gridplot(g3, map((x, y, z) -> (sin(x) * exp(0.1 * y) * z), g3), Plotter = P, flevel = flevel, zplane = zplane, colormap = :brg) # ╔═╡ Cell order: # ╠═60941eaa-1aea-11eb-1277-97b991548781 diff --git a/examples/experimental/pluto-wglmakie.jl b/examples/experimental/pluto-wglmakie.jl index 1b23f556..13a73878 100644 --- a/examples/experimental/pluto-wglmakie.jl +++ b/examples/experimental/pluto-wglmakie.jl @@ -6,7 +6,7 @@ using InteractiveUtils # This Pluto notebook uses @bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of @bind gives bound variables a default value (instead of an error). macro bind(def, element) - quote + return quote local el = $(esc(element)) global $(esc(def)) = Core.applicable(Base.get, el) ? Base.get(el) : missing el @@ -14,59 +14,59 @@ macro bind(def, element) end # ╔═╡ 60941eaa-1aea-11eb-1277-97b991548781 -using Revise,PlutoUI,ExtendableGrids +using Revise, PlutoUI, ExtendableGrids # ╔═╡ df8aefc0-5273-11eb-1a67-3552732deae0 using WGLMakie # ╔═╡ 08fa72d6-5274-11eb-03bd-ef885fdd216d -P=WGLMakie +P = WGLMakie # ╔═╡ e5cec5c8-5273-11eb-0e0b-03960befcc71 -X=collect(0:0.5:10) +X = collect(0:0.5:10) # ╔═╡ f088d080-5273-11eb-2085-e5d4734512ff -g1=simplexgrid(X) +g1 = simplexgrid(X) # ╔═╡ ff091d22-5273-11eb-2645-89630b592fa0 -gridplot(g1,Plotter=P,resolution=(500,500)) +gridplot(g1, Plotter = P, resolution = (500, 500)) # ╔═╡ 2dd685fe-5274-11eb-173c-6f2ccaf41aae -gridplot(g1,map(x->sin(x),g1),Plotter=P) +gridplot(g1, map(x -> sin(x), g1), Plotter = P) # ╔═╡ 8d1857ae-5274-11eb-265d-09604d9dba94 -g2=simplexgrid(X,X) +g2 = simplexgrid(X, X) # ╔═╡ 9526886c-5274-11eb-33a6-d95a16547472 -gridplot(g2,Plotter=P) +gridplot(g2, Plotter = P) # ╔═╡ ae81ae16-5274-11eb-36e6-a1dd77e51abb -gridplot(g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),Plotter=P,colormap=:hot) +gridplot(g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), Plotter = P, colormap = :hot) # ╔═╡ eb380f5a-5274-11eb-2a7a-87c75706a2ae -p=GridPlotContext(Plotter=P,layout=(1,2),fignum=2,resolution=(800,400)) +p = GridPlotContext(Plotter = P, layout = (1, 2), fignum = 2, resolution = (800, 400)) # ╔═╡ 0114cb24-5275-11eb-3013-0f3b170f25de begin - gridplot!(p[1,1],g1,map(x->sin(x),g1),title="1D") - gridplot!(p[1,2],g2,map((x,y)->(sin(x)*exp(-(y-5)^2)),g2),colormap=:hot,title="2D") - reveal(p) + gridplot!(p[1, 1], g1, map(x -> sin(x), g1), title = "1D") + gridplot!(p[1, 2], g2, map((x, y) -> (sin(x) * exp(-(y - 5)^2)), g2), colormap = :hot, title = "2D") + reveal(p) end # ╔═╡ 677d67f4-5275-11eb-17d6-1954c7cd0131 -g3=simplexgrid(X,X,X) +g3 = simplexgrid(X, X, X) # ╔═╡ aaab7994-5275-11eb-159b-fdd7f6bee8c6 -@bind zplane Slider(0:0.51:10,default=4) +@bind zplane Slider(0:0.51:10, default = 4) # ╔═╡ 6dbcc196-5275-11eb-0bd5-2941757247b3 -gridplot(g3,Plotter=P,zplane=zplane) +gridplot(g3, Plotter = P, zplane = zplane) # ╔═╡ d6f7eb72-5275-11eb-2fa5-8b7b03a4743b -@bind flevel Slider(0:0.2:20,default=0.5) +@bind flevel Slider(0:0.2:20, default = 0.5) # ╔═╡ 77e28b4c-5275-11eb-199f-253cae56c928 -gridplot(g3,map((x,y,z)->(sin(x)*exp(0.1*y)*z),g3),Plotter=P,flevel=flevel,zplane=zplane,colormap=:brg,outline=false) +gridplot(g3, map((x, y, z) -> (sin(x) * exp(0.1 * y) * z), g3), Plotter = P, flevel = flevel, zplane = zplane, colormap = :brg, outline = false) # ╔═╡ Cell order: # ╠═60941eaa-1aea-11eb-1277-97b991548781 diff --git a/examples/experimental/rosetta.jl b/examples/experimental/rosetta.jl index c644e491..2e8f95f3 100644 --- a/examples/experimental/rosetta.jl +++ b/examples/experimental/rosetta.jl @@ -4,36 +4,36 @@ ############# module ModFoo -struct Foo - foo::Vector -end + struct Foo + foo::Vector + end -export Foo + export Foo end ############# module ModBar -struct Bar - bar::Vector -end + struct Bar + bar::Vector + end -export Bar + export Bar end ############# module ModBaz -struct Baz - baz::Vector -end + struct Baz + baz::Vector + end -export Baz + export Baz end ############################################# -# The Rosetta module stores code which knows +# The Rosetta module stores code which knows # how to convert between the data structures of the # different modules without drawing in all the dependencies @@ -42,29 +42,29 @@ end module Rosetta -# Dummy module to define default -module NoModule end + # Dummy module to define default + module NoModule end -# Heuristic methods to determine -# a particular module -isModFoo(mod)=isdefined(mod,:Foo) -isModBar(mod)=isdefined(mod,:Bar) + # Heuristic methods to determine + # a particular module + isModFoo(mod) = isdefined(mod, :Foo) + isModBar(mod) = isdefined(mod, :Bar) -# Concrete conversion methods -convert_foo_to_bar(foo,FooModule,BarModule)=BarModule.Bar(foo.foo) -convert_bar_to_foo(bar,BarModule,FooModule)=FooModule.Foo(bar.bar) + # Concrete conversion methods + convert_foo_to_bar(foo, FooModule, BarModule) = BarModule.Bar(foo.foo) + convert_bar_to_foo(bar, BarModule, FooModule) = FooModule.Foo(bar.bar) -# Generic conversion method - the one "exported" from Rosetta -function Base.convert(newtype, x; From=NoModule, To=NoModule) - if isModFoo(From)&&isModBar(To) - convert_foo_to_bar(x,From,To) - elseif isModBar(From)&&isModFoo(To) - convert_bar_to_foo(x,From,To) - else - throw( ArgumentError("undefined conversion from $(typeof(x)) to $(newtype)") ) + # Generic conversion method - the one "exported" from Rosetta + function Base.convert(newtype, x; From = NoModule, To = NoModule) + return if isModFoo(From)&&isModBar(To) + convert_foo_to_bar(x, From, To) + elseif isModBar(From)&&isModFoo(To) + convert_bar_to_foo(x, From, To) + else + throw(ArgumentError("undefined conversion from $(typeof(x)) to $(newtype)")) + end end -end end @@ -77,12 +77,12 @@ using .ModBaz using .Rosetta function testrosetta() - foo=Foo(rand(100)) - bar=convert(Bar, foo,From=ModFoo, To=ModBar) - foo1=convert(Foo, bar, From=ModBar, To=ModFoo) - foo1===foo || println("transitivity error") - try - baz=convert(Baz,foo) + foo = Foo(rand(100)) + bar = convert(Bar, foo, From = ModFoo, To = ModBar) + foo1 = convert(Foo, bar, From = ModBar, To = ModFoo) + foo1 === foo || println("transitivity error") + return try + baz = convert(Baz, foo) catch e println("undefined conversion:") println(e) diff --git a/examples/experimental/test_multiscene.jl b/examples/experimental/test_multiscene.jl index 8ca39400..5e51dbb5 100644 --- a/examples/experimental/test_multiscene.jl +++ b/examples/experimental/test_multiscene.jl @@ -1,4 +1,3 @@ - ###################################################### # # Example testing all features of multiscene @@ -14,7 +13,7 @@ import Pkg Pkg.activate(mktempdir()) -Pkg.add(["ExtendableGrids","GLMakie"]) +Pkg.add(["ExtendableGrids", "GLMakie"]) using ExtendableGrids.MultiScene using GLMakie @@ -23,62 +22,62 @@ setmakie!(GLMakie) # Scene with two internal variables, shown only once function subscene1!(ax) - N=10 - s=1.0 - makedata(s,N)=s*rand(N) - makestatus(s,N)="s=$(round(s,digits=2)) N=$(N)" - data=Node(makedata(s,N)) - status=Node(makestatus(s,N)) + N = 10 + s = 1.0 + makedata(s, N) = s * rand(N) + makestatus(s, N) = "s=$(round(s, digits = 2)) N=$(N)" + data = Node(makedata(s, N)) + status = Node(makestatus(s, N)) - scene=lines(lift(a->a,data), color = :red, linewidth = 4) + scene = lines(lift(a -> a, data), color = :red, linewidth = 4) - scene_interaction(scene,[:n,:s]) do delta,key + scene_interaction(scene, [:n, :s]) do delta, key # key: which key is "switched on" (of those passed in the array) # delta: increment/decrement value - if key==:n - N=max(2,N+delta) - elseif key==:s - s=max(0.1,s+0.01*Float64(delta)) + if key == :n + N = max(2, N + delta) + elseif key == :s + s = max(0.1, s + 0.01 * Float64(delta)) end - data[]=makedata(s,N) - status[]=makestatus(s,N) + data[] = makedata(s, N) + status[] = makestatus(s, N) update!(scene) end - add_scene!(ax,scene,title="lines",status=status) + return add_scene!(ax, scene, title = "lines", status = status) end # Scene with two internal variables, shown multiple times # as a consequence, data and nodes are stored in a context dict # which is stored in ax.attributes. Not sure if this is a good idea... -function subscene2!(ax,k,l) - ctx=subscenecontext(ax) - - N=100 - makedata(k,l)=[sinpi(2*k*i/N)*sinpi(2*l*j/N) for i=1:N, j=1:N] - makestatus(k,l)="k=$(round(k,digits=2)) l=$(round(k,digits=2))" - - - if isempty(ctx) - data=Node(makedata(k,l)) - status=Node(makestatus(k,l)) - scene=heatmap(lift(a->a,data)) - ctx[:data]=data - ctx[:status]=status - ctx[:k]=k - ctx[:l]=l - - scene_interaction(scene,[:k,:l]) do delta,key - ctx[key]=max(0.1,ctx[key]+0.1*delta) - status[]=makestatus(ctx[:k], ctx[:l]) - data[]=makedata(ctx[:k], ctx[:l]) +function subscene2!(ax, k, l) + ctx = subscenecontext(ax) + + N = 100 + makedata(k, l) = [sinpi(2 * k * i / N) * sinpi(2 * l * j / N) for i in 1:N, j in 1:N] + makestatus(k, l) = "k=$(round(k, digits = 2)) l=$(round(k, digits = 2))" + + + return if isempty(ctx) + data = Node(makedata(k, l)) + status = Node(makestatus(k, l)) + scene = heatmap(lift(a -> a, data)) + ctx[:data] = data + ctx[:status] = status + ctx[:k] = k + ctx[:l] = l + + scene_interaction(scene, [:k, :l]) do delta, key + ctx[key] = max(0.1, ctx[key] + 0.1 * delta) + status[] = makestatus(ctx[:k], ctx[:l]) + data[] = makedata(ctx[:k], ctx[:l]) end - add_scene!(ax,scene,title="heatmap2d",status=status) + add_scene!(ax, scene, title = "heatmap2d", status = status) ctx else - ctx[:k]=k - ctx[:l]=l - ctx[:status][]=makestatus(ctx[:k], ctx[:l]) - ctx[:data][]=makedata(ctx[:k], ctx[:l]) + ctx[:k] = k + ctx[:l] = l + ctx[:status][] = makestatus(ctx[:k], ctx[:l]) + ctx[:data][] = makedata(ctx[:k], ctx[:l]) yieldwait() ctx end @@ -87,60 +86,61 @@ end # simple subscene with one interior variable # so no need to care about switching function subscene3!(ax) - N=50 - data=Node(rand(N,3)) - status=Node("N=$(N)") - scene=scatter(lift(a->a,data), color = rand(RGBf0)) - scene_interaction(scene) do delta,key - N=max(4,N+delta) - status[]="N=$(N)" - data[]=rand(N,3) + N = 50 + data = Node(rand(N, 3)) + status = Node("N=$(N)") + scene = scatter(lift(a -> a, data), color = rand(RGBf0)) + scene_interaction(scene) do delta, key + N = max(4, N + delta) + status[] = "N=$(N)" + data[] = rand(N, 3) end - add_scene!(ax,scene,title="scatter3d",status=status) + return add_scene!(ax, scene, title = "scatter3d", status = status) end # simple subscene with one interior variable function subscene4!(ax) - iso=1.7 + iso = 1.7 r = LinRange(-1, 1, 100) - cube = [(x.^2 + y.^2 + z.^2) for x = r, y = r, z = r] - cubedata=cube .* (cube .> 1.4) - data=Node(iso) - status=Node("iso=$(round(iso,digits=2))") - scene=volume(cubedata, algorithm = :iso, isorange = 0.05, isovalue = lift(a->a,data)) - - scene_interaction(scene) do delta,key - iso=min(2.5,max(1.5,iso+0.05*delta)) - data[]=iso - status[]="iso=$(round(iso,digits=2))" + cube = [(x .^ 2 + y .^ 2 + z .^ 2) for x in r, y in r, z in r] + cubedata = cube .* (cube .> 1.4) + data = Node(iso) + status = Node("iso=$(round(iso, digits = 2))") + scene = volume(cubedata, algorithm = :iso, isorange = 0.05, isovalue = lift(a -> a, data)) + + scene_interaction(scene) do delta, key + iso = min(2.5, max(1.5, iso + 0.05 * delta)) + data[] = iso + status[] = "iso=$(round(iso, digits = 2))" end - add_scene!(ax,scene,title="cube3d",status=status) + return add_scene!(ax, scene, title = "cube3d", status = status) end function test_multiscene() - parent,subscenes=multiscene(layout=(2,2)) + parent, subscenes = multiscene(layout = (2, 2)) - subscene1!(subscenes[1,1]) - subscene2!(subscenes[1,2],1.9,3.5) - subscene3!(subscenes[2,1]) - subscene4!(subscenes[2,2]) + subscene1!(subscenes[1, 1]) + subscene2!(subscenes[1, 2], 1.9, 3.5) + subscene3!(subscenes[2, 1]) + subscene4!(subscenes[2, 2]) display(parent) # this loop runs "forever" and can be temporarily # stopped by the space key - k=2.0 - l=2.0 - dir=1.0 + k = 2.0 + l = 2.0 + dir = 1.0 while true - for i=1:1000 - k+=dir*0.01 - l+=dir*0.01 - subscene2!(subscenes[1,2],k,l) + for i in 1:1000 + k += dir * 0.01 + l += dir * 0.01 + subscene2!(subscenes[1, 2], k, l) end - dir=-dir + dir = -dir end + return end diff --git a/examples/experimental/test_multiscene_gridfunc.jl b/examples/experimental/test_multiscene_gridfunc.jl index 9b178f46..12fb2fce 100644 --- a/examples/experimental/test_multiscene_gridfunc.jl +++ b/examples/experimental/test_multiscene_gridfunc.jl @@ -1,12 +1,12 @@ # # Create multiscene plots for Makie, PyPlot and VTKView -# +# import Pkg Pkg.activate(mktempdir()) -Pkg.add(["GLMakie","PyPlot","ExtendableGrids"]) -Pkg.Registry.add(Pkg.RegistrySpec(url="https://github.com/j-fu/PackageNursery")) -Pkg.add(Pkg.PackageSpec(name="VTKView", uuid="63b67fce-66f7-11ea-1808-1361d8c55bf4")) +Pkg.add(["GLMakie", "PyPlot", "ExtendableGrids"]) +Pkg.Registry.add(Pkg.RegistrySpec(url = "https://github.com/j-fu/PackageNursery")) +Pkg.add(Pkg.PackageSpec(name = "VTKView", uuid = "63b67fce-66f7-11ea-1808-1361d8c55bf4")) import PyPlot import GLMakie @@ -17,7 +17,7 @@ using ExtendableGrids include("../plotting.jl") function test_multiscene() - plotting_multiscene(Plotter=PyPlot) - plotting__multiscene(Plotter=VTKView) - plotting__multiscene(Plotter=GLMakie) + plotting_multiscene(Plotter = PyPlot) + plotting__multiscene(Plotter = VTKView) + return plotting__multiscene(Plotter = GLMakie) end diff --git a/examples/gmsh.jl b/examples/gmsh.jl index e1abb3c3..96c5b14c 100644 --- a/examples/gmsh.jl +++ b/examples/gmsh.jl @@ -11,12 +11,12 @@ function gmsh_t1() gmsh.initialize() gmsh.option.setNumber("General.Terminal", 1) gmsh.model.add("t1") - - lc = 1e-2 + + lc = 1.0e-2 gmsh.model.geo.addPoint(0, 0, 0, lc, 1) gmsh.model.geo.addPoint(0.1, 0, 0, lc, 2) gmsh.model.geo.addPoint(0.1, 0.3, 0, lc, 3) - + p4 = gmsh.model.geo.addPoint(0, 0.3, 0, lc) gmsh.model.geo.addLine(1, 2, 1) @@ -38,7 +38,7 @@ function gmsh_t1() gmsh.model.mesh.generate(2) grid = ExtendableGrids.simplexgrid_from_gmsh(gmsh.model) gmsh.finalize() - grid + return grid end # ![](gmsh_t1.png) @@ -47,117 +47,128 @@ end # function gmsh_t4() gmsh.initialize() - + gmsh.model.add("t4") - cm = 1e-02 - e1 = 4.5 * cm; e2 = 6 * cm / 2; e3 = 5 * cm / 2 + cm = 1.0e-2 + e1 = 4.5 * cm; e2 = 6 * cm / 2; e3 = 5 * cm / 2 h1 = 5 * cm; h2 = 10 * cm; h3 = 5 * cm; h4 = 2 * cm; h5 = 4.5 * cm R1 = 1 * cm; R2 = 1.5 * cm; r = 1 * cm Lc1 = 0.01 Lc2 = 0.003 - + function hypot(a, b) return sqrt(a * a + b * b) end - - ccos = (-h5*R1 + e2 * hypot(h5, hypot(e2, R1))) / (h5*h5 + e2*e2) - ssin = sqrt(1 - ccos*ccos) - + + ccos = (-h5 * R1 + e2 * hypot(h5, hypot(e2, R1))) / (h5 * h5 + e2 * e2) + ssin = sqrt(1 - ccos * ccos) + factory = gmsh.model.geo - factory.addPoint(-e1-e2, 0 , 0, Lc1, 1) - factory.addPoint(-e1-e2, h1 , 0, Lc1, 2) - factory.addPoint(-e3-r , h1 , 0, Lc2, 3) - factory.addPoint(-e3-r , h1+r , 0, Lc2, 4) - factory.addPoint(-e3 , h1+r , 0, Lc2, 5) - factory.addPoint(-e3 , h1+h2, 0, Lc1, 6) - factory.addPoint( e3 , h1+h2, 0, Lc1, 7) - factory.addPoint( e3 , h1+r , 0, Lc2, 8) - factory.addPoint( e3+r , h1+r , 0, Lc2, 9) - factory.addPoint( e3+r , h1 , 0, Lc2, 10) - factory.addPoint( e1+e2, h1 , 0, Lc1, 11) - factory.addPoint( e1+e2, 0 , 0, Lc1, 12) - factory.addPoint( e2 , 0 , 0, Lc1, 13) - - factory.addPoint( R1 / ssin, h5+R1*ccos, 0, Lc2, 14) - factory.addPoint( 0 , h5 , 0, Lc2, 15) - factory.addPoint(-R1 / ssin, h5+R1*ccos, 0, Lc2, 16) - factory.addPoint(-e2 , 0.0 , 0, Lc1, 17) - - factory.addPoint(-R2 , h1+h3 , 0, Lc2, 18) - factory.addPoint(-R2 , h1+h3+h4, 0, Lc2, 19) - factory.addPoint( 0 , h1+h3+h4, 0, Lc2, 20) - factory.addPoint( R2 , h1+h3+h4, 0, Lc2, 21) - factory.addPoint( R2 , h1+h3 , 0, Lc2, 22) - factory.addPoint( 0 , h1+h3 , 0, Lc2, 23) - - factory.addPoint( 0, h1+h3+h4+R2, 0, Lc2, 24) - factory.addPoint( 0, h1+h3-R2, 0, Lc2, 25) - - factory.addLine(1 , 17, 1) + factory.addPoint(-e1 - e2, 0, 0, Lc1, 1) + factory.addPoint(-e1 - e2, h1, 0, Lc1, 2) + factory.addPoint(-e3 - r, h1, 0, Lc2, 3) + factory.addPoint(-e3 - r, h1 + r, 0, Lc2, 4) + factory.addPoint(-e3, h1 + r, 0, Lc2, 5) + factory.addPoint(-e3, h1 + h2, 0, Lc1, 6) + factory.addPoint(e3, h1 + h2, 0, Lc1, 7) + factory.addPoint(e3, h1 + r, 0, Lc2, 8) + factory.addPoint(e3 + r, h1 + r, 0, Lc2, 9) + factory.addPoint(e3 + r, h1, 0, Lc2, 10) + factory.addPoint(e1 + e2, h1, 0, Lc1, 11) + factory.addPoint(e1 + e2, 0, 0, Lc1, 12) + factory.addPoint(e2, 0, 0, Lc1, 13) + + factory.addPoint(R1 / ssin, h5 + R1 * ccos, 0, Lc2, 14) + factory.addPoint(0, h5, 0, Lc2, 15) + factory.addPoint(-R1 / ssin, h5 + R1 * ccos, 0, Lc2, 16) + factory.addPoint(-e2, 0.0, 0, Lc1, 17) + + factory.addPoint(-R2, h1 + h3, 0, Lc2, 18) + factory.addPoint(-R2, h1 + h3 + h4, 0, Lc2, 19) + factory.addPoint(0, h1 + h3 + h4, 0, Lc2, 20) + factory.addPoint(R2, h1 + h3 + h4, 0, Lc2, 21) + factory.addPoint(R2, h1 + h3, 0, Lc2, 22) + factory.addPoint(0, h1 + h3, 0, Lc2, 23) + + factory.addPoint(0, h1 + h3 + h4 + R2, 0, Lc2, 24) + factory.addPoint(0, h1 + h3 - R2, 0, Lc2, 25) + + factory.addLine(1, 17, 1) factory.addLine(17, 16, 2) - - factory.addCircleArc(14,15,16, 3) - factory.addLine(14,13, 4) - factory.addLine(13,12, 5) - factory.addLine(12,11, 6) - factory.addLine(11,10, 7) - factory.addCircleArc(8,9,10, 8) - factory.addLine(8,7, 9) - factory.addLine(7,6, 10) - factory.addLine(6,5, 11) - factory.addCircleArc(3,4,5, 12) - factory.addLine(3,2, 13) - factory.addLine(2,1, 14) - factory.addLine(18,19, 15) - factory.addCircleArc(21,20,24, 16) - factory.addCircleArc(24,20,19, 17) - factory.addCircleArc(18,23,25, 18) - factory.addCircleArc(25,23,22, 19) - factory.addLine(21,22, 20) - - factory.addCurveLoop([17,-15,18,19,-20,16], 21) + + factory.addCircleArc(14, 15, 16, 3) + factory.addLine(14, 13, 4) + factory.addLine(13, 12, 5) + factory.addLine(12, 11, 6) + factory.addLine(11, 10, 7) + factory.addCircleArc(8, 9, 10, 8) + factory.addLine(8, 7, 9) + factory.addLine(7, 6, 10) + factory.addLine(6, 5, 11) + factory.addCircleArc(3, 4, 5, 12) + factory.addLine(3, 2, 13) + factory.addLine(2, 1, 14) + factory.addLine(18, 19, 15) + factory.addCircleArc(21, 20, 24, 16) + factory.addCircleArc(24, 20, 19, 17) + factory.addCircleArc(18, 23, 25, 18) + factory.addCircleArc(25, 23, 22, 19) + factory.addLine(21, 22, 20) + + factory.addCurveLoop([17, -15, 18, 19, -20, 16], 21) factory.addPlaneSurface([21], 22) - factory.addCurveLoop([11,-12,13,14,1,2,-3,4,5,6,7,-8,9,10], 23) - - factory.addPlaneSurface([23,21], 24) - + factory.addCurveLoop([11, -12, 13, 14, 1, 2, -3, 4, 5, 6, 7, -8, 9, 10], 23) + + factory.addPlaneSurface([23, 21], 24) + factory.synchronize() - + v = gmsh.view.add("comments") gmsh.view.addListDataString(v, [10, -10], ["Created with Gmsh"]) - - gmsh.view.addListDataString(v, [0, 0.11, 0], ["Hole"], - ["Align", "Center", "Font", "Helvetica"]) - - gmsh.view.addListDataString(v, [0, 0.09, 0], ["file://../t4_image.png@0.01x0"], - ["Align", "Center"]) - - gmsh.view.addListDataString(v, [-0.01, 0.09, 0], - ["file://../t4_image.png@0.01x0,0,0,1,0,1,0"]) - - gmsh.view.addListDataString(v, [0, 0.12, 0], - ["file://../t4_image.png@0.01x0#"], - ["Align", "Center"]) - + + gmsh.view.addListDataString( + v, [0, 0.11, 0], ["Hole"], + ["Align", "Center", "Font", "Helvetica"] + ) + + gmsh.view.addListDataString( + v, [0, 0.09, 0], ["file://../t4_image.png@0.01x0"], + ["Align", "Center"] + ) + + gmsh.view.addListDataString( + v, [-0.01, 0.09, 0], + ["file://../t4_image.png@0.01x0,0,0,1,0,1,0"] + ) + + gmsh.view.addListDataString( + v, [0, 0.12, 0], + ["file://../t4_image.png@0.01x0#"], + ["Align", "Center"] + ) + gmsh.view.addListDataString(v, [150, -7], ["file://../t4_image.png@20x0"]) - - gmsh.view.option.setString(v, "DoubleClickedCommand", - "Printf('View[0] has been double-clicked!');") + + gmsh.view.option.setString( + v, "DoubleClickedCommand", + "Printf('View[0] has been double-clicked!');" + ) gmsh.option.setString( "Geometry.DoubleClickedLineCommand", - "Printf('Curve %g has been double-clicked!', Geometry.DoubleClickedEntityTag);") - + "Printf('Curve %g has been double-clicked!', Geometry.DoubleClickedEntityTag);" + ) + gmsh.model.setColor([(2, 22)], 127, 127, 127) gmsh.model.setColor([(2, 24)], 160, 32, 240) gmsh.model.setColor([(1, i) for i in 1:14], 255, 0, 0) gmsh.model.setColor([(1, i) for i in 15:20], 255, 255, 0) - + gmsh.model.mesh.generate(2) grid = ExtendableGrids.simplexgrid_from_gmsh(gmsh.model) gmsh.finalize() - grid + return grid end # ![](gmsh_t4.png) @@ -166,97 +177,97 @@ end # [Example t5](https://gmsh.info/doc/texinfo/gmsh.html#t5) from the GMSH docs # function gmsh_t5() - + gmsh.initialize() gmsh.model.add("t5") - - lcar1 = .1 - lcar2 = .0005 - lcar3 = .055 - - gmsh.model.geo.addPoint(0.5,0.5,0.5, lcar2, 1) - gmsh.model.geo.addPoint(0.5,0.5,0, lcar1, 2) - gmsh.model.geo.addPoint(0,0.5,0.5, lcar1, 3) - gmsh.model.geo.addPoint(0,0,0.5, lcar1, 4) - gmsh.model.geo.addPoint(0.5,0,0.5, lcar1, 5) - gmsh.model.geo.addPoint(0.5,0,0, lcar1, 6) - gmsh.model.geo.addPoint(0,0.5,0, lcar1, 7) - gmsh.model.geo.addPoint(0,1,0, lcar1, 8) - gmsh.model.geo.addPoint(1,1,0, lcar1, 9) - gmsh.model.geo.addPoint(0,0,1, lcar1, 10) - gmsh.model.geo.addPoint(0,1,1, lcar1, 11) - gmsh.model.geo.addPoint(1,1,1, lcar1, 12) - gmsh.model.geo.addPoint(1,0,1, lcar1, 13) - gmsh.model.geo.addPoint(1,0,0, lcar1, 14) - - gmsh.model.geo.addLine(8,9, 1); gmsh.model.geo.addLine(9,12, 2) - gmsh.model.geo.addLine(12,11, 3); gmsh.model.geo.addLine(11,8, 4) - gmsh.model.geo.addLine(9,14, 5); gmsh.model.geo.addLine(14,13, 6) - gmsh.model.geo.addLine(13,12, 7); gmsh.model.geo.addLine(11,10, 8) - gmsh.model.geo.addLine(10,13, 9); gmsh.model.geo.addLine(10,4, 10) - gmsh.model.geo.addLine(4,5, 11); gmsh.model.geo.addLine(5,6, 12) - gmsh.model.geo.addLine(6,2, 13); gmsh.model.geo.addLine(2,1, 14) - gmsh.model.geo.addLine(1,3, 15); gmsh.model.geo.addLine(3,7, 16) - gmsh.model.geo.addLine(7,2, 17); gmsh.model.geo.addLine(3,4, 18) - gmsh.model.geo.addLine(5,1, 19); gmsh.model.geo.addLine(7,8, 20) - gmsh.model.geo.addLine(6,14, 21); - - gmsh.model.geo.addCurveLoop([-11,-19,-15,-18], 22) + + lcar1 = 0.1 + lcar2 = 0.0005 + lcar3 = 0.055 + + gmsh.model.geo.addPoint(0.5, 0.5, 0.5, lcar2, 1) + gmsh.model.geo.addPoint(0.5, 0.5, 0, lcar1, 2) + gmsh.model.geo.addPoint(0, 0.5, 0.5, lcar1, 3) + gmsh.model.geo.addPoint(0, 0, 0.5, lcar1, 4) + gmsh.model.geo.addPoint(0.5, 0, 0.5, lcar1, 5) + gmsh.model.geo.addPoint(0.5, 0, 0, lcar1, 6) + gmsh.model.geo.addPoint(0, 0.5, 0, lcar1, 7) + gmsh.model.geo.addPoint(0, 1, 0, lcar1, 8) + gmsh.model.geo.addPoint(1, 1, 0, lcar1, 9) + gmsh.model.geo.addPoint(0, 0, 1, lcar1, 10) + gmsh.model.geo.addPoint(0, 1, 1, lcar1, 11) + gmsh.model.geo.addPoint(1, 1, 1, lcar1, 12) + gmsh.model.geo.addPoint(1, 0, 1, lcar1, 13) + gmsh.model.geo.addPoint(1, 0, 0, lcar1, 14) + + gmsh.model.geo.addLine(8, 9, 1); gmsh.model.geo.addLine(9, 12, 2) + gmsh.model.geo.addLine(12, 11, 3); gmsh.model.geo.addLine(11, 8, 4) + gmsh.model.geo.addLine(9, 14, 5); gmsh.model.geo.addLine(14, 13, 6) + gmsh.model.geo.addLine(13, 12, 7); gmsh.model.geo.addLine(11, 10, 8) + gmsh.model.geo.addLine(10, 13, 9); gmsh.model.geo.addLine(10, 4, 10) + gmsh.model.geo.addLine(4, 5, 11); gmsh.model.geo.addLine(5, 6, 12) + gmsh.model.geo.addLine(6, 2, 13); gmsh.model.geo.addLine(2, 1, 14) + gmsh.model.geo.addLine(1, 3, 15); gmsh.model.geo.addLine(3, 7, 16) + gmsh.model.geo.addLine(7, 2, 17); gmsh.model.geo.addLine(3, 4, 18) + gmsh.model.geo.addLine(5, 1, 19); gmsh.model.geo.addLine(7, 8, 20) + gmsh.model.geo.addLine(6, 14, 21) + + gmsh.model.geo.addCurveLoop([-11, -19, -15, -18], 22) gmsh.model.geo.addPlaneSurface([22], 23) - gmsh.model.geo.addCurveLoop([16,17,14,15], 24) + gmsh.model.geo.addCurveLoop([16, 17, 14, 15], 24) gmsh.model.geo.addPlaneSurface([24], 25) - gmsh.model.geo.addCurveLoop([-17,20,1,5,-21,13], 26) + gmsh.model.geo.addCurveLoop([-17, 20, 1, 5, -21, 13], 26) gmsh.model.geo.addPlaneSurface([26], 27) - gmsh.model.geo.addCurveLoop([-4,-1,-2,-3], 28) + gmsh.model.geo.addCurveLoop([-4, -1, -2, -3], 28) gmsh.model.geo.addPlaneSurface([28], 29) - gmsh.model.geo.addCurveLoop([-7,2,-5,-6], 30) + gmsh.model.geo.addCurveLoop([-7, 2, -5, -6], 30) gmsh.model.geo.addPlaneSurface([30], 31) - gmsh.model.geo.addCurveLoop([6,-9,10,11,12,21], 32) + gmsh.model.geo.addCurveLoop([6, -9, 10, 11, 12, 21], 32) gmsh.model.geo.addPlaneSurface([32], 33) - gmsh.model.geo.addCurveLoop([7,3,8,9], 34) + gmsh.model.geo.addCurveLoop([7, 3, 8, 9], 34) gmsh.model.geo.addPlaneSurface([34], 35) - gmsh.model.geo.addCurveLoop([-10,18,-16,-20,4,-8], 36) + gmsh.model.geo.addCurveLoop([-10, 18, -16, -20, 4, -8], 36) gmsh.model.geo.addPlaneSurface([36], 37) - gmsh.model.geo.addCurveLoop([-14,-13,-12,19], 38) + gmsh.model.geo.addCurveLoop([-14, -13, -12, 19], 38) gmsh.model.geo.addPlaneSurface([38], 39) - + shells = [] - - sl = gmsh.model.geo.addSurfaceLoop([35,31,29,37,33,23,39,25,27]) + + sl = gmsh.model.geo.addSurfaceLoop([35, 31, 29, 37, 33, 23, 39, 25, 27]) push!(shells, sl) - + function cheeseHole(x, y, z, r, lc, shells) - p1 = gmsh.model.geo.addPoint(x, y, z, lc) - p2 = gmsh.model.geo.addPoint(x+r,y, z, lc) - p3 = gmsh.model.geo.addPoint(x, y+r,z, lc) - p4 = gmsh.model.geo.addPoint(x, y, z+r, lc) - p5 = gmsh.model.geo.addPoint(x-r,y, z, lc) - p6 = gmsh.model.geo.addPoint(x, y-r,z, lc) - p7 = gmsh.model.geo.addPoint(x, y, z-r, lc) - - c1 = gmsh.model.geo.addCircleArc(p2,p1,p7) - c2 = gmsh.model.geo.addCircleArc(p7,p1,p5) - c3 = gmsh.model.geo.addCircleArc(p5,p1,p4) - c4 = gmsh.model.geo.addCircleArc(p4,p1,p2) - c5 = gmsh.model.geo.addCircleArc(p2,p1,p3) - c6 = gmsh.model.geo.addCircleArc(p3,p1,p5) - c7 = gmsh.model.geo.addCircleArc(p5,p1,p6) - c8 = gmsh.model.geo.addCircleArc(p6,p1,p2) - c9 = gmsh.model.geo.addCircleArc(p7,p1,p3) - c10 = gmsh.model.geo.addCircleArc(p3,p1,p4) - c11 = gmsh.model.geo.addCircleArc(p4,p1,p6) - c12 = gmsh.model.geo.addCircleArc(p6,p1,p7) - - l1 = gmsh.model.geo.addCurveLoop([c5,c10,c4]) - l2 = gmsh.model.geo.addCurveLoop([c9,-c5,c1]) - l3 = gmsh.model.geo.addCurveLoop([c12,-c8,-c1]) - l4 = gmsh.model.geo.addCurveLoop([c8,-c4,c11]) - l5 = gmsh.model.geo.addCurveLoop([-c10,c6,c3]) - l6 = gmsh.model.geo.addCurveLoop([-c11,-c3,c7]) - l7 = gmsh.model.geo.addCurveLoop([-c2,-c7,-c12]) - l8 = gmsh.model.geo.addCurveLoop([-c6,-c9,c2]) - + p1 = gmsh.model.geo.addPoint(x, y, z, lc) + p2 = gmsh.model.geo.addPoint(x + r, y, z, lc) + p3 = gmsh.model.geo.addPoint(x, y + r, z, lc) + p4 = gmsh.model.geo.addPoint(x, y, z + r, lc) + p5 = gmsh.model.geo.addPoint(x - r, y, z, lc) + p6 = gmsh.model.geo.addPoint(x, y - r, z, lc) + p7 = gmsh.model.geo.addPoint(x, y, z - r, lc) + + c1 = gmsh.model.geo.addCircleArc(p2, p1, p7) + c2 = gmsh.model.geo.addCircleArc(p7, p1, p5) + c3 = gmsh.model.geo.addCircleArc(p5, p1, p4) + c4 = gmsh.model.geo.addCircleArc(p4, p1, p2) + c5 = gmsh.model.geo.addCircleArc(p2, p1, p3) + c6 = gmsh.model.geo.addCircleArc(p3, p1, p5) + c7 = gmsh.model.geo.addCircleArc(p5, p1, p6) + c8 = gmsh.model.geo.addCircleArc(p6, p1, p2) + c9 = gmsh.model.geo.addCircleArc(p7, p1, p3) + c10 = gmsh.model.geo.addCircleArc(p3, p1, p4) + c11 = gmsh.model.geo.addCircleArc(p4, p1, p6) + c12 = gmsh.model.geo.addCircleArc(p6, p1, p7) + + l1 = gmsh.model.geo.addCurveLoop([c5, c10, c4]) + l2 = gmsh.model.geo.addCurveLoop([c9, -c5, c1]) + l3 = gmsh.model.geo.addCurveLoop([c12, -c8, -c1]) + l4 = gmsh.model.geo.addCurveLoop([c8, -c4, c11]) + l5 = gmsh.model.geo.addCurveLoop([-c10, c6, c3]) + l6 = gmsh.model.geo.addCurveLoop([-c11, -c3, c7]) + l7 = gmsh.model.geo.addCurveLoop([-c2, -c7, -c12]) + l8 = gmsh.model.geo.addCurveLoop([-c6, -c9, c2]) + s1 = gmsh.model.geo.addSurfaceFilling([l1]) s2 = gmsh.model.geo.addSurfaceFilling([l2]) s3 = gmsh.model.geo.addSurfaceFilling([l3]) @@ -265,13 +276,13 @@ function gmsh_t5() s6 = gmsh.model.geo.addSurfaceFilling([l6]) s7 = gmsh.model.geo.addSurfaceFilling([l7]) s8 = gmsh.model.geo.addSurfaceFilling([l8]) - + sl = gmsh.model.geo.addSurfaceLoop([s1, s2, s3, s4, s5, s6, s7, s8]) v = gmsh.model.geo.addVolume([sl]) push!(shells, sl) return v end - + x = 0 y = 0.75; z = 0; r = 0.09 for t in 1:5 @@ -280,17 +291,17 @@ function gmsh_t5() v = cheeseHole(x, y, z, r, lcar3, shells) gmsh.model.geo.addPhysicalGroup(3, [v], t) end - - gmsh.model.geo.addVolume(shells, 186); - + + gmsh.model.geo.addVolume(shells, 186) + gmsh.model.geo.synchronize() - - gmsh.model.addPhysicalGroup(3, [186], 10); + + gmsh.model.addPhysicalGroup(3, [186], 10) gmsh.model.mesh.generate(3) grid = ExtendableGrids.simplexgrid_from_gmsh(gmsh.model) gmsh.finalize() - grid + return grid end # ![](gmsh_t5.png) @@ -299,10 +310,11 @@ end # Unit tests using Test function runtests() - ok(grid)= num_nodes(grid) > 0 && num_cells(grid) > 0 && num_bfaces(grid) > 0 + ok(grid) = num_nodes(grid) > 0 && num_cells(grid) > 0 && num_bfaces(grid) > 0 @test ok(gmsh_t1()) @test ok(gmsh_t4()) @test ok(gmsh_t5()) +nothing end # Plot generation @@ -314,4 +326,5 @@ function generateplots(picdir; Plotter = nothing) Plotter.save(joinpath(picdir, "gmsh_t4.png"), gridplot(gmsh_t4(); Plotter, size)) Plotter.save(joinpath(picdir, "gmsh_t5.png"), gridplot(gmsh_t5(); Plotter, size)) end +nothing end diff --git a/examples/pluto-partitioning.jl b/examples/pluto-partitioning.jl index 29d71abd..4fecc33f 100644 --- a/examples/pluto-partitioning.jl +++ b/examples/pluto-partitioning.jl @@ -9,13 +9,13 @@ begin import Pkg as _Pkg haskey(ENV, "PLUTO_PROJECT") && _Pkg.activate(ENV["PLUTO_PROJECT"]) using Revise, Test - import PlutoUI + import PlutoUI import Metis using ExtendableGrids: simplexgrid, partition, num_partitions, num_pcolors - using ExtendableGrids: partition_cells, pcolor_partitions, pcolors - using ExtendableGrids: PlainMetisPartitioning, RecursiveMetisPartitioning - using ExtendableGrids: PColorPartitions, PartitionNodes, PartitionCells - using ExtendableGrids: CellVolumes + using ExtendableGrids: partition_cells, pcolor_partitions, pcolors + using ExtendableGrids: PlainMetisPartitioning, RecursiveMetisPartitioning + using ExtendableGrids: PColorPartitions, PartitionNodes, PartitionCells + using ExtendableGrids: CellVolumes using GridVisualize: gridplot, default_plotter! import CairoMakie isdefined(Main, :PlutoRunner) && default_plotter!(CairoMakie) @@ -188,7 +188,7 @@ Assembly loops can be run in parallel on for partitions of the same color. # ╔═╡ dea042ab-f115-4556-87d5-845ee2da0315 begin - cvol = pgrid1[CellVolumes] + cvol = pgrid1[CellVolumes] pvol = zeros(num_partitions(pgrid1)) for color in pcolors(pgrid1) Threads.@threads for part in pcolor_partitions(pgrid1, color) @@ -201,7 +201,7 @@ begin end # ╔═╡ 622185fe-02ec-4e3d-b77b-95b881cca9ac -@test sum(pvol)-sum(cvol) ≈ 0.0 +@test sum(pvol) - sum(cvol) ≈ 0.0 # ╔═╡ 139872f0-5158-4f09-af6e-e8822048f41f md""" diff --git a/examples/vtk_write.jl b/examples/vtk_write.jl index bce60c14..3ee038f7 100644 --- a/examples/vtk_write.jl +++ b/examples/vtk_write.jl @@ -4,31 +4,38 @@ function grid3D_test(filename::String) X = collect(0:0.1:1) Y = collect(0:0.1:2) Z = collect(0:0.1:3) - g = simplexgrid(X,Y,Z) + g = simplexgrid(X, Y, Z) - point_data = map((x,y,z)->(sinpi(2*x)*sinpi(2*y)*z), g) + point_data = map((x, y, z) -> (sinpi(2 * x) * sinpi(2 * y) * z), g) nc = num_cells(g) - field_data = [1,2,3,4,5,6] + field_data = [1, 2, 3, 4, 5, 6] cell_data = rand(Float64, nc) - - writeVTK(filename, g, true; point_data = point_data, - cell_data = cell_data, - field_data = field_data) + + writeVTK( + filename, g, true; point_data = point_data, + cell_data = cell_data, + field_data = field_data + ) +nothing + end function grid2D_test(filename::String) X = collect(0:0.1:1) Y = collect(0:0.1:2) - g = simplexgrid(X,Y) + g = simplexgrid(X, Y) - point_data = map((x,y)->(sinpi(2*x)*sinpi(2*y)), g) + point_data = map((x, y) -> (sinpi(2 * x) * sinpi(2 * y)), g) nc = num_cells(g) - field_data = [1,2,3,4,5,6] + field_data = [1, 2, 3, 4, 5, 6] cell_data = rand(Float64, nc) - - writeVTK("output.vtu", g, true; point_data = point_data, - cell_data = cell_data, - field_data = field_data) + + writeVTK( + "output.vtu", g, true; point_data = point_data, + cell_data = cell_data, + field_data = field_data + ) +nothing end function grid1D_test(filename::String) @@ -36,12 +43,15 @@ function grid1D_test(filename::String) g = simplexgrid(X) - point_data = map((x)->(sinpi(2*x)), g) + point_data = map((x) -> (sinpi(2 * x)), g) nc = num_cells(g) - field_data = [1,2,3,4,5,6] - cell_data = rand(Float64, nc) - - writeVTK(filename::String, g, true; point_data = point_data, - cell_data = cell_data, - field_data = field_data) + field_data = [1, 2, 3, 4, 5, 6] + cell_data = rand(Float64, nc) + + writeVTK( + filename::String, g, true; point_data = point_data, + cell_data = cell_data, + field_data = field_data + ) +nothing end diff --git a/ext/ExtendableGridsGmshExt.jl b/ext/ExtendableGridsGmshExt.jl index 19736193..c967cc69 100644 --- a/ext/ExtendableGridsGmshExt.jl +++ b/ext/ExtendableGridsGmshExt.jl @@ -8,7 +8,7 @@ import ExtendableGrids: mixedgrid_from_gmsh, mixedgrid_to_gmsh import ExtendableGrids: ExtendableGrid, simplexgrid, VariableTargetAdjacency, num_sources import ExtendableGrids: Coordinates, CellNodes, CellRegions, BFaceNodes, BFaceRegions, CellGeometries, BFaceGeometries import ExtendableGrids: Edge1D, Triangle2D, Quadrilateral2D, Tetrahedron3D, Hexahedron3D, Prism3D, VectorOfConstants, - ElementGeometries, Cartesian2D, Cartesian3D, CoordinateSystem, num_cells + ElementGeometries, Cartesian2D, Cartesian3D, CoordinateSystem, num_cells #!!! Make a license warning at initialization ? Gmsh is GPL - mention this in the readme. @@ -30,7 +30,7 @@ The mesh can also contain an incomplete grid. For this, the function has to be c """ function simplexgrid_from_gmsh(filename::String; incomplete = false, Tc = Float32, Ti = Int32) - gmshfile_to_simplexgrid(filename; incomplete, Tc, Ti) + return gmshfile_to_simplexgrid(filename; incomplete, Tc, Ti) end """ @@ -44,7 +44,7 @@ This only works for dim=2 grids and the orientation may be wrong. """ function mixedgrid_from_gmsh(filename::String; Tc = Float32, Ti = Int32) - gmshfile_to_mixedgrid(filename, Tc, Ti) + return gmshfile_to_mixedgrid(filename, Tc, Ti) end """ @@ -59,7 +59,7 @@ The mesh can also contain an incomplete grid. For this, the function has to be c """ function simplexgrid_from_gmsh(mod::Module; incomplete = false, Tc = Float32, Ti = Int32) - if !incomplete + return if !incomplete mod_to_simplexgrid(mod, Tc, Ti) else incomplete_mod_to_simplexgrid(mod, Tc, Ti) @@ -76,7 +76,7 @@ The mesh contained in the gmsh module is converted to an ExtendableGrid. """ function mixedgrid_from_gmsh(mod::Module; Tc = Float32, Ti = Int32) - mod_to_mixedgrid(mod, Tc, Ti) + return mod_to_mixedgrid(mod, Tc, Ti) end """ @@ -90,7 +90,7 @@ If a string (not "") is passed via 'filename', the mesh is written into this fil """ function simplexgrid_to_gmsh(g::ExtendableGrid; filename::String = "") - simplexgrid_to_gmshfile(g; filename) + return simplexgrid_to_gmshfile(g; filename) end """ @@ -103,7 +103,7 @@ If a string (not "") is passed via 'filename', the mesh is written into this fil """ function mixedgrid_to_gmsh(g::ExtendableGrid; filename::String = "") - mixedgrid_to_gmshfile(g; filename) + return mixedgrid_to_gmshfile(g; filename) end #------------------------------------------------------------------------------------------- @@ -177,7 +177,7 @@ function simplexgrid_to_gmshfile(grid::ExtendableGrid; filename::String = "") if filename != "" gmsh.write(filename) end - gmsh.finalize() + return gmsh.finalize() end """ @@ -199,7 +199,7 @@ function mixedgrid_to_gmshfile(grid::ExtendableGrid; filename::String = "") if filename != "" gmsh.write(filename) end - gmsh.finalize() + return gmsh.finalize() end #--------------------------------------------------------------------------------------------- @@ -226,7 +226,7 @@ If not, it will be initialized. """ function test_gmsh_init() - try + return try gmsh.option.setNumber("General.Terminal", 1) catch e @warn "gmsh may not have been initialized. but is initialized now!" @@ -245,7 +245,7 @@ an array of the second entries is returned function take_second(x) a, b = x[1] y = zeros(typeof(b), length(x)) - for i = 1:length(x) + for i in 1:length(x) _, t = x[i] y[i] = t end @@ -267,7 +267,7 @@ This function can be used, if you have the indices of cells, and you want to get function multiply_indices(indices, n) m = length(indices) ind_new = zeros(typeof(indices[1]), n * m) - for i = 1:n + for i in 1:n ind_new[((i - 1) * m + 1):(i * m)] = n * indices .- (n - i) end return sort(ind_new) @@ -286,7 +286,7 @@ function use_vta(VTA, col_ids, num) #note result = zeros(Int64, num * length(col_ids)) count = 1 for j in col_ids - for i = 1:num + for i in 1:num result[count] = VTA[i, j] count += 1 end @@ -364,7 +364,7 @@ function mod_to_simplexgrid(model::Module, Tc, Ti) ncells = Int(length(cell_node_tags[1]) / (dim + 1)) #the nodes making up the cells is stored in "cell_node_tags", - #just in the wrong format and permuted + #just in the wrong format and permuted simplices = zeros(Ti, dim + 1, ncells) for i in eachindex(simplices) @@ -393,7 +393,7 @@ function mod_to_simplexgrid(model::Module, Tc, Ti) cr_count += 1 end - for i = 1:ncells + for i in 1:ncells _, _, _, entitytag = model.mesh.getElement(element_tags_cells[1][i]) for pg in pgs if entitytag in model.getEntitiesForPhysicalGroup(dim, pg) @@ -433,7 +433,7 @@ function mod_to_simplexgrid(model::Module, Tc, Ti) fr_count += 1 end - for i = 1:nfaces + for i in 1:nfaces _, _, _, entitytag = model.mesh.getElement(element_tags_faces[1][i]) for pg in pgs if entitytag in model.getEntitiesForPhysicalGroup(dim - 1, pg) @@ -486,7 +486,7 @@ function incomplete_mod_to_simplexgrid(model::Module, Tc, Ti) coords_new = reshape(coords, (3, Int(length(coords) / 3))) else coords_new = zeros(Tc, 2, Int(length(coords) / 3)) - for i = 1:Int(length(coords) / 3) + for i in 1:Int(length(coords) / 3) coords_new[:, i] = coords[(3 * i - 2):(3 * i - 1)] end end @@ -576,7 +576,7 @@ function mod_to_mixedgrid(model::Module, Tc, Ti) end coords_new = zeros(Tc, 2, Int(length(coords) / 3)) - for i = 1:Int(length(coords) / 3) + for i in 1:Int(length(coords) / 3) coords_new[:, i] = coords[(3 * i - 2):(3 * i - 1)] end grid[Coordinates] = convert(Matrix{Tc}, coords_new) @@ -720,14 +720,14 @@ function mixedgrid_to_mod(grid::ExtendableGrid) if dim == 3 coords3d = reshape(coords, 3 * num_nodes) #vec(reshape(coords, (1,3*num_nodes))) - elementtypes_nn_face = elementtypes_nn_2d #Dict(2=>3, 3=>4) + elementtypes_nn_face = elementtypes_nn_2d #Dict(2=>3, 3=>4) elementtypes_na_face = elementtypes_na_2d #Dict(Triangle2D=>2, Quadrilateral2D=>3) - elementtypes_nn_cell = elementtypes_nn_3d #Dict(4=>4, 5=>8, 6=>6) + elementtypes_nn_cell = elementtypes_nn_3d #Dict(4=>4, 5=>8, 6=>6) elementtypes_na_cell = elementtypes_na_3d #Dict(Tetrahedron3D=>4, Hexahedron3D=>5, Prism3D=>6) - #elementtype_cell = 4 #tetrahedron - #elementtype_face = 2 #triangle + #elementtype_cell = 4 #tetrahedron + #elementtype_face = 2 #triangle else coords3d = vcat(coords, zeros(Float64, (1, num_nodes))) coords3d = reshape(coords3d, 3 * num_nodes) @@ -735,7 +735,7 @@ function mixedgrid_to_mod(grid::ExtendableGrid) elementtypes_nn_face = Dict(1 => 2) elementtypes_na_face = Dict(Edge1D => 1) - elementtypes_nn_cell = elementtypes_nn_2d #Dict(2=>3, 3=>4) + elementtypes_nn_cell = elementtypes_nn_2d #Dict(2=>3, 3=>4) elementtypes_na_cell = elementtypes_na_2d #Dict(Triangle2D=>2, Quadrilateral2D=>3) #elementtype_cell = 2 #triangle diff --git a/ext/ExtendableGridsMetisExt.jl b/ext/ExtendableGridsMetisExt.jl index 1425ad9c..83768fb6 100644 --- a/ext/ExtendableGridsMetisExt.jl +++ b/ext/ExtendableGridsMetisExt.jl @@ -5,7 +5,7 @@ import ExtendableGrids: dopartition, partgraph, reorder_cells using ExtendableGrids, Graphs -function dopartition(grid::ExtendableGrid{Tc,Ti}, alg::PlainMetisPartitioning) where {Tc,Ti} +function dopartition(grid::ExtendableGrid{Tc, Ti}, alg::PlainMetisPartitioning) where {Tc, Ti} nn = num_nodes(grid) ncells = num_cells(grid) cn = asparse(grid[CellNodes]) @@ -13,58 +13,58 @@ function dopartition(grid::ExtendableGrid{Tc,Ti}, alg::PlainMetisPartitioning) w cellcelladj = nc * cn mg = Metis.graph(cellcelladj) cellpartitions = Metis.partition(mg, alg.npart) - uniquecellpartitions=unique(cellpartitions) - ncellpartitions=length(uniquecellpartitions) - gr=partgraph(cellpartitions,ncellpartitions,cellcelladj) - col=Graphs.degree_greedy_color(gr) + uniquecellpartitions = unique(cellpartitions) + ncellpartitions = length(uniquecellpartitions) + gr = partgraph(cellpartitions, ncellpartitions, cellcelladj) + col = Graphs.degree_greedy_color(gr) # Reorder partitions such that all partitions with the same color # are numbered contiguously - partperm=similar(uniquecellpartitions) - colctr=zeros(Int,col.num_colors+1) - colctr[1]=1 - for i=1:length(colctr)-1 - colctr[i+1]=colctr[i]+sum(x->x==i, col.colors) + partperm = similar(uniquecellpartitions) + colctr = zeros(Int, col.num_colors + 1) + colctr[1] = 1 + for i in 1:(length(colctr) - 1) + colctr[i + 1] = colctr[i] + sum(x -> x == i, col.colors) end - colpart=copy(colctr) - for ip=1:ncellpartitions - color=col.colors[ip] - partperm[ip]=colctr[color] - colctr[color]+=1 + colpart = copy(colctr) + for ip in 1:ncellpartitions + color = col.colors[ip] + partperm[ip] = colctr[color] + colctr[color] += 1 end - + # Renumber cell partition according to new order - for i=1:ncells - cellpartitions[i]=partperm[cellpartitions[i]] + for i in 1:ncells + cellpartitions[i] = partperm[cellpartitions[i]] end - - pgrid=reorder_cells(grid,cellpartitions,ncellpartitions,colpart) - pgrid, cn, nc + + pgrid = reorder_cells(grid, cellpartitions, ncellpartitions, colpart) + return pgrid, cn, nc end -function coloredpartition(cc; npart = 4, depth = 0, maxdepth=4, separatorwidth=2) +function coloredpartition(cc; npart = 4, depth = 0, maxdepth = 4, separatorwidth = 2) mg = Metis.graph(cc) - Metis.options[Int(Metis.METIS_OPTION_CCORDER)+1] = 1 - - # primary partition + Metis.options[Int(Metis.METIS_OPTION_CCORDER) + 1] = 1 + + # primary partition primarypartition = Metis.partition(mg, npart) npp = maximum(primarypartition) ncells = length(primarypartition) sepamark = zeros(Bool, ncells) - + # Calculate separator: all cells with neighbours of a different partition # go into the separator nx = 0 - for icell = 1:ncells + for icell in 1:ncells ipart = primarypartition[icell] - for k = cc.colptr[icell]:cc.colptr[icell+1]-1 + for k in cc.colptr[icell]:(cc.colptr[icell + 1] - 1) jcell = cc.rowval[k] jpart = primarypartition[jcell] if ipart != jpart sepamark[icell] = 1 - if separatorwidth>2 - for l = cc.colptr[jcell]:cc.colptr[jcell+1]-1 + if separatorwidth > 2 + for l in cc.colptr[jcell]:(cc.colptr[jcell + 1] - 1) kcell = cc.rowval[l] sepamark[kcell] = 1 end @@ -75,18 +75,18 @@ function coloredpartition(cc; npart = 4, depth = 0, maxdepth=4, separatorwidth=2 end # Color separator cells with new color # and collect their indices - sepaparent = zeros(Int,sum(sepamark)) - iparent=1 - for icell = 1:ncells + sepaparent = zeros(Int, sum(sepamark)) + iparent = 1 + for icell in 1:ncells if sepamark[icell] > 0 primarypartition[icell] = npp + 1 - sepaparent[iparent]=icell - iparent=iparent+1 - end + sepaparent[iparent] = icell + iparent = iparent + 1 + end end # get adjacency matrix for separator - sepacc=cc[sepaparent,sepaparent] + sepacc = cc[sepaparent, sepaparent] if depth > 0 # Check if separator consists of several connected components @@ -95,15 +95,15 @@ function coloredpartition(cc; npart = 4, depth = 0, maxdepth=4, separatorwidth=2 sepacomp = connected_components(SimpleGraph(sepacc)) nsepacomp = length(sepacomp) if nsepacomp > 1 - for icomp = 1:nsepacomp + for icomp in 1:nsepacomp for icell in sepacomp[icomp] primarypartition[sepaparent[icell]] = npp + icomp end end return primarypartition, [1, npp + 1, npp + nsepacomp + 1] - elseif depth==maxdepth && size(sepacc,2)>0 + elseif depth == maxdepth && size(sepacc, 2) > 0 # all separator is one partition - for icell in size(sepacc,2) + for icell in size(sepacc, 2) primarypartition[sepaparent[icell]] = npp + 1 end return primarypartition, [1, npp + 1, npp + 2] @@ -115,31 +115,31 @@ function coloredpartition(cc; npart = 4, depth = 0, maxdepth=4, separatorwidth=2 # if just one connected component, # partition separator - if depth0 + if depth < maxdepth && size(sepacc, 1) > 0 p1, c1 = coloredpartition(sepacc; npart, depth = depth + 1, maxdepth, separatorwidth) - for icell = 1:length(p1) - primarypartition[sepaparent[icell]] = npp + p1[icell] + for icell in 1:length(p1) + primarypartition[sepaparent[icell]] = npp + p1[icell] end return primarypartition, vcat([1], c1 .+ (npp)) - elseif size(sepacc,1)>0 - for icell in size(sepacc,2) + elseif size(sepacc, 1) > 0 + for icell in size(sepacc, 2) primarypartition[sepaparent[icell]] = npp + 1 end return primarypartition, [1, npp + 1, npp + 2] else return primarypartition, [1, npp + 1] end - + end -function dopartition(grid::ExtendableGrid{Tc,Ti}, alg::RecursiveMetisPartitioning) where {Tc,Ti} +function dopartition(grid::ExtendableGrid{Tc, Ti}, alg::RecursiveMetisPartitioning) where {Tc, Ti} cn = asparse(grid[CellNodes]) nc = asparse(atranspose(grid[CellNodes])) cc = nc * cn - cellpartitions, colpart = coloredpartition(cc; npart=alg.npart, maxdepth=alg.maxdepth, separatorwidth=alg.separatorwidth) - ncellpartitions=maximum(cellpartitions) - pgrid=reorder_cells(grid,cellpartitions,ncellpartitions,colpart) - pgrid, cn, nc + cellpartitions, colpart = coloredpartition(cc; npart = alg.npart, maxdepth = alg.maxdepth, separatorwidth = alg.separatorwidth) + ncellpartitions = maximum(cellpartitions) + pgrid = reorder_cells(grid, cellpartitions, ncellpartitions, colpart) + return pgrid, cn, nc end diff --git a/ext/ExtendableGridsTetGenExt.jl b/ext/ExtendableGridsTetGenExt.jl index 9bcdf98d..9fb58200 100644 --- a/ext/ExtendableGridsTetGenExt.jl +++ b/ext/ExtendableGridsTetGenExt.jl @@ -3,15 +3,15 @@ module ExtendableGridsTetGenExt using TetGen import ExtendableGrids: simplexgrid -function simplexgrid(tio::RawTetGenIO; flags=nothing) +function simplexgrid(tio::RawTetGenIO; flags = nothing) if !isnothing(flags) tetout = TetGen.tetrahedralize(tio, flags) else tetout = tio end - + pointlist = tetout.pointlist - + tetrahedronlist = tetout.tetrahedronlist if size(tetout.tetrahedronattributelist, 2) == 0 @@ -24,7 +24,7 @@ function simplexgrid(tio::RawTetGenIO; flags=nothing) segmentmarkerlist = tetout.trifacemarkerlist - simplexgrid(pointlist, tetrahedronlist, cellregions, segmentlist, segmentmarkerlist) + return simplexgrid(pointlist, tetrahedronlist, cellregions, segmentlist, segmentmarkerlist) end end diff --git a/ext/ExtendableGridsTriangulateExt.jl b/ext/ExtendableGridsTriangulateExt.jl index 7a44cfab..0509014e 100644 --- a/ext/ExtendableGridsTriangulateExt.jl +++ b/ext/ExtendableGridsTriangulateExt.jl @@ -4,7 +4,7 @@ using Triangulate import ExtendableGrids: simplexgrid -function simplexgrid(tio::TriangulateIO; flags=nothing) +function simplexgrid(tio::TriangulateIO; flags = nothing) if !isnothing(flags) triout, vorout = Triangulate.triangulate(tio, flags) else @@ -34,7 +34,7 @@ function simplexgrid(tio::TriangulateIO; flags=nothing) error("Empty list of generated triangles. May be the geometry description is not watertight ?") |> throw end - simplexgrid(pointlist, trianglelist, cellregions, segmentlist, segmentmarkerlist) + return simplexgrid(pointlist, trianglelist, cellregions, segmentlist, segmentmarkerlist) end end diff --git a/src/ExtendableGrids.jl b/src/ExtendableGrids.jl index d3153f7b..fa2f6bb9 100644 --- a/src/ExtendableGrids.jl +++ b/src/ExtendableGrids.jl @@ -81,12 +81,11 @@ export seemingly_equal, numbers_match include("partitioning.jl") export PColorPartitions, PartitionCells, PartitionBFaces, PartitionNodes, NodePermutation, PartitionEdges -export num_pcolors, num_partitions, pcolors, pcolor_partitions, partition_cells, partition_bfaces, partition_nodes, partition_edges +export num_pcolors, num_partitions, pcolors, pcolor_partitions, partition_cells, partition_bfaces, partition_nodes, partition_edges export partition, num_partitions_per_color, num_cells_per_color, check_partitioning, num_nodes_per_partition, num_edges_per_partition export AbstractPartitioningAlgorithm, TrivialPartitioning, PlainMetisPartitioning, RecursiveMetisPartitioning - include("assemblytypes.jl") export AssemblyType export AT_NODES, ON_CELLS, ON_FACES, ON_IFACES, ON_BFACES, ON_EDGES, ON_BEDGES diff --git a/src/adaptive_meshrefinements.jl b/src/adaptive_meshrefinements.jl index da652de3..0bc0b3ac 100644 --- a/src/adaptive_meshrefinements.jl +++ b/src/adaptive_meshrefinements.jl @@ -1,4 +1,4 @@ -function bulk_mark(xgrid::ExtendableGrid{Tv,Ti}, refinement_indicators::AbstractVector{T}, theta = 0.5; indicator_AT = ON_CELLS) where {T,Tv,Ti} +function bulk_mark(xgrid::ExtendableGrid{Tv, Ti}, refinement_indicators::AbstractVector{T}, theta = 0.5; indicator_AT = ON_CELLS) where {T, Tv, Ti} xFaceCells = xgrid[FaceCells] xFaceNodes = xgrid[FaceNodes] @@ -6,11 +6,11 @@ function bulk_mark(xgrid::ExtendableGrid{Tv,Ti}, refinement_indicators::Abstract # redistribute refinement_indicators to face indicators (if necessary) if indicator_AT == ON_CELLS - refind4face::Array{T,1} = zeros(T,nfaces) + refind4face::Array{T, 1} = zeros(T, nfaces) cell::Int = 0 - for face = 1 : nfaces - for k = 1 : 2 - cell = xFaceCells[k,face] + for face in 1:nfaces + for k in 1:2 + cell = xFaceCells[k, face] if cell > 0 refind4face[face] += refinement_indicators[cell] end @@ -28,8 +28,8 @@ function bulk_mark(xgrid::ExtendableGrid{Tv,Ti}, refinement_indicators::Abstract totalsum = sum(refind4face) csum = 0 j = 0 - facemarker = zeros(Bool,nfaces) - while csum <= theta*totalsum + facemarker = zeros(Bool, nfaces) + while csum <= theta * totalsum j += 1 csum += refind4face[p[j]] facemarker[p[j]] = true @@ -39,18 +39,17 @@ function bulk_mark(xgrid::ExtendableGrid{Tv,Ti}, refinement_indicators::Abstract end - # adaptive refinement rules fo TRIANGLE2D # first k nodes are the CellNodes # next m nodes are the CellFaces midpoints # next node is the CellMidpoint (if needed) -const _no_refinement_Triangle2D = reshape([1 2 3],3,1) +const _no_refinement_Triangle2D = reshape([1 2 3], 3, 1) const _red_refinement_Triangle2D = [1 4 6; 4 2 5; 6 5 3; 5 6 4]' const _blueR_refinement_Triangle2D = [3 1 4; 4 2 5; 3 4 5]' const _blueL_refinement_Triangle2D = [1 4 6; 2 3 4; 4 3 6]' const _gree_refinement_Triangle2D = [3 1 4; 2 3 4]' -function RGB_refinement_rule(::Type{<:Triangle2D}, marked_faces::Array{Bool,1}) +function RGB_refinement_rule(::Type{<:Triangle2D}, marked_faces::Array{Bool, 1}) if any(marked_faces) == true @assert marked_faces[1] = true # closure should always mark reference face if marked_faces[2] == true && marked_faces[3] == true @@ -68,7 +67,6 @@ function RGB_refinement_rule(::Type{<:Triangle2D}, marked_faces::Array{Bool,1}) end - """ $(TYPEDSIGNATURES) @@ -81,10 +79,10 @@ Constr Approx 20, 549–564 (2004). https://doi.org/10.1007/s00365-003-0550-5 The bool array facemarkers determines which faces should be bisected. Note, that a closuring is performed such that the first face in every triangle with a marked face is also refined. """ -function RGB_refine(source_grid::ExtendableGrid{T,K}, facemarkers::Array{Bool,1}; store_parents = true) where {T,K} - - xgrid = ExtendableGrid{T,K}() - xgrid[CoordinateSystem]=source_grid[CoordinateSystem] +function RGB_refine(source_grid::ExtendableGrid{T, K}, facemarkers::Array{Bool, 1}; store_parents = true) where {T, K} + + xgrid = ExtendableGrid{T, K}() + xgrid[CoordinateSystem] = source_grid[CoordinateSystem] # unpack stuff from source grid oldCoordinates = source_grid[Coordinates] @@ -94,17 +92,17 @@ function RGB_refine(source_grid::ExtendableGrid{T,K}, facemarkers::Array{Bool,1} # get dimension of CellGeometries # currently it is assumed to be the same for all cells - dim = dim_element(EG[1]) - + dim = dim_element(EG[1]) + xCellNodes = VariableTargetAdjacency(K) xCellGeometries = [] - xCellRegions = zeros(K,0) + xCellRegions = zeros(K, 0) oldCellNodes = source_grid[CellNodes] oldCellFaces = source_grid[CellFaces] oldFaceNodes = source_grid[FaceNodes] nfaces = num_sources(oldFaceNodes) ncells = num_sources(oldCellNodes) - xCellParents::Array{K,1} = zeros(K,0) + xCellParents::Array{K, 1} = zeros(K, 0) # closuring # @logmsg MoreInfo "RGB refinement with $(sum(facemarkers)) marked faces" @@ -112,17 +110,17 @@ function RGB_refine(source_grid::ExtendableGrid{T,K}, facemarkers::Array{Bool,1} closure_finished = false while closure_finished == false closure_finished = true - for cell = 1 : ncells + for cell in 1:ncells is_refined = false - for f = 1 : 3 - if facemarkers[oldCellFaces[f,cell]] == true + for f in 1:3 + if facemarkers[oldCellFaces[f, cell]] == true is_refined = true - break; + break end end - if is_refined && facemarkers[oldCellFaces[1,cell]] == false + if is_refined && facemarkers[oldCellFaces[1, cell]] == false # mark also reference edge - facemarkers[oldCellFaces[1,cell]] = true + facemarkers[oldCellFaces[1, cell]] = true closure_finished = false end end @@ -131,140 +129,140 @@ function RGB_refine(source_grid::ExtendableGrid{T,K}, facemarkers::Array{Bool,1} # determine number of new vertices - oldvertices = size(oldCoordinates,2) + oldvertices = size(oldCoordinates, 2) newvertices = 0 - for face = 1 : nfaces + for face in 1:nfaces if facemarkers[face] == true newvertices += 1 end end - xCoordinates = zeros(T,size(oldCoordinates,1),oldvertices+newvertices) - @views xCoordinates[:,1:oldvertices] = oldCoordinates + xCoordinates = zeros(T, size(oldCoordinates, 1), oldvertices + newvertices) + @views xCoordinates[:, 1:oldvertices] = oldCoordinates # refine faces - newvertex = zeros(T,size(xCoordinates,1)) + newvertex = zeros(T, size(xCoordinates, 1)) newnodenr = oldvertices - newnode4facemidpoint = zeros(Int,nfaces) + newnode4facemidpoint = zeros(Int, nfaces) nnodes4item = 0 # add face midpoints to Coordinates - for face = 1 : nfaces + for face in 1:nfaces if facemarkers[face] == true - nnodes4item = num_targets(oldFaceNodes,face) - fill!(newvertex,0.0) - for k = 1 : nnodes4item, d = 1 : size(xCoordinates,1) - newvertex[d] += xCoordinates[d,oldFaceNodes[k,face]] - end + nnodes4item = num_targets(oldFaceNodes, face) + fill!(newvertex, 0.0) + for k in 1:nnodes4item, d in 1:size(xCoordinates, 1) + newvertex[d] += xCoordinates[d, oldFaceNodes[k, face]] + end newvertex ./= nnodes4item newnodenr += 1 newnode4facemidpoint[face] = newnodenr - for d = 1 : size(xCoordinates,1) - xCoordinates[d,newnodenr] = newvertex[d] + for d in 1:size(xCoordinates, 1) + xCoordinates[d, newnodenr] = newvertex[d] end end - end - + end + # determine new cells nnodes4item = 3 nfaces4item = 3 ncells = 0 - subitemnodes = zeros(K,3) + subitemnodes = zeros(K, 3) m = 0 - localmarked_faces = zeros(Bool,3) - refine_rule = RGB_refinement_rule(Triangle2D,localmarked_faces) + localmarked_faces = zeros(Bool, 3) + refine_rule = RGB_refinement_rule(Triangle2D, localmarked_faces) nnewcells::Int = 0 - for cell = 1 : num_sources(oldCellNodes) - for f = 1 : 3 - localmarked_faces[f] = facemarkers[oldCellFaces[f,cell]] + for cell in 1:num_sources(oldCellNodes) + for f in 1:3 + localmarked_faces[f] = facemarkers[oldCellFaces[f, cell]] end - refine_rule = RGB_refinement_rule(Triangle2D,localmarked_faces) - nnewcells = size(refine_rule,2) - for j = 1 : nnewcells - for k = 1 : 3 - m = refine_rule[k,j] - if m <= nnodes4item - subitemnodes[k] = oldCellNodes[m,cell] + refine_rule = RGB_refinement_rule(Triangle2D, localmarked_faces) + nnewcells = size(refine_rule, 2) + for j in 1:nnewcells + for k in 1:3 + m = refine_rule[k, j] + if m <= nnodes4item + subitemnodes[k] = oldCellNodes[m, cell] elseif m <= nnodes4item + nfaces4item - subitemnodes[k] = newnode4facemidpoint[oldCellFaces[m-nnodes4item,cell]] - end - end - append!(xCellNodes,view(subitemnodes,:)) - push!(xCellGeometries,Triangle2D) - push!(xCellRegions,oldCellRegions[cell]) + subitemnodes[k] = newnode4facemidpoint[oldCellFaces[m - nnodes4item, cell]] + end + end + append!(xCellNodes, view(subitemnodes, :)) + push!(xCellGeometries, Triangle2D) + push!(xCellRegions, oldCellRegions[cell]) if store_parents - push!(xCellParents,cell) + push!(xCellParents, cell) end - end + end ncells += nnewcells end # assign new cells to grid xgrid[Coordinates] = xCoordinates - if typeof(oldCellNodes) == Array{K,2} - nnodes4item = size(oldCellNodes,1) - xgrid[CellNodes] = reshape(xCellNodes.colentries,nnodes4item,num_sources(xCellNodes)) + if typeof(oldCellNodes) == Array{K, 2} + nnodes4item = size(oldCellNodes, 1) + xgrid[CellNodes] = reshape(xCellNodes.colentries, nnodes4item, num_sources(xCellNodes)) else xgrid[CellNodes] = xCellNodes end if typeof(oldCellRegions) <: VectorOfConstants - xgrid[CellRegions] = VectorOfConstants{K}(1,ncells) + xgrid[CellRegions] = VectorOfConstants{K}(1, ncells) else xgrid[CellRegions] = xCellRegions end - xgrid[CellGeometries] = VectorOfConstants{ElementGeometries,Int}(Triangle2D,ncells) + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(Triangle2D, ncells) # determine new boundary faces oldBFaceNodes = source_grid[BFaceNodes] oldBFaceFaces = source_grid[BFaceFaces] oldBFaceRegions = source_grid[BFaceRegions] - - xBFaceRegions = zeros(K,0) + + xBFaceRegions = zeros(K, 0) nbfaces = num_sources(oldBFaceNodes) - xBFaceNodes = zeros(K,0) + xBFaceNodes = zeros(K, 0) refine_rule = uniform_refine_rule(Edge1D) newbfaces = 0 - xBFaceParents::Array{K,1} = zeros(K,0) - for bface = 1 : nbfaces + xBFaceParents::Array{K, 1} = zeros(K, 0) + for bface in 1:nbfaces face = oldBFaceFaces[bface] if facemarkers[face] == true - for j = 1 : 2 - for k = 1 : 2 - m = refine_rule[j,k] + for j in 1:2 + for k in 1:2 + m = refine_rule[j, k] if dim == 2 - if m <= 2 - subitemnodes[k] = oldBFaceNodes[m,bface] + if m <= 2 + subitemnodes[k] = oldBFaceNodes[m, bface] else subitemnodes[k] = newnode4facemidpoint[face] - end + end end end - append!(xBFaceNodes,view(subitemnodes,1:2)) - push!(xBFaceRegions,oldBFaceRegions[bface]) + append!(xBFaceNodes, view(subitemnodes, 1:2)) + push!(xBFaceRegions, oldBFaceRegions[bface]) if store_parents push!(xBFaceParents, bface) end - end - newbfaces +=1 + end + newbfaces += 1 else - append!(xBFaceNodes,view(oldBFaceNodes,:,bface)) - push!(xBFaceRegions,oldBFaceRegions[bface]) + append!(xBFaceNodes, view(oldBFaceNodes, :, bface)) + push!(xBFaceRegions, oldBFaceRegions[bface]) if store_parents push!(xBFaceParents, bface) end end end @debug "bisected bfaces = $newbfaces" - - xgrid[BFaceNodes] = reshape(xBFaceNodes,(2,nbfaces+newbfaces)) + + xgrid[BFaceNodes] = reshape(xBFaceNodes, (2, nbfaces + newbfaces)) xgrid[BFaceRegions] = xBFaceRegions - xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries,Int}(Edge1D,nbfaces+newbfaces) + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(Edge1D, nbfaces + newbfaces) xgrid[ParentGrid] = source_grid xgrid[ParentGridRelation] = RefinedGrid if store_parents - xgrid[CellParents] = xCellParents + xgrid[CellParents] = xCellParents xgrid[BFaceParents] = xBFaceParents end diff --git a/src/adjacency.jl b/src/adjacency.jl index b49d2838..fb53d2ad 100644 --- a/src/adjacency.jl +++ b/src/adjacency.jl @@ -21,23 +21,23 @@ end Comparison of two adjacencies """ -function Base.:(==)(a::VariableTargetAdjacency{Ta}, b::VariableTargetAdjacency{Tb}) where {Ta,Tb} - Ta==Tb && a.colentries==b.colentries && a.colstart==b.colstart -end +function Base.:(==)(a::VariableTargetAdjacency{Ta}, b::VariableTargetAdjacency{Tb}) where {Ta, Tb} + return Ta == Tb && a.colentries == b.colentries && a.colstart == b.colstart +end """ $(TYPEDSIGNATURES) Create an empty VariableTargetAdjacency """ -VariableTargetAdjacency(t::Type{T}) where T=VariableTargetAdjacency{T}(Vector{T}(undef,0),[one(T)]) +VariableTargetAdjacency(t::Type{T}) where {T} = VariableTargetAdjacency{T}(Vector{T}(undef, 0), [one(T)]) """ $(TYPEDSIGNATURES) Create an empty VariableTargetAdjacency with default type """ -VariableTargetAdjacency()=VariableTargetAdjacency(Int64) +VariableTargetAdjacency() = VariableTargetAdjacency(Int64) """ @@ -45,20 +45,21 @@ $(TYPEDSIGNATURES) Create a VariableTargetAdjacency from Matrix """ -VariableTargetAdjacency(m::Matrix{T}) where T=VariableTargetAdjacency{T}(vec(m),collect(1:size(m,1):size(m,1)*size(m,2)+1)) +VariableTargetAdjacency(m::Matrix{T}) where {T} = VariableTargetAdjacency{T}(vec(m), collect(1:size(m, 1):(size(m, 1) * size(m, 2) + 1))) """ $(TYPEDSIGNATURES) Show adjacency (in transposed form; preliminary) """ -function Base.show(io::IO,adj::VariableTargetAdjacency) - for isource=1:num_sources(adj) - for itarget=1:num_targets(adj,isource) - print(io,adj[itarget,isource], " ") +function Base.show(io::IO, adj::VariableTargetAdjacency) + for isource in 1:num_sources(adj) + for itarget in 1:num_targets(adj, isource) + print(io, adj[itarget, isource], " ") end - println(io, " "); + println(io, " ") end + return end """ @@ -66,67 +67,67 @@ $(TYPEDSIGNATURES) Access adjacency as if it is a 2D Array """ -Base.getindex(adj::VariableTargetAdjacency,i,isource)=adj.colentries[adj.colstart[isource]+i-1] -Base.getindex(adj::VariableTargetAdjacency,::Colon,isource)=adj.colentries[adj.colstart[isource]:adj.colstart[isource+1]-1] -Base.view(adj::VariableTargetAdjacency,::Colon,isource)=view(adj.colentries,adj.colstart[isource]:adj.colstart[isource+1]-1) +Base.getindex(adj::VariableTargetAdjacency, i, isource) = adj.colentries[adj.colstart[isource] + i - 1] +Base.getindex(adj::VariableTargetAdjacency, ::Colon, isource) = adj.colentries[adj.colstart[isource]:(adj.colstart[isource + 1] - 1)] +Base.view(adj::VariableTargetAdjacency, ::Colon, isource) = view(adj.colentries, adj.colstart[isource]:(adj.colstart[isource + 1] - 1)) """ $(TYPEDSIGNATURES) Number of targets for given source """ -num_targets(adj::VariableTargetAdjacency,isource)=adj.colstart[isource+1]-adj.colstart[isource] +num_targets(adj::VariableTargetAdjacency, isource) = adj.colstart[isource + 1] - adj.colstart[isource] """ $(TYPEDSIGNATURES) Number of sources in adjacency """ -num_sources(adj::VariableTargetAdjacency)=length(adj.colstart)-1 +num_sources(adj::VariableTargetAdjacency) = length(adj.colstart) - 1 """ $(TYPEDSIGNATURES) Number of targeta """ -num_targets(adj::VariableTargetAdjacency)=maximum(adj.colentries) +num_targets(adj::VariableTargetAdjacency) = maximum(adj.colentries) """ $(TYPEDSIGNATURES) Number of links """ -num_links(adj::VariableTargetAdjacency)=length(adj.colentries) +num_links(adj::VariableTargetAdjacency) = length(adj.colentries) """ $(TYPEDSIGNATURES) Maximum number of targets per source """ -max_num_targets_per_source(adj::VariableTargetAdjacency)=maximum(adj.colstart[2:end].-adj.colstart[1:end-1]) +max_num_targets_per_source(adj::VariableTargetAdjacency) = maximum(adj.colstart[2:end] .- adj.colstart[1:(end - 1)]) -Base.size(adj::VariableTargetAdjacency)=(max_num_targets_per_source(adj),num_sources(adj)) +Base.size(adj::VariableTargetAdjacency) = (max_num_targets_per_source(adj), num_sources(adj)) -function Matrix(adj::VariableTargetAdjacency{T}) where T - m=zeros(T,size(adj)...) - for isrc=1:num_sources(adj) - for itgt=1:num_targets(adj,isrc) - m[itgt,isrc]=adj[itgt,isrc] +function Matrix(adj::VariableTargetAdjacency{T}) where {T} + m = zeros(T, size(adj)...) + for isrc in 1:num_sources(adj) + for itgt in 1:num_targets(adj, isrc) + m[itgt, isrc] = adj[itgt, isrc] end end - m + return m end """ $(TYPEDSIGNATURES) Append a column to adjacency. """ -function Base.append!(adj::VariableTargetAdjacency,column) - for i=1:length(column) - push!(adj.colentries,column[i]) +function Base.append!(adj::VariableTargetAdjacency, column) + for i in 1:length(column) + push!(adj.colentries, column[i]) end - push!(adj.colstart,length(adj.colentries)+1) + return push!(adj.colstart, length(adj.colentries) + 1) end """ @@ -141,42 +142,42 @@ $(TYPEDSIGNATURES) Number of targets per source if adjacency is a matrix """ -num_targets(adj::FixedTargetAdjacency,isource)=size(adj)[1] +num_targets(adj::FixedTargetAdjacency, isource) = size(adj)[1] """ $(TYPEDSIGNATURES) Number of sources in adjacency """ -num_sources(adj::FixedTargetAdjacency)=size(adj)[2] +num_sources(adj::FixedTargetAdjacency) = size(adj)[2] """ $(TYPEDSIGNATURES) Overall number of targets """ -num_targets(adj::FixedTargetAdjacency)=maximum(vec(adj)) +num_targets(adj::FixedTargetAdjacency) = maximum(vec(adj)) """ $(TYPEDSIGNATURES) Number of entries """ -num_links(adj::FixedTargetAdjacency)=length(adj) +num_links(adj::FixedTargetAdjacency) = length(adj) """ $(TYPEDSIGNATURES) Maximum number of targets per source """ -max_num_targets_per_source(adj::FixedTargetAdjacency)=size(adj,1) +max_num_targets_per_source(adj::FixedTargetAdjacency) = size(adj, 1) """ $(TYPEDEF) Adjacency type as union of FixedTargetAdjacency and VariableTargetAdjacency """ -const Adjacency{T}=Union{FixedTargetAdjacency{T},VariableTargetAdjacency{T}} +const Adjacency{T} = Union{FixedTargetAdjacency{T}, VariableTargetAdjacency{T}} """ @@ -184,24 +185,24 @@ $(TYPEDSIGNATURES) Constructors for Adjacency """ -Adjacency{T}(a::FixedTargetAdjacency{T}) where T =a -Adjacency{T}(a::VariableTargetAdjacency{T}) where T =a +Adjacency{T}(a::FixedTargetAdjacency{T}) where {T} = a +Adjacency{T}(a::VariableTargetAdjacency{T}) where {T} = a """ Transpose adjacency """ -function atranspose(adj::Adjacency{T}) where T +function atranspose(adj::Adjacency{T}) where {T} # 0th pass: calculate number of rows !!! todo: how to call ? - t_adj=VariableTargetAdjacency(zeros(T,num_links(adj)),zeros(T,num_targets(adj)+1)) - + t_adj = VariableTargetAdjacency(zeros(T, num_links(adj)), zeros(T, num_targets(adj) + 1)) + # 1st pass: calculate new column sizes, store them in t_adj.colstart - for isource=1:num_sources(adj) - for itarget=1:num_targets(adj,isource) - t_adj.colstart[adj[itarget,isource]]+=1 + for isource in 1:num_sources(adj) + for itarget in 1:num_targets(adj, isource) + t_adj.colstart[adj[itarget, isource]] += 1 end end - + # 2nd pass: calculate initial assembly # addresses in adj.ja by summing up deltas # store them a the ends of the columns: @@ -209,30 +210,30 @@ function atranspose(adj::Adjacency{T}) where T # it will be overwritten when the last element of # the column is assembled. # We get automatically increasing column indices. - - delta=t_adj.colstart[1]; - t_adj.colstart[1]=1; - for isource=2:num_sources(t_adj) - save=t_adj.colstart[isource]; - t_adj.colstart[isource]=t_adj.colstart[isource-1]+delta; - if delta>0 - t_adj.colentries[t_adj.colstart[isource]-1]=t_adj.colstart[isource-1]; + + delta = t_adj.colstart[1] + t_adj.colstart[1] = 1 + for isource in 2:num_sources(t_adj) + save = t_adj.colstart[isource] + t_adj.colstart[isource] = t_adj.colstart[isource - 1] + delta + if delta > 0 + t_adj.colentries[t_adj.colstart[isource] - 1] = t_adj.colstart[isource - 1] end - delta=save; + delta = save end - isource=num_sources(t_adj)+1 - t_adj.colstart[isource]=t_adj.colstart[isource-1]+delta; - if delta>0 - t_adj.colentries[t_adj.colstart[isource]-1]=t_adj.colstart[isource-1]; + isource = num_sources(t_adj) + 1 + t_adj.colstart[isource] = t_adj.colstart[isource - 1] + delta + if delta > 0 + t_adj.colentries[t_adj.colstart[isource] - 1] = t_adj.colstart[isource - 1] end - + # 3rd pass: assemble new columns - for isource=1:num_sources(adj) - for itarget=1:num_targets(adj,isource) - asm_idx=t_adj.colstart[adj[itarget,isource]+1]-1; - asm_loc=t_adj.colentries[asm_idx]; - t_adj.colentries[asm_idx]+=1; - t_adj.colentries[asm_loc]=isource; + for isource in 1:num_sources(adj) + for itarget in 1:num_targets(adj, isource) + asm_idx = t_adj.colstart[adj[itarget, isource] + 1] - 1 + asm_loc = t_adj.colentries[asm_idx] + t_adj.colentries[asm_idx] += 1 + t_adj.colentries[asm_loc] = isource end end return t_adj @@ -243,14 +244,14 @@ $(TYPEDSIGNATURES) Try to turn variable target adjacency into fixed target adjacency """ -function tryfix(a::Adjacency{T}) where T - ntargets=num_targets(a,1) - for i=1:num_sources(a) - if num_targets(a,i)!=ntargets +function tryfix(a::Adjacency{T}) where {T} + ntargets = num_targets(a, 1) + for i in 1:num_sources(a) + if num_targets(a, i) != ntargets return a end end - FixedTargetAdjacency(reshape(a.colentries,(ntargets,num_sources(a)))) + return FixedTargetAdjacency(reshape(a.colentries, (ntargets, num_sources(a)))) end """ @@ -258,11 +259,11 @@ $(TYPEDSIGNATURES) Turn fixed target adjacency into variable target adjacency """ -function makevar(a::FixedTargetAdjacency{T}) where T - ntargets=num_targets(a,1) - nsources=num_sources(a) - colstart=T[i*ntargets+1 for i=0:nsources] - VariableTargetAdjacency(vec(a),colstart) +function makevar(a::FixedTargetAdjacency{T}) where {T} + ntargets = num_targets(a, 1) + nsources = num_sources(a) + colstart = T[i * ntargets + 1 for i in 0:nsources] + return VariableTargetAdjacency(vec(a), colstart) end @@ -272,10 +273,10 @@ $(TYPEDSIGNATURES) Create sparse incidence matrix from adjacency """ function asparse(a::VariableTargetAdjacency) - n=num_sources(a) - m=num_targets(a) - nzval=ones(Int,length(a.colentries)) - SparseMatrixCSC(m,n,a.colstart,a.colentries,nzval) + n = num_sources(a) + m = num_targets(a) + nzval = ones(Int, length(a.colentries)) + return SparseMatrixCSC(m, n, a.colstart, a.colentries, nzval) end @@ -284,7 +285,7 @@ $(TYPEDSIGNATURES) Create sparse incidence matrix from adjacency """ -asparse(a::FixedTargetAdjacency)=asparse(makevar(a)) +asparse(a::FixedTargetAdjacency) = asparse(makevar(a)) """ @@ -292,4 +293,4 @@ $(TYPEDSIGNATURES) Create variable target adjacency from adjacency matrix """ -VariableTargetAdjacency(m::SparseMatrixCSC{Tv,Ti}) where {Tv<:Integer, Ti<:Integer} = VariableTargetAdjacency{Ti}(m.rowval,m.colptr) +VariableTargetAdjacency(m::SparseMatrixCSC{Tv, Ti}) where {Tv <: Integer, Ti <: Integer} = VariableTargetAdjacency{Ti}(m.rowval, m.colptr) diff --git a/src/arraytools.jl b/src/arraytools.jl index b52fad0d..a62053a2 100644 --- a/src/arraytools.jl +++ b/src/arraytools.jl @@ -3,7 +3,7 @@ $(TYPEDSIGNATURES) Resurrect linspace despite https://github.com/JuliaLang/julia/pull/25896#issuecomment-363769368 """ -linspace(a,b,n)=collect(range(a,b,length=n)) +linspace(a, b, n) = collect(range(a, b, length = n)) """ @@ -21,163 +21,163 @@ Caveat: the algorithm behind this is tested for many cases but unproven. Returns an Array containing the points of the subdivision. """ -function geomspace(a, b, ha, hb ; tol=1.0e-10, maxiterations=100) - a=Float64(a) - b=Float64(b) - ha=Float64(ha) - hb=Float64(hb) - - function _geomspace0(l,h0, hl, tol=1.0e-10) - @assert (l>0.0) - @assert (h0>0.0) - @assert (hl>=h0) - @assert((hl+h0) 0.0) + @assert (h0 > 0.0) + @assert (hl >= h0) + @assert((hl + h0) < l) # We need to adjust two things: - + # The sum of the geometric progression must # match the length of the interval, so lmismatch # should be zero: - function lmismatch(q,k) - return l - h0*(1-q^k)/(1-q) + function lmismatch(q, k) + return l - h0 * (1 - q^k) / (1 - q) end - - # The claim from experimenral evidence (Wolfram) + + # The claim from experimenral evidence (Wolfram) # is that, if written as a polynomial, # it has two real zeros: one and the value searched for which # is slightly larger than one. All other zeros are on one circle. - - # The size of the last interval should be close to + + # The size of the last interval should be close to # to hl, so hmismatch should be close to one and not larger than one - function hmismatch(q,k) - return h0*q^(k-1)/hl + function hmismatch(q, k) + return h0 * q^(k - 1) / hl end # define initial number of intervals from # average of minimal and maximal h - n=Int(ceil((2.0*l/(h0+hl)))) - + n = Int(ceil((2.0 * l / (h0 + hl)))) + - if n==1 - n=2 + if n == 1 + n = 2 end - + # define initial q such that hmismatch is one. - q=(hl/h0)^(1.0/(n-1.0)) - + q = (hl / h0)^(1.0 / (n - 1.0)) + # Iteration until both mismatches are satisfactory # Outer loop runs until hmismatch is less than 1 - hmiss=10.0 # some initial value >1 just to run the loop at least once - if abs(q-1.0)1 just to run the loop at least once + if abs(q - 1.0) < tol + hmiss = 1.0 end - while hmiss>1.0 + while hmiss > 1.0 # increase number of intervals until # lmismatch becomes less than zero - while lmismatch(q,n)>tol - n+=1 + while lmismatch(q, n) > tol + n += 1 end # find initial interval for q containing - # value with zero lmismatch - ns=0 - - while lmismatch(q,n)tol && ns tol && ns < maxiterations + ns += 1 + mmm = lmismatch(qm, n) + if abs(mmm) < tol break - elseif lmismatch(ql,n)*mmm<0 - qr=qm + elseif lmismatch(ql, n) * mmm < 0 + qr = qm else - ql=qm + ql = qm end - qm=0.5*(ql+qr) + qm = 0.5 * (ql + qr) end # increase q slightly to increase probability # for last interval to be <=hl - q=qm*(1.0+tol) + q = qm * (1.0 + tol) - @assert ns1.0+tol - n=n+1 + @assert ns < maxiterations "Unable to determine geomspace data after $(maxiterations) iterations" + hmiss = hmismatch(q, n) + if hmiss > 1.0 + tol + n = n + 1 end end # printf("%d %g %g %g\n",n,q,lmismatch(q,n),hmismatch(q,n)) - X = Array{Float64,1}(undef,n+1) - X[1]=0 - h=h0 - for i=1:n - X[i+1]=X[i]+h - h*=q + X = Array{Float64, 1}(undef, n + 1) + X[1] = 0 + h = h0 + for i in 1:n + X[i + 1] = X[i] + h + h *= q end - X[n+1]=l + X[n + 1] = l # Fix last interval and remove "overshoots" - if X[end]>l - X[end]=l + if X[end] > l + X[end] = l end - - while X[end-1]+tol>X[end] + + while X[end - 1] + tol > X[end] pop!(X) - X[end]=l + X[end] = l end - + return X end - @assert (ha>0.0) "Start step size $(ha) should be positive" - @assert (hb>0.0) "End step size $(hb) should be positive" - @assert (a 0.0) "Start step size $(ha) should be positive" + @assert (hb > 0.0) "End step size $(hb) should be positive" + @assert (a < b) "Interval ends $(a), $(b) should be increasing" + @assert ((ha + hb) < b - a) "Sum of step sizes $(ha)+$(hb) should not exceed interval size $(b)-$(a)" + - # Map things to [0,b-a] - tol=tol*(b-a) - if hahb+tol - X=-reverse(_geomspace0(b-a,hb,ha,tol)) - X.+=b + tol = tol * (b - a) + if ha < hb - tol + X = _geomspace0(b - a, ha, hb, tol) + X .+= a + elseif ha > hb + tol + X = -reverse(_geomspace0(b - a, hb, ha, tol)) + X .+= b else - n=Int(ceil((b-a)/ha)) - X=collect(range(a,b,length=n+1)) + n = Int(ceil((b - a) / ha)) + X = collect(range(a, b, length = n + 1)) end -# @show X[2]-X[1],ha, X[end]-X[end-1],hb + # @show X[2]-X[1],ha, X[end]-X[end-1],hb + + @assert (X[2] - X[1]) <= ha + tol "First interval turned out to be larger than $(ha)" + @assert (X[2] - X[1]) > ha / 2 "First interval turned out to be less than $(ha)/10" + @assert (X[end] - X[end - 1]) <= hb + tol "Last interval turned out to be larger than $(hb)" + @assert (X[end] - X[end - 1]) >= hb / 2 "Last interval turned out to be less than $(hb)/10" + @assert abs(X[1] - a) < tol "Range start $(X[1]) differs from $(a)" + @assert abs(X[end] - b) < tol "Range end $(X[end]) differs from $(b)" - @assert (X[2]-X[1])<=ha+tol "First interval turned out to be larger than $(ha)" - @assert (X[2]-X[1])>ha/2 "First interval turned out to be less than $(ha)/10" - @assert (X[end]-X[end-1])<=hb+tol "Last interval turned out to be larger than $(hb)" - @assert (X[end]-X[end-1])>=hb/2 "Last interval turned out to be less than $(hb)/10" - @assert abs(X[1]-a)0) +is_monotone(X) = all(X[2:end] - X[1:(end - 1)] .> 0) # See https://discourse.julialang.org/t/how-to-execute-code-depending-on-julia-version/75029/3 @static if VERSION < v"1.7" - is_monotone(X::UnitRange)=all(collect(X[2:end])-collect(X[1:end-1]).>0) + is_monotone(X::UnitRange) = all(collect(X[2:end]) - collect(X[1:(end - 1)]) .> 0) end """ @@ -187,32 +187,32 @@ Glue together two vectors `a` and b resulting in a vector c. They last element of `a` shall be equal (up to tol) to the first element of b. The result fulfills `length(c)=length(a)+length(b)-1` """ -function glue(_a::AbstractVector, _b::AbstractVector; tol=1.0e-10) +function glue(_a::AbstractVector, _b::AbstractVector; tol = 1.0e-10) is_monotone(_a) || error("non-monotonous first argument of glue") is_monotone(_b) || error("non-monotonous second argument of glue") - Tv=promote_type(eltype(_a),eltype(_b)) - - a=convert(Vector{Tv},_a) - b=convert(Vector{Tv},_b) - - na=length(a) - nb=length(b) - - d=b[1]-a[na-1] - @assert(d>0) - d=b[1]-a[na] - @assert(d>-tol) - @assert(d 0) + d = b[1] - a[na] + @assert(d > -tol) + @assert(d < tol) + + c = Vector{Tv}(undef, na + nb - 1) + ic = 0 + for ia in 1:na + ic += 1 + c[ic] = a[ia] end - for ib=2:nb - ic+=1 - c[ic]=b[ib] + for ib in 2:nb + ic += 1 + c[ic] = b[ib] end return c end diff --git a/src/assemblytypes.jl b/src/assemblytypes.jl index c3a6678d..42eec792 100644 --- a/src/assemblytypes.jl +++ b/src/assemblytypes.jl @@ -5,7 +5,7 @@ # this type is used to steer where certain things live and assemble on # mainly if it lives on CELLs, FACEs or BFACEs -abstract type AssemblyType end +abstract type AssemblyType end """ $(TYPEDEF) @@ -19,7 +19,7 @@ $(TYPEDEF) causes assembly/interpolation on cells of the grid """ -abstract type ON_CELLS <: AssemblyType end # on all cells +abstract type ON_CELLS <: AssemblyType end # on all cells """ $(TYPEDEF) @@ -57,25 +57,25 @@ causes assembly/interpolation on boundary edges of the grid (only in 3D) abstract type ON_BEDGES <: AssemblyType end # on boundary edges function Base.show(io::Core.IO, ::Type{AT_NODES}) - print(io,"AT_NODES") + return print(io, "AT_NODES") end function Base.show(io::Core.IO, ::Type{ON_CELLS}) - print(io,"ON_CELLS") + return print(io, "ON_CELLS") end function Base.show(io::Core.IO, ::Type{ON_FACES}) - print(io,"ON_FACES") + return print(io, "ON_FACES") end function Base.show(io::Core.IO, ::Type{ON_BFACES}) - print(io,"ON_BFACES") + return print(io, "ON_BFACES") end function Base.show(io::Core.IO, ::Type{ON_IFACES}) - print(io,"ON_IFACES") + return print(io, "ON_IFACES") end function Base.show(io::Core.IO, ::Type{ON_EDGES}) - print(io,"ON_EDGES") + return print(io, "ON_EDGES") end function Base.show(io::Core.IO, ::Type{ON_BEDGES}) - print(io,"ON_BEDGES") + return print(io, "ON_BEDGES") end ItemType4AssemblyType(::Type{ON_CELLS}) = ITEMTYPE_CELL @@ -84,9 +84,9 @@ ItemType4AssemblyType(::Type{ON_BFACES}) = ITEMTYPE_BFACE ItemType4AssemblyType(::Type{<:ON_EDGES}) = ITEMTYPE_EDGE ItemType4AssemblyType(::Type{ON_BEDGES}) = ITEMTYPE_BEDGE -GridComponentNodes4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT),PROPERTY_NODES) -GridComponentVolumes4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT),PROPERTY_VOLUME) -GridComponentGeometries4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT),PROPERTY_GEOMETRY) -GridComponentUniqueGeometries4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT),PROPERTY_UNIQUEGEOMETRY) -GridComponentRegions4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT),PROPERTY_REGION) -GridComponentAssemblyGroups4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT),PROPERTY_ASSEMBLYGROUP) +GridComponentNodes4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT), PROPERTY_NODES) +GridComponentVolumes4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT), PROPERTY_VOLUME) +GridComponentGeometries4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT), PROPERTY_GEOMETRY) +GridComponentUniqueGeometries4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT), PROPERTY_UNIQUEGEOMETRY) +GridComponentRegions4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT), PROPERTY_REGION) +GridComponentAssemblyGroups4AssemblyType(AT::Type{<:AssemblyType}) = GridComponent4TypeProperty(ItemType4AssemblyType(AT), PROPERTY_ASSEMBLYGROUP) diff --git a/src/binnedpointlist.jl b/src/binnedpointlist.jl index ddaa78de..ef3fe754 100644 --- a/src/binnedpointlist.jl +++ b/src/binnedpointlist.jl @@ -98,12 +98,14 @@ end Create and initialize binned point list """ -function BinnedPointList(::Type{T}, dim; - tol = 1.0e-12, - number_of_directional_bins = 10, - binning_region_increase_factor = 0.01, - num_allowed_unbinned_points = 5, - max_unbinned_ratio = 0.05) where {T} +function BinnedPointList( + ::Type{T}, dim; + tol = 1.0e-12, + number_of_directional_bins = 10, + binning_region_increase_factor = 0.01, + num_allowed_unbinned_points = 5, + max_unbinned_ratio = 0.05 + ) where {T} bpl = BinnedPointList{T}(nothing) bpl.dim = dim @@ -122,7 +124,7 @@ function BinnedPointList(::Type{T}, dim; bpl.bins = Array{Vector{Int32}, dim}(undef, zeros(Int32, dim)...) bpl.current_bin = zeros(Int32, bpl.dim) - bpl + return bpl end """ @@ -140,7 +142,7 @@ Find point in index list (by linear search) Return its index, or zero if not found """ function _findpoint(bpl, index, p) - for i = 1:length(index) + for i in 1:length(index) @views if norm(bpl.points[:, index[i]] - p) < bpl.tol return index[i] end @@ -153,9 +155,9 @@ end Calculate the bin of the point. Result is stored in bpl.current_bin -""" +""" function _bin_of_point!(bpl, p) - for idim = 1:(bpl.dim) + for idim in 1:(bpl.dim) # scaled value for particular dimension s = (p[idim] - bpl.binning_region_min[idim]) / (bpl.binning_region_max[idim] - bpl.binning_region_min[idim]) if s > 1 || s < 0 @@ -166,6 +168,7 @@ function _bin_of_point!(bpl, p) bpl.current_bin[idim] = ceil(Int32, bpl.number_of_directional_bins * s) end end + return end """ @@ -177,13 +180,15 @@ This amounts to two steps: - Re-calculate all point bins """ function _rebin_all_points!(bpl) - if length(bpl.unbinned) > max(bpl.num_allowed_unbinned_points, - bpl.max_unbinned_ratio * size(bpl.points, 2)) + if length(bpl.unbinned) > max( + bpl.num_allowed_unbinned_points, + bpl.max_unbinned_ratio * size(bpl.points, 2) + ) # Calculate extrema of unbinned points @views e = extrema(bpl.points[:, bpl.unbinned]; dims = 2) - for i = 1:(bpl.dim) + for i in 1:(bpl.dim) # Increase binning region according to unbinned extrema bpl.binning_region_min[i] = min(bpl.binning_region_min[i], e[i][1]) bpl.binning_region_max[i] = max(bpl.binning_region_max[i], e[i][2]) @@ -197,17 +202,24 @@ function _rebin_all_points!(bpl) # Re-allocate all bins if bpl.dim == 1 - bpl.bins = [zeros(Int32, 0) for i = 1:(bpl.number_of_directional_bins)] + bpl.bins = [zeros(Int32, 0) for i in 1:(bpl.number_of_directional_bins)] elseif bpl.dim == 2 - bpl.bins = [zeros(Int32, 0) for i = 1:(bpl.number_of_directional_bins), j = 1:(bpl.number_of_directional_bins)] + bpl.bins = [ + zeros(Int32, 0) + for i in 1:(bpl.number_of_directional_bins), + j in 1:(bpl.number_of_directional_bins) + ] elseif bpl.dim == 3 - bpl.bins = [zeros(Int32, 0) - for i = 1:(bpl.number_of_directional_bins), j = 1:(bpl.number_of_directional_bins), - k = 1:(bpl.number_of_directional_bins)] + bpl.bins = [ + zeros(Int32, 0) + for i in 1:(bpl.number_of_directional_bins), + j in 1:(bpl.number_of_directional_bins), + k in 1:(bpl.number_of_directional_bins) + ] end # Register all points in their respctive bins - for i = 1:size(bpl.points, 2) + for i in 1:size(bpl.points, 2) @views _bin_of_point!(bpl, bpl.points[:, i]) push!(bpl.bins[bpl.current_bin...], i) end @@ -215,6 +227,7 @@ function _rebin_all_points!(bpl) # Re-allocate unbinned index bpl.unbinned = zeros(Int32, 0) end + return end @@ -265,6 +278,7 @@ function Base.insert!(bpl::BinnedPointList{T}, p) where {T} return i end end + return end """ @@ -296,11 +310,11 @@ Insert via linear search, without any binning. Just for being able to check of all of the above was worth the effort... """ function naiveinsert!(bpl::BinnedPointList{T}, p) where {T} - for i = 1:size(bpl.points, 2) + for i in 1:size(bpl.points, 2) @views if norm(bpl.points[:, i] - p) < bpl.tol return i end end append!(bpl.points, p) - size(bpl.points, 2) + return size(bpl.points, 2) end diff --git a/src/cellfinder.jl b/src/cellfinder.jl index 99bd64f8..dafead3a 100644 --- a/src/cellfinder.jl +++ b/src/cellfinder.jl @@ -3,17 +3,17 @@ CellFinder supports finding cells in grids. """ -struct CellFinder{Tv,Ti} - xgrid::ExtendableGrid{Tv,Ti} +struct CellFinder{Tv, Ti} + xgrid::ExtendableGrid{Tv, Ti} xCellFaces::Adjacency{Ti} xFaceCells::Adjacency{Ti} xCellGeometries::GridEGTypes - facetogo::Array{Array{Ti,1},1} - previous_cells::Array{Ti,1} - EG::Array{Type{<:AbstractElementGeometry},1} - L2G4EG::Vector{L2GTransformer{Tv,Ti,EG,CS} where {EG<:AbstractElementGeometry,CS<:AbstractCoordinateSystem}} # each Geometry has its own L2GTransformer + facetogo::Array{Array{Ti, 1}, 1} + previous_cells::Array{Ti, 1} + EG::Array{Type{<:AbstractElementGeometry}, 1} + L2G4EG::Vector{L2GTransformer{Tv, Ti, EG, CS} where {EG <: AbstractElementGeometry, CS <: AbstractCoordinateSystem}} # each Geometry has its own L2GTransformer invA::Matrix{Tv} - xreftest::Array{Tv,1} + xreftest::Array{Tv, 1} cx::Vector{Tv} end @@ -48,38 +48,38 @@ end Create a cell finder on grid. """ -function CellFinder(xgrid::ExtendableGrid{Tv,Ti}) where {Tv,Ti} +function CellFinder(xgrid::ExtendableGrid{Tv, Ti}) where {Tv, Ti} CS = xgrid[CoordinateSystem] EG = xgrid[UniqueCellGeometries] - L2G4EG = Vector{L2GTransformer{Tv,Ti,EG,CS} where {EG<:AbstractElementGeometry,CS<:AbstractCoordinateSystem}}(undef,length(EG)) - facetogo = Array{Array{Ti,1},1}(undef,length(EG)) + L2G4EG = Vector{L2GTransformer{Tv, Ti, EG, CS} where {EG <: AbstractElementGeometry, CS <: AbstractCoordinateSystem}}(undef, length(EG)) + facetogo = Array{Array{Ti, 1}, 1}(undef, length(EG)) xreftestlength::Int = 0 - for j = 1 : length(EG) + for j in 1:length(EG) L2G4EG[j] = L2GTransformer(EG[j], xgrid, ON_CELLS) if EG[j] <: AbstractElementGeometry1D facetogo[j] = [1, 2] - xreftestlength = max(xreftestlength,2) + xreftestlength = max(xreftestlength, 2) elseif EG[j] <: Triangle2D facetogo[j] = [3, 1, 2] - xreftestlength = max(xreftestlength,3) + xreftestlength = max(xreftestlength, 3) elseif EG[j] <: Tetrahedron3D facetogo[j] = [4, 2, 1, 3] - xreftestlength = max(xreftestlength,4) + xreftestlength = max(xreftestlength, 4) elseif EG[j] <: Parallelogram2D facetogo[j] = [4, 1, 2, 3] - xreftestlength = max(xreftestlength,4) + xreftestlength = max(xreftestlength, 4) elseif EG[j] <: Parallelepiped3D facetogo[j] = [5, 2, 1, 3, 4, 6] - xreftestlength = max(xreftestlength,6) + xreftestlength = max(xreftestlength, 6) else @error "ElementGeometry not supported by CellFinder" end end edim = dim_element(EG[1]) - A = zeros(Tv,edim,edim) - xreftest = zeros(Tv,xreftestlength) - return CellFinder{Tv,Ti}(xgrid, xgrid[CellFaces], xgrid[FaceCells], xgrid[CellGeometries], facetogo, zeros(Ti,3), EG, L2G4EG, A, xreftest, zeros(Tv,edim)) + A = zeros(Tv, edim, edim) + xreftest = zeros(Tv, xreftestlength) + return CellFinder{Tv, Ti}(xgrid, xgrid[CellFaces], xgrid[FaceCells], xgrid[CellGeometries], facetogo, zeros(Ti, 3), EG, L2G4EG, A, xreftest, zeros(Tv, edim)) end @@ -95,7 +95,7 @@ Upon return, xref contains the barycentric coordinates of the point in the seque !!! warning Currently implemented for simplex grids only. """ -function gFindLocal!(xref, CF::CellFinder{Tv,Ti}, x; icellstart::Int = 1,trybrute=true, eps = 1e-14) where{Tv,Ti} +function gFindLocal!(xref, CF::CellFinder{Tv, Ti}, x; icellstart::Int = 1, trybrute = true, eps = 1.0e-14) where {Tv, Ti} # works for convex domainsand simplices only ! xCellFaces::Adjacency{Ti} = CF.xCellFaces @@ -104,12 +104,12 @@ function gFindLocal!(xref, CF::CellFinder{Tv,Ti}, x; icellstart::Int = 1,trybrut EG::GridEGTypes = CF.EG cx::Vector{Tv} = CF.cx cEG::Int = 0 - facetogo::Array{Array{Ti,1},1} = CF.facetogo + facetogo::Array{Array{Ti, 1}, 1} = CF.facetogo icell::Int = icellstart - previous_cells::Array{Ti,1} = CF.previous_cells - fill!(previous_cells,0) - xreftest::Array{Tv,1} = CF.xreftest - L2G::L2GTransformer{Tv,Ti} = CF.L2G4EG[1] + previous_cells::Array{Ti, 1} = CF.previous_cells + fill!(previous_cells, 0) + xreftest::Array{Tv, 1} = CF.xreftest + L2G::L2GTransformer{Tv, Ti} = CF.L2G4EG[1] L2Gb::Vector{Tv} = L2G.b invA::Matrix{Tv} = CF.invA @@ -128,19 +128,19 @@ function gFindLocal!(xref, CF::CellFinder{Tv,Ti}, x; icellstart::Int = 1,trybrut L2Gb = L2G.b # compute barycentric coordinates of node - for j = 1 : length(cx) + for j in 1:length(cx) cx[j] = x[j] - L2Gb[j] end - mapderiv!(invA,L2G,xref) - fill!(xreftest,0) - for j = 1 : length(cx), k = 1 : length(cx) - xreftest[k] += invA[j,k] * cx[j] + mapderiv!(invA, L2G, xref) + fill!(xreftest, 0) + for j in 1:length(cx), k in 1:length(cx) + xreftest[k] += invA[j, k] * cx[j] end - postprocess_xreftest!(xreftest,xCellGeometries[icell]) + postprocess_xreftest!(xreftest, xCellGeometries[icell]) # find minimal barycentric coordinate with imin = 1 - for i = 2 : length(facetogo[cEG]) + for i in 2:length(facetogo[cEG]) if xreftest[imin] >= xreftest[i] imin = i end @@ -148,22 +148,22 @@ function gFindLocal!(xref, CF::CellFinder{Tv,Ti}, x; icellstart::Int = 1,trybrut # if all barycentric coordinates are within [0,1] the including cell is found if xreftest[imin] >= -eps - xref .= view(xreftest,1:length(xref)) + xref .= view(xreftest, 1:length(xref)) return icell end # otherwise: go into direction of minimal barycentric coordinates - for j = 1 : length(previous_cells)-1 - previous_cells[j] = previous_cells[j+1] + for j in 1:(length(previous_cells) - 1) + previous_cells[j] = previous_cells[j + 1] end previous_cells[end] = icell - icell = xFaceCells[1,xCellFaces[facetogo[cEG][imin],icell]] + icell = xFaceCells[1, xCellFaces[facetogo[cEG][imin], icell]] if icell == previous_cells[end] - icell = xFaceCells[2,xCellFaces[facetogo[cEG][imin],icell]] + icell = xFaceCells[2, xCellFaces[facetogo[cEG][imin], icell]] if icell == 0 !trybrute if trybrute - return gFindBruteForce!(xref,CF,x; eps) + return gFindBruteForce!(xref, CF, x; eps) else @debug "could not find point in any cell and ended up at boundary of domain (maybe x lies outside of the domain ?)" return 0 @@ -171,16 +171,16 @@ function gFindLocal!(xref, CF::CellFinder{Tv,Ti}, x; icellstart::Int = 1,trybrut end end - if icell == previous_cells[end-1] && !trybrute + if icell == previous_cells[end - 1] && !trybrute if trybrute - return gFindBruteForce!(xref,CF,x; eps) + return gFindBruteForce!(xref, CF, x; eps) else @debug "could not find point in any cell and ended up at boundary of domain (maybe x lies outside of the domain ?)" return 0 end end end - + return 0 end @@ -197,19 +197,19 @@ Upon return, xref contains the barycentric coordinates of the point in the seque Currently implemented for simplex grids only. """ -function gFindBruteForce!(xref, CF::CellFinder{Tv,Ti}, x; eps = 1e-14) where {Tv,Ti} +function gFindBruteForce!(xref, CF::CellFinder{Tv, Ti}, x; eps = 1.0e-14) where {Tv, Ti} cx::Vector{Tv} = CF.cx cEG::Int = 0 EG::GridEGTypes = CF.EG - xreftest::Array{Tv,1} = CF.xreftest + xreftest::Array{Tv, 1} = CF.xreftest xCellGeometries::GridEGTypes = CF.xCellGeometries - facetogo::Array{Array{Ti,1},1} = CF.facetogo + facetogo::Array{Array{Ti, 1}, 1} = CF.facetogo L2Gb::Vector{Tv} = CF.L2G4EG[1].b invA::Matrix{Tv} = CF.invA imin::Int = 0 - for icell = 1 : num_sources(CF.xgrid[CellNodes]) + for icell in 1:num_sources(CF.xgrid[CellNodes]) # find current cell geometry index cEG = 1 @@ -223,19 +223,19 @@ function gFindBruteForce!(xref, CF::CellFinder{Tv,Ti}, x; eps = 1e-14) where {Tv L2Gb = L2G.b # compute barycentric coordinates of node - for j = 1 : length(x) + for j in 1:length(x) cx[j] = x[j] - L2Gb[j] end - mapderiv!(invA,L2G,xref) - fill!(xreftest,0) - for j = 1 : length(x), k = 1 : length(x) - xreftest[k] += invA[j,k] * cx[j] + mapderiv!(invA, L2G, xref) + fill!(xreftest, 0) + for j in 1:length(x), k in 1:length(x) + xreftest[k] += invA[j, k] * cx[j] end - postprocess_xreftest!(xreftest,xCellGeometries[icell]) + postprocess_xreftest!(xreftest, xCellGeometries[icell]) # find minimal barycentric coordinate with imin = 1 - for i = 2 : length(facetogo[cEG]) + for i in 2:length(facetogo[cEG]) if xreftest[imin] >= xreftest[i] imin = i end @@ -243,13 +243,13 @@ function gFindBruteForce!(xref, CF::CellFinder{Tv,Ti}, x; eps = 1e-14) where {Tv # if all barycentric coordinates are within [0,1] the including cell is found if xreftest[imin] >= -eps - xref .= view(xreftest,1:length(xref)) + xref .= view(xreftest, 1:length(xref)) return icell end end @debug "gFindBruteForce did not find any cell that contains x = $x (make sure that x is inside the domain, or try reducing $eps)" - + return 0 end @@ -259,42 +259,42 @@ end Mutating form of [`interpolate`](@ref) """ -function interpolate!(u_to::AbstractArray,grid_to, u_from::AbstractArray, grid_from;eps=1.0e-14,trybrute=true) - shuffle=[[2,1], [3,1,2], [4,1,2,3]] - - update!(u_to::AbstractVector,inode_to,λ,u_from::AbstractVector,inode_from)=u_to[inode_to]+=λ*u_from[inode_from] - update!(u_to::AbstractMatrix,inode_to,λ,u_from::AbstractMatrix,inode_from)=@views u_to[:,inode_to]+=λ*u_from[:,inode_from] - - coord=grid_to[Coordinates] - dim=size(coord,1) - nnodes_to=size(coord,2) - nnodes_from=num_nodes(grid_from) - if ndims(u_from)==1 - @assert length(u_to)==nnodes_to - @assert length(u_from)==nnodes_from - elseif ndims(u_from)==2 - @assert size(u_to,2)==nnodes_to - @assert size(u_from,2)==nnodes_from - @assert size(u_to,1)==size(u_from,1) +function interpolate!(u_to::AbstractArray, grid_to, u_from::AbstractArray, grid_from; eps = 1.0e-14, trybrute = true) + shuffle = [[2, 1], [3, 1, 2], [4, 1, 2, 3]] + + update!(u_to::AbstractVector, inode_to, λ, u_from::AbstractVector, inode_from) = u_to[inode_to] += λ * u_from[inode_from] + update!(u_to::AbstractMatrix, inode_to, λ, u_from::AbstractMatrix, inode_from) = @views u_to[:, inode_to] += λ * u_from[:, inode_from] + + coord = grid_to[Coordinates] + dim = size(coord, 1) + nnodes_to = size(coord, 2) + nnodes_from = num_nodes(grid_from) + if ndims(u_from) == 1 + @assert length(u_to) == nnodes_to + @assert length(u_from) == nnodes_from + elseif ndims(u_from) == 2 + @assert size(u_to, 2) == nnodes_to + @assert size(u_from, 2) == nnodes_from + @assert size(u_to, 1) == size(u_from, 1) else - @assert ndims(u_from)<3 + @assert ndims(u_from) < 3 end - - λ = zeros(dim+1) - λ_shuffle=view(λ,shuffle[dim]) - cn_from=grid_from[CellNodes] + + λ = zeros(dim + 1) + λ_shuffle = view(λ, shuffle[dim]) + cn_from = grid_from[CellNodes] cf = CellFinder(grid_from) - icellstart=1 - for inode_to=1:nnodes_to - @views icell_from=gFindLocal!(λ, cf, coord[:,inode_to];icellstart,eps,trybrute) - @assert icell_from>0 - for i=1:dim+1 - inode_from=cn_from[i,icell_from] + icellstart = 1 + for inode_to in 1:nnodes_to + @views icell_from = gFindLocal!(λ, cf, coord[:, inode_to]; icellstart, eps, trybrute) + @assert icell_from > 0 + for i in 1:(dim + 1) + inode_from = cn_from[i, icell_from] update!(u_to, inode_to, λ_shuffle[i], u_from, inode_from) - end - icell_start=icell_from + end + icell_start = icell_from end - u_to + return u_to end @@ -309,12 +309,12 @@ Works for matrices with second dimension corresponding to grid nodes and for vec !!! warning Currently implemented for simplex grids only. """ -function interpolate(grid_to, u_from::AbstractVector, grid_from;eps=1.0e-14,trybrute=true) - u_to=zeros(eltype(u_from),num_nodes(grid_to)) - interpolate!(u_to,grid_to, u_from, grid_from;eps,trybrute) +function interpolate(grid_to, u_from::AbstractVector, grid_from; eps = 1.0e-14, trybrute = true) + u_to = zeros(eltype(u_from), num_nodes(grid_to)) + return interpolate!(u_to, grid_to, u_from, grid_from; eps, trybrute) end -function interpolate(grid_to, u_from::AbstractMatrix, grid_from;eps=1.0e-14,trybrute=true) - u_to=zeros(eltype(u_from),size(u_from,1),num_nodes(grid_to)) - interpolate!(u_to,grid_to, u_from, grid_from;eps,trybrute) +function interpolate(grid_to, u_from::AbstractMatrix, grid_from; eps = 1.0e-14, trybrute = true) + u_to = zeros(eltype(u_from), size(u_from, 1), num_nodes(grid_to)) + return interpolate!(u_to, grid_to, u_from, grid_from; eps, trybrute) end diff --git a/src/commongrids.jl b/src/commongrids.jl index bbdf5191..fd0d3712 100644 --- a/src/commongrids.jl +++ b/src/commongrids.jl @@ -9,30 +9,30 @@ Generates an ExtendableGrid{T,Int32} for the reference domain of the specified Element Geometry. With scale and shift the coordinates can be manipulated. """ -function reference_domain(EG::Type{<:AbstractElementGeometry}, T::Type{<:Real} = Float64; scale = [1,1,1], shift = [0,0,0]) - xgrid=ExtendableGrid{T,Int32}() - xCoordinates=Array{T,2}(refcoords_for_geometry(EG)) - for j = 1 : size(xCoordinates,1) - xCoordinates[j,:] .+= shift[j] - xCoordinates[j,:] .*= scale[j] +function reference_domain(EG::Type{<:AbstractElementGeometry}, T::Type{<:Real} = Float64; scale = [1, 1, 1], shift = [0, 0, 0]) + xgrid = ExtendableGrid{T, Int32}() + xCoordinates = Array{T, 2}(refcoords_for_geometry(EG)) + for j in 1:size(xCoordinates, 1) + xCoordinates[j, :] .+= shift[j] + xCoordinates[j, :] .*= scale[j] end xgrid[Coordinates] = xCoordinates - xCellNodes = zeros(Int32,num_nodes(EG),1) + xCellNodes = zeros(Int32, num_nodes(EG), 1) xCellNodes[:] = 1:num_nodes(EG) xgrid[CellNodes] = xCellNodes - xgrid[CellGeometries] = VectorOfConstants{ElementGeometries,Int}(EG,1); - xgrid[CellRegions]=ones(Int32,1) - xgrid[BFaceRegions]=Array{Int32,1}(1:num_faces(EG)) - xgrid[BFaceNodes]=Array{Int32,2}(local_cellfacenodes(EG)) - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(facetype_of_cellface(EG, 1), num_faces(EG)) + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(EG, 1) + xgrid[CellRegions] = ones(Int32, 1) + xgrid[BFaceRegions] = Array{Int32, 1}(1:num_faces(EG)) + xgrid[BFaceNodes] = Array{Int32, 2}(local_cellfacenodes(EG)) + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(facetype_of_cellface(EG, 1), num_faces(EG)) if dim_element(EG) == 0 - xgrid[CoordinateSystem]=Cartesian0D + xgrid[CoordinateSystem] = Cartesian0D elseif dim_element(EG) == 1 - xgrid[CoordinateSystem]=Cartesian1D + xgrid[CoordinateSystem] = Cartesian1D elseif dim_element(EG) == 2 - xgrid[CoordinateSystem]=Cartesian2D + xgrid[CoordinateSystem] = Cartesian2D elseif dim_element(EG) == 3 - xgrid[CoordinateSystem]=Cartesian3D + xgrid[CoordinateSystem] = Cartesian3D end return xgrid end @@ -43,9 +43,9 @@ end Generates a single triangle with the given coordinates, that should be a 2 x 3 array with the coordinates of the three vertices, e.g. coords = [0.0 0.0; 1.0 0.0; 0.0 1.0]'. """ -function grid_triangle(coords::AbstractArray{T,2}) where {T} +function grid_triangle(coords::AbstractArray{T, 2}) where {T} xgrid = reference_domain(Triangle2D) - xgrid[Coordinates]=coords + xgrid[Coordinates] = coords return xgrid end @@ -54,7 +54,7 @@ end Unit cube as one cell with six boundary regions (bottom, front, right, back, left, top) """ -function grid_unitcube(EG::Type{<:Hexahedron3D}; scale = [1,1,1], shift = [0,0,0]) +function grid_unitcube(EG::Type{<:Hexahedron3D}; scale = [1, 1, 1], shift = [0, 0, 0]) return reference_domain(EG; scale = scale, shift = shift) end @@ -63,26 +63,26 @@ end Unit cube as six tets with six boundary regions (bottom, front, right, back, left, top) """ -function grid_unitcube(::Type{Tetrahedron3D}; scale = [1,1,1], shift = [0,0,0]) - xgrid=ExtendableGrid{Float64,Int32}() - xCoordinates=Array{Float64,2}([0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1]') - for j = 1 : size(xCoordinates,1) - xCoordinates[j,:] .+= shift[j] - xCoordinates[j,:] .*= scale[j] +function grid_unitcube(::Type{Tetrahedron3D}; scale = [1, 1, 1], shift = [0, 0, 0]) + xgrid = ExtendableGrid{Float64, Int32}() + xCoordinates = Array{Float64, 2}([0 0 0; 1 0 0; 1 1 0; 0 1 0; 0 0 1; 1 0 1; 1 1 1; 0 1 1]') + for j in 1:size(xCoordinates, 1) + xCoordinates[j, :] .+= shift[j] + xCoordinates[j, :] .*= scale[j] end xgrid[Coordinates] = xCoordinates - xCellNodes=Array{Int32,2}([1 2 3 7; 1 3 4 7; 1 5 6 7; 1 8 5 7; 1 6 2 7;1 4 8 7]') + xCellNodes = Array{Int32, 2}([1 2 3 7; 1 3 4 7; 1 5 6 7; 1 8 5 7; 1 6 2 7;1 4 8 7]') xgrid[CellNodes] = xCellNodes - xgrid[CellGeometries] = VectorOfConstants{ElementGeometries,Int}(Tetrahedron3D,6); + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(Tetrahedron3D, 6) ncells = num_sources(xCellNodes) - xgrid[CellRegions]=ones(Int32,ncells) - xgrid[BFaceRegions]=Array{Int32,1}([1,1,2,2,3,3,4,4,5,5,6,6]) - xBFaceNodes=Array{Int32,2}([1 3 2; 1 4 3; 1 2 6;1 6 5;2 3 7;2 7 6;3 4 7;7 4 8;8 4 1;1 5 8; 5 6 7; 5 7 8]') - xgrid[BFaceNodes]=xBFaceNodes + xgrid[CellRegions] = ones(Int32, ncells) + xgrid[BFaceRegions] = Array{Int32, 1}([1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6]) + xBFaceNodes = Array{Int32, 2}([1 3 2; 1 4 3; 1 2 6;1 6 5;2 3 7;2 7 6;3 4 7;7 4 8;8 4 1;1 5 8; 5 6 7; 5 7 8]') + xgrid[BFaceNodes] = xBFaceNodes nbfaces = num_sources(xBFaceNodes) - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(Triangle2D,nbfaces) - xgrid[CoordinateSystem]=Cartesian3D + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(Triangle2D, nbfaces) + xgrid[CoordinateSystem] = Cartesian3D return xgrid end @@ -90,7 +90,7 @@ end grid_unitsquare(EG::Type{<:Quadrilateral2D}; scale = [1,1], shift = [0,0]) Unit square as one cell with four boundary regions (bottom, right, top, left) """ -function grid_unitsquare(EG::Type{<:Quadrilateral2D}; scale = [1,1], shift = [0,0]) +function grid_unitsquare(EG::Type{<:Quadrilateral2D}; scale = [1, 1], shift = [0, 0]) return reference_domain(EG; scale = scale, shift = shift) end @@ -98,21 +98,21 @@ end grid_unitsquare(::Type{<:Triangle2D}; scale = [1,1], shift = [0,0]) Unit square as two triangles with four boundary regions (bottom, right, top, left) """ -function grid_unitsquare(::Type{<:Triangle2D}; scale = [1,1], shift = [0,0]) - xgrid=ExtendableGrid{Float64,Int32}() - xCoordinates=Array{Float64,2}([0 0; 1 0; 1 1; 0 1; 0.5 0.5]') - for j = 1 : size(xCoordinates,1) - xCoordinates[j,:] .+= shift[j] - xCoordinates[j,:] .*= scale[j] +function grid_unitsquare(::Type{<:Triangle2D}; scale = [1, 1], shift = [0, 0]) + xgrid = ExtendableGrid{Float64, Int32}() + xCoordinates = Array{Float64, 2}([0 0; 1 0; 1 1; 0 1; 0.5 0.5]') + for j in 1:size(xCoordinates, 1) + xCoordinates[j, :] .+= shift[j] + xCoordinates[j, :] .*= scale[j] end xgrid[Coordinates] = xCoordinates - xgrid[CellNodes]=Array{Int32,2}([1 2 5; 2 3 5; 3 4 5; 4 1 5]') - xgrid[CellGeometries]=VectorOfConstants{ElementGeometries,Int}(Triangle2D,4) - xgrid[CellRegions]=ones(Int32,4) - xgrid[BFaceRegions]=Array{Int32,1}([1,2,3,4]) - xgrid[BFaceNodes]=Array{Int32,2}([1 2; 2 3; 3 4; 4 1]') - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(Edge1D,4) - xgrid[CoordinateSystem]=Cartesian2D + xgrid[CellNodes] = Array{Int32, 2}([1 2 5; 2 3 5; 3 4 5; 4 1 5]') + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(Triangle2D, 4) + xgrid[CellRegions] = ones(Int32, 4) + xgrid[BFaceRegions] = Array{Int32, 1}([1, 2, 3, 4]) + xgrid[BFaceNodes] = Array{Int32, 2}([1 2; 2 3; 3 4; 4 1]') + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(Edge1D, 4) + xgrid[CoordinateSystem] = Cartesian2D return xgrid end @@ -121,21 +121,21 @@ end grid_lshape(::Type{<:Triangle2D}; scale = [1,1], shift = [0,0]) Lshape domain """ -function grid_lshape(::Type{<:Triangle2D}; scale = [1,1], shift = [0,0]) - xgrid=ExtendableGrid{Float64,Int32}() - xCoordinates=Array{Float64,2}([0 0; 1 0; 1 1; 0 1; -1 1; -1 0; -1 -1; 0 -1]') - for j = 1 : size(xCoordinates,1) - xCoordinates[j,:] .+= shift[j] - xCoordinates[j,:] .*= scale[j] +function grid_lshape(::Type{<:Triangle2D}; scale = [1, 1], shift = [0, 0]) + xgrid = ExtendableGrid{Float64, Int32}() + xCoordinates = Array{Float64, 2}([0 0; 1 0; 1 1; 0 1; -1 1; -1 0; -1 -1; 0 -1]') + for j in 1:size(xCoordinates, 1) + xCoordinates[j, :] .+= shift[j] + xCoordinates[j, :] .*= scale[j] end xgrid[Coordinates] = xCoordinates - xgrid[CellNodes]=Array{Int32,2}([4 2 3; 2 4 1; 6 4 5; 4 6 1; 6 8 1; 8 6 7]') - xgrid[CellGeometries]=VectorOfConstants{ElementGeometries,Int}(Triangle2D,6) - xgrid[CellRegions]=Array{Int32,1}([1,1,1,1,1,1]) - xgrid[BFaceRegions]=Array{Int32,1}([1,2,3,4,5,6,7,8]) - xgrid[BFaceNodes]=Array{Int32,2}([1 2; 2 3; 3 4; 4 5; 5 6; 6 7; 7 8; 8 1]') - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(Edge1D,8) - xgrid[CoordinateSystem]=Cartesian2D + xgrid[CellNodes] = Array{Int32, 2}([4 2 3; 2 4 1; 6 4 5; 4 6 1; 6 8 1; 8 6 7]') + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(Triangle2D, 6) + xgrid[CellRegions] = Array{Int32, 1}([1, 1, 1, 1, 1, 1]) + xgrid[BFaceRegions] = Array{Int32, 1}([1, 2, 3, 4, 5, 6, 7, 8]) + xgrid[BFaceNodes] = Array{Int32, 2}([1 2; 2 3; 3 4; 4 5; 5 6; 6 7; 7 8; 8 1]') + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(Edge1D, 8) + xgrid[CoordinateSystem] = Cartesian2D return xgrid end @@ -146,28 +146,28 @@ Unit suqare as mixed triangles and squares with four boundary regions (bottom, r """ function grid_unitsquare_mixedgeometries() - xgrid=ExtendableGrid{Float64,Int32}() - xgrid[Coordinates]=Array{Float64,2}([0 0; 4//10 0; 1 0; 0 6//10; 4//10 6//10; 1 6//10;0 1; 4//10 1; 1 1]') - xCellNodes=VariableTargetAdjacency(Int32) - xCellGeometries=ElementGeometries[Triangle2D, Triangle2D, Parallelogram2D, Parallelogram2D, Triangle2D, Triangle2D]; - - append!(xCellNodes,[1,5,4]) - append!(xCellNodes,[1,2,5]) - append!(xCellNodes,[2,3,6,5]) - append!(xCellNodes,[4,5,8,7]) - append!(xCellNodes,[5,6,9]) - append!(xCellNodes,[8,5,9]) + xgrid = ExtendableGrid{Float64, Int32}() + xgrid[Coordinates] = Array{Float64, 2}([0 0; 4 // 10 0; 1 0; 0 6 // 10; 4 // 10 6 // 10; 1 6 // 10;0 1; 4 // 10 1; 1 1]') + xCellNodes = VariableTargetAdjacency(Int32) + xCellGeometries = ElementGeometries[Triangle2D, Triangle2D, Parallelogram2D, Parallelogram2D, Triangle2D, Triangle2D] + + append!(xCellNodes, [1, 5, 4]) + append!(xCellNodes, [1, 2, 5]) + append!(xCellNodes, [2, 3, 6, 5]) + append!(xCellNodes, [4, 5, 8, 7]) + append!(xCellNodes, [5, 6, 9]) + append!(xCellNodes, [8, 5, 9]) xgrid[CellNodes] = xCellNodes xgrid[CellGeometries] = xCellGeometries ncells = num_sources(xCellNodes) - xgrid[CellRegions]=ones(Int32,ncells) - xgrid[BFaceRegions]=Array{Int32,1}([1,1,2,2,3,3,4,4]) - xBFaceNodes=Array{Int32,2}([1 2; 2 3; 3 6; 6 9; 9 8; 8 7; 7 4; 4 1]') - xgrid[BFaceNodes]=xBFaceNodes + xgrid[CellRegions] = ones(Int32, ncells) + xgrid[BFaceRegions] = Array{Int32, 1}([1, 1, 2, 2, 3, 3, 4, 4]) + xBFaceNodes = Array{Int32, 2}([1 2; 2 3; 3 6; 6 9; 9 8; 8 7; 7 4; 4 1]') + xgrid[BFaceNodes] = xBFaceNodes nbfaces = num_sources(xBFaceNodes) - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(Edge1D,nbfaces) - xgrid[CoordinateSystem]=Cartesian2D + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(Edge1D, nbfaces) + xgrid[CoordinateSystem] = Cartesian2D return xgrid end @@ -178,77 +178,77 @@ end Sector of ring or full ring (if `ang[begin]-ang[end]≈2π`) """ -function ringsector(rad,ang; eltype=Triangle2D) - Tv=Float32 - Ti=Int32 - - coord=ElasticArray{Tv,2}(undef,2,0) - cells=ElasticArray{Ti,2}(undef,3,0) - bfaces=ElasticArray{Ti,2}(undef,2,0) - bfaceregions=Vector{Ti}(undef,0) - cellregions=Vector{Ti}(undef,0) - - nrad=length(rad) - icell=0 - ibface=0 - - fullcircle= ang[end]-ang[1]≈2π - - narc=length(ang) - - for iarc=1:narc - ϕ=ang[iarc] - x=cos(ϕ) - y=sin(ϕ) - for irad=1:nrad - append!(coord,(rad[irad]*x,rad[irad]*y)) - icoord=size(coord,2) - if irad $(tuple[1])") end + return end # FaceNodes = nodes for each face (implicitly defines the enumerations of faces) -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNodes}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceNodes}) where {Tc, Ti} if haskey(xgrid, ParentGrid) && haskey(xgrid, ParentGridRelation) if xgrid[ParentGridRelation] <: SubGrid{ON_CELLS} @@ -178,17 +179,17 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo else singleEG = false end - SFaceNodes::Union{VariableTargetAdjacency{Ti}, Matrix{Ti}} = singleEG ? zeros(Ti,size(PFaceNodes,1), 0) : VariableTargetAdjacency(Ti) - + SFaceNodes::Union{VariableTargetAdjacency{Ti}, Matrix{Ti}} = singleEG ? zeros(Ti, size(PFaceNodes, 1), 0) : VariableTargetAdjacency(Ti) + pnode2snode = zeros(Ti, num_nodes(pgrid)) - pnode2snode[pnodes] .= 1 : length(pnodes) + pnode2snode[pnodes] .= 1:length(pnodes) pfaces = Ti[] - for face = 1 : nfaces + for face in 1:nfaces is_subface = true - for k = 1 : num_targets(PFaceNodes, face) + for k in 1:num_targets(PFaceNodes, face) if !(PFaceNodes[k, face] in pnodes) is_subface = false - break; + break end end if is_subface @@ -198,23 +199,23 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo pface2sface = zeros(Ti, nfaces) pface2sface[pfaces] = 1:length(pfaces) SFaceNodes = PFaceNodes[:, pfaces] - for face = 1:length(pfaces) - for k = 1 : num_targets(SFaceNodes, face) + for face in 1:length(pfaces) + for k in 1:num_targets(SFaceNodes, face) SFaceNodes[k, face] = pnode2snode[SFaceNodes[k, face]] end end xgrid[FaceNodes] = SFaceNodes if typeof(pgrid[FaceGeometries]) <: VectorOfConstants - xgrid[FaceGeometries] = VectorOfConstants{ElementGeometries,Int}(pgrid[FaceGeometries][1], length(pfaces)) + xgrid[FaceGeometries] = VectorOfConstants{ElementGeometries, Int}(pgrid[FaceGeometries][1], length(pfaces)) else xgrid[FaceGeometries] = pgrid[FaceGeometries][pfaces] end xgrid[FaceRegions] = pgrid[FaceRegions][pfaces] npcells = num_cells(pgrid) - for cell = 1 : npcells + for cell in 1:npcells is_subcell = true - for k = 1 : num_targets(PCellFaces, cell) + for k in 1:num_targets(PCellFaces, cell) PCellFaces[k, cell] = pface2sface[PCellFaces[k, cell]] end end @@ -223,17 +224,17 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo xgrid[CellFaces] = PCellFaces[:, pcells] xgrid[CellFaceSigns] = pgrid[CellFaceSigns][:, pcells] SFaceCells = pgrid[FaceCells][:, pfaces] - for face = 1 : 1:length(pfaces) - for k = 1 : num_targets(SFaceCells, face) + for face in 1:1:length(pfaces) + for k in 1:num_targets(SFaceCells, face) if SFaceCells[k, face] > 0 SFaceCells[k, face] = pcell2scell[SFaceCells[k, face]] else SFaceCells[k, face] = 0 end end - if SFaceCells[1,face] == 0 && SFaceCells[2,face] > 0 - SFaceCells[1,face] = SFaceCells[2,face] - SFaceCells[2,face] = 0 + if SFaceCells[1, face] == 0 && SFaceCells[2, face] > 0 + SFaceCells[1, face] = SFaceCells[2, face] + SFaceCells[2, face] = 0 end end xgrid[FaceCells] = SFaceCells @@ -254,27 +255,27 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo max_ncell4node::Ti = max_num_targets_per_source(xNodeCells) # instantiate new empty adjacency fields - xFaceCells = zeros(Ti,0) # cells are appended and at the end rewritten into 2,nfaces array + xFaceCells = zeros(Ti, 0) # cells are appended and at the end rewritten into 2,nfaces array # find unique face enumeration rules EG = unique(xCellGeometries) dim::Ti = dim_element(EG[1]) - face_rules = Array{Array{Ti,2},1}(undef,length(EG)) + face_rules = Array{Array{Ti, 2}, 1}(undef, length(EG)) maxfacenodes::Ti = 0 FEG = [] - for j = 1 : length(EG) + for j in 1:length(EG) face_rules[j] = local_cellfacenodes(EG[j]) - maxfacenodes = max(size(face_rules[j],2),maxfacenodes) - for k = 1 : num_faces(EG[j]) - append!(FEG,[facetype_of_cellface(EG[j],j)]) + maxfacenodes = max(size(face_rules[j], 2), maxfacenodes) + for k in 1:num_faces(EG[j]) + append!(FEG, [facetype_of_cellface(EG[j], j)]) end end unique!(FEG) # check if only one type of cell geometry is present in grid if length(EG) == 1 - singleEG = true + singleEG = true else singleEG = false end @@ -284,24 +285,24 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo else singleFEG = false end - + if singleFEG - xFaceNodes = zeros(Ti,0) + xFaceNodes = zeros(Ti, 0) else xFaceNodes = VariableTargetAdjacency(Ti) - xFaceGeometries::Array{ElementGeometries,1} = [] + xFaceGeometries::Array{ElementGeometries, 1} = [] end - xCellFaces::Union{VariableTargetAdjacency{Ti}, Matrix{Ti}} = singleEG*singleFEG ? zeros(Ti,num_faces(EG[1]),ncells) : VariableTargetAdjacency(Ti) - xCellFaceSigns::Union{VariableTargetAdjacency{Ti}, Matrix{Ti}} = singleEG*singleFEG ? zeros(Ti,num_faces(EG[1]),ncells) : VariableTargetAdjacency(Ti) + xCellFaces::Union{VariableTargetAdjacency{Ti}, Matrix{Ti}} = singleEG * singleFEG ? zeros(Ti, num_faces(EG[1]), ncells) : VariableTargetAdjacency(Ti) + xCellFaceSigns::Union{VariableTargetAdjacency{Ti}, Matrix{Ti}} = singleEG * singleFEG ? zeros(Ti, num_faces(EG[1]), ncells) : VariableTargetAdjacency(Ti) if singleEG == true && singleFEG == true else # pre-allocate xCellFaces cellEG = xCellGeometries[1] - for cell = 1 : ncells + for cell in 1:ncells cellEG = xCellGeometries[cell] - append!(xCellFaces,zeros(Ti,num_faces(cellEG))) - append!(xCellFaceSigns,zeros(Ti,num_faces(cellEG))) - end + append!(xCellFaces, zeros(Ti, num_faces(cellEG))) + append!(xCellFaceSigns, zeros(Ti, num_faces(cellEG))) + end end # temporary variables @@ -314,127 +315,127 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo cell2EG = EG[1] faceEG = FEG[1] faceEG2 = FEG[1] - face_rule::Array{Ti,2} = face_rules[1] - face_rule2::Array{Ti,2} = face_rules[1] + face_rule::Array{Ti, 2} = face_rules[1] + face_rule2::Array{Ti, 2} = face_rules[1] iEG::Ti = 1 nneighbours::Ti = 0 faces_per_cell::Ti = num_faces(cellEG) faces_per_cell2::Ti = num_faces(cellEG) nodes_per_cellface::Ti = num_nodes(faceEG) - current_item::Array{Ti,1} = zeros(Ti,maxfacenodes) # should be large enough to store largest nnodes per cellface - flag4item::Array{Bool,1} = zeros(Bool,nnodes) + current_item::Array{Ti, 1} = zeros(Ti, maxfacenodes) # should be large enough to store largest nnodes per cellface + flag4item::Array{Bool, 1} = zeros(Bool, nnodes) no_neighbours_found::Bool = true same_face::Bool = false - + # loop over cells - for cell = 1 : ncells + for cell in 1:ncells # find EG index for geometry if !singleEG cellEG = xCellGeometries[cell] faces_per_cell = num_faces(cellEG) - for j=1:length(EG) + for j in 1:length(EG) if cellEG == EG[j] iEG = j - break; + break end end face_rule = face_rules[iEG] end # loop over cell faces - for k = 1 : faces_per_cell + for k in 1:faces_per_cell # check if face is already known to cell - if xCellFaces[k,cell] > 0 - continue; - end + if xCellFaces[k, cell] > 0 + continue + end # get face geometry if !singleFEG faceEG = facetype_of_cellface(cellEG, k) nodes_per_cellface = num_nodes(faceEG) end - + # flag face nodes and commons4cells - for j = nodes_per_cellface:-1:1 - node = xCellNodes[face_rule[j,k],cell] + for j in nodes_per_cellface:-1:1 + node = xCellNodes[face_rule[j, k], cell] current_item[j] = node - flag4item[node] = true; + flag4item[node] = true end # get neighbours for first node - nneighbours = num_targets(xNodeCells,node) + nneighbours = num_targets(xNodeCells, node) # loop over neighbours no_neighbours_found = true - for n = 1 : nneighbours - cell2 = xNodeCells[n,node] + for n in 1:nneighbours + cell2 = xNodeCells[n, node] # skip if cell2 is the same as cell - if (cell == cell2) - continue; + if (cell == cell2) + continue end # find face enumeration rule if !singleEG cell2EG = xCellGeometries[cell2] faces_per_cell2 = num_faces(cell2EG) - for j=1:length(EG) + for j in 1:length(EG) if cell2EG == EG[j] iEG = j - break; + break end end face_rule2 = face_rules[iEG] end # loop over faces face2 of adjacent cell2 - for f2 = 1 : faces_per_cell2 + for f2 in 1:faces_per_cell2 # check if face f2 is already known to cell2 - if xCellFaces[f2,cell2] != 0 - continue; - end + if xCellFaces[f2, cell2] != 0 + continue + end # check if face f2 has same geometry if !singleFEG faceEG2 = facetype_of_cellface(cell2EG, f2) if faceEG != faceEG2 - continue; + continue end end # otherwise compare nodes of face and face2 same_face = true - for j = 1 : nodes_per_cellface - if flag4item[xCellNodes[face_rule2[j,f2],cell2]] == false + for j in 1:nodes_per_cellface + if flag4item[xCellNodes[face_rule2[j, f2], cell2]] == false same_face = false - break; - end + break + end end - + # if all nodes are the same, register face if (same_face) no_neighbours_found = false face += 1 - push!(xFaceCells,cell) - push!(xFaceCells,cell2) + push!(xFaceCells, cell) + push!(xFaceCells, cell2) if singleEG == false - xCellFaces.colentries[xCellFaces.colstart[cell]+k-1] = face - xCellFaces.colentries[xCellFaces.colstart[cell2]+f2-1] = face - xCellFaceSigns.colentries[xCellFaceSigns.colstart[cell]+k-1] = 1 - xCellFaceSigns.colentries[xCellFaceSigns.colstart[cell2]+f2-1] = -1 + xCellFaces.colentries[xCellFaces.colstart[cell] + k - 1] = face + xCellFaces.colentries[xCellFaces.colstart[cell2] + f2 - 1] = face + xCellFaceSigns.colentries[xCellFaceSigns.colstart[cell] + k - 1] = 1 + xCellFaceSigns.colentries[xCellFaceSigns.colstart[cell2] + f2 - 1] = -1 if singleFEG == false - push!(xFaceGeometries,faceEG) + push!(xFaceGeometries, faceEG) end else - xCellFaces[k,cell] = face - xCellFaces[f2,cell2] = face - xCellFaceSigns[k,cell] = 1 - xCellFaceSigns[f2,cell2] = -1 + xCellFaces[k, cell] = face + xCellFaces[f2, cell2] = face + xCellFaceSigns[k, cell] = 1 + xCellFaceSigns[f2, cell2] = -1 end - append!(xFaceNodes,view(current_item,1:nodes_per_cellface)) - break; + append!(xFaceNodes, view(current_item, 1:nodes_per_cellface)) + break end end end @@ -442,81 +443,80 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNo # if no common neighbour cell is found, register face (boundary faces) if no_neighbours_found == true face += 1 - push!(xFaceCells,cell) - push!(xFaceCells,0) + push!(xFaceCells, cell) + push!(xFaceCells, 0) if singleEG == false - xCellFaces.colentries[xCellFaces.colstart[cell]+k-1] = face - xCellFaceSigns.colentries[xCellFaceSigns.colstart[cell]+k-1] = 1 + xCellFaces.colentries[xCellFaces.colstart[cell] + k - 1] = face + xCellFaceSigns.colentries[xCellFaceSigns.colstart[cell] + k - 1] = 1 if singleFEG == false - push!(xFaceGeometries,faceEG) + push!(xFaceGeometries, faceEG) end else - xCellFaces[k,cell] = face - xCellFaceSigns[k,cell] = 1 + xCellFaces[k, cell] = face + xCellFaceSigns[k, cell] = 1 end - append!(xFaceNodes,view(current_item,1:nodes_per_cellface)) + append!(xFaceNodes, view(current_item, 1:nodes_per_cellface)) end # reset flag4item - for j = 1:nodes_per_cellface - flag4item[current_item[j]] = false + for j in 1:nodes_per_cellface + flag4item[current_item[j]] = false end - end + end end if singleFEG - xFaceNodes = reshape(xFaceNodes,(nodes_per_cellface,Ti(length(xFaceNodes)/nodes_per_cellface))) - xgrid[FaceGeometries] = VectorOfConstants{ElementGeometries,Int}(facetype_of_cellface(EG[1], 1), face) + xFaceNodes = reshape(xFaceNodes, (nodes_per_cellface, Ti(length(xFaceNodes) / nodes_per_cellface))) + xgrid[FaceGeometries] = VectorOfConstants{ElementGeometries, Int}(facetype_of_cellface(EG[1], 1), face) else xgrid[FaceGeometries] = xFaceGeometries end xgrid[CellFaces] = xCellFaces xgrid[CellFaceSigns] = xCellFaceSigns - xgrid[FaceCells] = reshape(xFaceCells,(2,face)) - xFaceNodes + xgrid[FaceCells] = reshape(xFaceCells, (2, face)) + return xFaceNodes end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{NodePatchGroups}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{NodePatchGroups}) where {Tc, Ti} xCellNodes::Adjacency = xgrid[CellNodes] xNodeCells = atranspose(xCellNodes) - nnodes = size(xgrid[Coordinates],2) + nnodes = size(xgrid[Coordinates], 2) ncells = num_sources(xCellNodes) ncells4node::Ti = 0 - group4node = zeros(Ti,nnodes) + group4node = zeros(Ti, nnodes) cgroup::Ti = 0 - cell_in_group = zeros(Bool,ncells) + cell_in_group = zeros(Bool, ncells) take_into = false cgroup = 0 while minimum(group4node) == 0 cell_in_group .= false cgroup += 1 - for node = 1 : nnodes + for node in 1:nnodes if group4node[node] == 0 - ncells4node = num_targets(xNodeCells,node) + ncells4node = num_targets(xNodeCells, node) take_into = true - for c = 1 : ncells4node - if cell_in_group[xNodeCells[c,node]] == true + for c in 1:ncells4node + if cell_in_group[xNodeCells[c, node]] == true take_into = false break end end if take_into group4node[node] = cgroup - for c = 1 : ncells4node - cell_in_group[xNodeCells[c,node]] = true + for c in 1:ncells4node + cell_in_group[xNodeCells[c, node]] = true end end end end end - group4node + return group4node end - # FaceNodes = nodes for each face (implicitly defines the enumerations of faces) -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNodes}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeNodes}) where {Tc, Ti} xCellNodes = xgrid[CellNodes] ncells::Ti = num_sources(xCellNodes) @@ -528,7 +528,7 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo if dim == 1 xgrid[EdgeNodes] = xgrid[CellNodes] xgrid[EdgeGeometries] = xgrid[CellGeometries] - xgrid[CellEdges] = reshape(collect(Ti,1:size(xCellNodes,2)), (1,size(xCellNodes,2))) + xgrid[CellEdges] = reshape(collect(Ti, 1:size(xCellNodes, 2)), (1, size(xCellNodes, 2))) xgrid[EdgeCells] = xgrid[CellEdges] return xgrid[EdgeNodes] end @@ -540,10 +540,10 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo xgrid[EdgeNodes] = xgrid[FaceNodes] xgrid[EdgeGeometries] = xgrid[FaceGeometries] return xgrid[EdgeNodes] -# return prepare_edges!(xgrid) # keep compatible to VoronoiFVM + # return prepare_edges!(xgrid) # keep compatible to VoronoiFVM end - + # transpose CellNodes to get NodeCells xNodeCells = atranspose(xCellNodes) max_ncell4node::Ti = max_num_targets_per_source(xNodeCells) @@ -552,37 +552,37 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo xEdgeCells = VariableTargetAdjacency(Ti) # find unique edge enumeration rules for each cell geometry - EG::Array{ElementGeometries,1} = unique(xCellGeometries) - edge_rules::Array{Array{Ti,2},1} = Array{Array{Ti,2},1}(undef,length(EG)) + EG::Array{ElementGeometries, 1} = unique(xCellGeometries) + edge_rules::Array{Array{Ti, 2}, 1} = Array{Array{Ti, 2}, 1}(undef, length(EG)) maxedgenodes::Ti = 0 - for j = 1 : length(EG) + for j in 1:length(EG) edge_rules[j] = local_celledgenodes(EG[j]) - maxedgenodes = max(size(edge_rules[j],1),maxedgenodes) + maxedgenodes = max(size(edge_rules[j], 1), maxedgenodes) end # pre-allocate xCellEdges and xCellEdgeSigns if length(EG) == 1 singleEG = true - xCellEdges = zeros(Ti,num_edges(EG[1]),ncells) - xCellEdgeSigns = zeros(Ti,num_edges(EG[1]),ncells) + xCellEdges = zeros(Ti, num_edges(EG[1]), ncells) + xCellEdgeSigns = zeros(Ti, num_edges(EG[1]), ncells) else singleEG = false xCellEdges = VariableTargetAdjacency(Ti) cellEG = xCellGeometries[1] - for cell = 1 : ncells + for cell in 1:ncells cellEG = xCellGeometries[cell] - append!(xCellEdges,zeros(Ti,num_edges(cellEG))) - end + append!(xCellEdges, zeros(Ti, num_edges(cellEG))) + end xCellEdgeSigns = deepcopy(xCellEdges) end # init xEdgeNodes - xEdgeNodes::Array{Ti,1} = zeros(Ti,0) + xEdgeNodes::Array{Ti, 1} = zeros(Ti, 0) - edge_rule::Array{Ti,2} = edge_rules[1] - edge_rule2::Array{Ti,2} = edge_rules[1] - current_item::Array{Ti,1} = zeros(Ti,2) - flag4item::Array{Bool,1} = zeros(Bool,nnodes) + edge_rule::Array{Ti, 2} = edge_rules[1] + edge_rule2::Array{Ti, 2} = edge_rules[1] + current_item::Array{Ti, 1} = zeros(Ti, 2) + flag4item::Array{Bool, 1} = zeros(Bool, nnodes) cellEG = EG[1] node::Ti = 0 cell2::Ti = 0 @@ -590,58 +590,58 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo edges_per_cell::Ti = num_edges(EG[1]) edges_per_cell2::Ti = num_edges(EG[1]) common_nodes::Ti = 0 - cells_with_common_edge::Array{Ti,1} = zeros(Ti,max_ncell4node) - pos_in_cells_with_common_edge::Array{Ti,1} = zeros(Ti,max_ncell4node) - sign_in_cells_with_common_edge::Array{Ti,1} = zeros(Ti,max_ncell4node) + cells_with_common_edge::Array{Ti, 1} = zeros(Ti, max_ncell4node) + pos_in_cells_with_common_edge::Array{Ti, 1} = zeros(Ti, max_ncell4node) + sign_in_cells_with_common_edge::Array{Ti, 1} = zeros(Ti, max_ncell4node) ncells_with_common_edge::Ti = 0 edge::Ti = 0 iEG::Ti = 0 # loop over cells - for cell = 1 : ncells + for cell in 1:ncells # find EG index for geometry if singleEG == false cellEG = xCellGeometries[cell] edges_per_cell = num_edges(cellEG) - for j=1:length(EG) + for j in 1:length(EG) if cellEG == EG[j] iEG = j - break; + break end end edge_rule = edge_rules[iEG] end # loop over cell edges - for k = 1 : edges_per_cell + for k in 1:edges_per_cell # check if edge is already known to cell - if xCellEdges[k,cell] > 0 - continue; - end + if xCellEdges[k, cell] > 0 + continue + end ncells_with_common_edge = 1 cells_with_common_edge[1] = cell pos_in_cells_with_common_edge[1] = k sign_in_cells_with_common_edge[1] = 1 # flag edge nodes and commons4cells - for j = 2 : - 1 : 1 - node = xCellNodes[edge_rule[j,k],cell] + for j in 2:- 1:1 + node = xCellNodes[edge_rule[j, k], cell] current_item[j] = node - flag4item[node] = true; + flag4item[node] = true end # get first node and its neighbours - nneighbours = num_targets(xNodeCells,node) + nneighbours = num_targets(xNodeCells, node) # loop over neighbours - for n = 1 : nneighbours - cell2 = xNodeCells[n,node] + for n in 1:nneighbours + cell2 = xNodeCells[n, node] # skip if cell2 is the same as cell - if (cell == cell2) - continue; + if (cell == cell2) + continue end # loop over edges of cell2 @@ -650,24 +650,24 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo edges_per_cell2 = num_edges(cellEG) # find edge enumeration rule - for j=1:length(EG) + for j in 1:length(EG) if cellEG == EG[j] iEG = j - break; + break end end edge_rule2 = edge_rules[iEG] end - for f2 = 1 : edges_per_cell2 + for f2 in 1:edges_per_cell2 # compare nodes of edge and edge2 common_nodes = 0 - for j = 1 : 2 - if flag4item[xCellNodes[edge_rule2[j,f2],cell2]] + for j in 1:2 + if flag4item[xCellNodes[edge_rule2[j, f2], cell2]] common_nodes += 1 else - continue; - end + continue + end end # if all nodes are the same, register edge @@ -675,7 +675,7 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo ncells_with_common_edge += 1 cells_with_common_edge[ncells_with_common_edge] = cell2 pos_in_cells_with_common_edge[ncells_with_common_edge] = f2 - if xCellNodes[edge_rule2[1,f2],cell2] == current_item[1] + if xCellNodes[edge_rule2[1, f2], cell2] == current_item[1] sign_in_cells_with_common_edge[ncells_with_common_edge] = 1 else sign_in_cells_with_common_edge[ncells_with_common_edge] = -1 @@ -686,28 +686,28 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeNo # register edge edge += 1 - for c = 1 : ncells_with_common_edge - xCellEdges[pos_in_cells_with_common_edge[c],cells_with_common_edge[c]] = edge - xCellEdgeSigns[pos_in_cells_with_common_edge[c],cells_with_common_edge[c]] = sign_in_cells_with_common_edge[c] + for c in 1:ncells_with_common_edge + xCellEdges[pos_in_cells_with_common_edge[c], cells_with_common_edge[c]] = edge + xCellEdgeSigns[pos_in_cells_with_common_edge[c], cells_with_common_edge[c]] = sign_in_cells_with_common_edge[c] end - append!(xEdgeCells,view(cells_with_common_edge,1:ncells_with_common_edge)) - append!(xEdgeNodes,current_item) + append!(xEdgeCells, view(cells_with_common_edge, 1:ncells_with_common_edge)) + append!(xEdgeNodes, current_item) #reset flag4item - for j = 1 : 2 - flag4item[current_item[j]] = false + for j in 1:2 + flag4item[current_item[j]] = false end - end + end end xgrid[CellEdges] = xCellEdges xgrid[EdgeCells] = xEdgeCells xgrid[CellEdgeSigns] = xCellEdgeSigns - xgrid[EdgeGeometries] = VectorOfConstants{ElementGeometries,Int}(Edge1D,edge) - reshape(xEdgeNodes,(2,edge)) + xgrid[EdgeGeometries] = VectorOfConstants{ElementGeometries, Int}(Edge1D, edge) + return reshape(xEdgeNodes, (2, edge)) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellFaceOrientations}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellFaceOrientations}) where {Tc, Ti} xCellFaceSigns = xgrid[CellFaceSigns] xCellGeometries = xgrid[CellGeometries] xCellFaces = xgrid[CellFaces] @@ -716,18 +716,18 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellFa ncells = num_sources(xCellNodes) EG = unique(xCellGeometries) - face_rules = Array{Array{Ti,2},1}(undef,length(EG)) + face_rules = Array{Array{Ti, 2}, 1}(undef, length(EG)) maxfacenodes = 0 - for j = 1 : length(EG) + for j in 1:length(EG) face_rules[j] = local_cellfacenodes(EG[j]) - maxfacenodes = max(size(face_rules[j],2),maxfacenodes) + maxfacenodes = max(size(face_rules[j], 2), maxfacenodes) end singleEG = false if typeof(xCellFaceSigns) <: VariableTargetAdjacency xCellFaceOrientations = deepcopy(xCellFaceSigns) else singleEG = true - xCellFaceOrientations = zeros(Ti, size(xCellFaceSigns,1), size(xCellFaceSigns,2)) + xCellFaceOrientations = zeros(Ti, size(xCellFaceSigns, 1), size(xCellFaceSigns, 2)) end cellEG = EG[1] @@ -735,50 +735,50 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellFa ncellfaces::Ti = 0 nfacenodes::Ti = 0 face::Ti = 0 - facenodes = zeros(Ti,maxfacenodes) + facenodes = zeros(Ti, maxfacenodes) found_configuration::Bool = false n::Ti = 0 iEG::Ti = 0 - for cell = 1 : ncells + for cell in 1:ncells cellEG = xCellGeometries[cell] # find EG index for geometry - for j=1:length(EG) + for j in 1:length(EG) if cellEG == EG[j] iEG = j - break; + break end end face_rule = face_rules[iEG] # determines local enumeration of faces # determine orientation - ncellfaces = num_targets(xCellFaces,cell) - for j = 1 : ncellfaces - face = xCellFaces[j,cell] - nfacenodes = num_targets(xFaceNodes,face) - if xCellFaceSigns[j,cell] == 1 + ncellfaces = num_targets(xCellFaces, cell) + for j in 1:ncellfaces + face = xCellFaces[j, cell] + nfacenodes = num_targets(xFaceNodes, face) + if xCellFaceSigns[j, cell] == 1 if singleEG == false - xCellFaceOrientations.colentries[xCellFaceOrientations.colstart[cell]+j-1] = 1 + xCellFaceOrientations.colentries[xCellFaceOrientations.colstart[cell] + j - 1] = 1 else xCellFaceOrientations[j, cell] = 1 end else - for k = 1 : nfacenodes - facenodes[nfacenodes + 1 - k] = xFaceNodes[k,face] + for k in 1:nfacenodes + facenodes[nfacenodes + 1 - k] = xFaceNodes[k, face] end found_configuration = false n = 0 while !found_configuration n += 1 - if facenodes[n] == xCellNodes[face_rule[1,j],cell] + if facenodes[n] == xCellNodes[face_rule[1, j], cell] found_configuration = true end end - n = mod(n-1,3) + 1 + n = mod(n - 1, 3) + 1 if singleEG == false - xCellFaceOrientations.colentries[xCellFaceOrientations.colstart[cell]+j-1] = 1+n + xCellFaceOrientations.colentries[xCellFaceOrientations.colstart[cell] + j - 1] = 1 + n else - xCellFaceOrientations[j, cell] = 1+n + xCellFaceOrientations[j, cell] = 1 + n end end end @@ -788,7 +788,7 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellFa end # FaceEdges = Edges for each face (in 3D) -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEdges}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceEdges}) where {Tc, Ti} xFaceNodes = xgrid[FaceNodes] xFaceCells = xgrid[FaceCells] xEdgeNodes = xgrid[EdgeNodes] @@ -796,33 +796,33 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEd xFaceEdges = VariableTargetAdjacency(Ti) xFaceGeometries = xgrid[FaceGeometries] nfaces::Ti = num_sources(xFaceNodes) - nnodes::Ti = size(xgrid[Coordinates],2) + nnodes::Ti = size(xgrid[Coordinates], 2) # find unique edge enumeration rules EG = unique(xFaceGeometries) - edge_rules = Array{Array{Ti,2},1}(undef,length(EG)) + edge_rules = Array{Array{Ti, 2}, 1}(undef, length(EG)) maxedgenodes = 0 - for j = 1 : length(EG) + for j in 1:length(EG) edge_rules[j] = local_cellfacenodes(EG[j]) # face edges are faces of the face - maxedgenodes = max(size(edge_rules[j],2),maxedgenodes) + maxedgenodes = max(size(edge_rules[j], 2), maxedgenodes) end - edge_rule::Array{Ti,2} = edge_rules[1] + edge_rule::Array{Ti, 2} = edge_rules[1] if length(EG) == 1 singleEG = true - xFaceEdges = zeros(Ti,num_edges(EG[1]),nfaces) - xFaceEdgeSigns = zeros(Ti,num_edges(EG[1]),nfaces) + xFaceEdges = zeros(Ti, num_edges(EG[1]), nfaces) + xFaceEdgeSigns = zeros(Ti, num_edges(EG[1]), nfaces) else singleEG = false faceEG = xFaceGeometries[1] xFaceEdgeSigns = VariableTargetAdjacency(Ti) - for face = 1 : nfaces + for face in 1:nfaces faceEG = xFaceGeometries[face] - append!(xFaceEdges,zeros(Ti,num_edges(faceEG))) - append!(xFaceEdgeSigns,zeros(Ti,num_edges(faceEG))) - end + append!(xFaceEdges, zeros(Ti, num_edges(faceEG))) + append!(xFaceEdgeSigns, zeros(Ti, num_edges(faceEG))) + end end - nfacenodes::Ti = num_targets(xFaceNodes,1) + nfacenodes::Ti = num_targets(xFaceNodes, 1) ncelledges::Ti = 0 nedgenodes::Ti = 0 nfaceedges::Ti = num_edges(EG[1]) @@ -832,46 +832,46 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEd faceedge::Ti = 0 edge_is_in_face::Bool = false found_pos::Bool = false - flag4face = zeros(Bool,nnodes) - flag4edge = zeros(Bool,nnodes) + flag4face = zeros(Bool, nnodes) + flag4edge = zeros(Bool, nnodes) faceEG = EG[1] iEG::Ti = 1 pos::Ti = 0 - for face = 1 : nfaces + for face in 1:nfaces if singleEG == false faceEG = xFaceGeometries[face] nfaceedges = num_edges(faceEG) # find EG index for geometry - for j=1:length(EG) + for j in 1:length(EG) if faceEG == EG[j] iEG = j - break; + break end end edge_rule = edge_rules[iEG] # determines local enumeration of face edges - nfacenodes = num_targets(xFaceNodes,face) + nfacenodes = num_targets(xFaceNodes, face) end # mark nodes of face - for j = 1 : nfacenodes + for j in 1:nfacenodes node = xFaceNodes[j, face] - flag4face[node] = true; + flag4face[node] = true end # find edges in first adjacent cell - cell = xFaceCells[1,face] - ncelledges = num_targets(xCellEdges,cell) + cell = xFaceCells[1, face] + ncelledges = num_targets(xCellEdges, cell) faceedge = 0 - for cedge = 1 : ncelledges - edge = xCellEdges[cedge,cell] - nedgenodes = num_targets(xEdgeNodes,edge) + for cedge in 1:ncelledges + edge = xCellEdges[cedge, cell] + nedgenodes = num_targets(xEdgeNodes, edge) edge_is_in_face = true - for j = 1 : nedgenodes + for j in 1:nedgenodes if flag4face[xEdgeNodes[j, edge]] == false edge_is_in_face = false - break; + break end end @@ -882,9 +882,9 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEd # to ensure correct dof handling (e.g. for P2-FEM on boundary) # mark nodes of edge - for j = 1 : nedgenodes + for j in 1:nedgenodes node = xEdgeNodes[j, edge] - flag4edge[node] = true; + flag4edge[node] = true end pos = 0 @@ -892,10 +892,10 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEd while found_pos == false pos += 1 found_pos = true - for k = 1 : nedgenodes - if flag4edge[xFaceNodes[edge_rule[k,pos], face]] == false + for k in 1:nedgenodes + if flag4edge[xFaceNodes[edge_rule[k, pos], face]] == false found_pos = false - break; + break end end end @@ -903,126 +903,126 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEd xFaceEdges[pos, face] = edge xFaceEdgeSigns[pos, face] = xEdgeNodes[1, edge] == xFaceNodes[edge_rule[1, pos], face] ? 1 : -1 else - xFaceEdges.colentries[xFaceEdges.colstart[face]+pos-1] = edge - xFaceEdgeSigns.colentries[xFaceEdgeSigns.colstart[face]+pos-1] = xEdgeNodes[1, edge] == xFaceNodes[edge_rule[1, pos], face] ? 1 : -1 + xFaceEdges.colentries[xFaceEdges.colstart[face] + pos - 1] = edge + xFaceEdgeSigns.colentries[xFaceEdgeSigns.colstart[face] + pos - 1] = xEdgeNodes[1, edge] == xFaceNodes[edge_rule[1, pos], face] ? 1 : -1 end # reset flag4edge - for j = 1 : nedgenodes + for j in 1:nedgenodes node = xEdgeNodes[j, edge] - flag4edge[node] = false; + flag4edge[node] = false end faceedge += 1 if faceedge == nfaceedges - break; + break end end end #reset flag4face - for j = 1 : nfacenodes + for j in 1:nfacenodes node = xFaceNodes[j, face] - flag4face[node] = false; + flag4face[node] = false end end - + xgrid[FaceEdgeSigns] = xFaceEdgeSigns - xFaceEdges + return xFaceEdges end - # CellFaces = faces for each cell -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellFaces}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellFaces}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, FaceNodes) - xgrid[CellFaces] + return xgrid[CellFaces] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceGeometries}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceGeometries}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, FaceNodes) - xgrid[FaceGeometries] + return xgrid[FaceGeometries] end # CellEdges = edges for each cell -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellEdges}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellEdges}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, EdgeNodes) - xgrid[CellEdges] + return xgrid[CellEdges] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeCells}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeCells}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, EdgeNodes) - xgrid[EdgeCells] + return xgrid[EdgeCells] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellEdgeSigns}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellEdgeSigns}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, EdgeNodes) - xgrid[CellEdgeSigns] + return xgrid[CellEdgeSigns] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceEdgeSigns}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceEdgeSigns}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, FaceEdges) - xgrid[FaceEdgeSigns] + return xgrid[FaceEdgeSigns] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellFaceSigns}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellFaceSigns}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, FaceNodes) - xgrid[CellFaceSigns] + return xgrid[CellFaceSigns] end -function collectVolumes4Geometries(xgrid::ExtendableGrid{Tc,Ti}, ItemType) where {Tc,Ti} +function collectVolumes4Geometries(xgrid::ExtendableGrid{Tc, Ti}, ItemType) where {Tc, Ti} # get links to other stuff xCoordinates = xgrid[Coordinates] xCoordinateSystem = xgrid[CoordinateSystem] - xItemNodes = xgrid[GridComponent4TypeProperty(ItemType,PROPERTY_NODES) ] - xGeometries = xgrid[GridComponent4TypeProperty(ItemType,PROPERTY_GEOMETRY) ] + xItemNodes = xgrid[GridComponent4TypeProperty(ItemType, PROPERTY_NODES)] + xGeometries = xgrid[GridComponent4TypeProperty(ItemType, PROPERTY_GEOMETRY)] nitems = num_sources(xItemNodes) # keep existing volume array if possible if haskey(xgrid.components, GridComponent4TypeProperty(ItemType, PROPERTY_VOLUME)) - xVolumes = xgrid[GridComponent4TypeProperty(ItemType,PROPERTY_VOLUME)] + xVolumes = xgrid[GridComponent4TypeProperty(ItemType, PROPERTY_VOLUME)] else - xVolumes = zeros(Tc,nitems) + xVolumes = zeros(Tc, nitems) end # Introduce a function barrier: this will be compiled for each different type # of coordinate systems. - function barrier!(xVolumes,xCoordinateSystem) - for item = 1 : nitems + function barrier!(xVolumes, xCoordinateSystem) + for item in 1:nitems xVolumes[item] = volume(xCoordinates, xItemNodes, item, xGeometries[item], xCoordinateSystem) end + return end - nalloc=@allocated barrier!(xVolumes,xCoordinateSystem) - nalloc >0 && @warn " $nalloc allocations during $ItemType volume calculation" - xVolumes + nalloc = @allocated barrier!(xVolumes, xCoordinateSystem) + nalloc > 0 && @warn " $nalloc allocations during $ItemType volume calculation" + return xVolumes end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellVolumes}) where {Tc,Ti} - collectVolumes4Geometries(xgrid, ITEMTYPE_CELL) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellVolumes}) where {Tc, Ti} + return collectVolumes4Geometries(xgrid, ITEMTYPE_CELL) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceVolumes}) where {Tc,Ti} - collectVolumes4Geometries(xgrid, ITEMTYPE_FACE) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceVolumes}) where {Tc, Ti} + return collectVolumes4Geometries(xgrid, ITEMTYPE_FACE) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceVolumes}) where {Tc,Ti} - collectVolumes4Geometries(xgrid, ITEMTYPE_BFACE) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BFaceVolumes}) where {Tc, Ti} + return collectVolumes4Geometries(xgrid, ITEMTYPE_BFACE) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeVolumes}) where {Tc,Ti} - collectVolumes4Geometries(xgrid, ITEMTYPE_EDGE) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeVolumes}) where {Tc, Ti} + return collectVolumes4Geometries(xgrid, ITEMTYPE_EDGE) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeVolumes}) where {Tc,Ti} - collectVolumes4Geometries(xgrid, ITEMTYPE_BEDGE) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BEdgeVolumes}) where {Tc, Ti} + return collectVolumes4Geometries(xgrid, ITEMTYPE_BEDGE) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceFaces}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BFaceFaces}) where {Tc, Ti} # get links to other stuff xCoordinates = xgrid[Coordinates] xFaceNodes = xgrid[FaceNodes] @@ -1031,7 +1031,7 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceF nbfaces::Ti = num_sources(xBFaceNodes) # init BFaceFaces - xBFaceFaces::Array{Ti,1} = zeros(Ti,nbfaces) + xBFaceFaces::Array{Ti, 1} = zeros(Ti, nbfaces) #xBFaceGeometries = xgrid[BFaceGeometries] #if typeof(xBFaceGeometries) == VectorOfConstants{ElementGeometries} # EG = xBFaceGeometries[1] @@ -1044,34 +1044,34 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceF # transpose FaceNodes to get NodeFaces xNodeFaces = atranspose(xFaceNodes) - flag4item::Array{Bool,1} = zeros(Bool,nnodes) + flag4item::Array{Bool, 1} = zeros(Bool, nnodes) nodes_per_bface::Ti = 0 nodes_per_face::Ti = 0 common_nodes::Ti = 0 node::Ti = 0 nneighbours::Ti = 0 - for bface = 1 : nbfaces - nodes_per_bface = num_targets(xBFaceNodes,bface) - for j = 1 : nodes_per_bface - node = xBFaceNodes[j,bface] + for bface in 1:nbfaces + nodes_per_bface = num_targets(xBFaceNodes, bface) + for j in 1:nodes_per_bface + node = xBFaceNodes[j, bface] flag4item[node] = true - end + end # get faces for last node of bface - nneighbours = num_targets(xNodeFaces,node) + nneighbours = num_targets(xNodeFaces, node) # loop over faces and find the one that matches the bface - for n = 1 : nneighbours - face = xNodeFaces[n,node] - nodes_per_face = num_targets(xFaceNodes,face) + for n in 1:nneighbours + face = xNodeFaces[n, node] + nodes_per_face = num_targets(xFaceNodes, face) common_nodes = 0 - for k = 1 : nodes_per_face - if flag4item[xFaceNodes[k,face]] == true + for k in 1:nodes_per_face + if flag4item[xFaceNodes[k, face]] == true common_nodes += 1 else - break + break end - end + end if common_nodes == nodes_per_face xBFaceFaces[bface] = face break @@ -1079,44 +1079,43 @@ function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceF end if xBFaceFaces[bface] == 0 - println("WARNING(BFaceFaces): found no matching face for bface $bface with nodes $(xBFaceNodes[:,bface])") + println("WARNING(BFaceFaces): found no matching face for bface $bface with nodes $(xBFaceNodes[:, bface])") end - for j = 1 : nodes_per_bface - flag4item[xBFaceNodes[j,bface]] = false - end + for j in 1:nodes_per_bface + flag4item[xBFaceNodes[j, bface]] = false + end end # enforce that BFaceNodes have same ordering as FaceNodes newBFaceNodes = deepcopy(xBFaceNodes) - for bface = 1 : nbfaces - nodes_per_face = num_targets(xBFaceNodes,bface) - for j = 1 : nodes_per_bface + for bface in 1:nbfaces + nodes_per_face = num_targets(xBFaceNodes, bface) + for j in 1:nodes_per_bface if typeof(xBFaceNodes) <: VariableTargetAdjacency - newBFaceNodes.colentries[newBFaceNodes.colstart[bface]+j-1] = xFaceNodes[j,xBFaceFaces[bface]] + newBFaceNodes.colentries[newBFaceNodes.colstart[bface] + j - 1] = xFaceNodes[j, xBFaceFaces[bface]] else - newBFaceNodes[j,bface] = xFaceNodes[j,xBFaceFaces[bface]] + newBFaceNodes[j, bface] = xFaceNodes[j, xBFaceFaces[bface]] end end end xgrid[BFaceNodes] = newBFaceNodes - # xgrid[BFaceGeometries] = xBFaceGeometries - xBFaceFaces + # xgrid[BFaceGeometries] = xBFaceGeometries + return xBFaceFaces end +function instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BEdgeNodes}) where {Tc, Ti} -function instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeNodes}) where {Tc,Ti} - - dim = size(xgrid[Coordinates],1) - if dim==1 - xgrid[BEdgeEdges]=zeros(Ti,0) - xgrid[BEdgeNodes]=zeros(Ti,0,0) + dim = size(xgrid[Coordinates], 1) + if dim == 1 + xgrid[BEdgeEdges] = zeros(Ti, 0) + xgrid[BEdgeNodes] = zeros(Ti, 0, 0) return xgrid[BEdgeNodes] end - - if dim==2 + + if dim == 2 xgrid[BEdgeEdges] = xgrid[BFaceFaces] xgrid[BEdgeNodes] = xgrid[BFaceNodes] return xgrid[BEdgeNodes] @@ -1127,19 +1126,19 @@ function instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeNodes}) where {Tc xFaceEdges = xgrid[FaceEdges] xBFaceGeometries = xgrid[BFaceGeometries] nbfaces = length(xBFaceFaces) - xBEdgeEdges = zeros(Ti,0) + xBEdgeEdges = zeros(Ti, 0) EG = Triangle2D edge::Ti = 0 face::Ti = 0 nfaceedges::Ti = 0 - for bface = 1 : nbfaces + for bface in 1:nbfaces EG = xBFaceGeometries[bface] nfaceedges = num_edges(EG) face = xBFaceFaces[bface] - for k = 1 : nfaceedges - edge = xFaceEdges[k,face] + for k in 1:nfaceedges + edge = xFaceEdges[k, face] if !(edge in xBEdgeEdges) push!(xBEdgeEdges, edge) end @@ -1148,203 +1147,201 @@ function instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeNodes}) where {Tc end nbedges = length(xBEdgeEdges) - xBEdgeNodes = zeros(Ti,2,nbedges) - for bedge = 1 : nbedges, k = 1 : 2 - xBEdgeNodes[k,bedge] = xEdgeNodes[k,xBEdgeEdges[bedge]] + xBEdgeNodes = zeros(Ti, 2, nbedges) + for bedge in 1:nbedges, k in 1:2 + xBEdgeNodes[k, bedge] = xEdgeNodes[k, xBEdgeEdges[bedge]] end xgrid[BEdgeEdges] = xBEdgeEdges - xBEdgeNodes + return xBEdgeNodes end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeEdges}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BEdgeEdges}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, BEdgeNodes) - xgrid[BEdgeEdges] + return xgrid[BEdgeEdges] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceCells}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceCells}) where {Tc, Ti} ExtendableGrids.instantiate(xgrid, FaceNodes) - xgrid[FaceCells] + return xgrid[FaceCells] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceRegions}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceRegions}) where {Tc, Ti} # interior faces get region number 0, boundary faces get their boundary region xBFaceFaces = xgrid[BFaceFaces] xBFaceRegions = xgrid[BFaceRegions] - xFaceRegions = zeros(Ti,num_sources(xgrid[FaceNodes])) - for j = 1 : length(xBFaceFaces) + xFaceRegions = zeros(Ti, num_sources(xgrid[FaceNodes])) + for j in 1:length(xBFaceFaces) xFaceRegions[xBFaceFaces[j]] = xBFaceRegions[j] end - xFaceRegions + return xFaceRegions end - ### Not a good idea to have them as vectors of constants -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeRegions}) where {Tc,Ti} - return VectorOfConstants(Ti(0),num_sources(xgrid[EdgeNodes])) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeRegions}) where {Tc, Ti} + return VectorOfConstants(Ti(0), num_sources(xgrid[EdgeNodes])) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeRegions}) where {Tc,Ti} - return Array{Int32,1}(1:num_bedges(xgrid)) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BEdgeRegions}) where {Tc, Ti} + return Array{Int32, 1}(1:num_bedges(xgrid)) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeGeometries}) where {Tc,Ti} - return VectorOfConstants{ElementGeometries,Ti}(Edge1D,num_sources(xgrid[EdgeNodes])) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeGeometries}) where {Tc, Ti} + return VectorOfConstants{ElementGeometries, Ti}(Edge1D, num_sources(xgrid[EdgeNodes])) end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeGeometries}) where {Tc,Ti} - return VectorOfConstants{ElementGeometries,Ti}(Edge1D,num_sources(xgrid[BEdgeNodes])) +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BEdgeGeometries}) where {Tc, Ti} + return VectorOfConstants{ElementGeometries, Ti}(Edge1D, num_sources(xgrid[BEdgeNodes])) end - -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceCellPos}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BFaceCellPos}) where {Tc, Ti} # gets the local position of a bface in the CellFaces row of its adjacent cell # get links to other stuff xCellFaces = xgrid[CellFaces] xFaceCells = xgrid[FaceCells] - xBFaceFaces::Array{Ti,1} = xgrid[BFaceFaces] + xBFaceFaces::Array{Ti, 1} = xgrid[BFaceFaces] nbfaces = length(xBFaceFaces) # init BFaceFaces - xBFaceCellPos = zeros(Ti,nbfaces) + xBFaceCellPos = zeros(Ti, nbfaces) cface = 0 cell = 0 nfaces4cell = 0 - for bface = 1 : nbfaces + for bface in 1:nbfaces cface = xBFaceFaces[bface] - cell = xFaceCells[1,cface] - nfaces4cell = num_targets(xCellFaces,cell) - for face = 1 : nfaces4cell - if cface == xCellFaces[face,cell] + cell = xFaceCells[1, cface] + nfaces4cell = num_targets(xCellFaces, cell) + for face in 1:nfaces4cell + if cface == xCellFaces[face, cell] xBFaceCellPos[bface] = face break end end end - xBFaceCellPos + return xBFaceCellPos end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceNormals}) where {Tc,Ti} - dim::Int = size(xgrid[Coordinates],1) - xCoordinates::Array{Tc,2} = xgrid[Coordinates] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceNormals}) where {Tc, Ti} + dim::Int = size(xgrid[Coordinates], 1) + xCoordinates::Array{Tc, 2} = xgrid[Coordinates] xFaceNodes::Adjacency{Ti} = xgrid[FaceNodes] nfaces::Int = num_sources(xFaceNodes) xFaceGeometries::GridEGTypes = xgrid[FaceGeometries] xCoordinateSystem::Type{<:AbstractCoordinateSystem} = xgrid[CoordinateSystem] - xFaceNormals::Array{Tc,2} = haskey(xgrid.components, FaceNormals) ? xgrid[FaceNormals] : zeros(Tc,dim,nfaces) + xFaceNormals::Array{Tc, 2} = haskey(xgrid.components, FaceNormals) ? xgrid[FaceNormals] : zeros(Tc, dim, nfaces) - normal::Array{Tc,1} = zeros(Tc,dim) + normal::Array{Tc, 1} = zeros(Tc, dim) EG = xFaceGeometries[1] - for face = 1 : nfaces + for face in 1:nfaces EG = xFaceGeometries[face] - Normal4ElemType!(normal,xCoordinates,xFaceNodes,face,EG,xCoordinateSystem) - for k = 1 : dim + Normal4ElemType!(normal, xCoordinates, xFaceNodes, face, EG, xCoordinateSystem) + for k in 1:dim xFaceNormals[k, face] = normal[k] - end + end end - xFaceNormals + return xFaceNormals end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeTangents}) where {Tc,Ti} - dim::Int = size(xgrid[Coordinates],1) - xCoordinates::Array{Tc,2} = xgrid[Coordinates] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeTangents}) where {Tc, Ti} + dim::Int = size(xgrid[Coordinates], 1) + xCoordinates::Array{Tc, 2} = xgrid[Coordinates] xEdgeNodes::Adjacency{Ti} = xgrid[EdgeNodes] nedges::Int = num_sources(xEdgeNodes) xEdgeGeometries::GridEGTypes = xgrid[EdgeGeometries] xCoordinateSystem::Type{<:AbstractCoordinateSystem} = xgrid[CoordinateSystem] - xEdgeTangents::Array{Tc,2} = haskey(xgrid.components, EdgeTangents) ? xgrid[EdgeTangents] : zeros(Tc,dim,nedges) + xEdgeTangents::Array{Tc, 2} = haskey(xgrid.components, EdgeTangents) ? xgrid[EdgeTangents] : zeros(Tc, dim, nedges) EG = xEdgeGeometries[1] - tangent::Array{Tc,1} = zeros(Tc,dim) - for edge = 1 : nedges + tangent::Array{Tc, 1} = zeros(Tc, dim) + for edge in 1:nedges EG = xEdgeGeometries[edge] - Tangent4ElemType!(tangent,xCoordinates,xEdgeNodes,edge,EG,xCoordinateSystem) - for k = 1 : dim + Tangent4ElemType!(tangent, xCoordinates, xEdgeNodes, edge, EG, xCoordinateSystem) + for k in 1:dim xEdgeTangents[k, edge] = tangent[k] - end + end end - xEdgeTangents + return xEdgeTangents end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{UniqueCellGeometries}) where {Tc,Ti} - xUniqueCellGeometries = ElementGeometries[unique(xgrid[CellGeometries])...] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{UniqueCellGeometries}) where {Tc, Ti} + return xUniqueCellGeometries = ElementGeometries[unique(xgrid[CellGeometries])...] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{UniqueFaceGeometries}) where {Tc,Ti} - xUniqueFaceGeometries = ElementGeometries[unique(xgrid[FaceGeometries])...] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{UniqueFaceGeometries}) where {Tc, Ti} + return xUniqueFaceGeometries = ElementGeometries[unique(xgrid[FaceGeometries])...] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{UniqueBFaceGeometries}) where {Tc,Ti} - xUniqueBFaceGeometries = ElementGeometries[unique(xgrid[BFaceGeometries])...] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{UniqueBFaceGeometries}) where {Tc, Ti} + return xUniqueBFaceGeometries = ElementGeometries[unique(xgrid[BFaceGeometries])...] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{UniqueEdgeGeometries}) where {Tc,Ti} - xUniqueEdgeGeometries = ElementGeometries[unique(xgrid[EdgeGeometries])...] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{UniqueEdgeGeometries}) where {Tc, Ti} + return xUniqueEdgeGeometries = ElementGeometries[unique(xgrid[EdgeGeometries])...] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{UniqueBEdgeGeometries}) where {Tc,Ti} - xUniqueBEdgeGeometries = ElementGeometries[unique(xgrid[BEdgeGeometries])...] +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{UniqueBEdgeGeometries}) where {Tc, Ti} + return xUniqueBEdgeGeometries = ElementGeometries[unique(xgrid[BEdgeGeometries])...] end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{CellAssemblyGroups}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{CellAssemblyGroups}) where {Tc, Ti} xGeometryGroups = VariableTargetAdjacency(Ti) xCellGeometries = xgrid[CellGeometries] xUniqueCellGeometries = xgrid[UniqueCellGeometries] for EG in xUniqueCellGeometries append!(xGeometryGroups, findall(==(EG), xCellGeometries)) end - xGeometryGroups + return xGeometryGroups end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{FaceAssemblyGroups}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{FaceAssemblyGroups}) where {Tc, Ti} xGeometryGroups = VariableTargetAdjacency(Ti) xFaceGeometries = xgrid[FaceGeometries] xUniqueFaceGeometries = xgrid[UniqueFaceGeometries] for EG in xUniqueFaceGeometries append!(xGeometryGroups, findall(==(EG), xFaceGeometries)) end - xGeometryGroups + return xGeometryGroups end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BFaceAssemblyGroups}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BFaceAssemblyGroups}) where {Tc, Ti} xGeometryGroups = VariableTargetAdjacency(Ti) xBFaceGeometries = xgrid[BFaceGeometries] xUniqueBFaceGeometries = xgrid[UniqueBFaceGeometries] for EG in xUniqueBFaceGeometries append!(xGeometryGroups, findall(==(EG), xBFaceGeometries)) end - xGeometryGroups + return xGeometryGroups end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{EdgeAssemblyGroups}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{EdgeAssemblyGroups}) where {Tc, Ti} xGeometryGroups = VariableTargetAdjacency(Ti) xEdgeGeometries = xgrid[EdgeGeometries] xUniqueEdgeGeometries = xgrid[UniqueEdgeGeometries] for EG in xUniqueEdgeGeometries append!(xGeometryGroups, findall(==(EG), xEdgeGeometries)) end - xGeometryGroups + return xGeometryGroups end -function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc,Ti}, ::Type{BEdgeAssemblyGroups}) where {Tc,Ti} +function ExtendableGrids.instantiate(xgrid::ExtendableGrid{Tc, Ti}, ::Type{BEdgeAssemblyGroups}) where {Tc, Ti} xGeometryGroups = VariableTargetAdjacency(Ti) xBEdgeGeometries = xgrid[BEdgeGeometries] xUniqueBEdgeGeometries = xgrid[UniqueBEdgeGeometries] for EG in xUniqueBEdgeGeometries append!(xGeometryGroups, findall(==(EG), xBEdgeGeometries)) end - xGeometryGroups + return xGeometryGroups end diff --git a/src/elementgeometry.jl b/src/elementgeometry.jl index db302767..ab06bf0c 100644 --- a/src/elementgeometry.jl +++ b/src/elementgeometry.jl @@ -8,7 +8,7 @@ $(TYPEDSIGNATURES) List supported element geometries. """ -elementgeometries()=AbstractTrees.print_tree(AbstractElementGeometry) +elementgeometries() = AbstractTrees.print_tree(AbstractElementGeometry) """ @@ -122,28 +122,30 @@ abstract type HyperCube4D <: AbstractElementGeometry4D end """ $(TYPEDSIGNATURES) """ -dim_element(::Type{<:AbstractElementGeometry0D})=0 +dim_element(::Type{<:AbstractElementGeometry0D}) = 0 """ $(TYPEDSIGNATURES) """ -dim_element(::Type{<:AbstractElementGeometry1D})=1 +dim_element(::Type{<:AbstractElementGeometry1D}) = 1 """ $(TYPEDSIGNATURES) """ -dim_element(::Type{<:AbstractElementGeometry2D})=2 +dim_element(::Type{<:AbstractElementGeometry2D}) = 2 """ $(TYPEDSIGNATURES) """ -dim_element(::Type{<:AbstractElementGeometry3D})=3 +dim_element(::Type{<:AbstractElementGeometry3D}) = 3 """ $(TYPEDSIGNATURES) """ -dim_element(::Type{<:AbstractElementGeometry4D})=4 +dim_element(::Type{<:AbstractElementGeometry4D}) = 4 -const allgeometrytypes=vcat(allsubtypes(AbstractElementGeometry0D), - allsubtypes(AbstractElementGeometry1D), - allsubtypes(AbstractElementGeometry2D), - allsubtypes(AbstractElementGeometry3D)) +const allgeometrytypes = vcat( + allsubtypes(AbstractElementGeometry0D), + allsubtypes(AbstractElementGeometry1D), + allsubtypes(AbstractElementGeometry2D), + allsubtypes(AbstractElementGeometry3D) +) -const ElementGeometries=Union{[Type{t} for t in allgeometrytypes]...} +const ElementGeometries = Union{[Type{t} for t in allgeometrytypes]...} diff --git a/src/extendablegrid.jl b/src/extendablegrid.jl index 7fa57419..6f0bd083 100644 --- a/src/extendablegrid.jl +++ b/src/extendablegrid.jl @@ -50,7 +50,7 @@ $(TYPEDEF) abstract type AbstractGridFloatArray1D <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridFloatArray1D})::Array{Tc, 1} where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -60,7 +60,7 @@ $(TYPEDEF) abstract type AbstractGridFloatArray2D <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridFloatArray2D})::Array{Tc, 2} where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -70,7 +70,7 @@ $(TYPEDEF) abstract type AbstractGridIntegerArray1D <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridIntegerArray1D})::Array{Ti, 1} where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -80,7 +80,7 @@ $(TYPEDEF) abstract type AbstractGridIntegerArray2D <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridIntegerArray2D})::Array{Ti, 1} where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -90,7 +90,7 @@ Integer number abstract type AbstractGridIntegerConstant <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridIntegerConstant})::Ti where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -100,7 +100,7 @@ Floating point number abstract type AbstractGridFloatConstant <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridFloatConstant})::Tc where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -111,7 +111,7 @@ Any kind of adjacency between grid components abstract type AbstractGridAdjacency <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractGridAdjacency})::Adjacency{Ti} where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -121,9 +121,11 @@ Array of element geometry information. """ abstract type AbstractElementGeometries <: AbstractGridComponent end -function Base.getindex(grid::ExtendableGrid{Tc, Ti}, - T::Type{<:AbstractElementGeometries})::ElementInfo{ElementGeometries} where {Tc, Ti} - get!(grid, T) +function Base.getindex( + grid::ExtendableGrid{Tc, Ti}, + T::Type{<:AbstractElementGeometries} + )::ElementInfo{ElementGeometries} where {Tc, Ti} + return get!(grid, T) end """ @@ -134,7 +136,7 @@ Array of element region number information. abstract type AbstractElementRegions <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{<:AbstractElementRegions})::ElementInfo{Ti} where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end """ @@ -145,7 +147,7 @@ Coordinate system abstract type CoordinateSystem <: AbstractGridComponent end function Base.getindex(grid::ExtendableGrid{Tc, Ti}, T::Type{CoordinateSystem})::CoordinateSystems where {Tc, Ti} - get!(grid, T) + return get!(grid, T) end ################################################################## @@ -269,8 +271,10 @@ $(TYPEDSIGNATURES) To be called by getindex. This triggers lazy creation of non-existing gridcomponents """ -Base.get!(grid::ExtendableGrid, T::Type{<:AbstractGridComponent}) = get!(() -> veryform(grid, instantiate(grid, T), T), - grid.components, T) +Base.get!(grid::ExtendableGrid, T::Type{<:AbstractGridComponent}) = get!( + () -> veryform(grid, instantiate(grid, T), T), + grid.components, T +) """ $(TYPEDSIGNATURES) @@ -290,7 +294,7 @@ veryform(grid::ExtendableGrid{Tc,Ti},v,T::Type{<:AbstractGridAdjacency}) where{T Check proper type of adjacencies upon insertion """ veryform(grid::ExtendableGrid{Tc, Ti}, v, T::Type{<:AbstractGridAdjacency}) where {Tc, Ti} = typeof(v) <: Adjacency{Ti} ? v : - throw("Type mismatch") + throw("Type mismatch") ############################################################ # Instantiation methods @@ -307,7 +311,7 @@ $(TYPEDSIGNATURES) Instantiate number of bface regions """ -instantiate(grid, ::Type{NumBFaceRegions}) = length(grid[BFaceRegions])> 0 ? maximum(grid[BFaceRegions]) : 0 +instantiate(grid, ::Type{NumBFaceRegions}) = length(grid[BFaceRegions]) > 0 ? maximum(grid[BFaceRegions]) : 0 """ $(TYPEDSIGNATURES) @@ -319,7 +323,7 @@ instantiate(grid, ::Type{NumBEdgeRegions}) = maximum(grid[BEdgeRegions]) function prepare_bedgeregions!(grid::ExtendableGrid) bedges = grid[BEdgeNodes] bedgeregions = zeros(Int32, num_bedges(grid)) - grid[BEdgeRegions] = bedgeregions + return grid[BEdgeRegions] = bedgeregions end instantiate(grid, ::Type{BEdgeRegions}) = prepare_bedgeregions!(grid) @@ -367,7 +371,7 @@ $(SIGNATURES) Number of nodes in grid """ -num_nodes(grid::ExtendableGrid)::Int = haskey(grid,Coordinates) ? size(grid[Coordinates], 2) : 0 +num_nodes(grid::ExtendableGrid)::Int = haskey(grid, Coordinates) ? size(grid[Coordinates], 2) : 0 """ $(TYPEDSIGNATURES) @@ -425,20 +429,20 @@ Type of indices index_type(grid::ExtendableGrid{Tc, Ti}) where {Tc, Ti} = Ti function dangling_nodes(grid) - coord=grid[Coordinates] - nnodes=size(coord,2) - nodemark=zeros(Bool,nnodes) - cn=grid[CellNodes] - ncells=num_cells(grid) - for icell = 1:ncells - for inode=1:num_targets(cn,icell) - nodemark[cn[inode,icell]] = true + coord = grid[Coordinates] + nnodes = size(coord, 2) + nodemark = zeros(Bool, nnodes) + cn = grid[CellNodes] + ncells = num_cells(grid) + for icell in 1:ncells + for inode in 1:num_targets(cn, icell) + nodemark[cn[inode, icell]] = true end end if all(nodemark) return nothing else - return coord[:,findall(!,nodemark)] + return coord[:, findall(!, nodemark)] end end @@ -453,17 +457,17 @@ Check consistency of grid: a grid is consistent if If grid is consistent, return true, otherwise throw an error, or, if `warnoly==true`, return false. """ -function isconsistent(grid; warnonly=false) - consistent= true - dnodes=dangling_nodes(grid) +function isconsistent(grid; warnonly = false) + consistent = true + dnodes = dangling_nodes(grid) if !isnothing(dnodes) @warn "Found dangling nodes: $(dnodes)" - consistent=false + consistent = false end if !consistent && ! warnonly error("Consistency error(s) found in grid") end - consistent + return consistent end """ @@ -483,50 +487,50 @@ and are possible. """ -function Base.map(f::Function, grid::ExtendableGrid{Tc, Ti}) where {Tc,Ti} +function Base.map(f::Function, grid::ExtendableGrid{Tc, Ti}) where {Tc, Ti} coord = grid[Coordinates] - c1=coord[:,1] + c1 = coord[:, 1] dim = dim_space(grid) ## Check if f can be called with number args like f(x,y) - function checknumargs(f,args...) - if !hasmethod(f,Tuple(typeof.(args))) - return false - end - try - y=f(args...) - catch e - if isa(e,MethodError) - return false - end - if isa(e,BoundsError) - return false - end - rethrow(e) - end - return true + function checknumargs(f, args...) + if !hasmethod(f, Tuple(typeof.(args))) + return false + end + try + y = f(args...) + catch e + if isa(e, MethodError) + return false + end + if isa(e, BoundsError) + return false + end + rethrow(e) + end + return true end ## Check if f can be called with vector args like f(X::Vector) and returns a number - function checkvecargs(f,v) - if !hasmethod(f,Tuple{Vector}) - return false - end - try - y=f(v) - catch e - if isa(e,MethodError) - return false - end - rethrow(e) - end - return true + function checkvecargs(f, v) + if !hasmethod(f, Tuple{Vector}) + return false + end + try + y = f(v) + catch e + if isa(e, MethodError) + return false + end + rethrow(e) + end + return true end - - use_numargs=checknumargs(f,c1...) - use_vecargs=checkvecargs(f,c1) - if use_numargs + use_numargs = checknumargs(f, c1...) + use_vecargs = checkvecargs(f, c1) + + return if use_numargs if dim == 1 @views Base.map(f, coord[1, :]) elseif dim == 2 @@ -535,7 +539,7 @@ function Base.map(f::Function, grid::ExtendableGrid{Tc, Ti}) where {Tc,Ti} @views Base.map(f, coord[1, :], coord[2, :], coord[3, :]) end elseif use_vecargs - Base.map(f,reinterpret(reshape, SVector{dim, Tc}, coord)) + Base.map(f, reinterpret(reshape, SVector{dim, Tc}, coord)) else error("Cannot map function $f on grid. Check for consistency of function args and grid dimension.") end @@ -547,32 +551,36 @@ end # See https://discourse.julialang.org/t/show-and-showcompact-on-custom-types/8493/6 # function Base.show(io::IO, ::MIME"text/plain", grid::ExtendableGrid) - str = @sprintf("%s\n dim = %7d\n nnodes = %7d\n ncells = %7d\n nbfaces = %7d", - typeof(grid), - dim_space(grid), num_nodes(grid), num_cells(grid), num_bfaces(grid)) + str = @sprintf( + "%s\n dim = %7d\n nnodes = %7d\n ncells = %7d\n nbfaces = %7d", + typeof(grid), + dim_space(grid), num_nodes(grid), num_cells(grid), num_bfaces(grid) + ) if num_edges(grid) > 0 - str*=@sprintf("\n nedges = %7d", num_edges(grid)) + str *= @sprintf("\n nedges = %7d", num_edges(grid)) end - if num_partitions(grid)>1 - str*="\n npartitions/color = $(num_partitions_per_color(grid))" + if num_partitions(grid) > 1 + str *= "\n npartitions/color = $(num_partitions_per_color(grid))" end print(io, str) - nothing + return nothing end function Base.show(io::IO, grid::ExtendableGrid) - str = @sprintf("%s(dim=%d, nnodes=%d, ncells=%d, nbfaces=%d", - typeof(grid), - dim_space(grid), num_nodes(grid), num_cells(grid), num_bfaces(grid)) + str = @sprintf( + "%s(dim=%d, nnodes=%d, ncells=%d, nbfaces=%d", + typeof(grid), + dim_space(grid), num_nodes(grid), num_cells(grid), num_bfaces(grid) + ) if num_edges(grid) > 0 - str*=@sprintf(", nedges=%d", num_edges(grid)) + str *= @sprintf(", nedges=%d", num_edges(grid)) end - if num_partitions(grid)>1 - str*=", npart/color=$(num_partitions_per_color(grid))" + if num_partitions(grid) > 1 + str *= ", npart/color=$(num_partitions_per_color(grid))" end - str*=")" + str *= ")" print(io, str) - nothing + return nothing end ### Tests for the gmsh extension: @@ -580,7 +588,7 @@ end function multidimsort(A) i1 = sortperm(A[1, :]) A1 = A[:, i1] - for j = 2:size(A, 1) + for j in 2:size(A, 1) cm = countmap(A1[j - 1, :]) for (key, val) in cm if val > 1 #if there only is one entry with this key, the reordering is not necessary @@ -682,18 +690,18 @@ function seemingly_equal(array1::AbstractArray, array2::AbstractArray) return false end end - true + return true end function seemingly_equal(a1::VariableTargetAdjacency, a2::VariableTargetAdjacency) - seemingly_equal(a1.colentries, a2.colentries) && seemingly_equal(a1.colstart, a2.colstart) + return seemingly_equal(a1.colentries, a2.colentries) && seemingly_equal(a1.colstart, a2.colstart) end seemingly_equal(x1::Type, x2::Type) = (x1 == x2) seemingly_equal(x1::Number, x2::Number) = (x1 ≈ x2) seemingly_equal(x1::Any, x2::Any) = (x1 == x2) function numbers_match(grid, nn, nc, nb) - num_nodes(grid) == nn && + return num_nodes(grid) == nn && num_cells(grid) == nc && num_bfaces(grid) == nb end @@ -702,5 +710,5 @@ Base.extrema(grid::ExtendableGrid) = Base.extrema(grid[Coordinates]; dims = 2) function bbox(grid) e = extrema(grid) - map(a -> a[1], e), map(a -> a[2], e) + return map(a -> a[1], e), map(a -> a[2], e) end diff --git a/src/io.jl b/src/io.jl index 9b4402ab..fe17c2d5 100644 --- a/src/io.jl +++ b/src/io.jl @@ -21,7 +21,7 @@ For the arguments 'append' and 'compress', see documentation of vtk_grid of Writ """ function writeVTK(filename::String, grid::ExtendableGrid{Tc, Ti}; append = false, compress = false, kwargs...) where {Tc, Ti} ncells = num_cells(grid) # get number of cells in grid - coords = grid[Coordinates] # get coordinates + coords = grid[Coordinates] # get coordinates cells = grid[CellNodes] # get cell-node list geo_dim = size(coords, 1) @@ -29,11 +29,11 @@ function writeVTK(filename::String, grid::ExtendableGrid{Tc, Ti}; append = false vtk_cells = Array{MeshCell, 1}(undef, ncells) - for icell = 1:ncells + for icell in 1:ncells vtk_cells[icell] = MeshCell(VTKCellType(cell_geo[icell]), view(cells, :, icell)) end - vtk_grid(filename, coords, vtk_cells, append = append, compress = compress) do vtk + return vtk_grid(filename, coords, vtk_cells, append = append, compress = compress) do vtk for (key, value) in kwargs vtk[String(key)] = value end @@ -53,22 +53,22 @@ function Base.write(fname::String, g::ExtendableGrid; format = "", kwargs...) if format == "" format = fext[2:end] end - try - writegrid(fname,g, Val{Symbol(format)}; kwargs...) + return try + writegrid(fname, g, Val{Symbol(format)}; kwargs...) catch e throw(ErrorException("Writing $(fext) files not supported")) end end -function writegrid(filename::String, g::ExtendableGrid, ::Type{Val{:msh}};kwargs...) - try +function writegrid(filename::String, g::ExtendableGrid, ::Type{Val{:msh}}; kwargs...) + return try simplexgrid_to_gmsh(g; filename) catch e throw(ErrorException("Missing Gmsh extension. Add Gmsh.jl to your environment and import it to write msh files.")) end end -function writegrid(fname::String, g::ExtendableGrid, ::Type{Val{:sg}}; version=v"2.2", kwargs...) +function writegrid(fname::String, g::ExtendableGrid, ::Type{Val{:sg}}; version = v"2.2", kwargs...) dim_g = dim_grid(g) dim_s = dim_space(g) nn = num_nodes(g) @@ -84,54 +84,55 @@ function writegrid(fname::String, g::ExtendableGrid, ::Type{Val{:sg}}; version=v open(fname, "w") do file write(file, @sprintf("SimplexGrid")) write(file, @sprintf(" ")) - write(file, @sprintf("%s\n","$version"[1:end-2])) + write(file, @sprintf("%s\n", "$version"[1:(end - 2)])) write(file, @sprintf("#created by ExtendableGrids.jl (c) J.Fuhrmann et al\n")) write(file, @sprintf("#%s\n", Dates.format(Dates.now(), "yyyy-mm-ddTHH-mm-SS"))) write(file, @sprintf("DIMENSION %d\n", dim_g)) write(file, @sprintf("NODES %d %d\n", nn, dim_s)) - for inode = 1:nn - for idim = 1:dim_s + for inode in 1:nn + for idim in 1:dim_s write(file, @sprintf("%.20e ", coord[idim, inode])) write(file, @sprintf("\n")) end end write(file, @sprintf("CELLS %d\n", nc)) - for icell = 1:nc - for inode = 1:(dim_g + 1) + for icell in 1:nc + for inode in 1:(dim_g + 1) write(file, @sprintf("%d ", cellnodes[inode, icell])) end write(file, @sprintf("%d\n", cellregions[icell])) end write(file, @sprintf("FACES %d\n", nbf)) - for ibface = 1:nbf - for inode = 1:dim_g + for ibface in 1:nbf + for inode in 1:dim_g write(file, @sprintf("%d ", bfacenodes[inode, ibface])) end write(file, @sprintf("%d\n", bfaceregions[ibface])) end - if version>v"2.1" - function writeitems(key,label) - data=g[key] + if version > v"2.1" + function writeitems(key, label) + data = g[key] write(file, "$(label) $(length(data))\n") for d in data write(file, "$d\n") end + return end - writeitems(PColorPartitions,"PCOLORPARTITIONS") - writeitems(PartitionCells,"PARTITIONCELLS") - writeitems(PartitionBFaces,"PARTITIONBFACES") - writeitems(PartitionNodes,"PARTITIONNODES") + writeitems(PColorPartitions, "PCOLORPARTITIONS") + writeitems(PartitionCells, "PARTITIONCELLS") + writeitems(PartitionBFaces, "PARTITIONBFACES") + writeitems(PartitionNodes, "PARTITIONNODES") end write(file, @sprintf("END\n")) flush(file) flush(file) end - nothing + return nothing end ###################################################### @@ -153,15 +154,15 @@ function simplexgrid(file::String; format = "", kwargs...) if format == "" format = fext[2:end] end - try - simplexgrid(file,Val{Symbol(format)}; kwargs...) + return try + simplexgrid(file, Val{Symbol(format)}; kwargs...) catch e throw(ErrorException("Reading $(fext) files not supported")) end end function simplexgrid(file::String, ::Type{Val{:msh}}; kwargs...) - try + return try simplexgrid_from_gmsh(file) catch e throw(ErrorException("Missing Gmsh extension. Add Gmsh.jl to your environment and import it to read msh files.")) @@ -169,7 +170,7 @@ function simplexgrid(file::String, ::Type{Val{:msh}}; kwargs...) end function simplexgrid(file::String, ::Type{Val{:geo}}; kwargs...) - try + return try simplexgrid_from_gmsh(file) catch e throw(ErrorException("Missing Gmsh extension. Add Gmsh.jl to your environment and import it to read geo files.")) @@ -182,7 +183,7 @@ function simplexgrid(file::String, ::Type{Val{:sg}}; kwargs...) expecttoken(tks, "SimplexGrid") version = VersionNumber(gettoken(tks)) - if version=v"2.3" + if version < v"2.0" || version >= v"2.3" error("Read grid: wrong format version: $(version)") end @@ -193,12 +194,12 @@ function simplexgrid(file::String, ::Type{Val{:sg}}; kwargs...) faces = Array{Ti, 2}(undef, 0, 0) bregions = Array{Ti, 1}(undef, 0) - pcolorpartitions=Array{Ti, 1}(undef, 0) - partitioncells=Array{Ti, 1}(undef, 0) - partitionbfaces=Array{Ti, 1}(undef, 0) - partitionnodes=Array{Ti, 1}(undef, 0) - - + pcolorpartitions = Array{Ti, 1}(undef, 0) + partitioncells = Array{Ti, 1}(undef, 0) + partitionbfaces = Array{Ti, 1}(undef, 0) + partitionnodes = Array{Ti, 1}(undef, 0) + + while (true) if (trytoken(tks, "DIMENSION")) dim = parse(Ti, gettoken(tks)) @@ -209,8 +210,8 @@ function simplexgrid(file::String, ::Type{Val{:sg}}; kwargs...) error("Dimension error (DIMENSION $(dim)) in section NODES") end coord = Array{Float64, 2}(undef, dim, nnodes) - for inode = 1:nnodes - for idim = 1:embdim + for inode in 1:nnodes + for idim in 1:embdim coord[idim, inode] = parse(Float64, gettoken(tks)) end end @@ -218,13 +219,13 @@ function simplexgrid(file::String, ::Type{Val{:sg}}; kwargs...) ncells = parse(Ti, gettoken(tks)) cells = Array{Ti, 2}(undef, dim + 1, ncells) regions = Array{Ti, 1}(undef, ncells) - for icell = 1:ncells - for inode = 1:(dim + 1) + for icell in 1:ncells + for inode in 1:(dim + 1) cells[inode, icell] = parse(Ti, gettoken(tks)) end regions[icell] = parse(Ti, gettoken(tks)) - if version==v"2.0" - for j = 1:(dim + 1) + if version == v"2.0" + for j in 1:(dim + 1) gettoken(tks) # skip file format garbage end end @@ -233,42 +234,42 @@ function simplexgrid(file::String, ::Type{Val{:sg}}; kwargs...) nfaces = parse(Ti, gettoken(tks)) faces = Array{Ti, 2}(undef, dim, nfaces) bregions = Array{Ti, 1}(undef, nfaces) - for iface = 1:nfaces - for inode = 1:dim + for iface in 1:nfaces + for inode in 1:dim faces[inode, iface] = parse(Ti, gettoken(tks)) end bregions[iface] = parse(Ti, gettoken(tks)) if version == v"2.0" - for j = 1:(dim + 2) + for j in 1:(dim + 2) gettoken(tks) #skip file format garbage end end end - elseif trytoken(tks, "PCOLORPARTITIONS") && version>v"2.1" - n=parse(Ti, gettoken(tks)) - pcolorpartitions=[parse(Ti, gettoken(tks)) for i=1:n] - elseif trytoken(tks, "PARTITIONCELLS") && version>v"2.1" - n=parse(Ti, gettoken(tks)) - partitioncells=[parse(Ti, gettoken(tks)) for i=1:n] - elseif trytoken(tks, "PARTITIONBFACES") && version>v"2.1" - n=parse(Ti, gettoken(tks)) - partitionbfaces=[parse(Ti, gettoken(tks)) for i=1:n] - elseif trytoken(tks, "PARTITIONNODES") && version>v"2.1" - n=parse(Ti, gettoken(tks)) - partitionnodes=[parse(Ti, gettoken(tks)) for i=1:n] + elseif trytoken(tks, "PCOLORPARTITIONS") && version > v"2.1" + n = parse(Ti, gettoken(tks)) + pcolorpartitions = [parse(Ti, gettoken(tks)) for i in 1:n] + elseif trytoken(tks, "PARTITIONCELLS") && version > v"2.1" + n = parse(Ti, gettoken(tks)) + partitioncells = [parse(Ti, gettoken(tks)) for i in 1:n] + elseif trytoken(tks, "PARTITIONBFACES") && version > v"2.1" + n = parse(Ti, gettoken(tks)) + partitionbfaces = [parse(Ti, gettoken(tks)) for i in 1:n] + elseif trytoken(tks, "PARTITIONNODES") && version > v"2.1" + n = parse(Ti, gettoken(tks)) + partitionnodes = [parse(Ti, gettoken(tks)) for i in 1:n] else expecttoken(tks, "END") break end end - g=simplexgrid(coord, cells, regions, faces, bregions) - if version>v"2.1" - g[PColorPartitions]=pcolorpartitions - g[PartitionCells]=partitioncells - g[PartitionBFaces]=partitionbfaces - g[PartitionNodes]=partitionnodes + g = simplexgrid(coord, cells, regions, faces, bregions) + if version > v"2.1" + g[PColorPartitions] = pcolorpartitions + g[PartitionCells] = partitioncells + g[PartitionBFaces] = partitionbfaces + g[PartitionNodes] = partitionnodes end - g + return g end function simplexgrid_from_gmsh end diff --git a/src/l2gtransformations.jl b/src/l2gtransformations.jl index 9015c51a..28ab72b2 100644 --- a/src/l2gtransformations.jl +++ b/src/l2gtransformations.jl @@ -6,7 +6,7 @@ # and is e.g. used by FEBasisEvaluator or CellFinder # # needs call of update_trafo! on entry of a new cell (to update the trafo matrix and the determinant det) -# +# # eval_trafo! maps local xref on cell to global x (e.g. for evaluation of data functions) # mapderiv! gives the derivative of the mapping (for computation of derivatives of basis functions) @@ -18,290 +18,290 @@ Transforms reference coordinates to global coordinates mutable struct L2GTransformer{Tv <: Real, Ti <: Integer, EG <: AbstractElementGeometry, CS <: AbstractCoordinateSystem} citem::Int nonlinear::Bool # so that users know if derivatives of map change in every quadrature point of cell or not - Coords::Array{Tv,2} + Coords::Array{Tv, 2} Nodes::Adjacency{Ti} - ItemVolumes::Array{Tv,1} + ItemVolumes::Array{Tv, 1} A::Matrix{Tv} b::Vector{Tv} C::Matrix{Tv} # cache for subcalculations that stay the same for each x (like adjugates) det::Tv -end +end -function L2GTransformer(EG::Type{<:AbstractElementGeometry}, grid::ExtendableGrid{Tv,Ti}, AT::Type{<:AssemblyType} = ON_CELLS) where {Tv, Ti} - A = zeros(Tv,size(grid[Coordinates],1),dim_element(EG)) - b = zeros(Tv,size(grid[Coordinates],1)) - return L2GTransformer{Tv,Ti,EG,grid[CoordinateSystem]}(0,false,grid[Coordinates],grid[GridComponentNodes4AssemblyType(AT)],grid[GridComponentVolumes4AssemblyType(AT)],A,b,zeros(Tv,0,0),0) +function L2GTransformer(EG::Type{<:AbstractElementGeometry}, grid::ExtendableGrid{Tv, Ti}, AT::Type{<:AssemblyType} = ON_CELLS) where {Tv, Ti} + A = zeros(Tv, size(grid[Coordinates], 1), dim_element(EG)) + b = zeros(Tv, size(grid[Coordinates], 1)) + return L2GTransformer{Tv, Ti, EG, grid[CoordinateSystem]}(0, false, grid[Coordinates], grid[GridComponentNodes4AssemblyType(AT)], grid[GridComponentVolumes4AssemblyType(AT)], A, b, zeros(Tv, 0, 0), 0) end -function L2GTransformer(EG::Union{Type{<:Tetrahedron3D}, Type{<:Parallelepiped3D}}, grid::ExtendableGrid{Tv,Ti}, AT::Type{<:AssemblyType} = ON_CELLS) where {Tv, Ti} - A = zeros(Tv,size(grid[Coordinates],1),dim_element(EG)) - b = zeros(Tv,size(grid[Coordinates],1)) - return L2GTransformer{Tv,Ti,EG,grid[CoordinateSystem]}(0,false,grid[Coordinates],grid[GridComponentNodes4AssemblyType(AT)],grid[GridComponentVolumes4AssemblyType(AT)],A,b,zeros(Tv,3,3),0) +function L2GTransformer(EG::Union{Type{<:Tetrahedron3D}, Type{<:Parallelepiped3D}}, grid::ExtendableGrid{Tv, Ti}, AT::Type{<:AssemblyType} = ON_CELLS) where {Tv, Ti} + A = zeros(Tv, size(grid[Coordinates], 1), dim_element(EG)) + b = zeros(Tv, size(grid[Coordinates], 1)) + return L2GTransformer{Tv, Ti, EG, grid[CoordinateSystem]}(0, false, grid[Coordinates], grid[GridComponentNodes4AssemblyType(AT)], grid[GridComponentVolumes4AssemblyType(AT)], A, b, zeros(Tv, 3, 3), 0) end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Vertex0D,Cartesian1D}, item::Int) - T.b[1] = T.Coords[1,T.Nodes[1,item]] +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Vertex0D, Cartesian1D}, item::Int) + T.b[1] = T.Coords[1, T.Nodes[1, item]] return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian1D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian1D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] T.det = T.ItemVolumes[item] - end + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian2D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian2D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] T.det = T.ItemVolumes[item] - end + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian3D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian3D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.b[3] = T.Coords[3,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[3,1] = T.Coords[3,T.Nodes[2,item]] - T.b[3] + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.b[3] = T.Coords[3, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[3, 1] = T.Coords[3, T.Nodes[2, item]] - T.b[3] T.det = T.ItemVolumes[item] - end + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Triangle2D,Cartesian2D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Triangle2D, Cartesian2D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[1,2] = T.Coords[1,T.Nodes[3,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[2,2] = T.Coords[2,T.Nodes[3,item]] - T.b[2] - T.det = 2*T.ItemVolumes[item] - end + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[1, 2] = T.Coords[1, T.Nodes[3, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[2, 2] = T.Coords[2, T.Nodes[3, item]] - T.b[2] + T.det = 2 * T.ItemVolumes[item] + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Parallelogram2D,Cartesian2D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Parallelogram2D, Cartesian2D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[1,2] = T.Coords[1,T.Nodes[4,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[2,2] = T.Coords[2,T.Nodes[4,item]] - T.b[2] + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[1, 2] = T.Coords[1, T.Nodes[4, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[2, 2] = T.Coords[2, T.Nodes[4, item]] - T.b[2] T.det = T.ItemVolumes[item] - end + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Triangle2D,Cartesian3D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Triangle2D, Cartesian3D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.b[3] = T.Coords[3,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[1,2] = T.Coords[1,T.Nodes[3,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[2,2] = T.Coords[2,T.Nodes[3,item]] - T.b[2] - T.A[3,1] = T.Coords[3,T.Nodes[2,item]] - T.b[3] - T.A[3,2] = T.Coords[3,T.Nodes[3,item]] - T.b[3] - T.det = 2*T.ItemVolumes[item] - end + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.b[3] = T.Coords[3, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[1, 2] = T.Coords[1, T.Nodes[3, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[2, 2] = T.Coords[2, T.Nodes[3, item]] - T.b[2] + T.A[3, 1] = T.Coords[3, T.Nodes[2, item]] - T.b[3] + T.A[3, 2] = T.Coords[3, T.Nodes[3, item]] - T.b[3] + T.det = 2 * T.ItemVolumes[item] + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Parallelogram2D,Cartesian3D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Parallelogram2D, Cartesian3D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.b[3] = T.Coords[3,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[1,2] = T.Coords[1,T.Nodes[4,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[2,2] = T.Coords[2,T.Nodes[4,item]] - T.b[2] - T.A[3,1] = T.Coords[3,T.Nodes[2,item]] - T.b[3] - T.A[3,2] = T.Coords[3,T.Nodes[4,item]] - T.b[3] + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.b[3] = T.Coords[3, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[1, 2] = T.Coords[1, T.Nodes[4, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[2, 2] = T.Coords[2, T.Nodes[4, item]] - T.b[2] + T.A[3, 1] = T.Coords[3, T.Nodes[2, item]] - T.b[3] + T.A[3, 2] = T.Coords[3, T.Nodes[4, item]] - T.b[3] T.det = T.ItemVolumes[item] - end + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Tetrahedron3D,Cartesian3D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Tetrahedron3D, Cartesian3D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.b[3] = T.Coords[3,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[1,2] = T.Coords[1,T.Nodes[3,item]] - T.b[1] - T.A[1,3] = T.Coords[1,T.Nodes[4,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[2,2] = T.Coords[2,T.Nodes[3,item]] - T.b[2] - T.A[2,3] = T.Coords[2,T.Nodes[4,item]] - T.b[2] - T.A[3,1] = T.Coords[3,T.Nodes[2,item]] - T.b[3] - T.A[3,2] = T.Coords[3,T.Nodes[3,item]] - T.b[3] - T.A[3,3] = T.Coords[3,T.Nodes[4,item]] - T.b[3] - T.det = 6*T.ItemVolumes[item] - end + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.b[3] = T.Coords[3, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[1, 2] = T.Coords[1, T.Nodes[3, item]] - T.b[1] + T.A[1, 3] = T.Coords[1, T.Nodes[4, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[2, 2] = T.Coords[2, T.Nodes[3, item]] - T.b[2] + T.A[2, 3] = T.Coords[2, T.Nodes[4, item]] - T.b[2] + T.A[3, 1] = T.Coords[3, T.Nodes[2, item]] - T.b[3] + T.A[3, 2] = T.Coords[3, T.Nodes[3, item]] - T.b[3] + T.A[3, 3] = T.Coords[3, T.Nodes[4, item]] - T.b[3] + T.det = 6 * T.ItemVolumes[item] + end return nothing end -function update_trafo!(T::L2GTransformer{<:Real,<:Integer,<:Parallelepiped3D,Cartesian3D}, item::Int) +function update_trafo!(T::L2GTransformer{<:Real, <:Integer, <:Parallelepiped3D, Cartesian3D}, item::Int) if T.citem != item T.citem = item - T.b[1] = T.Coords[1,T.Nodes[1,item]] - T.b[2] = T.Coords[2,T.Nodes[1,item]] - T.b[3] = T.Coords[3,T.Nodes[1,item]] - T.A[1,1] = T.Coords[1,T.Nodes[2,item]] - T.b[1] - T.A[1,2] = T.Coords[1,T.Nodes[4,item]] - T.b[1] - T.A[1,3] = T.Coords[1,T.Nodes[5,item]] - T.b[1] - T.A[2,1] = T.Coords[2,T.Nodes[2,item]] - T.b[2] - T.A[2,2] = T.Coords[2,T.Nodes[4,item]] - T.b[2] - T.A[2,3] = T.Coords[2,T.Nodes[5,item]] - T.b[2] - T.A[3,1] = T.Coords[3,T.Nodes[2,item]] - T.b[3] - T.A[3,2] = T.Coords[3,T.Nodes[4,item]] - T.b[3] - T.A[3,3] = T.Coords[3,T.Nodes[5,item]] - T.b[3] + T.b[1] = T.Coords[1, T.Nodes[1, item]] + T.b[2] = T.Coords[2, T.Nodes[1, item]] + T.b[3] = T.Coords[3, T.Nodes[1, item]] + T.A[1, 1] = T.Coords[1, T.Nodes[2, item]] - T.b[1] + T.A[1, 2] = T.Coords[1, T.Nodes[4, item]] - T.b[1] + T.A[1, 3] = T.Coords[1, T.Nodes[5, item]] - T.b[1] + T.A[2, 1] = T.Coords[2, T.Nodes[2, item]] - T.b[2] + T.A[2, 2] = T.Coords[2, T.Nodes[4, item]] - T.b[2] + T.A[2, 3] = T.Coords[2, T.Nodes[5, item]] - T.b[2] + T.A[3, 1] = T.Coords[3, T.Nodes[2, item]] - T.b[3] + T.A[3, 2] = T.Coords[3, T.Nodes[4, item]] - T.b[3] + T.A[3, 3] = T.Coords[3, T.Nodes[5, item]] - T.b[3] T.det = T.ItemVolumes[item] - end + end return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Vertex0D,Cartesian1D}, xref) +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Vertex0D, Cartesian1D}, xref) x[1] = T.b[1] return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Union{Triangle2D, Parallelogram2D},Cartesian2D}, xref) - x[1] = T.A[1,1]*xref[1] + T.A[1,2]*xref[2] + T.b[1] - x[2] = T.A[2,1]*xref[1] + T.A[2,2]*xref[2] + T.b[2] +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Union{Triangle2D, Parallelogram2D}, Cartesian2D}, xref) + x[1] = T.A[1, 1] * xref[1] + T.A[1, 2] * xref[2] + T.b[1] + x[2] = T.A[2, 1] * xref[1] + T.A[2, 2] * xref[2] + T.b[2] return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Union{Triangle2D, Parallelogram2D},Cartesian3D}, xref) - x[1] = T.A[1,1]*xref[1] + T.A[1,2]*xref[2] + T.b[1] - x[2] = T.A[2,1]*xref[1] + T.A[2,2]*xref[2] + T.b[2] - x[3] = T.A[3,1]*xref[1] + T.A[3,2]*xref[2] + T.b[3] +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Union{Triangle2D, Parallelogram2D}, Cartesian3D}, xref) + x[1] = T.A[1, 1] * xref[1] + T.A[1, 2] * xref[2] + T.b[1] + x[2] = T.A[2, 1] * xref[1] + T.A[2, 2] * xref[2] + T.b[2] + x[3] = T.A[3, 1] * xref[1] + T.A[3, 2] * xref[2] + T.b[3] return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Union{Tetrahedron3D, Parallelepiped3D},Cartesian3D}, xref) - x[1] = T.A[1,1]*xref[1] + T.A[1,2]*xref[2] + T.A[1,3]*xref[3] + T.b[1] - x[2] = T.A[2,1]*xref[1] + T.A[2,2]*xref[2] + T.A[2,3]*xref[3] + T.b[2] - x[3] = T.A[3,1]*xref[1] + T.A[3,2]*xref[2] + T.A[3,3]*xref[3] + T.b[3] +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Union{Tetrahedron3D, Parallelepiped3D}, Cartesian3D}, xref) + x[1] = T.A[1, 1] * xref[1] + T.A[1, 2] * xref[2] + T.A[1, 3] * xref[3] + T.b[1] + x[2] = T.A[2, 1] * xref[1] + T.A[2, 2] * xref[2] + T.A[2, 3] * xref[3] + T.b[2] + x[3] = T.A[3, 1] * xref[1] + T.A[3, 2] * xref[2] + T.A[3, 3] * xref[3] + T.b[3] return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian1D}, xref) - x[1] = T.A[1,1]*xref[1] + T.b[1] +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian1D}, xref) + x[1] = T.A[1, 1] * xref[1] + T.b[1] return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian2D}, xref) - x[1] = T.A[1,1]*xref[1] + T.b[1] - x[2] = T.A[2,1]*xref[1] + T.b[2] +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian2D}, xref) + x[1] = T.A[1, 1] * xref[1] + T.b[1] + x[2] = T.A[2, 1] * xref[1] + T.b[2] return nothing end -function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian3D}, xref) - x[1] = T.A[1,1]*xref[1] + T.b[1] - x[2] = T.A[2,1]*xref[1] + T.b[2] - x[3] = T.A[3,1]*xref[1] + T.b[3] +function eval_trafo!(x::AbstractArray, T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian3D}, xref) + x[1] = T.A[1, 1] * xref[1] + T.b[1] + x[2] = T.A[2, 1] * xref[1] + T.b[2] + x[3] = T.A[3, 1] * xref[1] + T.b[3] return nothing end # EDGE1D/CARTESIAN1D map derivative # x = a*xref + b # Dxref/dx = a^{-1} = |E|^{-1} -function mapderiv!(M::Matrix, T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian1D}, xref) +function mapderiv!(M::Matrix, T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian1D}, xref) # transposed inverse of A - M[1,1] = 1.0/T.det + M[1, 1] = 1.0 / T.det return nothing end # EDGE1D/CARTESIAN2D (tangential) map derivative # x = A*xref + b # Dxref/dx = A*tangent^{-1} = |E|^{-1} -function mapderiv!(M::Matrix, T::L2GTransformer{<:Real,<:Integer,<:Edge1D,Cartesian2D}, xref) +function mapderiv!(M::Matrix, T::L2GTransformer{<:Real, <:Integer, <:Edge1D, Cartesian2D}, xref) # transposed inverse of A - M[1,1] = 1.0/T.det + M[1, 1] = 1.0 / T.det return nothing end # TRIANGLE2D/CARTESIAN2D map derivative # x = A*xref + b # Dxref/dx = A^{-T} -function mapderiv!(M::AbstractMatrix, T::L2GTransformer{<:Real,<:Integer,<:Triangle2D,Cartesian2D}, xref) +function mapderiv!(M::AbstractMatrix, T::L2GTransformer{<:Real, <:Integer, <:Triangle2D, Cartesian2D}, xref) # transposed inverse of A - M[2,2] = T.A[1,1]/T.det - M[2,1] = -T.A[1,2]/T.det - M[1,2] = -T.A[2,1]/T.det - M[1,1] = T.A[2,2]/T.det + M[2, 2] = T.A[1, 1] / T.det + M[2, 1] = -T.A[1, 2] / T.det + M[1, 2] = -T.A[2, 1] / T.det + M[1, 1] = T.A[2, 2] / T.det return nothing end # similar for parallelogram -function mapderiv!(M::Matrix, T::L2GTransformer{<:Real,<:Integer,<:Parallelogram2D,Cartesian2D}, xref) +function mapderiv!(M::Matrix, T::L2GTransformer{<:Real, <:Integer, <:Parallelogram2D, Cartesian2D}, xref) # transposed inverse of A - M[2,2] = T.A[1,1]/T.det - M[2,1] = -T.A[1,2]/T.det - M[1,2] = -T.A[2,1]/T.det - M[1,1] = T.A[2,2]/T.det + M[2, 2] = T.A[1, 1] / T.det + M[2, 1] = -T.A[1, 2] / T.det + M[1, 2] = -T.A[2, 1] / T.det + M[1, 1] = T.A[2, 2] / T.det return nothing end -function mapderiv!(M::Matrix, T::L2GTransformer{<:Real,<:Integer,<:Tetrahedron3D,Cartesian3D}, xref) +function mapderiv!(M::Matrix, T::L2GTransformer{<:Real, <:Integer, <:Tetrahedron3D, Cartesian3D}, xref) # adjugate matrix = T.determinant of subblocks (for faster map_deriv!) - T.C[1,1] = T.A[2,2]*T.A[3,3] - T.A[2,3] * T.A[3,2] - T.C[1,2] = -(T.A[2,1]*T.A[3,3] - T.A[2,3] * T.A[3,1]) - T.C[1,3] = T.A[2,1]*T.A[3,2] - T.A[2,2] * T.A[3,1] - T.C[2,1] = -(T.A[1,2]*T.A[3,3] - T.A[1,3] * T.A[3,2]) - T.C[2,2] = T.A[1,1]*T.A[3,3] - T.A[1,3] * T.A[3,1] - T.C[2,3] = -(T.A[1,1]*T.A[3,2] - T.A[1,2] * T.A[3,1]) - T.C[3,1] = T.A[1,2]*T.A[2,3] - T.A[1,3] * T.A[2,2] - T.C[3,2] = -(T.A[1,1]*T.A[2,3] - T.A[1,3] * T.A[2,1]) - T.C[3,3] = T.A[1,1]*T.A[2,2] - T.A[1,2] * T.A[2,1] + T.C[1, 1] = T.A[2, 2] * T.A[3, 3] - T.A[2, 3] * T.A[3, 2] + T.C[1, 2] = -(T.A[2, 1] * T.A[3, 3] - T.A[2, 3] * T.A[3, 1]) + T.C[1, 3] = T.A[2, 1] * T.A[3, 2] - T.A[2, 2] * T.A[3, 1] + T.C[2, 1] = -(T.A[1, 2] * T.A[3, 3] - T.A[1, 3] * T.A[3, 2]) + T.C[2, 2] = T.A[1, 1] * T.A[3, 3] - T.A[1, 3] * T.A[3, 1] + T.C[2, 3] = -(T.A[1, 1] * T.A[3, 2] - T.A[1, 2] * T.A[3, 1]) + T.C[3, 1] = T.A[1, 2] * T.A[2, 3] - T.A[1, 3] * T.A[2, 2] + T.C[3, 2] = -(T.A[1, 1] * T.A[2, 3] - T.A[1, 3] * T.A[2, 1]) + T.C[3, 3] = T.A[1, 1] * T.A[2, 2] - T.A[1, 2] * T.A[2, 1] # transposed inverse of A - for j = 1 : 3, k = 1 : 3 - M[j,k] = T.C[j,k] / T.det + for j in 1:3, k in 1:3 + M[j, k] = T.C[j, k] / T.det end return nothing end -function mapderiv!(M::Matrix, T::L2GTransformer{<:Real,<:Integer,<:Parallelepiped3D,Cartesian3D}, xref) +function mapderiv!(M::Matrix, T::L2GTransformer{<:Real, <:Integer, <:Parallelepiped3D, Cartesian3D}, xref) # adjugate matrix = T.determinant of subblocks (for faster map_deriv!) - T.C[1,1] = T.A[2,2]*T.A[3,3] - T.A[2,3] * T.A[3,2] - T.C[1,2] = -(T.A[2,1]*T.A[3,3] - T.A[2,3] * T.A[3,1]) - T.C[1,3] = T.A[2,1]*T.A[3,2] - T.A[2,2] * T.A[3,1] - T.C[2,1] = -(T.A[1,2]*T.A[3,3] - T.A[1,3] * T.A[3,2]) - T.C[2,2] = T.A[1,1]*T.A[3,3] - T.A[1,3] * T.A[3,1] - T.C[2,3] = -(T.A[1,1]*T.A[3,2] - T.A[1,2] * T.A[3,1]) - T.C[3,1] = T.A[1,2]*T.A[2,3] - T.A[1,3] * T.A[2,2] - T.C[3,2] = -(T.A[1,1]*T.A[2,3] - T.A[1,3] * T.A[2,1]) - T.C[3,3] = T.A[1,1]*T.A[2,2] - T.A[1,2] * T.A[2,1] + T.C[1, 1] = T.A[2, 2] * T.A[3, 3] - T.A[2, 3] * T.A[3, 2] + T.C[1, 2] = -(T.A[2, 1] * T.A[3, 3] - T.A[2, 3] * T.A[3, 1]) + T.C[1, 3] = T.A[2, 1] * T.A[3, 2] - T.A[2, 2] * T.A[3, 1] + T.C[2, 1] = -(T.A[1, 2] * T.A[3, 3] - T.A[1, 3] * T.A[3, 2]) + T.C[2, 2] = T.A[1, 1] * T.A[3, 3] - T.A[1, 3] * T.A[3, 1] + T.C[2, 3] = -(T.A[1, 1] * T.A[3, 2] - T.A[1, 2] * T.A[3, 1]) + T.C[3, 1] = T.A[1, 2] * T.A[2, 3] - T.A[1, 3] * T.A[2, 2] + T.C[3, 2] = -(T.A[1, 1] * T.A[2, 3] - T.A[1, 3] * T.A[2, 1]) + T.C[3, 3] = T.A[1, 1] * T.A[2, 2] - T.A[1, 2] * T.A[2, 1] # transposed inverse of A - for j = 1 : 3, k = 1 : 3 - M[j,k] = T.C[j,k] / T.det + for j in 1:3, k in 1:3 + M[j, k] = T.C[j, k] / T.det end return nothing end diff --git a/src/meshrefinements.jl b/src/meshrefinements.jl index 0a47dd16..fcca7c2d 100644 --- a/src/meshrefinements.jl +++ b/src/meshrefinements.jl @@ -1,6 +1,6 @@ # functions that tell how to split one ElementGeometry into another -const _split_rule_Triangle2D = reshape([1,2,3],3,1) -const _split_rule_Tetrahedron3D = reshape([1,2,3,4],4,1) +const _split_rule_Triangle2D = reshape([1, 2, 3], 3, 1) +const _split_rule_Tetrahedron3D = reshape([1, 2, 3, 4], 4, 1) const _split_rule_Quadrilateral2D_Triangle2D = [1 2 3;1 3 4]' const _split_rule_Hexahedron3D_Tetrahedron3D = [1 2 4 8; 2 3 4 8; 2 7 3 8; 6 8 7 2; 6 5 8 2; 8 5 1 2]' @@ -19,106 +19,106 @@ split rules exist for - Quadrilateral2D into Triangle2D - Hexahedron3D into Tetrahedron3D """ -function split_grid_into(source_grid::ExtendableGrid{T,K}, targetgeometry::Type{<:AbstractElementGeometry}; store_parents = true) where {T,K} - xgrid=ExtendableGrid{T,K}() - xgrid[Coordinates]=source_grid[Coordinates] +function split_grid_into(source_grid::ExtendableGrid{T, K}, targetgeometry::Type{<:AbstractElementGeometry}; store_parents = true) where {T, K} + xgrid = ExtendableGrid{T, K}() + xgrid[Coordinates] = source_grid[Coordinates] oldCellGeometries = source_grid[CellGeometries] oldCellRegions = source_grid[CellRegions] EG = Base.unique(oldCellGeometries) - - split_rules = Array{Array{Int,2},1}(undef,length(EG)) - for j = 1 : length(EG) - split_rules[j] = split_refine_rule(EG[j],targetgeometry) + + split_rules = Array{Array{Int, 2}, 1}(undef, length(EG)) + for j in 1:length(EG) + split_rules[j] = split_refine_rule(EG[j], targetgeometry) end singleEG::Bool = false if length(EG) == 1 singleEG = true end - xCellNodes = zeros(K,0) - xCellRegions = zeros(K,0) - oldCellNodes=source_grid[CellNodes] - nnodes4item::Int = num_targets(oldCellNodes,1) + xCellNodes = zeros(K, 0) + xCellRegions = zeros(K, 0) + oldCellNodes = source_grid[CellNodes] + nnodes4item::Int = num_targets(oldCellNodes, 1) ncells::Int = 0 itemEG = EG[1] iEG::Int = 1 - split_rule::Array{Int,2} = split_rules[iEG] - xCellParents::Array{K,1} = zeros(K,0) - for cell = 1 : num_sources(oldCellNodes) + split_rule::Array{Int, 2} = split_rules[iEG] + xCellParents::Array{K, 1} = zeros(K, 0) + for cell in 1:num_sources(oldCellNodes) if !singleEG - nnodes4item = num_targets(oldCellNodes,cell) + nnodes4item = num_targets(oldCellNodes, cell) itemEG = oldCellGeometries[cell] iEG = findfirst(isequal(itemEG), EG) split_rule = split_rules[iEG] end - for j = 1 : size(split_rule,2), k = 1 : size(split_rule,1) - append!(xCellNodes,oldCellNodes[split_rule[k,j],cell]) - end - for j = 1 : size(split_rule,2) - push!(xCellRegions,oldCellRegions[cell]) + for j in 1:size(split_rule, 2), k in 1:size(split_rule, 1) + append!(xCellNodes, oldCellNodes[split_rule[k, j], cell]) + end + for j in 1:size(split_rule, 2) + push!(xCellRegions, oldCellRegions[cell]) if store_parents - push!(xCellParents,cell) + push!(xCellParents, cell) end end - ncells += size(split_rule,2) + ncells += size(split_rule, 2) end - xCellNodes = reshape(xCellNodes,num_nodes(targetgeometry),ncells) + xCellNodes = reshape(xCellNodes, num_nodes(targetgeometry), ncells) xgrid[CellNodes] = xCellNodes - xgrid[CellGeometries] = VectorOfConstants{ElementGeometries,Int}(targetgeometry,ncells) - xgrid[CoordinateSystem]=source_grid[CoordinateSystem] + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(targetgeometry, ncells) + xgrid[CoordinateSystem] = source_grid[CoordinateSystem] if typeof(oldCellRegions) <: VectorOfConstants - xgrid[CellRegions] = VectorOfConstants{ElementGeometries,Int}(K,ncells) + xgrid[CellRegions] = VectorOfConstants{ElementGeometries, Int}(K, ncells) else xgrid[CellRegions] = xCellRegions end # find new boundary faces (easy in 2D, not so easy in 3D) - xBFaceParents::Array{K,1} = zeros(K,0) + xBFaceParents::Array{K, 1} = zeros(K, 0) if dim_element(targetgeometry) == 2 # BFaces are Edge1D which stay the same - xgrid[BFaceNodes]=source_grid[BFaceNodes] - xgrid[BFaceRegions]=source_grid[BFaceRegions] - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(facetype_of_cellface(targetgeometry,1),num_sources(xgrid[BFaceNodes])) + xgrid[BFaceNodes] = source_grid[BFaceNodes] + xgrid[BFaceRegions] = source_grid[BFaceRegions] + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(facetype_of_cellface(targetgeometry, 1), num_sources(xgrid[BFaceNodes])) if store_parents - xBFaceParents = Array{K,1}(1:num_bfaces(source_grid)) + xBFaceParents = Array{K, 1}(1:num_bfaces(source_grid)) end - elseif dim_element(targetgeometry) == 3 + elseif dim_element(targetgeometry) == 3 # BFaces may be split into different shapes, e.g. from Quadrilateral2D to two Triangle2D # and it is hard to predict how they are split # so we do something lazy here and search for new faces that lie in old bfaces oldBFaceNodes = source_grid[BFaceNodes] oldBFaceRegions = source_grid[BFaceRegions] newFaceNodes = xgrid[FaceNodes] - nfaces = num_sources(newFaceNodes) + nfaces = num_sources(newFaceNodes) nbfaces = num_sources(oldBFaceNodes) - - newBFaceNodes = zeros(K,0) - newBFaceRegions = zeros(K,0) + + newBFaceNodes = zeros(K, 0) + newBFaceRegions = zeros(K, 0) newnbfaces = 0 - nnodes = size(xgrid[Coordinates],2) - flag4item = zeros(Bool,nnodes) + nnodes = size(xgrid[Coordinates], 2) + flag4item = zeros(Bool, nnodes) nodes_per_bface::Int = 0 nodes_per_face::Int = 0 common_nodes::Int = 0 - for bface = 1 : nbfaces + for bface in 1:nbfaces # flag nodes of old bface - nodes_per_bface = num_targets(oldBFaceNodes,bface) - for j = 1 : nodes_per_bface - flag4item[oldBFaceNodes[j,bface]] = true - end - + nodes_per_bface = num_targets(oldBFaceNodes, bface) + for j in 1:nodes_per_bface + flag4item[oldBFaceNodes[j, bface]] = true + end + # find matching faces - for face = 1 : nfaces - nodes_per_face = num_targets(newFaceNodes,face) + for face in 1:nfaces + nodes_per_face = num_targets(newFaceNodes, face) common_nodes = 0 - for k = 1 : nodes_per_face - if flag4item[newFaceNodes[k,face]] == true + for k in 1:nodes_per_face + if flag4item[newFaceNodes[k, face]] == true common_nodes += 1 else - break + break end - end + end if common_nodes == nodes_per_face - append!(newBFaceNodes,newFaceNodes[:,face]) - push!(newBFaceRegions,oldBFaceRegions[bface]) + append!(newBFaceNodes, newFaceNodes[:, face]) + push!(newBFaceRegions, oldBFaceRegions[bface]) newnbfaces += 1 if store_parents push!(xBFaceParents, bface) @@ -127,15 +127,15 @@ function split_grid_into(source_grid::ExtendableGrid{T,K}, targetgeometry::Type{ end # reset flags - for j = 1 : nodes_per_bface - flag4item[oldBFaceNodes[j,bface]] = false - end + for j in 1:nodes_per_bface + flag4item[oldBFaceNodes[j, bface]] = false + end end - newBFaceNodes = reshape(newBFaceNodes,num_nodes(facetype_of_cellface(targetgeometry,1)),newnbfaces) - xgrid[BFaceNodes]=newBFaceNodes - xgrid[BFaceRegions]=newBFaceRegions - xgrid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(facetype_of_cellface(targetgeometry,1),newnbfaces) + newBFaceNodes = reshape(newBFaceNodes, num_nodes(facetype_of_cellface(targetgeometry, 1)), newnbfaces) + xgrid[BFaceNodes] = newBFaceNodes + xgrid[BFaceRegions] = newBFaceRegions + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(facetype_of_cellface(targetgeometry, 1), newnbfaces) end xgrid[ParentGrid] = source_grid @@ -158,22 +158,26 @@ uniform_refine_needcellmidpoints(::Type{<:AbstractElementGeometry}) = false const _uniform_rule_Edge1D = [1 3; 3 2]' const _uniform_rule_Triangle2D = [1 4 6; 4 2 5; 6 5 3; 5 6 4]' const _uniform_rule_Quadrilateral2D = [1 5 9 8; 2 6 9 5; 3 7 9 6; 4 8 9 7]' -const _uniform_rule_Tetrahedron3D = [ 1 5 6 7; - 2 5 9 8; - 3 10 6 8; - 4 10 9 7; - 10 5 8 9; - 5 10 7 9; - 5 10 8 6; - 10 5 7 6]' -const _uniform_rule_Hexahedron3D = [ 1 9 21 12 13 22 27 25; - 9 2 10 21 22 14 23 27; - 12 21 11 4 25 27 24 16; - 21 10 3 11 27 23 15 24; - 13 22 27 25 5 17 26 20; - 22 14 23 27 17 6 18 26; - 25 27 24 16 20 26 19 8; - 27 23 15 24 26 18 7 19]' +const _uniform_rule_Tetrahedron3D = [ + 1 5 6 7; + 2 5 9 8; + 3 10 6 8; + 4 10 9 7; + 10 5 8 9; + 5 10 7 9; + 5 10 8 6; + 10 5 7 6 +]' +const _uniform_rule_Hexahedron3D = [ + 1 9 21 12 13 22 27 25; + 9 2 10 21 22 14 23 27; + 12 21 11 4 25 27 24 16; + 21 10 3 11 27 23 15 24; + 13 22 27 25 5 17 26 20; + 22 14 23 27 17 6 18 26; + 25 27 24 16 20 26 19 8; + 27 23 15 24 26 18 7 19 +]' uniform_refine_rule(::Type{<:Edge1D}) = _uniform_rule_Edge1D uniform_refine_needcellmidpoints(::Type{<:Edge1D}) = true @@ -213,11 +217,11 @@ if multiple geometries are in the mesh uniform refinement will only work if all refinement rules refine faces and edges (in 3D) equally (so no hanging nodes are created) """ -function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) where {T,K} +function uniform_refine(source_grid::ExtendableGrid{T, K}; store_parents = true) where {T, K} # @logmsg MoreInfo "Uniform refinement of $(num_sources(source_grid[CellNodes])) cells" - - xgrid = ExtendableGrid{T,K}() - xgrid[CoordinateSystem]=source_grid[CoordinateSystem] + + xgrid = ExtendableGrid{T, K}() + xgrid[CoordinateSystem] = source_grid[CoordinateSystem] # unpack stuff from source grid oldCoordinates = source_grid[Coordinates] @@ -232,18 +236,18 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) # get dimension of CellGeometries # currently it is assumed to be the same for all cells - dim::Int = dim_element(EG[1]) - - refine_rules::Array{Array{Int,2},1} = Array{Array{Int,2},1}(undef,length(EG)) + dim::Int = dim_element(EG[1]) + + refine_rules::Array{Array{Int, 2}, 1} = Array{Array{Int, 2}, 1}(undef, length(EG)) need_facemidpoints = uniform_refine_needfacemidpoints(EG[1]) - for j = 1 : length(EG) + for j in 1:length(EG) refine_rules[j] = uniform_refine_rule(EG[j]) @assert uniform_refine_needfacemidpoints(EG[j]) == need_facemidpoints end xCellNodes = VariableTargetAdjacency(K) - xCellGeometries = Array{ElementGeometries,1}(undef,0) - xCellRegions = zeros(K,0) + xCellGeometries = Array{ElementGeometries, 1}(undef, 0) + xCellRegions = zeros(K, 0) oldCellNodes::Adjacency{K} = source_grid[CellNodes] oldCellFaces::Adjacency{K} = source_grid[CellFaces] nfaces::Int = 0 @@ -252,7 +256,7 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) nfaces = num_sources(oldFaceNodes) end nedges::Int = 0 - if dim > 2 + if dim > 2 oldEdgeNodes::Adjacency{K} = source_grid[EdgeNodes] nedges = num_sources(oldEdgeNodes) oldCellEdges::Adjacency{K} = source_grid[CellEdges] @@ -266,64 +270,64 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) newvertices = nfaces elseif dim == 3 # in 2D each face and edge is halved newvertices = nedges - if need_facemidpoints + if need_facemidpoints newvertices += nfaces end end - oldvertices::Int = size(oldCoordinates,2) + oldvertices::Int = size(oldCoordinates, 2) newnode::Int = oldvertices + newvertices # additionally cell midpoints are needed for some refinements - for cell = 1 : num_sources(oldCellNodes) + for cell in 1:num_sources(oldCellNodes) itemEG = oldCellGeometries[cell] if uniform_refine_needcellmidpoints(itemEG) == true newvertices += 1 - end + end end - xCoordinates::Array{T,2} = zeros(T,size(oldCoordinates,1),oldvertices+newvertices) - @views xCoordinates[:,1:oldvertices] = oldCoordinates + xCoordinates::Array{T, 2} = zeros(T, size(oldCoordinates, 1), oldvertices + newvertices) + @views xCoordinates[:, 1:oldvertices] = oldCoordinates + - - newvertex::Array{T,1} = zeros(T,size(xCoordinates,1)) + newvertex::Array{T, 1} = zeros(T, size(xCoordinates, 1)) nnodes4item::Int = 0 if dim > 2 # add edge midpoints to Coordinates - for edge = 1 : nedges - nnodes4item = num_targets(oldEdgeNodes,edge) - fill!(newvertex,0.0) - for k = 1 : nnodes4item, d = 1 : size(xCoordinates,1) - newvertex[d] += xCoordinates[d,oldEdgeNodes[k,edge]] - end + for edge in 1:nedges + nnodes4item = num_targets(oldEdgeNodes, edge) + fill!(newvertex, 0.0) + for k in 1:nnodes4item, d in 1:size(xCoordinates, 1) + newvertex[d] += xCoordinates[d, oldEdgeNodes[k, edge]] + end newvertex ./= nnodes4item - for d = 1 : size(xCoordinates,1) - xCoordinates[d,oldvertices+edge] = newvertex[d] + for d in 1:size(xCoordinates, 1) + xCoordinates[d, oldvertices + edge] = newvertex[d] end - end + end end - if dim > 1 && need_facemidpoints + if dim > 1 && need_facemidpoints # add face midpoints to Coordinates - for face = 1 : nfaces - nnodes4item = num_targets(oldFaceNodes,face) - fill!(newvertex,0.0) - for k = 1 : nnodes4item, d = 1 : size(xCoordinates,1) - newvertex[d] += xCoordinates[d,oldFaceNodes[k,face]] - end + for face in 1:nfaces + nnodes4item = num_targets(oldFaceNodes, face) + fill!(newvertex, 0.0) + for k in 1:nnodes4item, d in 1:size(xCoordinates, 1) + newvertex[d] += xCoordinates[d, oldFaceNodes[k, face]] + end newvertex ./= nnodes4item - for d = 1 : size(xCoordinates,1) - xCoordinates[d,oldvertices+nedges+face] = newvertex[d] + for d in 1:size(xCoordinates, 1) + xCoordinates[d, oldvertices + nedges + face] = newvertex[d] end - end + end end - + # determine new cells nnodes4item = num_nodes(itemEG) nfaces4item::Int = num_faces(itemEG) nedges4item::Int = num_edges(itemEG) ncells::Int = 0 iEG::Int = 1 - subitemnodes::Array{K,1} = zeros(K,max_num_targets_per_source(oldCellNodes)+max_num_targets_per_source(oldCellFaces)+1) + subitemnodes::Array{K, 1} = zeros(K, max_num_targets_per_source(oldCellNodes) + max_num_targets_per_source(oldCellFaces) + 1) m::Int = 0 - xCellParents::Array{K,1} = zeros(K,0) - refine_rule::Array{Int,2} = refine_rules[iEG] - for cell = 1 : num_sources(oldCellNodes) + xCellParents::Array{K, 1} = zeros(K, 0) + refine_rule::Array{Int, 2} = refine_rules[iEG] + for cell in 1:num_sources(oldCellNodes) if !singleEG itemEG = oldCellGeometries[cell] nnodes4item = num_nodes(itemEG) @@ -335,69 +339,69 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) if uniform_refine_needcellmidpoints(itemEG) == true # add cell midpoint to Coordinates newnode += 1 - fill!(newvertex,0.0) - for k = 1 : nnodes4item, d = 1 : size(xCoordinates,1) - newvertex[d] += xCoordinates[d,oldCellNodes[k,cell]] - end + fill!(newvertex, 0.0) + for k in 1:nnodes4item, d in 1:size(xCoordinates, 1) + newvertex[d] += xCoordinates[d, oldCellNodes[k, cell]] + end newvertex ./= nnodes4item - for d = 1 : size(xCoordinates,1) - xCoordinates[d,newnode] = newvertex[d] + for d in 1:size(xCoordinates, 1) + xCoordinates[d, newnode] = newvertex[d] end end - for j = 1 : size(refine_rule,2) - for k = 1 : size(refine_rule,1) - m = refine_rule[k,j] + for j in 1:size(refine_rule, 2) + for k in 1:size(refine_rule, 1) + m = refine_rule[k, j] if dim == 1 - if m <= nnodes4item - subitemnodes[k] = oldCellNodes[m,cell] + if m <= nnodes4item + subitemnodes[k] = oldCellNodes[m, cell] else subitemnodes[k] = newnode end elseif dim == 2 - if m <= nnodes4item - subitemnodes[k] = oldCellNodes[m,cell] + if m <= nnodes4item + subitemnodes[k] = oldCellNodes[m, cell] elseif m <= nnodes4item + nfaces4item - subitemnodes[k] = oldvertices + oldCellFaces[m-nnodes4item,cell] + subitemnodes[k] = oldvertices + oldCellFaces[m - nnodes4item, cell] else subitemnodes[k] = newnode - end + end elseif dim == 3 - if m <= nnodes4item - subitemnodes[k] = oldCellNodes[m,cell] + if m <= nnodes4item + subitemnodes[k] = oldCellNodes[m, cell] elseif m <= nnodes4item + nedges4item - subitemnodes[k] = oldvertices + oldCellEdges[m-nnodes4item,cell] + subitemnodes[k] = oldvertices + oldCellEdges[m - nnodes4item, cell] elseif m <= nnodes4item + nedges4item + nfaces4item - subitemnodes[k] = oldvertices + nedges + oldCellFaces[m-nnodes4item-nedges4item,cell] + subitemnodes[k] = oldvertices + nedges + oldCellFaces[m - nnodes4item - nedges4item, cell] else subitemnodes[k] = newnode - end + end end - end - append!(xCellNodes,view(subitemnodes,1:size(refine_rule,1))) + end + append!(xCellNodes, view(subitemnodes, 1:size(refine_rule, 1))) push!(xCellRegions, oldCellRegions[cell]) if !singleEG - push!(xCellGeometries,itemEG) + push!(xCellGeometries, itemEG) end if store_parents - push!(xCellParents,cell) + push!(xCellParents, cell) end - end - ncells += size(refine_rule,2) + end + ncells += size(refine_rule, 2) end # assign new cells to grid xgrid[Coordinates] = xCoordinates - + if singleEG - nnodes4item = size(oldCellNodes,1) - xgrid[CellNodes] = reshape(xCellNodes.colentries,nnodes4item,num_sources(xCellNodes)) - xgrid[CellGeometries] = VectorOfConstants{ElementGeometries,Int}(EG[1],ncells) + nnodes4item = size(oldCellNodes, 1) + xgrid[CellNodes] = reshape(xCellNodes.colentries, nnodes4item, num_sources(xCellNodes)) + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(EG[1], ncells) else xgrid[CellNodes] = xCellNodes xgrid[CellGeometries] = xCellGeometries end if typeof(oldCellRegions) <: VectorOfConstants - xgrid[CellRegions] = VectorOfConstants{K}(1,ncells) + xgrid[CellRegions] = VectorOfConstants{K}(1, ncells) else xgrid[CellRegions] = xCellRegions end @@ -405,28 +409,28 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) # determine new boundary faces oldBFaceNodes::Adjacency{K} = source_grid[BFaceNodes] - oldBFaceFaces::Array{K,1} = source_grid[BFaceFaces] + oldBFaceFaces::Array{K, 1} = source_grid[BFaceFaces] oldBFaceRegions = source_grid[BFaceRegions] oldBFaceGeometries = source_grid[BFaceGeometries] - xBFaceParents::Array{K,1} = zeros(K,0) - + xBFaceParents::Array{K, 1} = zeros(K, 0) + if dim == 1 xgrid[BFaceNodes] = oldBFaceNodes xgrid[BFaceRegions] = oldBFaceRegions xgrid[BFaceGeometries] = oldBFaceGeometries if store_parents - xBFaceParents = Array{K,1}(1:num_bfaces(source_grid)) + xBFaceParents = Array{K, 1}(1:num_bfaces(source_grid)) end else - xBFaceRegions = zeros(K,0) + xBFaceRegions = zeros(K, 0) BFEG = Base.unique(oldBFaceGeometries) singleEG = length(BFEG) == 1 if singleEG == false - xBFaceGeometries = Array{ElementGeometries,1}(undef,0) + xBFaceGeometries = Array{ElementGeometries, 1}(undef, 0) end nbfaces = num_sources(oldBFaceNodes) - if dim == 2 || typeof(oldBFaceNodes) == Array{K,2} - xBFaceNodes = zeros(K,0) + if dim == 2 || typeof(oldBFaceNodes) == Array{K, 2} + xBFaceNodes = zeros(K, 0) else xBFaceNodes = VariableTargetAdjacency(K) end @@ -439,8 +443,8 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) EG = Base.unique(oldBFaceGeometries) - refine_rules = Array{Array{Int,2},1}(undef,length(EG)) - for j = 1 : length(EG) + refine_rules = Array{Array{Int, 2}, 1}(undef, length(EG)) + for j in 1:length(EG) refine_rules[j] = uniform_refine_rule(EG[j]) end @@ -451,8 +455,8 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) n2::Int = 0 ne4n2::Int = 0 newnbfaces::Int = 0 - bface_enum_rule::Array{Int,2} = local_cellfacenodes(EG[1]) - for bface = 1 : nbfaces + bface_enum_rule::Array{Int, 2} = local_cellfacenodes(EG[1]) + for bface in 1:nbfaces face = oldBFaceFaces[bface] itemEG = oldBFaceGeometries[bface] nnodes4item = num_nodes(itemEG) @@ -461,61 +465,61 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) iEG = findfirst(isequal(itemEG), EG) bface_enum_rule = local_cellfacenodes(itemEG) - for j = 1 : size(refine_rules[iEG],2) - for k = 1 : size(refine_rules[iEG],1) - m = refine_rules[iEG][k,j] + for j in 1:size(refine_rules[iEG], 2) + for k in 1:size(refine_rules[iEG], 1) + m = refine_rules[iEG][k, j] if dim == 2 - if m <= nnodes4item - subitemnodes[k] = oldBFaceNodes[m,bface] + if m <= nnodes4item + subitemnodes[k] = oldBFaceNodes[m, bface] else subitemnodes[k] = oldvertices + face - end + end elseif dim == 3 - if m <= nnodes4item - subitemnodes[k] = oldBFaceNodes[m,bface] + if m <= nnodes4item + subitemnodes[k] = oldBFaceNodes[m, bface] elseif m <= nnodes4item + nfaces4item - edge = m-nnodes4item # local number + edge = m - nnodes4item # local number # find global edge number - n1 = oldBFaceNodes[bface_enum_rule[1,edge],bface] - n2 = oldBFaceNodes[bface_enum_rule[2,edge],bface] - ne4n1 = num_targets(xNodeEdges,n1) - ne4n2 = num_targets(xNodeEdges,n2) - for ge = 1 : ne4n1, gf = 1 : ne4n2 - if xNodeEdges[ge,n1] == xNodeEdges[gf,n2] - edge = xNodeEdges[gf,n2] + n1 = oldBFaceNodes[bface_enum_rule[1, edge], bface] + n2 = oldBFaceNodes[bface_enum_rule[2, edge], bface] + ne4n1 = num_targets(xNodeEdges, n1) + ne4n2 = num_targets(xNodeEdges, n2) + for ge in 1:ne4n1, gf in 1:ne4n2 + if xNodeEdges[ge, n1] == xNodeEdges[gf, n2] + edge = xNodeEdges[gf, n2] break end end - # edge = intersect(xNodeEdges[:,oldBFaceNodes[bface_enum_rule[1,edge],bface]],xNodeEdges[:,oldBFaceNodes[bface_enum_rule[2,edge],bface]])[1] + # edge = intersect(xNodeEdges[:,oldBFaceNodes[bface_enum_rule[1,edge],bface]],xNodeEdges[:,oldBFaceNodes[bface_enum_rule[2,edge],bface]])[1] subitemnodes[k] = oldvertices + edge else subitemnodes[k] = oldvertices + nedges + face - end + end end end - append!(xBFaceNodes,view(subitemnodes,1:size(refine_rules[iEG],1))) + append!(xBFaceNodes, view(subitemnodes, 1:size(refine_rules[iEG], 1))) if !singleEG - push!(xBFaceGeometries,itemEG) + push!(xBFaceGeometries, itemEG) end - push!(xBFaceRegions,oldBFaceRegions[bface]) + push!(xBFaceRegions, oldBFaceRegions[bface]) if store_parents push!(xBFaceParents, bface) end newnbfaces += 1 - end + end end - if dim == 2 || typeof(oldBFaceNodes) == Array{K,2} - xgrid[BFaceNodes] = reshape(xBFaceNodes,(size(oldBFaceNodes,1),newnbfaces)) + if dim == 2 || typeof(oldBFaceNodes) == Array{K, 2} + xgrid[BFaceNodes] = reshape(xBFaceNodes, (size(oldBFaceNodes, 1), newnbfaces)) else xgrid[BFaceNodes] = xBFaceNodes end xgrid[BFaceRegions] = xBFaceRegions if singleEG - xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries,Int}(BFEG[1],newnbfaces) + xgrid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(BFEG[1], newnbfaces) else xgrid[BFaceGeometries] = xBFaceGeometries end - end + end if store_parents xgrid[CellParents] = xCellParents @@ -529,10 +533,10 @@ function uniform_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) end -function uniform_refine(source_grid::ExtendableGrid{T,K}, nrefinements::Int; store_parents = false) where {T,K} +function uniform_refine(source_grid::ExtendableGrid{T, K}, nrefinements::Int; store_parents = false) where {T, K} xgrid = source_grid parents = 1:num_sources(xgrid[CellNodes]) - for j=1:nrefinements + for j in 1:nrefinements xgrid = uniform_refine(xgrid; store_parents = store_parents) if store_parents parents = parents[xgrid[CellParents]] @@ -564,98 +568,98 @@ barycentric refinement is available for these ElementGeometries - Quadrilateral2D (first split into Triangle2D) - Triangle2D """ -function barycentric_refine(source_grid::ExtendableGrid{T,K}; store_parents = true) where {T,K} +function barycentric_refine(source_grid::ExtendableGrid{T, K}; store_parents = true) where {T, K} # @logmsg MoreInfo "Barycentric refinement of $(num_sources(source_grid[CellNodes])) cells" - + # split first into triangles oldCellGeometries = source_grid[CellGeometries] dim = dim_element(oldCellGeometries[1]) itemEG = dim == 2 ? Triangle2D : Tetrahedron3D source_grid = split_grid_into(source_grid, itemEG) - xgrid = ExtendableGrid{T,K}() + xgrid = ExtendableGrid{T, K}() oldCoordinates = source_grid[Coordinates] oldCellGeometries = source_grid[CellGeometries] oldCellRegions = source_grid[CellRegions] EG = Base.unique(oldCellGeometries) - - refine_rules = Array{Array{Int,2},1}(undef,length(EG)) - for j = 1 : length(EG) + + refine_rules = Array{Array{Int, 2}, 1}(undef, length(EG)) + for j in 1:length(EG) refine_rules[j] = barycentric_refine_rule(EG[j]) end xCellNodes::Adjacency = VariableTargetAdjacency(K) - xCellGeometries = Array{ElementGeometries,1}(undef,0) - xCellRegions = zeros(K,0) + xCellGeometries = Array{ElementGeometries, 1}(undef, 0) + xCellRegions = zeros(K, 0) oldCellNodes = source_grid[CellNodes] oldCellFaces = source_grid[CellFaces] # determine number of new vertices newvertices = num_sources(oldCellNodes) - oldvertices = size(oldCoordinates,2) - xCoordinates = zeros(T,size(oldCoordinates,1),oldvertices+newvertices) - @views xCoordinates[:,1:oldvertices] = oldCoordinates + oldvertices = size(oldCoordinates, 2) + xCoordinates = zeros(T, size(oldCoordinates, 1), oldvertices + newvertices) + @views xCoordinates[:, 1:oldvertices] = oldCoordinates # determine new cells - nnodes4item::Int = num_targets(oldCellNodes,1) + nnodes4item::Int = num_targets(oldCellNodes, 1) ncells::Int = 0 iEG::Int = 1 - subitemnodes::Array{K,1} = zeros(K,max_num_targets_per_source(oldCellNodes)+max_num_targets_per_source(oldCellFaces)+1) + subitemnodes::Array{K, 1} = zeros(K, max_num_targets_per_source(oldCellNodes) + max_num_targets_per_source(oldCellFaces) + 1) newnode::Int = oldvertices m::Int = 0 - newvertex::Array{T,1} = zeros(T,size(xCoordinates,1)) - refine_rule::Array{Int,2} = refine_rules[iEG] - xCellParents::Array{K,1} = zeros(K,0) - for cell = 1 : num_sources(oldCellNodes) - + newvertex::Array{T, 1} = zeros(T, size(xCoordinates, 1)) + refine_rule::Array{Int, 2} = refine_rules[iEG] + xCellParents::Array{K, 1} = zeros(K, 0) + for cell in 1:num_sources(oldCellNodes) + # add cell midpoint to Coordinates newnode += 1 - fill!(newvertex,0.0) - for k = 1 : nnodes4item, d = 1 : size(xCoordinates,1) - newvertex[d] += xCoordinates[d,oldCellNodes[k,cell]] - end + fill!(newvertex, 0.0) + for k in 1:nnodes4item, d in 1:size(xCoordinates, 1) + newvertex[d] += xCoordinates[d, oldCellNodes[k, cell]] + end newvertex ./= nnodes4item - for d = 1 : size(xCoordinates,1) - xCoordinates[d,newnode] = newvertex[d] + for d in 1:size(xCoordinates, 1) + xCoordinates[d, newnode] = newvertex[d] end - for j = 1 : size(refine_rule,2) - for k = 1 : size(refine_rule,1) - m = refine_rules[iEG][k,j] - if m <= nnodes4item - subitemnodes[k] = oldCellNodes[m,cell] + for j in 1:size(refine_rule, 2) + for k in 1:size(refine_rule, 1) + m = refine_rules[iEG][k, j] + if m <= nnodes4item + subitemnodes[k] = oldCellNodes[m, cell] else subitemnodes[k] = newnode - end - end - append!(xCellNodes,view(subitemnodes,1:size(refine_rule,1))) + end + end + append!(xCellNodes, view(subitemnodes, 1:size(refine_rule, 1))) push!(xCellRegions, oldCellRegions[cell]) if store_parents - push!(xCellParents,cell) + push!(xCellParents, cell) end - end - ncells += size(refine_rule,2) + end + ncells += size(refine_rule, 2) end xgrid[Coordinates] = xCoordinates - nnodes4item = size(oldCellNodes,1) - xgrid[CellNodes] = reshape(xCellNodes.colentries,nnodes4item,num_sources(xCellNodes)) - xgrid[CellGeometries] = VectorOfConstants{ElementGeometries,Int}(EG[1],num_sources(xCellNodes)) + nnodes4item = size(oldCellNodes, 1) + xgrid[CellNodes] = reshape(xCellNodes.colentries, nnodes4item, num_sources(xCellNodes)) + xgrid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(EG[1], num_sources(xCellNodes)) if typeof(oldCellRegions) <: VectorOfConstants - xgrid[CellRegions] = VectorOfConstants{K}(1,num_sources(xCellNodes)) + xgrid[CellRegions] = VectorOfConstants{K}(1, num_sources(xCellNodes)) else xgrid[CellRegions] = xCellRegions end - xgrid[BFaceNodes]=source_grid[BFaceNodes] - xgrid[BFaceRegions]=source_grid[BFaceRegions] - xgrid[BFaceGeometries]=source_grid[BFaceGeometries] - xgrid[CoordinateSystem]=source_grid[CoordinateSystem] + xgrid[BFaceNodes] = source_grid[BFaceNodes] + xgrid[BFaceRegions] = source_grid[BFaceRegions] + xgrid[BFaceGeometries] = source_grid[BFaceGeometries] + xgrid[CoordinateSystem] = source_grid[CoordinateSystem] xgrid[ParentGrid] = source_grid xgrid[ParentGridRelation] = RefinedGrid if store_parents xgrid[CellParents] = xCellParents - xgrid[BFaceParents] = Array{K,1}(1:num_bfaces(source_grid)) + xgrid[BFaceParents] = Array{K, 1}(1:num_bfaces(source_grid)) end return xgrid end diff --git a/src/more.jl b/src/more.jl index ae874c0a..db1150dd 100644 --- a/src/more.jl +++ b/src/more.jl @@ -55,24 +55,24 @@ Prepare edge adjacencies (celledges, edgecells, edgenodes) Currently depends on ExtendableSparse, we may want to remove this adjacency. -""" +""" function prepare_edges!(grid::ExtendableGrid) - Ti=eltype(grid[CellNodes]) - cellnodes=grid[CellNodes] - geom=grid[CellGeometries][1] + Ti = eltype(grid[CellNodes]) + cellnodes = grid[CellNodes] + geom = grid[CellGeometries][1] # Create cell-node incidence matrix - cellnode_adj=asparse(cellnodes) - + cellnode_adj = asparse(cellnodes) + # Create node-node incidence matrix for neighboring - # nodes. - nodenode_adj=cellnode_adj*transpose(cellnode_adj) + # nodes. + nodenode_adj = cellnode_adj * transpose(cellnode_adj) # To get unique edges, we set the lower triangular part # including the diagonal to 0 - for icol=1:length(nodenode_adj.colptr)-1 - for irow=nodenode_adj.colptr[icol]:nodenode_adj.colptr[icol+1]-1 - if nodenode_adj.rowval[irow]>=icol - nodenode_adj.nzval[irow]=0 + for icol in 1:(length(nodenode_adj.colptr) - 1) + for irow in nodenode_adj.colptr[icol]:(nodenode_adj.colptr[icol + 1] - 1) + if nodenode_adj.rowval[irow] >= icol + nodenode_adj.nzval[irow] = 0 end end end @@ -80,72 +80,72 @@ function prepare_edges!(grid::ExtendableGrid) # Now we know the number of edges and - nedges=length(nodenode_adj.nzval) + nedges = length(nodenode_adj.nzval) + - - if dim_space(grid)==2 + if dim_space(grid) == 2 # Let us do the Euler test (assuming no holes in the domain) - v=num_nodes(grid) - e=nedges - f=num_cells(grid)+1 - @assert v-e+f==2 + v = num_nodes(grid) + e = nedges + f = num_cells(grid) + 1 + @assert v - e + f == 2 end - if dim_space(grid)==1 - @assert nedges==num_cells(grid) + if dim_space(grid) == 1 + @assert nedges == num_cells(grid) end - + # Calculate edge nodes and celledges - edgenodes=zeros(Ti,2,nedges) - celledges=zeros(Ti,num_edges(geom),num_cells(grid)) - cen=local_celledgenodes(geom) - - for icell=1:num_cells(grid) - for iedge=1:num_edges(geom) - n1=cellnodes[cen[1,iedge],icell] - n2=cellnodes[cen[2,iedge],icell] + edgenodes = zeros(Ti, 2, nedges) + celledges = zeros(Ti, num_edges(geom), num_cells(grid)) + cen = local_celledgenodes(geom) + + for icell in 1:num_cells(grid) + for iedge in 1:num_edges(geom) + n1 = cellnodes[cen[1, iedge], icell] + n2 = cellnodes[cen[2, iedge], icell] # We need to look in nodenod_adj for upper triangular part entries # therefore, we need to swap accordingly before looking - if (n1 i==dim, abc.nzval))) + + abc = asparse(cn)' * abn + abcx = dropzeros!( + SparseMatrixCSC( + abc.m, abc.n, abc.colptr, abc.rowval, + map(i -> i == dim, abc.nzval) + ) + ) grid[BFaceCells] = VariableTargetAdjacency(abcx) - true + return true end function prepare_bedges!(grid) - Ti = eltype(grid[CellNodes]) - bgeom = grid[BFaceGeometries][1] - bfacenodes = grid[BFaceNodes] - + Ti = eltype(grid[CellNodes]) + bgeom = grid[BFaceGeometries][1] + bfacenodes = grid[BFaceNodes] + # Create bface-node incidence matrix - bfacenode_adj = asparse(bfacenodes) - nodenode_adj = bfacenode_adj*transpose(bfacenode_adj) + bfacenode_adj = asparse(bfacenodes) + nodenode_adj = bfacenode_adj * transpose(bfacenode_adj) # To get unique edges, we set the lower triangular part # including the diagonal to 0 - for icol=1:length(nodenode_adj.colptr)-1 - for irow=nodenode_adj.colptr[icol]:nodenode_adj.colptr[icol+1]-1 - if nodenode_adj.rowval[irow]>=icol - nodenode_adj.nzval[irow]=0 + for icol in 1:(length(nodenode_adj.colptr) - 1) + for irow in nodenode_adj.colptr[icol]:(nodenode_adj.colptr[icol + 1] - 1) + if nodenode_adj.rowval[irow] >= icol + nodenode_adj.nzval[irow] = 0 end end end dropzeros!(nodenode_adj) # Now we know the number of bedges and - nbedges=length(nodenode_adj.nzval) + nbedges = length(nodenode_adj.nzval) + - - bedgenodes = zeros(Ti, 2, nbedges) - bfaceedges = zeros(Ti, num_edges(bgeom), num_bfaces(grid)) + bedgenodes = zeros(Ti, 2, nbedges) + bfaceedges = zeros(Ti, num_edges(bgeom), num_bfaces(grid)) - cen = local_celledgenodes(bgeom) - for ibface=1:num_bfaces(grid) - for ibedge=1:num_edges(bgeom) - n1 = bfacenodes[cen[1,ibedge], ibface] - n2 = bfacenodes[cen[2,ibedge], ibface] + cen = local_celledgenodes(bgeom) + for ibface in 1:num_bfaces(grid) + for ibedge in 1:num_edges(bgeom) + n1 = bfacenodes[cen[1, ibedge], ibface] + n2 = bfacenodes[cen[2, ibedge], ibface] # We need to look in nodenod_adj for upper triangular part entries # therefore, we need to swap accordingly before looking - if (n1length(partition_cells(grid,p)), pcolor_partitions(grid, col)) for col in pcolors(grid)] + return [sum(p -> length(partition_cells(grid, p)), pcolor_partitions(grid, col)) for col in pcolors(grid)] end """ @@ -246,7 +246,7 @@ Return a vector containing the number of nodes for each of the partitions of the grid partitioning. """ function num_nodes_per_partition(grid) - @show [length(partition_nodes(grid,ipart)) for ipart=1:num_partitions(grid)] + return @show [length(partition_nodes(grid, ipart)) for ipart in 1:num_partitions(grid)] end """ @@ -256,7 +256,7 @@ Return a vector containing the number of nodes for each of the partitions of the grid partitioning. """ function num_edges_per_partition(grid) - @show [length(partition_edges(grid,ipart)) for ipart=1:num_partitions(grid)] + return @show [length(partition_edges(grid, ipart)) for ipart in 1:num_partitions(grid)] end """ @@ -275,21 +275,21 @@ for parallel sparse matrix multiplication and ILU preconditioning - Check if no node belongs to two node partitions of the same color at once - Check if no node is a neighbor of nodes from two node partitions of the same color """ -function check_partitioning(grid::ExtendableGrid{Tc, Ti}; verbose=true, cellpartonly=false) where {Tc, Ti} - cn=grid[CellNodes] - ok=true - partnodes=Vector{Tc}[unique(vec(cn[:,partition_cells(grid,ipart)])) for ipart=1:num_partitions(grid)] +function check_partitioning(grid::ExtendableGrid{Tc, Ti}; verbose = true, cellpartonly = false) where {Tc, Ti} + cn = grid[CellNodes] + ok = true + partnodes = Vector{Tc}[unique(vec(cn[:, partition_cells(grid, ipart)])) for ipart in 1:num_partitions(grid)] if verbose @info "Check if every node belongs to one of the cell partitions..." end - if length(intersect(vcat(partnodes...), 1:num_nodes(grid))) !=num_nodes(grid) + if length(intersect(vcat(partnodes...), 1:num_nodes(grid))) != num_nodes(grid) if verbose @warn "Not all nodes in one of the partitions" end - ok=false + ok = false end - + if verbose @info "Check if no node belongs to two cell partitions of the same color at once..." end @@ -297,36 +297,36 @@ function check_partitioning(grid::ExtendableGrid{Tc, Ti}; verbose=true, cellpart for ipart in pcolor_partitions(grid, color) for jpart in pcolor_partitions(grid, color) if ipart != jpart - is=intersect(partnodes[ipart],partnodes[jpart]) - if length(is)>0 + is = intersect(partnodes[ipart], partnodes[jpart]) + if length(is) > 0 if verbose @warn "Found nodes belonging to cell partitions $ipart,$jpart of color $color" end - ok=false + ok = false end end end end end - + if cellpartonly return ok end if verbose @info "Check if no node belongs to two node partitions of the same color at once..." end - pnodes=grid[PartitionNodes] - partnodes=Vector{Tc}[collect(pnodes[ipart]:pnodes[ipart+1]-1) for ipart=1:num_partitions(grid)] + pnodes = grid[PartitionNodes] + partnodes = Vector{Tc}[collect(pnodes[ipart]:(pnodes[ipart + 1] - 1)) for ipart in 1:num_partitions(grid)] for color in pcolors(grid) for ipart in pcolor_partitions(grid, color) for jpart in pcolor_partitions(grid, color) if ipart != jpart - is=intersect(partnodes[ipart],partnodes[jpart]) - if length(is)>0 + is = intersect(partnodes[ipart], partnodes[jpart]) + if length(is) > 0 if verbose @warn "Found nodes belonging to both node partitions $ipart,$jpart of color $color" end - ok=false + ok = false end end end @@ -337,23 +337,23 @@ function check_partitioning(grid::ExtendableGrid{Tc, Ti}; verbose=true, cellpart @info "Check if no node is a neighbor of nodes from two node partitions of the same color..." end nc = asparse(atranspose(cn)) - rv=SparseArrays.getrowval(nc) - mark=zeros(Int, num_nodes(grid)) + rv = SparseArrays.getrowval(nc) + mark = zeros(Int, num_nodes(grid)) for color in pcolors(grid) - mark.=0 + mark .= 0 for ipart in pcolor_partitions(grid, color) - for inode in pnodes[ipart]:pnodes[ipart+1]-1 - for j in nzrange(nc,inode) - icell=rv[j] - for k=1:size(cn,1) - mpart=mark[cn[k,icell]] - if mpart!=0 && mpart !=ipart - if verbose - @warn "Found node $(cn[k,icell]) neighboring to both partitions $ipart,$mpart of color $color" + for inode in pnodes[ipart]:(pnodes[ipart + 1] - 1) + for j in nzrange(nc, inode) + icell = rv[j] + for k in 1:size(cn, 1) + mpart = mark[cn[k, icell]] + if mpart != 0 && mpart != ipart + if verbose + @warn "Found node $(cn[k, icell]) neighboring to both partitions $ipart,$mpart of color $color" end - ok=false + ok = false end - mark[cn[k,icell]]=ipart + mark[cn[k, icell]] = ipart end end end @@ -362,7 +362,7 @@ function check_partitioning(grid::ExtendableGrid{Tc, Ti}; verbose=true, cellpart if verbose && !ok throw(ErrorException("Inconsistency in grid partitioning. Errors in assembly and matrix-vector multiplication may occur.")) end - ok + return ok end """ @@ -371,18 +371,18 @@ end (internal) Create neighbourhood graph for given partitioning. """ -function partgraph(cellpartitions,ncellpartitions,cellcelladj) - gr=Graphs.Graph(ncellpartitions) - for ic = 1:size(cellcelladj,2) +function partgraph(cellpartitions, ncellpartitions, cellcelladj) + gr = Graphs.Graph(ncellpartitions) + for ic in 1:size(cellcelladj, 2) ir = cellpartitions[ic] - for k = cellcelladj.colptr[ic]:cellcelladj.colptr[ic+1]-1 + for k in cellcelladj.colptr[ic]:(cellcelladj.colptr[ic + 1] - 1) jr = cellpartitions[cellcelladj.rowval[k]] if ir != jr - Graphs.add_edge!(gr,ir,jr) + Graphs.add_edge!(gr, ir, jr) end end end - gr + return gr end @@ -394,7 +394,6 @@ Abstract super type for partitioning algorithms abstract type AbstractPartitioningAlgorithm end - """ $(TYPEDEF) @@ -404,7 +403,6 @@ Base.@kwdef struct TrivialPartitioning <: AbstractPartitioningAlgorithm end - """ $(TYPEDEF) @@ -420,7 +418,7 @@ $(TYPEDFIELDS) """ Base.@kwdef struct PlainMetisPartitioning <: AbstractPartitioningAlgorithm "Number of partitions (default: 20)" - npart::Int=20 + npart::Int = 20 end @@ -443,17 +441,16 @@ $(TYPEDFIELDS) """ Base.@kwdef struct RecursiveMetisPartitioning <: AbstractPartitioningAlgorithm "Number of color 1 partitions (default: 4)" - npart::Int=4 + npart::Int = 4 "Recursion depth (default: 1)" - maxdepth::Int=1 + maxdepth::Int = 1 "Separator width (default: 2)" - separatorwidth::Int=2 + separatorwidth::Int = 2 end - """ $(SIGNATURES) @@ -461,37 +458,39 @@ end Create cell permutation such that all cells belonging to one partition are numbered contiguously, return grid with reordered cells. """ -function reorder_cells(grid::ExtendableGrid{Tc,Ti}, cellpartitions,ncellpartitions,colpart) where {Tc,Ti} - ncells=num_cells(grid) - cellperm=copy(cellpartitions) - partctr=zeros(Int,ncellpartitions+1) - partctr[1]=1 - for i=1:ncellpartitions - partctr[i+1]=partctr[i]+sum(x->x==i, cellpartitions) - end - partcells=copy(partctr) - for ic=1:ncells - part=cellpartitions[ic] - cellperm[partctr[part]]=ic - partctr[part]+=1 - end - - pgrid=ExtendableGrid{Tc,Ti}() - pgrid[CellNodes]=grid[CellNodes][:,cellperm] - pgrid[CellRegions]=grid[CellRegions][cellperm] - pgrid[PColorPartitions]=colpart - pgrid[PartitionCells]=partcells - pgrid[PartitionBFaces]=trivial_partitioning(ncellpartitions,num_bfaces(grid)) - - for key in [Coordinates, - CellGeometries, - BFaceNodes, - BFaceRegions, - BFaceGeometries, - CoordinateSystem] - pgrid[key]=grid[key] - end - pgrid +function reorder_cells(grid::ExtendableGrid{Tc, Ti}, cellpartitions, ncellpartitions, colpart) where {Tc, Ti} + ncells = num_cells(grid) + cellperm = copy(cellpartitions) + partctr = zeros(Int, ncellpartitions + 1) + partctr[1] = 1 + for i in 1:ncellpartitions + partctr[i + 1] = partctr[i] + sum(x -> x == i, cellpartitions) + end + partcells = copy(partctr) + for ic in 1:ncells + part = cellpartitions[ic] + cellperm[partctr[part]] = ic + partctr[part] += 1 + end + + pgrid = ExtendableGrid{Tc, Ti}() + pgrid[CellNodes] = grid[CellNodes][:, cellperm] + pgrid[CellRegions] = grid[CellRegions][cellperm] + pgrid[PColorPartitions] = colpart + pgrid[PartitionCells] = partcells + pgrid[PartitionBFaces] = trivial_partitioning(ncellpartitions, num_bfaces(grid)) + + for key in [ + Coordinates, + CellGeometries, + BFaceNodes, + BFaceRegions, + BFaceGeometries, + CoordinateSystem, + ] + pgrid[key] = grid[key] + end + return pgrid end """ @@ -501,11 +500,11 @@ end Create a trivial partitioning such that all items fall in the first of nparts """ -function trivial_partitioning(npart,nitems) - part=zeros(Int,npart+1) - part[1]=1 - part[2:end].=nitems+1 - part +function trivial_partitioning(npart, nitems) + part = zeros(Int, npart + 1) + part[1] = 1 + part[2:end] .= nitems + 1 + return part end @@ -529,80 +528,80 @@ there is no node which is neighbour of nodes from two different partition with t This situation is detected and corrected by joining respective critical partitions, sacrificing a bit of parallel efficiency for correctness. """ -function induce_node_partitioning!(grid::ExtendableGrid{Tc,Ti},cn,nc; trivial=false, keep_nodepermutation=true) where {Tc, Ti} - partcells=grid[PartitionCells] - nnodepartitions=length(partcells)-1 +function induce_node_partitioning!(grid::ExtendableGrid{Tc, Ti}, cn, nc; trivial = false, keep_nodepermutation = true) where {Tc, Ti} + partcells = grid[PartitionCells] + nnodepartitions = length(partcells) - 1 if trivial - grid[PartitionNodes]=trivial_partitioning(nnodepartitions,num_nodes(grid)) + grid[PartitionNodes] = trivial_partitioning(nnodepartitions, num_nodes(grid)) if keep_nodepermutation - grid[NodePermutation]=1:num_nodes(grid) + grid[NodePermutation] = 1:num_nodes(grid) end return grid end - coord=grid[Coordinates] - cellnodes=grid[CellNodes] - bfacenodes=grid[BFaceNodes] - lnodes=size(cellnodes,1) + coord = grid[Coordinates] + cellnodes = grid[CellNodes] + bfacenodes = grid[BFaceNodes] + lnodes = size(cellnodes, 1) - nodepartitions=zeros(Ti,num_nodes(grid)) - for ipart=1:length(partcells)-1 - for icell in partition_cells(grid,ipart) - for k=1:lnodes - nodepartitions[cellnodes[k,icell]]=ipart + nodepartitions = zeros(Ti, num_nodes(grid)) + for ipart in 1:(length(partcells) - 1) + for icell in partition_cells(grid, ipart) + for k in 1:lnodes + nodepartitions[cellnodes[k, icell]] = ipart end end end - partcolors=zeros(Int,num_partitions(grid)) + partcolors = zeros(Int, num_partitions(grid)) for col in pcolors(grid) - for part in pcolor_partitions(grid,col) - partcolors[part]=col + for part in pcolor_partitions(grid, col) + partcolors[part] = col end end # Correct situation where a node is a neighbor of # two different partitions of the same color # which would lead to clashes in the matrix-vector product - nn=cn*nc - rv=SparseArrays.getrowval(nn) - showinfo=true + nn = cn * nc + rv = SparseArrays.getrowval(nn) + showinfo = true # Repeat several times until ok. while true # Detect the situation, record the corresponding # pairs of partitions - idpart=Pair{Int,Int}[] - for inode=1:num_nodes(grid) - ipart=nodepartitions[inode] - icol=partcolors[ipart] - for j in nzrange(nn,inode) - jnode=rv[j] - for m in nzrange(nn,jnode) - mnode=rv[m] - mpart=nodepartitions[mnode] - mcol=partcolors[mpart] + idpart = Pair{Int, Int}[] + for inode in 1:num_nodes(grid) + ipart = nodepartitions[inode] + icol = partcolors[ipart] + for j in nzrange(nn, inode) + jnode = rv[j] + for m in nzrange(nn, jnode) + mnode = rv[m] + mpart = nodepartitions[mnode] + mcol = partcolors[mpart] if (mpart != ipart) && (mcol == icol) if ipart > mpart - push!(idpart, ipart=>mpart) + push!(idpart, ipart => mpart) else - push!(idpart, mpart=>ipart) + push!(idpart, mpart => ipart) end end end end end - idpart=unique(idpart) + idpart = unique(idpart) # We are done if no such case is left - if length(idpart)==0 + if length(idpart) == 0 break end # Show this only once: if showinfo - @info """Renumber node partitions such that no node is neighbor of - two different partitions with same color:\n""" - showinfo=false + @info """Renumber node partitions such that no node is neighbor of + two different partitions with same color:\n""" + showinfo = false end @info "Renumbering: $idpart\n" @@ -610,11 +609,11 @@ function induce_node_partitioning!(grid::ExtendableGrid{Tc,Ti},cn,nc; trivial=fa # The partitions with the higher numbers will be just empty. # This renders the critical nodes into the same partition, so they # will be accessed from the same parallel task. - for inode=1:num_nodes(grid) - part=nodepartitions[inode] + for inode in 1:num_nodes(grid) + part = nodepartitions[inode] for id in idpart - if part==id[1] - nodepartitions[inode]=id[2] + if part == id[1] + nodepartitions[inode] = id[2] end end end @@ -623,50 +622,50 @@ function induce_node_partitioning!(grid::ExtendableGrid{Tc,Ti},cn,nc; trivial=fa # Create node permutation such that # all nodes belonging to one partition # are contiguous - nodeperm=copy(nodepartitions) - partctr=zeros(Int,nnodepartitions+1) - partctr[1]=1 - for i=1:nnodepartitions - partctr[i+1]=partctr[i]+sum(x->x==i, nodepartitions) + nodeperm = copy(nodepartitions) + partctr = zeros(Int, nnodepartitions + 1) + partctr[1] = 1 + for i in 1:nnodepartitions + partctr[i + 1] = partctr[i] + sum(x -> x == i, nodepartitions) end - partnodes=copy(partctr) - for inode=1:num_nodes(grid) - part=nodepartitions[inode] - nodeperm[partctr[part]]=inode - partctr[part]+=1 + partnodes = copy(partctr) + for inode in 1:num_nodes(grid) + part = nodepartitions[inode] + nodeperm[partctr[part]] = inode + partctr[part] += 1 end # Permute coordinate array - nodeperm=invperm(nodeperm) - xcoord=similar(coord) - xcoord[:,nodeperm].=coord + nodeperm = invperm(nodeperm) + xcoord = similar(coord) + xcoord[:, nodeperm] .= coord + - # Renumber node indices for cells - xcellnodes=similar(cellnodes) - for icell=1:num_cells(grid) - for k=1:lnodes - xcellnodes[k,icell]=nodeperm[cellnodes[k,icell]] + xcellnodes = similar(cellnodes) + for icell in 1:num_cells(grid) + for k in 1:lnodes + xcellnodes[k, icell] = nodeperm[cellnodes[k, icell]] end end # Renumber node indices for bfaces - xbfacenodes=similar(bfacenodes) - for ibface=1:num_bfaces(grid) - for k=1:lnodes-1 - xbfacenodes[k,ibface]=nodeperm[bfacenodes[k,ibface]] + xbfacenodes = similar(bfacenodes) + for ibface in 1:num_bfaces(grid) + for k in 1:(lnodes - 1) + xbfacenodes[k, ibface] = nodeperm[bfacenodes[k, ibface]] end end - grid[PartitionNodes]=partnodes - grid[CellNodes]=xcellnodes - grid[BFaceNodes]=xbfacenodes - grid[Coordinates]=xcoord + grid[PartitionNodes] = partnodes + grid[CellNodes] = xcellnodes + grid[BFaceNodes] = xbfacenodes + grid[Coordinates] = xcoord if keep_nodepermutation - grid[NodePermutation]=nodeperm + grid[NodePermutation] = nodeperm end - grid + return grid end """ @@ -681,86 +680,86 @@ number is taken. This method triggers creation of rather complex edge information and should be called only if this information is really necessary. """ -function induce_edge_partitioning!(grid::ExtendableGrid{Tc,Ti}; trivial=false) where {Tc, Ti} - partcells=grid[PartitionCells] - nedgepartitions=length(partcells)-1 - celledges=grid[CellEdges] +function induce_edge_partitioning!(grid::ExtendableGrid{Tc, Ti}; trivial = false) where {Tc, Ti} + partcells = grid[PartitionCells] + nedgepartitions = length(partcells) - 1 + celledges = grid[CellEdges] grid[EdgeNodes] # !!!workaround for bug in extendablegrids: sets num_edges right. if trivial - grid[PartitionEdges]=trivial_partitioning(nedgepartitions,num_edges(grid)) + grid[PartitionEdges] = trivial_partitioning(nedgepartitions, num_edges(grid)) return grid end - ledges=size(celledges,1) + ledges = size(celledges, 1) - edgepartitions=zeros(Ti,num_edges(grid)) - for ipart=1:length(partcells)-1 - for icell in partition_cells(grid,ipart) - for k=1:ledges - edgepartitions[celledges[k,icell]]=ipart + edgepartitions = zeros(Ti, num_edges(grid)) + for ipart in 1:(length(partcells) - 1) + for icell in partition_cells(grid, ipart) + for k in 1:ledges + edgepartitions[celledges[k, icell]] = ipart end end end - partcolors=zeros(Int,num_partitions(grid)) + partcolors = zeros(Int, num_partitions(grid)) for col in pcolors(grid) - for part in pcolor_partitions(grid,col) - partcolors[part]=col + for part in pcolor_partitions(grid, col) + partcolors[part] = col end end # Create edge permutation such that # all edges belonging to one partition # are contiguous - edgeperm=copy(edgepartitions) - partctr=zeros(Int,nedgepartitions+1) - partctr[1]=1 - for i=1:nedgepartitions - partctr[i+1]=partctr[i]+sum(x->x==i, edgepartitions) - end - partedges=copy(partctr) - for iedge=1:num_edges(grid) - part=edgepartitions[iedge] - edgeperm[partctr[part]]=iedge - partctr[part]+=1 - end - - invedgeperm=invperm(edgeperm) - + edgeperm = copy(edgepartitions) + partctr = zeros(Int, nedgepartitions + 1) + partctr[1] = 1 + for i in 1:nedgepartitions + partctr[i + 1] = partctr[i] + sum(x -> x == i, edgepartitions) + end + partedges = copy(partctr) + for iedge in 1:num_edges(grid) + part = edgepartitions[iedge] + edgeperm[partctr[part]] = iedge + partctr[part] += 1 + end + + invedgeperm = invperm(edgeperm) + # Renumber edge indices for cells - xcelledges=similar(celledges) - for icell=1:num_cells(grid) - for k=1:ledges - xcelledges[k,icell]=invedgeperm[celledges[k,icell]] + xcelledges = similar(celledges) + for icell in 1:num_cells(grid) + for k in 1:ledges + xcelledges[k, icell] = invedgeperm[celledges[k, icell]] end end - - grid[PartitionEdges]=partedges + + grid[PartitionEdges] = partedges grid[CellEdges] = xcelledges - grid[EdgeNodes] = grid[EdgeNodes][:,edgeperm] - - if dim_grid(grid)<3 - grid[EdgeCells]=grid[EdgeCells][:,edgeperm] + grid[EdgeNodes] = grid[EdgeNodes][:, edgeperm] + + if dim_grid(grid) < 3 + grid[EdgeCells] = grid[EdgeCells][:, edgeperm] else - ecells=grid[EdgeCells] - csnew=similar(ecells.colstart) - cenew=similar(ecells.colentries) - icenew=1 - csnew[1]=1 - for iedge=1:num_edges(grid) - iperm=edgeperm[iedge] - for ientry=ecells.colstart[iperm]:ecells.colstart[iperm+1]-1 - cenew[icenew]=ecells.colentries[ientry] - icenew=icenew+1 + ecells = grid[EdgeCells] + csnew = similar(ecells.colstart) + cenew = similar(ecells.colentries) + icenew = 1 + csnew[1] = 1 + for iedge in 1:num_edges(grid) + iperm = edgeperm[iedge] + for ientry in ecells.colstart[iperm]:(ecells.colstart[iperm + 1] - 1) + cenew[icenew] = ecells.colentries[ientry] + icenew = icenew + 1 end - csnew[iedge+1]=icenew + csnew[iedge + 1] = icenew end - grid[EdgeCells]=VariableTargetAdjacency(cenew,csnew) + grid[EdgeCells] = VariableTargetAdjacency(cenew, csnew) end # xgrid[CellEdgeSigns] is not changed by renumbering # xgrid[EdgeGeometries] is not changed - - grid + + return grid end @@ -818,20 +817,22 @@ that the above sample code stays valid. Currently, partitioning does not cover the boundary, boundary cells belong to one big trivial partition. """ -function partition(grid::ExtendableGrid, - alg::AbstractPartitioningAlgorithm; - nodes =false, - keep_nodepermutation=false, - edges = false ) - pgrid, cn, nc = dopartition(grid,alg) +function partition( + grid::ExtendableGrid, + alg::AbstractPartitioningAlgorithm; + nodes = false, + keep_nodepermutation = false, + edges = false + ) + pgrid, cn, nc = dopartition(grid, alg) if edges - nodes=true + nodes = true end if !isa(alg, TrivialPartitioning) - induce_node_partitioning!(pgrid,cn,nc; trivial=!nodes, keep_nodepermutation) - induce_edge_partitioning!(pgrid; trivial=!edges) + induce_node_partitioning!(pgrid, cn, nc; trivial = !nodes, keep_nodepermutation) + induce_edge_partitioning!(pgrid; trivial = !edges) end - pgrid + return pgrid end """ @@ -842,17 +843,17 @@ Core function for partitioning grid cells which dispatches over partitioning alg Partitioning extensions should add methods to this function. """ function dopartition(grid::ExtendableGrid, alg::AbstractPartitioningAlgorithm) - if isa(alg,PlainMetisPartitioning) || isa(alg,RecursiveMetisPartitioning) + return if isa(alg, PlainMetisPartitioning) || isa(alg, RecursiveMetisPartitioning) error("Import Metis.jl to use $(typeof(alg))") else error("unexpected Partitioning") end end -function dopartition(grid::ExtendableGrid{Tc,Ti}, ::TrivialPartitioning) where {Tc,Ti} - pgrid=ExtendableGrid{Tc,Ti}() - for (k,v) in pairs(grid.components) - pgrid.components[k]=v +function dopartition(grid::ExtendableGrid{Tc, Ti}, ::TrivialPartitioning) where {Tc, Ti} + pgrid = ExtendableGrid{Tc, Ti}() + for (k, v) in pairs(grid.components) + pgrid.components[k] = v end - trivial_partitioning!(pgrid), nothing, nothing + return trivial_partitioning!(pgrid), nothing, nothing end diff --git a/src/regionedit.jl b/src/regionedit.jl index 3b8c5c83..8c3b5b36 100644 --- a/src/regionedit.jl +++ b/src/regionedit.jl @@ -5,38 +5,40 @@ Edit region numbers of grid cells via rectangular mask. Examples: [Rectangle-with-multiple-regions](@ref) """ -function cellmask!(grid::ExtendableGrid, - maskmin, - maskmax, - ireg::Int; - tol=1.0e-10) - xmaskmin=maskmin.-tol - xmaskmax=maskmax.+tol - ncells=num_cells(grid) - cellnodes=grid[CellNodes] - dim=dim_space(grid) - cellregions=grid[CellRegions] - coord=grid[Coordinates] - for icell=1:ncells - in_region=true - for inode=1:num_targets(cellnodes,icell) - ignode=cellnodes[inode,icell] - for idim=1:dim - if coord[idim,ignode]xmaskmax[idim] - in_region=false + elseif coord[idim, ignode] > xmaskmax[idim] + in_region = false break end end end if in_region - cellregions[icell]=ireg + cellregions[icell] = ireg end end - grid[NumCellRegions]=max(num_cellregions(grid),ireg) - grid + grid[NumCellRegions] = max(num_cellregions(grid), ireg) + return grid end @@ -58,52 +60,54 @@ A zero region number removes boundary faces. Examples: [Rectangle-with-multiple-regions](@ref) """ -function bfacemask!(grid::ExtendableGrid, - maskmin, - maskmax, - ireg; - allow_new=true, - tol=1.0e-10) +function bfacemask!( + grid::ExtendableGrid, + maskmin, + maskmax, + ireg; + allow_new = true, + tol = 1.0e-10 + ) - xmaskmin=maskmin.-tol - xmaskmax=maskmax.+tol + xmaskmin = maskmin .- tol + xmaskmax = maskmax .+ tol - bfacenodes=grid[BFaceNodes] - nbfaces=size(bfacenodes,2) - - Ti=eltype(bfacenodes) - dim=dim_space(grid) - bfaceregions=grid[BFaceRegions] - coord=grid[Coordinates] + bfacenodes = grid[BFaceNodes] + nbfaces = size(bfacenodes, 2) - xfacenodes=bfacenodes + Ti = eltype(bfacenodes) + dim = dim_space(grid) + bfaceregions = grid[BFaceRegions] + coord = grid[Coordinates] + + xfacenodes = bfacenodes if allow_new - new_bfacenodes=ElasticArray{Ti,2}(bfacenodes) - facenodes=grid[FaceNodes] - bfacefaces=grid[BFaceFaces] - nfaces=size(facenodes,2) - bmark=zeros(Int,nfaces) - for ibface=1:nbfaces - bmark[bfacefaces[ibface]]=ibface + new_bfacenodes = ElasticArray{Ti, 2}(bfacenodes) + facenodes = grid[FaceNodes] + bfacefaces = grid[BFaceFaces] + nfaces = size(facenodes, 2) + bmark = zeros(Int, nfaces) + for ibface in 1:nbfaces + bmark[bfacefaces[ibface]] = ibface end - xfacenodes=facenodes + xfacenodes = facenodes end - newregion(ireg::Int, current_region)=ireg - - newregion(ireg::T, current_region) where T<:Function=ireg(current_region) - - ndim=dim_space(grid) - for ixface=1:size(xfacenodes,2) - in_region=true - for inode=1:num_targets(xfacenodes,ixface) - ignode=xfacenodes[inode,ixface] - for idim=1:ndim - if coord[idim,ignode]xmaskmax[idim] - in_region=false + newregion(ireg::Int, current_region) = ireg + + newregion(ireg::T, current_region) where {T <: Function} = ireg(current_region) + + ndim = dim_space(grid) + for ixface in 1:size(xfacenodes, 2) + in_region = true + for inode in 1:num_targets(xfacenodes, ixface) + ignode = xfacenodes[inode, ixface] + for idim in 1:ndim + if coord[idim, ignode] < xmaskmin[idim] + in_region = false + elseif coord[idim, ignode] > xmaskmax[idim] + in_region = false end end if !in_region @@ -113,55 +117,55 @@ function bfacemask!(grid::ExtendableGrid, if in_region if allow_new - ibface=bmark[ixface] - if ibface>0 # we are on an existing bface - reg=newregion(ireg,bfaceregions[ibface]) - bfaceregions[ibface]=reg + ibface = bmark[ixface] + if ibface > 0 # we are on an existing bface + reg = newregion(ireg, bfaceregions[ibface]) + bfaceregions[ibface] = reg else # new bface - reg=newregion(ireg,0) - if reg>0 - push!(bfaceregions,reg) - @views append!(new_bfacenodes,xfacenodes[:,ixface]) + reg = newregion(ireg, 0) + if reg > 0 + push!(bfaceregions, reg) + @views append!(new_bfacenodes, xfacenodes[:, ixface]) end end else # just updating bregion number - reg=newregion(ireg,bfaceregions[ixface]) - bfaceregions[ixface]=reg + reg = newregion(ireg, bfaceregions[ixface]) + bfaceregions[ixface] = reg end end end - + # This adjacency is not true anymore... - delete!(grid,BFaceFaces) + delete!(grid, BFaceFaces) if allow_new - grid[BFaceNodes]=Array{Ti,2}(new_bfacenodes) + grid[BFaceNodes] = Array{Ti, 2}(new_bfacenodes) end - + # Remove bfaces with zero region number. - nzeros=sum(iszero, bfaceregions) - if nzeros>0 - bfacenodes=grid[BFaceNodes] - newlength=length(bfaceregions)-nzeros - new_bfaceregions=zeros(Ti,newlength) - new_bfacenodes=zeros(Ti,size(bfacenodes,1),newlength) - ibfnew=1 + nzeros = sum(iszero, bfaceregions) + if nzeros > 0 + bfacenodes = grid[BFaceNodes] + newlength = length(bfaceregions) - nzeros + new_bfaceregions = zeros(Ti, newlength) + new_bfacenodes = zeros(Ti, size(bfacenodes, 1), newlength) + ibfnew = 1 for ibface in eachindex(bfaceregions) - if bfaceregions[ibface]>0 - new_bfaceregions[ibfnew]=bfaceregions[ibface] - @views new_bfacenodes[:,ibfnew].=bfacenodes[:,ibface] - ibfnew+=1 + if bfaceregions[ibface] > 0 + new_bfaceregions[ibfnew] = bfaceregions[ibface] + @views new_bfacenodes[:, ibfnew] .= bfacenodes[:, ibface] + ibfnew += 1 end end - grid[BFaceNodes]=new_bfacenodes - grid[BFaceRegions]=new_bfaceregions + grid[BFaceNodes] = new_bfacenodes + grid[BFaceRegions] = new_bfaceregions end # Update grid information - btype=grid[BFaceGeometries][1] - grid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(btype,length(grid[BFaceRegions])) - grid[NumBFaceRegions]=maximum(grid[BFaceRegions]) + btype = grid[BFaceGeometries][1] + grid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(btype, length(grid[BFaceRegions])) + grid[NumBFaceRegions] = maximum(grid[BFaceRegions]) return grid end @@ -172,53 +176,59 @@ end Edit region numbers of grid boundary edges via line mask. This only works for 3D grids. """ -function bedgemask!(grid::ExtendableGrid, - xa, - xb, - ireg::Int; - tol=1.0e-10) +function bedgemask!( + grid::ExtendableGrid, + xa, + xb, + ireg::Int; + tol = 1.0e-10 + ) # Masking of boundary edges makes only sense in 3D @assert (dim_space(grid) > 2) masked = false - nbedges = num_bedges(grid) - bedgenodes = grid[BEdgeNodes] - Ti = eltype(bedgenodes) - dim = dim_space(grid) - bedgeregions = grid[BEdgeRegions] - new_bedgenodes = ElasticArray{Ti,2}(bedgenodes) - coord = grid[Coordinates] - Tv = eltype(coord) + nbedges = num_bedges(grid) + bedgenodes = grid[BEdgeNodes] + Ti = eltype(bedgenodes) + dim = dim_space(grid) + bedgeregions = grid[BEdgeRegions] + new_bedgenodes = ElasticArray{Ti, 2}(bedgenodes) + coord = grid[Coordinates] + Tv = eltype(coord) # length of boundary edge region - distsq = sqrt((xa[1]-xb[1])^2 + (xa[2]-xb[2])^2 + (xa[3]-xb[3])^2) + distsq = sqrt((xa[1] - xb[1])^2 + (xa[2] - xb[2])^2 + (xa[3] - xb[3])^2) bedgenodes = grid[BEdgeNodes] # loop over boundary edges - for ibedge = 1:size(bedgenodes, 2) + for ibedge in 1:size(bedgenodes, 2) in_region = true - + #loop over nodes of boundary edge - for inode=1:num_targets(bedgenodes, ibedge) + for inode in 1:num_targets(bedgenodes, ibedge) ignode = bedgenodes[inode, ibedge] # we compute the distance of the boundary edge node to the endpoints # if the sum of the distances is larger (with tolerance) than the length # of the boundary region, the point does not lie on the edge - distxa = sqrt((xa[1]-coord[1,ignode])^2 - + (xa[2]-coord[2,ignode])^2 - + (xa[3]-coord[3,ignode])^2) - distxb = sqrt((coord[1,ignode]-xb[1])^2 - + (coord[2,ignode]-xb[2])^2 - + (coord[3,ignode]-xb[3])^2) - diff = distxa + distxb - distsq + distxa = sqrt( + (xa[1] - coord[1, ignode])^2 + + (xa[2] - coord[2, ignode])^2 + + (xa[3] - coord[3, ignode])^2 + ) + distxb = sqrt( + (coord[1, ignode] - xb[1])^2 + + (coord[2, ignode] - xb[2])^2 + + (coord[3, ignode] - xb[3])^2 + ) + diff = distxa + distxb - distsq if (diff > tol) in_region = false continue end end - + if in_region masked = true bedgeregions[ibedge] = ireg @@ -228,7 +238,7 @@ function bedgemask!(grid::ExtendableGrid, @warn "Couldn't mask any boundary edges for region $(ireg)" end - grid[NumBEdgeRegions]=max(num_bedgeregions(grid),ireg) + grid[NumBEdgeRegions] = max(num_bedgeregions(grid), ireg) return grid end @@ -250,60 +260,60 @@ im the sequence `w,e` in 1D. `s,e,n,w` in 2D and `s,e,n,w,b,t` in 3D. Examples: [Subgrid-from-rectangle](@ref), [Rect2d-with-bregion-function](@ref), [Cross3d](@ref) """ -function rect!(grid,maskmin,maskmax; region=1, bregion=1, bregions=nothing, tol=1.0e-10) - function findval(X,x) +function rect!(grid, maskmin, maskmax; region = 1, bregion = 1, bregions = nothing, tol = 1.0e-10) + function findval(X, x) for i in eachindex(X) - if abs(X[i]-x)=1 - nfaces=2 - X=grid[XCoordinates] - @assert findval(X,maskmin[1]) - @assert findval(X,maskmax[1]) + dim = dim_space(grid) + if dim >= 1 + nfaces = 2 + X = grid[XCoordinates] + @assert findval(X, maskmin[1]) + @assert findval(X, maskmax[1]) end - if dim>=2 - nfaces=4 - Y=grid[YCoordinates] - @assert findval(Y,maskmin[2]) - @assert findval(Y,maskmax[2]) + if dim >= 2 + nfaces = 4 + Y = grid[YCoordinates] + @assert findval(Y, maskmin[2]) + @assert findval(Y, maskmax[2]) end - if dim>=3 - nfaces=6 - Z=grid[ZCoordinates] - @assert findval(Z,maskmin[3]) - @assert findval(Z,maskmax[3]) + if dim >= 3 + nfaces = 6 + Z = grid[ZCoordinates] + @assert findval(Z, maskmin[3]) + @assert findval(Z, maskmax[3]) end - if bregions==nothing - bregions=fill(bregion,nfaces) + if bregions == nothing + bregions = fill(bregion, nfaces) end - @assert length(bregions)==nfaces - cellmask!(grid,maskmin,maskmax,region;tol) + @assert length(bregions) == nfaces + cellmask!(grid, maskmin, maskmax, region; tol) - if dim==1 - bfacemask!(grid, maskmin,maskmin,bregions[1];allow_new=true,tol) - bfacemask!(grid, maskmax,maskmax,bregions[2];allow_new=true,tol) + if dim == 1 + bfacemask!(grid, maskmin, maskmin, bregions[1]; allow_new = true, tol) + bfacemask!(grid, maskmax, maskmax, bregions[2]; allow_new = true, tol) end - if dim==2 - bfacemask!(grid, [maskmin[1],maskmin[2]],[maskmax[1],maskmin[2]],bregions[1];allow_new=true,tol) - bfacemask!(grid, [maskmax[1],maskmin[2]],[maskmax[1],maskmax[2]],bregions[2];allow_new=true,tol) - bfacemask!(grid, [maskmin[1],maskmax[2]],[maskmax[1],maskmax[2]],bregions[3];allow_new=true,tol) - bfacemask!(grid, [maskmin[1],maskmin[2]],[maskmin[1],maskmax[2]],bregions[4];allow_new=true,tol) + if dim == 2 + bfacemask!(grid, [maskmin[1], maskmin[2]], [maskmax[1], maskmin[2]], bregions[1]; allow_new = true, tol) + bfacemask!(grid, [maskmax[1], maskmin[2]], [maskmax[1], maskmax[2]], bregions[2]; allow_new = true, tol) + bfacemask!(grid, [maskmin[1], maskmax[2]], [maskmax[1], maskmax[2]], bregions[3]; allow_new = true, tol) + bfacemask!(grid, [maskmin[1], maskmin[2]], [maskmin[1], maskmax[2]], bregions[4]; allow_new = true, tol) end - if dim==3 - bfacemask!(grid, [maskmin[1],maskmin[2],maskmin[3]],[maskmax[1],maskmin[2],maskmax[3]],bregions[1];allow_new=true,tol) #south - bfacemask!(grid, [maskmax[1],maskmin[2],maskmin[3]],[maskmax[1],maskmax[2],maskmax[3]],bregions[2];allow_new=true,tol) #east - bfacemask!(grid, [maskmin[1],maskmax[2],maskmin[3]],[maskmax[1],maskmax[2],maskmax[3]],bregions[3];allow_new=true,tol) #north - bfacemask!(grid, [maskmin[1],maskmin[2],maskmin[3]],[maskmin[1],maskmax[2],maskmax[3]],bregions[4];allow_new=true,tol) #west - bfacemask!(grid, [maskmin[1],maskmin[2],maskmin[3]],[maskmax[1],maskmax[2],maskmin[3]],bregions[5];allow_new=true,tol) #bottom - bfacemask!(grid, [maskmin[1],maskmin[2],maskmax[3]],[maskmax[1],maskmax[2],maskmax[3]],bregions[5];allow_new=true,tol) #top + if dim == 3 + bfacemask!(grid, [maskmin[1], maskmin[2], maskmin[3]], [maskmax[1], maskmin[2], maskmax[3]], bregions[1]; allow_new = true, tol) #south + bfacemask!(grid, [maskmax[1], maskmin[2], maskmin[3]], [maskmax[1], maskmax[2], maskmax[3]], bregions[2]; allow_new = true, tol) #east + bfacemask!(grid, [maskmin[1], maskmax[2], maskmin[3]], [maskmax[1], maskmax[2], maskmax[3]], bregions[3]; allow_new = true, tol) #north + bfacemask!(grid, [maskmin[1], maskmin[2], maskmin[3]], [maskmin[1], maskmax[2], maskmax[3]], bregions[4]; allow_new = true, tol) #west + bfacemask!(grid, [maskmin[1], maskmin[2], maskmin[3]], [maskmax[1], maskmax[2], maskmin[3]], bregions[5]; allow_new = true, tol) #bottom + bfacemask!(grid, [maskmin[1], maskmin[2], maskmax[3]], [maskmax[1], maskmax[2], maskmax[3]], bregions[5]; allow_new = true, tol) #top end - grid + return grid end diff --git a/src/seal.jl b/src/seal.jl index 47726f75..36094476 100644 --- a/src/seal.jl +++ b/src/seal.jl @@ -11,9 +11,9 @@ Example: `` [x₁, x₂, x₃] → (x₁-1) + (x₂-1)*nn + (x₃-1)*nn²```` """ function encode(x::Vector, nn::Integer) - y = 0*nn - for i = 1:length(x) - y += (x[i]-1) * nn^(i - 1) + y = 0 * nn + for i in 1:length(x) + y += (x[i] - 1) * nn^(i - 1) end return y end @@ -80,7 +80,7 @@ function faces_of_ndim_simplex_direct(x::Vector) sort([x[2], x[3]]), ] end - return y + return y end """ @@ -115,10 +115,10 @@ In this function, the faces are encoded for performance reasons. If a large grid We can not guarantee, that the orientation of the BoundaryFaces is correct. """ function assemble_bfaces(simplices, dim, nn, Ti) - m = size(simplices, 2) + m = size(simplices, 2) poss_faces = zeros(Ti, (dim + 1) * m) - for i = 1:m - poss_faces[(dim+1)*i-dim:(dim+1)*i] = + for i in 1:m + poss_faces[((dim + 1) * i - dim):((dim + 1) * i)] = faces_of_ndim_simplex(simplices[:, i], dim, nn) end dict = countmap(poss_faces) @@ -141,9 +141,9 @@ function assemble_bfaces(simplices, dim, nn, Ti) k += 1 end end - + if dim == 3 - @warn "bfaces may not be oriented correctly" + @warn "bfaces may not be oriented correctly" end return bfaces @@ -162,10 +162,10 @@ For smaller grids it can lead to performance losses. We can not guarantee, that the orientation of the BoundaryFaces is correct. """ function assemble_bfaces_direct(simplices, dim, Ti) - m = size(simplices, 2) - poss_faces = fill(zeros(Ti, dim), (dim+1)*m)#zeros(Int64, (dim, (dim + 1)*m)) - for i = 1:m - poss_faces[(dim+1)*i-dim:(dim+1)*i] = faces_of_ndim_simplex_direct(simplices[:, i]) + m = size(simplices, 2) + poss_faces = fill(zeros(Ti, dim), (dim + 1) * m) #zeros(Int64, (dim, (dim + 1)*m)) + for i in 1:m + poss_faces[((dim + 1) * i - dim):((dim + 1) * i)] = faces_of_ndim_simplex_direct(simplices[:, i]) end dict = countmap(poss_faces) @@ -187,16 +187,15 @@ function assemble_bfaces_direct(simplices, dim, Ti) k += 1 end end - + if dim == 3 - @warn "bfaces may not be oriented correctly" + @warn "bfaces may not be oriented correctly" end return bfaces end - """ ```` function seal!(grid::ExtendableGrid; bfaceregions=[], encode=true, Ti=Int64) @@ -217,26 +216,26 @@ But for each `encoding_type` there is a limit on the number of nodes: \n If `encode=false` is passed, there is no limit (besides the MaxValue of the Integer type used). """ -function seal!(grid::ExtendableGrid; bfaceregions=[], encode=true, encoding_type=Int64) - dim = size(grid[Coordinates])[1] - Ti2 = typeof(grid[CellNodes][1,1]) - if encode - grid[BFaceNodes] = convert(Matrix{Ti2}, assemble_bfaces(grid[CellNodes], dim, size(grid[Coordinates])[2], encoding_type)) - else - grid[BFaceNodes] = convert(Matrix{Ti2}, assemble_bfaces_direct(grid[CellNodes], dim, encoding_type)) - end - - if bfaceregions==[] - grid[BFaceRegions] = ones(Ti2, size(grid[BFaceNodes])[2]) - else - grid[BFaceRegions] = bfaceregions #Int64.(collect(1:size(grid[BFaceNodes])[2])) - end - - if dim == 2 - grid[BFaceGeometries] = VectorOfConstants{ElementGeometries,Ti2}(Edge1D, size(grid[BFaceNodes])[2]) - else - grid[BFaceGeometries] = VectorOfConstants{ElementGeometries,Ti2}(Triangle2D, size(grid[BFaceNodes])[2]) - end - - return grid +function seal!(grid::ExtendableGrid; bfaceregions = [], encode = true, encoding_type = Int64) + dim = size(grid[Coordinates])[1] + Ti2 = typeof(grid[CellNodes][1, 1]) + if encode + grid[BFaceNodes] = convert(Matrix{Ti2}, assemble_bfaces(grid[CellNodes], dim, size(grid[Coordinates])[2], encoding_type)) + else + grid[BFaceNodes] = convert(Matrix{Ti2}, assemble_bfaces_direct(grid[CellNodes], dim, encoding_type)) + end + + if bfaceregions == [] + grid[BFaceRegions] = ones(Ti2, size(grid[BFaceNodes])[2]) + else + grid[BFaceRegions] = bfaceregions #Int64.(collect(1:size(grid[BFaceNodes])[2])) + end + + if dim == 2 + grid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Ti2}(Edge1D, size(grid[BFaceNodes])[2]) + else + grid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Ti2}(Triangle2D, size(grid[BFaceNodes])[2]) + end + + return grid end diff --git a/src/serialadjacency.jl b/src/serialadjacency.jl index 70ef90ae..61a55ae0 100644 --- a/src/serialadjacency.jl +++ b/src/serialadjacency.jl @@ -10,66 +10,67 @@ end Comparison of two adjacencies """ -function Base.:(==)(a::SerialVariableTargetAdjacency{Ta}, b::SerialVariableTargetAdjacency{Tb}) where {Ta,Tb} - Ta==Tb && a.colstart==b.colstart -end +function Base.:(==)(a::SerialVariableTargetAdjacency{Ta}, b::SerialVariableTargetAdjacency{Tb}) where {Ta, Tb} + return Ta == Tb && a.colstart == b.colstart +end """ $(TYPEDSIGNATURES) Create an empty SerialVariableTargetAdjacency """ -SerialVariableTargetAdjacency(t::Type{T}) where T=SerialVariableTargetAdjacency{T}([one(T)]) +SerialVariableTargetAdjacency(t::Type{T}) where {T} = SerialVariableTargetAdjacency{T}([one(T)]) """ $(TYPEDSIGNATURES) Create an empty SerialVariableTargetAdjacency with default type """ -SerialVariableTargetAdjacency()=SerialVariableTargetAdjacency(Int64) +SerialVariableTargetAdjacency() = SerialVariableTargetAdjacency(Int64) """ $(TYPEDSIGNATURES) Show adjacency (in transposed form; preliminary) """ -function Base.show(io::IO,adj::SerialVariableTargetAdjacency) - for isource=1:num_sources(adj) - for itarget=1:num_targets(adj,isource) - print(adj[itarget,isource], " ") +function Base.show(io::IO, adj::SerialVariableTargetAdjacency) + for isource in 1:num_sources(adj) + for itarget in 1:num_targets(adj, isource) + print(adj[itarget, isource], " ") end println() end + return end """ $(TYPEDSIGNATURES) Access adjacency as if it is a 2D Array """ -Base.getindex(adj::SerialVariableTargetAdjacency,i,isource)=adj.colstart[isource]+i-1 -Base.getindex(adj::SerialVariableTargetAdjacency,::Colon,isource)=adj.colstart[isource]:adj.colstart[isource+1]-1 -Base.view(adj::SerialVariableTargetAdjacency{Ti},::Colon,isource) where {Ti} = adj.colstart[isource]:adj.colstart[isource+1]-1 +Base.getindex(adj::SerialVariableTargetAdjacency, i, isource) = adj.colstart[isource] + i - 1 +Base.getindex(adj::SerialVariableTargetAdjacency, ::Colon, isource) = adj.colstart[isource]:(adj.colstart[isource + 1] - 1) +Base.view(adj::SerialVariableTargetAdjacency{Ti}, ::Colon, isource) where {Ti} = adj.colstart[isource]:(adj.colstart[isource + 1] - 1) """ $(TYPEDSIGNATURES) Number of targets for given source """ -ExtendableGrids.num_targets(adj::SerialVariableTargetAdjacency,isource)=adj.colstart[isource+1]-adj.colstart[isource] +ExtendableGrids.num_targets(adj::SerialVariableTargetAdjacency, isource) = adj.colstart[isource + 1] - adj.colstart[isource] """ $(TYPEDSIGNATURES) Number of sources in adjacency """ -ExtendableGrids.num_sources(adj::SerialVariableTargetAdjacency)=length(adj.colstart)-1 +ExtendableGrids.num_sources(adj::SerialVariableTargetAdjacency) = length(adj.colstart) - 1 """ $(TYPEDSIGNATURES) Maximum number of targets per source """ -ExtendableGrids.max_num_targets_per_source(adj::SerialVariableTargetAdjacency)=maximum(adj.colstart[2:end].-adj.colstart[1:end-1]) +ExtendableGrids.max_num_targets_per_source(adj::SerialVariableTargetAdjacency) = maximum(adj.colstart[2:end] .- adj.colstart[1:(end - 1)]) """ $(TYPEDSIGNATURES) Append a column to adjacency. """ -function Base.append!(adj::SerialVariableTargetAdjacency,len) - push!(adj.colstart,adj.colstart[end]+len) +function Base.append!(adj::SerialVariableTargetAdjacency, len) + return push!(adj.colstart, adj.colstart[end] + len) end diff --git a/src/shape_specs.jl b/src/shape_specs.jl index 60c22dbe..8e682a32 100644 --- a/src/shape_specs.jl +++ b/src/shape_specs.jl @@ -5,14 +5,14 @@ $(SIGNATURES) Number of edges in grid. """ -num_edges(grid::ExtendableGrid)=haskey(grid,EdgeNodes) ? num_sources(grid[EdgeNodes]) : 0 +num_edges(grid::ExtendableGrid) = haskey(grid, EdgeNodes) ? num_sources(grid[EdgeNodes]) : 0 ######################################### # VERTEX0D <: AbstractElementGeometry0D # [1] ######################################### -const _refcoords_for_geometry_Vertex0D = reshape([0],1,1) |> Matrix -const _local_celledgenodes_Vertex0D = reshape([1],1,1) |> Matrix +const _refcoords_for_geometry_Vertex0D = reshape([0], 1, 1) |> Matrix +const _local_celledgenodes_Vertex0D = reshape([1], 1, 1) |> Matrix """ $(SIGNATURES) @@ -47,17 +47,16 @@ $(SIGNATURES) Cell-edge node numbering for 1D edge """ -local_celledgenodes(::Type{Vertex0D}) = _local_celledgenodes_Vertex0D +local_celledgenodes(::Type{Vertex0D}) = _local_celledgenodes_Vertex0D - -####################################### +####################################### # EDGE1D <: AbstractElementGeometry1D # [1]-----[2] [1] = 0, [2] = 1 -####################################### +####################################### -const _refcoords_for_geometry_Edge1D = reshape([0; 1]',1,2) |> Matrix -const _local_cellfacenodes_Edge1D = reshape([1; 2],1,2) |> Matrix -const _local_celledgenodes_Edge1D = reshape([1; 2],2,1) |> Matrix +const _refcoords_for_geometry_Edge1D = reshape([0; 1]', 1, 2) |> Matrix +const _local_cellfacenodes_Edge1D = reshape([1; 2], 1, 2) |> Matrix +const _local_celledgenodes_Edge1D = reshape([1; 2], 2, 1) |> Matrix """ $(SIGNATURES) @@ -106,7 +105,7 @@ $(SIGNATURES) Number of edges of 1D edge """ -num_edges(::Type{Edge1D})=1 +num_edges(::Type{Edge1D}) = 1 """ $(SIGNATURES) @@ -116,18 +115,20 @@ Geometries of faces of 1D edge facetype_of_cellface(::Type{<:AbstractElementGeometry1D}, k) = Vertex0D -xrefFACE2xrefCELL(::Type{<:AbstractElementGeometry1D}) = [ [(xref4FACE) -> [1]], - [(xref4FACE) -> [1]] ] +xrefFACE2xrefCELL(::Type{<:AbstractElementGeometry1D}) = [ + [(xref4FACE) -> [1]], + [(xref4FACE) -> [1]], +] xrefFACE2xrefOFACE(::Type{<:AbstractElementGeometry1D}) = [(xref4FACE) -> xref4FACE, (xref4FACE) -> 1 .- xref4FACE] -# [3] -# | \ +# [3] +# | \ ############## | \ [1] = (0,0) # Triangle2D # | \ [2] = (1,0) ############## | \ [3] = (0,1) -# | \ +# | \ # [1]--------[2] @@ -184,20 +185,22 @@ Geometries of faces of 2D triangle facetype_of_cellface(::Type{<:Triangle2D}, k) = Edge1D # maps of reference coords on cell face to reference coords in cell -xrefFACE2xrefCELL(::Type{<:Triangle2D}) = [ (xref4FACE) -> [xref4FACE[1],0], - (xref4FACE) -> [1-xref4FACE[1],xref4FACE[1]], - (xref4FACE) -> [0,1-xref4FACE[1]] - ] +xrefFACE2xrefCELL(::Type{<:Triangle2D}) = [ + (xref4FACE) -> [xref4FACE[1], 0], + (xref4FACE) -> [1 - xref4FACE[1], xref4FACE[1]], + (xref4FACE) -> [0, 1 - xref4FACE[1]], +] # maps of reference coords on face to reference coords in face with other orientation -xrefFACE2xrefOFACE(::Type{<:Triangle2D}) = [(xref4FACE) -> xref4FACE, # orientation 1 = [1,2,3] - (xref4FACE) -> [xref4FACE[1],1-xref4FACE[1]-xref4FACE[2]], # orientation 2 = [1,3,2] - (xref4FACE) -> [1-xref4FACE[1]-xref4FACE[2],xref4FACE[2]], # orientation 3 = [3,2,1] - (xref4FACE) -> [xref4FACE[2],xref4FACE[1]] # orientation 4 = [2,1,3] - ] +xrefFACE2xrefOFACE(::Type{<:Triangle2D}) = [ + (xref4FACE) -> xref4FACE, # orientation 1 = [1,2,3] + (xref4FACE) -> [xref4FACE[1], 1 - xref4FACE[1] - xref4FACE[2]], # orientation 2 = [1,3,2] + (xref4FACE) -> [1 - xref4FACE[1] - xref4FACE[2], xref4FACE[2]], # orientation 3 = [3,2,1] + (xref4FACE) -> [xref4FACE[2], xref4FACE[1]], # orientation 4 = [2,1,3] +] -# [4]--------[3] +# [4]--------[3] # | | [1] = (0,0) ################### | | [2] = (1,0) # Quadrilateral2D # | | [3] = (1,1) @@ -213,7 +216,7 @@ $(SIGNATURES) Coordinates of reference geometry of 2D quadrilateral """ -refcoords_for_geometry(::Type{<:Quadrilateral2D}) = _refcoords_for_geometry_Quadrilateral2D +refcoords_for_geometry(::Type{<:Quadrilateral2D}) = _refcoords_for_geometry_Quadrilateral2D """ $(SIGNATURES) @@ -258,15 +261,16 @@ Geometries of faces of 2D quadrilateral facetype_of_cellface(::Type{<:Quadrilateral2D}, k) = Edge1D # maps of reference coords on cell face to reference coords in cell -xrefFACE2xrefCELL(::Type{<:Quadrilateral2D}) = [ (xref4FACE) -> [xref4FACE[1],0], - (xref4FACE) -> [1,xref4FACE[1]], - (xref4FACE) -> [1-xref4FACE[1],1], - (xref4FACE) -> [0,1-xref4FACE[1]] - ] +xrefFACE2xrefCELL(::Type{<:Quadrilateral2D}) = [ + (xref4FACE) -> [xref4FACE[1], 0], + (xref4FACE) -> [1, xref4FACE[1]], + (xref4FACE) -> [1 - xref4FACE[1], 1], + (xref4FACE) -> [0, 1 - xref4FACE[1]], +] -# [4] -# |\\ +# [4] +# |\\ ################# | \ \ [1] = (0,0,0) # Tetrahedron3D # | \ \ [2] = (1,0,0) ################# | \ \ [3] = (0,1,0) @@ -329,14 +333,15 @@ Cell-edge node numbering for 3D tetrahedron local_celledgenodes(::Type{<:Tetrahedron3D}) = _local_celledgenodes_Tetrahedron3D # maps of reference coords on cell face to reference coords in cell -xrefFACE2xrefCELL(::Type{<:Tetrahedron3D}) = [ (xref4FACE) -> [xref4FACE[2],xref4FACE[1],0], - (xref4FACE) -> [xref4FACE[1],0,xref4FACE[2]], - (xref4FACE) -> [1-xref4FACE[1]-xref4FACE[2],xref4FACE[1],xref4FACE[2]], - (xref4FACE) -> [0,xref4FACE[2],xref4FACE[1]] - ] +xrefFACE2xrefCELL(::Type{<:Tetrahedron3D}) = [ + (xref4FACE) -> [xref4FACE[2], xref4FACE[1], 0], + (xref4FACE) -> [xref4FACE[1], 0, xref4FACE[2]], + (xref4FACE) -> [1 - xref4FACE[1] - xref4FACE[2], xref4FACE[1], xref4FACE[2]], + (xref4FACE) -> [0, xref4FACE[2], xref4FACE[1]], +] -# +# # [8]--------[7] # / | / | [1] = (0,0,0) # [5]--------[6] | [2] = (1,0,0) @@ -398,7 +403,6 @@ Cell-edge node numbering for 3D hexahedron local_celledgenodes(::Type{<:Hexahedron3D}) = _local_celledgenodes_Hexahedron3D - ############### ### VOLUMES ### ############### @@ -408,59 +412,66 @@ function volume(Coords, Nodes, item, ::Type{<:Vertex0D}, ::Type{<:ExtendableGrid end function volume(Coords, Nodes, item, ::Type{<:Edge1D}, ::Type{Cartesian1D}) - return abs(Coords[1, Nodes[2,item]] - Coords[1, Nodes[1,item]]) + return abs(Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) end function volume(Coords, Nodes, item, ::Type{<:Edge1D}, ::Type{Cartesian2D}) - return sqrt((Coords[1, Nodes[2,item]] - Coords[1, Nodes[1,item]]).^2 + (Coords[2, Nodes[2,item]] - Coords[2, Nodes[1,item]]).^2) + return sqrt((Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) .^ 2 + (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) .^ 2) end function volume(Coords, Nodes, item, ::Type{<:Edge1D}, ::Type{Cartesian3D}) - return sqrt((Coords[1, Nodes[2,item]] - Coords[1, Nodes[1,item]]).^2 + (Coords[2, Nodes[2,item]] - Coords[2, Nodes[1,item]]).^2 + (Coords[3, Nodes[2,item]] - Coords[3, Nodes[1,item]]).^2) + return sqrt((Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) .^ 2 + (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) .^ 2 + (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]]) .^ 2) end function volume(Coords, Nodes, item, ::Type{Triangle2D}, ::Type{Cartesian2D}) - return 1 // 2 * ( Coords[1, Nodes[1, item]] * (Coords[2, Nodes[2,item]] - Coords[2, Nodes[3, item]]) - + Coords[1, Nodes[2, item]] * (Coords[2, Nodes[3,item]] - Coords[2, Nodes[1, item]]) - + Coords[1, Nodes[3, item]] * (Coords[2, Nodes[1,item]] - Coords[2, Nodes[2, item]]) ) + return 1 // 2 * ( + Coords[1, Nodes[1, item]] * (Coords[2, Nodes[2, item]] - Coords[2, Nodes[3, item]]) + + Coords[1, Nodes[2, item]] * (Coords[2, Nodes[3, item]] - Coords[2, Nodes[1, item]]) + + Coords[1, Nodes[3, item]] * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) + ) end function volume(Coords, Nodes, item, ::Type{<:Quadrilateral2D}, ::Type{Cartesian2D}) - return 1//2 * ( (Coords[1, Nodes[1, item]] - Coords[1, Nodes[3, item]]) * (Coords[2, Nodes[2, item]] - Coords[2, Nodes[4, item]]) - + (Coords[1, Nodes[4, item]] - Coords[1, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[3, item]]) ) + return 1 // 2 * ( + (Coords[1, Nodes[1, item]] - Coords[1, Nodes[3, item]]) * (Coords[2, Nodes[2, item]] - Coords[2, Nodes[4, item]]) + + (Coords[1, Nodes[4, item]] - Coords[1, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[3, item]]) + ) end function volume(Coords, Nodes, item, ::Type{<:Triangle2D}, ::Type{Cartesian3D}) - @views d12 = SVector{3}(Coords[:, Nodes[1,item]]) - SVector{3}(Coords[:,Nodes[2,item]]) - @views d14 = SVector{3}(Coords[:, Nodes[1,item]]) - SVector{3}(Coords[:,Nodes[3,item]]) - return sqrt((d12[2]*d14[3]-d12[3]*d14[2])^2 + (d12[3]*d14[1]-d12[1]*d14[3])^2 + (d12[1]*d14[2]-d12[2]*d14[1])^2) / 2 + @views d12 = SVector{3}(Coords[:, Nodes[1, item]]) - SVector{3}(Coords[:, Nodes[2, item]]) + @views d14 = SVector{3}(Coords[:, Nodes[1, item]]) - SVector{3}(Coords[:, Nodes[3, item]]) + return sqrt((d12[2] * d14[3] - d12[3] * d14[2])^2 + (d12[3] * d14[1] - d12[1] * d14[3])^2 + (d12[1] * d14[2] - d12[2] * d14[1])^2) / 2 end - -function volume(Coords::Array{Tc,2}, Nodes, item, ::Type{<:Parallelogram2D}, ::Type{Cartesian3D}) where {Tc} - @views d12 = SVector{3}(Coords[:, Nodes[1,item]]) - SVector{3}(Coords[:,Nodes[2,item]]) - @views d14 = SVector{3}(Coords[:, Nodes[1,item]]) - SVector{3}(Coords[:,Nodes[3,item]]) - return sqrt((d12[2]*d14[3]-d12[3]*d14[2])^2 + (d12[3]*d14[1]-d12[1]*d14[3])^2 + (d12[1]*d14[2]-d12[2]*d14[1])^2) +function volume(Coords::Array{Tc, 2}, Nodes, item, ::Type{<:Parallelogram2D}, ::Type{Cartesian3D}) where {Tc} + @views d12 = SVector{3}(Coords[:, Nodes[1, item]]) - SVector{3}(Coords[:, Nodes[2, item]]) + @views d14 = SVector{3}(Coords[:, Nodes[1, item]]) - SVector{3}(Coords[:, Nodes[3, item]]) + return sqrt((d12[2] * d14[3] - d12[3] * d14[2])^2 + (d12[3] * d14[1] - d12[1] * d14[3])^2 + (d12[1] * d14[2] - d12[2] * d14[1])^2) end function volume(Coords, Nodes, item, ::Type{<:Parallelepiped3D}, ::Type{Cartesian3D}) - return ((Coords[1, Nodes[5, item]] - Coords[1, Nodes[1, item]]) * ( (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[4, item]] - Coords[3, Nodes[1, item]]) - (Coords[2, Nodes[4, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]])) - + (Coords[2, Nodes[5, item]] - Coords[2, Nodes[1, item]]) * ( (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]]) * (Coords[1, Nodes[4, item]] - Coords[1, Nodes[1, item]]) - (Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[3, Nodes[4, item]] - Coords[3, Nodes[1, item]])) - + (Coords[3, Nodes[5, item]] - Coords[3, Nodes[1, item]]) * ( (Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[2, Nodes[4, item]] - Coords[2, Nodes[1, item]]) - (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[1, Nodes[4, item]] - Coords[1, Nodes[1, item]]))) + return ( + (Coords[1, Nodes[5, item]] - Coords[1, Nodes[1, item]]) * ((Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[4, item]] - Coords[3, Nodes[1, item]]) - (Coords[2, Nodes[4, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]])) + + (Coords[2, Nodes[5, item]] - Coords[2, Nodes[1, item]]) * ((Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]]) * (Coords[1, Nodes[4, item]] - Coords[1, Nodes[1, item]]) - (Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[3, Nodes[4, item]] - Coords[3, Nodes[1, item]])) + + (Coords[3, Nodes[5, item]] - Coords[3, Nodes[1, item]]) * ((Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[2, Nodes[4, item]] - Coords[2, Nodes[1, item]]) - (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[1, Nodes[4, item]] - Coords[1, Nodes[1, item]])) + ) end function volume(Coords, Nodes, item, ::Type{<:Tetrahedron3D}, ::Type{Cartesian3D}) - return 1 // 6 * ((Coords[1, Nodes[4, item]] - Coords[1, Nodes[1, item]]) * ( (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[3, item]] - Coords[3, Nodes[1, item]]) - (Coords[2, Nodes[3, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]])) - + (Coords[2, Nodes[4, item]] - Coords[2, Nodes[1, item]]) * ( (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]]) * (Coords[1, Nodes[3, item]] - Coords[1, Nodes[1, item]]) - (Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[3, Nodes[3, item]] - Coords[3, Nodes[1, item]])) - + (Coords[3, Nodes[4, item]] - Coords[3, Nodes[1, item]]) * ( (Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[2, Nodes[3, item]] - Coords[2, Nodes[1, item]]) - (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[1, Nodes[3, item]] - Coords[1, Nodes[1, item]]))) + return 1 // 6 * ( + (Coords[1, Nodes[4, item]] - Coords[1, Nodes[1, item]]) * ((Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[3, item]] - Coords[3, Nodes[1, item]]) - (Coords[2, Nodes[3, item]] - Coords[2, Nodes[1, item]]) * (Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]])) + + (Coords[2, Nodes[4, item]] - Coords[2, Nodes[1, item]]) * ((Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]]) * (Coords[1, Nodes[3, item]] - Coords[1, Nodes[1, item]]) - (Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[3, Nodes[3, item]] - Coords[3, Nodes[1, item]])) + + (Coords[3, Nodes[4, item]] - Coords[3, Nodes[1, item]]) * ((Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]]) * (Coords[2, Nodes[3, item]] - Coords[2, Nodes[1, item]]) - (Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]]) * (Coords[1, Nodes[3, item]] - Coords[1, Nodes[1, item]])) + ) end - + ############### ### NORMALS ### @@ -468,31 +479,31 @@ end function Normal4ElemType!(normal, Coords, Nodes, item, ::Type{<:Vertex0D}, ::Type{Cartesian2D}) normal[1] = 0.0 - normal[2] = 0.0 + return normal[2] = 0.0 end function Normal4ElemType!(normal, Coords, Nodes, item, ::Type{<:Vertex0D}, ::Type{Cartesian1D}) - normal[1] = 1.0 + return normal[1] = 1.0 end function Normal4ElemType!(normal, Coords, Nodes, item, ::Type{<:Edge1D}, ::Type{Cartesian2D}) # rotate tangent - normal[1] = Coords[2, Nodes[2,item]] - Coords[2, Nodes[1,item]] - normal[2] = Coords[1, Nodes[1,item]] - Coords[1, Nodes[2,item]] + normal[1] = Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]] + normal[2] = Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]] # divide by length - normal ./= sqrt(normal[1]^2+normal[2]^2) + return normal ./= sqrt(normal[1]^2 + normal[2]^2) end function Normal4ElemType!(normal, Coords, Nodes, item, ::Type{<:Quadrilateral2D}, ::Type{Cartesian3D}) # cross(p(1)-p(2), p(1)-p(4)) / length - normal[1] = (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) * (Coords[3, Nodes[1, item]] - Coords[3, Nodes[4, item]]) + normal[1] = (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) * (Coords[3, Nodes[1, item]] - Coords[3, Nodes[4, item]]) normal[1] -= (Coords[3, Nodes[1, item]] - Coords[3, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[4, item]]) - normal[2] = (Coords[3, Nodes[1, item]] - Coords[3, Nodes[2, item]]) * (Coords[1, Nodes[1, item]] - Coords[1, Nodes[4, item]]) + normal[2] = (Coords[3, Nodes[1, item]] - Coords[3, Nodes[2, item]]) * (Coords[1, Nodes[1, item]] - Coords[1, Nodes[4, item]]) normal[2] -= (Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]]) * (Coords[3, Nodes[1, item]] - Coords[3, Nodes[4, item]]) - normal[3] = (Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[4, item]]) + normal[3] = (Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[4, item]]) normal[3] -= (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) * (Coords[1, Nodes[1, item]] - Coords[1, Nodes[4, item]]) # divide by length - normal ./= sqrt(normal[1]^2+normal[2]^2+normal[3]^2) + return normal ./= sqrt(normal[1]^2 + normal[2]^2 + normal[3]^2) ## old version #d12 = @views Coords[:, Nodes[1, item]] - Coords[:, Nodes[2, item]] @@ -504,14 +515,14 @@ end function Normal4ElemType!(normal, Coords, Nodes, item, ::Type{<:Triangle2D}, ::Type{Cartesian3D}) # cross(p(1)-p(2), p(1)-p(3)) / length - normal[1] = (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) * (Coords[3, Nodes[1, item]] - Coords[3, Nodes[3, item]]) + normal[1] = (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) * (Coords[3, Nodes[1, item]] - Coords[3, Nodes[3, item]]) normal[1] -= (Coords[3, Nodes[1, item]] - Coords[3, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[3, item]]) - normal[2] = (Coords[3, Nodes[1, item]] - Coords[3, Nodes[2, item]]) * (Coords[1, Nodes[1, item]] - Coords[1, Nodes[3, item]]) + normal[2] = (Coords[3, Nodes[1, item]] - Coords[3, Nodes[2, item]]) * (Coords[1, Nodes[1, item]] - Coords[1, Nodes[3, item]]) normal[2] -= (Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]]) * (Coords[3, Nodes[1, item]] - Coords[3, Nodes[3, item]]) - normal[3] = (Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[3, item]]) + normal[3] = (Coords[1, Nodes[1, item]] - Coords[1, Nodes[2, item]]) * (Coords[2, Nodes[1, item]] - Coords[2, Nodes[3, item]]) normal[3] -= (Coords[2, Nodes[1, item]] - Coords[2, Nodes[2, item]]) * (Coords[1, Nodes[1, item]] - Coords[1, Nodes[3, item]]) # divide by length - normal ./= sqrt(normal[1]^2+normal[2]^2+normal[3]^2) + return normal ./= sqrt(normal[1]^2 + normal[2]^2 + normal[3]^2) end @@ -520,16 +531,16 @@ end ################ function Tangent4ElemType!(tangent, Coords, Nodes, item, ::Type{<:Edge1D}, ::Type{Cartesian2D}) - tangent[1] = Coords[1,Nodes[2,item]] - Coords[1, Nodes[1,item]] - tangent[2] = Coords[2,Nodes[2,item]] - Coords[2, Nodes[1,item]] + tangent[1] = Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]] + tangent[2] = Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]] # divide by length - tangent ./= sqrt(tangent[1]^2+tangent[2]^2) + return tangent ./= sqrt(tangent[1]^2 + tangent[2]^2) end function Tangent4ElemType!(tangent, Coords, Nodes, item, ::Type{<:Edge1D}, ::Type{Cartesian3D}) - tangent[1] = Coords[1,Nodes[2,item]] - Coords[1, Nodes[1,item]] - tangent[2] = Coords[2,Nodes[2,item]] - Coords[2, Nodes[1,item]] - tangent[3] = Coords[3,Nodes[2,item]] - Coords[3, Nodes[1,item]] + tangent[1] = Coords[1, Nodes[2, item]] - Coords[1, Nodes[1, item]] + tangent[2] = Coords[2, Nodes[2, item]] - Coords[2, Nodes[1, item]] + tangent[3] = Coords[3, Nodes[2, item]] - Coords[3, Nodes[1, item]] # divide by length - tangent ./= sqrt(tangent[1]^2+tangent[2]^2+tangent[3]^2) + return tangent ./= sqrt(tangent[1]^2 + tangent[2]^2 + tangent[3]^2) end diff --git a/src/simplexgrid.jl b/src/simplexgrid.jl index 12f65b01..7d4feb29 100644 --- a/src/simplexgrid.jl +++ b/src/simplexgrid.jl @@ -19,43 +19,42 @@ Create d-dimensional simplex grid from five arrays. Coordinate type `Tc` index type `Ti` are detected from the first two parameters. `cellregions`, `bfaceregions`, `bfacenodes` are converted to have the same element type as `cellnodes`. """ -function simplexgrid(coord::AbstractArray{Tc,2}, - cellnodes::AbstractArray{Ti,2}, - cellregions=ones(Ti, size(cellnodes,2)), - bfacenodes=zeros(Ti,size(coord,1),0), - bfaceregions=zeros(Ti,length(bfacenodes)) - ) where {Tc,Ti} - @assert size(coord,2)>0 - dim=size(coord,1) - if dim==1 - eltype=Edge1D - btype=Vertex0D - csys=Cartesian1D - elseif dim==2 - eltype=Triangle2D - btype=Edge1D - csys=Cartesian2D - elseif dim==3 - eltype=Tetrahedron3D - btype=Triangle2D - csys=Cartesian3D - end - - grid=ExtendableGrid{Tc,Ti}() - grid[Coordinates]=convert(Matrix{Tc},coord) - grid[CellNodes]=convert(Matrix{Ti},cellnodes) - grid[CellRegions]=convert(Vector{Ti},cellregions) - grid[CellGeometries]=VectorOfConstants{ElementGeometries,Int}(eltype,length(cellregions)) - grid[BFaceNodes]=convert(Matrix{Ti},bfacenodes) - grid[BFaceRegions]=convert(Vector{Ti},bfaceregions) - grid[BFaceGeometries]=VectorOfConstants{ElementGeometries,Int}(btype,length(bfaceregions)) - grid[CoordinateSystem]=csys +function simplexgrid( + coord::AbstractArray{Tc, 2}, + cellnodes::AbstractArray{Ti, 2}, + cellregions = ones(Ti, size(cellnodes, 2)), + bfacenodes = zeros(Ti, size(coord, 1), 0), + bfaceregions = zeros(Ti, length(bfacenodes)) + ) where {Tc, Ti} + @assert size(coord, 2) > 0 + dim = size(coord, 1) + if dim == 1 + eltype = Edge1D + btype = Vertex0D + csys = Cartesian1D + elseif dim == 2 + eltype = Triangle2D + btype = Edge1D + csys = Cartesian2D + elseif dim == 3 + eltype = Tetrahedron3D + btype = Triangle2D + csys = Cartesian3D + end + + grid = ExtendableGrid{Tc, Ti}() + grid[Coordinates] = convert(Matrix{Tc}, coord) + grid[CellNodes] = convert(Matrix{Ti}, cellnodes) + grid[CellRegions] = convert(Vector{Ti}, cellregions) + grid[CellGeometries] = VectorOfConstants{ElementGeometries, Int}(eltype, length(cellregions)) + grid[BFaceNodes] = convert(Matrix{Ti}, bfacenodes) + grid[BFaceRegions] = convert(Vector{Ti}, bfaceregions) + grid[BFaceGeometries] = VectorOfConstants{ElementGeometries, Int}(btype, length(bfaceregions)) + grid[CoordinateSystem] = csys return grid end - - """ ```` function simplexgrid(coord::Array{Tc,2}, @@ -75,17 +74,18 @@ boundary-edge-region-numbers arrays. The index type `Ti` is detected from `cellnodes`, all other arrays besides `coord` are converted to this index type. """ -function simplexgrid(coord::Array{Tc,2}, - cellnodes::Array{Ti,2}, - cellregions, - bfacenodes, - bfaceregions, - bedgenodes, - bedgeregions - ) where {Tc,Ti} +function simplexgrid( + coord::Array{Tc, 2}, + cellnodes::Array{Ti, 2}, + cellregions, + bfacenodes, + bfaceregions, + bedgenodes, + bedgeregions + ) where {Tc, Ti} grid = simplexgrid(coord, cellnodes, cellregions, bfacenodes, bfaceregions) - grid[BEdgeNodes] = convert(Matrix{Ti},bedgenodes) - grid[BEdgeRegions] = convert(Vector{Ti},bedgeregions) + grid[BEdgeNodes] = convert(Matrix{Ti}, bedgenodes) + grid[BEdgeRegions] = convert(Vector{Ti}, bedgeregions) return grid end @@ -115,30 +115,32 @@ grid marking control volumes: marked by `|`. |--|-----|-----|-----|-----|-----|-----|-----|--| ``` """ -function simplexgrid(_X::AbstractVector; bregions=[1,2],cellregion=1) - X=convert(Vector,_X) +function simplexgrid(_X::AbstractVector; bregions = [1, 2], cellregion = 1) + X = convert(Vector, _X) # is_monotone(X) || error("X not monotone") - coord=reshape(X,1,length(X)) - cellnodes=zeros(Cint,2,length(X)-1) - cellregions=zeros(Cint,length(X)-1) - for i=1:length(X)-1 - cellnodes[1,i]=i - cellnodes[2,i]=i+1 - cellregions[i]=1 - end - bfacenodes=Array{Cint}(undef,1,2) - bfaceregions=zeros(Cint,2) - bfacenodes[1,1]=1 - bfacenodes[1,2]=length(X) - bfaceregions[1]=bregions[1] - bfaceregions[2]=bregions[2] - grid=simplexgrid(coord, - cellnodes, - cellregions, - bfacenodes, - bfaceregions) - grid[XCoordinates]=X - grid + coord = reshape(X, 1, length(X)) + cellnodes = zeros(Cint, 2, length(X) - 1) + cellregions = zeros(Cint, length(X) - 1) + for i in 1:(length(X) - 1) + cellnodes[1, i] = i + cellnodes[2, i] = i + 1 + cellregions[i] = 1 + end + bfacenodes = Array{Cint}(undef, 1, 2) + bfaceregions = zeros(Cint, 2) + bfacenodes[1, 1] = 1 + bfacenodes[1, 2] = length(X) + bfaceregions[1] = bregions[1] + bfaceregions[2] = bregions[2] + grid = simplexgrid( + coord, + cellnodes, + cellregions, + bfacenodes, + bfaceregions + ) + grid[XCoordinates] = X + return grid end @@ -165,157 +167,158 @@ The keyword arguments allow to overwrite the default region numbers. """ -function simplexgrid(_X::AbstractVector, _Y::AbstractVector; bregions=[1,2,3,4], cellregion=1) +function simplexgrid(_X::AbstractVector, _Y::AbstractVector; bregions = [1, 2, 3, 4], cellregion = 1) is_monotone(_X) || error("X not monotone") is_monotone(_Y) || error("Y not monotone") - Tc=promote_type(eltype(_X),eltype(_Y)) + Tc = promote_type(eltype(_X), eltype(_Y)) + + X = convert(Vector{Tc}, _X) + Y = convert(Vector{Tc}, _Y) - X=convert(Vector{Tc},_X) - Y=convert(Vector{Tc},_Y) - - function leq(x, x1, x2) - if (x>x1) + if (x > x1) return false end - if (x>x2) + if (x > x2) return false end return true end - + function geq(x, x1, x2) - if (x0.0) - eps=1.0e-5*hmin - - x1=X[1]+eps - xn=X[nx]-eps - y1=Y[1]+eps - yn=Y[ny]-eps - - - function check_insert_bface(ibface,coord,n1,n2) - if (geq(x1,coord[1,n1],coord[1,n2])) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfaceregions[ibface]=bregions[4] - elseif (leq(xn,coord[1,n1],coord[1,n2])) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfaceregions[ibface]=bregions[2] - elseif (geq(y1,coord[2,n1],coord[2,n2])) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfaceregions[ibface]=bregions[1] - elseif (leq(yn,coord[2,n1],coord[2,n2])) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfaceregions[ibface]=bregions[3] - end - ibface - end - - - num_nodes=nx*ny - num_cells=2*(nx-1)*(ny-1) - num_bfacenodes=2*(nx-1)+2*(ny-1) - - coord=zeros(Tc,2,num_nodes) - cellnodes=zeros(Cint,3,num_cells) - cellregions=zeros(Cint,num_cells) - bfacenodes=Array{Cint}(undef,2,num_bfacenodes) - bfaceregions=zeros(Cint,num_bfacenodes) - - ipoint=0 - for iy=1:ny - for ix=1:nx - ipoint=ipoint+1 - coord[1,ipoint]=X[ix] - coord[2,ipoint]=Y[iy] - end - end - @assert(ipoint==num_nodes) - - icell=0 - for iy=1:ny-1 - for ix=1:nx-1 - ip=ix+(iy-1)*nx - p00 = ip - p10 = ip+1 - p01 = ip +nx - p11 = ip+1+nx - - icell=icell+1 - cellnodes[1,icell]=p00 - cellnodes[2,icell]=p10 - cellnodes[3,icell]=p11 - cellregions[icell]=cellregion - - - icell=icell+1 - cellnodes[1,icell]=p11 - cellnodes[2,icell]=p01 - cellnodes[3,icell]=p00 - cellregions[icell]=cellregion - end - end - @assert(icell==num_cells) - + nx = length(X) + ny = length(Y) + + hmin = X[2] - X[1] + for i in 1:(nx - 1) + h = X[i + 1] - X[i] + if h < hmin + hmin = h + end + end + for i in 1:(ny - 1) + h = Y[i + 1] - Y[i] + if h < hmin + hmin = h + end + end + + @assert(hmin > 0.0) + eps = 1.0e-5 * hmin + + x1 = X[1] + eps + xn = X[nx] - eps + y1 = Y[1] + eps + yn = Y[ny] - eps + + + function check_insert_bface(ibface, coord, n1, n2) + if (geq(x1, coord[1, n1], coord[1, n2])) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfaceregions[ibface] = bregions[4] + elseif (leq(xn, coord[1, n1], coord[1, n2])) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfaceregions[ibface] = bregions[2] + elseif (geq(y1, coord[2, n1], coord[2, n2])) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfaceregions[ibface] = bregions[1] + elseif (leq(yn, coord[2, n1], coord[2, n2])) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfaceregions[ibface] = bregions[3] + end + return ibface + end + + + num_nodes = nx * ny + num_cells = 2 * (nx - 1) * (ny - 1) + num_bfacenodes = 2 * (nx - 1) + 2 * (ny - 1) + + coord = zeros(Tc, 2, num_nodes) + cellnodes = zeros(Cint, 3, num_cells) + cellregions = zeros(Cint, num_cells) + bfacenodes = Array{Cint}(undef, 2, num_bfacenodes) + bfaceregions = zeros(Cint, num_bfacenodes) + + ipoint = 0 + for iy in 1:ny + for ix in 1:nx + ipoint = ipoint + 1 + coord[1, ipoint] = X[ix] + coord[2, ipoint] = Y[iy] + end + end + @assert(ipoint == num_nodes) + + icell = 0 + for iy in 1:(ny - 1) + for ix in 1:(nx - 1) + ip = ix + (iy - 1) * nx + p00 = ip + p10 = ip + 1 + p01 = ip + nx + p11 = ip + 1 + nx + + icell = icell + 1 + cellnodes[1, icell] = p00 + cellnodes[2, icell] = p10 + cellnodes[3, icell] = p11 + cellregions[icell] = cellregion + + + icell = icell + 1 + cellnodes[1, icell] = p11 + cellnodes[2, icell] = p01 + cellnodes[3, icell] = p00 + cellregions[icell] = cellregion + end + end + @assert(icell == num_cells) + #lazy way to create boundary grid - ibface=0 - for icell=1:num_cells - n1=cellnodes[1,icell] - n2=cellnodes[2,icell] - n3=cellnodes[3,icell] - ibface=check_insert_bface(ibface,coord,n1,n2) - ibface=check_insert_bface(ibface,coord,n1,n3) - ibface=check_insert_bface(ibface,coord,n2,n3) + ibface = 0 + for icell in 1:num_cells + n1 = cellnodes[1, icell] + n2 = cellnodes[2, icell] + n3 = cellnodes[3, icell] + ibface = check_insert_bface(ibface, coord, n1, n2) + ibface = check_insert_bface(ibface, coord, n1, n3) + ibface = check_insert_bface(ibface, coord, n2, n3) end - @assert(ibface==num_bfacenodes) + @assert(ibface == num_bfacenodes) - grid=simplexgrid(coord, - cellnodes, - cellregions, - bfacenodes, - bfaceregions) + grid = simplexgrid( + coord, + cellnodes, + cellregions, + bfacenodes, + bfaceregions + ) - grid[XCoordinates]=X - grid[YCoordinates]=Y - grid + grid[XCoordinates] = X + grid[YCoordinates] = Y + return grid end @@ -340,179 +343,181 @@ Boundary region numbers: The keyword arguments allow to overwrite the default region numbers. """ -function simplexgrid(_X::AbstractVector,_Y::AbstractVector,_Z::AbstractVector; bregions=[1,2,3,4,5,6],cellregion=1) +function simplexgrid(_X::AbstractVector, _Y::AbstractVector, _Z::AbstractVector; bregions = [1, 2, 3, 4, 5, 6], cellregion = 1) is_monotone(_X) || error("X not monotone") is_monotone(_Y) || error("Y not monotone") is_monotone(_Z) || error("Z not monotone") - Tc=promote_type(eltype(_X),eltype(_Y), eltype(_Z)) - - X=convert(Vector{Tc},_X) - Y=convert(Vector{Tc},_Y) - Z=convert(Vector{Tc},_Z) - - - leq(x, x1, x2, x3)=x≤x1 && x≤x2 && x≤x3 - geq(x, x1, x2, x3)=x≥x1 && x≥x2 && x≥x3 - - nx=length(X) - ny=length(Y) - nz=length(Z) - - hmin=X[2]-X[1] - for i=1:nx-1 - h=X[i+1]-X[i] - if h 0.0) - eps=1.0e-5*hmin - - x1=X[1]+eps - xn=X[end]-eps - - y1=Y[1]+eps - yn=Y[end]-eps - - z1=Z[1]+eps - zn=Z[end]-eps - - - function check_insert_bface(ibface,coord,bfacenodes,n1,n2,n3) - if geq(x1,coord[1,n1],coord[1,n2],coord[1,n3]) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfacenodes[3,ibface]=n3 - bfaceregions[ibface]=bregions[4] - elseif leq(xn,coord[1,n1],coord[1,n2],coord[1,n3]) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfacenodes[3,ibface]=n3 - bfaceregions[ibface]=bregions[2] - elseif geq(y1,coord[2,n1],coord[2,n2],coord[2,n3]) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfacenodes[3,ibface]=n3 - bfaceregions[ibface]=bregions[1] - elseif leq(yn,coord[2,n1],coord[2,n2],coord[2,n3]) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfacenodes[3,ibface]=n3 - bfaceregions[ibface]=bregions[3] - elseif geq(z1,coord[3,n1],coord[3,n2],coord[3,n3]) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfacenodes[3,ibface]=n3 - bfaceregions[ibface]=bregions[5] - elseif leq(zn,coord[3,n1],coord[3,n2],coord[3,n3]) - ibface=ibface+1 - bfacenodes[1,ibface]=n1 - bfacenodes[2,ibface]=n2 - bfacenodes[3,ibface]=n3 - bfaceregions[ibface]=bregions[6] - end - ibface - end - - num_nodes=nx*ny*nz - num_cells=6*(nx-1)*(ny-1)*(nz-1) - num_bfacenodes=4*(nx-1)*(ny-1)+4*(nx-1)*(nz-1)+4*(ny-1)*(nz-1) - - Ti=Cint - coord=zeros(Tc,3,num_nodes) - cellnodes=zeros(Ti,4,num_cells) - cellregions=fill(cellregion,num_cells) - bfacenodes=zeros(Ti,3,num_bfacenodes) - bfaceregions=zeros(Ti,num_bfacenodes) - - ipoint=0 - for iz=1:nz - for iy=1:ny - for ix=1:nx - ipoint=ipoint+1 - coord[1,ipoint]=X[ix] - coord[2,ipoint]=Y[iy] - coord[3,ipoint]=Z[iz] + Tc = promote_type(eltype(_X), eltype(_Y), eltype(_Z)) + + X = convert(Vector{Tc}, _X) + Y = convert(Vector{Tc}, _Y) + Z = convert(Vector{Tc}, _Z) + + + leq(x, x1, x2, x3) = x ≤ x1 && x ≤ x2 && x ≤ x3 + geq(x, x1, x2, x3) = x ≥ x1 && x ≥ x2 && x ≥ x3 + + nx = length(X) + ny = length(Y) + nz = length(Z) + + hmin = X[2] - X[1] + for i in 1:(nx - 1) + h = X[i + 1] - X[i] + if h < hmin + hmin = h + end + end + for i in 1:(ny - 1) + h = Y[i + 1] - Y[i] + if h < hmin + hmin = h + end + end + + for i in 1:(nz - 1) + h = Z[i + 1] - Z[i] + if h < hmin + hmin = h + end + end + + @assert(hmin > 0.0) + eps = 1.0e-5 * hmin + + x1 = X[1] + eps + xn = X[end] - eps + + y1 = Y[1] + eps + yn = Y[end] - eps + + z1 = Z[1] + eps + zn = Z[end] - eps + + + function check_insert_bface(ibface, coord, bfacenodes, n1, n2, n3) + if geq(x1, coord[1, n1], coord[1, n2], coord[1, n3]) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfacenodes[3, ibface] = n3 + bfaceregions[ibface] = bregions[4] + elseif leq(xn, coord[1, n1], coord[1, n2], coord[1, n3]) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfacenodes[3, ibface] = n3 + bfaceregions[ibface] = bregions[2] + elseif geq(y1, coord[2, n1], coord[2, n2], coord[2, n3]) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfacenodes[3, ibface] = n3 + bfaceregions[ibface] = bregions[1] + elseif leq(yn, coord[2, n1], coord[2, n2], coord[2, n3]) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfacenodes[3, ibface] = n3 + bfaceregions[ibface] = bregions[3] + elseif geq(z1, coord[3, n1], coord[3, n2], coord[3, n3]) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfacenodes[3, ibface] = n3 + bfaceregions[ibface] = bregions[5] + elseif leq(zn, coord[3, n1], coord[3, n2], coord[3, n3]) + ibface = ibface + 1 + bfacenodes[1, ibface] = n1 + bfacenodes[2, ibface] = n2 + bfacenodes[3, ibface] = n3 + bfaceregions[ibface] = bregions[6] + end + return ibface + end + + num_nodes = nx * ny * nz + num_cells = 6 * (nx - 1) * (ny - 1) * (nz - 1) + num_bfacenodes = 4 * (nx - 1) * (ny - 1) + 4 * (nx - 1) * (nz - 1) + 4 * (ny - 1) * (nz - 1) + + Ti = Cint + coord = zeros(Tc, 3, num_nodes) + cellnodes = zeros(Ti, 4, num_cells) + cellregions = fill(cellregion, num_cells) + bfacenodes = zeros(Ti, 3, num_bfacenodes) + bfaceregions = zeros(Ti, num_bfacenodes) + + ipoint = 0 + for iz in 1:nz + for iy in 1:ny + for ix in 1:nx + ipoint = ipoint + 1 + coord[1, ipoint] = X[ix] + coord[2, ipoint] = Y[iy] + coord[3, ipoint] = Z[iz] end end end - @assert(ipoint==num_nodes) - - icell=0 - nxy=nx*ny - for iz=1:nz-1 - for iy=1:ny-1 - for ix=1:nx-1 - - ip=ix+(iy-1)*nx+(iz-1)*nxy; - - p000 = ip ; - p100 = ip+1; - p010 = ip +nx; - p110 = ip+1+nx; - p001 = ip +nxy; - p101 = ip+1 +nxy; - p011 = ip +nx+nxy; - p111 = ip+1+nx+nxy; - - icell=icell+1; @. cellnodes[:,icell]=(p000,p100,p110,p111) - icell=icell+1; @. cellnodes[:,icell]=(p000,p101,p100,p111) - icell=icell+1; @. cellnodes[:,icell]=(p000,p010,p011,p111) - icell=icell+1; @. cellnodes[:,icell]=(p000,p110,p010,p111) - icell=icell+1; @. cellnodes[:,icell]=(p000,p001,p101,p111) - icell=icell+1; @. cellnodes[:,icell]=(p000,p011,p001,p111) + @assert(ipoint == num_nodes) + + icell = 0 + nxy = nx * ny + for iz in 1:(nz - 1) + for iy in 1:(ny - 1) + for ix in 1:(nx - 1) + + ip = ix + (iy - 1) * nx + (iz - 1) * nxy + + p000 = ip + p100 = ip + 1 + p010 = ip + nx + p110 = ip + 1 + nx + p001 = ip + nxy + p101 = ip + 1 + nxy + p011 = ip + nx + nxy + p111 = ip + 1 + nx + nxy + + icell = icell + 1; @. cellnodes[:, icell] = (p000, p100, p110, p111) + icell = icell + 1; @. cellnodes[:, icell] = (p000, p101, p100, p111) + icell = icell + 1; @. cellnodes[:, icell] = (p000, p010, p011, p111) + icell = icell + 1; @. cellnodes[:, icell] = (p000, p110, p010, p111) + icell = icell + 1; @. cellnodes[:, icell] = (p000, p001, p101, p111) + icell = icell + 1; @. cellnodes[:, icell] = (p000, p011, p001, p111) end end end - @assert(icell==num_cells) + @assert(icell == num_cells) #lazy way to create boundary grid # lazy but easy... bad for partitioning !!! - - ibface=0 - for icell=1:num_cells - n1=cellnodes[1,icell] - n2=cellnodes[2,icell] - n3=cellnodes[3,icell] - n4=cellnodes[4,icell] - ibface=check_insert_bface(ibface,coord,bfacenodes,n1,n2,n3) - ibface=check_insert_bface(ibface,coord,bfacenodes,n1,n2,n4) - ibface=check_insert_bface(ibface,coord,bfacenodes,n1,n3,n4) - ibface=check_insert_bface(ibface,coord,bfacenodes,n2,n3,n4) - end - @assert(ibface==num_bfacenodes) - - - grid=simplexgrid(coord, - cellnodes, - cellregions, - bfacenodes, - bfaceregions) - grid[XCoordinates]=X - grid[YCoordinates]=Y - grid[ZCoordinates]=Z - grid + ibface = 0 + for icell in 1:num_cells + n1 = cellnodes[1, icell] + n2 = cellnodes[2, icell] + n3 = cellnodes[3, icell] + n4 = cellnodes[4, icell] + ibface = check_insert_bface(ibface, coord, bfacenodes, n1, n2, n3) + ibface = check_insert_bface(ibface, coord, bfacenodes, n1, n2, n4) + ibface = check_insert_bface(ibface, coord, bfacenodes, n1, n3, n4) + ibface = check_insert_bface(ibface, coord, bfacenodes, n2, n3, n4) + end + @assert(ibface == num_bfacenodes) + + + grid = simplexgrid( + coord, + cellnodes, + cellregions, + bfacenodes, + bfaceregions + ) + + grid[XCoordinates] = X + grid[YCoordinates] = Y + grid[ZCoordinates] = Z + return grid end @@ -526,117 +531,114 @@ and added to `cell_offset` and `bface_offset`, respectively. Top an bottom facet regions are detected from the cell regions and added to `bot_offset` resp. `top_offset`. """ -function simplexgrid(grid2::ExtendableGrid, coordZ; bot_offset=0,cell_offset=0,top_offset=0) - coord2=grid2[Coordinates] - nnodes2=size(coord2,2) - nnodesZ=length(coordZ) - nnodes3=nnodes2*nnodesZ - coord3=zeros(3,nnodes3) +function simplexgrid(grid2::ExtendableGrid, coordZ; bot_offset = 0, cell_offset = 0, top_offset = 0) + coord2 = grid2[Coordinates] + nnodes2 = size(coord2, 2) + nnodesZ = length(coordZ) + nnodes3 = nnodes2 * nnodesZ + coord3 = zeros(3, nnodes3) - - cells2=grid2[CellNodes] - ncells2=size(cells2,2) - ncells3=ncells2*3*(nnodesZ-1) + cells2 = grid2[CellNodes] + ncells2 = size(cells2, 2) + ncells3 = ncells2 * 3 * (nnodesZ - 1) - Ti=eltype(cells2) + Ti = eltype(cells2) - cells3=zeros(Ti, 4,ncells3) + cells3 = zeros(Ti, 4, ncells3) + cellregions2 = grid2[CellRegions] + cellregions3 = zeros(Ti, ncells3) - - cellregions2=grid2[CellRegions] - cellregions3=zeros(Ti, ncells3) + bfaces2 = grid2[BFaceNodes] + nbfaces2 = size(bfaces2, 2) + nbfaces3 = 2 * ncells2 + 2 * nbfaces2 * (nnodesZ - 1) + bfaces3 = zeros(Ti, 3, nbfaces3) - bfaces2=grid2[BFaceNodes] - nbfaces2=size(bfaces2,2) - nbfaces3=2*ncells2+2*nbfaces2*(nnodesZ-1) - bfaces3=zeros(Ti,3,nbfaces3) - - bfaceregions2=grid2[BFaceRegions] - bfaceregions3=zeros(Ti,nbfaces3) + bfaceregions2 = grid2[BFaceRegions] + bfaceregions3 = zeros(Ti, nbfaces3) # 3D coordinates - i3=1 - for iZ=1:nnodesZ - for i2=1:nnodes2 - coord3[1,i3]=coord2[1,i2] - coord3[2,i3]=coord2[2,i2] - coord3[3,i3]=coordZ[iZ] - i3+=1 + i3 = 1 + for iZ in 1:nnodesZ + for i2 in 1:nnodes2 + coord3[1, i3] = coord2[1, i2] + coord3[2, i3] = coord2[2, i2] + coord3[3, i3] = coordZ[iZ] + i3 += 1 end end - + # 3D cells - i3=1 - ishift=0 - for iZ=1:nnodesZ-1 - for i2=1:ncells2 - n1 = cells2[1,i2]+ishift - n2 = cells2[2,i2]+ishift - n3 = cells2[3,i2]+ishift + i3 = 1 + ishift = 0 + for iZ in 1:(nnodesZ - 1) + for i2 in 1:ncells2 + n1 = cells2[1, i2] + ishift + n2 = cells2[2, i2] + ishift + n3 = cells2[3, i2] + ishift - n1>n2 ? (n1,n2) = (n2,n1) : nothing - n2>n3 ? (n2,n3) = (n3,n2) : nothing - n1>n2 ? (n1,n2) = (n2,n1) : nothing - - cells3[:,i3 ].=(n1,n2, n3, n3+nnodes2) - cells3[:,i3+1].=(n1,n2, n2+nnodes2,n3+nnodes2) - cells3[:,i3+2].=(n1,n1+nnodes2,n2+nnodes2,n3+nnodes2) + n1 > n2 ? (n1, n2) = (n2, n1) : nothing + n2 > n3 ? (n2, n3) = (n3, n2) : nothing + n1 > n2 ? (n1, n2) = (n2, n1) : nothing + cells3[:, i3] .= (n1, n2, n3, n3 + nnodes2) + cells3[:, i3 + 1] .= (n1, n2, n2 + nnodes2, n3 + nnodes2) + cells3[:, i3 + 2] .= (n1, n1 + nnodes2, n2 + nnodes2, n3 + nnodes2) - cellregions3[i3] = cellregions2[i2]+cell_offset - cellregions3[i3+1] = cellregions2[i2]+cell_offset - cellregions3[i3+2] = cellregions2[i2]+cell_offset - - i3+=3 + + cellregions3[i3] = cellregions2[i2] + cell_offset + cellregions3[i3 + 1] = cellregions2[i2] + cell_offset + cellregions3[i3 + 2] = cellregions2[i2] + cell_offset + + i3 += 3 end - ishift+=nnodes2 + ishift += nnodes2 end #xy parallel facets - i3=1 - for iZ ∈ [1,nnodesZ] - ishift=(iZ-1)*nnodes2 - for i2=1:ncells2 - @views bfaces3[:,i3].=cells2[:,i2].+ishift - bfaceregions3[i3] = iZ==1 ? cellregions2[i2]+bot_offset : cellregions2[i2]+top_offset - i3+=1 + i3 = 1 + for iZ in [1, nnodesZ] + ishift = (iZ - 1) * nnodes2 + for i2 in 1:ncells2 + @views bfaces3[:, i3] .= cells2[:, i2] .+ ishift + bfaceregions3[i3] = iZ == 1 ? cellregions2[i2] + bot_offset : cellregions2[i2] + top_offset + i3 += 1 end end # facets vertical to the x-y plane - ishift=0 - for iZ=1:nnodesZ-1 - for i2=1:nbfaces2 - n1=bfaces2[1,i2]+ishift - n2=bfaces2[2,i2]+ishift - n1>n2 ? (n1,n2)=(n2,n1) : nothing - @views bfaces3[:,i3 ].=(n1,n2, n2+nnodes2) - @views bfaces3[:,i3+1].=(n1,n1+nnodes2,n2+nnodes2) - bfaceregions3[i3] = bfaceregions2[i2] - bfaceregions3[i3+1] = bfaceregions2[i2] - i3+=2 + ishift = 0 + for iZ in 1:(nnodesZ - 1) + for i2 in 1:nbfaces2 + n1 = bfaces2[1, i2] + ishift + n2 = bfaces2[2, i2] + ishift + n1 > n2 ? (n1, n2) = (n2, n1) : nothing + @views bfaces3[:, i3] .= (n1, n2, n2 + nnodes2) + @views bfaces3[:, i3 + 1] .= (n1, n1 + nnodes2, n2 + nnodes2) + bfaceregions3[i3] = bfaceregions2[i2] + bfaceregions3[i3 + 1] = bfaceregions2[i2] + i3 += 2 end - ishift+=nnodes2 + ishift += nnodes2 end - xgrid = simplexgrid(coord3,cells3,cellregions3,bfaces3,bfaceregions3) + xgrid = simplexgrid(coord3, cells3, cellregions3, bfaces3, bfaceregions3) ## quick and dirty fix of local orderings to avoid negative CellVolumes ## and problems with CellFinder cellvolumes = xgrid[CellVolumes] cellnodes = xgrid[CellNodes] - for cell = 1 : num_cells(xgrid) + for cell in 1:num_cells(xgrid) if cellvolumes[cell] < 0 - ishift = cellnodes[1,cell] - cellnodes[1,cell] = cellnodes[2,cell] - cellnodes[2,cell] = ishift + ishift = cellnodes[1, cell] + cellnodes[1, cell] = cellnodes[2, cell] + cellnodes[2, cell] = ishift cellvolumes[cell] *= -1 end end @@ -644,8 +646,6 @@ function simplexgrid(grid2::ExtendableGrid, coordZ; bot_offset=0,cell_offset=0,t end - - """ glue(g1,g2; g1regions=1:num_bfaceregions(g1), @@ -669,140 +669,142 @@ Merge two grids along their common boundary facets. Deprecated: - breg: old notation for interface """ -function glue(g1::ExtendableGrid,g2::ExtendableGrid; - g1regions=1:num_bfaceregions(g1), - g2regions=1:num_bfaceregions(g2), - breg=nothing, - interface=0, - tol=1.0e-10, - strict = false, - naive = false) - Ti=eltype(g1[CellNodes]) - dim=dim_space(g1) - - if breg!=nothing - interface=breg +function glue( + g1::ExtendableGrid, g2::ExtendableGrid; + g1regions = 1:num_bfaceregions(g1), + g2regions = 1:num_bfaceregions(g2), + breg = nothing, + interface = 0, + tol = 1.0e-10, + strict = false, + naive = false + ) + Ti = eltype(g1[CellNodes]) + dim = dim_space(g1) + + if breg != nothing + interface = breg @warn "glue: set interface=$(interface) from deprecated breg kwarg" end - - bfreg1=g1[BFaceRegions] - bfreg2=g2[BFaceRegions] - bfn1=g1[BFaceNodes] - bfn2=g2[BFaceNodes] - - nbf1=size(bfn1,2) - nbf2=size(bfn2,2) - coord1=g1[Coordinates] - coord2=g2[Coordinates] + bfreg1 = g1[BFaceRegions] + bfreg2 = g2[BFaceRegions] + bfn1 = g1[BFaceNodes] + bfn2 = g2[BFaceNodes] + + nbf1 = size(bfn1, 2) + nbf2 = size(bfn2, 2) - nn1=size(coord1,2) - nn2=size(coord2,2) + coord1 = g1[Coordinates] + coord2 = g2[Coordinates] + + nn1 = size(coord1, 2) + nn2 = size(coord2, 2) # numbers of faces in grid1 which match nodes in grid2 - matching_faces=zeros(Ti,nbf2) - n_matching_faces=0 + matching_faces = zeros(Ti, nbf2) + n_matching_faces = 0 # numbers of nodes in grid1 which match nodes in grid2 - matching_nodes=zeros(Ti,nn2) - n_matching_nodes=0 + matching_nodes = zeros(Ti, nn2) + n_matching_nodes = 0 + - # Add matching pair to index of matching pairs - function add_match!(match_list, nmatch, i1,i2) - @assert match_list[i2]==0 || match_list[i2]==i1 - if match_list[i2]==0 - match_list[i2]=i1; - nmatch+=1 + function add_match!(match_list, nmatch, i1, i2) + @assert match_list[i2] == 0 || match_list[i2] == i1 + if match_list[i2] == 0 + match_list[i2] = i1 + nmatch += 1 end - nmatch + return nmatch end # Add two point indices to list of matching points - add_matching_points(ip1,ip2) = n_matching_nodes = add_match!(matching_nodes,n_matching_nodes,ip1,ip2) + add_matching_points(ip1, ip2) = n_matching_nodes = add_match!(matching_nodes, n_matching_nodes, ip1, ip2) # Add two face indices to list of matching faces - add_matching_faces(if1,if2) = n_matching_faces = add_match!(matching_faces,n_matching_faces,if1,if2) - + add_matching_faces(if1, if2) = n_matching_faces = add_match!(matching_faces, n_matching_faces, if1, if2) + ## Check if two points match (distance < tol) function points_match(ip1, ip2) - nm=0.0 - for i=1:dim - nm+=(coord1[i,ip1]-coord2[i,ip2])^2 + nm = 0.0 + for i in 1:dim + nm += (coord1[i, ip1] - coord2[i, ip2])^2 end - return nm0 - ibf1=bf1used[i] + if i > 0 + ibf1 = bf1used[i] # use "old" matching, double check at once - match_faces(ibf1,ibf2) + match_faces(ibf1, ibf2) end end end @@ -845,95 +847,94 @@ function glue(g1::ExtendableGrid,g2::ExtendableGrid; @info "glue: $(n_matching_faces) matching bfaces found" end - creg1=g1[CellRegions] - creg2=g2[CellRegions] - - breg1=g1[BFaceRegions] - breg2=g2[BFaceRegions] - - cn1=g1[CellNodes] - cn2=g2[CellNodes] + creg1 = g1[CellRegions] + creg2 = g2[CellRegions] + + breg1 = g1[BFaceRegions] + breg2 = g2[BFaceRegions] - nc1=size(cn1,2) - nc2=size(cn2,2) + cn1 = g1[CellNodes] + cn2 = g2[CellNodes] + + nc1 = size(cn1, 2) + nc2 = size(cn2, 2) # transposed list of matching faces - mf1=zeros(Ti,nbf1) - for if2=1:nbf2 - if matching_faces[if2]!=0 - mf1[matching_faces[if2]]=if2 + mf1 = zeros(Ti, nbf1) + for if2 in 1:nbf2 + if matching_faces[if2] != 0 + mf1[matching_faces[if2]] = if2 end end mfac = interface == 0 ? 2 : 1 - coordx=zeros(dim,nn1+nn2-n_matching_nodes) - cnx=zeros(Ti, dim+1,nc1+nc2) - cregx=zeros(Ti,nc1+nc2) - bfnx=zeros(Ti,dim,nbf1+nbf2-mfac*n_matching_faces) - bregx=zeros(Ti,nbf1+nbf2-mfac*n_matching_faces) - + coordx = zeros(dim, nn1 + nn2 - n_matching_nodes) + cnx = zeros(Ti, dim + 1, nc1 + nc2) + cregx = zeros(Ti, nc1 + nc2) + bfnx = zeros(Ti, dim, nbf1 + nbf2 - mfac * n_matching_faces) + bregx = zeros(Ti, nbf1 + nbf2 - mfac * n_matching_faces) # copy all data from g1 into new fields - for ix=1:nn1 - @views coordx[:,ix].=coord1[:,ix] + for ix in 1:nn1 + @views coordx[:, ix] .= coord1[:, ix] end - for ix=1:nc1 - cregx[ix]=creg1[ix] - @views cnx[:,ix].=cn1[:,ix] + for ix in 1:nc1 + cregx[ix] = creg1[ix] + @views cnx[:, ix] .= cn1[:, ix] end - ibfx=1 - for ibf1=1:nbf1 - if mf1[ibf1]!=0 && interface == 0 + ibfx = 1 + for ibf1 in 1:nbf1 + if mf1[ibf1] != 0 && interface == 0 continue end - if mf1[ibf1]!=0 - bregx[ibfx]=interface + if mf1[ibf1] != 0 + bregx[ibfx] = interface else - bregx[ibfx]=breg1[ibf1] + bregx[ibfx] = breg1[ibf1] end - @views bfnx[:,ibfx].=bfn1[:,ibf1] - ibfx+=1 + @views bfnx[:, ibfx] .= bfn1[:, ibf1] + ibfx += 1 end # re-calculate matching nodes with new global numbers # add missing coordinates - ix=nn1+1 - for in2=1:nn2 - if matching_nodes[in2]!=0 + ix = nn1 + 1 + for in2 in 1:nn2 + if matching_nodes[in2] != 0 continue end - matching_nodes[in2]=ix - @views coordx[:,ix].=coord2[:,in2] - ix=ix+1 + matching_nodes[in2] = ix + @views coordx[:, ix] .= coord2[:, in2] + ix = ix + 1 end # copy missing data from g2 into arrays. - ix=nc1+1 - for ic2=1:nc2 - cregx[ix]=creg2[ic2] - for id=1:dim+1 - cnx[id,ix]=matching_nodes[cn2[id,ic2]] + ix = nc1 + 1 + for ic2 in 1:nc2 + cregx[ix] = creg2[ic2] + for id in 1:(dim + 1) + cnx[id, ix] = matching_nodes[cn2[id, ic2]] end - ix=ix+1 + ix = ix + 1 end - for ibf2=1:nbf2 - if matching_faces[ibf2]!=0 + for ibf2 in 1:nbf2 + if matching_faces[ibf2] != 0 continue end - bregx[ibfx]=breg2[ibf2] - for id=1:dim - bfnx[id,ibfx]=matching_nodes[bfn2[id,ibf2]] + bregx[ibfx] = breg2[ibf2] + for id in 1:dim + bfnx[id, ibfx] = matching_nodes[bfn2[id, ibf2]] end - ibfx+=1 + ibfx += 1 end - @assert ibfx == nbf1+nbf2-mfac*n_matching_faces+1 - simplexgrid(coordx,cnx,cregx,bfnx,bregx) + @assert ibfx == nbf1 + nbf2 - mfac * n_matching_faces + 1 + return simplexgrid(coordx, cnx, cregx, bfnx, bregx) end diff --git a/src/subgrid.jl b/src/subgrid.jl index f48a88c8..2cf7706f 100644 --- a/src/subgrid.jl +++ b/src/subgrid.jl @@ -61,10 +61,11 @@ $(TYPEDSIGNATURES) Default transform for subgrid creation """ -function _copytransform!(a::AbstractArray,b::AbstractArray) - for i=1:length(a) - a[i]=b[i] +function _copytransform!(a::AbstractArray, b::AbstractArray) + for i in 1:length(a) + a[i] = b[i] end + return end struct XIPair{Tv, Ti} @@ -101,13 +102,17 @@ A subgrid is of type `ExtendableGrid` and stores two additional components: [`ParentGrid`](@ref) and [`NodeParents`](@ref) """ -function subgrid(parent, - subregions::AbstractArray; - transform::T=function(a,b) @views a.=b[1:length(a)] end, - boundary=false, - support=ON_CELLS, - project=true, - coordinatesystem=project ? codim1_coordinatesystem(parent[CoordinateSystem]) : parent[CoordinateSystem]) where T +function subgrid( + parent, + subregions::AbstractArray; + transform::T = function(a, b) + return @views a .= b[1:length(a)] + end, + boundary = false, + support = ON_CELLS, + project = true, + coordinatesystem = project ? codim1_coordinatesystem(parent[CoordinateSystem]) : parent[CoordinateSystem] + ) where {T} @assert support in [ON_CELLS, ON_FACES, ON_BFACES] "value ($support) for 'support' is not allowed" @@ -115,15 +120,15 @@ function subgrid(parent, support = ON_BFACES end - Tc=coord_type(parent) - Ti=index_type(parent) + Tc = coord_type(parent) + Ti = index_type(parent) # # TODO: make a flag array here # @inline function insubregions(xreg) for i in eachindex(subregions) - if subregions[i]==xreg + if subregions[i] == xreg return true end end @@ -131,111 +136,111 @@ function subgrid(parent, end if support == ON_BFACES - xregions=parent[BFaceRegions] - xnodes=parent[BFaceNodes] - xct=parent[BFaceGeometries] - sub_gdim=dim_grid(parent)-1 + xregions = parent[BFaceRegions] + xnodes = parent[BFaceNodes] + xct = parent[BFaceGeometries] + sub_gdim = dim_grid(parent) - 1 elseif support == ON_FACES - xregions=parent[FaceRegions] - xnodes=parent[FaceNodes] - xct=parent[FaceGeometries] - sub_gdim=dim_grid(parent)-1 + xregions = parent[FaceRegions] + xnodes = parent[FaceNodes] + xct = parent[FaceGeometries] + sub_gdim = dim_grid(parent) - 1 elseif support == ON_CELLS - xregions=parent[CellRegions] - xnodes=parent[CellNodes] - xct=parent[CellGeometries] - sub_gdim=dim_grid(parent) + xregions = parent[CellRegions] + xnodes = parent[CellNodes] + xct = parent[CellGeometries] + sub_gdim = dim_grid(parent) end - - nodemark=zeros(Ti,num_nodes(parent)) - - nsubcells=0 - nsubnodes=0 - cellparents=zeros(Ti,0) + + nodemark = zeros(Ti, num_nodes(parent)) + + nsubcells = 0 + nsubnodes = 0 + cellparents = zeros(Ti, 0) for icell in eachindex(xregions) if insubregions(xregions[icell]) - nsubcells+=1 - for inode=1:num_targets(xnodes,icell) - ipnode=xnodes[inode,icell] - if nodemark[ipnode]==0 - nsubnodes+=1 - nodemark[ipnode]=nsubnodes + nsubcells += 1 + for inode in 1:num_targets(xnodes, icell) + ipnode = xnodes[inode, icell] + if nodemark[ipnode] == 0 + nsubnodes += 1 + nodemark[ipnode] = nsubnodes end end push!(cellparents, icell) end end - - sub_xnodes=VariableTargetAdjacency(Ti) - sub_nip=zeros(Ti,nsubnodes) - sub_ct=Vector{ElementGeometries}(undef,0) - sub_cr=Vector{Ti}(undef,0) + + sub_xnodes = VariableTargetAdjacency(Ti) + sub_nip = zeros(Ti, nsubnodes) + sub_ct = Vector{ElementGeometries}(undef, 0) + sub_cr = Vector{Ti}(undef, 0) for inode in eachindex(nodemark) - if nodemark[inode]>0 - sub_nip[nodemark[inode]]=inode + if nodemark[inode] > 0 + sub_nip[nodemark[inode]] = inode end end - - isubcell=0 + + isubcell = 0 for icell in eachindex(xregions) if insubregions(xregions[icell]) - ncn=num_targets(xnodes,icell) - col=zeros(Ti,0) - for inode=1:ncn - push!(col,nodemark[xnodes[inode,icell]]) + ncn = num_targets(xnodes, icell) + col = zeros(Ti, 0) + for inode in 1:ncn + push!(col, nodemark[xnodes[inode, icell]]) end - append!(sub_xnodes,col) - push!(sub_ct,xct[icell]) - push!(sub_cr,xregions[icell]) + append!(sub_xnodes, col) + push!(sub_ct, xct[icell]) + push!(sub_cr, xregions[icell]) end end if project - sub_coord=zeros(Tc,sub_gdim,nsubnodes) + sub_coord = zeros(Tc, sub_gdim, nsubnodes) else - sub_coord=zeros(Tc,dim_space(parent),nsubnodes) + sub_coord = zeros(Tc, dim_space(parent), nsubnodes) end - coord=parent[Coordinates] - @views for inode=1:nsubnodes - transform(sub_coord[:,inode],coord[:,sub_nip[inode]]) + coord = parent[Coordinates] + @views for inode in 1:nsubnodes + transform(sub_coord[:, inode], coord[:, sub_nip[inode]]) end - subgrid=ExtendableGrid{Tc,Ti}() - subgrid[Coordinates]=sub_coord - subgrid[CellRegions]=sub_cr - subgrid[CellGeometries]=sub_ct - subgrid[CellNodes]=tryfix(sub_xnodes) - subgrid[ParentGrid]=parent - subgrid[NodeParents]=sub_nip - subgrid[CellParents]=cellparents - subgrid[ParentGridRelation]=SubGrid{support} + subgrid = ExtendableGrid{Tc, Ti}() + subgrid[Coordinates] = sub_coord + subgrid[CellRegions] = sub_cr + subgrid[CellGeometries] = sub_ct + subgrid[CellNodes] = tryfix(sub_xnodes) + subgrid[ParentGrid] = parent + subgrid[NodeParents] = sub_nip + subgrid[CellParents] = cellparents + subgrid[ParentGridRelation] = SubGrid{support} if support in [ON_BFACES, ON_FACES] - subgrid[NumBFaceRegions]=0 - subgrid[BFaceRegions]=Ti[] - subgrid[BFaceGeometries]=ElementGeometries[] - subgrid[BFaceNodes]=Matrix{Ti}(undef,sub_gdim,0) - subgrid[NumBFaceRegions]=0 + subgrid[NumBFaceRegions] = 0 + subgrid[BFaceRegions] = Ti[] + subgrid[BFaceGeometries] = ElementGeometries[] + subgrid[BFaceNodes] = Matrix{Ti}(undef, sub_gdim, 0) + subgrid[NumBFaceRegions] = 0 if !isnothing(coordinatesystem) - subgrid[CoordinateSystem]=coordinatesystem + subgrid[CoordinateSystem] = coordinatesystem end else - bfacenodes=parent[BFaceNodes] - bfaceregions=parent[BFaceRegions] - bfacetypes=parent[BFaceGeometries] - bfacecells=parent[BFaceCells] - bfaceparents=zeros(Ti,0) - - sub_bfacenodes=VariableTargetAdjacency(Ti) - sub_bfaceregions=Vector{Ti}(undef,0) - sub_bfacetypes=Vector{ElementGeometries}(undef,0) - + bfacenodes = parent[BFaceNodes] + bfaceregions = parent[BFaceRegions] + bfacetypes = parent[BFaceGeometries] + bfacecells = parent[BFaceCells] + bfaceparents = zeros(Ti, 0) + + sub_bfacenodes = VariableTargetAdjacency(Ti) + sub_bfaceregions = Vector{Ti}(undef, 0) + sub_bfacetypes = Vector{ElementGeometries}(undef, 0) + for ibface in eachindex(bfaceregions) - nbn=num_targets(bfacenodes,ibface) - insubgrid=true - for inode=1:nbn - if nodemark[bfacenodes[inode,ibface]]==0 - insubgrid=false + nbn = num_targets(bfacenodes, ibface) + insubgrid = true + for inode in 1:nbn + if nodemark[bfacenodes[inode, ibface]] == 0 + insubgrid = false continue end end @@ -243,55 +248,53 @@ function subgrid(parent, # yet that the bface is in subgrid - we need to check if the # neighboring cells are in the subgrid # TODO: this even may be sufficient! - insubgrid=false - for itarget=1:num_targets(bfacecells,ibface) - icell=bfacecells[itarget, ibface] + insubgrid = false + for itarget in 1:num_targets(bfacecells, ibface) + icell = bfacecells[itarget, ibface] if insubregions(xregions[icell]) - insubgrid=true + insubgrid = true continue end end if insubgrid - col=zeros(Ti,0) - for inode=1:nbn - push!(col,nodemark[bfacenodes[inode,ibface]]) + col = zeros(Ti, 0) + for inode in 1:nbn + push!(col, nodemark[bfacenodes[inode, ibface]]) end - append!(sub_bfacenodes,col) - push!(sub_bfacetypes,bfacetypes[ibface]) - push!(sub_bfaceregions,bfaceregions[ibface]) + append!(sub_bfacenodes, col) + push!(sub_bfacetypes, bfacetypes[ibface]) + push!(sub_bfaceregions, bfaceregions[ibface]) push!(bfaceparents, ibface) end end subgrid[BFaceParents] = bfaceparents - subgrid[BFaceRegions]=sub_bfaceregions - subgrid[BFaceGeometries]=sub_bfacetypes + subgrid[BFaceRegions] = sub_bfaceregions + subgrid[BFaceGeometries] = sub_bfacetypes if length(sub_bfaceregions) > 1 - subgrid[BFaceNodes]=tryfix(sub_bfacenodes) - subgrid[NumBFaceRegions]=maximum(sub_bfaceregions) + subgrid[BFaceNodes] = tryfix(sub_bfacenodes) + subgrid[NumBFaceRegions] = maximum(sub_bfaceregions) else - subgrid[BFaceNodes]=zeros(Ti, 2, 0) - subgrid[NumBFaceRegions]=0 + subgrid[BFaceNodes] = zeros(Ti, 2, 0) + subgrid[NumBFaceRegions] = 0 end - subgrid[CoordinateSystem]=parent[CoordinateSystem] + subgrid[CoordinateSystem] = parent[CoordinateSystem] end if sub_gdim == 1 && project # Sort nodes of grid for easy plotting - X=view(subgrid[Coordinates],1,:) - nx=length(X) - I=subgrid[NodeParents] - xipairs=[XIPair{Tc,Ti}(X[i],I[i]) for i=1:nx] - sort!(xipairs, 1,nx, Base.QuickSort, Base.Forward) - for i=1:nx - X[i]=xipairs[i].x - I[i]=xipairs[i].i + X = view(subgrid[Coordinates], 1, :) + nx = length(X) + I = subgrid[NodeParents] + xipairs = [XIPair{Tc, Ti}(X[i], I[i]) for i in 1:nx] + sort!(xipairs, 1, nx, Base.QuickSort, Base.Forward) + for i in 1:nx + X[i] = xipairs[i].x + I[i] = xipairs[i].i end end - - subgrid -end - + return subgrid +end """ @@ -300,7 +303,7 @@ Vector view on subgrid $(TYPEDFIELDS) """ -struct SubgridVectorView{Tv,Ti} <: AbstractVector{Tv} +struct SubgridVectorView{Tv, Ti} <: AbstractVector{Tv} sysarray::AbstractVector{Tv} node_in_parent::Vector{Ti} end @@ -311,7 +314,7 @@ $(TYPEDSIGNATURES) Create a view of the vector on a subgrid. """ -Base.view(a::AbstractVector,subgrid::ExtendableGrid) = SubgridVectorView(a,subgrid[NodeParents]) +Base.view(a::AbstractVector, subgrid::ExtendableGrid) = SubgridVectorView(a, subgrid[NodeParents]) ############################################################################## @@ -320,7 +323,7 @@ $(TYPEDSIGNATURES) Accessor method for subgrid vector view. """ -Base.getindex(aview::SubgridVectorView,inode::Integer) = aview.sysarray[aview.node_in_parent[inode]] +Base.getindex(aview::SubgridVectorView, inode::Integer) = aview.sysarray[aview.node_in_parent[inode]] ############################################################################## """ @@ -328,8 +331,8 @@ $(TYPEDSIGNATURES) Accessor method for subgrid vector view. """ -@inline function Base.setindex!(aview::SubgridVectorView,v,inode::Integer) - aview.sysarray[aview.node_in_parent[inode]]=v +@inline function Base.setindex!(aview::SubgridVectorView, v, inode::Integer) + aview.sysarray[aview.node_in_parent[inode]] = v return aview end @@ -339,4 +342,4 @@ $(TYPEDSIGNATURES) Return size of vector view. """ -Base.size(a::SubgridVectorView)=(size(a.node_in_parent,1),) +Base.size(a::SubgridVectorView) = (size(a.node_in_parent, 1),) diff --git a/src/tokenstream.jl b/src/tokenstream.jl index 25faab07..b9469466 100644 --- a/src/tokenstream.jl +++ b/src/tokenstream.jl @@ -15,7 +15,7 @@ mutable struct TokenStream """ Array of current tokens kept in memory. """ - tokens::Array{SubString{String},1} + tokens::Array{SubString{String}, 1} """ Position of actual token in tokens array @@ -37,7 +37,7 @@ mutable struct TokenStream Function telling if given character is a delimiter. """ dlm::Function - TokenStream()=new() + TokenStream() = new() end """ @@ -45,7 +45,7 @@ $(TYPEDSIGNATURES) Tokenstream destructor should close input """ -destruct!(tks::TokenStream)=close(tks.input) +destruct!(tks::TokenStream) = close(tks.input) """ @@ -61,7 +61,7 @@ struct UnexpectedTokenError <: Exception lineno::Int end -Base.showerror(io::IO, e::UnexpectedTokenError) = print(io,"Unexpected token in line $(e.lineno): $(e.found) (expected $(e.expected))") +Base.showerror(io::IO, e::UnexpectedTokenError) = print(io, "Unexpected token in line $(e.lineno): $(e.found) (expected $(e.expected))") """ @@ -69,21 +69,21 @@ $(TYPEDSIGNATURES) Create Tokenstream with file name argument. """ -TokenStream(filename::String;comment='#', dlm=isspace)=TokenStream(open(filename),comment=comment,dlm=dlm) +TokenStream(filename::String; comment = '#', dlm = isspace) = TokenStream(open(filename), comment = comment, dlm = dlm) """ $(TYPEDSIGNATURES) Create Tokenstream with IOStream argument. """ -function TokenStream(input::IOStream; comment='#', dlm=isspace) - tks=TokenStream() - tks.input=input - tks.lineno=0 - tks.comment=comment - tks.dlm=dlm - tks.tokens=split("") - tks.itoken=1 +function TokenStream(input::IOStream; comment = '#', dlm = isspace) + tks = TokenStream() + tks.input = input + tks.lineno = 0 + tks.comment = comment + tks.dlm = dlm + tks.tokens = split("") + tks.itoken = 1 _fetch!(tks) return tks end @@ -93,29 +93,29 @@ $(TYPEDSIGNATURES) Check if all tokens have been consumed. """ -Base.eof(tks::TokenStream)=!_fetch!(tks) +Base.eof(tks::TokenStream) = !_fetch!(tks) #= Read next line with tokens and split it. Return if tokens are left =# function _fetch!(tks::TokenStream) - if (tks.itoken<=length(tks.tokens)) + if (tks.itoken <= length(tks.tokens)) return true end - hasline=false - buffer="" + hasline = false + buffer = "" while !hasline && !eof(tks.input) - buffer=readline(tks.input) - tks.lineno+=1 - if (length(buffer)>0) && (buffer[1]!=tks.comment) - hasline=true + buffer = readline(tks.input) + tks.lineno += 1 + if (length(buffer) > 0) && (buffer[1] != tks.comment) + hasline = true end end # TODO: replace this by something non-allocating - tks.tokens=split(buffer,tks.dlm,keepempty=false) - tks.itoken=1 - return (tks.itoken<=length(tks.tokens)) + tks.tokens = split(buffer, tks.dlm, keepempty = false) + tks.itoken = 1 + return (tks.itoken <= length(tks.tokens)) end @@ -124,13 +124,13 @@ $(TYPEDSIGNATURES) Get next token from tokenstream. """ -function gettoken(tks::TokenStream) +function gettoken(tks::TokenStream) if _fetch!(tks) - token=tks.tokens[tks.itoken] - tks.itoken+=1 + token = tks.tokens[tks.itoken] + tks.itoken += 1 return token end - nothing + return nothing end """ @@ -142,12 +142,12 @@ If token is missing, an UnexpectedTokenError is thrown If the token has been found, reading will continue at the position after the token found. """ -function expecttoken(tks::TokenStream,expected::String) - token=gettoken(tks) - if token!=expected - throw(UnexpectedTokenError(token,expected,tks.lineno)) +function expecttoken(tks::TokenStream, expected::String) + token = gettoken(tks) + if token != expected + throw(UnexpectedTokenError(token, expected, tks.lineno)) end - true + return true end """ @@ -161,10 +161,10 @@ a value of false is returned and the next try/gettoken command continues at the Otherwise, true is returned, and reading continues after the token found. """ -function trytoken(tks::TokenStream,expected::String) - token=gettoken(tks) - if token!=expected - tks.itoken=tks.itoken-1 +function trytoken(tks::TokenStream, expected::String) + token = gettoken(tks) + if token != expected + tks.itoken = tks.itoken - 1 return false end return true diff --git a/src/typehierarchy.jl b/src/typehierarchy.jl index 58d86aa8..ed4e43cf 100644 --- a/src/typehierarchy.jl +++ b/src/typehierarchy.jl @@ -3,7 +3,7 @@ $(TYPEDSIGNATURES) Define children for types. """ -AbstractTrees.children(T::Type)=InteractiveUtils.subtypes(T) +AbstractTrees.children(T::Type) = InteractiveUtils.subtypes(T) """ $(TYPEDEF) @@ -18,31 +18,31 @@ $(TYPEDSIGNATURES) Print complete type hierarchy for ExtendableGrids """ -typehierarchy()=AbstractTrees.print_tree(AbstractExtendableGridApexType) +typehierarchy() = AbstractTrees.print_tree(AbstractExtendableGridApexType) function leaftypes(TApex) - function leaftypes!(leafs,t) - st=subtypes(t) - if length(st)==0 - push!(leafs,t) + function leaftypes!(leafs, t) + st = subtypes(t) + if length(st) == 0 + push!(leafs, t) else for tsub in st - leaftypes!(leafs,tsub) + leaftypes!(leafs, tsub) end end - leafs + return leafs end - leaftypes!(Type[],TApex) + return leaftypes!(Type[], TApex) end function allsubtypes(TApex) - function allsubtypes!(st,t) + function allsubtypes!(st, t) for tsub in subtypes(t) - push!(st,tsub) - allsubtypes!(st,tsub) + push!(st, tsub) + allsubtypes!(st, tsub) end - st + return st end - allsubtypes!(Type[],TApex) + return allsubtypes!(Type[], TApex) end diff --git a/src/vectorofconstants.jl b/src/vectorofconstants.jl index 81d154a7..15b65c07 100644 --- a/src/vectorofconstants.jl +++ b/src/vectorofconstants.jl @@ -3,7 +3,7 @@ $(TYPEDEF) Vector with constant value """ -struct VectorOfConstants{T,Tl} <: AbstractVector{T} +struct VectorOfConstants{T, Tl} <: AbstractVector{T} val::T len::Tl end @@ -16,25 +16,25 @@ $(TYPEDSIGNATURES) Length """ -Base.length(v::VectorOfConstants)=v.len +Base.length(v::VectorOfConstants) = v.len """ $(TYPEDSIGNATURES) Size """ -Base.size(v::VectorOfConstants)=(v.len,) +Base.size(v::VectorOfConstants) = (v.len,) """ $(TYPEDSIGNATURES) Access """ -function Base.getindex(v::VectorOfConstants,i) - if i>v.len +function Base.getindex(v::VectorOfConstants, i) + if i > v.len throw(BoundsError(v, i)) end - v.val + return v.val end """ @@ -42,17 +42,17 @@ $(TYPEDSIGNATURES) Iterator """ -Base.iterate(v::VectorOfConstants) = (v.val,1) +Base.iterate(v::VectorOfConstants) = (v.val, 1) """ $(TYPEDSIGNATURES) Iterator """ -Base.iterate(v::VectorOfConstants,state) = state>=v.len ? nothing : (v.val, state+1) +Base.iterate(v::VectorOfConstants, state) = state >= v.len ? nothing : (v.val, state + 1) """ $(TYPEDSIGNATURES) Shortcut for unique """ -Base.unique(v::VectorOfConstants)=[v.val] +Base.unique(v::VectorOfConstants) = [v.val] diff --git a/src/voronoi.jl b/src/voronoi.jl index 102eb3f0..e0acbdc6 100644 --- a/src/voronoi.jl +++ b/src/voronoi.jl @@ -8,7 +8,7 @@ Derived from C source of Jonathan R Shewchuk Modified to return absolute coordinates. """ -function tricircumcenter!(circumcenter,a,b,c) +function tricircumcenter!(circumcenter, a, b, c) # Use coordinates relative to point `a' of the triangle. xba = b[1] - a[1] @@ -19,42 +19,41 @@ function tricircumcenter!(circumcenter,a,b,c) # Squares of lengths of the edges incident to `a'. balength = xba * xba + yba * yba calength = xca * xca + yca * yca - + # Calculate the denominator of the formulae. # if EXACT - # Use orient2d() from http://www.cs.cmu.edu/~quake/robust.html - # to ensure a correctly signed (and reasonably accurate) result, - # avoiding any possibility of division by zero. + # Use orient2d() from http://www.cs.cmu.edu/~quake/robust.html + # to ensure a correctly signed (and reasonably accurate) result, + # avoiding any possibility of division by zero. # denominator = 0.5 / orient2d((double*) b, (double*) c, (double*) a) - - + + # Take your chances with floating-point roundoff denominator = 0.5 / (xba * yca - yba * xca) - # Calculate offset (from `a') of circumcenter. - xcirca = (yca * balength - yba * calength) * denominator - ycirca = (xba * calength - xca * balength) * denominator + # Calculate offset (from `a') of circumcenter. + xcirca = (yca * balength - yba * calength) * denominator + ycirca = (xba * calength - xca * balength) * denominator + + # The result is returned both in terms of x-y coordinates and xi-eta + # coordinates, relative to the triangle's point `a' (that is, `a' is + # the origin of both coordinate systems). Hence, the x-y coordinates + # returned are NOT absolute; one must add the coordinates of `a' to + # find the absolute coordinates of the circumcircle. However, this means + # that the result is frequently more accurate than would be possible if + # absolute coordinates were returned, due to limited floating-point + # precision. In general, the circumradius can be computed much more + # accurately. -# The result is returned both in terms of x-y coordinates and xi-eta -# coordinates, relative to the triangle's point `a' (that is, `a' is -# the origin of both coordinate systems). Hence, the x-y coordinates -# returned are NOT absolute; one must add the coordinates of `a' to -# find the absolute coordinates of the circumcircle. However, this means -# that the result is frequently more accurate than would be possible if -# absolute coordinates were returned, due to limited floating-point -# precision. In general, the circumradius can be computed much more -# accurately. - - circumcenter[1] = xcirca+a[1] - circumcenter[2] = ycirca+a[2] + circumcenter[1] = xcirca + a[1] + circumcenter[2] = ycirca + a[2] return circumcenter end - """ $(TYPEDEF) @@ -63,45 +62,49 @@ Centers of voronoi cell facets (currently 1D, 2D). abstract type VoronoiFaceCenters <: AbstractGridFloatArray2D end -function instantiate(grid::ExtendableGrid{Tc,Ti}, ::Type{VoronoiFaceCenters}) where {Tc,Ti} - coord=grid[Coordinates] - en=grid[EdgeNodes] - nedges=size(en,2) - dim=size(coord,1) - xsigma=zeros(Tc,dim,nedges) - - if dim==1 - for iedge=1:nedges - en1=en[1,iedge] - en2=en[2,iedge] - xsigma[1,iedge]=0.5*(coord[1,en1]+coord[1,en2]) +function instantiate(grid::ExtendableGrid{Tc, Ti}, ::Type{VoronoiFaceCenters}) where {Tc, Ti} + coord = grid[Coordinates] + en = grid[EdgeNodes] + nedges = size(en, 2) + dim = size(coord, 1) + xsigma = zeros(Tc, dim, nedges) + + if dim == 1 + for iedge in 1:nedges + en1 = en[1, iedge] + en2 = en[2, iedge] + xsigma[1, iedge] = 0.5 * (coord[1, en1] + coord[1, en2]) end - elseif dim==2 - cn=grid[CellNodes] - ec=grid[EdgeCells] - cc1=zeros(2) - cc2=zeros(2) - for iedge=1:nedges - icell1=ec[1,iedge] - icell2=ec[2,iedge] - + elseif dim == 2 + cn = grid[CellNodes] + ec = grid[EdgeCells] + cc1 = zeros(2) + cc2 = zeros(2) + for iedge in 1:nedges + icell1 = ec[1, iedge] + icell2 = ec[2, iedge] + # calculate voronoi face ends: either cellcenter -- cellcenter or cellcenter --edgecenter - @views tricircumcenter!(cc1, - coord[:,cn[1,icell1]], - coord[:,cn[2,icell1]], - coord[:,cn[3,icell1]]) - if icell2>0 - @views tricircumcenter!(cc2, - coord[:,cn[1,icell2]], - coord[:,cn[2,icell2]], - coord[:,cn[3,icell2]]) - - @views xsigma[:,iedge].=0.5*(cc1+cc2) + @views tricircumcenter!( + cc1, + coord[:, cn[1, icell1]], + coord[:, cn[2, icell1]], + coord[:, cn[3, icell1]] + ) + if icell2 > 0 + @views tricircumcenter!( + cc2, + coord[:, cn[1, icell2]], + coord[:, cn[2, icell2]], + coord[:, cn[3, icell2]] + ) + + @views xsigma[:, iedge] .= 0.5 * (cc1 + cc2) else - inode1=en[1,iedge] - inode2=en[2,iedge] - @views cc2.=0.5*(coord[:,inode1]+coord[:,inode2]) - @views xsigma[:,iedge].=cc2 + inode1 = en[1, iedge] + inode2 = en[2, iedge] + @views cc2 .= 0.5 * (coord[:, inode1] + coord[:, inode2]) + @views xsigma[:, iedge] .= cc2 end # We observe artifacts at domain corners if we use # this expression which would be consistent to the formal approach. @@ -110,5 +113,5 @@ function instantiate(grid::ExtendableGrid{Tc,Ti}, ::Type{VoronoiFaceCenters}) wh else error("3D Voronoi Face Centers not implemented yet") end - xsigma -end + return xsigma +end diff --git a/test/gmsh.jl b/test/gmsh.jl index 6e565dfd..e812052f 100644 --- a/test/gmsh.jl +++ b/test/gmsh.jl @@ -9,7 +9,7 @@ using Gmsh: gmsh gmsh.option.setNumber("General.Terminal", 1) gmsh.model.add("t1") - lc = 1e-2 + lc = 1.0e-2 gmsh.model.geo.addPoint(0, 0, 0, lc, 1) gmsh.model.geo.addPoint(0.1, 0, 0, lc, 2) gmsh.model.geo.addPoint(0.1, 0.3, 0, lc, 3) @@ -43,48 +43,48 @@ using Gmsh: gmsh end @testset "Read/write simplex gmsh 2d / 3d" begin - path = joinpath(pkgdir(ExtendableGrids),"test") + path = joinpath(pkgdir(ExtendableGrids), "test") X = collect(0:0.02:2) Y = collect(0:2:4) grid1 = simplexgrid(X, Y) #ExtendableGrids.simplexgrid_from_gmsh(path*"sto_2d.msh") - ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path,"testfile.msh")) + ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path, "testfile.msh")) - grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"testfile.msh"); Tc = Float32, Ti = Int64) + grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "testfile.msh"); Tc = Float32, Ti = Int64) @test seemingly_equal(grid2, grid1; sort = true, confidence = :low) @test seemingly_equal(grid2, grid1; sort = true, confidence = :full) - grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"sto_2d.msh"); Tc = Float64, Ti = Int64) + grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "sto_2d.msh"); Tc = Float64, Ti = Int64) - ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path,"testfile.msh")) - grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"testfile.msh"); Tc = Float64, Ti = Int64) + ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path, "testfile.msh")) + grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "testfile.msh"); Tc = Float64, Ti = Int64) @test seemingly_equal(grid1, grid2; sort = true, confidence = :low) @test seemingly_equal(grid1, grid2; sort = true, confidence = :full) - grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"sto_3d.msh"); Tc = Float32, Ti = Int64) + grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "sto_3d.msh"); Tc = Float32, Ti = Int64) - ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path,"testfile.msh")) - grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"testfile.msh"); Tc = Float64, Ti = Int32) + ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path, "testfile.msh")) + grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "testfile.msh"); Tc = Float64, Ti = Int32) @test seemingly_equal(grid1, grid2; sort = true, confidence = :low) @test seemingly_equal(grid1, grid2; sort = true, confidence = :full) - grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"sto_2d.msh")) + grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "sto_2d.msh")) - grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"sto_3d.msh"); Tc = Float32, Ti = Int32) + grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "sto_3d.msh"); Tc = Float32, Ti = Int32) @test !seemingly_equal(grid1, grid2; sort = true, confidence = :low) @test !seemingly_equal(grid1, grid2; sort = true, confidence = :full) - grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"testmesh.gmsh"); incomplete = true) + grid1 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "testmesh.gmsh"); incomplete = true) ExtendableGrids.seal!(grid1; encode = false) - ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path,"completed_testfile.msh")) - grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"completed_testfile.msh")) + ExtendableGrids.simplexgrid_to_gmsh(grid1; filename = joinpath(path, "completed_testfile.msh")) + grid2 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "completed_testfile.msh")) - grid3 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path,"testmesh.gmsh"); incomplete = true) + grid3 = ExtendableGrids.simplexgrid_from_gmsh(joinpath(path, "testmesh.gmsh"); incomplete = true) ExtendableGrids.seal!(grid3; encode = true) @test seemingly_equal(grid1, grid2; sort = true, confidence = :low) @@ -107,20 +107,20 @@ end end @testset "Read/write mixed gmsh 2d" begin - path = joinpath(pkgdir(ExtendableGrids),"test") - grid1 = ExtendableGrids.mixedgrid_from_gmsh(joinpath(path,"mixedgrid_2d.msh"); Tc = Float64, Ti = Int64) + path = joinpath(pkgdir(ExtendableGrids), "test") + grid1 = ExtendableGrids.mixedgrid_from_gmsh(joinpath(path, "mixedgrid_2d.msh"); Tc = Float64, Ti = Int64) ExtendableGrids.mixedgrid_to_gmsh(grid1; filename = joinpath(path, "testfile.msh")) - grid2 = ExtendableGrids.mixedgrid_from_gmsh(joinpath(path,"testfile.msh"); Tc = Float32, Ti = UInt64) + grid2 = ExtendableGrids.mixedgrid_from_gmsh(joinpath(path, "testfile.msh"); Tc = Float32, Ti = UInt64) @test seemingly_equal(grid1, grid2; sort = true, confidence = :low) @test seemingly_equal(grid1, grid2; sort = true, confidence = :full) end @testset "Read .geo files" begin - path = joinpath(pkgdir(ExtendableGrids),"test") - grid = simplexgrid(joinpath(path,"disk1hole.geo")) + path = joinpath(pkgdir(ExtendableGrids), "test") + grid = simplexgrid(joinpath(path, "disk1hole.geo")) @test num_cells(grid) > 0 - grid = simplexgrid(joinpath(path,"cube6.geo")) + grid = simplexgrid(joinpath(path, "cube6.geo")) @test num_cells(grid) > 0 end diff --git a/test/partitioning.jl b/test/partitioning.jl index f23857f8..feb5b751 100644 --- a/test/partitioning.jl +++ b/test/partitioning.jl @@ -1,51 +1,51 @@ import Metis @testset "partitioning" begin - for h in [0.4,0.5,0.6] + for h in [0.4, 0.5, 0.6] for dim in 1:3 @info "Test partitioning for dim=$(dim)" - X=0:0.1:10 - if dim==3 - X=0:h:10 + X = 0:0.1:10 + if dim == 3 + X = 0:h:10 end - - XXX=(X for i=1:dim) - grid1=simplexgrid(XXX...) - grid2=partition(grid1,TrivialPartitioning()) - grid3=partition(grid1,PlainMetisPartitioning(npart=1); nodes=true) - grid3=partition(grid1,RecursiveMetisPartitioning(npart=1); nodes=true) - - - for grid in (grid1, grid2, grid3) - @test num_pcolors(grid)==1 - @test num_partitions(grid)==1 + + XXX = (X for i in 1:dim) + grid1 = simplexgrid(XXX...) + grid2 = partition(grid1, TrivialPartitioning()) + grid3 = partition(grid1, PlainMetisPartitioning(npart = 1); nodes = true) + grid3 = partition(grid1, RecursiveMetisPartitioning(npart = 1); nodes = true) + + + for grid in (grid1, grid2, grid3) + @test num_pcolors(grid) == 1 + @test num_partitions(grid) == 1 @test pcolors(grid) == 1:1 - @test pcolor_partitions(grid,1) == 1:1 - @test partition_cells(grid,1) == 1:num_cells(grid) + @test pcolor_partitions(grid, 1) == 1:1 + @test partition_cells(grid, 1) == 1:num_cells(grid) @test num_partitions_per_color(grid) == [1] - @test check_partitioning(grid, verbose=false) + @test check_partitioning(grid, verbose = false) end - + for npart in [10, 15, 20] - grid4=partition(grid1,PlainMetisPartitioning(npart=npart); nodes=true, keep_nodepermutation=true) + grid4 = partition(grid1, PlainMetisPartitioning(npart = npart); nodes = true, keep_nodepermutation = true) @test num_pcolors(grid4) > 1 - @test num_partitions(grid4)==npart - @test pcolors(grid4) |> length >0 - @test partition_cells(grid4,1) |> length >0 - @test num_partitions_per_color(grid4) |> length >0 - @test grid4[Coordinates][:,grid4[NodePermutation]]≈grid1[Coordinates] - @test check_partitioning(grid4, verbose=false) + @test num_partitions(grid4) == npart + @test pcolors(grid4) |> length > 0 + @test partition_cells(grid4, 1) |> length > 0 + @test num_partitions_per_color(grid4) |> length > 0 + @test grid4[Coordinates][:, grid4[NodePermutation]] ≈ grid1[Coordinates] + @test check_partitioning(grid4, verbose = false) end - for npart in [3,4,5,6] - grid4=partition(grid1,RecursiveMetisPartitioning(npart=npart); nodes=true, keep_nodepermutation=true) + for npart in [3, 4, 5, 6] + grid4 = partition(grid1, RecursiveMetisPartitioning(npart = npart); nodes = true, keep_nodepermutation = true) @test num_pcolors(grid4) > 1 - @test num_partitions(grid4)>npart - @test pcolors(grid4) |> length >0 - @test partition_cells(grid4,1) |> length >0 - @test num_partitions_per_color(grid4) |> length >0 - @test grid4[Coordinates][:,grid4[NodePermutation]]≈grid1[Coordinates] - @test check_partitioning(grid4, verbose=false) + @test num_partitions(grid4) > npart + @test pcolors(grid4) |> length > 0 + @test partition_cells(grid4, 1) |> length > 0 + @test num_partitions_per_color(grid4) |> length > 0 + @test grid4[Coordinates][:, grid4[NodePermutation]] ≈ grid1[Coordinates] + @test check_partitioning(grid4, verbose = false) end end end diff --git a/test/runtests.jl b/test/runtests.jl index f3ae8e90..305bd65a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -7,20 +7,20 @@ using ExtendableGrids: seal! using AbstractTrees, StatsBase @testset "Aqua" begin -Aqua.test_ambiguities([ExtendableGrids, Base, Core], exclude=[view, ==, StatsBase.TestStat, copyto!]) -Aqua.test_unbound_args(ExtendableGrids) -Aqua.test_undefined_exports(ExtendableGrids) -Aqua.test_project_extras(ExtendableGrids) -Aqua.test_stale_deps(ExtendableGrids,ignore=[:Requires,:Bijections]) -Aqua.test_deps_compat(ExtendableGrids) -Aqua.test_piracies(ExtendableGrids,treat_as_own=[AbstractTrees.children]) -Aqua.test_persistent_tasks(ExtendableGrids) + Aqua.test_ambiguities([ExtendableGrids, Base, Core], exclude = [view, ==, StatsBase.TestStat, copyto!]) + Aqua.test_unbound_args(ExtendableGrids) + Aqua.test_undefined_exports(ExtendableGrids) + Aqua.test_project_extras(ExtendableGrids) + Aqua.test_stale_deps(ExtendableGrids, ignore = [:Requires, :Bijections]) + Aqua.test_deps_compat(ExtendableGrids) + Aqua.test_piracies(ExtendableGrids, treat_as_own = [AbstractTrees.children]) + Aqua.test_persistent_tasks(ExtendableGrids) end @testset "Constructors" begin # test whether the show() function throws an error - @test show( ExtendableGrid{Float64,Int32}() ) === nothing + @test show(ExtendableGrid{Float64, Int32}()) === nothing end @testset "Geomspace" begin @@ -38,7 +38,7 @@ end end function test_geomspace1(n, h0, h1) - for i = 1:n + for i in 1:n X = geomspace(0, 1, h0 * rand(), h1 * rand()) end true @@ -57,21 +57,27 @@ end end @testset "Basic" begin - nodes = [0.0 1; - 1 0; - 0 1; - 1 1]' - - cells = [1 2 3; - 2 3 4]' - + nodes = [ + 0.0 1; + 1 0; + 0 1; + 1 1 + ]' + + cells = [ + 1 2 3; + 2 3 4 + ]' + cellmat = [1, 1] - bfaces = [1 2; - 1 3; - 2 4]' - + bfaces = [ + 1 2; + 1 3; + 2 4 + ]' + bfacemat = [1, 1] - + grid = simplexgrid(nodes, cells, cellmat, bfaces, bfacemat) @test isconsistent(grid) @@ -79,53 +85,65 @@ end @test isconsistent(grid) seal!(grid) @test isconsistent(grid) - + grid = simplexgrid(nodes, cells, cellmat) @test isconsistent(grid) seal!(grid) @test isconsistent(grid) - + grid = simplexgrid(nodes, cells) @test isconsistent(grid) seal!(grid) @test isconsistent(grid) - - @test let - nodes = [0.0 1; - 1 0; - 0 1; - 1 1; - 1 2; - 2 4]' - cells = [1 2 3; - 2 3 4]' + @test let + nodes = [ + 0.0 1; + 1 0; + 0 1; + 1 1; + 1 2; + 2 4 + ]' + + cells = [ + 1 2 3; + 2 3 4 + ]' cellmat = [1, 1] - bfaces = [1 2; - 1 3; - 2 4]' + bfaces = [ + 1 2; + 1 3; + 2 4 + ]' bfacemat = [1, 1] grid = simplexgrid(nodes, cells, cellmat, bfaces, bfacemat) - !isconsistent(grid;warnonly=true) + !isconsistent(grid; warnonly = true) end function test_prepare_edges() ## Compared with pdelib; have more of these - nodes = [0.0 1; - 1 0; - 0 1; - 1 1]' - - cells = [1 2 3; - 2 3 4]' + nodes = [ + 0.0 1; + 1 0; + 0 1; + 1 1 + ]' + + cells = [ + 1 2 3; + 2 3 4 + ]' cellmat = [1, 1] - bfaces = [1 2; - 1 3; - 2 4]' + bfaces = [ + 1 2; + 1 3; + 2 4 + ]' bfacemat = [1, 1] @@ -152,14 +170,16 @@ end @test let g = simplexgrid(0:2, 0:2) bfn = g[BFaceNormals] - bfn == [0.0 -1.0; - -1.0 0.0; - 0.0 -1.0; - 1.0 0.0; - 0.0 1.0; - -1.0 0.0; - 1.0 0.0; - 0.0 1.0]' + bfn == [ + 0.0 -1.0; + -1.0 0.0; + 0.0 -1.0; + 1.0 0.0; + 0.0 1.0; + -1.0 0.0; + 1.0 0.0; + 0.0 1.0 + ]' end @test let @@ -181,25 +201,25 @@ end end @testset "Base.map" begin - X=0:0.1:1 - fx(x)=x - fxy(x,y)=x+y - fxyz(x,y,z)=x+y+z - fv(v)=sum(v) - gx=simplexgrid(X) - gxy=simplexgrid(X,X) - gxyz=simplexgrid(X,X,X) - @test map(fx,gx)==map(fv,gx) - @test map(fxy,gxy)==map(fv,gxy) - @test map(fxyz,gxyz)==map(fv,gxyz) - - vfx(x)=[x] - vfxy(x,y)=[x,y] - vfxyz(x,y,z)=[x,y,z] - vfv(v)=v - @test map(vfx,gx)==map(x->[vfv(x)],gx) - @test map(vfxy,gxy)==map(vfv,gxy) - @test map(vfxyz,gxyz)==map(vfv,gxyz) + X = 0:0.1:1 + fx(x) = x + fxy(x, y) = x + y + fxyz(x, y, z) = x + y + z + fv(v) = sum(v) + gx = simplexgrid(X) + gxy = simplexgrid(X, X) + gxyz = simplexgrid(X, X, X) + @test map(fx, gx) == map(fv, gx) + @test map(fxy, gxy) == map(fv, gxy) + @test map(fxyz, gxyz) == map(fv, gxyz) + + vfx(x) = [x] + vfxy(x, y) = [x, y] + vfxyz(x, y, z) = [x, y, z] + vfv(v) = v + @test map(vfx, gx) == map(x -> [vfv(x)], gx) + @test map(vfxy, gxy) == map(vfv, gxy) + @test map(vfxyz, gxyz) == map(vfv, gxyz) end function testrw(grid, format; confidence = :full, kwargs...) @@ -207,14 +227,14 @@ function testrw(grid, format; confidence = :full, kwargs...) ftmp = tempname() * "." * format write(ftmp, grid; kwargs...) grid1 = simplexgrid(ftmp) - seemingly_equal(grid1, grid; confidence = confidence) + return seemingly_equal(grid1, grid; confidence = confidence) end @testset "Read/Write sg" begin X = collect(0:0.05:1) for version in [v"2.1", v"2.2"] @test testrw(simplexgrid(X), "sg"; version) - @test testrw(simplexgrid(X, X), "sg";version) + @test testrw(simplexgrid(X, X), "sg"; version) @test testrw(simplexgrid(X, X, X), "sg"; version) end end @@ -258,7 +278,7 @@ end cellmask!(g, [-1, 0, 0], [0, 1, 1], 2) cellmask!(g, [1, 0, 0], [2, 1, 1], 2) # Mark interior region 3 with region 2 - cellmask!(g, [-0.75,0.25,0.25], [-0.25,0.75,0.75], 3) + cellmask!(g, [-0.75, 0.25, 0.25], [-0.25, 0.75, 0.75], 3) # add new interface elements bfacemask!(g, [-1, 0, 1], [2, 1, 1], 3; allow_new = true) @@ -269,7 +289,7 @@ end subgrid(g, [region]) end sub2 = subgen(; region = 3) - + @test numbers_match(subgen(), 756, 3000, 950) X = collect(0:0.25:1) @@ -284,12 +304,12 @@ end @testset "ParentGridRelation-SubGrid" begin ## generate a subgrid grid = grid_unitsquare(Triangle2D) - grid[CellRegions] = Int32[1,2,2,1] + grid[CellRegions] = Int32[1, 2, 2, 1] sgrid = subgrid(grid, [1]) @test sgrid[ParentGridRelation] == SubGrid{ON_CELLS} ## check if CellParents are assigned correctly - @test sgrid[CellParents] == [1,4] + @test sgrid[CellParents] == [1, 4] ## check if FaceNodes couple correctly with FaceNodes in parent grid facenodes = sgrid[FaceNodes] @@ -298,27 +318,27 @@ end parentfaces = sgrid[FaceParents] parentbfaces = sgrid[BFaceParents] @test all(parentnodes[facenodes] .== grid[FaceNodes][:, parentfaces]) - @test all(parentnodes[bfacenodes] .== grid[BFaceNodes][:,parentbfaces]) + @test all(parentnodes[bfacenodes] .== grid[BFaceNodes][:, parentbfaces]) end @testset "ParentGridRelation-RefinedGrid" begin - ## generate a refined + ## generate a refined grid = grid_unitsquare(Triangle2D) - + ## check uniform refinement rgrid = uniform_refine(grid) @test rgrid[ParentGridRelation] == RefinedGrid ## check if CellParents and BFaceParents are set - @test length(rgrid[CellParents]) == 4*num_cells(grid) - @test length(rgrid[BFaceParents]) == 2*num_bfaces(grid) + @test length(rgrid[CellParents]) == 4 * num_cells(grid) + @test length(rgrid[BFaceParents]) == 2 * num_bfaces(grid) ## check barycentric refinement rgrid = barycentric_refine(grid) @test rgrid[ParentGridRelation] == RefinedGrid ## check if CellParents and BFaceParents are set - @test length(rgrid[CellParents]) == 3*num_cells(grid) + @test length(rgrid[CellParents]) == 3 * num_cells(grid) @test length(rgrid[BFaceParents]) == num_bfaces(grid) @@ -353,7 +373,7 @@ function tglue(; dim = 2, breg = 0) g2 = simplexgrid(X2, Y2, Z2) end - glue(g1, g2; interface = breg) + return glue(g1, g2; interface = breg) end @testset "Glue" begin @@ -381,8 +401,10 @@ function voronoitest() g = simplexgrid(0:0.1:1) @test g[VoronoiFaceCenters] ≈ [0.05 0.15 0.25 0.35 0.45 0.55 0.65 0.75 0.85 0.95] g = simplexgrid(0:0.5:1, 0:0.5:1) - @test g[VoronoiFaceCenters] ≈ [0.25 0.5 0.25 0.25 0.0 0.75 1.0 0.75 0.75 0.5 0.25 0.25 0.0 1.0 0.75 0.75; - 0.0 0.25 0.25 0.5 0.25 0.0 0.25 0.25 0.5 0.75 0.75 1.0 0.75 0.75 0.75 1.0] + return @test g[VoronoiFaceCenters] ≈ [ + 0.25 0.5 0.25 0.25 0.0 0.75 1.0 0.75 0.75 0.5 0.25 0.25 0.0 1.0 0.75 0.75; + 0.0 0.25 0.25 0.5 0.25 0.0 0.25 0.25 0.5 0.75 0.75 1.0 0.75 0.75 0.75 1.0 + ] end @testset "Voronoi" begin @@ -448,10 +470,12 @@ end point_data = map((x, y, z) -> (x + y + z), g) field_data = [1.0, 2, 3, 4, 5, 6] - writeVTK("testfile_writevtk.vtu", g; - cellregions = g[CellRegions], - point_data = point_data, - field_data = field_data) + writeVTK( + "testfile_writevtk.vtu", g; + cellregions = g[CellRegions], + point_data = point_data, + field_data = field_data + ) sha_code = open("testfile_writevtk.vtu") do f sha256(f) @@ -461,9 +485,9 @@ end end -if VERSION < v"1.12.0-DEV.0" - notebooks = ["pluto-partitioning.jl"] - @testset "Notebooks" begin - @testscripts(joinpath(@__DIR__, "..", "examples"), notebooks) - end +if VERSION < v"1.12.0-DEV.0" + notebooks = ["pluto-partitioning.jl"] + @testset "Notebooks" begin + @testscripts(joinpath(@__DIR__, "..", "examples"), notebooks) + end end diff --git a/test/test_gridstuff.jl b/test/test_gridstuff.jl index 47a0368e..373373b8 100644 --- a/test/test_gridstuff.jl +++ b/test/test_gridstuff.jl @@ -1,35 +1,34 @@ - # using ExtendableSparse # using ExtendableGrids function get_testgrid(::Type{<:Edge1D}) - X=collect(0:0.05:1) - simplexgrid(X) + X = collect(0:0.05:1) + return simplexgrid(X) end function get_testgrid(::Type{<:Triangle2D}) - X=collect(0:0.05:1) - Y=collect(0:0.05:1) - simplexgrid(X,Y) + X = collect(0:0.05:1) + Y = collect(0:0.05:1) + return simplexgrid(X, Y) end function get_testgrid(::Type{<:Tetrahedron3D}) - X=collect(0:0.1:1) - Y=collect(0:0.1:1) - Z=collect(0:0.1:1) - simplexgrid(X,Y,Z) + X = collect(0:0.1:1) + Y = collect(0:0.1:1) + Z = collect(0:0.1:1) + return simplexgrid(X, Y, Z) end function get_testgrid(::Type{Parallelogram2D}) - return uniform_refine(grid_unitsquare(Parallelogram2D),2) + return uniform_refine(grid_unitsquare(Parallelogram2D), 2) end function get_testgrid(::Type{Parallelepiped3D}) - return uniform_refine(grid_unitcube(Parallelepiped3D),2) + return uniform_refine(grid_unitcube(Parallelepiped3D), 2) end -function get_testgrid(::Type{Triangle2D},::Type{Parallelogram2D}) - return uniform_refine(grid_unitsquare_mixedgeometries(),1) +function get_testgrid(::Type{Triangle2D}, ::Type{Parallelogram2D}) + return uniform_refine(grid_unitsquare_mixedgeometries(), 1) end function check_enumeration_consistency(G::Type{<:AbstractElementGeometry}, CellItems, ItemNodes, item_enum_rule) @@ -46,19 +45,19 @@ function check_enumeration_consistency(G::Type{<:AbstractElementGeometry}, CellI nnodes_per_item = max_num_targets_per_source(xItemNodes) ncells = num_sources(xCellNodes) - @assert nitems_per_cell == size(item_rule,2) - @assert nnodes_per_item == size(item_rule,1) + @assert nitems_per_cell == size(item_rule, 2) + @assert nnodes_per_item == size(item_rule, 1) item::Int = 0 everything_is_consistent = true itemnodes_found = zeros(Bool, nnodes_per_item) - for cell = 1 : ncells - for i = 1 : nitems_per_cell - item = xCellItems[i,cell] - fill!(itemnodes_found,false) - for n = 1 : nnodes_per_item - for k = 1 : nnodes_per_item - if xItemNodes[n,item] == xCellNodes[item_rule[k,i],cell] + for cell in 1:ncells + for i in 1:nitems_per_cell + item = xCellItems[i, cell] + fill!(itemnodes_found, false) + for n in 1:nnodes_per_item + for k in 1:nnodes_per_item + if xItemNodes[n, item] == xCellNodes[item_rule[k, i], cell] itemnodes_found[n] = true break end @@ -81,26 +80,26 @@ function check_cellfinder(xgrid) # @show xCoordinates xCellNodes = xgrid[CellNodes] edim = dim_element(EG[1]) - CF::CellFinder{Float64,Int32} = CellFinder(xgrid) + CF::CellFinder{Float64, Int32} = CellFinder(xgrid) cell_to_find = num_sources(xCellNodes) - 1 # compute midpoint of cell_to_find - x_source = zeros(Float64,edim) - for j = 1 : edim, k = 1 : num_targets(xCellNodes,cell_to_find) - x_source[j] += xCoordinates[j,xCellNodes[k,cell_to_find]] + 1e-6 + x_source = zeros(Float64, edim) + for j in 1:edim, k in 1:num_targets(xCellNodes, cell_to_find) + x_source[j] += xCoordinates[j, xCellNodes[k, cell_to_find]] + 1.0e-6 end - x_source ./= num_targets(xCellNodes,cell_to_find) + x_source ./= num_targets(xCellNodes, cell_to_find) @show x_source # find cell by local strategy - xref = zeros(Float64,edim+1) + xref = zeros(Float64, edim + 1) cell = gFindLocal!(xref, CF, x_source; icellstart = 1) - + # check xref - x = zeros(Float64,edim) + x = zeros(Float64, edim) L2G = L2GTransformer(xgrid[CellGeometries][cell], xgrid, ON_CELLS) - update_trafo!(L2G,cell) - eval_trafo!(x,L2G,xref) - + update_trafo!(L2G, cell) + eval_trafo!(x, L2G, xref) + @info "... found x=$x in cell = $cell by local search (and had to find x=$x_source in cell=$cell_to_find)" @assert cell == cell_to_find @@ -109,19 +108,18 @@ function check_cellfinder(xgrid) cell = gFindBruteForce!(xref, CF, x_source) # check xref - x = zeros(Float64,edim) - update_trafo!(L2G,cell) - eval_trafo!(x,L2G,xref) - + x = zeros(Float64, edim) + update_trafo!(L2G, cell) + eval_trafo!(x, L2G, xref) + @info "... found x=$x in cell = $cell by brute force (and had to find x=$x_source in cell=$cell_to_find)" @assert cell == cell_to_find - return (cell == cell_to_find) && (sqrt(sum((x - x_source).^2)) < 1e-14) + return (cell == cell_to_find) && (sqrt(sum((x - x_source) .^ 2)) < 1.0e-14) end - function check_uniform_refinement(xgrid, bary::Bool) xgrid2 = bary ? barycentric_refine(xgrid) : uniform_refine(xgrid) minvol = minimum(xgrid2[CellVolumes]) @@ -136,7 +134,7 @@ function run_grid_tests() @test check_enumeration_consistency(Parallelogram2D, CellFaces, FaceNodes, local_cellfacenodes) @test check_enumeration_consistency(Tetrahedron3D, CellFaces, FaceNodes, local_cellfacenodes) @test check_enumeration_consistency(Parallelepiped3D, CellFaces, FaceNodes, local_cellfacenodes) - + # test EDGE enumerations @test check_enumeration_consistency(Tetrahedron3D, CellEdges, EdgeNodes, local_celledgenodes) @test check_enumeration_consistency(Parallelepiped3D, CellEdges, EdgeNodes, local_celledgenodes) @@ -148,25 +146,29 @@ function run_grid_tests() @test check_cellfinder(get_testgrid(Edge1D)) @test check_cellfinder(get_testgrid(Triangle2D)) @test check_cellfinder(get_testgrid(Parallelogram2D)) - @test check_cellfinder(get_testgrid(Triangle2D,Parallelogram2D)) + @test check_cellfinder(get_testgrid(Triangle2D, Parallelogram2D)) @test check_cellfinder(get_testgrid(Tetrahedron3D)) @test check_cellfinder(get_testgrid(Parallelepiped3D)) X = LinRange(0, 1, 10) grid = simplexgrid(X, X) @test all(grid[CellVolumes] .> 0) - sub = subgrid(grid, [2], transform=function(a,b) a[1]=b[2] end, boundary=true) + sub = subgrid( + grid, [2], transform = function(a, b) + return a[1] = b[2] + end, boundary = true + ) @test check_cellfinder(sub) grid = simplexgrid(X, X, X) @test all(grid[CellVolumes] .> 0) - sub = subgrid(grid, [5], boundary=true) + sub = subgrid(grid, [5], boundary = true) @test check_cellfinder(sub) - + @test check_uniform_refinement(reference_domain(Triangle2D), false) @test check_uniform_refinement(reference_domain(Triangle2D), true) @test check_uniform_refinement(reference_domain(Parallelogram2D), false) @test check_uniform_refinement(reference_domain(Tetrahedron3D), false) - @test check_uniform_refinement(reference_domain(Tetrahedron3D), true) + return @test check_uniform_refinement(reference_domain(Tetrahedron3D), true) end diff --git a/test/testbinned.jl b/test/testbinned.jl index bcfe8a25..838ed114 100644 --- a/test/testbinned.jl +++ b/test/testbinned.jl @@ -3,19 +3,19 @@ function testbinning(a) n = size(a, 2) idx = rand(1:n, n ÷ 4) a1 = a[:, idx] - + bpl = BinnedPointList(dim) - for i = 1:size(a, 2) + for i in 1:size(a, 2) insert!(bpl, a[:, i]) end - - for i = 1:size(a1, 2) + + for i in 1:size(a1, 2) ix = insert!(bpl, a1[:, i]) if ix != idx[i] return false end end - true + return true end @test testbinning(rand(1, 10)) diff --git a/test/tritet.jl b/test/tritet.jl index a6e20236..9418a027 100644 --- a/test/tritet.jl +++ b/test/tritet.jl @@ -8,88 +8,106 @@ function example_domain_regions(; minangle = 20) triin.regionlist = Matrix{Cdouble}([0.2 0.8; 0.2 0.2; 1 2; 0.01 0.05]) angle = @sprintf("%.15f", minangle) (triout, vorout) = triangulate("paAq$(angle)Q", triin) - simplexgrid(triout) + return simplexgrid(triout) end -function example_domain_holes(;minangle = 20, maxarea = 0.001) +function example_domain_holes(; minangle = 20, maxarea = 0.001) triin = Triangulate.TriangulateIO() - triin.pointlist = Matrix{Cdouble}([0.0 0.0; - 1.0 0.0; - 1.0 1.0; - 0.0 1.0; - 0.2 0.2; - 0.3 0.2; - 0.3 0.3; - 0.2 0.3; - 0.6 0.6; - 0.7 0.6; - 0.7 0.7; - 0.6 0.7]') + triin.pointlist = Matrix{Cdouble}( + [ + 0.0 0.0; + 1.0 0.0; + 1.0 1.0; + 0.0 1.0; + 0.2 0.2; + 0.3 0.2; + 0.3 0.3; + 0.2 0.3; + 0.6 0.6; + 0.7 0.6; + 0.7 0.7; + 0.6 0.7 + ]' + ) triin.segmentlist = Matrix{Cint}([1 2; 2 3; 3 4; 4 1; 5 6; 6 7; 7 8; 8 5; 9 10; 10 11; 11 12; 12 9]') triin.segmentmarkerlist = Vector{Int32}([1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3]) triin.holelist = [0.25 0.25; 0.65 0.65]' area = @sprintf("%.15f", maxarea) # Don't use exponential format! angle = @sprintf("%.15f", minangle) (triout, vorout) = triangulate("pa$(area)q$(angle)Q", triin) - simplexgrid(triout) + return simplexgrid(triout) end @testset "Triangulate" begin - @test isa(example_domain_regions(),ExtendableGrid) - @test isa(example_domain_holes(),ExtendableGrid) + @test isa(example_domain_regions(), ExtendableGrid) + @test isa(example_domain_holes(), ExtendableGrid) end function prism(vol = 2) input = TetGen.RawTetGenIO{Cdouble}() - input.pointlist = [0 0 0; - 1 0 0; - 0 1 0; - 0 0 1; - 1 0 1; - 0 1 1]' + input.pointlist = [ + 0 0 0; + 1 0 0; + 0 1 0; + 0 0 1; + 1 0 1; + 0 1 1 + ]' - TetGen.facetlist!(input, [[1, 2, 3], - [4, 5, 6], - [1, 2, 5, 4], - [2, 3, 6, 5], - [3, 1, 4, 6]]) + TetGen.facetlist!( + input, [ + [1, 2, 3], + [4, 5, 6], + [1, 2, 5, 4], + [2, 3, 6, 5], + [3, 1, 4, 6], + ] + ) - simplexgrid(tetrahedralize(input, "pQa$(vol)")) + return simplexgrid(tetrahedralize(input, "pQa$(vol)")) end function material_prism(; vol1 = 0.01, vol2 = 0.1) input = TetGen.RawTetGenIO{Cdouble}() - input.pointlist = [0 0 0; - 1 0 0; - 0 1 0; - 0 0 1; - 1 0 1; - 0 1 1; - 0 0 2; - 1 0 2; - 0 1 2]' + input.pointlist = [ + 0 0 0; + 1 0 0; + 0 1 0; + 0 0 1; + 1 0 1; + 0 1 1; + 0 0 2; + 1 0 2; + 0 1 2 + ]' - TetGen.facetlist!(input, - [[1, 2, 3], - [7, 8, 9], - [1, 2, 5, 4], - [2, 3, 6, 5], - [3, 1, 4, 6], - [1, 2, 5, 4] .+ 3, - [2, 3, 6, 5] .+ 3, - [3, 1, 4, 6] .+ 3]) + TetGen.facetlist!( + input, + [ + [1, 2, 3], + [7, 8, 9], + [1, 2, 5, 4], + [2, 3, 6, 5], + [3, 1, 4, 6], + [1, 2, 5, 4] .+ 3, + [2, 3, 6, 5] .+ 3, + [3, 1, 4, 6] .+ 3, + ] + ) input.facetmarkerlist = [1, 2, 3, 3, 3, 3, 3, 3] - input.regionlist = [0.1 0.1 0.5 1 vol1; - 0.1 0.1 1.5 2 vol2]' + input.regionlist = [ + 0.1 0.1 0.5 1 vol1; + 0.1 0.1 1.5 2 vol2 + ]' - simplexgrid(tetrahedralize(input, "paAqQ")) + return simplexgrid(tetrahedralize(input, "paAqQ")) end @testset "TetGen" begin - @test isa(prism(),ExtendableGrid) - @test isa(material_prism(),ExtendableGrid) + @test isa(prism(), ExtendableGrid) + @test isa(material_prism(), ExtendableGrid) end From 1d07ac6f66a9412465631d83a32eb2475d26bf20 Mon Sep 17 00:00:00 2001 From: Jan Philipp Thiele <54978337+jpthiele@users.noreply.github.com> Date: Sat, 7 Dec 2024 08:48:37 +0100 Subject: [PATCH 2/5] Add runic pre-commit check --- .pre-commit-config.yaml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 83a49f5a..c1e860e2 100755 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -19,3 +19,7 @@ repos: rev: v2.2.4 hooks: - id: codespell +- repo: https://github.com/fredrikekre/runic-pre-commit + rev: v1.0.0 + hooks: + - id: runic From 106e6b4be29253012f914ec5c41ba7273e89bcef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Sat, 7 Dec 2024 14:08:37 +0100 Subject: [PATCH 3/5] some formatting updates --- examples/examples1d.jl | 4 ++-- examples/examples2d.jl | 6 +++--- examples/examples3d.jl | 6 +++--- examples/gmsh.jl | 4 ++-- examples/vtk_write.jl | 6 +++--- 5 files changed, 13 insertions(+), 13 deletions(-) diff --git a/examples/examples1d.jl b/examples/examples1d.jl index 7c4709a5..e95694d4 100644 --- a/examples/examples1d.jl +++ b/examples/examples1d.jl @@ -53,7 +53,7 @@ function runtests() @test numbers_match(interval_localref(), 27, 26, 2) @test numbers_match(interval_multiregion(), 21, 20, 3) @test numbers_match(interval_subgrid(), 51, 50, 2) - nothing + return nothing end # Plot generation @@ -67,5 +67,5 @@ function generateplots(picdir; Plotter = nothing) Plotter.save(joinpath(picdir, "interval_multiregion.png"), gridplot(interval_multiregion(); Plotter, size, legend)) Plotter.save(joinpath(picdir, "interval_subgrid.png"), gridplot(interval_subgrid(); Plotter, size, legend)) end - nothing + return nothing end diff --git a/examples/examples2d.jl b/examples/examples2d.jl index 97dcc549..71cb4bda 100644 --- a/examples/examples2d.jl +++ b/examples/examples2d.jl @@ -34,7 +34,7 @@ function rectangle_multiregion() bfacemask!(grid, [0.0, 0.0], [0.0, 0.5], 5) bfacemask!(grid, [1.0, 0.0], [1.0, 0.5], 6) bfacemask!(grid, [0.0, 0.5], [1.0, 0.5], 7) -grid + return grid end # ![](rectangle_multiregion.png) @@ -108,7 +108,7 @@ function runtests() @test numbers_match(g, 187, 306, 66) @test numbers_match(sg, 17, 16, 0) @test issorted(view(sg[Coordinates], 1, :)) -nothing + return nothing end # Plot generation @@ -130,5 +130,5 @@ function generateplots(picdir; Plotter = nothing) fname = joinpath(picdir, "sorted_subgrid.png") Plotter.save(fname, reveal(p)) end - nothing + return nothing end diff --git a/examples/examples3d.jl b/examples/examples3d.jl index 3e28b2ac..220660b3 100644 --- a/examples/examples3d.jl +++ b/examples/examples3d.jl @@ -47,16 +47,16 @@ function runtests() @test numbers_match(quadrilateral(), 330, 1200, 440) @test mask_bedges() @test numbers_match(cross3d(), 189, 480, 344) -nothing + return nothing end # Plot generation using GridVisualize function generateplots(picdir; Plotter = nothing) - if isdefined(Plotter, :Makie) + if isdefined(Plotter, :Makie) size = (400, 400) Plotter.save(joinpath(picdir, "quadrilateral.png"), gridplot(quadrilateral(); Plotter, size)) Plotter.save(joinpath(picdir, "cross3d.png"), gridplot(cross3d(); Plotter, size)) end -nothing + return nothing end diff --git a/examples/gmsh.jl b/examples/gmsh.jl index 96c5b14c..85585315 100644 --- a/examples/gmsh.jl +++ b/examples/gmsh.jl @@ -314,7 +314,7 @@ function runtests() @test ok(gmsh_t1()) @test ok(gmsh_t4()) @test ok(gmsh_t5()) -nothing + return nothing end # Plot generation @@ -326,5 +326,5 @@ function generateplots(picdir; Plotter = nothing) Plotter.save(joinpath(picdir, "gmsh_t4.png"), gridplot(gmsh_t4(); Plotter, size)) Plotter.save(joinpath(picdir, "gmsh_t5.png"), gridplot(gmsh_t5(); Plotter, size)) end -nothing + return nothing end diff --git a/examples/vtk_write.jl b/examples/vtk_write.jl index 3ee038f7..863d48c0 100644 --- a/examples/vtk_write.jl +++ b/examples/vtk_write.jl @@ -16,7 +16,7 @@ function grid3D_test(filename::String) cell_data = cell_data, field_data = field_data ) -nothing + return nothing end @@ -35,7 +35,7 @@ function grid2D_test(filename::String) cell_data = cell_data, field_data = field_data ) -nothing + return nothing end function grid1D_test(filename::String) @@ -53,5 +53,5 @@ function grid1D_test(filename::String) cell_data = cell_data, field_data = field_data ) -nothing + return nothing end From 708adde063790f2a82405283db1ea6dea654d0f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Sat, 7 Dec 2024 14:35:03 +0100 Subject: [PATCH 4/5] use anonymous function syntax for transform parameter --- src/subgrid.jl | 6 ++---- test/test_gridstuff.jl | 6 +++--- 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/src/subgrid.jl b/src/subgrid.jl index 2cf7706f..de19dd3d 100644 --- a/src/subgrid.jl +++ b/src/subgrid.jl @@ -80,7 +80,7 @@ Base.isless(x::XIPair, y::XIPair) = (x.x < y.x) """ subgrid(parent, subregions::AbstractArray; - transform::T=function(a,b) @views a.=b[1:length(a)] end, + transform::T = (a, b) -> a .= b[1:length(a)], boundary=false, coordinatesystem=codim1_coordinatesystem(parent[CoordinateSystem]), project=true) where T @@ -105,9 +105,7 @@ A subgrid is of type `ExtendableGrid` and stores two additional components: function subgrid( parent, subregions::AbstractArray; - transform::T = function(a, b) - return @views a .= b[1:length(a)] - end, + transform::T = (a, b) -> a .= b[1:length(a)], boundary = false, support = ON_CELLS, project = true, diff --git a/test/test_gridstuff.jl b/test/test_gridstuff.jl index 373373b8..6d628dbb 100644 --- a/test/test_gridstuff.jl +++ b/test/test_gridstuff.jl @@ -154,9 +154,9 @@ function run_grid_tests() grid = simplexgrid(X, X) @test all(grid[CellVolumes] .> 0) sub = subgrid( - grid, [2], transform = function(a, b) - return a[1] = b[2] - end, boundary = true + grid, [2], + transform = (a, b) -> a[1] = b[2], + boundary = true ) @test check_cellfinder(sub) From f6b6ba0596f08b3186e148b2f649056fb64908f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrgen=20Fuhrmann?= Date: Sat, 7 Dec 2024 16:22:36 +0100 Subject: [PATCH 5/5] Add runic badge --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 58a7bb19..e236926b 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,7 @@ [![Build status](https://github.com/WIAS-PDELib/ExtendableGrids.jl/workflows/linux-macos-windows/badge.svg)](https://github.com/WIAS-PDELib/ExtendableGrids.jl/actions) [![](https://img.shields.io/badge/docs-stable-blue.svg)](https://WIAS-PDELib.github.io/ExtendableGrids.jl/stable) [![](https://img.shields.io/badge/docs-dev-blue.svg)](https://WIAS-PDELib.github.io/ExtendableGrids.jl/dev) +[![code style: runic](https://img.shields.io/badge/code_style-%E1%9A%B1%E1%9A%A2%E1%9A%BE%E1%9B%81%E1%9A%B2-black)](https://github.com/fredrikekre/Runic.jl) Provide container structure `ExtendableGrid` with type stable content access and lazy content creation holding data for discretization