diff --git a/tutorials/03-advanced/04-cached-processes/README.md b/tutorials/03-advanced/04-cached-processes/README.md new file mode 100644 index 00000000..f273cba3 --- /dev/null +++ b/tutorials/03-advanced/04-cached-processes/README.md @@ -0,0 +1,28 @@ +# Using cached processes + +When you run an inventory or impact assessment, the engine builds a system of equations that represents all processes and their dependencies. +This system is then solved in one pass to compute the results. + +For small models this is fine, but for large models the system can grow very large, making the solve more costly. + +The `@cached` annotation helps here: it tells the engine to **pre-solve a subsystem** (a process and its dependencies) during runtime. +The result of this local solve is then reused in the global assessment. +This makes the final system smaller and the overall computation faster while giving exactly the same results. + +## Try it yourself + +The file `main.lca` defines a `bake` process annotated with `@cached`. + +Run the following command once with the annotation, and once without: + +```bash +lcaac trace sandwich_factory +``` + +Compare the two results: + +Without `@cached`, you will see `flour_production` and `salt_production` listed as dependencies of `sandwich_factory`. + +With `@cached`, these dependencies disappear from the trace. They are pre-solved and collapsed into the `bake` process. + +The impacts remain the same in both cases. Only the solving pipeline changes. \ No newline at end of file diff --git a/tutorials/03-advanced/04-cached-processes/main.lca b/tutorials/03-advanced/04-cached-processes/main.lca new file mode 100644 index 00000000..483b5811 --- /dev/null +++ b/tutorials/03-advanced/04-cached-processes/main.lca @@ -0,0 +1,56 @@ +process sandwich_factory { + products { + 1 u sandwich + } + inputs { + 200 g bread + 50 g ham + } +} + +@cached +process bake { + products { + 1 kg bread + } + inputs { + 1 kg flour + 30 g salt + } +} + +process flour_production { + products { + 1 kg flour + } + impacts { + 2 kg_CO2_Eq GWP + } +} + +process salt_production { + products { + 1 g salt + } + impacts { + 0.05 kg_CO2_Eq GWP + } +} + +process ham_production { + products { + 1 g ham + } + impacts { + 0.7 kg_CO2_Eq GWP + } +} + +test main_with_mock_data { + given { + 1 u sandwich from sandwich_factory + } + assert { + GWP between 35.6 kg_CO2_Eq and 35.7 kg_CO2_Eq + } +} \ No newline at end of file diff --git a/tutorials/run.sh b/tutorials/run.sh index 2f5caf5a..d1704979 100755 --- a/tutorials/run.sh +++ b/tutorials/run.sh @@ -36,6 +36,8 @@ lcaac test -p $TUTORIALS_PATH/03-advanced/02-circular-footprint-formula lcaac test -p $TUTORIALS_PATH/03-advanced/03-project-file/lcaac.yaml main_with_data lcaac test -p $TUTORIALS_PATH/03-advanced/03-project-file/lcaac-mock.yaml main_with_mock_data +lcaac test -p $TUTORIALS_PATH/03-advanced/04-cached-processes + # Check custom dimensions tutorial set -euo # The following assessment is expected to fail.