use of org.openlca.core.matrix.ProductSystemBuilder in project olca-modules by GreenDelta.
the class ModelHandler method createProductSystem.
@Rpc("create/product_system")
public RpcResponse createProductSystem(RpcRequest req) {
if (req.params == null || !req.params.isJsonObject())
return Responses.invalidParams("params must be an object with valid processId", req);
var obj = req.params.getAsJsonObject();
if (!obj.has("processId") || !obj.get("processId").isJsonPrimitive())
return Responses.invalidParams("params must be an object with valid processId", req);
var processId = obj.get("processId").getAsString();
if (Strings.nullOrEmpty(processId))
return Responses.invalidParams("params must be an object with valid processId", req);
var refProcess = new ProcessDao(db).getForRefId(processId);
if (refProcess == null)
return Responses.invalidParams("No process found for ref id " + processId, req);
var system = ProductSystem.of(refProcess);
system = new ProductSystemDao(db).insert(system);
var config = new LinkingConfig().preferredType(ProcessType.UNIT_PROCESS);
if (obj.has("preferredType") && obj.get("preferredType").getAsString().equalsIgnoreCase("lci_result")) {
config.preferredType(ProcessType.LCI_RESULT);
}
config.providerLinking(ProviderLinking.PREFER_DEFAULTS);
if (obj.has("providerLinking")) {
if (obj.get("providerLinking").getAsString().equalsIgnoreCase("ignore")) {
config.providerLinking(ProviderLinking.IGNORE_DEFAULTS);
} else if (obj.get("providerLinking").getAsString().equalsIgnoreCase("only")) {
config.providerLinking(ProviderLinking.ONLY_DEFAULTS);
}
}
var builder = new ProductSystemBuilder(MatrixCache.createLazy(db), config);
builder.autoComplete(system);
system = ProductSystemBuilder.update(db, system);
var res = new JsonObject();
res.addProperty("@id", system.refId);
return Responses.ok(res, req);
}
use of org.openlca.core.matrix.ProductSystemBuilder in project olca-modules by GreenDelta.
the class ProductSystemBuilderExample method main.
public static void main(String[] args) {
String dbPath = "C:/Users/Besitzer/openLCA-data-1.4/databases/ecoinvent_2_2_unit";
IDatabase db = new Derby(new File(dbPath));
// load the reference process of the new product system
Process p = new ProcessDao(db).getForRefId("81261285-cc4a-3588-8cce-3aabb786d7aa");
// create and auto-complete the product system
var config = new LinkingConfig().providerLinking(ProviderLinking.PREFER_DEFAULTS).preferredType(ProcessType.UNIT_PROCESS);
var system = new ProductSystemBuilder(MatrixCache.createLazy(db), config).build(p);
// save the product system
new ProductSystemDao(db).insert(system);
}
use of org.openlca.core.matrix.ProductSystemBuilder in project olca-modules by GreenDelta.
the class ProductSystemInMemoryCalculationExample method main.
public static void main(String[] args) throws Exception {
// load the database and matrix cache
String workspace = "C:/Users/Besitzer/openLCA-data-1.4";
String dbPath = workspace + "/databases/ecoinvent_2_2_unit";
IDatabase db = new Derby(new File(dbPath));
MatrixCache mcache = MatrixCache.createLazy(db);
// load the reference process and create
// the product system with auto-completion
// the system is not saved in the database
Process p = new ProcessDao(db).getForRefId("7ff672e3-a296-30e8-b1bb-a3173711a28b");
var config = new LinkingConfig().providerLinking(ProviderLinking.PREFER_DEFAULTS).preferredType(ProcessType.UNIT_PROCESS);
var builder = new ProductSystemBuilder(mcache, config);
var system = builder.build(p);
var method = db.get(ImpactMethod.class, "207ffac9-aaa8-401d-ac90-874defd3751a");
// create the calculation setup
var setup = CalculationSetup.simple(system).withImpactMethod(method);
// load the native library and calculate the result
// TODO: load Julia libraries first here
SystemCalculator calc = new SystemCalculator(db);
SimpleResult r = calc.calculateSimple(setup);
// print the LCIA results
for (ImpactDescriptor impact : r.getImpacts()) {
System.out.println(impact.name + "\t" + r.getTotalImpactResult(impact) + "\t" + impact.referenceUnit);
}
// finally, close the database
db.close();
}
use of org.openlca.core.matrix.ProductSystemBuilder in project olca-modules by GreenDelta.
the class SubResultsTest method testSubResults.
/**
* Calculate a simple sub-system model:
* <pre>
* {@code
* A [1] -> [2] B [0.5] -> [2] C [1]
*
* expected total CO2 result:
* C: 1 * 1 kg CO2
* B: 4 * 1 kg CO2
* A: 8 * 1 kg CO2
* ----------------
* 13 kg CO2
* }
* </pre>
*/
@Test
public void testSubResults() {
var units = UnitGroup.of("Mass units", "kg");
var mass = FlowProperty.of("Mass", units);
var co2 = Flow.elementary("CO2", mass);
var prodA = Flow.product("A", mass);
var prodB = Flow.product("B", mass);
var prodC = Flow.product("C", mass);
// A [1] -> [2] B [0.5] -> [2] C [1]
var procA = Process.of("A", prodA);
var procB = Process.of("B", prodB);
procB.quantitativeReference.amount = 0.5;
procB.input(prodA, 2);
var procC = Process.of("C", prodC);
procC.input(prodB, 2);
List.of(procA, procB, procC).forEach(p -> {
p.output(co2, 1);
p.tags = p.name;
});
db.insert(units, mass, co2, prodA, prodB, prodC, procA, procB, procC);
var systems = Stream.of(procA, procB, procC).map(p -> {
var linker = new SubSystemLinker(db);
var system = new ProductSystemBuilder(linker).build(p);
return db.insert(system);
}).toList();
var sysA = systems.get(0);
var sysB = systems.get(1);
var sysC = systems.get(2);
var setup = CalculationSetup.fullAnalysis(sysC);
var resultC = new SystemCalculator(db).calculateFull(setup);
var co2Idx = resultC.enviIndex().at(0);
// check the total CO2 result: 13 kg
assertEquals(13, resultC.getTotalFlowResult(co2Idx), 1e-10);
// check sub-result B
var techFlowB = TechFlow.of(sysB);
var resultB = (FullResult) resultC.subResultOf(techFlowB);
assertEquals(3, resultB.getTotalFlowResult(co2Idx), 1e-10);
assertEquals(4, resultC.getScalingFactor(techFlowB), 1e-10);
// check sub-result A
var techFlowA = TechFlow.of(sysA);
var resultA = (FullResult) resultB.subResultOf(techFlowA);
assertEquals(1, resultA.getTotalFlowResult(co2Idx), 1e-10);
assertEquals(2, resultB.getScalingFactor(techFlowA), 1e-10);
// scale a sub-result to get the full result
assertEquals(13, 1 + resultC.getScalingFactor(techFlowB) * resultB.getTotalFlowResult(co2Idx), 1e-10);
// test TagResult
var tagResults = TagResult.allOf(resultC);
var expectedTagResults = List.of(Pair.of("A", 8.0), Pair.of("B", 4.0), Pair.of("C", 1.0));
for (var expected : expectedTagResults) {
var tagResult = tagResults.stream().filter(r -> r.tag().equals(expected.first)).findFirst().orElseThrow();
assertEquals(expected.second, tagResult.inventoryResultOf(co2Idx).value(), 1e-10);
}
db.delete(sysC, sysB, sysA, procA, procB, procC, prodA, prodB, prodC, co2, mass, units);
}
Aggregations