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

performance topic: SpringDamperPtP #132

Closed
johhell opened this issue Jan 7, 2024 · 3 comments · Fixed by #133
Closed

performance topic: SpringDamperPtP #132

johhell opened this issue Jan 7, 2024 · 3 comments · Fixed by #133

Comments

@johhell
Copy link
Contributor

johhell commented Jan 7, 2024

I tried to run a Modia3D model several times and found differences in simulation time

test 1: using explicit functions for spring and damping

Instantiating model tetherTest
  in module: Main.T7
  in file: /home/hans/Projekte/OM/lib--------------
  0.273864 seconds (401.92 k allocations: 16.911 MiB, 64.15% compilation time)
  0.097515 seconds (329.34 k allocations: 12.203 MiB)
  0.123545 seconds (329.34 k allocations: 12.203 MiB, 16.86% gc time)
  0.097040 seconds (329.34 k allocations: 12.203 MiB)
  0.097082 seconds (329.34 k allocations: 12.203 MiB)
  0.096970 seconds (329.34 k allocations: 12.203 MiB)
Main.T7

test 2: using parameters for spring and damping

takes longer, due to additional compilation time for eval()

Instantiating model tetherTest
  in module: Main.T7
  in file: /home/hans/Projekte/OM/lib--------------
  0.287922 seconds (370.37 k allocations: 16.131 MiB, 64.70% compilation time)
  0.157830 seconds (309.42 k allocations: 12.206 MiB, 14.23% gc time, 17.60% compilation time)
  0.128409 seconds (309.40 k allocations: 12.204 MiB, 21.92% compilation time)
  0.129223 seconds (309.40 k allocations: 12.204 MiB, 21.82% compilation time)
  0.131308 seconds (309.40 k allocations: 12.204 MiB, 21.95% compilation time)
  0.129613 seconds (309.40 k allocations: 12.204 MiB, 21.62% compilation time)
Main.T7

test 3: using parameters for spring and damping with modified SpringDamperPtP.jl

Instantiating model tetherTest
  in module: Main.T7
  in file: /home/hans/Projekte/OM/lib--------------
  0.259682 seconds (364.23 k allocations: 15.735 MiB, 61.97% compilation time)
  0.099671 seconds (303.25 k allocations: 11.807 MiB)
  0.097721 seconds (303.25 k allocations: 11.807 MiB)
  0.098235 seconds (303.25 k allocations: 11.807 MiB)
  0.122374 seconds (303.25 k allocations: 11.807 MiB, 14.12% gc time)
  0.097432 seconds (303.25 k allocations: 11.807 MiB)
Main.T7

a PR will be opened soon. It works, but I'm not the expert - check it carefully. Danke.
code sniplet below:

        if (!isa(springForceLaw, Function))
            stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw)
            fkt1(_x) = stiffness*_x
        else
            fkt1 = springForceLaw
        end
@johhell
Copy link
Contributor Author

johhell commented Jan 7, 2024

an additional MWE (without Modia3D)

module T1

mutable struct SpringDamperPtP
    path::String
    springForceFunction::Function
    damperForceFunction::Function

    function SpringDamperPtP(; path::String = "",
                                  springForceLaw::Union{Real, Function} = 0.0,
                                  damperForceLaw::Union{Real, Function} = 0.0 )

        if (!isa(springForceLaw, Function))
#             stiffness = Modia3D.convertAndStripUnit(F, u"N/m", springForceLaw)
            stiffness = springForceLaw
            fkt1(_x) = stiffness*_x
        else
            fkt1 = springForceLaw
        end

        if (!isa(damperForceLaw, Function))
#             damping = Modia3D.convertAndStripUnit(F, u"N*s/m", damperForceLaw)
            damping = damperForceLaw
            fkt2(_x) = damping*_x
        else
            fkt2 = damperForceLaw
        end

        return new(path, fkt1, fkt2)
    end
end


function evaluateForceElement(force::SpringDamperPtP, )
    pos = 100.0
    vel = 10.0
    fc = force.springForceFunction(pos)
    fd = force.damperForceFunction(vel)

    println("$(force.path)   fc = $(fc)    fd = $(fd)")
    println()

    nothing
end


fc(x) = 100.0*x
fd(x) = 1.111*x

A = SpringDamperPtP(;path="function + function     ", springForceLaw=fc, damperForceLaw=fd)
B = SpringDamperPtP(;path="parameter + parameter   ", springForceLaw=33.0, damperForceLaw=0.999)
C = SpringDamperPtP(;path="function + parameter    ", springForceLaw=fc, damperForceLaw=0.999)
D = SpringDamperPtP(;path="parameter + function    ", springForceLaw=33.0, damperForceLaw=fd)
evaluateForceElement(A)
evaluateForceElement(B)
evaluateForceElement(C)
evaluateForceElement(D)

end

@GerhardHippmann GerhardHippmann linked a pull request Jan 8, 2024 that will close this issue
@GerhardHippmann
Copy link
Collaborator

Fixed by #133

@johhell
Copy link
Contributor Author

johhell commented Jan 8, 2024

the same code block (with eval(...) ) is in included in Bushing.jl. Should be updated as well.
Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants