Cache API¶
Content-addressed simulation result caching.
SimulationCache¶
idfkit.simulation.cache.SimulationCache
¶
Content-addressed simulation result cache.
Each entry is a directory named by the cache key containing a copy of
the simulation run directory, less the weather file (whose contents are
already part of the key), plus a _cache_meta.json manifest.
The cache grows with every new run unless it is bounded: pass max_bytes to evict the least recently used entries after each store, or call prune to trim it by size or age.
The key covers the model, the weather file, the run flags and the
contents of every other file the model names, so editing a staged
Schedule:File CSV (in output_dir or anywhere else) is a miss and
restoring it is a hit again. A model that runs a PythonPlugin:Instance
or an ExternalInterface in PtolemyServer or
FunctionalMockupUnitExport mode has inputs no key can cover, so
simulate and
async_simulate neither
look it up nor store it, and log a warning.
Source code in idfkit/simulation/cache.py
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 | |
cache_dir
property
¶
Root directory for cached simulation entries.
clear()
¶
compute_key(model, weather, *, expand_objects=True, annual=False, design_day=False, output_suffix='C', readvars=False, extra_args=None, search_dirs=())
¶
Compute a deterministic cache key for a simulation invocation.
The model is copied and normalised (Output:SQLite is ensured) so
that models differing only in the presence of that object produce the
same key.
Every file the model names for EnergyPlus to read contributes its
contents: Schedule:File, Schedule:File:Shading, the
Table:* external data files, Construction:WindowDataFile, the
ASHRAE 205 representation files and FMUs. An absolute path is read
as is; a relative path is looked up in search_dirs in order, as
EnergyPlus looks in its working directory and then next to its
executable. A file that cannot be found contributes a marker
instead, so its later appearance changes the key. A model that names
no such file gets the same key as it did before this was added.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
AnyIDFDocument
|
The EnergyPlus model. |
required |
weather
|
str | Path | None
|
Path to the weather file, or |
required |
expand_objects
|
bool
|
Whether ExpandObjects will run. |
True
|
annual
|
bool
|
Whether annual simulation is used. |
False
|
design_day
|
bool
|
Whether design-day-only simulation is used. |
False
|
output_suffix
|
Literal['C', 'L', 'D']
|
Output file naming suffix ( |
'C'
|
readvars
|
bool
|
Whether ReadVarsESO post-processing will run. |
False
|
extra_args
|
list[str] | tuple[str, ...] | None
|
Additional command-line arguments. |
None
|
search_dirs
|
Sequence[str | Path]
|
Directories a relative external file path resolves
against, in order. The runners pass the run directory
(when |
()
|
Returns:
| Type | Description |
|---|---|
CacheKey
|
Source code in idfkit/simulation/cache.py
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 | |
contains(key)
¶
get(key)
¶
Retrieve a cached simulation result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
CacheKey
|
Cache key from compute_key. |
required |
Returns:
| Type | Description |
|---|---|
SimulationResult | None
|
A SimulationResult if a cache hit exists, otherwise |
SimulationResult | None
|
|
SimulationResult | None
|
cache entry itself: treat that directory as read-only. |
SimulationResult | None
|
simulate copies a hit into its |
SimulationResult | None
|
|
Source code in idfkit/simulation/cache.py
prune(*, max_bytes=None, max_age=None)
¶
Evict entries to bound the cache's size and age.
Entries not used for longer than max_age are removed first; then, while the cache is larger than max_bytes, the least recently used entry is removed. An entry counts as used when it is stored and on every get hit.
A result whose run_dir is a cache entry (a hit read without an
output_dir) stops working when its entry is removed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_bytes
|
int | None
|
Size to bring the cache under, or |
None
|
max_age
|
float | None
|
Age in seconds since last use beyond which an entry is
removed, or |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The number of entries removed. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If max_bytes or max_age is negative. |
Source code in idfkit/simulation/cache.py
put(key, result)
¶
Store a successful simulation result in the cache.
Only results with success=True are cached. The run directory,
less any .epw weather file, is copied into the cache atomically.
With max_bytes set, the least recently used entries are then
evicted until the cache fits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
CacheKey
|
Cache key from compute_key. |
required |
result
|
SimulationResult
|
Successful simulation result to cache. |
required |
Source code in idfkit/simulation/cache.py
CacheKey¶
idfkit.simulation.cache.CacheKey
dataclass
¶
default_simulation_cache_dir¶
idfkit.simulation.cache.default_simulation_cache_dir()
¶
Return the platform-appropriate cache directory for simulation results.