Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Read VTK #686

Draft
wants to merge 45 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 43 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
59d7c0b
Update Project.toml
marcelschurer Oct 16, 2024
335d3f0
vtk2trixi
marcelschurer Nov 9, 2024
4169638
vtk2trixi
marcelschurer Nov 9, 2024
550b066
example_file
marcelschurer Nov 9, 2024
6710a12
Update documentation and enhance transport velocity formulation support
marcelschurer Nov 9, 2024
caf9439
add readvtk
marcelschurer Nov 15, 2024
4b74ef1
Merge branch 'trixi-framework:main' into custom-quantities-algorithm
marcelschurer Nov 20, 2024
5f37b55
boundary-fluid-concept
marcelschurer Nov 20, 2024
2a46613
Merge branch 'trixi-framework:main' into boundary-fluid-concept
marcelschurer Nov 22, 2024
f13fef2
compact-concept
marcelschurer Nov 22, 2024
696cf0c
Merge pull request #16 from marcelschurer/boundary-fluid-concept
marcelschurer Nov 22, 2024
e28b79b
integrate try and catch
marcelschurer Nov 25, 2024
499e285
added wall_velocity case
marcelschurer Nov 25, 2024
0943d7c
Merge remote-tracking branch 'origin/main' into read-vtk
marcelschurer Nov 25, 2024
e827bca
correct ndims
marcelschurer Nov 25, 2024
a0e042e
stable v1
marcelschurer Nov 25, 2024
1dbec17
delete using get_field_data()
marcelschurer Nov 25, 2024
642b417
update example file
marcelschurer Nov 25, 2024
cda17e7
use Regular Expressions
marcelschurer Dec 3, 2024
d353124
tesset begin
marcelschurer Dec 9, 2024
91025f1
use match
marcelschurer Dec 11, 2024
09c0a52
correct example file
marcelschurer Dec 11, 2024
3b008ee
Update src/preprocessing/readvtk/vtk2trixi.jl
marcelschurer Dec 13, 2024
46c8bea
Update src/preprocessing/readvtk/vtk2trixi.jl
marcelschurer Dec 13, 2024
716f488
Update src/preprocessing/readvtk/vtk2trixi.jl
marcelschurer Dec 13, 2024
599c97c
Update src/preprocessing/readvtk/vtk2trixi.jl
marcelschurer Dec 13, 2024
eae7395
info update
marcelschurer Dec 13, 2024
9c1a50a
update info
marcelschurer Dec 13, 2024
38d2ff0
Merge branch 'trixi-framework:main' into read-vtk
marcelschurer Dec 14, 2024
6d6a932
Merge branch 'trixi-framework:main' into tests
marcelschurer Dec 14, 2024
11391f6
Merge branch 'tests' of https://github.com/marcelschurer/TrixiParticl…
marcelschurer Dec 14, 2024
61e88fe
Update hydrostatic_water_column_3d.jl
marcelschurer Dec 16, 2024
e1a5972
changes
marcelschurer Dec 16, 2024
c73b2ca
Merge branch 'tests' of https://github.com/marcelschurer/TrixiParticl…
marcelschurer Dec 16, 2024
a51cf2f
repair
marcelschurer Dec 16, 2024
3ba15d8
tests
marcelschurer Dec 16, 2024
85792cd
tests
marcelschurer Dec 16, 2024
4400cda
Merge pull request #19 from marcelschurer/tests
marcelschurer Dec 16, 2024
b31753f
Update src/TrixiParticles.jl
marcelschurer Dec 18, 2024
5b272ad
Update src/preprocessing/readvtk/vtk2trixi.jl
marcelschurer Dec 18, 2024
c59f370
implement suggestions
marcelschurer Dec 19, 2024
3c0b005
read out simulation file
marcelschurer Dec 19, 2024
5bf34d4
implement suggestions
marcelschurer Dec 19, 2024
1363075
Reading out 'particle_spacing'
marcelschurer Jan 1, 2025
d567573
add test for 'fluid_system' and 'boundary_system'
marcelschurer Jan 1, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ PointNeighbors = "1c4d5385-0a27-49de-8e2c-43b175c8985c"
Polyester = "f517fe37-dbe3-4b94-8317-1923a5111588"
Printf = "de0858da-6303-5e67-8744-51eddeeeb8d7"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
ReadVTK = "dc215faf-f008-4882-a9f7-a79a826fadc3"
RecipesBase = "3cdcf5f2-1ef4-517c-9805-6587b60abb01"
Reexport = "189a3867-3050-52da-a836-e630ba90ab69"
SciMLBase = "0bca4576-84f4-4d90-8ffe-ffa030f20462"
Expand Down
18 changes: 18 additions & 0 deletions examples/readvtk/readvtk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Example File to read a vtk file and convert it to InitialCondition
using TrixiParticles

# Create a example rectangle
rectangle = RectangularShape(0.1, (10, 10), (0, 0), density=1.5, velocity=(1.0, -2.0),
pressure=1000.0)

trixi2vtk(rectangle; filename="rectangle", output_directory="out",
custom_quantity=nothing)

filename = "rectangle"
file = joinpath("out", filename * ".vtu")

# Read the vtk file and convert it to an 'InitialCondition'
ic = vtk2trixi(file)

trixi2vtk(ic; filename="readvtk", output_directory="out",
custom_quantity=nothing)
3 changes: 2 additions & 1 deletion src/TrixiParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using LinearAlgebra: norm, dot, I, tr, inv, pinv, det
using MuladdMacro: @muladd
using Polyester: Polyester, @batch
using Printf: @printf, @sprintf
using ReadVTK: ReadVTK
using RecipesBase: RecipesBase, @series
using Random: seed!
using SciMLBase: CallbackSet, DiscreteCallback, DynamicalODEProblem, u_modified!,
Expand Down Expand Up @@ -75,7 +76,7 @@ export BoundaryModelMonaghanKajtar, BoundaryModelDummyParticles, AdamiPressureEx

