-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add some files from prior developement
- Loading branch information
Showing
10 changed files
with
274 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,9 @@ uuid = "a75b99cc-be56-4638-99bc-d89fa43b9ca1" | |
authors = ["Warisa <[email protected]> and contributors"] | ||
version = "1.0.0-DEV" | ||
|
||
[deps] | ||
ShockwaveProperties = "77d2bf28-a3e9-4b9c-9fcf-b85f74cc8a50" | ||
|
||
[compat] | ||
julia = "1.9" | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
using ShockwaveDetection |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
using ShockwaveDetection |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
include("visualize.jl") | ||
|
||
# Use Interpolations.jl to perform np.gradient-like operations | ||
using Interpolations | ||
|
||
using StatsPlots | ||
|
||
# Function to compute gradients using Interpolations.jl | ||
# without explicit function definition | ||
function compute_gradients(arr::AbstractVector, x) | ||
# Convert StepRangeLen to LinRange because that's what Interpolations.jl expects | ||
x_linrange = LinRange(first(x), last(x), length(x)) | ||
itp = interpolate((x_linrange,), arr, Gridded(Linear())) | ||
grad = only.(Interpolations.gradient.(Ref(itp), x_linrange)) | ||
return grad | ||
end | ||
|
||
# Function to compute zero crossings, detecting points where the second derivative changes sign\ | ||
# suggesting a potential shock | ||
function zero_crossings(arr) | ||
return [i for i in 2:length(arr) if arr[i-1] * arr[i] < 0] | ||
end | ||
|
||
# Function to detect shock locations at a given time step | ||
function detect_shocks_at_timestep(density_at_t, velocity_at_t, pressure_at_t, x) | ||
# Compute first gradients | ||
density_grad = compute_gradients(density_at_t, x) | ||
velocity_grad = compute_gradients(velocity_at_t, x) | ||
pressure_grad = compute_gradients(pressure_at_t, x) | ||
|
||
# Find points where the gradient exceeds a certain threshold | ||
threshold = 0.5 # Adjust this threshold as needed | ||
shock_location_density = findall(gradient -> abs(gradient) > threshold, density_grad) | ||
shock_location_velocity = findall(gradient -> abs(gradient) > threshold, velocity_grad) | ||
shock_location_pressure = findall(gradient -> abs(gradient) > threshold, pressure_grad) | ||
|
||
# Combine detections (common shock location across variables) | ||
shock_locations = intersect(intersect(shock_location_density, shock_location_velocity), shock_location_pressure) | ||
|
||
return shock_locations | ||
end | ||
|
||
# Read the data | ||
x0_xmax, t_values, u_values, dims_u = read_output_file("C:/Users/user/Documents/School/Sem4/softwareentwicklungspraktikum/shock_wave_detection/ShockwaveProperties.jl/example/data/euler_scenario_2.out") | ||
x = range(x0_xmax[1], stop=x0_xmax[2], length=dims_u[2]) | ||
density_field, velocity_field, pressure_field = convert_to_primitive(u_values) | ||
|
||
# Detect shocks for each time step and store the positions | ||
shock_positions_over_time = [] | ||
|
||
for t_step in 1: length(t_values) | ||
density_field_t = density_field[:, t_step] | ||
velocity_field_t = velocity_field[:, t_step] | ||
pressure_field_t = pressure_field[:, t_step] | ||
shock_positions = detect_shocks_at_timestep(density_field_t,velocity_field_t,pressure_field_t, x) | ||
push!(shock_positions_over_time, shock_positions) | ||
end | ||
|
||
# Create an animation | ||
anim = @animate for (t_step, t) in enumerate(t_values) | ||
p1 = plot(x, density_field[:, t_step], title="Density at Time $t", xlabel="x", ylabel="Density", label = "Density across x", size=(800, 600)) | ||
p2 = plot(x, velocity_field[:, t_step], title="Velocity at Time $t", xlabel="x", ylabel="Velocity", label = "Velocity across x", size=(800, 600)) | ||
p3 = plot(x, pressure_field[:, t_step], title="Pressure at Time $t", xlabel="x", ylabel="Pressure", label = "Pressure across x", size=(800, 600)) | ||
|
||
# Add markers for the shock positions | ||
shock_positions_t = shock_positions_over_time[t_step] | ||
for pos in shock_positions_t | ||
scatter!(p1, [x[pos]], [density_field[pos, t_step]], color=:red, label=false) | ||
scatter!(p2, [x[pos]], [velocity_field[pos, t_step]], color=:red, label=false) | ||
scatter!(p3, [x[pos]], [pressure_field[pos, t_step]], color=:red, label=false) | ||
end | ||
|
||
plot(p1, p2, p3, layout = (3, 1)) | ||
end | ||
|
||
# Save the animation as a gif | ||
gif(anim, "shock_over_time.gif", fps = 10) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
using Plots | ||
using FFMPEG | ||
using ShockwaveProperties: primitive_state_vector, pressure, speed_of_sound, DRY_AIR | ||
using Unitful: Pa, ustrip | ||
|
||
function read_output_file(filename) | ||
open(filename, "r") do f | ||
dims_u = Vector{Int}(undef, 2) | ||
read!(f, dims_u) | ||
|
||
# Read the first and last x values | ||
x0_xmax = Vector{Float64}(undef, 2) | ||
read!(f, x0_xmax) | ||
|
||
# Read the number of time steps | ||
num_timesteps = Vector{Int}(undef, 1) | ||
read!(f, num_timesteps) | ||
|
||
# Read the time values | ||
t_values = Vector{Float64}(undef, num_timesteps[1]) | ||
read!(f, t_values) | ||
|
||
# Read the u values | ||
u_values = Vector{Float64}(undef, prod(dims_u)*num_timesteps[1]) | ||
read!(f, u_values) | ||
|
||
|
||
# Reshape u_values to a 3D array | ||
u_values = reshape(u_values, dims_u[1], dims_u[2], num_timesteps[1]) # N_u x N_x x N_t as u a vector of 3 is written in range of x according to each time step | ||
|
||
|
||
return x0_xmax, t_values, u_values, dims_u | ||
end | ||
end | ||
|
||
# Euler is in a "conserved" form and the vector u contains (density, momentum, total energy) at each point x and time t | ||
# So we want to convert these to (density, velocity, pressure) to calculate delta1 and delta2 according to [6] | ||
|
||
# Read the data and create the animation | ||
# x0_xmax, t_values, u_values, dims_u = read_output_file("C:/Users/user/Documents/School/Sem4/softwareentwicklungspraktikum/shock_wave_detection/ShockwaveProperties.jl/example/data/euler_scenario_2.out") | ||
|
||
# Convert u_values to primitive variables. It's convert to density, velocity, pressure | ||
function convert_to_primitive(u_values) | ||
u_prim = zeros(size(u_values)) | ||
for i in 1:size(u_values, 2) | ||
for j in 1:size(u_values, 3) | ||
# primitive_state_vector returns value without units | ||
u_p_M_T = primitive_state_vector(u_values[:, i, j]; gas=DRY_AIR) | ||
p_u = pressure(u_p_M_T[1], u_p_M_T[3]; gas=DRY_AIR) | ||
# Store density | ||
u_prim[1, i, j] = u_p_M_T[1] | ||
# Convert Mach to m/s using speed_of_sound | ||
u_prim[2, i, j] = u_p_M_T[2] * ustrip(speed_of_sound(u_p_M_T[3]; gas=DRY_AIR)) | ||
# Strip the unit of pressure so that it can be stored in an empty array | ||
u_prim[3, i, j] = ustrip(p_u) | ||
end | ||
end | ||
|
||
# Extract the density, velocity, and pressure fields | ||
density_field = u_prim[1, :, :] | ||
velocity_field = u_prim[2, :, :] | ||
pressure_field = u_prim[3, :, :] | ||
return density_field, velocity_field, pressure_field | ||
end | ||
|
||
function create_animation(x0_xmax, t_values, density_field, velocity_field, pressure_field) | ||
# Create a range of x values | ||
x = range(x0_xmax[1], stop=x0_xmax[2], length=dims_u[2]) | ||
|
||
# Create a range of t values | ||
t = t_values | ||
|
||
# Create an animation | ||
anim = @animate for (i, t) in enumerate(t_values) | ||
p1 = Plots.plot(x, density_field[:, i], title="Density at Time $t", xlabel="x", ylabel="Density(kg/m^3)", label = "Density across x", size=(800, 600)) | ||
p2 = Plots.plot(x, velocity_field[:, i], title="Velocity at Time $t", xlabel="x", ylabel="Velocity(m/s)", label = "Velocity across x", size=(800, 600)) | ||
p3 = Plots.plot(x, pressure_field[:, i], title="Pressure at Time $t", xlabel="x", ylabel="Pressure(Pa)", label = "Pressure across x", size=(800, 600)) | ||
plot(p1, p2, p3, layout = (3, 1)) | ||
end | ||
# Save the animation as a gif | ||
gif(anim, "density_velocity_pressure_over_time.gif", fps = 10) | ||
end | ||
|
||
#density_field, velocity_field, pressure_field = convert_to_primitive(u_values) | ||
|
||
#create_animation(x0_xmax, t_values, density_field, velocity_field, pressure_field) |