Skip to content

Commit

Permalink
add gmsh geometry description test
Browse files Browse the repository at this point in the history
  • Loading branch information
j-fu committed Nov 15, 2023
1 parent 108553f commit 248ef6c
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 108 deletions.
136 changes: 136 additions & 0 deletions test/gmsh.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
using Gmsh: gmsh
gmsh.initialize()

@testset "access gmsh" begin
gmsh.option.setNumber("General.Terminal", 1)
gmsh.model.add("t1")

lc = 1e-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)
gmsh.model.geo.addLine(3, 2, 2)
gmsh.model.geo.addLine(3, p4, 3)
gmsh.model.geo.addLine(4, 1, p4)

gmsh.model.geo.addCurveLoop([4, 1, -2, 3], 1)
gmsh.model.geo.addPlaneSurface([1], 1)

gmsh.model.geo.synchronize()

gmsh.model.addPhysicalGroup(0, [1, 2], 1)
gmsh.model.addPhysicalGroup(1, [1, 2], 2)
gmsh.model.addPhysicalGroup(2, [1], 6)

gmsh.model.setPhysicalName(2, 6, "My surface")

gmsh.model.mesh.generate(2)
grid = ExtendableGrids.simplexgrid_from_gmsh(gmsh.model)
@test testgrid(grid, (404, 726, 80))
gmsh.clear()
end

@testset "Read/write simplex gmsh 2d / 3d" begin
gmsh.option.setNumber("General.Terminal", 0)
gmsh.option.setNumber("General.Verbosity", 0)

path = ""

X = collect(0:0.02:2)
Y = collect(0:2:4)
grid1 = simplexgrid(X, Y) #ExtendableGrids.simplexgrid_from_gmsh(path*"sto_2d.msh")
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "testfile.msh"; Tc = Float32, Ti = Int64)
#gmsh.finalize()
@test seemingly_equal(grid2, grid1; sort = true, confidence = :low)
@test seemingly_equal(grid2, grid1; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_2d.msh"; Tc = Float64, Ti = Int64)
gmsh.clear()
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "testfile.msh"; Tc = Float64, Ti = Int64)
#gmsh.finalize()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_3d.msh"; Tc = Float32, Ti = Int64)
gmsh.clear()
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "testfile.msh"; Tc = Float64, Ti = Int32)
#gmsh.finalize()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_2d.msh")
gmsh.clear()
#grid2 =
#simplexgrid([0, 1, 2], [3, 4, 5])
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_3d.msh"; Tc = Float32, Ti = Int32)
#gmsh.finalize()

@test !seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test !seemingly_equal(grid1, grid2; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh("testmesh.gmsh"; incomplete = true)
ExtendableGrids.seal!(grid1; encode = false)
gmsh.clear()
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = "completed_testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh("completed_testfile.msh")

gmsh.clear()

grid3 = ExtendableGrids.simplexgrid_from_gmsh("testmesh.gmsh"; incomplete = true)
ExtendableGrids.seal!(grid3; encode = true)

gmsh.clear()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)
@test seemingly_equal(grid1, grid3; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid3; sort = true, confidence = :full)

x = collect(LinRange(0, 1, 50))
grid1 = simplexgrid(x, x)
grid1[BFaceRegions] = ones(Int32, length(grid1[BFaceRegions])) #num_faces(grid1))
grid2 = simplexgrid(x, x)
grid3 = simplexgrid(x, x)
ExtendableGrids.seal!(grid2)
ExtendableGrids.seal!(grid3; encode = false)

gmsh.finalize()

@test seemingly_equal(grid2, grid1; sort = true, confidence = :low)
@test seemingly_equal(grid2, grid1; sort = true, confidence = :full)
@test seemingly_equal(grid3, grid1; sort = true, confidence = :low)
@test seemingly_equal(grid3, grid1; sort = true, confidence = :full)
end

@testset "Read/write mixed gmsh 2d" begin
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)
gmsh.option.setNumber("General.Verbosity", 0)

path = ""
grid1 = ExtendableGrids.mixedgrid_from_gmsh(path * "mixedgrid_2d.msh"; Tc = Float64, Ti = Int64)
gmsh.clear()
ExtendableGrids.load_mixedgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.mixedgrid_from_gmsh(path * "testfile.msh"; Tc = Float32, Ti = UInt64)
gmsh.finalize()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)
end
112 changes: 4 additions & 108 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,117 +2,15 @@ ENV["MPLBACKEND"] = "agg"

using Test, ExtendableGrids, GridVisualize, SHA, SimplexGridFactory, Triangulate
import StatsBase
using Gmsh #: gmsh
using StatsBase: countmap

import CairoMakie

