Upstream trees

This method returns the result nodes of a given intervention flow for a given path in an upstream contribution tree. The path is a sequence of technosphere-flows that describe the path to the parent node of the returned nodes. If the path is empty, the root of the tree is returned. Note that such an upstream tree can be infinitely deep when the calculated system has cycles.

Rest APIPOST result/{result-id}/upstream-interventions-of/{envi-flow}
JSON-RPCresult/upstream-interventions-of
Snake caseresult.get_upstream_interventions_of
Camel caseresult.getUpstreamInterventionsOf
Return typeList[UpstreamNode]
Parameter 1EnviFlow
Parameter 2List[TechFlow]

Examples

olca-ipc.ts

The TypeScript example below uses the olca-ipc.ts client API:

async function main() {

  // connect to a local server running on port 8080
  const client = o.IpcClient.on(8080);
  const setup = o.CalculationSetup.of({
    target: o.Ref.of({
      id: "d3a9a9b2-ec3e-4811-8617-ae853573b50b",
      refType: o.RefType.ProductSystem,
    }),
  });
  const result = await client.calculate(setup);
  await result.untilReady();

  // select the first best inventory flow and expand a tree for it
  const flow = (await result.getEnviFlows())[0];
  console.log(`upstream tree for ${flow.flow?.name} (${flow.flow?.category})`);
  await expand(result, flow, []);

  // as always, dispose the result
  result.dispose();
}

async function expand(r: o.IpcResult, flow: o.EnviFlow, path: o.TechFlow[]) {
  const level = path.length;
  const indent = "  ".repeat(level);
  const unit = flow.flow?.refUnit;
  const nodes = await r.getUpstreamInterventionsOf(flow, path);
  for (const node of nodes) {
    if (node.result === 0) {
      continue;
    }
    const name = node.techFlow?.provider?.name;
    const value = node.result?.toExponential(2);
    console.log(`${indent}- ${value} ${unit} :: ${name}`);

    // we stop the expansion after 3 levels; you can set other cut-offs like
    // result contributions etc.
    if (level < 3) {
      const next = path.slice();
      next.push(node.techFlow!);
      await expand(r, flow, next);
    }
  }
}

main();

/*
upstream tree for COD, Chemical Oxygen Demand (Elementary flows/Emission to water/ground water)
- 1.73e-9 t :: ...
  - 1.73e-9 t :: ...
    - 7.21e-10 t :: ...
      - 3.45e-10 t :: ...
      - 2.04e-10 t :: ...
      - ...
    - 5.77e-10 t :: ...
      - 4.40e-10 t :: ...
      - 7.53e-11 t :: ...
      - 3.21e-11 t :: ...
      - ...
*/

olca-ipc.py

The Python example below uses the olca-ipc.py client API:

import olca_ipc as ipc
import olca_schema as o


def main():

    # calculate a result
    client = ipc.Client(8080)
    setup = o.CalculationSetup(
        target=o.Ref(
            id="d3a9a9b2-ec3e-4811-8617-ae853573b50b",
            ref_type=o.RefType.ProductSystem,
        )
    )
    result = client.calculate(setup)
    result.wait_until_ready()

    # select the first best inventory flow and expand a tree for it
    flow = result.get_envi_flows()[0]
    print(f"upstream tree for {flow.flow.name} ({flow.flow.category})")
    expand(result, flow, [])

    # as always, dispose the result
    result.dispose()


def expand(r: ipc.IpcResult, flow: o.EnviFlow, path: list[o.TechFlow]):
    level = len(path)
    indent = "  " * level
    unit = flow.flow.ref_unit
    nodes = r.get_upstream_interventions_of(flow, path)
    for node in nodes:
        if node.result == 0 or not node.tech_flow:
            continue
        name = node.tech_flow.provider.name
        value = node.result
        print(f"{indent}- {value:.2E} {unit} :: {name}")

        # we stop the expansion after 3 levels; you can set other cut-offs like
        # result contributions etc.
        if level < 3:
            expand(r, flow, path + [node.tech_flow])
    pass


if __name__ == "__main__":
    main()