Skip to content

Allow @. syntax for vectorized evaluation with single constitutive model#144

Open
miguelmaso wants to merge 1 commit intomainfrom
broadcastable
Open

Allow @. syntax for vectorized evaluation with single constitutive model#144
miguelmaso wants to merge 1 commit intomainfrom
broadcastable

Conversation

@miguelmaso
Copy link
Collaborator

By default, the @. macro interprets non-simplex types as iterables. It means that it'll try to iterate a single constitutive model.

The proposed implementation allow to evaluate vectorized functions where a constitutive model is passed.

Examples of usage

v = 0.05
θ_vals_cv  = 1:50:2.06θr
λ_vals_cv  = 1:0.5:8.0
cv_vals_cv = @. evaluate_cv(model, θ_vals_cv, λ_vals_cv', v)
p = plot(title="Specific heat under isochoric stretch, v=$v/s", xlabel="Stretch [-]", ylabel="θ/θR [-]", rightmargin=8mm, framestyle=:grid)
contourf!(λ_vals_cv, θ_vals_cv./θr, cv_vals_cv, color=diverging_rb, clims=(-cv_lim, cv_lim), lw=0, lc=:transparent)

I've simplified the code a little, it should produce a map like this:

image

Errors fixed

When the broadcastable dispatch is missing, the @. macro may throw an error like this:

broadcastable: Error During Test at c:\Users\Miguel\source\repos\HyperFEM\test\TestConstitutiveModels\PhysicalModelTests.jl:976
  Got exception outside of a @test
  MethodError: no method matching length(::LinearElasticity3D)
  The function `length` exists, but no method is defined for this combination of argument types.

  Closest candidates are:
    length(::DataStructures.SparseIntSet)
     @ DataStructures C:\Users\Miguel\.julia\packages\DataStructures\qUuAY\src\sparse_int_set.jl:61
    length(::DataStructures.DiBitVector)
     @ DataStructures C:\Users\Miguel\.julia\packages\DataStructures\qUuAY\src\dibit_vector.jl:40
    length(::Profile.HeapSnapshot.Edges)
     @ Profile C:\Users\Miguel\.julia\juliaup\julia-1.12.4+0.x64.w64.mingw32\share\julia\stdlib\v1.12\Profile\src\heapsnapshot_reassemble.jl:24
    ...

  Stacktrace:
    [1] _similar_shape(itr::LinearElasticity3D, ::Base.HasLength)
      @ Base .\array.jl:657
    [2] _collect(cont::UnitRange{Int64}, itr::LinearElasticity3D, ::Base.HasEltype, isz::Base.HasLength)
      @ Base .\array.jl:734
    [3] collect(itr::LinearElasticity3D)
      @ Base .\array.jl:728
    [4] broadcastable(x::LinearElasticity3D)
      @ Base.Broadcast .\broadcast.jl:733

@miguelmaso miguelmaso requested a review from jmartfrut March 19, 2026 10:16
@miguelmaso miguelmaso added the usability Enhancement not affecting directly to functionality, but to a better experience of usage label Mar 19, 2026
@github-actions
Copy link

Benchmark Results (Julia v1)

Time benchmarks
main e4f95cf... main / e4f95cf...
Constitutive models/Visco-elastic Ψ 0.054 ± 0.039 ms 0.055 ± 0.039 ms 0.982 ± 0.99
Constitutive models/Visco-elastic ∂Ψu 0.0596 ± 0.0039 ms 0.0606 ± 0.0029 ms 0.984 ± 0.08
Constitutive models/Visco-elastic ∂Ψuu 0.11 ± 0.016 ms 0.11 ± 0.015 ms 0.999 ± 0.2
Simulations/StaticMechanicalDirichlet 0.138 ± 0.0098 s 0.139 ± 0.01 s 0.996 ± 0.1
Simulations/StaticMechanicalNeumann 0.129 ± 0.019 s 0.132 ± 0.019 s 0.977 ± 0.2
Simulations/ViscoElastic 20 s 20.3 s 0.983
Tensor algebra/Cofactor 0.07 ± 0 μs 0.07 ± 0 μs 1 ± 0
Tensor algebra/Det(A)Inv(A') 0.13 ± 0.019 μs 0.13 ± 0.02 μs 1 ± 0.21
Tensor algebra/δδ_λ_2d 30 ± 0 ns 30 ± 0 ns 1 ± 0
Tensor algebra/δδ_μ_2d 30 ± 0 ns 30 ± 0 ns 1 ± 0
time_to_load 2.94 ± 0.048 s 2.83 ± 0.048 s 1.04 ± 0.024
Memory benchmarks
main e4f95cf... main / e4f95cf...
Constitutive models/Visco-elastic Ψ 1.24 k allocs: 0.0777 MB 1.24 k allocs: 0.0777 MB 1
Constitutive models/Visco-elastic ∂Ψu 1.29 k allocs: 0.0826 MB 1.29 k allocs: 0.0826 MB 1
Constitutive models/Visco-elastic ∂Ψuu 2.25 k allocs: 0.149 MB 2.25 k allocs: 0.149 MB 1
Simulations/StaticMechanicalDirichlet 1.51 M allocs: 0.108 GB 1.51 M allocs: 0.108 GB 1
Simulations/StaticMechanicalNeumann 1.48 M allocs: 0.093 GB 1.48 M allocs: 0.093 GB 1
Simulations/ViscoElastic 0.326 G allocs: 20.5 GB 0.326 G allocs: 20.5 GB 1
Tensor algebra/Cofactor 1 allocs: 0.0781 kB 1 allocs: 0.0781 kB 1
Tensor algebra/Det(A)Inv(A') 4 allocs: 0.25 kB 4 allocs: 0.25 kB 1
Tensor algebra/δδ_λ_2d 0 allocs: 0 B 0 allocs: 0 B
Tensor algebra/δδ_μ_2d 0 allocs: 0 B 0 allocs: 0 B
time_to_load 0.149 k allocs: 11.1 kB 0.149 k allocs: 11.1 kB 1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

usability Enhancement not affecting directly to functionality, but to a better experience of usage

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant