# \[ANN\] SimulationLogs.jl 🪵🪵🪵

**URL:** <https://discourse.julialang.org/t/ann-simulationlogs-jl/58527>\
**Category:** Package Announcements\
**Created:** [April 3, 2021, 9:35pm UTC](https://discourse.julialang.org/t/ann-simulationlogs-jl/58527 "2021-04-03T21:35:20Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 3, 2021, 9:35pm UTC](https://discourse.julialang.org/t/ann-simulationlogs-jl/58527/1 "2021-04-03T21:35:20Z")

</div>

TLDR: Place the `@log` macro in your DifferentialEquations simulation to log intermediate variables.

One of the killer features of Simulink is the ability to log or plot (with a scope) any intermediate signal in a simulation with a click. It’s one of the things that I’ve been sorely missing in my DifferentialEquations.jl simulations. ModelingToolkit gives this ability, but it would be nice to have for non-ModelingToolkit simulations as well.

So let me introduce [SimulationLogs.jl](https://github.com/jonniedie/SimulationLogs.jl). By simply placing the macro `@log` before any variable declaration in your simulation, you can now access that variable by calling `get_log` on your solution object. Logged variables can also be plotted by calling the `scope` function on your solution with the desired variables to be plotted.

For a pretty (I hope) illustrative example, see the [PI cruise control example](https://jonniedie.github.io/SimulationLogs.jl/dev/examples/cruise_control/) in the docs where I use the `scope` function to inspect intermediate variables like so:

 ![](https://global.discourse-cdn.com/julialang/original/3X/4/d/4d4ceb9d653c2d393846d6ac554a2935fcb83803.png)

I’m going to post the FAQ section of the README here in order to head off any of the questions I anticipate people having:

## FAQs

### How does this work with time stepping and variable caches and all that?

Despite the name, `@log` doesn’t actually log anything while the simulation is running. The “logging” happens by calculating values from the stored solutions when `get_log` is called.

### Wait, how does that work?

There is a global `SimulationLog` that is turned off by default. When it is off, the `@log` macro basically doesn’t do anything. The `get_log` function turns on the global log and then calls your simulation function (derivative function, vector field… whatever you want to call it) for each time point (these can be supplied, but will default to the saved time points). A copy of the global simulation log is passed as an output to the user, after which the global log then gets erased and turned back off.

### Will logging variables slow my simulation down?

Nope. There is no runtime overhead because no logging is actually happening during the simulation.

### How does this work when the same `@log` gets called multiple times in the same time step (e.g. in a subfunction that gets called more than once)?

It doesn’t. Don’t do that. There are some good ways we could handle this, but they aren’t implemented right now.

### What if my parameters are changed during the simulation or my simulation depends on some changing global state?

Then this probably won’t work.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 3, 2021, 9:37pm UTC](https://discourse.julialang.org/t/ann-simulationlogs-jl/58527/2 "2021-04-03T21:37:07Z")

</div>

Side note: If you’re going to use this right now, don’t get too comfortable with the current syntax. I just came up with this package last night so things definitely aren’t in their stable state yet.

---

<div class="post-metadata">

**Author:** ![arlk](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/arlk/32/2465_2.png) [@arlk](https://discourse.julialang.org/u/arlk)\
**Post date:** [April 4, 2021, 11:35pm UTC](https://discourse.julialang.org/t/ann-simulationlogs-jl/58527/3 "2021-04-04T23:35:12Z")

</div>

This will be so useful! Scoping signals for debugging has always been a pain in the current framework. Thanks for creating this package!

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 13, 2021, 3:00pm UTC](https://discourse.julialang.org/t/ann-simulationlogs-jl/58527/4 "2021-04-13T15:00:37Z")

</div>

Update: It’s registered now.

---

<div class="post-metadata">

**Author:** ![jonniedie](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jonniedie/32/12842_2.png) [@jonniedie](https://discourse.julialang.org/u/jonniedie)\
**Post date:** [April 19, 2021, 2:00pm UTC](https://discourse.julialang.org/t/ann-simulationlogs-jl/58527/5 "2021-04-19T14:00:08Z")

</div>

More updates:

- Added support for models with `DiscreteCallback`s (including parameters changed with `DiscreteCallbacks`). You just have to pass the callback/callback set in to `get_log` with the keyword `callback`. Alternatively, just use the new `logged_solve` in place of your old `solve` and it will give you a solution that has a property `.log` with all of your logged values in it.
- Can now handle multiple `@log` calls within a single time step. Instead of a vector of values, it will log an `n`x`m` `Matrix` where `n` is the number of time steps and `m` is the number of times that variable was logged per time step.
