MakieOrg / Makie.jl

Interactive data visualizations and plotting in Julia
https://docs.makie.org/stable
MIT License
2.36k stars 300 forks source link

Using MakiE to visualize JuAFEM meshes #94

Open mohamed82008 opened 6 years ago

mohamed82008 commented 6 years ago

Following the brief discussion on slack, I am opening this issue to host relevant discussions. Here is the text of my question:

Are there any recommendations for using MakiE to visualize JuAFEM grids? I am after visualizing deformation of 3D meshes with some node-associated color indicator proportional to the norm of the deformation of a node. That aside though, I am firstly interested in the mesh representation. I believe MakiE uses GeometryTypes.jl which I don't think has the Quadrilateral, Tetrahedron or Hexahedron elements often used in the FEM world. Does that mean to visualize any mesh, I would have to triangulate all the cells' faces? Is this the most efficient way? Is there a way to avoid triangulating non-surface faces, while allowing the slicing of the mesh and seeing a cross-section view with scalar or vector field coloring? Is there a way to define new primitives in GeometryTypes.jl to map those of the JuAFEM/VTK world and have those available for MakiE, or is MakiE limited by some GL-constraints in terms of the geometry types it can present on the screen.

@SimonDanisch's response was:

meshes in geometrytypes are not restricted to the number of vertices in one polygon

to display them, you need to triangulate them in any case... i can do this behind your back in makie ;)

the functionality is basically one function call away from being already in makie

if you give me a self contained script, i can make it part of the test suite i'm currently writing... i actually want to make a prerelease today!

i started to work on slicing meshes, and stuff did work.for fem use cases.. i'll need to get back to see what wasn't working yet

test cases to cover all use cases would help a lot :)

A script to showcase the mesh representation, expected visualization and desired API is coming up next...

SimonDanisch commented 6 years ago

cc @Kevin-Mattheus-Moerman

mohamed82008 commented 6 years ago

This script requires a couple of preparations to run. Firstly, you need JuAFEM and StaticArrays installed. Then you need to checkout this PR/branch https://github.com/KristofferC/JuAFEM.jl/pull/216. Then you should clone https://github.com/mohamed82008/LinearElasticity.jl. Then I believe you should be ready to go:

using LinearElasticity

# Define problem
nels = (60,20,4)
sizes = (1.0,1.0,1.0)
E = 1.0;
ν = 0.3;
force = -1.0;
problem = PointLoadCantilever(nels, sizes, E, ν, force)

# Build element stiffness matrices and force vectors
einfo = ElementFEAInfo(problem);

# Assemble global stiffness matrix and force vector
ginfo = assemble(problem, einfo);

# Solve for node displacements
u = ginfo.K \ ginfo.f

mesh = problem.ch.dh.grid
node_dofs = problem.metadata.node_dofs

node_displacements = reshape(u[node_dofs], 3, JuAFEM.getnnodes(mesh))
# visualize(mesh, node_displacements)

Please let me know if you run into difficulties or need more details on the internal representation of the mesh.

mohamed82008 commented 6 years ago

The desired visualization here is to transform the nodes of the mesh by the corresponding displacements and bonus points if the color of the mesh changes proportionally to the norm of the displacement at that part of the mesh.

ChrisRackauckas commented 6 years ago

Duplicate of https://github.com/JuliaPlots/Makie.jl/issues/64 ? We'd (@ysimillides) would like to use this with FEniCS.jl as well, so it makes sense to have a standard trisurf tie-in that both JuliaFEM and FEniCS (and then whoever else comes in the future as well) build package-specific recipes on top of.

mohamed82008 commented 6 years ago

The approach Simon used to visualize juafem cells using the above script is here https://github.com/JuliaPlots/Makie.jl/blob/sd/abstract/examples/fem.jl. I don't know how the fenics wrapper works but if you can sneak behind Python's back and reinterpret the (hopefully contiguous) data into a juafem-like unstructured grid then the same script above can be used. All I needed to generalize this script to other cell types was to define to_triangle for different cell types. If you want to trisurf the whole mesh yourself, e.g. in case of structured meshes, and then pass it to Makie, then you may want to try overloading to_gl_indices. You will also probably need to pass the Cells struct to Makie.mesh and mesh! instead of mesh.cells in the script above. There may be more to the story though that only Simon can tell us.

SimonDanisch commented 6 years ago

The api with to_triangle and to_gl_indices is not set in stone yet ;) Ideally I wanted to just have convert_arguments work for this in a standard way, but this was the easiest right now. I just opened an issue at GeometryTypes to talk more about it: https://github.com/JuliaGeometry/GeometryTypes.jl/issues/131