-
Notifications
You must be signed in to change notification settings - Fork 0
/
calculate.ts
55 lines (48 loc) · 1.6 KB
/
calculate.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import * as o from "../../mod.ts";
async function main() {
// we can use a client for the REST or IPC (JSON-RPC) protocol
// both clients implement the same interface
const protocol: "REST" | "IPC" = "IPC";
const client = protocol === "IPC"
? o.IpcClient.on(8080)
: o.RestClient.on(8080);
// select a product system
const systems = await client.getDescriptors(o.RefType.ProductSystem);
if (systems.length === 0) {
console.log("error: no product systems found");
return;
}
const system = systems[0];
console.log(`calculate system: ${system.name}`);
// select an impact assessment method, if available
const methods = await client.getDescriptors(o.RefType.ImpactMethod);
const method = methods.length >= 0 ? methods[0] : null;
if (!method) {
console.log(" no LCIA method available");
} else {
console.log(` using LCIA method: ${method.name}`);
}
// calculate the system
console.log(" ... calculating ...");
const setup = o.CalculationSetup.of({
target: system,
impactMethod: method,
});
const result = await client.calculate(setup);
const state = await result.untilReady();
if (state.error) {
throw new Error(`calculation failed: ${state.error}`);
}
console.log(" done");
// query the result
const impacts = await result.getTotalImpacts();
console.log("LCIA Results:");
for (const impact of impacts) {
const name = impact.impactCategory?.name;
const unit = impact.impactCategory?.refUnit;
console.log(` ${name}: ${impact.amount?.toExponential(2)} ${unit}`);
}
// finally, dispose the result
result.dispose();
}
main();