export BoundaryMovement
export examples_dir, validation_dir, trixi_include
export trixi2vtk
export trixi2vtk, vtk2trixi
export RectangularTank, RectangularShape, SphereShape, ComplexShape
export WindingNumberHormann, WindingNumberJacobson
export VoxelSphere, RoundSphere, reset_wall!, extrude_geometry, load_geometry
Expand Down
1 change: 1 addition & 0 deletions src/preprocessing/preprocessing.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
include("geometries/geometries.jl")
include("point_in_poly/point_in_poly.jl")
include("readvtk/vtk2trixi.jl")
49 changes: 49 additions & 0 deletions src/preprocessing/readvtk/vtk2trixi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
"""
vtk2trixi(file::String)

Convert data from VTK-file to InitialCondition

# Arguments
- `file`: Name of the file to be loaded.
"""
function vtk2trixi(file)
vtk_file = ReadVTK.VTKFile(file)

# Retrieve data fields (e.g., pressure, velocity, ...)
point_data = ReadVTK.get_point_data(vtk_file)
field_data = ReadVTK.get_field_data(vtk_file)

# Retrieve fields
ndims = ReadVTK.get_data(field_data["ndims"])
coordinates = ReadVTK.get_points(vtk_file)[1:ndims[1], :]
marcelschurer marked this conversation as resolved.
Show resolved Hide resolved

fields = ["velocity", "density", "pressure", "mass"]
results = Dict{String, Array{Float64}}()

for field in fields
found = false
for k in ReadVTK.keys(point_data)
if !isnothing(match(Regex("$field"), k))
results[field] = ReadVTK.get_data(point_data[k])
found = true
break
end
end
if !found
# Set fields to zero if not found
if field in ["density", "pressure", "mass"]
results[field] = zeros(size(coordinates, 2))
else
results[field] = zero(coordinates)
end
@info "No '$field' field found in VTK file. Will be set to zero."
end
end

return InitialCondition(
; coordinates,
marcelschurer marked this conversation as resolved.
Show resolved Hide resolved
velocity=results["velocity"],
mass=results["mass"],
density=results["density"],
pressure=results["pressure"])
end
3 changes: 2 additions & 1 deletion src/visualization/write2vtk.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function trixi2vtk(v_, u_, t, system_, periodic_box; output_directory="out", pre
# Store particle index
vtk["index"] = active_particles(system)
vtk["time"] = t
vtk["ndims"] = ndims(system)
marcelschurer marked this conversation as resolved.
Show resolved Hide resolved

if write_meta_data
vtk["solver_version"] = git_hash
Expand Down Expand Up @@ -194,6 +195,7 @@ function trixi2vtk(coordinates; output_directory="out", prefix="", filename="coo
vtk_grid(file, points, cells) do vtk
# Store particle index.
vtk["index"] = [i for i in axes(coordinates, 2)]
vtk["ndims"] = size(coordinates, 1)
marcelschurer marked this conversation as resolved.
Show resolved Hide resolved

# Extract custom quantities for this system.
for (key, quantity) in custom_quantities
Expand All @@ -202,7 +204,6 @@ function trixi2vtk(coordinates; output_directory="out", prefix="", filename="coo
end
end
end

return file
end

Expand Down
1 change: 1 addition & 0 deletions test/preprocessing/preprocessing.jl
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
include("geometries/geometries.jl")
include("readvtk/readvtk.jl")
40 changes: 40 additions & 0 deletions test/preprocessing/readvtk/vtk2trixi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
@testset verbose=true "vtk2trixi" begin
@testset verbose=true "Functionality Check - 2D" begin

# 'InitialCondition'-Files
saved_ic = RectangularShape(0.1, (10, 20),
(0, 0), density=1.5,
velocity=(1.0, 2.0), pressure=1000.0)

file = trixi2vtk(saved_ic; output_directory="test/preprocessing/readvtk",
filename="initial_condition")

loaded_ic = vtk2trixi(joinpath("test/preprocessing/readvtk",
"initial_condition.vtu"))

@test isapprox(saved_ic.coordinates, loaded_ic.coordinates)
@test isapprox(saved_ic.velocity, loaded_ic.velocity)
@test isapprox(saved_ic.density, loaded_ic.density)
@test isapprox(saved_ic.pressure, loaded_ic.pressure)
@test isapprox(saved_ic.mass, loaded_ic.mass)

# 'Fluidsystem'-Files
state_equation = StateEquationCole(; sound_speed=1.0, reference_density=1.0,
exponent=1, clip_negative_pressure=false)
fluid_system = WeaklyCompressibleSPHSystem(saved_ic,
SummationDensity(), state_equation,
WendlandC2Kernel{2}(), 0.2)

saved_fs = trixi2vtk(saved_ic.velocity, saved_ic.coordinates, 0.0, fluid_system,
nothing;
output_directory="test/preprocessing/readvtk", iter=1)

loaded_fs = vtk2trixi(joinpath("test/preprocessing/readvtk", "fluid_1.vtu"))

@test isapprox(saved_fs.coordinates, loaded_fs.coordinates)
@test isapprox(saved_fs.velocity, loaded_fs.velocity)
@test isapprox(saved_fs.density, loaded_fs.density)
@test isapprox(saved_fs.pressure, loaded_fs.pressure)
@test isapprox(saved_fs.mass, loaded_fs.mass)
end
end