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 #657

Draft
wants to merge 53 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
53 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
1d007c0
Fix callback `show` and remove hacky workaround (#669)
efaulhaber Nov 20, 2024
07fa6f0
Add ideal gas equation (#607)
svchb Nov 20, 2024
06a9352
Add tutorial for setting up a simulation (#514)
efaulhaber 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
231e2e0
Validation TVF (#640)
LasNikas 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
83e5c3b
Bump crate-ci/typos from 1.26.8 to 1.28.1 (#678)
dependabot[bot] Dec 1, 2024
a1cc9d6
Bump codecov/codecov-action from 4 to 5 (#679)
dependabot[bot] Dec 2, 2024
c93ab39
Plot recipes: support automatic marker size with `xlims` and `ylims` …
LasNikas Dec 2, 2024
cda17e7
use Regular Expressions
marcelschurer Dec 3, 2024
d353124
tesset begin
marcelschurer Dec 9, 2024
f051b55
Improve Test Coverage (#674)
svchb Dec 11, 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
6 changes: 6 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ version = "0.2.4-dev"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
DelimitedFiles = "8bb1440f-4735-579b-a4ab-409b98df4dab"
DiffEqCallbacks = "459566f4-90b8-5000-8ac3-15dfb0a30def"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"

FastPow = "c0e83750-1142-43a8-81cf-6c956b72b4d1"
FileIO = "5789e2e9-d7fb-5bc7-8068-2c6fae9b9549"
ForwardDiff = "f6369f11-7733-5829-9624-2563aa707210"
Expand All @@ -18,17 +20,20 @@ JSON = "682c06a0-de6a-54ab-a142-c8b1cf79cde6"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
MuladdMacro = "46d2c3a1-f734-5fdb-9937-b9b9aeba4221"
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OrdinaryDiffEq = "1dea7af3-3e70-54e6-95c3-0bf5283fa5ed"

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"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StrideArrays = "d1fa6d79-ef01-42a6-86c9-f7c551f8593b"
TimerOutputs = "a759f4b9-e2f1-59dc-863e-4aeb61b1ea8f"
TrixiBase = "9a0f1c46-06d5-4909-a5a3-ce25d3fa3284"
VTKDataIO = "c6703add-1d23-52c6-9943-3ad88652b9b2"
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
VTKDataIO = "c6703add-1d23-52c6-9943-3ad88652b9b2"

Please undo all this changes in the Project.toml except ReadVTK.

WriteVTK = "64499a7a-5c06-52f2-abe2-ccb03c286192"

[compat]
Expand All @@ -44,6 +49,7 @@ GPUArraysCore = "0.1, 0.2"
JSON = "0.21"
KernelAbstractions = "0.9"
MuladdMacro = "0.2"
OrdinaryDiffEq = "6.87.0"
PointNeighbors = "0.4.2"
Polyester = "0.7.10"
RecipesBase = "1"
Expand Down
8 changes: 8 additions & 0 deletions examples/readvtk/trixi2vtk.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# Example File to read a vtk file and convert it to InitialCondition
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Example File to read a vtk file and convert it to InitialCondition
# Example File to read a vtk file and convert it to `InitialCondition`

I find it better if you put this file into examples/reprocessing and name it read_vtk.jl

using TrixiParticles
using OrdinaryDiffEq
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
using OrdinaryDiffEq


ic = vtk2trixi("examples/readvtk/fluid_1_1.vtu")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please use joinpath() for this. Also it is better to store an example vtk file in examples/reprocessing/data and name it read_vtk_example.vtu or so.


trixi2vtk(ic; filename="trixi2vtk_test", output_directory="out_vtk",
custom_quantity=nothing)
61 changes: 61 additions & 0 deletions examples/vtk-reader/particle_saving.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using ReadVTK

struct Particle
index::Int64
coordinates::Tuple{Float64, Float64, Float64} # Assuming 3D coordinates
density::Float64
velocity::Tuple{Float64, Float64, Float64} # Assuming 3D velocity
pressure::Float64
end

vtk_file = VTKFile("out/fluid_1_1.vtu")
#vtk_file = VTKFile("out_vtk/rectangle_of_water.vtu")

# Retrieve particle coordinates
coords = get_points(vtk_file)

# Retrieve point data fields (e.g., pressure, velocity, ...)
vtk_point_data = get_point_data(vtk_file)

# Dynamically get all available field names from point_data
field_data = Dict()
for field_name in keys(vtk_point_data)
field_data[field_name] = get_data(vtk_point_data[field_name])
end

# Create an array of Particle instances
particles = Vector{Particle}(undef, size(coords, 2))

for i in 1:size(coords, 2)
# Retrieve required field "index"
index = field_data["index"][i]

# Coordinates
coordinates = (coords[1, i], coords[2, i], coords[3, i])

# Retrieve each required field directly, assuming all are present
density = field_data["density"][i]
pressure = field_data["pressure"][i]

velocity = if size(field_data["velocity"], 1) == 2
# If velocity is 2D, add 0.0 for the z component
(field_data["velocity"][1, i], field_data["velocity"][2, i], 0.0)
else
# If velocity is 3D, use all three components
(field_data["velocity"][1, i], field_data["velocity"][2, i],
field_data["velocity"][3, i])
end

# Create a new Particle instance
particles[i] = Particle(index, coordinates, density, velocity, pressure)
end

# Display some particles for verification
for particle in particles[1:2]
println(particle)
end

println("Coords: ", size(coords))
println("velocity ", size(field_data["velocity"]))

# TODO FieldData
23 changes: 23 additions & 0 deletions examples/vtk-reader/read_two_points_dynamic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using ReadVTK

#vtk = VTKFile("out/fluid_1_0.vtu")
#vtk = VTKFile("out_vtk/two_points.vtu")
vtk = VTKFile("out_vtk/rectangle_of_water.vtu")

point_data_file = get_point_data(vtk)

point_names = keys(point_data_file)

for point_name in point_names
point_array = point_data_file[point_name]

point_data = get_data(point_array)

println(point_name)
println(point_data)
end

coords = get_points(vtk)
for i in 1:size(coords, 2)
println("Coords $i: (", coords[1, i], ", ", coords[2, i], ", ", coords[3, i], ")")
end
51 changes: 51 additions & 0 deletions examples/vtk-reader/read_two_points_static.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using ReadVTK

vtk = VTKFile("out_vtk/two_points.vtu")
#vtk = VTKFile("out/fluid_1_0.vtu")

"""
# Fields of vtk

- `filename`: original path to the VTK file that has been read in
- `xml_file`: object that represents the XML file
- `file_type`: currently only `"UnstructuredGrid"` or `"ImageData"` are supported
- `version`: VTK XML file format version
- `byte_order`: can be `LittleEndian` or `BigEndian` and must currently be the same as the system's
- `compressor`: can be empty (no compression) or `vtkZLibDataCompressor`
- `appended_data`: in case of appended data (see XML documentation), the data is stored here for
convenient retrieval (otherwise it is empty)
- `n_points`: number of points in the VTK file
- `n_cells`: number of cells in the VTK file`
"""

""" Examples

#cell_data = get_cell_data(vtk)

point_data = get_point_data(vtk)
# create an vector with the names of the point data like "velocity"
names = point_data.names

# create an vector with the information about the Data like "offset"
data_arrays = point_data.data_arrays

# information about the file like "UnstructedGrid"
vtk_file = point_data.vtk_file

# gets the data of the vtk-file
appended_data = vtk.appended_data

# gets the coords of all the points
#coords = get_points(vtk)

# gets the information about the cells
#cells = get_cells(vtk)
"""
point_data = get_point_data(vtk)

velocity_array = point_data["velocity"]

velocity_data = get_data(velocity_array)

println("Velocity Data:")
println(velocity_data)
51 changes: 51 additions & 0 deletions examples/vtk-reader/write_n_points.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
using WriteVTK

# Function to create n points and their associated data
function create_vtk_file(n::Int, coords::Matrix{Float64}, scalars::Vector{Float64},
velocities::Matrix{Float64}, filename::String)
# Ensure the coordinates matrix has the correct size (3 x n)
@assert size(coords)==(3, n) "coords should be a 3 x n matrix, where n is the number of points"
# Ensure scalars is a vector of length n
@assert length(scalars)==n "scalars should be a vector of length n"
# Ensure velocities is a matrix of size (3 x n)
@assert size(velocities)==(3, n) "velocities should be a 3 x n matrix, where n is the number of points"

output_directory = "out_vtk"
mkpath(output_directory)

file = joinpath(output_directory, filename)

# Create VTK_VERTEX cells for all points
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (i,)) for i in 1:n]

# Initialize the VTK grid with the points and cells
vtk_file = vtk_grid(file, coords, cells)

# Assign scalar data to the points
vtk_file["scalar"] = scalars

# Flatten velocities into a 1D array for VTK
vtk_file["velocity"] = vec(velocities)

# Save the VTK file
vtk_save(vtk_file)
end

# Example usage with n points
n = 3 # Number of points

# Define coordinates for n points (3 rows for x, y, z; n columns for the points)
coords = [1.0 2.0 3.0; # x-coordinates
1.0 2.0 3.0; # y-coordinates
0.0 0.0 0.0] # z-coordinates (all on the z=0 plane)

# Define scalar values for each point
scalars = [100.0, 200.0, 300.0]

# Define velocity vectors for each point (3 rows for vx, vy, vz; n columns for the points)
velocities = [10.0 0.0 5.0; # x-components
0.0 5.0 2.0; # y-components
0.0 0.0 0.0] # z-components (all on the z=0 plane)

# Create the VTK file
create_vtk_file(n, coords, scalars, velocities, "n_points")
28 changes: 28 additions & 0 deletions examples/vtk-reader/write_one_point.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using WriteVTK

output_directory = "out_vtk"
mkpath(output_directory)

file = joinpath(output_directory, "one_point")

# Define the points array in the expected format: 3 rows (x, y, z), 1 column (for 1 point)
points = [1.0 # x-coordinate
2.0 # y-coordinate
0.0] # z-coordinate

# Create a VTK_VERTEX cell for the single point
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (1,))] # Cell for the single point

# Initialize the VTK grid with the point and cell
vtk_file = vtk_grid(file, points, cells)

# Assign scalar data to the point
vtk_file["scalar"] = [100.0] # Scalar value for the point

# Assign vector data (e.g., velocity) to the point
vtk_file["velocity"] = [10.0, 0.0, 0.0] # Velocity for the point

# Save the VTK file
vtk_save(vtk_file)

# Surprisingly, the code works not for one point, but for >=2 points.
28 changes: 28 additions & 0 deletions examples/vtk-reader/write_two_points.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using WriteVTK

output_directory = "out_vtk"
mkpath(output_directory)

file = joinpath(output_directory, "two_points")

# Define the points array in the expected format: 3 rows (x, y, z), 2 columns (for 2 points)
points = [1.0 3.0 # x-coordinates
2.0 4.0 # y-coordinates
0.0 0.0] # z-coordinates (both points on the z=0 plane)

# Create VTK_VERTEX cells for both points
cells = [MeshCell(VTKCellTypes.VTK_VERTEX, (1,)), # Cell for first point
MeshCell(VTKCellTypes.VTK_VERTEX, (2,))] # Cell for second point

# Initialize the VTK grid with the points and cells
vtk_file = vtk_grid(file, points, cells)

# Assign scalar data to the two points
vtk_file["scalar"] = [100.0, 200.0] # Scalar values for the two points

# Assign vector data (e.g., velocity) to the two points
vtk_file["velocity"] = [1.0, 2.0, 3.0, # Velocity for first point
4.0, 5.0, 6.0] # Velocity for second point

# Save the VTK file
vtk_save(vtk_file)
8 changes: 8 additions & 0 deletions examples/vtk-reader/write_with_trixi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using TrixiParticles

rectangle = RectangularShape(0.1, (1, 2), (0.0, 0.0), velocity=[10.0, 0.0], density=1000.0)

custom_quantity = ones(nparticles(rectangle))

trixi2vtk(rectangle; filename="rectangle_of_water", output_directory="out_vtk",
custom_quantity=nothing)
2 changes: 1 addition & 1 deletion src/TrixiParticles.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export BoundaryModelMonaghanKajtar, BoundaryModelDummyParticles, AdamiPressureEx
PressureMirroring, PressureZeroing, BoundaryModelLastiwka
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")
36 changes: 36 additions & 0 deletions src/preprocessing/readvtk/vtk2trixi.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using ReadVTK
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put this in src/TrixiParticles.jl between line 20 and 21. Packages are sorted alphabetically.


"""
Convert data from VTK-file to InitialCondition

# FluidSystem data only
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either add a TODO or elaborate more how to use the function. Look at other docs for inspiration.

"""

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

function vtk2trixi(filename)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tests are missing

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make this consistent with trixi2vtk. That is, please use the kwargs input_directory="out", prefix="", iter=nothing.
IMO, this makes it easier to later find the right file.

vtk_file = VTKFile(filename)

# Retrieve particle coordinates
coordinates = get_points(vtk_file)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Retrieve particle coordinates
coordinates = get_points(vtk_file)


# Retrieve point data fields (e.g., pressure, velocity, ...)
vtk_point_data = get_point_data(vtk_file)

# create field data arrays
density = get_data(vtk_point_data["density"])

pressure = get_data(vtk_point_data["pressure"])

velocity = get_data(vtk_point_data["velocity"])
if size(velocity, 1) == 2
# If velocity is 2D, add 0.0 for the z component
velocity = vcat(velocity, zeros(1, size(velocity, 2)))
end
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if size(velocity, 1) == 2
# If velocity is 2D, add 0.0 for the z component
velocity = vcat(velocity, zeros(1, size(velocity, 2)))
end
# Retrieve particle coordinates.
coordinates = get_points(vtk_file)[axes(velocity, 1), :]

Also, please add to the comment that the point coordinates are stored in 3D coordinates, but the velocity can be either 2D or 3D. I think this has something to do with the vtk data structure. You might want to read up on it.


mass = ones(size(coordinates, 2))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please create an issue that we need to write the mass per particle in trixi2vtk.


return InitialCondition(; coordinates, velocity, mass, density, pressure)
end

# TODO: edit the mass array --> InitialCondition needs a mass or a particle_spacing
# TODO: example file in folder examples/readvtk
# TODO: make it work with 2D velocity --> In ParaView the velocity vecor is 3D after trixi2vtk. It should be 2D if the initial data is 2D.
Loading