Skip to content

Commit

Permalink
Improved graph handling
Browse files Browse the repository at this point in the history
  • Loading branch information
axla-io committed Oct 30, 2024
1 parent 3333ec1 commit 2d9eb18
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/DynamicRelaxation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module DynamicRelaxation

# Arrays
using LinearAlgebra
using SparseArrays: sparse, spzeros, findnz
using SparseArrays: sparse, spzeros, findnz, nzrange, rowvals
using StaticArrays
using ConcreteStructs
using UnPack
Expand Down Expand Up @@ -47,7 +47,7 @@ export BigonTorqueCondition, clamped, free, pinned, roller
export Px, Py, Pz, Mx, My, Mz, uniform_load, point_loads

# System
export StructuralGraphSystem, default_system, get_cs, get_ode_jac, create_graph
export StructuralGraphSystem, default_system, get_cs, get_ode_jac, create_graph, edges

# Simulation
export LoadScaleRodSimulation, RodSimulation, BigonRodSimulation, get_u0, get_vel_ids, get_state
Expand Down
3 changes: 2 additions & 1 deletion src/analysis/3dof_acceleration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ function rod_acceleration(x, system::StructuralGraphSystem{Vector{Node3DOF}}, vt
a = @SVector zeros(eltype(x), 3)
s = @SVector zeros(eltype(x), 3)

for neighbor in neighbors(graph, vtx)
for idx in nzrange(graph, vtx)
neighbor = rowvals(graph)[idx]
n_i = 3 * (neighbor - 1) + 1
ep = elem_props[edge_index((vtx, neighbor), graph)]
(a, s) = rod_accelerate(a, x_vert, @view(x[n_i:(n_i+2)]), ep, s)
Expand Down
34 changes: 21 additions & 13 deletions src/analysis/graph.jl
Original file line number Diff line number Diff line change
@@ -1,28 +1,36 @@
function neighbors(graph, vtx)
neighbors = findnz(graph[vtx, :])[2]
neighbors = findnz(graph[vtx, :])[1]
return neighbors
end

function edges(graph)
return findnz(graph)
is, js, ids = findnz(graph)

# Filter list
pos_ids = ids .>= 0.0
is_pos = is[pos_ids]
js_pos = js[pos_ids]
ids_pos = ids[pos_ids]

# Order list
order_dict = Dict(v => i for (i, v) in enumerate(ids_pos))

ids_sort = sortperm(ids_pos, by=x -> order_dict[x])
is_sort = is_pos[ids_sort]
js_sort = js_pos[ids_sort]
return zip(is_sort, js_sort)
end

function create_graph(edges, pts)
n_pts = length(pts)
graph = spzeros(Int, n_pts, n_pts)
for edge in edges
graph[edge[1], edge[2]] = 1
graph[edge[2], edge[1]] = 1
for (i, edge) in enumerate(edges)
graph[edge[1], edge[2]] = i
graph[edge[2], edge[1]] = -i
end
return graph
end

function edge_index(adj_matrix, i, j)
row_indices, col_indices, _ = findnz(adj_matrix)
for idx in 1:length(row_indices)
if row_indices[idx] == i && col_indices[idx] == j
return idx # Position of the edge in the sparse storage
end
end
return nothing # Edge not found
function edge_index(edge, graph)
return abs(graph[edge[1], edge[2]])
end
4 changes: 2 additions & 2 deletions src/optimization/load_finding.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function f_acceleration(a, ext_f, i::Int, p)
end

function accelerate_system(u_v, system::StructuralGraphSystem{Vector{Node6DOF}},
simulation::LoadScaleRodSimulation{Node6DOF}, body,
simulation::LoadScaleRodSimulation{Vector{Node6DOF}}, body,
ext_f, du, dr_ids, ω, i, dt, u_t, p, t)
(a, τ, s, j) = rod_acceleration(u_v, system, body, i)
(a, τ) = f_acceleration(a, τ, ext_f, i, p)
Expand All @@ -32,7 +32,7 @@ function accelerate_system(u_v, system::StructuralGraphSystem{Vector{Node6DOF}},
end

function accelerate_system(u_v, system::StructuralGraphSystem{Vector{Node3DOF}},
simulation::LoadScaleRodSimulation{Node3DOF}, body,
simulation::LoadScaleRodSimulation{Vector{Node3DOF}}, body,
ext_f, du, dr_ids, ω, i, dt, u_t, p, t)
(a, s) = rod_acceleration(u_v, system, i)
rod_acceleration(u_v, system, i)
Expand Down
5 changes: 3 additions & 2 deletions src/simulation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ function accelerate_system(u_v, system::StructuralGraphSystem{Vector{Node6DOF}},
end

function accelerate_system(u_v, system::StructuralGraphSystem{Vector{Node3DOF}},
simulation::RodSimulation{Node3DOF}, body,
simulation::RodSimulation{Vector{Node3DOF}}, body,
ext_f, du, dr_ids, ω, i, dt, u_t, p, t)
rod_acceleration(u_v, system, i)
(a, s) = rod_acceleration(u_v, system, i)
a = f_acceleration(a, ext_f, i)
a = constrain_acceleration(a, body)
Expand All @@ -240,7 +241,7 @@ function accelerate_system(u_v, system::StructuralGraphSystem{Vector{Node3DOF}},
end

function get_system_forces(u_v, system::StructuralGraphSystem{Vector{Node6DOF}},
simulation::RodSimulation{Node6DOF}, body,
simulation::RodSimulation{Vector{Node6DOF}}, body,
ext_f, du, dr_ids, ω, i, dt, u_t, p)
(a, τ, s, j) = rod_acceleration(u_v, system, body, i)

Expand Down

0 comments on commit 2d9eb18

Please sign in to comment.