
The model’s independent spatial and temporal dimensions: nested geographies, a nested calendar, and the planning horizon.
Time: calendars and horizons
Time and space are the two dimensions on which energyRt balances
commodities. Their design is deliberately parallel: a commodity’s
timeframe chooses a calendar level, while its
geoframe chooses a geographic level. Start with the finest
resolution needed for the question; coarser balances are modelling
assumptions, not merely performance settings.
Why sub-annual time matters
Annual averages hide what makes energy systems hard: the sun sets, wind stalls, demand peaks in the evening. A model that balances electricity once a year sees none of it — storage is pointless, solar looks dispatchable, peak capacity is free. A calendar gives the model sub-annual time timeslices, and the timeslice count is the main dial between realism and model size:
| calendar | structure | timeslices |
|---|---|---|
annual |
one annual timeslice | 1 |
s4_hp3 |
4 seasons × day/night/peak | 12 |
s4_h24 |
an average day per season, 24 hours | 96 |
m12_h24 |
an average day per month, 24 hours | 288 |
d365 |
365 days | 365 |
d365_h24_1dps |
one day per season of the hourly year | 96 |
d365_h24_1dpm |
one day per month of the hourly year | 288 |
d365_h24 |
365 days × 24 hours | 8,760 |
Model variables scale roughly linearly with timeslices — the 96-timeslice TOPIA case solves in seconds on GLPK, the 288-timeslice variant is noticeably heavier.
Representative days: sampled or averaged
A representative or typical day can be modelled in two ways: by sampling, which keeps real days of the year and leaves the rest out, or by averaging, which merges all the days of a season into one average day.
Three numbers describe every timeslice of a calendar:
-
share: the length of one unit of the timeslice’s timeframe, as a fraction of the year. For a timeslice counted in hours it is one hour, 1/8760; for a month, a season or a day it is that timeslice’s own length. -
width: how long the timeslice is on the time axis, in those units. 1 by default. -
weight: how many times the timeslice repeats in the year, which is also the multiplicity of a sample. Zero leaves the timeslice out.
The hours a timeslice stands for are share x
width x weight x 8760, and one step of the
model is share x width.
one_timeslice <- function(cal, timeslice) {
tt <- as.data.frame(cal@timetable)
row <- tt[tt[[cal@default_timeframe]] == timeslice, ]
data.frame(
calendar = cal@name, timeframe = cal@default_timeframe,
timeslice = timeslice, share_hours = row$share * 8760, width = row$width,
weight = row$weight, year_fraction = round(cal@year_fraction, 4)
)
}
rbind(
one_timeslice(calendars$d365_h24, "d015_h00"),
one_timeslice(calendars$d365_h24_1dps, "d015_h00"),
one_timeslice(calendars$s4_h24, "WIN_h00"),
one_timeslice(calendars$s4_hp3, "WIN_DAY")
)
#> calendar timeframe timeslice share_hours width weight year_fraction
#> 1 d365_h24 HOUR d015_h00 1 1 1.00 1.000
#> 2 d365_h24_1dps HOUR d015_h00 1 1 91.25 0.011
#> 3 s4_h24 HOUR WIN_h00 1 1 90.00 0.011
#> 4 s4_hp3 HOUR WIN_DAY 1 11 90.00 0.011
calendars$d365_h24_1dps@misc$sample_of$name
#> [1] "d365_h24"
calendars$s4_h24@misc$sample_of
#> NULL
calendars$s4_hp3@units
#> $HOUR
#> [1] 1-
Full (
d365_h24): every timeslice is one hour of the year, 1 wide, with weight 1. -
Sampled (
d365_h24_1dps): the timeslice is one real hour of a day that was kept.weightscales it to the year (91.25, the hours of the year over the 96 kept), and the sample records the calendar it was drawn from, which locates the days it left out: a storage level is not carried across them. -
Averaged (
s4_h24):WIN_h00is the first hour of the average winter day. It is one hour too, and itsweightis the 90 days it stands for. The calendar records no source and has no gaps: its 96 hours are the whole year. -
Blocks (
s4_hp3) use width and weight together:WIN_DAYis anHOURtimeslice as well, with the share of one hour, but it is 11 hours wide and repeats on 90 days. The day is cut intoDAY(h06 to h16),PEAK(h17 to h20, 4 hours) andNIGHT(h21 to h05, 9 hours), in that order.
The numbers do not tell an averaged calendar from a sample. A sample
is known by the source it records, or by timeslices of zero weight:
setting the weight of a timeslice to zero leaves it out, as
sample_calendar() does. year_fraction is the
part of the year the timeslices of positive weight enumerate, the sum of
share x width: 96/8760 for all three 96-hour
designs.
The share of an HOUR timeslice is one hour in every
calendar; what tells a block from a single hour is its width. An hour of
the day is read off single hours only. The storage level and the ramp
limits move from one timeslice to the next by a step of
share x width, one hour on s4_h24
and eleven hours on WIN_DAY. A day cut into blocks
therefore sizes the same store as the same day written hour by hour when
the data are uniform inside each block, and the package tests solve both
(tests/testthat/test-calendar-width.R). What a sampled and
an averaged calendar do not share is the data: a sample keeps the
weather and the demand of the days it kept, an average day those of the
mean day.
The make_timetable() grammar
A calendar’s structure is a nested named list: each
element is a level (e.g. SEASON,
PART), holding its timeslices. Timeslice names
must be alphanumeric (they become set elements in the solver files). The
simplest form lists timeslice names — the year is divided equally:
tt <- make_timetable(list(
SEASON = c("WIN", "SPR", "SUM", "FAL"),
PART = c("NIGHT", "DAY", "EVENING")
))
head(tt) # 4 x 3 = 12 leaf timeslices, equal shares
#> ANNUAL SEASON PART share weight width
#> <char> <char> <char> <num> <num> <num>
#> 1: ANNUAL FAL FAL_DAY 0.08333333 1 1
#> 2: ANNUAL FAL FAL_EVENING 0.08333333 1 1
#> 3: ANNUAL FAL FAL_NIGHT 0.08333333 1 1
#> 4: ANNUAL SPR SPR_DAY 0.08333333 1 1
#> 5: ANNUAL SPR SPR_EVENING 0.08333333 1 1
#> 6: ANNUAL SPR SPR_NIGHT 0.08333333 1 1make_timetable() divides the year among the timeslices
it is given, so each is its own unit: share is its length,
and width and weight are 1. It does not build
hours. The share of a timeslice of a level named HOUR is
one hour of the year, and a table that gives it a longer share is
refused; the hours of an average day are written as a table with
share = 1/8760 and the days in weight, as in
calendars$s4_h24@timetable, and a block of hours with its
hours in width as well, as in
calendars$s4_hp3@timetable.
Unequal shares are given per timeslice; a nested
list(<share>, <LEVEL> = ...) attaches child
levels. TOPIA’s 12-timeslice calendar makes peak hours short and winter
nights long:
tt12 <- make_timetable(list(
SEASON = list(
WIN = list(1 / 4, HOURTYPE = list(DAY = 9 / 24, NGT = 12 / 24, PK = 3 / 24)),
SPR = list(1 / 4, HOURTYPE = list(DAY = 11 / 24, NGT = 11 / 24, PK = 2 / 24)),
SUM = list(1 / 4, HOURTYPE = list(DAY = 12 / 24, NGT = 9 / 24, PK = 3 / 24)),
FAL = list(1 / 4, HOURTYPE = list(DAY = 11 / 24, NGT = 11 / 24, PK = 2 / 24))
)
))
head(tt12)
#> ANNUAL SEASON HOURTYPE share weight width
#> <char> <char> <char> <num> <num> <num>
#> 1: ANNUAL FAL FAL_DAY 0.11458333 1 1
#> 2: ANNUAL FAL FAL_NGT 0.11458333 1 1
#> 3: ANNUAL FAL FAL_PK 0.02083333 1 1
#> 4: ANNUAL SPR SPR_DAY 0.11458333 1 1
#> 5: ANNUAL SPR SPR_NGT 0.11458333 1 1
#> 6: ANNUAL SPR SPR_PK 0.02083333 1 1From timetable to calendar
newCalendar() turns a timetable into a
calendar object. Note that name is the
first argument — always pass the timetable by name
(timetable =), or name name/desc
so the timetable lands in the right slot:
cal <- newCalendar(timetable = tt, name = "s4p3")
cal@name
#> [1] "s4p3"
nrow(cal@timeslice_share) # timeslices with their share of the year
#> [1] 17
head(as.data.frame(cal@timeslice_share), 3)
#> timeslice share weight
#> 1 ANNUAL 1.00 1
#> 2 FAL 0.25 1
#> 3 SPR 0.25 1
cal@timeframe_rank # levels, coarsest (ANNUAL) to finest
#> ANNUAL SEASON PART
#> 1 2 3The derived slots do the bookkeeping the model needs:
-
@timeslice_share— each timeslice’s share of the year (the weight used whenever timeslice values are aggregated); -
@timeframes— the timeslice sets at every level (ANNUAL,SEASON, …); -
@timeframe_rank— the level hierarchy; a commodity’stimeframepicks the level it is balanced on.
autoplot() draws the nested structure:
autoplot(cal)
Ready-made calendars
The package ships a calendars list, built by
data-raw/calendars.R. A few small designs use exactly the
grammar above; the mainstream family (m12,
m12a, q4, s4,
s4_h24, m12_h24, wd7_h24,
w52_h24) is generated from the timescales catalog
at data-build time — timescales is not a runtime
dependency — with day-proportional shares (a month’s share is its day
count over 365; seasons are WIN/SPR/SUM/FAL in calendar
order):
names(calendars)
#> [1] "annual" "m12" "m12a"
#> [4] "q4" "s4" "s4_h24"
#> [7] "m12_h24" "wd7_h24" "w52_h24"
#> [10] "d365" "d365_h24" "s4_hp3"
#> [13] "m12_subset_q1" "d365_h24_1dps" "d365_h24_1dpm"
#> [16] "d365_h24_3dpm" "m12_d365_h24" "m12_d365_h24_1dpm"
topia$modules$calendars$d365_h24@desc
#> [1] "YDAY/HOUR calendar (8760 timeslices; truncated, regular) (timescales 0.8.0)"
s4 <- as.data.frame(calendars$s4@timetable)[, c("SEASON", "share")]
s4$share <- round(s4$share, 4)
s4
#> SEASON share
#> 1 WIN 0.2466
#> 2 SPR 0.2521
#> 3 SUM 0.2521
#> 4 FAL 0.2493Four entries are sampled calendars: row subsets of a
parent design whose year_fraction < 1 is the surviving
share of the year. They solve partial years natively — declared
timeslices, weights, and storage cycles all follow the sample. Each
records its source calendar, from which interpolate_model()
reads where the sample leaves a gap: a storage level is not carried from
one sampled day to the next unless they are adjacent in the year
(bridge_storage_gaps):
calendars$d365_h24_1dpm@year_fraction # one day per month
#> [1] 0.03287671
calendars$d365_h24_1dpm@misc$sample_of$name
#> [1] "d365_h24"Pick one and pass it to newModel(calendar = ...). A
calendar has to hold the timeslices the model’s own data is declared at:
the TOPIA kit’s profiles are measured on d365_h24, so its
vignettes use that calendar or a sampled subset of it such as
d365_h24_1dpm. A design whose labels the data never
mentions is refused at interpolation, not quietly ignored.
Fiscal and non-calendar years
A milestone in energyRt is an integer year —
horizon@intervals$mid — and that stays true whatever the
reporting convention. What changes for a fiscal year is where the year
begins, which is a property of the calendar:
cal_fy <- newCalendar(
timetable = make_timetable(list(SEASON = c("WINTER", "SUMMER"))),
year_start = list(month = 4L, day = 1L) # April-start, e.g. India, Japan
)
cal_fy@year_start
#> $month
#> [1] 4
#>
#> $day
#> [1] 1This follows the convention timescales uses: model year
y spans [year_start(y), year_start(y + 1)),
and y is the starting Gregorian year, so
“FY 2021-22” is model year 2021.
A non-January anchor changes how milestones are displayed.
year_label() gives one label per milestone:
hor <- newHorizon(2025:2034, c(1, 4, 5))
mod <- newModel("fy_demo", region = "R1", calendar = cal_fy, horizon = hor)
year_label(mod)
#> 2025 2027 2032
#> "FY2025-26" "FY2027-28" "FY2032-33"The label is presentation only. It never reaches the solver: the sets, the parameter tables, the backend files and the decoded solution all keep the integer year. Ask for labels explicitly when you want them:
getData(scen, "vTechCap", yearsAsFactors = TRUE) # year as labelled factor
getData(scen, "vTechCap") # year as integer (default)To name periods yourself — whether or not a fiscal calendar is
involved — give intervals a label column. An
explicit label always wins over the derived one, and labels must be
unique:
newHorizon(intervals = data.frame(
start = c(2025, 2030),
mid = c(2025, 2030),
end = c(2029, 2034),
label = c("FY2025-26", "FY2030-31")
))@intervals
#> start mid end label
#> <int> <int> <int> <char>
#> 1: 2025 2025 2025 FY2025-26
#> 2: 2026 2026 2029 2026
#> 3: 2030 2030 2034 FY2030-31One limit worth knowing: because the key is the integer
mid, two milestones cannot share a calendar year. Labels
rename periods; they do not add them.
The anchor is currently carried and reported, not acted on — it sets
the default labels and travels with the calendar, but
timeslice-to-timestamp alignment and year_fraction still
work on the calendar year.
Reading time off a timeslice
A timeslice name is a label, not a data structure: what
"d100_h20" means is decided by the calendar it belongs to,
so every conversion takes that calendar. They live in timescales, which
owns the calendar vocabulary:
library(timescales)
# label -> number. The calendar is consulted; the text is never parsed.
tsl2hour(c("d001_h00", "d100_h20"), "d365_h24") # 0 20
#> [1] 0 20
tsl2yday(c("d001_h00", "d100_h20"), "d365_h24") # 1 100
#> [1] 1 100
# MONTH is not a timeframe of d365_h24 -- it is derived from the calendar
tsl2month(c("d001_h00", "d100_h20"), "d365_h24") # 1 4
#> [1] 1 4
# number -> label
hour2HOUR(c(0, 13, 23)) # "h00" "h13" "h23"
#> [1] "h00" "h13" "h23"
yday2YDAY(c(1, 365)) # "d001" "d365"
#> [1] "d001" "d365"A timeframe is available when the calendar determines it.
tsl2hour() works on "d365_h24", where each
timeslice is one hour, and is an error on "m12", where a
month spans twenty-four of them – rather than returning a guess.
tsl2dtm() / dtm2tsl() convert between
timeslices and date-times, which is what you want when joining model
output with observed hourly data. Timeslice labels carry no year, so
tsl2dtm() asks for one:
tsl2dtm(c("d001_h00", "d100_h20"), "d365_h24", year = 2021)
#> [1] "2021-01-01 00:00:00 UTC" "2021-04-10 20:00:00 UTC"
dtm2tsl(as.POSIXct("2021-04-10 20:00", tz = "UTC"), "d365_h24")
#> [1] "d100_h20"Timeframes: commodities and processes
Every commodity carries a timeframe — the calendar level
it is balanced on. Fuels are typically
ANNUAL; electricity HOUR:
ELC <- newCommodity("ELC", timeframe = "HOUR") # balanced every timeslice
COA <- newCommodity("COA", timeframe = "ANNUAL") # balanced once a yearThe timeframe is one the model’s calendar holds; a commodity declared
at a timeframe the calendar does not have is refused at interpolation,
with the calendar’s timeframes named in the message. The averaged
calendars hold their hours at HOUR, as the hourly year and
its samples do.
A process operates at the finest timeframe among its
commodities (a gas plant producing hourly ELC is dispatched
hourly), overridable via newTechnology(timeframe = ...) —
see the model bricks article for
details. The practical consequence: raising the calendar’s resolution
refines only the commodities and processes whose timeframes
follow it — annual bookkeeping stays cheap.
Space: regions and geoscales
A model’s region vector is its finest spatial
resolution. A Geoscale adds the hierarchy above those
regions: for example, province inside zone inside nation. It is built
from a leaf table with one row per finest region and one column per
level:
gs <- geoscale_from_leaftable(
data.frame(
nation = c("NA1", "NA1", "NA2", "NA2"),
province = c("R1", "R2", "R3", "R4"),
km2 = rep(1, 4) # equal illustrative weights for the icicle widths
),
geoframes = c("nation", "province"),
weights = "km2",
default_weight = "km2",
name = "demo"
)
gg_geoscale(gs, type = "icicle", weight = "km2")
Attach the hierarchy when the model is created, or later with
setGeoscale():
mod <- newModel("demo", region = c("R1", "R2", "R3", "R4"), geoscale = gs)
mod <- setGeoscale(mod, gs)Attaching a geoscale does not coarsen the model. It makes coarser
cells available. A commodity opts into one with
geoframe:
ELC <- newCommodity("ELC", timeframe = "HOUR")
STL <- newCommodity("STL", timeframe = "ANNUAL", geoframe = "nation")ELC is balanced separately in R1–R4. STL is
balanced in NA1 and NA2. Within each nation, that coarse steel balance
is exactly equivalent to free, unlimited, lossless transport. Use it
only when that is an acceptable physical and economic assumption.
Congestion, transport costs, or losses require an explicit
trade object at the relevant level.
Processes may be finer than the commodities they use: a regional mill can contribute to a national steel balance. A process cannot produce into a finer balance than its own location, because the destination would be ambiguous. Supply, demand, storage, import, export, weather, and trade declarations must likewise resolve to a level compatible with their commodity.
Changing spatial resolution
There are three distinct operations; choose by intent:
| intent | operation | what changes |
|---|---|---|
| study a sub-territory in full detail | subset_model_regions() |
drops regions and routes outside the sample |
| solve the whole territory at a coarser level | aggregate_model_regions() |
replaces fine regions with coarse cells |
| retain several process archetypes per coarse cell | aggregate_model_regions(..., clusters = ...) |
preserves selected within-cell variation |
Aggregation is lossy. Inspect candidate process families with
get_process_groups() and
process_cluster_sweep(), then compare the reduced model
with a fine benchmark. A coarse balance may introduce a copperplate that
did not exist, and a cost declared once on a coarse cell does not shrink
when only some child regions are sampled. The scaling article develops this workflow and
its diagnostics.
Choosing a resolution
- Start coarse (
s4_hp3, 12 timeslices) while the model structure is in flux — solves are instant. - Move to an average day per season or month (
s4_h24, 96,m12_h24, 288) once VRE profiles or peak pricing matter and nothing carries state between timeslices. - With storage or ramp limits, intra-day dynamics need real hours: a
sample of the hourly calendar (
d365_h24_1dps, 96 hourly timeslices, ord365_h24_1dpm, 288). - Full-year hourly detail (
d365_h24, 8,760) is for final runs; check tractability withmodel_size()first, and iterate on a sample. - On a multi-year horizon, interpolate with
fold = TRUE. A weather series that is the same in every milestone year is stored once instead of once per year, which is usually the single largest saving in an hourly model — on a 4-regiond365_h24model over 7 milestones it cuts the interpolated data by ~84%. Folding only collapses values that are uniform across the whole dimension, so it never changes the solution; a model with a genuinely different weather year per milestone simply does not fold.
The TOPIA vignettes build one model and run it on these calendars interchangeably — resolution is a configuration choice, not a rewrite.
