Process Simulations

Energy consumption simulation

The EnergySimulator class provides a realistic simulation of utility consumption (power, gas, and hot water) for a typical household or building. It models a continuous, cyclical daily pattern with added randomization to mimic real-world usage fluctuations.

This class is designed to run as a background process. You create an instance with your desired annual consumption targets, start the simulation, and then you can query it at any time to get the live consumption rate or the total accumulated consumption.

How It Works: The Simulation Model

The simulator's logic is based on a few key principles to achieve realism:

  • Annual Baseline: You provide the total annual consumption for power, gas, and water. The simulator uses this to calculate an average consumption rate per second.

  • Daily Cycles (Sine Wave): Consumption is not constant. It follows a daily rhythm. The simulator uses a sine wave to model this, creating natural peaks and troughs in usage. For example, power usage peaks in the evening, while hot water usage peaks in the morning. This is controlled by a phaseShift.

  • Randomization: To avoid a perfectly predictable pattern, a small amount of random "noise" is added to the calculation every second, making the live consumption values fluctuate slightly, just as they would in reality.

create

Creates a new energy simulator instance based on total annual consumption figures. The simulation starts automatically upon creation.

Parameters

  • config: An object specifying the total annual consumption for each utility.

    • power: Total annual power consumption in kilowatt-hours (kWh).

    • gas: Total annual gas consumption in cubic meters (m³).

    • water: Total annual hot water consumption in cubic meters (m³).

Example

start

Starts the simulation loop. Once started, the internal state of the simulator will be updated every second. Note: The simulation starts automatically when the instance is created.

Output

Returns true when the simulation has been started.

stop

Stops and pauses the simulation loop. The internal consumption values will no longer be updated until start() is called again.

Output

Returns true when the simulation has been stopped.

getLiveValue

Gets the current, instantaneous consumption rate for a specific utility.

Parameters

  • mediaType: The type of utility to query. Must be one of 'power', 'gas', or 'water'.

Example

Output

A number representing the live consumption rate. The units are:

  • Power: kilowatts (kW)

  • Gas: cubic meters per hour (m³/h)

  • Water: cubic meters per hour (m³/h)

getAggregatedValue

Gets the total accumulated consumption for a specific utility since the simulator instance was started.

Parameters

  • mediaType: The type of utility to query. Must be one of 'power', 'gas', or 'water'.

Example

Output

A number representing the total accumulated consumption. The units are:

  • Power: kilowatt-hours (kWh)

  • Gas: cubic meters (m³)

  • Water: cubic meters (m³)

Complete Usage Example

Here’s a step-by-step example showing how to use the EnergySimulator.

Step 1: Create a simulator instance

This will immediately start the simulation in the background.

Step 2: Periodically read live data

You can use another function, like a timer, to periodically query the live values from the simulator.

Step 3: Check total consumption after some time

After the simulation has been running for a while, you can check the total accumulated values.

Step 4: Stop the simulation

When you no longer need the data, stop the simulation to free up resources.

Silo fill level simulation

The SiloSimulator class provides a dynamic simulation of a silo's fill level. It models a continuous process where a silo gradually empties over a configurable period and then automatically triggers a rapid refill cycle once it reaches a low threshold (10% of capacity).

This class is an event-driven utility. You start the simulation and then listen for real-time level updates to power dashboards, trigger alerts, or test data pipelines. An instance must be created to represent a single silo.

create

Creates a new silo simulator instance with a specified capacity and emptying time.

Parameters

  • options: An object for configuring the silo's properties.

    • capacity: The maximum fill level of the silo. Defaults to 100.

    • timeToEmpty: The approximate time, in seconds, for the silo to empty from 100% down to the 10% refill threshold. Defaults to 60.

Example

start

Starts the simulation loop. Once started, the silo will begin its emptying/refilling cycle, and level updates will be emitted every second to any registered listeners.

Output

Returns true when the simulation has been started.

stop

Stops and pauses the simulation loop. No more level updates will be sent until start() is called again.

Output

Returns true when the simulation has been stopped.

getLevel

Manually retrieves the current fill level of the silo at any given moment.

Output

A number representing the current fill level (e.g., 85.34).

onLevelUpdate

Registers a handler (listener) that is triggered every second while the simulation is running. This is the primary way to receive data from the simulator. The listener function receives the current level as its only argument.

Parameters

  • callback: The callback function to execute on each level update.

Example

Complete Usage Example

Here’s a step-by-step example showing how to use the SiloSimulator.

Step 1: Create a silo instance

First, create the simulator with your desired parameters.

Step 2: Register a listener to receive data

Next, set up a handler to process the level updates as they are emitted.

Step 3: Start the simulation

The silo will now begin emptying, and your listener from Step 2 will be called every second with the new level.

After about 10 seconds, the silo level will drop to near 20 (10% of capacity). It will then automatically switch to "refilling" mode and quickly fill back up to 200 before starting the emptying cycle again.

Step 4: Manually check the level (optional)

At any point while it's running, you can get the current level on demand.

Step 5: Stop the simulation

When you no longer need the data, stop the simulation.

Last updated

Was this helpful?