Skip to content

Commit 62c1e4c

Browse files
Allow for ::AbstractVector (e.g. views) in write_solution (#1177)
1 parent cea0e8e commit 62c1e4c

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/Export/VTK.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ function component_names(::Type{S}) where {S}
175175
end
176176

177177
"""
178-
write_solution(vtk::VTKGridFile, dh::AbstractDofHandler, u::Vector, suffix="")
178+
write_solution(vtk::VTKGridFile, dh::AbstractDofHandler, u::AbstractVector, suffix="")
179179
180180
Save the values at the nodes in the degree of freedom vector `u` to `vtk`.
181181
Each field in `dh` will be saved separately, and `suffix` can be used to append
@@ -186,7 +186,7 @@ degree of freedom in `dh`, see [`write_node_data`](@ref write_node_data) for det
186186
Use `write_node_data` directly when exporting values that are already
187187
sorted by the nodes in the grid.
188188
"""
189-
function write_solution(vtk::VTKGridFile, dh::AbstractDofHandler, u::Vector, suffix = "")
189+
function write_solution(vtk::VTKGridFile, dh::AbstractDofHandler, u::AbstractVector, suffix = "")
190190
fieldnames = getfieldnames(dh) # all primary fields
191191
for name in fieldnames
192192
data = _evaluate_at_grid_nodes(dh, u, name, #=vtk=# Val(true))

test/test_vtk_export.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,29 @@
7878
end
7979
end
8080
end
81+
@testset "write_solution view" begin
82+
grid = generate_grid(Hexahedron, (5, 5, 5))
83+
dofhandler = DofHandler(grid)
84+
ip = geometric_interpolation(Hexahedron)
85+
add!(dofhandler, :temperature, ip)
86+
add!(dofhandler, :displacement, ip^3)
87+
close!(dofhandler)
88+
u = rand(ndofs(dofhandler))
89+
dofhandlerfilename = "dofhandler-no-views"
90+
VTKGridFile(dofhandlerfilename, grid) do vtk::VTKGridFile
91+
@test write_solution(vtk, dofhandler, u) === vtk
92+
end
93+
dofhandler_views_filename = "dofhandler-views"
94+
VTKGridFile(dofhandler_views_filename, grid) do vtk::VTKGridFile
95+
@test write_solution(vtk, dofhandler, (@view u[1:end])) === vtk
96+
end
97+
98+
# test the sha of the file
99+
sha = bytes2hex(open(SHA.sha1, dofhandlerfilename * ".vtu"))
100+
sha_views = bytes2hex(open(SHA.sha1, dofhandler_views_filename * ".vtu"))
101+
102+
@test sha == sha_views
103+
rm(dofhandlerfilename * ".vtu")
104+
rm(dofhandler_views_filename * ".vtu")
105+
end
81106
end

0 commit comments

Comments
 (0)