How do I add a daily or weekly cycle to a rate?¶
Demand, solar output and many flows swing over a day and drop at weekends. SIN with PI makes the daily swing, and MODULO picks the days of the week. The script drives a tank pump with both.
from nagasim import build as nb
# A pump fills a tank at 10 m3 an hour, swinging 50% up and down over a 24-hour day (SIN, PI), and
# only runs at 20% on the two days of every seven that are weekend (MODULO). Time is in hours.
m = nb.Model(time_limit=7 * 24, time_unit="hours", sd={"dt": 0.5, "method": "euler"})
m.add_auxiliary(name="Daily", expr="10 * (1 + 0.5 * SIN(2 * PI() * TNOW / 24))")
m.add_auxiliary(name="Weekend", expr="IF(MODULO(TNOW / 24, 7) >= 5, 0.2, 1)")
m.add_level(name="Tank", initial=0)
m.add_flow(name="pump", to="Tank", rate='AUX("Daily") * AUX("Weekend")')
series = m.run(seed=1)["timeseries"].pivot(index="Time", columns="Series", values="Value")
rate = series["aux/Daily"] * series["aux/Weekend"]
print(f"peak rate {rate.max():.1f} at hour {rate.idxmax():.0f}, lowest {rate.min():.1f} at hour {rate.idxmin():.0f}")
for day in (0, 4, 5, 6):
hours = series.loc[(series.index >= day * 24) & (series.index < day * 24 + 24)]
print(f"day {day}: tank gains {hours['level/Tank'].iloc[-1] - hours['level/Tank'].iloc[0]:6.1f} m3")
Save it as recipe-daily-cycle.py and run it with python. The script seeds itself, so the numbers below are the same on your machine.
peak rate 15.0 at hour 6, lowest 1.0 at hour 162
day 0: tank gains 235.5 m3
day 4: tank gains 227.3 m3
day 5: tank gains 47.1 m3
day 6: tank gains 45.5 m3
How it works¶
10 * (1 + 0.5 * SIN(2 * PI() * TNOW / 24))swings 50% either way around 10 once every 24 hours.MODULO(TNOW / 24, 7) >= 5is true on the sixth and seventh day, and the rate then falls to 20%.- A weekday gains about 230 m3 and a weekend day about 46, close to the 240 and 48 an exact integral gives.
Elements used¶
Back to all recipes.