Skip to content

Commit

Permalink
reduce memory allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
JTHesse committed Nov 6, 2024
1 parent 40f4678 commit 0f14779
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 40 deletions.
7 changes: 3 additions & 4 deletions src/Compute/compute_field_values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# SPDX-License-Identifier: BSD-3-Clause

using .Helpers: find_active_nodes, get_active_update_nodes
using .Helpers: find_active_nodes, get_active_update_nodes, add_in_place!
using StaticArrays: MMatrix, SMatrix
using .Helpers: invert
include("../Models/Material/material_basis.jl")
Expand Down Expand Up @@ -47,9 +47,8 @@ function get_partial_stresses(datamanager::Module, nodes::Union{SubArray,Vector{
stress = datamanager.get_field("Cauchy Stress", "NP1")

for iID in nodes
stress[iID, :, :] .+=
mapreduce(permutedims, vcat, bond_forces[iID])' *
mapreduce(permutedims, vcat, undeformed_bond[iID]) .* volume[iID]
str = @view stress[iID, :, :]
add_in_place!(str, bond_forces[iID], undeformed_bond[iID], volume[iID])
end
return datamanager
end
Expand Down
4 changes: 2 additions & 2 deletions src/Models/Damage/Energy_release.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ include("../../Support/geometry.jl")
include("../../Support/helpers.jl")
using .Material
using .Geometry
using .Helpers: rotate, fastdot, fastsubtract!
using .Helpers: rotate, fastdot, sub_in_place!
using LinearAlgebra
using StaticArrays
export compute_model
Expand Down Expand Up @@ -118,7 +118,7 @@ function compute_model(
projected_force::Vector{Float64} = zeros(Float64, dof)
relative_displacement::Vector{Float64} = zeros(Float64, dof)

fastsubtract!(bond_displacements, deformed_bond, undeformed_bond)
sub_in_place!(bond_displacements, deformed_bond, undeformed_bond)

for iID in nodes
nlist_temp = nlist[iID]
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Material/Material_Factory.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function init_fields(datamanager::Module)
datamanager.create_constant_node_field("Acceleration", Float64, dof)
datamanager.create_node_field("Velocity", Float64, dof)
datamanager.create_constant_bond_field("Bond Forces", Float64, dof)
dof = datamanager.get_dof()
datamanager.create_constant_bond_field("Temporary Bond Field", Float64, 1)
deformed_coorN, deformed_coorNP1 =
datamanager.create_node_field("Deformed Coordinates", Float64, dof)
deformed_coorN = copy(datamanager.get_field("Coordinates"))
Expand Down
15 changes: 5 additions & 10 deletions src/Models/Material/Material_Models/Ordinary/Ordinary.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

module Ordinary

include("../../../../Support/helpers.jl")
using .Helpers: div_in_place!, mul_in_place!
using LinearAlgebra

export compute_dilatation
Expand Down Expand Up @@ -86,18 +88,11 @@ function get_bond_forces(
deformed_bond::Vector{Vector{Vector{Float64}}},
deformed_bond_length::Vector{Vector{Float64}},
bond_force::Vector{Vector{Vector{Float64}}},
temp::Vector{Vector{Float64}},
)
div_in_place!(temp, bond_force_length, deformed_bond_length)
for iID in nodes
if any(deformed_bond_length[iID] .== 0)
@error "Length of bond is zero due to its deformation."
return nothing
else
mul!.(
bond_force[iID],
bond_force_length[iID] ./ deformed_bond_length[iID],
deformed_bond[iID],
)
end
mul_in_place!(bond_force[iID], deformed_bond[iID], temp[iID])
end
return bond_force
end
Expand Down
13 changes: 6 additions & 7 deletions src/Models/Material/Material_Models/Ordinary/PD_Solid_Elastic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module PD_Solid_Elastic
include("../../material_basis.jl")
using .Material_Basis: get_symmetry
include("./Ordinary.jl")
include("../../../../Support/helpers.jl")
using .Helpers: add_in_place!
using TimerOutputs
using StaticArrays
using .Ordinary:
Expand Down Expand Up @@ -136,6 +138,7 @@ function compute_model(
omega = datamanager.get_field("Influence Function")
undeformed_bond_length = datamanager.get_field("Bond Length")
bond_force = datamanager.get_field("Bond Forces")
temp = datamanager.get_field("Temporary Bond Field")

bond_force_deviatoric_part = datamanager.get_field("Bond Forces Deviatoric")
bond_force_isotropic_part = datamanager.get_field("Bond Forces Isotropic")
Expand Down Expand Up @@ -178,13 +181,9 @@ function compute_model(
bond_force_deviatoric_part,
bond_force_isotropic_part,
)
@timeit to "get_bond_forces" bond_force = get_bond_forces(
nodes,
bond_force_deviatoric_part + bond_force_isotropic_part,
deformed_bond,
deformed_bond_length,
bond_force,
)
add_in_place!(temp, bond_force_deviatoric_part, bond_force_isotropic_part)
@timeit to "get_bond_forces" bond_force =
get_bond_forces(nodes, temp, deformed_bond, deformed_bond_length, bond_force, temp)

return datamanager
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ function compute_model(
omega = datamanager.get_field("Influence Function")
bond_damage = datamanager.get_bond_damage("NP1")
bond_force = datamanager.get_field("Bond Forces")
temp = datamanager.get_field("Temporary Bond Field")
shear_modulus = material_parameter["Shear Modulus"]
bulk_modulus = material_parameter["Bulk Modulus"]
bond_force_deviatoric_part = datamanager.get_field("Bond Forces Deviatoric")
Expand Down Expand Up @@ -203,6 +204,7 @@ function compute_model(
deformed_bond,
deformed_bond_length,
bond_force,
temp,
)
return datamanager
end
Expand Down
76 changes: 70 additions & 6 deletions src/Support/helpers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export interpol_data
export progress_bar
export invert
export rotate
export fastsubtract!
export sub_in_place!
export add_in_place!
export div_in_place!
export fastdot
export fast_mul!
export mat_mul!
Expand Down Expand Up @@ -89,7 +91,25 @@ function mat_mul_transpose_mat!(C, A, B)
C[m, n] = 0.5 * Cmn
end
end
function add_in_place!(
C,
A::Vector{Vector{T}},
B::Vector{Vector{T}},
factor = 1,
) where {T<:Number}
m = length(A)
n = length(A[1])

for i = 1:n
for j = 1:n
for k = 1:m
C[i, j] += A[k][i] * B[k][j] * factor
end
end
end

return C
end
function get_MMatrix(len::Int64)

if len == 4
Expand All @@ -106,14 +126,58 @@ function get_MMatrix(len::Int64)
return nothing
end
end

function fastsubtract!(c, a, b)
@inbounds for i eachindex(a)
@inbounds for j eachindex(a[i])
c[i][j] .= a[i][j] .- b[i][j]
function mul_in_place!(
C::Vector{Vector{T}},
A::Vector{Vector{T}},
B::Vector{T},
) where {T<:Number}
# Check if dimensions match
@assert length(C) == length(A) == length(B)

@inbounds for i eachindex(A)
@inbounds for j eachindex(A[i])
C[i][j] = A[i][j] * B[i]
end
end
end
function sub_in_place!(
C::Vector{Vector{Vector{T}}},
A::Vector{Vector{Vector{T}}},
B::Vector{Vector{Vector{T}}},
) where {T<:Number}
# Check if dimensions match
@assert length(C) == length(A) == length(B)

@inbounds for i eachindex(A)
@inbounds for j eachindex(A[i])
C[i][j] .= A[i][j] .- B[i][j]
end
end
end
function add_in_place!(
C::Vector{Vector{T}},
A::Vector{Vector{T}},
B::Vector{Vector{T}},
) where {T<:Number}
# Check if dimensions match
@assert length(C) == length(A) == length(B)

@inbounds for i eachindex(A)
C[i] .= A[i] .+ B[i]
end
end
function div_in_place!(
C::Vector{Vector{T}},
A::Vector{Vector{T}},
B::Vector{Vector{T}},
) where {T<:Number}
# Check if dimensions match
@assert length(C) == length(A) == length(B)

@inbounds for i eachindex(A)
C[i] .= A[i] ./ B[i]
end
end
function fastdot(a, b, absolute = false)
c = 0.0
@inbounds @simd for i eachindex(a, b)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,25 @@ end
deformed_bond[1][1][1] = 1
deformed_bond[1][2][1] = 1
bond_force = [[fill(0.0, dof) for j = 1:n] for n in nBonds]
temp = [fill(0.0, n) for n in nBonds]
bond_force = Ordinary.get_bond_forces(
vec,
bond_force_length,
deformed_bond,
deformed_bond_length,
bond_force,
temp,
)
@test bond_force == [[[0.5, 0.0], [0.5, 0.0]], [[0.0, 0.0], [0.0, 0.0]]]
deformed_bond_length[2][1] = 0
@test isnothing(
Ordinary.get_bond_forces(
vec,
bond_force_length,
deformed_bond,
deformed_bond_length,
bond_force,
),
)
# deformed_bond_length[2][1] = 0
# @test isnothing(
# Ordinary.get_bond_forces(
# vec,
# bond_force_length,
# deformed_bond,
# deformed_bond_length,
# bond_force,
# temp,
# ),
# )
end

0 comments on commit 0f14779

Please sign in to comment.