# Implementing Dry friction in modeling toolkit

**URL:** <https://discourse.julialang.org/t/implementing-dry-friction-in-modeling-toolkit/70743>\
**Category:** Modelling & Simulations\
**Created:** [November 1, 2021, 3:38pm UTC](https://discourse.julialang.org/t/implementing-dry-friction-in-modeling-toolkit/70743 "2021-11-01T15:38:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![yjr](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yjr/32/207725_2.png) [@yjr](https://discourse.julialang.org/u/yjr)\
**Post date:** [November 1, 2021, 3:38pm UTC](https://discourse.julialang.org/t/implementing-dry-friction-in-modeling-toolkit/70743/1 "2021-11-01T15:38:59Z")

</div>

Hello,  
I would like to implement some function to have components simulating dry friction with modeling toolkit. As a starting point, I think the implementation from modelica is quite good. Most of it can be implemented with IFelse.ifelse function. What I 'm missing is how to have the previous values of a state in modeling toolkit i.e. in modelica they use the pre keyword… Using that they are building a small state machine to track the status of the component i.e. blocked / sliding forward / sliding backward etc …

The model i want to implement in modelica pseudo code (copy from modelica doc):

// part of mixed system of equations  
startFor = pre(mode) == Stuck and sa \> 1;  
startBack = pre(mode) == Stuck and sa \< -1;  
a = der(v);  
a = if pre(mode) == Forward or startFor then sa - 1 elseif  
pre(mode) == Backward or startBack then sa + 1 else 0;  
f = if pre(mode) == Forward or startFor then f0 + f1_v elseif  
pre(mode) == Backward or startBack then -f0 + f1_v else f0\*sa;

// state machine to determine configuration  
mode = if (pre(mode) == Forward or startFor) and v\>0 then Forward elseif  
(pre(mode) == Backward or startBack) and v\<0 then Backward else Stuck;

So any idea on how to have previous state value in modeling toolkit or on another way to implement dry friction ?  
Thanks a lot,  
Br Yves-Julien

---

<div class="post-metadata">

**Author:** ![contradict](https://avatars.discourse-cdn.com/v4/letter/c/ac91a4/32.png) [@contradict](https://discourse.julialang.org/u/contradict)\
**Post date:** [November 1, 2021, 4:38pm UTC](https://discourse.julialang.org/t/implementing-dry-friction-in-modeling-toolkit/70743/2 "2021-11-01T16:38:30Z")

</div>

Conditionals don’t work very well for this use case, I believe this issue is tracking progress on this feature: [https://github.com/SciML/ModelingToolkit.jl/issues/38](https://github.com/SciML/ModelingToolkit.jl/issues/38)

In the mean time, there is a nice implementation using callbacks here: [Modeling Friction with State Machines](https://klaff.github.io/friction_state_machine.jl.html)