CairoMakie.activate!(; type = "svg", visible = false)

@testset "Read/write simplex gmsh 2d / 3d" begin
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)
gmsh.option.setNumber("General.Verbosity", 0)

path = ""

X = collect(0:0.02:2)
Y = collect(0:2:4)
grid1 = simplexgrid(X, Y) #ExtendableGrids.simplexgrid_from_gmsh(path*"sto_2d.msh")
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "testfile.msh"; Tc = Float32, Ti = Int64)
#gmsh.finalize()
@test seemingly_equal(grid2, grid1; sort = true, confidence = :low)
@test seemingly_equal(grid2, grid1; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_2d.msh"; Tc = Float64, Ti = Int64)
gmsh.clear()
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "testfile.msh"; Tc = Float64, Ti = Int64)
#gmsh.finalize()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_3d.msh"; Tc = Float32, Ti = Int64)
gmsh.clear()
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "testfile.msh"; Tc = Float64, Ti = Int32)
#gmsh.finalize()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_2d.msh")
gmsh.clear()
#grid2 =
#simplexgrid([0, 1, 2], [3, 4, 5])
grid2 = ExtendableGrids.simplexgrid_from_gmsh(path * "sto_3d.msh"; Tc = Float32, Ti = Int32)
#gmsh.finalize()

@test !seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test !seemingly_equal(grid1, grid2; sort = true, confidence = :full)

gmsh.clear()

grid1 = ExtendableGrids.simplexgrid_from_gmsh("testmesh.gmsh"; incomplete = true)
ExtendableGrids.seal!(grid1; encode = false)
gmsh.clear()
ExtendableGrids.load_simplexgrid_to_gmsh(grid1; filename = "completed_testfile.msh")
grid2 = ExtendableGrids.simplexgrid_from_gmsh("completed_testfile.msh")

gmsh.clear()

grid3 = ExtendableGrids.simplexgrid_from_gmsh("testmesh.gmsh"; incomplete = true)
ExtendableGrids.seal!(grid3; encode = true)

gmsh.clear()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)
@test seemingly_equal(grid1, grid3; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid3; sort = true, confidence = :full)

x = collect(LinRange(0, 1, 50))
grid1 = simplexgrid(x, x)
grid1[BFaceRegions] = ones(Int32, length(grid1[BFaceRegions])) #num_faces(grid1))
grid2 = simplexgrid(x, x)
grid3 = simplexgrid(x, x)
ExtendableGrids.seal!(grid2)
ExtendableGrids.seal!(grid3; encode = false)

gmsh.finalize()

@test seemingly_equal(grid2, grid1; sort = true, confidence = :low)
@test seemingly_equal(grid2, grid1; sort = true, confidence = :full)
@test seemingly_equal(grid3, grid1; sort = true, confidence = :low)
@test seemingly_equal(grid3, grid1; sort = true, confidence = :full)
end

@testset "Read/write mixed gmsh 2d" begin
gmsh.initialize()
gmsh.option.setNumber("General.Terminal", 0)
gmsh.option.setNumber("General.Verbosity", 0)

path = ""
grid1 = ExtendableGrids.mixedgrid_from_gmsh(path * "mixedgrid_2d.msh"; Tc = Float64, Ti = Int64)
gmsh.clear()
ExtendableGrids.load_mixedgrid_to_gmsh(grid1; filename = path * "testfile.msh")
grid2 = ExtendableGrids.mixedgrid_from_gmsh(path * "testfile.msh"; Tc = Float32, Ti = UInt64)
gmsh.finalize()

@test seemingly_equal(grid1, grid2; sort = true, confidence = :low)
@test seemingly_equal(grid1, grid2; sort = true, confidence = :full)
function testgrid(grid, testdata)
(num_nodes(grid), num_cells(grid), num_bfaces(grid)) == testdata
end

### Original tests:

@testset "Geomspace" begin
function test_geomspace()
X1 = geomspace(2.0, 3.0, 0.2, 0.2)
Expand Down Expand Up @@ -230,10 +128,6 @@ end
@test testrw(simplexgrid(X, X, X), "sg")
end

function testgrid(grid, testdata)
(num_nodes(grid), num_cells(grid), num_bfaces(grid)) == testdata
end

examples1d = joinpath(@__DIR__, "..", "examples", "examples1d.jl")
include(examples1d)
examples2d = joinpath(@__DIR__, "..", "examples", "examples2d.jl")
Expand Down Expand Up @@ -453,3 +347,5 @@ end

@test sha_code == "93a31139ccb3ae3017351d7cef0c2639c5def97c9744699543fe8bc58e1ebcea"
end

include("gmsh.jl")

0 comments on commit 248ef6c

Please sign in to comment.