Skip to content
Snippets Groups Projects
Select Git revision
  • 252e733207e95c9a93707eb401ac84d79106efb0
  • master default protected
  • andreas/paper2
  • v1.0
  • v0.1
5 results

mesh.jl

Blame
  • mesh.jl 1.01 KiB
    export init_grid, save
    
    using WriteVTK
    
    # 2d, simplex grid
    struct Mesh
        vertices::Array{Float64, 2}
        cells::Array{Int, 2}
    end
    
    function init_grid(n)
        r = LinRange(0., 1., n + 1)
        coords = collect(Iterators.product(r, r))
    
        vertices = [x[i] for i in 1:2, x in vec(coords)]
        cells = Array{Int, 2}(undef, 3, 2 * n^2)
    
        vidx = LinearIndices((n+1, n+1))
        e1 = CartesianIndex(1, 0)
        e2 = CartesianIndex(0, 1)
        k = 0
        for I in CartesianIndices((n, n))
    	cells[:, k += 1] .= (vidx[I], vidx[I + e1], vidx[I + e1 + e2])
    	cells[:, k += 1] .= (vidx[I], vidx[I + e1 + e2], vidx[I + e2])
        end
    
        return Mesh(vertices, cells)
    end
    
    function save(filename, mesh::Mesh, fs...)
        cells = [MeshCell(VTKCellTypes.VTK_TRIANGLE, mesh.cells[:, i]) for i in axes(mesh.cells, 2)]
        #vertices = [mesh.vertices[i, j] for i in axes(mesh.vertices, 1), j in axes(mesh.vertices, 2)]
        vtkfile = vtk_grid(filename, mesh.vertices, cells)
        for f in fs
            append_data!(vtkfile, f)
        end
        vtk_save(vtkfile)
    end