Skip to content

Commit

Permalink
get rid of gmsh.jl (#4)
Browse files Browse the repository at this point in the history
* get rid of gmsh.jl

* add examples dir

* started mixedgrids, cellsets don't work

* Revert "started mixedgrids, cellsets don't work"

This reverts commit 42a4077.
  • Loading branch information
koehlerson authored Feb 8, 2022
1 parent 065d650 commit df077f3
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 28 deletions.
3 changes: 2 additions & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ version = "0.1.0"

[deps]
Ferrite = "c061ca5d-56c9-439f-9c0e-210fe06d3992"
gmsh = "a3f5b5a8-4c56-11eb-214e-350f4127dda5"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
gmsh_jll = "630162c2-fc9b-58b3-9910-8442a8a132e6"
3 changes: 0 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,7 @@ FerriteGmsh tries to simplify the conversion from a gmsh mesh to a Ferrite mesh.

## Installation

In order to use this package, you need to install `gmsh.jl` first

```
]add https://github.com/koehlerson/gmsh.jl.git
]add https://github.com/koehlerson/FerriteGmsh.jl.git
```

Expand Down
161 changes: 161 additions & 0 deletions examples/t2.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
# ------------------------------------------------------------------------------
#
# Gmsh Julia tutorial 2
#
# Transformations, extruded geometries, volumes
#
# ------------------------------------------------------------------------------
using FerriteGmsh

# If ARGS is passed to gmsh.initialize(), Gmsh will parse the command line
# in the same way as the standalone Gmsh app:
gmsh.initialize(append!(["gmsh"], ARGS))

gmsh.model.add("t2")

# Copied from t1.jl...
lc = 1e-2
gmsh.model.geo.addPoint(0, 0, 0, lc, 1)
gmsh.model.geo.addPoint(.1, 0, 0, lc, 2)
gmsh.model.geo.addPoint(.1, .3, 0, lc, 3)
gmsh.model.geo.addPoint(0, .3, 0, lc, 4)
gmsh.model.geo.addLine(1, 2, 1)
gmsh.model.geo.addLine(3, 2, 2)
gmsh.model.geo.addLine(3, 4, 3)
gmsh.model.geo.addLine(4, 1, 4)
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")

# We can then add new points and curves in the same way as we did in `t1.jl':
gmsh.model.geo.addPoint(0, .4, 0, lc, 5)
gmsh.model.geo.addLine(4, 5, 5)


# But Gmsh also provides tools to transform (translate, rotate, etc.)
# elementary entities or copies of elementary entities. Geometrical
# transformations take a vector of pairs of integers as first argument, which
# contains the list of entities, represented by (dimension, tag) pairs. For
# example, the point 5 (dimension=0, tag=5) can be moved by 0.02 to the left
# (dx=-0.02, dy=0, dz=0) with
gmsh.model.geo.translate([(0, 5)], -0.02, 0, 0)
# And it can be further rotated by -Pi/4 around (0, 0.3, 0) (with the rotation
# along the z axis) with:
gmsh.model.geo.rotate([(0, 5)], 0,0.3,0, 0,0,1, -pi/4)


# Note that there are no units in Gmsh: coordinates are just numbers - it's
# up to the user to associate a meaning to them.

# Point 3 can be duplicated and translated by 0.05 along the y axis by using the
# copy() function, which takes a vector of (dim, tag) pairs as input, and
# returns another vector of (dim, tag) pairs:
ov = gmsh.model.geo.copy([(0, 3)])
gmsh.model.geo.translate(ov, 0, 0.05, 0)

# The new point tag is available in ov[0][1], and can be used to create new
# lines:
gmsh.model.geo.addLine(3, ov[1][2], 7)
gmsh.model.geo.addLine(ov[1][2], 5, 8)
gmsh.model.geo.addCurveLoop([5,-8,-7,3], 10)
gmsh.model.geo.addPlaneSurface([10], 11)

# In the same way, we can translate copies of the two surfaces 1 and 11 to the
# right with the following command:
ov = gmsh.model.geo.copy([(2, 1), (2, 11)])
gmsh.model.geo.translate(ov, 0.12, 0, 0)

println("New surfaces ", ov[1][2], " and ", ov[2][2])

# Volumes are the fourth type of elementary entities in Gmsh. In the same way
# one defines curve loops to build surfaces, one has to define surface loops
# (i.e. `shells') to build volumes. The following volume does not have holes and
# thus consists of a single surface loop:
gmsh.model.geo.addPoint(0., 0.3, 0.12, lc, 100)
gmsh.model.geo.addPoint(0.1, 0.3, 0.12, lc, 101)
gmsh.model.geo.addPoint(0.1, 0.35, 0.12, lc, 102)

# We would like to retrieve the coordinates of point 5 to create point 103, so
# we synchronize the model, and use `getValue()'
gmsh.model.geo.synchronize()
xyz = gmsh.model.getValue(0, 5, [])
gmsh.model.geo.addPoint(xyz[1], xyz[2], 0.12, lc, 103)

gmsh.model.geo.addLine(4, 100, 110)
gmsh.model.geo.addLine(3, 101, 111)
gmsh.model.geo.addLine(6, 102, 112)
gmsh.model.geo.addLine(5, 103, 113)
gmsh.model.geo.addLine(103, 100, 114)
gmsh.model.geo.addLine(100, 101, 115)
gmsh.model.geo.addLine(101, 102, 116)
gmsh.model.geo.addLine(102, 103, 117)

gmsh.model.geo.addCurveLoop([115, -111, 3, 110], 118)
gmsh.model.geo.addPlaneSurface([118], 119)
gmsh.model.geo.addCurveLoop([111, 116, -112, -7], 120)
gmsh.model.geo.addPlaneSurface([120], 121)
gmsh.model.geo.addCurveLoop([112, 117, -113, -8], 122)
gmsh.model.geo.addPlaneSurface([122], 123)
gmsh.model.geo.addCurveLoop([114, -110, 5, 113], 124)
gmsh.model.geo.addPlaneSurface([124], 125)
gmsh.model.geo.addCurveLoop([115, 116, 117, 114], 126)
gmsh.model.geo.addPlaneSurface([126], 127)

gmsh.model.geo.addSurfaceLoop([127, 119, 121, 123, 125, 11], 128)
gmsh.model.geo.addVolume([128], 129)

# When a volume can be extruded from a surface, it is usually easier to use the
# `extrude()' function directly instead of creating all the points, curves and
# surfaces by hand. For example, the following command extrudes the surface 11
# along the z axis and automatically creates a new volume (as well as all the
# needed points, curves and surfaces). As expected, the function takes a vector
# of (dim, tag) pairs as input as well as the translation vector, and returns a
# vector of (dim, tag) pairs as output:
ov2 = gmsh.model.geo.extrude([ov[2]], 0, 0, 0.12)


# Mesh sizes associated to geometrical points can be set by passing a vector of
# (dim, tag) pairs for the corresponding points:
gmsh.model.geo.mesh.setSize([(0,103), (0,105), (0,109), (0,102), (0,28),
(0, 24), (0,6), (0,5)], lc * 3)

# We finish by synchronizing the data from the built-in CAD kernel with the Gmsh
# model:
gmsh.model.geo.synchronize()

# We group volumes 129 and 130 in a single physical group with tag `1' and name
# "The volume":
gmsh.model.addPhysicalGroup(3, [129,130], 1)
gmsh.model.setPhysicalName(3, 1, "The volume")

gmsh.model.mesh.generate(3)

# We finally generate and save the mesh:
gmsh.write("t2.msh")

# Note that, if the transformation tools are handy to create complex geometries,
# it is also sometimes useful to generate the `flat' geometry, with an explicit
# representation of all the elementary entities.
#
# With the built-in CAD kernel, this can be achieved by saving the model in the
# `Gmsh Unrolled GEO' format:
#
# gmsh.write("t2.geo_unrolled");
#
# With the OpenCASCADE CAD kernel, unrolling the geometry can be achieved by
# exporting in the `OpenCASCADE BRep' format:
#
# gmsh.write("t2.brep");
#
# (OpenCASCADE geometries can also be exported as STEP files.)

# It is important to note that Gmsh never translates geometry data into a common
# representation: all the operations on a geometrical entity are performed
# natively with the associated CAD kernel. Consequently, one cannot export a
# geometry constructed with the built-in kernel as an OpenCASCADE BRep file; or
# export an OpenCASCADE model as an Unrolled GEO file.
gmsh.finalize()
51 changes: 27 additions & 24 deletions src/FerriteGmsh.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
module FerriteGmsh

using gmsh
using Ferrite
using Reexport

import gmsh_jll
include(gmsh_jll.gmsh_api)
@reexport import .gmsh

const gmshtojuafemcell = Dict("Line 2" => Line,
const gmshtoferritecell = Dict("Line 2" => Line,
"Line 3" => QuadraticLine,
"Triangle 3" => Triangle,
"Triangle 6" => QuadraticTriangle,
Expand Down Expand Up @@ -39,25 +42,25 @@ function translate_elements(original_elements::Vector{Cell{3,20,6}})
ferrite_elements = Cell{3,20,6}[]
for original_ele in original_elements
push!(ferrite_elements,Cell{3,20,6}((original_ele.nodes[1],
original_ele.nodes[2],
original_ele.nodes[3],
original_ele.nodes[4],
original_ele.nodes[5],
original_ele.nodes[6],
original_ele.nodes[7],
original_ele.nodes[8],
original_ele.nodes[9],
original_ele.nodes[12],
original_ele.nodes[14],
original_ele.nodes[10],
original_ele.nodes[17],
original_ele.nodes[19],
original_ele.nodes[20],
original_ele.nodes[18],
original_ele.nodes[11],
original_ele.nodes[13],
original_ele.nodes[15],
original_ele.nodes[16])),)
original_ele.nodes[2],
original_ele.nodes[3],
original_ele.nodes[4],
original_ele.nodes[5],
original_ele.nodes[6],
original_ele.nodes[7],
original_ele.nodes[8],
original_ele.nodes[9],
original_ele.nodes[12],
original_ele.nodes[14],
original_ele.nodes[10],
original_ele.nodes[17],
original_ele.nodes[19],
original_ele.nodes[20],
original_ele.nodes[18],
original_ele.nodes[11],
original_ele.nodes[13],
original_ele.nodes[15],
original_ele.nodes[16])),)
end
return ferrite_elements
end
Expand All @@ -73,8 +76,8 @@ function toelements(dim::Int)
@assert length(elementtypes) == 1 "only one element type per mesh is supported"
elementname, dim, order, numnodes, localnodecoord, numprimarynodes = gmsh.model.mesh.getElementProperties(elementtypes[1])
nodetags = convert(Array{Array{Int64,1},1}, nodetags)[1]
juafemcell = gmshtojuafemcell[elementname]
elements_gmsh = [juafemcell(Tuple(nodetags[i:i + (numnodes - 1)])) for i in 1:numnodes:length(nodetags)]
ferritecell = gmshtoferritecell[elementname]
elements_gmsh = [ferritecell(Tuple(nodetags[i:i + (numnodes - 1)])) for i in 1:numnodes:length(nodetags)]
elements = translate_elements(elements_gmsh)
return elements, convert(Vector{Vector{Int64}}, elementtags)[1]
end
Expand Down

0 comments on commit df077f3

Please sign in to comment.